##################################
#                                #
# Last modified 02/23/2009       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys

try:
	import psyco
	psyco.full()
except:
	pass

def run():

    if len(sys.argv) < 2:
        print 'usage: python %s inputfilename -f | -q' % sys.argv[0]
        sys.exit(1)

    inputfilename = sys.argv[1]
    type = sys.argv[2]
    end=inputfilename.split('.')[-1]
    outputfilename1 = inputfilename.split('.')[0] + '.end1.' +end
    outputfilename2 = inputfilename.split('.')[0] + '.end2.' +end

    outfile1 = open(outputfilename1, 'w')
    outfile2 = open(outputfilename2, 'w')

    input_stream = open(inputfilename)
    i=0 
    if type=='-f':
        for line in input_stream:
            i+=1
            if i % 2000000 == 0:
                print i/2, 'reads processed' 
            if line[0]=='>':
                current_header=line
                continue
            else:
                if current_header.endswith('/1\n'):
                    outfile1.write(current_header)
                    outfile1.write(line)
                if current_header.endswith('/2\n'):
                    outfile2.write(current_header)
                    outfile2.write(line)
    outfile1.close()
    outfile2.close()

run()

