<h3>An example solution for Seurat:</h3>

<p>Retrieve sample IDs from the .csv used with <code>cellranger</code>:</p>

<pre><code>samples &lt;- read.csv(file.path("/path/to/csv", "nameof.csv"), stringsAsFactors=F)
</code></pre>

<p>Load the 10x dataset and initialize the Seurat object:</p>

<pre><code>cells.data &lt;- Read10X("path/to/filtered_gene_bc_matrices")

cells &lt;- new("seurat", raw.data=cells.data)
</code></pre>

<p>Get barcodes and suffix:</p>

<pre><code>cellcodes &lt;- as.data.frame(cells@raw.data@Dimnames[[2]])
colnames(cellcodes) &lt;- "barcodes"
rownames(cellcodes) &lt;- cellcodes$barcodes

cellcodes$libcodes &lt;- as.factor(gsub(pattern=".+-", replacement="", cellcodes$barcodes))
cellcodes$samples &lt;- as.vector(samples$library_id[cellcodes$libcodes])
</code></pre>

<p>Create dataframe for <code>meta.data</code> argument and set up object:</p>

<pre><code>sampleidentity &lt;- cellcodes["samples"]

cells &lt;- Setup(cells,
               meta.data=sampleidentity,
               min.cells=3,
               min.genes=200,
               do.logNormalize=T, total.expr=1e4, project="projectname")
</code></pre>

<p>Use the <code>group.by</code> argument of various functions.</p>

CC BY-SA 3.0
2017-08-22T10:48:35.683Z
The new Seurat 2.0 has the `MergeSeurat()` and `AddSamples()` functions. See [this question](https://bioinformatics.stackexchange.com/questions/2356/cellranger-aggr-and-seurat-merge-difference) and [seurat vignette](http://satijalab.org/seurat/merge_vignette.html)
Peter
CC BY-SA 3.0
2017-07-04T14:13:22.690Z
2017-07-04T14:13:22.690Z
Peter
<p>You have to keep track of the file order you used for <code>aggr</code>.  The suffix number represents which represents the aggregated sample. You could store this information in a text file and load it into R independently of the <code>Read10X</code> function and combine it with the cell names to get their sample names. From the page you <a href="https://support.10xgenomics.com/single-cell-gene-expression/software/pipelines/latest/using/aggregate#gem_groups" rel="nofollow noreferrer">link</a>:</p>

<blockquote>
  <p>Gem Groups</p>
  
  <p>This is an integer that is appended to each barcode in the
  gene-barcode matrix. For example, <em>AGACCATTGAGACTTA-1</em> and
  <em>AGACCATTGAGACTTA-2</em> are distinct cell barcodes from different
  libraries, despite having the same nucleotide sequence.</p>
  
  <p>The numbering of the GEM groups will reflect the order that the
  libraries were provided in the <a href="https://support.10xgenomics.com/single-cell-gene-expression/software/pipelines/latest/using/aggregate#csv_setup" rel="nofollow noreferrer">Aggregation CSV</a>.</p>
</blockquote>

CC BY-SA 3.0
2017-06-16T13:17:22.843Z
2017-06-16T13:44:38.527Z
2017-06-16T13:44:38.527Z
GWW
<p><code>cellranger aggr</code> can combine multiple libraries (samples), and appends each barcode with an integer (e.g. AGACCATTGAGACTTA-1). The sample identity is not recorded in the combined <code>matrix.mtx</code> file.</p>

<p><a href="https://support.10xgenomics.com/single-cell-gene-expression/software/pipelines/latest/using/aggregate#gem_groups" rel="nofollow noreferrer">https://support.10xgenomics.com/single-cell-gene-expression/software/pipelines/latest/using/aggregate#gem_groups</a></p>

<p>How can we keep and assign sample information to each cell after loading the data into <code>R</code>? (e.g. <code>Seurat::Read10X()</code>)</p>

<rna-seq><r><scrnaseq><10x-genomics>
Handling sample identity in aggregated 10x libraries?
# Handling sample identity in aggregated 10x libraries?

(Asked by: username_0 on 2017-06-16)

`cellranger aggr` can combine multiple libraries (samples), and appends each barcode with an integer (e.g. AGACCATTGAGACTTA-1). The sample identity is not recorded in the combined `matrix.mtx` file.

<https://support.10xgenomics.com/single-cell-gene-expression/software/pipelines/latest/using/aggregate#gem_groups>

How can we keep and assign sample information to each cell after loading the data into `R`? (e.g. `Seurat::Read10X()`)

**Tags:** rna-seq, r, scrnaseq, 10x-genomics

---

## 2 answers

### Answer by username_0 on 2017-07-04. Score: 1 | Accepted answer

An example solution for Seurat:
Retrieve sample IDs from the .csv used with cellranger:
samples <- read.csv(file.path("/path/to/csv", "nameof.csv"), stringsAsFactors=F)

Load the 10x dataset and initialize the Seurat object:
cells.data <- Read10X("path/to/filtered_gene_bc_matrices")

cells <- new("seurat", raw.data=cells.data)

Get barcodes and suffix:
cellcodes <- as.data.frame(cells@raw.data@Dimnames[[2]])
colnames(cellcodes) <- "barcodes"
rownames(cellcodes) <- cellcodes$barcodes

cellcodes$libcodes <- as.factor(gsub(pattern=".+-", replacement="", cellcodes$barcodes))
cellcodes$samples <- as.vector(samples$library_id[cellcodes$libcodes])

Create dataframe for meta.data argument and set up object:
sampleidentity <- cellcodes["samples"]

cells <- Setup(cells,
               meta.data=sampleidentity,
               min.cells=3,
               min.genes=200,
               do.logNormalize=T, total.expr=1e4, project="projectname")

Use the group.by argument of various functions.

> **Comments:**
> **username_0**: The new Seurat 2.0 has the `MergeSeurat()` and `AddSamples()` functions. See [this question](https://bioinformatics.stackexchange.com/questions/2356/cellranger-aggr-and-seurat-merge-difference) and [seurat vignette](http://satijalab.org/seurat/merge_vignette.html)  

---

### Answer by username_1 on 2017-06-16. Score: 1

You have to keep track of the file order you used for `aggr`. The suffix number represents which represents the aggregated sample. You could store this information in a text file and load it into R independently of the `Read10X` function and combine it with the cell names to get their sample names. From the page you [link](https://support.10xgenomics.com/single-cell-gene-expression/software/pipelines/latest/using/aggregate#gem_groups):

> Gem Groups
>
> This is an integer that is appended to each barcode in the
> gene-barcode matrix. For example, *AGACCATTGAGACTTA-1* and
> *AGACCATTGAGACTTA-2* are distinct cell barcodes from different
> libraries, despite having the same nucleotide sequence.
>
> The numbering of the GEM groups will reflect the order that the
> libraries were provided in the [Aggregation CSV](https://support.10xgenomics.com/single-cell-gene-expression/software/pipelines/latest/using/aggregate#csv_setup).


---

