envipat.isopattern

Isotope pattern calculation.

Port of R’s isopattern. Calls envipat._core.enviPat_py.pattern_algo1/ pattern_algo2 (the Python bindings of the original iso_core.c algorithms – see src/envipat/_core/enviPat_py.c).

Result shape

Returns dict[str, pandas.DataFrame | str] keyed by (stringified) input formula, matching R’s pattern list (named by as.character(chemforms)). Unlike R – where a duplicate name in names()<- simply overwrites the earlier list entry, silently discarding its result – this port raises ValueError as soon as a duplicate formula is detected in the input, naming every duplicated formula (not just the first). A dict keyed by formula cannot represent two results for the same formula anyway, so silently keeping only the last one would be a silent data-loss bug in Python even though it mirrors R’s own list-naming semantics; see MR description for this decision. Each value is a DataFrame with columns "m/z", "abundance", then one integer column per isotope actually present in that formula’s peaks (e.g. "12C", "13C", "1H", …), in the same order _core returns them.

For an invalid formula (the same “unknown element” / malformed-formula condition check_chemform flags), R’s isopattern sets that list entry to the literal string "error" instead of a data.frame. This is reproduced here exactly: on EnviPatCoreError (or an empty result – _core never actually returns one for pattern_algo1/2, but the empty-result path is mirrored anyway for parity with the same idiom in envelope/vdetect), the dict value is the string "error", not a DataFrame. Callers must check for this (isinstance(pattern[f], str)) before treating a value as a DataFrame – exactly as R callers must check identical(pattern[[i]], "error") (see check_several’s R original, which does exactly that).

envipat.isopattern.isopattern(isotopes, chemforms, threshold=0.001, charge=False, emass=0.00054857990924, algo=1, rel_to=0, verbose=True, return_iso_calc_amount=False)[source]

Calculate isotope fine structure pattern(s) for one or more formulas.

Port of R’s isopattern.

Parameters:
isotopes

Isotope reference table (columns, by position: element, isotope, mass, abundance, …), e.g. envipat.data.isotopes. Must have exactly 5 columns, matching R’s length(isotopes) != 5 check (a data.frame’s length() is its column count).

chemforms

A single formula string or a sequence of formula strings, such as envipat.data.chemforms or check_chemform(...)["new_formula"].

threshold

Probability (percentage points, 0-100) below which isotope peaks are pruned, as defined by rel_to.

charge

False to omit charge/m-to-z conversion (default), a single int/float charge applied to every formula, or a sequence of per-formula charges (length must equal len(chemforms)).

emass

Electron mass (Da), only used when charge is not False.

algo

1 (element-wise transition trees, iso_core_pattern_algo1) or 2 (two-element transition trees, iso_core_pattern_algo4 – R’s “algo_4”).

rel_to

Probability definition/pruning basis: 0=relative to the most intense peak, 1=relative to the “monoisotopic” peak, 2=absolute, 3/4=percentage-of-(0/1) but returned as absolute probabilities. See R’s ?isopattern details section.

verbose

If True, prints " done." on completion (matching R’s message(" done."); R’s own per-formula progress messages from the C layer are not reproduced here).

return_iso_calc_amount

If True, returns raw per-element isotope-combination counts (dict[str, int]) instead of the pattern DataFrame for every formula – mirrors R’s rica/return_iso_calc_amount branch, which returns a completely different (named-vector) shape.

Returns:
dict[str, pandas.DataFrame | str | dict[str, int]]

Keyed by (stringified) formula; see module docstring for the per-formula value shapes, including the "error" sentinel.

Raises:
ValueError

If chemforms contains the same (stringified) formula more than once – see module docstring; this diverges from R, which silently keeps only the last duplicate’s result.

Warns:
UserWarning

If emass differs from the default electron mass.

Notes

R’s plotit argument (base-graphics stick plot per formula) is intentionally not ported – no plotting side effect, matching envipat.resolution.get_resolution’s (issue #9) same omission of getR’s own plotit.