-
Notifications
You must be signed in to change notification settings - Fork 0
/
Score_Calculator.py
234 lines (164 loc) · 6.75 KB
/
Score_Calculator.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
import copy
import xlsxwriter
import argparse
import numpy as np
import pandas as pd
#s sheet must include interacting-inner protein interactions with the sorted interacting protein IDS in the first column
#s3 must include the interactions between only the interacting proteins.
#In s3, the symmetrical interactions must be also listed, which means the row number is twice the number of interactions.
parser = argparse.ArgumentParser(description='PIN_Connectivity')
parser.add_argument('-i', type=int, help='Interacting protein number')
parser.add_argument('-t', type=int, help='Total protein number')
parser.add_argument('-f', type=str, help='File name')
parser.add_argument('-s1', type=str, help='Sheet of direct interactions of interacting proteins with non-interacting proteins')
parser.add_argument('-s2', type=str, help='Sheet of interactions of non-interacting proteins')
parser.add_argument('-s3', type=str, help='Sheet of interactions of interacting proteins')
parser.add_argument('-s4', type=str, help='Score sheet')
parser.add_argument('-cn', type=int, help='Top contributor number')
parser.add_argument('-r', type=str, help='Name of the result file')
args = parser.parse_args()
def Get_Connections(int_protein_number, di_sheet_name, oi_sheet_name ):
df = pd.read_excel(args.f, sheet_name=di_sheet_name, header=None)
M=df.values.tolist()
df2= pd.read_excel(args.f, sheet_name=oi_sheet_name, header=None)
I1=df2.values.tolist()
Interacting=list(range(1,int_protein_number+1))
C=[]
Final=[]
C0=[]
for i in range(0,len(Interacting)):
list1 = []
to_erase=[]
for j in M:
if j[0]>i+1:
break
if j[0]==i+1:
list1.append(int(j[1]))
to_erase.append(j)
C0.append(list1)
for a in to_erase:
M.remove(a)
for m in range(0,len(Interacting)):
count=1
C.clear()
C.append(C0[m])
t = 0
I=copy.deepcopy(I1)
while count>=1 and len(I)!=0:
new_list = []
count = 0
for k in C[t]:
remove_list=[]
for j in I:
if j[0]==k:
count+=1
new_list.append(int(j[1]))
remove_list.append(j)
elif j[1]==k:
count += 1
new_list.append(int(j[0]))
remove_list.append(j)
for element in remove_list:
I.remove(element)
new_list=list(dict.fromkeys(new_list))
C.append(new_list)
t+=1
for i in range(len(C)):
for item in C[i]:
if (m+1)!=item:
Final.append([str(m+1)+","+str(item),i+1])
return Final
def Score_Calculation(data, int_protein_number, type):
column_values = ['interaction', 'degree']
df = pd.DataFrame(data=data, columns=column_values)
table = pd.pivot_table(df, index='interaction', values='degree', aggfunc= np.min)
df_final=pd.DataFrame(table)
interaction_pairs=df_final.index.values
df_final=np.array(df_final)
array=[]
for i in range(0,len(interaction_pairs)):
separate_protein_ids=interaction_pairs[i].split(",")
array.append([int(separate_protein_ids[0]), int(separate_protein_ids[1]), df_final[i][0]])
array=np.array(array)
array = array[array[:, 0].argsort()]
processed_PR_scores=[]
contribution=[]
for i in range(0,len(interaction_pairs)):
processed_PR_scores.append([array[i][0],PR_Scores[array[i][1]-1]/array[i][2]])
if type==1:
contribution.append([array[i][0],array[i][1],float(PR_Scores[array[i][1]-1]/array[i][2])])
if type==1:
protein_id = 1
contributors=[]
#print(len(contribution))
#print(contribution[0][0])
i=0
while i<len(contribution):
contributors_row=[]
while contribution[i][0]==protein_id:
#print(i)
contributors_row.append([contribution[i][1],contribution[i][2]])
i+=1
if i>=len(contribution):
break
contributors_row=np.array(contributors_row)
contributors.append(contributors_row)
protein_id+=1
contributors=np.array(contributors)
#print("c",contributors)
top_contributors=[]
top_number=args.cn
for row in contributors:
#row=np.array(row)
#print("r",row)
if len(row)==0:
top_contributors.append([])
elif len(row)<top_number:
top_contributors.append(row[:,0])
else:
row = row[row[:, 1].argsort()[::-1]]
print(row)
top_contributors.append(row[:,0][:top_number])
top_contributors=np.array(top_contributors)
print("*",top_contributors)
column_values = ['InteractingProtein', 'Score']
df3 = pd.DataFrame(data=processed_PR_scores, columns=column_values)
table2 = pd.pivot_table(df3, index='InteractingProtein', values='Score', aggfunc= np.sum)
df_final=pd.DataFrame(table2)
ids=df_final.index.values
df_final=np.array(df_final)
array=[]
j = 0
for i in range(1,int_protein_number+1):
if i in ids:
array.append([i, df_final[j][0]])
j+=1
else:
array.append([i,0])
if type==1:
return array, top_contributors
else:
return array
df = pd.read_excel(args.f, sheet_name=args.s4, header=None)
PR_Scores= df.values.tolist()
print("Running")
Inner_connections=Get_Connections(args.i, args.s1, args.s2)
Interacting_protein_connections=Get_Connections(args.i, args.s3,args.s3)
array_1, top_contributors=Score_Calculation(Inner_connections, args.i, 1)
array_2=Score_Calculation(Interacting_protein_connections,args.i, 2)
workbook = xlsxwriter.Workbook(args.r)
sheet0 = workbook.add_worksheet("Scores")
sheet0.write(0,0,"Interacting Protein ID")
sheet0.write(0,1, "Overall Score")
for i in range(args.i):
sheet0.write(i+1, 0, i+1)
sheet0.write(i+1,1,array_1[i][1] + array_2[i][1])
sheet1=workbook.add_worksheet("Contributors")
sheet1.write(0,0,"Interacting Protein ID")
sheet1.write(0,1,"Intracellular Contributors")
for i in range(args.i):
sheet1.write(i+1,0,i+1)
for j in range(len(top_contributors[i])):
sheet1.write(i+1, j+1, top_contributors[i][j])
workbook.close()
print("Finished")