Search code examples
rconcatenationbioconductor

How to concatenate two DNAStringSet sequences per sample in R?


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

Solution

  • 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.