Skip to content

Commit

Permalink
reduce debt
Browse files Browse the repository at this point in the history
  • Loading branch information
Adafede committed Jul 14, 2023
1 parent 07223fa commit 180f3c9
Show file tree
Hide file tree
Showing 44 changed files with 1,118 additions and 458 deletions.
88 changes: 44 additions & 44 deletions R/annotate_masses.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ annotate_masses <-

log_debug("... neutral lossses")

adducts_m <- adducts_mass_table$mass
names(adducts_m) <- adducts_mass_table$adduct
add_m <- adducts_mass_table$mass
names(add_m) <- adducts_mass_table$adduct

if (ms_mode == "pos") {
adduct_db_file <-
Expand Down Expand Up @@ -571,62 +571,62 @@ annotate_masses <-
) |>
tidytable::rowwise() |>
dplyr::mutate(
`[1M+(H)3]3+` = (mz - adduct_mass + 3 * adducts_m["H (proton)"]) / 3,
`[1M+(H)3]3+` = (mz - adduct_mass + 3 * add_m["H (proton)"]) / 3,
`[1M+(H)2(Na)1]3+` = (mz - adduct_mass +
2 * adducts_m["H (proton)"] +
adducts_m["Na (sodium)"]) / 3,
2 * add_m["H (proton)"] +
add_m["Na (sodium)"]) / 3,
`[1M+(H)1(Na)2]3+` = (mz - adduct_mass +
adducts_m["H (proton)"] +
2 * adducts_m["Na (sodium)"]) / 3,
`[1M+(Na)3]3+` = (mz - adduct_mass + 3 * adducts_m["Na (sodium)"]) / 3,
`[1M+(H)2]2+` = (mz - adduct_mass + 2 * adducts_m["H (proton)"]) / 2,
add_m["H (proton)"] +
2 * add_m["Na (sodium)"]) / 3,
`[1M+(Na)3]3+` = (mz - adduct_mass + 3 * add_m["Na (sodium)"]) / 3,
`[1M+(H)2]2+` = (mz - adduct_mass + 2 * add_m["H (proton)"]) / 2,
`[1M+(H)2(NH3)1]2+` = (mz - adduct_mass +
2 * adducts_m["H (proton)"] +
adducts_m["NH4 (ammonium)"]) / 2,
2 * add_m["H (proton)"] +
add_m["NH4 (ammonium)"]) / 2,
`[1M+(H)1(Na)1]2+` = (mz - adduct_mass +
adducts_m["H (proton)"] +
adducts_m["Na (sodium)"]) / 2,
add_m["H (proton)"] +
add_m["Na (sodium)"]) / 2,
`[1M+(Mg)1]2+` = (mz - adduct_mass +
adducts_m["Mg (magnesium)"]) / 2,
add_m["Mg (magnesium)"]) / 2,
`[1M+(H)1(K)1]2+` = (mz - adduct_mass +
adducts_m["H (proton)"] +
adducts_m["K (potassium)"]) / 2,
add_m["H (proton)"] +
add_m["K (potassium)"]) / 2,
`[1M+(Ca)1]2+` = (mz - adduct_mass +
adducts_m["Ca (calcium)"]) / 2,
add_m["Ca (calcium)"]) / 2,
`[1M+(H)2(ACN)1]2+` = (mz - adduct_mass +
2 * adducts_m["H (proton)"] +
adducts_m["C2H3N (acetonitrile)"]) / 2,
2 * add_m["H (proton)"] +
add_m["C2H3N (acetonitrile)"]) / 2,
`[1M+(Na)2]2+` = (mz - adduct_mass +
2 * adducts_m["Na (sodium)"]) / 2,
2 * add_m["Na (sodium)"]) / 2,
`[1M+(Fe)1]2+` = (mz - adduct_mass +
adducts_m["Fe (iron)"]) / 2,
add_m["Fe (iron)"]) / 2,
`[1M+(H)2(ACN)2]2+` = (mz - adduct_mass +
2 * adducts_m["H (proton)"] +
2 * adducts_m["C2H3N (acetonitrile)"]) / 2,
2 * add_m["H (proton)"] +
2 * add_m["C2H3N (acetonitrile)"]) / 2,
`[1M+(H)2(ACN)3]2+` = (mz - adduct_mass +
2 * adducts_m["H (proton)"] +
3 * adducts_m["C2H3N (acetonitrile)"]) / 2,
2 * add_m["H (proton)"] +
3 * add_m["C2H3N (acetonitrile)"]) / 2,
`[2M+(Mg)1]2+` = (2 * (mz - adduct_mass) +
adducts_m["Mg (magnesium)"]) / 2,
add_m["Mg (magnesium)"]) / 2,
`[2M+(Ca)1]2+` = (2 * (mz - adduct_mass) +
adducts_m["Ca (calcium)"]) / 2,
add_m["Ca (calcium)"]) / 2,
`[2M+(Fe)1]2+` = (2 * (mz - adduct_mass) +
adducts_m["Fe (iron)"]) / 2,
add_m["Fe (iron)"]) / 2,
`[2M+(H)1]1+` = 2 * (mz - adduct_mass) +
adducts_m["H (proton)"],
add_m["H (proton)"],
`[2M+(H)1(NH3)1]1+` = 2 * (mz - adduct_mass) +
adducts_m["H (proton)"] +
adducts_m["NH4 (ammonium)"],
add_m["H (proton)"] +
add_m["NH4 (ammonium)"],
`[2M+(Na)1]1+` = 2 * (mz - adduct_mass) +
adducts_m["Na (sodium)"],
`[2M+(K)1]1+` = 2 * (mz - adducts_m["H (proton)"]) +
adducts_m["K (potassium)"],
add_m["Na (sodium)"],
`[2M+(K)1]1+` = 2 * (mz - add_m["H (proton)"]) +
add_m["K (potassium)"],
`[2M+(H)1(ACN)1]1+` = 2 * (mz - adduct_mass) +
adducts_m["H (proton)"] +
adducts_m["C2H3N (acetonitrile)"],
add_m["H (proton)"] +
add_m["C2H3N (acetonitrile)"],
`[2M+(Na)1(ACN)1]1+` = 2 * (mz - adduct_mass) +
adducts_m["C2H3N (acetonitrile)"] +
adducts_m["Na (sodium)"]
add_m["C2H3N (acetonitrile)"] +
add_m["Na (sodium)"]
) |>
tidytable::select(-adduct_mass) |>
tidytable::ungroup()
Expand All @@ -646,17 +646,17 @@ annotate_masses <-
tidytable::rowwise() |>
dplyr::mutate(
`[1M-(H)3]3-` = (mz + adduct_mass -
3 * adducts_m["H (proton)"]) / 3,
3 * add_m["H (proton)"]) / 3,
`[1M-(H)2]2-` = (mz + adduct_mass -
2 * adducts_m["H (proton)"]) / 2,
2 * add_m["H (proton)"]) / 2,
`[2M-(H)1]1-` = 2 * (mz + adduct_mass) -
adducts_m["H (proton)"],
add_m["H (proton)"],
`[2M+(FA)1-(H)1]1-` = 2 * (mz + adduct_mass) +
adducts_m["CH2O2 (formic)"] - adducts_m["H (proton)"],
add_m["CH2O2 (formic)"] - add_m["H (proton)"],
`[2M+(Hac)1-(H)1]1-` = 2 * (mz + adduct_mass) +
adducts_m["C2H4O2 (acetic)"] - adducts_m["H (proton)"],
add_m["C2H4O2 (acetic)"] - add_m["H (proton)"],
`[3M-(H)1]1-` = 3 * (mz + adduct_mass) -
adducts_m["H (proton)"]
add_m["H (proton)"]
) |>
tidytable::select(-adduct_mass) |>
tidytable::ungroup()
Expand Down
58 changes: 35 additions & 23 deletions R/annotate_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ utils::globalVariables(
#'
#' @description This function annotates spectra
#'
#' @details It takes two files as input. A query file that will be matched against a library file.
#' @details It takes two files as input.
#' A query file that will be matched against a library file.
#' Multiple comparison distances are available
#' ('gnps', 'navdist','ndotproduct','neuclidean', 'nspectraangle' (See MsCoreUtils for details)).
#' Number of matched peaks and their ratio are also available (See MatchForwardReverseParam for details).
#' ('gnps', 'navdist','ndotproduct','neuclidean', 'nspectraangle')
#' (See MsCoreUtils for details).
#' Number of matched peaks and their ratio are also available
#' (See MatchForwardReverseParam for details).
#' Parallel processing is also made available.
#'
#' @include export_output.R
#' @include export_params.R
#' @include import_spectra.R
#'
#' @param input Query file containing spectra. Currently an '.mgf' file
#' @param library Library containing spectra to match against. Can be '.mgf' or '.sqlite' (Spectra formatted)
#' @param library Library containing spectra to match against.
#' Can be '.mgf' or '.sqlite' (Spectra formatted)
#' @param polarity MS polarity. Must be 'pos' or 'neg'.
#' @param output Output file.
#' @param method Method to be used to perform spectral comparison
Expand All @@ -45,8 +49,9 @@ utils::globalVariables(
#' @param dalton Absolute Dalton tolerance to be used
#' @param npeaks Absolute minimum number of peaks to be matched
#' @param rpeaks Relative minimum number of peaks to be matched
#' @param condition Condition to be fulfilled. Either 'OR' or 'AND' (mass and peaks minima).
#' @param qutoff Intensity under which ms2 fragments will be removed previous to comparison.
#' @param condition Condition to be fulfilled.
#' Either 'OR' or 'AND' (mass and peaks minima).
#' @param qutoff Intensity under which ms2 fragments will be removed.
#' @param parallel Boolean. Process in parallel
#' @param fast Boolean. Do it fast
#' @param approx Perform matching without precursor match
Expand Down Expand Up @@ -82,14 +87,17 @@ annotate_spectra <- function(input = params$files$spectral$raw,
lapply(X = unlist(library), file.exists)
)
stopifnot(
"Your similarity is not supported, supported similarities are 'gnps', 'navdist', 'ndotproduct', 'neuclidean', 'nspectraangle'" = method %in%
c(
"gnps",
"navdist",
"ndotproduct",
"neuclidean",
"nspectraangle"
)
"Your similarity is not supported,
supported similarities are
'gnps', 'navdist', 'ndotproduct', 'neuclidean', 'nspectraangle'" =
method %in%
c(
"gnps",
"navdist",
"ndotproduct",
"neuclidean",
"nspectraangle"
)
)

## Not checking for ppm and Da limits, everyone is free.
Expand Down Expand Up @@ -158,8 +166,7 @@ annotate_spectra <- function(input = params$files$spectral$raw,
tolerance = dalton,
MAPFUN = Spectra::joinPeaksGnps,
FUN = sim_fun,
requirePrecursor = ifelse(
test = approx,
requirePrecursor = ifelse(test = approx,
yes = FALSE,
no = TRUE
),
Expand All @@ -174,13 +181,14 @@ annotate_spectra <- function(input = params$files$spectral$raw,
args = match_args
)
} else {
params_sim <- do.call(
what = MetaboAnnotation::MatchForwardReverseParam,
args = match_args
)
params_sim <-
do.call(
what = MetaboAnnotation::MatchForwardReverseParam,
args = match_args
)
}

## COMMENT (AR): TODO Maybe implement some safety sanitization of the spectra?
## COMMENT (AR): TODO Maybe implement sanitization of the spectra?
## Can be very slow otherwise

log_debug("Applying initial intensity filter to query spectra")
Expand Down Expand Up @@ -240,7 +248,10 @@ annotate_spectra <- function(input = params$files$spectral$raw,
),
no = target_inchikey_2D
),
structure_smiles_2D = tidytable::coalesce(target_smiles_2D, target_smiles)
structure_smiles_2D = tidytable::coalesce(
target_smiles_2D,
target_smiles
)
) |>
tidytable::select(tidytable::any_of(
c(
Expand Down Expand Up @@ -305,7 +316,8 @@ annotate_spectra <- function(input = params$files$spectral$raw,
df_final <- df_empty
}
} else {
log_debug("No spectra matched the given polarity, returning an empty dataframe")
log_debug("No spectra matched the given polarity,
returning an empty dataframe")
df_final <- df_empty
}

Expand Down
Loading

0 comments on commit 180f3c9

Please sign in to comment.