From ce243a57b24d6adecb169487e706290d91b22d19 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 20 Feb 2024 10:57:56 +0300 Subject: Track filename in the errors R/qtl2 bundles can contain more than one file, of the same type. When errors are encountered in any of the files, we need to be able to inform the user which file it is, in addition to the line and column number. --- r_qtl/r_qtl2_qc.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'r_qtl') diff --git a/r_qtl/r_qtl2_qc.py b/r_qtl/r_qtl2_qc.py index 43f7d94..be1eac4 100644 --- a/r_qtl/r_qtl2_qc.py +++ b/r_qtl/r_qtl2_qc.py @@ -63,12 +63,13 @@ def validate_bundle(zfile: ZipFile): "The following files do not exist in the bundle: " + ", ".join(mfile[1] for mfile in missing)) -def make_genocode_checker(genocode: dict) -> Callable[[int, str, str], Optional[InvalidValue]]: +def make_genocode_checker(genocode: dict, filename: str) -> Callable[ + [int, str, str], Optional[InvalidValue]]: """Make a checker from the genotypes in the control data""" def __checker__(lineno: int, field: str, value: str) -> Optional[InvalidValue]: genotypes = tuple(genocode.keys()) if value not in genotypes: - return InvalidValue(lineno, field, value, ( + return InvalidValue(filename, lineno, field, value, ( f"Invalid value '{value}'. Expected one of {genotypes}.")) return None return __checker__ @@ -78,14 +79,15 @@ def geno_errors(zfile: ZipFile) -> Iterator[Union[InvalidValue, MissingFile]]: cdata = rqtl2.control_data(zfile) return ( error for error in retrieve_errors( - zfile, "geno", (make_genocode_checker(cdata.get("genotypes", {})),)) + zfile, "geno", (make_genocode_checker(cdata.get("genotypes", {}), "geno"),)) if error is not None) def pheno_errors(zfile: ZipFile) -> Iterator[Union[InvalidValue, MissingFile]]: """Check for and retrieve pheno errors.""" return ( error for error in retrieve_errors( - zfile, "pheno", (partial(decimal_points_error, mini=3),)) + zfile, "pheno", (partial( + decimal_points_error, mini=3, filename="pheno"),)) if error is not None) def phenose_errors(zfile: ZipFile) -> Iterator[Union[InvalidValue, MissingFile]]: @@ -108,7 +110,7 @@ def retrieve_errors(zfile: ZipFile, filetype: str, checkers: tuple[Callable]) -> continue if value is not None: for checker in checkers: - yield checker(lineno, field, value) + yield checker(lineno=lineno, field=field, value=value) except rqe.MissingFileError: fname = cdata.get(filetype) yield MissingFile(filetype, fname, f"Missing '{filetype}' file '{fname}'.") -- cgit v1.2.3