satijalab / seurat

R toolkit for single cell genomics
http://www.satijalab.org/seurat
Other
2.24k stars 901 forks source link

Dimplot doesn't plot ATAC cells in the integration Seurat object #7320

Closed KunFang93 closed 1 year ago

KunFang93 commented 1 year ago

Hi,

I met a problem that Dimplot doesn't plot ATAC cells in the integration Seurat object. Here is my codes

NTs.transfer.anchors <- FindTransferAnchors(
  reference = NTs.rna.seu,
  query = NTs.combined.seu,
  features = VariableFeatures(object = NTs.rna.seu),
  reference.assay = "SCT", query.assay = "ACTIVITY",
  reduction = 'cca',
  normalization.method ='SCT'
)
NTs.rna.seu$celltype <- Idents(NTs.rna.seu)
NTs.predicted.labels <- TransferData(
  anchorset = NTs.transfer.anchors,
  refdata = NTs.rna.seu$celltype,
  weight.reduction = NTs.combined.seu[['lsi']],
  dims = 2:30
)

NTs.combined.seu <- AddMetaData(object = NTs.combined.seu, metadata = NTs.predicted.labels)

# note that we restrict the imputation to variable genes from scRNA-seq, but could impute the
# full transcriptome if we wanted to
NTs.genes.use <- VariableFeatures(NTs.rna.seu)
NTs.refdata <-  GetAssayData(NTs.rna.seu, assay = "SCT", slot = "data")[NTs.genes.use, ]

# refdata (input) contains a scRNA-seq expression matrix for the scRNA-seq cells.  imputation
# (output) will contain an imputed scRNA-seq matrix for each of the ATAC cells
NTs.imputation <- TransferData(anchorset = NTs.transfer.anchors, refdata = NTs.refdata, 
                           weight.reduction = NTs.combined.seu[["lsi"]],
                           dims = 2:30)
NTs.combined.seu[["RNA"]] <- NTs.imputation
NTs.rna.seu <- RenameCells(object = NTs.rna.seu, add.cell.id = "RNA")
NTs.combined.seu <- RenameCells(object = NTs.combined.seu, add.cell.id = "ATAC")
NTs.coembed <- merge(x = NTs.rna.seu, y = NTs.combined.seu)

# Finally, we run PCA and UMAP on this combined object, to visualize the co-embedding of both
# datasets
NTs.coembed <- ScaleData(NTs.coembed, features = NTs.genes.use, do.scale = FALSE)
NTs.coembed <- RunPCA(NTs.coembed, features = NTs.genes.use, verbose = FALSE)
NTs.coembed <- RunUMAP(NTs.coembed, dims = 1:30)

DimPlot(NTs.coembed, group.by = c("orig.ident"))

And this the output plot debug dimplot

But there is 'ATAC' information in the orig.ident in metadata

> table(NTs.coembed$orig.ident)

ATAC  RNA 
8653 7529 

And also when I tried to subset the ATAC cells, there is a error

> NTs.coembed.atac = subset(NTs.coembed, subset=orig.ident %in% c('ATAC'))
SCT assay doesn't leave any cells, so it is removed
Error in subset.Seurat(NTs.coembed, subset = orig.ident %in% c("ATAC")) : 
  No cells left in the default assay, please change the default assay

I wondered how could I solve this problem? Thanks in advances!

Best, Kun

saketkc commented 1 year ago

Hi @KunFang93, can you create a separate metadata column for the RNA and ATAC objects before mergin?

NTs.rna.seu$modality <- "RNA"
NTs.combined.seu$modality <- "ATAC"
NTs.coembed <- merge(x = NTs.rna.seu, y = NTs.combined.seu)

and then use this "modality" column in group.by?

KunFang93 commented 1 year ago

Thanks for your reply! I found the solution but forgot to update here. I thought the problem resulted from the name of the assay that NTs.imputation assigned to. The following codes works for me:

# SCT version Identify anchors
NTs.transfer.anchors.sct <- FindTransferAnchors(
  reference = NTs.rna.seu,
  query = NTs.combined.seu,
  features = VariableFeatures(object = NTs.rna.seu),
  reference.assay = "SCT", query.assay = "ACTIVITY",
  reduction = 'cca',
  normalization.method ='SCT'
)
NTs.rna.seu$celltype <- Idents(NTs.rna.seu)
NTs.predicted.labels.sct <- TransferData(
  anchorset = NTs.transfer.anchors.sct,
  refdata = NTs.rna.seu$celltype,
  weight.reduction = NTs.combined.seu[['lsi']],
  dims = 2:30
)

NTs.combined.seu <- AddMetaData(object = NTs.combined.seu, metadata = NTs.predicted.labels.sct)

# note that we restrict the imputation to variable genes from scRNA-seq, but could impute the
# full transcriptome if we wanted to
NTs.genes.use <- VariableFeatures(NTs.rna.seu)
NTs.refdata.sct <-  LayerData(NTs.rna.seu, assay = "SCT", layer = "data")[NTs.genes.use, ]

# refdata (input) contains a scRNA-seq expression matrix for the scRNA-seq cells.  imputation
# (output) will contain an imputed scRNA-seq matrix for each of the ATAC cells
NTs.imputation <- TransferData(anchorset = NTs.transfer.anchors.sct, refdata = NTs.refdata.sct, 
                               weight.reduction = NTs.combined.seu[["lsi"]],
                               dims = 2:30)
NTs.combined.seu[["SCT"]] <- NTs.imputation

NTs.rna.seu <- RenameCells(object = NTs.rna.seu, add.cell.id = "RNA")
NTs.combined.seu <- RenameCells(object = NTs.combined.seu, add.cell.id = "ATAC")
NTs.combined.seu$celltype <- NTs.combined.seu$predicted.id
NTs.rna.seu$Libtype <- 'RNA'
NTs.combined.seu$Libtype <- 'ATAC'
NTs.coembed <- merge(x = NTs.rna.seu, y = NTs.combined.seu)

# Finally, we run PCA and UMAP on this combined object, to visualize the co-embedding of both
# datasets
NTs.coembed <- ScaleData(NTs.coembed, features = NTs.genes.use, do.scale = FALSE)
NTs.coembed <- RunPCA(NTs.coembed, features = NTs.genes.use, verbose = FALSE)
NTs.coembed <- RunUMAP(NTs.coembed, dims = 1:30)

DimPlot(NTs.coembed, group.by = c("Libtype","celltype"))