Error in as.data.frame.default(x[[i]], optional = TRUE)
Closed this issue · 5 comments
Hi, thank you for your amazing work :)
I was trying to generate some in-silicon data with my spatial transcriptomic clustered data and I faced an error that I am not really sure how to solve. This is my output and error message:
Loading required package: viridisLite
class: SingleCellExperiment
dim: 140 16094
metadata(0):
assays(2): counts logcounts
rownames(140): eGFP PTPRC-exon4-6-introns ... FRMD5 mCherry2
rowData names(0):
colnames(16094): 183816621152166943455673244778245380484
198194891178223149109664428303794678308 ...
45312782973589652226064002619926238148
62901559251622040146779944472043861759
colData names(23): fov volume ... nFeature_RNA ident
reducedDimNames(3): PCA UMAP SPATIAL
mainExpName: RNA
altExpNames(0):
Input Data Construction Start
Input Data Construction End
Start Marginal Fitting
Marginal Fitting End
Start Copula Fitting
Convert Residuals to Multivariate Gaussian
Converting End
Copula group Myofibroblast starts
Copula group vCM starts
Copula group Myeloid starts
Copula group aCM starts
Copula group Pericyte starts
Copula group Endothelial starts
Copula group Fibroblast starts
Copula Fitting End
Start Parameter Extraction
Parameter
Extraction End
Start Generate New Data
Use Copula to sample a multivariate quantile matrix
Sample Copula group Myofibroblast starts
Sample Copula group vCM starts
Sample Copula group Myeloid starts
Sample Copula group aCM starts
Sample Copula group Pericyte starts
Sample Copula group Endothelial starts
Sample Copula group Fibroblast starts
New Data Generating End
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ‘structure("dgCMatrix", package = "Matrix")’ to a data.frame
Calls: %>% ... data.frame -> as.data.frame -> as.data.frame.default
Execution halted
It seems like it is the last step but I do not get any output data out of it. If you have any suggestions on how to solve it I would be very grateful!
Also one question out of interest: Have you ever considered the possibility of generating data out of MERFISH experiment and not only spot-based spatial transcriptomic data? If yes will be the process of data generation similar to this tutorial: https://songdongyuan1994.github.io/scDesign3/docs/articles/scDesign3-spatial-vignette.html?
Thank you in advance and looking forward to hearing from you :)
Hi Dana,
This error seems to be weird to me --- you have already sampled the data and we just need it to be returned. Can you try the following steps sequentially?
- Re-install the
scDesign3
package; - Check if you have any sparse Matrix (
dgCMatrix
) in your input sce. It is Ok to have your assay (counts, logcounts) to be a sparse matrix, but any other elements are not allowed; - Could you please post your code if it still does not work?
"generating data out of MERFISH experiment": good question! We actually have tried that, and it still worked to some extent but not as good as the spot-level data. The reason is that in MERFISH the spatial patterns are often not very clear, which I think is caused by the high-resolution of MERFISH. Yes, you can still run the https://songdongyuan1994.github.io/scDesign3/docs/articles/scDesign3-spatial-vignette.html tutorial, and check the final result. Let me know if it works for you.
Best,
Dongyuan
Hi Dongyuan,
thank you for your quick answer, I did as you suggested but the error remains the same. I used the code from your tutorial and only slightly modified it based on the column names:
library(scDesign3)
library(SingleCellExperiment)
library(ggplot2)
library(dplyr)
library(viridis)
theme_set(theme_bw())
example_sce <- readRDS("path_to_the_file/R/scDesign3/data/sce_2022PK2_clustered.rds")
print(example_sce)
set.seed(123)
example_simu <- scdesign3(
sce = example_sce,
assay_use = "counts",
celltype = "merged",
pseudotime = NULL,
spatial = NULL,
other_covariates = NULL,
mu_formula = 'merged',
sigma_formula = "1",
family_use = "nb",
n_cores = 2,
usebam = FALSE,
corr_formula = "1",
copula = "gaussian",
DT = TRUE,
pseudo_obs = FALSE,
return_model = FALSE,
nonzerovar = FALSE
)
logcounts(example_sce) <- log1p(counts(example_sce))
simu_sce <- example_sce
counts(simu_sce) <- example_simu$new_count
logcounts(simu_sce) <- log1p(counts(simu_sce))
VISIUM_dat_test <- data.frame(t(log1p(counts(example_sce)))) %>% as_tibble() %>% dplyr::mutate(X = colData(example_sce)$spatial1, Y = colData(example_sce)$spatial2) %>% tidyr::pivot_longer(-c("X", "Y"), names_to = "Gene", values_to = "Expression") %>% dplyr::mutate(Method = "Reference")
VISIUM_dat_scDesign3 <- data.frame(t(log1p(counts(simu_sce)))) %>% as_tibble() %>% dplyr::mutate(X = colData(simu_sce)$spatial1, Y = colData(simu_sce)$spatial2) %>% tidyr::pivot_longer(-c("X", "Y"), names_to = "Gene", values_to = "Expression") %>% dplyr::mutate(Method = "scDesign3")
VISIUM_dat <- bind_rows(VISIUM_dat_test, VISIUM_dat_scDesign3) %>% dplyr::mutate(Method = factor(Method, levels = c("Reference", "scDesign3")))
VISIUM_dat %>% filter(Gene %in% rownames(example_sce)[1:5]) %>% ggplot(aes(x = X, y = Y, color = Expression)) + geom_point(size = 0.5) + scale_colour_gradientn(colors = viridis_pal(option = "magma")(10), limits=c(0, 8)) + coord_fixed(ratio = 1) + facet_grid(Method ~ Gene )+ theme_gray()
Because everything is working just fine with VISIUM example data that you posted I assume that the problem is somewhere in my data. I converted it from h5ad to rds can it potentially cause the error?
Best wishes,
Dana
@Dana162001 Hi Dana,
Does your error occur before the code example_simu <- scdesign3()
is finished or before that? If it is after that, I do have a guess now.
After reading your code, I realize that you have a code VISIUM_dat_test <- data.frame(t(log1p(counts(example_sce))))
, which will not work if your counts(example_sce)
is a sparse matrix. You have to use as.matrix()
to first convert the sparse matrix to a dense matrix then you can use as.data.frame
. If your error is here, this should fix your bug.
Best,
Dongyuan
Hi Dongyuan,
Thank you for your suggestion! Yes, it was the problem with the matrix. I used this code to solve it:
new_test <- data.frame(t(log1p(as.matrix(counts(example_sce))))) %>% as_tibble() %>% dplyr::mutate(X = colData(example_sce)$min_x, Y = colData(example_sce)$min_y) %>% tidyr::pivot_longer(-c("X", "Y"), names_to = "Gene", values_to = "Expression") %>% dplyr::mutate(Method = "Reference")
new_scDesign3 <- data.frame(t(log1p(as.matrix(counts(simu_sce))))) %>% as_tibble() %>% dplyr::mutate(X = colData(simu_sce)$min_x, Y = colData(simu_sce)$min_y) %>% tidyr::pivot_longer(-c("X", "Y"), names_to = "Gene", values_to = "Expression") %>% dplyr::mutate(Method = "scDesign3")
I think, I can close the issue now. A propos the MERFISH data, it also worked but not as good as VISIUM I will have to play around with some parameters to improve it. If you will have any ideas about it I'd love to hear them :) Thank you again!
Best wishes,
Dana
Sounds great! Some parameters you may play with can be found in https://songdongyuan1994.github.io/scDesign3/docs/articles/scDesign3-spatial-deconvolution.html . We specify the m = c(1, 2, 1)
in marginal models. For more details please read https://stat.ethz.ch/R-manual/R-devel/library/mgcv/html/smooth.construct.gp.smooth.spec.html . I would be happy to report your results if it is improved by your parameter tunning.
Best,
Dongyuan