-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq_cleaner_stages.py
408 lines (312 loc) · 22 KB
/
seq_cleaner_stages.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
import os
import re
import sys
import time
from datetime import datetime as dt
import seq_cleaner_commands as q_com
import seq_cleaner_utilities 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_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()
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.clean_dir)
def host_filter_bowtie2(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())
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_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 = os.path.join(self.dir_obj.host_dir_top, ref_basename + "_host_bt2_mkr")
if(os.path.exists(host_bt2_marker_path)):
print("skipping Host filter:", ref_basename)
else:
command = ""
if(self.op_mode == "single"):
#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.clean_reads_bowtie2_simple_s(host_ref_path, ref_basename, self.dir_obj.start_s, host_bt2_marker_path)
#self.job_control.launch_and_create_v2_with_mp_store(script_path, command)
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.clean_reads_bowtie2_simple_p(host_ref_path, ref_basename, self.dir_obj.start_f, self.dir_obj.start_r, host_bt2_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_" + 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 = os.path.join(self.dir_obj.host_dir_top, ref_basename + "_sift_mkr")
sam_path = ""
score_out_path = ""
sam_name = ref_basename
sam_path = os.path.join(self.dir_obj.host_dir_sam, sam_name + ".sam")
if(self.op_mode == "single"):
score_out_path = os.path.join(self.dir_obj.host_dir_sam, sam_name + "_s_score_bt2.out")
else:
score_out_path = os.path.join(self.dir_obj.host_dir_sam, sam_name + "_p_score_bt2.out")
if(os.path.exists(sam_sift_marker_path)):
print(dt.today(), "skipping sam sift:", ref_basename)
else:
command = self.command_obj.sift_bwa_sam_command(sam_path, score_out_path, sam_sift_marker_path)
script_path = os.path.join(self.dir_obj.host_dir_top, "sam_sift_" + ref_basename + ".sh")
self.job_control.launch_and_create_v2_with_mp_store(script_path, command)
self.job_control.wait_for_mp_store()
#command = self.command_obj.clean_reads_reconcile(self.dir_obj.host_dir_data, self.dir_obj.host_dir_end, self.dir_obj.clean_dir_final_s, self.dir_obj.clean_dir_final_f, self.dir_obj.clean_dir_final_r, self.dir_obj.host_recon_mkr)
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)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.host_recon_job, command)
self.job_control.wait_for_mp_store()
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.host_dir)
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())
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_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_bwa_marker_path = os.path.join(self.dir_obj.host_dir_top, ref_basename + "_host_bwa_mkr")
if(os.path.exists(host_bwa_marker_path)):
print("skipping Host filter:", ref_basename)
else:
command = ""
if(self.op_mode == "single"):
#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.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)
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.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_" + 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 = os.path.join(self.dir_obj.host_dir_top, ref_basename + "_sift_mkr")
sam_path = ""
score_out_path = ""
sam_name = ref_basename
sam_path = os.path.join(self.dir_obj.host_dir_sam, sam_name + ".sam")
if(self.op_mode == "single"):
score_out_path = os.path.join(self.dir_obj.host_dir_sam, sam_name + "_s_score_bwa.out")
else:
score_out_path = os.path.join(self.dir_obj.host_dir_sam, sam_name + "_p_score_bwa.out")
if(os.path.exists(sam_sift_marker_path)):
print(dt.today(), "skipping sam sift:", ref_basename)
else:
command = self.command_obj.sift_bwa_sam_command(sam_path, score_out_path, sam_sift_marker_path)
script_path = os.path.join(self.dir_obj.host_dir_top, "sam_sift_" + ref_basename + ".sh")
self.job_control.launch_and_create_v2_with_mp_store(script_path, command)
self.job_control.wait_for_mp_store()
#command = self.command_obj.clean_reads_reconcile(self.dir_obj.host_dir_data, self.dir_obj.host_dir_end, self.dir_obj.clean_dir_final_s, self.dir_obj.clean_dir_final_f, self.dir_obj.clean_dir_final_r, self.dir_obj.host_recon_mkr)
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)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.host_recon_job, command)
self.job_control.wait_for_mp_store()
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.host_dir)
def assembly(self):
command = ""
if(not os.path.exists(self.dir_obj.assembly_mkr)):
if((self.path_obj.contig_tool == "metaspades") or (self.path_obj.contig_tool == "MetaSPAdes") or (self.path_obj.contig_tool == "mspades")):
print(dt.today(), "Choosing MetaSPADES for contig generation")
if(self.op_mode == "single"):
command = self.command_obj.metaspades_command_s(self.dir_obj.host_final_s, self.quality_encoding, self.dir_obj.assembly_dir_data, self.dir_obj.assembly_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.assembly_mspades_s_job, command)
else:
command = self.command_obj.metaspades_command_p(self.dir_obj.host_final_f, self.dir_obj.host_final_r, self.quality_encoding, self.dir_obj.assembly_dir_data, self.dir_obj.assembly_mkr)
self.job_control.launch_and_create_v2_with_mp_store(self.dir_obj.assembly_mspades_p_job, command)
self.job_control.wait_for_mp_store()
#if not(os.path.exists(self.dir_obj.assembly_contigs)):
# print(dt.today(), "metaspades failed to make contigs. entering backup mode")
elif((self.path_obj.contig_tool == "megahit") or (self.path_obj.contig_tool == "MEGAHIT") or self.path_obj.contig_tool == "mhit"):
print(dt.today(), "choosing MEGAHIT for contig generation")
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)
else:
command = self.command_obj.megahit_command_p(self.dir_obj.host_final_f, self.dir_obj.host_final_r, 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(), "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)):
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, command)
self.job_control.wait_for_mp_store()
else:
print(dt.today(), "skipping contig-read reconciliation")
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.assembly_dir)
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)
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.cct_bin_dir)
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()
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.mbat2_bin_dir)
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()
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.mbin2_bin_dir)
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()
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.mwrap_bin_r_dir)
def gtdbtk_classify(self):
print("running GTDB-tk classify")
for bin_choice in self.bin_tools:
marker_path = ""
job_path = ""
if(bin_choice == "cct"):
marker_path = self.dir_obj.gtdbtk_cct_mkr
job_path = self.dir_obj.gtdbtk_cct_job
elif(bin_choice == "mbat2"):
marker_path = self.dir_obj.gtdbtk_mbat2_mkr
job_path = self.dir_obj.gtdbtk_mbat2_job
elif(bin_choice == "mbin2"):
marker_path = self.dir_obj.gtdbtk_mbin2_mkr
job_path = self.dir_obj.gtdbtk_mbin2_job
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()
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.gtdbtk_class_dir)
def metawrap_quant(self):
print(dt.today(), "running metawrap quant bin")
marker_path = ""
job_path = ""
for bin_choice in self.bin_tools:
if(bin_choice == "cct"):
marker_path = self.dir_obj.mwrap_quant_cct_mkr
job_path = self.dir_obj.mwrap_quant_cct_job
elif(bin_choice == "mbat2"):
marker_path = self.dir_obj.mwrap_quant_mbat2_mkr
job_path = self.dir_obj.mwrap_quant_mbat2_job
elif(bin_choice == "mbin2"):
marker_path = self.dir_obj.mwrap_quant_mbin2_mkr
job_path = self.dir_obj.mwrap_quant_mbin2_job
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()
self.job_control.write_to_bypass_log(self.path_obj.bypass_log, self.path_obj.mwrap_quant_dir)