##################################
#                                #
# Last modified 2019/05/17       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
import math
	
def run():

    if len(sys.argv) < 2:
        print 'usage: python %s config outprefix' % sys.argv[0]
        print '\tconfig format:'
        print '\t\t#label\tplus_bigwig\tminus_bigwig\tpeaks.bed3\tfasta'
        print '\tfiles for each label will be put together as different tasks'
        sys.exit(1)

    input = sys.argv[1]
    outprefix = sys.argv[2]

    ConfigDict = {}

    linelist = open(input)
    for line in linelist:
        fields = line.strip().split('\t')
        label = fields[0]
        if ConfigDict.has_key(label):
            pass
        else:
            ConfigDict[label] = []
        ConfigDict[label].append((fields[1],fields[2],fields[3],fields[4]))

    for label in ConfigDict.keys():
        outfile = open(outprefix + '.' + label + '-dataspec.yaml','w')
        fasta = ConfigDict[label][0][3]
        outline = 'fasta_file: ' + fasta
        outfile.write(outline + '\n')
        outfile.write('task_specs:\n')
        i=0
        for (P,M,peaks,fasta) in ConfigDict[label]:
            i+=1
            outline = '  task' + str(i) + ':'
            outfile.write(outline + '\n')
            outline = '    pos_counts: ' + P
            outfile.write(outline + '\n')
            outline = '    neg_counts: ' + M
            outfile.write(outline + '\n')
            outline = '    peaks: ' + peaks
            outfile.write(outline + '\n')
            outfile.write('    # optional' + '\n')
            outfile.write('    ignore_strand: False' + '\n')
            outfile.write('    bias_bigwig:' + '\n')
            outfile.write('    bias_model: null' + '\n')
        outfile.close()
    
run()
