register_immunarch_method: Register an Immunarch method (developer)

View source: R/aaa-registry.R

register_immunarch_methodR Documentation

Register an Immunarch method (developer)

Description

[Experimental]

Usage

register_immunarch_method(
  core,
  family,
  name,
  register_family = TRUE,
  required_cols = NULL,
  need_repertoires = TRUE
)

Arguments

core

A function with signature ⁠function(idata, ...)⁠. This is your core implementation; it must accept an ImmunData as the first argument and must not declare autojoin, format, or features.

family

String. Method family name used for dispatch (e.g., "airr_stats").

name

String. Method name within the family (e.g., "lengths").

register_family

Logical (default TRUE). If TRUE, attempts to create/ensure the family environment by calling register_airr_family() when available.

required_cols

Character vector of column names that must be present in idata$annotations. Use this to declare the minimal input schema your core needs.

need_repertoires

Logical. Use this to declare the necessity of having aggregated repertoires.

Details

Wrap a core implementation into a user-facing function and (optionally) register it in the in-memory method registry. The wrapper adds common arguments and runs safety checks so your core stays minimal.

What your core must look like

  • Signature: ⁠function(idata, ...)⁠

  • Must not declare autojoin, format, or features - these are added by the wrapper.

What the wrapper adds

  • Common args from im_common_args(): autojoin, format, features (with autojoin default controlled by getOption("immunarch.autojoin", FALSE)).

  • Validates idata is an immundata::ImmunData object.

  • Ensures all columns in required_cols exist in idata$annotations.

  • If autojoin = TRUE and the result is a data frame containing the repertoire id column (immundata::imd_schema("repertoire")), joins repertoire metadata from idata$repertoires.

Value

A function - the user-facing wrapper around core. Typical usage is to assign it to the exported symbol of the method, e.g.: airr_stats_lengths <- register_immunarch_method(...).

Examples

## Not run: 
# Minimal core implementation (must accept `idata`)
airr_stats_lengths_impl <- function(idata, seq_col = "cdr3_aa") {
  dplyr::as_tibble(idata$annotations) |>
    dplyr::distinct(.data[[immundata::imd_schema("repertoire")]], .data[[seq_col]]) |>
    dplyr::mutate(seq_len = nchar(.data[[seq_col]])) |>
    dplyr::count(.data[[immundata::imd_schema("repertoire")]], seq_len, name = "n")
}

# Register and expose a user-facing function
airr_stats_lengths <- register_immunarch_method(
  core = airr_stats_lengths_impl,
  family = "airr_stats",
  name = "lengths",
  required_cols = c("cdr3_aa", immundata::imd_schema("repertoire"))
)

# Optional: call via dispatcher
# make_airr_dispatcher("airr_stats")(idata = immdata, method = "lengths")

## End(Not run)


immunarch documentation built on Nov. 5, 2025, 7:21 p.m.