-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDDN.py
337 lines (265 loc) · 12.7 KB
/
DDN.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#ddn
import sys
sys.path.append('.')
lambda1 = 0.3
lambda2 = 0.05
import os
import math
import numpy as np
from tqdm import tqdm
## TODO: auto install sklearn or write our own standarscaler
from sklearn.preprocessing import StandardScaler
import networkx as nx
import matplotlib.pyplot as plt ### matplotlib==2.2.3
from pandas.core.frame import DataFrame
class DDN:
def __init__(self):
print("DNN package")
def readGeneName(self, filename):
with open(filename, 'r') as file:
genename = file.read().split('\n')
while len(genename) > 0 and genename[-1] == '':
genename.pop()
return genename
def readGeneData(self, filename):
genedata = np.loadtxt(filename)
genedata = genedata.transpose()
return genedata
def standardizeGeneData(self, genedata, scaler='rms', zero_mean=True):
# sample standardization : z = (x - u) / s
standarddata = np.zeros(genedata.shape)
for i in range(genedata.shape[1]):
# mean value
u = np.mean(genedata[:, i]) if not zero_mean else 0
if scaler == 'std':
# standard deviation
s = np.std(genedata[:, i])
elif scaler == 'rms':
# root mean square
s = np.sqrt(np.mean(np.square(genedata[:, i])))
else:
s = 1
standarddata[:, i] = (genedata[:, i] - u) / s
return standarddata
def concatenateGeneData(self, controldata, casedata, method='diag'):
if method == 'row':
return np.concatenate((controldata, casedata), axis=0)
elif method == 'col':
return np.concatenate((controldata, casedata), axis=1)
elif method == 'diag':
return np.concatenate((np.concatenate((controldata, casedata * 0), axis=0),
np.concatenate((controldata * 0 ,casedata), axis=0)), axis=1)
else:
return []
def solve2d(self, rho1, rho2, lambda1, lambda2):
"""
description
input
output:
cite:
"""
# initialize output
area_index = 0
beta1 = 0
beta2 = 0
if (rho2 <= (rho1 + 2*lambda2) and rho2 >= (rho2 - 2*lambda2) and rho2 >= (2*lambda1 - rho1)):
area_index = 1
beta1 = (rho1 + rho2)/2 - lambda1
beta2 = (rho1 + rho2)/2 - lambda1
elif (rho2 > (rho1 + 2*lambda2) and rho1 >= (lambda1 - lambda2)):
area_index = 2
beta1 = rho1 - lambda1 + lambda2
beta2 = rho2 - lambda1 - lambda2
elif (rho1 < (lambda1 - lambda2) and rho1 >= -(lambda1 + lambda2) and rho2 >= (lambda1 + lambda2)):
area_index = 3
beta1 = 0
beta2 = rho2 - lambda1 - lambda2
elif (rho1 < -(lambda1 + lambda2) and rho2 >= (lambda1 + lambda2)):
area_index = 4
beta1 = rho1 + lambda1 + lambda2
beta2 = rho2 - lambda1 - lambda2
elif (rho1 < -(lambda1 + lambda2) and rho2 < (lambda1 + lambda2) and rho2 >= -(lambda1 + lambda2)):
area_index = 5
beta1 = rho1 + lambda1 + lambda2
beta2 = 0
elif (rho2 < -(lambda1 - lambda2) and rho2 >= (rho1 + 2*lambda2)):
area_index = 6
beta1 = rho1 + lambda1 + lambda2
beta2 = rho2 + lambda1 - lambda2
elif (rho2 >= (rho1 - 2*lambda2) and rho2 < (rho1 + 2*lambda2) and rho2 <= (-2*lambda1 - rho1)):
area_index = 7
beta1 = (rho1 + rho2)/2 + lambda1
beta2 = (rho1 + rho2)/2 + lambda1
elif (rho2 < (rho1 - 2*lambda2) and rho1 <= -(lambda1 - lambda2)):
area_index = 8
beta1 = rho1 + lambda1 - lambda2
beta2 = rho2 + lambda1 + lambda2
elif (rho1 <= (lambda1 + lambda2) and rho1 >= -(lambda1 - lambda2) and rho2 <= -(lambda1 + lambda2)):
area_index = 9
beta1 = 0
beta2 = rho2 + lambda1 + lambda2
elif (rho1 > (lambda1 + lambda2) and rho2 <= -(lambda1 + lambda2)):
area_index = 10
beta1 = rho1 - lambda1 - lambda2
beta2 = rho2 + lambda1 + lambda2
elif (rho2 > -(lambda1 + lambda2) and rho2 <= (lambda1 - lambda2) and rho1 >= (lambda1 + lambda2)):
area_index = 11
beta1 = rho1 - lambda1 - lambda2
beta2 = 0
elif (rho2 > (lambda1 - lambda2) and rho2 < (rho1 - 2*lambda2)):
area_index = 12
beta1 = rho1 - lambda1 - lambda2
beta2 = rho2 - lambda1 + lambda2
return [beta1, beta2]
def bcdResi(self, X1, X2, CurrIdx, lambda1, lambda2, threshold, max_iter):
if (X1.shape[1] != X2.shape[1]):
return []
p = X1.shape[1]
n1 = X1.shape[0]
n2 = X2.shape[0]
beta1 = np.zeros(p)
beta2 = np.zeros(p)
y1_resi = X1[:, CurrIdx]
y2_resi = X2[:, CurrIdx]
r = 0
k_last = CurrIdx
while True:
beta1_old = np.copy(beta1)
beta2_old = np.copy(beta2)
for i in range(p):
if i == CurrIdx:
continue
r = r + 1
k = i
y1_resi = y1_resi - beta1[k_last] * X1[:, k_last] + beta1[k] * X1[:, k]
y2_resi = y2_resi - beta2[k_last] * X2[:, k_last] + beta2[k] * X2[:, k]
rho1 = np.sum(y1_resi * X1[:, k]) / n1
rho2 = np.sum(y2_resi * X2[:, k]) / n2
beta2d = self.solve2d(rho1, rho2, lambda1, lambda2)
beta1[k] = beta2d[0]
beta2[k] = beta2d[1]
k_last = k
betaerr = np.mean(np.abs(np.concatenate([beta1 - beta1_old, beta2 - beta2_old])))
if (betaerr < threshold) or (r > max_iter):
break
beta = np.concatenate([beta1, beta2])
return beta
def generateDifferentialNetwork(self, case_data, control_data, genename, lambda1=lambda1, lambda2=lambda2, threshold=1e-6, max_iter=1e4):
# feature size (gene size)
p = control_data.shape[1]
# sample size
n1 = control_data.shape[0]
n2 = case_data.shape[0]
# start calculations
diffedges = {}
for gene in tqdm(range(p)):
# choose one gene as target
y = self.concatenateGeneData(control_data[:, gene], case_data[:, gene], method='row')
# choose other genes as feature
idx = [i for i in range(p) if i != gene]
X = self.concatenateGeneData(control_data[:, idx], case_data[:, idx], method='diag')
# perform bcd algorithm
beta = self.bcdResi(control_data, case_data, gene,lambda1, lambda2, threshold, max_iter)
# reindex the features
beta1 = np.array(beta[:p])
beta2 = np.array(beta[p:])
# construct neighbours under two conditions
condition1 = [genename[i] for i in range(p) if beta1[i] != 0 and beta2[i] == 0]
condition2 = [genename[i] for i in range(p) if beta2[i] != 0 and beta1[i] == 0]
weight1 = [beta1[i] for i in range(p) if beta1[i] != 0 and beta2[i] == 0]
weight2 = [beta2[i] for i in range(p) if beta2[i] != 0 and beta1[i] == 0]
# update results
for neighbors, weights, condition in zip([condition1, condition2], [weight1, weight2], ['condition1', 'condition2']):
for neighbor, weight in zip(neighbors, weights):
tuple_diffedge = (min(genename[gene], neighbor), max(genename[gene], neighbor), condition)
diffedges.setdefault(tuple_diffedge, 0.0)
diffedges[tuple_diffedge] += weight
diffedges = sorted([k + tuple([v]) for k, v in diffedges.items()])
return diffedges
def plotDifferentialNetwork(self, diffedges, maxalpha=1.0, minalpha=0.2):
G = nx.Graph()
color_condition = {'condition1': [1, 0, 0], 'condition2': [0, 0, 1]}
maxbeta = max([beta for _, _, _, beta in diffedges])
for gene1, gene2, condition, beta in diffedges:
if condition in color_condition:
alpha = beta / maxbeta * (maxalpha - minalpha) + minalpha
color = list(1 - (1 - np.array(color_condition[condition])) * alpha)
G.add_edge(gene1, gene2, color=color)
pos = nx.circular_layout(G)
#pos = nx.random_layout(G)
#pos = nx.spring_layout(G)
edges = G.edges()
edge_color = [G[u][v]['color'] for u, v in edges]
node_size = [d * 200 for n, d in G.degree()]
fig, ax = plt.subplots(figsize=(30, 30))
nx.draw(G, pos=pos, node_color='lightblue', node_size=node_size, \
edges=edges, edge_color=edge_color, width=3, \
with_labels=True, font_size=15, font_weight='normal', font_color='magenta')
ax.set_xlim((-1.2, +1.2))
ax.set_ylim(ax.get_xlim())
ax.set_title(''.join([
'DDN Network\n',
'\n',
'condition #1: red edge\n',
'condition #2: blue edge'
]))
plt.savefig('figure_differential_network.jpg')
def printDifferentialNetwork(self, diffedges, filename=''):
genes = [i[0] for i in diffedges]
neighbors = [i[1] for i in diffedges]
conditions = [i[2] for i in diffedges]
weights = [i[3] for i in diffedges]
# if len(filename) == 0:
# for gene, neighbor, condition, weight in diffedges:
# print(f"{gene},{neighbor},{condition},{weight}")
# else:
# with open(filename, 'w') as file:
# for gene, neighbor, condition, weight in diffedges:
# file.write(f"{gene},{neighbor},{condition},{weight}\r")
df = DataFrame({'gene': genes, 'neighbors': neighbors, 'conditions': conditions, 'weights': weights})
return df
def DDNPipline(self, case_data_file, control_data_file, gene_name_file, output_file='', lambda1=lambda1, lambda2=lambda2):
# import case data
casedata = self.readGeneData(case_data_file)
# import control data
controldata = self.readGeneData(control_data_file)
# import gene name
genename = self.readGeneName(gene_name_file)
# feature size must be equivalent
assert(casedata.shape[1] == controldata.shape[1])
# feature standardization
case_standard = self.standardizeGeneData(casedata)
control_standard = self.standardizeGeneData(controldata)
# generate differential network
diffedges = self.generateDifferentialNetwork(case_standard, control_standard, genename, lambda1, lambda2)
# print differential network
self.printDifferentialNetwork(diffedges, output_file)
return diffedges
def DDNBatchPipline(self, path='.', \
case_data_suffix='_case.txt', \
control_data_suffix='_control.txt', \
gene_name_suffix='_genename.txt', \
output_suffix='_differential_network.csv', \
lambda1=lambda1, lambda2=lambda2):
idx = 0
for file in os.listdir(path):
if file.endswith(gene_name_suffix):
gene_name_file = os.path.join(path, file)
prefix = file[:-len(gene_name_suffix)]
case_data_file = os.path.join(path, prefix+case_data_suffix)
control_data_file = os.path.join(path, prefix+control_data_suffix)
output_file = os.path.join(path, prefix+output_suffix)
if os.path.exists(case_data_file) and os.path.exists(control_data_file):
self.DDNPipline(case_data_file, control_data_file, \
gene_name_file, output_file, lambda1, lambda2)
idx += 1
print(f"Dataset#{idx} : {prefix} successfully processed!")
return
ddn = DDN()
neighbors = ddn.DDNPipline(case_data_file='case.txt', \
control_data_file='control.txt', \
gene_name_file='genename.txt', \
output_file='differential_network.csv', \
lambda1=lambda1, lambda2=lambda2)
# neighbors
ddn.plotDifferentialNetwork(neighbors)