-
Notifications
You must be signed in to change notification settings - Fork 0
/
shapemapper-txome
executable file
·404 lines (355 loc) · 12.2 KB
/
shapemapper-txome
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
#!/usr/bin/env python
import datetime
start_time = datetime.datetime.now()
import sys, os
from scripts.logger import Logger
from scripts.job_wrapper import stage
from scripts.shapemapper_wrapper import fmt_shapemapper_cmds
from scripts.parse_args import parse_args
from scripts.util import timestamp, makedirs
from scripts.globals import god
THIS_DIR = os.path.split(os.path.realpath(__file__))[0]
if '--test' in sys.argv[1:]:
args = '''
--paired
--out test
--target test_data/16S.fa
test_data/23S.fa
test_data/TPP.fa
'''.strip().split()
args += ['--shapemapper-args', '--random-primer-len 9']
if '--sam' in sys.argv[1:]:
args += ['--modified', 'test_data/sam/modified.sam',
'--untreated', 'test_data/sam/untreated.sam']
elif '--bam' in sys.argv[1:]:
args += ['--modified', 'test_data/bam/modified.bam',
'--untreated', 'test_data/bam/untreated.bam']
else:
args += ['--modified', 'test_data/modified_10k_fastq',
'--untreated', 'test_data/untreated_10k_fastq']
else:
args = sys.argv[1:]
p = parse_args(args)
outlog_path = p.out + "/" + "kallisto-txome_log.txt"
outlog = Logger(outlog_path,
sys.stdout)
# override stdout and stderr globally to redirect through logger
sys.stdout = outlog
sys.stderr = outlog
s = "Started kallisto-txome at {}".format(timestamp())
print('#' * len(s))
print(s)
print("arguments = {}".format(p))
print("Will write all output to {}.".format(p.out))
makedirs(p.out)
# parameters used in job_wrapper.py functions
god.platform = p.platform
god.max_jobs = p.max_jobs
god.bsub_opts = "-n{} -R span[hosts=1]".format(p.nproc)
# -----------------------------------------------------------------------------
# check fasta target names for characters that don't play nicely with bowtie2
# and/or bbmerge
dir = p.out + "/fasta_check"
cmd = (
'{THIS_DIR}/scripts/check_fasta_names.py '
'--target {target} '
).format(THIS_DIR=THIS_DIR,
dir=dir,
target=' '.join(p.target))
stage(name="checking fasta target names",
dir=dir,
done=dir + "/fasta_check_done",
cmd=cmd)
kallisto_dir = p.out + "/kallisto_pseudomap"
bam_to_sam_dir = p.out + "/convert_sam"
samples = ['modified','untreated']
sam_paths = {}
bam_paths = {}
id_file = None
if p.filetype == 'sam':
for sample in samples:
sam_paths[sample] = p.input_files[sample][0]
elif p.filetype == 'bam':
for sample in samples:
bam_paths[sample] = p.input_files[sample][0]
sam_paths[sample] = "{bam_to_sam_dir}/{sample}/converted.sam".format(
bam_to_sam_dir=bam_to_sam_dir, sample=sample)
elif p.filetype == 'fastq':
for sample in samples:
bam_paths[sample] = "{kallisto_dir}/{sample}/pseudoalignments.bam".format(
kallisto_dir=kallisto_dir, sample=sample)
sam_paths[sample] = "{bam_to_sam_dir}/{sample}/pseudoalignments.sam".format(
bam_to_sam_dir=bam_to_sam_dir, sample=sample)
if p.filetype == 'fastq':
# -----------------------------------------------------------------------------
# generate kallisto index
dir = p.out + "/kallisto_index"
cmd = 'kallisto index -i {dir}/index {target}'.format(dir=dir,
target=' '.join(p.target))
stage(name="kallisto index building",
dir=dir,
done=dir + "/kallisto_index_building_done",
cmd=cmd)
# -----------------------------------------------------------------------------
# run pseudomapping
prev_dir = dir
dir = kallisto_dir
extra_flags = '--single --fragment-length {fragment_length} --sd {fragment_sd}'
extra_flags = extra_flags.format(fragment_length=p.fragment_length,
fragment_sd=p.fragment_sd)
if p.paired:
extra_flags = ''
cmds = []
for sample in p.input_files.keys():
if len(p.input_files[sample]) == 0:
continue
cmd = ('kallisto quant -i "{prev_dir}/index" --pseudobam --plaintext '
'--threads {threads} '
'-o "{dir}/{sample}" {inputs} {extra_flags}')
cmd = cmd.format(dir=dir,
prev_dir=prev_dir,
target=' '.join(p.target),
sample=sample,
inputs=' '.join(['"{}"'.format(f) for f in p.input_files[sample]]),
extra_flags=extra_flags,
threads=p.nproc)
cmds.append(cmd)
stage(name="kallisto pseudomapping to targets",
dir=dir,
done=dir + "/kallisto_pseudomapping_done",
cmds=cmds)
if p.filetype in ['bam','fastq']:
# -----------------------------------------------------------------------------
# convert BAM to SAM for simpler parsing
dir = bam_to_sam_dir
cmds = []
for sample in p.input_files.keys():
if len(p.input_files[sample]) == 0:
continue
cmd = (
'mkdir -p "{{dir}}/{{sample}}" '
'&& '
'samtools view -h -o "{output}" "{input}"'
)
cmd = cmd.format(
output=sam_paths[sample],
input=bam_paths[sample],
)
cmd = cmd.format(dir=dir,
sample=sample)
cmds.append(cmd)
stage(name="SAM conversion",
dir=dir,
done=dir + "/sam_conversion_done",
cmds=cmds)
sam_dir = dir
# -------------------------------------------------------------------------------------
# handle reads mapping to multiple targets and exclude headers and unmapped reads
dir = p.out + "/multimapper_processed_sam"
cmds = []
for sample in p.input_files.keys():
if len(p.input_files[sample]) == 0:
continue
cmd = (
'mkdir -p "{{dir}}/{{sample}}" '
'&& '
'{{THIS_DIR}}/scripts/filter_multimappers.py '
'--in "{input}" '
'--out "{output}" '
'--multimapper-mode {multimapper_mode}'
)
cmd = cmd.format(
output="{dir}/{sample}/processed.sam",
input=sam_paths[sample],
multimapper_mode=p.multimapper_mode,
)
cmd = cmd.format(THIS_DIR=THIS_DIR,
dir=dir,
sample=sample)
cmds.append(cmd)
stage(name="multimapper handling",
dir=dir,
done=dir + "/multimapper_handling_done",
cmds=cmds)
processed_sam_dir = dir
# -----------------------------------------------------------------------------
# sort SAM files by pseudomapped target
dir = p.out + "/target_sorted_sam"
cmds = []
for sample in samples:
if len(p.input_files[sample]) == 0:
continue
cmd = (
'mkdir -p "{{dir}}/{{sample}}" '
'&& '
'sort --stable '
'-k 3,3 '
'-S 20% '
'-T "{{dir}}/{{sample}}" '
'-o "{output}" '
'"{input}"'
)
cmd = cmd.format(
output="{dir}/{sample}/sorted.sam",
input="{processed_sam_dir}/{sample}/processed.sam",
)
cmd = cmd.format(processed_sam_dir=processed_sam_dir,
dir=dir,
sample=sample)
cmds.append(cmd)
stage(name="SAM sorting",
dir=dir,
done=dir + "/sam_sorting_done",
cmds=cmds)
sorted_sam_dir = dir
# --------------------------------------------------------------------------------
# identify transcripts above some potential coverage or total read count threshold
dir = p.out + "/select_targets"
id_file = "{dir}/selected_ids.txt".format(dir=dir)
if p.filetype == 'fastq':
cmd = (
'{THIS_DIR}/scripts/select_targets.py '
'--in {input_args} '
'--out "{id_file}" '
'--min-reads {min_reads} '
'--min-mean-coverage {min_mean_coverage} '
'--frag-len {frag_len} '
)
input_args = ''
for sample in p.input_files.keys():
if len(p.input_files[sample]) == 0:
continue
input_args += (
'"{kallisto_dir}/{sample}/abundance.tsv" '.format(
kallisto_dir=kallisto_dir, sample=sample
)
)
cmd = cmd.format(
THIS_DIR=THIS_DIR,
dir=dir,
input_args=input_args,
min_reads=p.min_reads,
min_mean_coverage=p.min_mean_coverage,
frag_len=p.fragment_length,
id_file=id_file,
)
elif p.filetype in ['sam','bam']:
sam = ''
for sample in samples:
if len(p.input_files[sample]) == 0:
continue
sam += '"{sorted_sam_dir}/{sample}/sorted.sam" '.format(
sorted_sam_dir=sorted_sam_dir,
sample=sample,
)
cmd = (
'{THIS_DIR}/scripts/select_targets_from_sam.py '
'--in {sam} '
'--out "{id_file}" '
'--min-reads {min_reads} '
)
cmd = cmd.format(
THIS_DIR=THIS_DIR,
dir=dir,
sam=sam,
min_reads=p.min_reads,
id_file=id_file,
)
stage(name="target coverage selection",
dir=dir,
done=dir + "/target_coverage_selection_done",
cmd=cmd)
# -----------------------------------------------------------------------------
# generate fasta with selected transcripts only,
# folder hierarchy of individual fasta files and a file indexing
# those paths by target name
dir = p.out + "/filter_targets"
cmd = (
'{THIS_DIR}/scripts/filter_targets.py '
'--target {target} '
'--out-dir "{dir}/single_seqs" '
'--out-fasta-paths "{fasta_locations_file}" '
'--max-files-per-folder {max_files_per_folder} '
)
if id_file is not None:
cmd += '--id-file "{id_file}" '
#cmd += '--out "{dir}/selected_targets.fa" '
fasta_locations_file = '{dir}/fasta_locations.txt'.format(dir=dir)
cmd = cmd.format(
THIS_DIR=THIS_DIR,
dir=dir,
target=' '.join(['"{}"'.format(f) for f in p.target]),
id_file=id_file,
fasta_locations_file=fasta_locations_file,
max_files_per_folder=p.max_files_per_folder,
)
stage(name="target filtering",
dir=dir,
done=dir + "/target_filtering_done",
cmd=cmd)
# -----------------------------------------------------------------------------
# split sam file(s) into FASTQ or pairs of FASTQ files for each selected target transcript
dir = p.out + "/fastq_by_target"
cmds = []
paired_arg = '--unpaired'
if p.paired:
paired_arg = '--paired'
for sample in samples:
if len(p.input_files[sample]) == 0:
continue
cmd = (
'mkdir -p "{{dir}}/{{sample}}" '
'&& '
'{{THIS_DIR}}/scripts/split_sam_to_fastqs.py '
'--in "{input}" '
'--selected-target-ids "{id_file}" '
'--out "{output}" '
'{paired_arg} '
'--max-files-per-folder {max_files_per_folder}'
)
cmd = cmd.format(
output="{dir}/{sample}",
id_file=id_file,
input="{sorted_sam_dir}/{sample}/sorted.sam",
paired_arg=paired_arg,
max_files_per_folder=p.max_files_per_folder,
)
cmd = cmd.format(sorted_sam_dir=sorted_sam_dir,
THIS_DIR=THIS_DIR,
dir=dir,
sample=sample)
cmds.append(cmd)
stage(name="SAM to FASTQ conversion and splitting",
dir=dir,
done=dir + "/fastq_conversion_done",
cmds=cmds)
fastq_dir = dir
# -----------------------------------------------------------------------------
# run shapemapper on each batch of reads and associated target sequence
dir = p.out + "/shapemapper"
done = dir + "/shapemapper_done"
try:
shapemapper_args = p.shapemapper_args
except AttributeError:
shapemapper_args = ''
cmds, dirs = fmt_shapemapper_cmds(
dir=dir,
fasta_locations_file=fasta_locations_file,
input_files=p.input_files,
fastq_dir=fastq_dir,
paired=p.paired,
nproc=p.nproc,
shapemapper_args=shapemapper_args,
max_files_per_folder=p.max_files_per_folder,
)
stage(name="ShapeMapper",
dirs=dirs,
done=done,
cmds=cmds)
# -----------------------------------------------------------------------------
end_time = datetime.datetime.now()
delta = end_time - start_time
hours, remain = divmod(delta.seconds, 3600)
minutes, seconds = divmod(remain, 60)
print("\nkallisto-txome completed. Total turnaround time {} hours, {} minutes, {} seconds".format(
hours, minutes, seconds))