generated from Redmar-van-den-Berg/snakemake-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
265 lines (234 loc) · 6.89 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
include: "common.smk"
pepfile: config["pepfile"]
# Apply the settings from the pepfile, overwriting the default ones
default.update(pep.config.get("freebayes-snakemake", dict()))
# Apply the options specified to snakemake, overwriting the default settings
# and the settings from the PEP file
default.update(config)
# Set the updated dict as the configuration for the pipeline
config = default
rule all:
input:
vcf=expand(
"{sample}/{sample}.phased.vcf.gz", sample=pep.sample_table["sample_name"]
),
bam=expand(
"{sample}/{sample}.phased.bam", sample=pep.sample_table["sample_name"]
),
stats=expand(
"{sample}/{sample}.phased.tsv", sample=pep.sample_table["sample_name"]
),
multiqc="multiqc_report.html",
rule cutadapt:
input:
fin=get_forward,
rin=get_reverse,
output:
fout=temp("{sample}/{readgroup, rg\d+}_R1.fastq.gz"),
rout=temp("{sample}/{readgroup, rg\d+}_R2.fastq.gz"),
log:
"log/{sample}_{readgroup}_cutadapt.txt",
container:
containers["cutadapt"]
threads: 4
shell:
"""
cutadapt \
-a AGATCGGAAGAG \
-A AGATCGGAAGAG \
--compression-level=1 \
--cores {threads} \
--output {output.fout} \
--paired-output {output.rout} \
{input.fin} {input.rin} \
> {log}
"""
rule align:
input:
fin=rules.cutadapt.output.fout,
rin=rules.cutadapt.output.rout,
reference=config["reference"],
output:
bam=temp("{sample}/{readgroup}.sorted.bam"),
bai=temp("{sample}/{readgroup}.sorted.bam.bai"),
params:
compression_level=1,
rg="@RG\\tID:{sample}-{readgroup}\\tSM:{sample}",
log:
bwa="log/{sample}_{readgroup}.align.bwa.log",
sam="log/{sample}_{readgroup}.align.samtools.log",
container:
containers["bwa-mem2"]
threads: 11
shell:
"""
# WARNING: This works fine for 1 thread (testing) and 11 threads, but
# will under- or overprovision the tasks when other values are chosen
# Use at most 8 threads for bwa-mem2
bwa_cores=$(({threads} >= 8 ? 8 : 1))
# Use at most 3 threads for samtools sort, but only when {threads} is
# at least 8
sam_cores=$(({threads} >= 8 ? 3 : 1))
bwa-mem2 mem \
{input.reference} \
-R '{params.rg}' \
-t $bwa_cores \
{input.fin} {input.rin} 2> {log.bwa} |
samtools sort \
-l {params.compression_level} \
-@ $sam_cores \
- -o {output.bam} 2> {log.sam};
samtools index {output}
"""
rule markdup:
input:
bam=get_bamfiles,
bai=get_baifiles,
output:
bam=temp("{sample}/{sample}.bam"),
bai=temp("{sample}/{sample}.bam.bai"),
params:
compression_level=1,
log:
"log/{sample}_markdup.txt",
container:
containers["sambamba"]
threads: 4
shell:
"""
sambamba markdup \
--nthreads={threads} \
--compression-level={params.compression_level} \
{input.bam} {output.bam} 2> {log}
"""
rule collect_hs_metrics:
input:
bam=rules.markdup.output.bam,
bai=rules.markdup.output.bai,
bed=config.get("capture_bed", ""),
reference=config["reference"],
dictionary=get_dict_file,
output:
metrics="{sample}/hs_metrics.txt",
log:
"log/{sample}_hs_metrics.txt",
container:
containers["picard"]
shell:
"""
picard BedToIntervalList \
INPUT={input.bed} \
SEQUENCE_DICTIONARY={input.dictionary} \
OUTPUT=capture.list
picard CollectHsMetrics \
I={input.bam} \
R={input.reference} \
BAIT_INTERVALS=capture.list \
TARGET_INTERVALS=capture.list \
O={output.metrics}
"""
rule call_variants:
input:
bam=rules.markdup.output.bam,
bai=rules.markdup.output.bai,
reference=config["reference"],
output:
vcf=temp("{sample}/{sample}.vcf.gz"),
tbi=temp("{sample}/{sample}.vcf.gz.tbi"),
log:
"log/{sample}_call_variants.txt",
container:
containers["freebayes"]
shell:
"""
freebayes \
--fasta-reference {input.reference} \
--bam {input.bam} 2> {log} | bgzip > {output.vcf}
tabix -p vcf {output.vcf}
"""
rule phase_variants:
input:
bam=rules.markdup.output.bam,
reference=config["reference"],
vcf=rules.call_variants.output.vcf,
tbi=rules.call_variants.output.tbi,
output:
vcf="{sample}/{sample}.phased.vcf.gz",
tbi="{sample}/{sample}.phased.vcf.gz.tbi",
log:
"log/{sample}_phase_variants.txt",
container:
containers["whatshap"]
shell:
"""
whatshap phase \
--indels \
--reference {input.reference} \
{input.vcf} \
{input.bam} 2> {log} | bgzip > {output.vcf}
tabix -p vcf {output.vcf}
"""
rule phase_reads:
input:
bam=rules.markdup.output.bam,
bai=rules.markdup.output.bai,
reference=config["reference"],
vcf=rules.phase_variants.output.vcf,
output:
bam="{sample}/{sample}.phased.bam",
log:
"log/{sample}_phase_reads.txt",
container:
containers["whatshap"]
shell:
"""
whatshap haplotag \
--reference {input.reference} \
--output {output.bam} \
{input.vcf} \
{input.bam} 2> {log}
"""
rule phase_stats:
input:
vcf=rules.phase_variants.output.vcf,
output:
block="{sample}/{sample}.phased.blocklist",
gtf="{sample}/{sample}.phased.gtf",
tsv="{sample}/{sample}.phased.tsv",
log:
"log/{sample}_phase_stats.txt",
container:
containers["whatshap"]
shell:
"""
whatshap stats \
--block-list {output.block} \
--gtf {output.gtf} \
--tsv {output.tsv} \
{input.vcf} 2> {log}
"""
rule multiqc:
input:
markdup=expand(
"log/{sample}_markdup.txt",
sample=pep.sample_table["sample_name"],
),
hs_metrics=get_hs_metrics,
config=srcdir("config/multiqc_config.yml"),
output:
"multiqc_report.html",
log:
"log/multiqc.log",
container:
containers["multiqc"]
shell:
"""
rm -f multiqc_file_list.txt
for file in {input.markdup} {input.hs_metrics}; do
echo $file >> multiqc_file_list.txt
done
multiqc \
--force \
--file-list multiqc_file_list.txt \
--config {input.config}
"""