From 16fc2613bb7f3e5f3596f1470e0de2741bd55f5a Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 20 Dec 2023 12:25:44 +0300 Subject: Read R/qtl2 format files * Set up error objects. * Read the control data. --- r_qtl/__init__.py | 1 + r_qtl/errors.py | 8 ++++++++ r_qtl/r_qtl2.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 r_qtl/__init__.py create mode 100644 r_qtl/errors.py create mode 100644 r_qtl/r_qtl2.py (limited to 'r_qtl') diff --git a/r_qtl/__init__.py b/r_qtl/__init__.py new file mode 100644 index 0000000..5663832 --- /dev/null +++ b/r_qtl/__init__.py @@ -0,0 +1 @@ +"""The R/qtl parsing and processing code.""" diff --git a/r_qtl/errors.py b/r_qtl/errors.py new file mode 100644 index 0000000..648611e --- /dev/null +++ b/r_qtl/errors.py @@ -0,0 +1,8 @@ +"""R/qtl and R/qtl2 error types.""" +from collections import namedtuple + +class RQTLError(Exception): + """Base class for R/qtl and R/qtl2 errors.""" + +class InvalidFormat(RQTLError): + """Raised when the format of the file(s) is invalid.""" diff --git a/r_qtl/r_qtl2.py b/r_qtl/r_qtl2.py new file mode 100644 index 0000000..9c9d67b --- /dev/null +++ b/r_qtl/r_qtl2.py @@ -0,0 +1,32 @@ +"""The R/qtl2 parsing and processing code.""" +import json +import yaml +from pathlib import Path +from typing import List, Union +from zipfile import ZipFile, ZipInfo, is_zipfile + +from quality_control.debug import __pk__ +from r_qtl.errors import InvalidFormat + +def control_data(zfile: ZipFile) -> dict: + """Retrieve the control file from the zip file info.""" + files = tuple(filename + for filename in zfile.namelist() + if (filename.endswith(".yaml") or filename.endswith(".json"))) + num_files = len(files) + if num_files == 0: + raise InvalidFormat("Expected a json or yaml control file.") + + if num_files > 1: + raise InvalidFormat("Found more than one possible control file.") + + return (json.loads(zfile.read(files[0])) + if files[0].endswith(".json") + else yaml.safe_load(zfile.read(files[0]))) + + +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