Skip to content

dispatch

parse_chromatogram

parse_chromatogram(path: str | Path) -> Chromatogram

Parse a single chromatogram file and infer parser type from file content/type.

Supported: - Agilent: .ch - Chromeleon: .txt (validated by metadata signature)

Source code in src/chromstream/parsers/dispatch.py
def parse_chromatogram(path: str | Path) -> Chromatogram:
    """
    Parse a single chromatogram file and infer parser type from file content/type.

    Supported:
    - Agilent: .ch
    - Chromeleon: .txt (validated by metadata signature)
    """
    path = Path(path)

    if not path.exists() or not path.is_file():
        raise FileNotFoundError(f"File not found: {path}")

    suffix = path.suffix.lower()
    if suffix == ".ch":
        try:
            return parse_agilent_ch(path)
        except Exception as e:
            raise ValueError(f"Failed to parse Agilent .ch file '{path}': {e}") from e

    if suffix == ".txt":
        if not _is_chromeleon_txt(path):
            raise ValueError(
                f"File '{path}' is .txt but does not match Chromeleon chromatogram metadata."
            )
        try:
            return parse_chromatogram_txt(path)
        except Exception as e:
            raise ValueError(
                f"Failed to parse Chromeleon .txt file '{path}': {e}"
            ) from e

    raise ValueError(
        f"Unsupported chromatogram file type '{suffix}' for '{path}'. Expected .ch or .txt."
    )