vignettes/count_touches.Rmd
count_touches.Rmd
The phenoptr::count_touching_cells
function finds, counts and visualizes touching cells of multiple phenotypes in a single field.
This vignette gives an example of counting touches in multiple fields and aggregating across slides. This vignette does not create visualizations of the touching cells; the comments show how to change this.
This example counts touches for the nine fields included in this package.
library(phenoptr)
library(tidyverse)
# Find cell seg data files
base_path <- system.file("extdata", "samples", package = "phenoptrExamples")
files <- list_cell_seg_files(base_path)
# The phenotype pairs to locate. This will find CD8 cells touching
# tumor cells, and, separately, CD8 cells touching CD68 cells.
pairs <- list(c("CD8+", "CK+"),
c("CD8+", "CD68+"))
# Colors for all the phenotypes mentioned in pairs
colors <- list(
'CD8+' = 'yellow',
'CK+' = 'cyan',
'CD68+' = 'magenta'
)
# Set this true to write images in the same directory as the data files
write_images <- FALSE
# Count touching cells
touch_counts <- purrr::map_df(files, function(path) {
count_touching_cells(path, pairs, colors, write_images=write_images)
})
glimpse(touch_counts)
## Observations: 18
## Variables: 9
## $ slide_id <chr> "Set12_20-6plex", "Set12_20-6plex", "Set12_20-6ple...
## $ source <chr> "Set12_20-6plex_[14146,53503]", "Set12_20-6plex_[1...
## $ phenotype1 <chr> "CD8+", "CD8+", "CD8+", "CD8+", "CD8+", "CD8+", "C...
## $ phenotype2 <chr> "CK+", "CD68+", "CK+", "CD68+", "CK+", "CD68+", "C...
## $ total1 <int> 101, 101, 123, 123, 79, 79, 497, 497, 349, 349, 22...
## $ total2 <int> 2323, 557, 2367, 87, 3799, 344, 1731, 599, 1434, 1...
## $ p1_touch_p2 <int> 27, 22, 67, 8, 28, 19, 127, 122, 156, 28, 95, 48, ...
## $ p2_touch_p1 <int> 58, 29, 138, 6, 69, 21, 210, 133, 238, 24, 148, 48...
## $ touch_pairs <int> 63, 31, 151, 8, 73, 22, 258, 163, 289, 32, 177, 56...
The counts are easily aggregated over Slide ID
using dplyr::group_by
and dplyr::summarize_at
:
touch_counts %>% group_by(slide_id, phenotype1, phenotype2) %>%
summarize_at(vars(total1:touch_pairs), sum)
## # A tibble: 6 x 8
## # Groups: slide_id, phenotype1 [?]
## slide_id phenotype1 phenotype2 total1 total2 p1_touch_p2
## <chr> <chr> <chr> <int> <int> <int>
## 1 Set12_20-6plex CD8+ CD68+ 303 988 49
## 2 Set12_20-6plex CD8+ CK+ 303 8489 122
## 3 Set4_1-6plex CD8+ CD68+ 1074 1128 198
## 4 Set4_1-6plex CD8+ CK+ 1074 5422 378
## 5 Set8_11-6plex CD8+ CD68+ 514 1258 108
## 6 Set8_11-6plex CD8+ CK+ 514 5452 134
## p2_touch_p1 touch_pairs
## <int> <int>
## 1 56 61
## 2 265 287
## 3 205 251
## 4 596 724
## 5 124 136
## 6 228 250