Groups ultra-rare variants (MAC below threshold) and collapses each group into a single "super-variant" by summing genotype dosages. Common variants (MAC >= threshold) are left unchanged.

collapse_rare_variants(
  G,
  mac_threshold = 10L,
  max_group_size = Inf,
  spatial_grouping = FALSE,
  agg_method = "mean"
)

Arguments

G

Numeric matrix (n x p) of genotype dosage.

mac_threshold

Integer MAC threshold. Variants with MAC < this are candidates for collapsing. Default 10.

max_group_size

Integer maximum variants per collapsed group. Inf means all rare variants collapse into one. Default Inf.

spatial_grouping

Logical. If TRUE, groups rare variants only between pairs of adjacent common variants (respects genomic order). If FALSE, groups all rare variants sequentially. Default FALSE.

agg_method

Character method for aggregating B/PI values of collapsed variants. One of "mean", "max", "sum". Default "mean".

Value

A list with components:

G_collapsed

Numeric matrix (n x p') of collapsed genotypes

col_mapping

List of length p'. Each element is an integer vector of original column indices that were merged into this column.

is_collapsed

Logical vector of length p'. TRUE for output columns formed by merging 2 or more variants. A singleton rare variant that did not get merged with anything is treated as a passthrough column and reported as FALSE.

agg_method

Character aggregation method (for downstream B/PI computation).

Examples

# 50 samples, 5 variants with varying MAC
set.seed(1)
G <- cbind(
  rbinom(50, 2, 0.3),   # common (MAC ~ 30)
  rbinom(50, 2, 0.02),  # rare   (MAC ~ 2)
  rbinom(50, 2, 0.01),  # rare   (MAC ~ 1)
  rbinom(50, 2, 0.25),  # common (MAC ~ 25)
  rbinom(50, 2, 0.03)   # rare   (MAC ~ 3)
)
result <- collapse_rare_variants(G, mac_threshold = 10)
ncol(result$G_collapsed)  # fewer columns: rare variants grouped
#> [1] 3
result$is_collapsed       # TRUE for collapsed groups
#> [1] FALSE  TRUE FALSE