aboutsummaryrefslogtreecommitdiff
path: root/r_qtl
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-01-02 08:49:25 +0300
committerFrederick Muriuki Muriithi2024-01-02 08:49:25 +0300
commit7a2bcc9e86bde0eb9c0d370f83df4684e5522f26 (patch)
treec3ab56044e34cc06165b2be972477635fbaf4158 /r_qtl
parent2162cec2084f712993180618eb92c5a6dfdc5963 (diff)
downloadgn-uploader-7a2bcc9e86bde0eb9c0d370f83df4684e5522f26.tar.gz
Cleanup: Fix linting and typing errors and update docs.
Diffstat (limited to 'r_qtl')
-rw-r--r--r_qtl/errors.py1
-rw-r--r--r_qtl/r_qtl2.py45
2 files changed, 21 insertions, 25 deletions
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.")