"""Create a targeted adduct search space and annotate matching peaks."""

import emzed

targets = emzed.Table.create_table(
    ["mf", "rt"],
    [str, emzed.RtType],
    rows=[["C6H12O6", 20.0]],
)
targets.add_enumeration()

adducts = emzed.Table.stack_tables(
    [
        emzed.adducts.M_plus_Br,
        emzed.adducts.Two_M_plus_H,
        emzed.adducts.M_plus_ACN_plus_H,
    ]
)
adducts.drop_columns("id")
adducts.add_enumeration()

# solution_space expands each target formula/RT pair into expected adduct and
# isotope hypotheses (candidate peaks) with predicted masses and abundances.
candidates = emzed.targeted.solution_space(targets, adducts, 0.99)
print("solution-space preview:")
print(candidates[:5])
print()

candidates.rename_columns(adduct_name="original_adduct_name")

# annotate_adducts links compatible hypotheses across adduct types using m/z
# and RT tolerances, then assigns cluster IDs for consistent explanations.
annotated = emzed.annotate.annotate_adducts(
    candidates,
    adducts,
    mz_tol=2e-5,
    rt_tol=5.0,
    explained_abundance=0.95,
).sort_by("adduct_name")

print(
    annotated.extract_columns(
        "mf",
        "rt",
        "adduct_name",
        "isotope_decomposition",
        "adduct_isotopes",
        "m0",
        "abundance",
        "adduct_cluster_id",
    )
)
