Open alikhuseynov opened 9 months ago
Hi Alik,
This sounds like you'd be interested in SeuratObject::Overlay()
Crop()
was designed for simple x/y subsets, whereas Overlay()
takes an FOV
object x
and subsets it to the region defined in another spatial object y
(or to the region outside of y
if invert = TRUE
). Currently, Overlay()
works with classes defined by sp
or other FOV
objects, but uses sf
to perform the actual overlay (we don't use sf
objects directly as sf
is an incredibly heavy package due to its sysreqs)
Can you give Overlay()
a whirl and see if that works for you? I'd also be happy to discuss any potential changes/improvements to Overlay()
that you can think of
Hi Paul,
Thanks for that. I will try out, so the spatial obj y
can be just sp
object representing the convex hull/polygon to overlay with?
I think letting user to provide selected xy tissue coordinates (which makes the polygon) to crop out or preserve the desired region would be awesome.
Attached is the example done with my convex hull crop function
Hi Alik,
Yep; currently, we've implemented Overlay()
where x
is an FOV
(v4 spatial, no support for v3 spatial objects) object and y
is an sp::SpatialPolygons
object (cf. sp
's constructor function). A new method to Overlay()
where y
is a data.frame
or matrix
with coordinates to handle the automatic coercion to sp::SpatialPolygons
would be nice and something we'd happily take a PR for
If Overlay()
doesn't cover the functionality you need, we can talk more about integrating Crop_custom()
into SeuratObject. As it stands:
rlang::check_installed()
should be used for checking suggested packagessf
should not be used unconditionally; instead, it should be checked for with rlang::check_installed()
and used conditionally in the sf::<function>()
form (example)rlang::abort()
and rlang::warn()
should be used in place of stop()
and warning()
, respectivelyprogressr::progressor()
objects should be used instead of message()
(example)future
instead of BiocParallel
(see the future.apply
package for future-enabled *apply()
functions) (example)sessionInfo()$basePkgs
) should be used in favor over dplyr
and magrittr
(pipe functionality can be had with the R-native |>
as the next version of SeuratObject will require R >= 4.2)inherits()
instead of grep(x = class())
(both data.table
and tibble
inherit from data.frame
, so inherits(data.table(), "data.frame")
and inherits(tibble(), "data.frame")
both return TRUE
) (example for checking multiple unrelated classes at once)tibble
or data.table
functions should be conditional (like sf
)UpdateSeuratObject()
should not be called; if you want to ensure object validity, use validObject()
(example)Sounds good, thanks for the tips!
I think implementing a part to Overlay()
where y
is a data.frame
to make a SpatialPolygons
polygon would be very useful. I can play around and see how it works.
I think implementing a part to
Overlay()
wherey
is adata.frame
to make aSpatialPolygons
polygon would be very useful
This could (and should) be done with a new S4 method for Overlay()
à la
setMethod(
f = 'Overlay',
signature = c(x = 'ANY', y = 'data.frame'),
definition = function(x, y, invert = FALSE, ...) {
# validate `y` to match requirements of `sp::SpatialPolygons()`
# note: class checking with `inherits()` is not needed as `y` is
# guaranteed to be a `data.frame` or a derivative of one (eg. `tibble`)
stopifnot(.ValidCoords(y)) # or however the validation checks are structured
# convert `y` to `SpatialPolygons using `sp::SpatialPolygons()`
y <- sp::SpatialPolygons(y) # or however the final call is structured
# re-trigger method dispatch for `Overlay()` to select a method where `y` is a `SpatialPolygons`
return(Overlay(x = x, y = y, invert = invert, ...))
}
)
I don't currently have the bandwidth to build this out, but would happily take PR that implements this
sounds good, I will take a look.
Hi there, Partially related to this #3552 I'm wondering someone is already working on supporting convex hull like for cropping. Currently it's only possible to crop a box given x and y coordinates range. I implemented a small function for custom crop to remove any tissue area using
sf
package. It would be a good idea to integrate such support withinSeuratObject::Crop
@mojaveazure any suggestions? I could try to PR that. Thanks