def testReroot(self): tree = NX.DiGraph() edges = [(1,2), (1,3), (2,4), (2,5), (5,6), (5,7), (5,8)] for edge in edges: tree.add_edge(edge[0], edge[1]) nxTree = NXTree(tree) nxTree.setWeight(1, 2, 3.1) to = [x for x in nxTree.breadthFirstTraversal()] testTree = copy.deepcopy(nxTree) testTree.reroot(1) t = [x for x in testTree.breadthFirstTraversal()] assert t == to testTree = copy.deepcopy(nxTree) testTree.reroot(2) t = [x for x in testTree.breadthFirstTraversal()] assert t[0] == 2 assert sorted(t[1:4]) == [1,4,5] assert sorted(t[4:]) == [3,6,7,8] assert testTree.getWeight(2, 1) == 3.1 testTree = copy.deepcopy(nxTree) testTree.reroot(7) t = [x for x in testTree.breadthFirstTraversal()] assert t[0] == 7 assert t[1] == 5 assert sorted(t[2:5]) == [2,6,8] assert sorted(t[5:7]) == [1,4] assert t[7] == 3
def parseString(self, newickString, addImpliedRoots=True): self.nxTree = NXTree() self.inString = self.__filterWhitespace(newickString) self.__createBracketTable() self.nextId = 0 assert self.inString[-1] == ';' self.__addNode(0, len(self.inString) - 1, None, addImpliedRoots) self.nxTree.isTree() return self.nxTree
def testTraversals(self): for tree in self.trees: nxTree = NXTree(tree) dfs = [x for x in nxTree.preOrderTraversal()] assert len(set(dfs)) == len(nxTree.nxDg.nodes()) dfs = [x for x in nxTree.postOrderTraversal()] assert len(set(dfs)) == len(nxTree.nxDg.nodes()) bfs = [x for x in nxTree.breadthFirstTraversal()] assert len(set(bfs)) == len(nxTree.nxDg.nodes())
def starTree(self): self.tree = NXTree() label = 0 self.tree.nxDg.add_node(label) self.tree.rootId = label for name in self.pathMap.keys(): label += 1 self.tree.nxDg.add_edge(0, label) self.tree.setName(label, name) self.tree.setWeight(0, label, SeqFile.branchLen)
def testRoot(self): for tree in self.trees: nxTree = NXTree(tree) rootId = nxTree.getRootId() assert rootId is not None assert nxTree.getParent(rootId) is None