-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
318 lines (293 loc) · 12.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
################################################################################
# Config file and variable declaration.
################################################################################
configfile: "config.yaml"
SAMPLES, = glob_wildcards("data/{sample}_1.fastq.gz")
PATH_DB=config['path_DB']
PATH_ANNOT=config['path_annot']
PATH_RESF=config['path_resf']
SEQ_LEN=config['seq_len']
#create adaptersfile? fastqc?
################################################################################
# Rule "all" executes the entire pipeline when no argument is given.
################################################################################
rule all:
input:
expand("10_annotation/{sample}/{sample}_summary_AMR.tsv", sample=SAMPLES),
expand("06_checkm_result/{sample}/qa_result_checkm.tsv", sample=SAMPLES),
"01_fastqc/multiqc_report.html"
################################################################################
#
# Rules to execute the pipeline:
#
################################################################################
################################################################################
# 1) Run FastQC
################################################################################
rule fastQC:
input:
A="data/{sample}_1.fastq.gz",
B="data/{sample}_2.fastq.gz"
output:
"01_fastqc/{sample}_1_fastqc.zip"
params:
dir="01_fastqc"
threads: 4
shell:
"fastqc -t {threads} -o {params.dir} {input}"
################################################################################
# 2) MultiQC to summarize fastQC
################################################################################
rule multiQC:
input:
expand("01_fastqc/{sample}_1_fastqc.zip", sample=SAMPLES)
output:
"01_fastqc/multiqc_report.html"
params:
dir="01_fastqc/"
shell:
"multiqc {params.dir} -o {params.dir}"
################################################################################
# 3) Trim adapters using trimmomatic.
################################################################################
rule trim:
input:
A="data/{sample}_1.fastq.gz",
B="data/{sample}_2.fastq.gz",
adp="adapters.fa"
output:
"02_trimmed/{sample}/{sample}_1_paired.fastq.gz",
"02_trimmed/{sample}/{sample}_1_unpaired.fastq.gz",
"02_trimmed/{sample}/{sample}_2_paired.fastq.gz",
"02_trimmed/{sample}/{sample}_2_unpaired.fastq.gz"
threads: 8
shell:
'''
trimmomatic PE -threads {threads} -phred33 {input.A} {input.B} {output} \
ILLUMINACLIP:{input.adp}:2:30:10 LEADING:3 TRAILING:3 SLIDINGWINDOW:4:15 MINLEN:35
'''
################################################################################
# 4) Create contigs using megahit, with specified minimum contig length 1500 bp.
################################################################################
rule assemble:
input:
A="02_trimmed/{sample}/{sample}_1_paired.fastq.gz",
B="02_trimmed/{sample}/{sample}_2_paired.fastq.gz"
output:
"03_megahit_assembly/{sample}/final.contigs.fa"
params:
dir="03_megahit_assembly/{sample}"
threads: 16
shell:
# -fo : force outputfolder; without -f megahit requires a non-existing directory.
# -f is needed because snakemake creates te outputfolder before executing the command
"megahit -1 {input.A} -2 {input.B} --min-contig-len 1500 -fo {params.dir} -t {threads}"
################################################################################
# 5) Using BWA (Burrows-Wheeler Aligner),
# align trimmed reads to contigs created by megahit.
################################################################################
rule align:
input:
fa="03_megahit_assembly/{sample}/final.contigs.fa",
A="02_trimmed/{sample}/{sample}_1_paired.fastq.gz",
B="02_trimmed/{sample}/{sample}_2_paired.fastq.gz"
output:
bam="04_align/{sample}/{sample}_results.bwa.bam",
flag="04_align/{sample}/{sample}_results.flagstat"
params:
tmp="04_align/{sample}/temp.bwa"
threads: 8
shell:
'''
bwa index {input.fa}
bwa mem -t {threads} {input.fa} {input.A} {input.B} | samtools view -T \
{input.fa} -bS - | samtools sort -T {params.tmp} -o {output.bam} -
samtools flagstat {output.bam} > {output.flag}
'''
################################################################################
# 6) Calculate coverage & depth of contigs, the jgi script is part of MetaBAT.
################################################################################
rule coverage:
input:
"04_align/{sample}/{sample}_results.bwa.bam"
output:
"04_align/{sample}/{sample}_results_depth.txt"
shell:
'''
jgi_summarize_bam_contig_depths --outputDepth {output} --minContigLength \
2000 --minContigDepth 2 {input}
'''
################################################################################
# 7) Using MetaBAT, create bins with contigs having similar depth.
# Based on contig length and frequency of seq.
################################################################################
rule binning:
input:
asm="03_megahit_assembly/{sample}/final.contigs.fa",
depth="04_align/{sample}/{sample}_results_depth.txt"
output:
"05_metabat_bins/{sample}/{sample}_bin.1.fa"
params:
dir="05_metabat_bins/{sample}/{sample}_bin"
threads: 8
shell:
# -m = min contig length, -o = prefix for output
"metabat2 -m 2000 -i {input.asm} -a {input.depth} -o {params.dir} -t {threads}"
################################################################################
# 8) Using checkm, check bins for marker genes and give taxonomic rank.
# checkm needs +/- 35 G RAM !
# execute snakemake --use-conda to read conda file for checkm rule
# add --cores <int> to make use of threads with <int> >= threads
# checkm uses python 2.7 , other software python 3 --> need seperate conda file
# with checkm environment.
################################################################################
################################################################################
# 8a) Download the checkm database and set root for checkm.
################################################################################
rule checkm_root:
output:
"00_checkm_data/done_tar.log"
threads: 4
params:
dir="00_checkm_data/"
conda: "envs/checkm.yaml"
shell:
# root needs to be set for every new checkm environment
# wget -N and tar --skip-old-files prevent redownloading and unpacking same thing
'''
wget -N https://data.ace.uq.edu.au/public/CheckM_databases/checkm_data_2015_01_16.tar.gz -P {params.dir}
tar --skip-old-files -xzvf {params.dir}*.tar.gz -C {params.dir} --index-file={output}
echo {params.dir} | checkm data setRoot {params.dir}
'''
################################################################################
# 8b) Checkm lineage_wf: look for taxonomic markers in bins in order to classify
# these bins.
################################################################################
rule checkm_lineage:
input:
"00_checkm_data/done_tar.log",
"05_metabat_bins/{sample}/{sample}_bin.1.fa"
output:
"06_checkm_result/{sample}/lineage.ms"
threads: 16
params:
dir="06_checkm_result/{sample}/",
bins="05_metabat_bins/{sample}/"
conda: "envs/checkm.yaml"
shell:
# add -f to save output in default outputtype 1
"checkm lineage_wf -t {threads} -x fa {params.bins} {params.dir}"
################################################################################
# 8c) Checkm qa: this step is incorporated by the previous command, but is
# repeated to generate an extended summary of bin quality, specified by -o 2.
################################################################################
rule checkm_qa:
input:
"06_checkm_result/{sample}/lineage.ms"
output:
"06_checkm_result/{sample}/qa_result_checkm.tsv"
threads: 4
params:
dir="06_checkm_result/{sample}/"
conda: "envs/checkm.yaml"
shell:
# -o 2 for tsv version of output
"checkm qa -t {threads} {input} {params.dir} -f {output} --tab_table -o 2"
################################################################################
# 9) Prodigal to predict proteins from contigs of step 2.
################################################################################
rule predict:
input:
"03_megahit_assembly/{sample}/final.contigs.fa"
output:
meta="07_prodigal/{sample}/{sample}_metadata_genes.txt",
pred="07_prodigal/{sample}/{sample}_predicted_genes.fa"
#threads: 4 # no option for threads
shell:
"prodigal -i {input} -o {output.meta} -a {output.pred}"
################################################################################
# 10) Run diamond results against database
################################################################################
rule diamond_kegg:
input:
db= PATH_DB,
pred="07_prodigal/{sample}/{sample}_predicted_genes.fa"
output:
"08_diamond_blastP/{sample}/{sample}_blastp_matches_kegg.m8"
threads: 8
shell:
"diamond blastp --max-target-seqs 10 -d {input.db} -q {input.pred} -o {output} --threads {threads}"
################################################################################
# 11) Run diamond results against original reads
################################################################################
########################
# 11a) make protein DB #
########################
rule makedb:
input:
"07_prodigal/{sample}/{sample}_predicted_genes.fa"
output:
"09_diamond_blastX/{sample}/{sample}_predicted.dmnd"
threads: 16
shell:
"diamond makedb --in {input} -d {output} --threads {threads}"
####################
# 11b) run blastX #
####################
rule blastx:
input:
db="09_diamond_blastX/{sample}/{sample}_predicted.dmnd",
reads="02_trimmed/{sample}/{sample}_1_paired.fastq.gz"
output:
"09_diamond_blastX/{sample}/{sample}_blastx_matches_reads.m8"
threads: 16
shell:
"diamond blastx --max-target-seqs 1 -d {input.db} -q {input.reads} -o {output} --threads {threads}"
################################################################################
# 12) Annotate blastP results from step 10
################################################################################
rule annotate_blastP:
input:
m8="08_diamond_blastP/{sample}/{sample}_blastp_matches_kegg.m8"
output:
"10_annotation/{sample}/{sample}_summary.tsv"
params:
dir="10_annotation/{sample}/{sample}_",
annot=PATH_ANNOT
shell:
"scripts/kegg_info.py {input.m8} {params.annot} {params.dir}"
################################################################################
# 13) Count and annotate blastX results from step 11
################################################################################
rule count_blastX:
input:
m8="09_diamond_blastX/{sample}/{sample}_blastx_matches_reads.m8",
need="10_annotation/{sample}/{sample}_summary.tsv"
output:
"10_annotation/{sample}/{sample}_counts_protein"
params:
dir="10_annotation/{sample}/{sample}_",
sl=SEQ_LEN
shell:
"scripts/count_annotate.py {input.m8} {params.dir} {params.sl}"
################################################################################
# 14) Look for AMR genes using ABRicate
################################################################################
rule abricate:
input:
fa="03_megahit_assembly/{sample}/final.contigs.fa"
output:
tsv="11_AMR/{sample}/{sample}_AMR.tsv"
shell:
"abricate {input.fa} > {output.tsv}"
################################################################################
# 15) Annotate AMR genes
################################################################################
rule amr:
input:
amr="11_AMR/{sample}/{sample}_AMR.tsv",
resf=PATH_RESF
output:
tsv="10_annotation/{sample}/{sample}_summary_AMR.tsv"
shell:
"scripts/presence_amr.py {input.amr} {input.resf} {output.tsv}"