diff options
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/genotypes/preprocess_csv_collect_info.py | 53 | ||||
| -rw-r--r-- | scripts/genotypes/types.py | 17 |
2 files changed, 46 insertions, 24 deletions
diff --git a/scripts/genotypes/preprocess_csv_collect_info.py b/scripts/genotypes/preprocess_csv_collect_info.py index 32ef1b2..9813f34 100644 --- a/scripts/genotypes/preprocess_csv_collect_info.py +++ b/scripts/genotypes/preprocess_csv_collect_info.py @@ -3,7 +3,7 @@ import sys import logging from pathlib import Path from dataclasses import asdict -from typing import Union, Iterator +from typing import Union, Iterator, Optional from argparse import Namespace, ArgumentParser from gn_libs.cli import SysExit @@ -19,8 +19,9 @@ from scripts.cli.validators import file_exists from .types import (FileStats, LineError, - LineDetails, - FieldsIdentity) + FieldsIdentity, + TransposedLineDetails, + UntransposedLineDetails) logger = logging.getLogger(__name__) @@ -65,7 +66,10 @@ def identify_columns( return FieldsIdentity(**_id, samples=_samples) -def process_transposed(line: tuple[str, ...], na_strings: tuple[str, ...]) -> LineDetails: +def process_transposed( + line: tuple[str, ...], + na_strings: tuple[str, ...] +) -> TransposedLineDetails: """Process a line in a transposed file.""" raise NotImplementedError("Please implement this!") @@ -74,8 +78,9 @@ def process_untransposed( line: tuple[str, ...], settings: dict, headers: tuple[str, ...] -) -> LineDetails: +) -> UntransposedLineDetails: """Processed line in a non-transposed file.""" + _marker: Optional[str] = None _errors: tuple[LineError, ...] = tuple() _symbols = tuple(_symb for _head, _symb in zip(headers, line) if _head not in ( @@ -86,6 +91,7 @@ def process_untransposed( try: _idx = headers.index(settings["markers"]) + _marker = line[_idx] except ValueError as _verr: _idx = 0 @@ -99,11 +105,12 @@ def process_untransposed( line=line ),) - return LineDetails( + return UntransposedLineDetails( line_content=line, line_header=line[0], geno_symbols=tuple(set(_symbols)), - errors=_errors) + errors=_errors, + marker=_marker) def validate_headers( @@ -164,6 +171,7 @@ def file_statistics(filepath: Path, fileconfigs: dict) -> FileStats: "comment_lines": 0, "dot_geno_lines": 0 } + _markers: tuple[str, ...] = tuple() genosymbols: set[str] = set() # NOTES: Maybe do the .geno file custom fields check in a separate script? @@ -174,28 +182,31 @@ def file_statistics(filepath: Path, fileconfigs: dict) -> FileStats: if line == headers: continue# skip headers line - line_dets = ( - process_transposed( - line, - na_strings=fileconfigs["na_strings"]) - if fileconfigs["transposed"] - else process_untransposed( + if fileconfigs["transposed"]: + t_line_dets = process_transposed( + line, + na_strings=fileconfigs["na_strings"]) + headers = headers + (t_line_dets.line_header,) + genosymbols.update(t_line_dets.geno_symbols) + _errors = _errors + tuple(t_line_dets.errors) + if t_line_dets.is_markers_line: + _markers = tuple(t_line_dets.line_content[1:]) + else: + line_dets = process_untransposed( line, fileconfigs, - headers=headers)) - _errors = _errors + tuple(line_dets.errors) - - if fileconfigs["transposed"]: - headers = headers + (line_dets.line_header,) - - genosymbols.update(line_dets.geno_symbols) - # TODO: do more processing + headers=headers) + genosymbols.update(line_dets.geno_symbols) + _errors = _errors + tuple(line_dets.errors) + if bool(line_dets.marker): + _markers = _markers + (str(line_dets.marker),) continue # end of line processing return FileStats( **stats, geno_symbols=tuple(genosymbols), samples_list=col_ids.samples, + markers_list=_markers, errors=_errors) diff --git a/scripts/genotypes/types.py b/scripts/genotypes/types.py index 472bb1b..077e90e 100644 --- a/scripts/genotypes/types.py +++ b/scripts/genotypes/types.py @@ -1,6 +1,6 @@ """The datatypes this package will use.""" +from typing import Sequence, Optional from dataclasses import field, dataclass -from typing import Union, Sequence, Optional @dataclass @@ -34,12 +34,23 @@ class LineError: @dataclass(frozen=True) -class LineDetails: +class TransposedLineDetails: """Data type for processed line details""" - line_content: Sequence[Union[str, None]] + line_content: Sequence[str] geno_symbols: Sequence[str] line_header: str errors: Sequence[LineError] + is_markers_line: bool = False + + +@dataclass(frozen=True) +class UntransposedLineDetails: + """Data type for processed line details""" + line_content: Sequence[str] + geno_symbols: Sequence[str] + line_header: str + errors: Sequence[LineError] + marker: Optional[str] = None @dataclass(frozen=True) |
