How to concatenate two DNAStringSet sequences per sample in R?

拥有回忆 提交于 2020-01-07 02:03:57

问题


I have two Large DNAStringSet objects, where each of them contain 2805 entries and each of them has length of 201. I want to simply combine them, so to have 2805 entries because each of them are this size, but I want to have one object, combination of both.

I tried to do this

s12 <- c(unlist(s1), unlist(s2))

But that created single Large DNAString object with 1127610 elements, and this is not what I want. I simply want to combine them per sample.

EDIT:

Each entry in my DNASTringSet objects named s1 and s2, have similar format to this:

    width seq
[1]   201 CCATCCCAGGGGTGATGCCAAGTGATTCCA...CTAACTCTGGGGTAATGTCCTGCAGCCGG

回答1:


If your goal is to return a list where each list element is the concatenation of the corresponding list elements from the original lists restulting in a list of with length 2805 where each list element has a length of 402, you can achieve this with Map. Here is an example with a smaller pair of lists.

# set up the lists
set.seed(1234)
list.a <- list(a=1:5, b=letters[1:5], c=rnorm(5))
list.b <- list(a=6:10, b=letters[6:10], c=rnorm(5))

Each list contains 3 elements, which are vectors of length 5. Now, concatenate the lists by list position with Map and c:

Map(c, list.a, list.b)
$a
 [1]  1  2  3  4  5  6  7  8  9 10

$b
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

$c
 [1] -1.2070657  0.2774292  1.0844412 -2.3456977  0.4291247  0.5060559 
     -0.5747400 -0.5466319 -0.5644520 -0.8900378

For your problem as you have described it, you would use

s12 <- Map(c, s1, s2)

The first argument of Map is a function that tells Map what to do with the list items that you have given it. Above those list items are a and b, in your example, they are s1 and s2.




回答2:


You can convert each DNAStringSet into characters. for example:

library(Biostrings)
set1 <- DNAStringSet(c("GCT", "GTA", "ACGT"))
set2 <- DNAStringSet(c("GTC", "ACGT", "GTA"))

as.character(set1)
as.character(set2)

Then paste them together into a DNAStringSet:

DNAStringSet(paste0(as.character(set1), as.character(set2)))


来源:https://stackoverflow.com/questions/38308134/how-to-concatenate-two-dnastringset-sequences-per-sample-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!