##################################
#                                #
# Last modified 05/18/2012       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
from sets import Set

def run():

    if len(sys.argv) < 2:
        print 'usage: python %s fasta out_prefix' % sys.argv[0]
        print '   Note: this script relies on read IDs ending on /1 and /2'
        sys.exit(1)

    fasta = sys.argv[1]
    outend1 = open(sys.argv[2]+'.end1.fasta','w')
    outend2 = open(sys.argv[2]+'.end2.fasta','w')

    i=0
    input_stream = open(fasta)
    for line in input_stream:
        i+=1
        if i % 20000000 == 0:
            print str(i/2000000) + 'M reads processed'
        if line.startswith('>'):
            File=0
            readID = line.strip()
            if readID.endswith('/1'):
                File=1
                outend1.write(line)
            if readID.endswith('/2'):
                File=2
                outend2.write(line)
            continue
        else:
            if File == 1:
                outend1.write(line)
            if File == 2:
                outend2.write(line)

    outend1.close()
    outend2.close()

run()

