... | ... |
@@ -58,7 +58,7 @@ lisa <- |
58 | 58 |
lisaFunc = "K", |
59 | 59 |
minLambda = 0.05, |
60 | 60 |
fast = TRUE, |
61 |
- spatialCoords = c("x","y"), |
|
61 |
+ spatialCoords = c("x", "y"), |
|
62 | 62 |
cellType = "cellType", |
63 | 63 |
imageID = "imageID") { |
64 | 64 |
|
... | ... |
@@ -329,18 +329,54 @@ weightCounts <- function(dt, X, maxD, lam) { |
329 | 329 |
mat |
330 | 330 |
} |
331 | 331 |
|
332 |
+#' Calculate the inhomogenous local K function. |
|
333 |
+#' |
|
334 |
+#' @param data The data. |
|
335 |
+#' @param Rs |
|
336 |
+#' A vector of the radii that the measures of association should be |
|
337 |
+#' calculated. |
|
338 |
+#' @param sigma |
|
339 |
+#' A numeric variable used for scaling when filting inhomogeneous L-curves. |
|
340 |
+#' @param window |
|
341 |
+#' Should the window around the regions be 'square', 'convex' or 'concave'. |
|
342 |
+#' @param window.length |
|
343 |
+#' A tuning parameter for controlling the level of concavity. |
|
344 |
+#' @param minLambda |
|
345 |
+#' Minimum value for density for scaling when fitting inhomogeneous L-curves. |
|
346 |
+#' @param lisaFunc Either "K" or "L" curve. |
|
347 |
+#' |
|
348 |
+#' @return A matrix of LISA curves |
|
349 |
+#' |
|
350 |
+#' @examples |
|
351 |
+#' library(spicyR) |
|
352 |
+#' # Read in data as a SegmentedCells objects |
|
353 |
+#' isletFile <- system.file("extdata", "isletCells.txt.gz", package = "spicyR") |
|
354 |
+#' cells <- read.table(isletFile, header = TRUE) |
|
355 |
+#' cells$x <- cells$AreaShape_Center_X |
|
356 |
+#' cells$y <- cells$AreaShape_Center_Y |
|
357 |
+#' cells$cellType <- as.factor(sample( |
|
358 |
+#' c("big", "medium", "small"), |
|
359 |
+#' length(cells$AreaShape_Center_Y), |
|
360 |
+#' replace = TRUE |
|
361 |
+#' )) |
|
362 |
+#' cells$cellID <- as.factor(cells$ObjectNumber) |
|
363 |
+#' |
|
364 |
+#' inhom <- inhomLocalK(cells[1:100,]) |
|
365 |
+#' |
|
366 |
+#' @export |
|
367 |
+#' @rdname inhomLocalK |
|
332 | 368 |
#' @importFrom spatstat.geom ppp closepairs marks area |
333 | 369 |
#' @importFrom spatstat.explore density.ppp |
334 | 370 |
#' @importFrom tidyr pivot_longer |
335 | 371 |
#' @importFrom dplyr left_join |
336 | 372 |
inhomLocalK <- |
337 |
- function (data, |
|
338 |
- Rs = c(20, 50, 100, 200), |
|
339 |
- sigma = 10000, |
|
340 |
- window = "convex", |
|
341 |
- window.length = NULL, |
|
342 |
- minLambda = 0.05, |
|
343 |
- lisaFunc = "K") { |
|
373 |
+ function(data, |
|
374 |
+ Rs = c(20, 50, 100, 200), |
|
375 |
+ sigma = 10000, |
|
376 |
+ window = "convex", |
|
377 |
+ window.length = NULL, |
|
378 |
+ minLambda = 0.05, |
|
379 |
+ lisaFunc = "K") { |
|
344 | 380 |
|
345 | 381 |
ow <- makeWindow(data, window, window.length) |
346 | 382 |
X <- |
... | ... |
@@ -350,15 +386,15 @@ inhomLocalK <- |
350 | 386 |
window = ow, |
351 | 387 |
marks = data$cellType |
352 | 388 |
) |
353 |
- |
|
389 |
+ |
|
354 | 390 |
if (is.null(Rs)) |
355 |
- Rs = c(20, 50, 100, 200) |
|
391 |
+ Rs <- c(20, 50, 100, 200) |
|
356 | 392 |
if (is.null(sigma)) |
357 |
- sigma = 100000 |
|
358 |
- |
|
393 |
+ sigma <- 100000 |
|
394 |
+ |
|
359 | 395 |
maxR <- min(ow$xrange[2]- ow$xrange[1], ow$yrange[2]- ow$yrange[1])/2.01 |
360 | 396 |
Rs <- unique(pmin(c(0, sort(Rs)),maxR)) |
361 |
- |
|
397 |
+ |
|
362 | 398 |
den <- spatstat.explore::density.ppp(X, sigma = sigma) |
363 | 399 |
den <- den / mean(den) |
364 | 400 |
den$v <- pmax(den$v, minLambda) |
... | ... |
@@ -367,37 +403,36 @@ inhomLocalK <- |
367 | 403 |
n <- X$n |
368 | 404 |
p$j <- data$cellID[p$j] |
369 | 405 |
p$i <- data$cellID[p$i] |
370 |
- |
|
406 |
+ |
|
371 | 407 |
cT <- data$cellType |
372 | 408 |
names(cT) <- data$cellID |
373 |
- |
|
409 |
+ |
|
374 | 410 |
p$d <- cut(p$d, Rs, labels = Rs[-1], include.lowest = TRUE) |
375 |
- |
|
411 |
+ |
|
376 | 412 |
# inhom density |
377 | 413 |
np <- spatstat.geom::nearest.valid.pixel(X$x, X$y, den) |
378 | 414 |
w <- den$v[cbind(np$row, np$col)] |
379 | 415 |
names(w) <- data$cellID |
380 |
- p$wt <- 1/w[p$j]*mean(w) |
|
416 |
+ p$wt <- 1 / w[p$j] * mean(w) |
|
381 | 417 |
rm(np) |
382 |
- |
|
383 |
- lam <- table(data$cellType)/spatstat.geom::area(X) |
|
384 |
- |
|
385 |
- |
|
418 |
+ |
|
419 |
+ lam <- table(data$cellType) / spatstat.geom::area(X) |
|
420 |
+ |
|
421 |
+ |
|
386 | 422 |
|
387 | 423 |
p$cellTypeJ <- cT[p$j] |
388 | 424 |
p$cellTypeI <- cT[p$i] |
389 | 425 |
p$i <- factor(p$i, levels = data$cellID) |
390 |
- |
|
391 |
- edge <- sapply(Rs[-1],function(x)borderEdge(X,x)) |
|
426 |
+ edge <- sapply(Rs[-1], function(x) borderEdge( X, x)) |
|
392 | 427 |
edge <- as.data.frame(edge) |
393 | 428 |
colnames(edge) <- Rs[-1] |
394 | 429 |
edge$i <- data$cellID |
395 |
- edge <- tidyr::pivot_longer(edge,-i, names_to = "d") |
|
396 |
- |
|
430 |
+ edge <- tidyr::pivot_longer(edge, -i, names_to = "d") |
|
431 |
+ |
|
397 | 432 |
p <- dplyr::left_join(as.data.frame(p), edge, c("i", "d")) |
398 | 433 |
p$d <- factor(p$d, levels = Rs[-1]) |
399 |
- |
|
400 |
- |
|
434 |
+ |
|
435 |
+ |
|
401 | 436 |
|
402 | 437 |
p <- as.data.frame(p) |
403 | 438 |
|
... | ... |
@@ -471,7 +506,6 @@ getL <- |
471 | 506 |
|
472 | 507 |
#' @importFrom spicyR cellSummary |
473 | 508 |
#' @importFrom SummarizedExperiment colData |
474 |
-#' @importFrom spicyR as.data.frame |
|
475 | 509 |
#' @import SpatialExperiment SingleCellExperiment |
476 | 510 |
prepCellSummary <- function(cells, spatialCoords, cellType, imageID, region = NULL, bind = FALSE){ |
477 | 511 |
if (is.data.frame(cells)) { |
478 | 512 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,55 @@ |
1 |
+% Generated by roxygen2: do not edit by hand |
|
2 |
+% Please edit documentation in R/LISA.R |
|
3 |
+\name{inhomLocalK} |
|
4 |
+\alias{inhomLocalK} |
|
5 |
+\title{Calculate the inhomogenous local K function.} |
|
6 |
+\usage{ |
|
7 |
+inhomLocalK( |
|
8 |
+ data, |
|
9 |
+ Rs = c(20, 50, 100, 200), |
|
10 |
+ sigma = 10000, |
|
11 |
+ window = "convex", |
|
12 |
+ window.length = NULL, |
|
13 |
+ minLambda = 0.05, |
|
14 |
+ lisaFunc = "K" |
|
15 |
+) |
|
16 |
+} |
|
17 |
+\arguments{ |
|
18 |
+\item{data}{The data.} |
|
19 |
+ |
|
20 |
+\item{Rs}{A vector of the radii that the measures of association should be |
|
21 |
+calculated.} |
|
22 |
+ |
|
23 |
+\item{sigma}{A numeric variable used for scaling when filting inhomogeneous L-curves.} |
|
24 |
+ |
|
25 |
+\item{window}{Should the window around the regions be 'square', 'convex' or 'concave'.} |
|
26 |
+ |
|
27 |
+\item{window.length}{A tuning parameter for controlling the level of concavity.} |
|
28 |
+ |
|
29 |
+\item{minLambda}{Minimum value for density for scaling when fitting inhomogeneous L-curves.} |
|
30 |
+ |
|
31 |
+\item{lisaFunc}{Either "K" or "L" curve.} |
|
32 |
+} |
|
33 |
+\value{ |
|
34 |
+A matrix of LISA curves |
|
35 |
+} |
|
36 |
+\description{ |
|
37 |
+Calculate the inhomogenous local K function. |
|
38 |
+} |
|
39 |
+\examples{ |
|
40 |
+library(spicyR) |
|
41 |
+# Read in data as a SegmentedCells objects |
|
42 |
+isletFile <- system.file("extdata", "isletCells.txt.gz", package = "spicyR") |
|
43 |
+cells <- read.table(isletFile, header = TRUE) |
|
44 |
+cells$x <- cells$AreaShape_Center_X |
|
45 |
+cells$y <- cells$AreaShape_Center_Y |
|
46 |
+cells$cellType <- as.factor(sample( |
|
47 |
+ c("big", "medium", "small"), |
|
48 |
+ length(cells$AreaShape_Center_Y), |
|
49 |
+ replace = TRUE |
|
50 |
+)) |
|
51 |
+cells$cellID <- as.factor(cells$ObjectNumber) |
|
52 |
+ |
|
53 |
+inhom <- inhomLocalK(cells[1:100,]) |
|
54 |
+ |
|
55 |
+} |