#!/usr/bin/env python
# Time-stamp: <2012-02-22 16:41:27 Tao Liu>

"""Module Description

Copyright (c) 2012 Tao Liu <taoliu@jimmy.harvard.edu>

This code is free software; you can redistribute it and/or modify it
under the terms of the BSD License (see the file COPYING included with
the distribution).

@status:  experimental
@version: $Revision$
@author:  Tao Liu
@contact: taoliu@jimmy.harvard.edu
"""

# ------------------------------------
# python modules
# ------------------------------------

import os
import sys
from array import array as pyarray
from optparse import OptionParser
from random import gammavariate as rgamma
from math import log

# ------------------------------------
# constants
# ------------------------------------

# ------------------------------------
# Misc functions
# ------------------------------------

# ------------------------------------
# Classes
# ------------------------------------

# ------------------------------------
# Main function
# ------------------------------------
def main():
    usage = "usage: %prog [options]"
    description = "Calculate ratios between two Posterior Poisson with a given probability cutoff c."
    
    optparser = OptionParser(version="%prog 0.1",description=description,usage=usage,add_help_option=False)
    optparser.add_option("-h","--help",action="help",help="Show this help message and exit.")
    optparser.add_option("-m","--min",dest="mincount",type="int",
                         help="Minimum of pileup height. >=0. Default: 0", default=0)
    optparser.add_option("-M","--max",dest="maxcount",type="int",
                         help="Maximum of pileup height. <=10000. Default: 100", default=100)
    optparser.add_option("-N","--samplesize",dest="samplesize",type="int",
                         help="Sampling size for Gamma distribution. First 500 will be burned out. So must > 500. Default: 100000", default=100000)
    optparser.add_option("-c","--cutoff",dest="cutoff",type="float",
                         help="Cutoff, must be <1. Default: 0.01", default=0.01)
    optparser.add_option("-o","--ofile",dest="ofile",
                         help="output file") 
    (options,args) = optparser.parse_args()
    if options.mincount < 0 or options.maxcount > 10000 or options.samplesize <= 500 or options.cutoff>=1 or not options.ofile:
        optparser.print_help()
        sys.exit(1)

    ofhd = file(options.ofile,"w")

    sample_number = options.samplesize
    cutoff = options.cutoff

    gfold = pyarray('f',[])

    for i in xrange(options.mincount,options.maxcount+1):
        for j in xrange(options.mincount,options.maxcount+1):        
            P_X = MLEPoissonPosteriorRatio(sample_number,500,i,j)
            P_X = sorted(P_X)
            c = int(sample_number * cutoff)
            if i > j:
                # X >= 0
                ret = max(0,P_X[c])
            else:
                # X < 0
                ret = min(0,P_X[-1*c])        
            gfold.append(ret)

    gfold.tofile(ofhd)
    ofhd.close()

def MLEPoissonPosteriorRatio (sample_number, burn, count1, count2):
    """MLE method to calculate ratio distribution of two Posterior Poisson distributions.

    MLE of Posterior Poisson is Gamma(k+1,1) if there is only one observation k.
    * 1 is a pseudo-count to avoid zeros.

    sample_number: number of sampling. It must be greater than burn, however there is no check.
    burn: number of samples being burned.
    count1: observed counts of condition 1
    count2: observed counts of condition 2

    return: list of log2-ratios
    """
    ratios = pyarray('f',[])
    ra = ratios.append
    for i in xrange(sample_number):
        x1 = rgamma(count1+1,1)
        x2 = rgamma(count2+1,1)
        ra( log(x1,2) - log(x2,2) )
    return ratios[int(burn):]


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        sys.stderr.write("User interrupt me! ;-) See you!\n")
        sys.exit(0)
