about summary refs log tree commit diff
path: root/uploader/genotypes/collector_config.py
blob: 776dcc8735ef77d0fa72e3c976a2fe8132f06a59 (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
"""Build the genotype statistics collector configuration from the upload form."""
import sys
from pathlib import Path
from typing import Any, Mapping

from r_qtl.utils import parse_missing_values_symbols


def from_form(form: Mapping[str, str]) -> dict:
    """Build the statistics collector's config from the upload form.

    The upload form uses its own field names for the metadata; the collector
    expects a different set of config keys. This maps between them, falling
    back to the collector's defaults when a field is absent.
    """
    return {
        "markers": form.get("markers_field", "Locus"),
        "chromosome": form.get("chromosomes_field", "Chr"),
        "cm": form.get("centimorgans_field", "cM"),
        "mb": form.get("megabases_field", "Mb"),
        "separator": form.get("file-separator", ","),
        "comment.char": form.get("file-comment-char", "#"),
        "na_strings": parse_missing_values_symbols(
            form.get("file-na", "- NA N/A")),
        "transposed": (form.get("transposed") or "off") == "on",
    }


def command(
        filepath: Path,
        config: Mapping[str, Any],
        loglevel: str = "info"
) -> list[str]:
    """Build the CLI command that collects statistics for ``filepath``."""
    _command = [
        sys.executable,
        "-m",
        "uploader.scripts.genotypes.preprocess_csv_collect_info",
        "--log-level", loglevel,
        "--separator", str(config["separator"]),
        "--comment_char", str(config["comment.char"]),
        "--markers_field", str(config["markers"]),
        "--chr_field", str(config["chromosome"]),
        "--cm_field", str(config["cm"]),
        "--mb_field", str(config["mb"]),
    ]
    if bool(config["transposed"]):
        _command = _command + ["--is-transposed"]
    for na_string in config["na_strings"]:
        _command = _command + ["--na_strings", str(na_string)]
    return _command + [str(filepath)]