-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
365 lines (296 loc) · 12.5 KB
/
tools.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import pyhgvs as hgvs
import math
import copy
from operator import itemgetter
import blosum as bl
bl_mat = bl.BLOSUM(62)
bl_dict = dict(bl_mat)
"""
genetic_code = {
'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',
'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',
'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',
'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',
'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',
'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',
'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',
'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',
'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',
'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',
'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',
'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',
'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',
'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',
'TAC':'Y', 'TAT':'Y', 'TAA':'_', 'TAG':'_',
'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W'
}
"""
amino_acids = {'Cys': 'C', 'Asp': 'D', 'Ser': 'S', 'Gln': 'Q', 'Lys': 'K',
'Ile': 'I', 'Pro': 'P', 'Thr': 'T', 'Phe': 'F', 'Asn': 'N',
'Gly': 'G', 'His': 'H', 'Leu': 'L', 'Arg': 'R', 'Trp': 'W',
'Ala': 'A', 'Val':'V', 'Glu': 'E', 'Tyr': 'Y', 'Met': 'M',
'Asx': 'B', 'Glx': 'Z', 'Xaa': "X", "Xle": "J"}
chemical_properties = {"Aliphatic": ["G", "A", "V", "L", "I", "P", "M"],
"Aromatic": ["F", "Y", "W"],
"Polar uncharged": ["S", "T", "C", "N", "Q"],
"Acidic": ["D", "E"],
"Basic": ["R", "H", "K"],
"Unknown": ["B", "Z", "X", "J"]}
def fasta_reader(filename): #Taken from the labs
seqDict = {}
fileIn = open(filename, "r")
sequence = ""
header = ""
for line in fileIn:
if line[0] == ">":
if sequence:
seqDict[header] = sequence
header = line[1:].strip()
sequence = ""
else:
sequence += line.strip()
seqDict[header] = sequence
fileIn.close()
return seqDict
def shannon_entropy(msa_dict): #Adapted from the function with the same name from Lab 4 of BIO 310 (Spring 2022)
msa_length = len(list(msa_dict.values())[0])
number_of_proteins = len(list(msa_dict.keys()))
entropy_list = []
aa_count_dict = {}
for i in range(msa_length):
aa_count_dict[i] = {}
for seq in msa_dict.values():
aa = seq[i]
if not aa in aa_count_dict[i].keys():
aa_count_dict[i][aa] = 1
else:
aa_count_dict[i][aa] += 1
for pos in aa_count_dict.keys():
for aa,count in aa_count_dict[pos].items():
P_i = count/float(number_of_proteins)
entropy_i = P_i*(math.log(P_i,2))
entropy_list.append(entropy_i)
sh_entropy = -(sum(entropy_list))
return sh_entropy
"""
def SPcalc(msa_dict, substitution_matrix): #Adapted from the function with the same name from Lab 4 of BIO 310 (Spring 2022)
SP = 0
msa_length = len(list(msa_dict.values())[0])
number_of_proteins = len(list(msa_dict.keys()))
protein_sequences = list(msa_dict.values())
for i in range(msa_length):
for j in range(number_of_proteins):
for k in range(j+1,number_of_proteins):
if protein_sequences[j][i] == "-" or protein_sequences[k][i] == "-":
SP += 0
else:
SP += substitution_matrix[protein_sequences[j][i] + protein_sequences[k][i]]
return SP
"""
def list_to_fasta(seq_list, file_name): #Writes a list of sequences into a FASTA file
fasta_file = open(file_name + ".fa", "a")
for i in range(0, len(seq_list)):
fasta_file.write(">" + str(i) + "\n")
fasta_file.write(seq_list[i] + "\n")
fasta_file.close()
def mutation_generator(ref_seq, mut_list, type = "aa", ref_transcript_name = "NM_000492.3"): #Generates and returns the mutated sequences described in the database; assumes point substitutions
#Note that nucleotide numbering in HGVS format starts from 1.
mutated_seqs = []
for mutation in mut_list:
ref_copy = ref_seq
temp_copy = [*ref_copy]
hgvs_info = hgvs.HGVSName(ref_transcript_name + ":" + mutation)
if type == "aa":
pos = hgvs_info.start
new_aa = amino_acids[hgvs_info.alt_allele]
temp_copy[pos - 1] = new_aa
elif type == "nuc":
pos = hgvs_info.cdna_start.coord
new_nuc = hgvs_info.alt_allele
temp_copy[pos - 1] = new_nuc
else:
pass
new_seq = "".join(temp_copy)
mutated_seqs.append(new_seq)
return mutated_seqs
def variant_interpreter(mutation, type = "aa", ref_transcript_name = "NM_000492.3"): #Interprets a variant in HGVS notation
pos = 0
subst = ""
ref = ""
hgvs_info = hgvs.HGVSName(ref_transcript_name + ":" + mutation)
if type == "aa":
pos = hgvs_info.start - 1
ref = amino_acids[hgvs_info.ref_allele]
subst = amino_acids[hgvs_info.alt_allele]
elif type == "nuc":
pos = hgvs_info.cdna_start.coord - 1
ref = hgvs_info.ref_allele
subst = hgvs_info.alt_allele
else:
pass
score = bl_dict[amino_acids[hgvs_info.ref_allele] + subst]
return pos, ref, subst, score
"""
def translator(DNA): #Translates a DNA sequence to a protein sequence; actually redundant
protein_seq = ""
i = 0
while (i <= len(DNA) - 3):
codon = DNA[i:i+3]
protein_seq += genetic_code[codon]
i += 3
return protein_seq
"""
def gap_pass(sequence, loc): #Returns the gapped position of a location in a gapless sequence
pos = 0
counter = 0
for x in sequence:
if pos < loc:
if x == "-":
counter += 1
continue
else:
counter += 1
pos += 1
return counter
def profiler(seq_list, type = "aa", result = "percentage"): #Profiles a list of sequences; it assumes that each sequence has the same length and dismisses gaps
profile = []
chrList = {}
if type == "aa":
chrList = {
"A": 0, "C": 0, "D": 0,
"E": 0, "F": 0, "G": 0,
"H": 0, "I": 0, "K": 0,
"L": 0, "M": 0, "N": 0,
"P": 0, "Q": 0, "R": 0,
"S": 0, "T": 0, "V": 0,
"W": 0, "Y": 0, "B": 0,
"Z": 0, "X":0, "J": 0
}
elif type == "nuc":
chrList = {
"A": 0,
"T": 0,
"G": 0,
"C": 0
}
else:
pass
for i in range(0, len(seq_list[0])):
profile.append(chrList.copy())
for j in range(0, len(seq_list)):
for k in range(0, len(seq_list[j])):
if seq_list[j][k] in profile[k]: #If it is not a gap
profile[k][seq_list[j][k]] += 1
else: #If it is a gap
pass
if result == "percentage":
for m in range(0, len(profile)):
sum = 0
for key in profile[m]:
sum += profile[m][key]
for key in profile[m]:
profile[m][key] = round(profile[m][key] / sum, 2)
else:
pass
return profile
def profile_modifier(profile, ref_seq, seq_dict, ref_seq_key = "NP 000483.3 cystic fibrosis transmembrane conductance regulator Homo sapiens"): #Returns a profile that retains only the positions that are not gapped in the sequence of interest
modified_profile = []
for i in range(0, len(ref_seq)):
loc = gap_pass(seq_dict[ref_seq_key], i)
modified_profile.append(profile[loc])
return modified_profile
def vus_interpreter(vus_list, profile, msa_dict, regions, variant_key = "NP 000483.3 cystic fibrosis transmembrane conductance regulator Homo sapiens"): #Provides guiding information on whether a VUS is pathogenic; assumes that the VUSes describe amino acid sequences
for vus in vus_list: #Inspects each VUS and prints the most common character vs. the substitution
prediction = ""
pos, ref, subst, bl_score = variant_interpreter(vus)
max_freq_dict = dict(sorted(profile[pos].items(), key = itemgetter(1), reverse = True)[:4])
max_freq_keys = sorted(max_freq_dict, key = max_freq_dict.get, reverse = True)
max_freq_values = sorted(max_freq_dict.values(), reverse = True)
max_freq = max(max_freq_values)
max_chrs = []
for key in profile[pos]:
if profile[pos][key] == max_freq:
max_chrs.append(key)
else:
max_chrs.append(key)
entropy_before = shannon_entropy(msa_dict)
modified_msa_dict = copy.deepcopy(msa_dict)
replaced_seq = msa_dict[variant_key]
replaced_seq_list = [*replaced_seq]
actual_pos = gap_pass(replaced_seq, pos)
replaced_seq_list[actual_pos] = subst
replaced_seq = "".join(replaced_seq_list)
modified_msa_dict[variant_key] = replaced_seq
entropy_after = shannon_entropy(modified_msa_dict)
entropy_change = entropy_after - entropy_before
max_freq_chem_groups = []
chemical_group_after = ""
ref_chemical_group = ""
for aa in max_freq_keys:
chemical_group_before = ""
for key in chemical_properties:
if aa in chemical_properties[key]:
chemical_group_before = key
else:
pass
max_freq_chem_groups.append(chemical_group_before)
for key in chemical_properties:
if (ref in chemical_properties[key]) and (subst in chemical_properties[key]):
chemical_group_after = key
ref_chemical_group = key
elif ref in chemical_properties[key]:
ref_chemical_group = key
elif subst in chemical_properties[key]:
chemical_group_after = key
else:
pass
inCriticalPart = False
for key in regions:
for region in regions[key]:
start_pos = gap_pass(msa_dict[variant_key], region[0])
end_pos = gap_pass(msa_dict[variant_key], region[1])
if start_pos == end_pos:
if pos == start_pos:
inCriticalPart = True
break
else:
pass
elif start_pos <= pos <= end_pos:
inCriticalPart = True
break
else:
pass
if inCriticalPart == True:
if ref == subst:
prediction = "Non-pathogenic"
elif bl_score >= 0 and ref_chemical_group == chemical_group_after:
prediction = "Likely non-pathogenic"
else:
prediction = "Likely pathogenic"
else:
if ref == subst:
prediction = "Non-pathogenic"
elif bl_score >= 0 and ref_chemical_group == chemical_group_after:
prediction = "Likely non-pathogenic"
elif bl_score >= 0 and subst in max_freq_keys[0:2]:
prediction = "Likely non-pathogenic"
elif bl_score >= 0 and chemical_group_after in max_freq_chem_groups[0:2]:
prediction = "Likely non-pathogenic"
elif bl_score >= 0:
prediction = "Possibly (<likely) non-pathogenic"
else:
prediction = "Likely pathogenic"
print("The most common characters in the profile: ")
for i in range(0, len(max_freq_keys)):
print("-" + max_freq_keys[i] + " with " + str(max_freq_values[i] * 100) + "%")
print("Chemical properties of the most common characters: ")
for j in range(0, len(max_freq_keys)):
print("-" + max_freq_chem_groups[j] + " with " + str(max_freq_values[j] * 100) + "%")
print("Substitution: " + ref + " -> " + subst)
print("Position in the aligned sequence: " + str(actual_pos))
print("Chemical property of the reference character: " + ref_chemical_group)
print("Chemical property of the substitution character: " + chemical_group_after)
print("Change in entropy: " + str(entropy_change))
print("Substitution score (BLOSUM62): " + str(bl_score))
print("In a critical part of the protein? " + str(inCriticalPart))
print("Automated prediction: " + prediction + "\n")