Repository navigation
Expand file tree
/
Copy pathSnakefile
More file actions
executable file
·187 lines (150 loc) · 7.17 KB
/
Copy pathSnakefile
File metadata and controls
executable file
·187 lines (150 loc) · 7.17 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
# -*- coding: utf-8
"""Snakemake file for the metagenomics worklfow.
See the online documentation for the jargon, and
how to use it.
"""
import os
import anvio
import argparse
import pandas as pd
import anvio.utils as u
import anvio.workflows as w
import anvio.dbops as dbops
import anvio.terminal as terminal
import anvio.filesnpaths as filesnpaths
from anvio.errors import ConfigError, FilesNPathsError
from anvio.workflows.metagenomics import MetagenomicsWorkflow
from anvio.tables.miscdata import TableForLayerAdditionalData
__copyright__ = "Copyleft 2015-2024, The Anvi'o Project (http://anvio.org/)"
__credits__ = []
__license__ = "GPL 3.0"
__version__ = anvio.__version__
__maintainer__ = "Alon Shaiber"
__email__ = "alon.shaiber@gmail.com"
run = terminal.Run()
progress = terminal.Progress()
this_workflow_is_inherited_by_another = (
False
if "anvio/workflows/metagenomics" in str(workflow.included[0].abspath())
else True
)
if not this_workflow_is_inherited_by_another:
# it is important that this comes before the include statement since
# M is defined here and will also be used in the contigs workflow
M = MetagenomicsWorkflow(argparse.Namespace(config=config))
M.init()
dirs_dict = M.dirs_dict
# in order to generate the contigs databases we include the snakefile
# for the generation of contigs databases
include: w.get_workflow_snake_file_path("contigs")
def rule_log(rule_name, log_file_name="log"):
"""Return a canonical workflow log path under 00_LOGS/<rule_name>/."""
if not log_file_name.endswith(".log"):
log_file_name = f"{log_file_name}.log"
log_path = os.path.join(dirs_dict["LOGS_DIR"], rule_name, log_file_name)
# Ensure the per-rule log subdirectory exists. This is essential for cluster
# submission (e.g. via clusterize), where the submit command opens the log file
# at submission time and fails if its parent directory does not already exist.
# Doing it here guarantees the directory exists for *every* rule that declares a
# log, regardless of whether the rule is registered in `self.rules`.
os.makedirs(os.path.dirname(log_path), exist_ok=True)
return log_path
# SR/LR assembler availability from the workflow class
available_sr, available_lr = M.get_assembly_software_list()
# Which assemblers are enabled in the config?
sr_enabled = [
a for a in available_sr if M.get_param_value_from_config([a, "run"]) is True
]
lr_enabled = [
a for a in available_lr if M.get_param_value_from_config([a, "run"]) is True
]
sr_count = len(sr_enabled)
lr_count = len(lr_enabled)
# Enforce assembler selection by read type (SR vs LR)
if not M.references_mode:
# If SR data exists, require exactly one SR assembler
if M.has_sr and sr_count == 0:
raise WorkflowError(
f"You didn't specify any assembler for your short-reads metagenomes and yet "
f"you config file shows that you are not using references_mode. If you already "
f"have your assemblies or reference fasta files, then maybe you need to add "
f'\'"references_mode": true, and a \'"fasta_txt: "name-of-your-fasta-txt-file" '
f"to your config file. Otherwise, if you do mean to use assembly, please set "
f"'run: true' for one of the following available short-read assembler: {available_sr})."
)
if sr_count > 1:
raise WorkflowError(
f"Multiple short-reads assemblers are enabled; enable only one of: {available_sr}."
)
# If LR data exists, require exactly one LR assembler
if M.has_lr and lr_count == 0:
raise WorkflowError(
f"You didn't specify any assembler for your long-reads metagenomes and yet "
f"you config file shows that you are not using references_mode. If you already "
f"have your assemblies or reference fasta files, then maybe you need to add "
f'\'"references_mode": true, and a \'"fasta_txt: "name-of-your-fasta-txt-file" '
f"to your config file. Otherwise, if you do mean to use assembly, please set "
f"'run: true' for one of the following available long-read assembler: {available_lr})."
)
if lr_count > 1:
raise WorkflowError(
f"Multiple long-read assemblers are enabled; enable only one of: {available_lr}."
)
# if an assembler is enable, but there is no matching reads:
if not M.has_sr and sr_count == 1:
raise WorkflowError(
"A short-read assembler is enabled but there are no short-reads in your samples-txt"
)
if not M.has_lr and lr_count == 1:
raise WorkflowError(
"A long-read assembler is enabled but there are no long-reads in your samples-txt"
)
# by default fastq files will be zipped after qc is done
run_gzip_fastqs = M.get_param_value_from_config(["gzip_fastqs", "run"]) == True
run_import_percent_of_reads_mapped = (
M.get_param_value_from_config(["import_percent_of_reads_mapped", "run"]) == True
)
# convenience sets:
ASSEMBLIES = M.get_assembly_ids()
SR_READSETS = M.get_sr_readset_ids()
LR_READSETS = M.get_lr_readset_ids()
SR_ASSEMBLIES = [
a for a in ASSEMBLIES if getattr(M, "assembly_types", {}).get(a) == "SR"
]
LR_ASSEMBLIES = [
a for a in ASSEMBLIES if getattr(M, "assembly_types", {}).get(a) == "LR"
]
# will be used as contrained readset's wildcards for either bowtie or minimap2
SR_RS_RE = w.regex_from_ids(SR_READSETS)
LR_RS_RE = w.regex_from_ids(LR_READSETS)
# same idea but for assemblies:
SR_GRP_RE = w.regex_from_ids(SR_ASSEMBLIES)
LR_GRP_RE = w.regex_from_ids(LR_ASSEMBLIES)
ruleorder: import_percent_of_reads_mapped > import_krakenuniq_taxonomy
rule metagenomics_workflow_target_rule:
"""
The target rule for the workflow.
The final product of the workflow is an anvi'o merged profile directory
for each group
"""
input:
targets=M.get_metagenomics_target_files(),
# Metagenomics workflow rule modules. Keep this order: later modules rely on
# helper functions and wildcard constraints defined above or in earlier modules.
if M.sra_mode:
# only when the samples-txt asks for reads to be downloaded, so that users who bring their
# own FASTQ files never need the SRA toolkit installed
include: w.get_workflow_rule_file_path("sra_reads", "download.smk")
include: w.get_workflow_rule_file_path("metagenomics", "qc.smk")
include: w.get_workflow_rule_file_path("metagenomics", "read_filtering.smk")
include: w.get_workflow_rule_file_path("metagenomics", "assembly_short_reads.smk")
include: w.get_workflow_rule_file_path("metagenomics", "assembly_long_reads.smk")
include: w.get_workflow_rule_file_path("read_recruitment")
include: w.get_workflow_rule_file_path("metagenomics", "profile_postprocessing.smk")
include: w.get_workflow_rule_file_path("metagenomics", "taxonomy.smk")
include: w.get_workflow_rule_file_path("metagenomics", "binning.smk")
if "anvio/workflows/metagenomics" in str(workflow.included[0].abspath()):
# check if all program dependencies are met. for this line to be effective,
# there should be an initial dry run step (which is the default behavior of
# the `WorkflowSuperClass`, so you are most likely covered).
M.check_workflow_program_dependencies(workflow, dont_raise=True)