Skip to content

Diversity - estimating the heterogeneity of immune repertoires

Description

[Experimental]

A family of functions to quantify receptor diversity in each repertoire. Diversity describes how many different receptors are present and how evenly their abundances are distributed. These methods help you compare repertoire structure, detect clonal expansion, and assess sampling depth.

Available functions

The following methods are available.

1) Coverage diversity (airr_diversity_dxx). Calculate the minimum number of the most abundant receptors needed to cover perc% of a repertoire. For example, D50 is the number of receptors that cover half of the repertoire. Use it to identify repertoires dominated by expanded receptors.

2) Chao1 richness (airr_diversity_chao1). Estimate the total number of receptors, including receptors that may be missing because of limited sampling. Use this non-parametric estimator when rare receptors suggest that the observed repertoire is incomplete.

3) Rarefaction and extrapolation (airr_diversity_rarefaction). Estimate how receptor richness changes with sample size. Use interpolation to compare repertoires at a common sampling depth and extrapolation to estimate how many additional receptors may be found with deeper sampling.

Computation is done in RAM after one database query that fetches only the repertoire identifier and receptor counts. Interpolation uses the exact hypergeometric expectation for sampling without replacement. Extrapolation uses the finite-sample Chao estimator, and confidence bounds use bootstrap standard errors from an estimated abundance distribution. Clonotype counts must be positive integers.

4) Shannon entropy (airr_diversity_shannon). Calculate Shannon entropy from receptor proportions in each repertoire. The value increases when a repertoire contains more receptors or has a more even abundance distribution. Use it as one summary that reflects both richness and evenness.

5) Pielou evenness (airr_diversity_pielou). Calculate how evenly receptor abundance is distributed within each repertoire. The method divides Shannon entropy by the maximum entropy for the observed richness. Use it to compare evenness between repertoires with different numbers of receptors.

6) Diversity index (airr_diversity_index). Calculate the Hill number at q = 1, which is the exponential of Shannon entropy calculated with natural logarithms. Use it as an effective number of receptors that is easy to compare between repertoires.

7) Hill numbers (airr_diversity_hill). Calculate a diversity profile for one or more orders q. At q = 0, the result is receptor richness; at q = 1, it is the exponential of Shannon entropy; higher values of q give more weight to abundant receptors. Use several orders to compare the influence of rare and abundant receptors.

Usage

airr_diversity_dxx(
  idata,
  perc = 50,
  autojoin = getOption(IMMUNARCH_AUTOJOIN_OPTION, IMMUNARCH_AUTOJOIN_DEFAULT),
  format = c("long", "wide")
)

airr_diversity_chao1(
  idata,
  autojoin = getOption(IMMUNARCH_AUTOJOIN_OPTION, IMMUNARCH_AUTOJOIN_DEFAULT),
  format = c("long", "wide")
)

airr_diversity_rarefaction(
  idata,
  step = NA,
  quantile = c(0.025, 0.975),
  extrapolation = NA,
  nboot = 50,
  norm = TRUE,
  verbose = TRUE,
  autojoin = getOption(IMMUNARCH_AUTOJOIN_OPTION, IMMUNARCH_AUTOJOIN_DEFAULT),
  format = c("long", "wide")
)

airr_diversity_shannon(
  idata,
  autojoin = getOption(IMMUNARCH_AUTOJOIN_OPTION, IMMUNARCH_AUTOJOIN_DEFAULT),
  format = c("long", "wide")
)

airr_diversity_pielou(
  idata,
  autojoin = getOption(IMMUNARCH_AUTOJOIN_OPTION, IMMUNARCH_AUTOJOIN_DEFAULT),
  format = c("long", "wide")
)

airr_diversity_index(
  idata,
  autojoin = getOption(IMMUNARCH_AUTOJOIN_OPTION, IMMUNARCH_AUTOJOIN_DEFAULT),
  format = c("long", "wide")
)

airr_diversity_hill(
  idata,
  q = 0:5,
  autojoin = getOption(IMMUNARCH_AUTOJOIN_OPTION, IMMUNARCH_AUTOJOIN_DEFAULT),
  format = c("long", "wide")
)

Arguments

idata An immundata::ImmunData object.
perc A number or numeric vector in (0, 100] (default 50), e.g. 50 for D50, 20 for D20.
autojoin Logical. If TRUE, join repertoire metadata by the schema repertoire id. Change the default behaviour by calling options(immunarch.autojoin = FALSE).
format String. One of "long" ("long" tibble with imd_repertoire_id, facet columns, and value; useful for visualizations) or "wide" (wide/unmelted table of features, with each row corresponding to a specific repertoire / pair of repertoires; useful for Machine Learning).
step A whole number greater than or equal to 1, or NA. The default, NA, uses up to 50 evenly spaced points through the smallest observed depth. Each repertoire gets up to 50 points for each remaining interpolation and extrapolation interval.
quantile A numeric vector of length 2 containing confidence probabilities strictly between 0 and 1.
extrapolation A nonnegative whole number or NA giving the upper size limit for extrapolation. Use 0 to disable extrapolation. The default, NA, uses twice each repertoire's observed clone count.
nboot A nonnegative whole number that is either 0 or at least 2. It gives the number of bootstrap replicates used to estimate confidence bounds. Defaults to 50; use 0 to return NA confidence bounds.
norm A single logical value; if TRUE, size is divided by total repertoire size and richness estimates are divided by observed richness.
verbose A single logical value controlling whether a concise progress message is shown.
q A numeric scalar or vector of nonnegative orders. Defaults to 0:5.

Value

1) Coverage diversity (airr_diversity_dxx)

A tibble with:

  • imd_repertoire_id
  • perc
  • dxx - minimal count of top receptors to reach perc%
  • plus repertoire metadata from idata\$repertoires

2) Chao1 richness (airr_diversity_chao1)

A tibble with:

  • imd_repertoire_id
  • Estimator - estimated receptor richness
  • SD - standard deviation for the estimator value
  • Conf.95.lo - CI 0.025
  • Conf.95.hi - CI 0.975
  • plus repertoire metadata from idata\$repertoires

3) Rarefaction and extrapolation (airr_diversity_rarefaction)

A tibble with:

  • imd_repertoire_id
  • size - sample size (absolute or normalised)
  • q_low - lower confidence bound
  • mean - expected richness
  • q_high - upper confidence bound
  • type - interpolation or extrapolation
  • plus repertoire metadata from idata\$repertoires

4) Shannon entropy (airr_diversity_shannon)

A tibble with:

  • imd_repertoire_id
  • shannon - entropy in bits

5) Pielou evenness (airr_diversity_pielou)

A tibble with:

  • imd_repertoire_id
  • shannon
  • n_receptors
  • pielou - evenness in [0, 1] (NA if S <= 1)

6) Diversity index (airr_diversity_index)

A tibble with:

  • imd_repertoire_id
  • q = 1
  • hill_number
  • plus repertoire metadata from idata\$repertoires

7) Hill numbers (airr_diversity_hill)

A tibble with:

  • imd_repertoire_id
  • q - Hill order
  • hill_number - true diversity of order q
  • plus repertoire metadata from idata\$repertoires

Visualisation

Most airr_diversity_*() results can be passed directly to vis(). With autojoin = TRUE (the default), repertoire metadata is included in the result and can be selected by the plotting arguments.

1) Coverage diversity (airr_diversity_dxx)

vis() plots dxx for each repertoire. When several perc values are present, use fill = "perc" to show a separate column for each coverage threshold.

2) Chao1 richness (airr_diversity_chao1)

vis() produces a column plot of the estimated richness (Estimator) for each repertoire.

3) Rarefaction and extrapolation (airr_diversity_rarefaction)

vis() plots estimated richness (mean) against sample size (size). Use color to group curves, show_ci = FALSE to hide confidence intervals, and log = TRUE to use a logarithmic x-axis.

4) Shannon entropy (airr_diversity_shannon)

vis() produces a column plot of shannon for each repertoire.

5) Pielou evenness (airr_diversity_pielou)

vis() produces a column plot of pielou for each repertoire.

6) Diversity index (airr_diversity_index)

vis() produces a column plot of hill_number at q = 1 for each repertoire.

7) Hill numbers (airr_diversity_hill)

This result does not currently have a dedicated vis() method. Use the returned q and hill_number columns to create a diversity profile.

References

Chao, A. et al. (2014). Rarefaction and extrapolation with Hill numbers: a framework for sampling and estimation in species diversity studies. Ecological Monographs, 84, 45-67. doi:10.1890/13-0133.1

See Also

immundata::ImmunData

Examples

library("immunarch")

# Limit the number of threads used by the underlying DB for this example.
# Generally, you should NOT do this in your session.
db_exec("SET threads TO 1")

# Load example data.
immdata <- get_test_idata()

#
# Calculate D50 and several coverage thresholds.
d50 <- airr_diversity_dxx(immdata, perc = 50)


d_multi <- airr_diversity_dxx(immdata, perc = c(20, 50, 80))

# Visualise the coverage diversity.
vis(d_multi)

#
# Estimate receptor richness.
chao <- airr_diversity_chao1(immdata)

# Visualise the richness estimates.
vis(chao)

#
# Calculate a rarefaction curve without extrapolation.
raref <- airr_diversity_rarefaction(immdata, step = 2000)

# Visualise the rarefaction curve.
vis(raref)

#
# Calculate Shannon entropy.
sh <- airr_diversity_shannon(immdata)
vis(sh)

#
# Calculate Pielou evenness.
pj <- airr_diversity_pielou(immdata)
vis(pj)

#
# Calculate the default diversity index.
idx <- airr_diversity_index(immdata)
vis(idx)

#
# Calculate a diversity profile for three Hill orders.
hill <- airr_diversity_hill(immdata, q = c(0, 1, 2))