##################################
#                                #
# Last modified 2018/03/26       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
from sets import Set


def run():

    if len(sys.argv) < 3:
        print 'usage: python %s <list of files filename> <fieldID> outputfilename ' % sys.argv[0]
        print 'one file name per line in input file' 
        sys.exit(1)
    
    cachePages = 2000000

    input = sys.argv[1]
    fieldID = int(sys.argv[2])
    outfilename = sys.argv[3]

    outfile = open(outfilename, 'w')
    outfile.write('#file\tNum_peaks\tRPM_Sum\n')

    lines = open(input)
    for filename in lines:
        file=filename.strip()
        lineslist=open(file)
#        label=file.strip().split('/')[-1]
        label=file.strip()
        totalscore=0
        print label
        i=0
        for line in lineslist:
            i+=1
            if line.startswith('#'):
                continue
            fields=line.strip().split('\t')
            score=float(fields[fieldID])
            totalscore+=score
        outfile.write(label+'\t'+str(i)+'\t'+str(totalscore)+'\n')
    
    outfile.close()
            
run()
