-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNAcodeWebCore_build_DB.py
276 lines (219 loc) · 10.1 KB
/
RNAcodeWebCore_build_DB.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
"""Build custom blast DB for parent jobs."""
import sys
import os
import re
import json
from shutil import rmtree, copyfile
from time import sleep
import RNAcodeWebCore
from RNAcodeWebCore import start_pipeline
from RNAcodeWebCore import eprint
from RNAcodeWebCore import vprint
from RNAcodeWebCore import write_status_file
from RNAcodeWebCore import notify_frontend
from RNAcodeWebCore import build_taxid_list
from RNAcodeWebCore import get_version
from RNAcodeWebCore import render_jinja
import SeqSelection
from SeqSelection import DB_SIZE
from SeqSelection import collect_sequences
from SeqSelection_build_DB import TAXIDMAPFILE_PATH
RNAcodeWebCore.VERBOSE = True
MAX_TARGET_SEQS_BLASTN = DB_SIZE * 1000
E_VALUE_CUTOFF = 0.01
# Word size for blast in each iteration.
WORD_SIZE_DIC = {1: 18, 2: 14, 3: 10, 4: 8, 5: 7}
# will be set in get_arguments()
JOB_ID = None
INPUT_SEQ_NUC = None
BLAST_WRAPPER_TEMPLATE = (
"#!/bin/bash\n\n"
"set -e\n\n"
f'blastn -db "{{}}" -query {{}} -max_target_seqs {MAX_TARGET_SEQS_BLASTN} '
f'-taxidlist {{}} -outfmt "{RNAcodeWebCore.OUTFMT}" -out {{}} -evalue {E_VALUE_CUTOFF} '
"-num_threads {} -max_hsps 1 -task blastn -word_size {}\n"
)
SEQSEL_WRAPPER_TEMPLATE = (
"#!/bin/bash\n\n"
"set -e\n\n"
"source $PYTHON_ENV/bin/activate\n\n"
'python3 SeqSelection_pipeline.py {} "{}"\n'
)
BUILD_DB_WRAPPER_TEMPLATE = (
"#!/bin/bash\n\n"
"set -e\n\n"
"makeblastdb -in {0} -parse_seqids -dbtype nucl -taxid_map {3} -title {1} -out {2}/{1}\n"
)
def get_arguments():
"""Get arguments from sys."""
global JOB_ID, INPUT_SEQ_NUC
JOB_ID = sys.argv[1]
RNAcodeWebCore.JOB_ID = JOB_ID
RNAcodeWebCore.STDOUT_FILE_PATH = RNAcodeWebCore.STDOUT_FILE_PATH.format(JOB_ID)
RNAcodeWebCore.STDERR_FILE_PATH = RNAcodeWebCore.STDERR_FILE_PATH.format(JOB_ID)
with open(RNAcodeWebCore.STDOUT_FILE_PATH, "w", encoding="UTF-8") as file_handle:
file_handle.write(" ".join(sys.argv) + "\n")
with open(RNAcodeWebCore.STDERR_FILE_PATH, "w", encoding="UTF-8") as file_handle:
file_handle.write("")
# Uncomment to print to stdout/stderr
# RNAcodeWebCore.STDOUT_FILE_PATH = None
# RNAcodeWebCore.STDERR_FILE_PATH = None
INPUT_SEQ_NUC = sys.argv[2]
RNAcodeWebCore.DB_TYPE = sys.argv[3]
if RNAcodeWebCore.DB_TYPE == "nt":
RNAcodeWebCore.BLAST_DB = "nt"
elif RNAcodeWebCore.DB_TYPE == "refseq":
RNAcodeWebCore.BLAST_DB = "ref_euk_rep_genomes ref_prok_rep_genomes ref_viroids_rep_genomes ref_viruses_rep_genomes"
RNAcodeWebCore.NCBI = SeqSelection.load_ncbi()
def make_readme():
"""Build README for workdir."""
readme_path = RNAcodeWebCore.README_PATH_TEMPLATE.format(JOB_ID)
context = {}
context["blast_version"] = get_version("blastn")
context["name_aligner"] = "Clustal Omega"
with open(readme_path, "w", encoding="UTF-8") as file_handle:
file_handle.write(render_jinja("templates/pipeline/README_build_db.md", context))
def init_work_dir():
"""Initialize work directory for analysis."""
line_length = 60
current_work_dir = RNAcodeWebCore.CURRENT_WORK_DIR_TEMPLATE.format(JOB_ID)
job_status_file = RNAcodeWebCore.JOB_STATUS_FILE_TEMPLATE.format(JOB_ID)
input_file_path = RNAcodeWebCore.INPUT_FILE_PATH_TEMPLATE.format(JOB_ID)
seq_selection_script_path = RNAcodeWebCore.SEQ_SELECTION_SCRIPT_PATH_TEMPLATE.format(JOB_ID)
seq_selection_module_path = RNAcodeWebCore.SEQ_SELECTION_MODULE_PATH_TEMPLATE.format(JOB_ID)
python_req_path = RNAcodeWebCore.PYTHON_REQ_PATH_TEMPLATE.format(JOB_ID)
selected_species_file = current_work_dir + "/" + SeqSelection.SELECTED_SPECIES_FILE_PATH
taxidmapfile_path = current_work_dir + "/" + TAXIDMAPFILE_PATH
custom_db_path = current_work_dir + "/" + RNAcodeWebCore.CUSTOM_DB_PATH
if not os.path.isdir(current_work_dir):
os.mkdir(current_work_dir)
os.mkdir(custom_db_path)
else:
rmtree(current_work_dir)
os.mkdir(current_work_dir)
os.mkdir(custom_db_path)
with open(input_file_path, "w", encoding="UTF-8") as file_handle:
file_handle.write(">Target\n")
seq = "\n".join(re.findall(f".{{1,{line_length}}}", INPUT_SEQ_NUC)) + "\n"
file_handle.write(seq)
job_status = {
"blastn_1": ["R", 0],
"seqSel_1": ["NS", 0],
"buildDB": ["NS", 0],
"fullJob": ["R", 0],
}
with open(job_status_file, "w", encoding="UTF-8") as file_handle:
json.dump(job_status, file_handle, indent=4)
with open(selected_species_file, "w", encoding="UTF-8") as file_handle:
file_handle.write("None")
with open(taxidmapfile_path, "w", encoding="UTF-8") as file_handle:
file_handle.write("")
copyfile("./templates/pipeline/python_requirements.txt", python_req_path)
copyfile("./SeqSelection.py", seq_selection_module_path)
copyfile("./SeqSelection_build_DB.py", seq_selection_script_path)
make_readme()
def blastn(iteration):
"""Call blast."""
job_type = f"blastn_{iteration}"
input_file_path = RNAcodeWebCore.INPUT_FILE_PATH_TEMPLATE.format(JOB_ID)
blast_result_path = RNAcodeWebCore.BLAST_RESULT_PATH_TEMPLATE.format(JOB_ID, iteration)
blast_wrapper_path = RNAcodeWebCore.BLAST_WRAPPER_PATH_TEMPLATE.format(JOB_ID, iteration)
taxids_path = RNAcodeWebCore.TAXIDS_PATH_TEMPLATE.format(JOB_ID, iteration)
error_file = RNAcodeWebCore.SLURM_ERROR_TEMPLATE.format(JOB_ID, job_type)
idle_cores = RNAcodeWebCore.get_number_idles_cpus()
cores = [v for k, v in RNAcodeWebCore.CORE_USE_DIC.items() if idle_cores in k][0]
build_taxid_list(iteration)
blast_wrapper = BLAST_WRAPPER_TEMPLATE.format(
RNAcodeWebCore.BLAST_DB,
input_file_path.split("/")[-1],
taxids_path.split("/")[-1],
blast_result_path.split("/")[-1],
cores,
WORD_SIZE_DIC[iteration],
)
with open(blast_wrapper_path, "w", encoding="UTF-8") as file_handle:
file_handle.write(blast_wrapper)
if not RNAcodeWebCore.slurm_batch(blast_wrapper_path, job_type, cores=cores):
eprint("Blast exited with error!")
with open(error_file, "r", encoding="UTF-8") as file_handle:
eprint(file_handle.read())
raise RNAcodeWebCore.PipelineError
def build_db():
"""Build blast db."""
job_type = "buildDB"
blast_db_fasta_path = RNAcodeWebCore.BLAST_DB_FASTA_PATH_TEMPLATE.format(JOB_ID)
custom_db_path = RNAcodeWebCore.CUSTOM_DB_PATH
build_db_wrapper_path = RNAcodeWebCore.BUILD_DB_WRAPPER_PATH_TEMPLATE.format(JOB_ID)
error_file = RNAcodeWebCore.SLURM_ERROR_TEMPLATE.format(JOB_ID, job_type)
build_db_wrapper = BUILD_DB_WRAPPER_TEMPLATE.format(
blast_db_fasta_path.split("/")[-1],
JOB_ID,
custom_db_path,
TAXIDMAPFILE_PATH,
)
with open(build_db_wrapper_path, "w", encoding="UTF-8") as file_handle:
file_handle.write(build_db_wrapper)
if not RNAcodeWebCore.slurm_batch(build_db_wrapper_path, job_type):
eprint("makeblastdb exited with error!")
with open(error_file, "r", encoding="UTF-8") as file_handle:
eprint(file_handle.read())
raise RNAcodeWebCore.PipelineError
def seq_selection(iteration):
"""2. Step of analysis. Select candidates from blast output."""
job_type = f"seqSel_{iteration}"
seqsel_wrapper_path = RNAcodeWebCore.SEQSEL_WRAPPER_PATH_TEMPLATE.format(JOB_ID, iteration)
error_file = RNAcodeWebCore.SLURM_ERROR_TEMPLATE.format(JOB_ID, job_type)
with open(seqsel_wrapper_path, "w", encoding="UTF-8") as file_handle:
file_handle.write(SEQSEL_WRAPPER_TEMPLATE.format(iteration, RNAcodeWebCore.BLAST_DB))
if not RNAcodeWebCore.slurm_batch(seqsel_wrapper_path, job_type):
eprint("Region selection exited with error!")
with open(error_file, "r", encoding="UTF-8") as file_handle:
eprint(file_handle.read())
raise RNAcodeWebCore.PipelineError
def count_last_blast_results(iteration):
"""Count the number of hits found by blastn in the last search."""
blast_result_path = RNAcodeWebCore.BLAST_RESULT_PATH_TEMPLATE.format(JOB_ID, iteration)
with open(blast_result_path, "r", encoding="UTF-8") as file_handle:
return len(list(file_handle.readlines()))
def concat_sequences_fasta(iteration):
"""Concatinate all selected sequences in one."""
blast_db_fasta_path = RNAcodeWebCore.BLAST_DB_FASTA_PATH_TEMPLATE.format(JOB_ID)
with open(blast_db_fasta_path, "w", encoding="UTF-8") as db_handle:
for i in range(1, iteration + 1):
selected_sequences_path = f"{RNAcodeWebCore.SELECTED_SEQUENCES_PATH_TEMPLATE.format(JOB_ID, i)}"
if not os.path.isfile(selected_sequences_path):
continue
with open(selected_sequences_path, "r", encoding="UTF-8") as seq_handle:
db_handle.write(
re.sub(r":c*[0-9]+-[0-9]+", "", seq_handle.read())
)
def main():
"""Build custom blast DB for parent jobs."""
get_arguments()
vprint("Initializing work directory")
init_work_dir()
for iteration in range(1, 6):
write_status_file(f"seqSel_{iteration}.{JOB_ID}", ["NS", "0"])
write_status_file(f"blastn_{iteration}.{JOB_ID}", ["NS", "0"])
vprint(f"{iteration}. Iteration of selection")
vprint("Start blast")
blastn(iteration)
vprint("Select sequences")
seq_selection(iteration)
num_seqs = len(collect_sequences(iteration + 1, current_work_dir=RNAcodeWebCore.CURRENT_WORK_DIR_TEMPLATE.format(JOB_ID)))
vprint(f"{num_seqs} sequences found.")
if num_seqs >= DB_SIZE:
break
if num_seqs < RNAcodeWebCore.MIN_NUM_SEQ:
write_status_file(f"fullJob.{JOB_ID}", ["F", num_seqs])
raise RNAcodeWebCore.PipelineFailed
concat_sequences_fasta(iteration)
build_db()
# Wait sometime. For reason beyond my comprehension some files seem to be
# not written to disk when the process ended. Might cause error in child
# processes.
sleep(4)
write_status_file(f"fullJob.{JOB_ID}", ["CD", num_seqs])
notify_frontend(JOB_ID)
if __name__ == "__main__":
start_pipeline(main)