%\VignetteIndexEntry{Multiple Alignments}
%\VignetteKeywords{DNA, RNA, Sequence, Biostrings, Sequence alignment}
%\VignettePackage{Biostrings}


\documentclass[10pt]{article}

\usepackage{times}
\usepackage{hyperref}

\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{\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{MultipleAlignment Objects}
\author{Marc Carlson\\
  Bioconductor Core Team \\
  Fred Hutchinson Cancer Research Center \\
  Seattle, WA}
\date{\today}
\maketitle

\tableofcontents

\section{Introduction}
The \Rclass{DNAMultipleAlignment} \Rclass{RNAMultipleAlignment} and
\Rclass{AAMultipleAlignment} classes allow users to represent groups
of aligned DNA, RNA or amino acid sequences as a single object.  The
frame of reference for aligned sequences is static, so manipulation of
these objects is confined to be non-destructive.  In practice, this
means that these objects contain slots to mask ranges of rows and
columns on the original sequence.  These masks are then respected by
methods that manipulate and display the objects, allowing the user to
remove or expose columns and rows without invalidating the original
alignment.

\section{Creation and masking}
To create a \Rclass{MultipleAlignment}, call the appropriate read
function to read in and parse the original alignment.  There are
functions to read clustaW, Phylip and Stolkholm data formats.

<<objectCreation>>=
library(Biostrings)
origMAlign <-
  readDNAMultipleAlignment(filepath =
                           system.file("extdata",
                                       "msx2_mRNA.aln",
                                       package="Biostrings"),
                           format="clustal")

phylipMAlign <-
  readAAMultipleAlignment(filepath =
                          system.file("extdata",
                                      "Phylip.txt",
                                      package="Biostrings"),
                          format="phylip")
@

Rows can be renamed with \Rfunction{rownames}.

<<renameRows>>=
rownames(origMAlign)
rownames(origMAlign) <- c("Human","Chimp","Cow","Mouse","Rat",
                          "Dog","Chicken","Salmon")
origMAlign
@ 

To see a more detailed version of your \Rclass{MultipleAlignment}
object, you can use the \Rmethod{detail} method, which will show
the details of the alignment interleaved and without the rows and
columns that you have masked out.

<<detail, eval=FALSE>>=
detail(origMAlign)
@ 

Applying masks is a simple matter of specifying which ranges to hide.

<<usingMasks>>=
maskTest <- origMAlign
rowmask(maskTest) <- IRanges(start=1,end=3)
rowmask(maskTest)
maskTest

colmask(maskTest) <- IRanges(start=c(1,1000),end=c(500,2343))
colmask(maskTest)
maskTest
@ 

Remove row and column masks by assigning \code{NULL}:

<<nullOut masks>>=
rowmask(maskTest) <- NULL
rowmask(maskTest)
colmask(maskTest) <- NULL
colmask(maskTest)
maskTest
@ 

When setting a mask, you might want to specify the rows or columns to
keep, rather than to hide.  To do that, use the \Rfunarg{invert}
argument.  Taking the above example, we can set the exact same masks
as before by specifying their inverse and using \code{invert=TRUE}.

<<invertMask>>=
rowmask(maskTest, invert=TRUE) <- IRanges(start=4,end=8)
rowmask(maskTest)
maskTest
colmask(maskTest, invert=TRUE) <- IRanges(start=501,end=999)
colmask(maskTest)
maskTest
@ 

In addition to being able to invert these masks, you can also choose
the way in which the ranges you provide will be merged with any
existing masks. The \Rfunarg{append} argument allows you to specify
the way in which new mask ranges will interact with any existing
masks.  By default, these masks will be the "union" of the new mask
and any existing masks, but you can also specify that these masks be
the mask that results from when you "intersect" the current mask and
the new mask, or that the new mask simply "replace" the current mask.
The \Rfunarg{append} argument can be used in combination with the
\Rfunarg{invert} argument to make things even more interesting.  In
this case, the inversion of the mask will happen before it is combined
with the existing mask.  For simplicity, I will only demonstrate this
on \Rmethod{rowmask}, but it also works for \Rmethod{colmask}.  Before
we begin, lets set the masks back to being NULL again.

<<setup>>=
## 1st lets null out the masks so we can have a fresh start.
colmask(maskTest) <- NULL
rowmask(maskTest) <- NULL
@ 

Then we can do a series of examples, starting with the default which
uses the "union" value for the \Rfunarg{append} argument.

<<appendMask>>=
## Then we can demonstrate how the append argument works
rowmask(maskTest) <- IRanges(start=1,end=3)
maskTest

rowmask(maskTest,append="intersect") <- IRanges(start=2,end=5)
maskTest

rowmask(maskTest,append="replace") <- IRanges(start=5,end=8)
maskTest

rowmask(maskTest,append="replace",invert=TRUE) <- IRanges(start=5,end=8)
maskTest

rowmask(maskTest,append="union") <- IRanges(start=7,end=8)
maskTest
@ 

The function \Rfunction{maskMotif} works on \Rclass{MultipleAlignment}
objects too, and takes the same arguments that it does
elsewhere. \Rfunction{maskMotif} is useful for masking occurances of a
string from columns where it is present in the consensus sequence.

<<maskMotif>>= 
tataMasked <- maskMotif(origMAlign, "TATA")
colmask(tataMasked)
@ 

\Rfunction{maskGaps} also operates on columns and will mask collumns
based on the fraction of each column that contains gaps
\Rfunarg{min.fraction} along with the width of columns that contain
this fraction of gaps \Rfunarg{min.block.width}.


<<maskGaps>>=
autoMasked <- maskGaps(origMAlign, min.fraction=0.5, min.block.width=4)
autoMasked
@ 

Sometimes you may want to cast your \Rclass{MultipleAlignment} to be a
matrix for usage eslewhere.  \Rmethod{as.matrix} is supported for
these circumstances.  The ability to convert one object into another
is not very unusual so why mention it?  Because when you cast your
object, the masks WILL be considered so that the masked rows and
columns will be left out of the matrix object.

<<asmatrix>>=
full = as.matrix(origMAlign)
dim(full)
partial = as.matrix(autoMasked)
dim(partial)
@ 

One example of where you might want to use \Rmethod{as.matrix} is when
using the \Rpackage{ape} package. For example if you needed to use the
\Rmethod{dist.dna} function you would want to use \Rmethod{as.matrix}
followed by \Rfunction{as.alignment} and then the
\Rfunction{as.DNAbin} to create a \Robject{DNAbin} object for the
\Rfunction{dist.dna}.




\section{Analytic utilities}
Once you have masked the sequence, you can then ask questions about
the properties of that sequence.  For example, you can look at the
alphabet frequency of that sequence.  The alphabet frequency will only
be for the masked sequence.

<<alphabetFreq>>=
alphabetFrequency(autoMasked)
@ 

You can also calculate a consensus matrix, extract the consensus
string or look at the consensus views.  These methods too will all
consider the masking when you run them.  

<<consensus>>=
consensusMatrix(autoMasked, baseOnly=TRUE)[, 84:90]
substr(consensusString(autoMasked),80,130)
consensusViews(autoMasked)
@ 

You can also cluster the alignments based on their distance to each
other.  Because you must pass in a DNAStringSet, the clustering will
also take into account the masking. So for example, you can see how
clustering the unmasked \Robject{DNAMultipleAlignment} will draw a
funky looking tree.  

<<cluster>>=
library(pwalign)
sdist <- pwalign::stringDist(as(origMAlign,"DNAStringSet"), method="hamming")
clust <- hclust(sdist, method = "single")
pdf(file="badTree.pdf")
plot(clust)
dev.off()
@ 

\begin{figure}
\begin{center}
\includegraphics[width=0.6\textwidth]{badTree}
\caption{\label{f1} Funky tree produced by using unmasked strings.}
\end{center}
\end{figure}

But, if we use the gap-masked \Robject{DNAMultipleAlignment}, to
remove the long uninformative regions, and then make our plot, we can
see the real relationships.

%% could also have just done <<cluster2, fig=TRUE>>=
<<cluster2>>=
sdist <- pwalign::stringDist(as(autoMasked,"DNAStringSet"), method="hamming")
clust <- hclust(sdist, method = "single")
pdf(file="goodTree.pdf")
plot(clust)
dev.off()
fourgroups <- cutree(clust, 4)
fourgroups
@ 

\begin{figure}
\begin{center}
\includegraphics[width=0.6\textwidth]{goodTree}
\caption{\label{f2} A tree produced by using strings with masked gaps.}
\end{center}
\end{figure}

In the "good" plot, the Salmon sequence is once again the most distant
which is what we expect to see.  A closer examination of the sequence
reveals that the similarity between the mouse, rat and human sequences
was being inflated by virtue of the fact that those sequences were
simply much longer (had more information than) the other species
represented.  This is what caused the "funky" result. The relationship
between the sequences in the funky tree was being driven by extra
"length" in the rodent/mouse/human sequences, instead of by the
similarity of the conserved regions.


\section{Exporting to file}
One possible export option is to write to fasta files
If you need to write your \Rclass{MultipleAlignment} object out as a fasta
file, you can cast it to a \Rclass{DNAStringSet} and then write
it out as a fasta file like so:

<<fastaExample, eval=FALSE>>=
DNAStr = as(origMAlign, "DNAStringSet")
writeXStringSet(DNAStr, file="myFile.fa")
@ 

One other format that is of interest is the Phylip format.  The Phylip
format stores the column masking of your object as well as the
sequence that you are exporting. So if you have masked the sequence
and you write out a Phylip file, this mask will be recorded into the
file you export.  As with the fasta example above, any rows that you
have masked out will be removed from the exported file.

<<write.phylip, eval=FALSE>>=
write.phylip(phylipMAlign, filepath="myFile.txt")
@ 


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

<<sessinfo>>=
sessionInfo()
@

\end{document}

