-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquackers_stages.py
More file actions
395 lines (322 loc) · 22.5 KB
/
quackers_stages.py
File metadata and controls
395 lines (322 loc) · 22.5 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import os
import re
import sys
import time
from datetime import datetime as dt
import quackers_commands as q_com
import MetaPro_utilities_v2 as mp_util
class q_stage:
def __init__(self, out_path, path_obj, dir_obj, args_pack):
self.path_obj = path_obj
self.dir_obj = dir_obj
self.bin_tools = ["cct", "mbat2", "mbin2"]
self.start_s_path = args_pack["s_path"]
self.start_f_path = args_pack["p1_path"]
self.start_r_path = args_pack["p2_path"]
self.job_control = mp_util.mp_util(out_path, self.path_obj.bypass_log_name)
self.op_mode = args_pack["op_mode"]
self.quality_encoding = "64"
if(self.op_mode == "single"):
self.quality_encoding = self.job_control.determine_encoding(self.start_s_path)
else:
self.quality_encoding = self.job_control.determine_encoding(self.start_f_path)
self.command_obj = q_com.command_obj(path_obj, dir_obj, self.quality_encoding)
self.mspades_contig_fail = False
if(os.path.exists(self.dir_obj.assembly_alt_contigs)):
print(dt.today(), "metaspades contigs overrided with megahit-backup")
self.dir_obj.assembly_contigs = self.dir_obj.assembly_alt_contigs
self.hosts_bypassed = False
def check_host_bypass(self):
#checks if there are hosts. if not, trigger the bypass
list_of_hosts = sorted(self.path_obj.config["hosts"].keys())
if(len(list_of_hosts) == 0):
#skip host-cleaning. move data
self.hosts_bypassed = True
print(dt.today(), "no hosts used: bypassing host filter")
if(self.op_mode == "single"):
self.dir_obj.host_final_s = self.dir_obj.clean_dir_final_s
#self.dir_obj.host_final_s = self.dir_obj.start_s
elif(self.op_mode == "paired"):
self.dir_obj.host_final_f = self.dir_obj.clean_dir_final_f
self.dir_obj.host_final_r = self.dir_obj.clean_dir_final_r
self.dir_obj.host_final_s = self.dir_obj.clean_dir_final_s
#self.dir_obj.host_final_f = self.dir_obj.start_f
#self.dir_obj.host_final_r = self.dir_obj.start_r
def low_quality_filter(self):
if (os.path.exists(self.dir_obj.clean_dir_mkr)):
print(dt.today(), "skipping cleaning step")
else:
command = self.command_obj.adapterremoval_command(self.quality_encoding, self.dir_obj.clean_dir_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.clean_dir_job, command)
self.job_control.wait_for_mp_store()
if(self.dir_obj.check_mkr_clean):
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.clean_dir)
else:
message_line = str(dt.today()) + " broken at: "+ self.path_obj.clean_dir
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, message_line)
sys.exit("bad_stage")
def host_filter(self):
#launch for each new ref path
#walk through each host and launch a bwa job
list_of_hosts = sorted(self.path_obj.config["hosts"].keys())
list_of_mkrs = list()
if(len(list_of_hosts) == 0):
#skip host-cleaning. move data
self.hosts_bypassed = True
print(dt.today(), "no hosts used: bypassing host filter")
if(self.op_mode == "single"):
self.dir_obj.host_final_s = self.dir_obj.clean_dir_final_s
#self.dir_obj.host_final_s = self.dir_obj.start_s
elif(self.op_mode == "paired"):
self.dir_obj.host_final_f = self.dir_obj.clean_dir_final_f
self.dir_obj.host_final_r = self.dir_obj.clean_dir_final_r
self.dir_obj.host_final_s = self.dir_obj.clean_dir_final_s
#self.dir_obj.host_final_f = self.dir_obj.start_f
#self.dir_obj.host_final_r = self.dir_obj.start_r
else:
for host_key in list_of_hosts:
host_ref_path = self.path_obj.hosts_path_dict[host_key]
ref_basename = os.path.basename(host_ref_path)
ref_basename = ref_basename.split(".")[0]
host_bt2_marker_path_p = os.path.join(self.dir_obj.host_dir_top, ref_basename + "_p_host_bt2_mkr")
host_bt2_marker_path_s = os.path.join(self.dir_obj.host_dir_top, ref_basename + "_s_host_bt2_mkr")
list_of_mkrs.append(host_bt2_marker_path_p)
list_of_mkrs.append(host_bt2_marker_path_s)
if(os.path.exists(host_bt2_marker_path_p)):
print("skipping Host filter P:", ref_basename)
else:
#command = self.command_obj.clean_reads_bwa_simple_p(host_ref_path, ref_basename, self.dir_obj.clean_dir_final_f, self.dir_obj.clean_dir_final_r, host_bwa_marker_path)
command = self.command_obj.host_rem_bt2_p(host_ref_path, ref_basename, self.dir_obj.clean_dir_final_f, self.dir_obj.clean_dir_final_r, host_bt2_marker_path_p)
#command = self.command_obj.clean_reads_bwa_simple_p(host_ref_path, ref_basename, self.dir_obj.start_f, self.dir_obj.start_r, host_bwa_marker_path)
#self.job_control.launch_and_create_v2_with_mp_store(script_path, command)
script_path = os.path.join(self.dir_obj.host_dir_top, "host_filter_p_" + ref_basename + ".sh")
self.job_control.launch_and_create_v2_with_mp_store(script_path, command)
if(os.path.exists(host_bt2_marker_path_s)):
print(dt.today(), "skipping host filter S:", ref_basename)
else:
#command = self.command_obj.clean_reads_bwa_simple_s(host_ref_path, ref_basename, self.dir_obj.clean_dir_final_s, host_bwa_marker_path)
command = self.command_obj.host_rem_bt2_s(host_ref_path, ref_basename, self.dir_obj.clean_dir_final_s, host_bt2_marker_path_s)
#command = self.command_obj.clean_reads_bwa_simple_s(host_ref_path, ref_basename, self.dir_obj.start_s, host_bwa_marker_path)
#self.job_control.launch_and_create_v2_with_mp_store(script_path, command)
script_path = os.path.join(self.dir_obj.host_dir_top, "host_filter_s_" + ref_basename + ".sh")
self.job_control.launch_and_create_v2_with_mp_store(script_path, command)
self.job_control.wait_for_mp_store()
for host_key in list_of_hosts:
host_ref_path = self.path_obj.hosts_path_dict[host_key]
ref_basename = os.path.basename(host_ref_path)
ref_basename = ref_basename.split(".")[0]
sam_sift_marker_path_p = os.path.join(self.dir_obj.host_dir_top, ref_basename + "_sift_mkr_p")
sam_sift_marker_path_s = os.path.join(self.dir_obj.host_dir_top, ref_basename + "_sift_mkr_s")
list_of_mkrs.append(sam_sift_marker_path_p)
list_of_mkrs.append(sam_sift_marker_path_s)
sam_path = ""
score_out_path = ""
sam_name = ref_basename
sam_path_p = os.path.join(self.dir_obj.host_dir_sam, sam_name + "_p.sam")
sam_path_s = os.path.join(self.dir_obj.host_dir_sam, sam_name + "_s.sam")
score_out_path_s = os.path.join(self.dir_obj.host_dir_sam, sam_name + "_s_score_bt2.out")
score_out_path_p = os.path.join(self.dir_obj.host_dir_sam, sam_name + "_p_score_bt2.out")
if(os.path.exists(sam_sift_marker_path_p)):
print(dt.today(), "skipping sam sift on P:", ref_basename)
else:
#if(os.path.exists(score_out_path_p)):
command = self.command_obj.sift_bt2_sam_command(sam_path_p, score_out_path_p, sam_sift_marker_path_p)
script_path = os.path.join(self.dir_obj.host_dir_top, "sam_sift_p_" + ref_basename + ".sh")
self.job_control.launch_and_create_v2_with_mp_store(script_path, command)
if(os.path.exists(sam_sift_marker_path_s)):
print(dt.today(), "skipping SAM SIFT on s")
else:
command = self.command_obj.sift_bt2_sam_command(sam_path_s, score_out_path_s, sam_sift_marker_path_s)
script_path = os.path.join(self.dir_obj.host_dir_top, "sam_sift_s_" + ref_basename + ".sh")
self.job_control.launch_and_create_v2_with_mp_store(script_path, command)
self.job_control.wait_for_mp_store()
if(os.path.exists(self.dir_obj.host_recon_mkr_p)):
print(dt.today(), "Skipping host read reconcile P")
else:
list_of_mkrs.append(self.dir_obj.host_recon_mkr_p)
command = self.command_obj.clean_reads_reconcile(self.dir_obj.host_dir_sam, self.dir_obj.host_dir_end, "None", self.dir_obj.clean_dir_final_f, self.dir_obj.clean_dir_final_r, self.dir_obj.host_recon_mkr_p)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.host_recon_job_p, command)
if(os.path.exists(self.dir_obj.host_recon_mkr_s)):
print(dt.today(), "skipping host read reconcile S")
else:
if(os.path.getsize(self.dir_obj.clean_dir_final_s) > 1000):
list_of_mkrs.append(self.dir_obj.host_recon_mkr_s)
print("FILE:", self.dir_obj.clean_dir_final_s, " is not empty:", os.path.getsize(self.dir_obj.clean_dir_final_s))
print(dt.today(), "working on clean reads reconcile: S")
command = self.command_obj.clean_reads_reconcile(self.dir_obj.host_dir_sam, self.dir_obj.host_dir_end, self.dir_obj.clean_dir_final_s, "None", "None", self.dir_obj.host_recon_mkr_s)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.host_recon_job_s, command)
else:
print("size of clean dir final s:", os.path.getsize(self.dir_obj.clean_dir_final_s))
print("file:", self.dir_obj.clean_dir_final_s)
time.sleep(10)
print(dt.today(), "No singletons for host filtering. Skip")
#command = self.command_obj.clean_reads_reconcile(self.dir_obj.host_dir_data, self.dir_obj.host_dir_end, self.dir_obj.start_s, self.dir_obj.start_f, self.dir_obj.start_r, self.dir_obj.host_recon_mkr)
print(dt.today(), "working on clean reads reconcile")
self.job_control.wait_for_mp_store()
if(self.dir_obj.check_mkr_host(list_of_mkrs)):
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.host_dir)
else:
message_line = str(dt.today()) + " broken at: " + self.path_obj.host_dir
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, message_line)
sys.exit("bad_stage")
def assembly(self):
if(not os.path.exists(self.dir_obj.assembly_mkr)):
command = ""
if(("megahit" in self.path_obj.contig_tool) or ("MEGAHIT" in self.path_obj.contig_tool)):
print(dt.today(), "choosing MEGAHIT for contig generation")
time.sleep(3)
if(self.op_mode == "single"):
command = self.command_obj.megahit_command_s(self.dir_obj.host_final_s, self.dir_obj.assembly_alt_dir_data, self.dir_obj.assembly_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.assembly_mhit_s_job, command)
print("command:", command)
else:
print("working in contigs paired")
time.sleep(3)
if(os.path.getsize(self.dir_obj.clean_dir_final_s) > 1000):
command = self.command_obj.megahit_command_p(self.dir_obj.host_final_f, self.dir_obj.host_final_r, self.dir_obj.host_final_s, self.dir_obj.assembly_alt_dir_data, self.dir_obj.assembly_mkr)
print("command:", command)
else:
command = self.command_obj.megahit_command_p(self.dir_obj.host_final_f, self.dir_obj.host_final_r, "None", self.dir_obj.assembly_alt_dir_data, self.dir_obj.assembly_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.assembly_mhit_p_job, command)
self.job_control.wait_for_mp_store()
#set new contigs
self.dir_obj.assembly_contigs = self.dir_obj.assembly_alt_contigs
else:
print(dt.today(), "MetaSPADES discontinued. Use MegaHit")
sys.exit("Stop")
else:
print(dt.today(), "skipping: contig assembly")
if(not os.path.exists(self.dir_obj.assembly_bt2_idx_mkr)):
print(dt.today(), "indexing contigs for BT2")
print("using:", self.dir_obj.assembly_bt2_idx)
command = self.command_obj.bowtie2_index_ref_command(self.dir_obj.assembly_contigs, self.dir_obj.assembly_bt2_idx, self.dir_obj.assembly_bt2_idx_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.assembly_bwa_idx_job, command)
self.job_control.wait_for_mp_store()
else:
print(dt.today(), "skipping BT2 contig indexing")
if(not os.path.exists(self.dir_obj.assembly_pp_mkr)):
command = ""
if(self.op_mode == "single"):
command = self.command_obj.clean_reads_bowtie2_command_s(self.dir_obj.assembly_bt2_idx, self.dir_obj.host_final_s, self.dir_obj.assembly_pp_mkr)
else:
command = self.command_obj.clean_reads_bowtie2_command_p(self.dir_obj.assembly_bt2_idx, self.dir_obj.host_final_f, self.dir_obj.host_final_r, self.dir_obj.assembly_pp_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.assembly_pp_job, command)
self.job_control.wait_for_mp_store()
else:
print(dt.today(), "skipping BT2 align contigs")
if(not os.path.exists(self.dir_obj.assembly_scan_sam_mkr)):
command = self.command_obj.bt2_scan_sam(self.dir_obj.assembly_scan_sam_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.assembly_scan_sam_job, command)
self.job_control.wait_for_mp_store()
else:
print(dt.today(), "skipping BT2 sam/bam business")
if(not os.path.exists(self.dir_obj.assembly_reconcile_mkr_p)):
command = self.command_obj.contig_reconcile(self.dir_obj.assembly_score_out, "None", self.dir_obj.host_final_f, self.dir_obj.host_final_r, "None", self.dir_obj.assembly_final_f, self.dir_obj.assembly_final_r, self.dir_obj.assembly_reconcile_mkr_p)
#command = self.command_obj.contig_reconcile(self.dir_obj.assembly_score_out, self.dir_obj.assembly_dir_data, self.dir_obj.host_final_s, self.dir_obj.host_final_f, self.dir_obj.host_final_r, self.dir_obj.assembly_reconcile_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.assembly_recon_job_p, command)
else:
print(dt.today(), "skipping contig-read reconciliation P")
if(not os.path.exists(self.dir_obj.assembly_reconcile_mkr_s)):
#command = self.command_obj.contig_reconcile(self.dir_obj.assembly_score_out, self.dir_obj.assembly_dir_data, "None", self.dir_obj.host_final_f, self.dir_obj.host_final_r, self.dir_obj.assembly_reconcile_mkr)
command = self.command_obj.contig_reconcile(self.dir_obj.assembly_score_out, self.dir_obj.host_final_s, "None", "None", self.dir_obj.assembly_final_s, "None", "None", self.dir_obj.assembly_reconcile_mkr_s)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.assembly_recon_job_s, command)
else:
print(dt.today(), "skipping contig-read reconciliation S")
self.job_control.wait_for_mp_store()
if(self.dir_obj.check_mkr_assembly()):
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.assembly_dir)
else:
message_line = str(dt.today()) + " broken at: " + self.path_obj.assembly_dir
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, message_line)
#sys.exit("bad_stage")
def concoct_binning(self):
#print(dt.today(), "temp holder")
#requires adjusting headers and junk before sending off to concoct.
#code just removes the dangling portion of the ID in each contig
if(not os.path.exists(self.dir_obj.cct_prep_mkr)):
command = self.command_obj.concoct_prep_command(self.dir_obj.cct_prep_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.cct_prep_job_path, command)
self.job_control.wait_for_mp_store()
else:
print(dt.today(), "skipping concoct prep")
if(not os.path.exists(self.dir_obj.cct_mkr)):
command = self.command_obj.concoct_command(self.dir_obj.cct_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.cct_job_path, command)
self.job_control.wait_for_mp_store()
else:
print(dt.today(), "skipping concoct binning")
if(not os.path.exists(self.dir_obj.cct_checkm_mkr)):
print(dt.today(), "running checkm")
command = self.command_obj.checkm_command(self.dir_obj.cct_checkm_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.cct_checkm_job_path, command)
self.job_control.wait_for_mp_store()
else:
print(dt.today(), "skipping checkm")
print(self.dir_obj.cct_checkm_mkr)
if(self.dir_obj.check_mkr_bin_cct()):
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.cct_bin_dir)
else:
message_line = str(dt.today()) + " broken at: " + self.path_obj.cct_bin_dir
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, message_line)
sys.exit("bad_stage")
def metabat2_binning(self):
print(dt.today(), "running metawrap-binning: metabat2")
command = self.command_obj.metabat2_bin_command(self.op_mode, self.hosts_bypassed, self.dir_obj.mbat2_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.mbat2_job, command)
self.job_control.wait_for_mp_store()
if(self.dir_obj.check_mkr_bin_mbat2()):
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.mbat2_bin_dir)
else:
message_line = str(dt.today()) + " broken at: " + self.path_obj.mbat2_bin_dir
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, message_line)
sys.exit("bad_stage")
def maxbin2_binning(self):
print(dt.today(), "running metawrap-binning: maxbin2")
command = self.command_obj.maxbin2_bin_command(self.op_mode, self.hosts_bypassed, self.dir_obj.mbin2_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.mbin2_job, command)
self.job_control.wait_for_mp_store()
if(self.dir_obj.check_mkr_bin_mbin2()):
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.mbin2_bin_dir)
else:
message_line = str(dt.today()) + " broken at: " + self.path_obj.mbin2_bin_dir
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, message_line)
sys.exit("bad_stage")
def metawrap_bin_refine(self):
print("running metawrap bin_refinement")
command = self.command_obj.metawrap_bin_refinement_command(self.dir_obj.mwrap_bin_r_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.mwrap_bin_r_job, command)
self.job_control.wait_for_mp_store()
if(self.dir_obj.check_mkr_mwrap_bin_r()):
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.mwrap_bin_r_dir)
else:
message_line = str(dt.today()) + " broken at: " + self.path_obj.mwrap_bin_r_dir
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, message_line)
sys.exit("bad_stage")
def gtdbtk_classify(self):
print("running GTDB-tk classify")
marker_path = self.dir_obj.gtdbtk_mkr
job_path = self.dir_obj.gtdbtk_job
bin_choice = "refined"
command = self.command_obj.gtdbtk_command(bin_choice, marker_path)
self.job_control.launch_and_create_v2_with_mp_store(job_path, command)
self.job_control.wait_for_mp_store()
if(self.dir_obj.check_mkr_gtdbtk()):
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.gtdbtk_class_dir)
else:
message_line = str(dt.today()) + " broken at: " + self.path_obj.gtdbtk_class_dir
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, message_line)
sys.exit("bad_stage")
def metawrap_quant(self):
print(dt.today(), "running metawrap quant bin")
marker_path = self.dir_obj.mwrap_quant_mkr
job_path = self.dir_obj.mwrap_quant_job
bin_choice = "refined"
command = self.command_obj.metawrap_quantify_command(bin_choice, self.dir_obj.host_final_f, self.dir_obj.host_final_r, self.dir_obj.host_final_s, marker_path)
self.job_control.launch_and_create_v2_with_mp_store(job_path, command)
self.job_control.wait_for_mp_store()
if(self.dir_obj.check_mkr_mwrap_quant()):
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.mwrap_quant_dir)
else:
message_line = str(dt.today()) + " broken at: " + self.path_obj.mwrap_quant_dir
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, message_line)
sys.exit("bad_stage")