Skip to content

DimReduc

Paul Hoffman edited this page Aug 13, 2018 · 4 revisions

The DimReduc object represents a dimensional reduction taken upon the Seurat object.

Slots

Slot Function
cell.embeddings A matrix with cell embeddings
feature.loadings A matrix with feature loadings
feature.loadings.projected A matrix with projected feature loadings
assay.used Assay used to calculate this dimensional reduction
stdev Standard deviation for the dimensional reduction
key A character string to facilitate looking up features from a specific DimReduc
jackstraw Results from the JackStraw function
misc ...

Object Information

Summary information about DimReduc objects can be had quickly and easily using standard R functions. The number of cell embeddings and feature loadings can be found with ncol and nrow, respectively, or dim for both. The number of dimensions calculated can be found with length; feature and cell names can be found with rownames and colnames, respectively, or the dimnames function.

# The following examples use the PCA dimensional reduction from the PBMC 3k dataset
> pca
A dimensional reduction object with key PC
 Number of dimensions: 20
 Projected dimensional reduction calculated: FALSE
 Jackstraw run: FALSE
# nrow and ncol provide the number of features and cells, respectively
# dim provides both nrow and ncol at the same time
> dim(x = pca)
[1] 1838 2638
# length provides the number of dimensions calculated
> length(x = pca)
[1] 20
# In addtion to rownames and colnames, one can use dimnames
# which provides a two-length list with both rownames and colnames
> head(x = rownames(x = rna))
[1] "TNFRSF4"  "CPSF3L"   "ATAD3C"   "C1orf86"  "RER1"     "TNFRSF25"
> head(x = colnames(x = rna))
[1] "AAACATACAACCAC" "AAACATTGAGCTAC" "AAACATTGATCAGC" "AAACCGTGCTTCCG"
[5] "AAACCGTGTATGCG" "AAACGCACTGGTAC"

Data Access

Accessing data from an DimReduc object is done in several ways. Feature loadings is accessed with the Loadings function. Pulling feature loadings can also be done with the single [ extract operator. Use of the single [ extract operator will pull from the feature.loadings.projected slot unless it's not set, in which case it will pull from feature.loadings; the Loadings function can be forced to pull from one slot or the other.

# Slicing data using the single [ extract operator can take
# numeric slices or vectors of row/column names
> pca[1:3, 1:3]
                PC1         PC2           PC3
TNFRSF4 0.026010991 0.003256709  0.0018341968
CPSF3L  0.008282783 0.009079823 -0.0007640280
ATAD3C  0.003307989 0.003211707  0.0004175542
# Loadings allows pulling from either the projected loadings or the calculated set
# The default will pull from projected if projected has been calculated, otherwise
# it will pull from feature.loadings
> Loadings(object = pca, projected = FALSE)[1:3, 1:3]
                PC1         PC2           PC3
TNFRSF4 0.026010991 0.003256709  0.0018341968
CPSF3L  0.008282783 0.009079823 -0.0007640280
ATAD3C  0.003307989 0.003211707  0.0004175542

Cell embeddings can be accessed with either the Embeddings function or the double [[ extract operator.

# Pulling cell embeddings doesn't differ between Embeddings and [[
> pca[[1:3, 1:3]]
                    PC1        PC2         PC3
AAACATACAACCAC 5.569384 -0.2601651  0.07208744
AAACATTGAGCTAC 7.216456 -7.4833577 -0.27232060
AAACATTGATCAGC 2.706629  1.5814099  0.54774967
> Embeddings(object = pca)[1:3, 1:3]
                    PC1        PC2         PC3
AAACATACAACCAC 5.569384 -0.2601651  0.07208744
AAACATTGAGCTAC 7.216456 -7.4833577 -0.27232060
AAACATTGATCAGC 2.706629  1.5814099  0.54774967

Other data accessors include DefaultAssay for the name of the Assay used to calculate this dimensional reduction, Key for the key of the DimReduc, and Stdev for the vector of standard deviations.

# The key can be used to pull cell embeddings for specific dimensions from the Seurat level
> Key(object = pca)
"PC"
> head(x = FetchData(object = pbmc, vars.fetch = 'PC1'))
                      PC1
AAACATACAACCAC   5.569384
AAACATTGAGCTAC   7.216456
AAACATTGATCAGC   2.706629
AAACCGTGCTTCCG -10.134042
AAACCGTGTATGCG  -1.099311
AAACGCACTGGTAC   1.455335
# DefaultAssay gets the name of the Assay object used to calculate the DimReduc
> DefaultAssay(object = pca)
[1] "RNA"
# Stdev gets the vector of standard deviations for each dimension embedded.
Stdev(object = pca)
 [1] 5.666584 4.326466 3.952192 3.638124 2.191529 1.996551 1.877891 1.798251
 [9] 1.766873 1.753684 1.731568 1.720525 1.718079 1.715879 1.707009 1.702660
[17] 1.697318 1.692549 1.686149 1.683967

Methods

Methods for the DimReduc class can be found with the following:

library(Seurat)
utils::methods(class = 'DimReduc')
  • [
  • [<-
  • [[
  • [[<-
  • DefaultAssay
  • DefaultAssay<-
  • dim
  • dimnames
  • Embeddings
  • Key
  • length
  • Loadings
  • Print
  • RenameCells
  • Stdev