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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# Usage: python draw_subnetwork.py edges.txt
# Purpose: draw a sub-network given a list of genes.
# Created on 5 December 2019 by Hui Lan (lanhui@zjnu.edu.cn)
import os, sys
import networkx as nx
import pylab as plt
import glob
import math
from networkx.algorithms.distance_measures import diameter, eccentricity
def build_network_from_file(edge_fname, gene_lst, gene_dict):
G = nx.DiGraph()
for g in gene_lst:
G.add_node(gene_dict[g])
f = open(edge_fname)
for line in f:
line = line.strip()
lst = line.split('\t')
if len(lst) == 10:
g1 = lst[0].split()[0] # target gene ID
g2 = lst[1].split()[0] # source gene ID
strength = float(lst[8])
method_or_tissue = lst[9]
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_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 ''
if g1 in gene_lst and g2 in gene_lst:
G.add_node(gene_dict[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(gene_dict[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(gene_dict[g2], gene_dict[g1], weight=strength, strength=strength, method=method_or_tissue) # g2 is source, and g1 is target
f.close()
return G
def compute_total_edge_weight(edges, G):
total = 0
for e in edges:
u = e[0]
v = e[1]
total += G[u][v]['weight']
return total
def draw_graph(G, fname):
pos=nx.circular_layout(G)
tau = 2.5
elarge=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] >tau]
esmall=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] <=tau]
labels = {}
for (n,d) in G.nodes(data=True):
if 'label' in d:
labels[n] = d['label']
else:
labels[n] = n
nx.draw_networkx_nodes(G,pos,alpha=0.1)
nx.draw_networkx_edges(G,pos,edgelist=elarge,width=1,alpha=0.2)
nx.draw_networkx_edges(G,pos,edgelist=esmall,width=1,alpha=0.1,edge_color='k',style='dashed')
nx.draw_networkx_labels(G,pos,font_size=8,font_color='k',font_family='sans-serif')
plt.axis('off')
plt.savefig(fname) # save as png
#plt.show() # display
def better_date(s):
if len(s) == 8:
return '-'.join([s[:4], s[4:6], s[6:]])
else:
return s
def draw_graph2(G, fname, date):
pos=nx.circular_layout(G)
all_edges = []
all_widths = []
for (u, v, d) in G.edges(data=True):
all_edges.append((u, v))
all_widths.append(math.sqrt(d['weight']))
nx.draw_networkx_nodes(G,pos,alpha=0.05)
nx.draw_networkx_edges(G,pos,edgelist=all_edges,width=all_widths,alpha=0.05,edge_color='k',style='dashed')
nx.draw_networkx_labels(G,pos,font_size=11,font_color='b',font_family='sans-serif')
plt.axis('off')
plt.title(better_date(date))
plt.savefig(fname) # save as png
#plt.show() # display
## main
thermomorphogenesis_genes = [
'AT4G28720',
'AT2G25930',
'AT2G40080',
'AT3G46640',
'AT5G11260',
'AT2G43010',
'AT3G59060',
'AT4G10180',
'AT2G32950',
'AT3G13550',
'AT4G05420',
'AT4G21100',
'AT2G46340',
'AT4G11110',
'AT3G15354',
'AT1G53090',
'AT1G02340',
'AT4G08920',
'AT4G39950',
'AT2G22330',
'AT2G42870',
'AT5G39860',
'AT1G70560',
'AT3G62980',
'AT4G03190',
'AT3G26810',
'AT1G12820',
'AT4G24390',
'AT5G49980',
'AT5G01830',
'AT5G18010',
'AT5G18020',
'AT5G18050',
'AT5G18060',
'AT5G18080',
'AT1G29440',
'AT1G29510',
'AT4G18710',
'AT1G75080',
'AT1G30330',
'AT1G19850',
'AT3G33520',
'AT4G16280',
'AT2G43060',
'AT2G18300',
'AT4G16780',
'AT1G01060',
'AT1G22770',
'AT4G25420',
'AT1G15550',
'AT1G78440',
'AT5G43700',
'AT4G32280',
'AT2G38120',
'AT1G15580',
]
thermomorphogenesis_genes_small = [
'AT2G43010', #PIF4
'AT5G11260', #HY5
'AT2G42870', #PAR1
'AT5G39860', #PRE1
'AT4G16280', #FCA
'AT2G43060', #IBH1
'AT2G18300', #HBI1
'AT4G28720', #YUC8
'AT1G70560', #TAA1
'AT1G30330', #ARF6
'AT1G19850', #ARF5
'AT1G75080', #BZR1
'AT2G25930', #ELF3
'AT2G40080', #ELF4
'AT3G46640', #LUX
]
gene_dict = {
'AT2G43010':'PIF4',
'AT5G11260':'HY5',
'AT2G42870':'PAR1',
'AT5G39860':'PRE1',
'AT4G16280':'FCA',
'AT2G43060':'IBH1',
'AT2G18300':'HBI1',
'AT4G28720':'YUC8',
'AT1G70560':'TAA1',
'AT1G30330':'ARF6',
'AT1G19850':'ARF5',
'AT1G75080':'BZR1',
'AT2G25930':'ELF3',
'AT2G40080':'ELF4',
'AT3G46640':'LUX'
}
print('Make sub graph...')
graph_lst = []
graph_names = []
for fname in sorted(glob.glob('../Analysis/edges.txt.2019*')):
if fname == '../Analysis/edges.txt.20190801':
continue
print(fname)
graph_names.append(fname)
graph_lst.append(build_network_from_file(fname, thermomorphogenesis_genes_small, gene_dict))
#G1205 = build_network_from_file('../Analysis/edges.txt.20191205', thermomorphogenesis_genes)
#G1203 = build_network_from_file('../Analysis/edges.txt.20191203', thermomorphogenesis_genes)
#G1126 = build_network_from_file('../Analysis/edges.txt.20191126', thermomorphogenesis_genes)
#G1108 = build_network_from_file('../Analysis/edges.txt.20191108', thermomorphogenesis_genes)
for i in range(len(graph_lst)-1):
G1 = graph_lst[i]
G2 = graph_lst[i+1]
print(nx.is_isomorphic(G1, G2))
print('Graph 1 from %s' % (graph_names[i]))
e1 = G1.edges(data=True)
n1 = len(e1)
tw1 = compute_total_edge_weight(e1, G1)
print('Number of edges is %d' % (n1))
if n1 > 0:
print('Total edge association strength is %4.2f (avg=%4.2f).' % (tw1, tw1/n1))
#print(e1)
print('Graph 2 from %s' % (graph_names[i+1]))
e2 = G2.edges(data=True)
n2 = len(e2)
tw2 = compute_total_edge_weight(e2, G2)
print('Number of edges is %d' % (n2))
if n2 > 0:
print('Total edge association strength is %4.2f (avg=%4.2f).' % (tw2, tw2/n2))
#print(e2)
ged = nx.algorithms.similarity.graph_edit_distance(G1, G2)
print(ged)
print('------------------------------------------------------------------------')
draw_graph2(G1, '../Data/temp/graph-%s.png' % (graph_names[i].split('.')[-1]), graph_names[i].split('.')[-1])
|