开发者

Split into 3 character length

开发者 https://www.devze.com 2023-04-05 06:23 出处:网络
I have very simple question: How can I divide the following text into 3 in a single code mycodes <- c(\"ATTTGGGCTAATTTTGTTTCTTTCTGGGTCTCTC\")

I have very simple question: How can I divide the following text into 3 in a single code

mycodes <- c("ATTTGGGCTAATTTTGTTTCTTTCTGGGTCTCTC")
strsplit(mycodes, split = character(3), fixed = T, perl = FALSE, useBytes = FALSE)

[[1]]
 [1] "A" "T" "T" "T" "G" "G" "G" "C" "T" "A" "A" "T" "T" "T" "T" "G" "T" "T" "T" "C"
[21] "T" "T" "T" "C" "T" "G" "G" "G" "T" "C" "T" 开发者_开发百科"C" "T" "C"

This is not what I want; I want three letters at a time:

[1] "ATT"  "TGG", "GCT"...............and so on the final may be of one, two or three letters depending upon the letter availability.

Thanks;


I assume you want to work with codons. If that's the case, you might want to look at the Biostrings package from Bioconductor. It provides a variety of tools for working with biological sequence data.

library(Biostrings)
?codons

You can achieve what you want, with a little bit of clumsy coercion:

as.character(codons(DNAString(mycodes)))


Here is one approach using stringr package

require(stringr)
start = seq(1, nchar(mycodes), 3)
stop  = pmin(start + 2, nchar(mycodes))
str_sub(mycodes, start, stop)

Output is

[1] "ATT" "TGG" "GCT" "AAT" "TTT" "GTT" "TCT" "TTC" "TGG"
[10] "GTC" "TCT" "C" 


You can also use:

 strsplit(data, '(?<=.{3})', perl=TRUE)
[[1]]
 [1] "ATT" "TGG" "GCT" "AAT" "TTT" "GTT" "TCT" "TTC" "TGG" "GTC" "TCT" "C" 

or

library(stringi)
stri_extract_all_regex(data, '.{1,3}')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号