about summary refs log tree commit diff
path: root/uploader/scripts/genotypes/qc.py
blob: 0255901a64566df51a4d9318290d03164f675dc7 (plain)
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
"""Normalise a genotype file and run QC for a genotype-file-qc job."""
import sys
import json
import logging
from pathlib import Path
from argparse import ArgumentParser

from gn_libs import jobs, sqlite3

from uploader.genotypes import normalise, qc

logger = logging.getLogger(__name__)


def parse_args():
    """Parse the command-line arguments."""
    parser = ArgumentParser(
        prog="genotype-file-qc",
        description="Normalise a genotype file and run QC on it.")
    parser.add_argument("jobs_db", help="Path to the jobs SQLite database.")
    parser.add_argument(
        "qc_job_id", help="The id of the genotype-file-qc job.")
    parser.add_argument(
        "--log-level", default="info",
        choices=("debug", "info", "warning", "error", "critical"))
    return parser.parse_args()


def main():
    """Normalise the genotype file, then run QC on it."""
    args = parse_args()
    logging.basicConfig(level=getattr(logging, args.log_level.upper()))
    with sqlite3.connection(args.jobs_db) as conn:
        job = jobs.job(conn, args.qc_job_id, fulldetails=True)
        metadata = job["metadata"]
        config = json.loads(metadata["config"])
        stage02 = json.loads(metadata.get("stage02") or "{}")
        try:
            work_file = normalise.normalise_file(
                Path(metadata["file"]), config)
        except normalise.NormalisationError as _exc:
            print(json.dumps({"errors": [{"message": str(_exc)}]}))
            return 1
        result = qc.check_file(
            work_file, {**config, "transposed": False}, stage02)
    print(json.dumps(result))
    return 0 if not result["errors"] else 1


if __name__ == "__main__":
    sys.exit(main())