Example #1
0
 def getBipartitionFoundInTreeByIndex(self,tr,brind,topol=None):
     
     ''' Given a tree node and a branch index, return the associated
     bipartition.
     
     :param tr: a tree name
     :param brind: a branch index in that tree, a la post-order traversal
     :return: a :class:`.tree.bipartition` object
     
     '''
     
     # Check for presence of tree.
     if (not topol) and (not tr in self.graph.node): return None
     
     # Acquire metadata for tree.
     if not topol:
         tr = self.graph.node[tr]['tree']
         topl = tr.toTopology()
     
     # Get bipartition for branch.
     nodes = postOrderTraversal(topl.getRoot())
     if not brind in xrange(len(nodes)): return None
     bnode = nodes[brind]
     bipa  = tree.bipartition(topl,bnode.parent)
     
     # Return the bipartition.
     return bipa
    def getBipartitionFoundInTreeByIndex(self, tr, brind, topol=None):
        ''' Given a tree node and a branch index, return the associated
        bipartition.
        
        :param tr: a tree name
        :param brind: a branch index in that tree, a la post-order traversal
        :return: a :class:`.tree.bipartition` object
        
        '''

        # Check for presence of tree.
        if (not topol) and (not tr in self.graph.node): return None

        # Acquire metadata for tree.
        if not topol:
            tr = self.graph.node[tr]['tree']
            topl = tr.toTopology()

        # Get bipartition for branch.
        nodes = postOrderTraversal(topl.getRoot())
        if not brind in xrange(len(nodes)): return None
        bnode = nodes[brind]
        bipa = tree.bipartition(topl, bnode.parent)

        # Return the bipartition.
        return bipa
Example #3
0
    def lockBranchFoundInTree(self,tr,br):
        
        ''' Given a tree node and a branch object, add a given 
        bipartition to the bipartition lock list. Returns 
        bipartition if locked.
        
        :param tr: a tree name
        :param br: a branch in that tree
        :type br: a :class:`.base.treeBranch` object
        :return: a :class:`.tree.bipartition` object that has been locked or None
        
        '''

        # Check for presence of tree.
        g = self.graph.node
        if (not tr in g): return None
        
        # Acquire metadata for tree.
        tr = self.getTree(tr)
        topl = tr.toTopology()
        
        # Get bipartition for branch.
        bipa  = tree.bipartition(topl,br)
        
        # Toggle the lock.
        self.toggleLock(bipa)
        return bipa
    def lockBranchFoundInTree(self, tr, br):
        ''' Given a tree node and a branch object, add a given 
        bipartition to the bipartition lock list. Returns 
        bipartition if locked.
        
        :param tr: a tree name
        :param br: a branch in that tree
        :type br: a :class:`.base.treeBranch` object
        :return: a :class:`.tree.bipartition` object that has been locked or None
        
        '''

        # Check for presence of tree.
        g = self.graph.node
        if (not tr in g): return None

        # Acquire metadata for tree.
        tr = self.getTree(tr)
        topl = tr.toTopology()

        # Get bipartition for branch.
        bipa = tree.bipartition(topl, br)

        # Toggle the lock.
        self.toggleLock(bipa)
        return bipa
    def getNeighborsOfBranch(self, br):
        ''' Get corresponding neighbors of a branch in this vertex's tree. '''

        tr = self.getTree()
        topl = tr.toTopology()
        bipa = tree.bipartition(topl, br)
        return self.getNeighborsOfBipartition(bipa)
Example #6
0
 def getNeighborsOfBranch(self, br):
     
     ''' Get corresponding neighbors of a branch in this vertex's tree. '''
     
     tr   = self.getTree()
     topl = tr.toTopology()
     bipa = tree.bipartition(topl,br)
     return self.getNeighborsOfBipartition(bipa)
def dup(topo, where=None):
    if where: new = topology(toLeaf=where)
    else: new = topology()
    new.fromNewick(topo.toNewick())
    if where:
        fake = tree.bipartition(topo, new.fakebranch)
        # Retain locks.
        for lock in new.locked:
            if lock != fake: new.locked.append(lock)
    return new
    def getBipartitions(self):
        ''' Get all bipartitions.
        
        :return: a list of :class:`.tree.bipartition` objects
        
        '''

        bili = []
        br = self.branches
        for b in br:
            bi = tree.bipartition(self, b)
            if not bi in bili: bili.append(bi)
        return bili
Example #9
0
 def iterBipartitions(self):
     
     ''' Return a generator to iterate over all bipartitions for this vertex.
     '''
 
     # Get tree object.
     tr = self.getTree()
     
     # Get corresponding topology.
     topo  = tr.toTopology()
     nodes = postOrderTraversal(topo.getRoot())
     
     # Get branches + bipartitions of the topology.
     bp = []
     br = [x.parent for x in nodes]
     for b in br:
         if b:
             bi = tree.bipartition(topo,b)
             yield bi
         else: yield None
    def iterBipartitions(self):
        ''' Return a generator to iterate over all bipartitions for this vertex.
        '''

        # Get tree object.
        tr = self.getTree()

        # Get corresponding topology.
        topo = tr.toTopology()
        nodes = postOrderTraversal(topo.getRoot())

        # Get branches + bipartitions of the topology.
        bp = []
        br = [x.parent for x in nodes]
        for b in br:
            if b:
                bi = tree.bipartition(topo, b)
                yield bi
            else:
                yield None
    def lockBranch(self, branch):
        ''' Given a branch, lock it such that no transitions can ever occur
        across it.
        
        :param branch: a branch
        :type branch: a :class:`.newick.branch` object
        :return: a boolean (True if success)
        
        '''

        # Transform to bipartition object.
        bipart = tree.bipartition(self, branch)

        # Has already been locked?
        if bipart in self.locked: return True

        # Is it even in this topology?
        isintopol = (branch in self.branches)
        if (not isintopol): return False
        self.locked.append(bipart)
        return True
    def _flipOperation(self, opname, br):
        ''' PRIVATE: Try a particular rearrangement operation on a flipped
        version of the tree. '''

        # Check what bipartition is represented.
        bipart = tree.bipartition(self, br)
        # Get a leaf that can be rerooted to.
        newleaf = None
        leaves = base.treeStructure.leaves(br.child)
        curleaf = sorted(self.getAllLeaves(), key=lambda d: d.label)[0]
        for leaf in leaves:
            if leaf != curleaf:
                newleaf = leaf
                break
        if (newleaf == None): return []
        # Make flipped tree structure.
        n = dup(self, newleaf)
        # Get corresponding branch.
        b = n.getBranchFromBipartition(bipart)
        # Do possible moves from the other way.
        op = getattr(n, opname)
        return op(b, flip=False)