From cabe83cf89abed914a6309f20f49e30704ba2ff1 Mon Sep 17 00:00:00 2001 From: Ethan Bass Date: Thu, 4 Apr 2024 13:53:34 -0400 Subject: [PATCH] v0.4.1, refactor: search_msp for speed --- DESCRIPTION | 4 ++-- NEWS.md | 5 +++++ R/search_spectra.R | 14 +++++++++++--- README.md | 2 +- inst/CITATION | 4 ++-- man/spectral_similarity.Rd | 7 +------ 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 584b5c5..a6c5be3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: mzinspectr Type: Package Title: Read and Analyze Mass Spectrometry Alignment Files -Version: 0.4.0 +Version: 0.4.1 Authors@R: person("Ethan", "Bass", , "ethanbass@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6175-6739")) @@ -27,4 +27,4 @@ Suggests: License: GPL (>= 3) Encoding: UTF-8 LazyData: true -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.0 diff --git a/NEWS.md b/NEWS.md index 15776b0..7a7e204 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# mzinspectr 0.4.1 + +* Refactored `spectral_similarity` to speed up spectral library search. +* Silenced warnings for malformed database entries in spectral library search. + # mzinspectr 0.4.0 * Changed name of package to "mzinspectr". diff --git a/R/search_spectra.R b/R/search_spectra.R index 9eeb26f..bd3cb8e 100644 --- a/R/search_spectra.R +++ b/R/search_spectra.R @@ -125,7 +125,7 @@ search_msp <- function(x, db, ..., n_results = 10, parallel, mc.cores = 2, laplee <- choose_apply_fnc(progress_bar = progress_bar, cl = mc.cores) sim <- unlist(lapply(seq_along(db), function(i){ db[[i]]$Spectra <- as.data.frame(apply(db[[i]]$Spectra, 2, as.numeric)) - try(spectral_similarity(spec.top = x, spec.bottom = db[[i]]$Spectra, ...)) + try(spectral_similarity(spec.top = x, spec.bottom = db[[i]]$Spectra, ...), silent = TRUE) })) sim <- suppressWarnings(as.numeric(sim)) if (what == "scores"){ @@ -151,10 +151,12 @@ msp_to_dataframe <- function(db){ } #' Calculate spectral similarity between two peaks +#' #' This function is slightly adapted from the \code{SpectrumSimilarity} function #' in [OrgMassSpecR](https://orgmassspec.github.io/) where it is licensed under #' BSD-2 (© 2011-2017, Nathan Dodder). The function was re-factored here for #' increased speed. +#' #' @param spec.top data frame containing the experimental spectrum's peak list #' with the m/z values in the first column and corresponding intensities in the #' second. @@ -174,6 +176,7 @@ msp_to_dataframe <- function(db){ #' are used. #' @author Nathan G. Dodder #' @author Ethan Bass + spectral_similarity <- function(spec.top, spec.bottom, t = 0.25, b = 10, xlim = c(50, 1200), x.threshold = 0){ if (x.threshold < 0){ @@ -193,10 +196,15 @@ spectral_similarity <- function(spec.top, spec.bottom, t = 0.25, b = 10, top[, 1][which(bottom[, 1][i] >= top[,1] - t & bottom[, 1][i] <= top[, 1] + t)] <- bottom[,1][i] } - alignment <- merge(top[,-2], bottom[,-2], by = 1, all = TRUE) + mz <- unique(c(top$mz, bottom$mz)) + alignment <- cbind(mz, x = top[match(mz, top[, "mz"]), "normalized"], + y = bottom[match(mz, bottom[, "mz"]), "normalized"]) + if (length(unique(alignment[, 1])) != length(alignment[, 1])) warning("the m/z tolerance is set too high") - alignment[, c(2, 3)][is.na(alignment[, c(2, 3)])] <- 0 + alignment[, 3][is.na(alignment[, 3])] <- 0 + alignment[, c(2,3)][is.na(alignment[, c(2,3)])] <- 0 + names(alignment) <- c("mz", "intensity.top", "intensity.bottom") alignment <- alignment[alignment[, 1] >= x.threshold, ] diff --git a/README.md b/README.md index c98c969..4b36c13 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,6 @@ Spectra can be plotted using either "base R" graphics or "plotly" graphics using If you use mzinspectr in published work, please cite it as follows: -Bass, E. (2023). mzinspectr: Read and Analyze Mass Spectrometry Alignment Files (version 0.4.0). https://doi.org/10.5281/zenodo.10426253. +Bass, E. (2023). mzinspectr: Read and Analyze Mass Spectrometry Alignment Files (version 0.4.1). https://doi.org/10.5281/zenodo.10426253. diff --git a/inst/CITATION b/inst/CITATION index 5de2527..0ae5316 100644 --- a/inst/CITATION +++ b/inst/CITATION @@ -5,11 +5,11 @@ citEntry( title = "mzinspectr: Read and Analyze Mass Spectrometry Alignment Files", author = "Ethan Bass", year = "2023", - note = "version 0.4.0", + note = "version 0.4.1", url = "", doi = "10.5281/zenodo.10426253", textVersion = paste("Bass, E. (2023).", - "mzinspectr: Read and Analyze Mass Spectrometry Alignment Files (version 0.4.0).", + "mzinspectr: Read and Analyze Mass Spectrometry Alignment Files (version 0.4.1).", "https://doi.org/10.5281/zenodo.10426253." ) ) diff --git a/man/spectral_similarity.Rd b/man/spectral_similarity.Rd index 48b0b39..f144146 100644 --- a/man/spectral_similarity.Rd +++ b/man/spectral_similarity.Rd @@ -2,11 +2,7 @@ % Please edit documentation in R/search_spectra.R \name{spectral_similarity} \alias{spectral_similarity} -\title{Calculate spectral similarity between two peaks -This function is slightly adapted from the \code{SpectrumSimilarity} function -in [OrgMassSpecR](https://orgmassspec.github.io/) where it is licensed under -BSD-2 (© 2011-2017, Nathan Dodder). The function was re-factored here for -increased speed.} +\title{Calculate spectral similarity between two peaks} \usage{ spectral_similarity( spec.top, @@ -42,7 +38,6 @@ and/or non-specific ions at the low end of the spectrum. By default all ions are used.} } \description{ -Calculate spectral similarity between two peaks This function is slightly adapted from the \code{SpectrumSimilarity} function in [OrgMassSpecR](https://orgmassspec.github.io/) where it is licensed under BSD-2 (© 2011-2017, Nathan Dodder). The function was re-factored here for