From 8e692b0c372db3999dfc3989f361676c579fb9cd Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 20 Feb 2024 05:15:00 +0300 Subject: Stand-alone function to read control file Add a function that, given the path to the zip file, will read the control data. It creates its own context manager. --- r_qtl/r_qtl2.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/r_qtl/r_qtl2.py b/r_qtl/r_qtl2.py index 96776b7..93b8c8e 100644 --- a/r_qtl/r_qtl2.py +++ b/r_qtl/r_qtl2.py @@ -318,3 +318,19 @@ def missing_value_codes_to_none(value: str, def replace_genotype_codes(value: str, genocodes: dict): """Convert genotype codes into values specified in control file.""" return genocodes.get(value, value) + +def read_control_file(zipfilepath: Union[str, Path]) -> dict: + """Read control data.""" + with ZipFile(str(zipfilepath), "r") as zfile: + # move `control_data` code here and replace existing function. + cdata = control_data(zfile) + return { + **cdata, + **{ + ftype: ([cdata[ftype]] + if isinstance(cdata[ftype], str) + else cdata[ftype]) + for ftype in FILE_TYPES + if bool(cdata.get(ftype)) + } + } -- cgit v1.2.3