序列说到底就是文本/字符串类型的数据,你完全可以用纯纯的R base函数来处理,只是太麻烦,而且效率很低。BioC的IRanges包从数据结构和运算规则等角度对生物序列做了很细致的定义,是使用R高效处理生物序列数据的基础。但是IRanges包定义的类和方法(函数)实在太多了,学习起来很乏味,有空自己啃吧。我们从Biostrings包开始。
一、用R base的函数来处理序列
先写一个用于产生随机DNA/RNA/AA序列的函数:
- # 按长度设置产生随机序列的函数
- rndSeq <- function(dict, n) {
- paste(sample(dict, n, replace = T), collapse = "")
- }
这个函数的参数dict用于指定字符字典,n 是要产生的序列长度,后面有几个地方要用到它。有关R处理文本/字符串的函数我已经在另一篇文章里面介绍过了,这里只演示一下如何用这些函数来做简单的序列处理(mapply函数的用法可以参考数据汇总/透视一文):
- # 产生3条长度均为50bp的序列
- set.seed(1000)
- (seqs <- mapply(rndSeq, list(c("A", "T", "G", "C")), rep(50, 3)))
- ## [1] "TCAGGAGGATTCTCCATGAGACTAGGTCAAGAGCGGACGTCGAGCGGGTG"
- ## [2] "GGAGGGGGCTTACCACTCACGGTTCAAAAATACAGATTTTCCGAGGACAT"
- ## [3] "GTAGGCAGGTCGAGGACAAAGTTGCCCGTATGTACTACGAAGGGGTCGTC"
- # 用regexpr函数查找第一次匹配ATG的位置和序列长度
- (pos <- regexpr("ATG", seqs))
- ## [1] 16 -1 30
- ## attr(,"match.length")
- ## [1] 3 -1 3
- ## attr(,"useBytes")
- ## [1] TRUE
- as.numeric(pos)
- ## [1] 16 -1 30
- # 用gregexpr函数可以查找匹配ATG的全部起始位置,但后续处理不是很方便
- pos <- gregexpr("ATG", seqs)
- # 获取互补序列很简单
- (seqs_comp <- chartr("ATGC", "TACG", seqs))
- ## [1] "AGTCCTCCTAAGAGGTACTCTGATCCAGTTCTCGCCTGCAGCTCGCCCAC"
- ## [2] "CCTCCCCCGAATGGTGAGTGCCAAGTTTTTATGTCTAAAAGGCTCCTGTA"
- ## [3] "CATCCGTCCAGCTCCTGTTTCAACGGGCATACATGATGCTTCCCCAGCAG"
- # 获取反向序列不大容易,要经过几个步骤
- vseq <- function(x) {
- substring(x, 1:nchar(x), 1:nchar(x))
- } #把字符串向量化的函数
- seqs_rc <- lapply(seqs_comp, vseq)
- seqs_rc <- lapply(seqs_rc, rev)
- (seqs_rc <- sapply(seqs_rc, paste, collapse = "")) #这一步才得到反向互补序列
- ## [1] "CACCCGCTCGACGTCCGCTCTTGACCTAGTCTCATGGAGAATCCTCCTGA"
- ## [2] "ATGTCCTCGGAAAATCTGTATTTTTGAACCGTGAGTGGTAAGCCCCCTCC"
- ## [3] "GACGACCCCTTCGTAGTACATACGGGCAACTTTGTCCTCGACCTGCCTAC"
二、Biostrings定义的常量
包括DNA、RNA、AA、密码子的符号,载入Biostrings包后这些常量就可以直接使用,虽然看起来简单,但很方便:
- library(Biostrings)
- DNA_BASES
- ## [1] "A" "C" "G" "T"
- DNA_ALPHABET
- ## [1] "A" "C" "G" "T" "M" "R" "W" "S" "Y" "K" "V" "H" "D" "B" "N" "-" "+"
- RNA_BASES
- ## [1] "A" "C" "G" "U"
- RNA_ALPHABET
- ## [1] "A" "C" "G" "U" "M" "R" "W" "S" "Y" "K" "V" "H" "D" "B" "N" "-" "+"
- RNA_GENETIC_CODE
- ## UUU UUC UUA UUG UCU UCC UCA UCG UAU UAC UAA UAG UGU UGC UGA UGG CUU CUC
- ## "F" "F" "L" "L" "S" "S" "S" "S" "Y" "Y" "*" "*" "C" "C" "*" "W" "L" "L"
- ## CUA CUG CCU CCC CCA CCG CAU CAC CAA CAG CGU CGC CGA CGG AUU AUC AUA AUG
- ## "L" "L" "P" "P" "P" "P" "H" "H" "Q" "Q" "R" "R" "R" "R" "I" "I" "I" "M"
- ## ACU ACC ACA ACG AAU AAC AAA AAG AGU AGC AGA AGG GUU GUC GUA GUG GCU GCC
- ## "T" "T" "T" "T" "N" "N" "K" "K" "S" "S" "R" "R" "V" "V" "V" "V" "A" "A"
- ## GCA GCG GAU GAC GAA GAG GGU GGC GGA GGG
- ## "A" "A" "D" "D" "E" "E" "G" "G" "G" "G"
- AA_ALPHABET
- ## [1] "A" "R" "N" "D" "C" "Q" "E" "G" "H" "I" "L" "K" "M" "F" "P" "S" "T"
- ## [18] "W" "Y" "V" "U" "B" "Z" "X" "*" "-" "+"
- AMINO_ACID_CODE
- ## A R N D C Q E G H I L K
- ## "Ala" "Arg" "Asn" "Asp" "Cys" "Gln" "Glu" "Gly" "His" "Ile" "Leu" "Lys"
- ## M F P S T W Y V U B Z X
- ## "Met" "Phe" "Pro" "Ser" "Thr" "Trp" "Tyr" "Val" "Sec" "Asx" "Glx" "Xaa"
- GENETIC_CODE
- ## TTT TTC TTA TTG TCT TCC TCA TCG TAT TAC TAA TAG TGT TGC TGA TGG CTT CTC
- ## "F" "F" "L" "L" "S" "S" "S" "S" "Y" "Y" "*" "*" "C" "C" "*" "W" "L" "L"
- ## CTA CTG CCT CCC CCA CCG CAT CAC CAA CAG CGT CGC CGA CGG ATT ATC ATA ATG
- ## "L" "L" "P" "P" "P" "P" "H" "H" "Q" "Q" "R" "R" "R" "R" "I" "I" "I" "M"
- ## ACT ACC ACA ACG AAT AAC AAA AAG AGT AGC AGA AGG GTT GTC GTA GTG GCT GCC
- ## "T" "T" "T" "T" "N" "N" "K" "K" "S" "S" "R" "R" "V" "V" "V" "V" "A" "A"
- ## GCA GCG GAT GAC GAA GAG GGT GGC GGA GGG
- ## "A" "A" "D" "D" "E" "E" "G" "G" "G" "G"
- IUPAC_CODE_MAP
- ## A C G T M R W S Y K
- ## "A" "C" "G" "T" "AC" "AG" "AT" "CG" "CT" "GT"
- ## V H D B N
- ## "ACG" "ACT" "AGT" "CGT" "ACGT"
还有就是用于核酸和蛋白比对的取代矩阵,这些数据需用户自行载入(最后三个是函数,但可以不设参数):
- data(BLOSUM45)
- data(BLOSUM50)
- data(BLOSUM62)
- data(BLOSUM80)
- data(BLOSUM100)
- data(PAM30)
- data(PAM40)
- data(PAM70)
- data(PAM120)
- data(PAM250)
- nucleotideSubstitutionMatrix()
- qualitySubstitutionMatrices()
- errorSubstitutionMatrices()
三、Biostrings定义的类(序列容器)
1、容纳单一序列的类
有XString、BString,DNAString,RNAString和AAString共5类,其中XString是虚拟类,不能产生XString对象,其他4类都是XString的子类:
- getClass("XString")
- ## Virtual Class "XString" [package "Biostrings"]
- ##
- ## Slots:
- ##
- ## Name: shared offset length elementMetadata
- ## Class: SharedRaw integer integer DataTableORNULL
- ##
- ## Name: metadata
- ## Class: list
- ##
- ## Extends:
- ## Class "XRaw", directly
- ## Class "XVector", by class "XRaw", distance 2
- ## Class "Vector", by class "XRaw", distance 3
- ## Class "Annotated", by class "XRaw", distance 4
- ##
- ## Known Subclasses: "BString", "DNAString", "RNAString", "AAString"
再追究XString的来源就复杂了,你可以用getClass函数一个个回溯着看。
除XString外,用与类名称同名的函数就可以由字符串向量(向量长度为1)产生相应的序列对象。BString可以用任意字符(当然要符合R的字符串规则),而DNAString,RNAString和AAString对字符有相应限制。但测试结果却是AAString对字符也不检查,可能函数有问题:
- xxx <- "qwertyuiopasdfghjklzxcvbnm,./;'@#$%^&*()_+~"
- (BString(xxx))
- ## 43-letter "BString" instance
- ## seq: qwertyuiopasdfghjklzxcvbnm,./;'@#$%^&*()_+~
- (DNAString(rndSeq(DNA_BASES, 20)))
- ## 20-letter "DNAString" instance
- ## seq: GGGTATGGTTCGTCTTTGCC
- # 用法错误
- (DNAString(xxx))
- ## Error: key 113 (char 'q') not in lookup table
- (RNAString(rndSeq(RNA_BASES, 20)))
- ## 20-letter "RNAString" instance
- ## seq: UCUAGAUUUUCACGACGCUG
- # 用法错误
- (RNAString(xxx))
- ## Error: key 113 (char 'q') not in lookup table
- (AAString(rndSeq(AA_ALPHABET, 20)))
- ## 20-letter "AAString" instance
- ## seq: LYPBXIC+UNHSFMIBPPVS
- # 下面语句并不出错
- (AAString(xxx))
- ## 43-letter "AAString" instance
- ## seq: qwertyuiopasdfghjklzxcvbnm,./;'@#$%^&*()_+~
2、容纳序列集的类
与容纳单一序列的类对应,容纳序列集(多个序列)的类有XStringSet、BStringSet,DNAStringSet,RNAStringSet和AAStringSet(下面统称为XXXset)。XStringSet是虚拟类,不能实例化,其他4类由它派生而来。此外,由XStringSet直接派生出来还有QualityScaledXStringSet:
- getClass("XStringSet")
- ## Virtual Class "XStringSet" [package "Biostrings"]
- ##
- ## Slots:
- ##
- ## Name: pool ranges elementType elementMetadata
- ## Class: SharedRaw_Pool GroupedIRanges character DataTableORNULL
- ##
- ## Name: metadata
- ## Class: list
- ##
- ## Extends:
- ## Class "XRawList", directly
- ## Class "XVectorList", by class "XRawList", distance 2
- ## Class "List", by class "XRawList", distance 3
- ## Class "Vector", by class "XRawList", distance 4
- ## Class "Annotated", by class "XRawList", distance 5
- ##
- ## Known Subclasses:
- ## Class "BStringSet", directly
- ## Class "DNAStringSet", directly
- ## Class "RNAStringSet", directly
- ## Class "AAStringSet", directly
- ## Class "QualityScaledXStringSet", directly
- ## Class "XStringQuality", by class "BStringSet", distance 2
- ## Class "PhredQuality", by class "BStringSet", distance 3
- ## Class "SolexaQuality", by class "BStringSet", distance 3
- ## Class "IlluminaQuality", by class "BStringSet", distance 3
- ## Class "QualityScaledBStringSet", by class "BStringSet", distance 2
- ## Class "QualityScaledDNAStringSet", by class "DNAStringSet", distance 2
- ## Class "QualityScaledRNAStringSet", by class "RNAStringSet", distance 2
- ## Class "QualityScaledAAStringSet", by class "AAStringSet", distance 2
序列集类型的对象和单条序列一样用与类名称同名的函数由字符串向量产生。下面的代码用 rndSeq 和 mapply 函数产生了一个DNA序列向量,包含有4条长短不一的序列,然后用它生成了一个 DNAStringSet 对象:
- set.seed(1000)
- DNAs <- mapply(rndSeq, list(DNA_BASES), c(10000, 20000, 50000, 25000))
- names(DNAs) <- paste("SEQ", 1:4, sep = "-")
- (DNAset <- DNAStringSet(DNAs))
- ## A DNAStringSet instance of length 4
- ## width seq names
- ## [1] 10000 CTAGGAGGACCTCTTACGAGA...TTGCCCTAACCTCTCCCATG SEQ-1
- ## [2] 20000 TACATGATGGGGCCATTGGTG...TGGCTTGAAATGAGGGAATA SEQ-2
- ## [3] 50000 CGCGCGGTGACTTAACGTGGA...GATGACTCTACGACTATACC SEQ-3
- ## [4] 25000 CAATAATTCGGCCTAGGTGCG...GGCAAACCCACTCGCACCGG SEQ-4
但更多的时候是用这些容器来放置由fasta格式文件读取得到序列,这个过程由相应的readXXXSet函数完成。readXXXSet 函数在老版本的Biostrings中是read.XXXSet,现在旧版函数已被弃用。下面例子用file.choose函数选择了包含拟南芥基因组序列FASTA格式文件,有7条序列,读入后得到一个DNAStringSet对象:
- (chrs <- readDNAStringSet(file.choose(), "fasta"))
- ## A DNAStringSet instance of length 7
- ## width seq names
- ## [1] 30427671 CCCTAAACCCTAAACCCTA...TAGGGTTTAGGGTTTAGGG Chr1 CHROMOSOME
- ## [2] 19698289 NNNNNNNNNNNNNNNNNNN...TAGGGTTTAGGGTTTAGGG Chr2 CHROMOSOME
- ## [3] 23459830 NNNNNNNNNNNNNNNNNNN...AACCCTAAACCCTAAACCC Chr3 CHROMOSOME
- ## [4] 18585056 NNNNNNNNNNNNNNNNNNN...TTAGGGTTTAGGGTTTAGG Chr4 CHROMOSOME
- ## [5] 26975502 TATACCATGTACCCTCAAC...GATTTAGGGTTTTTAGATC Chr5 CHROMOSOME
- ## [6] 154478 ATGGGCGAACGACGGGAAT...TAACTTGGTCCCGGGCATC chloroplast CHROM...
- ## [7] 366924 GGATCCGTTCGAAACAGGT...AATGGAAACAAACCGGATT mitochondria CHRO...
- rm(chrs)
当然也有readBStringSet、readRNAStringSet 和 readAAStringSet 函数。还可以直接用构造函数由 DNAStringSet 对象直接产生 RNAStringSet 对象,但 AAStringSet 对象不行:
- RNAStringSet(DNAset)
- ## A RNAStringSet instance of length 4
- ## width seq names
- ## [1] 10000 CUAGGAGGACCUCUUACGAGA...UUGCCCUAACCUCUCCCAUG SEQ-1
- ## [2] 20000 UACAUGAUGGGGCCAUUGGUG...UGGCUUGAAAUGAGGGAAUA SEQ-2
- ## [3] 50000 CGCGCGGUGACUUAACGUGGA...GAUGACUCUACGACUAUACC SEQ-3
- ## [4] 25000 CAAUAAUUCGGCCUAGGUGCG...GGCAAACCCACUCGCACCGG SEQ-4
XXXSet对象构造函数同时也是XXXSet取子序列的函数,它还有几个参数,分别是start, end, width 和 use.names。前三个参数很有用,比如从染色体上截取两个位置间的序列或者截取某些位点上游或下游N个碱基的序列:
- # 使用start和end 参数获取子序列
- DNAStringSet(DNAset, start = seq(1000, length = 4, by = 1000), end = seq(1500,
- length = 4, by = 1000))
- ## A DNAStringSet instance of length 4
- ## width seq names
- ## [1] 501 GTTGCCGAACAGAGCACGGCT...AGGGAATCGTTAGCGATGAC SEQ-1
- ## [2] 501 AGCGGTGATGTCTGACATTGA...CTATCGCGTGGGACTAGCAC SEQ-2
- ## [3] 501 CCAGCGTAGTTGGGAGAATTG...ACTGCACCCGCTTCGTTGTA SEQ-3
- ## [4] 501 AGCACTGAGGAACAGCTGTAG...GGGAGCCGACACTAAAATTC SEQ-4
- # 使用 end(或start)和width参数获取子序列
- DNAStringSet(DNAset, end = c(234, 3000, 1029), width = 100)
- ## A DNAStringSet instance of length 4
- ## width seq names
- ## [1] 100 TCATGAAGGGGCTGCTCGGGT...ACTATTTCCCCGCTTGCAGG SEQ-1
- ## [2] 100 GGGATCATGATCGTACGCTAT...CACGCTCCAGGCCTATGAGG SEQ-2
- ## [3] 100 CCCATGTGGCGTTCTTATAGG...CATCAGTCAATAATCATACG SEQ-3
- ## [4] 100 CCACTATGGCGCGACGTAGAC...TTGTTTATCTTACTCCTCAT SEQ-4
start, end, width的组合运算使用向量循环规则,即短向量循环使用。
如果原序列是XXXSet类对象,start/end/width的向量长度不能超过原序列:
- class(DNAset)
- ## [1] "DNAStringSet"
- ## attr(,"package")
- ## [1] "Biostrings"
- class(DNAset[1])
- ## [1] "DNAStringSet"
- ## attr(,"package")
- ## [1] "Biostrings"
- DNAStringSet(DNAset, start = seq(1000, length = 2, by = 1000), width = c(5,
- 10, 15))
- ## A DNAStringSet instance of length 4
- ## width seq names
- ## [1] 5 GTTGC SEQ-1
- ## [2] 10 AGCGGTGATG SEQ-2
- ## [3] 15 GCTTAAGTCCCATCA SEQ-3
- ## [4] 5 TGTCC SEQ-4
- DNAStringSet(DNAset[1], start = seq(1000, length = 2, by = 1000), width = c(5,
- 10, 15))
- ## Error: 'start', 'end' or 'width' is longer than 'refwidths'
如果原序列是XString类对象,对start/end/width的向量长度没有限制,但取子序列的位置(start/end)应在原序列的范围之内:
- class(DNAset[[1]])
- ## [1] "DNAString"
- ## attr(,"package")
- ## [1] "Biostrings"
- DNAStringSet(DNAset[[1]], start = seq(1000, length = 2, by = 1000), width = c(5,
- 10, 15))
- ## A DNAStringSet instance of length 3
- ## width seq
- ## [1] 5 GTTGC
- ## [2] 10 ATAAATGGGA
- ## [3] 15 GTTGCCGAACAGAGC
- DNAStringSet(DNAset[[1]], start = seq(1000, length = 2, by = 1e+05), width = c(5,
- 10, 15))
- ## Error: solving row 2: 'allow.nonnarrowing' is FALSE and the supplied start
- ## (101000) is > refwidth + 1
- DNAStringSet(DNAset[[1]], start = seq(1000, length = 2, by = 1000), width = 1e+05)
- ## Error: solving row 1: 'allow.nonnarrowing' is FALSE and the solved end
- ## (100999) is > refwidth
组合使用3个参数中的2个基本可以满足序列截取的需要。XXXSet 对象是列表,可取元素或子集,元素是对应的String(从上面的代码已经可以看出)。
3、XStringViews类
XStringViews类由通过继承Views类(IRanges包定义)而来,XStringViews类对象用于存储同一条序列(目标序列)上的一系列“视野”,即序列区域或子序列。每一个视野由起始点(start)和终止点(end)确定,也隐含了序列的长度信息。
- getClass("XStringViews")
- ## Class "XStringViews" [package "Biostrings"]
- ##
- ## Slots:
- ##
- ## Name: subject ranges elementType elementMetadata
- ## Class: XString IRanges character DataTableORNULL
- ##
- ## Name: metadata
- ## Class: list
- ##
- ## Extends:
- ## Class "Views", directly
- ## Class "List", by class "Views", distance 2
- ## Class "Vector", by class "Views", distance 3
- ## Class "Annotated", by class "Views", distance 4
- ##
- ## Known Subclasses: "XStringPartialMatches"
XStringViews和显微镜下观察物体类似,从一大片区域内挑选感兴趣的区域存储起来。用于挑选(即获取对象)的是Views()函数(IRanges包提供),用法和XXXSet的对象构造函数差不多,但只能使用XString和长度为1的字符串向量,XXXSet类对象即使只包含有一条序列也不行:
- Views(DNAset[[1]], start = seq(1000, length = 2, by = 1000), width = 100)
- ## Views on a 10000-letter DNAString subject
- ## subject: CTAGGAGGACCTCTTACGAGATCAGGCTAAGA...ACGTTCACTATTTGCCCTAACCTCTCCCATG
- ## views:
- ## start end width
- ## [1] 1000 1099 100 [GTTGCCGAACAGAGCACGGCTCGAT...ACGACCGTTTTGTATAGGAATGAC]
- ## [2] 2000 2099 100 [ATAAATGGGATCCGATTCAGGTTCC...CCAAATATGAGCACCGGCGCCACT]
- Views(DNAs[1], start = seq(1000, length = 2, by = 1000), width = 100)
- ## Views on a 10000-letter BString subject
- ## subject: CTAGGAGGACCTCTTACGAGATCAGGCTAAGA...ACGTTCACTATTTGCCCTAACCTCTCCCATG
- ## views:
- ## start end width
- ## [1] 1000 1099 100 [GTTGCCGAACAGAGCACGGCTCGAT...ACGACCGTTTTGTATAGGAATGAC]
- ## [2] 2000 2099 100 [ATAAATGGGATCCGATTCAGGTTCC...CCAAATATGAGCACCGGCGCCACT]
- Views(DNAs, start = seq(1000, length = 2, by = 1000), width = 100)
- ## Error: zero or more than one input sequence
- Views(DNAset[1], start = seq(1000, length = 2, by = 1000), width = 100)
- ## Error: unable to find an inherited method for function 'Views' for
- ## signature '"DNAStringSet"'
IRanges包提供successiveViews方法同样可以用来产生XStringViews类对象,获得的是目标序列上连续的区域,它的用法是:
- successiveViews(subject, width, gapwidth = 0, from = 1)
使用该函数可以非常方便地获得长度和间隔都很规律的序列,例如:
- (successiveViews(DNAset[[1]], width = rep(20, 4), gapwidth = 0, from = 1))
- ## Views on a 10000-letter DNAString subject
- ## subject: CTAGGAGGACCTCTTACGAGATCAGGCTAAGA...ACGTTCACTATTTGCCCTAACCTCTCCCATG
- ## views:
- ## start end width
- ## [1] 1 20 20 [CTAGGAGGACCTCTTACGAG]
- ## [2] 21 40 20 [ATCAGGCTAAGAGTGGATGC]
- ## [3] 41 60 20 [TGAGTGGGCGGGAGGGGGTC]
- ## [4] 61 80 20 [CATTATCTATGGCCTAAAAA]
- (successiveViews(DNAset[[1]], width = rep(20, 4), gapwidth = 80, from = 1))
- ## Views on a 10000-letter DNAString subject
- ## subject: CTAGGAGGACCTCTTACGAGATCAGGCTAAGA...ACGTTCACTATTTGCCCTAACCTCTCCCATG
- ## views:
- ## start end width
- ## [1] 1 20 20 [CTAGGAGGACCTCTTACGAG]
- ## [2] 101 120 20 [GCAGGTAGGCTGAGGATAAA]
- ## [3] 201 220 20 [ACGCCCTGGGGGGGACTATT]
- ## [4] 301 320 20 [ACTAGTTTCGAGACGAGCAA]
- (successiveViews(DNAset[[1]], width = rep(20, 4), gapwidth = -19, from = 1))
- ## Views on a 10000-letter DNAString subject
- ## subject: CTAGGAGGACCTCTTACGAGATCAGGCTAAGA...ACGTTCACTATTTGCCCTAACCTCTCCCATG
- ## views:
- ## start end width
- ## [1] 1 20 20 [CTAGGAGGACCTCTTACGAG]
- ## [2] 2 21 20 [TAGGAGGACCTCTTACGAGA]
- ## [3] 3 22 20 [AGGAGGACCTCTTACGAGAT]
- ## [4] 4 23 20 [GGAGGACCTCTTACGAGATC]
4、MaskedXString掩膜序列类
序列的掩膜方法也是由IRanges包定义的,Biostrings包只是将该方法应用到了XString类对象上。和XString一样,MaskedXString也是虚拟类,能够实例化(产生对象实体)的是它的子类:"MaskedBString", "MaskedDNAString", "MaskedRNAString", "MaskedAAString"类。
- getClass("MaskedXString")
- ## Virtual Class "MaskedXString" [package "Biostrings"]
- ##
- ## Slots:
- ##
- ## Name: unmasked masks
- ## Class: XString MaskCollection
- ##
- ## Known Subclasses: "MaskedBString", "MaskedDNAString", "MaskedRNAString", "MaskedAAString"
用于产生掩膜序列对象的函数是maskMotif(),如:
- DNAset[[1]]
- ## 10000-letter "DNAString" instance
- ## seq: CTAGGAGGACCTCTTACGAGATCAGGCTAAGAGT...GTACGTTCACTATTTGCCCTAACCTCTCCCATG
- (mDNA <- maskMotif(DNAset[[2]], "CATTAG"))
- ## 20000-letter "MaskedDNAString" instance (# for masking)
- ## seq: TACATGATGGGGCCATTGGTGGACAGCGTTTTTA...CTCATGGTACTCGTGGCTTGAAATGAGGGAATA
- ## masks:
- ## maskedwidth maskedratio active desc
- ## 1 12 6e-04 TRUE CATTAG-blocks
通过类型转换可以把MaskedXString类对象转成Views类对象,但是BStrings有更直接的方式:mask()函数。两种方式是完全等价的:
- as(mDNA, "Views")
- ## Views on a 20000-letter DNAString subject
- ## subject: TACATGATGGGGCCATTGGTGGACAGCGTTTT...CATGGTACTCGTGGCTTGAAATGAGGGAATA
- ## views:
- ## start end width
- ## [1] 1 3129 3129 [TACATGATGGGGCCATTGGTGGAC...GACCTACCACGCATCGTAATCCAT]
- ## [2] 3136 4979 1844 [GTTCGTCCGTTCAGAAGATATCCG...AAAGCGCGTGCCACTCTGGTGTGC]
- ## [3] 4986 20000 15015 [AATCGATAGAGTGTAGGGCCCAGG...CTCGTGGCTTGAAATGAGGGAATA]
- mask(DNAset[[2]], "CATTAG")
- ## Views on a 20000-letter DNAString subject
- ## subject: TACATGATGGGGCCATTGGTGGACAGCGTTTT...CATGGTACTCGTGGCTTGAAATGAGGGAATA
- ## views:
- ## start end width
- ## [1] 1 3129 3129 [TACATGATGGGGCCATTGGTGGAC...GACCTACCACGCATCGTAATCCAT]
- ## [2] 3136 4979 1844 [GTTCGTCCGTTCAGAAGATATCCG...AAAGCGCGTGCCACTCTGGTGTGC]
- ## [3] 4986 20000 15015 [AATCGATAGAGTGTAGGGCCCAGG...CTCGTGGCTTGAAATGAGGGAATA]
有时候被掩膜掉的区域才是我们敢兴趣的,比如寻找酶切位点,这时候我们要对掩膜区和非掩膜区进行反转,使用gaps()函数可以完成。而去除掩膜可以用unmasked()函数,去除掩膜对象的序列(仅保留掩膜信息)用masks()函数:
- as(gaps(mDNA), "Views")
- ## Views on a 20000-letter DNAString subject
- ## subject: TACATGATGGGGCCATTGGTGGACAGCGTTTT...CATGGTACTCGTGGCTTGAAATGAGGGAATA
- ## views:
- ## start end width
- ## [1] 3130 3135 6 [CATTAG]
- ## [2] 4980 4985 6 [CATTAG]
- unmasked(mDNA)
- ## 20000-letter "DNAString" instance
- ## seq: TACATGATGGGGCCATTGGTGGACAGCGTTTTTA...CTCATGGTACTCGTGGCTTGAAATGAGGGAATA
- masks(mDNA)
- ## MaskCollection of length 1 and width 20000
- ## masks:
- ## maskedwidth maskedratio active desc
- ## 1 12 6e-04 TRUE CATTAG-blocks
原文来自:http://blog.csdn.net/u014801157/article/details/24372449