I would like to merge more than two sample in the Seurat, and the mergeseurat can only merge two sample. So what should I do now. The screenshot is my script.
4 Answers
You have fed arguments to the MergeSeurat()
function that it does not expect. In terms of objects, MergeSeurat()
accepts only 2 arguments object1
and object2
, please do ?MergeSeurat
and see yourself.
Seurat is probably the best documented single cell package, your exact question has its own dedicated vignette: https://satijalab.org/seurat/v3.0/merge_vignette.html
As the vignette above is for Seurat v3 and in case you would like to use Seurat v2:
You can create a named list of Seurat objects to be merged and then use reduce()
of the purrr
package.
# create a named list of seurat objects to be merged
# code not shown
# optional but probably a good idea
# rename cells using object names as prefix
for (i in names(seurat_object_list)) {
seurat_object_list[[i]] <- RenameCells(seurat_object_list[[i]],
add.cell.id = i)
}
# merge all the objects in the list
merged_combined <- reduce(seurat_object_list,
MergeSeurat,
do.normalize = FALSE)
-
$\begingroup$ Hi haci, I have tried, and it got the error "Error in .f(out, .x[[i]], ...) : trying to get slot "raw.data" from an object of a basic class ("character") with no slots". Do you have any idea about this error? Much appreciated! $\endgroup$– huaAug 20, 2019 at 16:31
-
$\begingroup$ You are feeding a character vector to your function (probably MergeSeurat() used within reduce() ) and it complains because it is looking for the "raw.data" slot of your object. So you should feed a proper Seurat object to your function. You can use class() to check the nature of your object. $\endgroup$– haciAug 20, 2019 at 16:42
-
$\begingroup$ I have check it. And it shows " [1] "seurat" ". I don't know whether it is correct. $\endgroup$– huaAug 20, 2019 at 19:23
-
$\begingroup$ What are the outputs of these commands? 1) lapply(seurat_object_list, class), 2) lapply(seurat_object_list, function(x) dim([email protected])) $\endgroup$– haciAug 20, 2019 at 20:27
-
$\begingroup$ I got "[[1]][1] "character"[[2]][1] "character"[[3]][1] "character"[[4]][1] "character" on your first command. And got the error "Error in FUN(X[[i]], ...) : trying to get slot "raw.data" from an object of a basic class ("character") with no slots" in the second command. Much appreciated. $\endgroup$– huaAug 20, 2019 at 21:47
The MergeSeurat
command is from Seurat v2.4 and only accepts two objects as parameters. If you need to merge more than one you can first merge two, then merge the combined object with the third and so on.
In the newer Seurat v3.0 this is replaced by the merge
command that can have a named list of Seurat objects as input
# merge two objects
merge(x = pbmc_small, y = pbmc_small)
# to merge more than two objects, pass one to x and a list of objects to y
merge(x = pbmc_small, y = c(pbmc_small, pbmc_small))
If you want to perform batch correction at the same time as merging the data you should have a look at the IntegrateData
functionality of Seurat v3.0, explained in the corresponding vignettes:
https://satijalab.org/seurat/v3.0/immune_alignment.html
https://satijalab.org/seurat/v3.0/integration.html
A similar workflow for Seurat v2.4 can be cound here:
As of 2023 (or Seurat >4 or >3?), simply use merge
and write
combined.obj <- merge(x = ls.Seurat[[1]], y = ls.Seurat[2:l(ls.Seurat)], add.cell.ids = vec_sample_IDs)
Since y
can take a list.
For now, Seurat does not have an option to merge more than two samples at a time. You can write a function to run the MergeSeurat
function in a loop. Following issue in Github has an example function for multiple samples.