drieslab / Giotto

Spatial omics analysis toolbox
https://drieslab.github.io/Giotto_website/
Other
258 stars 98 forks source link

Error During runSpatialDeconv #106

Closed DavidM222555 closed 2 years ago

DavidM222555 commented 3 years ago

Hello! I am getting the below error from runSpatialDeconv:

Error in enrich_matrix[, ct_spot_k[m]] : subscript out of bounds
Calls: runSpatialDeconv -> runDWLSDeconv -> spot_deconvolution -> which
In addition: Warning message:
In cbind(allCounts_DWLS, solDWLS) :
  number of rows of result is not a multiple of vector length (arg 2)
Execution halted

I have used the same code for other spatial slices utilizing the same sign_matrix successfully but for this sample I am getting the above error and I am not entirely sure why. Any guidance as to what the issue might be would be greatly appreciated.

Thanks! David Martin

ndelrossi7 commented 3 years ago

Hi David! Can you try to to create a signature matrix with the function: makeSignMatrixDWLS?

sign_matrix <- makeSignmatrixDWLS(gobject = my_giotto_object, sign_gene = signature_genes, cell_type = cell_types)

Let me know if this works or if you're still having issues!

DavidM222555 commented 3 years ago

My reference was generated outside of Giotto (https://github.com/RubD/Giotto/issues/99#issuecomment-856213680). This is what the head of the sign_matrix looks like so you can get an idea of the format used.

        RNA.norm.Astro RNA.B.cell     RNA.DC      RNA.TAM    RNA.Mono
SLC1A2       29.600864 0.00000000 0.01935147 0.0335618805 0.012580832
GPC5         22.794883 0.00000000 0.00000000 0.0000000000 0.000000000
SLC1A3       19.279128 0.56640095 0.45516158 3.4817928176 0.830137756
SLC4A4        9.289343 0.00000000 0.00203655 0.0152744775 0.004810005
PITPNC1      16.048347 0.04631309 0.09183284 0.0522992788 0.148530316
RYR3         11.725950 0.00000000 0.00000000 0.0003111872 0.011682630
GPM6A        22.818194 0.16260114 0.04580532 0.0875337590 0.009884885
COL5A3        6.985291 0.00000000 0.00000000 0.0030883652 0.000000000
DTNA         18.461512 0.02553289 0.06031501 0.1330593781 0.018473905
NKAIN3        7.192212 0.01950002 0.00000000 0.0038635139 0.014375252
ndelrossi7 commented 3 years ago

Thanks for sending this along @DavidM222555. Can you post the code you ran for this that generated the error? In addition, can you please post the type of object and dimensions of the matrix? Thanks!

DavidM222555 commented 3 years ago

The type of the matrix is 'list' and the dimensions are 1655 x 22.

library(Giotto)
library(dplyr)
data_path = ""  # replace with cellranger spatial outs path to your sample of interest
save_dir = ""
instrs = createGiottoInstructions(save_dir = save_dir, save_plot = TRUE, show_plot = FALSE)

obj <- createGiottoVisiumObject(visium_dir = data_path, expr_data = 'filter', png_name = "tissue_lowres_image.png", gene_column_index = 2, instructions = instrs) 

spatPlot(gobject = obj, cell_color = 'in_tissue', show_image = T, point_alpha = 0.7, save_param = list(save_name = 'unfiltered_spatplot_image')) 

obj <- updateGiottoImage(obj, image_name = 'image', ymin_adj = 1400, ymax_adj = 2600, xmax_adj = 2200, xmin_adj = 2300)

spatPlot(gobject = obj, cell_color = 'in_tissue', show_image = T, point_alpha = 0.7, save_param = list(save_name = 'spatplot_image'))
pDataDT(obj) # for accessing phenodata- cel ID, etc., everything should be in tissue (in tissue = 1) since using filtered cell ranger results

obj <- normalizeGiotto(gobject = obj, scalefactor = 6000, verbose = T) # log2 normalize data by genes and cells
obj <- addStatistics(gobject = obj) # add number of genes and percent of genes to pData
spatPlot2D(gobject = obj, show_image = T, point_alpha = 0.7, cell_color = c("nr_genes"), color_as_factor = F, save_param = list(save_name = 'numberofgenes_aligned'))
obj <- calculateHVG(gobject = obj, save_param = list(save_name = 'obj-02_HVGplot_6-23-2021'))
gene_metadata = fDataDT(obj) # visualize gene metadata
featgenes = gene_metadata[hvg == 'yes' & perc_cells > 3 & mean_expr_det > 0.4]$gene_ID # select genes to use for PCA input, these were suggested input parameters in Giotto but can/should be modified in future
obj <- runPCA(gobject = obj, genes_to_use = featgenes, scale_unit = F, center = T, method = "factominer")
screePlot(obj, ncp = 30, save_param = list(save_name = 'screeplot'))
obj <- runUMAP(obj, dimensions_to_use = 1:30)
plotUMAP(gobject = obj, save_param = list(save_name = 'UMAP_PC30'))
obj <- createNearestNetwork(gobject = obj, dimensions_to_use = 1:30)

obj <- doLeidenCluster(gobject = obj, resolution = 0.4)

plotUMAP(gobject = obj, cell_color = 'leiden_clus', show_NN_network = T, point_size = 2.5, save_param = list(save_name = 'UMAP-leiden'))

spatDimPlot(gobject = obj, cell_color = 'leiden_clus', dim_point_size = 2, spat_point_size = 2.5, save_param = list(save_name = 'obj-02_covisleiden_6-23-2021')) # this plot will show both clusters or other features on umap and on tissue section

spatDimPlot(gobject = obj, cell_color = 'nr_genes', color_as_factor = F, dim_point_size = 2, spat_point_size = 2.5, save_param = list(save_name = 'obj-02_covisnr_genes_6-23-2021'))

markers_scran <- findMarkers_one_vs_all(gobject = obj, method = "scran", expression_values = "normalized", cluster_column = 'leiden_clus', logFC = 0.5)
markers_scran_small <- markers_scran[markers_scran$FDR < 0.05,]
top10 <- markers_scran_small %>% group_by(cluster) %>% top_n(n=10, wt = logFC)
violinPlot(obj, genes = unique(top10$genes), cluster_column = 'leiden_clus', strip_text = 8, strip_position = 'right', save_param = list(save_name = 'violinplot_scran_giotto', base_width = 5, base_height = 30))

# PLOT on UMAP
dimGenePlot(obj, expression_values = 'scaled', genes = unique(top10$genes), cow_n_col = 3, point_size = 1, save_param = list(save_name = 'scran_top10_umap', base_width = 8, base_height = 30))

# PLOT on spatial slice:
spatGenePlot(gobject = obj, genes = unique(top10$genes), cow_n_col = 3, point_size = 2, save_param = list(save_name = '_scran_top10_onspatplot')) 

sign_matrix <- read.table(file = "", header = TRUE, as.is = TRUE, sep = ",", row.names = 1)

obj <- runSpatialDeconv(gobject = obj, deconv_method = "DWLS", expression_values = "normalized", logbase = 2, cluster_column = "leiden_clus", sign_matrix = sign_matrix, n_cell = 10, cutoff = 2, name = "DWLS_1")

spatDimCellPlot(gobject = obj, spat_enr_names = "DWLS_1", cell_annotation_values = colnames(sign_matrix), cow_n_col = 1, spat_point_size =1, plot_alignment = "horizontal", save_param = list(save_name = "DWLS_spatDimCellPlot", base_width = 7, base_height = 40))

saveRDS(object = obj, file = "")
ndelrossi7 commented 3 years ago

Thanks for your patience! Just to clarify, this code (specifically runSpatialDeconv()) and a matrix of the same format has worked for you before but with other data?

DavidM222555 commented 3 years ago

Thanks for being so responsive! Yes, this code has worked for similarly formatted data.

ndelrossi7 commented 3 years ago

@DavidM222555 I'm working on reproducing this error and will connect with you once I have more information/if I have any questions for you!

DavidM222555 commented 3 years ago

Thanks! I greatly appreciate it! @ndelrossi7

ndelrossi7 commented 3 years ago

Hi @DavidM222555 thanks for your patience! I think the issue may be the format of the matrix you are inputting. The function makeSignMatrixDWLS() outputs a an object with the class of "matrix" and "array". I converted my matrix to type list to try to replicate the problem you had and I received an error. Can you try converting your data into class matrix and see if this helps? Let me know if this doesn't work or if you have other questions!