bio-shark 1.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bio_shark/VERSION +1 -0
- bio_shark/__init__.py +5 -0
- bio_shark/core/__init__.py +0 -0
- bio_shark/core/alf_scoring.py +33 -0
- bio_shark/core/sequence_pair_similarity.py +295 -0
- bio_shark/core/utils.py +120 -0
- bio_shark/data/dive_pred_model_v2.pkl +0 -0
- bio_shark/data/grantham.tsv +21 -0
- bio_shark/data/sample_fasta_file.fasta +20 -0
- bio_shark/data/substitution_matrix_grantham.tsv +21 -0
- bio_shark/dive/__init__.py +0 -0
- bio_shark/dive/prediction.py +193 -0
- bio_shark/dive/run.py +98 -0
- bio_shark/dive/visualize.py +185 -0
- bio_shark/exceptions.py +13 -0
- bio_shark/settings.py +13 -0
- bio_shark-1.2.1.dist-info/LICENSE +29 -0
- bio_shark-1.2.1.dist-info/METADATA +212 -0
- bio_shark-1.2.1.dist-info/RECORD +22 -0
- bio_shark-1.2.1.dist-info/WHEEL +5 -0
- bio_shark-1.2.1.dist-info/entry_points.txt +3 -0
- bio_shark-1.2.1.dist-info/top_level.txt +1 -0
bio_shark/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
1.2.1
|
bio_shark/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from .. import settings
|
|
2
|
+
|
|
3
|
+
from alfpy.utils.seqrecords import SeqRecords
|
|
4
|
+
from alfpy import word_distance
|
|
5
|
+
from alfpy import word_pattern
|
|
6
|
+
from alfpy import word_vector
|
|
7
|
+
|
|
8
|
+
ALLOWED_K_MAX = 6
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def get_google_distance_score(sequence1: str, sequence2: str, k: int) -> float:
|
|
12
|
+
"""
|
|
13
|
+
For two sequences, compute the Google distance similarity score
|
|
14
|
+
|
|
15
|
+
:param(str) sequence1: Sequence 1
|
|
16
|
+
:param(str) sequence2: Sequence 2
|
|
17
|
+
:param(int) k: K-Mer length
|
|
18
|
+
:return(float): Similarity Score
|
|
19
|
+
"""
|
|
20
|
+
if k >= ALLOWED_K_MAX:
|
|
21
|
+
print("Alf Google Distance score can only be computed for k <= {}".format(ALLOWED_K_MAX))
|
|
22
|
+
return
|
|
23
|
+
|
|
24
|
+
seq_records = SeqRecords()
|
|
25
|
+
seq_records.add('seq1', sequence1)
|
|
26
|
+
seq_records.add('seq2', sequence2)
|
|
27
|
+
|
|
28
|
+
pattern = word_pattern.create(seq_records.seq_list, word_size=k)
|
|
29
|
+
counts = word_vector.Counts(seq_records.length_list, pattern)
|
|
30
|
+
dist = word_distance.Distance(counts, 'google')
|
|
31
|
+
score = dist.pairwise_distance(0, 1)
|
|
32
|
+
rounded_score = round(score, settings.ROUND_OFF_DP)
|
|
33
|
+
return rounded_score
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from typing import Mapping, Tuple, List
|
|
3
|
+
|
|
4
|
+
TYPE_SCORE_MATRIX = Mapping[str, Mapping[str, float]]
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class SeqPairSimilarity:
|
|
8
|
+
def __init__(self, sequence1: str,
|
|
9
|
+
sequence2: str, k: int,
|
|
10
|
+
substitution_matrix: Tuple[TYPE_SCORE_MATRIX, float, float]):
|
|
11
|
+
"""
|
|
12
|
+
:param(str) sequence1: First Sequence
|
|
13
|
+
:param(str) sequence2: Second Sequence
|
|
14
|
+
:param(int) k: Length of k-mer
|
|
15
|
+
:param(dict[str, dict[str, float]], float, float) substitution_matrix: Subs matrix table, Max score, Min score
|
|
16
|
+
"""
|
|
17
|
+
self.sequence1 = sequence1
|
|
18
|
+
self.sequence2 = sequence2
|
|
19
|
+
self.k = k
|
|
20
|
+
if len(self.sequence1) < self.k or len(self.sequence2) < self.k:
|
|
21
|
+
print('Sequence 1 Length: {}; Sequence 2 Length: {}; K = {}'.format(
|
|
22
|
+
len(self.sequence1), len(self.sequence2), self.k))
|
|
23
|
+
raise Exception('K-Mer length should not be greater than sequence length')
|
|
24
|
+
self.substitution_matrix_obj = substitution_matrix
|
|
25
|
+
self.kmer_count_map_1 = {}
|
|
26
|
+
self.kmer_count_map_2 = {}
|
|
27
|
+
self.similarity_matrix = {}
|
|
28
|
+
|
|
29
|
+
@staticmethod
|
|
30
|
+
def get_length_difference(a: float, b: float) -> float:
|
|
31
|
+
"""
|
|
32
|
+
Length Difference function, often used for tie-breakers
|
|
33
|
+
|
|
34
|
+
:param(float) a: Count 1
|
|
35
|
+
:param(float) b: Count 2
|
|
36
|
+
:return(float): Length Difference
|
|
37
|
+
"""
|
|
38
|
+
numerator_sq = 2 * (a * a + b * b)
|
|
39
|
+
return math.sqrt(numerator_sq) / (a + b)
|
|
40
|
+
|
|
41
|
+
@staticmethod
|
|
42
|
+
def generate_kmer_map(sequence: str, k_value: int) -> Mapping[str, int]:
|
|
43
|
+
"""
|
|
44
|
+
For a given sequence and size of k-mer, count the occurrences of all possible k-mers
|
|
45
|
+
|
|
46
|
+
:param(str) sequence: Given sequence
|
|
47
|
+
:param(int) k_value: Given k-value / Length of k-mer
|
|
48
|
+
:return(dict[str, int]): Key -> K-Mer; Value -> Count in respective sequence
|
|
49
|
+
"""
|
|
50
|
+
kmer_count_map = {}
|
|
51
|
+
for i in range(len(sequence) - k_value + 1):
|
|
52
|
+
kmer = sequence[i: i + k_value]
|
|
53
|
+
# residues can be masked with the '*' character. K-mers that contain masked residues should be ignored
|
|
54
|
+
if '*' in kmer:
|
|
55
|
+
continue
|
|
56
|
+
elif kmer in kmer_count_map:
|
|
57
|
+
kmer_count_map[kmer] += 1
|
|
58
|
+
else:
|
|
59
|
+
kmer_count_map[kmer] = 1
|
|
60
|
+
return kmer_count_map
|
|
61
|
+
|
|
62
|
+
@staticmethod
|
|
63
|
+
def get_kmer_similarity_score(kmer1: str, kmer2: str,
|
|
64
|
+
substitution_matrix: Tuple[TYPE_SCORE_MATRIX, float, float]) -> float:
|
|
65
|
+
"""
|
|
66
|
+
For two given k-mers of same size, compute the similarity score b/w them using a substitution matrix
|
|
67
|
+
Score is normalised to be between 0 and 1
|
|
68
|
+
|
|
69
|
+
:param(dict[str, dict[str, float]], float, float) substitution_matrix: Subs matrix table, Max score, Min score
|
|
70
|
+
:param(str) kmer1: First k-mer
|
|
71
|
+
:param(str) kmer2: Second k-mer
|
|
72
|
+
:return(float): Similarity score
|
|
73
|
+
"""
|
|
74
|
+
subs_matr, subs_max, subs_min = substitution_matrix
|
|
75
|
+
kmer_len = len(kmer1)
|
|
76
|
+
if kmer_len != len(kmer2):
|
|
77
|
+
raise Exception('K-Mer lengths should be same')
|
|
78
|
+
|
|
79
|
+
score_sum = 0
|
|
80
|
+
for i in range(kmer_len):
|
|
81
|
+
try:
|
|
82
|
+
d_score = 1 - ((subs_matr[kmer1[i]][kmer2[i]] - subs_min) / (subs_max - subs_min))
|
|
83
|
+
except KeyError:
|
|
84
|
+
print('ERROR: An amino acid of either {} or {} is not present in substitution matrix'.format(kmer1,
|
|
85
|
+
kmer2))
|
|
86
|
+
d_score = 0
|
|
87
|
+
# Or we can also abort from here
|
|
88
|
+
score_sum += d_score
|
|
89
|
+
return score_sum / kmer_len
|
|
90
|
+
|
|
91
|
+
@staticmethod
|
|
92
|
+
def get_axis_score(axis1_kmer_count_map: Mapping[str, int],
|
|
93
|
+
axis2_kmer_count_map: Mapping[str, int],
|
|
94
|
+
similarity_matrix: TYPE_SCORE_MATRIX,
|
|
95
|
+
reverse=False, is_ld_needed=True) -> list:
|
|
96
|
+
"""
|
|
97
|
+
A common function to iterate row-wise and column-wise over the similarity matrix and compute sums
|
|
98
|
+
|
|
99
|
+
:param(boolean) is_ld_needed: Flag to indicate if length difference should be used for tie-breaker
|
|
100
|
+
:param(dict[str, int]) axis1_kmer_count_map: Mapping of each k-mer count of protein in first position
|
|
101
|
+
:param(dict[str, int]) axis2_kmer_count_map: Mapping of each k-mer count of protein in second position
|
|
102
|
+
:param(dict[str, dict[str, float]]) similarity_matrix: Matrix to represent similarity of k-mers
|
|
103
|
+
Similar to the class variable self.similarity_matrix (output of function generate_similarity_matrix())
|
|
104
|
+
:param(bool) reverse: If matrix access needs to be reversed (True for columns)
|
|
105
|
+
:return(list): Summed score
|
|
106
|
+
"""
|
|
107
|
+
axis_scores = []
|
|
108
|
+
for kmer_seq1, count_kmer_seq1 in axis1_kmer_count_map.items():
|
|
109
|
+
deno = 0
|
|
110
|
+
num_sum = 0
|
|
111
|
+
for kmer_seq2, count_kmer_seq2 in axis2_kmer_count_map.items():
|
|
112
|
+
if not reverse:
|
|
113
|
+
cell_value = similarity_matrix[kmer_seq1][kmer_seq2]
|
|
114
|
+
else:
|
|
115
|
+
cell_value = similarity_matrix[kmer_seq2][kmer_seq1]
|
|
116
|
+
if cell_value < 0:
|
|
117
|
+
continue
|
|
118
|
+
deno += count_kmer_seq2
|
|
119
|
+
num_sum += count_kmer_seq2 * cell_value
|
|
120
|
+
if deno != 0:
|
|
121
|
+
axis_score = count_kmer_seq1 * num_sum / deno
|
|
122
|
+
if is_ld_needed:
|
|
123
|
+
ld_factor = SeqPairSimilarity.get_length_difference(count_kmer_seq1, deno)
|
|
124
|
+
axis_score = axis_score / ld_factor
|
|
125
|
+
else:
|
|
126
|
+
axis_score = 0
|
|
127
|
+
axis_scores.append(axis_score)
|
|
128
|
+
return axis_scores
|
|
129
|
+
|
|
130
|
+
@staticmethod
|
|
131
|
+
def max_value_indexes(axis1_kmer_count_map: Mapping[str, int],
|
|
132
|
+
axis2_kmer_count_map: Mapping[str, int],
|
|
133
|
+
similarity_matrix: TYPE_SCORE_MATRIX,
|
|
134
|
+
reverse=False) -> List[Tuple[str, str]]:
|
|
135
|
+
"""
|
|
136
|
+
Parse the similarity matrix row/column-wise and return the list of indexes with max value
|
|
137
|
+
|
|
138
|
+
:param(dict[str, int]) axis1_kmer_count_map: Mapping of each k-mer count of protein in first position
|
|
139
|
+
:param(dict[str, int]) axis2_kmer_count_map: Mapping of each k-mer count of protein in second position
|
|
140
|
+
:param(dict[str, dict[str, float]]) similarity_matrix: Matrix to represent similarity of k-mers
|
|
141
|
+
Similar to the class variable self.similarity_matrix (output of function generate_similarity_matrix())
|
|
142
|
+
:param(bool) reverse: If matrix access needs to be reversed (True for columns)
|
|
143
|
+
:return(list of tuples[str, str]): Each tuple points to one cell in the similarity matrix with the index k_mers
|
|
144
|
+
"""
|
|
145
|
+
retained_kmers = []
|
|
146
|
+
for kmer_seq1, count_kmer_seq1 in axis1_kmer_count_map.items():
|
|
147
|
+
max_value = -1
|
|
148
|
+
max_value_idxs = ()
|
|
149
|
+
prev_ld_score = -1
|
|
150
|
+
for kmer_seq2, count_kmer_seq2 in axis2_kmer_count_map.items():
|
|
151
|
+
max_flag = False
|
|
152
|
+
if not reverse:
|
|
153
|
+
cell_value = similarity_matrix[kmer_seq1][kmer_seq2]
|
|
154
|
+
else:
|
|
155
|
+
cell_value = similarity_matrix[kmer_seq2][kmer_seq1]
|
|
156
|
+
if cell_value > max_value:
|
|
157
|
+
max_flag = True
|
|
158
|
+
if cell_value == max_value:
|
|
159
|
+
ld_score = SeqPairSimilarity.get_length_difference(count_kmer_seq1, count_kmer_seq2)
|
|
160
|
+
if ld_score < prev_ld_score:
|
|
161
|
+
max_flag = True
|
|
162
|
+
if max_flag:
|
|
163
|
+
max_value = cell_value
|
|
164
|
+
max_value_idxs = (kmer_seq1, kmer_seq2) if not reverse else (kmer_seq2, kmer_seq1)
|
|
165
|
+
prev_ld_score = SeqPairSimilarity.get_length_difference(count_kmer_seq1, count_kmer_seq2)
|
|
166
|
+
retained_kmers.append(max_value_idxs)
|
|
167
|
+
return retained_kmers
|
|
168
|
+
|
|
169
|
+
def generate_similarity_matrix(self):
|
|
170
|
+
"""
|
|
171
|
+
Generate the similarity matrix in a dict of dict format
|
|
172
|
+
Outer-map: Keys -> k-mers of the first sequence; Value -> Inner-map
|
|
173
|
+
Inner-map: Keys -> K-mers of the second sequence; Value -> Similarity score
|
|
174
|
+
"""
|
|
175
|
+
for kmer_seq1 in self.kmer_count_map_1.keys():
|
|
176
|
+
similarity_matrix_row = {}
|
|
177
|
+
for kmer_seq2 in self.kmer_count_map_2.keys():
|
|
178
|
+
score = self.get_kmer_similarity_score(kmer_seq1, kmer_seq2, self.substitution_matrix_obj)
|
|
179
|
+
similarity_matrix_row[kmer_seq2] = score
|
|
180
|
+
self.similarity_matrix[kmer_seq1] = similarity_matrix_row
|
|
181
|
+
|
|
182
|
+
def compute_kmer_maps(self):
|
|
183
|
+
"""
|
|
184
|
+
Compute kmer-count hashmap for both the sequences
|
|
185
|
+
"""
|
|
186
|
+
self.kmer_count_map_1 = self.generate_kmer_map(sequence=self.sequence1, k_value=self.k)
|
|
187
|
+
self.kmer_count_map_2 = self.generate_kmer_map(sequence=self.sequence2, k_value=self.k)
|
|
188
|
+
|
|
189
|
+
def filter_similarity_matrix(self, threshold: float):
|
|
190
|
+
"""
|
|
191
|
+
Normal static threshold filtering
|
|
192
|
+
"""
|
|
193
|
+
for kmer_seq1 in self.kmer_count_map_1.keys():
|
|
194
|
+
for kmer_seq2 in self.kmer_count_map_2.keys():
|
|
195
|
+
if self.similarity_matrix[kmer_seq1][kmer_seq2] <= threshold:
|
|
196
|
+
self.similarity_matrix[kmer_seq1][kmer_seq2] = -1
|
|
197
|
+
|
|
198
|
+
def filter_similarity_matrix__sparse(self) -> Tuple[List[Tuple[str, str]], List[Tuple[str, str]]]:
|
|
199
|
+
"""
|
|
200
|
+
Only retain the cells if they are the max in either their own row or column
|
|
201
|
+
"""
|
|
202
|
+
retained_indexes_rows = self.max_value_indexes(self.kmer_count_map_1, self.kmer_count_map_2,
|
|
203
|
+
self.similarity_matrix)
|
|
204
|
+
retained_indexes_cols = self.max_value_indexes(self.kmer_count_map_2, self.kmer_count_map_1,
|
|
205
|
+
self.similarity_matrix, reverse=True)
|
|
206
|
+
retained_indexes = retained_indexes_rows + retained_indexes_cols
|
|
207
|
+
for kmer_seq1 in self.kmer_count_map_1.keys():
|
|
208
|
+
for kmer_seq2 in self.kmer_count_map_2.keys():
|
|
209
|
+
if (kmer_seq1, kmer_seq2) in retained_indexes:
|
|
210
|
+
continue
|
|
211
|
+
self.similarity_matrix[kmer_seq1][kmer_seq2] = -1
|
|
212
|
+
# M_dashed = self.similarity_matrix at this point
|
|
213
|
+
return retained_indexes_rows, retained_indexes_cols
|
|
214
|
+
|
|
215
|
+
def get_similarity_score(self, threshold: float) -> float:
|
|
216
|
+
"""
|
|
217
|
+
Get similarity score (variant: normal)
|
|
218
|
+
|
|
219
|
+
:param(float) threshold: Threshold for normal filtering
|
|
220
|
+
:return(float): Normal Similarity Score
|
|
221
|
+
"""
|
|
222
|
+
if not (self.kmer_count_map_1 and self.kmer_count_map_2 and self.similarity_matrix):
|
|
223
|
+
raise Exception('Compute pre-requisites')
|
|
224
|
+
self.filter_similarity_matrix(threshold)
|
|
225
|
+
row_scores = self.get_axis_score(self.kmer_count_map_1, self.kmer_count_map_2, self.similarity_matrix)
|
|
226
|
+
column_scores = self.get_axis_score(self.kmer_count_map_2, self.kmer_count_map_1, self.similarity_matrix,
|
|
227
|
+
reverse=True)
|
|
228
|
+
final_score = (sum(row_scores) + sum(column_scores)) / (
|
|
229
|
+
sum(self.kmer_count_map_1.values()) + sum(self.kmer_count_map_2.values()))
|
|
230
|
+
return final_score
|
|
231
|
+
|
|
232
|
+
def get_similarity_score__sparse(self) -> float:
|
|
233
|
+
"""
|
|
234
|
+
Get similarity score (variant: sparse)
|
|
235
|
+
|
|
236
|
+
:return(float): Sparse Score
|
|
237
|
+
"""
|
|
238
|
+
if not (self.kmer_count_map_1 and self.kmer_count_map_2 and self.similarity_matrix):
|
|
239
|
+
raise Exception('Compute pre-requisites')
|
|
240
|
+
self.filter_similarity_matrix__sparse()
|
|
241
|
+
sparse_score = 0
|
|
242
|
+
norm_factor = 0
|
|
243
|
+
for kmer_seq1, count_kmer_seq1 in self.kmer_count_map_1.items():
|
|
244
|
+
for kmer_seq2, count_kmer_seq2 in self.kmer_count_map_2.items():
|
|
245
|
+
cell_value = self.similarity_matrix[kmer_seq1][kmer_seq2]
|
|
246
|
+
if cell_value > -1:
|
|
247
|
+
norm_factor += (count_kmer_seq1 * count_kmer_seq2)
|
|
248
|
+
sparse_score += (count_kmer_seq1 * count_kmer_seq2 * cell_value) / self.get_length_difference(
|
|
249
|
+
count_kmer_seq1, count_kmer_seq2)
|
|
250
|
+
if norm_factor == 0:
|
|
251
|
+
return 0
|
|
252
|
+
final_sparse_score = sparse_score / norm_factor
|
|
253
|
+
return final_sparse_score
|
|
254
|
+
|
|
255
|
+
def get_similarity_score__collapsed(self) -> float:
|
|
256
|
+
"""
|
|
257
|
+
Get similarity score (variant: collapsed)
|
|
258
|
+
|
|
259
|
+
:return(float): Collapsed Score
|
|
260
|
+
"""
|
|
261
|
+
if not (self.kmer_count_map_1 and self.kmer_count_map_2 and self.similarity_matrix):
|
|
262
|
+
raise Exception('Compute pre-requisites')
|
|
263
|
+
self.filter_similarity_matrix__sparse()
|
|
264
|
+
row_scores = self.get_axis_score(self.kmer_count_map_1, self.kmer_count_map_2, self.similarity_matrix,
|
|
265
|
+
is_ld_needed=False)
|
|
266
|
+
column_scores = self.get_axis_score(self.kmer_count_map_2, self.kmer_count_map_1,
|
|
267
|
+
self.similarity_matrix, reverse=True, is_ld_needed=False)
|
|
268
|
+
final_score = (sum(row_scores) + sum(column_scores)) / (
|
|
269
|
+
len(self.sequence1) + len(self.sequence2) - 2 * (self.k - 1))
|
|
270
|
+
return final_score
|
|
271
|
+
|
|
272
|
+
def generate_reciprocal_matrix(self) -> Tuple[TYPE_SCORE_MATRIX, TYPE_SCORE_MATRIX]:
|
|
273
|
+
"""
|
|
274
|
+
:return(dict[str, dict[str, float]], dict[str, dict[str, float]]): Reciprocal matrix,
|
|
275
|
+
its hadamard product with the similarity matrix
|
|
276
|
+
"""
|
|
277
|
+
if not (self.kmer_count_map_1 and self.kmer_count_map_2 and self.similarity_matrix):
|
|
278
|
+
raise Exception('Compute pre-requisites')
|
|
279
|
+
reciprocal_matrix = {}
|
|
280
|
+
hadamard_matrix = {} # C = M (.) R
|
|
281
|
+
retained_indexes_rows, retained_indexes_cols = self.filter_similarity_matrix__sparse()
|
|
282
|
+
for kmer_seq1 in self.kmer_count_map_1.keys():
|
|
283
|
+
reciprocal_matrix_row = {}
|
|
284
|
+
hadamard_matrix_row = {}
|
|
285
|
+
for kmer_seq2 in self.kmer_count_map_2.keys():
|
|
286
|
+
if not ((kmer_seq1, kmer_seq2) in retained_indexes_rows and
|
|
287
|
+
(kmer_seq1, kmer_seq2) in retained_indexes_cols):
|
|
288
|
+
continue # r_val = 0; so this entry be ignored
|
|
289
|
+
r_val = 1
|
|
290
|
+
reciprocal_matrix_row[kmer_seq2] = r_val
|
|
291
|
+
hadamard_matrix_row[kmer_seq2] = self.similarity_matrix[kmer_seq1][kmer_seq2] * r_val
|
|
292
|
+
if reciprocal_matrix_row or hadamard_matrix_row:
|
|
293
|
+
reciprocal_matrix[kmer_seq1] = reciprocal_matrix_row
|
|
294
|
+
hadamard_matrix[kmer_seq1] = hadamard_matrix_row
|
|
295
|
+
return reciprocal_matrix, hadamard_matrix
|
bio_shark/core/utils.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import csv
|
|
2
|
+
import math
|
|
3
|
+
from typing import Mapping, Tuple, Union
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from .. import settings
|
|
7
|
+
from ..exceptions import DuplicateSequenceIdError
|
|
8
|
+
|
|
9
|
+
TYPE_SCORE_MATRIX = Mapping[str, Mapping[str, float]]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def load_subs_matrix(file_url: str) -> Tuple[TYPE_SCORE_MATRIX, float, float]:
|
|
13
|
+
"""
|
|
14
|
+
Read a substitution matrix tsv/csv file present in tabular format and return it in dictionary format
|
|
15
|
+
|
|
16
|
+
:param(str) file_url: Tab-separated file path for the substitution matrix file
|
|
17
|
+
:return(dict[str, dict[str, float]], float, float): The substitution matrix in dict of dict form.
|
|
18
|
+
The outer dict has keys as amino acids and values as a dict of scores for other amino acids
|
|
19
|
+
Second and third values are min and max scores from the table
|
|
20
|
+
"""
|
|
21
|
+
subs_matrix = {}
|
|
22
|
+
all_aas = []
|
|
23
|
+
matrix_min = math.inf
|
|
24
|
+
matrix_max = 0
|
|
25
|
+
with open(file=file_url, mode='r', newline='') as csvfile:
|
|
26
|
+
for row in csv.reader(csvfile, delimiter='\t'):
|
|
27
|
+
if row[0] == '.':
|
|
28
|
+
all_aas = row[1:]
|
|
29
|
+
continue
|
|
30
|
+
subs_matrix_aa = {}
|
|
31
|
+
for i in range(len(all_aas)):
|
|
32
|
+
cell_score = int(row[i + 1])
|
|
33
|
+
matrix_min = cell_score if cell_score < matrix_min else matrix_min
|
|
34
|
+
matrix_max = cell_score if cell_score > matrix_max else matrix_max
|
|
35
|
+
subs_matrix_aa[all_aas[i]] = cell_score
|
|
36
|
+
subs_matrix[row[0]] = subs_matrix_aa
|
|
37
|
+
return subs_matrix, matrix_max, matrix_min
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def get_grantham_subs_matrix() -> Tuple[TYPE_SCORE_MATRIX, float, float]:
|
|
41
|
+
"""Load a specific substitution matrix from the given file path"""
|
|
42
|
+
return load_subs_matrix(file_url=settings.SUBS_MATRIX_DIR)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def read_fasta_file(file_path: Union[str, Path]) -> Mapping[str, str]:
|
|
46
|
+
"""
|
|
47
|
+
Read a sequence fasta file and prepare a hash-map with ID and sequence
|
|
48
|
+
Also accounts for a fasta file where a same sequence has line-breaks in between
|
|
49
|
+
|
|
50
|
+
:param(str) file_path: URL of fasta file for sequences
|
|
51
|
+
:return(dict[str, str]): Key -> Sequence ID; Value: Sequence
|
|
52
|
+
"""
|
|
53
|
+
id_seq_map = {}
|
|
54
|
+
skip_count = 0
|
|
55
|
+
|
|
56
|
+
def _insert_map(sequence_id: str,
|
|
57
|
+
sequence: str,
|
|
58
|
+
_id_seq_map: Mapping[str, str],
|
|
59
|
+
_skip_count: int) -> Tuple[Mapping[str, str], int]:
|
|
60
|
+
if sequence_id and sequence:
|
|
61
|
+
has_non_cano_aa = False
|
|
62
|
+
for aa in sequence:
|
|
63
|
+
if aa not in settings.CANONICAL_AAS:
|
|
64
|
+
has_non_cano_aa = True
|
|
65
|
+
break
|
|
66
|
+
if sequence_id in _id_seq_map:
|
|
67
|
+
raise DuplicateSequenceIdError(seq_id=sequence_id)
|
|
68
|
+
if not has_non_cano_aa:
|
|
69
|
+
_id_seq_map[sequence_id] = sequence
|
|
70
|
+
else:
|
|
71
|
+
_skip_count += 1
|
|
72
|
+
return _id_seq_map, _skip_count
|
|
73
|
+
|
|
74
|
+
curr_id = None
|
|
75
|
+
curr_sequence = ''
|
|
76
|
+
with open(file=file_path) as fasta_file:
|
|
77
|
+
for line in fasta_file.readlines():
|
|
78
|
+
if line.startswith('>'):
|
|
79
|
+
# Map existing sequence to ID; This line has next ID
|
|
80
|
+
id_seq_map, skip_count = _insert_map(curr_id, curr_sequence, id_seq_map, skip_count)
|
|
81
|
+
curr_id = line[1:].strip()
|
|
82
|
+
curr_sequence = ''
|
|
83
|
+
continue
|
|
84
|
+
if curr_id and not line.startswith('>'):
|
|
85
|
+
curr_sequence = curr_sequence.strip() + line.strip()
|
|
86
|
+
|
|
87
|
+
id_seq_map, skip_count = _insert_map(curr_id, curr_sequence, id_seq_map, skip_count)
|
|
88
|
+
|
|
89
|
+
print("Read fasta file from path {}; Found {} sequences; Skipped {} sequences for having non-canonical AAs".
|
|
90
|
+
format(file_path, len(id_seq_map), skip_count))
|
|
91
|
+
return id_seq_map
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def form_sequence_pairs(id_seq_map: Mapping[str, str],
|
|
95
|
+
id_seq_map2: Mapping[str, str] = None
|
|
96
|
+
) -> Mapping[str, Mapping[str, str]]:
|
|
97
|
+
"""
|
|
98
|
+
Generate unique sequence pairs from one / two list of sequences
|
|
99
|
+
For one list, compute intra-group pairs and inter-group for two.
|
|
100
|
+
|
|
101
|
+
:param (dict[str, str]) id_seq_map: Mapping of sequence with fasta ID. Key -> Fasta ID; Value -> Fasta Sequence
|
|
102
|
+
:param (dict[str, str]) id_seq_map2: Mapping of sequence with fasta ID. Key -> Fasta ID; Value -> Fasta Sequence
|
|
103
|
+
:return (dict[str, dict[str, str]]): Key -> Unique Sequence Pair ID (concatenated);
|
|
104
|
+
Value -> Dictionary with 'seq_id1', 'seq_id2', 'sequence1', 'sequence2'
|
|
105
|
+
"""
|
|
106
|
+
pair_id__input_param_map = {} # Logic: Unique sequence pairs (not all pairs)
|
|
107
|
+
# In the above dict, key is a unique identifier for the pair
|
|
108
|
+
id_seq_map2 = id_seq_map2 if id_seq_map2 else id_seq_map
|
|
109
|
+
for seq_id1, sequence1 in id_seq_map.items():
|
|
110
|
+
for seq_id2, sequence2 in id_seq_map2.items():
|
|
111
|
+
pair_id = '__'.join(sorted([seq_id1, seq_id2]))
|
|
112
|
+
if pair_id in pair_id__input_param_map:
|
|
113
|
+
continue
|
|
114
|
+
pair_id__input_param_map[pair_id] = {
|
|
115
|
+
'seq_id1': seq_id1,
|
|
116
|
+
'seq_id2': seq_id2,
|
|
117
|
+
'sequence1': sequence1,
|
|
118
|
+
'sequence2': sequence2
|
|
119
|
+
}
|
|
120
|
+
return pair_id__input_param_map
|
|
Binary file
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
. A R N D C Q E G H I L K M F P S T W Y V
|
|
2
|
+
A 0 112 111 126 195 91 107 60 86 94 96 106 84 113 27 99 58 148 112 64
|
|
3
|
+
R 112 0 86 96 180 43 54 125 29 97 102 26 91 97 103 110 71 101 77 96
|
|
4
|
+
N 111 86 0 23 139 46 42 80 68 149 153 94 142 158 91 46 65 174 143 133
|
|
5
|
+
D 126 96 23 0 154 61 45 94 81 168 172 101 160 177 108 65 85 181 160 152
|
|
6
|
+
C 195 180 139 154 0 154 170 159 174 198 198 202 196 205 169 112 149 215 194 192
|
|
7
|
+
Q 91 43 46 61 154 0 29 87 24 109 113 53 101 116 76 68 42 130 99 96
|
|
8
|
+
E 107 54 42 45 170 29 0 98 40 134 138 56 126 140 93 80 65 152 122 121
|
|
9
|
+
G 60 125 80 94 159 87 98 0 98 135 138 127 127 153 42 56 59 184 147 109
|
|
10
|
+
H 86 29 68 81 174 24 40 98 0 94 99 32 87 100 77 89 47 115 83 84
|
|
11
|
+
I 94 97 149 168 198 109 134 135 94 0 5 102 10 21 95 142 89 61 33 29
|
|
12
|
+
L 96 102 153 172 198 113 138 138 99 5 0 107 15 22 98 145 92 61 36 32
|
|
13
|
+
K 106 26 94 101 202 53 56 127 32 102 107 0 95 102 103 121 78 110 85 97
|
|
14
|
+
M 84 91 142 160 196 101 126 127 87 10 15 95 0 28 87 135 81 67 36 21
|
|
15
|
+
F 113 97 158 177 205 116 140 153 100 21 22 102 28 0 114 155 103 40 22 50
|
|
16
|
+
P 27 103 91 108 169 76 93 42 77 95 98 103 87 114 0 74 38 147 110 68
|
|
17
|
+
S 99 110 46 65 112 68 80 56 89 142 145 121 135 155 74 0 58 177 144 124
|
|
18
|
+
T 58 71 65 85 149 42 65 59 47 89 92 78 81 103 38 58 0 128 92 69
|
|
19
|
+
W 148 101 174 181 215 130 152 184 115 61 61 110 67 40 147 177 128 0 37 88
|
|
20
|
+
Y 112 77 143 160 194 99 122 147 83 33 36 85 36 22 110 144 92 37 0 55
|
|
21
|
+
V 64 96 133 152 192 96 121 109 84 29 32 97 21 50 68 124 69 88 55 0
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
>A0A091RJG2_9GRUI/256-425/PF16620
|
|
2
|
+
QPPCIPSKPTTLAVKAFENSEFKQATSNGFAKEIKSSSTEHMVVETKDVVSAKKETFYITREAKDSKQEQHQEGNAVSPQELKEGKKGSQVLQKTSSTITLQAVKLQPEAKAEPQATFIRQGEDRKRAVQSLMSAQQTPSLTGQVSPRSRDAENRCEAQNAVLEEKREPL
|
|
3
|
+
>A0A402EUI4_9SAUR/266-430/PF16620
|
|
4
|
+
HPLSTSTKSSVTTKVTETSSFRKATSNGFAKDIKSTTTELLLKPKDRAALKKENSLISSEETKQSSLEQTNELNVQSPKEERRGYLVLQKTSSTITLQPIKVPTGPKVGPSTTSLKQEEEKGTKQFITAGRQVPSTMKLGGLRTKELEDNVELKKSTAELKREPL
|
|
5
|
+
>A0A4X2KGZ0_VOMUR/250-412/PF16620
|
|
6
|
+
QGTEKTNRYPLNAVATENSETKKVTADKISKHPQPSGLESVDKSQNHSDLPKMFSRENERTHRQPQSLSESRPEPYKLKEKVPEVAPVLQKTSSTIILQSVRAPVETKPQAMGNLPLQGEEKKVPTAQPTASPLSKELDSKGQELETKIATKKITLENRRNKT
|
|
7
|
+
>A0A5N4EJH9_CAMDR/133-187/PF16620
|
|
8
|
+
QGLDSTNRSLVRGTKVANSDISKEVTNGITQGPKPDGPEAAGGISGIPKPEVTWF
|
|
9
|
+
>A0A662YXF4_ACIRT/481-649/PF16620
|
|
10
|
+
GIISAKPSARVIKTEETSESQHTTANGIIKQQKSTTTAVKTEPKETLTTMRAISRESNVLTQRKAEPVKKASAAEPKKSLVDPKAEVRRSEVQQKTASTITLQSVKVNQKTKADVKPAPDPRVEAAKKVTQQVTVESRITFQAIQGVTKEPGKLRETSKITAEQKWETK
|
|
11
|
+
>A0A670HY74_PODMU/241-359/PF16620
|
|
12
|
+
MVQGSEFVLFKSNQNQEGNPWPQGIKLEDGSQHSRKGCRVLQKTSSTITLQSVKVQTGPKAQVKAASSGQEDDGKKPKQPLTTGWQAAPSIRPSGLRTKELDDRVGLKKSITDVKRETQ
|
|
13
|
+
>G1KKG2_ANOCA/284-445/PF16620
|
|
14
|
+
ASIQPTITTKAVNTSEFKNATSNGITKELKSTSTELLLESKERPVMKQDPPPASSKETKLSTLEKVKEPNIEPPKEEGRGYQTLQKSSSTITLQPVKVQTGPRAEVKAATSRQEDDDKKPKQSLTANWKGTTSAKLSGLKTKEPEERLGFKKSITEVKREPQ
|
|
15
|
+
>H0V3V2_CAVPO/258-419/PF16620
|
|
16
|
+
PGLDSANRSSVRATKDLDADIKREVTNGITKGLKLDSVESMARSKTNPSLDKGGCQAQVSNSPSHSLWEPKPELCADSSRKVLKTPVLQKTSSTITLQATKVQPEPRAPALEALSPSREEKRRLATSQPTTSPTTRPSPENPEVVSTVATRKTPVESQKEWT
|
|
17
|
+
>I3LVW2_ICTTR/258-399/PF16620
|
|
18
|
+
KTLEVRGGAEVPNSDVRKEVTNGVSKGLETAAASINCSSPQGGQSPICTSNSQPQPLQEPKVMLTGTPVLQKTSSSITLQATKVQLEQRAPIMGTLSPRGEMSPSVSQPATLSTGLLDLGSPEVVSEVATRKIPMESQSHSA
|
|
19
|
+
>L9KKH0_TUPCH/355-517/PF16620
|
|
20
|
+
QGLDNVNRPSVRGTKATTSSDLRREVTNGIAKGPKLDSLETAAESKNCPSPQRGGPPSWATNSQAPPQRESQPEPSRDSLRKGSQNPVLQKTSSTITLQAMKAQLEPKASVSGALSSSEEERKRPSAPHPVTLPTRLSGVGSQDTVSKAATRKIPLDSRRDLT
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
. A R N D C Q E G H I L K M F P S T W Y V
|
|
2
|
+
A 0 112 111 126 195 91 107 60 86 94 96 106 84 113 27 99 58 148 112 64
|
|
3
|
+
R 112 0 86 96 180 43 54 125 29 97 102 26 91 97 103 110 71 101 77 96
|
|
4
|
+
N 111 86 0 23 139 46 42 80 68 149 153 94 142 158 91 46 65 174 143 133
|
|
5
|
+
D 126 96 23 0 154 61 45 94 81 168 172 101 160 177 108 65 85 181 160 152
|
|
6
|
+
C 195 180 139 154 0 154 170 159 174 198 198 202 196 205 169 112 149 215 194 192
|
|
7
|
+
Q 91 43 46 61 154 0 29 87 24 109 113 53 101 116 76 68 42 130 99 96
|
|
8
|
+
E 107 54 42 45 170 29 0 98 40 134 138 56 126 140 93 80 65 152 122 121
|
|
9
|
+
G 60 125 80 94 159 87 98 0 98 135 138 127 127 153 42 56 59 184 147 109
|
|
10
|
+
H 86 29 68 81 174 24 40 98 0 94 99 32 87 100 77 89 47 115 83 84
|
|
11
|
+
I 94 97 149 168 198 109 134 135 94 0 5 102 10 21 95 142 89 61 33 29
|
|
12
|
+
L 96 102 153 172 198 113 138 138 99 5 0 107 15 22 98 145 92 61 36 32
|
|
13
|
+
K 106 26 94 101 202 53 56 127 32 102 107 0 95 102 103 121 78 110 85 97
|
|
14
|
+
M 84 91 142 160 196 101 126 127 87 10 15 95 0 28 87 135 81 67 36 21
|
|
15
|
+
F 113 97 158 177 205 116 140 153 100 21 22 102 28 0 114 155 103 40 22 50
|
|
16
|
+
P 27 103 91 108 169 76 93 42 77 95 98 103 87 114 0 74 38 147 110 68
|
|
17
|
+
S 99 110 46 65 112 68 80 56 89 142 145 121 135 155 74 0 58 177 144 124
|
|
18
|
+
T 58 71 65 85 149 42 65 59 47 89 92 78 81 103 38 58 0 128 92 69
|
|
19
|
+
W 148 101 174 181 215 130 152 184 115 61 61 110 67 40 147 177 128 0 37 88
|
|
20
|
+
Y 112 77 143 160 194 99 122 147 83 33 36 85 36 22 110 144 92 37 0 55
|
|
21
|
+
V 64 96 133 152 192 96 121 109 84 29 32 97 21 50 68 124 69 88 55 0
|
|
File without changes
|