forked from khandaud15/RNA-Seq-Variant-Calling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
418 lines (378 loc) · 15.4 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
# @author: Wenjin Gu
# @date: Nov 5, 2023
# @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.
RUNS,SAMPLES, READS= glob_wildcards(config['datadirs']['fastq'] + "/" + "{run}/{file}.{read}.fq.gz")
# List of "{sample}.g.vcf.gz"
# used for rule "combineGVCFs"
gvcfLst = expand(config['datadirs']['vcf'] + "/" + "{file}.g.vcf" , file=SAMPLES)
# Rules --------------------------------------------------------------------------------
rule all:
input:
# 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']['bam'] + "/" + "{file}_Aligned.toTranscriptome.out.bam", file = SAMPLES),
# config['datadirs']['sj_files'] + "/" + "SJ.out.pass1_merged.tab",
# config['reference']['stargenomedir']['hg38'],
# 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),
expand(config['datadirs']['vcf'] + "/" + "{file}.snp.vcf",file=SAMPLES)
# QC of raw fastq files.
rule fastqc:
input:
f1 = config['datadirs']['fastq'] + "/" + "{file}/{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= 400000
shell:
"""
module load Bioinformatics
module load fastqc
fastqc --thread 8 --outdir {params.prefix} --nogroup {input.f1}
"""
#Trimmming of the illumina adapters.
rule trimmomatic:
input:
f1 = config['datadirs']['fastq'] + "/" + "{file}/{file}.R1.fq.gz",
f2 = config['datadirs']['fastq'] + "/" + "{file}/{file}.R2.fq.gz"
output:
fwd_pai = config['datadirs']['trim'] + "/" + "{file}_R1_val_R1.fq.gz",
rev_pai = config['datadirs']['trim'] + "/" + "{file}_R2_val_R2.fq.gz",
fwd_unpai = config['datadirs']['trim'] + "/" + "{file}_1_unpai.fq.gz",
rev_unpai = config['datadirs']['trim'] + "/" + "{file}_2_unpai.fq.gz",
shell:"""
module load Bioinformatics
module load trimmomatic
TrimmomaticPE -threads {threads} \
-phred33 {input.f1} {input.f2} \
{output.fwd_pai} {output.fwd_unpai} {output.rev_pai} {output.rev_unpai} \
ILLUMINACLIP:TruSeq3-PE.fa:2:30:10 LEADING:3 TRAILING:3 SLIDINGWINDOW:4:15 MINLEN:36
"""
# 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}_R1_val_R1.fq.gz",
# f2 = config['datadirs']['trim'] + "/" + "{file}_R2_val_R2.fq.gz",
# 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= 400000
# shell: """
# module load Bioinformatics
# module load star
# 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",
# output:
# genomedir = directory(config['reference']['stargenomedir']['hg38']),
# params:
# overhang = 149
# threads:24
# resources:
# mem_mb = 400000
# shell: """
# module load Bioinformatics
# module load star
# STAR \
# --runThreadN {threads} \
# --runMode genomeGenerate \
# --genomeDir {output.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}_R1_val_R1.fq.gz",
# f2 = config['datadirs']['trim'] + "/" + "{file}_R2_val_R2.fq.gz",
# genomedir = config['reference']['stargenomedir']['hg38'],
# output: config['datadirs']['pass2'] + "/" + "{file}_Aligned.toTranscriptome.out.bam", config['datadirs']['pass2'] + "/" + "{file}_Aligned.sortedByCoord.out.bam"
# params:
# prefix = config['datadirs']['pass2'] + "/" + "{file}_"
# threads: 24
# shell: """
# module load Bioinformatics
# module load star
# STAR \
# --runThreadN {threads} \
# --genomeDir {input.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 3 \
# --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:
picard = "java -jar $PICARDLIB/picard.jar",
group = "RGLB=lib1 RGPL=illumina RGPU={file} RGSM={file}"
shell:"""
module load Bioinformatics
module load picard-tools
{params.picard} AddOrReplaceReadGroups {params.group} 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 $PICARDLIB/picard.jar"
resources:
mem_mb = 400000
shell: """
module load Bioinformatics
module load picard-tools
{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 Bioinformatics
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= 400000
shell:"""
module load Bioinformatics
module load gatk/4.3.0.0
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'],
fasta = config['reference']['fasta']['hg38']
output:
Recall = config['datadirs']['Recal1'] + "/" + "{file}_recal.table"
resources:
mem_mb = 400000
shell:"""
module load Bioinformatics
module load gatk/4.3.0.0
gatk BaseRecalibrator \
-I {input.bam} \
-R {input.fasta} \
--known-sites {input.GSNPs} \
--known-sites {input.Indels} \
-O {output.Recall}
"""
rule ApplyBQSR_pass1:
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 = 400000
shell:"""
module load Bioinformatics
module load gatk/4.3.0.0
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'],
fasta = config['reference']['fasta']['hg38']
output:
Recall = config['datadirs']['Recal2'] + "/" + "{file}_recal.table"
resources:
mem_mb = 400000
shell:"""
module load Bioinformatics
module load gatk/4.3.0.0
gatk BaseRecalibrator \
-I {input.bam} \
-R {input.fasta} \
--known-sites {input.GSNPs} \
--known-sites {input.Indels} \
-O {output.Recall}
"""
#detects systematic errors made by the sequencer when it estimates the quality score of each base call
rule ApplyBQSR_pass2:
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 = 400000
shell:"""
module load Bioinformatics
module load gatk/4.3.0.0
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 = 400000
shell:"""
module load Bioinformatics
module load gatk/4.3.0.0
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}
"""
rule select_SNPs:
input:
vcf = config['datadirs']['vcf'] + "/" + "{file}.g.vcf"
output:
snp_vcf = config['datadirs']['vcf'] + "/" + "{file}.snp.vcf"
shell:
"""
module load Bioinformatics
module load gatk/4.3.0.0
gatk SelectVariants -V {input.vcf} -select-type SNP -O {output.snp_vcf}
"""
#Variant Calling
# rule gatk_somatic:
# 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 = 400000
# shell:"""
# module load Bioinformatics
# module load gatk/4.3.0.0
# gatk Mutect2 \
# -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}
# """
# rule select_SNPs:
# input:
# vcf = config['datadirs']['vcf'] + "/" + "{file}.g.vcf"
# output:
# snp_vcf = config['datadirs']['vcf'] + "/" + "{file}.snp.vcf"
# shell:
# """
# module load Bioinformatics
# module load gatk/4.3.0.0
# gatk SelectVariants -V {input.vcf} -select-type SNP -O {output.snp_vcf}
# """