reptb_nbi.py
2.52 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
# coding: utf-8
# In[1]:
import numpy as np
import csv
import sys
import numpy.matlib
dti_input = sys.argv[1]
fp = open(dti_input,'r')
drugid = []
targetid = []
##1. Reading edge list line by line##
for line in fp:
line = line.strip()
tmp = line.split(',')
drugid.append(tmp[0])
targetid.append(tmp[1])
fp.close()
##End 1##
drug = np.array(drugid)
target =np.array(targetid)
uni_drugid = np.unique(np.array(drugid))
uni_targetid = np.unique(np.array(targetid))
##creating zero incidence matrix for the graph##
A = np.zeros((uni_targetid.shape[0], uni_drugid.shape[0]))
for i in range(len(drugid)):
idx1 = np.where(uni_targetid==targetid[i])
idx2 = np.where(uni_drugid==drugid[i])
A[idx1,idx2] = 1
nd = uni_drugid.shape[0]
mt = uni_targetid.shape[0]
A_T = np.transpose(A)
no_edges = np.sum(A)
print nd, mt, A.shape, np.sum(A), A_T.shape
#NBI calculation for A
Ky = np.diag((1/sum(A)))
n = A.shape[0]
m = A.shape[1]
Ky[np.isinf(Ky) | np.isnan(Ky)] = 0
kx = np.transpose(np.sum(A,1))
Nx = np.matlib.repmat(1/kx,n,1)
Nx[np.isinf(Nx) | np.isnan(Nx)] = 0
W = np.transpose(np.dot(A, Ky))
W1 = np.dot(A, W)
W2 = np.multiply(Nx, W1)
print W2.shape
NBIscore = np.dot(W2, A)
print NBIscore.shape
#NBI calculation for A_T
Ky = np.diag((1/sum(A_T)))
n = A_T.shape[0]
m = A_T.shape[1]
Ky[np.isinf(Ky) | np.isnan(Ky)] = 0
kx = np.transpose(np.sum(A_T,1))
Nx = np.matlib.repmat(1/kx,n,1)
Nx[np.isinf(Nx) | np.isnan(Nx)] = 0
W = np.transpose(np.dot(A_T, Ky))
W1 = np.dot(A_T, W)
W2 = np.multiply(Nx, W1)
NBIscore_T = np.dot(W2, A_T)
##Normalizing NBI scores
NBIscore = np.true_divide(NBIscore, np.max(NBIscore, axis=0))
NBIscore_T = np.true_divide(NBIscore_T, np.max(NBIscore_T, axis=0))
nbi_idx = np.argsort(NBIscore, axis=0)
nbi_idx_T= np.argsort(NBIscore_T, axis=0)
nbi_score = sys.argv[2] # predicted DTI
wp = open(nbi_score,'w')
for d in range(nd):
idx1 = nbi_idx[:,d]
idx2 = A[:,d]
idx3 = idx2[idx1]
idx4 = np.where(idx3 == 0)[0]
p_targets_idx = idx1[idx4[-n:]]
p_targets_idx = p_targets_idx[::-1]
p_targets = NBIscore[p_targets_idx,d]
if p_targets[0] == 0.0:
continue
else:
p_diff = np.diff(p_targets)
th = p_targets[0]*0.20
th_f = p_targets[0]-th
f_idx = p_targets_idx[p_targets > th_f]
f_scores = p_targets[p_targets > th_f]
f_targets = uni_targetid[f_idx]
for i,t in enumerate(f_targets):
wp.write(uni_drugid[d] + ',' + t + ',' + str(f_scores[i]) + '\n')
wp.close()