#NEWLINE# def write_random_records( fqa , fqb , outfa , outfb , N ) : #NEWLINE# #INDENT# ' get N random headers from a fastq file without reading the\n    whole thing into memory' #NEWLINE# records = ( sum ( ( 1 for _ in gzip . open ( fqa ) ) ) / 4 ) #NEWLINE# rand_records = sorted ( [ random . randint ( 0 , ( records - 1 ) ) for _ in xrange ( N ) ] ) #NEWLINE# ( suba , subb ) = ( gzip . open ( outfa , 'w' ) , gzip . open ( outfb , 'w' ) ) #NEWLINE# ( fha , fhb ) = ( gzip . open ( fqa ) , gzip . open ( fqb ) ) #NEWLINE# rec_no = ( - 1 ) #NEWLINE# for rr in rand_records : #NEWLINE# #INDENT# while ( fhb < rr ) : #NEWLINE# #INDENT# rec_no += 1 #NEWLINE# for i in range ( 4 ) : #NEWLINE# #INDENT# fha . readline ( ) #NEWLINE# #UNINDENT# for i in range ( 4 ) : #NEWLINE# #INDENT# fhb . readline ( ) #NEWLINE# #UNINDENT# #UNINDENT# for i in range ( 4 ) : #NEWLINE# #INDENT# suba . write ( fha . readline ( ) ) #NEWLINE# subb . write ( fhb . readline ( ) ) #NEWLINE# #UNINDENT# rec_no += 1 #NEWLINE# #UNINDENT# ( ( print >> sys . stderr ) , ( 'wrote to %s, %s' % ( suba . name , subb . name ) ) )