envipat.check_several

Check for overlapping molecules.

Port of R’s check_several. Screens isotope fine structures (from envipat.isopattern.isopattern()) or envelope centroids/intensoids (from envipat.vdetect.vdetect()) for m/z overlaps between different compounds, within a tolerance window (absolute m/z or ppm).

Algorithm (mirrors R exactly): every peak of every compound is flattened into one long (compound_index, mz, peak_number_within_compound) list, stably sorted by mz ascending, then walked pairwise. Whenever two adjacent peaks in that sorted order belong to different compounds and the lower one’s mz plus the tolerance window reaches at least the higher one’s mz, both compounds are flagged, and each accumulates a "/" separated trail of which other compound it overlapped with and which of that other compound’s peaks.

Known R bug – deliberately fixed here, not reproduced

R’s ppm==TRUE branch has a stray trailing comma in one of its four paste0() calls (the second get3[...] update): paste0(get3[getit1[i - 1]], getit3[i], "/",). R’s parser accepts a trailing comma as an empty (missing) argument rather than a syntax error, but paste0 then raises "argument is missing, with no default" the moment it actually tries to evaluate that missing argument – which happens on the very first overlap found while ppm=TRUE. In other words, check_several(..., ppm=TRUE) is not merely “a bit buggy”: it hard-errors on every input where an overlap exists, i.e. on the one condition the function exists to report. Golden fixture tests/golden/check_several/case_004.csv captures this: R’s own check_several(pat_overlap, dmz=50, ppm=TRUE) errors with exactly "argument is missing, with no default" instead of producing a normal output table.

This is a plain typo (a copy-paste of the ppm==FALSE block’s four updates with one stray comma), not a deliberate-but-odd design choice worth preserving for behavioural parity. Reproducing it would make ppm=True unusable in Python for the one scenario it is meant to detect, for no compatibility benefit – nothing in the R package or its docs relies on this branch raising. This port removes the extra comma so the ppm=True branch is symmetric with ppm=False and returns a normal result instead of raising. See MR description for the same disclosure, per this repo’s established norm (MRs !4-!7) of calling out such decisions explicitly rather than silently diverging from R.

Result shape

Returns a pandas.DataFrame with columns "compound", "warning", "to?" and "peak_number", corresponding column-for-column to R’s data.frame(names(pattern), get1, get2, get3) – but reshaped to native Python/pandas types rather than R’s own string-serialization idioms (per reviewer decision on MR !8; see MR description):

  • "compound": str (unchanged – already native on both sides).

  • "warning": bool, not R’s character column of ""/"TRUE". In R’s original, "" isn’t a missing/NA marker – get1 is initialized to "" for every compound and only ever reassigned to the literal string "TRUE" when an overlap involving that compound is found, so "" unambiguously means “no overlap found” i.e. False, not “unknown”/NA. Hence a plain bool column (True/False), not a nullable one.

  • "to?" / "peak_number": list[int] per row (empty list, not "", when there is no overlap), not R’s "/"-terminated string of 1-based row numbers / peak indices (e.g. "2/2/2/" -> [2, 2, 2]). These are always non-negative integer indices (1-based, R-style, preserved as-is rather than renumbered to 0-based, since they index into pattern / a compound’s own peak table the same way R’s did), so a list of Python int is the natural, idiomatic shape – not parsed floats, and not re-encoded as another delimited string.

A caller diffing this against R’s own data.frame output (e.g. the golden fixtures, which are frozen from R and still hold the original ""/"TRUE" and "2/2/2/" strings) must parse those fixture strings into the same native types before comparing – see tests/python/test_pipeline.py’s check_several test.

envipat.check_several.check_several(pattern, dmz, ppm=True)[source]

Check for m/z overlaps between different compounds’ peaks.

Port of R’s check_several.

Parameters:
pattern

Mapping of compound -> DataFrame (columns "m/z", "abundance", …), as returned by envipat.isopattern.isopattern() or envipat.vdetect.vdetect(). Must have at least 2 entries.

dmz

Overlap tolerance window: an absolute m/z value if ppm=False, or a ppm value (must be > 0) if ppm=True.

ppm

Whether dmz is a ppm value (default True, matching R) or an absolute m/z value.

Returns:
pandas.DataFrame

Columns "compound" (str), "warning" (bool), "to?" (list[int]) and "peak_number" (list[int]) – see module docstring for what each represents and why these are the native types, rather than R’s own string-serialized column values.