cnio-bu / beyondcell

Beyondcell is a computational methodology for identifying tumour cell subpopulations with distinct drug responses in single-cell RNA-seq and Spatial Transcriptomics data.
Other
40 stars 4 forks source link

Error in bcClusters/bcSignatures when spatial = TRUE and images = NULL #115

Closed mj-jimenez closed 1 year ago

mj-jimenez commented 1 year ago

This is the error message:

Error in images %||% Images(object = object, assay = DefaultAssay(object = object)) : 
  objet 'in.images' not found

In order to fix it, we have to add these lines in bcClusters and bcSignatures functions:

# Update images check
if (!is.null(images)) {
  if (!spatial) {
    images <- NULL
    warning(paste('images parameter is only valid when spatial = TRUE.', 
                  'Setting images = NULL.'))
  } else {
    in.images <- images %in% names(bc@SeuratInfo$images)
    if (all(!in.images)) stop('None of the specified slides were found.')
    else if (any(!in.images)) {
      warning(paste0('These images were not found in bc: ',
                     paste0(images[!in.images], collapse = ", "), "."))
    }
    images <- images[in.images]
  }
} else if (spatial) images <- names(bc@SeuratInfo$images)

# Update plot section (replace images[in.images] by images
# Example for bcClusters
if (spatial) {
  if (identical(mfrow, c(1, 1))) scale <- NULL
  else scale <- ggplot2::scale_fill_discrete(drop = FALSE)
  p <- Seurat::SpatialDimPlot(sc, images = images, combine = FALSE, ...)
  p <- lapply(seq_along(p), FUN = function(i) {
    suppressMessages(
      p[[i]] + ggplot2::theme_minimal() +
        ggplot2::theme(plot.title = element_text(face = "bold", hjust = 0.5),
                       legend.title = element_blank(), 
                       axis.title = element_blank(), 
                       axis.text = element_blank()) + 
        ggtitle(images[i]) + scale
    )
  })
}

Work-around: specify all the image names in the function call. I.e.:

bcClusters(bcobj, idents = "Phase", spatial = TRUE, 
           images = names(bcobj@SeuratInfo$images))
bcSignatures(bcobj, i, spatial = TRUE, images = names(bcobj@SeuratInfo$images), 
             signatures = list(values = "sig-21462")