10

假设我有一个 DNA 序列。我想得到它的补充。我使用了以下代码,但我没有得到它。我究竟做错了什么 ?

s=readline()
ATCTCGGCGCGCATCGCGTACGCTACTAGC
p=unlist(strsplit(s,""))
h=rep("N",nchar(s))
unlist(lapply(p,function(d){
for b in (1:nchar(s)) {    
    if (p[b]=="A") h[b]="T"
    if (p[b]=="T") h[b]="A"
    if (p[b]=="G") h[b]="C"
    if (p[b]=="C") h[b]="G"
}
4

7 回答 7

15

chartr为此目的而构建的用途:

> s
[1] "ATCTCGGCGCGCATCGCGTACGCTACTAGC"
> chartr("ATGC","TACG",s)
[1] "TAGAGCCGCGCGTAGCGCATGCGATGATCG"

只需给它两个等长的字符串和你的字符串。还对翻译的论点进行了矢量化:

> chartr("ATGC","TACG",c("AAAACG","TTTTT"))
[1] "TTTTGC" "AAAAA" 

请注意,我正在替换 DNA 的字符串表示而不是向量。要转换向量,我将创建一个查找图作为命名向量和索引:

> p
 [1] "A" "T" "C" "T" "C" "G" "G" "C" "G" "C" "G" "C" "A" "T" "C" "G" "C" "G" "T"
[20] "A" "C" "G" "C" "T" "A" "C" "T" "A" "G" "C"
> map=c("A"="T", "T"="A","G"="C","C"="G")
> unname(map[p])
 [1] "T" "A" "G" "A" "G" "C" "C" "G" "C" "G" "C" "G" "T" "A" "G" "C" "G" "C" "A"
[20] "T" "G" "C" "G" "A" "T" "G" "A" "T" "C" "G"
于 2013-12-04T10:54:57.513 回答
13

BioconductorBiostrings为此类操作提供了许多有用的功能。安装一次:

source("http://bioconductor.org/biocLite.R")
biocLite("Biostrings")

然后使用

library(Biostrings)
dna = DNAStringSet(c("ATCTCGGCGCGCATCGCGTACGCTACTAGC", "ACCGCTA"))
complement(dna)
于 2013-12-04T17:42:28.953 回答
8

为了补充,在大写和小写中,您可以使用chartr()

n <- "ACCTGccatGCATC"
chartr("acgtACGT", "tgcaTGCA", n)
# [1] "TGGACggtaCGTAG"

为了更进一步并反向互补核苷酸序列,您可以使用以下功能:

library(stringi)

rc <- function(nucSeq)
  return(stri_reverse(chartr("acgtACGT", "tgcaTGCA", nucSeq)))

rc("AcACGTgtT")
# [1] "AacACGTgT"
于 2016-06-14T14:31:02.987 回答
5
sapply(p, switch,  "A"="T", "T"="A","G"="C","C"="G")
  A   T   C   T   C   G   G   C   G   C   G   C   A   T   C   G   C   G   T 
"T" "A" "G" "A" "G" "C" "C" "G" "C" "G" "C" "G" "T" "A" "G" "C" "G" "C" "A" 
  A   C   G   C   T   A   C   T   A   G   C 
"T" "G" "C" "G" "A" "T" "G" "A" "T" "C" "G" 

如果你不想要互补的名字,你总是可以用unname.

unname(sapply(p, switch,  "A"="T", "T"="A","G"="C","C"="G") )
 [1] "T" "A" "G" "A" "G" "C" "C" "G" "C" "G" "C" "G" "T" "A" "G" "C" "G" "C"
[19] "A" "T" "G" "C" "G" "A" "T" "G" "A" "T" "C" "G"
> 
于 2013-12-04T09:58:11.553 回答
5

还有一个包 seqinr

library(seqinr)
comp(seq) # gives complement
rev(comp(seq)) # gives the reverse complement

Biostrings 的内存配置要小得多,但 seqinr 也很好,因为您可以选择碱基的大小写(包括混合)并将它们更改为您想要的任何内容,例如,如果您想要在同一序列中混合 T 和 U。Biostrings 强制您使用 T 或 U。

于 2014-01-14T04:10:10.093 回答
0

我已经rev(comp(seq))seqinr包概括了解决方案:

install.packages("devtools")
devtools::install_github("TomKellyGenetics/tktools")
tktools::revcomp(seq)

此版本与字符串输入兼容,并且矢量化以处理多个字符串的列表或向量输入。输出类应与输入匹配,包括案例和类型。这也支持包含“U”的输入,用于 RNA 和 RNA 输出序列。

> seq <- "ATCTCGGCGCGCATCGCGTACGCTACTAGC"
> revcomp(seq)
[1] "GCTAGTAGCGTACGCGATGCGCGCCGAGAT"

> seq <- c("TATAAT", "TTTCGC", "atgcat")
> revcomp(seq)
  TATAAT   TTTCGC   atgcat 
 "ATTATA" "GCGAAA" "atgcat" 

请参阅手册TomKellyGenetics/tktools github 包存储库。

于 2019-04-04T08:25:04.083 回答
0

这里是使用 base r 的答案。以一种可怕的格式编写,以使事情清晰并保持单行。它支持大写和小写。

revc = function(s){
       paste0(
           rev(
            unlist(
             strsplit(
                chartr("ATGCatgc","TACGtacg",s)
                      , "")                        # from strsplit
                   )                               # from unlist
               )                                   # from rev
             , collapse='')                        # from paste0
       }
于 2018-11-06T14:36:07.807 回答