drieslab / Giotto

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

I did not find the slice@minmax parameter, slice@minmax parameter, slice@boundaries parameter, etc. Where should I find them? #931

Closed sunjiahe-hub closed 1 month ago

sunjiahe-hub commented 3 months ago

Hello dear developers, Giotto is a very powerful tool. In version 3.3, I tried to write the function getNoshiftSpatialLocs to obtain the coordinate position of each slice in a multi-slice sample before it was translated; But in the latest version V4.0.5, the Giotto data structure has undergone major changes. I did not find the slice@minmax parameter, slice@minmax parameter, slice@boundaries parameter, etc. Where should I find them?

My purpose is to get the original coordinate position of each slice without translation.

The getNoshiftSpatialLocs function is as follows:

#' @title getNoshiftSpatialLocs
#' @name Function getNoshiftSpatialLocs
#' @description Coveret shiftData To NoShiftData
#' @param object giotto object
#' @param cell_meta_anno cell meta table
#' @param spatial_locs spatial location
#' @importFrom dplyr mutate arrange

getNoshiftSpatialLocs <- function(object,cell_meta_anno = NULL,spatial_locs = NULL) {
  add_to_x_dict <- list()
  slice_names <- names(object@images)
  for (i in seq_along(slice_names)) {
    slice <- object@images[[i]]

    xmax_sloc <- slice@minmax[[1]]
    xmin_sloc <- slice@minmax[[2]]

    xmax_adj <- slice@boundaries[[1]]
    xmin_adj <- slice@boundaries[[2]]

    add_to_x <- (i-1) * (xmax_sloc - xmin_sloc + xmax_adj + xmin_adj) + (i-1) * 1000

    add_to_x_dict[[slice@name]] <- add_to_x
  }

  slice_names <- sub("^(.*?)\\-.*", "\\1", spatial_locs$cell_ID)

  slice_names <- paste0(slice_names, "-image")

  add_to_x_values <- unlist(add_to_x_dict[slice_names])

  processed_spatial_locs <- spatial_locs %>%
    dplyr::mutate(sdimx = sdimx - add_to_x_values,
           sdimy = abs(sdimy)) %>%
    dplyr::arrange(match(cell_ID, cell_meta_anno$list_ID))

  return(processed_spatial_locs)
}
jiajic commented 1 month ago

Hi @sunjiahe-hub,

Apologies for the slow reply. It looks like what your code is doing is finding the x coordinate start that an image is mapped to when using joinGiottoObjects() with the default x padding value of 1000 between datasets. Then it applies that x coordinate as the x shift for the spatial locations and ensures that the y values are positive.

With Giotto 4.0.8, you can run ext(slice), where slice is your giottoImage or giottoLargeImage object and that will return a SpatExtent object which contains the final mapped xmin, xmax, ymin, ymax. You can run ext(slice)[][["xmin"]] to directly get the add_to_x value you calculate above.

I also wanted to mention that there are some spatial manipulation generics that you can use with spatLocsObj (the Giotto subobject that encapsulates the spatial_locs information) spatShift() can perform the x shift you apply to the coordinates and flip() will flip the entire object across the x axis by default, which might be a safer alternative than abs() for cases where the dataset coordinates straddle the x axis.

I also want to give a heads up that Giotto 4.0.8 is now using the @images slot for both giottoImages and giottoLargeImages.

Best, George