Skip to content

Commit

Permalink
updated plotCells object scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
lassedochreden committed Feb 12, 2024
1 parent 8de28d5 commit 21e6c4c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
37 changes: 22 additions & 15 deletions 10-image_visualization.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ spe <- readRDS("data/spe.rds")
images <- readRDS("data/images.rds")
masks <- readRDS("data/masks.rds")
# Sample images
# Sample images and masks
set.seed(220517)
cur_id <- sample(unique(spe$sample_id), 3)
cur_images <- images[names(images) %in% cur_id]
Expand Down Expand Up @@ -180,7 +180,7 @@ distribution of cell phenotypes, the visual assessment of morphological
features and quality control in terms of cell segmentation and
phenotyping.

### Visualzing metadata
### Visualizing metadata

The `cytomapper` package provides the `plotCells` function that accepts
a `CytoImageList` object containing segmentation masks. These are
Expand Down Expand Up @@ -245,33 +245,40 @@ plotCells(cur_masks,

In terms of visualizing metadata, any entry in the `colData(spe)` slot
can be visualized. The `plotCells` function automatically detects if the
entry is continuous or discrete. In this fashion, we can now visualize
the area of each cell:
entry is continuous or discrete. For continuous features, `cytomapper` scales
across the whole `SpatialExperiment` object. Therefore, we will subset the
object to only contain the 3 visualized images. In this fashion, we can now
visualize the area of each cell:

```{r area}
cur_spe <- spe[,spe$sample_id %in% cur_id]
plotCells(cur_masks,
object = spe,
object = cur_spe,
cell_id = "ObjectNumber",
img_id = "sample_id",
colour_by = "area")
```

### Visualizating expression
### Visualizing expression

Similar to visualizing single-cell metadata on segmentation masks, we
can use the `plotCells` function to visualize the aggregated pixel
intensities per cell. In the current dataset pixel intensities were
aggregated by computing the mean pixel intensity per cell and per
channel. The `plotCells` function accepts the `exprs_values` argument
(default `counts`) that allows selecting the assay which stores the
expression values that should be visualized.
expression values that should be visualized. Similar to continuous
`colData(spe)` features, `cytomapper` scales the expression values across
the whole `SpatialExperiment` object. We will again subset the object
to only contain the 3 visualized images.

In the following example, we visualize the asinh-transformed mean pixel
intensities of the epithelial marker E-cadherin on segmentation masks.

```{r Ecad-expression}
plotCells(cur_masks,
object = spe,
object = cur_spe,
cell_id = "ObjectNumber",
img_id = "sample_id",
colour_by = "Ecad",
Expand All @@ -286,7 +293,7 @@ spatial distribution of tumor cells (E-cadherin), T cells (CD3), B cells

```{r 6-channel-expression}
plotCells(cur_masks,
object = spe,
object = cur_spe,
cell_id = "ObjectNumber",
img_id = "sample_id",
colour_by = c("Ecad", "CD3", "CD20", "CD8a", "CD38", "Ki67"),
Expand All @@ -304,7 +311,7 @@ argument:

```{r setting-expression-colors}
plotCells(cur_masks,
object = spe,
object = cur_spe,
cell_id = "ObjectNumber",
img_id = "sample_id",
colour_by = c("Ecad", "CD3", "CD20"),
Expand All @@ -330,30 +337,30 @@ the cells' outlines by their cell phenotype.
```{r outlining-all-cells}
plotPixels(image = cur_images,
mask = cur_masks,
object = spe,
object = cur_spe,
cell_id = "ObjectNumber",
img_id = "sample_id",
colour_by = c("Ecad", "CD3", "CD20"),
outline_by = "celltype",
bcg = list(Ecad = c(0, 5, 1),
CD3 = c(0, 5, 1),
CD20 = c(0, 5, 1)),
colour = list(celltype = metadata(spe)$color_vectors$celltype),
colour = list(celltype = metadata(cur_spe)$color_vectors$celltype),
thick = TRUE)
```

Distinguishing individual cell phenotypes is nearly impossible in the
images above.

However, the `SpatialExperiment` object can be subsetted to only contain
However, the `SpatialExperiment` object can be further subsetted to only contain
cells of a single or few phenotypes. This allows the selective
visualization of cell outlines on composite images.

Here, we select all CD8+ T cells from the dataset and outline them on a
2-channel composite image displaying the expression of CD3 and CD8a.

```{r outlining-CD8}
CD8 <- spe[,spe$celltype == "CD8"]
CD8 <- cur_spe[,cur_spe$celltype == "CD8"]
plotPixels(image = cur_images,
mask = cur_masks,
Expand Down Expand Up @@ -467,7 +474,7 @@ out1 <- plotCells(cur_masks,
return_plot = TRUE)
out2 <- plotCells(cur_masks,
object = spe,
object = cur_spe,
cell_id = "ObjectNumber",
img_id = "sample_id",
colour_by = c("Ecad", "CD3", "CD20"),
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@

**Version 1.0.3** [2024-01-05]

- Updated cytoviewer citation and corresponding text
- Updated cytoviewer citation and corresponding text

**Version 1.0.4** [2024-02-12]

- Updated cell-level visualization with subsetted object

0 comments on commit 21e6c4c

Please sign in to comment.