Git Product home page Git Product logo

lisaclust's Introduction

lisaClust

Clustering of Local Indicators of Spatial Association

Overview

lisaClust provides a series of functions to identify and visualise regions of tissue where spatial associations between cell-types is similar. This package can be used to provide a high-level summary of cell-type colocalisation in multiplexed imaging data that has been segmented at a single-cell resolution.

Installation

For the Bioconductor release version, run the following.

if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("lisaClust")

If you would like the most up-to-date features, install the most recent development version.

# Install the development version from Bioconductor:
if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

# The following initializes usage of Bioc devel
# This will update all your Bioconductor packages to devel version
BiocManager::install(version='devel')

BiocManager::install("lisaClust")

# Otherwise install the development version from GitHub:
# install.packages("devtools")
devtools::install_github("ellispatrick/lisaClust")
library(lisaClust)

Submitting an issue or feature request

listClust is still under active development. We would greatly appreciate any and all feedback related to the package.

  • R package related issues should be raised here.
  • For general questions and feedback, please contact us directly via [email protected].

Authors

Citation

Patrick, E, Canete, NP, Iyengar, SS, Harman, AN, Sutherland, GT, Yang, P. Spatial analysis for highly multiplexed imaging data to identify tissue microenvironments. Cytometry. 2023. https://doi.org/10.1002/cyto.a.24729

lisaclust's People

Contributors

nick-robo avatar ellispatrick avatar jwokaty avatar nturaga avatar alexrunqin avatar rubak avatar

Stargazers

Lambda Moses avatar TY Qian avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

spatstat-revdep

lisaclust's Issues

Remove fast = FALSE

We should depreciate fast = FALSE and remove the warning If you run out of memory, try 'fast = FALSE'.

.format_data Gone Missing

.format_data function can't be found.

library(lisaClust)
IMC <- lisaClust(IMC, k = 10, sigma = 50, BPPARAM = MulticoreParam(16), fast = FALSE,
                 imageID = "ImageNumber", cellType = "description",
                 spatialCoords = c("Location_Center_X", "Location_Center_Y"))
Error in lisaClust(IMC, k = 10, sigma = 50, BPPARAM = MulticoreParam(16),  : 
  object '.format_data' not found

It looks like it is expected to be housed in SpicyR: cd <- spicyR:::.format_data(cells, imageID, cellType, spatialCoords, FALSE).

Hatching plot

Need to convert region to factor if it is numeric

Add in the effect of sigma to the vignette

There's an example in the lisaClust paper that demonstrates the effect of sigma on the regions identified by lisaClust using the mouse olfactory bulb as an example image.

Quicker hatching

We might be able to do faster hatching using ggpatterns. Below is some prelim code. The main development would be defining nice looking hatching patterns with what is available. Their defaults aren't great for our purposes.

library(ggpattern)
library(tidySingleCellExperiment)
library(class)

######## Map predicted regions to a regular grid
#' @importFrom class knn
#' @importFrom grid linesGrob gpar gList
#' @importFrom spatstat.geom as.mask
regionGrid <- function(pp, nbp = 250) {
ow <- pp$window
m <- spatstat.geom::as.mask(ow, dimyx = c(nbp, nbp))$m
x <-
seq(
from = ow$xrange[1],
to = ow$xrange[2],
length.out = nrow(m)
)
y <-
seq(
from = ow$yrange[1],
to = ow$yrange[2],
length.out = ncol(m)
)
grid <- expand.grid(x = x, y = y)
grid <- data.frame(x = grid[, 1], y = grid[, 2])
df <- as.data.frame(pp)
k <- rep(NA, length(m))
K <-
class::knn(
train = df[, c("x", "y")],
test = grid[t(m)[seq_len(length(m))],],
cl = pp$region,
k = 1
)
k[t(m)[seq_len(length(m))]] <- as.character(K)
data.frame(grid, regions = k)
}

######## Convert grid of regions into a polygon mask for a particular region.
#' @importFrom spatstat.geom owin as.polygonal
regionPoly <- function(grid, region) {
rx <- range(grid$x)
ry <- range(grid$y)
mat <-
matrix(
grid$regions == region,
nrow = length(unique(grid$x)),
ncol = length(unique(grid$y)),
byrow = TRUE
)
mat[is.na(mat)] <- FALSE
ow <- spatstat.geom::owin(xrange = rx,
yrange = ry,
mask = mat)
return(spatstat.geom::as.polygonal(ow))
}

data("kerenSPE", package = "StatialBioc2023")
set.seed(51773)

Preparing features for lisaClust

kerenSPE <- lisaClust::lisaClust(kerenSPE, k = 5)

Filter colData to image 6

coords <- kerenSPE |>
filter(imageID == 6) |>
colData()

Set some parameters

window <- "convex"
nbp <- 300

Make the window for the ppp

ow <- makeWindow(coords, window)

Create a ppp

pp <-
spatstat.geom::ppp(coords$x,
coords$y,
window = ow,
marks = coords$region)

pp$region <- pp$marks
pp$cellType <- coords$cellType

Convert to mask

rG <- regionGrid(pp, nbp)

Convert grids for each region into polygons and create columns that

identify each polygon and the region it belongs to.

tree <- sapply(unique(pp$region), function(y){
rPoly <- regionPoly(rG, y)
rPoly <- lapply(rPoly$bdry, function(x){
data.frame(x)
})
rPoly <- bind_rows(rPoly, .id = "id")
}, simplify = FALSE) |>
bind_rows(.id = "region") |>
mutate(group = paste(region, id, sep = "_"))

Make the plot.

The key parameter is fill = NA which allows to see the cells.

We would need to make some nice hatching patterns and better spacings etc.

p1 <- ggplot() +
geom_point(data = data.frame(coords), aes(x,y, colour = cellType))+
geom_polygon_pattern(data = tree,
aes(x, y, group = group, pattern = region, pattern_angle = region),
colour = 'black',
pattern_density = 0.05,
pattern_spacing = 0.02,
pattern_size = 0.05,
fill = NA,
pattern_colour = 'black'
) + theme_bw(18)

p1

generateCurves Creates an Empty Result

An list of NULLs is returned by a call to generateCurves function, so lisa function can't set row names on it.

Browse[1]> head(cellSummary)
SplitDataFrameList of length 6
$`1`
DataFrame with 1625 rows and 6 columns
      imageID      cellID imageCellID         x         y               cellType
     <factor> <character> <character> <numeric> <numeric>            <character>
1           1 cell_358130         1_1    49.000   1.05263                  HER2+
2           1 cell_358131         1_2   345.000   3.92308         HR- CKlow CK5+
3           1 cell_358132         1_3    29.500   5.00000      Fibroblasts CD68+
4           1 cell_358133         1_4   291.192   4.88462 Macrophages Vim+ CD4..
5           1 cell_358134         1_5   261.254   4.29851 Macrophages Vim+ Slug+
...       ...         ...         ...       ...       ...                    ...
1621        1 cell_359750      1_1621   383.965   502.807 Macrophages Vim+ Slug-
1622        1 cell_359751      1_1622   304.675   502.725 Macrophages Vim+ CD4..
1623        1 cell_359752      1_1623   404.263   501.800          Myoepithelial
1624        1 cell_359753      1_1624   464.870   502.522 Macrophages Vim+ Slug-
1625        1 cell_359754      1_1625   394.486   502.486          Myoepithelial

...
<5 more elements>
Browse[1]> fast
  FALSE
Browse[1]> str(generateCurves)
function (data, Rs, window, window.length, BPcellType = BPcellType, sigma = sigma, ...)  
Browse[1]> Rs
  20  50 100
Browse[1]> window
  "convex"
Browse[1]> window.length
NULL
Browse[1]> sigma
  50
Browse[1]> curveList <- BiocParallel::bplapply(cellSummary, generateCurves, 
        Rs = Rs, window = window, window.length = window.length, 
        BPcellType = BPcellType, BPPARAM = BPimage, sigma = sigma)
Browse[1]> head(curveList, 3)
$`1`
NULL
$`3`
NULL
$`4`
NULL

Browse[1]>   rownames(curves) <- as.character(unlist(lapply(cellSummary, function(x) x$cellID)))
  Error in dimnames(x) <- dn: length of 'dimnames' [1] not equal to array extent

Traceback.

1: lisaClust(IMC, k = 10, sigma = 50, BPPARAM = MulticoreParam(16), fast = FALSE, 
2: lisa(cd, Rs = Rs, BPPARAM = BPPARAM, window = window, window.length = window.le
3: `rownames<-`(`*tmp*`, value = as.character(unlist(lapply(cellSummary, function(

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.