Add trace_aggpy/src/trace_aggpy/cli.py
This commit is contained in:
parent
636b3cd76d
commit
14df35ca07
1 changed files with 56 additions and 0 deletions
56
trace_aggpy/src/trace_aggpy/cli.py
Normal file
56
trace_aggpy/src/trace_aggpy/cli.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from trace_aggpy.main import analyze_reads
|
||||
|
||||
|
||||
def _load_json(path: Path) -> Any:
|
||||
if not path.exists():
|
||||
raise FileNotFoundError(f"Input file does not exist: {path}")
|
||||
with path.open('r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
if not isinstance(data, list):
|
||||
raise ValueError("Expected a list of read events in JSON input.")
|
||||
return data
|
||||
|
||||
|
||||
def _write_json(path: Path, data: dict) -> None:
|
||||
with path.open('w', encoding='utf-8') as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2, sort_keys=True)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""CLI entry point: Reads JSON input, calls analyze_reads, writes summary output."""
|
||||
parser = argparse.ArgumentParser(description="Analyze N40 Read Events and output JSON summary.")
|
||||
parser.add_argument("--input", required=True, help="Pfad zur JSON-Input-Datei mit Read-Events.")
|
||||
parser.add_argument("--output", required=True, help="Pfad zur zu erzeugenden Summary-JSON.")
|
||||
args = parser.parse_args()
|
||||
|
||||
input_path = Path(args.input)
|
||||
output_path = Path(args.output)
|
||||
|
||||
read_data = _load_json(input_path)
|
||||
|
||||
if not read_data:
|
||||
raise ValueError("Input read data is empty.")
|
||||
|
||||
corr_id = read_data[0].get('corr_id') if isinstance(read_data[0], dict) else None
|
||||
if not corr_id or not isinstance(corr_id, str):
|
||||
raise ValueError("Missing or invalid 'corr_id' in input data.")
|
||||
|
||||
result = analyze_reads(corr_id=corr_id, read_data=read_data)
|
||||
|
||||
if hasattr(result, 'to_json') and callable(getattr(result, 'to_json')):
|
||||
output_data = result.to_json()
|
||||
elif isinstance(result, dict):
|
||||
output_data = result
|
||||
else:
|
||||
raise TypeError("Unexpected output type from analyze_reads. Expected dict or object with to_json().")
|
||||
|
||||
_write_json(output_path, output_data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in a new issue