-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
488 lines (406 loc) · 18.3 KB
/
Snakefile
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import os
import glob
import pysam
from snakemake.utils import min_version
min_version("7.25.3")
SAMPLES, = glob_wildcards(config["input_path"]+"/{sample}.Aligned.sortedByCoord.out.patched.md.bam")
FIRST_SAMPLE = str(SAMPLES[0])
def get_mem_mb(wildcards, attempt):
"""
To adjust resources in rule run_irap
attemps = reiterations + 1
Max number attemps = 6
"""
mem_avail = [ 32, 56, 64, 128, 256, 300 ]
if attempt > len(mem_avail):
print(f"Attemps {attempt} exceeds the maximum number of attemps: {len(mem_avail)}")
print(f"modify value of --restart-times or adjust mem_avail resources accordingly")
sys.exit(1)
else:
return mem_avail[attempt-1] * 1000
def detect_read_type(wildcards):
if os.path.isfile(config["input_path"]+ f"/{wildcards['sample']}.Aligned.sortedByCoord.out.patched.md.bam.bai"):
print(f"BAM index exists")
else:
print(f"generating index...")
pysam.index(config["input_path"]+ f"/{wildcards['sample']}.Aligned.sortedByCoord.out.patched.md.bam")
with pysam.AlignmentFile(config["input_path"]+ f"/{wildcards['sample']}.Aligned.sortedByCoord.out.patched.md.bam", "rb") as bam:
for read in bam.fetch():
if read.is_paired:
return "pe"
return "se"
def detect_read_type_first_sample(bam_file_name):
if os.path.isfile(config["input_path"]+ f"/{bam_file_name}.Aligned.sortedByCoord.out.patched.md.bam.bai"):
print(f"BAM index exists")
else:
print(f"generating index...")
pysam.index(config["input_path"]+ f"/{bam_file_name}.Aligned.sortedByCoord.out.patched.md.bam")
with pysam.AlignmentFile(config["input_path"]+ f"/{bam_file_name}.Aligned.sortedByCoord.out.patched.md.bam", "rb") as bam:
for read in bam.fetch():
if read.is_paired:
return "pe"
return "se"
rule all:
input: "out/workflow.done"
#input: expand(["out/{sample}/{sample}.fastq.val", "out/{sample}/{sample}_prepare_aggregation.done"], sample=SAMPLES), "out/workflow.done", f"out/stage0_{FIRST_SAMPLE}.txt"
rule check_bam:
input:
config["input_path"]+ "/{sample}.Aligned.sortedByCoord.out.patched.md.bam"
output:
bam_check = temp("out/{sample}/{sample}_1.bam_checked")
log: "logs/{sample}/{sample}_check_bam.log"
priority: 1
conda:
"envs/samtools.yml"
threads: 4
params:
bai = config["input_path"]+ "/{sample}.Aligned.sortedByCoord.out.patched.md.bam.bai"
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
exec &> "{log}"
# ensure that the index file exists, otherwise create it
if [ -e {params.bai} ]; then
echo "BAM index file exists for {wildcards.sample}"
else
echo "BAM index file does not exist for {wildcards.sample}. Generating it..."
samtools index -b --threads {threads} {input}
fi
samtools quickcheck {input}
if [ $? -ne 0 ]; then
echo "ERROR: {input} is not a valid BAM file"
else
echo "BAM file is valid"
touch {output.bam_check}
fi
"""
rule bam_to_fastq:
"""
Produces interleaved Fastq from a sorted bam file
"""
input:
bam = config["input_path"]+"/{sample}.Aligned.sortedByCoord.out.patched.md.bam",
check_bam = rules.check_bam.output.bam_check
output:
fastq = temp("out/{sample}/{sample}.fq.gz"),
sorted_bam= temp("out/{sample}/sorted_{sample}.bam")
conda:
"envs/samtools.yml"
threads: 8
params:
read_type = detect_read_type,
uncompressed = "out/{sample}/{sample}.fq"
log: "logs/{sample}/{sample}_bam_to_fastq.log"
priority: 2
resources:
mem_mb=get_mem_mb,
disk_mb=70000
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
exec &> "{log}"
echo "read_type: {params.read_type}"
samtools sort -n --threads {threads} -o {output.sorted_bam} {input.bam}
# detect SE or PE
if [[ {params.read_type} == "pe" ]]; then
#If the input contains read-pairs which are to be interleaved or written to separate
#files in the same order, then the input should be first collated by name. Use samtools
#collate or samtools sort -n to ensure this.
samtools fastq --threads {threads} -0 /dev/null -s /dev/null {output.sorted_bam} > {params.uncompressed}
gzip {params.uncompressed}
elif [[ {params.read_type} == "se" ]]; then
samtools fastq --threads {threads} {output.sorted_bam} > {params.uncompressed}
gzip {params.uncompressed}
else
echo "ERROR: read type not detected"
exit 1
fi
"""
checkpoint validating_fastq:
"""
Here if else statement could be modified to run iRAP/ISL for PE and SE data.
"""
input:
fastq = rules.bam_to_fastq.output.fastq
output:
val_fastq = temp("out/{sample}/{sample}.fastq.val")
log: "logs/{sample}/{sample}_validating_fastq.log"
priority: 3
params:
fastq = "out/{sample}/{sample}.fastq.gz"
conda:
"envs/fastq_utils.yml"
resources:
mem_mb=get_mem_mb
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
fastq_info {input.fastq}
if [ $? -ne 0 ]; then
echo "ERROR: Failed fastq validation {input.fastq}"
mv {input.fastq} {params.fastq}
else
echo "validation successful"
fi
touch {output.val_fastq}
"""
rule fastqc:
"""
Not currently used
"""
input:
check = rules.validating_fastq.output.val_fastq
output:
fqc = "out/{sample}/{fq}_1_fastqc.html"
params:
fq = "out/{sample}/{fq}.fastq",
fqc_dir = "out/{sample}"
conda:
"envs/fastqc.yml"
threads: 4
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
fastqc -c {threads} {params.fq} --outdir={params.fqc_dir}
"""
rule run_irap_stage0:
"""
This ensures Irap stage0 is run only once, for the first sample
"""
input:
fastq = f"out/{FIRST_SAMPLE}/{FIRST_SAMPLE}.fq.gz",
check = f"out/{FIRST_SAMPLE}/{FIRST_SAMPLE}.fastq.val"
output:
completed = f"out/stage0_{FIRST_SAMPLE}.txt"
conda: "envs/isl.yaml"
log: f"logs/irap_stage0_{FIRST_SAMPLE}.log"
priority: 4
params:
private_script=config["private_script"],
conf=config["irap_config"],
root_dir=config["atlas_gtex_root"],
strand="both",
irapMem=16000000000,
irapDataOption="",
filename= f"{FIRST_SAMPLE}",
read_type = detect_read_type_first_sample(FIRST_SAMPLE)
resources:
mem_mb=16000
threads: 16
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
exec &> "{log}"
source {params.private_script}/gtex_bulk_env.sh
source {params.private_script}/gtex_bulk_init.sh
#source {params.root_dir}/isl/lib/functions.sh
source {params.root_dir}/isl/lib/generic_routines.sh
source {params.root_dir}/isl/lib/process_routines.sh
source {params.root_dir}/isl/lib/irap.sh
cp {params.private_script}/gtex_bulk_env.sh $IRAP_SINGLE_LIB
pushd {params.root_dir}/scripts > /dev/null
wget https://github.com/biopet/validatefastq/releases/download/v0.1.1/validatefastq-assembly-0.1.1.jar
chmod +x validatefastq-assembly-0.1.1.jar
popd
cat {input.check}
library={params.filename}
echo "library: $library"
workingDir=$ISL_WORKING_DIR
source {params.root_dir}/scripts/aux.sh
which get_local_relative_library_path
which get_library_path
localFastqPath=$(get_local_relative_library_path $library )
echo "workingDir: $workingDir"
echo "localFastqPath: $localFastqPath"
mkdir -p $(dirname $workingDir/$localFastqPath)
pushd $workingDir > /dev/null
# get quality enconding: 33 (sanger), 64 (illumina)
qual=$(testformat.sh {params.root_dir}/{input.fastq} | awk '{{print $1}}')
if [[ $qual == "sanger" ]]; then
qual_val="33"
maxcalledquality="93"
mincalledquality="0"
else
qual_val="64"
maxcalledquality="62"
mincalledquality="0"
fi
echo "quality enconding in fastq: $qual_val"
rm -f {params.root_dir}/{input.fastq}_seqtk
seqtk seq -q $mincalledquality -X $maxcalledquality -n N -C {params.root_dir}/{input.fastq} > {params.root_dir}/{input.fastq}_seqtk
if [[ {params.read_type} == "se" ]]; then
# fastq is SE
cp {params.root_dir}/{input.fastq} $workingDir/${{localFastqPath}}.fastq
java -jar {params.root_dir}/scripts/validatefastq-assembly-0.1.1.jar --fastq1 $workingDir/${{localFastqPath}}.fastq
echo "Calling irap_single_lib...SE mode"
cmd="irap_single_lib -0 -A -f -o irap_single_lib -1 $workingDir/${{localFastqPath}}.fastq -c {params.conf} -s {params.strand} -m {params.irapMem} -t {threads} -C {params.irapDataOption}"
echo "stage0 will run now:"
eval $cmd
echo "stage0 finished"
else
# fastq is PE
#split_fastq {input.fastq} $workingDir ${{localFastqPath}}
reformat.sh tossjunk=t tossbrokenreads=t changequality=t quantize=t mincalledquality=2 maxcalledquality=41 qin=$qual_val qout=$qual_val ow=t ibq=f vint=t in={params.root_dir}/{input.fastq}_seqtk out1=$workingDir/${{localFastqPath}}_1.fastq out2=$workingDir/${{localFastqPath}}_2.fastq
rm -f {params.root_dir}/{input.fastq}_seqtk
java -jar {params.root_dir}/scripts/validatefastq-assembly-0.1.1.jar --fastq1 $workingDir/${{localFastqPath}}_1.fastq --fastq2 $workingDir/${{localFastqPath}}_2.fastq
echo "Calling irap_single_lib...PE mode"
cmd="irap_single_lib -0 -A -f -o irap_single_lib -1 ${{localFastqPath}}_1.fastq -2 ${{localFastqPath}}_2.fastq -c {params.conf} -s {params.strand} -m {params.irapMem} -t {threads} -C {params.irapDataOption}"
echo "stage0 will run now:"
eval $cmd
echo "stage0 finished"
fi
popd
touch {output.completed}
"""
rule run_irap:
input:
fastq=rules.bam_to_fastq.output.fastq,
check = rules.validating_fastq.output.val_fastq,
stage0_completed=rules.run_irap_stage0.output.completed
output:
completed = "out/{sample}/{sample}_irap_completed.done"
conda: "envs/isl.yaml"
log: "logs/{sample}/{sample}_irap.log"
priority: 4
params:
private_script=config["private_script"],
conf=config["irap_config"],
root_dir=config["atlas_gtex_root"],
strand="both",
irapDataOption="",
filename="{sample}",
first_sample=f"{FIRST_SAMPLE}",
read_type = detect_read_type
resources:
mem_mb=get_mem_mb
threads: 24
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
exec &> "{log}"
irapMem=$(("{resources.mem_mb}000000"))
source {params.private_script}/gtex_bulk_env.sh
#source {params.private_script}/gtex_bulk_init.sh
#source {params.root_dir}/isl/lib/functions.sh
source {params.root_dir}/isl/lib/generic_routines.sh
source {params.root_dir}/isl/lib/process_routines.sh
source {params.root_dir}/isl/lib/irap.sh
##cp {params.private_script}/gtex_bulk_env.sh $IRAP_SINGLE_LIB
cat {input.check}
library={params.filename}
echo "library: $library"
workingDir=$ISL_WORKING_DIR
source {params.root_dir}/scripts/aux.sh
which get_local_relative_library_path
which get_library_path
localFastqPath=$(get_local_relative_library_path $library )
echo "workingDir: $workingDir"
echo "localFastqPath: $localFastqPath"
echo "sample: {wildcards.sample}"
echo "first sample: {params.first_sample}"
if [[ "{wildcards.sample}" != "{params.first_sample}" ]]; then
mkdir -p $(dirname $workingDir/$localFastqPath)
fi
pushd $workingDir > /dev/null
# get quality enconding: 33 (sanger), 64 (illumina)
qual=$(testformat.sh {params.root_dir}/{input.fastq} | awk '{{print $1}}')
if [[ $qual == "sanger" ]]; then
qual_val="33"
maxcalledquality="93"
mincalledquality="0"
else
qual_val="64"
maxcalledquality="62"
mincalledquality="0"
fi
echo "quality enconding in fastq: $qual_val"
rm -f {params.root_dir}/{input.fastq}_seqtk
seqtk seq -q $mincalledquality -X $maxcalledquality -n N -C {params.root_dir}/{input.fastq} > {params.root_dir}/{input.fastq}_seqtk
if [[ {params.read_type} == "se" ]]; then
# fastq is SE
cp {params.root_dir}/{input.fastq} $workingDir/${{localFastqPath}}.fastq
java -jar {params.root_dir}/scripts/validatefastq-assembly-0.1.1.jar --fastq1 $workingDir/${{localFastqPath}}.fastq
echo "Calling irap_single_lib...SE mode"
cmd="irap_single_lib -A -f -o irap_single_lib -1 $workingDir/${{localFastqPath}}.fastq -c {params.conf} -s {params.strand} -m $irapMem -t {threads} -C {params.irapDataOption}"
echo "SE IRAP will run now:"
eval $cmd
echo "irap_single_lib SE finished for {wildcards.sample}"
else
# fastq is PE
reformat.sh tossjunk=t tossbrokenreads=t changequality=t quantize=t mincalledquality=2 maxcalledquality=41 qin=$qual_val qout=$qual_val ow=t ibq=f vint=t in={params.root_dir}/{input.fastq}_seqtk out1=$workingDir/${{localFastqPath}}_1.fastq out2=$workingDir/${{localFastqPath}}_2.fastq
rm -f {params.root_dir}/{input.fastq}_seqtk
java -jar {params.root_dir}/scripts/validatefastq-assembly-0.1.1.jar --fastq1 $workingDir/${{localFastqPath}}_1.fastq --fastq2 $workingDir/${{localFastqPath}}_2.fastq
echo "Calling irap_single_lib..."
cmd="irap_single_lib -A -f -o irap_single_lib -1 ${{localFastqPath}}_1.fastq -2 ${{localFastqPath}}_2.fastq -c {params.conf} -s {params.strand} -m $irapMem -t {threads} -C {params.irapDataOption}"
echo "PE IRAP will run now:"
eval $cmd
echo "irap_single_lib PE finished for {wildcards.sample}"
fi
popd
# The files to collected by aggregation from irap are in $IRAP_SINGLE_LIB/out
touch {output.completed}
"""
rule prepare_aggregation:
"""
Prepare aggregation of irap outputs, which are at
$ISL_WORKING_DIR/irap_single_lib/{sample[0:5]}/{sample}
and copy essential aggregation files into:
$IRAP_SINGLE_LIB/out/{sample[0:5]}/{sample}
"""
input: "out/{sample}/{sample}_irap_completed.done"
output: "out/{sample}/{sample}_prepare_aggregation.done"
log: "logs/{sample}/{sample}_prepare_agreggation.log"
priority: 5
params:
private_script=config["private_script"]
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
exec &> "{log}"
source {params.private_script}/gtex_bulk_env.sh
prefix_sample=$(echo {wildcards.sample} | cut -c1-6)
# move irap outputs to $ISL_WORKING_DIR/irap_single_lib/out/sample[0:5]/sample
destination_dir=$ISL_RESULTS_DIR/$prefix_sample/{wildcards.sample}
mkdir -p $destination_dir
cp $ISL_WORKING_DIR/irap_single_lib/$prefix_sample/{wildcards.sample}/{wildcards.sample}*kallisto* $destination_dir/
cp $ISL_WORKING_DIR/irap_single_lib/$prefix_sample/{wildcards.sample}/{wildcards.sample}*htseq2* $destination_dir/
cp $ISL_WORKING_DIR/irap_single_lib/$prefix_sample/{wildcards.sample}/{wildcards.sample}.versions.tsv $destination_dir/irap.versions.tsv
cp -r $ISL_WORKING_DIR/irap_single_lib/$prefix_sample/{wildcards.sample}/logs $destination_dir/
cp -r $ISL_WORKING_DIR/irap_single_lib/$prefix_sample/{wildcards.sample}/qc $destination_dir/
# file checks
if ls "$destination_dir"/*kallisto* 1> /dev/null 2>&1; then
echo "Files matching the pattern '*kallisto*' exist in the folder $destination_dir"
else
echo "No files matching the pattern '*kallisto*' exist in the folder $destination_dir"
exit 1
fi
if ls "$destination_dir"/*htseq2* 1> /dev/null 2>&1; then
echo "Files matching the pattern '*htseq2*' exist in the folder $destination_dir"
else
echo "No files matching the pattern '*htseq2*' exist in the folder $destination_dir"
exit 1
fi
echo "tophat2 align summary:"
cat $ISL_WORKING_DIR/processing_data/h/homo_sapiens/irap_qc/tophat2/*/{wildcards.sample}/{wildcards.sample}/align_summary.txt
echo "removing other temp files in $ISL_RAW_DIR and $ISL_WORKING_DIR "
rm -f $ISL_RAW_DIR/$prefix_sample/{wildcards.sample}/{wildcards.sample}*
rm -rf $ISL_WORKING_DIR/irap_single_lib/$prefix_sample/{wildcards.sample}
rm -rf $ISL_WORKING_DIR/processing_data/h/homo_sapiens/irap_qc/tophat2/*/{wildcards.sample}
rm -rf $ISL_WORKING_DIR/processing_data/h/homo_sapiens/irap_qc/none/kallisto/*/{wildcards.sample}
rm -rf $ISL_WORKING_DIR/processing_data/h/homo_sapiens/irap_qc/tophat2/htseq2/*/{wildcards.sample}
echo "GTEX analysis completed for {wildcards.sample}. Ready for aggregation"
touch {output}
"""
#rule aggregate_libraries:
# """
# Final rule to agregate all library outputs, and store them in a single folder
# $IRAP_SINGLE_LIB/studies/E-GTEX-8/homo_sapiens/
# """
rule final_workflow_check:
input: expand(["out/{sample}/{sample}_prepare_aggregation.done"], sample=SAMPLES)
output: "out/workflow.done"
shell:
"""
set -e # snakemake on the cluster doesn't stop on error when --keep-going is set
touch {output}
"""