Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying ext and spatShift tranformations on gobj #945

Closed
rbutleriii opened this issue Apr 24, 2024 · 2 comments
Closed

Applying ext and spatShift tranformations on gobj #945

rbutleriii opened this issue Apr 24, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@rbutleriii
Copy link

rbutleriii commented Apr 24, 2024

Is there a way to apply the spatial transformations to the entire giotto object, including the images?

I am joining multiple gobjs via joinGiottoObjects shift method, each with several fovs. However each has global coordinates from it's own original slide.
image

I would like to align them each to xmin = 0, ymin = 0 before joining them. Something like this:

# Load slide objects squares ----------------------------------------
my_slides <- unlist(strsplit(slides, ","))
gobjs <- lapply(my_slides, function(i){
  print(sprintf("Loading %s giotto object...", i))
  loadGiotto(file.path(data_path, paste(i, group, "giotto_obj", sep=".")))
})

# shift each slide to the bottom-left most global coords (0,0) prior to merging
gobjs <- lapply(gobjs, function(i) {
  coords <- ext(i)
  spatShift(i, dx = -coords$xmin, dy = -coords$ymin)
})

# merge the slides 
gobj <- joinGiottoObjects(
  gobjs,
  gobject_names = my_slides,
  join_method = "shift",
  x_shift = NULL,
  y_shift = NULL,
  x_padding = 0,
  y_padding = NULL,
  verbose = TRUE
)

That presents two problems: (1) figuring out the absolute bounds of all parts and (2) applying to all parts.

For the first, ext does not apply to a gobj:

library(Giotto)
library(GiottoData)

viz <- loadGiottoMini(dataset = 'vizgen')
activeSpatUnit(viz) <- 'aggregate'

> ext(viz)
Error in (function (classes, fdef, mtable)  :
  unable to find an inherited method for functionextfor signature"giotto"

It would appear that I would have to call it on all images, spat_locs, spatNetwork, feature_info, and all polygon types and find the absolute xmin and ymin?

For the second, spatShift does appear to apply to a gobj, and seems to shift everything but the images. So for that do I basically have to modify the ext for each image after the initial? I was trying unsuccessfully to apply that with set.ext, but the way it works is counterintuitive to say the least

library(Giotto)
library(GiottoData)

go <- loadGiottoMini(dataset = 'cosmx')

go <- spatShift(go, dx = 0, dy = 150000)
lapply(names(go@images), function(i) {
  img <- getGiottoImage(go, image_type = 'image', name = i)
  e <- ext(img)
  e <- e + c(-0, 0, -150000, 150000) # I do not understand why it subtracts from ymin instead of adding
  terra::set.ext(img, e) # this fails unable to find an inherited method for function ‘set.ext’ for signature ‘"giottoImage"’
  setGiottoImage(go, 
    image = img, 
    image_type = 'image', 
    name = i
  )
})
# now the same for largeImages
@rbutleriii rbutleriii added the bug Something isn't working label Apr 24, 2024
@jiajic
Copy link
Member

jiajic commented May 13, 2024

Hi @rbutleriii,

These functionalities should have just been added in GiottoClass 0.3.0

  • ext() now works on Giotto objects. It has a couple of params intended for finding the SpatExtent of specific pieces of information, but defaults to all the information in the object.
  • rescale() and spatShift() should also work on all the info in the Giotto object by default.
  • giottoImage extent is now settable. We don't provide methods for set.ext(), and have instead been writing methods for the ext()<- replacement function. Currently, the SpatExtent of giottoImage, giottoLargeImage, giottoPoints, and giottoPolygon are directly settable this way.

For the behavior of the SpatExtent object, we import this functionality from terra.
Numeric additions to a SpatExtent will expand one of the bounds, which is why you needed to apply negative values in order perform a shift.

Best,
George

@jiajic jiajic self-assigned this May 13, 2024
@jiajic jiajic closed this as completed May 22, 2024
@rbutleriii
Copy link
Author

Looks good:

library(Giotto)
library(GiottoData)

go <- loadGiottoMini(dataset = 'cosmx')
go <- spatShift(go, dx = 60000, dy = 150000)
go2 <- loadGiottoMini(dataset = 'cosmx')
my_slides <- c("go", "go2")
gobjs <- list(go, go2)


# merge the slides 
gobj <- joinGiottoObjects(
  gobjs,
  gobject_names = my_slides,
  join_method = "shift",
  x_shift = NULL,
  y_shift = NULL,
  x_padding = 0,
  y_padding = NULL,
  verbose = TRUE
)

# plot merged obj
img_list = grep("*-composite", names(gobj@images), value = TRUE)

gobj %>%
  spatPlot2D(
    point_size = 2,
    point_alpha = 0.5,
    show_image = TRUE,
    image_name = img_list,
    return_plot = FALSE,
    show_plot = TRUE
  )

image

# shift each slide to the bottom-left most global coords (0,0) prior to merging
gobjs <- lapply(gobjs, function(i) {
  coords <- ext(i)
  spatShift(i, dx = -coords$xmin, dy = -coords$ymin)
})

# merge the slides 
gobj <- joinGiottoObjects(
  gobjs,
  gobject_names = my_slides,
  join_method = "shift",
  x_shift = NULL,
  y_shift = NULL,
  x_padding = 0,
  y_padding = NULL,
  verbose = TRUE
)

gobj %>%
  spatPlot2D(
    point_size = 2,
    point_alpha = 0.5,
    show_image = TRUE,
    image_name = img_list,
    return_plot = FALSE,
    show_plot = TRUE
  )

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants