-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSnakefile
More file actions
224 lines (190 loc) · 5.46 KB
/
Snakefile
File metadata and controls
224 lines (190 loc) · 5.46 KB
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
import glob
import os
def get_run_accessions(file):
accns = list()
meta = open(file, mode="r")
for row in meta:
arr = row.strip('\n').split('\t')
accns.append(arr[0])
return(accns)
SETONE = get_run_accessions("samples.tsv")
SETTWO = SETONE
print(SETONE)
localrules: combine_coverage, filter, mash_build
rule all:
input: expand("metabat_single/{sample}/", sample=SETONE), expand("metabat_combined/{sample}/", sample=SETONE), expand("checkm/single/{sample}/checkm.tsv", sample=SETONE), expand("checkm/combined/{sample}/checkm.tsv", sample=SETONE), expand("filtered/single/{sample}", sample=SETONE), expand("filtered/combined/{sample}", sample=SETONE), "checkm_collated/single_checkm.tsv"
rule download_fastq:
output:
R1="fastq/{id}_1.fastq.gz",
R2="fastq/{id}_2.fastq.gz"
params:
ID="{id}"
conda: "envs/fastqdump.yaml"
shell:
'''
fastq-dump --outdir fastq --split-spot --split-files --gzip {params.ID}
'''
rule trim_paired:
input:
R1="fastq/{id}_1.fastq.gz",
R2="fastq/{id}_2.fastq.gz"
output:
R1="trimmed/{id}_1_val_1.fq.gz",
R2="trimmed/{id}_2_val_2.fq.gz"
conda: "envs/trim.yaml"
threads: 2
shell:
'''
trim_galore --cores 2 --output_dir trimmed --illumina --gzip --paired {input.R1} {input.R2} && rm {input.R1} {input.R2}
'''
rule megahit:
input:
R1="trimmed/{id}_1_val_1.fq.gz",
R2="trimmed/{id}_2_val_2.fq.gz"
params:
di="megahit/{id}"
output:
di="megahit/{id}/",
fa="megahit/{id}/final.contigs.fa"
conda: "envs/megahit.yaml"
threads: 8
shell: "mkdir -p {params.di} && megahit --continue --k-list 27,47,67,87 --kmin-1pass -m 0.95 --min-contig-len 1000 -t {threads} -1 {input.R1} -2 {input.R2} -o {params.di} && rm {params.di}/intermediate_contigs"
rule bwa_index:
input: "megahit/{id}/final.contigs.fa"
output:
pac="bwa_indices/{id}.fa.pac",
params:
idx="bwa_indices/{id}.fa"
conda: "envs/bwa.yaml"
threads: 8
shell:
'''
bwa index -p {params.idx} {input}
'''
rule bwa_mem:
input:
R1="trimmed/{id}_1_val_1.fq.gz",
R2="trimmed/{id}_2_val_2.fq.gz",
pac="bwa_indices/{id2}.fa.pac"
output:
cov="coverage/{id}.{id2}.txt"
params:
idx="bwa_indices/{id2}.fa",
bam="bam/{id}.{id2}.bam",
fla="bam/{id}.{id2}.bam.flagstat"
conda: "envs/bwa.yaml"
threads: 8
shell:
'''
mkdir -p bam
bwa mem -t 8 {params.idx} {input.R1} {input.R2} | samtools sort -@8 -m 500M -o {params.bam} -
samtools index {params.bam}
samtools flagstat {params.bam} > {params.fla}
jgi_summarize_bam_contig_depths --outputDepth {output.cov} {params.bam} && rm {params.bam} {params.bam}.bai
'''
rule combine_coverage:
input: expand("coverage/{sample}.{id}.txt", sample=SETONE, allow_missing=True)
output: "combined_coverage/{id}.txt"
shell:
'''
perl scripts/combine.pl {input} > {output}
'''
rule metabat_single:
input:
cov="coverage/{id}.{id}.txt",
asm="megahit/{id}/final.contigs.fa"
output:
unb="metabat_single/{id}/{id}.unbinned.fa",
dir=directory("metabat_single/{id}/")
params:
out="metabat_single/{id}/{id}"
threads: 8
conda: "envs/metabat2.yaml"
shell:
'''
metabat2 --minContig 1500 -t {threads} -i {input.asm} -a {input.cov} --unbinned -o {params.out}
'''
rule metabat_combined:
input:
cov="combined_coverage/{id}.txt",
asm="megahit/{id}/final.contigs.fa"
output:
unb="metabat_combined/{id}/{id}.unbinned.fa",
dir=directory("metabat_combined/{id}/")
params:
out="metabat_combined/{id}/{id}"
threads: 8
conda: "envs/metabat2.yaml"
shell:
'''
metabat2 --minContig 1500 -t {threads} -i {input.asm} -a {input.cov} --unbinned -o {params.out}
'''
rule checkm_data:
output: directory("checkm_data")
conda: "envs/checkm.yaml"
shell:
'''
mkdir -p {output}
cd {output}
wget https://data.ace.uq.edu.au/public/CheckM_databases/checkm_data_2015_01_16.tar.gz
gunzip < checkm_data_2015_01_16.tar.gz | tar xvf -
cd ..
echo checkm_data | checkm data setRoot checkm_data
'''
rule checkm_single:
input:
gin="metabat_single/{id}/",
dat=ancient("checkm_data")
output:
dir=directory("checkm/single/{id}"),
tsv="checkm/single/{id}/checkm.tsv"
threads: 8
conda: "envs/checkm.yaml"
shell:
'''
checkm lineage_wf --tab_table -f {output.tsv} -t {threads} -x fa {input.gin} {output.dir}
'''
rule checkm_combined:
input:
gin="metabat_combined/{id}/",
dat=ancient("checkm_data")
output:
dir=directory("checkm/combined/{id}"),
tsv="checkm/combined/{id}/checkm.tsv"
threads: 8
conda: "envs/checkm.yaml"
shell:
'''
checkm lineage_wf --tab_table -f {output.tsv} -t {threads} -x fa {input.gin} {output.dir}
'''
rule filter:
input:
sinc="checkm/single/{id}/checkm.tsv",
comc="checkm/combined/{id}/checkm.tsv",
sinb="metabat_single/{id}",
comb="metabat_combined/{id}"
output:
sin=directory("filtered/single/{id}"),
com=directory("filtered/combined/{id}")
shell:
'''
perl scripts/filter_genomes.pl {input.sinc} {input.sinb} {output.sin}
perl scripts/filter_genomes.pl {input.comc} {input.comb} {output.com}
'''
rule collate_checkm:
input:
hed=expand("checkm/single/{sample}/checkm.tsv", sample=SETONE[1]),
all=expand("checkm/single/{sample}", sample=SETONE)
output:
sin="checkm_collated/single_checkm.tsv",
com="checkm_collated/combined_checkm.tsv"
shell:
'''
mkdir -p checkm_collated
touch {output.sin}
touch {output.com}
cat {input.hed} | head -1 >> {output.sin}
cat checkm/single/*/checkm.tsv | grep -v Bin >> {output.sin}
cat {input.hed} | head -1 >> {output.com}
cat checkm/combined/*/checkm.tsv | grep -v Bin >> {output.com}
'''