def _getStructure(self,prsd=None,reroot=False): ''' PRIVATE: Acquires a newick string without any defined branch lengths. ''' if prsd: p = prsd else: p = newick.newickParser(self.newick).parse() if reroot: topo = rearrangement.topology(p) p = topo.getRoot() newick.removeBranchLengths(p) return str(p) + ';'
def findTreeTopology(self, newick): ''' Find a tree by topology, not taking into account branch lengths. :param newick: a Newick string :type newick: a string :return: a tree name (usually an integer index) or None if not found ''' s = newickParser(newick).parse() removeBranchLengths(s) s = str(s) + ';' return self.findTreeTopologyByStructure(s)
def findTreeTopology(self,newick): ''' Find a tree by topology, not taking into account branch lengths. :param newick: a Newick string :type newick: a string :return: a tree name (usually an integer index) or None if not found ''' s = newickParser(newick).parse() removeBranchLengths(s) s = str(s) + ';' return self.findTreeTopologyByStructure(s)
def getSimpleNewick(self): ''' Return a Newick string with all taxa name replaced with successive integers. :return: a string ''' o = newick.newickParser(self.newick).parse() t = base.treeStructure(o) n = t.getAllLeaves() for _ in xrange(1,len(n)+1): n[_-1].label = str(_) return str(o) + ';'
def reinterpretNewick(self, tr): ''' Revert the replacing of taxa names with shortened names by changing them back to their original form. :param tr: a Newick string :type tr: a string :return: a Newick string with all replaced names ''' p = newickParser(tr).parse() nodes = base.treeStructure(p).getAllNodes() for node in nodes: l = node.label if l in self.namedict: node.label = self.namedict[l] return str(p) + ';'
def reinterpretNewick(self,tr): ''' Revert the replacing of taxa names with shortened names by changing them back to their original form. :param tr: a Newick string :type tr: a string :return: a Newick string with all replaced names ''' p = newickParser(tr).parse() nodes = base.treeStructure(p).getAllNodes() for node in nodes: l = node.label if l in self.namedict: node.label = self.namedict[l] return str(p) + ';'
def parse(self, newickstr): ''' Parse a newick string and assign the tree to this object. Cannot already be initialized with a tree. :return: None ''' if self.root != None: raise RearrangementError('Structure already initialized.') p = newick.newickParser(newickstr) self.root = p.parse() self.orig = newickstr if self.rerootFlag: self.rerootToLeaf(self.rerootLoc) self._getAllBranches() self._getForbiddenStates() self._clearInteriorNodeNames() if self.rerootFlag: self._lockLeafBranch()
def convertOriginalNewick(self, tr): ''' Return a Newick string with (original) taxa names that are replaced with the shortened forms as they are defined in this object. :param tr: a Newick string :type tr: a string :return: a Newick string with all replaced names ''' p = newickParser(tr).parse() nodes = base.treeStructure(p).getAllNodes() for node in nodes: l = node.label if l in self.namedict.values(): for n in self.namedict: if self.namedict[n] == l: node.label = n break return str(p) + ';'
def convertOriginalNewick(self,tr): ''' Return a Newick string with (original) taxa names that are replaced with the shortened forms as they are defined in this object. :param tr: a Newick string :type tr: a string :return: a Newick string with all replaced names ''' p = newickParser(tr).parse() nodes = base.treeStructure(p).getAllNodes() for node in nodes: l = node.label if l in self.namedict.values(): for n in self.namedict: if self.namedict[n] == l: node.label = n break return str(p) + ';'