def test_gnj(self): """testing gnj""" results = gnj(self.dists, keep=1) (length, reconstructed) = results[0] self.assertTreeDistancesEqual(self.tree, reconstructed) results = gnj(self.dists, keep=10) (length, reconstructed) = results[0] self.assertTreeDistancesEqual(self.tree, reconstructed) # Results should be a TreeCollection len(results) results.get_consensus_tree() # From GNJ paper. Pearson, Robins, Zhang 1999. tied_dists = { ("a", "b"): 3, ("a", "c"): 3, ("a", "d"): 4, ("a", "e"): 3, ("b", "c"): 3, ("b", "d"): 3, ("b", "e"): 4, ("c", "d"): 3, ("c", "e"): 3, ("d", "e"): 3, } results = gnj(tied_dists, keep=3) scores = [score for (score, tree) in results] self.assertEqual(scores[:2], [7.75, 7.75]) self.assertNotEqual(scores[2], 7.75)
def quick_tree(self, dists): """estimates a neighbor joining tree""" size = dists.shape[0] dists = dists.drop_invalid() if self._drop_invalid else dists if dists is None or (dists.shape[0] != size and not self._drop_invalid): raise ValueError("invalid pairwise distances") # how many species do we have if size == 2: dist = dists.array[0, 1] / 2.0 newick = ",".join(f"{sp}:{dist:.4f}" for sp in dists.names) newick = f"({newick})" tree = make_tree(treestring=newick, underscore_unmunge=True) else: (result, ) = gnj(dists.to_dict(), keep=1, show_progress=False) (score, tree) = result return tree
def quick_tree(self, dists): """estimates a neighbor joining tree""" size = dists.shape[0] dists = dists.drop_invalid() if self._drop_invalid else dists if dists is None or (dists.shape[0] != size and not self._drop_invalid): msg = (f"some pairwise distances could not be computed with" " {self._distance}, pick a different distance") raise ValueError(msg) # how many species do we have species = dists.keys() if len(species) == 2: dist = list(dists.values())[0] / 2.0 treestring = "(%s:%.4f,%s:%.4f)" % (species[0], dist, species[1], dist) tree = make_tree(treestring=treestring, underscore_unmunge=True) else: (result, ) = gnj(dists.to_dict(), keep=1, show_progress=False) (score, tree) = result return tree