This file is indexed.

/usr/lib/R/site-library/Biostrings/doc/MultipleAlignments.Rnw is in r-bioc-biostrings 2.42.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
%\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>>=
sdist <- 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 <- 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}