-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSnakefile
379 lines (345 loc) · 13.6 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
# @author: Daud Khan
# @date: September 15, 2019
# @desc: Snakemake pipeline for https://www.broadinstitute.org/gatk/guide/article?id=3891
shell.prefix("source ~/.bash_profile; set -euo pipefail;")
from util.varsub import varsub
configfile: "config.yaml"
varsub(config)
import glob, os
# A snakemake regular expression matching the forward mate FASTQ files.
SAMPLES, = glob_wildcards(config['datadirs']['fastq'] + "/" + "{file}_1.fq.gz")
# Patterns for the 1st mate and the 2nd mate using the 'sample' wildcard.
READS = ["1", "2"]
# List of "{sample}.g.vcf.gz"
# used for rule "combineGVCFs"
gvcfLst = expand(config['datadirs']['vcf'] + "/" + "{file}.g.vcf" , file=SAMPLES)
# Rules --------------------------------------------------------------------------------
rule all:
input:
config['reference']['stargenomedir']['hg38'] + "/" + "SAindex"
config['datadirs']['sj_files'] + "/" + "SJ.out.pass1_merged.tab",
expand(config['datadirs']['qc'] + "/" + "{file}_{read}_fastqc.html", file=SAMPLES, read= READS),
expand(config['datadirs']['trim'] + "/" + "{file}_{read}_val_{read}.fq.gz", file = SAMPLES, read= READS),
expand(config['datadirs']['bam'] + "/" + "{file}_SJ.out.tab", file = SAMPLES),
expand(config['datadirs']['pass2'] + "/" + "{file}_Aligned.sortedByCoord.out.bam", file = SAMPLES ),
expand(config['datadirs']['RGbam'] + "/" + "{file}_Aligned.sortedByCoord.out.RG.bam", file=SAMPLES),
expand(config['datadirs']['dedup'] + "/" + "{file}_Aligned.sortedByCoord.out.md.bam",file=SAMPLES),
expand(config['datadirs']['dedup'] + "/" + "{file}_Aligned.sortedByCoord.out.md.bam.bai", file=SAMPLES),
expand(config['datadirs']['splitNcigar'] + "/" + "{file}_split.out.bam", file=SAMPLES),
expand(config['datadirs']['Recal1'] + "/" + "{file}_recal.table", file=SAMPLES),
expand(config['datadirs']['BQSR_1'] + "/" + "{file}_recal.pass1.bam", file=SAMPLES),
expand(config['datadirs']['Recal2'] + "/" + "{file}_recal.table", file=SAMPLES),
expand(config['datadirs']['BQSR_2'] + "/" + "{file}_recal.pass2.bam", file=SAMPLES),
expand(config['datadirs']['vcf'] + "/" + "{file}.g.vcf" , file=SAMPLES),
config['datadirs']['CombinedGvcfs'] + "/" + "all.g.vcf"
# QC of raw fastq files.
rule fastqc:
input:
f1 = config['datadirs']['fastq'] + "/" + "{file}_{read}.fq.gz"
output: config['datadirs']['qc'] + "/" + "{file}_{read}_fastqc.html", config['datadirs']['qc'] + "/" + "{file}_{read}_fastqc.zip"
params:
prefix = config['datadirs']['qc'],
resources:
mem_mb= 10000
shell:
"""
fastqc --thread 8 --outdir {params.prefix} --nogroup {input.f1}
"""
#Trimmming of the illumina adapters.
rule trim_galore_pe:
input:
f1 = config['datadirs']['fastq'] + "/" + "{file}_1.fq.gz",
f2 = config['datadirs']['fastq'] + "/" + "{file}_1.fq.gz"
output:
fwd_pai = config['datadirs']['trim'] + "/" + "{file}_1_val_1.fq.gz",
rev_pai = config['datadirs']['trim'] + "/" + "{file}_2_val_2.fq.gz",
params:
extra = " -j 8 --illumina -q 20 --phred33 --length 20",
prefix = config['datadirs']['trim'],
resources:
mem_mb= 20000
shell:"""
trim_galore \
{params.extra} \
--paired {input.f1} {input.f2} \
-o {params.prefix} \
--fastqc
"""
# 1. Map paired-end RNA-seq reads to the genome.
# 2. Count the number of reads supporting each splice junction.
rule pass1:
input:
f1 = config['datadirs']['trim'] + "/" + "{file}_1_val_1.fq.gz",
f2 = config['datadirs']['trim'] + "/" + "{file}_2_val_2.fq.gz",
queue = rules.trim_galore_pe.output.rev_pai
output: config['datadirs']['bam'] + "/" + "{file}_SJ.out.tab", config['datadirs']['bam'] + "/" + "{file}_Aligned.toTranscriptome.out.bam"
params:
genomedir = config['reference']['star_ref'],
prefix = config['datadirs']['bam'] + "/" + "{file}_"
threads: 16
resources:
mem_mb= 40000
shell: """
STAR \
--runThreadN {threads} \
--genomeDir {params.genomedir} \
--readFilesIn {input.f1} {input.f2} \
--readFilesCommand zcat \
--outFileNamePrefix {params.prefix} \
--outSAMtype None \
--outSAMunmapped Within \
--quantMode TranscriptomeSAM \
--outSAMattributes NH HI AS NM MD \
--outFilterType BySJout \
--outFilterMultimapNmax 20 \
--outFilterMismatchNmax 999 \
--outFilterMismatchNoverReadLmax 0.04 \
--alignIntronMin 20 \
--alignIntronMax 1000000 \
--alignMatesGapMax 1000000 \
--alignSJoverhangMin 8 \
--alignSJDBoverhangMin 1 \
--sjdbScore 1 \
--limitBAMsortRAM 50000000000
"""
# Merge the Splice junction informtaion from Pass1 Mapping
rule SJ_Merge:
input:
sjs = expand(config['datadirs']['bam'] + "/" + "{file}_SJ.out.tab" , file = SAMPLES)
output:
sjs= config['datadirs']['sj_files'] + "/" + "SJ.out.pass1_merged.tab"
threads: 1
shell: """
cat {input.sjs} | awk '$7 >= 3' | cut -f1-4 | sort -u > {output.sjs}
"""
# Make an index of the genome for STAR using the merged splice junction information to get better alignments around novel splice junctions.
rule star_genome:
input:
fasta = config['reference']['fasta']['hg38'],
gtf = config['reference']['gtf']['hg38'],
sjs = config['datadirs']['sj_files'] + "/" + "SJ.out.pass1_merged.tab",
genomedir = config['reference']['stargenomedir']['hg38'],
queue = rules.merge.output.sjs
output:
starindex = config['reference']['stargenomedir']['hg38'] + "/" + "SAindex"
params:
overhang = 149
threads: 12
resources:
mem_mb = 40000
shell: """
STAR \
--runThreadN {threads} \
--runMode genomeGenerate \
--genomeDir {input.genomedir} \
--outFileNamePrefix {input.genomedir} \
--genomeFastaFiles {input.fasta} \
--sjdbGTFfile {input.gtf} \
--limitSjdbInsertNsj 2037800 \
--sjdbFileChrStartEnd {input.sjs} \
--sjdbOverhang {params.overhang}
"""
# 1. Map paired-end RNA-seq reads to the genome.
# 2. Make a coordinate sorted BAM with genomic coordinates.
# 3. Count the number of reads mapped to each gene.
# 4. Count the number of reads supporting each splice junction.
rule pass2:
input:
f1 = config['datadirs']['trim'] + "/" + "{file}_1_val_1.fq.gz",
f2 = config['datadirs']['trim'] + "/" + "{file}_2_val_2.fq.gz",
line = rules.star_genome.output.starindex
output: config['datadirs']['pass2'] + "/" + "{file}_Aligned.toTranscriptome.out.bam", config['datadirs']['pass2'] + "/" + "{file}_Aligned.sortedByCoord.out.bam"
params:
genomedir = config['reference']['stargenomedir']['hg38'],
prefix = config['datadirs']['pass2'] + "/" + "{file}_"
threads: 16
resources:
mem_mb= 50000
shell: """
STAR \
--runThreadN {threads} \
--genomeDir {params.genomedir} \
--readFilesIn {input.f1} {input.f2} \
--readFilesCommand zcat \
--outFileNamePrefix {params.prefix} \
--outSAMtype BAM SortedByCoordinate \
--outSAMunmapped Within \
--quantMode TranscriptomeSAM \
--outSAMattributes NH HI AS NM MD \
--outFilterType BySJout \
--outFilterMultimapNmax 20 \
--outFilterMismatchNmax 999 \
--outFilterMismatchNoverReadLmax 0.04 \
--alignIntronMin 20 \
--alignIntronMax 1000000 \
--alignMatesGapMax 1000000 \
--alignSJoverhangMin 8 \
--alignSJDBoverhangMin 1 \
--sjdbScore 1 \
--outBAMsortingThreadN 5 \
--limitBAMsortRAM 50000000000
"""
# add read groups,Platform
rule AddRG:
input:
bam = config['datadirs']['pass2'] + "/" + "{file}_Aligned.sortedByCoord.out.bam"
output:
RG = config['datadirs']['RGbam'] + "/" + "{file}_Aligned.sortedByCoord.out.RG.bam"
params: "RGLB=lib1 RGPL=illumina RGPU={file} RGSM={file}"
shell:"""
module load picard
java -jar $EBROOTPICARD/picard.jar AddOrReplaceReadGroups {params} I={input.bam} O={output.RG}
"""
# mark duplicates
rule mark_dups:
input:
bam = config['datadirs']['RGbam'] + "/" + "{file}_Aligned.sortedByCoord.out.RG.bam"
output:
dbam = config['datadirs']['dedup'] + "/" + "{file}_Aligned.sortedByCoord.out.md.bam",
metric = config['datadirs']['dedup'] + "/" + "{file}_Aligned.sortedByCoord.out.metrics.txt"
params:
picard = "java -jar $EBROOTPICARD/picard.jar"
resources:
mem_mb = 10000
shell: """
module load picard
{params.picard} MarkDuplicates INPUT={input.bam} OUTPUT={output.dbam} METRICS_FILE={output.metric} ASSUME_SORT_ORDER=coordinate OPTICAL_DUPLICATE_PIXEL_DISTANCE=100
"""
# Index bam file using samtools
rule index:
input:
bam = config['datadirs']['dedup'] + "/" + "{file}_Aligned.sortedByCoord.out.md.bam"
output:
bai = config['datadirs']['dedup'] + "/" + "{file}_Aligned.sortedByCoord.out.md.bam.bai"
shell:"""
module load samtools
samtools index {input.bam} {output.bai}
"""
#Splits N Cigar Reads from bam file
rule splitNcigar:
input:
bam = config['datadirs']['dedup'] + "/" + "{file}_Aligned.sortedByCoord.out.md.bam",
fasta = config['reference']['fasta']['hg38']
output:
SBam = config['datadirs']['splitNcigar'] + "/" + "{file}_split.out.bam"
resources:
mem_mb= 60000
shell:"""
module load gatk
gatk SplitNCigarReads \
-R {input.fasta} \
-I {input.bam} \
-O {output.SBam}
"""
# base recalibration
rule BQSR_Pass1:
input:
bam = config['datadirs']['splitNcigar'] + "/" + "{file}_split.out.bam",
GSNPs = config['reference']['1000G']['hg38'],
Indels = config['reference']['Indels']['hg38'],
DbSNP = config['reference']['DbSNP']['hg38'],
fasta = config['reference']['fasta']['hg38']
output:
Recall = config['datadirs']['Recal1'] + "/" + "{file}_recal.table"
resources:
mem_mb = 50000
shell:"""
gatk BaseRecalibrator \
-I {input.bam} \
-R {input.fasta} \
--known-sites {input.GSNPs} \
--known-sites {input.Indels} \
--known-sites {input.DbSNP} \
-O {output.Recall}
"""
rule ApplyBQSR:
input:
bam = config['datadirs']['splitNcigar'] + "/" + "{file}_split.out.bam",
fasta = config['reference']['fasta']['hg38'],
recal = config['datadirs']['Recal1'] + "/" + "{file}_recal.table"
output:
Rbam = config['datadirs']['BQSR_1'] + "/" + "{file}_recal.pass1.bam"
resources:
mem_mb = 50000
shell:"""
gatk ApplyBQSR \
-I {input.bam} \
-R {input.fasta} \
--bqsr-recal-file {input.recal} \
-O {output.Rbam}
"""
#Base Recalibration
rule BQSR_Pass2:
input:
bam = config['datadirs']['BQSR_1'] + "/" + "{file}_recal.pass1.bam",
GSNPs = config['reference']['1000G']['hg38'],
Indels = config['reference']['Indels']['hg38'],
DbSNP = config['reference']['DbSNP']['hg38'],
fasta = config['reference']['fasta']['hg38']
output:
Recall = config['datadirs']['Recal2'] + "/" + "{file}_recal.table"
resources:
mem_mb = 50000
shell:"""
gatk BaseRecalibrator \
-I {input.bam} \
-R {input.fasta} \
--known-sites {input.GSNPs} \
--known-sites {input.Indels} \
--known-sites {input.DbSNP} \
-O {output.Recall}
"""
#detects systematic errors made by the sequencer when it estimates the quality score of each base call
rule ApplyBQSR:
input:
bam = config['datadirs']['BQSR_1'] + "/" + "{file}_recal.pass1.bam",
fasta = config['reference']['fasta']['hg38'],
recal = config['datadirs']['Recal2'] + "/" + "{file}_recal.table"
output:
Rbam = config['datadirs']['BQSR_2'] + "/" + "{file}_recal.pass2.bam"
resources:
mem_mb = 50000
shell:"""
gatk ApplyBQSR \
-I {input.bam} \
-R {input.fasta} \
--bqsr-recal-file {input.recal} \
-O {output.Rbam}
"""
#Variant Calling
rule gatk_HaplotypeCaller:
input:
bam = config['datadirs']['BQSR_2'] + "/" + "{file}_recal.pass2.bam"
fasta = config['reference']['fasta']['hg38']
output:
vcf = config['datadirs']['vcf'] + "/" + "{file}.g.vcf"
resources:
mem_mb = 50000
shell:"""
gatk HaplotypeCaller \
-R {input.fasta} \
-I {input.bam} \
-ERC GVCF --output-mode EMIT_ALL_CONFIDENT_SITES \
--dont-use-soft-clipped-bases \
-stand-call-conf 20.0 \
-O {output.vcf}
"""
#Combine all the gVCFS for joint calling
rule CombineGvfs:
input:
vcfs = gvcfLst,
fasta = config['reference']['fasta']['hg38']
output:
combined = config['datadirs']['CombinedGvcfs'] + "/" + "all.g.vcf"
params:
lst = " --variant " .join(gvcfLst)
resources:
mem_mb = 100000
shell:"""
module load gatk
export JAVA_TOOL_OPTIONS=-Xmx140g
gatk CombineGVCFs \
-R {input.fasta} \
-O {output.combined} \
--variant {params.lst}
"""