Skip to content

Formula From Mz

Fit molecular-formula candidates to an observed m/z value.

"""Generate formula candidates for an observed m/z value."""

import time

import emzed

neutral_mass = emzed.mass.of("C6H12O6")

t0 = time.perf_counter()
candidates = emzed.chemistry.formula_table(
    neutral_mass - 0.01,
    neutral_mass + 0.01,
    c_range=(0, 12),
    h_range=(0, 24),
    n_range=(0, 2),
    o_range=(0, 12),
    p_range=(0, 1),
    s_range=(0, 1),
    apply_rules=False,
)
dt = time.perf_counter() - t0

candidates.add_column(
    "error_ppm",
    1e6 * (candidates.m0 - neutral_mass) / neutral_mass,
    float,
)
candidates = candidates.sort_by("error_ppm")

print(f"neutral mass target: {neutral_mass:.6f}")
print(f"formula fit runtime: {dt:.4f} s")
print()

top = candidates.extract_columns("mf", "m0", "error_ppm")
print(top[:10])
neutral mass target: 180.063390
formula fit runtime: 0.0141 s

mf          m0           error_ppm
str         MzType       float
----------  -----------  ----------
C8H8N2O3     180.053493  -54.964685
C2H14NO6S    180.054185  -51.122031
C6H13O4P     180.055147  -45.780978
C5H12N2O3S   180.056864  -36.244291
C9H11NOP     180.057826  -30.903239
C3H17O4PS    180.058518  -27.060584
CH12N2O8     180.059368  -22.336578
C10H12OS     180.060886  -13.907713
C6H15NOPS    180.061197  -12.182845
C6H12O6      180.063390    0.000000