R语言 使用gsub的互补序列

jpfvwuh4  于 2023-02-01  发布在  其他
关注(0)|答案(3)|浏览(197)

我试图把一条dna链的互补序列存储在一个载体中。它应该把“A”换成“T”,把“C”换成“G”,反之亦然。问题是,我需要在第一个载体上发生这种情况,并正确地打印互补序列。这是我尝试过的,但卡住了:

pilot_sequence <- c("C","G","A","T","C","C","T","A","T")

complement_sequence_display <- function(pilot_sequence){
  complement_chain_Incom <- gsub("A", "T", pilot_sequence)
  complement_chain <- paste(complement_chain_Incom, collapse = "")
  cat("Complement sequence: ", complement_chain, "\n")
} 
complement_chain_Incom <- gsub("A","T", pilot_sequence)
complement_chain <- paste(complement_chain_Incom, collapse= "")
complement_sequence_display(pilot_sequence)

我得到的回答是:CGTTCCTTT,只有第二个和倒数第二个T是正确的,我如何解出其余的字母?
pilot_sequence向量是字符类型,并且函数显示没有执行错误。

db2dz4w8

db2dz4w81#

这是chartr函数的理想用例:

chartr("ATGC","TACG",pilot_sequence)

输出:

[1] "G" "C" "T" "A" "G" "G" "A" "T" "A"
oxalkeyp

oxalkeyp2#

您可以使用purrr::map执行此操作:

pilot_sequence |> purrr::map_chr(~case_when(
  .x == "T" ~ "A",
  .x == "G" ~ "C",
  .x == "A" ~ "T",
  .x == "C" ~ "G"
))
#> [1] "G" "C" "T" "A" "G" "G" "A" "T" "A"
wgeznvg7

wgeznvg73#

您可以使用dplyr中的recode

library(dplyr)

recode(pilot_sequence, "C" = "G", "G" = "C", "A" = "T", "T" = "A")

或者在基R中,创建一个命名向量并使用match匹配命名向量中的值位置,然后调用name获取名称
x一个一个一个一个x一个一个二个x

相关问题