forked from cmkobel/CompareM2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakefile
799 lines (555 loc) Β· 24.4 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# snakemake --snakefile ~/assemblycomparator2/snakefile --profile ~/assemblycomparator2/configs/slurm/ --cluster-config ~/assemblycomparator2/configs/cluster.yaml
__version__ = "v2.3.0"
__author__ = 'Oliver Kjærlund Hansen & Carl M. Kobel'
import os
from os import listdir
from os.path import isfile, join
#import yaml
import pandas as pd
import numpy as np
from shutil import copyfile
#import time
#import re
#from shutil import copyfile
#import re
cwd = os.getcwd()
batch_title = cwd.split("/")[-1]
print("/*")
print()
print(" ββββββ ββββββββββββββββ βββββββ βββββββ ββββ βββββββββββ ")
print(" ββββββββββββββββββββββββββββββββββββββββββββββ βββββββββββββ")
print(" βββββββββββββββββββββββββββ βββ ββββββββββββββ βββββββ")
print(" βββββββββββββββββββββββββββ βββ βββββββββββββββββββββ ")
print(" βββ βββββββββββββββββββββββββββββββββββββββ βββ βββββββββββ")
print(" βββ βββββββββββββββββββ βββββββ βββββββ βββ βββββββββββ")
print(" A.K.A. assemblycomparator2 ")
print(" Please log issues at: ")
print(" github.com/cmkobel/assemblycomparator2/issues ")
print()
print(f" batch_title: {batch_title}")
print(f" roary_blastp_identity: {config['roary_blastp_identity']} (default 95)")
print(f" mlst_scheme: {config['mlst_scheme']} (default automatic)")
print()
out_base_var = "output_asscom2"
base_variable = os.environ['ASSCOM2_BASE']
print('base_variable:', base_variable)
#reference = config["reference"]
# --- Read in relevant files in the current working directory ----------------
relative_wd = "."
#extension_whitelist = ["fna", "fa", "fas", "fasta", "seq"] # old
extension_whitelist = ["fna", "fa", "fas", "fasta", "seq", "gb", "fq", "gff", "gfa", "clw", "sth", "gz", "bz2"]
present_files = [f for f in listdir(relative_wd) if isfile(join(relative_wd,f))]
df = pd.DataFrame(data = {'input_file': present_files})
# Check that the directory is not empty.
if df.shape[0] == 0:
print("Error: No fasta files in the current directory. Quitting ...")
raise Exception("Zero genomic files present.")
df = df[~df["input_file"].str.startswith(".", na = False)] # Remove hidden files
df['sample_raw'] = [".".join(i.split(".")[:-1]) for i in df['input_file'].tolist()] # Extract everything before the extension dot.
df['sample'] = df['sample_raw'].str.replace(' ','_')
df['extension'] = [i.split(".")[-1] for i in df['input_file'].tolist()] # Extract extension
df['input_file_fasta'] = out_base_var + "/samples/" + df['sample'] + "/" + df['sample'] + ".fa" # This is where the input file is copied to in the first snakemake rule.
df = df[df['extension'].isin(extension_whitelist)] # Remove files with unsupported formats.
# Check that the directory is not empty, again.
if df.shape[0] == 0:
print("Error: No fasta files in the current directory. Quitting ...(2)")
raise Exception("Zero genomic files present.")
#df_mini = df_mini.apply(np.vectorize(lambda x: str(x).strip().replace(" ", ""))) # strip whitespace and replace spaces with underscores.
# --- Displaying filtered dataframe ready for analysis --------------
df = df.reset_index(drop = True)
#print(df[['input_file', 'sample', 'extension']])
print(df)
print("//")
print()
# --- Make sure the output directory exists. ---------------------------
try:
os.mkdir("output_asscom2")
except:
pass
# The modification time of this file tells the report subpipeline whether it needs to run. Thus, void_report is called in the end of every successful rule.
void_report = f"touch {out_base_var}/.asscom2_void_report.flag"
# --- Collect all targets. ------------------------------------------
rule all:
input: expand(["{out_base}/metadata.tsv", \
"{out_base}/.install_report_environment_aot.flag", \
"{out_base}/assembly-stats/assembly-stats.tsv", \
"{out_base}/collected_results/sequence_lengths.tsv", \
"{out_base}/collected_results/GC_summary.tsv", \
"{out_base}/collected_results/prokka_summarized.txt", \
"{out_base}/collected_results/kraken2_reports.tsv", \
"{out_base}/roary/summary_statistics.txt", \
"{out_base}/abricate/card_detailed.tsv", \
"{out_base}/mashtree/mashtree.newick", \
"{out_base}/mlst/mlst.tsv", \
"{out_base}/fasttree/fasttree.newick", \
"{out_base}/gtdbtk/gtdbtk.bac.summary.tsv", \
"{out_base}/snp-dists/snp-dists.tsv"], \
out_base = out_base_var, sample = df["sample"], batch_title = batch_title) # copy
# Dummy test
rule test:
output: ".test_done.flag"
shell: """
#sleep 5
touch {output}
{void_report}
"""
# Copy the input file to its new home
# Homogenizes the file extension as well (.fa)
rule copy:
#input: "{sample}"
input:
genome = lambda wildcards: df[df["sample"]==wildcards.sample]["input_file"].values[0],
output: "{out_base}/samples/{sample}/{sample}.fa"
#log: "logs/{out_base}_{wildcards.sample}.out.log"
container: "docker://pvstodghill/any2fasta"
conda: "conda_definitions/any2fasta.yaml"
resources:
runtime = "01:00:00"
shell: """
any2fasta "{input.genome}" > {output}
"""
# Write the df table to the directory for later reference.
# Why isn't this a run: instead of a shell: ?
rule metadata:
input: expand("{out_base}/samples/{sample}/{sample}.fa", out_base = out_base_var, sample = df["sample"]) # From rule copy
output: "{out_base}/metadata.tsv"
params: dataframe = df.to_csv(None, index_label = "index", sep = "\t")
resources:
runtime = "01:00:00"
#run:
#df.to_csv(str(output), index_label = "index", sep = "\t")
#os.system(f"cp ${{ASSCOM2_BASE}}/scripts/{report_template_file_basename} {out_base_var}")
shell: """
echo '''{params.dataframe}''' > {output}
{void_report}
"""
# Seems that checkm doesn't work on mac. pplacer does not exist, and I get errors:
# AttributeError: 'MarkerGeneFinder' object has no attribute '__reportProgress'
# AttributeError: 'MarkerGeneFinder' object has no attribute '__processBin'
# [2022-09-27 10:35:55] INFO: Saving HMM info to file.
# [2022-09-27 10:35:55] INFO: Calculating genome statistics for 3 bins with 1 threads:
# ...
# [2022-09-27 10:35:56] INFO: Extracting marker genes to align.
# [2022-09-27 10:35:56] ERROR: Models must be parsed before identifying HMM hits.
# I will have to consider if I will develop this on linux, or find an alternative.
# rules download_checkm and checkm have been disabled below
# rule download_checkm:
# output:
# flag = touch("{out_base}/.checkm_OK.flag")
# conda: "conda_definitions/wget.yaml"
# params:
# directory = base_variable + "/databases/checkm/"
# shell: """
#
# # Check if the database exists.
# # If it doesn't, download/untar the db
# if [ ! -f {params.directory}/checkm_OK.flag ]; then
#
# wget --directory-prefix={params.directory} https://data.ace.uq.edu.au/public/CheckM_databases/checkm_data_2015_01_16.tar.gz
# tar -xvf {params.directory}/checkm_data_2015_01_16.tar.gz -C {params.directory}
# touch {params.directory}/checkm_OK.flag
#
# fi
#
# """
#
#rule checkm:
# input: "{out_base}/.checkm_OK.flag"
# output: touch("{out_base}/checkm/output")
# conda: "conda_definitions/checkm.yaml"
# params:
# directory = base_variable + "/databases/checkm/"
# shell: """
#
# checkm data setRoot {params.directory}
#
#
#
# checkm lineage_wf /Users/kartoffel/assemblycomparator2/tests/E._faecium ./output
#
# """
# --- Targets for each sample below: --------------------------------
rule seqlen_individual:
input: "{out_base}/samples/{sample}/{sample}.fa"
output: "{out_base}/samples/{sample}/sequence_lengths/{sample}_seqlen.tsv"
container: "docker://cmkobel/bioawk"
conda: "conda_definitions/bioawk.yaml"
shell: """
bioawk -v sam={wildcards.sample} -c fastx '{{ print sam, $name, length($seq) }}' < {input} \
> {output}
"""
rule gc_summary_individual:
input: "{out_base}/samples/{sample}/{sample}.fa"
output: "{out_base}/samples/{sample}/statistics/{sample}_gc.tsv"
container: "docker://rocker/tidyverse" # remember to add devtools
conda: "conda_definitions/r-tidyverse.yaml" # like r-markdown, but much simpler.
params: base_variable = base_variable
shell: """
Rscript $ASSCOM2_BASE/scripts/tabseq_gc.r $ASSCOM2_BASE/scripts/tabseq_tiny.r {input} \
> {output} 2> {output}.fail || echo what
"""
rule prokka_individual:
input: "{out_base}/samples/{sample}/{sample}.fa"
output:
gff = "{out_base}/samples/{sample}/prokka/{sample}.gff",
log = "{out_base}/samples/{sample}/prokka/{sample}.log",
tsv = "{out_base}/samples/{sample}/prokka/{sample}.tsv",
summarized_txt = "{out_base}/samples/{sample}/prokka/{sample}_summary.txt",
labelled_tsv = "{out_base}/samples/{sample}/prokka/{sample}_labelled.tsv"
container: "docker://staphb/prokka"
conda: "conda_definitions/prokka.yaml"
benchmark: "{out_base}/benchmarks/benchmark.prokka_individual.{sample}.tsv"
#retries: 3 # too hacky
resources:
mem_mb = 8192
threads: 4
shell: """
minced --version
prokka \
--cpus {threads} \
--force \
--outdir {wildcards.out_base}/samples/{wildcards.sample}/prokka \
--prefix {wildcards.sample} {input} \
> tee {output.log}
cat {output.log} \
| grep "Found" \
| grep -E "tRNAs|rRNAs|CRISPRs|CDS|unique" \
| cut -d" " -f 3,4 \
| awk -v sam={wildcards.sample} '{{ print sam " " $0 }}' \
>> {output.summarized_txt} # jeg undrer mig over hvorfor den har to gt question mark
cat {output.tsv} \
| awk -v sam={wildcards.sample} '{{ print $0 "\t" sam }}' \
> {output.labelled_tsv}
"""
rule kraken2_individual:
input: "{out_base}/samples/{sample}/{sample}.fa"
output: "{out_base}/samples/{sample}/kraken2/{sample}_kraken2_report.tsv"
container: "docker://staphb/kraken2"
conda: "conda_definitions/kraken2.yaml"
threads: 4
resources:
mem_mb = 65536
benchmark: "{out_base}/benchmarks/benchmark.kraken2_individual.{sample}.tsv"
shell: """
if [ ! -z $ASSCOM2_KRAKEN2_DB ]; then
echo using kraken2 database $ASSCOM2_KRAKEN2_DB
# Run kraken2
kraken2 \
--threads 4 \
--db $ASSCOM2_KRAKEN2_DB \
--report {output}_tmp \
{input} \
> /dev/null
# Put sample names in front
cat {output}_tmp \
| awk -v sam={wildcards.sample} '{{ print sam "\t" $0 }}' \
> {output}
# Remove temp file
rm {output}_tmp
else
echo "The ASSCOM2_KRAKEN2_DB variable is not set, and thus the kraken2 rule and its jobs will not be run. Consider using the scripts/set_up_kraken2.sh script for downloading and linking the latest kraken2 database."
fi
"""
# --- Collect results among all samples -----------------------------
rule kraken2:
input: expand("{out_base}/samples/{sample}/kraken2/{sample}_kraken2_report.tsv", out_base = out_base_var, sample = df["sample"]),
output: "{out_base}/collected_results/kraken2_reports.tsv",
resources:
runtime = "01:00:00"
shell: """
# kraken2
echo -e "sample\tmatch_percent\tclade_mappings\tlevel_mappings\tlevel\ttaxonomic_id\tclade" \
> {output}
cat {input} >> {output}
{void_report}
"""
rule seqlen:
input: expand("{out_base}/samples/{sample}/sequence_lengths/{sample}_seqlen.tsv", out_base = out_base_var, sample = df["sample"])
output: "{out_base}/collected_results/sequence_lengths.tsv"
resources:
runtime = "01:00:00"
shell: """
# Sequence lengths
echo -e "sample\trecord\tlength" \
> {output}
cat {input} >> {output}
{void_report}
"""
rule gc_summary:
input: expand("{out_base}/samples/{sample}/statistics/{sample}_gc.tsv", out_base = out_base_var, sample = df["sample"])
output: "{out_base}/collected_results/GC_summary.tsv"
resources:
runtime = "01:00:00"
shell: """
# Sequence lengths
echo -e "sample\tpart\tlength\tGC" \
> {output}
cat {input} | grep -vE "^#" >> {output} # Append content without headers
{void_report}
"""
rule prokka:
input:
summarized_txt = expand("{out_base}/samples/{sample}/prokka/{sample}_summary.txt", out_base = out_base_var, sample = df["sample"]),
labelled_tsv = expand("{out_base}/samples/{sample}/prokka/{sample}_labelled.tsv", out_base = out_base_var, sample = df["sample"]),
output:
summarized_txt = "{out_base}/collected_results/prokka_summarized.txt",
labelled_tsv = "{out_base}/collected_results/prokka_labelled.tsv",
resources:
runtime = "01:00:00"
shell: """
# prokka
echo "sample value name" \
> {output.summarized_txt}
cat {input.summarized_txt} >> {output.summarized_txt}
cat {input.labelled_tsv} > {output.labelled_tsv}
{void_report}
"""
rule sample_pathway_enrichment_analysis:
input: "{out_base}/collected_results/prokka_labelled.tsv"
output: "{out_base}/collected_results/sample_pathway_enrichment_analysis.tsv"
conda: "conda_definitions/r-clusterProfiler.yaml"
shell: """
Rscript $ASSCOM2_BASE/scripts/sample_pathway_enrichment_analysis.R $ASSCOM2_BASE/assets/ko {input} \
> {output}
{void_report}
"""
# --- Targets for the complete set below: ---------------------------
def get_mem_roary(wildcards, attempt):
return [32000, 64000, 128000][attempt-1]
rule roary:
input:
metadata = "{out_base}/metadata.tsv",
gff = expand("{out_base}/samples/{sample}/prokka/{sample}.gff", sample = df["sample"], out_base = out_base_var)
output: ["{out_base}/roary/summary_statistics.txt", "{out_base}/roary/core_gene_alignment.aln", "{out_base}/roary/gene_presence_absence.csv", "{out_base}/roary/roary_done.flag"]
params:
blastp_identity = int(config['roary_blastp_identity']), # = 95 # For clustering genes
core_perc = 99 # Definition of the core genome
#conda: "envs/roary.yml"
threads: 16
#retries: 2
resources:
#mem_mb = 32768,
mem_mb = get_mem_roary,
runtime = "23:59:59" # Well, fuck me if this doesn't work on PBS
container: "docker://sangerpathogens/roary"
conda: "conda_definitions/roary.yaml"
shell: """
# Since I reinstalled conda, I've had problems with "Can't locate Bio/Roary/CommandLine/Roary.pm in INC". Below is a hacky fix
export PERL5LIB=$CONDA_PREFIX/lib/perl5/site_perl/5.22.0
# Silence parallel's citation pester:
echo "will cite" | parallel --citation > /dev/null 2> /dev/null
# Roary is confused by the way snakemake creates directories ahead of time.
# So I will delete it manually here before calling roary.
rm -r {wildcards.out_base}/roary
roary -a -r -e --mafft \
-p {threads} \
-i {params.blastp_identity} \
-cd {params.core_perc} \
-f {wildcards.out_base}/roary \
{input.gff} || echo roary failed
touch {output}
{void_report}
"""
rule snp_dists:
input:
metadata = "{out_base}/metadata.tsv",
aln = "{out_base}/roary/core_gene_alignment.aln"
output: "{out_base}/snp-dists/snp-dists.tsv"
conda: "conda_definitions/snp-dists.yaml"
container: "docker://staphb/snp-dists"
shell: """
snp-dists {input.aln} > {output}
{void_report}
"""
rule assembly_stats:
input:
metadata = "{out_base}/metadata.tsv",
fasta = df["input_file_fasta"].tolist()
output: "{out_base}/assembly-stats/assembly-stats.tsv"
container: "docker://sangerpathogens/assembly-stats"
conda: "conda_definitions/assembly-stats.yaml"
shell: """
assembly-stats -t {input.fasta} > {output}
{void_report}
"""
def get_mem_gtdbtk(wildcards, attempt):
return [150000, 300000, 400000, 500000][attempt-1]
rule gtdbtk:
input:
metadata = "{out_base}/metadata.tsv",
fasta = df["input_file_fasta"].tolist()
output: "{out_base}/gtdbtk/gtdbtk.bac.summary.tsv"
params:
batchfile_content = df[['input_file_fasta', 'sample']].to_csv(header = False, index = False, sep = "\t"),
out_dir = "{out_base}/gtdbtk/"
threads: 8
#retries: 3
resources:
#mem_mb = 150000 # Last time I remember, it used 130000
mem_mb = get_mem_gtdbtk
conda: "conda_definitions/gtdbtk.yaml"
benchmark: "{out_base}/benchmarks/benchmark.gtdbtk.tsv"
shell: """
echo "GTDBTK_DATA_PATH is $GTDBTK_DATA_PATH"
# Create batchfile
echo '''{params.batchfile_content}''' > {wildcards.out_base}/gtdbtk/batchfile.tsv
gtdbtk classify_wf -h
gtdbtk classify_wf \
--batchfile {wildcards.out_base}/gtdbtk/batchfile.tsv \
--out_dir {params.out_dir} \
--cpus {threads} \
--keep_intermediates \
--force
# Homogenize database version number
cp {wildcards.out_base}/gtdbtk/gtdbtk.bac120.summary.tsv {output}
{void_report}
"""
rule abricate:
input:
metadata = "{out_base}/metadata.tsv",
fasta = df["input_file_fasta"].tolist()
output:
card_detailed = "{out_base}/abricate/card_detailed.tsv",
card_sum = "{out_base}/abricate/card_summarized.tsv",
plasmidfinder_detailed = "{out_base}/abricate/plasmidfinder_detailed.tsv",
plasmidfinder_sum = "{out_base}/abricate/plasmidfinder_summarized.tsv",
ncbi_detailed = "{out_base}/abricate/ncbi_detailed.tsv",
ncbi_sum = "{out_base}/abricate/ncbi_summarized.tsv",
vfdb_detailed = "{out_base}/abricate/vfdb_detailed.tsv",
vfdb_sum = "{out_base}/abricate/vfdb_summarized.tsv"
container: "docker://staphb/abricate"
conda: "conda_definitions/abricate.yaml"
shell: """
# TODO: update these databases
abricate --db ncbi {input.fasta} > {output.ncbi_detailed}
abricate --summary {output.ncbi_detailed} > {output.ncbi_sum}
abricate --db card {input.fasta} > {output.card_detailed}
abricate --summary {output.card_detailed} > {output.card_sum}
abricate --db plasmidfinder {input.fasta} > {output.plasmidfinder_detailed}
abricate --summary {output.plasmidfinder_detailed} > {output.plasmidfinder_sum}
abricate --db vfdb {input.fasta} > {output.vfdb_detailed}
abricate --summary {output.vfdb_detailed} > {output.vfdb_sum}
{void_report}
"""
# Parse the mlst scheme for bash
if config["mlst_scheme"] == "automatic":
mlst_scheme_interpreted = ""
else:
mlst_scheme_interpreted = f"--scheme {config['mlst_scheme']}"
#print(f"Info: The mlst_scheme is set to <{mlst_scheme_interpreted}>") # Debug message.
rule mlst:
input:
metadata = "{out_base}/metadata.tsv",
fasta = df["input_file_fasta"].tolist()
output: "{out_base}/mlst/mlst.tsv",
params:
mlst_scheme_interpreted = mlst_scheme_interpreted,
list_ = "{out_base}/mlst/mlst_schemes.txt"
container: "docker://staphb/mlst"
conda: "conda_definitions/mlst.yaml"
shell: """
mlst {params.mlst_scheme_interpreted} {input.fasta} > {output}
mlst --list > {params.list_}
{void_report}
"""
rule mashtree:
input:
metadata = "{out_base}/metadata.tsv",
fasta = df["input_file_fasta"].tolist()
output:
tree = "{out_base}/mashtree/mashtree.newick",
dist = "{out_base}/mashtree/mash_dist.tsv"
container: "docker://staphb/mashtree"
conda: "conda_definitions/mashtree.yaml"
threads: 4
resources:
mem_mb = 16000
shell: """
mashtree \
--numcpus {threads} \
--outmatrix {output.dist} \
{input.fasta} > {output.tree}
{void_report}
"""
# TODO:
#rule mash_screen:
def get_mem_fasttree(wildcards, attempt):
return [8000, 64000][attempt-1]
rule fasttree:
input:
metadata = "{out_base}/metadata.tsv",
fasta = "{out_base}/roary/core_gene_alignment.aln"
output: "{out_base}/fasttree/fasttree.newick"
container: "docker://staphb/fasttree"
conda: "conda_definitions/fasttree.yaml"
threads: 4
#retries: 1
resources:
mem_mb = get_mem_fasttree,
runntime = "23:59:59"
shell: """
OMP_NUM_THREADS={threads}
FastTree -nt -gtr {input.fasta} > {output} 2> {output}.log || echo "fasttree failed"
touch {output}
{void_report}
"""
rule fetch_report_template:
output: "{out_base}/rmarkdown_template.rmd"
shell: """
cp $ASSCOM2_BASE/scripts/genomes_to_report_v2.Rmd {output}
{void_report}
"""
# rule report:
# input:
# roary = "{out_base}/roary/roary_done.flag", # fasttree depends on roary, so the roary dependency is not necessary.
# fasttree = "{out_base}/fasttree/fasttree.newick",
# snp_dists = "{out_base}/snp-dists/snp-dists.tsv",
# rmarkdown_template = "{out_base}/rmarkdown_template.rmd"
# #output: "{out_base}/report.html"
# output: "{out_base}/report_{batch_title}.html"
# params:
# #markdown_template_rmd = "rmarkdown_template.rmd", # "genomes_to_report_v2.Rmd"
# markdown_template_html = "genomes_to_report_v2.html"
# container: "docker://cmkobel/assemblycomparator2_report"
# conda: "conda_definitions/r-markdown.yaml"
# shell: """
# cd {wildcards.out_base}
# Rscript -e 'library(rmarkdown); rmarkdown::render("rmarkdown_template.rmd", "html_document")'
# rm rmarkdown_template.rmd
# mv rmarkdown_template.html ../{output}
# """
# This rule might seem silly, but it makes sure that the report environment is ready to rock when the report subpipeline eventually is run: This has two pros:
# 1) The vastly faster mamba configuration in the asscom2 pipeline is used
# 2) The conda/mamba debugging is taken care of, without having to wait for jobs to finish on fresh installations.
# Since all snakemake conda environments are installed in $SNAKEMAKE_CONDA_PREFIX set to ${ASSCOM2_BASE}/conda_base, reuse is guaranteed.
rule install_report_environment_aot:
output: touch("{out_base}/.install_report_environment_aot.flag")
conda: "report_subpipeline/conda_definitions/r-markdown.yaml"
shell: """
echo OK
"""
# Just a dummy rule if you wanna force the report
# assemblycomparator2 --until report
# TODO: Should never run on the queue system
rule report:
resources:
runtime = "00:01:00"
shell: """
{void_report}
"""
# Call the report subpipeline
report_call = f"""
mkdir -p output_asscom2/logs; \
snakemake \
--snakefile $ASSCOM2_BASE/report_subpipeline/snakefile \
--cores 4 \
--use-conda \
--config out_base=$(pwd)/output_asscom2 base_variable={base_variable} batch_title={batch_title} 2> output_asscom2/logs/report.err.log
"""
onsuccess:
print("onsuccess: calling report subpipeline ...")
shell(report_call)
onerror:
print("onerror: calling report subpipeline ...")
shell(report_call)
print("*/")