From 7a2bcc9e86bde0eb9c0d370f83df4684e5522f26 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 2 Jan 2024 08:49:25 +0300 Subject: Cleanup: Fix linting and typing errors and update docs. --- r_qtl/errors.py | 1 - r_qtl/r_qtl2.py | 45 +++++++++++++++++++++------------------------ 2 files changed, 21 insertions(+), 25 deletions(-) (limited to 'r_qtl') diff --git a/r_qtl/errors.py b/r_qtl/errors.py index 648611e..20c5ced 100644 --- a/r_qtl/errors.py +++ b/r_qtl/errors.py @@ -1,5 +1,4 @@ """R/qtl and R/qtl2 error types.""" -from collections import namedtuple class RQTLError(Exception): """Base class for R/qtl and R/qtl2 errors.""" diff --git a/r_qtl/r_qtl2.py b/r_qtl/r_qtl2.py index ec7a954..22cf62c 100644 --- a/r_qtl/r_qtl2.py +++ b/r_qtl/r_qtl2.py @@ -2,16 +2,16 @@ import io import csv import json -import yaml -from pathlib import Path +from zipfile import ZipFile from functools import reduce -from zipfile import ZipFile, ZipInfo, is_zipfile -from typing import Any, List, Union, Iterator, Iterable +from typing import Iterator, Iterable, Callable -from r_qtl.errors import InvalidFormat +import yaml from quality_control.parsing import take +from r_qtl.errors import InvalidFormat + def thread_op(value, *functions): """Thread the `value` through the sequence of `functions`.""" return reduce(lambda result, func: func(result), functions, value) @@ -80,19 +80,22 @@ def genotype_data(zfile: ZipFile, cdata: dict) -> Iterator[dict]: lines = (line.strip().split(cdata.get("sep", ",")) for line in filter(lambda line: not line.startswith("#"), io.TextIOWrapper(genofile))) - id_line = next(lines) - id_key, samples = id_line[0], id_line[1:] - def __organise_by_id__(acc, item): - row = acc.get(item[id_key], {}) - return {**acc, item[id_key]: {**row, **item}} - for _key, row in reduce(# type: ignore[var-annotated] - __organise_by_id__, - (row - for batch in __n_batch__(lines, 300) - for line in batch - for row in __merge__(id_key, samples, line)), - {}).items(): - yield row + try: + id_line = next(lines) + id_key, samples = id_line[0], id_line[1:] + def __organise_by_id__(acc, item): + row = acc.get(item[id_key], {}) + return {**acc, item[id_key]: {**row, **item}} + for _key, row in reduce(# type: ignore[var-annotated] + __organise_by_id__, + (row + for batch in __n_batch__(lines, 300) + for line in batch + for row in __merge__(id_key, samples, line)), + {}).items(): + yield row + except StopIteration: + return None def map_data(zfile: ZipFile, map_type: str, cdata: dict) -> tuple[dict, ...]: """Read gmap files to get the genome mapping data""" @@ -125,9 +128,3 @@ def map_data(zfile: ZipFile, map_type: str, cdata: dict) -> tuple[dict, ...]: lambda gmap, row: gmap + (dict(zip(headers, row)),), zip(*(line[1:] for line in lines)), tuple()) - -def read_r_qtl2_files(filepath: Path): - """Read R/qtl2 format zip files.""" - with ZipFile(filepath, "r") as zfile: - cf = control_data(zfile) - raise NotImplementedError("Implementation is incomplete.") -- cgit v1.2.3