
%\VignetteIndexEntry{Cardinal 2.0: User's Guide}
%\VignetteKeyword{Infrastructure, Bioinformatics, Proteomics, MassSpectrometry, ImagingMassSpectrometry, Clustering, Classification}

\documentclass[a4paper]{article}
\usepackage{caption}
\usepackage{subcaption}


<<eval=TRUE, echo=FALSE, results=tex>>=
BiocStyle::latex()
@

\title{\Rpackage{Cardinal} 2.0: User's Guide}

\author{Kylie A. Bemis}

\begin{document}
\SweaveOpts{concordance=TRUE}

\maketitle

\tableofcontents

\section{Introduction and installation}

<<echo=FALSE,results=hide>>=
library(Cardinal)
options(Cardinal.verbose=FALSE)
options(Cardinal.progress=FALSE)
options(width=100)
register(SerialParam())
@ 


\Rpackage{Cardinal} 2.0 provides new classes and methods for the manipulation, transformation, visualization, and analysis of imaging experiments--specifically mass spectrometry (MS) imaging experiments.

Classes and methods from older versions of Cardinal will continue to be supported; however, new development should focus on the new classes implemented in Cardinal 2.0.

New features include:

\begin{itemize}

\item New imaging data classes such as \Robject{ImagingExperiment}, \Robject{SparseImagingExperiment}, and \Robject{MSImagingExperiment} which will provide better support for larger-than-memory datasets

\item New imaging metadata classes such as \Robject{PositionDataFrame} and \Robject{MassDataFrame} which make it easier to manipulate experimental runs, pixel coordinates, and $m/z$-values by storing them as separate slots rather than ordinary columns

\item New \verb|plot()| and \verb|image()| visualization methods that can handle non-gridded pixel coordinates and allow assigning the resulting plot (and data) to a variable for later re-plotting

\item Support for writing imzML in addition to reading; improved support and options for importing larger-than-memory imzML for both `continuous' and `processed' formats

\item Data manipulation and summarization verbs borrowed from \Rpackage{dplyr} such as \verb|select()|, \verb|filter()|, \verb|mutate()|, and \verb|summarize()| for easier transformation, subsetting, and summarization of imaging datasets

\item Delayed batch processing via a new \verb|process()| method that allows queueing of pre-processing methods such as \verb|normalize()| and \verb|peakPick()| for later parallel execution

\end{itemize}

This document provides an introduction to several new and existing features of \Rpackage{Cardinal}.

\Rpackage{Cardinal} can be installed via the \Rpackage{BiocManager} package.

<<eval=FALSE>>=
install.packages("BiocManager")
BiocManager::install("Cardinal")
@

The same function can be used to update \Rpackage{Cardinal} and other Bioconductor packages.



\section{Data import and export}

In order to be imported into R by \Rpackage{Cardinal}, input data must be in either imzML or Analyze 7.5 format. Data can be loaded into memory, or it can be attached on-disk using the \verb|attach.only=TRUE| argument to the data import function.


\subsection{imzML}

To import or attach imzML, use the \verb|readImzML()| function. Both files must be present in the same folder and have the same name (except for the file extension).

<<eval=FALSE>>=
name <- "common name of your .imzML and .ibd files"
folder <- "/path/to/the/folder/containing/the/files"
data <- readImzML(name, folder, as="MSImagingExperiment")
@

The imzML format is an open standard designed specifically for interchange of mass spectrometry imaging datasets \cite{Schramm}. Many other formats can be converted to imzML with the help of free applications available online. See \url{http://www.imzml.org} for more information and links to free converters.

The imzML format uses two files with extensions `.imzML' and `.ibd' to store data. The former is an XML-based human-readable text file that stores the metadata about the MS imaging experiment. The latter is a binary file storing the $m/z$ and intensity data.

The \verb|as="MSImagingExperiment"| argument is optional and may be used to specify whether to load the data using new Cardinal 2.0 classes (\Robject{MSImagingExperiment}) or legacy Cardinal classes (\Robject{MSImageSet}). Its defaults may change in future versions of Cardinal.

<<eval=FALSE>>=
# import large datasets without loading them into memory
data <- readImzML(name, folder, attach.only=TRUE, as="MSImagingExperiment")
@

Large imzML files can be attached on-disk without fully loading them into memory by using the \verb|attach.only=TRUE| option. Not all \Rpackage{Cardinal} features are supported for on-disk datasets.

Both `continuous' and `processed' imzML format are supported. When using on-disk data with \verb|attach.only=TRUE|, accessing images of `processed' data may be slow (but accessing spectra should remain fast). This is due to the way the data is stored in the imzML file.

<<eval=FALSE>>=
# import 'processed' data between m/z 500 - 600
data <- readImzML(name, folder, mass.range=c(500,600), as="MSImagingExperiment")

# import 'processed' data binned to 100 ppm
data <- readImzML(name, folder, resolution=100, units="ppm", as="MSImagingExperiment")

# import 'processed' data binned to 1 m/z
data <- readImzML(name, folder, resolution=1, units="mz", as="MSImagingExperiment")
@

For `processed' imzML files, there are additional options. The \verb|resolution| and \verb|units| arguments determine how the data is binned. If the data is imported using the new classes (\Robject{MSImagingExperiment}), this can be changed and the spectra re-binned on-the-fly later. Additionally, the \verb|mass.range| argument allows specifying the mass range, which can be computationally efficient. This avoids having to parse this information from the data (which can potentially take a long time for very large datasets).

<<eval=FALSE>>=
writeImzML(data, name, folder, mz.type="64-bit float", intensity.type="32-bit float")
@

The \verb|writeImzML()| function can be used to write an MS imaging dataset to an imzML file. For `processed' data, re-importing the same dataset may result in the spectra being binned differently; however, the underlying data is preserved. Any metadata columns will not be written to the file.

For more information on reading and writing imzML files, see \verb|?readImzML| and \verb|?writeImzML|.

\subsection{Analyze 7.5}

Originally designed for MRI data by the Mayo Clinic, Analyze 7.5 is another common format used for mass spectrometry imaging data.

The Analyze format uses a collection of three files with extensions `.hdr', `.img', and `.t2m' to store data. To read datasets stored in the Analyze format, use the \verb|readAnalyze()| function. All three files must be present in the same folder and have the same name (except for the file extension) for the data to be read properly.

<<eval=FALSE>>=
name <- "common name of your .hdr, .img, and .t2m files"
folder <- "/path/to/the/folder/containing/the/files"
data <- readAnalyze(name, folder, as="MSImagingExperiment")
@

The \verb|as="MSImagingExperiment"| argument is optional may be used to specify whether to load the data using new Cardinal 2.0 classes (\Robject{MSImagingExperiment}) or legacy Cardinal classes (\Robject{MSImageSet}). Its defaults may change in future versions of Cardinal.

<<eval=FALSE>>=
# import large datasets without loading them into memory
data <- readAnalyze(name, folder, attach.only=TRUE, as="MSImagingExperiment")
@

Large Analyze files can be attached on-disk without fully loading them into memory by using the \verb|attach.only=TRUE| option. Not all \Rpackage{Cardinal} features are supported for on-disk datasets.

<<eval=FALSE>>=
writeAnalyze(data, name, folder, intensity.type="16-bit integer")
@

The \verb|writeAnalyze()| function can be used to write an MS imaging dataset to an Analyze 7.5 file. Any metadata columns will not be written to the file.

For more information on reading Analyze files, see \verb|?readAnalyze| and \verb|?writeAnalyze|.

\subsection{Using \Robject{readMSIData()} and \Robject{writeMSIData()}}

<<eval=FALSE>>=
file <- "/path/to/an/imaging/data/file.extension"
data <- readMSIData(file, as="MSImagingExperiment")
@

\Rpackage{Cardinal} also provides the convenience functions \verb|readMSIData()| and \verb|writeMSIData()|, which will attempt to automatically infer the correct function to use from the provided file extensions. The same rules for naming conventions apply as described above, but one need only provide the path to any of the data files. For example, to read/write an Analyze file, providing a path containing any of `.hdr', `.img', or `.t2m'. Likewise, to read or write imzML, provide a path containing either `.imzML' or `.ibd'.

Any additional arguments will be passed to the appropriate underlying read/write functions.



\section{Components of an imaging experiment}

In \Rpackage{Cardinal}, imaging experiment datasets are composed of multiple sets of metadata, in addition to the actual experimental data. These are (1) pixel metadata, (2) feature ($m/z$) metadata, (3) the actual imaging data, and (4) a class that holds all of these and represents the experiment as a whole.

Unlike many software packages designed for analysis of MS imaging experiments, \Rpackage{Cardinal} is designed to work with multiple datasets and incorporate all aspects of experimental design and metadata.

This section will discuss in detail each component of an imaging dataset in \Rpackage{Cardinal}, assuming the context of MS. If you would like to jump straight into working with a dataset, you may skip ahead to the next section and return here when you have questions on particular aspects of an imaging dataset.



\subsection{Metadata with \Robject{XDataFrame}}

For storing metadata related to pixels (i.e., individual mass spectra, their coordinates, etc.), and features (i.e., $m/z$-values, peaks, proteins, lipids, etc.), \Rpackage{Cardinal} extends the \Robject{DataFrame} object from the \Rpackage{S4Vectors} package.

The \Robject{XDataFrame} class is a data frame class with eXtra "slot-columns" for storing additional metadata columns separately from ordinary columns. These additional "slot-columns" may be required to obey special rules not required of other columns.

<<>>=
xdf <- XDataFrame(a=1:10, b=letters[1:10])
xdf
@

An ordinary \Robject{XDataFrame} behaves the same as an ordinary data frame. It exists primarily to allow shared behavior for its sub-classes \Robject{PositionDataFrame} and \Robject{MassDataFrame}.


\subsubsection{Pixel metadata with \Robject{PositionDataFrame}}

The \Robject{PositionDataFrame} class provides extra columns for storing spatial coordinates and identifying experimental runs.

<<>>=
coord <- expand.grid(x=1:9, y=1:9)
run <- factor(rep("Run 1", nrow(coord)))
pid <- seq_len(nrow(coord))

pdata <- PositionDataFrame(run=run, coord=coord, pid=pid)
pdata
@

A \Robject{PositionDataFrame} can be created with any number of ordinary columns (or none), but the \verb|run| and \verb|coord| must always be provided.

<<>>=
head(run(pdata))
@

The \verb|run| slot-column, accessed via \verb|run()|, uniquely identifies experimental runs. It must be a \Robject{factor}. It is analogous to the "sample" column in the legacy \Robject{IAnnotatedDataFrame} class from older versions of Cardinal, but with a less-confusing name. For MS imaging experiments, this typically identifies data gathered from unique slides.

<<>>=
coord(pdata)
@

The \verb|coord| slot-columns, accessed via \verb|coord()|, provides the spatial coordinates of pixels in an imaging experiment. It must be a \Robject{DataFrame} with numeric columns. The spatial coordinates do not need to be unique; they do not need to be integers; they may also be redundant with other columns such as \verb|run|.

<<>>=
gridded(pdata)
resolution(pdata)
@

The \Robject{PositionDataFrame} object provides several additional methods useful for plotting and manipulating imaging data. The \verb|gridded()| and \verb|resolution()| return whether the pixels lie on a gridded raster and their spatial resolution, which are calculated automatically from \verb|coord|.

See \verb|?PositionDataFrame| for additional methods.



\subsubsection{Feature metadata with \Robject{MassDataFrame}}

The \Robject{MassDataFrame} class provides extra columns for storing the $m/z$-values associated with mass spectral features.

<<>>=
mz <- seq(from=500, to=600, by=0.2)
fid <- seq_along(mz)

fdata <- MassDataFrame(mz=mz, fid=fid)
fdata
@

A \Robject{MassDataFrame} can be created with any number of ordinary columns (or none), but the \verb|mz| column must always be provided.

<<>>=
head(mz(fdata))
resolution(fdata)
@

The $m/z$-values can be accessed via \verb|mz()|. They must be a non-negative numeric vector sorted in increasing order. An approximate $m/z$ resolution can be accessed via \verb|resolution()|.



\subsection{Image data with \Robject{ImageArrayList}}

The \Robject{ImageList} and \Robject{ImageArrayList} classes are list-like classes used to store the actual imaging data. \Robject{ImageList} allows elements of type, as long as they are array-like (i.e., have a \verb|dim| attribute). \Robject{ImageArrayList} further imposes that each element has the same number of dimensions--specifically, the first two dimensions are treated as "rows" and "columns", and these two dimensions must be equal for all elements.

<<>>=
set.seed(1)
data0 <- generateSpectrum(nrow(pdata), range=c(500, 600), peaks=3,
  baseline=3000, noise=0.01, sd=0.5, resolution=300, step=0.2)
data1 <- generateSpectrum(nrow(pdata), range=c(500, 600), peaks=3,
  baseline=3000, noise=0.01, sd=0.5, resolution=300, step=0.2)
idata <- ImageArrayList(list(data0=data0$x, data1=data1$x))
idata
@

An \Robject{ImageArrayList} can be created with a constructor of the same name. It takes a list of data elements.

<<>>=
dim(idata[[1]])
dim(idata[["data1"]])
@

The data elements of an \Robject{ImageArrayList} can be accessed via subsetting with double-brackets like an ordinary list.



\subsection{MS imaging experiments with \Robject{MSImagingExperiment}}

The \Robject{MSImagingExperiment} class represents MS imaging experiments as a whole, and brings together the pixel metadata, feature ($m/z$) metadata, and experimental imaging data.

In the long-term, it is intended to replace the legacy \Robject{MSImageSet} class from older versions of Cardinal. \Robject{MSImagingExperiment} is designed to be more flexible and provide better support for large datasets.

If needed, the \Robject{SparseImagingExperiment} superclass provides a more general interface for working with imaging experiments, not specific to MS.

<<>>=
msdata <- MSImagingExperiment(imageData=idata, featureData=fdata, pixelData=pdata)
msdata
@

The individual components can be accessed via the \verb|pixelData()|, \verb|featureData()|, and \verb|imageData()| methods.

<<>>=
imageData(msdata)
pixelData(msdata)
featureData(msdata)
@

While \verb|pData()| and \verb|fData()| can be used as shortcuts for \verb|pixelData()| and \verb|featureData()|, as in older versions of Cardinal, the \verb|iData()| function for accessing the imaging data works slightly differently.

<<>>=
dim(iData(msdata))
dim(iData(msdata, 1))
dim(iData(msdata, "data0"))
dim(spectra(msdata))
@

The \verb|iData()| function directly accesses the data elements of \verb|imageData()|. By default, it returns the first dataset. It can take an additional argument to specify which dataset to return. The \verb|spectra()| function is an analog for it for \Robject{MSImagingExperiment} objects.

\Rpackage{Cardinal} provides a few subclasses of \Robject{MSImagingExperiment} that are specialized for particular kinds of data: \Robject{MSContinuousImagingExperiment} and \Robject{MSProcessedImagingExperiment}.



\subsubsection{Continuous MS imaging experiments}

The \Robject{MSContinuousImagingExperiment} subclass is specialized for MS imaging datasets where the spectra are stored in a dense matrix, either in-memory or on-disk. This includes MS experiments loaded from `continuous' imzML files.

An \Robject{MSContinuousImagingExperiment} object is created automatically from the \verb|MSImagingExperiment()| constructor function when provided a dense R matrix or \Robject{matter\_matc} matrix.

<<>>=
msdata0 <- MSImagingExperiment(imageData=data0$x, featureData=fdata, pixelData=pdata)
msdata0
@

This class works nearly identically to \Robject{MSImagingExperiment}, but methods written for it can assume that the data is stored densely, and both spectra and images can be accessed relatively quickly and efficiently.



\subsubsection{Processed MS imaging experiments}

The \Robject{MSProcessedImagingExperiment} subclass is specialized for MS imaging datasets where the spectra are stored sparsely, either in-memory or on-disk. This includes MS experiments loaded from `processed' imzML files.

An \Robject{MSProcessedImagingExperiment} object is created automatically from the \verb|MSImagingExperiment()| constructor function when provided a \Robject{sparse\_matc} matrix.

<<>>=
t <- matter::rep_vt(list(data1$t), ncol(data1$x))
x <- lapply(1:ncol(data1$x), function(i) data1$x[,i])
data1b <- matter::sparse_mat(data=list(keys=t, values=x),
  nrow=length(t[[1]]), ncol=length(x), keys=t[[1]])
@

A \Robject{sparse\_matc} matrix is a sparse matrix as implemented by the \Rpackage{matter} package. Each column is sparse and are stored as key-value pairs, which may either be an R list in-memory, or a \Robject{matter\_list} stored on-disk.

<<>>=
msdata1 <- MSImagingExperiment(imageData=data1b, featureData=fdata, pixelData=pdata)
msdata1
@

This class works very similarly to \Robject{MSImagingExperiment}, but methods written for it should assume that the data is stored sparsely, so spectra can be accessed relatively quickly and efficiently, but images may be accessed more slowly.

Additionally, the original, observed $m/z$-values are stored as the keys for reconstructing the spectra. They may not match the canonical vector of $m/z$-values accessed via \verb|mz()|. This means that spectra are binned on-the-fly.

The data accessed via \verb|iData()| and \verb|spectra()| are the binned data. The original, observed $m/z$-values and intensities can be accessed via \verb|mzData()| and \verb|peakData()|.

<<>>=
head(mzData(msdata1)[[1]]) # m/z of spectrum 1
head(peakData(msdata1)[[1]]) # intensities of spectrum 1
head(mzData(msdata1)[[2]]) # m/z of spectrum 2
head(peakData(msdata1)[[2]]) # intensities of spectrum 2
@

Parameters for how the data are binned for \Robject{MSProcessedImagingExperiment} objects can be changed with the \verb|tolerance()| method.



\section{Data manipulation and transformation}

\Rpackage{Cardinal} provides a number of methods for manipulation and transformation of imaging datasets. They are described below.



\subsection{Subsetting and combining imaging experiments}

\Robject{MSImagingExperiment} objects can be subset as a whole using the standard semantics of R, where the "rows" are the mass features, and the "columns" are the pixels.

<<>>=
msdata[1:10,]
msdata[,1:10]
@

Datasets can likewise be combined using \verb|rbind()| and \verb|cbind()|, where the "rows" are the mass features, and the "columns" are the pixels.

<<>>=
cbind(msdata0, msdata1)
@

For \verb|cbind()|, the mass features (i.e., the $m/z$-values) must match between all of the datasets. For \verb|rbind()|, the run information and spatial coordinates must match.

Several data manipulation verbs are borrowed from the \Rpackage{dplyr} package for subsetting imaging experiments as well.

<<>>=
select(msdata, x < 4, y < 4) # select based on pixels
filter(msdata, mz < 550) # filter based on m/z features
@

The \verb|select()| function subsets based on pixels ("columns") of the imaging dataset.

The \verb|filter()| function subsets based on the mass features ("rows") of the imaging dataset.

Similar to those functions from \Rpackage{dplyr}, the names of metadata columns such as `mz' can be used literally. They can also be chained together via the \verb|%>%| operator.

<<>>=
msdata %>%
  select(x < 5, y < 5) %>%
  filter(mz > 525)
@




\subsection{Using \Robject{pixelApply()} and \Robject{featureApply()}}

The \verb|pixelApply()| and \verb|featureApply()| functions apply functions over pixels (i.e., mass spectra) or over features (i.e., flattened images). When applied to new \Rpackage{Cardinal} 2.0 classes, these can be executed in parallel.

<<>>=
tic <- pixelApply(msdata, sum, BPPARAM=SerialParam()) # calculate TIC
head(tic)
ms <- featureApply(msdata, mean, BPPARAM=SerialParam()) # calculate mean spectrum
head(ms)
@

Both functions take an argument \verb|BPPARAM| which will be passed to the \verb|bplapply()| function. By default, the registered parallel backend will be used. Otherwise, the specified backend will be used.

Note that for true parallel backends (i.e., not \verb|SerialParam()|), \Rpackage{Cardinal} cannot print a progress bar to the console. A progress bar must be specified as part of the parallel backend, which will be updated according to the number of workers (rather than the number of iterations).

See \verb|?pixelApply| for more details on how the function is applied, and other avaialble options to these functions. See \verb|?bplapply| for more information on the parallel backends.

\subsection{Summarization of imaging experiments}

The \verb|summarize()| function allows efficient summarization over an imaging dataset. Summarization can be applied over either pixels or features. Internally, it is implemented using \verb|pixelApply()| and \verb|featureApply()|.

<<>>=
summarize(msdata, sum, .by="pixel") # calculate TIC
summarize(msdata, .stat="mean") # calculate mean spectrum
@

Either a summary function(s) can be provided explicitly, or a small number of statistics can be chosen from the \verb|.stat| argument. 

Statistics calculated via the \verb|.stat| argument will be calculated efficiently according to the format of the data, regardless of the desired direction of iteration.





\section{Visualization}

As in previous versions of \Rpackage{Cardinal}, the \verb|plot()| function is used to visualize mass spectra, and the \verb|image()| function is used to visualize molecular ion images.

The primary differences between these methods when used on older classes and the new classes are:

\begin{itemize}

\item A new default color scale for images that doesn't use the rainbow color scheme

\item Non-gridded pixel coordinates are allowed to better allow for non-rastered image data

\item The output visualization can be assigned to a variable for later re-plotting

\end{itemize}




\subsection{Visualizing mass spectra with \Robject{plot()}}

The \verb|plot()| method is used for plotting mass spectra.

<<plot0>>=
plot(msdata, pixel=1)
@

<<plot1>>=
plot(msdata, coord=list(x=2, y=2))
@

\setkeys{Gin}{width=\textwidth}
\begin{figure}[h]
\centering
\begin{subfigure}{.45\textwidth}
  \centering
<<fig=TRUE, echo=FALSE>>=
<<plot0>>
@
\caption{\small Plot of pixel = 1}
\label{fig:plot0}
\end{subfigure}
\begin{subfigure}{.45\textwidth}
  \centering
<<fig=TRUE, echo=FALSE>>=
<<plot1>>
@
\caption{\small Plot of x = 2, y = 2}
\label{fig:plot1}
\end{subfigure}
\end{figure}

This section will be expanded in the future. See the original ``Cardinal walkthrough'' vignette for additional information on plotting options.




\subsection{Visualizing molecular ion images with \Robject{image()}}

The \verb|image()| method is used for plotting molecular ion images.

<<image0>>=
image(msdata, feature=1)
@

<<image1>>=
image(msdata, mz=550, plusminus=0.5)
@

\setkeys{Gin}{width=\textwidth}
\begin{figure}[h]
\centering
\begin{subfigure}{.45\textwidth}
  \centering
<<fig=TRUE, echo=FALSE>>=
<<image0>>
@
\caption{\small Image of feature = 1}
\label{fig:image0}
\end{subfigure}
\begin{subfigure}{.45\textwidth}
  \centering
<<fig=TRUE, echo=FALSE>>=
<<image1>>
@
\caption{\small Image of $m/z$ = 550}
\label{fig:image1}
\end{subfigure}
\end{figure}

This section will be expanded in the future. See the original ``Cardinal walkthrough'' vignette for additional information on plotting options.




\subsection{Region-of-interest selection}

The \verb|selectROI()| function is used to interactively select regions-of-interest. See \verb|?selectROI| for details.





\section{Processing}

The pre-processing workflow has been overhauled in \Rpackage{Cardinal} 2.0 to support more efficient processing of larger-than-memory datasets.




\subsection{Queueing delayed processing with \Robject{process()}}

The \verb|process()| method allows queueing of delayed pre-processing steps to an imaging dataset. It expects a function that takes a vector (e.g., a mass spectra), processes it, and returns a vector of the same length as the original vector.

At its simplest, this can be used to immediately apply a transformation function to each spectrum in an MS imaging dataset.

<<>>=
tmp <- process(msdata, function(x) x + 1, label="add1")
tmp
@

By supplying the argument \verb|delay=TRUE|, the transformation function is instead queued, but not applied. This can be used to add multiple transformation functions to the queue. These are unaffected by additional transformations, such as subsetting the dataset.

A call to \verb|process()| without \verb|delay=TRUE| will apply all of the queued processing functions to the dataset.

<<>>=
tmp <- msdata %>%
  process(function(x) ifelse(x > 0, x, 0), label="pos", delay=TRUE) %>%
  process(function(x) x + 1, label="add1", delay=TRUE) %>%
  process(log2, label="log2", delay=TRUE) %>%
  select(x <= 4, y <= 4) %>%
  filter(mz < 550)

process(tmp, BPPARAM=SerialParam())
@

Internally, \verb|process()| applies the processing functions using \verb|pixelApply()| or \verb|featureApply()|, so it will be executed in parallel if a parallel backend is registered. Use the \verb|BPPARAM| argument to specify another parallel backend.




\subsection{Example processing workflow}

When applied to new \Rpackage{Cardinal} classes such as \Robject{MSImagingExperiment}, processing methods such as \verb|smoothSignal()|, \verb|reduceBaseline()|, and \verb|peakPick()| are queued to the dataset. They are applied the next time \verb|process()| is called.

<<process0, eval=FALSE>>=
tmp <- msdata %>%
  smoothSignal() %>%
  reduceBaseline() %>%
  peakPick() %>%
  peakFilter() %>%
  select(x == 1, y == 1) %>%
  process(plot=TRUE,
    par=list(layout=c(1,3)),
    BPPARAM=SerialParam())
@

\setkeys{Gin}{width=\textwidth}
\begin{figure}[h]
\centering
<<fig=TRUE, echo=FALSE>>=
<<process0>>
@
\caption{\small Example processing workflow}
\label{fig:process0}
\end{figure}

\Rpackage{Cardinal} 2.0 implements the following pre-processing methods for \Robject{MSImagingExperiment}:

\begin{itemize}

\item \verb|normalize()| performs normalization, including TIC normalization

\item \verb|smoothSignal()| performs smoothing, to reduce noise in the spectra

\item \verb|reduceBaseline()| performs baseline reduction

\item \verb|peakPick()| detects peaks

\item \verb|peakAlign()| aligns peaks to a set of reference peaks

\item \verb|peakFilter()| removes peaks according to criteria such as peak frequency

\item \verb|peakBin()| bins spectra to a set of reference peaks

\end{itemize}

This section will be expanded in the future. See the help pages of these functions for more details on their use.




% \subsection{Smoothing spectra}

% \subsection{Baseline reduction}

% \subsection{Normalization}

% \subsection{Peak detection and alignment}

% \subsubsection{Peak picking}

% \subsubsection{Peak alignment}

% \subsubsection{Peak filtering}

% \subsubsection{Peak binning}




\section{Analysis}

Many of the analysis methods in \Rpackage{Cardinal} still need to be updated to work with the new classes. To continue using the analysis methods designed for older classes, an \Robject{MSImageExperiment} object can be coerced to a \Robject{MSImageSet} object using the \verb|as()| method.

<<>>=
msdata0b <- as(msdata0, "MSImageSet")
msdata0b
@

\Robject{MSImageSet} objects can be coerced to the newer \Robject{MSImageExperiment} objects as well.

<<>>=
msdata0c <- as(msdata0b, "MSImagingExperiment")
msdata0c
@

See the vignettes from the \Rpackage{CardinalWorkflows} package for a more in-depth walkthrough of Cardinal's analytic methods on real experimental data.




\section{Session info}

<<results=tex, echo=FALSE>>=
toLatex(sessionInfo())
@

% \bibliographystyle{unsrt}
\bibliography{Cardinal}

\end{document}
