I am new to coding, with a background in life sciences/biology.
I am aiming to analyse the co-localisation of leukaemia cells to areas of blood vessels and adipocytes.
I have exported the cell locations as points with marks representing either CKIT positivity or DAPI-only positivity.
I have exported point patterns of the cells in Qupath and annotations of the locations of blood vessels/endomucin stained structures and adipocytes/perilipin stained structures as .geojson files from Qupath.
.geojson file of exported Qupath annotations
The window should be a square box from which I have sampled the tissue.
I have attached an image of the example of how I obtained the points and annotations from qupath. The endomucin and perilipin stained annotations are in white and green respectively, identified through qupath’s pixel classifier which I trained. The points are the cells.
Qupath cell points and annotations
I would like the window to be the square outter box, the points as a multitype point pattern and the annotations for perilipin and endomucin staining to act as spatial covariates for the point pattern analysis. However, how do I best model these latter annotations? After importing them as a .geojson into R, converting to sf format, some the edges are touching when checking with st_is_valid
.
Should I convert this to a pixel image or binary mask? Can they remain as polygons and be converted to owin objects? (such as the Murchison gold data in Baddeley’s wonderful spatial point pattern analysis book). I have not had much luck converting these polygons to owin objects.
The other key part of this is the annotations need to retain their class as either perilipin or endomucin stained… Perhaps this would be easier as pixel images with factor levels?
I have attempted to import the .geojson files and convert them to spatstat based windows with the following code:
library(sf)
library(spatstat)
library(spatstat.geom)
setwd("~/RStudio/ImageAnalysis")
pt_geojson <- "Veh Tibia - Proximal Tibia.geojson"
annotations_sf <- st_read(pt_geojson)
# Check resulting sf object
head(annotations_sf)
annotations_transform <- st_transform(annotations_sf, crs = 26717)
annotations_owin <- as.owin(annotations_transform)
plot(annotations_owin)
Unfortunately this just generates the following error:
Error in if (!all(st_dimension(W) == 2)) stop("as.owin.sfc needs polygonal geometries") :
missing value where TRUE/FALSE needed
I added the following code:
# Check for invalid geometries
invalid_geometries <- st_is_valid(annotations_sf, reason = TRUE)
print(invalid_geometries)
# Fix invalid geometries
annotations_sf <- st_make_valid(annotations_sf)
# Verify geometries are now valid
valid_geometries <- st_is_valid(annotations_sf, reason = TRUE)
print(valid_geometries)
This resulted in the following:
> # Check for invalid geometries
> invalid_geometries <- st_is_valid(annotations_sf, reason = TRUE)
Warning messages:
1: In st_is_longlat(x) :
bounding box has potentially an invalid value range for longlat data
2: In st_is_longlat(x) :
bounding box has potentially an invalid value range for longlat data
> print(invalid_geometries)
[1] "Valid Geometry" "Edge 2 crosses edge 4"
[3] "Edge 16 crosses edge 20" "Valid Geometry"
[5] "Valid Geometry" "Edge 14 crosses edge 20"
[7] "Valid Geometry" "Valid Geometry"
[9] "Edge 0 crosses edge 8" "Edge 16 crosses edge 18"
[11] "Edge 6 crosses edge 8" "Valid Geometry"
[13] "Valid Geometry" "Valid Geometry"
[15] "Edge 0 crosses edge 26" "Edge 4 crosses edge 10"
[17] "Valid Geometry" "Valid Geometry"
[19] "Edge 12 crosses edge 18" "Valid Geometry"
[21] "Edge 0 crosses edge 10" "Valid Geometry"
[23] "Valid Geometry" "Valid Geometry"
[25] "Edge 10 crosses edge 12" "Valid Geometry"
[27] "Valid Geometry" "Valid Geometry"
[29] "Valid Geometry" "Edge 4 crosses edge 24"
>
> # Fix invalid geometries
> annotations_sf <- st_make_valid(annotations_sf)
Warning message:
In st_is_longlat(x) :
bounding box has potentially an invalid value range for longlat data
>
> # Verify geometries are now valid
> valid_geometries <- st_is_valid(annotations_sf, reason = TRUE)
> print(valid_geometries)
[1] "Valid Geometry" "Edge 2 crosses edge 4"
[3] "Edge 16 crosses edge 20" "Valid Geometry"
[5] "Valid Geometry" "Edge 14 crosses edge 20"
[7] "Valid Geometry" "Valid Geometry"
[9] "Edge 0 crosses edge 8" "Edge 16 crosses edge 18"
[11] "Edge 6 crosses edge 8" "Valid Geometry"
[13] "Valid Geometry" "Valid Geometry"
[15] "Edge 0 crosses edge 26" "Edge 4 crosses edge 10"
[17] "Valid Geometry" "Valid Geometry"
[19] "Edge 12 crosses edge 18" "Valid Geometry"
[21] "Edge 0 crosses edge 10" "Valid Geometry"
[23] "Valid Geometry" "Valid Geometry"
[25] "Edge 10 crosses edge 12" "Valid Geometry"
[27] "Valid Geometry" "Valid Geometry"
[29] "Valid Geometry" "Edge 4 crosses edge 24"
And rerunning the plot afterwards generated this horribly merged plot of all the annotations.
Failed polygonal window plot
Callum Fernando is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.