-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathbuild_features.py
312 lines (239 loc) · 10.4 KB
/
build_features.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
import sys, os
import pandas as pd
import random
import numpy as np
# sys.path.append(os.path.abspath("C:/Users/yinr0002/Google Drive/Tier_2_MOE2014/2_Conference/CIKM2019/code/influenza-master"))
from trigram import Trigram
import validation
def sample_strains(strains_by_year, num_of_samples):
"""
Randomly picks num_of_samples strains from each year,
sampling is done with replacement.
Returns a 2d list of strings.
"""
sampled_strains_by_year = []
for year_strains in strains_by_year:
sampled_strains_by_year.append(random.choices(year_strains, k=num_of_samples))
return sampled_strains_by_year
def sample_strains_cluster(strains_by_year, num_of_samples):
"""
Picks num_of_samples strains from each year after clustering
sampling is done with replacement.
Returns a 2d list of strings.
"""
sampled_strains_by_year = []
# i = random.randint(0, 100)
for year_strains in strains_by_year:
sampled_strains_by_year.append(year_strains[:num_of_samples])
return sampled_strains_by_year
def split_to_trigrams(strains_by_year, overlapping=True):
"""
Splits the strains into trigrams, by default overlapping.
If non-overlapping approach is used, the last amino acids are padded to make
an extra trigram if the strain length is not evenly divisible by three.
Expects a 2d [year, strain] list of strings,
returns a 3d [year, strain, trigram] list of Trigram objects.
"""
if overlapping:
step_size = 1
num_of_trigrams = len(strains_by_year[0][0]) - 2
else:
step_size = 3
num_of_trigrams = len(strains_by_year[0][0]) // step_size
trigrams_by_year = []
for year_strains in strains_by_year:
year_trigrams = []
for strain in year_strains:
strain_trigrams = []
for i in range(num_of_trigrams):
pos = i * step_size
trigram = Trigram(strain[pos:pos + 3], pos)
strain_trigrams.append(trigram)
remainder = len(strain) % step_size
if remainder > 0:
padding = '-' * (3 - remainder)
amino_acids = strain[-remainder:] + padding
trigram = Trigram(amino_acids, len(strain) - remainder)
strain_trigrams.append(trigram)
year_trigrams.append(strain_trigrams)
trigrams_by_year.append(year_trigrams)
return trigrams_by_year
def make_triplet_strains(strains_by_year, positions):
"""
Splits each strain into substrings of 'triplets' refering to 3 overlapping
trigrams (5 amino acids), centered at the given positions.
Expects and returns a 2d [year, strain] list of strings.
"""
triplet_strains_by_year = []
triplet_strain_margin = 2
for strains_in_year in strains_by_year:
triplet_strains_in_year = []
for strain in strains_in_year:
for p in positions:
if p < triplet_strain_margin:
padding_size = triplet_strain_margin - p
triplet_strain = '-' * padding_size + strain[:p + triplet_strain_margin + 1]
elif p > len(strain) - 1 - triplet_strain_margin:
padding_size = p - (len(strain) - 1 - triplet_strain_margin)
triplet_strain = strain[p - triplet_strain_margin:] + '-' * padding_size
else:
triplet_strain = strain[p - triplet_strain_margin:p + triplet_strain_margin + 1]
triplet_strains_in_year.append(triplet_strain)
triplet_strains_by_year.append(triplet_strains_in_year)
return triplet_strains_by_year
def make_triplet_labels(triplet_strains_by_year):
"""
Creates labels indicating whether the center amino acid in each triplet
mutates in the last year (1 for yes, 0 for no).
Expects a 2d [year, triplet] list of strings and returns a list of ints.
"""
num_of_triplets = len(triplet_strains_by_year[0])
epitope_position = 2
labels = []
for i in range(num_of_triplets):
if triplet_strains_by_year[-1][i][epitope_position] == triplet_strains_by_year[-2][i][epitope_position]:
labels.append(0)
else:
labels.append(1)
return labels
def get_majority_baselines(triplet_strains_by_year, labels):
"""
Returns accuracy, precision, recall, f1-score and mcc for the baseline
approach of simply predicting mutation epitope in the last year differs
from the majority one.
"""
epitope_position = 2
predictions = []
for i in range(len(labels)):
epitopes = []
for year in range(len(triplet_strains_by_year) - 1):
epitopes.append(triplet_strains_by_year[year][i][epitope_position])
majority_epitope = max(set(epitopes), key=epitopes.count)
if triplet_strains_by_year[-2][i][epitope_position] == majority_epitope:
predictions.append(0)
else:
predictions.append(1)
conf_matrix = validation.get_confusion_matrix(np.array(labels), np.array(predictions))
acc = validation.get_accuracy(conf_matrix)
precision = validation.get_precision(conf_matrix)
recall = validation.get_recall(conf_matrix)
f1score = validation.get_f1score(conf_matrix)
mcc = validation.get_mcc(conf_matrix)
return acc, precision, recall, f1score, mcc
def extract_positions_by_year(positions, trigrams_by_year):
"""
Extracts trigrams that contain an amino acid from one of the given positions.
Expects and returns a 3d [year, strain, trigram] list of Trigram objects.
"""
strain = trigrams_by_year[0][0]
strain_idxs_to_extract = []
idx = 0
for pos in positions:
pos_found = False
while not pos_found:
trigram = strain[idx]
if trigram.contains_position(pos):
pos_found = True
else:
idx += 1
pos_extracted = False
while not pos_extracted:
trigram = strain[idx]
if trigram.contains_position(pos):
strain_idxs_to_extract.append(idx)
idx += 1
else:
pos_extracted = True
def extract_idxs(strain_trigrams):
return [strain_trigrams[i] for i in strain_idxs_to_extract]
extracted_by_year = []
for year_trigrams in trigrams_by_year:
extracted_by_year.append(list(map(extract_idxs, year_trigrams)))
return extracted_by_year
def squeeze_trigrams(trigrams_by_year):
"""
Takes a 3d [year, strain, trigram] list and squeezes the 2nd dimension
to return a 2d list [year, trigram].
"""
squeezed_trigrams_by_year = []
for year_trigrams in trigrams_by_year:
squeezed_trigrams = []
for trigrams in year_trigrams:
squeezed_trigrams += trigrams
squeezed_trigrams_by_year.append(squeezed_trigrams)
return squeezed_trigrams_by_year
def replace_uncertain_amino_acids(amino_acids):
"""
Randomly selects replacements for all uncertain amino acids.
Expects and returns a string.
"""
replacements = {'B': 'DN',
'J': 'IL',
'Z': 'EQ',
'X': 'ACDEFGHIKLMNPQRSTVWY'}
for uncertain in replacements.keys():
amino_acids = amino_acids.replace(uncertain, random.choice(replacements[uncertain]))
return amino_acids
def map_trigrams_to_idxs(nested_trigram_list, trigram_to_idx):
"""
Takes a nested list containing Trigram objects and maps them to their index.
"""
dummy_idx = len(trigram_to_idx)
def mapping(trigram):
if isinstance(trigram, Trigram):
trigram.amino_acids = replace_uncertain_amino_acids(trigram.amino_acids)
if '-' not in trigram.amino_acids:
return trigram_to_idx[trigram.amino_acids]
else:
return dummy_idx
elif isinstance(trigram, list):
return list(map(mapping, trigram))
else:
raise TypeError('Expected nested list of Trigrams, but encountered {} in recursion.'.format(type(trigram)))
return list(map(mapping, nested_trigram_list))
def map_idxs_to_vecs(nested_idx_list, idx_to_vec):
"""
Takes a nested list of indexes and maps them to their trigram vec (np array).
"""
# represent the 3-grams containing '-' by zero vector in ProVect
# dummy_vec = np.array([0] * idx_to_vec.shape[1])
# represent the 3-grams containing '-' by 'unknown' vector in ProVect
dummy_vec = idx_to_vec[idx_to_vec.shape[0] - 1]
def mapping(idx):
if isinstance(idx, int):
if idx < idx_to_vec.shape[0]:
return idx_to_vec[idx]
else:
return dummy_vec
elif isinstance(idx, list):
return list(map(mapping, idx))
else:
raise TypeError('Expected nested list of ints, but encountered {} in recursion.'.format(type(idx)))
return list(map(mapping, nested_idx_list))
def get_diff_vecs(trigram_vecs_by_year):
"""
Calculates the elementwise difference between each consecutive trigram vec.
Expects numpy array.
"""
diff_vecs_by_year = np.zeros(
(trigram_vecs_by_year.shape[0] - 1, trigram_vecs_by_year.shape[1], trigram_vecs_by_year.shape[2]))
for i in range(diff_vecs_by_year.shape[0]):
diff_vecs_by_year[i] = trigram_vecs_by_year[i + 1] - trigram_vecs_by_year[i]
return diff_vecs_by_year
def indexes_to_mutations(trigram_indexes_x, trigram_indexes_y):
"""
Creates an numpy array containing 1's in positions where trigram_indexes_x and
trigram_indexes_y differ, corresponding to mutated sites and zeros elsewhere.
"""
assert (len(trigram_indexes_x) == len(trigram_indexes_y))
mutations = np.zeros(len(trigram_indexes_x))
for i in range(len(trigram_indexes_x)):
if trigram_indexes_x[i] != trigram_indexes_y[i]:
mutations[i] = 1
return mutations
def reshape_to_linear(vecs_by_year, window_size=3):
reshaped = [[]] * len(vecs_by_year[0])
for year_vecs in vecs_by_year[-window_size:]:
for i, vec in enumerate(year_vecs):
reshaped[i] = reshaped[i] + vec.tolist()
return reshaped