Skip to content

Commit

Permalink
Fix #166
Browse files Browse the repository at this point in the history
  • Loading branch information
Adafede committed Aug 6, 2024
1 parent a87375a commit 85843a1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 45 deletions.
3 changes: 2 additions & 1 deletion R/annotate_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ annotate_spectra <- function(input = get_params(step = "annotate_spectra")$files
cutoff = qutoff,
dalton = dalton,
polarity = polarity,
ppm = ppm
ppm = ppm,
sanitize = FALSE
) |>
lapply(FUN = applyProcessing) |>
concatenateSpectra()
Expand Down
40 changes: 17 additions & 23 deletions R/import_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import::from(stringi, stri_replace_all_regex, .into = environment())
#' @param dalton Dalton tolerance
#' @param polarity Polarity
#' @param ppm PPM tolerance
#' @param sanitize Flag indicating whether to sanitize. Default TRUE
#'
#' @return Spectra object containing the imported spectra
#'
Expand All @@ -31,7 +32,8 @@ import_spectra <- function(file,
cutoff = 0,
dalton = 0.01,
polarity = NA,
ppm = 10) {
ppm = 10,
sanitize = TRUE) {
file_ext <-
stri_replace_all_regex(
str = file,
Expand All @@ -40,29 +42,17 @@ import_spectra <- function(file,
vectorize_all = FALSE
)

switch(
spectra <- switch(
EXPR = file_ext,
"mgf" = {
readMgf(f = file) |>
# TODO Change as soon as R 4.4.0 becomes oldrel
# readMgfSplit(f = file) |>
Spectra() |>
sanitize_spectra(
cutoff = cutoff,
dalton = dalton,
polarity = polarity,
ppm = ppm
)
Spectra()
},
"msp" = {
readMsp(f = file) |>
Spectra() |>
sanitize_spectra(
cutoff = cutoff,
dalton = dalton,
polarity = polarity,
ppm = ppm
)
Spectra()
},
# "sqlite" = {
# CompDb(x = file) |>
Expand All @@ -72,13 +62,17 @@ import_spectra <- function(file,
"rds" = {
readRDS(file = file) |>
data.frame() |>
Spectra() |>
sanitize_spectra(
cutoff = cutoff,
dalton = dalton,
polarity = polarity,
ppm = ppm
)
Spectra()
}
)
if (sanitize) {
spectra <- spectra |>
sanitize_spectra(
cutoff = cutoff,
dalton = dalton,
polarity = polarity,
ppm = ppm
)
}
return(spectra)
}
9 changes: 2 additions & 7 deletions R/prepare_libraries_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import::from(tidytable, tidytable, .into = environment())
#' @include harmonize_spectra.R
#' @include import_spectra.R
#' @include parse_yaml_paths.R
#' @include sanitize_spectra.R
#'
#' @param input File containing spectra
#' @param polarity MS polarity
Expand Down Expand Up @@ -91,13 +90,9 @@ prepare_libraries_spectra <-
log_debug("Importing")
spectra <- lapply(X = input, FUN = import_spectra)

log_debug("Sanitizing")
spectra_sanitized <- lapply(X = spectra, FUN = sanitize_spectra)
rm(spectra)

log_debug("Extracting")
spectra_extracted <- lapply(X = spectra_sanitized, FUN = extract_spectra)
rm(spectra_sanitized)
spectra_extracted <- lapply(X = spectra, FUN = extract_spectra)
rm(spectra)

log_debug("Harmonizing ...")
log_debug("... pos")
Expand Down
6 changes: 2 additions & 4 deletions R/sanitize_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ sanitize_spectra <-
cutoff = 0,
dalton = 0.01,
polarity = NA,
ppm = 10,
ratio = 10000) {
ppm = 10) {
log_debug("Applying sanitization of the spectra")

if ("msLevel" %in% colnames(spectra@backend@spectraData)) {
Expand Down Expand Up @@ -74,8 +73,7 @@ sanitize_spectra <-
ppm = ppm,
mz = c(">=")
) |>
scalePeaks() |>
filterIntensity(intensity = c(1 / ratio, 1))
scalePeaks()

if ("FEATURE_ID" %in% colnames(spectra@backend@spectraData)) {
message("Combining spectra in case...")
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@
"SystemRequirements": null
},
"keywords": ["metaboliteannotation", "chemotaxonomy", "scoringsystem", "naturalproducts", "computationalmetabolomics", "taxonomicdistance", "specializedmetabolome"],
"fileSize": "3166.925KB",
"fileSize": "3166.651KB",
"citation": [
{
"@type": "ScholarlyArticle",
Expand Down
11 changes: 10 additions & 1 deletion man/import_spectra.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions man/sanitize_spectra.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 85843a1

Please sign in to comment.