Answers.Body: <p>The <code>MergeSeurat</code> 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.</p>
  
  <p>In the newer Seurat v3.0 this is replaced by the <code>merge</code> command that can have a named list of Seurat objects as input</p>
  
  <pre><code># 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))
  </code></pre>
  
  <p>If you want to perform batch correction at the same time as merging the data you should have a look at the <code>IntegrateData</code> functionality of Seurat v3.0, explained in the corresponding vignettes:  </p>
  
  <p><a href="https://satijalab.org/seurat/v3.0/immune_alignment.html" rel="nofollow noreferrer">https://satijalab.org/seurat/v3.0/immune_alignment.html</a><br>
  <a href="https://satijalab.org/seurat/v3.0/integration.html" rel="nofollow noreferrer">https://satijalab.org/seurat/v3.0/integration.html</a>  </p>
  
  <p>A similar workflow for Seurat v2.4 can be cound here:  </p>
  
  <p><a href="https://satijalab.org/seurat/v2.4/immune_alignment.html" rel="nofollow noreferrer">https://satijalab.org/seurat/v2.4/immune_alignment.html</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-19T13:26:19.763Z
Answers.LastActivityDate: 2019-08-19T13:26:19.763Z
Answers.UserDisplayName: PPK
Answers.Body: <p>As of 2023 (or Seurat &gt;4 or &gt;3?), simply use <code>merge</code> and write</p>
  <pre class="lang-r prettyprint-override"><code>combined.obj &lt;- merge(x = ls.Seurat[[1]], y = ls.Seurat[2:l(ls.Seurat)], add.cell.ids = vec_sample_IDs)
  </code></pre>
  <p>Since <code>y</code> can take a list.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-17T11:25:26.033Z
Answers.LastActivityDate: 2023-07-17T11:25:26.033Z
Answers.UserDisplayName: bud.dugong
Answers.Body: <p>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 <code>MergeSeurat</code> function in a loop. Following issue in Github has an example function for multiple samples.</p>
  
  <p><a href="https://github.com/satijalab/seurat/issues/432" rel="nofollow noreferrer">https://github.com/satijalab/seurat/issues/432</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-19T07:00:08.353Z
Answers.LastActivityDate: 2019-08-19T07:00:08.353Z
Answers.UserDisplayName: arup
Answers.Body: <p>You have fed arguments to the <code>MergeSeurat()</code> function that it does not expect. In terms of objects, <code>MergeSeurat()</code> accepts only 2 arguments <code>object1</code> and <code>object2</code>, please do <code>?MergeSeurat</code> and see yourself.</p>
  
  <p>Seurat is probably the best documented single cell package, your exact question has its own dedicated vignette: <a href="https://satijalab.org/seurat/v3.0/merge_vignette.html" rel="nofollow noreferrer">https://satijalab.org/seurat/v3.0/merge_vignette.html</a></p>
  
  <p>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 <code>reduce()</code> of the <code>purrr</code> package.</p>
  
  <pre><code># 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]] &lt;- RenameCells(seurat_object_list[[i]],
                                           add.cell.id = i)
  }
  
  # merge all the objects in the list
  merged_combined &lt;- reduce(seurat_object_list,
                            MergeSeurat,
                            do.normalize = FALSE)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-20T16:31:54.477Z
Answers.Text: 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!
Answers.UserDisplayName: hua
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-20T16:42:48.767Z
Answers.Text: 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.
Answers.UserDisplayName: haci
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-20T19:23:16.837Z
Answers.Text: I have check it. And it shows " [1] "seurat" ". I don't know whether it is correct.
Answers.UserDisplayName: hua
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-20T20:27:01.760Z
Answers.Text: What are the outputs of these commands? 1) lapply(seurat_object_list, class), 2) lapply(seurat_object_list, function(x) dim(x@raw.data))
Answers.UserDisplayName: haci
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-20T21:47:29.230Z
Answers.Text: 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.
Answers.UserDisplayName: hua
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-20T22:28:52.747Z
Answers.Text: These prove that your object list does not contain seurat objects but just character vectors which obviously does not have raw.data slots. Please double check the way you create your object list. Should be sth like object_list <- list(name1 = object1, name2 = object2)
Answers.UserDisplayName: haci
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-19T07:11:10.853Z
Answers.LastActivityDate: 2019-08-19T07:11:10.853Z
Answers.UserDisplayName: haci
Body: <p>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.<a href="https://i.sstatic.net/dN8oy.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/dN8oy.jpg" alt="enter image description here"></a></p>
  
Tags: <rna-seq><seurat><single-cell>
Title: how to merge more than two sample in Seurat?
ThreadText: # how to merge more than two sample in Seurat?
  
  (Asked by: username_0 on 2019-08-19)
  
  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.[![enter image description here](https://i.sstatic.net/dN8oy.jpg)](https://i.sstatic.net/dN8oy.jpg)
  
  **Tags:** rna-seq, seurat, single-cell
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_4 on 2019-08-19. Score: 4 | Accepted answer
  
  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)
  
  ```
  
  > **Comments:**
  > **username_0**: Hi username_4, 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!  
  > **username_4**: 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.  
  > **username_0**: I have check it. And it shows " [1] "seurat" ". I don't know whether it is correct.  
  > **username_4**: What are the outputs of these commands? 1) lapply(seurat_object_list, class), 2) lapply(seurat_object_list, function(x) dim(x@raw.data))  
  > **username_0**: 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.  
  > **username_4**: These prove that your object list does not contain seurat objects but just character vectors which obviously does not have raw.data slots. Please double check the way you create your object list. Should be sth like object_list <- list(name1 = object1, name2 = object2)  
  
  ---
  
  ### Answer by username_1 on 2019-08-19. Score: 2
  
  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:
  
  <https://satijalab.org/seurat/v2.4/immune_alignment.html>
  
  
  ---
  
  ### Answer by username_2 on 2023-07-17. Score: 2
  
  As of 2023 (or Seurat >4 or >3?), simply use `merge` and write
  
  ```r
  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.
  
  
  ---
  
  ### Answer by username_3 on 2019-08-19. Score: 1
  
  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.
  
  <https://github.com/satijalab/seurat/issues/432>
  
  
  ---
  
  