def compare_trees(tree_filename1, tree_filename2): from dendropy import Tree, TreeList from dendropy.treecalc import symmetric_difference, euclidean_distance, robinson_foulds_distance as rbd, PatristicDistanceMatrix as pdm c = TreeList([g(tree_filename1), g(tree_filename2)]) pp1 = pdm(c[0]).distances() pp2 = pdm(c[1]).distances() sumbl1 = sum(n.edge_length for n in c[0].nodes() if n.edge_length is not None) sumbl2 = sum(n.edge_length for n in c[1].nodes() if n.edge_length is not None) e = [n.edge_length for n in c[0].nodes() if n.edge_length is not None] return {'nBSD':euclidean_distance(c[0], c[1]), 'SDD':symmetric_difference(c[0], c[1]), 'RBD':rbd(c[0], c[1]), 'edgeDelta1': max(pp1)-min(pp1), 'edgeStd1': np.std(pp1),\ 'edgeDelta2': max(pp2)-min(pp2), 'edgeStd2': np.std(pp2), 'SumBranchLen1': sumbl1, 'SumBranchLen2': sumbl2}
def runTest(self): tree_list = dendropy.TreeList(stream=StringIO( """((t5:0.161175,t6:0.161175):0.392293,((t4:0.104381,(t2:0.075411,t1:0.075411):1):0.065840,t3:0.170221):0.383247); ((t5:2.161175,t6:0.161175):0.392293,((t4:0.104381,(t2:0.075411,t1:0.075411):1):0.065840,t3:0.170221):0.383247); ((t5:0.161175,t6:0.161175):0.392293,((t2:0.075411,(t4:0.104381,t1:0.075411):1):0.065840,t3:0.170221):0.383247); ((t5:0.161175,t6:0.161175):0.392293,((t4:0.104381,(t2:0.075411,t1:0.075411):0.028969):0.065840,t3:0.170221):0.383247); """), schema="newick") for i in tree_list: encode_splits(i) self.assertAlmostEqual( treecalc.euclidean_distance(tree_list[0], tree_list[1]), 2.0) self.assertAlmostEqual( treecalc.euclidean_distance(tree_list[0], tree_list[2]), math.sqrt(2.0)) self.assertAlmostEqual( treecalc.euclidean_distance(tree_list[0], tree_list[3]), 0.97103099999999998) self.assertAlmostEqual( treecalc.euclidean_distance(tree_list[1], tree_list[2]), math.sqrt(6.0)) self.assertAlmostEqual( treecalc.euclidean_distance(tree_list[1], tree_list[3]), 2.2232636377544162) self.assertAlmostEqual( treecalc.euclidean_distance(tree_list[2], tree_list[3]), 1.000419513484718)
def calc_dtcs(self): for cfactor in self.clist: min_bic=self.min_criterion['BIC'][cfactor] for model in self.models: dtc_sum=0.0 model_tree=self.models[model].tree for other_model in self.models: if other_model==model: continue other_tree=self.models[other_model].tree distance=euclidean_distance(model_tree,other_tree) if distance <= 0.0: continue other_bic=self.models[other_model].criterion['BIC'][cfactor] power = log(distance) - other_bic + min_bic if power > -30.0: dtc_sum+=exp(power) self.models[model].criterion['DTC'][cfactor]=dtc_sum
def calc_dtcs(self): for cfactor in self.clist: min_bic = self.min_criterion['BIC'][cfactor] for model in self.models: dtc_sum = 0.0 model_tree = self.models[model].tree for other_model in self.models: if other_model == model: continue other_tree = self.models[other_model].tree distance = euclidean_distance(model_tree, other_tree) if distance <= 0.0: continue other_bic = self.models[other_model].criterion['BIC'][ cfactor] power = log(distance) - other_bic + min_bic if power > -30.0: dtc_sum += exp(power) self.models[model].criterion['DTC'][cfactor] = dtc_sum
def runTest(self): tree_list = dendropy.TreeList( stream=StringIO("""((t5:0.161175,t6:0.161175):0.392293,((t4:0.104381,(t2:0.075411,t1:0.075411):1):0.065840,t3:0.170221):0.383247); ((t5:2.161175,t6:0.161175):0.392293,((t4:0.104381,(t2:0.075411,t1:0.075411):1):0.065840,t3:0.170221):0.383247); ((t5:0.161175,t6:0.161175):0.392293,((t2:0.075411,(t4:0.104381,t1:0.075411):1):0.065840,t3:0.170221):0.383247); ((t5:0.161175,t6:0.161175):0.392293,((t4:0.104381,(t2:0.075411,t1:0.075411):0.028969):0.065840,t3:0.170221):0.383247); """), schema="newick") for i in tree_list: encode_splits(i) self.assertAlmostEqual(treecalc.euclidean_distance(tree_list[0], tree_list[1]), 2.0) self.assertAlmostEqual(treecalc.euclidean_distance(tree_list[0], tree_list[2]), math.sqrt(2.0)) self.assertAlmostEqual(treecalc.euclidean_distance(tree_list[0], tree_list[3]), 0.97103099999999998) self.assertAlmostEqual(treecalc.euclidean_distance(tree_list[1], tree_list[2]), math.sqrt(6.0)) self.assertAlmostEqual(treecalc.euclidean_distance(tree_list[1], tree_list[3]), 2.2232636377544162) self.assertAlmostEqual(treecalc.euclidean_distance(tree_list[2], tree_list[3]), 1.000419513484718)
def compare_trees(tree_filename1, tree_filename2): from dendropy import Tree, TreeList from dendropy.treecalc import symmetric_difference, euclidean_distance, robinson_foulds_distance as rbd c = TreeList([g(tree_filename1), g(tree_filename2)]) return {'nBSD':euclidean_distance(c[0],c[1]), 'SDD':symmetric_difference(c[0], c[1]), \ 'RBD':rbd(c[0],c[1])}