#NEWLINE# def getRateMatrix( trained_model , terminals = None ) : #NEWLINE# #INDENT# 'return a rate matrix from an xrate grammar.\n\n    terminals: return rate matrix and frequencies for these\n        terminals. If none are given, a dictionaries of\n        matrices and frequencies are returned.\n\n\n    ' #NEWLINE# if terminals : #NEWLINE# #INDENT# xt = ( terminals , ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# xt = trained_model . mGrammar . getTerminals ( ) #NEWLINE# ( pis , matrices ) = ( { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# for tt in xt : #NEWLINE# #INDENT# xpi = trained_model . evaluateTerminalFrequencies ( ) [ tt ] #NEWLINE# pi = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( codon , f ) in xpi . items ( ) : #NEWLINE# #INDENT# pi [ '' . join ( codon ) . upper ( ) ] = f #NEWLINE# #UNINDENT# xmatrix = trained_model . evaluateRateMatrix ( ) [ tt ] #NEWLINE# matrix = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( codon1 , v ) in xmatrix . items ( ) : #NEWLINE# #INDENT# x = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( codon2 , f ) in xmatrix [ codon1 ] . items ( ) : #NEWLINE# #INDENT# x [ '' . join ( codon2 ) . upper ( ) ] = v #NEWLINE# #UNINDENT# matrix [ '' . join ( codon1 ) . upper ( ) ] = x #NEWLINE# #UNINDENT# if terminals : #NEWLINE# #INDENT# return ( pi , matrix ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# pis [ tt ] = pi #NEWLINE# matrices [ tt ] = matrix #NEWLINE# #UNINDENT# #UNINDENT# return ( pis , matrices )