<s> """<STR_LIT>""" <EOL> ambiguous_nucleotide_code_codec = { <EOL> "<STR_LIT:A>" : "<STR_LIT:A>" , <EOL> "<STR_LIT:C>" : "<STR_LIT:C>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:T>" : "<STR_LIT:T>" , <EOL> "<STR_LIT:M>" : "<STR_LIT>" , <EOL> "<STR_LIT:R>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:S>" : "<STR_LIT>" , <EOL> "<STR_LIT:Y>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:H>" : "<STR_LIT>" , <EOL> "<STR_LIT:D>" : "<STR_LIT>" , <EOL> "<STR_LIT:B>" : "<STR_LIT>" , <EOL> "<STR_LIT:N>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:M>" , <EOL> "<STR_LIT>" : "<STR_LIT:R>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:S>" , <EOL> "<STR_LIT>" : "<STR_LIT:Y>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:H>" , <EOL> "<STR_LIT>" : "<STR_LIT:D>" , <EOL> "<STR_LIT>" : "<STR_LIT:B>" , <EOL> "<STR_LIT>" : "<STR_LIT:N>" <EOL> } <EOL> class pwMatrix ( object ) : <EOL> nucleotide_column = { "<STR_LIT:A>" : <NUM_LIT:0> , "<STR_LIT:C>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:2> , "<STR_LIT:T>" : <NUM_LIT:3> , "<STR_LIT:a>" : <NUM_LIT:0> , "<STR_LIT:c>" : <NUM_LIT:1> , "<STR_LIT:g>" : <NUM_LIT:2> , "<STR_LIT:t>" : <NUM_LIT:3> , <NUM_LIT:0> : "<STR_LIT:A>" , <NUM_LIT:1> : "<STR_LIT:C>" , <NUM_LIT:2> : "<STR_LIT>" , <NUM_LIT:3> : "<STR_LIT:T>" } <EOL> def __init__ ( self ) : <EOL> self . pwm = [ ] <EOL> def __str__ ( self ) : <EOL> lines = [ ] <EOL> lines . append ( "<STR_LIT>" ) <EOL> for index in range ( len ( self ) ) : <EOL> lines . append ( "<STR_LIT>" % ( index + <NUM_LIT:1> , self . getProbabilityWeight ( index , "<STR_LIT:A>" ) , self . getProbabilityWeight ( index , "<STR_LIT:C>" ) , self . getProbabilityWeight ( index , "<STR_LIT>" ) , self . getProbabilityWeight ( index , "<STR_LIT:T>" ) , self . consensus ( index ) , self . degenerate ( index ) ) ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <EOL> def toDBstring ( self ) : <EOL> lines = [ ] <EOL> for index in range ( len ( self ) ) : <EOL> lines . append ( "<STR_LIT>" % ( self . getProbabilityWeight ( index , "<STR_LIT:A>" ) , self . getProbabilityWeight ( index , "<STR_LIT:C>" ) , self . getProbabilityWeight ( index , "<STR_LIT>" ) , self . getProbabilityWeight ( index , "<STR_LIT:T>" ) , ) ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <EOL> def append ( self , nucleotide_weight_dict ) : <EOL> if len ( nucleotide_weight_dict ) != <NUM_LIT:4> : <EOL> raise Exception , "<STR_LIT>" <EOL> row = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> for nucleotide in nucleotide_weight_dict . keys ( ) : <EOL> row [ self . __getColumn__ ( nucleotide ) ] = nucleotide_weight_dict [ nucleotide ] <EOL> self . pwm . append ( row ) <EOL> def __getColumn__ ( self , nucleotide ) : <EOL> return pwMatrix . nucleotide_column [ nucleotide ] <EOL> def getProbabilityWeight ( self , position , nucleotide ) : <EOL> return self . pwm [ position ] [ self . __getColumn__ ( nucleotide ) ] <EOL> def __len__ ( self ) : <EOL> return len ( self . pwm ) <EOL> def clear ( self ) : <EOL> self . pwm = [ ] <EOL> def consensus ( self , position ) : <EOL> weightRow = self . pwm [ position ] <EOL> maxP = max ( weightRow ) <EOL> baseWithMaxP = "<STR_LIT>" <EOL> for index in range ( <NUM_LIT:4> ) : <EOL> if weightRow [ index ] == maxP : <EOL> baseWithMaxP += pwMatrix . nucleotide_column [ index ] <EOL> return ambiguous_nucleotide_code_codec [ baseWithMaxP ] <EOL> def degenerate ( self , position ) : <EOL> weightRow = self . pwm [ position ] <EOL> degenerateBases = "<STR_LIT>" <EOL> for index in range ( <NUM_LIT:4> ) : <EOL> if weightRow [ index ] >= <NUM_LIT> : <EOL> degenerateBases += pwMatrix . nucleotide_column [ index ] <EOL> return ambiguous_nucleotide_code_codec [ degenerateBases ] <EOL> def getICat ( self , position ) : <EOL> ic = <NUM_LIT> <EOL> letters = "<STR_LIT>" <EOL> for letter in letters : <EOL> p = self . getProbabilityWeight ( position , letter ) <EOL> if p != <NUM_LIT:0> : <EOL> ic += p * math . log ( p , <NUM_LIT:2> ) <EOL> return ic <EOL> def getIC ( self ) : <EOL> total_ic = <NUM_LIT:0> <EOL> for position in range ( len ( self ) ) : <EOL> total_ic += self . getICat ( position ) <EOL> return total_ic </s>
