##################################
#                                #
# Last modified 2023/09/13       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string

def run():

    if len(sys.argv) < 2:
        print 'usage: python %s RCADE outfile' % sys.argv[0]
        sys.exit(1)

    RCADE = sys.argv[1]
    outfilename = sys.argv[2]

    PFM = []
    M = ''

    linelist = open(RCADE)
    InMotif = False
    outline = ''
    for line in linelist:
        if line.startswith('TF\t'):
            continue
        if line.startswith('Gene\t'):
            fields = line.strip().split('\t')
            motif = fields[1]
            continue
        if line.startswith('Motif\t'):
            continue
        if line.startswith('Family\t'):
            continue
        if line.startswith('Species\t'):
            continue
        if line.strip() == '':
            continue
        if line.startswith('Pos\t'):
            InMotif = True
            continue
        if InMotif:
            fields = line.strip().split('\t')
            (a,c,g,t) = (float(fields[1]),float(fields[2]),float(fields[3]),float(fields[4]))
            if a >= 0.60:
                M += 'A'
            elif c >= 0.60:
                M += 'C'
            elif g >= 0.60:
                M += 'G'
            elif t >= 0.60:
                M += 'T'
            elif a >= 0.30 and c >= 0.30 and g >= 0.30:
                M += 'V'
            elif a >= 0.30 and c >= 0.30 and t >= 0.30:
                M += 'H'
            elif a >= 0.30 and g >= 0.30 and t >= 0.30:
                M += 'D'
            elif c >= 0.30 and g >= 0.30 and t >= 0.30:
                M += 'B'
            elif a >= 0.35 and c >= 0.35:
                M += 'M'
            elif g >= 0.35 and t >= 0.35:
                M += 'K'
            elif a >= 0.35 and t >= 0.35:
                M += 'W'
            elif g >= 0.35 and c >= 0.35:
                M += 'S'
            elif c >= 0.35 and t >= 0.35:
                M += 'Y'
            elif a >= 0.35 and g >= 0.35:
                M += 'R'
            else:
                M += 'N'
            PFM.append((a,c,g,t))

    outfile = open(outfilename,'w')

#    outline = 'tagid' + '\t' + motif
#    outfile.write(outline + '\n')
#    outline = 'info'
#    outfile.write(outline + '\n')
#    outline = 'threshold'
#    outfile.write(outline + '\n')
#    outline = 'motif\t' + M
#    outfile.write(outline + '\n')
#    for (a,c,g,t) in PFM:
#        outline = 'acgt' + '\t' + str(a) + '\t' + str(c) + '\t' + str(g) + '\t' + str(t)
#        outfile.write(outline + '\n')

    outline = 'MEME version 4'
    outfile.write(outline + '\n')
    outfile.write('\n')
    outline = 'ALPHABET= ACGT'
    outfile.write(outline + '\n')
    outfile.write('\n')
    outline = 'strands: + -'
    outfile.write(outline + '\n')
    outfile.write('\n')
    outline = 'Background letter frequencies (from uniform background):'
    outfile.write(outline + '\n')
    outline = 'A 0.25000 C 0.25000 G 0.25000 T 0.25000 '
    outfile.write(outline + '\n')
    outfile.write('\n')
    outline = 'MOTIF ' + motif
    outfile.write(outline + '\n')
    outfile.write('\n')
    outline = 'letter-probability matrix: alength= 4 w= ' + str(len(PFM)) + ' nsites= 1 E= 0'
    outfile.write(outline + '\n')
    for (a,c,g,t) in PFM:
        outline = '  ' + str(a) + '	  ' + str(c) + '	  ' + str(g) + '	  ' + str(t)
        outfile.write(outline + '\n')

    outfile.close()

run()

