%\VignetteIndexEntry{The TargetSearch Package}
%\VignetteDepends{TargetSearchData}
%\VignetteKeywords{preprocess, GC-MS, analysis}
%\VignettePackage{TargetSearch}
%\VignetteEngine{knitr::knitr}

\documentclass{article}

<<style-knitr, eval=TRUE, echo=FALSE, results="asis">>=
BiocStyle::latex()
@

\usepackage{booktabs}

\title{The TargetSearch Package}
\author{Alvaro Cuadros-Inostroza}
\author{Jan Lisec}
\author{Henning Redestig}
\author{Matthew A Hannah}
\affil{Max Planck Institute for Molecular Plant Physiology, Potsdam, Germany}

\newcommand{\Rmethod}[1]{{\Rfunction{#1}}}

\begin{document}

\maketitle

\begin{abstract}
This document describes how to use \Biocpkg{TargetSearch} to preprocess
GC-MS data.
\end{abstract}

\packageVersion{\Sexpr{BiocStyle::pkg_ver("TargetSearch")}}

Report issues on \url{https://github.com/acinostroza/TargetSearch/issues}

\newpage

\tableofcontents

\newpage

\section{Supplied files}

This section describes the files that have to be prepared before running
\Biocpkg{TargetSearch}. These comprise a sample definition file, a reference library
file and a retention marker definition file. Throughout this manual, we will
use the example files provided by the package \Biocpkg{TargetSearchData}.

\subsection{NetCDF files and sample File}

\Biocpkg{TargetSearch} can currently read only NetCDF files. Many GC-MS
software packages are able to convert raw chromatograms to NetCDF. Although some
baseline correction functionality is included in \Biocpkg{TargetSearch}, it is
recommended to baseline correct your chromatograms before exporting
to NetCDF to increase processing speed. Please refer to your vendor's software documentation
for further details.

Export the NetCDF files into a convenient location. Then prepare a tab-delimited
text file describing your samples. It must be contain (at least) the two columns
entitled: ``CDF\_FILE'' and ``MEASUREMENT\_DAY'', or, column names that match ``cdf''
and ``day'', respectively. If more than one column matches the pattern, then the
first (leftmost) is taken. Other columns such as sample name,
sample group, treatment, etc. may be additionally included to aid sample sub-setting
and downstream analyses. An example is shown in table~\ref{sample:file}.


\begin{table}[ht]
\begin{tabular}{lll}
\toprule
CDF\_FILE & MEASUREMENT\_DAY & TIME\_POINT \\
\midrule
7235eg08.cdf & 7235 & 1 \\
7235eg11.cdf & 7235 & 1 \\
7235eg26.cdf & 7235 & 1 \\
7235eg04.cdf & 7235 & 3 \\
7235eg30.cdf & 7235 & 3 \\
7235eg32.cdf & 7235 & 3 \\
...&&\\
\bottomrule
\end{tabular}
%\caption{Sample file example, \file{samples.txt}}
\caption{Sample file example}
\label{sample:file}
\end{table}

To import the sample list into R, use the function \Rfunction{ImportSamples()}
specifying the options \Robject{CDFpath} (directory containing the NetCDF files)
and \Robject{RIpath} (directory where the transformed cdf files, containing the
retention index corrected apex data, also called RI-files, will be saved).
This function will warn you if CDF samples are not found in the chosen path.

<<ImportSample>>=
library(TargetSearchData)
library(TargetSearch)
cdf.path <- system.file("gc-ms-data", package = "TargetSearchData")
sample.file <- file.path(cdf.path, "samples.txt")
samples <- ImportSamples(sample.file, CDFpath = cdf.path, RIpath = ".")
@

Alternatively, you could create a \Robject{tsSample} object by using the sample class
methods.

<<ImportSample2>>=
cdffiles <- dir(cdf.path, pattern="cdf$")
# take the measurement day info from the cdf file names.
days <- sub("^([[:digit:]]+).*$","\\1",cdffiles)
# sample names
smp_names <- sub("\\.cdf", "", cdffiles)
# add some sample info
smp_data <- data.frame(CDF_FILE =cdffiles, GROUP = gl(5,3))
# create the sample object
samples <- new("tsSample", Names = smp_names, CDFfiles = cdffiles,
               CDFpath = cdf.path, RIpath = ".", days = days,
               data = smp_data)
@

\subsection{Retention index markers}

To align different chromatograms using RI markers, a tab-delimited definition file
for these markers has to be provided. At a minimum it should contain three columns
specifying the search window (lower and upper threshod) and the fixed standard value
for each marker (table \ref{rim:file}). Further, a characteristic ion mass ($m/z$)
has to be provided either in an additional column or as an argument \Robject{mass}
to function \Rfunction{ImportFameSettings()}.

<<ImportFameSettings>>=
rim.file  <- file.path(cdf.path, "rimLimits.txt")
rimLimits <- ImportFameSettings(rim.file, mass = 87)
@

This will import the limits in \file{rimLimits.txt} file and set the
marker mass to 87 for all markers.

\begin{table}[ht]
\begin{tabular}{rrr}
\toprule
LowerLimit & UpperLimit & RIstandard \\
\midrule
230 & 280 &	262320\\
290	& 340 &	323120\\
350 & 400 &	381020\\
\bottomrule
\end{tabular}
%    \caption{Retention time definition file example, \file{rimLimits.txt}}
    \caption{Retention time definition file example}
\label{rim:file}
\end{table}

If you do not use RI markers, you can skip this part by setting the parameter
\Robject{rimLimits} to \Robject{NULL} in \Rfunction{RIcorrect()} function.
Please note that in this case, no retention time correction will be performed.

To make sure that you have correctly set the time ranges for the RI markers,
the function \Rfunction{checkRimLim()} can be used. This function plots
the $m/z$ of all the markers in one, randomly chosen, sample. Repeat
the function to see other samples. In the figure~\ref{fig:checkRimLim}
a example is shown. The gray area indicates the search window of each
marker.

<<checkRimLim, fig.cap="Example of the RI markers $m/z$ traces in one sample">>=
checkRimLim(samples, rimLimits)
@

\section{Pre-processing}

\subsection{Baseline correction}

\Biocpkg{TargetSearch} expects baseline corrected NetCDF files, which is
usually performed by your GC-MS vendor's software. If that is the case,
you may continue reading the following subsection.

However, if your chromatograms are not baseline corrected, \Biocpkg{TargetSearch}
provides a method implemented in the function \Rfunction{baselineCorrection()}.
This algorithm is based on the work of Chang et al. \cite{chang07} and a description
is found in the \Rfunction{baselineCorrection()} documentation.

There is no need to call this function directly, as it is automatically
called by \Rfunction{RIcorrect()}, which will be explained in the next subsection.

\subsection{Peak detection}

Initially, \Biocpkg{TargetSearch} identifies the local apex intensities
in all chromatograms, finds the retention time (RT) of the RI markers and converts
RT to RI using linear interpolation \cite{VandenDool1963}.
This is done by the function \Rfunction{RIcorrect()}. Parameter to this function are
sample and retention time limits objects, the intensity threshhold (\Rcode{IntThreshold = 100}),
the peak picking method (\Robject{pp.method}) and a \Robject{Window} parameter
that will be used by said method. The function will return a matrix
with the retention times of all RI markers and create a file with all
extracted peaks of the respective NetCDF file, which will we call
\emph{RI file}. This file can be a tab-delimited file (RI file) (old
format) or a custom binary file (default format). The difference between them
is that the binary file allows much faster parsing compared to the other format.

If you chromatograms are not baseline corrected, it is at this point
where you can pass the parameters \Rcode{baseline=TRUE} to \Rfunction{RIcorrect()}.
Additional baseline correction parameters can be passed via
\Robject{baseline.opts}.

<<RIcorrection>>=
RImatrix <- RIcorrect(samples, rimLimits, IntThreshold = 100,
		      pp.method = "smoothing", Window = 15)
@

There are three peak picking methods available: \emph{smoothing} implements the
algorithm used by Tagfinder \cite{Luedemann2008}, \emph{gaussian} uses basically
the same algorithm, but the difference is the smoother is gaussian based, and
the \emph{ppc} algorithm, which is an adaptation of the function \Rfunction{ppc.peaks}
from the package \CRANpkg{ppc}.
\bioccomment{This package seems to be deprecated. It is not in CRAN anymore}.
This method simply searches for local maxima within a given time window.
The \Robject{Window} parameter sets the window size of the chosen
peak picking method in terms of number of scan points.
\warning[Note]{The number of points actually used is $2 \times Window + 1$}.

\subsection{RI correction}

After running \Rfunction{RIcorrect()}, your chromatograms have been already
retention time corrected. However, it is necessary to check that the RI markers
detection was actually correct. To do so, \Biocpkg{TargetSearch} examines
the generated RI matrix \Robject{RImatrix} with
the function \Rfunction{FAMEoutliers()}. It creates a PDF report
of the RI markers including information about possible outliers that the
user can remove or not. Alternatively, RI markers can
be checked manually using the function \Rfunction{plotFAME} (Figure~\ref{fig:plotFAME}).

<<PeakIdentification>>=
outliers <- FAMEoutliers(samples, RImatrix, threshold = 3)
@

Here, \Robject{threshold} sets the number of standard deviations a value has to be
away from the mean before it will be considered an outlier. In this example, however,
no outliers were detected.

<<plotFAME, fig.cap="Retention Index Marker 1.", fig.small='TRUE'>>=
plotFAME(samples, RImatrix, 1)
@

Note that \Biocpkg{TargetSearch} assumes that the RI markers
are injected together with the biological samples. A different approach is to
inject them separately. If this is the case, please look at the vignette
\emph{RI correct extra} and its accompanying file
\file{RetentionIndexCorrection.R} for a workaround.

In case of outliers, there is no single method to fix them as this really depends
on the particular measurement conditions. However, there are few posibilities. i) If the
time deviation from the mean is small, for example, less than one second, as a rule
of thumb the outliers can usually be ignored. ii) if the outliers are due to chromatogram
shift, then it is also fine to ignore them, because that is the point of the markers.
iii) On the contrary, if the shift is because of a problem with the marker itself
(eg, overloaded peak, low intense peak) and \textbf{\emph{not}} because of a chromatogram shift,
then do not ignore the outliers. In this case, you can manually set the retention
time of the outlier to the mean/median, or choose another peak (which is stable across samples)
as a new marker, or even just remove the RI marker from the list.

\section{Library Search}

\subsection{Reference Library File}

The \emph{reference library} file contains the information of the
metabolites or mass spectral tags (MSTs) that will be searched for in
the chromatograms. A public spectra database could be found here
\url{http://gmd.mpimp-golm.mpg.de/} at
\emph{The Golm Metabolome Database} \cite{Kopka2005,Hummel2007,Hummel2013}.

Required information is the metabolite name
(``Name''), expected retention time index (``RI''),
selective masses (``SEL\_MASS''), most abundant masses
(``TOP\_MASS''), spectrum (``SPECTRUM'') and RI deviations
(``Win\_1'', ``Win\_2'', ``Win\_3''). See example in table~\ref{library:file}.
The columns ``Name'' and ``RI'' are mandatory and you have
at least to include one of the columns ``SEL\_MASS'', ``TOP\_MASS''
or ``SPECTRUM'' in the file (see below). The RI deviation columns are
optional.

\begin{table}[ht]
\scalebox{0.85}{%
\begin{tabular}{lllll}
\toprule
Name & RI & Win\_1 & SEL\_MASS & SPECTRUM\\
\midrule
Pyruvic acid    & 222767 & 4000 & 89;115;158;174;189  & 85:7 86:14 87:7 88:5 8... \\
Glycine (2TMS)  & 228554 & 4000 & 86;102;147;176;204  & 86:26 87:19 88:8 89:4...\\
Valine & 271500 & 2000 & 100;144;156;218;246          & 85:8 86:14 87:6 88:5 8...\\
Glycerol (3TMS) & 292183 & 2000 & 103;117;205;293     & 85:14 86:2 87:16 88:13...\\
Leucine         & 306800 & 1500 & 102;158;232;260     & 158:999 159:148 160:45...\\
Isoleucine      & 319900 & 1500 & 102;103;158;163;218 & 90:11 91:2 92:1 93:1 9...\\
Glycine & 325000 & 2000 & 86;100;174;248;276          & 85:6 86:245 87:24 88:12...\\
\bottomrule
\end{tabular}
}%end scalebox%
    \caption{Reference Library example}
\label{library:file}
\end{table}

In this file, masses and intensities must be positive integers. RIs and RI deviations
can be any positive real number. The selective and most abundant masses list
must be delimited by semicolon (;). The spectrum is described by
a list of mass and intensity pair. Every mass-intensity pair is separated by
colon (:) and different pairs are separated by spaces.

The function \Rfunction{ImportLibrary()} imports the reference file.

<<ImportLibrary>>=
lib.file <- file.path(cdf.path, "library.txt")
lib      <- ImportLibrary(lib.file, RI_dev = c(2000,1000,200),
            TopMasses = 15, ExcludeMasses = c(147, 148, 149))
@


Here we set the RI window deviations to 2000, 1000 and 200 RI units. Since ``Win\_1''
column is already in the file, the first value (2000) is ignored. Also, the 15th most
abundant masses are taken but excluding the masses 147, 148 and 149 (common
confounding masses)

\subsection{Library Search Algorithm}

The library search is performed in three steps. First, for every metabolite,
selective masses are searched in a given time window around the expected RI.
This is done by the function \Rfunction{medianRILib()}. This function calculates
the median RI of the selective masses and return new library object with the
updated RI. The time deviation is given either in the library file (column
``Win\_1'') or when the library is imported (see \Rfunction{ImportLibrary()}).


<<LibrarySearch1>>=
lib <- medianRILib(samples, lib)
@

It is also posible to examinate visually the RI deviation of the metabolites
by setting the parameter \Robject{makeReport=TRUE}, which creates a pdf report
like the one shown in figure~\ref{fig:medianLib}. This may help to set or update
the expected RI deviation.

<<medianLib, fig.cap="RI deviation of first 9 metabolites in the library", fig.dim=c(8,8), fig.wide=TRUE, out.width='95%', echo=FALSE>>=
resPeaks <- FindPeaks(RIfiles(samples), refLib(lib, w = 1, sel = TRUE))
plotRIdev(lib, resPeaks, libId = 1:9)
@

In the second step, the function \Rfunction{sampleRI()} searches the selective
masses again, but using the updated RI and the RI deviation defined in the library
object (``Win\_2''). After that, the intensities of the selected masses
are normalised to the median of the day, and then used to extract
other masses with correlated apex profiles. The masses for which
the Pearson correlation coefficient is above \Robject{r\_thres} are
taken as metabolite markers and their RIs are averaged on a per
sample basis. This average RI represents the exact position where
the metabolite elutes in the respective sample, which is returned
in a matrix form.

<<LibrarySearch2>>=
cor_RI <- sampleRI(samples, lib, r_thres = 0.95,
                     method = "dayNorm")
@

The third step will look up for all the masses (selective and most abundant masses)
in all the samples. This is done by the function \Rfunction{peakFind()}. It returns
a \Rclass{tsMSdata} object with the intensities and RI of every mass (rows) and every
sample (columns) that were search for.


<<LibrarySearch3>>=
peakData <- peakFind(samples, lib, cor_RI)
@


The intensity and RI slots can be accessed by using the \Rmethod{Intensity}
and \Rmethod{retIndex} methods. Each slot is a list, where every component of the list
is a matrix, which is related to a metabolite in the library. There should be as
many components as metabolites in the library. In every matrix, columns are
samples and rows are masses.

<<LibrarySearch4>>=
met.RI        <- retIndex(peakData)
met.Intensity <- Intensity(peakData)
# show the intensity values of the first metabolite.
met.Intensity[[1]]
@


\section{Metabolite Profile}

The function \Rfunction{Profile} makes a profile of the MS data by
averaging all the normalised mass intensities whose Pearson coefficient
is greater that \Robject{r\_thresh}.


<<MetaboliteProfile>>=
MetabProfile <- Profile(samples, lib, peakData, r_thres = 0.95,
                      method = "dayNorm")
@


A \Rclass{msProfile} object is returned. The averaged intensities
and RI matrices that can be obtained by \Rmethod{Intensity} and \Rmethod{retIndex}
methods. The profile information is represented by a \Rclass{data.frame}
in the \Rmethod{info} slot (accessible by \Rmethod{profileInfo} method). The
columns are:

\begin{description}

\item[Name]
    The metabolite/analyte name.

\item[Lib\_RI]
    The expected RI (library RI).

\item[Mass\_count]
    The number of correlating masses.

\item[Non\_consecutive\_Mass\_count]
     Same as above, but not counting the consecutive masses.

\item[Sample\_Count\_per\_Mass]
    The number of samples in which a correlating mass was found. The numbers
    are separated by semi-colon (;) and each number corresponds to one correlating
    masses (same order). If all the numbers are the same, only that unique number
    is shown.

\item[Masses]
    The correlating masses.

\item[RI]
    The average RI.

\item[Score\_all\_masses]
    The similarity score calculated using the average intensity of all
	the masses that were searched for, regardless of whether they are
	correlating masses.

\item[Score\_cor\_masses]
    Same as above, but only correlating masses are considered.

\end{description}

As metabolites with similar selective masses and RIs can be
present in metabolite libraries, it is necessary to reduce redundancy.
This is performed by the function \Rfunction{ProfileCleanUp} which selects
peaks for which the RI gap is smaller than \Robject{timeSplit} and
computes the Pearson correlation between them. When two
metabolites within such a time-group are tightly correlated (given by \Robject{r\_thres})
only the one with more correlated masses is retained.

<<MetaboliteProfile2>>=
finalProfile <- ProfileCleanUp(MetabProfile, timeSplit = 500,
                                r_thres = 0.95)
@

The function returns a \Rclass{msProfile} object. The \Rmethod{info} slot
is similar as described above, but extra columns with a ``Cor\_'' preffix
(e.g., ``Cor\_Name'') are included. They provide information about
metabolite redundancy.

\section{Peaks and Spectra Visualisation}

Finally, it may be of interest to check the chromatographic peak of selected
metabolites and compare the median spectra of the metabolites, i.e., the median
intensities of the selected masses across all the samples, with the reference
spectra of the library. There are two functions to do so: \Rfunction{plotPeak}
and \Rfunction{plotSpectra}.

For example, we can compare the median spectrum of ``Valine'' against its
spectrum reference. Here we look for the library index of ``Valine'' and plot the
spectra comparison in a ``head-tail'' plot (figure~\ref{fig:plotSpectra}).

<<plotSpectra, fig.cap="Spectra comparison of ``Valine''">>=
grep("Valine", libName(lib))
plotSpectra(lib, peakData, libId = 3, type = "ht")
@

To look at the chromatographic peak of ``Valine'' in a given sample,
we use the functions \Rfunction{peakCDFextraction} to extract
the raw chromatogram and \Rfunction{plotPeak} to plot the peak (figure~\ref{fig:plotPeak}).

<<plotPeak, fig.cap="Chromatographic peak of Valine.">>=
# we select the first sample
sample.id <- 1
cdf.file  <- file.path(cdf.path, cdffiles[sample.id])
rawpeaks  <- peakCDFextraction(cdf.file)
# which.met=3 (id of Valine)
plotPeak(samples, lib, MetabProfile, rawpeaks, which.smp=sample.id,
    which.met=3, corMass=FALSE)
@

Refer to the documentation of the functions \Rfunction{plotPeak}
and \Rfunction{plotSpectra} for further options not covered here.

\section{TargetSearch GUI}

We provide a grahical user interface intended to facilitate the use
of \Biocpkg{TargetSearch} for users unfamiliar with R. Many parameters that
would be set calling the individual \Biocpkg{TargetSearch} functions as
described in this document can be set here ``in one go'' before running the
complete analysis. A screenshot of the GUI is shown in figure~\ref{fig:05}.

\begin{figure}[htp]
\centering
\includegraphics[width=0.8\textwidth]{tsGUI.png}
\caption{The \Biocpkg{TargetSearch} GUI.}\label{fig:05}
\end{figure}

This is a descript of all the GUI options.

\begin{description}
\item[Working Directory]: Use the \emph{Browse}-button to select the folder
on your hard drive containing all your GC-MS data files. The output of
\Biocpkg{TargetSearch} will be written to this folder too.

\item[File Import]: Clicking \emph{NetCDF Data} or \emph{Apex Data} radio buttons will open
a file select dialog. Choose the files you would like to be processed. You
may check your selection pressing the \emph{Show}-button.

\item[Baseline Correction]: Clicking \emph{on/off} button will perform
baseline correction before peak detection. If selected, the \Robject{threshold}
parameter is a numeric value between 0 and 1. A value of one returns a baseline
above the noise, 0.5 in the middle of the noise and 0 below the noise. See
\Rfunction{baselineCorrection} documentation for further details.

\item[Peak Detection]: \emph{Intensity Counts
threshold} defines the minimum apex intensity incorporated in the analysis.
A value of 1 would include all peaks. \emph{Mass Range} allows to limit the mass
values ($m/z$) to be included in the analysis. \emph{Smoothing} averages raw data
to eliminate some inherent noise leading to multiple peaks otherwise.

\item[Retention Index Correction]: Retention Index Correction is neccessary and
applied only if you supply NetCDF Data (Apex Data contain already Retention
Indices). You may \emph{Load} or \emph{Create} the search windows for your RI-Markers
here.

\item[Library]: A Library (to detect metabolites) usable by \Biocpkg{TargetSearch} contains
at least information about the metabolite `Name', its expected `RI' and the
selective masses in its spectrum `SEL\_MASS'. You may \emph{Load} or \emph{Create} one
yourself using the respective buttons. A more detailed description of
the file formats can be found in \Rfunction{ImportLibrary}.
\emph{Search Windows} refers to the allowed RI deviation of your
metabolites which are narrowed in 3 consecutive searches.
\emph{No. of top masses} is the number of most abundant masses that will be
selected from the spectra, besides the selected masses.
\emph{ExcludeMasses} is only used when masses are obtained from the spectra.
For example, "126,147:149,160" means that the masses 126, 147, 148, 149,
and 160 will be excluded from the analysis (unless they are used as selected
masses).

\item[Normalization]: This selects how the data will be normalized during
the metabolite search. Options are \Robject{dayNorm}, a day based median
normalization, \Robject{medianNorm}, normalization using the median of all the intensities
of a given mass, and \Robject{none}, no normalization at all.

\item[Final Profiles]: Here you may set the parameters used by the functions
\Rfunction{Profile} and \Rfunction{ProfileCleanUp}. \emph{timesplit} sets an
RI window that will be used to look for metabolites that could have been redundanly
identified. \emph{correl. thr.} is the correlation threshold and \emph{min. number
of correlation samples} is a threshold used to make sure that correlations are
computed with at least said number of observations. \emph{prioritization}
is used to resolv ambiguities by taking the metabolite with the most correlating
masses (\emph{corr. mass}) or the highest similarity \emph{score}.
Metabolites with corr. mass and score higher than the thresholds will be
suggested above the one that do not pass the thresholds, which are marked
as ``unidentified.''

\item[Quantification Mass]: This section is used to generate an intensity
matrix based on one quantification mass. The quantification mass can
be specified in the library file (column "QUANT\_MASS") or selected
automatically by choosing \emph{max intensity} or \emph{max observations}.
\emph{max intensity} selects the mass with the highest intensity across the
samples, while \emph{max observation} takes the mass with less missing
values.

\item[Parameters]: You may \emph{Save} the current parameters as an \texttt{*.RData}
file or \emph{Load} previously saved parameters to compare the outcome of different settings or
just repeat the analysis.

\item[Program]: \emph{Run} starts to process all currently selected files using the current
parameters and saving output to \emph{Working Directory}. \emph{Quit} closes the GUI.
\end{description}

\section{Untargeted search}

Although \Biocpkg{TargetSearch} was designed to be targeted oriented, it is possible
to perform untargeted searches. The basic idea is to create a library that contains
evenly distributed ``metabolites'' in time and every ``metabolite'' uses the whole
range of possible masses as selective masses. An example:

<<untargeted1>>=
metRI    <- seq(200000, 300000, by = 5000)
metMZ    <- 85:250
metNames <- paste("Metab",format(metRI,digits=6), sep = "_")
@

Here we define a set of metabolites located every 5000 RI units
from each other in the RI range 200000-300000, with selective masses
in the range of 85-300, and assign them a name. After that, we
create an \Robject{tsLib} object.

<<untargeted2>>=
metLib   <- new("tsLib", Name = metNames, RI = metRI,
     selMass = rep(list(metMZ), length(metRI)), RIdev = c(3000, 1500, 500))
@

Now we can use this library object to perform a targetive search with
this library on the \emph{E. coli} samples as we did before.

<<untargeted3>>=
metLib <- medianRILib(samples, metLib)
metCorRI <- sampleRI(samples, metLib)
metPeakData <- peakFind(samples, metLib, metCorRI)
metProfile <- Profile(samples, metLib, metPeakData)
metFinProf <- ProfileCleanUp(metProfile, timeSplit = 500)
@

The \Robject{metFinProf} object can be used to create
a new library by taking only the metabolites that have, for
example, more than 5 correlating masses and using the correlating
masses as selective ones.

<<untargeted4>>=
sum(  profileInfo(metFinProf)$Mass_count > 5)
tmp   <-  profileInfo(metFinProf)$Mass_count > 5
metRI    <- profileInfo(metFinProf)$RI[tmp]
metNames <-  as.character( profileInfo(metFinProf)$Name[tmp] )
metMZ <- sapply(profileInfo(metFinProf)$Masses[tmp],
	function(x) as.numeric(unlist(strsplit(x,";"))) )
metLib   <- new("tsLib", Name = metNames, RI = metRI,
     selMass = metMZ, RIdev = c(1500, 750, 250))
@

After the new library object is created, the process can be
repeated as shown above.

Finally, by using the function \Rfunction{writeMSP},
it is possible to export the spectrum of the
unknown metabolites to MSP format used by NIST mass spectra
search (\url{http://www.nist.gov/srd/mslist.htm}),
so the unknown spectra can be search
against known metabolite spectra databases.

\bibliography{biblio}

\section{Session info}

Output of \Rfunction{sessionInfo()} on the system on which
this document was compiled.

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

\end{document}

% vim: set ts=4 sw=4 et: