Seurat object with multiple PCAs/UMAPs from different assays
2
0
Entering edit mode
5 months ago
solo.albif • 0

Hi all,

I have a Seurat object with two assays ("Nanostring" and "metadata") and if I run the PCA/UMAP first on "Nanostring" and then on "metadata", the "metadata" PCA/UMAP overwrites the one generated from the "Nanostring" assay. How can save multiple PCAs/UMAPs from different assays in a single seurat object?

Here is the code I use to generate the PCA/UMAP for the "Nanostring" assay:

# Normalize the count data present in a given assay
patient_rna_only <- Seurat::NormalizeData(patient_rna_only, assay = "Nanostring")
# Scales and centers features in the dataset
patient_rna_only <- Seurat::ScaleData(patient_rna_only)
# Detect highly variable genes for the pca
# Identifies features that are outliers on a 'mean variability plot'
patient_rna_only <- Seurat::FindVariableFeatures(patient_rna_only)
# Run a PCA dimensionality reduction
patient_rna_only <- Seurat::RunPCA(patient_rna_only, seed.use = 1)
# Computes the k.param nearest neighbors
patient_rna_only <- Seurat::FindNeighbors(patient_rna_only, dims = patient_dims)
# Identify clusters of cells by a shared nearest neighbor (SNN) modularity optimization based clustering algorithm
# Use the resolution parameter to fine tune the number of expected clusters
patient_rna_only <- Seurat::FindClusters(
  patient_rna_only,
  resolution = patient_res,
  random.seed = 1,
  cluster.name = "Seurat_clusters")
# Uniform Manifold Approximation and Projection (UMAP) dimensional reduction technique
patient_rna_only <- Seurat::RunUMAP(patient_rna_only, dims = patient_dims, repulsion.strength = 5, seed.use = 1)

And here is what I tried to save the second PCA/UMAP somewhere else:

# Run PCA and specify the number of PCs to compute
  # Only 10 features available, use 10 PCs
  n_pcs <- 9
  patient_data[['proteins']] <- RunPCA(patient_data, assay = "metadata", features = features_to_scale, npcs = n_pcs)

  # Use the available number of PCs for FindNeighbors and clustering
  patient_data <- FindNeighbors(patient_data, dims = 1:n_pcs, assay = "metadata")
  patient_data <- FindClusters(patient_data, resolution = 0.5, assay = "metadata")

  # Run UMAP for visualization
  patient_data[['proteins']] <- RunUMAP(patient_data, dims = 1:n_pcs, assay = "metadata")

But obviously this does not work. Please be patient as I am very new to R and Seurat.

Thank you very much, Alberto

R Seurat • 618 views
ADD COMMENT
2
Entering edit mode
5 months ago
bk11 ★ 3.0k

You need to provide the separate reduction names for your different assays-

# Load necessary libraries
library(Seurat)
library(dplyr)

# Generate mock raw count data for two assays
set.seed(123)
data1 <- matrix(rpois(20000, lambda = 10), nrow = 2000, ncol = 100)
data2 <- matrix(rpois(20000, lambda = 10), nrow = 2000, ncol = 100)

rownames(data1) <- paste0("Gene", 1:2000)
rownames(data2) <- paste0("Gene", 1:2000)
colnames(data1) <- paste0("Cell", 1:100)
colnames(data2) <- paste0("Cell", 1:100)

# Create a Seurat object with the first assay
seurat_obj <- CreateSeuratObject(counts = data1, assay = "RNA")

# Add the second assay
seurat_obj[["Assay2"]] <- CreateAssayObject(counts = data2)

# Normalize and identify variable features for the RNA assay
seurat_obj <- NormalizeData(seurat_obj, assay = "RNA")
seurat_obj <- FindVariableFeatures(seurat_obj, assay = "RNA")

# Normalize and identify variable features for the Assay2
seurat_obj <- NormalizeData(seurat_obj, assay = "Assay2")
seurat_obj <- FindVariableFeatures(seurat_obj, assay = "Assay2")

# Scale the data and run PCA for the RNA assay
seurat_obj <- ScaleData(seurat_obj, assay = "RNA")
seurat_obj <- RunPCA(seurat_obj, assay = "RNA", reduction.name = "pca_RNA")

# Scale the data and run PCA for the Assay2
seurat_obj <- ScaleData(seurat_obj, assay = "Assay2")
seurat_obj <- RunPCA(seurat_obj, assay = "Assay2", reduction.name = "pca_Assay2")

# Run UMAP for the RNA assay
seurat_obj <- RunUMAP(seurat_obj, reduction = "pca_RNA", dims = 1:10, reduction.name = "umap_RNA")

# Run UMAP for the Assay2
seurat_obj <- RunUMAP(seurat_obj, reduction = "pca_Assay2", dims = 1:10, reduction.name = "umap_Assay2")

# Check the results
print(seurat_obj)

p1 <- DimPlot(seurat_obj, reduction = "umap_RNA")
p2 <- DimPlot(seurat_obj, reduction = "umap_Assay2")
p1|p2

enter image description here

ADD COMMENT
1
Entering edit mode
5 months ago

You just need to supply reduction.name to your RunPCA and RunUMAP functions, which will allow you to set the name of the resulting dimensionality reduction. Note that you will also have to change the reduction parameter in RunUMAP to the name of your new PCA as well. See ?RunPCA and ?RunUMAP for more details.

ADD COMMENT

Login before adding your answer.

Traffic: 2178 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6