Skip to contents

General-purpose reader for the common spatial layout of one cell-by-feature expression table plus one cell-metadata table holding coordinates. Each argument may be a file path (read with data.table::fread), a matrix, or a data frame.

Usage

ReadMatrixCoords(
  expression,
  metadata,
  x_col = "x",
  y_col = "y",
  cell_id_col = NULL,
  id_cols = character(0),
  sample_id = "sample1",
  transpose = FALSE,
  project = "SpatialProject"
)

Arguments

expression

Cells-by-markers expression (matrix, data frame, or CSV path). Non-numeric and id_cols columns are dropped.

metadata

Cell metadata containing the coordinate columns (data frame or CSV path).

x_col, y_col

Character. Coordinate column names in metadata; values must be real numeric, finite vectors. Factor codes are never used as coordinate values.

cell_id_col

Character or NULL. Shared cell-id column; if present in both tables, rows are matched on it, otherwise row order is assumed. Further columns (for example "fov") can follow: those present in both tables are matched as well, so ids need only be unique within them. The first column becomes cell_id. Matched ids must be unique. Declared ID columns and reserved cell/sample identifiers in CSV files are read as text before type inference, so leading-zero identities remain distinct.

id_cols

Character vector. Non-marker id columns to drop from expression (e.g. "fov", "cell_ID").

sample_id

Character. Sample identifier. Default "sample1".

transpose

Logical. Set TRUE if expression is markers-by-cells. Default FALSE.

project

Character. Project name.

Value

A SpatialCellData-class object.

Examples

expr <- matrix(rpois(40, 5), nrow = 10,
               dimnames = list(NULL, c("CD3", "CD8", "CD20", "PanCK")))
meta <- data.frame(x = runif(10, 0, 100), y = runif(10, 0, 100))
obj <- ReadMatrixCoords(expr, meta)
obj
#> A SpatialCellData object
#>   10 cells across 1 sample
#>   Markers: CD3, CD8, CD20, PanCK 
#>   Normalised: FALSE 
#>   Project: SpatialProject