Skip to content

Commit

Permalink
v0.3.2, updates to ms_tidy_msdial function
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanbass committed Jul 15, 2023
1 parent ee2153e commit 23651e6
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 25 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: msdialreadr
Type: Package
Title: Read and Analyze MS-DIAL Alignment Files
Version: 0.3.1
Version: 0.3.2
Authors@R:
person("Ethan", "Bass", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-6175-6739"))
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# msdialreadr 0.3.2

* Added option to for renaming peaks via `ms_tidy_msdial` by providing a named character vector.
* Deprecated `treatment` argument in `ms_tidy_msdial`, replacing it with new `metadata` argument.

# msdialreadr 0.3.1

* Made rcdk (for rendering structures in `ms_search_gadget`) suggested instead of
Expand Down
13 changes: 9 additions & 4 deletions R/search_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ms_search_spectra <- function(x, db, cols, ..., ri_thresh = 100, spectral_weight
if (missing(parallel)){
parallel <- .Platform$OS.type != "windows"
}
laplee <- laplee <- choose_apply_fnc(progress_bar = progress_bar, cl = mc.cores)
laplee <- choose_apply_fnc(progress_bar = progress_bar, cl = mc.cores)
x$matches[cols] <- laplee(cols, function(col){
try({
sp <- ms_get_spectrum(x, col)
Expand All @@ -71,7 +71,7 @@ ms_search_spectra <- function(x, db, cols, ..., ri_thresh = 100, spectral_weight
sp_score <- search_msp(sp, db[idx], ..., what = "scores", parallel = FALSE)
ri_score <- ri_diff[idx]/ri_thresh
total_score <- sp_score*spectral_weight + ri_score*(1 - spectral_weight)
sel <- order(total_score, decreasing = TRUE)[seq_len(n_results)]
sel <- order(total_score, decreasing = TRUE, method="radix")[seq_len(n_results)]
results <- msp_to_dataframe(db[idx][sel])
results$spectral_match <- sp_score[sel]
results$ri_match <- ri_score[sel]
Expand Down Expand Up @@ -119,8 +119,8 @@ search_msp <- function(x, db, ..., n_results = 10, parallel, mc.cores = 2,
parallel <- FALSE
warning("Parallel processing is not currently available on Windows.")
}
laplee <- laplee <- choose_apply_fnc(progress_bar = progress_bar, cl = mc.cores)
sim <- unlist(laplee(seq_along(db), function(i){
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, ...))
}))
Expand Down Expand Up @@ -149,6 +149,11 @@ msp_to_dataframe <- function(db){
}

#' Calculate spectral similarity between two peaks
#' This function is adapted from the \code{SpectrumSimilarity} function in
#' [OrgMassSpecR](https://orgmassspec.github.io/) but was optimized for increased
#' speed.
#' @author Nathan G. Dodder
#' @author Ethan Bass
#' @noRd
spectral_similarity <- function(spec.top, spec.bottom, t = 0.25, b = 10,
xlim = c(50, 1200), x.threshold = 0){
Expand Down
37 changes: 27 additions & 10 deletions R/tidy_msdial.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
#' Convert peak to tidy format for plotting
#' @param x An MS dial alignment object
#' @param peaks Peaks to include in tidy output
#' @param treatments Treatments to include in tidy output
#' @param x An MS dial alignment object.
#' @param peaks A character vector specifying the peaks to include in tidy output.
#' If the character vector is named, the names of the vector elements will be
#' used in place of the original peak names.
#' @param metadata A character vector specifying the metadata to include in
#' the tidy output.
#' @param treatments This argument is deprecated as of version 0.3.2. It is
#' synonymous with the new metadata argument which should be used instead.
#' @importFrom dplyr select mutate any_of
#' @importFrom tidyr pivot_longer
#' @return If \code{export} is \code{TRUE}, returns spectrum as \code{data.frame}.
#' Otherwise, no return value.
#' @author Ethan Bass
#' @export

ms_tidy_msdial <- function(x, peaks, treatments){
df <- as.data.frame(x$tab)
if (is.numeric(peaks)){
peaks <- colnames(df)[peaks]
ms_tidy_msdial <- function(x, peaks, metadata, treatments){
if (!missing(treatments)){
.Deprecated("metadata", package="msdialreadr", old = "treatments",
msg="The `treatments` argument is deprecated as of msdialreadr v0.3.2.
\t Please use the `metadata` argument instead.")
metadata <- treatments
}
df <- select(df, any_of(peaks))
df <- cbind(df, select(x$sample_meta, any_of(treatments)))
if (!missing(peaks)){
if (is.numeric(peaks)){
peaks <- colnames(df)[peaks]
}
x$tab <- x$tab[,which(colnames(x$tab) %in% peaks), drop = FALSE]
if (!is.null(names(peaks))){
colnames(x$tab) <- names(peaks)
peaks <- names(peaks)
}
x}
df <- as.data.frame(x$tab)
df <- cbind(df, select(x$sample_meta, any_of(metadata)))
df <- mutate(df, sample = row.names(x))
df <- pivot_longer(df, cols=peaks, names_to="peak")
df <- pivot_longer(df, cols = peaks, names_to="peak")
df
}
11 changes: 8 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ choose_apply_fnc <- function(progress_bar, parallel = FALSE, cl = NULL){
fn
}

#' Check for required package

#' Check for suggested package
#' @author Ethan Bass
#' @noRd
check_for_pkg <- function(pkg){
if (!requireNamespace(pkg, quietly = TRUE)) {
check_for_pkg <- function(pkg, return_boolean = FALSE){
pkg_exists <- requireNamespace(pkg, quietly = TRUE)
if (return_boolean){
return(pkg_exists)
} else if (!pkg_exists) {
stop(paste(
"Package", sQuote(pkg), "must be installed to perform this action:
try", paste0("`install.packages('", pkg, "')`.")),
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ Spectra can be plotted using either "base R" graphics or "plotly" graphics using

If you use msdialreadr in published work, please cite it as follows:

Bass, E. (2023). msdialreadr: Read and Analyze MS-DIAL Alignment Files (version 0.3.1).
Bass, E. (2023). msdialreadr: Read and Analyze MS-DIAL Alignment Files (version 0.3.2).
4 changes: 2 additions & 2 deletions inst/CITATION
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ citEntry(
title = "msdialreadr: Read and Analyze MS-DIAL Alignment Files",
author = "Ethan Bass",
year = "2023",
note = "version 0.3.1",
note = "version 0.3.2",
url = "",
doi = "",
textVersion = paste("Bass, E. (2023).",
"msdialreadr: Read and Analyze MS-DIAL Alignment Files (version 0.3.1)."
"msdialreadr: Read and Analyze MS-DIAL Alignment Files (version 0.3.2)."
)
)
14 changes: 10 additions & 4 deletions man/ms_tidy_msdial.Rd

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

0 comments on commit 23651e6

Please sign in to comment.