rezakj / iCellR

Single (i) Cell R package (iCellR) is an interactive R package to work with high-throughput single cell sequencing technologies (i.e scRNA-seq, scVDJ-seq, scATAC-seq, CITE-Seq and Spatial Transcriptomics (ST)).
120 stars 19 forks source link

convert seurat object to iCellR object #19

Closed gt7901b closed 4 years ago

gt7901b commented 4 years ago

Hi:

I tried to convert a Seurat object to iCellR object with the command my.obj <- make.obj(seurat.obj) but got the following error. What is the reason?

Thanks in advance

Error in dimnames(x) <- dn: 'dimnames' applied to non-array Traceback:

  1. make.obj(seurat.obj)
  2. row.names<-(*tmp*, value = gsub("-", ".", row.names(x)))
  3. row.names<-.default(*tmp*, value = gsub("-", ".", row.names(x)))
  4. rownames<-(x, value)
rezakj commented 4 years ago

Hi,

make.obj function only takes data frame (expression matrix) files to make an iCellR object. For Seurat objects, here is how to do it:

objectName = seurat.obj

my.data <- as.data.frame(as.matrix(objectName@assays$RNA@data))
myUMAP <- as.data.frame(objectName@reductions$umap@cell.embeddings)
myPCA <- as.data.frame(objectName@reductions$pca@cell.embeddings)

my.obj <- make.obj(my.data)

my.obj@main.data <- my.obj@raw.data
my.obj@umap.data <- myUMAP
my.obj@pca.data <- myPCA

If you did the above, your iCellR object only has the expression matrix, UMAP and PCA data from your Seurat object. You can add more data from Seurat to iCellR object just like above (e.g. tSNE, clusters, etc.). Use slotNames(iCellR.obj) #or slotNames(Seurat.obj) slot names function to see which ones you need and simply take from one and put into the other as shown above. Or just add the matrix file and do the rest of the analysis in iCellR.

Note that the conditions in iCellR are in the column.names (the barcodes also called cell ids). So if you have multiple samples make sure you fix this too. I recommend re-running your analysis using iCellR for less complications.

Reza

gt7901b commented 4 years ago

Thank you so much for your reply. I will give it a try.

joybarrett08 commented 3 years ago

Hi:

As a follow-up to this issue, how do you add the Seurat IDs to the slot names? For example, when I run the following:

my.data <- as.data.frame(as.matrix(seurat.obj@assays$RNA@data))
myUMAP <- as.data.frame(seurat.obj@reductions$umap@cell.embeddings)
myPCA <- as.data.frame(seurat.obj@reductions$pca@cell.embeddings)
dim(my.data)
dim(myUMAP)
dim(myPCA)
head(my.data)[1:5]
head(myUMAP)
Cond <- (seurat.obj@meta.data$orig.ident)
Cond <- gsub("-",".",Cond)
Cond <- gsub("_",".",Cond)
MyCols <- row.names(myUMAP)
MyCols <- gsub("-",".",MyCols)
MyCols <- gsub("_",".",MyCols)
MyCols <- paste0(Cond,"_",MyCols,sep="")
colnames(my.data) <- MyCols
rownames(myPCA) <- MyCols
rownames(myUMAP) <- MyCols

icellr.obj <- make.obj(my.data)
icellr.objj@main.data <- icellr.objj@raw.data

icellr.obj@umap.data <- myUMAP
icellr.obj@pca.data <- myPCA
icellr.obj <- run.pc.tsne(icellr.obj, dims = 1:10)
MyClust <- icellr.obj@metadata$seurat_clusters

MyClust <- as.numeric(MyClust)
MyClust <- as.numeric(MyClust)
unique(MyClust)

it returns

numeric(0)

Because of this, when I run

MyClust <- as.data.frame(MyClust)
colnames(MyClust) <- "clusters"
rownames(MyClust) <- rownames(myPCA)

I receive Error in.rowNamesDF<-(x, value = value) : invalid 'row.names' length

I checked the slotNames for both the icellr and Seurat object and the metadata for the icellr object is empty. How do I assign the meta.data from the Seurat object to the new icellr metadata slotName?