Biostrings包测试2_MultipleAlignment Objects_2020-01-30

Biostrings包测试2_MultipleAlignment Objects _20200130Thursday

1.设置当前工作目录

getwd()
setwd("./Biostrings/")

2.导入R包

library(Biostrings)

3.测试:MultipleAlignment Objects

3.1 Introduction

#@ The DNAMultipleAlignment RNAMultipleAlignment and 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.

3.2 Creation and masking

To create a 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.

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 rownames.
rownames(origMAlign)

[1] “gi|84452153|ref|NM_002449.4|”

[2] “gi|208431713|ref|NM_001135625.”

[3] “gi|118601823|ref|NM_001079614.”

[4] “gi|114326503|ref|NM_013601.2|”

[5] “gi|119220589|ref|NM_012982.3|”

[6] “gi|148540149|ref|NM_001003098.”

[7] “gi|45383056|ref|NM_204559.1|”

[8] “gi|213515133|ref|NM_001141603.”

rownames(origMAlign) <- c(“Human”,“Chimp”,“Cow”,“Mouse”,“Rat”, “Dog”,“Chicken”,“Salmon”)
origMAlign

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] -----TCCCGTCTCCGCAGCAAAAAAGTTTGAGTCGCCGCTGCCGGGTTGC…TTATTGCAGTAAAGTTTGTCCAAACTCACAATTAAAAAAAAAAAAAAAAA Human

[2] ---------------------------------------------------…-------------------------------------------------- Chimp

[3] ---------------------------------------------------…-------------------------------------------------- Cow

[4] ----------------------AAAAGTTGGAGTCTTCGCTTGAGAGTTGC…TTATTGCAGT---------------------------------------- Mouse

[5] ---------------------------------------------------…TTATTGCAGTAAA------------------------------------- Rat

[6] ---------------------------------------------------…-------------------------------------------------- Dog

[7] --------------CGGCTCCGCAGCGCCTCACTCGCGCAGTCCCCGCGCA…-------------------------------------------------- Chicken

[8] GGGGGAGACTTCAGAAGTTGTTGTCCTCTCCGCTGATAACAGTTGAGATGC…-------------------------------------------------- Salmon

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

detail(origMAlign)

#@ Applying masks is a simple matter of specifying which ranges to hide.
maskTest <- origMAlign
maskTest

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] ###################################################…################################################## Human

[2] ###################################################…################################################## Chimp

[3] ###################################################…################################################## Cow

[4] ----------------------AAAAGTTGGAGTCTTCGCTTGAGAGTTGC…TTATTGCAGT---------------------------------------- Mouse

[5] ---------------------------------------------------…TTATTGCAGTAAA------------------------------------- Rat

[6] ---------------------------------------------------…-------------------------------------------------- Dog

[7] --------------CGGCTCCGCAGCGCCTCACTCGCGCAGTCCCCGCGCA…-------------------------------------------------- Chicken

[8] GGGGGAGACTTCAGAAGTTGTTGTCCTCTCCGCTGATAACAGTTGAGATGC…-------------------------------------------------- Salmon

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

NormalIRanges object with 1 range and 0 metadata columns:

start end width

[1] 1 3 3

maskTest

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] ###################################################…################################################## Human

[2] ###################################################…################################################## Chimp

[3] ###################################################…################################################## Cow

[4] ----------------------AAAAGTTGGAGTCTTCGCTTGAGAGTTGC…TTATTGCAGT---------------------------------------- Mouse

[5] ---------------------------------------------------…TTATTGCAGTAAA------------------------------------- Rat

[6] ---------------------------------------------------…-------------------------------------------------- Dog

[7] --------------CGGCTCCGCAGCGCCTCACTCGCGCAGTCCCCGCGCA…-------------------------------------------------- Chicken

[8] GGGGGAGACTTCAGAAGTTGTTGTCCTCTCCGCTGATAACAGTTGAGATGC…-------------------------------------------------- Salmon

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

NormalIRanges object with 2 ranges and 0 metadata columns:

start end width

[1] 1 500 500

[2] 1000 2343 1344

maskTest

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] ###################################################…################################################## Human

[2] ###################################################…################################################## Chimp

[3] ###################################################…################################################## Cow

[4] ###################################################…################################################## Mouse

[5] ###################################################…################################################## Rat

[6] ###################################################…################################################## Dog

[7] ###################################################…################################################## Chicken

[8] ###################################################…################################################## Salmon

#@ Remove row and column masks by assigning NULL:
rowmask(maskTest) <- NULL
rowmask(maskTest)

NormalIRanges object with 0 ranges and 0 metadata columns:

start end width

colmask(maskTest) <- NULL
colmask(maskTest)

NormalIRanges object with 0 ranges and 0 metadata columns:

start end width

maskTest

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] -----TCCCGTCTCCGCAGCAAAAAAGTTTGAGTCGCCGCTGCCGGGTTGC…TTATTGCAGTAAAGTTTGTCCAAACTCACAATTAAAAAAAAAAAAAAAAA Human

[2] ---------------------------------------------------…-------------------------------------------------- Chimp

[3] ---------------------------------------------------…-------------------------------------------------- Cow

[4] ----------------------AAAAGTTGGAGTCTTCGCTTGAGAGTTGC…TTATTGCAGT---------------------------------------- Mouse

[5] ---------------------------------------------------…TTATTGCAGTAAA------------------------------------- Rat

[6] ---------------------------------------------------…-------------------------------------------------- Dog

[7] --------------CGGCTCCGCAGCGCCTCACTCGCGCAGTCCCCGCGCA…-------------------------------------------------- Chicken

[8] GGGGGAGACTTCAGAAGTTGTTGTCCTCTCCGCTGATAACAGTTGAGATGC…-------------------------------------------------- Salmon

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

rowmask(maskTest, invert=TRUE) <- IRanges(start=4,end=8)
rowmask(maskTest)

NormalIRanges object with 1 range and 0 metadata columns:

start end width

[1] 1 3 3

maskTest

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] ###################################################…################################################## Human

[2] ###################################################…################################################## Chimp

[3] ###################################################…################################################## Cow

[4] ----------------------AAAAGTTGGAGTCTTCGCTTGAGAGTTGC…TTATTGCAGT---------------------------------------- Mouse

[5] ---------------------------------------------------…TTATTGCAGTAAA------------------------------------- Rat

[6] ---------------------------------------------------…-------------------------------------------------- Dog

[7] --------------CGGCTCCGCAGCGCCTCACTCGCGCAGTCCCCGCGCA…-------------------------------------------------- Chicken

[8] GGGGGAGACTTCAGAAGTTGTTGTCCTCTCCGCTGATAACAGTTGAGATGC…-------------------------------------------------- Salmon

colmask(maskTest, invert=TRUE) <- IRanges(start=501,end=999)
colmask(maskTest)

NormalIRanges object with 2 ranges and 0 metadata columns:

start end width

[1] 1 500 500

[2] 1000 2343 1344

maskTest

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] ###################################################…################################################## Human

[2] ###################################################…################################################## Chimp

[3] ###################################################…################################################## Cow

[4] ###################################################…################################################## Mouse

[5] ###################################################…################################################## Rat

[6] ###################################################…################################################## Dog

[7] ###################################################…################################################## Chicken

[8] ###################################################…################################################## Salmon

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 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 append argument can be used in combination with the 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 rowmask, but it also works for colmask. Before

we begin, lets set the masks back to being NULL again.

#@ 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 append argument.
##@ Then we can demonstrate how the append argument works
rowmask(maskTest) <- IRanges(start=1,end=3)
maskTest

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] ###################################################…################################################## Human

[2] ###################################################…################################################## Chimp

[3] ###################################################…################################################## Cow

[4] ----------------------AAAAGTTGGAGTCTTCGCTTGAGAGTTGC…TTATTGCAGT---------------------------------------- Mouse

[5] ---------------------------------------------------…TTATTGCAGTAAA------------------------------------- Rat

[6] ---------------------------------------------------…-------------------------------------------------- Dog

[7] --------------CGGCTCCGCAGCGCCTCACTCGCGCAGTCCCCGCGCA…-------------------------------------------------- Chicken

[8] GGGGGAGACTTCAGAAGTTGTTGTCCTCTCCGCTGATAACAGTTGAGATGC…-------------------------------------------------- Salmon

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

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] -----TCCCGTCTCCGCAGCAAAAAAGTTTGAGTCGCCGCTGCCGGGTTGC…TTATTGCAGTAAAGTTTGTCCAAACTCACAATTAAAAAAAAAAAAAAAAA Human

[2] ###################################################…################################################## Chimp

[3] ###################################################…################################################## Cow

[4] ----------------------AAAAGTTGGAGTCTTCGCTTGAGAGTTGC…TTATTGCAGT---------------------------------------- Mouse

[5] ---------------------------------------------------…TTATTGCAGTAAA------------------------------------- Rat

[6] ---------------------------------------------------…-------------------------------------------------- Dog

[7] --------------CGGCTCCGCAGCGCCTCACTCGCGCAGTCCCCGCGCA…-------------------------------------------------- Chicken

[8] GGGGGAGACTTCAGAAGTTGTTGTCCTCTCCGCTGATAACAGTTGAGATGC…-------------------------------------------------- Salmon

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

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] -----TCCCGTCTCCGCAGCAAAAAAGTTTGAGTCGCCGCTGCCGGGTTGC…TTATTGCAGTAAAGTTTGTCCAAACTCACAATTAAAAAAAAAAAAAAAAA Human

[2] ---------------------------------------------------…-------------------------------------------------- Chimp

[3] ---------------------------------------------------…-------------------------------------------------- Cow

[4] ----------------------AAAAGTTGGAGTCTTCGCTTGAGAGTTGC…TTATTGCAGT---------------------------------------- Mouse

[5] ###################################################…################################################## Rat

[6] ###################################################…################################################## Dog

[7] ###################################################…################################################## Chicken

[8] ###################################################…################################################## Salmon

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

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] ###################################################…################################################## Human

[2] ###################################################…################################################## Chimp

[3] ###################################################…################################################## Cow

[4] ###################################################…################################################## Mouse

[5] ---------------------------------------------------…TTATTGCAGTAAA------------------------------------- Rat

[6] ---------------------------------------------------…-------------------------------------------------- Dog

[7] --------------CGGCTCCGCAGCGCCTCACTCGCGCAGTCCCCGCGCA…-------------------------------------------------- Chicken

[8] GGGGGAGACTTCAGAAGTTGTTGTCCTCTCCGCTGATAACAGTTGAGATGC…-------------------------------------------------- Salmon

rowmask(maskTest,append=“union”) <- IRanges(start=7,end=8)
maskTest

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] ###################################################…################################################## Human

[2] ###################################################…################################################## Chimp

[3] ###################################################…################################################## Cow

[4] ###################################################…################################################## Mouse

[5] ---------------------------------------------------…TTATTGCAGTAAA------------------------------------- Rat

[6] ---------------------------------------------------…-------------------------------------------------- Dog

[7] ###################################################…################################################## Chicken

[8] ###################################################…################################################## Salmon

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

tataMasked <- maskMotif(origMAlign, “TATA”)
colmask(tataMasked)

NormalIRanges object with 5 ranges and 0 metadata columns:

start end width

[1] 811 814 4

[2] 1180 1183 4

[3] 1186 1191 6

[4] 1204 1207 4

[5] 1218 1221 4

#@ maskGaps also operates on columns and will mask collumns based on the fraction of each column that contains gaps min.fraction along with the width of columns that contain this fraction of gaps min.block.width.
autoMasked <- maskGaps(origMAlign, min.fraction=0.5, min.block.width=4)
autoMasked

DNAMultipleAlignment with 8 rows and 2343 columns

aln names

[1] ###################################################…################################################## Human

[2] ###################################################…################################################## Chimp

[3] ###################################################…################################################## Cow

[4] ###################################################…################################################## Mouse

[5] ###################################################…################################################## Rat

[6] ###################################################…################################################## Dog

[7] ###################################################…################################################## Chicken

[8] ###################################################…################################################## Salmon

#@ Sometimes you may want to cast your MultipleAlignment to be a matrix for usage eslewhere. 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.

full <- as.matrix(origMAlign)
dim(full)

[1] 8 2343

partial <- as.matrix(autoMasked)
dim(partial)

#@ One example of where you might want to use as.matrix is when using the ape package. For example if you needed to use the dist.dna function you would want to use as.matrix followed by as.alignment and then the as.DNAbin to create a DNAbin object for the dist.dna.

3.3 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.

alphabetFrequency(autoMasked)

A C G T M R W S Y K V H D B N - + .

[1,] 260 351 296 218 0 0 0 0 0 0 0 0 0 0 0 18 0 0

[2,] 171 271 231 128 0 0 0 0 0 0 0 0 0 0 3 339 0 0

[3,] 277 360 275 209 0 0 0 0 0 0 0 0 0 0 0 22 0 0

[4,] 265 343 277 226 0 0 0 0 0 0 0 0 0 0 0 32 0 0

[5,] 251 345 287 229 0 0 0 0 0 0 0 0 0 0 0 31 0 0

[6,] 160 285 241 118 0 0 0 0 0 0 0 0 0 0 0 339 0 0

[7,] 224 342 273 190 0 0 0 0 0 0 0 0 0 0 0 114 0 0

[8,] 268 289 273 262 0 0 0 0 0 0 0 0 0 0 0 51 0 0

#@ 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.
consensusMatrix(autoMasked, baseOnly=TRUE)[, 84:90]

[,1] [,2] [,3] [,4] [,5] [,6] [,7]

A 0 3 0 3 0 3 3

C 3 0 0 1 1 1 2

G 1 2 4 1 2 0 0

T 1 0 1 0 1 1 0

other 3 3 3 3 4 3 3

substr(consensusString(autoMasked),80,130)

[1] “####CRGABAMGTCA-YRGCTTCTCYGTSCAWAGGCRRTGRCYTGTTYTCG”

consensusViews(autoMasked)

Views on a 2343-letter BString subject

subject: ----------------------VWVMKYYBSRSTSDYVVMDKBHSVGHKSMSVRYVKDR…-----------------------------------------------------------

views:

start end width

[1] 84 325 242 [CRGABAMGTCA-YRGCTTCTCYGTSCAWAGGCRRTGRCYTGTTYTCGYCCRR…GGARKCGYCSSCGSKGCCRSCCGAMWGCGSCTCSGCYGGSGCYRYCCTGSGG]

[2] 330 332 3 [CCR]

[3] 338 1191 854 [CTGCTGCTGYCGGGVCACGGCGYYCGG-GAMGCKCMCAGYCCC—GGGCCK…CTTTTCCYYTCASARW-KATTGGCTCTGMTAGTTTTTATGTATAAATATATA]

[4] 1198 1241 44 [ATAAAATATAAKAC–TTTTTATAYRSCARATGTAAAAATTCAA]

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 DNAMultipleAlignment will draw a funky looking tree.

sdist <- stringDist(as(origMAlign,“DNAStringSet”), method=“hamming”)
clust <- hclust(sdist, method = “single”)
pdf(file=“badTree.pdf”)
plot(clust)
dev.off()

#@ But, if we use the gap-masked DNAMultipleAlignment, to remove the long uninformative regions, and then make our plot, we can see the real relationships.
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

Human Chimp Cow Mouse Rat Dog Chicken Salmon

1 2 1 1 1 2 3 4

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.

3.4 Exporting to file

One possible export option is to write to fasta files If you need to write your MultipleAlignment object out

as a fasta file, you can cast it to a DNAStringSet and then write it out as a fasta file like so:

DNAStr = as(origMAlign, “DNAStringSet”)
writeXStringSet(DNAStr, file=“myFile.fa”)

#@ One other format that is of interest is the Phylip format. The Phylip format sOne 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(phylipMAlign, filepath=“myFile.txt”)

4.结束

sessionInfo()

R version 3.6.2 (2019-12-12)

Platform: x86_64-w64-mingw32/x64 (64-bit)

Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:

[1] LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese (Simplified)_China.936

[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C

[5] LC_TIME=Chinese (Simplified)_China.936

attached base packages:

[1] stats4 parallel stats graphics grDevices utils datasets methods base

other attached packages:

[1] Biostrings_2.54.0 XVector_0.26.0 IRanges_2.20.2 S4Vectors_0.24.2 BiocGenerics_0.32.0

loaded via a namespace (and not attached):

[1] Seurat_3.1.2 TH.data_1.0-10 Rtsne_0.15 colorspace_1.4-1 seqinr_3.6-1 pryr_0.1.4

[7] ggridges_0.5.2 rstudioapi_0.10 leiden_0.3.2 listenv_0.8.0 npsurv_0.4-0 ggrepel_0.8.1

[13] alakazam_0.3.0 mvtnorm_1.0-12 codetools_0.2-16 splines_3.6.2 R.methodsS3_1.7.1 mnormt_1.5-5

[19] lsei_1.2-0 TFisher_0.2.0 zeallot_0.1.0 ade4_1.7-13 jsonlite_1.6 packrat_0.5.0

[25] ica_1.0-2 cluster_2.1.0 png_0.1-7 R.oo_1.23.0 uwot_0.1.5 sctransform_0.2.1

[31] readr_1.3.1 compiler_3.6.2 httr_1.4.1 backports_1.1.5 assertthat_0.2.1 Matrix_1.2-18

[37] lazyeval_0.2.2 htmltools_0.4.0 prettyunits_1.1.0 tools_3.6.2 rsvd_1.0.2 igraph_1.2.4.2

[43] gtable_0.3.0 glue_1.3.1 RANN_2.6.1 reshape2_1.4.3 dplyr_0.8.3 Rcpp_1.0.3

[49] Biobase_2.46.0 vctrs_0.2.1 multtest_2.42.0 gdata_2.18.0 ape_5.3 nlme_3.1-142

[55] gbRd_0.4-11 lmtest_0.9-37 stringr_1.4.0 globals_0.12.5 lifecycle_0.1.0 irlba_2.3.3

[61] gtools_3.8.1 future_1.16.0 zlibbioc_1.32.0 MASS_7.3-51.4 zoo_1.8-7 scales_1.1.0

[67] hms_0.5.3 sandwich_2.5-1 RColorBrewer_1.1-2 reticulate_1.14 pbapply_1.4-2 gridExtra_2.3

[73] ggplot2_3.2.1 stringi_1.4.3 mutoss_0.1-12 plotrix_3.7-7 caTools_1.17.1.4 bibtex_0.4.2.2

[79] Rdpack_0.11-1 SDMTools_1.1-221.2 rlang_0.4.2 pkgconfig_2.0.3 bitops_1.0-6 lattice_0.20-38

[85] ROCR_1.0-7 purrr_0.3.3 htmlwidgets_1.5.1 cowplot_1.0.0 tidyselect_0.2.5 RcppAnnoy_0.0.14

[91] plyr_1.8.5 magrittr_1.5 R6_2.4.1 gplots_3.0.1.2 multcomp_1.4-12 pillar_1.4.3

[97] sn_1.5-4 fitdistrplus_1.0-14 survival_3.1-8 tibble_2.1.3 future.apply_1.4.0 tsne_0.1-3

[103] crayon_1.3.4 KernSmooth_2.23-16 plotly_4.9.1 progress_1.2.2 grid_3.6.2 data.table_1.12.8

[109] metap_1.2 digest_0.6.23 tidyr_1.0.0 numDeriv_2016.8-1.1 R.utils_2.9.2 RcppParallel_4.4.4

[115] munsell_0.5.0 viridisLite_0.3.0


版权声明:本文为qq_32391411原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。