%\VignetteIndexEntry{Getting Started DECIPHERing}
%\VignettePackage{DECIPHER}

\documentclass[10pt]{article}

\usepackage{times}
\usepackage{hyperref}
\usepackage{underscore}
\usepackage{enumerate}
\usepackage{graphics}

\textwidth=6.5in
\textheight=8.5in
%\parskip=.3cm
\oddsidemargin=-.1in
\evensidemargin=-.1in
\headheight=-.3in

\newcommand{\scscst}{\scriptscriptstyle}
\newcommand{\scst}{\scriptstyle}

\newcommand{\R}{{\textsf{R}}}
\newcommand{\C}{{\textsf{C}}}
\newcommand{\code}[1]{{\texttt{#1}}}
\newcommand{\term}[1]{{\emph{#1}}}
\newcommand{\Rpackage}[1]{\textsf{#1}}
\newcommand{\Rfunction}[1]{\texttt{#1}}
\newcommand{\Robject}[1]{\texttt{#1}}
\newcommand{\Rclass}[1]{{\textit{#1}}}
\newcommand{\Rmethod}[1]{{\textit{#1}}}
\newcommand{\Rfunarg}[1]{{\textit{#1}}}

\bibliographystyle{plainnat}

\begin{document}
%\setkeys{Gin}{width=0.55\textwidth}

\title{Getting Started DECIPHERing}
\author{Erik S. Wright}
\date{\today}
\maketitle

\newenvironment{centerfig}
{\begin{figure}[htp]\centering}
{\end{figure}}
\renewcommand{\indent}{\hspace*{\tindent}}
\DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em} \DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em} \DefineVerbatimEnvironment{Scode}{Verbatim}{xleftmargin=2em} \fvset{listparameters={\setlength{\topsep}{0pt}}} \renewenvironment{Schunk}{\vspace{\topsep}}{\vspace{\topsep}}
\SweaveOpts{keep.source=TRUE}
<<echo=false>>=
options(continue=" ")
options(width=80)
@

\tableofcontents

%------------------------------------------------------------
\section{About DECIPHER}
%------------------------------------------------------------

\Rpackage{DECIPHER} is a software toolset that can be used for deciphering and managing biological sequences efficiently using the \R{} statistical programming language.  The program features tools falling into five categories:
\begin{itemize}
\item Sequence databases: import, maintain, view, and export a massive number of sequences.
\item Sequence alignment: accurately align thousands of DNA, RNA, or amino acid sequences. Quickly find and align the syntenic regions of multiple genomes.
\item Oligo design: test oligos in silico, or create new primer and probe sequences optimized for a variety of objectives.
\item Manipulate sequences: trim low quality regions, correct frameshifts, reorient nucleotides, determine consensus, or digest with restriction enzymes.
\item Analyze sequences: find chimeras, classify into a taxonomy, predict secondary structure, create phylogenetic trees, and reconstruct ancestral states.
\end{itemize}
\Rpackage{DECIPHER} is available under the terms of the \underline{\href{http://www.gnu.org/copyleft/gpl.html}{GNU Public License version 3}}.

%------------------------------------------------------------
\section{Design Philosophy}
%------------------------------------------------------------

\subsection{Curators Protect the Originals}

One of the core principles of \Rpackage{DECIPHER} is the idea of the non-destructive workflow.  This revolves around the concept that the original sequence information should never be altered:  sequences are exported looking identical to how they were when they were first imported.  Essentially, the sequence information in the database is thought of as a backup of the original sequence file and no function is able to directly alter the sequence data.  All of the workflows simply \term{add} information to the database, which can be used to analyze, organize, and maintain the sequences.  When it comes time to export all or part of the sequences they are preserved in their original state without alteration.

\subsection{Don't Reinvent the Wheel}

\Rpackage{DECIPHER} makes use of the \Rpackage{Biostrings} package that is a core part of the \underline{\href{http://www.bioconductor.org/}{Bioconductor suite}}.  This package contains numerous functions for common operations such as searching, manipulating, and reverse complementing sequences.  Furthermore, \Rpackage{DECIPHER} makes use of the \Rpackage{Biostrings} interface for handling sequence data so that sequences are stored in \Robject{XStringSet} objects.  These objects are compatible with many useful packages in the Bioconductor suite.

A wide variety of user objectives necessitates that \Rpackage{DECIPHER} be extensible to customized projects.  \R{} provides a simple way to place the power of thousands of packages at your fingertips.  Likewise, \R{} enables direct access to the speed and efficiency of the programming language \C{} while maintaining the utility of a scripting language.  Therefore, minimal code is required to solve complex new problems.  Best of all, the \R{} statistical programming language is open source, and maintains a thriving user community so that direct collaboration with other \R{} users is available on \underline{\href{https://stat.ethz.ch/mailman/listinfo}{several Internet forums}}.

\subsection{That Which is the Most Difficult, Make Fastest}

A core objective of \Rpackage{DECIPHER} is to make massive tasks feasible in minimal time.  To this end, many of the most time consuming functions are parallelized to make use of multiple processors.  For example, the function \Rfunction{DistanceMatrix} gets almost a 1x speed boost for each processor core.  A modern processor with 8 cores can see a factor of close to eight times speed improvement.  Similar speedups can be achieved in many other \Rpackage{DECIPHER} functions by setting the \Rfunarg{processors} argument.  This is all made possible through the use of OpenMP in \C{}-level code.

Other time consuming tasks are handled efficiently.  The function \Rfunction{FindChimeras} can uncover sequence chimeras by searching through a reference database of over a million sequences for thousands of 30-mer fragments in a number of minutes.  This incredible feat is accomplished by using the \Rclass{PDict} class provided by \Rpackage{Biostrings}.  Similarly, the \Rfunction{SearchDB} function can obtain the one-in-a-million sequences that match a targeted query in a matter of seconds.  Such high-speed functions enable the user to find solutions to problems that previously would have been extremely difficult or nearly impossible to solve using antiquated methods.

\subsection{Stay Organized}

It is no longer necessary to store related data in several different files.  \Rpackage{DECIPHER} is enabled by \Rpackage{RSQLite}, which is an \R{} interface to \underline{\href{http://www.sqlite.org/}{SQLite databases}}.  \Rpackage{DECIPHER} creates an organized collection of sequences and their associated information known as a sequence database.  \term{SQLite} databases are flat files, meaning they can be handled just like any other file.  There is no setup required since \term{SQLite} does not require a server, unlike many other database engines.  These attributes of \term{SQLite} databases make storing, backing-up, and sharing sequence databases relatively straightforward.

Separate projects can be stored in distinct tables in the same sequence database.  Each new table is structured to include every sequence's description, identifier, and a unique key called the \term{row_name} all in one place.  The sequences are referenced by their \term{row_names} or \term{identifier} throughout most functions in the package.  Using \term{row_names}, new information created with \Rpackage{DECIPHER} functions can be added as additional database columns to their respective sequences' rows in the database table.  To prevent the database from seeming like a black box there is a function named \Rfunction{BrowseDB} that facilitates viewing of the database contents in a web browser.  A similar function is available to view sequences called \Rfunction{BrowseSeqs}.

The amount of DNA sequence information available is currently increasing at a phenomenal rate.  \Rpackage{DECIPHER} stores individual sequences using a custom compression format, called \term{nbit}, so that the database file takes up much less drive space than a standard text file of sequences.  The compressed sequences are stored in a hidden table that is linked to the main information table that the user interacts with regularly.  For example, by default sequence information is stored in the table ``Seqs'', and the associated sequences are stored in the table ``_Seqs''.  Storing the sequences in a separate table greatly improves access speed when there is a large amount of sequence information.  Separating projects into distinct tables further increases query speed over that of storing every project in a single table.

%------------------------------------------------------------
\section{Functionality}
%------------------------------------------------------------

The functions of DECIPHER can be grouped into several categories based on intended use:
\begin{enumerate}
\item Primary functions for interacting with a sequence database:
\begin{enumerate} \item \Rfunction{Add2DB} \item \Rfunction{DB2Seqs} \item \Rfunction{SearchDB} \item \Rfunction{Seqs2DB} \end{enumerate}
\item Secondary functions for typical database tasks:
\begin{enumerate} \item \Rfunction{IdentifyByRank} \item \Rfunction{IdLengths} \end{enumerate}
\item Functions related to forming consensus:
\begin{enumerate} \item \Rfunction{ConsensusSequence} \item \Rfunction{Disambiguate} \item \Rfunction{IdConsensus} \end{enumerate}
\item Phylogenetics and sequence comparison:
\begin{enumerate} \item \Rfunction{DistanceMatrix} \item \Rfunction{IdClusters} \item \Rfunction{MaskAlignment} \item \Rfunction{ReadDendrogram} \item \Rfunction{StaggerAlignment} \item \Rfunction{WriteDendrogram} \end{enumerate}
\item Visualization with a web browser:
\begin{enumerate} \item \Rfunction{BrowseDB} \item \Rfunction{BrowseSeqs} \end{enumerate}
\item Manipulating sequences:
\begin{enumerate} \item \Rfunction{CorrectFrameshifts} \item \Rfunction{OrientNucleotides} \item \Rfunction{RemoveGaps} \item \Rfunction{TrimDNA} \end{enumerate}
\item Analyzing sequences:
\begin{enumerate} \item \Rfunction{DigestDNA} \item \Rfunction{PredictDBN} \item \Rfunction{PredictHEC} \end{enumerate}
\item Multiple sequence alignment:
\begin{enumerate} \item \Rfunction{AdjustAlignment} \item \Rfunction{AlignDB} \item \Rfunction{AlignProfiles} \item \Rfunction{AlignSeqs} \item \Rfunction{AlignTranslation} \end{enumerate}
\item Comparison of sequences that are not collinear (i.e., genomes):
\begin{enumerate} \item \Rfunction{AlignSynteny} \item \Rfunction{FindSynteny} \item \Rclass{Synteny-class} \end{enumerate}
\item Functions related to chimeras (PCR artifacts):
\begin{enumerate} \item \Rfunction{CreateChimeras} \item \Rfunction{FindChimeras} \item \Rfunction{FormGroups} \end{enumerate}
\item Functions related to DNA microarrays:
\begin{enumerate} \item \Rfunction{Array2Matrix} \item \Rfunction{CalculateEfficiencyArray} \item \Rfunction{DesignArray} \item \Rfunction{NNLS} \end{enumerate}
\item Functions related to probes for fluorescence \textit{in situ} hybridization (FISH):
\begin{enumerate} \item \Rfunction{CalculateEfficiencyFISH} \item \Rfunction{DesignProbes} \item \Rfunction{TileSeqs} \end{enumerate}
\item Functions related to primers for polymerase chain reaction (PCR):
\begin{enumerate} \item \Rfunction{AmplifyDNA} \item \Rfunction{CalculateEfficiencyPCR} \item \Rfunction{DesignPrimers} \item \Rfunction{DesignSignatures} \item \Rfunction{MeltDNA} \end{enumerate}
\item Classifying sequences into a taxonomy:
\begin{enumerate} \item \Rfunction{LearnTaxa} \item \Rfunction{IdTaxa} \item \Rclass{Taxa-class} \end{enumerate}
\end{enumerate}

%------------------------------------------------------------
\section{Installation}
%------------------------------------------------------------

\newlength\tindent
\setlength{\tindent}{\parindent}
\setlength{\parindent}{0pt}

\label{sec:Installation}
\subsection{Typical Installation (recommended)}

\begin{enumerate}
\item Install the latest version of \R{} from \underline{\url{http://www.r-project.org/}}.
\item Install \Rpackage{DECIPHER} in R by entering:
\begin{Schunk}
\begin{Sinput}
> if (!requireNamespace("BiocManager", quietly=TRUE))
+     install.packages("BiocManager")
> BiocManager::install("DECIPHER")
\end{Sinput}
\end{Schunk}
\end{enumerate}

\subsection{Manual Installation}

\subsubsection{All platforms}

\begin{enumerate}
\item Install the latest \R{} version from \underline{\url{http://www.r-project.org/}}.
\item Install \Rpackage{Biostrings} in R by entering:
\begin{Schunk}
\begin{Sinput}
> if (!requireNamespace("BiocManager", quietly=TRUE))
+     install.packages("BiocManager")
> BiocManager::install("Biostrings")
\end{Sinput}
\end{Schunk}
\item Install \Rpackage{RSQLite} in R by entering:
\begin{Schunk}
\begin{Sinput}
> install.packages("RSQLite")
\end{Sinput}
\end{Schunk}
\item Download \Rpackage{DECIPHER} from \underline{\url{http://DECIPHER.codes}}.
\end{enumerate}

\subsubsection{Mac OS X}

\begin{enumerate}
\item First install Command Line Tools from Apple, which contains compliers that are required to build packages on the Mac.  Then, in \R{} run:
\begin{Schunk}
\begin{Sinput}
> install.packages("<<path to Mac OS X DECIPHER.tgz>>", repos=NULL)
\end{Sinput}
\end{Schunk}
\end{enumerate}

For parallelization on Mac OS X, extra steps are required to \underline{\url{http://mac.r-project.org/openmp/}{enable OpenMP}} before install.

\subsubsection{Linux}
In a shell enter:
\begin{Schunk}
\begin{Sinput}
R CMD build --no-build-vignettes "<<path to DECIPHER source>>"
R CMD INSTALL "<<path to newly built DECIPHER.tar.gz>>"
\end{Sinput}
\end{Schunk}

\subsubsection{Windows}
Two options are available:  the first is simplest, but requires the pre-built binary (DECIPHER.zip).

\begin{enumerate}
\item First Option:
\begin{Schunk}
\begin{Sinput}
> install.packages("<<path to Windows DECIPHER.zip>>", repos=NULL)
\end{Sinput}
\end{Schunk}
\item Second Option (more difficult):
\begin{enumerate}[(a)]
\item Install Rtools from \underline{\url{http://cran.r-project.org/bin/windows/Rtools/}}.   Be sure to check the box that says edit PATH during installation.
\item Open a MS-DOS command prompt by clicking Start -> All Programs -> Accessories -> Command Prompt.
\item In the command prompt enter:
\begin{Schunk}
\begin{Sinput}
R CMD build --no-build-vignettes "<<path to DECIPHER source>>"
R CMD INSTALL "<<path to newly built DECIPHER.zip>>"
\end{Sinput}
\end{Schunk}
\end{enumerate}
\end{enumerate}

%------------------------------------------------------------
\section{Example Workflow}
%------------------------------------------------------------

To get started we need to load the \Rpackage{DECIPHER} package, which automatically loads several other required packages:

<<startup,results=hide>>=
library(DECIPHER)
@

Help for any function can be accessed through a command such as:

\begin{Schunk}
\begin{Sinput}
> ? DECIPHER
\end{Sinput}
\end{Schunk}

To begin, we can import a FASTA, FASTQ, or GenBank file into a sequence database.  Here we will import the sequences into an in-memory database that will be removed when we disconnect from the database.  In-memory databases are useful for temporary examples, but typically we would specify the path to a file where we want to store the database.  This is especially the case when there are many sequences, as they might not all fit into memory.

In this example, we will use a GenBank file that is included with \Rpackage{DECIPHER} installation.  We must set the sequences' \term{identifier} when importing with \Rfunction{Seqs2DB}.  Here we will identify the sequences with the word ``Bacteria''.  The \term{identifier} is used by many \Rpackage{DECIPHER} functions to reference a specific subset of sequences in the database, and can be reset at a later time using a number of different methods:
<<expr1>>=
# access a sequence file included in the package:
gen <- system.file("extdata", "Bacteria_175seqs.gen", package="DECIPHER")

# connect to a database:
dbConn <- dbConnect(SQLite(), ":memory:")

# import the sequences into the sequence database
Seqs2DB(gen, "GenBank", dbConn, "Bacteria")
@

Now we can view the table of information we just added to the database in a web browser (Fig. 1):
<<expr2>>=
BrowseDB(dbConn)
@

Suppose we wanted to count the number of bases in each sequence and add that information to the database:
<<expr3>>=
l <- IdLengths(dbConn)
head(l)
Add2DB(l, dbConn, verbose=FALSE)
BrowseDB(dbConn, maxChars=20)
@

Next let's identify our sequences by phylum and update this information in the database:
<<expr4>>=
r <- IdentifyByRank(dbConn, level=3, add2tbl=TRUE)
BrowseDB(dbConn, maxChars=20)
@

\begin{figure}
\begin{center}
\includegraphics[width=1\textwidth]{BrowseDBOutput}
\caption{\label{f1} Database table shown in web browser}
\end{center}
\end{figure}

\clearpage

We can now look at only those sequences that belong to the phylum \term{Bacteroidetes} (Fig. 2):
<<expr5>>=
dna <- SearchDB(dbConn, identifier="Bacteroidetes")
BrowseSeqs(subseq(dna, 140, 240))
@

\begin{figure}
\begin{center}
\includegraphics[width=1\textwidth]{BrowseSeqsOutput}
\caption{\label{f2} Sequences shown in web browser}
\end{center}
\end{figure}

\clearpage

Let's construct a phylogenetic tree from the \term{Bacteroidetes} sequences (Fig. 3):
\begin{centerfig}
<<expr6,fig=TRUE,height=3.5,width=6>>=
d <- DistanceMatrix(dna, correction="Jukes-Cantor", verbose=FALSE)
c <- IdClusters(d, method="ML", cutoff=.05, showPlot=TRUE, myXStringSet=dna, verbose=FALSE)
@
\caption{Maximum likelihood tree showing the relationships between sequences.}
\end{centerfig}

\clearpage

Here we created the database in-memory by connecting to ``:memory:'' above.  This works fine for small databases, but it is necessary to initialize larger databases with a quoted file path rather than ``:memory:''.  Optionally, we could use the command below to save an in-memory database to a file for long term storage.  Be sure to change the path names to those on your system by replacing all of the text inside quotes labeled ``$<<$path to ...$>>$'' with the actual path on your system.

\begin{Schunk}
\begin{Sinput}
> sqliteCopyDatabase(dbConn, "<<path to database>>")
\end{Sinput}
\end{Schunk}

Finally, we should disconnect from the database connection.  Since the sequence database was created in temporary memory, all of the information will be erased:
<<expr7>>=
dbDisconnect(dbConn)
@

\section{Session Information}
All of the output in this vignette was produced under the following
conditions:

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

\end{document}
