dmcable / spacexr

Spatial-eXpression-R: Cell type identification (including cell type mixtures) and cell type-specific differential expression for spatial transcriptomics
GNU General Public License v3.0
310 stars 73 forks source link

"Cannot assign an object of class 'numeric' to the slot @‘counts’ in an object of class 'SpatialRNA'; is(value, 'dgCMatrix') is not TRUE." #218

Open Roger-GOAT opened 2 weeks ago

Roger-GOAT commented 2 weeks ago

Hi, great tool! However, when I run

> myRCTD <- create.RCTD(puck, reference, max_cores = 4)
Begin: process_cell_type_info
process_cell_type_info: number of cells in reference: 5481
process_cell_type_info: number of genes in reference: 14594

      VEIN       CAPI     ARTERY     Tip EC Pre-artery Stalk cell 
       470       1892        319        558        945       1297 
End: process_cell_type_info
Error in (function (cl, name, valueClass)  : 
"Cannot assign an object of class 'numeric' to the slot @‘counts’ in an object of class 'SpatialRNA'; is(value, 'dgCMatrix') is not TRUE."

I do check my counts in puck is dgCMatrix.

> str(counts)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:89570] 2 4 0 1 2 5 0 2 4 5 ...
  ..@ p       : int [1:26313] 0 2 6 10 14 16 19 21 25 29 ...
  ..@ Dim     : int [1:2] 6 26312
  ..@ Dimnames:List of 2
  .. ..$ : chr [1:6] "Bsg" "Col4a1" "Glul" "Lcn2" ...
  .. ..$ : chr [1:26312] "cell3" "cell7" "cell8" "cell9" ...
  ..@ x       : num [1:89570] 16 2 3 1 6 1 4 5 3 2 ...
  ..@ factors : list()

Full code:

library(spacexr)
library(Matrix)
library(doParallel)
Idents(rds) <- 'batch'
DefaultAssay(rds) <- 'OrRNA'
sub <- subset(rds,idents = 'day2')
counts <- sub@assays$OrRNA$counts
colnames(counts) <- str_remove_all(colnames(counts),pattern = '-1-day2')
cell_types <- sub@meta.data$cluster
names(cell_types) <- colnames(counts)
cell_types <- as.factor(cell_types)
nUMI <- sub@meta.data$total_counts; names(nUMI) <- colnames(counts)
reference <- Reference(counts, cell_types, nUMI)

counts <- test@assays$RNA$counts
counts <- counts[, colSums(counts) > 0]
colnames(counts) <- str_remove_all(colnames(counts),'_')
coords <- test@reductions$spatial@cell.embeddings %>% as.data.frame()
colnames(coords) <- c('x','y')
rownames(coords) <- str_remove_all(rownames(coords),"_")
nUMI <- colSums(counts)
puck <- SpatialRNA(coords, counts, nUMI)

## Examine SpatialRNA object (optional)
print(dim(puck@counts)) # observe Digital Gene Expression matrix
hist(log(puck@nUMI,2)) # histogram of log_2 nUMI

plot_puck_continuous(puck, rownames(coords), puck@nUMI, ylimit = c(0,round(quantile(puck@nUMI,0.9))), 
                     title ='plot of nUMI') 

myRCTD <- create.RCTD(puck, reference, max_cores = 4)

Thank you very much!

Roger-GOAT commented 4 days ago

Any one has the same issue?