-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
387 lines (343 loc) · 11.5 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
configfile: "config.yaml"
rule all:
input:
expand("data/original_bam/filtering/{sample}_sorted.bam",
sample=config["samples"]),
expand("results/fastqc_result/{sample}_1_fastqc.html",
sample=config["samples"]),
expand("results/fastqc_result/{sample}_2_fastqc.html",
sample=config["samples"]),
expand("data/processed/{sample}_1_fastp.fastq.gz",
sample=config["samples"]),
expand("data/processed/{sample}_2_fastp.fastq.gz",
sample=config["samples"]),
expand("results/fastqc_result/trimmed/{sample}_1_fastp_fastqc.html",
sample=config["samples"]),
expand("results/fastqc_result/trimmed/{sample}_2_fastp_fastqc.html",
sample=config["samples"]),
expand("results/mapped_reads/{sample}.sam",
sample=config["samples"]),
expand("results/mapped_reads/{sample}_sorted.sam",
sample=config["samples"]),
expand("results/mapped_reads/bam_files/{sample}.bam",
sample=config["samples"]),
expand("results/mapped_reads/bam_files/{sample}_sorted.bam",
sample=config["samples"]),
expand("results/mapped_reads/bam_files/{sample}_dedup.bam",
sample=config["samples"]),
expand("results/variants/{sample}.vcf",
sample=config["samples"]),
expand("results/variants/vep/{sample}.txt",
sample=config["samples"])
##-----------------------------------##
## SETUP FOR THE WORKFLOW ##
##-----------------------------------##
## These first 3 rules are necessary to set everything ready
## 1 Downloading the data == "no_pain_no_gain"
rule download_data:
input:
script_download = "code/01dl_rawdata.bash",
script_rename="code/02rename.py"
output:
touch("tasks/01download_data.done")
conda:
"code/environments/Greference_tools.yml"
shell:
"""
bash {input.script_download}
python {input.script_rename}
"""
## 2 Preprocessing the data
rule pre_processing:
input:
script = "code/03extracting_fastq.sh"
output:
## choose one sample, if you execute either sample, it will generate all results
"data/original_bam/filtering/{sample}_sorted.bam"
params:
chr_choosed = config["chromosome"]
conda:
"code/environments/Greference_tools.yml"
shell:
"""
bash {input.script} {params.chr_choosed}
"""
## 3 Downloading the reference genome:
# if this is your 2º run, you might need to use the option --force
rule reference_genome:
output:
"data/reference/genome.fa"
params:
url = config["url_reference_genome"]
conda:
"code/environments/Greference_tools.yml"
shell:
"""
rm {output}* || \
echo "==> If you hadn't a previous reference in the directory and there is an ERROR, is NORMAL <==\n"
wget -O {output}.gz {params.url}
gzip -d {output}.gz
"""
##-----------------------------------------##
## REAL STARTING POINT WORKFLOW ##
##-----------------------------------------##
## 4 View the quality of the samples
rule fastqc:
input:
read1 = "data/raw/{sample}_1.fastq.gz",
read2 = "data/raw/{sample}_2.fastq.gz"
output:
read1 = "results/fastqc_result/{sample}_1_fastqc.html",
read2 = "results/fastqc_result/{sample}_2_fastqc.html"
conda:
"code/environments/Greference_tools.yml"
shell:
"""
for read in {input.read1} {input.read2}
do
fastqc $read -o results/fastqc_result/
done
"""
## 5 Pre-processed of the data
rule fastp:
input:
read1 = "data/raw/{sample}_1.fastq.gz",
read2 = "data/raw/{sample}_2.fastq.gz"
output:
read1 = "data/processed/{sample}_1_fastp.fastq.gz",
read2 = "data/processed/{sample}_2_fastp.fastq.gz"
params:
cut_tail=config["fastp_cuttail"],
cut_front=config["fastp_cutfront"],
cut_meanq=config["fastp_cutmeanq"],
length=config["fastp_length"]
conda:
"code/environments/Greference_tools.yml"
shell:
"""
fastp -i {input.read1} -I {input.read2} \
-o {output.read1} -O {output.read2} \
--cut_tail '{params.cut_tail}' \
--cut_front '{params.cut_front}' \
--cut_mean_quality '{params.cut_meanq}' \
-l {params.length}
mv *.json data/processed/
mv fastp.html data/processed
"""
## 6 View the quality of the trimmed samples
rule fastqc_trimmed:
input:
read1 = "data/processed/{sample}_1_fastp.fastq.gz",
read2 = "data/processed/{sample}_2_fastp.fastq.gz"
output:
read1 = "results/fastqc_result/trimmed/{sample}_1_fastp_fastqc.html",
read2 = "results/fastqc_result/trimmed/{sample}_2_fastp_fastqc.html"
conda:
"code/environments/Greference_tools.yml"
shell:
"""
for read in {input.read1} {input.read2}
do
fastqc $read -o results/fastqc_result/trimmed/
done
"""
## 7 Creating sam files for forward and reverse reads
rule bwa_mapping:
input:
reference = "data/reference/genome.fa",
read1 = "data/processed/{sample}_1_fastp.fastq.gz",
read2 = "data/processed/{sample}_2_fastp.fastq.gz"
output:
sam = "results/mapped_reads/{sample}.sam",
sam_sorted = "results/mapped_reads/{sample}_sorted.sam"
log:
"metadata/logs/sam/{sample}_infosam.out"
conda:
"code/environments/Greference_tools.yml"
shell:
"""
## Mapping
bwa index {input.reference}
bwa mem -a {input.reference} \
{input.read1} {input.read2} \
-o {output.sam} \
2> {log}
## sorting the SAM files
samtools sort {output.sam} \
-o {output.sam_sorted}
"""
# 8 Transforming SAM to BAM and sorting
rule sam_to_bam:
input:
"results/mapped_reads/{sample}_sorted.sam",
output:
bam = "results/mapped_reads/bam_files/{sample}.bam",
bam_sorted = "results/mapped_reads/bam_files/{sample}_sorted.bam"
log:
"metadata/logs/flagstats/{sample}.flagstats"
conda:
"code/environments/Greference_tools.yml"
shell:
"""
## From SAM to BAM
samtools view -bS \
{input} \
> {output.bam}
## Sorting
samtools sort \
{output.bam} \
> {output.bam_sorted}
## Index
samtools index {output.bam_sorted}
## Summary, basic statistics
samtools flagstat \
{output.bam_sorted} \
> {log}
"""
## 9 Deleting duplicates
rule delete_duplicates:
input:
"results/mapped_reads/bam_files/{sample}_sorted.bam"
output:
"results/mapped_reads/bam_files/{sample}_dedup.bam"
conda:
"code/environments/Greference_tools.yml"
log:
"metadata/logs/markduplicates/{sample}_markDuplicatesMetrics.txt"
shell:
"""
## Mark duplicates
picard MarkDuplicates --INPUT {input} \
--OUTPUT {output} \
--METRICS_FILE {log} \
--ASSUME_SORTED True
## Indexing the new BAM file generated
samtools index {output}
"""
## 10 Extracting variants
rule extracting_variants:
input:
reference = "data/reference/genome.fa",
bam = "results/mapped_reads/bam_files/{sample}_dedup.bam"
output:
"results/variants/{sample}.vcf"
params:
ref_genome = config["ref_genome_name_file"],
min_reads= config["min_reads_variant"]
log:
"metadata/logs/vcfstats/{sample}.vcfstats"
conda:
"code/environments/Greference_tools.yml"
shell:
"""
freebayes \
-C {params.min_reads} \
-f {input.reference} \
{input.bam} \
> {output}
rtg vcfstats {output} > {log}
"""
## 11 Variant Effect Prediction DB (version of GRCh38 109)
rule vep_install_db:
output:
touch("tasks/11vep_dependencies.done")
params:
species = config["vep_species"],
assembly = config["vep_assembly"]
conda:
"code/environments/vep.yml"
shell:
"""
vep_install -a cf \
-s {params.species} \
--ASSEMBLY {params.assembly} \
--NO_UPDATE
"""
## 12 Running VEP in the command line
rule vep_cli:
input:
script_dl_clivar = "code/04vep.sh",
vcf = "results/variants/{sample}.vcf"
output:
"results/variants/vep/{sample}.txt"
params:
species = config["vep_species"],
assembly=config["vep_assembly"]
conda:
"code/environments/vep.yml"
shell:
"""
bash {input.script_dl_clivar}
vep -i {input.vcf} \
--offline \
--force_overwrite \
--assembly {params.assembly} \
--appris \
--biotype \
--variant_class \
--check_existing \
--filter_common \
--mane \
--polyphen b \
--pubmed \
--regulatory \
--sift b \
--species {params.species} \
--symbol \
--transcript_version \
--tsl \
--cache \
--tab \
-o {output} \
--custom data/ClinVar/clinvar.vcf.gz,ClinVar,vcf,exact,0,CLNSIG,CLNREVSTAT,CLNDN
"""
## 13 Parsing data from the VCF files with R
rule parsing_dataR:
input:
script = "code/05parsing_vep_data.R"
output:
touch("tasks/13parsing_dataR.done")
params:
sample_list=config["samples_names"],
dir1 = "results/biostatistics/",
dir2 = "results/biostatistics/tables",
dir3 = "results/biostatistics/plots",
dir4 = "results/biostatistics/joined_tables",
gene_filter=config["gene_to_filterR"],
chr_choosed=config["chromosome"],
conda:
"code/environments/biostatisticsR.yml"
shell:
"""
for dir in {params.dir1} {params.dir2} {params.dir3} {params.dir4}
do
if [[ ! -d $dir ]]
then
mkdir $dir
fi
done
Rscript {input.script} {params.gene_filter} {params.sample_list}
## Joining parsed tables for each sample
cat results/biostatistics/tables/*{params.chr_choosed}* \
| awk "!/^$(cat results/biostatistics/tables/*{params.chr_choosed}* \
| cut -f 1 | head -n 1)/ || NR == 1" \
> results/biostatistics/joined_tables/{params.gene_filter}.tsv
"""
##---------------------------------------------------------##
## FINAL BOSS, YOU NEED TO HAVE THE 5 GENE TABLES ##
##---------------------------------------------------------##
## 14 plotting with R
rule R_plotting:
input:
script = "code/06final_plot.R"
output:
png1="results/biostatistics/plots/final_plot.png",
png2="results/biostatistics/plots/other_plot.png"
conda:
"code/environments/biostatisticsR.yml"
shell:
"""
Rscript {input.script} && echo "THE SCRIPT RAN WELL congrats :)"
# If this is red you didn't made it yet, maybe this will encourage you :)
# https://www.youtube.com/watch?v=tYzMYcUty6s&ab_channel=TeamPsycosmos
"""