summaryrefslogtreecommitdiff
path: root/Code/degree_of_separation2.py
diff options
context:
space:
mode:
authorHui Lan <lanhui@zjnu.edu.cn>2019-12-04 19:03:19 +0800
committerHui Lan <lanhui@zjnu.edu.cn>2019-12-04 19:03:19 +0800
commit97fdefab064f63642fa3ece05b807d29b459df31 (patch)
treea058530023224f3e35b1783996f3530c80c04bc5 /Code/degree_of_separation2.py
brain: add python and R code to local repository.
Diffstat (limited to 'Code/degree_of_separation2.py')
-rw-r--r--Code/degree_of_separation2.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/Code/degree_of_separation2.py b/Code/degree_of_separation2.py
new file mode 100644
index 0000000..fd8d6e7
--- /dev/null
+++ b/Code/degree_of_separation2.py
@@ -0,0 +1,45 @@
+# Usage: python degree_of_deparation.py edges.txt
+# Purpose: get the maximum degree of separation
+import os, sys
+import networkx as nx
+#import util_networkx
+from networkx.algorithms.distance_measures import diameter, eccentricity
+
+def build_network_from_file(edge_fname):
+ G = nx.DiGraph()
+ source_nodes = []
+ f = open(edge_fname)
+ for line in f:
+ line = line.strip()
+ lst = line.split('\t')
+ if line != '':
+ strength = float(lst[8])
+ method_or_tissue = lst[9]
+ g1 = lst[0].split()[0] # target gene ID
+ g1_label = lst[0].split()[1].split(';')[0] if lst[0].split()[1] != '.' else g1
+ g1_name = lst[0].split()[1] if lst[0].split()[1] != '.' else ''
+ g2 = lst[1].split()[0] # source gene ID
+ g2_label = lst[1].split()[1].split(';')[0] if lst[1].split()[1] != '.' else g2
+ g2_name = lst[1].split()[1] if lst[1].split()[1] != '.' else ''
+ G.add_node(g1, full_name=g1_name, label=g1_label) # if g1 is also a TF, then istf='0' will overwrite it in the following for loop
+ G.add_node(g2, full_name=g2_name, label=g2_label) # tf_category contains default TF category code. It can be modified later given user's input
+
+ G.add_edge(g2, g1, weight=float(lst[2]), strength=strength, method=method_or_tissue) # g2 is source, and g1 is target
+ source_nodes.append(g2)
+
+ f.close()
+
+ source_nodes = list(set(source_nodes))
+ return G, source_nodes
+
+
+## main
+print('Load graph...')
+G, source_nodes = build_network_from_file(sys.argv[1])
+print('Convert to undirected...')
+G2 = G.to_undirected()
+print('Compute eccentricity...')
+for node in G2.nodes():
+ d = eccentricity(G2,v=node)
+ print('%s\t%d' % (node, d))
+