cole-trapnell-lab / monocle3

Other
318 stars 98 forks source link

Error with find_gene_modules. requires numeric/complex matrix/vector arguments #623

Open Sankalpsp21 opened 1 year ago

Sankalpsp21 commented 1 year ago

I was following the tutorial and after running gene_module_df <- find_gene_modules(cds[deg_ids,], resolution=1e-2) I get this error:

image

I expected to get the gene modules based on coexpression

SessionInfo: image

troutena commented 1 year ago

Same issue for me

dimurali93 commented 1 year ago

same issue for me too :(

jgarces02 commented 1 year ago

Are you guys coming from a Seurat's object? If yes, I solved the problem calculating again PCA values inside monocle...

https://github.com/cole-trapnell-lab/monocle3/blob/87f6e88760566065179ae5039d524da7d032cc15/R/cluster_genes.R#L156

seurat2monocle_object <- preprocess_cds(seurat2monocle_object)
gene_module_df <- find_gene_modules(seurat2monocle_object[degs_monocle_sig,], resolution=1e-2)

... or (better option) correctly importing PCA results:

seurat2monocle_object@clusters@listData[["PCA"]][["partitions"]] <- as.factor(setNames(c(rep(1, length(seurat2monocle_object@colData@rownames))), seurat2monocle_object@colData@rownames))
seurat2monocle_object@clusters@listData[["PCA"]][["clusters"]] <- seurat_object$seurat_clusters
seurat2monocle_object@int_colData@listData[["reducedDims"]]@listData[["PCA"]] <- seurat_object@reductions$pca@cell.embeddings

gene_module_df <- find_gene_modules(seurat2monocle_object[degs_monocle_sig,], resolution=1e-2)
r-osorio commented 1 year ago

same issue here, the above solution by @jgarces02 did not work unfortunately :/

fjrossello commented 1 year ago

Adding to the solution posted by @jgarces02, the find_genes_modules function requires pre-calculated gene/feature loadings (PCA/LSI, more here #191).

If imported from a Seurat object and using PCA, gene loadings should be placed as svd_v in the model list of the PCA list of the reduce_dim_aux slot as follows (previously running Seurat's ProjectDim to generate gene loadings for all genes; more discussions here and here):

seurat_imported_cds@reduce_dim_aux[['PCA']][['model']][['svd_v']] <- seurat@reductions[["pca"]]@feature.loadings.projected

I tested this approach and it worked.

I hope it helps.

ishikorotaro commented 1 year ago

@fjrossello Thank you very much for sharing this. May I ask you to provide more details for your analysis pipeline? Re-calculation PCA in monocle3 method (by @jgarces02) worked for me, but I still face errors when I try to import PCA values from Seurat. I applied ProjectDim to my Seurat object, and then converted it to monocle object with as.cell_data_set with default.reduction = "pca" option (otherwise umap was default). I applied your suggestions to load pca values into svd_v, but still it says Error in cds@reduce_dim_aux[[preprocess_method]][["model"]]$svd_v %*% : non-conformable arguments.

fjrossello commented 1 year ago

As previously mentioned, I used mostly what described by @jgarces02 and here with the modifications described in my comment.

I have use the SeuratWrapper function as.cell_data_set using the default.reduction of the default assay of the Seurat object used (UMAP in my case not PCA).

I then transfer key PCA values as follows (following the naming convention from my previous code):

# Transfer projected feature loadings

seurat_imported_cds@reduce_dim_aux[['PCA']][['model']][['svd_v']] <- seurat@reductions[["pca"]]@feature.loadings.projected

# Transfer PCs stdevs

seurat_imported_cds@reduce_dim_aux[['PCA']][['model']][['svd_sdev']] <- seurat@reductions$pca@stdev

# Transfer PCs cell embeddings (not necessary if UMAP transferred from Seurat used)

reducedDim(seurat_imported_cds, type = "PCA") <-   seurat@reductions$pca@cell.embeddings

# Further processing with Monocle's native functions

# Create partitions (UMAP based)

seurat_imported_cds <-
  cluster_cells(seurat_imported_cds, reduction_method = "UMAP")

# Learn graph (UMAP based)

seurat_imported_cds <- learn_graph(seurat_imported_cds, use_partition = T)

I then applied both graph_test (to identify genes that change across the trajectory) and find_gene_modules function (on identified genes q<0.05) following Monocle3 vignette. As described by @jgarces02 , I think that the error produced in your code is related to the matrix multiplication step in the find_gene_modules function to create feature loadings (see code here).

Also, bear in mind that the way that feature loadings are calculated in Seurat's ProjectDim function is different from the way they are calculated in Monocle using both preprocess_cds and then find_gene_modules thus I am not sure if it is still sound to use Seurat's projected feature loadings as input to calculate gene modules with Monocle3. It would be great if it could be further clarified.

I hope it helps.

sergio-rutella commented 2 weeks ago

cds = preprocess_cds(cds) followed by find_gene_modules worked for me, thanks.

rachadele commented 2 weeks ago

@fjrossello Thank you very much for sharing this. May I ask you to provide more details for your analysis pipeline? Re-calculation PCA in monocle3 method (by @jgarces02) worked for me, but I still face errors when I try to import PCA values from Seurat. I applied ProjectDim to my Seurat object, and then converted it to monocle object with as.cell_data_set with default.reduction = "pca" option (otherwise umap was default). I applied your suggestions to load pca values into svd_v, but still it says Error in cds@reduce_dim_aux[[preprocess_method]][["model"]]$svd_v %*% : non-conformable arguments.

if anyone else encounters this, note that if seurat_object@reductions$harmony@stdev is empty, find_gene_modules will throw this error. I filled it by running the following:

seurat_object@reductions$harmony@stdev = as.numeric(apply(seurat_object@reductions$harmony@cell.embeddings, 2, stats::sd))

Taken from:

harmonydata <- Seurat::CreateDimReducObject(
  embeddings = harmonyEmbed,
  stdev = as.numeric(apply(harmonyEmbed, 2, stats::sd)),
  assay = assay.use,
  key = reduction.save
)

also note that preprocess_method should be passed to the function call to find_gene_modules (not passing this cause more issues for me).