"""Chemistry basics: formulas, isotopes, adducts, and abundances."""

import emzed

print()

# --- formula + mass basics ---

glucose = emzed.mf("C6H12O6")
glucose_mass = glucose.mass()

water = emzed.mf("H2O")
water_mass = emzed.mass.of("H2O")

dehydrated = glucose - water
dehydrated_mass = dehydrated.mass()

protonated_composition = glucose + emzed.mf("H")
protonated_mz = glucose_mass + emzed.mass.H1 - emzed.mass.e

sodium_adduct = glucose + emzed.mf("Na")
sodium_adduct_mass = sodium_adduct.mass()

c13_glucose = emzed.mf("[13]CC5H12O6")
c13_glucose_mass = c13_glucose.mass()

print("formula + mass basics:\n")
print(f"glucose: {glucose}  m0={glucose_mass:.6f}")
print(f"glucose monoisotopic mass: {glucose_mass:.6f}")
print(f"water mass: {water_mass:.6f}")
print(f"glucose - water: {dehydrated}  m0={dehydrated_mass:.6f}")
print(
    f"[M+H]+ composition: {protonated_composition}  "
    f"m/z={protonated_mz:.6f} (mass uses H - electron)"
)
print(f"[M+Na]+ composition: {sodium_adduct}  m0={sodium_adduct_mass:.6f}")
print(f"13C-labeled glucose: {c13_glucose}  m0={c13_glucose_mass:.6f}")

print()

# --- elements + abundance basics ---

print("elements + abundance basics:\n")
print(f"carbon-12 mass: {emzed.mass.C12:.6f}")
print(f"carbon-13 abundance: {emzed.abundance.C13:.6f}")

carbon_isotopes = emzed.elements.filter(
    emzed.elements.symbol == "C",
    keep_view=True,
).sort_by("mass_number")

print(carbon_isotopes)
print()
print(f"O18 abundance: {emzed.abundance.O18:.6f}")
print(f"N15 abundance: {emzed.abundance.N15:.6f}")

print()

# --- adduct tables ---

print("adduct tables:")
print(emzed.adducts.positive[:5])
print()
print(emzed.adducts.single_charged[:8])

print()

# --- isotope centroids ---

print("isotope centroids of C6H12O6 (99% explained abundance):")
isotopes = emzed.chemistry.compute_centroids("C6H12O6", 0.99)
print(isotopes)
