I have a newick tree newick_tree_string = "(p1,(((((((p10,p5),p11),((p14,p6),p8)),p16),(((p12,p13),p15),p2)),p9),(p3,p7)),p4)"
And I need to get it into networkx form where the nodes are the "p_i". I tried reading it using BioPython:
tree = Phylo.read(io.StringIO(newick_tree_string), 'newick')
#name nameless clades
def tabulate_names(tree):
names = {}
for idx, clade in enumerate(tree.find_clades()):
if clade.name:
clade.name = clade.name
else:
clade.name = str(idx)
names[clade.name] = clade
return names
tabulate_names(tree)
G = Phylo.to_networkx(tree)
I have another networkx graph which I need to compare it to node by node:
g = nx.Graph()
g.add_edges_from([('p17','p9'),('p17','p18'),('p17','p19'),('p19','p20'),('p9','p21'),('p21','p4'),('p21','p1'),('p9','p7'),('p7','p3'),('p20','p10'),('p20','p5'),('p19','p11'),('p19','p8'),('p8','p6'),('p8','p14'),('p18','p2'),('p18','p13'),('p18','p15'),('p18','p16'),('p13','p12')])
root_node = 17
The problem is that the first graph's nodes are of these weird 'clade' data_type which I can't use in functions with normal networkx graphs due to the different data structure. I need to turn the clades into nodes whilst keeping the same structure and names.
Is there an easy way of doing this?
u can check edge and node