Skip to content

Commit

Permalink
v0.4.1, refactor: search_msp for speed
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanbass committed Apr 4, 2024
1 parent 958d186 commit cabe83c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-6175-6739"))
Expand All @@ -27,4 +27,4 @@ Suggests:
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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".
Expand Down
14 changes: 11 additions & 3 deletions R/search_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"){
Expand All @@ -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.
Expand All @@ -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){
Expand All @@ -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, ]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


4 changes: 2 additions & 2 deletions inst/CITATION
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
)
7 changes: 1 addition & 6 deletions man/spectral_similarity.Rd

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

0 comments on commit cabe83c

Please sign in to comment.