def graph2tree(G, root=0, names=[]): # assum G is acyclic seed_node = Node() seed_node.label = names[root] if names else str(root) T = Tree(seed_node=seed_node) n = len(G) node_refs = [None for i in range(n)] node_refs[root] = seed_node count = 1 curr_v = root stk = [root] while len(stk) > 0: curr_v = stk.pop() for v, length in G[curr_v]: if node_refs[v] is None: stk.append(v) new_node = Node() new_node.label = names[v] if names else str(v) node_refs[v] = new_node node_refs[curr_v].add_child(new_node) new_node.edge_length = length for node in T.leaf_node_iter(): node.taxon = T.taxon_namespace.new_taxon(label=node.label) return T
phylum] != node.nleaf: #if phylum not in convergence or c.phylCount[phylum] > convergence[phylum][0]: if phylum not in groupings: groupings[phylum] = set([c]) else: groupings[phylum].add(c) #for phylum in global_phylCount: # if global_phylCount[phylum] > 1 and not ('Candi' in phylum or 'candi' in phylum): # print(phylum + " " + str(global_phylCount[phylum]) + " " + str(purity[phylum][0]) + " " + str(convergence[phylum][0])) # print(phylum,global_phylCount[phylum]) #print(global_phylCount['Firmicutes']) #print(purity['Firmicutes']) for phylum in groupings: if global_phylCount[phylum] < 2: continue if len(list(groupings[phylum])) == 1: ID = None else: ID = 1 for c in groupings[phylum]: #print(phylum + " " + c.label) subTree = Tree(seed_node=c) suffix = ("_" + str(ID)) if ID else '' for node in subTree.leaf_node_iter(): print(node.taxon.label + " " + phylum + suffix) if ID: ID += 1