iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🎃

[Single-cell Analysis] Seurat: Cell Subsetting Examples using subset()

に公開
2024/03/14

After performing a rough analysis of cell data obtained from single-cell analysis with Seurat, it is common to extract specific cell populations for more detailed analysis. From a Seurat object, cells can be extracted using index operations or the subset() function. I have summarized some small tips here.

Table of Contents

Demo data

Preparing the data

Using the PBMC 2,700 cell data available from the Seurat tutorial page.
https://satijalab.org/seurat/articles/pbmc3k_tutorial.html

Processing proceeds in the same way as the tutorial. For demonstration purposes, cluster number information and cell name information are stored in the meta.data slot.

Loading necessary packages
library(Seurat)
library(dplyr)
pbmc.data <- Read10X(data.dir = "pbmc3k_filtered_gene_bc_matrices/filtered_gene_bc_matrices/hg19")
pbmc <- CreateSeuratObject(counts = pbmc.data, project = "pbmc3k", min.cells = 3, min.features = 200)
pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-")
pbmc <- subset(pbmc, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)

pbmc <- pbmc %>%
  NormalizeData() %>%
  FindVariableFeatures() %>%
  ScaleData() %>%
  RunPCA() %>%
  RunUMAP(dims = 1:10) %>%
  FindNeighbors(dims = 1:10) %>%
  FindClusters(resolution = 0.5)
pbmc$Cluster <- pbmc@active.ident
DimPlot(pbmc, reduction = "umap", label = TRUE, pt.size = 0.5) + NoLegend()

new.cluster.ids <- c("Naive CD4 T", "CD14+ Mono", "Memory CD4 T", "B", "CD8 T", "FCGR3A+ Mono",
    "NK", "DC", "Platelet")
names(new.cluster.ids) <- levels(pbmc)
pbmc <- RenameIdents(pbmc, new.cluster.ids)
pbmc$Cell_type <- pbmc@active.ident
DimPlot(pbmc, reduction = "umap", label = TRUE, pt.size = 0.5, repel = T) + NoLegend()


Extracting by active.ident information

active.ident is the information stored in seurat_object@active.ident, which temporarily holds the cluster information currently being used.
Various plotting functions and features such as FindMarkers() and AverageExpression() also use the active.ident information unless specified otherwise.

active.ident can be changed simply by specifying the column name of the meta.data slot as follows.

Changing active.ident Example 1
Idents(pbmc) <- "Cluster"
Changing active.ident Example 2
pbmc <- SetIdent(pbmc, value = "Cluster")
Changing active.ident Example 3
pbmc@active.ident <- pbmc@meta.data$Cluster

To extract cells based on active.ident information, use the idents= argument.
The return value of subset() is a Seurat object.

When active.ident is cluster numbers

pbmc2 <- subset(pbmc, idents = 0)
DimPlot(pbmc2)

For cluster numbers, you can use them with or without quotation marks.

pbmc2 <- subset(pbmc, idents = "1")
DimPlot(pbmc2)

You can extract multiple clusters at once using a vector of cluster numbers.

pbmc2 <- subset(pbmc, idents = c(2,3,4))
DimPlot(pbmc2)

When active.ident is text such as cell names

Changing active.ident to cell type
Idents(pbmc) <- "Cell_type"

In the case of strings, specify them with quotation marks. You can also extract multiple items at once here as well.

pbmc2 <- subset(pbmc, idents = c("Naive CD4 T","B"))
DimPlot(pbmc2)

Extracting everything except the specified items (invert)

By setting the invert= argument to TRUE, you can extract cells other than those specified.

pbmc2 <- subset(pbmc, idents = c("Naive CD4 T","B"), invert = TRUE)
DimPlot(pbmc2)


Extracting by meta.data columns

To extract cells using meta.data columns instead of active.ident, use the subset= argument within the subset() function.
Write it like subset(seurat_object, subset = column_name == "desired_factor").

The column name is specified without quotation marks.

Extraction using Cluster column information
pbmc2 <- subset(pbmc, subset = Cluster == "1")
DimPlot(pbmc2)

Extracting by expression levels

It is also possible to extract cells based on specific gene expression. In this case, too, do not use quotation marks for gene names.

Extracting cells with CD4 expression greater than 1
pbmc2 <- subset(pbmc, subset = CD4 > 1)
DimPlot(pbmc2)

Multiple conditions can also be combined.

Extracting cells with CD3E and CD4 expression greater than 1
pbmc2 <- subset(pbmc, subset = CD3E>1 & CD4>1)
DimPlot(pbmc2)

In Seurat, there are three types of expression data in the assay slot: (1) counts, (2) data, and (3) scale.data.
The slot can be specified with slot=. By default, it seems to refer to the data slot.

Using the counts slot
pbmc2 <- subset(pbmc, subset = CD3E>1 & CD4>1, slot = "counts")
DimPlot(pbmc2)

If there are multiple assays, it is safer to change the DefaultAssay before proceeding.


Extracting by cell barcodes

If you have prepared a vector of cell barcodes under specific conditions, it is also possible to extract cells using the cells= argument.

Obtain cell barcodes where active.idents is Naive CD4 T
cells <- WhichCells(object = pbmc, idents = "Naive CD4 T")

Specify the vector of cell barcodes in the cells argument
pbmc2 <- subset(pbmc, cells = cells)
DimPlot(pbmc2)


Extraction by UMAP values

When performing dimensional reduction, small cell populations that look like outliers may sometimes be generated.
Even if they can be distinguished by a different cluster number from other populations through clustering, it doesn't always go that conveniently.

If they are sufficiently separated from other populations on UMAP, you can also extract cells using the values of the 1st and 2nd dimensions of UMAP.

For example, in this UMAP, the B cell population is located separately in the upper right, and it seems possible to separate them using only the values of the 2nd dimension of UMAP.

DimPlot(pbmc)

Extracting the part where UMAP_2 is less than 10
pbmc2 <- subset(pbmc, subset = UMAP_2 < 10)
DimPlot(pbmc2)

Extract the lower right part

pbmc2 <- subset(pbmc, subset = UMAP_1 > -1 & UMAP_2 < 3)
DimPlot(pbmc2)


Cell extraction by mouse operation

Passing the output of DimPlot() to CellSelector() launches a viewer that allows you to enclose cell populations using mouse operations, powered by the shiny package.

CellSelector
plot <- DimPlot(pbmc)
select.cells <- CellSelector(plot = plot)

Drag on the screen to enclose the desired cell population. Press Done to finish.

The return value of CellSelector is a vector of cell barcodes.

It can be used by passing it to the cells= argument.

pbmc2 <- subset(pbmc, cells = select.cells)
DimPlot(pbmc2)

Discussion