-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlmsa.py
312 lines (235 loc) · 11.3 KB
/
tlmsa.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
'''
@author: onurdogan
'''
from bioservices import *
import pandas as pd
import numpy as np
from sumonet.utils.encodings import Encoding
from sumonet.model.architecture import SUMOnet
class TLMSA:
def __init__(self, df):
self.data = df
self.new_seqCol = 'mutated_seq'
self.subseqCol = 'subseq'
def get_sequence(self, uniprotCol):
self.data['seq'] = np.nan
u = UniProt()
unique_uniprotid = self.data[uniprotCol].unique()
print(len(unique_uniprotid), 'unique uniprotID detected')
print('Started retrieving sequence from Uniprot!')
counter = 0
for j in unique_uniprotid:
df_nn = self.data.loc[self.data[uniprotCol] == j]
sequence2 = u.search(str(j), columns="sequence")
for i in range(len(df_nn)):
self.data.loc[self.data[uniprotCol] == j, ('seq')] = str(sequence2[9:-1])
counter = counter + 1
if (counter % 100 == 0):
print(len(unique_uniprotid) - counter, 'left')
def get_mutated_sequence(self, caseIdCol, HugoSymbolCol, PositionCol, seqCol, aaNameCol,new_seqCol=''):
self.data[new_seqCol] = np.nan
if new_seqCol!='':
self.new_seqCol=new_seqCol
self.PositionCol=PositionCol
self.aaNameCol=aaNameCol
unique_case_id = self.data[caseIdCol].unique()
for i in unique_case_id:
df_n = self.data.loc[self.data[caseIdCol] == i]
unique_genes_case_id = df_n[HugoSymbolCol].unique()
for j in unique_genes_case_id:
df_nn = df_n.loc[self.data[HugoSymbolCol] == j]
df_nn = df_nn.reset_index(drop=True)
seq = list(df_nn[seqCol][0])
for k in range(int(len(df_nn))):
n_pos = int(int(df_nn[PositionCol][k]))
if n_pos > len(seq):
print('position is out of sequence')
continue
else:
seq[int(n_pos) - 1] = df_nn[aaNameCol][k]
new_seq = "".join(seq)
for s in range(int(len(df_nn))):
self.data.loc[(self.data[caseIdCol] == i) & (self.data[HugoSymbolCol] == j), self.new_seqCol] = new_seq
def get_subsequence(self, aaNameCol='', PositionCol='',new_seqCol='',subseqCol=''):
if aaNameCol!='':
self.aaNameCol=aaNameCol
if PositionCol!='':
self.PositionCol=PositionCol
if new_seqCol!='':
self.new_seqCol=new_seqCol
if subseqCol!='':
self.subseqCol=subseqCol
self.data[self.subseqCol] = np.nan
count_row = self.data.shape[0]
for i in range(count_row):
df_n = self.data.iloc[[i]]
if df_n[self.aaNameCol][i] == 'K':
pos = df_n[self.PositionCol]
seq = str(df_n[self.new_seqCol].values)[2:-2]
len_seq = len(seq)
if len_seq < int(pos):
continue
else:
if int(pos) < 11:
a = seq[0:int(pos) + 10]
for s in range(int(11 - pos)):
a = "X" + a
elif int((len_seq - int(pos))) < 11:
a = seq[int(pos) - 11:]
for s in range(int(10 - (len_seq - int(pos)))):
a = a + "X"
else:
a = seq[int(pos) - 11:int(pos) + 10]
self.data.loc[int(i), self.subseqCol] = str(a)
else:
continue
def remove_nonK_aa(self, aaNameCol=''):
if aaNameCol!='':
self.aaNameCol=aaNameCol
self.data=self.data[self.data[self.aaNameCol]=='K']
self.data=self.data.reset_index(drop=True)
def get_motif(self,subseqCol=''):
if subseqCol!='':
self.subseqCol=subseqCol
self.data['motif'] = np.nan
self.data['motif_names'] = np.nan
for i in range(self.data.shape[0]):
a, b, c = self.motif_predict(self.data[self.subseqCol][i])
self.data['motif'][i] = c
self.data['motif_names'][i] = b
def get_sumonet_res(self,subseqCol='',EncoderType='blosum62'):
if subseqCol!='':
self.subseqCol=subseqCol
x=self.data[self.subseqCol].values.tolist()
encoder = Encoding(encoderType=EncoderType)
x_Test = encoder.encode_data(x)
input_shape=x_Test.shape
SUMOnet3_model = SUMOnet()
SUMOnet3_model.build(input_shape)
SUMOnet3_model.load_weights()
y_preds = SUMOnet3_model.predict(x_Test)
self.data['nonSumo_prob']=np.nan
self.data['Sumo_prob']=np.nan
for i in range(len(x)):
self.data['nonSumo_prob'][i]=y_preds[i][0]
self.data['Sumo_prob'][i]=y_preds[i][1]
def motif_discover(self, motif):
motif_inds = []
motif_check = False
y1 = ['I','L','V']
y2 = ['A','F','I','L','M','P','V','W']
y3 = ['A','F','G','I','L','M','P','V','W','Y']
y4 = ['A','F','G','I','L','P','V']
alpha = ['D','E']
pS = ['S','T']
if motif[9] in y1 and motif[12] in alpha: #motif1
motif_inds.append(1)
motif_check = True
if motif[9] in y2 and motif[12] in alpha:#motif2
motif_inds.append(2)
motif_check = True
if motif[9] in y3 and motif[12] in alpha:#motif3
motif_inds.append(3)
motif_check = True
if motif[9] in y2 and motif[12] in alpha and motif[15] == 'S' and motif[16] == 'P' :#motif4
motif_inds.append(4)
motif_check = True
if motif[9] in y2 and motif[12] in alpha: #motif5
specific_motif = motif[14:-1]
motif_counter = 0
for aa in specific_motif:
if aa in alpha:
motif_counter += 1
if motif_counter >= 2:
motif_inds.append(5)
motif_check = True
if motif[7] in y4 and motif[8] in y4 and motif[9] in y4 and motif[12] == 'E':#motif6
motif_inds.append(6)
motif_check = True
if motif[8] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[13] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[8] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[14] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[8] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[15] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[8] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[16] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[7] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[13] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[7] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[14] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[7] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[15] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[7] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[16] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[6] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[13] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[6] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[14] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[6] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[15] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[6] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[16] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[5] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[13] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[5] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[14] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[5] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[15] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[5] in ['P','G'] and motif[9] in ['I','V'] and motif[12]=='E' and motif[16] in ['P','G']:#motif7
motif_inds.append(7)
motif_check = True
if motif[9] in ['I','V'] and motif[12]=='E' and motif[13] == 'P':#motif8 -> expand
motif_inds.append(8)
motif_check = True
if motif[9] in ['I','V'] and motif[12]=='E' and motif[14] == 'P':#motif8 -> expand
motif_inds.append(8)
motif_check = True
if motif[9] in ['I','V'] and motif[12]=='E' and motif[15] == 'P':#motif8 -> expand
motif_inds.append(8)
motif_check = True
if motif[9] in ['I','V'] and motif[12]=='E' and motif[16] == 'P':#motif8 -> expand
motif_inds.append(8)
motif_check = True
if motif[9] in y2 and motif[12] in alpha and motif[13] == 'P':#motif9
motif_inds.append(9)
motif_check = True
if motif[9] in y2 and motif[12] == 'S' and motif[13] == 'P':#motif10
motif_inds.append(10)
motif_check = True
if motif[8] in alpha and motif[11] in y1:#motif11
motif_inds.append(11)
motif_check = True
if motif[8] in alpha and motif[11] in y2:#motif12
motif_inds.append(12)
motif_check = True
if motif[8] in alpha and motif[11] in y3:#motif13
motif_inds.append(13)
motif_check = True
if motif_check == False:
motif_inds.append(0)
return motif_inds
def motif_predict(self, seq):
motif_names = ['non_motif','motif1','motif2','motif3','motif4','motif5','motif6',
'motif7','motif8','motif9','motif10','motif11','motif12','motif13']
motifIndices = self.motif_discover(seq)
if 0 not in motifIndices: #non motif
motifs = list(motif_names[i] for i in motifIndices)
return (seq,motifs,1)
else:
return (seq,['non_motif'],0)