-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub-cut.py
180 lines (149 loc) · 6.33 KB
/
sub-cut.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
import json
import pandas as pd
import multiprocessing as mp
from Bio import SeqIO, Align
from Bio.Align import substitution_matrices
from datetime import datetime
def setup_aligner():
# Set up the aligner
aligner = Align.PairwiseAligner()
# Valid Substitution Matrices
# 'BENNER22', 'BENNER6', 'BENNER74', 'BLOSUM45',
# 'BLOSUM50', 'BLOSUM62', 'BLOSUM80', 'BLOSUM90',
# 'DAYHOFF', 'FENG', 'GENETIC', 'GONNET1992', 'HOXD70',
# 'JOHNSON', 'JONES', 'LEVIN', 'MCLACHLAN', 'MDM78',
# 'NUC.4.4', 'PAM250', 'PAM30', 'PAM70', 'RAO',
# 'RISLER', 'SCHNEIDER', 'STR', 'TRANS'
matrix = 'PAM70'
aligner.substitution_matrix = substitution_matrices.load(matrix)
aligner.mode = 'local'
aligner.open_gap_score = -10
aligner.extend_gap_score = -5
return aligner
def get_params(score_type="biopython", include_proteases_with_one_substrate=True):
# Load the data
with open("data/substrates_data.json", "r") as in_file_name:
substrates_dict = dict(json.load(in_file_name))
if score_type == "positionwise":
with open("data/scored_substrates_positionwise.json", "r") as scores_file:
scores_dict = dict(json.load(scores_file))
elif score_type == "biopython":
with open("data/scored_substrates_biopython.json", "r") as scores_file:
scores_dict = dict(json.load(scores_file))
else:
raise ValueError(
"Invalid score_type: must be 'positionwise' or 'biopython'")
# Set the specificity threshold here:
# range from 0 to 1, 0 will include all protease,
# 0.25 will include the top 75% of protease,
# 0.42 will include the top 58% of proteases,
# 0.5 will include the top 50% of proteases,
# 0.8 will include the top 20% of proteases, etc
thresh = 0.6
proteases_dict = {}
# Always include proteases with only one substrate, as they are highly specific,
# unless otherwise specified
for k, v in substrates_dict.items():
if include_proteases_with_one_substrate:
for one_substrate_proteases in scores_dict["Proteases with only one substrate"]:
proteases_dict[one_substrate_proteases] = substrates_dict[one_substrate_proteases]
# exclude the cursed trypsin 1
if k not in scores_dict["Proteases with only one substrate"] and k != "trypsin 1":
if scores_dict["Proteases with more than one substrate"][k] >= thresh:
proteases_dict[k] = v
targets = {}
# Get the target name and target_sequence from the .txt file
with open("data/target_peptides.fasta", "r") as targets_file:
records = SeqIO.parse(targets_file, "fasta")
for record in records:
targets[str(record.id)] = str(record.seq)
return proteases_dict, targets
def batchalign(proteases_dict, aligner, target_name, target_seq):
# Create empty lists
proteases = []
rec_sites = []
scores = []
alignments = []
# Loop over each protease, substrates_list pair
for k, v in proteases_dict.items():
# Loop over each substrate sequence
for seq in v:
# This will look like this:
# ["Prot1", "Prot1", "Prot1", "Prot2", "Prot3",...]
# ["P1seq1", "P1seq2", "P1seq3", "P2seq1", "P3seq1",...]
proteases.append(k)
rec_sites.append(seq)
try:
# Align the protease substrate seq with the target peptide seq
aln = aligner.align(target_seq, seq)
score = aln.score
alignment = str(aln[0]).rstrip("\n")
except IndexError:
# IndexError raised on `aln[0]` if no alignments are made
score = None
alignment = "No significant alignment was made!"
scores.append(score)
alignments.append(alignment)
# Store all lists in a DF
df = pd.DataFrame(
{
"Protease": proteases,
"Recognition Site": rec_sites,
"Score": scores,
"Alignment": alignments}
)
# Sort the scores by descending order
df = df.sort_values(by='Score', ascending=False)
# Headers must have the form:
# >7BZ5_1|Chain_A_433-510|Spike_protein_S1
# for this to work properly, the file name will look like this:
# <currentDate&Time>-7BZ5_1Chain_A_433-510.xlsx
peptide_code = "".join(target_name.split("|")[:2])
now = datetime.now()
out_file_name = f"results/{now.year}-{now.month}-{now.day}_{now.hour}{now.minute}-{peptide_code}.xlsx"
# Write the output in an xlsx file so that the alignments can also be visualized
with pd.ExcelWriter(out_file_name, engine="xlsxwriter") as xlsxwriter:
df.to_excel(xlsxwriter, sheet_name="Sheet1", index=False)
workbook = xlsxwriter.book
format = workbook.add_format(
{
"text_wrap": True,
"font_name": "Consolas"
}
)
worksheet = xlsxwriter.sheets["Sheet1"]
worksheet.set_column("A:F", None, cell_format=format)
def multiprocessing(proteases_dict, aligner, targets_dict):
# MP magic, run every target peptide in parallel
processes = []
for target_name, target_seq in targets_dict.items():
p = mp.Process(target=batchalign, args=[
proteases_dict,
aligner,
target_name,
target_seq
])
p.start()
processes.append(p)
[process.join() for process in processes]
def move_targets():
# Appends the target peptides processed to the file data/old_target_peptides.txt
# and empties out the data/target_peptides.fasta
now = datetime.now()
now = f"{now.year}-{now.month}-{now.day}_{now.hour}{now.minute}"
with open("data/target_peptides.fasta", "r") as current_targets_file:
current_targets = current_targets_file.read()
with open("data/old_target_peptides.txt", "a") as old_targets_file:
old_targets_file.write(f"{now}\n")
old_targets_file.write(current_targets)
old_targets_file.write("\n")
with open("data/target_peptides.fasta", "w") as current_targets_file:
current_targets_file.write("")
def main():
# Run the functions
aligner = setup_aligner()
proteases_dict, targets_dict = get_params()
multiprocessing(proteases_dict, aligner, targets_dict)
move_targets()
if __name__ == '__main__':
main()