-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
163 lines (132 loc) · 6.21 KB
/
Copy pathcli.py
File metadata and controls
163 lines (132 loc) · 6.21 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
"""Command line interface for dm-bip."""
import logging
from pathlib import Path
from typing import Annotated, Optional
import typer
from dm_bip import __version__
__all__ = [
"app",
"main",
]
logger = logging.getLogger(__name__)
app = typer.Typer(help="CLI for dm-bip.")
def version_callback(value: bool):
"""Print version and exit."""
if value:
typer.echo(f"dm-bip {__version__}")
raise typer.Exit()
@app.callback()
def main(
verbose: Annotated[int, typer.Option("-v", "--verbose", count=True, help="Increase verbosity")] = 0,
quiet: Annotated[Optional[bool], typer.Option("-q", "--quiet", help="Suppress output")] = None,
version: Annotated[
Optional[bool], typer.Option("--version", callback=version_callback, is_eager=True, help="Show version")
] = None,
):
"""CLI for dm-bip."""
if verbose >= 2:
logger.setLevel(level=logging.DEBUG)
elif verbose == 1:
logger.setLevel(level=logging.INFO)
else:
logger.setLevel(level=logging.WARNING)
if quiet:
logger.setLevel(level=logging.ERROR)
@app.command()
def run():
"""Display usage information for the dm-bip pipeline."""
typer.echo("The dm-bip pipeline is run using make.")
typer.echo("Run 'make help' to see available targets and usage information.")
@app.command()
def generate_trans_specs(
input_csv: Annotated[Path, typer.Option("--input", "-i", help="Path to the metadata CSV")],
output_dir: Annotated[Path, typer.Option("--output", "-o", help="Directory for YAML output files")],
cohort: Annotated[str, typer.Option("--cohort", "-c", help="Cohort to filter on (e.g. aric, jhs, whi)")],
entity: Annotated[str, typer.Option("--entity", "-e", help="Entity type to filter on")] = "MeasurementObservation",
template: Annotated[str, typer.Option("--template", "-t", help="Jinja2 template filename")] = "yaml_measobs.j2",
):
"""Generate trans-spec YAML files from a metadata CSV."""
from dm_bip.trans_spec_gen.generate_trans_specs import generate_yaml
results = generate_yaml(
input_csv=input_csv,
output_dir=output_dir,
entity=entity,
cohort=cohort,
template_name=template,
)
if results:
typer.echo(f"Generated {len(results)} YAML files in {output_dir}")
for path in results:
typer.echo(f" {path}")
else:
typer.echo(f"No matching rows for entity={entity}, cohort={cohort}")
raise typer.Exit(code=1)
@app.command()
def prepare_metadata(
raw_files: Annotated[list[Path], typer.Option("--raw", "-r", help="Raw metadata Excel file(s)")],
bdchv_defs: Annotated[Path, typer.Option("--bdchv-defs", help="Path to bdchv_defs.csv")],
contextual_vars: Annotated[Path, typer.Option("--contextual-vars", help="Path to contextual_variables_key.csv")],
unit_key: Annotated[Path, typer.Option("--unit-key", help="Path to unit_key.xlsx")],
output: Annotated[Path, typer.Option("--output", "-o", help="Output CSV path")],
fixes: Annotated[Optional[Path], typer.Option("--fixes", "-f", help="Curator fixes CSV")] = None,
):
"""Prepare metadata for trans-spec generation from raw dbGaP exports."""
from dm_bip.trans_spec_gen.prepare_metadata import prepare_metadata as _prepare
result = _prepare(
raw_files=raw_files,
bdchv_defs_path=bdchv_defs,
contextual_vars_path=contextual_vars,
unit_key_path=unit_key,
output_path=output,
fixes_file=fixes,
)
if result is None:
typer.echo("No data loaded from raw files")
raise typer.Exit(code=1)
typer.echo(f"Output written to {result}")
@app.command()
def fetch_digests(
cohort_key: Annotated[
Optional[str],
typer.Argument(help="Cohort key (e.g. jhs, aric). Omit with --list to list cohorts."),
] = None,
cache_dir: Annotated[Path, typer.Option("--cache-dir", help="Local cache directory")] = Path(".dbgap-cache"),
refresh: Annotated[bool, typer.Option("--refresh", help="Force re-fetch of cached files")] = False,
list_cohorts: Annotated[bool, typer.Option("--list", help="List available cohorts and exit")] = False,
):
"""Fetch dbGaP variable digest files (data_dict.xml, var_report.xml) for a cohort."""
from dm_bip.prepare_study.fetch_digests import fetch_digests as _fetch
from dm_bip.prepare_study.fetch_digests import load_cohorts
cohorts = load_cohorts(cache_dir=cache_dir, refresh=refresh)
if list_cohorts:
for key, cohort in sorted(cohorts.items()):
typer.echo(f" {key:<12} {cohort.study_id}.{cohort.data_version} {cohort.display_name}")
return
if cohort_key is None:
typer.echo("Error: cohort_key is required (use --list to see options)")
raise typer.Exit(code=2)
if cohort_key not in cohorts:
typer.echo(f"Unknown cohort '{cohort_key}'. Available: {', '.join(sorted(cohorts))}")
raise typer.Exit(code=2)
result = _fetch(cohorts[cohort_key], cache_root=cache_dir, refresh=refresh)
typer.echo(
f"Cached {len(result.data_dicts)} data_dict.xml + {len(result.var_reports)} var_report.xml "
f"under {result.cache_root}"
)
@app.command()
def parse_digests(
cohort_key: Annotated[str, typer.Argument(help="Cohort key (e.g. jhs, aric)")],
cache_dir: Annotated[Path, typer.Option("--cache-dir", help="dbGaP digest cache (input)")] = Path(".dbgap-cache"),
output_dir: Annotated[Path, typer.Option("--output-dir", "-o", help="Output root directory")] = Path("output"),
refresh_cohorts: Annotated[bool, typer.Option("--refresh-cohorts", help="Re-fetch cohorts.yaml")] = False,
):
"""Convert cached dbGaP digests for a cohort into schema-automator canonical-DD TSVs."""
from dm_bip.prepare_study.fetch_digests import load_cohorts, parse_cached_digests
cohorts = load_cohorts(cache_dir=cache_dir, refresh=refresh_cohorts)
if cohort_key not in cohorts:
typer.echo(f"Unknown cohort '{cohort_key}'. Available: {', '.join(sorted(cohorts))}")
raise typer.Exit(code=2)
written = parse_cached_digests(cohorts[cohort_key], cache_root=cache_dir, output_root=output_dir)
typer.echo(f"Wrote {len(written)} data dictionaries under {output_dir / cohort_key / 'dd'}")
if __name__ == "__main__":
app()