-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNP.py
More file actions
230 lines (194 loc) · 7.68 KB
/
NP.py
File metadata and controls
230 lines (194 loc) · 7.68 KB
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
# -*- coding: utf-8 -*-
import os
import random
import numpy as np
import numpy.ma as ma
import networkx as nx
from matplotlib.pyplot import plot
# ========================= Distance ========================= #
def Distance(SD, modFrom, modTo=None):
if modTo is None:
return SD[np.ix_(modFrom, modFrom)].min(1).mean()
else:
return SD[np.ix_(modFrom, modTo)].min(1).mean()
def Shortest(SD, mod1, mod2):
return SD[np.ix_(mod1, mod2)].mean()
def Closest(SD, mod1, mod2):
sub = SD[np.ix_(mod1, mod2)]
closest1 = sub.min(0)
closest2 = sub.min(1)
return (closest1.sum() + closest2.sum()) / (closest1.count() + closest2.count())
def Separation(SD, mod1, mod2):
dAA = Distance(SD, mod1)
dBB = Distance(SD, mod2)
dAB = Closest(SD, mod1, mod2)
return dAB - (dAA + dBB) / 2
def Center(SD, mod1, mod2):
c1 = GetCenterNodes(SD, mod1)
c2 = GetCenterNodes(SD, mod2)
return SD[np.ix_(c1, c2)].mean()
def GetCenterNodes(SD, mod):
sum_ = SD[np.ix_(mod, mod)].sum(1)
return [mod[idx] for idx, isCenter in enumerate(sum_ == sum_.min()) if isCenter] # bool(numpy.ma.core.MaskedConstant) => False
def Kernel(SD, mod1, mod2):
sub = SD[np.ix_(mod1, mod2)]
exp = ma.exp(-sub)
alb = ma.log(exp.sum(1))
bla = ma.log(exp.sum(0))
nA = alb.count()
nB = bla.count()
return (nA * np.log(nB) + nB * np.log(nA) - alb.sum() - bla.sum()) / (nA + nB) + 1
# ========================= Network ========================== #
class Network(object):
DISTANCE_MEASURE = {"DISTANCE" : Distance,
"SHORTEST" : Shortest,
"CLOSEST" : Closest,
"SEPARATION": Separation,
"CENTER" : Center,
"KERNEL" : Kernel}
def __init__(self, pathG, pathSD):
self.G = nx.read_adjlist(pathG)
self.SD = np.load(pathSD, allow_pickle=True)
self.nodes = sorted(self.G.nodes())
self.i2n = {index: node for index, node in enumerate(self.nodes)}
self.n2i = {node: index for index, node in enumerate(self.nodes)}
self.i2d = {} # index: degree
self.d2i = {} # degree: [index1, index2, ...]
for node, degree in self.G.degree():
index = self.n2i[node]
if degree in self.d2i:
self.d2i[degree].append(index)
else:
self.d2i[degree] = [index]
self.i2d[index] = degree
self.dmin = min(self.d2i.keys())
self.dmax = max(self.d2i.keys())
self.d2b = {} # degree: bin
self.b2i = {} # bin: [index1, index2, ...]
def Name2Index(self, names, skipUnknown=True):
if skipUnknown:
return [self.n2i[n] for n in names if n in self.n2i]
else:
return [self.n2i[n] for n in names]
def Index2Name(self, indexes, skipUnknown=True):
if skipUnknown:
return [self.i2n[i] for i in indexes if i in self.i2n]
else:
return [self.i2n[i] for i in indexes]
# ------------------------------------------
def PrepareBins(self, binSize=150):
index = 0
self.d2b = {}
self.b2i[index] = []
degrees = sorted(self.d2i.keys())
for curr, next in zip(degrees, degrees[1:] + [degrees[-1] + 1]):
for d in range(curr, next):
self.d2b[d] = index
self.b2i[index].extend(self.d2i[curr])
if curr != degrees[-1] and len(self.b2i[index]) >= binSize:
index += 1
self.b2i[index] = []
if len(self.b2i[index]) < binSize and index > 0: # Merge last two bins if last bin < binSize
for d in range(degrees[-1], -1, -1):
if self.d2b[d] != index:
break
self.d2b[d] = index - 1
self.b2i[index - 1].extend(self.b2i[index])
del self.b2i[index]
def DegreeMimicSampling(self, indexes):
binCount = {}
for index in indexes:
d = self.i2d[index]
if d < self.dmin:
d = self.dmin
elif d > self.dmax:
d = self.dmax
b = self.d2b[d]
if b in binCount:
binCount[b] += 1
else:
binCount[b] = 1
while True: # TODO ValueError
yield sum([random.sample(self.b2i[b], binCount[b]) for b in sorted(binCount.keys())], [])
def TotalRandomSampling(self, n):
indexes = list(range(len(self.nodes)))
while True:
yield random.sample(indexes, n)
# ------------------------------------------
def Proximity(self, mod1, mod2, method="DISTANCE"):
return self.DISTANCE_MEASURE[method](self.SD, mod1, mod2)
def ProximityRandom(self, mod1, mod2, method="DISTANCE", repeat=1000, seed=None):
if seed is not None:
random.seed(seed)
method = self.DISTANCE_MEASURE[method]
result = np.zeros(repeat)
index = 0
for mod1r, mod2r in zip(self.DegreeMimicSampling(mod1), self.DegreeMimicSampling(mod2)):
v = method(self.SD, mod1r, mod2r)
if not ma.is_masked(v):
result[index] = v
index += 1
if index == repeat:
break
return result
def LCC(self, mod):
return len(max(nx.connected_components(self.G.subgraph(self.Index2Name(mod))), key=len))
def LCCRandom(self, mod, repeat=1000, seed=None):
if seed is not None:
random.seed(seed)
result = np.zeros(repeat)
index = 0
for modr in self.DegreeMimicSampling(mod):
result[index] = self.LCC(modr)
index += 1
if index == repeat:
break
return result
# ========================= Utility ========================== #
def Tsv2Adj(pathIn, pathOut, encoding="utf-8", removeSelfloop=True):
G = nx.Graph()
with open(pathIn, encoding=encoding) as fi:
for line in fi:
if not line.startswith("#"):
G.add_edge(*line.strip().split("\t")[0:2])
if removeSelfloop:
G.remove_edges_from(nx.selfloop_edges(G))
nx.write_adjlist(G, pathOut)
return G
def Z_Score(real, background):
m = background.mean()
s = background.std(ddof=1)
z = (real - m) / s
p = np.mean(background < real) # left
return z, p
# ========================= ======== ========================= #
def ToDict(fp, Net):
mapping = {}
with open(fp) as fi:
for line in fi:
k, v = line.split()
if v in Net.nodes:
if k in mapping:
mapping[k].append(Net.n2i[v])
else:
mapping[k] = [Net.n2i[v]]
return mapping
if __name__ == "__main__":
FILE1 = "drug.txt"
FILE2 = "PDL1.txt"
OUTPUT = "PDL1_result_1k.txt"
REPEAT = 1000
MEASURE = "CLOSEST"
Net = Network("HumanInteractome.adj", "HumanInteractome.npy")
Net.PrepareBins(100)
MAPPING1 = ToDict(FILE1, Net)
MAPPING2 = ToDict(FILE2, Net)
with open(OUTPUT, "w") as fo:
for k1 in sorted(MAPPING1.keys()):
for k2 in sorted(MAPPING2.keys()):
g1 = MAPPING1[k1]
g2 = MAPPING2[k2]
d = Net.Proximity(g1, g2, method=MEASURE)
b = Net.ProximityRandom(g1, g2, method=MEASURE, repeat=REPEAT, seed=1024)
z, p = Z_Score(d, b)
fo.write("%s\t%s\t%.3f\t%.3f\t%.3f\n" % (k1, k2, d, z, p))