Skip to contents

Records the ordered features and the constants needed to convert a specimen's measurements. For log-scale measurements, it estimates a single fill value from the retained training features.

Usage

fit_singlesample_preprocessor(
  X_raw,
  type = c("counts", "ct", "log"),
  ct_limit = 40,
  ct_base = 2,
  scale = c("cpm", "none"),
  features = colnames(X_raw),
  log_base = 2,
  fill_offset = 1
)

Arguments

X_raw

Raw specimens-by-features matrix or data frame, with unique, non-empty column names. Counts must be numeric, finite and non-negative. Ct input may include character markers, which are converted to NA. Log-scale input must be numeric. Use NA, NaN or -Inf for non-detects. Logical columns may contain only missing values in a data frame of any measurement type.

type

Measurement type: "counts", "ct" or "log".

ct_limit

Positive finite Ct detection limit (default 40).

ct_base

Finite exponential base greater than 1 (default 2).

scale

For counts, "cpm" divides each row by its own total over available frozen features and multiplies by one million; "none" leaves counts unchanged. Used only for counts.

features

Ordered character vector of unique feature names present in X_raw. Defaults to all training columns.

log_base

Finite log base greater than 1 (default 2). Used only for type = "log".

fill_offset

Finite non-negative number subtracted from the smallest detected training value (default 1). Used only for type = "log".

Value

An S3 singlesample_preprocessor holding type, features and the conversion constants. For log-scale input these are log_base and fill_value on the input log scale.

Details

Counts of zero are non-detects. An all-zero row remains zero. Ct values that are missing, non-finite, non-numeric, or at or above ct_limit are non-detects. Their Ct is replaced by ct_limit before computing ct_base^(ct_limit - Ct), so they map to abundance 1. Negative finite Ct values below the limit are converted by the same formula. For log-scale input, the preprocessor fills non-detects with the smallest finite training value across features minus fill_offset. It then converts each value \(x\) to abundance by log_base^x - 1. The detection mask records which values were finite before filling. A positive infinity in a retained feature is rejected as saturation. Converted abundances, including the training fill value, must exceed -1.

Columns outside the frozen universe are dropped. Missing frozen columns remain absent, without imputation. For CPM, only available frozen columns contribute to that specimen's total; missing columns can therefore change its conversion. Scorer input guards determine whether support is sufficient.

Invalid inputs raise singlesample_input_error. Codes include INVALID_INPUT, EMPTY_INPUT, INVALID_FEATURE_NAMES, INVALID_PREPROCESSOR, INVALID_COUNT_VALUE (negative counts), NONFINITE_REQUIRED_VALUE (non-finite frozen count values), NONFINITE_ABUNDANCE (conversion or count-total overflow), LOG_SATURATION (positive infinity in log-scale input), LOG_UNDERFLOW (converted log-scale values at or below -1), and ALL_NONDETECT (no finite log-scale value among the training features). Conversion errors report affected row positions. If the training fill is too low, the error reports rows that supplied the minimum or needed the fill.

Examples

counts <- rbind(c(a = 10, b = 30), c(a = 6, b = 2))
prep <- fit_singlesample_preprocessor(counts, type = "counts")
apply_singlesample_preprocessor(prep, counts[1, ])
#> $abundance
#>           a      b
#> [1,] 250000 750000
#> 
#> $detected
#>         a    b
#> [1,] TRUE TRUE
#> 

log2_signals <- rbind(c(a = 4, b = NA), c(a = 3, b = 5))
prep <- fit_singlesample_preprocessor(log2_signals, type = "log")
apply_singlesample_preprocessor(prep, log2_signals[1, ])
#> $abundance
#>       a b
#> [1,] 15 3
#> 
#> $detected
#>         a     b
#> [1,] TRUE FALSE
#>