-
Notifications
You must be signed in to change notification settings - Fork 0
/
bowtie_pipeline.py
270 lines (200 loc) · 10.2 KB
/
bowtie_pipeline.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
import luigi
from os import listdir, makedirs
from os.path import join as djoin
from os.path import abspath, basename, exists
import subprocess
import shutil
from assembly_support import (
barcode_sorter,
bam_to_annotate,
add_barcodes,
subdiv_annotations,
concat_annotations,
fastq_enhance
)
from evaluate_support import (
fastq_to_table,
generate_summaries
)
# PYTHONPATH='.' luigi --module X_pipeline de_Novo_Assembly --local-scheduler
def check_make(curr_dir, sub):
outdir = djoin(curr_dir, sub)
if not exists(outdir):
makedirs(outdir)
return outdir
class Single_Sort_FastQ(luigi.Task):
in_dir = luigi.Parameter()
prefix = luigi.Parameter()
direction = luigi.Parameter()
enhance_mode = luigi.BoolParameter(parsing = luigi.BoolParameter.EXPLICIT_PARSING)
def output(self):
return luigi.LocalTarget(djoin(gp.read_dir, '.'.join([self.prefix + '_bsort', self.direction, 'fastq'])))
def run(self):
if self.enhance_mode:
yield Single_Enhanced_FastQ(enh_csv = djoin(gp.enhd_cld_dir, '.'.join(['bsort', self.direction, 'csv'])), direction = self.direction)
barcode_sorter(djoin(self.in_dir, '.'.join([self.prefix, self.direction, 'fastq'])), gp.read_dir, False)
class Sort_FastQs_by_Barcode(luigi.Task):
"""Sort raw barcoded reads by barcode."""
def requires(self):
return [Single_Sort_FastQ(in_dir = gp.read_dir, prefix = gp.base_prefix, direction = 'R1', enhance_mode = False), Single_Sort_FastQ(in_dir = gp.read_dir, prefix = gp.base_prefix, direction = 'R2', enhance_mode = False)]
def output(self):
return self.input()
class Map_Original_Clouds(luigi.Task):
"""Map barcoded reads to the metagenome and add barcode information."""
reference = luigi.Parameter()
ids_to_names = luigi.Parameter()
fragments = luigi.BoolParameter(parsing = luigi.BoolParameter.EXPLICIT_PARSING)
def requires(self):
return Sort_FastQs_by_Barcode()
def output(self):
return luigi.LocalTarget(djoin(gp.work_dir, 'bsort.final.csv'))
def run(self):
raw_sam = djoin(gp.work_dir, 'bsort.sam')
raw_bam = djoin(gp.work_dir, 'bsort_raw.bam')
srt_bam = djoin(gp.work_dir, 'bsort.bam')
map_step = subprocess.run(['bowtie2', '--sensitive-local', '-p', gp.num_threads, '-x', self.reference, '-1', self.input()[0].path, '-2', self.input()[1].path, '-S', raw_sam])
bam_step = subprocess.run(['samtools', 'view', '-hb', '-f', '0', '-F', '256', raw_sam, '-o', raw_bam]) # Primary alignments only
srt_step = subprocess.run(['samtools', 'sort', raw_bam, '-o', srt_bam])
idx_step = subprocess.run(['samtools', 'index', srt_bam])
bam_to_annotate(srt_bam, self.ids_to_names, gp.work_dir, self.fragments)
add_barcodes(self.input()[0].path, djoin(gp.work_dir, 'bsort.csv'), gp.work_dir)
class Subdivide_Original_Clouds(luigi.Task):
"""Split species/fragment/scaffold-based annotations into multiple chunks for faster cloud generation/FastQ processing."""
def requires(self):
return Map_Original_Clouds()
def output(self):
return luigi.LocalTarget(djoin(gp.work_dir, 'subdivide_original_clouds.out'))
def run(self):
subdiv_annotations(self.input().path, gp.num_chunks, gp.orig_map_dir)
with self.output().open('w') as of:
of.write(f'{gp.num_chunks} chunks were created')
class Single_Enhanced_Chunk(luigi.Task):
chunk_num = luigi.IntParameter()
fragments = luigi.BoolParameter(parsing = luigi.BoolParameter.EXPLICIT_PARSING)
def __init__(self, *args, **kwargs):
super(Single_Enhanced_Chunk, self).__init__(*args, **kwargs)
self.chunk_file_name = '.'.join(['bsort', str(self.chunk_num), 'csv'])
def requires(self):
return Subdivide_Original_Clouds()
def output(self):
return luigi.LocalTarget(djoin(gp.enhd_cld_dir, 'R1', self.chunk_file_name)), luigi.LocalTarget(djoin(gp.enhd_cld_dir, 'R2', self.chunk_file_name))
def run(self):
concat_annotations(djoin(gp.orig_map_dir, self.chunk_file_name), gp.enhd_cld_dir, self.fragments)
class Generate_Enhanced_Chunks(luigi.Task):
"""Convert reference sequence (and position) information to enhanced read clouds."""
def __init__(self, *args, **kwargs):
super(Generate_Enhanced_Chunks, self).__init__(*args, **kwargs)
self.enhd_file_prefix = djoin(gp.enhd_cld_dir, 'bsort')
def requires(self):
for i in range(gp.num_chunks):
yield Single_Enhanced_Chunk(chunk_num = i)
def output(self):
return [luigi.LocalTarget(self.enhd_file_prefix + '.R1.csv'), luigi.LocalTarget(self.enhd_file_prefix + '.R2.csv')]
def run(self):
for i, j in enumerate(['R1','R2']):
enhd_file_path = '.'.join([self.enhd_file_prefix, j, 'csv'])
with open(enhd_file_path, 'w') as outf:
for chunk_file in listdir(djoin(gp.enhd_cld_dir, j)):
with open(djoin(gp.enhd_cld_dir, j, chunk_file)) as f:
for l in f:
outf.write(l)
if gp.deconv_type == gp.gstd_prefix:
shutil.copy(enhd_file_path, djoin(gp.analyses_dir, '.'.join([gp.gstd_prefix, j, 'csv'])))
class Single_Enhanced_FastQ(luigi.Task):
enh_csv = luigi.Parameter()
direction = luigi.Parameter()
def requires(self):
return Generate_Enhanced_Chunks()
def output(self):
return luigi.LocalTarget(djoin(gp.enhd_cld_dir, '.'.join([gp.edit_prefix, self.direction, 'fastq'])))
def run(self):
fastq_enhance(djoin(gp.read_dir, '.'.join([gp.base_prefix + '_bsort', self.direction, 'fastq'])), self.enh_csv, gp.enhd_cld_dir, gp.edit_prefix)
class Enhance_Original_FastQs(luigi.Task):
"""Create read files with enhanced read clouds."""
def requires(self):
for i in ['R1','R2']:
yield Single_Sort_FastQ(in_dir = gp.enhd_cld_dir, prefix = gp.edit_prefix, direction = i, enhance_mode = True)
def output(self):
return [luigi.LocalTarget(djoin(gp.read_dir, gp.final_prefix + '.R1.fastq')), luigi.LocalTarget(djoin(gp.read_dir, gp.final_prefix + '.R2.fastq'))]
class cloudSPAdes(luigi.Task):
"""Run cloudSPAdes on the bwt-enhanced read clouds."""
memory = luigi.Parameter()
def requires(self):
return Enhance_Original_FastQs()
def output(self):
return luigi.LocalTarget(djoin(gp.analyses_dir, gp.final_prefix + '.scaffolds.fasta'))
def run(self):
subprocess.run(['/home/lam4003/bin/spades/assembler/spades.py', '--meta', '--only-assembler', '--gemcode1-1', self.input()[0].path, '--gemcode1-2', self.input()[1].path, '--search-distance', '0', '--size-cutoff', '6', '-t', gp.num_threads, '-m', self.memory, '-o', gp.cldspades_dir])
shutil.copy(djoin(gp.cldspades_dir, 'scaffolds.fasta'), self.output().path)
class Single_FastQ_to_Table(luigi.Task):
direction = luigi.Parameter()
def requires(self):
return Enhance_Original_FastQs()
def output(self):
return luigi.LocalTarget(djoin(gp.analyses_dir, '.'.join([gp.final_prefix, self.direction, 'csv'])))
def run(self):
fastq_to_table(djoin(gp.read_dir, '.'.join([gp.final_prefix, self.direction, 'fastq'])),
djoin(gp.analyses_dir, '.'.join([gp.gstd_prefix, self.direction, 'csv'])), gp.analyses_dir)
class Summarize_FastQ_Statistics(luigi.Task):
"""Generate size, purity, and entropy summary statistics from enhanced reads."""
def requires(self):
# Match predicted read clouds to actual (reference sequence) read clouds
fastq_tbls = []
for i in ['R1','R2']:
fastq_tbls.append(Single_FastQ_to_Table(direction = i))
return fastq_tbls
def output(self):
return luigi.LocalTarget(djoin(gp.analyses_dir, gp.final_prefix + '.statistics.csv'))
def run(self):
# Generate read cloud quality statistics tables
generate_summaries(self.input()[0].path, self.input()[1].path, gp.analyses_dir, None)
class de_Novo_Assembly(luigi.WrapperTask):
"""Top-level function calling cloudSPAdes and enhancement summaries."""
read_dir = luigi.Parameter()
prefix = luigi.Parameter()
master_dir = luigi.Parameter()
deconv_type = luigi.Parameter()
gold_standard = luigi.Parameter()
num_threads = luigi.Parameter()
num_chunks = luigi.IntParameter()
def requires(self):
gp.set_params(self.read_dir, self.prefix, self.master_dir, self.deconv_type, self.gold_standard, self.num_threads, self.num_chunks)
yield cloudSPAdes()
yield Summarize_FastQ_Statistics()
class Global_Parameters:
"""Store parameters, prefixes, and target paths. Adapted from https://stackoverflow.com/questions/51152485/luigi-global-variables."""
def __init__(self):
self.read_dir = None
self.work_dir = None
self.base_prefix = None
self.deconv_type = None
self.fragments = None
self.edit_prefix = None
self.final_prefix = None
self.gstd_prefix = None
self.num_threads = None
self.orig_map_dir = None
self.enhd_cld_dir = None
self.cldspades_dir = None
self.analyses_dir = None
self.num_chunks = None
def set_params(self, read_dir, prefix, master_dir, deconv_type, gold_standard, num_threads, num_chunks):
self.read_dir = read_dir
self.base_prefix = prefix
self.deconv_type = deconv_type
self.fragments = True if deconv_type is 'frg' else False
self.edit_prefix = prefix + '_' + deconv_type
self.work_dir = check_make(master_dir, self.edit_prefix)
self.final_prefix = self.edit_prefix + '_bsort'
self.gstd_prefix = gold_standard
self.num_threads = num_threads
self.orig_map_dir = check_make(self.work_dir, 'original_mapping')
self.enhd_cld_dir = check_make(self.work_dir, 'enhanced_clouds')
check_make(self.enhd_cld_dir, 'R1')
check_make(self.enhd_cld_dir, 'R2')
self.cldspades_dir = check_make(self.work_dir, 'cloudSPAdes')
self.analyses_dir = check_make(master_dir, prefix + '_analyses')
self.num_chunks = num_chunks
gp = Global_Parameters()
if __name__ == '__main__':
luigi.run(main_cls_task = de_Novo_Assembly)