##################################
#                                #
# Last modified 2019/08/23       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string

def run():

    if len(sys.argv) < 7:
        print 'usage: python %s motif_list fieldID seq.fa fimo.tsv fieldID (0),number1,number2,number3...,numberN outputfilename' % sys.argv[0]
        sys.exit(1)

    MotListFile = sys.argv[1]
    FID = int(sys.argv[2])
    mFID = int(sys.argv[5])

    MotifListDict = {}

    linelist = open(MotListFile)
    for line in linelist:
        if line.startswith('#'):
            continue
        fields = line.strip().split('\t')
        motif = fields[FID]
        MotifListDict[motif] = 1

    fasta = sys.argv[3]
    fimo = sys.argv[4]
    outfilename = sys.argv[7]

    bins = sys.argv[6].split(',')
    binList = []
    binDict = {}
    for bin in bins:
        binList.append(float(bin))
        binDict[float(bin)] = 1
    if binDict.has_key(0):
        pass
    else:
        binDict[0] = 1
        binList.append(0)
    binList.sort()
    print binList

    MotifDict = {}

    inputdatafile = open(fasta)
    for line in inputdatafile:
        if line[0]=='>':
            chr = line.strip().split('>')[1]
            if MotifDict.has_key(chr):
                pass
            else:
                MotifDict[chr] = {}

    linelist = open(fimo)
    for line in linelist:
        if line.startswith('motif_id\t'):
            continue
        if line.startswith('#'):
            continue
        fields = line.strip().split('\t')
        if line.strip() == '':
            continue
        motif = fields[mFID]
        chr = fields[2]
        left = int(fields[3])
        right = int(fields[4])
        if MotifDict[chr].has_key(motif):
            pass
        else:
            MotifDict[chr][motif] = {}
        MotifDict[chr][motif][(left,right)] = 1

    MotifCountsDict = {}
    for motif in MotifListDict.keys():
        MotifCountsDict[motif] = {}
        for chr in MotifDict.keys():
            if MotifDict[chr].has_key(motif):
                N = len(MotifDict[chr][motif])
            else:
                N = 0
            if binDict.has_key(N):
                bin = N
            else:
                if N > max(binList):
                    bin = max(binList)
                elif N < min(binList):
                    bin = min(binList)
                else:
                    for B in binList:
                        if N > B:
                            bin = B
                            break
            if MotifCountsDict[motif].has_key(bin):
                pass
            else:
                MotifCountsDict[motif][bin] = 0
            MotifCountsDict[motif][bin] += 1
            
    outfile = open(outfilename, 'w')

    outline = '#motif'
    for bin in binList:
        outline = outline + '\t' + str(bin)
    outfile.write(outline + '\n')

    for motif in MotifCountsDict.keys():
        outline = motif
        for bin in binList:
            if MotifCountsDict[motif].has_key(bin):
                outline = outline + '\t' + str(MotifCountsDict[motif][bin])
            else:
                outline = outline + '\t' + str(0)
        outfile.write(outline + '\n')

    outfile.close()
   
run()
