-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewick-to-graph.py
53 lines (44 loc) · 1.27 KB
/
newick-to-graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from ete2 import Tree
import networkx as nx
def build_edges(G, t, node):
children_list = []
if node.is_root():
children_list = node.get_children()
## create edges between root and children
elif node.is_leaf():
print "%s is leaf." % node
else:
path_list = []
children_list = node.get_children()
for child in children_list:
temp_list = []
temp_list.append(node.name)
temp_list.append(child.name)
path_list.append(temp_list)
## create edges here with list
G.add_edges_from(path_list)
print "Loading taxonomy newick..."
t = Tree("ncbi_taxonomy.newick", format=8)
print "Complete."
G=nx.Graph()
node_list = []
print "Building node name list..."
for node in t.traverse():
if node.name == 'NoName':
node_list.append('Root')
else:
node_list.append(node.name)
print "Complete."
print "Building graph nodes..."
for n in node_list:
G.add_node(n)
print "Complete."
print "Building graph edges..."
for node in t.traverse():
build_edges(G, t, node)
print "Complete."
print "Number of nodes: %s" % G.number_of_nodes()
print "Number of edges: %s" % G.number_of_edges()
print "Writing to GraphML file..."
nx.write_graphml(G, "ncbi.gml")
print "Complete."