Browse code

Added bubble plot for region enrichment

Ellis Patrick authored on 22/07/2022 13:24:35
Showing 7 changed files

... ...
@@ -1,7 +1,7 @@
1 1
 Package: lisaClust
2 2
 Type: Package
3 3
 Title: lisaClust: Clustering of Local Indicators of Spatial Association
4
-Version: 1.5.4
4
+Version: 1.5.5
5 5
 Authors@R: c(
6 6
     person("Ellis", "Patrick", email = "[email protected]",
7 7
     role = c("aut", "cre")),
... ...
@@ -22,6 +22,6 @@ BugReports: https://github.com/ellispatrick/lisaClust/issues
22 22
 Imports: ggplot2, class, concaveman, grid, BiocParallel, spatstat.core,
23 23
         spatstat.geom, BiocGenerics, S4Vectors, methods, spicyR,
24 24
         purrr, stats, data.table, dplyr, tidyr, SingleCellExperiment, SpatialExperiment,
25
-        SummarizedExperiment
25
+        SummarizedExperiment, pheatmap
26 26
 Suggests: BiocStyle, knitr, rmarkdown
27 27
 RoxygenNote: 7.2.0
... ...
@@ -4,6 +4,7 @@ export(geom_hatching)
4 4
 export(hatchingPlot)
5 5
 export(lisa)
6 6
 export(lisaClust)
7
+export(regionMap)
7 8
 export(scale_region)
8 9
 export(scale_region_manual)
9 10
 import(SingleCellExperiment)
... ...
@@ -26,6 +27,7 @@ importFrom(data.table,setkey)
26 27
 importFrom(dplyr,.data)
27 28
 importFrom(dplyr,bind_rows)
28 29
 importFrom(dplyr,left_join)
30
+importFrom(dplyr,mutate)
29 31
 importFrom(ggplot2,aes)
30 32
 importFrom(ggplot2,discrete_scale)
31 33
 importFrom(ggplot2,facet_wrap)
... ...
@@ -33,6 +35,7 @@ importFrom(ggplot2,geom_point)
33 35
 importFrom(ggplot2,ggplot)
34 36
 importFrom(ggplot2,labs)
35 37
 importFrom(ggplot2,layer)
38
+importFrom(ggplot2,scale_colour_gradient2)
36 39
 importFrom(ggplot2,theme_minimal)
37 40
 importFrom(grid,gList)
38 41
 importFrom(grid,gpar)
... ...
@@ -41,6 +44,7 @@ importFrom(grid,grobTree)
41 44
 importFrom(grid,linesGrob)
42 45
 importFrom(grid,polylineGrob)
43 46
 importFrom(methods,is)
47
+importFrom(pheatmap,pheatmap)
44 48
 importFrom(purrr,map)
45 49
 importFrom(purrr,map_dfr)
46 50
 importFrom(spatstat.core,density.ppp)
... ...
@@ -537,3 +537,78 @@ prepCellSummary <- function(cells, spatialCoords, cellType, imageID){
537 537
 }
538 538
 
539 539
 
540
+
541
+
542
+#' Plot heatmap of cell type enrichment for lisaClust regions
543
+#'
544
+#' @param cells SegmentedCells, SingleCellExperiment, SpatialExperiment or data.frame
545
+#' @param type Make a "bubble" or "heatmap" plot.
546
+#' @param region The column storing the regions
547
+#' @param cellType The column storing the cell types
548
+#' @param limit limits to the lower and upper relative frequencies
549
+#' @param ... Any arguments to be passed to the pheatmap package
550
+#'
551
+#' @return A bubble plot or heatmap
552
+#'
553
+#'
554
+#' @examples
555
+#' set.seed(51773)
556
+#'x <- round(c(runif(200),runif(200)+1,runif(200)+2,runif(200)+3,
557
+#'             runif(200)+3,runif(200)+2,runif(200)+1,runif(200)),4)*100
558
+#'y <- round(c(runif(200),runif(200)+1,runif(200)+2,runif(200)+3,
559
+#'             runif(200),runif(200)+1,runif(200)+2,runif(200)+3),4)*100
560
+#'cellType <- factor(paste('c',rep(rep(c(1:2),rep(200,2)),4),sep = ''))
561
+#'imageID <- rep(c('s1', 's2'),c(800,800))
562
+#'
563
+#'cells <- data.frame(x, y, cellType, imageID)
564
+#'
565
+#'cellExp <- spicyR::SegmentedCells(cells, cellTypeString = 'cellType')
566
+#'
567
+#'cellExp <- lisaClust(cellExp, k = 2)
568
+#'
569
+#'regionMap(cellExp)
570
+#'
571
+#' @export
572
+#' @importFrom SummarizedExperiment colData
573
+#' @importFrom pheatmap pheatmap
574
+#' @importFrom ggplot2 ggplot aes geom_point scale_colour_gradient2 theme_minimal labs
575
+#' @importFrom dplyr mutate
576
+#' @import SpatialExperiment SingleCellExperiment
577
+regionMap <- function(cells, type = "bubble", cellType = "cellType", region = "region", limit = c(0.33,3), ...) {
578
+  
579
+  if (is.data.frame(cells)) {
580
+    df <- cells[,c(cellType, region)]
581
+  }
582
+  
583
+  if (is(cells, "SingleCellExperiment")|is(cells, "SpatialExperiment")) {
584
+    df <- as.data.frame(SummarizedExperiment::colData(cells))[,c(cellType, region)]
585
+  }
586
+  
587
+  if (is(cells, "SegmentedCells")) {
588
+    cellSummary <- cellSummary(cells, bind = TRUE)
589
+    df <- as.data.frame(cellSummary[,c(cellType, region)])
590
+  }
591
+  
592
+  tab <- table(df[,cellType], df[,region])
593
+  tab <- tab/rowSums(tab)%*%t(colSums(tab))*sum(tab)
594
+  
595
+  ph <- pheatmap::pheatmap(pmax(pmin(tab,limit[2]),limit[1]), cluster_cols = FALSE, silent = TRUE, ...)
596
+  
597
+  if(type == "bubble"){
598
+  
599
+  p1 <- tab |>
600
+    as.data.frame() |>
601
+    dplyr::mutate(cellType = factor(Var1, levels = levels(Var1)[ph$tree_row$order]), region = Var2, Freq2 = pmax(pmin(Freq,limit[2]),limit[1])) |>
602
+    ggplot2::ggplot(ggplot2::aes(x = region, y = cellType, colour = Freq2, size = Freq2)) + 
603
+    ggplot2::geom_point() + 
604
+    ggplot2::scale_colour_gradient2(low ="#4575B4", mid = "grey90", high = "#D73027", midpoint = 1, guide = "legend") + 
605
+    ggplot2::theme_minimal() + 
606
+    ggplot2::labs(x = "Region", y = "Cell-type", colour = "Relative\nFrequency", size = "Relative\nFrequency") 
607
+  
608
+  return(p1)
609
+  }
610
+  
611
+  pheatmap::pheatmap(pmax(pmin(tab,limit[2]),limit[1]), cluster_cols = FALSE, ...)
612
+  
613
+}
614
+
... ...
@@ -21,7 +21,7 @@ lisa(
21 21
 )
22 22
 }
23 23
 \arguments{
24
-\item{cells}{A SegmentedCells or data frame that contains at least the 
24
+\item{cells}{A SegmentedCells, SingleCellExperiment, SpatialExperiment or data frame that contains at least the 
25 25
 variables x and y, giving the  coordinates of each cell, imageID and cellType.}
26 26
 
27 27
 \item{Rs}{A vector of the radii that the measures of association should be calculated.}
... ...
@@ -23,7 +23,7 @@ lisaClust(
23 23
 )
24 24
 }
25 25
 \arguments{
26
-\item{cells}{A SegmentedCells or data frame that contains at least the 
26
+\item{cells}{A SegmentedCells, SingleCellExperiment, SpatialExperiment or data frame that contains at least the 
27 27
 variables x and y, giving the  coordinates of each cell, imageID and cellType.}
28 28
 
29 29
 \item{k}{The number of regions to cluster.}
30 30
new file mode 100644
... ...
@@ -0,0 +1,52 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/LISA.R
3
+\name{regionMap}
4
+\alias{regionMap}
5
+\title{Plot heatmap of cell type enrichment for lisaClust regions}
6
+\usage{
7
+regionMap(
8
+  cells,
9
+  type = "bubble",
10
+  cellType = "cellType",
11
+  region = "region",
12
+  limit = c(0.33, 3),
13
+  ...
14
+)
15
+}
16
+\arguments{
17
+\item{cells}{SegmentedCells, SingleCellExperiment, SpatialExperiment or data.frame}
18
+
19
+\item{type}{Make a "bubble" or "heatmap" plot.}
20
+
21
+\item{cellType}{The column storing the cell types}
22
+
23
+\item{region}{The column storing the regions}
24
+
25
+\item{limit}{limits to the lower and upper relative frequencies}
26
+
27
+\item{...}{Any arguments to be passed to the pheatmap package}
28
+}
29
+\value{
30
+A bubble plot or heatmap
31
+}
32
+\description{
33
+Plot heatmap of cell type enrichment for lisaClust regions
34
+}
35
+\examples{
36
+set.seed(51773)
37
+x <- round(c(runif(200),runif(200)+1,runif(200)+2,runif(200)+3,
38
+            runif(200)+3,runif(200)+2,runif(200)+1,runif(200)),4)*100
39
+y <- round(c(runif(200),runif(200)+1,runif(200)+2,runif(200)+3,
40
+            runif(200),runif(200)+1,runif(200)+2,runif(200)+3),4)*100
41
+cellType <- factor(paste('c',rep(rep(c(1:2),rep(200,2)),4),sep = ''))
42
+imageID <- rep(c('s1', 's2'),c(800,800))
43
+
44
+cells <- data.frame(x, y, cellType, imageID)
45
+
46
+cellExp <- spicyR::SegmentedCells(cells, cellTypeString = 'cellType')
47
+
48
+cellExp <- lisaClust(cellExp, k = 2)
49
+
50
+regionMap(cellExp)
51
+
52
+}
... ...
@@ -286,6 +286,16 @@ cellAnnotation(cellExp, "region") |>
286 286
 ```
287 287
 
288 288
 
289
+## Examine cell type enrichment
290
+
291
+We should check to see which cell types appear more frequently in each region than
292
+expected by chance. 
293
+
294
+```{r}
295
+regionMap(cellExp, type = "bubble")
296
+```
297
+
298
+
289 299
 
290 300
 ## Plot identified regions
291 301