<s> from collections import namedtuple <EOL> FastaRecord = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:name>' , '<STR_LIT>' ] ) <EOL> FastqRecord = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def seqlen ( record ) : <EOL> '''<STR_LIT>''' <EOL> return len ( record . seq ) <EOL> def fastaIterator ( fh ) : <EOL> l = fh . readline ( ) <EOL> if ( not l or not l . startswith ( "<STR_LIT:>>" ) ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> name = l . strip ( ) [ <NUM_LIT:1> : ] <EOL> seq = "<STR_LIT>" <EOL> while True : <EOL> l = fh . readline ( ) <EOL> if not l or l . startswith ( "<STR_LIT:>>" ) : <EOL> yield FastaRecord ( name , seq ) <EOL> if not l : <EOL> break <EOL> name = l . strip ( ) [ <NUM_LIT:1> : ] <EOL> seq = "<STR_LIT>" <EOL> else : <EOL> seq += l . strip ( ) <EOL> def recordToString ( record ) : <EOL> f = fastqRecordToString if hasattr ( record , "<STR_LIT>" ) else fastaRecordToString <EOL> return f ( record ) <EOL> def fastaRecordToString ( record ) : <EOL> return "<STR_LIT:\n>" . join ( [ "<STR_LIT:>>" + record . name , record . seq ] ) <EOL> def fastqIterator ( fh ) : <EOL> l = fh . readline ( ) <EOL> if ( not l or not l . startswith ( "<STR_LIT:@>" ) ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> name = l . strip ( ) [ <NUM_LIT:1> : ] <EOL> while True : <EOL> nxt = [ fh . readline ( ) for _ in range ( <NUM_LIT:3> ) ] <EOL> if ( not all ( nxt ) ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> yield FastqRecord ( name , nxt [ <NUM_LIT:0> ] . strip ( ) , nxt [ <NUM_LIT:1> ] [ <NUM_LIT:1> : ] . strip ( ) , nxt [ <NUM_LIT:2> ] . strip ( ) ) <EOL> l = fh . readline ( ) <EOL> if not l : <EOL> break <EOL> name = l . strip ( ) [ <NUM_LIT:1> : ] <EOL> def fastqRecordToString ( record ) : <EOL> return "<STR_LIT:\n>" . join ( [ "<STR_LIT:@>" + record . name , record . seq , "<STR_LIT:+>" + record . desc , record . qual ] ) <EOL> def iteratorFromExtension ( filename ) : <EOL> '''<STR_LIT>''' <EOL> ext = filename . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:1> ] <EOL> if ext in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> return fastaIterator <EOL> elif ext in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> return fastqIterator <EOL> raise Exception , "<STR_LIT>" % ( ext , filename ) </s>
