#NEWLINE# def main( argv = None ) : #NEWLINE# #INDENT# parser = E . OptionParser ( version = '%prog version: $Id$' , usage = globals ( ) [ '__doc__' ] ) #NEWLINE# parser . add_option ( '-w' , '--weights-tsv-file' , dest = 'filename_weights' , type = 'string' , help = 'filename with codon frequencies. Multiple filenames can be separated by comma.' ) #NEWLINE# parser . add_option ( '-s' , '--section' , dest = 'sections' , type = 'choice' , action = 'append' , choices = ( 'length' , 'sequence' , 'hid' , 'na' , 'aa' , 'cpg' , 'dn' , 'degeneracy' , 'gaps' , 'codons' , 'codon-usage' , 'codon-translator' , 'codon-bias' ) , help = 'which sections to output [%default]' ) #NEWLINE# parser . add_option ( '-t' , '--sequence-type' , dest = 'seqtype' , type = 'choice' , choices = ( 'na' , 'aa' ) , help = 'type of sequence: na=nucleotides, aa=amino acids [%default].' ) #NEWLINE# parser . add_option ( '-e' , '--regex-identifier' , dest = 'regex_identifier' , type = 'string' , help = 'regular expression to extract identifier from fasta description line.' ) #NEWLINE# parser . add_option ( '--split-fasta-identifier' , dest = 'split_id' , action = 'store_true' , help = 'split fasta description line (starting >) and use only text before first space' ) #NEWLINE# parser . add_option ( '--add-total' , dest = 'add_total' , action = 'store_true' , help = 'add a row with column totals at the end of the table[%default]' ) #NEWLINE# parser . set_defaults ( filename_weights = None , pseudocounts = 1 , sections = [ ] , regex_identifier = '(.+)' , seqtype = 'na' , gap_chars = 'xXnN' , split_id = False , add_total = False ) #NEWLINE# ( options , args ) = E . Start ( parser , argv = argv ) #NEWLINE# rx = re . compile ( options . regex_identifier ) #NEWLINE# reference_codons = [ ] #NEWLINE# if options . filename_weights : #NEWLINE# #INDENT# options . filename_weights = options . filename_weights . split ( ',' ) #NEWLINE# for filename in options . filename_weights : #NEWLINE# #INDENT# if ( filename == 'uniform' ) : #NEWLINE# #INDENT# reference_codons . append ( Genomics . GetUniformCodonUsage ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# reference_codons . append ( IOTools . ReadMap ( IOTools . openFile ( filename , 'r' ) , has_header = True , map_functions = ( str , float ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# options . stdlog . write ( '# Difference between supplied codon usage preferences.\n' ) #NEWLINE# for x in range ( 0 , len ( reference_codons ) ) : #NEWLINE# #INDENT# for y in range ( 0 , len ( reference_codons ) ) : #NEWLINE# #INDENT# if ( x == y ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# a = reference_codons [ x ] #NEWLINE# b = reference_codons [ y ] #NEWLINE# d = 0 #NEWLINE# for ( codon , p ) in a . items ( ) : #NEWLINE# #INDENT# if Genomics . IsStopCodon ( codon ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# d += ( b [ codon ] * math . log ( ( b [ codon ] / p ) ) ) #NEWLINE# #UNINDENT# options . stdlog . write ( ( '# tablediff\t%s\t%s\t%f\n' % ( options . filename_weights [ x ] , options . filename_weights [ y ] , d ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# iterator = FastaIterator . FastaIterator ( options . stdin ) #NEWLINE# def getCounter( section ) : #NEWLINE# #INDENT# if ( options . seqtype == 'na' ) : #NEWLINE# #INDENT# if ( section == 'length' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesLength ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'sequence' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesSequence ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'hid' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesHid ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'na' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesNA ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'gaps' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesGaps ( options . gap_chars ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'cpg' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesCpg ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'dn' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesDN ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'aa' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesAA ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'degeneracy' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesDegeneracy ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'codon-bias' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesBias ( reference_codons ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'codons' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesCodons ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'codon-usage' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesCodonUsage ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'codon-translator' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesCodonTranslator ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( ( 'unknown section %s' % section ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( options . seqtype == 'aa' ) : #NEWLINE# #INDENT# if ( section == 'length' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesLength ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'sequence' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesSequence ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'hid' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesHid ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( section == 'aa' ) : #NEWLINE# #INDENT# s = SequenceProperties . SequencePropertiesAminoAcids ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( ( 'unknown section %s' % section ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return s #NEWLINE# #UNINDENT# totals = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for section in options . sections : #NEWLINE# #INDENT# totals [ section ] = getCounter ( section ) #NEWLINE# #UNINDENT# options . stdout . write ( 'id' ) #NEWLINE# for section in options . sections : #NEWLINE# #INDENT# options . stdout . write ( ( '\t' + '\t' . join ( totals [ section ] . getHeaders ( ) ) ) ) #NEWLINE# #UNINDENT# options . stdout . write ( '\n' ) #NEWLINE# options . stdout . flush ( ) #NEWLINE# s = getCounter ( 'hid' ) #NEWLINE# s . loadSequence ( 'AAAAAAAAA' , 'na' ) #NEWLINE# for cur_record in iterator : #NEWLINE# #INDENT# sequence = re . sub ( ' ' , '' , cur_record . sequence ) . upper ( ) #NEWLINE# if ( len ( sequence ) == 0 ) : #NEWLINE# #INDENT# raise ValueError ( ( 'empty sequence %s' % cur_record . title ) ) #NEWLINE# #UNINDENT# id = rx . search ( cur_record . title ) . groups ( ) [ 0 ] #NEWLINE# if ( options . split_id is True ) : #NEWLINE# #INDENT# options . stdout . write ( ( '%s' % id . split ( ) [ 0 ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# options . stdout . write ( ( '%s' % id ) ) #NEWLINE# #UNINDENT# options . stdout . flush ( ) #NEWLINE# for section in options . sections : #NEWLINE# #INDENT# s = getCounter ( section ) #NEWLINE# s . loadSequence ( sequence , options . seqtype ) #NEWLINE# totals [ section ] . addProperties ( s ) #NEWLINE# options . stdout . write ( ( '\t' + '\t' . join ( s . getFields ( ) ) ) ) #NEWLINE# #UNINDENT# options . stdout . write ( '\n' ) #NEWLINE# #UNINDENT# if options . add_total : #NEWLINE# #INDENT# options . stdout . write ( 'total' ) #NEWLINE# for section in options . sections : #NEWLINE# #INDENT# options . stdout . write ( ( '\t' + '\t' . join ( totals [ section ] . getFields ( ) ) ) ) #NEWLINE# #UNINDENT# options . stdout . write ( '\n' ) #NEWLINE# #UNINDENT# E . Stop ( )