<s> DNA_SEQTYPE = <NUM_LIT:0> <EOL> RNA_SEQTYPE = <NUM_LIT:1> <EOL> PROTEIN_SEQTYPE = <NUM_LIT:2> <EOL> def guess_seqtype ( s ) : <EOL> dna_letters = '<STR_LIT>' <EOL> ndna = <NUM_LIT:0> <EOL> nU = <NUM_LIT:0> <EOL> nT = <NUM_LIT:0> <EOL> for l in s : <EOL> if l in dna_letters : <EOL> ndna += <NUM_LIT:1> <EOL> if l == '<STR_LIT>' or l == '<STR_LIT:u>' : <EOL> nU += <NUM_LIT:1> <EOL> elif l == '<STR_LIT:T>' or l == '<STR_LIT:t>' : <EOL> nT += <NUM_LIT:1> <EOL> ratio = ndna / float ( len ( s ) ) <EOL> if ratio > <NUM_LIT> : <EOL> if nT > nU : <EOL> return DNA_SEQTYPE <EOL> else : <EOL> return RNA_SEQTYPE <EOL> else : <EOL> return PROTEIN_SEQTYPE <EOL> seq_id_counter = <NUM_LIT:0> <EOL> def new_seq_id ( ) : <EOL> global seq_id_counter <EOL> seq_id_counter += <NUM_LIT:1> <EOL> return str ( seq_id_counter - <NUM_LIT:1> ) <EOL> def write_fasta ( ofile , s , chunk = <NUM_LIT> , id = None , reformatter = None ) : <EOL> "<STR_LIT>" <EOL> if id is None : <EOL> try : <EOL> id = str ( s . id ) <EOL> except AttributeError : <EOL> id = new_seq_id ( ) <EOL> ofile . write ( '<STR_LIT:>>' + id + '<STR_LIT:\n>' ) <EOL> seq = str ( s ) <EOL> if reformatter is not None : <EOL> seq = reformatter ( seq ) <EOL> end = len ( seq ) <EOL> pos = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> ofile . write ( seq [ pos : pos + chunk ] + '<STR_LIT:\n>' ) <EOL> pos += chunk <EOL> if pos >= end : <EOL> break <EOL> return id <EOL> def read_fasta ( ifile ) : <EOL> "<STR_LIT>" <EOL> id = None <EOL> isEmpty = True <EOL> for line in ifile : <EOL> if '<STR_LIT:>>' == line [ <NUM_LIT:0> ] : <EOL> if id is not None and len ( seq ) > <NUM_LIT:0> : <EOL> yield id , title , seq <EOL> isEmpty = False <EOL> id = line [ <NUM_LIT:1> : ] . split ( ) [ <NUM_LIT:0> ] <EOL> title = line [ len ( id ) + <NUM_LIT:2> : ] <EOL> seq = '<STR_LIT>' <EOL> elif id is not None : <EOL> for word in line . split ( ) : <EOL> seq += word <EOL> if id is not None and len ( seq ) > <NUM_LIT:0> : <EOL> yield id , title , seq <EOL> elif isEmpty : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> def read_fasta_one_line ( ifile ) : <EOL> "<STR_LIT>" <EOL> id = None <EOL> seq = '<STR_LIT>' <EOL> while True : <EOL> line = ifile . readline ( <NUM_LIT> ) <EOL> if line == '<STR_LIT>' : <EOL> break <EOL> elif '<STR_LIT:>>' == line [ <NUM_LIT:0> ] : <EOL> id = line [ <NUM_LIT:1> : ] . split ( ) [ <NUM_LIT:0> ] <EOL> title = line [ len ( id ) + <NUM_LIT:2> : ] <EOL> elif id is not None : <EOL> for word in line . split ( ) : <EOL> seq += word <EOL> if len ( seq ) > <NUM_LIT:0> : <EOL> return id , title , seq <EOL> raise IOError ( '<STR_LIT>' ) <EOL> def read_fasta_lengths ( ifile ) : <EOL> "<STR_LIT>" <EOL> id = None <EOL> seqLength = <NUM_LIT:0> <EOL> isEmpty = True <EOL> for line in ifile : <EOL> if '<STR_LIT:>>' == line [ <NUM_LIT:0> ] : <EOL> if id is not None and seqLength > <NUM_LIT:0> : <EOL> yield id , seqLength <EOL> isEmpty = False <EOL> id = line [ <NUM_LIT:1> : ] . split ( ) [ <NUM_LIT:0> ] <EOL> seqLength = <NUM_LIT:0> <EOL> elif id is not None : <EOL> for word in line . split ( ) : <EOL> seqLength += len ( word ) <EOL> if id is not None and seqLength > <NUM_LIT:0> : <EOL> yield id , seqLength <EOL> elif isEmpty : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> class AATranslation ( object ) : <EOL> '<STR_LIT>' <EOL> geneticCode = dict ( TTY = '<STR_LIT:F>' , TTR = '<STR_LIT:L>' , TCN = '<STR_LIT:S>' , TAY = '<STR_LIT:Y>' , TGY = '<STR_LIT:C>' , TGG = '<STR_LIT>' , <EOL> CTN = '<STR_LIT:L>' , CCN = '<STR_LIT:P>' , CAY = '<STR_LIT:H>' , CAR = '<STR_LIT>' , CGN = '<STR_LIT:R>' , <EOL> ATY = '<STR_LIT:I>' , ATA = '<STR_LIT:I>' , ATG = '<STR_LIT:M>' , ACN = '<STR_LIT:T>' , AAY = '<STR_LIT:N>' , AAR = '<STR_LIT>' , <EOL> AGY = '<STR_LIT:S>' , AGR = '<STR_LIT:R>' , <EOL> GTN = '<STR_LIT>' , GCN = '<STR_LIT:A>' , GAY = '<STR_LIT:D>' , GAR = '<STR_LIT:E>' , GGN = '<STR_LIT>' , <EOL> TAR = '<STR_LIT:*>' , TGA = '<STR_LIT:*>' ) <EOL> def __init__ ( self ) : <EOL> '<STR_LIT>' <EOL> geneticCode = self . geneticCode . copy ( ) <EOL> for codon , aa in self . geneticCode . items ( ) : <EOL> if codon [ <NUM_LIT:2> ] == '<STR_LIT:N>' : <EOL> geneticCode [ codon [ : <NUM_LIT:2> ] + '<STR_LIT:A>' ] = aa <EOL> geneticCode [ codon [ : <NUM_LIT:2> ] + '<STR_LIT:T>' ] = aa <EOL> geneticCode [ codon [ : <NUM_LIT:2> ] + '<STR_LIT>' ] = aa <EOL> geneticCode [ codon [ : <NUM_LIT:2> ] + '<STR_LIT:C>' ] = aa <EOL> elif codon [ <NUM_LIT:2> ] == '<STR_LIT:Y>' : <EOL> geneticCode [ codon [ : <NUM_LIT:2> ] + '<STR_LIT:T>' ] = aa <EOL> geneticCode [ codon [ : <NUM_LIT:2> ] + '<STR_LIT:C>' ] = aa <EOL> elif codon [ <NUM_LIT:2> ] == '<STR_LIT:R>' : <EOL> geneticCode [ codon [ : <NUM_LIT:2> ] + '<STR_LIT:A>' ] = aa <EOL> geneticCode [ codon [ : <NUM_LIT:2> ] + '<STR_LIT>' ] = aa <EOL> self . geneticCode = geneticCode <EOL> def __call__ ( self , s ) : <EOL> '<STR_LIT>' <EOL> s = s . upper ( ) <EOL> s = s . replace ( '<STR_LIT>' , '<STR_LIT:T>' ) <EOL> l = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( s ) , <NUM_LIT:3> ) : <EOL> try : <EOL> l . append ( self . geneticCode [ s [ i : i + <NUM_LIT:3> ] ] ) <EOL> except KeyError : <EOL> l . append ( '<STR_LIT:X>' ) <EOL> return '<STR_LIT>' . join ( l ) <EOL> translate_orf = AATranslation ( ) </s>
