Skip to contents

Drop cells whose summed membership across nonnegative programs is too high, following the ImmGenT rule, and return rescaled activities and effects for the kept cells. Reproduce the ImmGenT post-fit filter on nonnegative sample activities. Each pass scales each factor to a maximum of one, then removes samples whose summed membership exceeds max_val. The retained activities and feature effects are rescaled together, preserving their reconstructed expression.

Usage

filter_cells_by_total_membership(
  fit,
  sample_side = NULL,
  max_val = 10,
  numiter = 12L
)

Arguments

fit

Native flashier fit or a view with nonnegative activities.

sample_side

"rows" or "columns"; needed only for a bare native fit.

max_val

Maximum retained total membership, inclusive (default 10).

numiter

Number of passes (default 12, matching the ImmGenT pipeline).

Value

A membership_filter_result list with $activity, $effects, $offset, $kept_samples, $removed_samples, $history (one row per pass) and $converged. Matrices use sample by factor and feature by factor axes, regardless of native sample_side.

Details

Zero-max activity columns remain zero; their effects stay unchanged. Negative activities are rejected. The input fit is never modified or refitted. No native PVE or posterior sampler is recalculated for the selected population. A warning reports a pass limit that leaves memberships above max_val. This implements a declared selection rule, not a general cell-quality test.

Author

Mikhael Manurung. Conceptual workflow: Ziang Zhang / AgueroZZ, ImmGenT-GP analysis; per-script authorship is unresolved.

Examples

L <- rbind(
  broad = c(10, 10, 0), next_cell = c(4, 4, .2),
  a = c(5, 0, 0), b = c(0, 5, 0), c = c(0, 0, 1)
)
Fmat <- diag(3)
dimnames(Fmat) <- list(paste0("g", 1:3), paste0("F", 1:3))
colnames(L) <- colnames(Fmat)
native <- flashier::flash_factors_init(
  flashier::flash_init(tcrossprod(L, Fmat)), list(L, Fmat),
  ebnm_fn = list(ebnm::ebnm_point_exponential, ebnm::ebnm_normal)
)
filtered <- filter_cells_by_total_membership(native, "rows", max_val = 1.5)
filtered$kept_samples
#> [1] "a" "b" "c"
filtered$history
#>    iteration before removed remaining
#> 1          1      5       1         4
#> 2          2      4       1         3
#> 3          3      3       0         3
#> 4          4      3       0         3
#> 5          5      3       0         3
#> 6          6      3       0         3
#> 7          7      3       0         3
#> 8          8      3       0         3
#> 9          9      3       0         3
#> 10        10      3       0         3
#> 11        11      3       0         3
#> 12        12      3       0         3