satijalab / seurat

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

renaming genes in Seurat v3 #1207

Closed zolotarovgl closed 5 years ago

zolotarovgl commented 5 years ago

Is it possible to rename genes (features) in the seurat v3?
When I'm trying to run:

rownames(object) = new_names Error in dimnames(x) <- dn : 'dimnames' applied to non-array.

The problem here is that there is no @raw.data slot anymore. When trying to set gene names using GetAssayData(): rownames(GetAssayData(object, slot = "counts")) = new_names

Error in rownames(GetAssayData(object, slot = "counts")) = new_names : could not find function "GetAssayData<-"

I would be very grateful for any help with this issue.

mojaveazure commented 5 years ago

Hi Grygoriy,

It is not currently possible to rename features in Seurat v3. If you want to rename features, we recommend renaming the features in the expression matrix before creating a Seurat object. We have looked into adding this functionality, but trying to keep track of feature names across all Assay, DimReduc, and Graph objects is a tall order. We may build this functionality at a later date, but have nothing to report at this time.

zolotarovgl commented 5 years ago

Got it, thank you very much for a quick response. I've found a way around by just re-creating the datasets from the data that could be extracted from the objects. I also have a question left: if it's possible to perform batch correction in Seurat v3?

Thank you very much.

andrewwbutler commented 5 years ago

Please see this vignette for our new integration procedure in Seurat v3.

mlukosev commented 4 years ago

Hi all. I am having the same problems. I am trying to plot velocities on my integrated Seurat object based on anchors (following this vignette https://satijalab.org/seurat/v3.1/immune_alignment.html). I have a merged loom file for them, too. However, in order to project velocities on the integrated Seurat object, colnames on loom and Seurat objects need to match. Since I have integrated Seurat object from control and mutant my colnames are for example AAACCCAAGGTATTGA_1 or TTTGTTGTCCGTTTCG2 to mark from which original dataset it came from. Therefore ideally I would want to remove *. However, I am getting the same error as @zolotarovgl when I try this command: colnames(seurat.object) <- paste(substring(colnames(seurat.object),1,16),sep=""). Error in dimnames(x) <- dn : 'dimnames' applied to non-array Is there a way around it? @zolotarovgl could you explain more how re-created the datasets from the data that could be extracted from the objects? Thanks a lot for your help! :)

andrewwbutler commented 4 years ago

Hi,

Please see the RenameCells function to rename the cells. This should allow you to remove the trailing _* as long as that doesn't result in duplicate cell names.

nh-codem commented 2 years ago

Hi, I used to abstruct counts to reaname featrues, and then reconstruct seurat object: counts <- GetAssayData(sc,assay = "RNA",slot = "counts") colnames(counts) <- newid sc <- CreateSeuratObject(counts = counts,meta.data = sc@meta.data)

genecell commented 1 year ago

Hi, I used to abstruct counts to reaname featrues, and then reconstruct seurat object: counts <- GetAssayData(sc,assay = "RNA",slot = "counts") colnames(counts) <- newid sc <- CreateSeuratObject(counts = counts,meta.data = sc@meta.data)

You could also use:

colnames(sc@assay$RNA@counts)<-newid
colnames(sc@assay$RNA@data)<-newid

without creating the object again, it works for me.

mihem commented 1 year ago

@genecell actually it's rownames and not colnames; and assays and not assay; but otherwise you are right, works for me too.

This small helper function works for my purposes and may be useful for some other Seurat users

convertRownames <- function(seu_object) {
  lookup <- homologene::mouse2human(rownames(seu_object), db = homologene::homologeneData2)
  new_rownames <- lookup$humanGene[match(rownames(seu_object), lookup$mouseGene)]
  rownames(seu_object@assays$RNA@counts) <- new_rownames
  rownames(seu_object@assays$RNA@data) <- new_rownames
  rownames(seu_object@assays$RNA@scale.data) <- new_rownames
  return(seu_object)
}
mihem commented 1 year ago

While this worked well for me for Seurat v4, I had some troubles with this script with Seurat v5. So first remove genes that were not converted (so rownames with NA), and subset the object, and then rename @meta.features

convertRownames <- function(seu_object) {
  lookup <- homologene::mouse2human(rownames(seu_object), db = homologene::homologeneData2)
  new_rownames <- lookup$humanGene[match(rownames(seu_object), lookup$mouseGene)]
  rownames(seu_object@assays$RNA@counts) <- new_rownames
  rownames(seu_object@assays$RNA@data) <- new_rownames
  rownames(seu_object@assays$RNA@scale.data) <- new_rownames
  #remove columns with NA
  features_keep <- rownames(seu_object)[!is.na(rownames(seu_object))]
  obj_new <- subset(seu_object, features = features_keep)
  rownames(obj_new@assays$RNA@meta.features) <- rownames(obj_new)
  return(obj_new)
}

Note: You need to remove the line with scale.data, if you don't have scaled data. And you would need to adjust it, if you have further assay, like a sketch assay.