Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Created February 16, 2025 22:18
Show Gist options
  • Save vjcitn/8717325bee449c751d081f6a3761d568 to your computer and use it in GitHub Desktop.
Save vjcitn/8717325bee449c751d081f6a3761d568 to your computer and use it in GitHub Desktop.
extraction of shapes and counts of transcripts within shape boundaries
library(SpatialData)
library(SpatialData.data)
library(sf)
library(SummarizedExperiment)
if (!exists("mb")) mb = MouseBrainMERFISH()
make_circles = function(spd, shapetype="cells") {
cshape = SpatialData::shapes(spd)[[shapetype]]
df1 = data(cshape) |> as.data.frame()
hh = df1$geometry |> wk::wk_coords()
cents = st_as_sf(hh[,c("x", "y")], coords=c(1,2))
st_buffer(cents, dist=df1$radius)
}
make_points = function(spd, pointtype="single_molecule") {
p = SpatialData::points(spd)[[pointtype]]
tmp = p |> dplyr::select(x,y) |> as.data.frame()
st_as_sf(tmp, coords=c("x", "y"))
}
p = make_points(mb)
circs = make_circles(mb)
int = st_intersects(p,circs)
hits = sapply(int, length) # how often is a point found within cell
sum(hits)
sum(assay(SpatialData::table(mb)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment