def init(self, **kwargs): """Valid keyword arguments include filename, format, newick, taxon_labels, trees and title. filename should be a string and should represent the path to a tree file. format is relevant only if filename is specified. The values for format are in the FileFormats class (but currently include only FileFormats.NEXUS). newick is a newick string representation of a tree topology (with or without edge lengths). taxon_labels provide names for the numbers used in a newick description (alternatively, taxon labels can be provided directly in the newick string) trees is an iterable collection of phylogeny.Tree objects. title is a label for the collection. """ self.reset() self.title = kwargs.get("title") self.filename = kwargs.get("filename") if self.filename is None: self.filename = kwargs.get("file") if self.filename: self.format = kwargs.get("format", FileFormats.NEXUS) self.trees = [] self.taxon_labels = None else: self.format = None self.taxon_labels = kwargs.get("taxon_labels") newick = kwargs.get("newick") if newick: if not isinstance(newick, Newick): newick = Newick(newick) tree = phylogeny.Tree() # by default the tree is read as 1-based (as in NEXUS trees) newick.buildTree(tree) self.trees = [tree] else: self.trees = kwargs.get("trees", []) self.active_taxa = self.taxon_labels
def _simulateTree(self): if (not self.taxon_labels) and self.n_taxa < 1 and (not self.newick): raise ValueError("TreeSimulator cannot be created with an empty list of taxon_labels and n_taxa set to 0") tl = self.taxon_labels if tl: t = Phylogeny.Tree(taxon_labels=self.taxon_labels) ntax = len(self.taxon_labels) else: t = Phylogeny.Tree() ntax = self.n_taxa tm = Phylogeny.TreeManip(t) eld = self.edgelen_dist self.stdout.debugging("RandomTree._simulateTree seed = %d" % self.r.getSeed()) n = self.newick if n: if not isinstance(n, Newick): n = Newick(n) n.buildTree(t) tm.setRandomEdgeLengths(self.edgelen_dist) elif self.distribution.lower() == "yule": if eld is None: isp = self.speciation_rate if isp is None or isp <= 0.0: raise ValueError( "edgelen_dist or the speciation_rate must be set to generate trees from the Yule process" ) eld = Exponential(isp) eld.setLot(self.r) self.stdout.debugging("Calling TreeManip.yuleTree") tm.yuleTree(ntax, self.r, eld) else: self.stdout.debugging("Calling TreeManip.randTree") tm.randTree(ntax, self.r, eld) else: self.stdout.debugging("Calling TreeManip.equiprobTree") tm.equiprobTree(ntax, self.r, eld) return t