forked from arthurvinx/MEDUSA
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Snakefile
362 lines (333 loc) · 17.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
from os.path import join
import os
# input args
preprocessingDIR = "Pipeline/data"
alignmentDIR = "Pipeline/alignment"
taxonomicDIR = "Pipeline/taxonomic"
functionalDIR = "Pipeline/functional"
resultDIR = "Pipeline/result"
inputDIR = join(preprocessingDIR, "raw")
phredQuality = "20"
trimmedDIR = join(preprocessingDIR, "trimmed")
removalDIR = join(preprocessingDIR, "removal")
referenceDIR = join(removalDIR, "reference")
ensemblRelease = "104"
bowtie2IndexDIR = join(removalDIR, "index")
# max memory in byte to be used in SdBG construction (if set between 0-1, fraction of the machine's total memory)
megahitMemory = "0.9"
# max number of threads for kaiju-mkbwt
kaijumkbwtThreads = 10
assembledDIR = join(preprocessingDIR, "assembled")
mergedDIR = join(preprocessingDIR, "merged")
collapsedDIR = join(preprocessingDIR, "collapsed")
NRDIR = join(alignmentDIR, "db")
diamondIndexDIR = join(alignmentDIR, "index")
IDs = glob_wildcards(join(inputDIR,
"{id, [A-Za-z0-9]+}{suffix, (_[1-2])?}.fastq"))
ruleorder: qualityControlPaired > qualityControlSingle
ruleorder: removeHumanContaminantsPaired > removeHumanContaminantsSingle
ruleorder: deduplicatePaired > deduplicateSingle
ruleorder: diamondMakeDB > kaijuPrepare
ruleorder: assemblyPaired > assemblySingle
ruleorder: taxonomicClassificationPaired > taxonomicClassificationSingle
rule all:
input:
expand(join(resultDIR, "{id}_kaiju.names"), id = IDs.id),
expand(join(resultDIR, "{id}_contigs_kaiju.names"), id = IDs.id),
expand(join(resultDIR, "{id}_functional_GO.txt"), id = IDs.id),
expand(join(resultDIR, "{id}_functional_contigs_GO.txt"), id = IDs.id)
rule qualityControlSingle:
input: join(inputDIR, "{id}.fastq")
output:
trimmed = "{trimmedDIR}/{id}_trim.fastq",
html = "{trimmedDIR}/{id}_report.html",
json = "{trimmedDIR}/{id}_report.json"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& fastp -i {input} -o {output.trimmed} -q {phredQuality} -w {threads} -h {output.html} -j {output.json}"
rule qualityControlPaired:
input:
f = join(inputDIR, "{id}_1.fastq"),
r = join(inputDIR, "{id}_2.fastq"),
output:
f = "{trimmedDIR}/{id}_1_trim.fastq",
r = "{trimmedDIR}/{id}_2_trim.fastq",
html = "{trimmedDIR}/{id}_report.html",
json = "{trimmedDIR}/{id}_report.json"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& fastp -i {input.f} -I {input.r} -o {output.f} \
-O {output.r} -q {phredQuality} -w {threads} \
--detect_adapter_for_pe -h {output.html} -j {output.json}"
rule downloadHumanPrimaryAssembly:
output: "{referenceDIR}/Homo_sapiens.GRCh38.dna.primary_assembly.fa"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& wget -P {referenceDIR} \
ftp://ftp.ensembl.org/pub/release-{ensemblRelease}/fasta/homo_sapiens/dna/Homo_sapiens.GRCh38.dna.primary_assembly.fa.gz \
&& pigz -d {output} -p {threads}"
rule bowtie2BuildHumanIndex:
params: indexPrefix = join(bowtie2IndexDIR, "hostHS")
input: join(referenceDIR, "Homo_sapiens.GRCh38.dna.primary_assembly.fa")
output:
expand(join(bowtie2IndexDIR, "hostHS.{index}.bt2l"), index = range(1, 5)),
expand(join(bowtie2IndexDIR, "hostHS.rev.{index}.bt2l"), index = range(1, 3))
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& bowtie2-build --large-index {input} {params.indexPrefix} --threads {threads} \
&& rm {input}"
rule removeHumanContaminantsSingle:
params: indexPrefix = join(bowtie2IndexDIR, "hostHS"),
input:
expand(join(bowtie2IndexDIR, "hostHS.{index}.bt2l"), index = range(1, 5)),
expand(join(bowtie2IndexDIR, "hostHS.rev.{index}.bt2l"), index = range(1, 3)),
trimmed = join(trimmedDIR, "{id}_trim.fastq")
output: "{removalDIR}/{id}_unaligned.fastq"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& bowtie2 -x {params.indexPrefix} -U {input.trimmed} -S {removalDIR}/{wildcards.id}.sam -p {threads} \
&& samtools view -bS {removalDIR}/{wildcards.id}.sam > {removalDIR}/{wildcards.id}.bam \
&& samtools view -b -f 4 -F 256 {removalDIR}/{wildcards.id}.bam > {removalDIR}/{wildcards.id}_unaligned.bam \
&& samtools sort -n {removalDIR}/{wildcards.id}_unaligned.bam -o {removalDIR}/{wildcards.id}_unaligned_sorted.bam \
&& samtools bam2fq {removalDIR}/{wildcards.id}_unaligned_sorted.bam > {removalDIR}/{wildcards.id}_unaligned.fastq \
&& rm {removalDIR}/{wildcards.id}.sam {removalDIR}/{wildcards.id}_unaligned.bam {removalDIR}/{wildcards.id}_unaligned_sorted.bam {input.trimmed}"
rule removeHumanContaminantsPaired:
params: indexPrefix = join(bowtie2IndexDIR, "hostHS"),
input:
expand(join(bowtie2IndexDIR, "hostHS.{index}.bt2l"), index = range(1, 5)),
expand(join(bowtie2IndexDIR, "hostHS.rev.{index}.bt2l"), index = range(1, 3)),
f = join(trimmedDIR, "{id}_1_trim.fastq"),
r = join(trimmedDIR, "{id}_2_trim.fastq")
output:
f = "{removalDIR}/{id}_unaligned_1.fastq",
r = "{removalDIR}/{id}_unaligned_2.fastq"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& bowtie2 -x {params.indexPrefix} -1 {input.f} -2 {input.r} -S {removalDIR}/{wildcards.id}.sam -p {threads} \
&& samtools view -bS {removalDIR}/{wildcards.id}.sam > {removalDIR}/{wildcards.id}.bam \
&& samtools view -b -f 12 -F 256 {removalDIR}/{wildcards.id}.bam > {removalDIR}/{wildcards.id}_unaligned.bam \
&& samtools sort -n {removalDIR}/{wildcards.id}_unaligned.bam -o {removalDIR}/{wildcards.id}_unaligned_sorted.bam \
&& samtools bam2fq {removalDIR}/{wildcards.id}_unaligned_sorted.bam > {removalDIR}/{wildcards.id}_unaligned.fastq \
&& cat {removalDIR}/{wildcards.id}_unaligned.fastq | grep '^@.*/1$' -A 3 --no-group-separator > {removalDIR}/{wildcards.id}_unaligned_1.fastq \
&& cat {removalDIR}/{wildcards.id}_unaligned.fastq | grep '^@.*/2$' -A 3 --no-group-separator > {removalDIR}/{wildcards.id}_unaligned_2.fastq \
&& rm {removalDIR}/{wildcards.id}.sam {removalDIR}/{wildcards.id}_unaligned.bam {removalDIR}/{wildcards.id}_unaligned_sorted.bam {removalDIR}/{wildcards.id}_unaligned.fastq {input.f} {input.r}"
rule mergePaired:
input:
f = join(removalDIR, "{id}_unaligned_1.fastq"),
r = join(removalDIR, "{id}_unaligned_2.fastq")
output:
merged = "{mergedDIR}/{id}_merged.fastq",
html = "{mergedDIR}/{id}_report.html",
json = "{mergedDIR}/{id}_report.json"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& fastp -i {input.f} -I {input.r} -o {mergedDIR}/{wildcards.id}_unmerged_1.fastq \
-O {mergedDIR}/{wildcards.id}_unmerged_2.fastq -q {phredQuality} -w {threads} \
--detect_adapter_for_pe -h {output.html} -j {output.json} \
-m --merged_out {output.merged}"
rule assemblySingle:
input:
reads = join(removalDIR, "{id}_unaligned.fastq")
output:
contigs = "{assembledDIR}/{id}/final.contigs.fa"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& megahit -r {input.reads} --force -o {assembledDIR}/{wildcards.id} -t {threads} -m {megahitMemory}"
rule assemblyPaired:
input:
f = join(removalDIR, "{id}_unaligned_1.fastq"),
r = join(removalDIR, "{id}_unaligned_2.fastq")
output:
contigs = "{assembledDIR}/{id}/final.contigs.fa"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& megahit -1 {input.f} -2 {input.r} --force -o {assembledDIR}/{wildcards.id} -t {threads} -m {megahitMemory}"
rule taxonomicClassificationSingle:
input:
reads = join(collapsedDIR, "{id}_collapsed.fasta"),
fmi = join(taxonomicDIR, "db/kaijuNR.fmi"),
names = join(taxonomicDIR, "db/names.dmp"),
nodes = join(taxonomicDIR, "db/nodes.dmp")
output:
ranks = "{resultDIR}/{id}_kaiju.names",
html = "{resultDIR}/{id}_krona.html"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& kaiju -t {input.nodes} -f {input.fmi} -i {input.reads} -o {resultDIR}/kaiju.out -z {threads} \
&& kaiju-addTaxonNames -t {input.nodes} -n {input.names} -r superkingdom,phylum,class,order,family,genus,species -i {resultDIR}/kaiju.out -o {output.ranks} \
&& kaiju2krona -t {input.nodes} -n {input.names} -i {resultDIR}/kaiju.out -o {resultDIR}/kaiju_krona \
&& ktImportText -o {output.html} {resultDIR}/kaiju_krona \
&& rm {resultDIR}/kaiju.out {resultDIR}/kaiju_krona"
rule taxonomicClassificationPaired:
input:
f = join(removalDIR, "{id}_unaligned_1.fastq"),
r = join(removalDIR, "{id}_unaligned_2.fastq"),
fmi = join(taxonomicDIR, "db/kaijuNR.fmi"),
names = join(taxonomicDIR, "db/names.dmp"),
nodes = join(taxonomicDIR, "db/nodes.dmp")
output:
ranks = "{resultDIR}/{id}_kaiju.names",
html = "{resultDIR}/{id}_krona.html"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& kaiju -t {input.nodes} -f {input.fmi} -i {input.f} -j {input.r} -o {resultDIR}/kaiju.out -z {threads} \
&& kaiju-addTaxonNames -t {input.nodes} -n {input.names} -r superkingdom,phylum,class,order,family,genus,species -i {resultDIR}/kaiju.out -o {output.ranks} \
&& kaiju2krona -t {input.nodes} -n {input.names} -i {resultDIR}/kaiju.out -o {resultDIR}/kaiju_krona \
&& ktImportText -o {output.html} {resultDIR}/kaiju_krona \
&& rm {resultDIR}/kaiju.out {resultDIR}/kaiju_krona"
rule taxonomicClassificationContigs:
input:
reads = join(assembledDIR, "{id}/final.contigs.fa"),
fmi = join(taxonomicDIR, "db/kaijuNR.fmi"),
names = join(taxonomicDIR, "db/names.dmp"),
nodes = join(taxonomicDIR, "db/nodes.dmp")
output:
ranks = "{resultDIR}/{id}_contigs_kaiju.names",
html = "{resultDIR}/{id}_contigs_krona.html"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& kaiju -t {input.nodes} -f {input.fmi} -i {input.reads} -o {resultDIR}/kaiju.out -z {threads} \
&& kaiju-addTaxonNames -t {input.nodes} -n {input.names} -r superkingdom,phylum,class,order,family,genus,species -i {resultDIR}/kaiju.out -o {output.ranks} \
&& kaiju2krona -t {input.nodes} -n {input.names} -i {resultDIR}/kaiju.out -o {resultDIR}/kaiju_krona \
&& ktImportText -o {output.html} {resultDIR}/kaiju_krona \
&& rm {resultDIR}/kaiju.out {resultDIR}/kaiju_krona"
rule deduplicateSingle:
input: join(removalDIR, "{id}_unaligned.fastq")
output: "{collapsedDIR}/{id}_collapsed.fasta"
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& fastx_collapser -i {input} -o {output}"
rule deduplicatePaired:
input: join(mergedDIR, "{id}_merged.fastq")
output: "{collapsedDIR}/{id}_collapsed.fasta"
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& fastx_collapser -i {input} -o {output}"
rule downloadNR:
output: "{NRDIR}/nr"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& wget -P {NRDIR} \
ftp://ftp.ncbi.nlm.nih.gov/blast/db/FASTA/nr.gz \
&& pigz -d {output} -p {threads}"
rule diamondMakeDB:
input: join(NRDIR, "nr")
output: "{diamondIndexDIR}/nr.dmnd"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& diamond makedb --in {input} -d {output} --threads {threads}"
rule kaijuPrepare:
input: join(NRDIR, "nr")
output:
convertedNR = "{taxonomicDIR}/db/kaijuNR.fasta",
names = "{taxonomicDIR}/db/names.dmp",
nodes = "{taxonomicDIR}/db/nodes.dmp"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& wget -P {taxonomicDIR}/db ftp://ftp.ncbi.nlm.nih.gov/pub/taxonomy/taxdump.tar.gz \
&& tar -xf {taxonomicDIR}/db/taxdump.tar.gz nodes.dmp names.dmp \
&& mv nodes.dmp {taxonomicDIR}/db && mv names.dmp {taxonomicDIR}/db \
&& rm {taxonomicDIR}/db/taxdump.tar.gz \
&& wget -P {taxonomicDIR}/db ftp://ftp.ncbi.nlm.nih.gov/pub/taxonomy/accession2taxid/prot.accession2taxid.gz \
&& pigz -d {taxonomicDIR}/db/prot.accession2taxid.gz -p {threads} \
&& kaiju-convertNR -t {output.nodes} -g {taxonomicDIR}/db/prot.accession2taxid -e $(conda info --base)/envs/medusaPipeline/bin/kaiju-excluded-accessions.txt -a -o {output.convertedNR} -i {input} \
&& rm {taxonomicDIR}/db/prot.accession2taxid {input}"
rule kaijuMakeBWT:
input: join(taxonomicDIR, "db/kaijuNR.fasta")
output:
bwt = "{taxonomicDIR}/db/kaijuNR.bwt",
sa = "{taxonomicDIR}/db/kaijuNR.sa"
threads: kaijumkbwtThreads
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& kaiju-mkbwt -n {threads} -a ACDEFGHIKLMNPQRSTVWY -o {taxonomicDIR}/db/kaijuNR {input} \
&& rm {input}"
rule kaijuMakeFMI:
input:
bwt = join(taxonomicDIR, "db/kaijuNR.bwt"),
sa = join(taxonomicDIR, "db/kaijuNR.sa")
output:
fmi = "{taxonomicDIR}/db/kaijuNR.fmi"
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& kaiju-mkfmi {taxonomicDIR}/db/kaijuNR \
&& rm {input.bwt} {input.sa}"
rule alignment:
input:
index = join(diamondIndexDIR, "nr.dmnd"),
reads = join(collapsedDIR, "{id}_collapsed.fasta")
output:
matches = "{alignmentDIR}/{id}.m8",
unaligned = "{alignmentDIR}/{id}_unaligned.fasta"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& touch {output.unaligned} \
&& diamond blastx -d {input.index} -q {input.reads} -o {output.matches} --top 3 --un {output.unaligned} --threads {threads}"
rule alignmentContigs:
input:
index = join(diamondIndexDIR, "nr.dmnd"),
reads = join(assembledDIR, "{id}/final.contigs.fa")
output:
matches = "{alignmentDIR}/{id}_contigs.m8",
unaligned = "{alignmentDIR}/{id}_contigs_unaligned.fasta"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& touch {output.unaligned} \
&& diamond blastx -d {input.index} -q {input.reads} -o {output.matches} --top 3 -F 15 --range-culling --un {output.unaligned} --threads {threads}"
rule downloadUniprotMapping:
output: "{functionalDIR}/idmapping_selected.tab"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& wget -P {functionalDIR} \
ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/idmapping/idmapping_selected.tab.gz \
&& pigz -d {output} -p {threads}"
rule createDictionaries:
input: join(functionalDIR, "idmapping_selected.tab")
output:
directory("{functionalDIR}/db/NR2GO.ldb")
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& awk -F \"\t\" '{{if(($7!=\"\") && ($18!=\"\")){{print $18\"\t\"$7}}}}' {input} > {functionalDIR}/genbank2GO.txt \
&& awk -F \"\t\" '{{if(($4!=\"\") && ($7!=\"\")){{print $4\"\t\"$7}}}}' {input} > {functionalDIR}/refseq2GO.txt \
&& Rscript createDictionary.R {functionalDIR}/NR2GO.txt {functionalDIR}/genbank2GO.txt {functionalDIR}/refseq2GO.txt {threads} \
&& annotate createdb {functionalDIR}/NR2GO.txt NR2GO 0 1 -d {functionalDIR}/db \
&& rm {functionalDIR}/genbank2GO.txt {functionalDIR}/refseq2GO.txt"
rule annotateGO:
input:
matches = join(alignmentDIR, "{id}.m8"),
NR2GO = join(functionalDIR, "db/NR2GO.ldb")
output:
GO = "{resultDIR}/{id}_functional_GO.txt",
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& annotate idmapping {input.matches} {output.GO} NR2GO -l 1 -d {functionalDIR}/db"
rule annotateGOContigs:
input:
matchesContigs = join(alignmentDIR, "{id}_contigs.m8"),
NR2GO = join(functionalDIR, "db/NR2GO.ldb")
output:
contigsGO = "{resultDIR}/{id}_functional_contigs_GO.txt"
threads: workflow.cores
shell: "set +eu \
&& . $(conda info --base)/etc/profile.d/conda.sh && conda activate medusaPipeline \
&& annotate idmapping {input.matchesContigs} {output.contigsGO} NR2GO -l 1 -d {functionalDIR}/db"