简体   繁体   English

用密码子对 DNA 序列进行分组

[英]Group a DNA sequence in codons

I have generated a random DNA sequence我生成了一个随机 DNA 序列

base <- c("A","G","U")
seq <- sample(base, 15, replace = T)
[1] "A" "G" "A" "U" "A" "G" "U" "A" "U" "A" "G" "U" "G" "U" "G"

How can I group the resulting sequence to codons (set of three nucleotides) in order to look for the stop codons?如何将生成的序列分组为密码子(三个核苷酸组)以查找终止密码子? I need something like these:我需要这样的东西:

new_seq <- c("AGA","UAG", "UAU", "AGU", "GUG")

Convert to 3 column matrix, then paste:转换为 3 列矩阵,然后粘贴:

base <- c("A","G","U")
set.seed(1); x <- sample(base, 15, replace = T)
x
# [1] "A" "U" "A" "G" "A" "U" "U" "G" "G" "U" "U" "A" "A" "A" "G"

do.call(paste0, as.data.frame(matrix(x, ncol = 3, byrow = TRUE)))
# [1] "AUA" "GAU" "UGG" "UUA" "AAG"

We can use gl to create the group, and using tapply do a group by paste我们可以使用gl创建组,并使用tapply通过paste进行组

unname(tapply(seq, as.integer(gl(length(seq), 3, 
        length(seq))), FUN = paste, collapse=""))
#[1] "GAU" "UUG" "AAG" "GGU" "AGA"

NOTE: This would also work when the length is not a multiple注意:当长度不是倍数时,这也可以工作


Or another option is to split after paste ing into a single string或者另一种选择是在paste后拆分为单个字符串

strsplit(paste(seq, collapse=""), "(?<=...)", perl = TRUE)[[1]]
#[1] "GAU" "UUG" "AAG" "GGU" "AGA"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM