| Title: | Annotation of Mass Spectra |
|---|---|
| Description: | Provides a comprehensive suite of functions to efficiently annotate mass spectra data. Motivated by the need for rapid and accurate chemical identification in high-resolution mass spectrometry, it integrates built-in chemical databases and high-performance C++ algorithms. Users can perform mass-to-charge (m/Z) and retention time searches, determine elemental compositions of molecules using heuristic rules, including specific isotopes, and annotate MS2 spectra with structural metrics using configurable chemistry rules. |
| Authors: | Sylvain Dechaumet [aut, cre], Etienne Thevenot [ctb], Eric Venot [rev], Annelaure Damont [ctb], Anaïs Legrand [ctb] |
| Maintainer: | Sylvain Dechaumet <[email protected]> |
| License: | CeCILL |
| Version: | 1.3.4 |
| Built: | 2026-09-27 10:26:02 UTC |
| Source: | https://github.com/odisce/spec2annot |
Add formula to annotate_mz()
add_formula_to_annot(mzannot_dt)add_formula_to_annot(mzannot_dt)
mzannot_dt |
A |
Return the mzannot_dt table with a new formula column
containing a string with the elemental composition.
adduct data
Adduct_dbAdduct_db
A data.table with the common adducts in LC-ESI-MS
Formula
charge
Exact mass value
Convert annotation to html string
annot_to_html(annotation, compo = "", compo_replace = "X")annot_to_html(annotation, compo = "", compo_replace = "X")
annotation |
String corresponding to the annotation |
compo |
Elemental composition to replace to in the annotation string |
compo_replace |
String to replace with |
An html string of the annotation.
annot_to_html("[M+H]+_13C2", "C6H12O2", "M") annot_to_html("[M+H-H2O]+_13C2", "C6H12O2", "M") annot_to_html("[M+H-H2O]+_13C2_18O", "C6H12O2", "M") annot_to_html("C6H13O2-H2O+_13C2_18O", "", "M") annot_to_html("[M+H-(OH•)]+_13C2_18O", "C6H12O2", "M")annot_to_html("[M+H]+_13C2", "C6H12O2", "M") annot_to_html("[M+H-H2O]+_13C2", "C6H12O2", "M") annot_to_html("[M+H-H2O]+_13C2_18O", "C6H12O2", "M") annot_to_html("C6H13O2-H2O+_13C2_18O", "", "M") annot_to_html("[M+H-(OH•)]+_13C2_18O", "C6H12O2", "M")
This function annotate a full mass spectrum using a full brute force
search optionnaly restricted by an elemental composition compo.
Since generating the space to research can take some times, the
parameter search_space can be pre-calculated in advance.
annotate_mz( input_spectrum, ppm = 5, polarity = NULL, compo = NULL, use_golden_ratio = TRUE )annotate_mz( input_spectrum, ppm = 5, polarity = NULL, compo = NULL, use_golden_ratio = TRUE )
input_spectrum |
A mass spectrum as a data.table with |
ppm |
mass tolerance for the search in ppm. |
polarity |
Polarity is used to convert the mass spectrum to neutral mass before running the search. |
compo |
(optional) Elemental composition as a string ( |
use_golden_ratio |
Logical to return the maximum
number of each element by using
the ratios from the seven golden
rules ( |
Return a data.table with the annotated spectrum.
annotate_mz(spectra_ms2, ppm = 3, polarity = 1, compo = "C10H12N5O6P1")annotate_mz(spectra_ms2, ppm = 3, polarity = 1, compo = "C10H12N5O6P1")
This function returns edges of networked ions
annotate_spectra(mass, debugL = FALSE)annotate_spectra(mass, debugL = FALSE)
mass |
A numeric vector |
debugL |
logisfggdcal to print debug message |
Return a numeric vector of network edges
This function returns a matrix with m/Z groups based on a tolerance.
brute_force_const( mass = 120, ppm = 5, mass_vc = 0L, maxiter_vc_ = NULL, name_vc = 0L, debugl = 0L, debugit = 0L )brute_force_const( mass = 120, ppm = 5, mass_vc = 0L, maxiter_vc_ = NULL, name_vc = 0L, debugl = 0L, debugit = 0L )
mass |
Targeted mass |
ppm |
Mass tolerance in ppm to restrict results |
mass_vc |
Vector of elemnt masses (size = n) |
maxiter_vc_ |
Vector of element maximum limits (optional) (size = n) |
name_vc |
Vector of elemnt names (size = n) |
debugl |
Integer (0: no message, 1: short, 2: verbose) |
debugit |
Integer for max iter to do |
A NumericMatrix containing the count of elements, exact mass mass,
ppm deviation and iteration number of solutions found by the
brute force algorithm.
Cross join of two data.table
CJ1(DT1, DT2)CJ1(DT1, DT2)
DT1 |
A data.table |
DT2 |
A data.table |
A data.table corresponding to the cross join of the two tables.
require(data.table) CJ1( data.table(A = 1:10, B = "A"), data.table( C = 50:100, D = rep(c("C", "D", "E", "F"), length.out = 51) ) )require(data.table) CJ1( data.table(A = 1:10, B = "A"), data.table( C = 50:100, D = rep(c("C", "D", "E", "F"), length.out = 51) ) )
Mono charged ions
db_monochargedb_monocharge
A data.table with the common charges in LC-ESI-MS
Formula
charge
Does the resulting ion can produce in-source losses ?
Exact mass value
Return the mass of an electorn
electron_mass()electron_mass()
Return the mass of an electron as a numeric value
electron_mass()electron_mass()
Periodic table as a data.table
ElementElement
A data.table with the Periodic Table Elements fields
Atomic number
Atomic symbol
Mass number
Exact atomic mass
Relative isotopic abundance
https://physics.nist.gov/cgi-bin/Compositions/stand_alone.pl?ele=&ascii=ascii2&isotype=some
Get element count from formula
element_from_formula(formula)element_from_formula(formula)
formula |
Formula as returned by |
Return a data.table with element count
# Exempl A formula <- gen_formula_from_compo("C6H2O3Ca2K1") element_from_formula(formula) # Exempl B formula <- gen_formula_from_compo("[C6H12O2+H]+_13C2") element_from_formula(formula)# Exempl A formula <- gen_formula_from_compo("C6H2O3Ca2K1") element_from_formula(formula) # Exempl B formula <- gen_formula_from_compo("[C6H12O2+H]+_13C2") element_from_formula(formula)
Find composition from mass
find_compo_from_mass( mass_target, ppm = 5, use_golden_ratio = TRUE, elements_vc = NULL, debugl = 0 )find_compo_from_mass( mass_target, ppm = 5, use_golden_ratio = TRUE, elements_vc = NULL, debugl = 0 )
mass_target |
mass to decompose |
ppm |
mass error to filter the results (in ppm) |
use_golden_ratio |
Logical to return the maximum
number of each element by using
the ratios from the seven golden
rules ( |
elements_vc |
(optional) character vector containing the element to include |
debugl |
Integer (0: no message, 1: short, 2: verbose) |
A data.table with one proposition by line with it's elemental composition, the theoretical mass and the mass deviation with the query in ppm.
find_compo_from_mass(125.215, ppm = 10) find_compo_from_mass(528.125, ppm = 3) find_compo_from_mass(89.0476, ppm = 3, elements_vc = "C3H7NO2")find_compo_from_mass(125.215, ppm = 10) find_compo_from_mass(528.125, ppm = 3) find_compo_from_mass(89.0476, ppm = 3, elements_vc = "C3H7NO2")
Calculate the ions database
fun_generate_ions_from_mz( mass = 252.2534, losses = Spec2Annot::Losses_db, db_iso = Spec2Annot::Isotopes_db, charges = Spec2Annot::db_monocharge, adducts = Spec2Annot::Adduct_db, polarity = NULL )fun_generate_ions_from_mz( mass = 252.2534, losses = Spec2Annot::Losses_db, db_iso = Spec2Annot::Isotopes_db, charges = Spec2Annot::db_monocharge, adducts = Spec2Annot::Adduct_db, polarity = NULL )
mass |
numerical mass to use as base |
losses |
losses as data.table (see Spec2Annot::Losses_db) or NULL |
db_iso |
A data.table with isotopes informations
(see |
charges |
charges as data.table (see Spec2Annot::db_monocharge) |
adducts |
adducts as data.table (see Spec2Annot::Adduct_db) or NULL |
polarity |
Polarity to subset the forms (1 for positive, 0 for negative mode or NULL for both) |
A data.table containing the full list of possible ions with the formula to calculate them
fun_get_ion_list()fun_get_ion_list()
Calculate the ions database
fun_get_ion_list( losses = Spec2Annot::Losses_db, charges = Spec2Annot::db_monocharge, adducts = Spec2Annot::Adduct_db, polarity = NULL )fun_get_ion_list( losses = Spec2Annot::Losses_db, charges = Spec2Annot::db_monocharge, adducts = Spec2Annot::Adduct_db, polarity = NULL )
losses |
losses as data.table (see Spec2Annot::Losses_db) or NULL |
charges |
charges as data.table (see Spec2Annot::db_monocharge) |
adducts |
adducts as data.table (see Spec2Annot::Adduct_db) or NULL |
polarity |
Polarity to subset the forms (1 for positive, 0 for negative mode or NULL for both) |
A data.table containing the full list of possible ions with the formula to calculate them
fun_get_ion_list()fun_get_ion_list()
Generate adduct list from mz
gen_adduct( mz, label = NULL, mz_type = c("neutral", "pos", "neg")[[1]], adduct_db = Spec2Annot::Adduct_db )gen_adduct( mz, label = NULL, mz_type = c("neutral", "pos", "neg")[[1]], adduct_db = Spec2Annot::Adduct_db )
mz |
Exact mass |
label |
(optional) a string to use in the ion labels if not set it will be just 'X'. |
mz_type |
Type of mass for targeted compound: "pos", "neg" or "neutral" |
adduct_db |
A data.table with adduct informations
see ( |
A data.table with adducts mass and labels.
gen_adduct(125.53658, "Isovalerine", "neutral") gen_adduct(116.0837, "C6H12O2", "neutral") gen_adduct(116.0837, "C6H12O2", "pos")gen_adduct(125.53658, "Isovalerine", "neutral") gen_adduct(116.0837, "C6H12O2", "neutral") gen_adduct(116.0837, "C6H12O2", "pos")
Generate formula from composition
gen_formula_from_compo(compo)gen_formula_from_compo(compo)
compo |
Elemental composition as a string (ex.: "C6H12O3NH2") |
Return a string with an arithmetic formula used to calculate the final mass. This function can be used to check if the string parser behave correctly.
gen_formula_from_compo("C6H2O3NH4") gen_formula_from_compo("-(C6H2O)-(H2O)") gen_formula_from_compo("-C6H2O-H2O+Ca2-") gen_formula_from_compo("[C6H12O2+H-H2O]+_13C") gen_formula_from_compo("[2(C6H2O3)+NH4]+_13C3") gen_formula_from_compo("[C6H12O2+H-H2O]++") gen_formula_from_compo("[C6H12O2+H-H2O]--_13C1")gen_formula_from_compo("C6H2O3NH4") gen_formula_from_compo("-(C6H2O)-(H2O)") gen_formula_from_compo("-C6H2O-H2O+Ca2-") gen_formula_from_compo("[C6H12O2+H-H2O]+_13C") gen_formula_from_compo("[2(C6H2O3)+NH4]+_13C3") gen_formula_from_compo("[C6H12O2+H-H2O]++") gen_formula_from_compo("[C6H12O2+H-H2O]--_13C1")
Calculate list of targetd ions from neutral mz
gen_ionlist( neutral_mz = 153.5125, polarity = c(0, 1)[1], iso = c(TRUE, FALSE)[1], multi = 0, losses = c(TRUE, FALSE)[1], adducts = c(TRUE, FALSE)[1], mono_db = Spec2Annot::db_monocharge, loss_db = Spec2Annot::Losses_db, adduct_db = Spec2Annot::Adduct_db, db_iso = Spec2Annot::Isotopes_db )gen_ionlist( neutral_mz = 153.5125, polarity = c(0, 1)[1], iso = c(TRUE, FALSE)[1], multi = 0, losses = c(TRUE, FALSE)[1], adducts = c(TRUE, FALSE)[1], mono_db = Spec2Annot::db_monocharge, loss_db = Spec2Annot::Losses_db, adduct_db = Spec2Annot::Adduct_db, db_iso = Spec2Annot::Isotopes_db )
neutral_mz |
Neutral Mass |
polarity |
Polarity |
iso |
Logical |
multi |
Multimers |
losses |
Logical |
adducts |
Logical |
mono_db |
Database |
loss_db |
Database |
adduct_db |
Database |
db_iso |
Database |
a data.table
gen_ionlist(153.5125, 1, TRUE, 2, TRUE, TRUE)gen_ionlist(153.5125, 1, TRUE, 2, TRUE, TRUE)
Function to generate isotopes from an exact mass
gen_isotopes(mz, label = NULL, db_iso = Spec2Annot::Isotopes_db)gen_isotopes(mz, label = NULL, db_iso = Spec2Annot::Isotopes_db)
mz |
Exact mass |
label |
(optional) a string to use in the ion labels if not set it will be just 'X'. |
db_iso |
A data.table with isotopes informations
(see |
Generate a data.table with isotopes mass and label
based on the mz value. By default, the isotopes are
chosen from Spec2Annot::Isotopes_db but it could
be specified by the user.
gen_isotopes(125.53658, "Isovalerine")gen_isotopes(125.53658, "Isovalerine")
Generate losses list from mz
gen_losses( mz, label = NULL, mz_type = c("neutral", "pos", "neg")[[1]], loss_db = Spec2Annot::Losses_db )gen_losses( mz, label = NULL, mz_type = c("neutral", "pos", "neg")[[1]], loss_db = Spec2Annot::Losses_db )
mz |
Exact mass |
label |
(optional) a string to use in the ion labels if not set it will be just 'X'. |
mz_type |
Type of mass for targeted compound: "pos", "neg" or "neutral" |
loss_db |
A data.table with losses information
see |
A data.table with losses mass and labels.
gen_losses(125.53658, "Isovalerine", "neutral") gen_losses(116.0837, "C6H12O2", "neutral") gen_losses(116.0837, "C6H12O2", "pos")gen_losses(125.53658, "Isovalerine", "neutral") gen_losses(116.0837, "C6H12O2", "neutral") gen_losses(116.0837, "C6H12O2", "pos")
Function to generate monocharges from an exact mass
gen_monocharge( mz, label = NULL, mz_type = c("neutral", "pos", "neg")[[1]], ion_mode = c("pos", "neg")[[1]], mono_db = Spec2Annot::db_monocharge )gen_monocharge( mz, label = NULL, mz_type = c("neutral", "pos", "neg")[[1]], ion_mode = c("pos", "neg")[[1]], mono_db = Spec2Annot::db_monocharge )
mz |
Exact mass |
label |
(optional) a string to use in the ion labels if not set it will be just 'X'. |
mz_type |
Type of mass for targeted compound: "pos", "neg" or "neutral" |
ion_mode |
Output mode wanted: "pos", "neg" |
mono_db |
A data.table with charges information
(see |
A data.table with ID, label and mz_query
gen_monocharge(125.53658, "Isovalerine", "neutral", "neg") gen_monocharge(116.0837, "C6H12O2", "neutral", "neg") gen_monocharge(116.0837, "C6H12O2", "neutral", "pos")gen_monocharge(125.53658, "Isovalerine", "neutral", "neg") gen_monocharge(116.0837, "C6H12O2", "neutral", "neg") gen_monocharge(116.0837, "C6H12O2", "neutral", "pos")
Get charge from composition
get_charge_from_compo(compo)get_charge_from_compo(compo)
compo |
Elemental composition as a string (ex.: "C6H12O3NH2") |
Return the number of positive or negative charge
get_charge_from_compo("C6H2O3NH4") get_charge_from_compo("C6H2O3NH4+") get_charge_from_compo("C6H2O3NH4++") get_charge_from_compo("C6H2O3NH4-") get_charge_from_compo("C6H2O3NH4---") get_charge_from_compo("[C6H12O2+H-H2O]+_13C") get_charge_from_compo("[2(C6H2O3)+NH4]++_13C3") get_charge_from_compo("[2(C6H2O3)-NH4]--_13C3_15N1")get_charge_from_compo("C6H2O3NH4") get_charge_from_compo("C6H2O3NH4+") get_charge_from_compo("C6H2O3NH4++") get_charge_from_compo("C6H2O3NH4-") get_charge_from_compo("C6H2O3NH4---") get_charge_from_compo("[C6H12O2+H-H2O]+_13C") get_charge_from_compo("[2(C6H2O3)+NH4]++_13C3") get_charge_from_compo("[2(C6H2O3)-NH4]--_13C3_15N1")
Calculation is performed as explained in http://ms-textbook.com/chapter-6/answer-6-3/
get_dbe(element_dt)get_dbe(element_dt)
element_dt |
Either a data.table as returned by
|
Return a string from an element table
get_dbe("C32H25N3O2S")get_dbe("C32H25N3O2S")
Get Element from a mass
get_element_from_mass(mass, use_golden_ratio = FALSE)get_element_from_mass(mass, use_golden_ratio = FALSE)
mass |
input mass |
use_golden_ratio |
Logical to return the maximum
number of each element by using
the ratios from the seven golden
rules ( |
Return a string with the number maximum number of each element to expect from the mass. get_element_from_mass(125.2535, FALSE) get_element_from_mass(125.2535, TRUE)
Get isotopes from annotation
get_iso_from_annot(annotation)get_iso_from_annot(annotation)
annotation |
Annotation in the form of |
Return a data.table with the following isotopes informations:
element: element type (13C, 18O, ...)
text: label to use for the element
elmt_nb: Element count
isotope: Isotope number (13, 18, ...)
mass: Mass of the isotope
ID: Unique identifier (integer)
get_iso_from_annot("[M+H]+_13C_18O") get_iso_from_annot("[M+H]+_13C2")get_iso_from_annot("[M+H]+_13C_18O") get_iso_from_annot("[M+H]+_13C2")
Calculation is performed as explained in https://en.wikipedia.org/wiki/Nitrogen_rule
get_nrule(n, mass)get_nrule(n, mass)
n |
Nitrogen atom number |
mass |
Ion mass |
A logical corresponding to TRUE if the N
rule is respected or FALSE if not.
get_nrule(5, 125.1235)get_nrule(5, 125.1235)
Get index in range
get_range(input, valA, valB)get_range(input, valA, valB)
input |
a numeric vector |
valA |
a numeric value for the lower bound |
valB |
a numeric value for the higher bound |
An integer vector of closest indexes found
Check the Senior theorems as described in Morikawa and Newbold (2003) with the following: i) the sum of valencies is an even number, or the total number of atoms having odd valencies is even. ii) the sum of valencies is greater than or equal to twice the maximum valency. iii) the sum of valencies is greater than or equal to twice the number of atoms minus 1.
get_senior(element_dt, global = TRUE)get_senior(element_dt, global = TRUE)
element_dt |
Either a data.table as returned by
|
global |
Logical to return a unique value if all the
theorem are valid ( |
If global is set to TRUE, return a unique logical value
corresponding to TRUE if all the theorems pass or FALSE
if any of them isn't.
If global is set to FALSE, return a named logical vector with
the result of each theorem.
Senior JK (1951) Partitions and Their Representative Graphs. Am J Math 73:663. doi: 10.2307/2372318
Morikawa T, Newbold BT (2003) Analogous odd-even parities in mathematics and chemistry. Chemistry 12:445–450
get_senior("C6H12O3")get_senior("C6H12O3")
ions data
ions_databaseions_database
A data.table with the common ions found in LC-ESI-MS
Encoded formula
Exact mass formula as a character string
charge
Molecular form (Monocharge, Loss, Adduct, ...)
isotopes data
Isotopes_dbIsotopes_db
A data.table with the common isotopes found in LC-ESI-MS
Isotope formula
Exact mass
Natural abundance (in percent)
Exact mass difference from non-isotopic element
losses data
Losses_dbLosses_db
A data.table with the common losses found in LC-ESI-MS
loss formula
Exact mass
Character encoding
This function match two table, searching every entries in B which are contained in A mz +- ppmtol and A.rt +- rttol
match_tables(db_dt, exp_dt, ppmtol, rttol, debugL = FALSE)match_tables(db_dt, exp_dt, ppmtol, rttol, debugL = FALSE)
db_dt |
a data.frame with at least mz and rt values (ref table) |
exp_dt |
a data.frame with at least mz and rt values (exp table) |
ppmtol |
a numeric value for the ppm tolerance |
rttol |
a numeric value for the rt tolerance |
debugL |
logisfggdcal to print debug message |
A data.frame with matched entries between db_dt and exp_dt.
Calculate ion mass using form
mz_calc_ion(mass, form = "-H")mz_calc_ion(mass, form = "-H")
mass |
numerical mass to use as base |
form |
chemical form to add or substract (on
of |
A numeric value corresponding to the mass form.
mz_calc_ion(142.5236, "-H")mz_calc_ion(142.5236, "-H")
Calculate mz from a string with signs
mz_from_string(string)mz_from_string(string)
string |
Formula as a string of the form "C6H5O3+" or with isotopes
|
Return a numeric value corresponding to the mass of the string input
mz_from_string("C6H5O3+") mz_from_string("C6H5O3++") mz_from_string("[C6H4O3+H]+_13C1") mz_from_string("[C6H4O3+H]+_13C2")mz_from_string("C6H5O3+") mz_from_string("C6H5O3++") mz_from_string("[C6H4O3+H]+_13C1") mz_from_string("[C6H4O3+H]+_13C2")
This function returns a numeric value
corresponding to the mass deviation
between massa and massb in ppm.
mz_ppm(massa = 120.1253, massb = 120.1263)mz_ppm(massa = 120.1253, massb = 120.1263)
massa |
First mass |
massb |
Second mass |
ppm as a numeric value between massa and massb.
Get mass range with ppm
mz_range(mass = 120.1253, ppm = 10)mz_range(mass = 120.1253, ppm = 10)
mass |
First mass |
ppm |
ppm tolerance mass +- ppm(mass) |
A numeric vector with the mass window.
This function returns a matrix with m/Z groups based on a tolerance.
mz_vec_aggregate(xx, tt)mz_vec_aggregate(xx, tt)
xx |
A numeric and sorted vector of m/Z |
tt |
Tolerance in absolute m/Z to group peaks |
A numeric vector with group indexes
Search DB egaint EXP
search_db(db_dt, exp_dt, ppmtol = 5, rttol = 5)search_db(db_dt, exp_dt, ppmtol = 5, rttol = 5)
db_dt |
a data.table with id, mz, rt columns |
exp_dt |
a data.table with id, mz, rt columns |
ppmtol |
numeric value to set ppm search in m/Z dimension. Can be set to NULL to use only rt search. |
rttol |
numeric value to set rt tolerance in rt dimension. Can be set to NULL to use only mz search. |
Return the exp_dt data.table entries matching the criteria with 2 new columns: dbid (ids in db_dt) and expid (ids in exp_dt).
Search DB egaint EXP using cpp function
search_db_cpp(in_db, in_exp, ppmtol = 5, rttol = 5)search_db_cpp(in_db, in_exp, ppmtol = 5, rttol = 5)
in_db |
a data.table with id, mz, rt columns |
in_exp |
a data.table with id, mz, rt columns |
ppmtol |
numeric value to set ppm search in m/Z dimension. |
rttol |
numeric value to set rt tolerance in rt dimension. |
Return the exp_dt data.table entries matching the criteria with 2 new columns: dbid (ids in db_dt) and expid (ids in exp_dt).
Spectra example
spectra_fullspectra_full
A data.table with a centroided MS spectra used as example
m/Z value
Measured intensity
Unique identifier
MS2 spectrum of C10H12N5O6P1 Precursor mass: 330.0597 Mode: ESI + Collision mode: HCD Energy: 30
spectra_ms2spectra_ms2
A data.table with a centroided MS2 spectra used as example
m/Z value
Measured intensity
Return a string from an element table
string_from_element(element_dt)string_from_element(element_dt)
element_dt |
A data.table as returned by
|
Return a string from an element table
"C6H12O3" %>% gen_formula_from_compo() %>% element_from_formula() %>% string_from_element()"C6H12O3" %>% gen_formula_from_compo() %>% element_from_formula() %>% string_from_element()
Valence data
valence_dbvalence_db
A data.table with element valence
Atomic symbol
valence value