There are a few ways to construct a Seurat object from a “outs”
folder from cellranger.
## Method 1 - use the .h5 file
# reading a h5
counts <- Read10X_h5(filename = "/Users/cathal.king/Desktop/SAGC_workshop/data/cellranger/outs/outs/filtered_feature_bc_matrix.h5")
seurat_object <- CreateSeuratObject(counts = counts,
project = "awesome_single_cell")
## Method 2 - use the "/filtered_feature_bc_matrix/" folder which contains the tabulated count data.
# Point your path to the "/filtered_feature_bc_matrix/" folder within the outs "folder"
# outs_data <- Read10X(data.dir = "/Users/cathal.king/Desktop/SAGC_workshop/data/cellranger/outs/outs/filtered_feature_bc_matrix/")
# # Initialize the Seurat object with the raw (non-normalized data).
# seu_object <- CreateSeuratObject(counts = outs_data,
# project = "awesome_single_cell",
# assay = "RNA")
Data walk-through
# explore seurat object
seurat_object
## An object of class Seurat
## 32285 features across 559 samples within 1 assay
## Active assay: RNA (32285 features, 0 variable features)
## 2 layers present: counts, data
# genes by cells shown with the dim function
dim(seurat_object)
## [1] 32285 559
## the meta-data of the object can be accessed with $ and @
#seurat_object$orig.ident
#seurat_object$nCount_RNA
#seurat_object@meta.data
head(seurat_object) # show all meta-data
## orig.ident nCount_RNA nFeature_RNA
## AAACCCAGTCTCTCTG-1 awesome_single_cell 5601 2137
## AAACCCATCCATCCGT-1 awesome_single_cell 22089 5061
## AAACGAAAGTTCTCTT-1 awesome_single_cell 11173 4027
## AAACGAATCCACAGCG-1 awesome_single_cell 11562 4047
## AAAGGTACATCACGGC-1 awesome_single_cell 1087 784
## AAAGTGATCTGCCTCA-1 awesome_single_cell 10788 3616
## AACACACCAAGAGGTC-1 awesome_single_cell 501 386
## AACACACCACGGCCAT-1 awesome_single_cell 30433 6434
## AACACACCATATCTGG-1 awesome_single_cell 15635 4334
## AACAGGGAGATTAGCA-1 awesome_single_cell 14614 3808
## rownames contain genes
#rownames(seurat_object)
str(rownames(seurat_object))
## chr [1:32285] "Xkr4" "Gm1992" "Gm19938" "Gm37381" "Rp1" "Sox17" "Gm37587" ...
## column names contain 10x cell barcodes
#colnames(seurat_object)
Raw gene counts
# the raw assay can be accessed with
Assays(seurat_object)
## [1] "RNA"
# extract assay from Seurat object
counts <- as.data.frame(seurat_object@assays$RNA$counts)
# View counts
#View(counts)
Subset Seurat object
## subset single-cell object to only contain certain genes
# define genes
genes <- c("Lypd1", "Kdsr", "Psmd1", "Sp100", "Dner", "Xrcc5", "Atic")
# first check if genes are in the object / row-names
genes %in% rownames(seurat_object)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## gene names for rows and column names for cell / barcode
# subset object to only contain the listed genes
seurat_filtered <- seurat_object[genes,]
# subset object to remove the listed genes
seurat_filtered2 <- seurat_object[genes,]
## Remove only those genes from the object
counts <- as.data.frame(seurat_object@assays$RNA$counts)
counts <- counts[-(which(rownames(counts) %in% genes)),]
seu_obj_fil2 <- subset(seurat_object, features = rownames(counts))
Data QC
# calculate % mitochondrial content for each cell
# MT genes can be used as a measurement of cell stress
seurat_object <- PercentageFeatureSet(object = seurat_object, "^mt-", col.name = "percent_mito")
# Visualize QC metrics as a violin plot
VlnPlot(object = seurat_object, features = c("nFeature_RNA", "nCount_RNA", "percent_mito"), ncol = 3)

# ?construct the same plot using the ggplot package
# FeatureScatter is typically used to visualize feature-feature relationships, but can be used
# for anything calculated by the object, i.e. columns in object metadata, PC scores etc.
plot1 <- FeatureScatter(seurat_object, feature1 = "nCount_RNA", feature2 = "percent_mito")
plot2 <- FeatureScatter(seurat_object, feature1 = "nCount_RNA", feature2 = "nFeature_RNA")
plot1 + plot2

Remove low quality cells from Seurat object
# filter data by removing cells
seurat_object_qc <- subset(seurat_object, subset = nFeature_RNA < 7500 & nCount_RNA < 4000 & percent_mito < 50)
# check dimensions again of filtered object
dim(seurat_object_qc)
## [1] 32285 326