Skip to contents

Each cell is assigned to exactly one grid cell (cells on an interior edge go to the upper or right cell; cells on the outer boundary are kept).

Usage

buildgridpp(grid_size, dataset, gene = NULL, bbox = NULL, boundary = NULL)

Arguments

grid_size

Number of grid cells per axis, or c(nx, ny).

dataset

Data frame with columns x, y and the expression column(s), one row per cell.

gene

Expression column to use (default: the first non-coordinate column).

bbox

Optional bounding box c(xmin, xmax, ymin, ymax); the default is the data range, or the extent of boundary when one is given. Cells outside the box are dropped.

boundary

Optional tissue polygon from denmark_alpha_shape(). The grid then spans the tissue, and every grid cell gets its area inside the tissue (area) and a type: interior, boundary or exterior.

Value

A list. The vectors are ordered with x varying fastest, that is grid_index = i + (j - 1) * nx, identical to as.vector(Ymat). The first three elements keep the order of the original DenMark function, so res[[1]], res[[2]] and res[[3]] still work.

loc_mark_mat

N x 2 matrix with the columns point (= Y) and mark (= M).

Ymat

nx x ny matrix of cell counts (rows = x, columns = y).

Mmat

nx x ny matrix of summed expression.

grid

Data frame with the columns grid_index, i, j, x, y (centroids), Y, M, area (area inside the tissue; the full grid cell area without a boundary) and type.

bbox, step, grid_area, grid_size, gene, boundary

Bounding box, grid steps c(dx, dy), area of one full grid cell, c(nx, ny), the gene name and the tissue polygon (NULL without one).

Examples

toy <- denmark_toy_data()
tumor <- subset(toy, celltype == "Tumor", select = -celltype)
g <- buildgridpp(grid_size = 10, dataset = tumor, gene = "geneA")
dim(g$Ymat)
#> [1] 10 10
head(g$grid)
#>   grid_index i j         x         y  Y  M      area     type
#> 1          1 1 1 0.8713599 0.7820836  0  0 0.1277374 interior
#> 2          2 2 1 1.3814337 0.7820836  0  0 0.1277374 interior
#> 3          3 3 1 1.8915075 0.7820836 14 21 0.1277374 interior
#> 4          4 4 1 2.4015813 0.7820836 32 69 0.1277374 interior
#> 5          5 5 1 2.9116551 0.7820836 35 86 0.1277374 interior
#> 6          6 6 1 3.4217289 0.7820836 14 13 0.1277374 interior
tissue <- denmark_alpha_shape(toy$x, toy$y)
gb <- buildgridpp(grid_size = 10, dataset = tumor, gene = "geneA",
                  boundary = tissue)
table(gb$grid$type)
#> 
#> boundary exterior interior 
#>       36       18       46