Complement a DNA sequence

后端 未结 7 1742
情书的邮戳
情书的邮戳 2020-12-31 12:07

Suppose I have a DNA sequence. I want to get the complement of it. I used the following code but I am not getting it. What am I doing wrong ?

s=readline()
AT         


        
7条回答
  •  感情败类
    2020-12-31 12:24

    Use chartr which is built for this purpose:

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

    Just give it two equal-length character strings and your string. Also vectorised over the argument for translation:

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

    Note I'm doing the replacement on the string representation of the DNA rather than the vector. To convert the vector I'd create a lookup-map as a named vector and index that:

    > 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"
    

提交回复
热议问题