def testPhylogenyFromNewick(self): """ Tries to load a newick tree from file. """ phylo_file = os.path.dirname(os.path.realpath(__file__)) + '/example_data/Asp_protease_2.nhx' phylo_graph = BioBayesGraph() graph = phylo_graph.populate_from_newick(phylo_file) self.assertTrue(("name" in graph.vertex_properties), msg="Clade names not imported correctly.") self.assertTrue(("branch_length" in graph.edge_properties), msg="Branch lengths not imported correctly.") bl_sum = 0.0 for e in graph.edges(): bl_sum += float(graph.edge_properties['branch_length'][e]) self.assertTrue(expr=abs(bl_sum - 168.58699) < 1e-6, msg="Branch lengths not imported correctly "\ +"(sum is wrong)") self.assertEqual(graph.num_vertices(), 608, "Didn't get expected number of nodes from phylogeny.") self.assertEqual(graph.num_edges(), 607, "Didn't get expected number of nodes from phylogeny.")
class SIFTER(object): def __init__(self): """ Constructor """ self.phylo_graph = BioBayesGraph() self.evidence_processors = {} def load_phylogeny(self, phylo_file, phylo_format='phyloxml'): """ """ if phylo_format == 'phyloxml': self.phylo_graph.populate_from_phyloxml(phylo_file) elif phylo_format == 'newick': self.phylo_graph.populate_from_newick(phylo_file) else: raise Exception, "Phylo format requested isn't supported." def load_evidence_processor(self, evidence_type, evidence_processor_class, processor_settings): ''' Loads evidence processor to internal reference ''' self.evidence_processors[evidence_type] = evidence_processor_class(processor_settings) def parse_evidence(self, evidence_type, evidence_file, evidence_constraints, evidence_format): """ """ if not evidence_type in self.evidence_processors: raise Exception, "Evidence type requested doesn't have a handler." return self.evidence_processors[evidence_type].parse_evidence(\ evidence_file=evidence_file, evidence_format=evidence_format, evidence_constraints=evidence_constraints) def setup_nodes(self, node_to_fcn_model_map): ''' Node to fcn_model_map is a function mapping "vertex_id" to { 'auxiliary_info':{'num_functions':num_fcns, 'max_num_simul':3}, 'prob_dist_class':prob_dist_class } E.g. node_to_fcn_model_map = \ lambda v: { \ 'auxiliary_info':{'num_functions':num_fcns, 'max_num_simul':3}, 'prob_dist_class':'FunctionModels.Sifter2.FunctionModel' } ''' dist_fcn_classes = {} for n in self.phylo_graph.g.vertices(): node_index = int(n) fcn_model_info = node_to_fcn_model_map(node_index) # Store auxiliary info by node self.phylo_graph.set_node_auxiliary_information(node_index=node_index, auxiliary_info=fcn_model_info['auxiliary_info']) # Make an instance of the custom prob dist function dist_model = fcn_model_info['prob_dist_class'] if dist_model.__name__ not in dist_fcn_classes: self.phylo_graph.add_prob_dist(prob_dist_class=dist_model) dist_fcn_classes[dist_model.__name__] = dist_model(None,None,None,None) dist_inst = dist_fcn_classes[dist_model.__name__] # Query the number of variables from the custom function self.phylo_graph.set_node_variable_count(node_index=node_index, num_vars=1) # Query the domain of each variable from the custom function protein_states = [f for f in dist_inst.possible_protein_states(\ fcn_variants_cnt=fcn_model_info['auxiliary_info']['num_functions'], max_fcn_cnt=fcn_model_info['auxiliary_info']['max_num_simul'])] self.phylo_graph.set_node_variable_domains(node_index=node_index, var_domains=[protein_states]) # Store the distribution function in the graph for the node. self.phylo_graph.set_node_probability_dist(node_index=node_index, prob_dist_class=fcn_model_info['prob_dist_class'].__name__) def process_evidence(self, evidence_type, evidence_set, evidence_constraints): ''' Incorporates evidence into the graph using the appropriate processor ''' if not evidence_type in self.evidence_processors: raise Exception, "Evidence type requested doesn't have a handler." return self.evidence_processors[evidence_type].process_evidence(\ evidence_set=evidence_set, evidence_constraints=evidence_constraints)