1

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
zx8754
  • 52,746
  • 12
  • 114
  • 209
typos
  • 5,932
  • 13
  • 40
  • 52
  • Does this mean you want a list with length 2805 each of which has a length of 402? – lmo Jul 11 '16 at 13:24
  • What packages are you using? – Axeman Jul 11 '16 at 13:25
  • are you aiming for a table with 2805 rows and 2 columns. Where each entry is a DNA string of 201 nucleotides? – Sergio.pv Jul 11 '16 at 13:40
  • @Sergio.pv Not quite, I aim for `DNAStringSet` object, that will have 2805 entries, and each entry will be concatenation of the two sequences from two objects. For example, I have `s1` object which has 2805 entries of 201 length sequence each, and `s2` object similarly. I want to have `s12` with 2805 entries and each sequence of length 402, where to the end of `s1` sequences we concatenate `s2` sequences. First sequence with first, second with second, and so on. – typos Jul 11 '16 at 13:49
  • This question and provided answers ignore sequence names, which could produce chimeras. I asked a new question and provided an answer to address this here https://stackoverflow.com/questions/53549442/r-concatenate-merge-aastringsets-by-name/53549444#53549444. – Alex Thomas Nov 30 '18 at 00:55

3 Answers3

3

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)))
Sergio.pv
  • 1,380
  • 4
  • 14
  • 23
  • I just need to concatenate the sequences, and keep using `DNAStringSet`, maybe if you can show how to concatenate them and form back a `DNAStringSet` it would be a good answer. – typos Jul 11 '16 at 13:53
3

Since you're using DNAStringSet which is in Biostrings package, i recommend you to use this package's default functions for dealing with XStringSets. Using r base functions would take a lot of time because they need unnecessary conversions.

So you can use Biostrings xscat function. for example:

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

xscat(set1, set2)

the result would be:

DNAStringSet object of length 3:
width seq
[1] 6 GCTGTC
[2] 7 GTAACGT
[3] 7 ACGTGTA
Soheil_r
  • 66
  • 7
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.

lmo
  • 37,904
  • 9
  • 56
  • 69
  • Yes, I want just that, but your approach doesn't work for me. I tried this, `s12 <- DNAStringSet()` to new object which will store concatenation of the other two. Then, did `Map(s12, s1, s2)`. I get error `Error in get(as.character(FUN), mode = "function", envir = envir) : object 'f' of mode 'function' was not found`. Please note that I'm working on `DNAStringSet` objects, as mentioned above. – typos Jul 11 '16 at 13:46
  • But what is concatenate function? I just want to concatenate them purely, I mean to the end of the first sequence in `s1` I want to concatenate the first sequence in `s2`, the second with second, third with third, and so on. – typos Jul 11 '16 at 13:51
  • If i do `s12 <- Map(rnorm(5), s1, s2)` I still get the same error message mentioned above. – typos Jul 11 '16 at 13:56