| Title: | Read, Validate and Store Multiplexed Imaging Cell Tables |
|---|---|
| Description: | Defines 'cellspec', a versioned table format for segmented cells from multiplexed tissue images: cell identifiers, centroids in micrometres, per-marker measurements with an explicit compartment and statistic, image and channel metadata, and provenance. Reads measurement exports of image-analysis tools such as 'QuPath' and 'MCMICRO' into this format, validates its structure and content, selects one signal per marker through an explicit policy, and writes directories with a 'SHA-256' checksum manifest so that later analysis steps can verify what they read. 'QuPath' is described by Bankhead et al. (2017) <doi:10.1038/s41598-017-17204-5> and 'MCMICRO' by Schapiro et al. (2022) <doi:10.1038/s41592-021-01308-y>. |
| Authors: | R. Heller [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-8006-9742>) |
| Maintainer: | R. Heller <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.1.0 |
| Built: | 2026-10-29 17:11:17 UTC |
| Source: | https://github.com/CTTIR/cellspecR |
x[i, j] keeps the cells selected by i and the features selected by j.
All components stay aligned: cells and measurements rows are subset
together, dictionary rows follow the kept features, and adjacency contacts
whose cells were removed are dropped. images and channels are kept
unchanged.
## S3 method for class 'cellspec' x[i, j, ..., drop = FALSE]## S3 method for class 'cellspec' x[i, j, ..., drop = FALSE]
x |
A |
i |
Cells to keep: a logical vector with one value per cell, or positive or negative integer positions. Missing means all cells. |
j |
Features to keep: a character vector of feature identifiers, a logical vector with one value per feature, or integer positions. Missing means all features. |
... |
Not used; must be empty. |
drop |
Not used; subsetting always returns a |
A cellspec object.
Other methods:
as.data.frame.cellspec(),
cellspec-methods,
plot.cellspec()
x <- cs_example() dim(x) big <- x[cs_cells(x)$area > 60, ] dim(big) means <- x[, cs_features(x, statistic = "mean")] dim(means)x <- cs_example() dim(x) big <- x[cs_cells(x)$area > 60, ] dim(big) means <- x[, cs_features(x, statistic = "mean")] dim(means)
Combines the cells columns with selected measurement columns into one
wide data frame, for example to hand a table to modelling code.
Measurement columns are named by their feature identifiers.
## S3 method for class 'cellspec' as.data.frame(x, row.names = NULL, optional = FALSE, ..., features = NULL) ## S3 method for class 'cellspec' as_tibble(x, ..., features = NULL)## S3 method for class 'cellspec' as.data.frame(x, row.names = NULL, optional = FALSE, ..., features = NULL) ## S3 method for class 'cellspec' as_tibble(x, ..., features = NULL)
x |
A |
row.names, optional
|
Ignored; present for compatibility with the generic. |
... |
Unused. |
features |
Character vector of feature identifiers to include (default: all features). |
A data.frame with one row per cell: the cells columns followed
by one double column per selected feature. as_tibble() returns the
same content as a tibble (requires the tibble package).
Other methods:
[.cellspec(),
cellspec-methods,
plot.cellspec()
x <- cs_example() df <- as.data.frame(x, features = cs_features(x, statistic = "mean")) head(df)x <- cs_example() df <- as.data.frame(x, features = cs_features(x, statistic = "mean")) head(df)
print() shows a compact overview that fits in 80 columns: cells, images,
samples, features by kind, markers, pixel size, specification version and
the adapter that produced the object. summary() returns one row per
image. dim() returns the number of cells and features.
## S3 method for class 'cellspec' print(x, ...) ## S3 method for class 'cellspec' summary(object, ...) ## S3 method for class 'cellspec_summary' print(x, ...) ## S3 method for class 'cellspec' dim(x)## S3 method for class 'cellspec' print(x, ...) ## S3 method for class 'cellspec' summary(object, ...) ## S3 method for class 'cellspec_summary' print(x, ...) ## S3 method for class 'cellspec' dim(x)
x, object
|
A |
... |
Unused; for compatibility with the generics. |
print() returns x invisibly.
summary() returns a data frame of class cellspec_summary with one row
per image and the columns image_id, sample_id (character), n_cells,
n_markers, n_features (integer; markers and features with at least
one non-missing value in that image), pixel_size, x_min, x_max,
y_min, y_max (double, micrometres) and na_fraction (double,
fraction of missing measurement values in that image).
dim() returns an integer vector c(cells, features).
Other methods:
[.cellspec(),
as.data.frame.cellspec(),
plot.cellspec()
x <- cs_example() x summary(x) dim(x)x <- cs_example() x summary(x) dim(x)
Accessors return the components of a cellspec object. Use them instead
of x$cells so code keeps working if the internal layout changes.
cs_cells(x) cs_measurements(x) cs_dictionary(x) cs_images(x) cs_channels(x) cs_provenance(x) cs_adjacency(x)cs_cells(x) cs_measurements(x) cs_dictionary(x) cs_images(x) cs_channels(x) cs_provenance(x) cs_adjacency(x)
x |
A |
cs_cells(): data frame, one row per cell.
cs_measurements(): double matrix, cells x features, column names are
feature identifiers.
cs_dictionary(): data frame, one row per measurement column.
cs_images(): data frame, one row per image.
cs_channels(): data frame, one row per image and channel.
cs_provenance(): list with producer, reader, created_utc,
inputs, counts, parameters and history.
cs_adjacency(): data frame of cell-cell contacts, or NULL.
vignette("specification", package = "cellspecR")
Other object:
cs_bind(),
cs_example(),
cs_features(),
cs_merge_tiles(),
cs_new(),
is_cellspec()
x <- cs_example() head(cs_cells(x)) dim(cs_measurements(x)) cs_dictionary(x)[1:3, ] cs_images(x) cs_channels(x) names(cs_provenance(x)) cs_adjacency(x)x <- cs_example() head(cs_cells(x)) dim(cs_measurements(x)) cs_dictionary(x)[1:3, ] cs_images(x) cs_channels(x) names(cs_provenance(x)) cs_adjacency(x)
Builds a local Shiny application for reading an export, reviewing format
detection and validation, and downloading the resulting tables. The
function returns an application object; launch it with shiny::runApp().
cs_app( path = NULL, max_upload_mb = 2048, allow_local_paths = FALSE, max_plot_points = 50000L, ... )cs_app( path = NULL, max_upload_mb = 2048, allow_local_paths = FALSE, max_plot_points = 50000L, ... )
path |
Optional initial file or directory path. The user can also select a file in the application. |
max_upload_mb |
Maximum upload size in MB. It is applied as the
|
allow_local_paths |
Whether a local path field is available. |
max_plot_points |
Maximum cells passed to plots. |
... |
Reserved for launcher settings. |
A shiny.appobj.
if (requireNamespace("shiny", quietly = TRUE)) { app <- cs_app() inherits(app, "shiny.appobj") }if (requireNamespace("shiny", quietly = TRUE)) { app <- cs_app() inherits(app, "shiny.appobj") }
Creates an AnnData object with cells in obs, selected marker signals in
X, and x/y in obsm$spatial. The optional anndataR package and its
Python runtime are required.
cs_as_anndata(x, policy = NULL)cs_as_anndata(x, policy = NULL)
x |
A |
policy |
Signal policy used to form |
An AnnData object supplied by anndataR.
Other interoperability:
cs_as_spe(),
cs_from_spe()
if (requireNamespace("anndataR", quietly = TRUE)) { x <- cs_example() cs_as_anndata(x, cs_signal_policy(cs_markers(x))) }if (requireNamespace("anndataR", quietly = TRUE)) { x <- cs_example() cs_as_anndata(x, cs_signal_policy(cs_markers(x))) }
Converts selected signals to the main assay, stores every raw measurement
in an altExp, and records the cellspec metadata needed for a lossless
round trip.
cs_as_spe(x, policy = NULL, assay_name = "intensity")cs_as_spe(x, policy = NULL, assay_name = "intensity")
x |
A |
policy |
A signal policy. It is required when a marker has more than one compartment or statistic; otherwise it can be omitted. |
assay_name |
Name of the main signal assay. |
A SpatialExperiment with cells as columns, markers as rows,
spatialCoords containing x and y, and a measurements altExp.
Other interoperability:
cs_as_anndata(),
cs_from_spe()
if (requireNamespace("SpatialExperiment", quietly = TRUE)) { x <- cs_example() policy <- cs_signal_policy(cs_markers(x)) spe <- cs_as_spe(x, policy) spe }if (requireNamespace("SpatialExperiment", quietly = TRUE)) { x <- cs_example() policy <- cs_signal_policy(cs_markers(x)) spe <- cs_as_spe(x, policy) spe }
cs_bind() combines cell tables, measurements and metadata from one or
more cellspec objects. Objects must describe different images and must
use the same measurement dictionary.
cs_bind(...)cs_bind(...)
... |
|
A combined cellspec object.
Other object:
cs_accessors,
cs_example(),
cs_features(),
cs_merge_tiles(),
cs_new(),
is_cellspec()
cs_bind(cs_example())cs_bind(cs_example())
Creates a validated description of how a generic delimited table maps to
the cellspec columns. Source names are checked for shape here and checked
against the input table by cs_read().
cs_column_map( cell_id, x, y, image_id = NULL, sample_id = NULL, area = NULL, coordinate_unit = c("um", "px"), measurements = NULL )cs_column_map( cell_id, x, y, image_id = NULL, sample_id = NULL, area = NULL, coordinate_unit = c("um", "px"), measurements = NULL )
cell_id |
Source column containing the cell identifier. |
x |
Source column containing the x coordinate. |
y |
Source column containing the y coordinate. |
image_id |
Optional source column containing image identifiers. |
sample_id |
Optional source column containing sample identifiers. |
area |
Optional source column containing positive cell areas. |
coordinate_unit |
Coordinate unit for |
measurements |
Optional data frame with |
An object of class cs_column_map, a named list with the mapped
source names, coordinate unit and validated measurement metadata.
Other adapters:
cs_detect_format(),
cs_formats(),
cs_read()
cs_column_map( "Cell ID", "Centroid X", "Centroid Y", measurements = data.frame( source_name = "CD3e Mean", kind = "intensity", marker = "CD3e", compartment = "cell", statistic = "mean", unit = "a.u." ) )cs_column_map( "Cell ID", "Centroid X", "Centroid Y", measurements = data.frame( source_name = "CD3e Mean", kind = "intensity", marker = "CD3e", compartment = "cell", statistic = "mean", unit = "a.u." ) )
Reads only the header and at most n_max data rows. A generic table is
deliberately returned as a low-confidence candidate because its biological
meaning cannot be inferred safely without an explicit column map.
cs_detect_format(path, n_max = 50L)cs_detect_format(path, n_max = 50L)
path |
A delimited table file. |
n_max |
Maximum number of data rows to inspect after the header. |
A data frame with format, score and evidence columns.
Other adapters:
cs_column_map(),
cs_formats(),
cs_read()
path <- tempfile(fileext = ".csv") utils::write.csv(data.frame(id = 1, x = 2, y = 3), path, row.names = FALSE) cs_detect_format(path)path <- tempfile(fileext = ".csv") utils::write.csv(data.frame(id = 1, x = 2, y = 3), path, row.names = FALSE) cs_detect_format(path)
Builds a small, deterministic cellspec object for examples and
experiments: two images from two samples, 40 cells each, four markers
(DAPI, CD3e, Pan-Cytokeratin, FOXP3) measured as mean and median
in the cell and nucleus compartments, cell and nucleus areas, and one
tool-specific column. The values are simulated, not measured. The random
number generator state of the session is left untouched.
cs_example()cs_example()
A valid cellspec object with 80 cells and 19 features.
Other object:
cs_accessors,
cs_bind(),
cs_features(),
cs_merge_tiles(),
cs_new(),
is_cellspec()
x <- cs_example() x cs_validate(x)x <- cs_example() x cs_validate(x)
Describes, for every image and feature, how much usable variation a
feature carries. A marker whose channel was blank, saturated or not
acquired shows up as "constant" or "near_constant" here before any
normalisation or gating hides the problem. Other packages (for example a
gating package) use the support column to declare markers non-callable.
The support classes are assigned in this order:
"mostly_na": more than half of the values are missing.
"constant": all non-missing values are identical (for example an
all-zero channel).
"near_constant": the 1st and 99th percentiles are identical, so at
most about 1% of cells differ from the rest.
"ok": otherwise.
cs_feature_support( x, features = cs_features(x, kind = "intensity", statistic = "mean"), zero_tol = 0 )cs_feature_support( x, features = cs_features(x, kind = "intensity", statistic = "mean"), zero_tol = 0 )
x |
A |
features |
Character vector of feature identifiers. Defaults to all mean intensity features. |
zero_tol |
Values with absolute value at most |
A data frame with one row per image and feature and the columns
image_id, feature_id, marker, compartment, statistic
(character), n, n_na (integer), fraction_zero, sd, mad, q01,
q50, q99 (double, computed on non-missing values; NA when none)
and support (character, see Details).
Other validation:
cs_validate()
x <- cs_example() cs_feature_support(x) # A blank channel is reported as constant x$measurements[, "cell:FOXP3:mean"] <- 0 s <- cs_feature_support(x) s[s$marker == "FOXP3", c("image_id", "feature_id", "support")]x <- cs_example() cs_feature_support(x) # A blank channel is reported as constant x$measurements[, "cell:FOXP3:mean"] <- 0 s <- cs_feature_support(x) s[s$marker == "FOXP3", c("image_id", "feature_id", "support")]
cs_features() returns the identifiers of measurement columns whose
dictionary entries match every supplied filter. Select features through
this function rather than by splitting feature_id strings; the identifier
layout is not part of the contract downstream code may parse.
cs_markers() returns the markers that have at least one intensity
feature.
cs_features( x, kind = NULL, marker = NULL, compartment = NULL, statistic = NULL ) cs_markers(x)cs_features( x, kind = NULL, marker = NULL, compartment = NULL, statistic = NULL ) cs_markers(x)
x |
A |
kind, marker, compartment, statistic
|
Character vectors of allowed
values, or |
cs_features(): character vector of feature_id values in
dictionary order (length 0 when nothing matches). cs_markers():
character vector of marker names in order of first appearance.
Other object:
cs_accessors,
cs_bind(),
cs_example(),
cs_merge_tiles(),
cs_new(),
is_cellspec()
x <- cs_example() cs_features(x, kind = "intensity", statistic = "mean") cs_features(x, marker = "FOXP3") cs_markers(x)x <- cs_example() cs_features(x, kind = "intensity", statistic = "mean") cs_features(x, marker = "FOXP3") cs_markers(x)
Lists the adapters implemented by this build of cellspecR. The generic
table adapter accepts delimited files when the source columns are mapped
explicitly with cs_column_map().
cs_formats()cs_formats()
A data frame with format, adapter_version, description,
tested_with and status columns.
cs_detect_format(), cs_column_map(), cs_read()
Other adapters:
cs_column_map(),
cs_detect_format(),
cs_read()
cs_formats()cs_formats()
Reconstructs the cellspec components stored by cs_as_spe().
cs_from_spe(spe)cs_from_spe(spe)
spe |
A |
A validated cellspec object.
Other interoperability:
cs_as_anndata(),
cs_as_spe()
if (requireNamespace("SpatialExperiment", quietly = TRUE)) { x <- cs_example() policy <- cs_signal_policy(cs_markers(x)) identical(cs_cells(x), cs_cells(cs_from_spe(cs_as_spe(x, policy)))) }if (requireNamespace("SpatialExperiment", quietly = TRUE)) { x <- cs_example() policy <- cs_signal_policy(cs_markers(x)) identical(cs_cells(x), cs_cells(cs_from_spe(cs_as_spe(x, policy)))) }
Builds a two-column mapping from source marker names to canonical marker names. Source names must be unique; several source names may map to the same canonical name when they represent the same marker across files.
cs_marker_map(from, to)cs_marker_map(from, to)
from |
Character vector of source marker names. |
to |
Character vector of canonical marker names, the same length as
|
A two-column data frame with character columns from and to.
Other signals:
cs_read_signal_policy(),
cs_signal_matrix(),
cs_signal_policy(),
cs_write_signal_policy()
cs_marker_map(c("PanCK", "CD68"), c("Pan-Cytokeratin", "CD68"))cs_marker_map(c("PanCK", "CD68"), c("Pan-Cytokeratin", "CD68"))
Every centroid is assigned to the half open outer box containing it. A centroid found in more than one box is an error. Cells in a tile whose centroid is owned by another tile, or by no tile, are treated as halo cells and dropped.
cs_merge_tiles(tiles, tile_bounds, ownership = "centroid", tolerance = 1e-09)cs_merge_tiles(tiles, tile_bounds, ownership = "centroid", tolerance = 1e-09)
tiles |
A named or positional list of |
tile_bounds |
A data frame with |
ownership |
Ownership method. The only supported value is
|
tolerance |
Non-negative tolerance used to snap coordinates close to a box boundary before applying its half open comparisons. |
A merged cellspec object.
Other object:
cs_accessors,
cs_bind(),
cs_example(),
cs_features(),
cs_new(),
is_cellspec()
tile_bounds <- data.frame( tile_id = "tile-1", xmin = 0, xmax = 10000, ymin = 0, ymax = 10000 ) cs_merge_tiles(list(cs_example()), tile_bounds)tile_bounds <- data.frame( tile_id = "tile-1", xmin = 0, xmax = 10000, ymin = 0, ymax = 10000 ) cs_merge_tiles(list(cs_example()), tile_bounds)
Builds a cellspec object from its components and checks it against the
structure rules of the specification. Tool exports are normally read with
cs_read(); use cs_new() when you already hold the pieces in memory, for
example in another package that produces cell tables.
Inputs are normalised before validation: any data frame subclass becomes a
plain data.frame without row names, factors become character vectors,
identifier columns (cell_id, image_id, sample_id, subject_id) are
converted to character, whole-number doubles in integer columns become
integers, and NaN/Inf in measurements become NA (counted in
provenance$counts$nonfinite_converted). A missing sample_id column in
cells is filled from images.
cs_new( cells, measurements = NULL, dictionary = NULL, images, channels = NULL, provenance = NULL, adjacency = NULL )cs_new( cells, measurements = NULL, dictionary = NULL, images, channels = NULL, provenance = NULL, adjacency = NULL )
cells |
A data frame with one row per cell and at least the columns
|
measurements |
A numeric matrix (or data frame of numeric columns) with
one row per cell, in the row order of |
dictionary |
A data frame describing each measurement column (see
|
images |
A data frame with one row per image and at least |
channels |
A data frame with one row per image and channel
( |
provenance |
A list describing the origin of the data. Missing fields
are filled with defaults; |
adjacency |
Optional data frame of cell-cell contacts
( |
A cellspec object: a list of class "cellspec" with elements
spec_version (string), cells (data frame), measurements (double
matrix), dictionary, images, channels (data frames), provenance
(list) and adjacency (data frame or NULL).
vignette("specification", package = "cellspecR") for every
column and rule; cs_validate() for the checks applied.
Other object:
cs_accessors,
cs_bind(),
cs_example(),
cs_features(),
cs_merge_tiles(),
is_cellspec()
cells <- data.frame( cell_id = c("1", "2", "3"), image_id = "img1", sample_id = "s1", x = c(10, 20, 30), y = c(5, 15, 25), area = c(50, 62.5, 48) ) measurements <- cbind( "cell:CD3e:mean" = c(120, 8, 95), "nucleus:FOXP3:mean" = c(3, 1, 40) ) dictionary <- data.frame( feature_id = colnames(measurements), kind = "intensity", marker = c("CD3e", "FOXP3"), compartment = c("cell", "nucleus"), statistic = "mean", unit = "a.u.", source_name = c("Cell: CD3e: Mean", "Nucleus: FOXP3: Mean") ) images <- data.frame(image_id = "img1", sample_id = "s1", pixel_size = 0.5) channels <- data.frame( image_id = "img1", channel_index = 1:3, channel_name = c("DAPI", "CD3e", "FOXP3"), marker = c("DAPI", "CD3e", "FOXP3") ) x <- cs_new(cells, measurements, dictionary, images, channels) xcells <- data.frame( cell_id = c("1", "2", "3"), image_id = "img1", sample_id = "s1", x = c(10, 20, 30), y = c(5, 15, 25), area = c(50, 62.5, 48) ) measurements <- cbind( "cell:CD3e:mean" = c(120, 8, 95), "nucleus:FOXP3:mean" = c(3, 1, 40) ) dictionary <- data.frame( feature_id = colnames(measurements), kind = "intensity", marker = c("CD3e", "FOXP3"), compartment = c("cell", "nucleus"), statistic = "mean", unit = "a.u.", source_name = c("Cell: CD3e: Mean", "Nucleus: FOXP3: Mean") ) images <- data.frame(image_id = "img1", sample_id = "s1", pixel_size = 0.5) channels <- data.frame( image_id = "img1", channel_index = 1:3, channel_name = c("DAPI", "CD3e", "FOXP3"), marker = c("DAPI", "CD3e", "FOXP3") ) x <- cs_new(cells, measurements, dictionary, images, channels) x
Reads a delimited cell table using a format-specific adapter or an explicit
cs_column_map(). See cs_formats() for supported formats.
The segmantr adapter expects the one-based row and column centroid
means returned by segmantR::sg_extract_features().
It subtracts 0.5 before converting to micrometres and scales pixel area
by pixel_size^2. Coordinates refer to the supplied image: cropped or
downsampled inputs require an explicit external frame transform before
combining with a full-resolution image. This adapter assumes square pixels.
Shape measurements retain source units in the feature dictionary.
cs_read( path, format = "auto", pixel_size = NULL, image_id = NULL, sample_id = NULL, marker_map = NULL, column_map = NULL, tile_bounds = NULL, verify_done = TRUE, keep_other = TRUE, keep_paths = FALSE, quiet = FALSE )cs_read( path, format = "auto", pixel_size = NULL, image_id = NULL, sample_id = NULL, marker_map = NULL, column_map = NULL, tile_bounds = NULL, verify_done = TRUE, keep_other = TRUE, keep_paths = FALSE, quiet = FALSE )
path |
One delimited table file. |
format |
Reader format. Use |
pixel_size |
Micrometres per pixel. Required for pixel coordinates;
micrometre-coordinate tables use |
image_id |
Optional scalar image identifier overriding the mapped column; otherwise the mapped column or file basename is used. |
sample_id |
Optional scalar sample identifier overriding the mapped column; otherwise the mapped column or image identifier is used. |
marker_map |
Reserved for future adapters; not used by the table adapter. |
column_map |
A validated object returned by |
tile_bounds |
Optional tile-bound data frame for |
verify_done |
Whether tiled readers should verify optional |
keep_other |
Whether unmapped numeric columns become |
keep_paths |
Whether the full input path may be recorded in provenance. |
quiet |
Reserved for consistency with future readers; suppresses the
informational warning about dropped columns when |
A validated cellspec object.
Input files whose names end in .gz are decompressed in R and checked
against their gzip trailer before they are parsed, for every format. A
truncated or corrupt file, also one zero-filled back to its original
size, raises an error of class cellspec_error_format. A BGZF file
(written by bgzip) is checked block by block and must end with its
end-of-file block. Other files made of several gzip members, such as
concatenated .gz files, can be checked only in their last member; they
are read with a warning of class cellspec_warning_integrity.
cs_formats(), cs_detect_format(), cs_column_map()
Other adapters:
cs_column_map(),
cs_detect_format(),
cs_formats()
path <- tempfile(fileext = ".csv") utils::write.csv(data.frame(id = c("c1", "c2"), x = 1:2, y = 3:4), path, row.names = FALSE) map <- cs_column_map("id", "x", "y") x <- cs_read(path, format = "table", column_map = map) dim(x)path <- tempfile(fileext = ".csv") utils::write.csv(data.frame(id = c("c1", "c2"), x = 1:2, y = 3:4), path, row.names = FALSE) map <- cs_column_map("id", "x", "y") x <- cs_read(path, format = "table", column_map = map) dim(x)
Reads the sidecar and cell table written by cs_write(), verifies the manifest and DONE marker by default, and returns a validated object. DONE is required even when verification is explicitly disabled.
cs_read_cellspec(dir, verify = TRUE)cs_read_cellspec(dir, verify = TRUE)
dir |
A canonical cellspec directory. |
verify |
Whether to verify all manifest digests before reading data. |
A validated cellspec object.
Tables written with cs_write(format = "tsv.gz") are decompressed in R
and checked against their gzip trailer, also when verify = FALSE. A
truncated or corrupt file, also one zero-filled back to its original
size, raises an error of class cellspec_error_format. A BGZF file
(written by bgzip) is checked block by block and must end with its
end-of-file block. Other files made of several gzip members can be checked
only in their last member; they are read with a warning of class
cellspec_warning_integrity.
cs_write(), cs_verify()
Other canonical-format:
cs_verify(),
cs_write()
path <- tempfile("cellspec-") cs_write(cs_example(), path) y <- cs_read_cellspec(path) identical(cs_cells(y), cs_cells(cs_example()))path <- tempfile("cellspec-") cs_write(cs_example(), path) y <- cs_read_cellspec(path) identical(cs_cells(y), cs_cells(cs_example()))
cs_read_npy() reads one .npy file and cs_read_npz() reads the arrays
of an .npz archive, both without Python. Cell tables exported from Python
pipelines (centroids, labels, per-cell flags) can so be brought into
cs_new() directly. cs_write_npy() and cs_write_npz() write the same
formats, for example to hand results back or to build test fixtures.
Supported data types are booleans, signed and unsigned integers, 32- and
64-bit floats, 128-bit complex numbers, fixed-width Unicode (U) and byte
(S) strings, in either byte order and in C or Fortran order. Object
arrays (O, stored with Python's pickle) and structured data types are
refused. 64-bit and unsigned 32-bit integers are returned as doubles; a
value that a double cannot hold exactly is an error.
cs_read_npy(path) cs_read_npz(path, arrays = NULL) cs_write_npy(x, path) cs_write_npz(x, path)cs_read_npy(path) cs_read_npz(path, arrays = NULL) cs_write_npy(x, path) cs_write_npz(x, path)
path |
Path to a |
arrays |
Names of the arrays to read from the archive, without the
|
x |
For |
Arrays keep their logical shape: element [i, j] in R is element
[i - 1, j - 1] in NumPy, whatever the storage order. Trailing NUL
characters of fixed-width strings are dropped, as NumPy does.
cs_write_npz() stores the members uncompressed (as numpy.savez()
does) and needs no external zip program. Character data are written as
little-endian U strings, doubles as <f8, integers as <i4 and logical
values as |b1; missing values are not allowed.
cs_read_npy() returns a vector for one-dimensional arrays, a
matrix or array with the same shape otherwise, and a length-one vector
for zero-dimensional arrays. cs_read_npz() returns a named list of
these, in archive order. The writers return path invisibly.
path <- tempfile(fileext = ".npz") cs_write_npz(list(x_um = c(1.5, 2.5), label = c("Tumour", "Treg")), path) cs_read_npz(path) cs_read_npz(path, arrays = "label")$labelpath <- tempfile(fileext = ".npz") cs_write_npz(list(x_um = c(1.5, 2.5), label = c("Tumour", "Treg")), path) cs_read_npz(path) cs_read_npz(path, arrays = "label")$label
Reads and validates a policy written by cs_write_signal_policy(). The
format is inferred from the .json or .csv extension unless format is
supplied explicitly.
cs_read_signal_policy(path, format = NULL)cs_read_signal_policy(path, format = NULL)
path |
Source file path. |
format |
Either |
A validated data frame with class cs_signal_policy.
Other signals:
cs_marker_map(),
cs_signal_matrix(),
cs_signal_policy(),
cs_write_signal_policy()
path <- tempfile(fileext = ".json") cs_write_signal_policy(cs_signal_policy("CD3e"), path) cs_read_signal_policy(path)path <- tempfile(fileext = ".json") cs_write_signal_policy(cs_signal_policy("CD3e"), path) cs_read_signal_policy(path)
Applies a cs_signal_policy to the intensity measurements in a
cellspec object. A preferred finite value is used when its declared domain
accepts it; otherwise the configured fallback is tested using its own domain.
Default domains require values at least min_value. The "finite" domain
accepts negative finite values. Non-finite values remain unavailable in both.
cs_signal_matrix(x, policy, image_id = NULL)cs_signal_matrix(x, policy, image_id = NULL)
x |
A |
policy |
A |
image_id |
Optional image identifier. When supplied, only cells from that image are returned. |
A list with signal, a cells by markers numeric matrix, source, a
character matrix describing the selected source, and policy, the
validated policy. Source values are "<compartment>:<statistic>",
"fallback:<compartment>:<statistic>" or "unavailable".
Other signals:
cs_marker_map(),
cs_read_signal_policy(),
cs_signal_policy(),
cs_write_signal_policy()
x <- cs_example() policy <- cs_signal_policy(cs_markers(x)) selected <- cs_signal_matrix(x, policy) selected$signal[, 1:2]x <- cs_example() policy <- cs_signal_policy(cs_markers(x)) selected <- cs_signal_matrix(x, policy) selected$signal[, 1:2]
Creates one validated row per marker. Signal policies make the selected compartment, statistic, fallback and minimum usable value explicit.
cs_signal_policy( marker, compartment = "cell", statistic = "mean", fallback_compartment = NA_character_, min_value = 0, preferred_domain = "minimum", fallback_domain = "minimum" )cs_signal_policy( marker, compartment = "cell", statistic = "mean", fallback_compartment = NA_character_, min_value = 0, preferred_domain = "minimum", fallback_domain = "minimum" )
marker |
Character vector of unique marker names. |
compartment |
Preferred compartment for each marker. A single value is
recycled across |
statistic |
Intensity statistic for each marker. A single value is
recycled across |
fallback_compartment |
Optional fallback compartment for each marker;
|
min_value |
Minimum usable value for each marker. Values below this
threshold are unavailable. A single value is recycled across |
preferred_domain, fallback_domain
|
Usability domain for each branch:
|
A data frame with class cs_signal_policy and columns marker,
compartment, statistic, fallback_compartment and min_value.
Non-default domains append preferred_domain and fallback_domain.
Default policies retain the original five-column representation.
cs_signal_matrix(), cs_marker_map()
Other signals:
cs_marker_map(),
cs_read_signal_policy(),
cs_signal_matrix(),
cs_write_signal_policy()
cs_signal_policy( marker = c("PanCK", "FOXP3"), compartment = c("cytoplasm", "nucleus"), fallback_compartment = "cell" )cs_signal_policy( marker = c("PanCK", "FOXP3"), compartment = c("cytoplasm", "nucleus"), fallback_compartment = "cell" )
Returns the version of the cellspec format that this build of cellspecR
writes and validates. Readers of major version 1 accept every 1.y file
whose minor version is not newer than their own.
cs_spec_version()cs_spec_version()
A single string in semantic versioning form, for example "1.0.0".
vignette("specification", package = "cellspecR")
Other specification:
cs_vocabulary()
cs_spec_version()cs_spec_version()
cs_validate() checks an object against the cellspec specification and
returns a report with one row per check. cs_assert_valid() runs the same
checks and aborts when any check fails.
level = "structure" runs the MUST rules of the specification: required
columns and types, unique keys, finite centroids, dictionary vocabulary,
alignment of measurements, channels and optional adjacency. A failing
structure check means other packages cannot rely on the object.
level = "semantic" adds plausibility checks that produce warnings, not
failures: centroids outside the image, implausible cell areas, negative
intensities, features with many missing values, markers without usable
signal (cs_feature_support()) and duplicated centroids, which usually
indicate cells counted twice when tiles were merged.
cs_validate( x, level = c("structure", "semantic"), area_range = c(1, 5000), na_max = 0.01 ) cs_assert_valid(x, level = "structure", call = rlang::caller_env())cs_validate( x, level = c("structure", "semantic"), area_range = c(1, 5000), na_max = 0.01 ) cs_assert_valid(x, level = "structure", call = rlang::caller_env())
x |
A |
level |
|
area_range |
Numeric vector of length 2: plausible whole-cell area in
square micrometres for the |
na_max |
Largest acceptable fraction of missing values per feature
for the |
call |
The execution environment used in error messages; internal callers pass their own. |
cs_validate() returns a data frame of class
cellspec_validation with one row per check and the columns check
(character), level ("structure" or "semantic"), status ("pass",
"warn", "fail", or "skip" when a prerequisite check failed), n
(integer number of offending rows, features or values) and message
(character, naming columns or features and up to five examples).
Rows are in catalogue order; print() shows failures first.
cs_assert_valid() returns x invisibly, or aborts with an error of
class cellspec_error_invalid whose report field holds the report.
vignette("specification", package = "cellspecR")
Other validation:
cs_feature_support()
x <- cs_example() cs_validate(x) report <- cs_validate(x, level = "semantic") report[report$status != "pass", ] # A broken object fails with a classed error bad <- x bad$cells$x[1] <- NA try(cs_assert_valid(bad))x <- cs_example() cs_validate(x) report <- cs_validate(x, level = "semantic") report[report$status != "pass", ] # A broken object fails with a classed error bad <- x bad$cells$x[1] <- NA try(cs_assert_valid(bad))
Recomputes every digest in MANIFEST.sha256 and checks the DONE marker against the manifest digest. The returned report has one row per declared file plus DONE; it does not hide a failed digest behind an error.
cs_verify(dir)cs_verify(dir)
dir |
A cellspec directory written by cs_write(). |
A data frame with character columns file, expected and observed, and logical column ok. ok is FALSE for a missing, modified or unexpected file, or for an invalid DONE marker.
cs_read_cellspec()
Other canonical-format:
cs_read_cellspec(),
cs_write()
path <- tempfile("cellspec-") cs_write(cs_example(), path) cs_verify(path)path <- tempfile("cellspec-") cs_write(cs_example(), path) cs_verify(path)
Returns the specification tables as data: the columns each component may
carry, and the allowed values of every controlled vocabulary. The
validator (cs_validate()) enforces exactly these tables, so other
packages can use them to build compatible objects.
cs_vocabulary(what = c("columns", "vocabularies", "checks"))cs_vocabulary(what = c("columns", "vocabularies", "checks"))
what |
Which table to return: |
For what = "columns", a data frame with one row per specified
column and the character columns component ("cells", "dictionary",
"images", "channels", "adjacency"), column, type (R storage
type), requirement ("MUST", "SHOULD", "MAY") and description.
For what = "vocabularies", a data frame with the character columns
vocabulary, value and description. For what = "checks", the
validation catalogue used by cs_validate(): character columns check,
level ("structure" or "semantic") and description.
vignette("specification", package = "cellspecR")
Other specification:
cs_spec_version()
cols <- cs_vocabulary("columns") cols[cols$component == "cells", c("column", "type", "requirement")] voc <- cs_vocabulary("vocabularies") voc$value[voc$vocabulary == "compartment"] head(cs_vocabulary("checks"))cols <- cs_vocabulary("columns") cols[cols$component == "cells", c("column", "type", "requirement")] voc <- cs_vocabulary("vocabularies") voc$value[voc$vocabulary == "compartment"] head(cs_vocabulary("checks"))
Writes a canonical cellspec directory in a neighbouring staging directory, then renames it into place after the data, sidecar, manifest and DONE marker are complete. Parquet is the default because it preserves doubles exactly; the tsv.gz option is available for interoperable text output.
cs_write(x, dir, format = c("parquet", "tsv.gz"), overwrite = FALSE)cs_write(x, dir, format = c("parquet", "tsv.gz"), overwrite = FALSE)
x |
A valid cellspec object. |
dir |
Destination directory. Its parent is created when needed. |
format |
Storage format: parquet or tsv.gz. |
overwrite |
Whether an existing destination may be replaced. Only an
empty directory or earlier |
x, invisibly.
cs_read_cellspec(), cs_verify()
Other canonical-format:
cs_read_cellspec(),
cs_verify()
path <- tempfile("cellspec-") cs_write(cs_example(), path)path <- tempfile("cellspec-") cs_write(cs_example(), path)
Writes a validated signal policy. The format is inferred from the .json
or .csv extension unless format is supplied explicitly.
cs_write_signal_policy(policy, path, format = NULL)cs_write_signal_policy(policy, path, format = NULL)
policy |
A |
path |
Destination file path. |
format |
Either |
policy, invisibly.
Other signals:
cs_marker_map(),
cs_read_signal_policy(),
cs_signal_matrix(),
cs_signal_policy()
path <- tempfile(fileext = ".json") cs_write_signal_policy(cs_signal_policy("CD3e"), path)path <- tempfile(fileext = ".json") cs_write_signal_policy(cs_signal_policy("CD3e"), path)
Test for a cellspec object
is_cellspec(x)is_cellspec(x)
x |
Any R object. |
TRUE if x inherits from "cellspec", otherwise FALSE. The
object is not validated; use cs_validate() for that.
Other object:
cs_accessors,
cs_bind(),
cs_example(),
cs_features(),
cs_merge_tiles(),
cs_new()
is_cellspec(cs_example()) is_cellspec(data.frame())is_cellspec(cs_example()) is_cellspec(data.frame())
Draws a centroid map, marker distribution, feature-support heatmap, cell
area distribution, or an overview of up to four panels. The overview leaves
out the area panel when the cells table has no area column and the marker
panel when there are no intensity features. Large objects are subsampled
deterministically for plotting.
## S3 method for class 'cellspec' plot( x, type = c("overview", "map", "marker", "support", "area"), colour_by = NULL, marker = NULL, policy = NULL, max_points = 50000L, seed = 1L, ... )## S3 method for class 'cellspec' plot( x, type = c("overview", "map", "marker", "support", "area"), colour_by = NULL, marker = NULL, policy = NULL, max_points = 50000L, seed = 1L, ... )
x |
A |
type |
Plot type: |
colour_by |
A cells or measurement column used by the map. |
marker |
Marker used by the marker plot. |
policy |
Optional signal policy for the marker plot. |
max_points |
Maximum number of cells used by a plot. |
seed |
Seed used for deterministic subsampling. |
... |
Reserved for future plot-specific parameters. |
A ggplot object, or a patchwork object for type = "overview".
Other methods:
[.cellspec(),
as.data.frame.cellspec(),
cellspec-methods
if (requireNamespace("ggplot2", quietly = TRUE)) { plot(cs_example(), type = "map") }if (requireNamespace("ggplot2", quietly = TRUE)) { plot(cs_example(), type = "map") }