typos
typos

Reputation: 6632

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

Upvotes: 1

Views: 4015

Answers (3)

Soheil_r
Soheil_r

Reputation: 66

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

Upvotes: 3

lmo
lmo

Reputation: 38500

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.

Upvotes: 1

Sergio.pv
Sergio.pv

Reputation: 1400

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)))

Upvotes: 4

Related Questions