## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ## ----installation, include = TRUE, eval=FALSE--------------------------------- # if (!requireNamespace("BiocManager")) { # install.packages("BiocManager") # } # BiocManager::install("sosta") ## ----setup, message=FALSE----------------------------------------------------- library("sosta") library("dplyr") library("tidyr") library("ggplot2") library("sf") library("SpatialExperiment") theme_set(theme_bw()) ## ----loading, echo=TRUE, message=FALSE---------------------------------------- # load the data data("sostaSPE") sostaSPE ## ----------------------------------------------------------------------------- cbind(colData(sostaSPE), spatialCoords(sostaSPE)) |> as.data.frame() |> ggplot(aes(x = x, y = y, color = cellType)) + geom_point(size = 0.25) + facet_wrap(~imageName) + coord_equal() ## ----------------------------------------------------------------------------- shapeIntensityImage( sostaSPE, marks = "cellType", imageCol = "imageName", imageId = "image1", markSelect = "A" ) ## ----------------------------------------------------------------------------- n <- estimateReconstructionParametersSPE( sostaSPE, marks = "cellType", imageCol = "imageName", markSelect = "A", plotHist = TRUE ) ## ----------------------------------------------------------------------------- (thresSPE <- mean(n$thres)) (bndwSPE <- mean(n$bndw)) ## ----------------------------------------------------------------------------- (struct <- reconstructShapeDensityImage( sostaSPE, marks = "cellType", imageCol = "imageName", imageId = "image1", markSelect = "A", bndw = bndwSPE, dim = 500, thres = thresSPE )) ## ----------------------------------------------------------------------------- cbind( colData(sostaSPE[, sostaSPE$imageName == "image1"]), spatialCoords(sostaSPE[, sostaSPE$imageName == "image1"]) ) |> as.data.frame() |> ggplot(aes(x = x, y = y, color = cellType)) + geom_point(size = 0.25) + facet_wrap(~imageName) + coord_equal() + geom_sf( data = struct, fill = NA, color = "darkblue", inherit.aes = FALSE, # this is important linewidth = 1 ) ## ----------------------------------------------------------------------------- struct2 <- reconstructShapeDensityImage( sostaSPE, marks = "cellType", imageCol = "imageName", imageId = "image1", markSelect = "A", dim = 500 ) ## ----------------------------------------------------------------------------- cbind( colData(sostaSPE[, sostaSPE$imageName == "image1"]), spatialCoords(sostaSPE[, sostaSPE$imageName == "image1"]) ) |> as.data.frame() |> ggplot(aes(x = x, y = y, color = cellType)) + geom_point(size = 0.25) + facet_wrap(~imageName) + coord_equal() + geom_sf( data = struct2, fill = NA, color = "darkblue", inherit.aes = FALSE, # this is important linewidth = 1 ) ## ----eval=TRUE---------------------------------------------------------------- allStructs <- reconstructShapeDensitySPE( sostaSPE, marks = "cellType", imageCol = "imageName", markSelect = "A", bndw = bndwSPE, thres = thresSPE, nCores = 1 ) ## ----------------------------------------------------------------------------- assign <- assingCellsToStructures(sostaSPE, allStructs, imageCol = "imageName", nCores = 1 ) sostaSPE$structAssign <- assign ## ----------------------------------------------------------------------------- cbind(colData(sostaSPE), spatialCoords(sostaSPE)) |> as.data.frame() |> ggplot(aes(x = x, y = y, color = structAssign)) + geom_point(size = 0.25) + facet_wrap(~imageName) + coord_equal() ## ----------------------------------------------------------------------------- cellTypeProportions(sostaSPE, "structAssign", "cellType") ## ----------------------------------------------------------------------------- (shapeMetrics <- totalShapeMetrics(allStructs)) cbind(allStructs, t(shapeMetrics)) |> ggplot() + geom_sf(aes(fill = Area)) + facet_wrap(~imageName) ## ----------------------------------------------------------------------------- sostaSPE$minDist <- minBoundaryDistances( sostaSPE, "imageName", "structAssign", allStructs ) cbind(colData(sostaSPE), spatialCoords(sostaSPE)) |> as.data.frame() |> ggplot(aes(x = x, y = y, color = minDist)) + geom_point(size = 0.25) + facet_wrap(~imageName) + coord_equal() + scale_colour_gradient2() + geom_sf( data = allStructs, fill = NA, inherit.aes = FALSE ) + facet_wrap(~imageName) ## ----------------------------------------------------------------------------- sostaSPE$border <- ifelse(abs(sostaSPE$minDist) < 3, TRUE, FALSE) cbind(colData(sostaSPE), spatialCoords(sostaSPE)) |> as.data.frame() |> ggplot(aes(x = x, y = y, color = border)) + geom_point(size = 0.25) + facet_wrap(~imageName) + coord_equal() + geom_sf( data = allStructs, fill = NA, inherit.aes = FALSE ) + facet_wrap(~imageName) ## ----------------------------------------------------------------------------- borders <- lapply( st_geometry(allStructs), \(x) st_difference(st_buffer(x, 3), st_buffer(x, -3)) ) |> # both needed for proper conversion st_as_sfc() |> st_as_sf() borders$imageName <- allStructs$imageName borders$borderID <- paste0("border_", allStructs$structID) sostaSPE$borderSf <- assingCellsToStructures(sostaSPE, borders, imageCol = "imageName", uniqueId = "borderID", nCores = 1 ) ## ----------------------------------------------------------------------------- cbind(colData(sostaSPE), spatialCoords(sostaSPE)) |> as.data.frame() |> ggplot(aes(x = x, y = y, color = borderSf)) + geom_point(size = 0.25) + facet_wrap(~imageName) + coord_equal() + geom_sf( data = borders, fill = NA, inherit.aes = FALSE ) + facet_wrap(~imageName) ## ----sessionInfo-------------------------------------------------------------- sessionInfo()