Skip to contents

The grid is centered at the origin, so that the region of interest becomes [-L1, L1] x [-L2, L2]; the grid centroids are the HSGP input coordinates and the HSGP domain is boundfactor * c(L1, L2). With a tissue boundary, the grid cells outside the tissue are removed first and the region of interest is that of the retained grid.

Usage

denmark_prepare(
  dataset,
  gene = NULL,
  grid_size = 10,
  basis = 25,
  boundfactor = 1.5,
  bbox = NULL,
  L1 = NULL,
  L2 = NULL,
  boundary = NULL,
  min_area_frac = 0
)

Arguments

dataset

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

gene

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

grid_size

Grid cells per axis, or c(nx, ny).

basis

Number of HSGP basis functions per axis, or c(m1, m2).

boundfactor

HSGP boundary factor c (>= 1; 1.5 is a good default).

bbox

Optional region of interest c(xmin, xmax, ymin, ymax); the default is the data range, or the extent of boundary when one is given.

L1, L2

Optional half-ranges. Only used to warn if they disagree with the data (the old interface required them; they are now computed).

boundary

Optional tissue polygon from denmark_alpha_shape(). Grid cells outside the tissue are removed, and each remaining grid cell enters with its own area inside the tissue: log_grid_area becomes a vector and the variable-area model DenMark-vararea.stan is used.

min_area_frac

Drop boundary grid cells whose area inside the tissue is below this fraction of a full grid cell (default 0: keep all).

Value

A list with the elements

stan_data

Data list for the Stan models: n, y1 (cell counts), y2 (summed expression), d, mstar, coords (centered grid centroids), log_grid_area (one value, or one per grid cell with a boundary), mrange, indices, L and is_centerted_PHI.

grid

The grid from buildgridpp() (without the exterior grid cells when a boundary is given) with the centered coordinates cx and cy.

info

Gene, grid geometry, HSGP settings, number of cells, the count matrices Ymat and Mmat, and for the tissue boundary vararea, boundary, n_grid_total, n_interior and n_boundary.

Examples

toy <- denmark_toy_data()
tumor <- subset(toy, celltype == "Tumor", select = -celltype)
prep <- denmark_prepare(tumor, gene = "geneA", grid_size = 10, basis = 10)
str(prep$stan_data[c("n", "mstar", "L", "log_grid_area", "mrange")])
#> List of 5
#>  $ n            : int 100
#>  $ mstar        : int 100
#>  $ L            : num [1:2] 3.83 1.88
#>  $ log_grid_area: num -2.06
#>  $ mrange       : num 0.93
tissue <- denmark_alpha_shape(toy$x, toy$y)
prep_b <- denmark_prepare(tumor, gene = "geneA", grid_size = 10, basis = 10,
                          boundary = tissue)
prep_b$info[c("n_grid_total", "n_interior", "n_boundary")]
#> $n_grid_total
#> [1] 100
#> 
#> $n_interior
#> [1] 46
#> 
#> $n_boundary
#> [1] 36
#> 
summary(exp(prep_b$stan_data$log_grid_area))
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
#> 0.001567 0.093493 0.129962 0.104128 0.129962 0.129962