##################################
#                                #
# Last modified 06/22/2010       # 
#                                #
# 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]
        print label
        for line in lineslist:
            if line.startswith('#stats:'):
                RPM=line.strip().split(' RPM in ')[0].split('\t')[-1]
                regions=line.strip().split(' RPM in ')[1].split(' ')[0]
        outfile.write(label+'\t'+str(regions)+'\t'+str(RPM)+'\n')
    
    outfile.close()
            
run()
