##################################
#                                #
# Last modified 02/09/2015       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys

def run():

    if len(sys.argv) < 2:
        print 'usage: python %s fastq output' % sys.argv[0]
        print '\tuse - for stdin'
        sys.exit(1)

    fastq = sys.argv[1]

    i=0
    BP=0
    pos=1
    if fastq == '-':
        input_stream = sys.stdin
    else:
        input_stream = open(fastq)
    for line in input_stream:
        i+=1
        if i % 20000000 == 0:
            print str(i/4000000) + 'M reads processed'
        if pos==1 and line.startswith('@'):
            pos=2
            continue
        if pos==1 and line[0] != '@':
            print 'fastq broken'
            sys.exit(1)
        if pos==2:
            BP += len(line.strip())
            pos=3
            continue
        if pos==3:
            pos=4
            continue
        if pos==4:
            pos=1
            continue

    outfile = open(sys.argv[2],'w')
    outfile.write('total base pairs in fastq file:\t' + str(BP) + '\n')
    outfile.close()

run()

