Skip to content

Commit

Permalink
change name of alignment class to ms_alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanbass committed Nov 16, 2023
1 parent 87bbf63 commit 5fcb854
Show file tree
Hide file tree
Showing 23 changed files with 139 additions and 76 deletions.
12 changes: 6 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Generated by roxygen2: do not edit by hand

S3method(dim,msdial_alignment)
S3method(head,msdial_alignment)
S3method(dim,ms_alignment)
S3method(head,ms_alignment)
S3method(ms_mirror_plot,data.frame)
S3method(ms_mirror_plot,msdial_alignment)
S3method(print,msdial_alignment)
S3method(row.names,msdial_alignment)
S3method(tail,msdial_alignment)
S3method(ms_mirror_plot,ms_alignment)
S3method(print,ms_alignment)
S3method(row.names,ms_alignment)
S3method(tail,ms_alignment)
export(ms_attach_metadata)
export(ms_calculate_RIs)
export(ms_call_msdial)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Updated `ms_filter_alignment` so it filters matches when removing columns from the peak table.
* Added titles to mass spectra plots with peak name and retention index or time.
* Fixed bug in retention index matching algorithm for `ms_search_spectra` function.
* Added `fixed_levels` argument to `ms_reshape_peaktable` so features can be automatically plotted in the order they are provided by the user.
* Changed the name of the `mzinspectr` alignment object from `msdial_alignment` to `ms_alignment`.

# msdialreadr 0.3.3

Expand Down
8 changes: 4 additions & 4 deletions R/attach_metadata.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#' Attach experimental metadata
#'
#' Attaches experimental metadata to `MSDIAL_alignment` object. One of the columns in
#' Attaches experimental metadata to `ms_alignment` object. One of the columns in
#' the supplied metadata must match exactly the row names of the peak table.
#'
#' @aliases attach_metadata
#' @param x An `MSDIAL_alignment` object.
#' @param x A \code{ms_alignment} object.
#' @param metadata A `data.frame` containing the sample metadata.
#' @param col The name of the column containing the sample names.
#' @return A \code{MSDIAL_alignment} object with attached metadata in the \code{
#' @return A \code{ms_alignment} object with attached metadata in the \code{
#' $sample_meta} slot.
#' @author Ethan Bass
#' @export
Expand All @@ -24,7 +24,7 @@ ms_attach_metadata <- function(x, metadata, col){
if (sum((duplicated(metadata[,col]))) > 0)
stop(paste("Sample names must be unique. Please check column", sQuote(col),
"for duplicates."))
if (!inherits(x,"msdial_alignment"))
if (!inherits(x,"ms_alignment"))
stop(paste("Provided peak table object must be of class 'x'."))
meta <- data.frame(rownames(x$tab))
names(meta) <- col
Expand Down
2 changes: 1 addition & 1 deletion R/ms_search_gadget.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Launch MS search gadget for interactive viewing of spectral matches.
#' @name ms_search_gadget
#' @importFrom graphics rasterImage par plot.new
#' @param data An \code{msdial_alignment} object.
#' @param data An \code{ms_alignment} object.
#' @export

ms_search_gadget <- function(data){
Expand Down
28 changes: 14 additions & 14 deletions R/normalization.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#'
#' @description Performs Probabilistic Quotient Normalization on peak table.
#'
#' @param x An \code{msdial_alignment} object or matrix with rows as samples and features as columns.
#' @param x An \code{ms_alignment} object or matrix with rows as samples and features as columns.
#' @param ref Reference for normalization: either \code{median} (default) to use
#' the overall median of variables as the reference, or \code{mean} to use the
#' overall average of variables as the reference.
#' @param QC vector of number(s) to specify samples which average to use as reference
#' (e.g. QC samples)
#'
#' @return A normalized \code{msdial_alignment} object or \code{matrix},
#' @return A normalized \code{ms_alignment} object or \code{matrix},
#' according to the input.
#'
#' @importFrom stats median
Expand All @@ -26,7 +26,7 @@

ms_normalize_pqn <- function(x, ref = c("median", "mean"), QC = NULL) {
ref <- match.arg(ref, c("median","mean"))
if (inherits(x, what = "msdial_alignment")){
if (inherits(x, what = "ms_alignment")){
X <- x$tab
} else if (class(x) %in% c("data.frame","matrix")){
X <- x
Expand Down Expand Up @@ -61,7 +61,7 @@ ms_normalize_pqn <- function(x, ref = c("median", "mean"), QC = NULL) {

X.norm <- as.data.frame(X.norm)

if (inherits(x, what = "msdial_alignment")){
if (inherits(x, what = "ms_alignment")){
x$tab <- X.norm
} else{
x <- X.norm
Expand All @@ -73,13 +73,13 @@ ms_normalize_pqn <- function(x, ref = c("median", "mean"), QC = NULL) {
#'
#' Divides each row by the sum of the features in that row.
#'
#' @param x An \code{msdial_alignment} object or matrix with rows as samples and features as columns.
#' @return A normalized \code{msdial_alignment} object or \code{matrix},
#' @param x An \code{ms_alignment} object or matrix with rows as samples and features as columns.
#' @return A normalized \code{ms_alignment} object or \code{matrix},
#' according to the input.
#' @export

ms_normalize_tsn <- function(x) {
if (inherits(x, what = "msdial_alignment")){
if (inherits(x, what = "ms_alignment")){
X <- x$tab
} else if (class(x) %in% c("data.frame","matrix")){
X <- x
Expand All @@ -92,7 +92,7 @@ ms_normalize_tsn <- function(x) {

X.norm <- as.data.frame(X.norm)

if (inherits(x, what = "msdial_alignment")){
if (inherits(x, what = "ms_alignment")){
x$tab <- X.norm
} else{
x <- X.norm
Expand All @@ -104,18 +104,18 @@ ms_normalize_tsn <- function(x) {
#'
#' Normalize by internal standard.
#'
#' @param x An \code{msdial_alignment} object or matrix with rows as samples and
#' @param x An \code{ms_alignment} object or matrix with rows as samples and
#' features as columns.
#' @param idx Column index of internal standard.
#' @param plot_it Logical. Whether to plot ITSD against total peak area.
#' @importFrom graphics abline legend plot
#' @importFrom stats lm
#' @author Ethan Bass
#' @return A normalized \code{msdial_alignment} object or \code{matrix},
#' @return A normalized \code{ms_alignment} object or \code{matrix},
#' according to the input.
#' @export
ms_normalize_itsd <- function(x, idx, plot_it = FALSE) {
if (inherits(x, what = "msdial_alignment")){
if (inherits(x, what = "ms_alignment")){
X <- x$tab
} else if (class(x) %in% c("data.frame","matrix")){
X <- x
Expand All @@ -141,7 +141,7 @@ ms_normalize_itsd <- function(x, idx, plot_it = FALSE) {

X.norm <- as.data.frame(X.norm)

if (inherits(x, what = "msdial_alignment")){
if (inherits(x, what = "ms_alignment")){
x$tab <- X.norm
} else{
x <- X.norm
Expand All @@ -150,12 +150,12 @@ ms_normalize_itsd <- function(x, idx, plot_it = FALSE) {
}

#' Subtract blanks
#' @param x A \code{msdial_alignment} object.
#' @param x A \code{ms_alignment} object.
#' @param blanks.idx Indices of blank samples
#' @param blanks.pattern A string that uniquely identifies blank samples by name
#' @param what Whether to subtract the mean or median value
#' @param drop Logical. Whether to drop columns containing only zeros. Defaults to TRUE.
#' @return A \code{msdial_alignment} object with the mean or median of the blanks
#' @return A \code{ms_alignment} object with the mean or median of the blanks
#' subtracted from each peak.
#' @export

Expand Down
8 changes: 4 additions & 4 deletions R/plot_spectrum.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ms_plot_spectrum <- function(x, col, plot_labels = TRUE, lab_int = 0.2,
title = TRUE, type = c("plotly", "base"),
scale = FALSE, bar_width = 1, digits = 1, ...){
if (inherits(x,"msdial_alignment")){
if (inherits(x,"ms_alignment")){
xx <- x$peak_meta
} else if (inherits(x, "data.frame") && "EI.spectrum" %in% colnames(x)){
xx <- x
Expand Down Expand Up @@ -152,7 +152,7 @@ ms_mirror_plot.data.frame <- function(x, y, plot_labels = TRUE, type = c("plotly
}

#' Plot two spectra as a mirror plot.
#' @param x A \code{msdial_alignment} object.
#' @param x A \code{ms_alignment} object.
#' @param cols One or more columns in the peak table \code{tab} to plot.
#' @param ref A row in the matches slot corresponding to the provided column.
#' @param type What kind of plot to produce. Either base R (\code{base}) or
Expand All @@ -165,10 +165,10 @@ ms_mirror_plot.data.frame <- function(x, y, plot_labels = TRUE, type = c("plotly
#' @param bar_width Width of bars.
#' @param match_score Logical. Whether to plot match score or not.
#' @rdname ms_mirror_plot
#' @method ms_mirror_plot msdial_alignment
#' @method ms_mirror_plot ms_alignment
#' @export

ms_mirror_plot.msdial_alignment <- function(x, cols, ref, type=c("plotly", "base"),
ms_mirror_plot.ms_alignment <- function(x, cols, ref, type=c("plotly", "base"),
scale = TRUE, plot_labels = TRUE,
lab_int = 0.2, digits = 1,
bar_width = 1, match_score = TRUE, ...){
Expand Down
16 changes: 8 additions & 8 deletions R/read_alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @param format The format of the provided alignment file. Currently, only
#' MS-DIAL '.txt' files are supported (\code{msdial}).
#' @importFrom utils read.csv
#' @return Returns \code{msdial_alignment} object. A list of 3 data.frames,
#' @return Returns \code{ms_alignment} object. A list of 3 data.frames,
#' containing peak data (\code{tab}), peak metadata (\code{peak_meta}) and
#' sample metadata (\code{sample_meta}).
#' @export
Expand All @@ -27,12 +27,12 @@ ms_read_alignment <- function(path, format = c("msdial")){

structure(.Data = list(tab = tab, peak_meta = peak_meta,
sample_meta = data.frame(full.name = rownames(tab))),
class = "msdial_alignment")
class = "ms_alignment")
}


#' Filter alignment by provided indices.
#' @param x An \code{msdial_alignment} object or matrix with rows as samples and features as columns.
#' @param x An \code{ms_alignment} object or matrix with rows as samples and features as columns.
#' @param idx Indices to be retained or excluded according to the value of \code{inverse}.
#' @param what Which dimension to filter on. Either (\code{rows}) or columns
#' (\code{cols}).
Expand Down Expand Up @@ -60,32 +60,32 @@ ms_filter_alignment <- function(x, idx, what=c("rows","cols"), inverse = FALSE){
#' @importFrom utils head
#' @noRd
#' @export
head.msdial_alignment <- function(x,...){
head.ms_alignment <- function(x,...){
head(x$tab)
}

#' @importFrom utils tail
#' @noRd
#' @export
tail.msdial_alignment <- function(x,...){
tail.ms_alignment <- function(x,...){
tail(x$tab)
}

#' @noRd
#' @export
print.msdial_alignment <- function(x, ...){
print.ms_alignment <- function(x, ...){
print(x$tab)
}

#' @noRd
#' @export
dim.msdial_alignment <- function(x){
dim.ms_alignment <- function(x){
dim(x$tab)
}

#' @noRd
#' @export
row.names.msdial_alignment <- function(x){
row.names.ms_alignment <- function(x){
row.names(x$tab)
}

27 changes: 16 additions & 11 deletions R/search_spectra.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Search spectra in MSDIAL alignment against database
#' Search spectra in MS alignment against database
#'
#' This function can be used to identify peaks in a peak table by matching them
#' to a spectral database (\code{db}). It takes several arguments that can
Expand All @@ -11,7 +11,7 @@
#' index similarity) when calculating the the total similarity score, which is
#' used to rank matches.
#'
#' @param x An \code{msdial_alignment} object.
#' @param x An \code{ms_alignment} object.
#' @param db MSP database. The provided object should be a nested list, where the
#' sublists contain the following elements: retention indices in an element named
#' \code{RI} and mass spectra in an element called \code{Spectra}. All other elements
Expand All @@ -32,7 +32,7 @@
#' @param progress_bar Logical. Whether to display progress bar or not.
#' @note See \href{https://github.com/QizhiSu/mspcompiler}{mspcompiler} for help compiling
#' an msp database.
#' @return Returns a modified \code{msdial_alignment} object with database matches
#' @return Returns a modified \code{ms_alignment} object with database matches
#' in the \code{matches} slot as a list of data frames. Each \code{data.frame}
#' will contain the database matches as rows and columns corresponding to the
#' elements of the database entry (e.g. "Name", "InChIKey", etc.) as well as
Expand Down Expand Up @@ -86,7 +86,7 @@ ms_search_spectra <- function(x, db, cols, ..., ri_thresh = 100, spectral_weight
}

#' Get spectrum from MSDIAL alignment object
#' @param x An \code{msdial_alignment} object or matrix with rows as samples and features as columns.
#' @param x An \code{ms_alignment} object or matrix with rows as samples and features as columns.
#' @param col Index of the feature (column).
#' @return Returns spectrum as a data.frame with two columns: "mz" and "intensity".
#' @author Ethan Bass
Expand All @@ -105,7 +105,7 @@ ms_get_spectrum <- function(x, col){
#' @param parallel Logical. Whether to use parallel processing. (This feature
#' does not work on Windows).
#' @param mc.cores How many cores to use for parallel processing? Defaults to 2.
#' @param what What kind of object to return. Either \code{msdial_alignment} object,
#' @param what What kind of object to return. Either \code{ms_alignment} object,
#' (\code{msd}), or \code{data.frame} (\code{df}).
#' @param progress_bar Logical. Whether to display progress bar or not.
#' @importFrom pbapply pblapply
Expand Down Expand Up @@ -151,9 +151,9 @@ 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/) where it is licensed under
#' BSD-2 (© 2011-2017, Nathan Dodder). The function was refactored here for
#' 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
Expand All @@ -167,11 +167,18 @@ msp_to_dataframe <- function(db){
#' identification. Expressed as a percent of the maximum intensity.
#' @param xlim numeric vector of length 2, defining the beginning and ending
#' values of the x-axis.
#' @param x.threshold
#' @param x.threshold numeric value of length 1 specifying the m/z threshold
#' used for the similarity score calculation. Only peaks with m/z values above
#' the threshold are used in the calculation. This can be used to exclude noise
#' and/or non-specific ions at the low end of the spectrum. By default all ions
#' 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){
stop("x.threshold argument must be zero or a positive number")
}
colnames(spec.top) <- c("mz", "intensity")
spec.top$normalized <- round((spec.top$intensity/max(spec.top$intensity)) * 100)
spec.top <- spec.top[which(spec.top$mz >= xlim[1] & spec.top$mz <= xlim[2]),]
Expand All @@ -192,8 +199,6 @@ spectral_similarity <- function(spec.top, spec.bottom, t = 0.25, b = 10,
alignment[, c(2, 3)][is.na(alignment[, c(2, 3)])] <- 0
names(alignment) <- c("mz", "intensity.top", "intensity.bottom")

if (x.threshold < 0)
stop("x.threshold argument must be zero or a positive number")
alignment <- alignment[alignment[, 1] >= x.threshold, ]
u <- alignment[, 2]
v <- alignment[, 3]
Expand Down
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ condense_eispectrum <- function(x){
}

#' Find peak based on retention time and/or mass
#' @param x An \code{msdial_alignment} object
#' @param x An \code{ms_alignment} object
#' @param rt Retention time
#' @param mz Quant.mass
#' @param rt.tol Tolerance for matching retention time
Expand Down Expand Up @@ -98,7 +98,7 @@ ms_rt_to_ri <- function(rts, RIs){
}

#' Convert retention times to retention indices in alignment object.
#' @param x An \code{msdial_alignment} object.
#' @param x An \code{ms_alignment} object.
#' @param Ris A matrix or data.frame containing retention times in column one
#' and retention indices in column two.
#' @export
Expand Down
6 changes: 3 additions & 3 deletions man/ms_attach_metadata.Rd

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

2 changes: 1 addition & 1 deletion man/ms_calculate_RIs.Rd

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

Loading

0 comments on commit 5fcb854

Please sign in to comment.