##################################
#                                #
# Last modified 2024/11/08       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
import scipy.stats
import numpy
import math
import random
from sets import Set
import time

def run():

    if len(sys.argv) < 5:
        print 'usage: python %s coverage1,coverage2,...,coverageN window readlength rounds_simulation outfile' % sys.argv[0]
        sys.exit(1)

    covs = sys.argv[1].split(',')
    CovLevels = []
    for C in covs:
        CovLevels.append(int(C))
    W = int(sys.argv[2])
    RL = int(sys.argv[3])
    N = int(sys.argv[4])
    outfilename = sys.argv[5]

    OutputDict = {}
    for n in range(N):
        print n, 
        OutputDict[n] = {}
        for C in CovLevels:
            print C, 
            OutputDict[n][C] = 0
            L = (C+0.0)/RL
            endCov = {}
            for i in range(2*W):
                endCov[i] = numpy.random.poisson(L)
            Wpos1 = int(W/2)
            Wpos2 = 2*W - int(W/2)
            for i in range(2*W):
                if i <= Wpos1 and i+RL >= Wpos1:
                    OutputDict[n][C] += endCov[i]
        print ''
 
    outfile = open(outfilename, 'w')

    outline = '#CovLevel'
    for n in range(N):
        outline += '\t' + str(n)
    outfile.write(outline + '\n')

    for C in CovLevels:
        outline = str(C)
        for n in range(N):
            outline += '\t'
            outline += str(OutputDict[n][C])
        outfile.write(outline + '\n')

    outfile.close()

run()