-
Notifications
You must be signed in to change notification settings - Fork 17
/
base.py
175 lines (162 loc) · 5.54 KB
/
base.py
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
"""Balsamic init command."""
import json
import logging
import subprocess
import sys
from datetime import datetime
from pathlib import Path
from typing import Union, List, Optional
import click
from BALSAMIC.commands.options import (
OPTION_RUN_MODE,
OPTION_CLUSTER_PROFILE,
OPTION_CLUSTER_QOS,
OPTION_CLUSTER_ACCOUNT,
OPTION_SNAKEMAKE_OPT,
OPTION_GENOME_VERSION,
OPTION_FORCE_ALL,
OPTION_RUN_ANALYSIS,
OPTION_CLUSTER_MAIL,
OPTION_CLUSTER_MAIL_TYPE,
OPTION_QUIET,
OPTION_CACHE_VERSION,
OPTION_SNAKEFILE,
OPTION_OUT_DIR,
OPTION_CLUSTER_CONFIG,
OPTION_COSMIC_KEY,
)
from BALSAMIC.constants.analysis import BIOINFO_TOOL_ENV, RunMode
from BALSAMIC.constants.cache import GenomeVersion, REFERENCE_FILES
from BALSAMIC.constants.cluster import (
ClusterMailType,
QOS,
ClusterProfile,
ClusterConfigType,
)
from BALSAMIC.models.cache import CacheConfig, ReferencesHg, ReferencesCanFam
from BALSAMIC.models.snakemake import SnakemakeExecutable
from BALSAMIC.utils.analysis import get_cache_singularity_bind_paths
from BALSAMIC.utils.cache import get_containers
from BALSAMIC.utils.cli import get_snakefile, get_config_path
from BALSAMIC.utils.io import write_json, generate_workflow_graph
LOG = logging.getLogger(__name__)
@click.command(
"init", short_help="Download singularity containers and build the reference cache"
)
@OPTION_OUT_DIR
@OPTION_CACHE_VERSION
@OPTION_CLUSTER_ACCOUNT
@OPTION_CLUSTER_CONFIG
@OPTION_CLUSTER_MAIL
@OPTION_CLUSTER_MAIL_TYPE
@OPTION_CLUSTER_PROFILE
@OPTION_CLUSTER_QOS
@OPTION_COSMIC_KEY
@OPTION_FORCE_ALL
@OPTION_GENOME_VERSION
@OPTION_QUIET
@OPTION_RUN_ANALYSIS
@OPTION_RUN_MODE
@OPTION_SNAKEFILE
@OPTION_SNAKEMAKE_OPT
@click.pass_context
def initialize(
context: click.Context,
account: Optional[str],
cache_version: str,
cluster_config: Path,
cosmic_key: str,
force_all: bool,
genome_version: GenomeVersion,
mail_type: Optional[ClusterMailType],
mail_user: Optional[str],
out_dir: str,
profile: ClusterProfile,
qos: QOS,
quiet: bool,
run_analysis: bool,
run_mode: RunMode,
snakefile: Path,
snakemake_opt: List[str],
) -> None:
"""Validate inputs and download reference caches and containers."""
LOG.info(f"BALSAMIC started with log level {context.obj['log_level']}")
if run_mode == RunMode.CLUSTER and not run_analysis:
LOG.info("Changing run-mode to local on dry-run")
run_mode: RunMode = RunMode.LOCAL
if run_mode == RunMode.CLUSTER and not account:
LOG.error("A cluster account is required for cluster run mode")
raise click.Abort()
if genome_version in [GenomeVersion.HG19, GenomeVersion.HG38] and not cosmic_key:
LOG.error(
f"No COSMIC authentication key specified. It is required when using {genome_version} reference"
)
raise click.Abort()
out_dir: Path = Path(out_dir, cache_version).absolute()
references_dir: Path = Path(out_dir, genome_version)
genome_dir = Path(references_dir, "genome")
variants_dir = Path(references_dir, "variants")
vep_dir = Path(references_dir, "vep")
containers_dir: Path = Path(out_dir, "containers")
config_path: Path = Path(references_dir, "config.json")
log_dir: Path = Path(references_dir, "logs")
script_dir: Path = Path(references_dir, "scripts")
for dir_path in [references_dir, log_dir, script_dir]:
dir_path.mkdir(parents=True, exist_ok=True)
references: Union[ReferencesHg, ReferencesCanFam] = REFERENCE_FILES[genome_version]
cache_config: CacheConfig = CacheConfig(
analysis={"case_id": f"reference.{genome_version}.{cache_version}"},
references_dir=references_dir.as_posix(),
genome_dir=genome_dir.as_posix(),
variants_dir=variants_dir.as_posix(),
vep_dir=vep_dir.as_posix(),
containers_dir=containers_dir.as_posix(),
genome_version=genome_version,
cosmic_key=cosmic_key,
bioinfo_tools=BIOINFO_TOOL_ENV,
containers=get_containers(cache_version),
references=references,
references_date=datetime.now().strftime("%Y-%m-%d %H:%M"),
)
write_json(
json_obj=json.loads(cache_config.model_dump_json(exclude_none=True)),
path=config_path.as_posix(),
)
LOG.info(f"Reference workflow configured successfully ({config_path.as_posix()})")
snakefile: Path = (
snakefile if snakefile else get_snakefile("generate_ref", "balsamic")
)
generate_workflow_graph(
config_path=config_path,
directory_path=references_dir,
snakefile=snakefile,
title="reference",
)
LOG.info("Starting reference generation workflow...")
snakemake_executable: SnakemakeExecutable = SnakemakeExecutable(
account=account,
case_id=cache_config.analysis.case_id,
cluster_config_path=cluster_config
if cluster_config
else get_config_path(ClusterConfigType.CACHE),
config_path=config_path,
force=force_all,
log_dir=log_dir,
mail_type=mail_type,
mail_user=mail_user,
profile=profile,
qos=qos,
quiet=quiet,
result_dir=references_dir,
run_analysis=run_analysis,
run_mode=run_mode,
script_dir=script_dir,
snakemake_options=snakemake_opt,
singularity_bind_paths=get_cache_singularity_bind_paths(cache_config),
snakefile=snakefile,
working_dir=references_dir,
)
subprocess.run(
f"{sys.executable} -m {snakemake_executable.get_command()}",
shell=True,
)