MarcElosua / SPOTlight

Spatial Transcriptomics Capture Location Deconvolution
https://marcelosua.github.io/SPOTlight
GNU General Public License v3.0
164 stars 25 forks source link

function plotSpatialScatterpie with image=TRUE does not work #86

Open sondvo opened 1 year ago

sondvo commented 1 year ago

my code:

plotSpatialScatterpie(
    x = spatial_data,
    y = deconvolution_matrix,
    cell_types = colnames(deconvolution_matrix),
    img = TRUE,                                  # TRUE if you want to view image in background
    scatterpie_alpha = 1) +
    scale_fill_manual(
        values = pal,
        breaks = names(pal))

Error:

Error in plotImage(x = img): is.matrix(x) | is.character(x) | is.array(x) | is(x, "rastergrob") | .... is not TRUE Traceback:

  1. plotSpatialScatterpie(x = spatial_data, y = deconvolution_matrix, . cell_types = colnames(deconvolution_matrix), img = TRUE, . scatterpie_alpha = 1)
  2. plotImage(x = img)
  3. stopifnot(is.matrix(x) | is.character(x) | is.array(x) | is(x, . "rastergrob") | is(x, "Seurat") | is(x, "SpatialExperiment"), . is.null(slice) | is.character(slice))

str(spatial_data):

Formal class 'SpatialExperiment' [package "SpatialExperiment"] with 9 slots ..@ int_elementMetadata:Formal class 'DFrame' [package "S4Vectors"] with 6 slots .. .. ..@ rownames : NULL .. .. ..@ nrows : int 16687 .. .. ..@ elementType : chr "ANY" .. .. ..@ elementMetadata: NULL .. .. ..@ metadata : list() .. .. ..@ listData :List of 1 .. .. .. ..$ rowPairs:Formal class 'DFrame' [package "S4Vectors"] with 6 slots .. .. .. .. .. ..@ rownames : NULL .. .. .. .. .. ..@ nrows : int 16687 .. .. .. .. .. ..@ elementType : chr "ANY" .. .. .. .. .. ..@ elementMetadata: NULL .. .. .. .. .. ..@ metadata : list() .. .. .. .. .. ..@ listData : Named list() ..@ int_colData :Formal class 'DFrame' [package "S4Vectors"] with 6 slots .. .. ..@ rownames : NULL .. .. ..@ nrows : int 1438 .. .. ..@ elementType : chr "ANY" .. .. ..@ elementMetadata: NULL .. .. ..@ metadata : list() .. .. ..@ listData :List of 4 .. .. .. ..$ reducedDims :Formal class 'DFrame' [package "S4Vectors"] with 6 slots .. .. .. .. .. ..@ rownames : NULL .. .. .. .. .. ..@ nrows : int 1438 .. .. .. .. .. ..@ elementType : chr "ANY" .. .. .. .. .. ..@ elementMetadata: NULL .. .. .. .. .. ..@ metadata : list() .. .. .. .. .. ..@ listData : Named list() .. .. .. ..$ altExps :Formal class 'DFrame' [package "S4Vectors"] with 6 slots .. .. .. .. .. ..@ rownames : NULL .. .. .. .. .. ..@ nrows : int 1438 .. .. .. .. .. ..@ elementType : chr "ANY" .. .. .. .. .. ..@ elementMetadata: NULL .. .. .. .. .. ..@ metadata : list() .. .. .. .. .. ..@ listData : Named list() .. .. .. ..$ colPairs :Formal class 'DFrame' [package "S4Vectors"] with 6 slots .. .. .. .. .. ..@ rownames : NULL .. .. .. .. .. ..@ nrows : int 1438 .. .. .. .. .. ..@ elementType : chr "ANY" .. .. .. .. .. ..@ elementMetadata: NULL .. .. .. .. .. ..@ metadata : list() .. .. .. .. .. ..@ listData : Named list() .. .. .. ..$ spatialCoords: int [1:1438, 1:2] 4116 6932 6669 5279 6037 5839 4938 5009 5504 7698 ... .. .. .. .. ..- attr(, "dimnames")=List of 2 .. .. .. .. .. ..$ : chr [1:1438] "AAACCGTTCGTCCAGG-1" "AAACCTAAGCAGCCGG-1" "AAACGAGACGGTTGAT-1" "AAACGGTTGCGAACTG-1" ... .. .. .. .. .. ..$ : chr [1:2] "pxl_col_in_fullres" "pxl_row_in_fullres" ..@ int_metadata :List of 3 .. ..$ version :Classes 'package_version', 'numeric_version' hidden list of 1 .. .. ..$ : int [1:3] 1 20 0 .. ..$ spatialDataNames: chr(0) .. ..$ imgData :Formal class 'DFrame' [package "S4Vectors"] with 6 slots .. .. .. ..@ rownames : NULL .. .. .. ..@ nrows : int 1 .. .. .. ..@ elementType : chr "ANY" .. .. .. ..@ elementMetadata: NULL .. .. .. ..@ metadata : list() .. .. .. ..@ listData :List of 4 .. .. .. .. ..$ sample_id : chr "10X_Mouse_Kidney" .. .. .. .. ..$ image_id : chr "hires" .. .. .. .. ..$ data :List of 1 .. .. .. .. .. ..$ :Formal class 'StoredSpatialImage' [package "SpatialExperiment"] with 1 slot .. .. .. .. .. .. .. ..@ path: chr "/home/ub-sonvo-25d094476064960/spatial/SPOTlight/data/SPOTlight/sample/outs/spatial/tissue_hires_image.png" .. .. .. .. ..$ scaleFactor: num 0.177 ..@ rowRanges :Formal class 'CompressedGRangesList' [package "GenomicRanges"] with 5 slots .. .. ..@ unlistData :Formal class 'GRanges' [package "GenomicRanges"] with 7 slots .. .. .. .. ..@ seqnames :Formal class 'Rle' [package "S4Vectors"] with 4 slots .. .. .. .. .. .. ..@ values : Factor w/ 0 levels: .. .. .. .. .. .. ..@ lengths : int(0) .. .. .. .. .. .. ..@ elementMetadata: NULL .. .. .. .. .. .. ..@ metadata : list() .. .. .. .. ..@ ranges :Formal class 'IRanges' [package "IRanges"] with 6 slots .. .. .. .. .. .. ..@ start : int(0) .. .. .. .. .. .. ..@ width : int(0) .. .. .. .. .. .. ..@ NAMES : NULL .. .. .. .. .. .. ..@ elementType : chr "ANY" .. .. .. .. .. .. ..@ elementMetadata: NULL .. .. .. .. .. .. ..@ metadata : list() .. .. .. .. ..@ strand :Formal class 'Rle' [package "S4Vectors"] with 4 slots .. .. .. .. .. .. ..@ values : Factor w/ 3 levels "+","-","": .. .. .. .. .. .. ..@ lengths : int(0) .. .. .. .. .. .. ..@ elementMetadata: NULL .. .. .. .. .. .. ..@ metadata : list() .. .. .. .. ..@ seqinfo :Formal class 'Seqinfo' [package "GenomeInfoDb"] with 4 slots .. .. .. .. .. .. ..@ seqnames : chr(0) .. .. .. .. .. .. ..@ seqlengths : int(0) .. .. .. .. .. .. ..@ is_circular: logi(0) .. .. .. .. .. .. ..@ genome : chr(0) .. .. .. .. ..@ elementMetadata:Formal class 'DFrame' [package "S4Vectors"] with 6 slots .. .. .. .. .. .. ..@ rownames : NULL .. .. .. .. .. .. ..@ nrows : int 0 .. .. .. .. .. .. ..@ elementType : chr "ANY" .. .. .. .. .. .. ..@ elementMetadata: NULL .. .. .. .. .. .. ..@ metadata : list() .. .. .. .. .. .. ..@ listData : Named list() .. .. .. .. ..@ elementType : chr "ANY" .. .. .. .. ..@ metadata : list() .. .. ..@ elementMetadata:Formal class 'DFrame' [package "S4Vectors"] with 6 slots .. .. .. .. ..@ rownames : NULL .. .. .. .. ..@ nrows : int 16687 .. .. .. .. ..@ elementType : chr "ANY" .. .. .. .. ..@ elementMetadata: NULL .. .. .. .. ..@ metadata : list() .. .. .. .. ..@ listData :List of 1 .. .. .. .. .. ..$ symbol: chr [1:16687] "0610005C13Rik" "0610009B22Rik" "0610009L18Rik" "0610010K14Rik" ... .. .. ..@ elementType : chr "GRanges" .. .. ..@ metadata : list() .. .. ..@ partitioning :Formal class 'PartitioningByEnd' [package "IRanges"] with 5 slots .. .. .. .. ..@ end : int [1:16687] 0 0 0 0 0 0 0 0 0 0 ... .. .. .. .. ..@ NAMES : chr [1:16687] "0610005C13RIK" "0610009B22RIK" "0610009L18RIK" "0610010K14RIK" ... .. .. .. .. ..@ elementType : chr "ANY" .. .. .. .. ..@ elementMetadata: NULL .. .. .. .. ..@ metadata : list() ..@ colData :Formal class 'DFrame' [package "S4Vectors"] with 6 slots .. .. ..@ rownames : chr [1:1438] "AAACCGTTCGTCCAGG-1" "AAACCTAAGCAGCCGG-1" "AAACGAGACGGTTGAT-1" "AAACGGTTGCGAACTG-1" ... .. .. ..@ nrows : int 1438 .. .. ..@ elementType : chr "ANY" .. .. ..@ elementMetadata: NULL .. .. ..@ metadata : list() .. .. ..@ listData :List of 4 .. .. .. ..$ in_tissue: logi [1:1438] TRUE TRUE TRUE TRUE TRUE TRUE ... .. .. .. ..$ array_row: int [1:1438] 52 65 35 67 66 45 62 55 24 44 ... .. .. .. ..$ array_col: int [1:1438] 42 83 79 59 70 67 54 55 62 94 ... .. .. .. ..$ sample_id: chr [1:1438] "10X_Mouse_Kidney" "10X_Mouse_Kidney" "10X_Mouse_Kidney" "10X_Mouse_Kidney" ... ..@ assays :Formal class 'SimpleAssays' [package "SummarizedExperiment"] with 1 slot .. .. ..@ data:Formal class 'SimpleList' [package "S4Vectors"] with 4 slots .. .. .. .. ..@ listData :List of 1 .. .. .. .. .. ..$ counts:Formal class 'DelayedMatrix' [package "DelayedArray"] with 1 slot .. .. .. .. .. .. .. ..@ seed:Formal class 'DelayedSubset' [package "DelayedArray"] with 2 slots .. .. .. .. .. .. .. .. .. ..@ index:List of 2 .. .. .. .. .. .. .. .. .. .. ..$ : int [1:16687] 12222 19113 20672 19512 26587 10210 5568 14571 3888 9179 ... .. .. .. .. .. .. .. .. .. .. ..$ : NULL .. .. .. .. .. .. .. .. .. ..@ seed :Formal class 'TENxMatrixSeed' [package "HDF5Array"] with 6 slots .. .. .. .. .. .. .. .. .. .. .. ..@ filepath : chr "/home/ub-sonvo-25d094476064960/spatial/SPOTlight/data/SPOTlight/sample/outs/filtered_feature_bc_matrix.h5" .. .. .. .. .. .. .. .. .. .. .. ..@ group : chr "/matrix" .. .. .. .. .. .. .. .. .. .. .. ..@ subdata : NULL .. .. .. .. .. .. .. .. .. .. .. ..@ dim : int [1:2] 32285 1438 .. .. .. .. .. .. .. .. .. .. .. ..@ indptr_ranges:'data.frame': 1438 obs. of 2 variables: .. .. .. .. .. .. .. .. .. .. .. .. ..$ start: num [1:1438] 1 5000 11093 19145 25487 ... .. .. .. .. .. .. .. .. .. .. .. .. ..$ width: int [1:1438] 4999 6093 8052 6342 7964 7517 7710 8754 6924 3921 ... .. .. .. .. .. .. .. .. .. .. .. ..@ dimnames :List of 2 .. .. .. .. .. .. .. .. .. .. .. .. ..$ : chr [1:32285] "XKR4" "GM1992" "GM19938" "GM37381" ... .. .. .. .. .. .. .. .. .. .. .. .. ..$ : chr [1:1438] "AAACCGTTCGTCCAGG-1" "AAACCTAAGCAGCCGG-1" "AAACGAGACGGTTGAT-1" "AAACGGTTGCGAACTG-1" ... .. .. .. .. ..@ elementType : chr "ANY" .. .. .. .. ..@ elementMetadata: NULL .. .. .. .. ..@ metadata : list() ..@ NAMES : NULL ..@ elementMetadata :Formal class 'DFrame' [package "S4Vectors"] with 6 slots .. .. ..@ rownames : NULL .. .. ..@ nrows : int 16687 .. .. ..@ elementType : chr "ANY" .. .. ..@ elementMetadata: NULL .. .. ..@ metadata : list() .. .. ..@ listData : Named list() ..@ metadata : list()

HelenaLC commented 1 year ago

Could you perhaps post imgData(spe) here as a code chunk? The above output of str() is hard to digest. Note that spatial_data is expected to be a SpatialExperiment for this to work.

sondvo commented 1 year ago

Hi, this is the class and content of imgData

class(spatial_data) 'SpatialExperiment'

imgData(spatial_data) DataFrame with 1 row and 4 columns sample_id image_id data scaleFactor \<character> \<character> \<list> \<numeric> 1 10X_Mouse_Kidney hires #### 0.176991

HelenaLC commented 1 year ago

Hm, I can't see intuitively what's going wrong. Could you try

  1. Test that the plotting the image works, in principle:
    library(SpatialExperiment)
    plot(imgRaster(spatial_data))
  2. Use SPOTlight's internal function that is used to extract the image (this is being called by plotSpatialScatterpie), which is passed to plotImage where the error occurs.
    x <- SPOTlight:::. extract_image(spatial_data)
    class(x)
    dim(x)
super-hu-man commented 1 year ago

I got the same error, it looks like the error of the function plotImage(), it works after add return(plt) to the function

plotImage <- function(x, slice = NULL) {
# check validity of input arguments
stopifnot(
    # Check for valid x classes
    is.matrix(x) | is.character(x) | is.array(x) | is(x, "rastergrob") |
        is(x, "Seurat") | is(x, "SpatialExperiment"),
    # Check for valid slice classes
    is.null(slice) | is.character(slice))

if (!is.array(x))
    x <- .extract_image(x)

# Plot image
plt <- .plot_image(x)
return(plt)
}
zmbiolab commented 9 months ago

sample is seurat object, My code: img = Seurat::Images(sample)[1], and then get error: Error in .extract_image(x) : file.exists(x) is not TRUE

panxueling commented 5 months ago

plotSpatialScatterpie( x = brain_st_cortex@images$anterior1@coordinates[,c('imagerow','imagecol')], y = mat, cell_types = colnames(mat), img = png::readPNG(img_file),
scatterpie_alpha = 1, pie_scale = 0.4, degrees = -90, axis = "h") + scale_fill_manual( values = pal, breaks = names(pal))

The resulting graph only shows the tissue original I passed along without the pie chart