-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProfiles.snake
325 lines (290 loc) · 14.2 KB
/
Profiles.snake
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
#from Bio.SeqIO.FastaIO import SimpleFastaParser as SFP
from os.path import dirname,basename,join
from functools import partial
#import gzip
# ---- generate contig bed ----------------------------------------------------
rule bogus_bed:
input: contig="{group}/contigs/contigs.fa"
output: bed=temp("{group}/annotation/contigs.bedtemp")
conda : CONDA_ENV + "/pythonenv.yaml"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "docker://quay.io/annacprice/pythonenv:3.9"
shell : "{SCRIPTS}/bogus_bed.py -i {input.contig} -o {output.bed}"
# ---- generate orfs bed ----------------------------------------------------
rule bed_orfs:
input: gff = "{path}/contigs.gff"
output: bed = temp("{path}/orf.bedtemp")
conda : CONDA_ENV + "/pythonenv.yaml"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "docker://quay.io/annacprice/pythonenv:3.9"
shell : "{SCRIPTS}/Gff_to_bed.py {input.gff} {output.bed}"
rule sort_bed:
input: bed = "{path}/annotation/{type}.bedtemp",
cont = "{path}/contigs/contigs.fa"
output: bed = "{path}/annotation/{type}.bed",
gfile = temp("{path}/annotation/{type}_bedtools_target_definition.tsv")
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
conda : "%s/pythonenv.yaml"%CONDA_ENV
singularity : "docker://quay.io/annacprice/pythonenv:3.9"
shell: "{SCRIPTS}/sort_bed.py {input.bed} {input.cont} {output.bed} -g {output.gfile}"
# ---- use bedtool to compute coverage ----------------------------------------------------
rule bedtools:
input: bam = "{group}/map/{sample}_mapped_sorted.bam",
bed = "{group}/annotation/{type}.bed",
genome = "{group}/annotation/{type}_bedtools_target_definition.tsv"
output: "{group}/map/{sample}.{type}.cov"
log: "{group}/map/{sample}_{type}.log"
priority: 50
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
conda : CONDA_ENV + "/bedtools.yaml"
singularity : "docker://quay.io/annacprice/bedtools:2.29.2"
shell: "bedtools coverage -a {input.bed} -b {input.bam} -g {input.genome} -mean -sorted> {output} 2>{log} "
# ---- collate all files -----------------------
if COVERAGE_UNIQUE_SAMPLE:
rule coverage:
input: lambda w : ["%s/map/%s.{type}.cov"%(w.group,sample.split('/')[-1]) for sample in GROUPS[w.group]]
output: "{group}/profile/coverage_{type}.tsv"
conda : CONDA_ENV + "/pythonenv.yaml"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "docker://quay.io/annacprice/pythonenv:3.9"
shell : "{SCRIPTS}/collate_coverage.py -o {output} -l {input} "
else:
rule coverage:
input: expand("{{group}}/map/{sample}.{{type}}.cov",sample=SAMPLE_READS)
output: "{group}/profile/coverage_{type}.tsv"
conda : CONDA_ENV + "/pythonenv.yaml"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "docker://quay.io/annacprice/pythonenv:3.9"
shell : "{SCRIPTS}/collate_coverage.py -o {output} -l {input} "
# ---- generate a mapping of annotation to orfs -----------------------
rule map_annotation_to_orfs:
input: "{path}/contigs_{annotation}_best_hits.tsv"
output: "{path}/map_{annotation}_to_orfs.tsv"
conda : CONDA_ENV + "/pythonenv.yaml"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "docker://quay.io/annacprice/pythonenv:3.9"
shell: """{SCRIPTS}/Annotation_listcontig.py {input} {output} """
rule generate_mcard:
input: card = "{path}/contigs_CARD_best_hits.tsv",
genomad = "{path}/genomad/genomad.done"
output: "{path}/map_mCARD_to_orfs.tsv"
conda : CONDA_ENV + "/pythonenv.yaml"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "docker://quay.io/annacprice/pythonenv:3.9"
shell: """{SCRIPTS}/genomad_card.py {input} {output} """
# ---- generate annotation profile -----------------------
rule generate_profile:
input: cov="{group}/profile/coverage_orf.tsv",
map="{group}/annotation/map_{annotation}_to_orfs.tsv"
output: "{group}/profile/cov_{annotation}.tsv"
conda : CONDA_ENV + "/pythonenv.yaml"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "docker://quay.io/annacprice/pythonenv:3.9"
shell: "{SCRIPTS}/Extract_gene_profile.py {input.map} {input.cov} {output}"
# ----------------------------------------------------------
# ---- generate Normalisation file -----------------------
# ----------------------------------------------------------
def filtered(file,FILTER):
if FILTER:
file = "%s/Filtered_%s"%(dirname(file),basename(file))
return(file)
# ---- generate nb nucleotides by sample -----------------------
if IS_FASTA :
rule nb_bases_fasta:
input: lambda w : [fastq_file for sample in GROUPS[w.group] for fastq_file in SAMPLE_READS[basename(sample)]]
output: temp("{group}/profile/nucleotides.tsv")
conda : CONDA_ENV + "/pythonenv.yaml"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "docker://quay.io/annacprice/pythonenv:3.9"
script: "{SCRIPTS}/nb_bases_fasta.py {input} {output}"
else :
rule nb_bases:
input: lambda w : [filtered(fastq_file,FILTER)+"_trimming_report.txt" for sample in GROUPS[w.group] for fastq_file in SAMPLE_READS[basename(sample)]]
output: temp("{group}/profile/nucleotides.tsv")
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
run:
dict_sample_nb=defaultdict(list)
for file in input:
sample=basename(dirname(file))
with open(file) as handle:
line=next(handle)
while "Total written (filtered):" not in line:
line=next(handle)
nb=line.split(":")[1].split("bp")[0].replace(",","").replace(" ","")
dict_sample_nb[sample].append(float(nb))
dict_sample_nb={sample:str(sum(list_nb)) for sample,list_nb in dict_sample_nb.items()}
print(output[0])
with open(output[0],"w") as handle:
handle.write("Normalisation\t"+"\t".join(dict_sample_nb.keys())+"\n")
handle.write("Nb_nucleotides\t"+"\t".join(dict_sample_nb.values())+"\n")
handle.close()
# ---- generate cog profile -----------------------
rule median_SCG_cov:
input: cov="{group}/profile/cov_cogs.tsv",
nuc="{group}/profile/nucleotides.tsv"
output: "{group}/profile/Normalisation.tsv"
params: scgdata=SCG_DATA
conda: CONDA_ENV + "/pythonenv.yaml"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity: "docker://quay.io/annacprice/pythonenv:3.9"
shell : """
{SCRIPTS}/median_scg.py -c {input.cov} -n {input.nuc} -o {output} -s {params.scgdata}
"""
# ---- percent maped info -----------------------
rule mapped_reads:
input: bam="{group}/map/{sample}_mapped_sorted.bam"
output: readnb="{group}/map/{sample}_mapped_read.txt"
threads: 8
priority: 50
conda : CONDA_ENV + "/bwasamtools.yaml"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "docker://quay.io/annacprice/bwasamtools:1.10"
shell: """samtools flagstat {input.bam} -@ {threads} | grep 'paired in sequencing' | cut -f1 -d " " > {output.readnb}"""
if IS_FASTA :
rule reads_qty:
input: R1=lambda w:R1[w["sample"]]
output: IN+"/{sample}/{sample}_readsnb.txt"
shell: "echo $(( $(zgrep -c '>' {input.R1})*2 )) > {output}"
else :
rule reads_qty:
input: R1=lambda w:R1[w["sample"]]
output: IN+"/{sample}/{sample}_readsnb.txt"
shell: "echo $(($(zcat {input.R1} | wc -l)/2)) > {output}"
rule get_percent:
input: mapped=lambda w:[w.group+"/map/"+sample.split('/')[-1]+"_mapped_read.txt" for sample in GROUPS[w.group]],
total=lambda w:[IN+"/"+sample.split('/')[-1]+"/"+sample.split('/')[-1]+"_readsnb.txt" for sample in GROUPS[w.group]]
output: "{group}/profile/mapping_percent.tsv"
conda : CONDA_ENV + "/pythonenv.yaml"
singularity : "docker://quay.io/annacprice/pythonenv:3.9"
shell: """
for path in {input.mapped}
do
file=$(basename $path)
sample=${{file%_mapped_read.txt}}
tot_reads=$(cat {IN}/$sample/$sample"_readsnb.txt")
mapped_reads=$(cat $path)
echo $tot_reads $mapped_reads
echo -e $sample\t$(bc -l <<< "$mapped_reads / $tot_reads")>>{output}
done
"""
# ---- mags infos ----------------------
rule mag_coverage:
input: mags="{group}/binning/{binner}/{binner}_MAG_list.txt",
cluster="{group}/binning/{binner}/clustering_{binner}.csv",
cov="{group}/profile/coverage_contigs.tsv",
len="{group}/annotation/contigs.bed",
nb_nuc="{group}/profile/Normalisation.tsv"
output: mag_cov="{group}/profile/mag_{binner}_coverage.tsv",
mag_map="{group}/profile/mag_{binner}_percent_mapped.tsv"
conda : CONDA_ENV + "/pythonenv.yaml"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "docker://quay.io/annacprice/pythonenv:3.9"
shell:"""
{SCRIPTS}/mag_coverage.py -m {input.mags} -c {input.cluster} -t {input.cov} -l {input.len} -n {input.nb_nuc} -v {output.mag_cov} -p {output.mag_map}
"""
# ---------------------- look at duplicates ----------------------
# (PICARD)
rule picard_duplicate:
input: bam = "{group}/map/{sample}_mapped_sorted.bam",
# index = "{group}/map/{sample}_mapped_sorted.bam.bai",
output: bam = "{group}/map/{sample}_mapped_sorted_dup.bam",
stat = "{group}/map/{sample}_marked_dup_metrics.txt"
priority: 50
params: tmp = "{group}/tmp/{sample}"
resources:
slurm_partition = get_resource("partition", mult=1),
mem_mb = 60000,
# exclude= "e4096n[1-3],e512n[25-64]"
singularity : "docker://biocontainers/picard-tools:v2.18.25dfsg-2-deb_cv1"
shell: """
mkdir -p {params.tmp}/
export JAVA_OPTIONS="-Xmx{resources.mem_mb}m -Xms{resources.mem_mb}m -Djava.io.tmpdir={params.tmp}"
PicardCommandLine MarkDuplicates I={input.bam} O={output.bam} M={output.stat} SORTING_COLLECTION_SIZE_RATIO=0.10 TMP_DIR={params.tmp}/
"""
# samtools
rule samtools_check_duplicate:
input: bam = "{group}/map/{sample}_mapped_sorted.bam",
# index = "{group}/map/{sample}_mapped_sorted.bam.bai",
output: bam = "{group}/map/{sample}_mapped_sorted_dup_samtools.bam",
stat = "{group}/map/{sample}_marked_dup_metrics_samtools.txt"
priority: 50
resources:
slurm_partition = get_resource("partition", mult=10),
mem_mb = get_resource("mem", mult=10)
singularity : "docker://quay.io/annacprice/bwasamtools:1.10"
shell: """
samtools sort -n {input.bam} | samtools fixmate -m - - | samtools sort - | samtools markdup -r -f {output.stat} - {output.bam}
"""
# aggregate info on duplicates
rule check_duplicates:
input: lambda w : ["%s/map/%s_marked_dup_metrics.txt"%(w.group,sample.split('/')[-1]) for sample in GROUPS[w.group]]
output: out = "{group}/profile/percent_duplicate.tsv"
run:
sorted_samples = sorted([basename(el).split("_marked_dup_metrics.txt")[0] for el in input])
sample_stats = {}
for file in input:
with open(file) as handle:
sample = basename(file).split("_marked_dup_metrics.txt")[0]
for _ in handle:
if "LIBRARY" in _:
break
# catch small file with no estimated library size
splitline = next(handle).rstrip().split("\t")
if len(splitline)==9:
percent_dup, estimated_lib_size = splitline[-1],"0"
else:
# LIBRARY UNPAIRED_READS_EXAMINED READ_PAIRS_EXAMINED SECONDARY_OR_SUPPLEMENTARY_RDS UNMAPPED_READS UNPAIRED_READ_DUPLICATES READ_PAIR_DUPLICATES READ_PAIR_OPTICAL_DUPLICATES PERCENT_DUPLICATION ESTIMATED_LIBRARY_SIZE
library, unpaired_reads, read_paired, second_sup_reads,unmap_reads,unpaired_dup,paired_dup, pair_opt_dup, percent_dup, estimated_lib_size = splitline
sample_stats[sample] = [percent_dup, estimated_lib_size]
with open(output["out"],"w") as handle:
handle.write("Samples\t%s\n"%"\t".join(sorted_samples))
handle.write("Percent_duplicate\t%s\n"%"\t".join([sample_stats[sample][0] for sample in sorted_samples]))
handle.write("Estimated_lib_size\t%s\n"%"\t".join([sample_stats[sample][1] for sample in sorted_samples]))
# ---------------------- get cov hist and cov variation ----------------------
rule index:
input: "{path}.bam"
output: "{path}.bam.bai"
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "docker://quay.io/annacprice/bwasamtools:1.10"
shell: "samtools index {input}"
rule get_cov_stats:
input: bam = "{group}/map/{sample}_mapped_sorted.bam",
bai = "{group}/map/{sample}_mapped_sorted.bam.bai"
output: cov_stat = "{group}/map/{sample}_cov.stats",
cov_hists = "{group}/map/{sample}_cov.hists"
priority: 50
resources:
slurm_partition = get_resource("partition"),
mem_mb = get_resource("mem")
singularity : "%s/../singularity_container/pythonenv_pysam.sif"%SCRIPTS
shell: "{SCRIPTS}/CovEvaluate.py {input.bam} {output.cov_stat} {output.cov_hists} -l 10000"