Skip to content

Descriptive immune repertoire statistics

Description

[Experimental]

A family of functions that summarise the basic structure of immune repertoires. These summaries help you check data quality, compare samples, and describe chain counts, sequence lengths, and gene usage before more detailed analyses.

Available functions

The following methods are available.

1) Chain counts (airr_desc_chains). Count V(D)J chains in each repertoire, optionally grouped by locus. Use this method to check capture depth, compare library sizes, examine TRA/TRB/IGH balance, and identify locus-specific loss or over-representation.

2) Sequence-length distribution (airr_desc_lengths). Count sequences of each length per repertoire, optionally grouped by annotation columns. Use this method to describe CDR3 length distributions, detect possible library preparation bias, compare groups, and create length-based features.

3) Gene usage (airr_desc_genes). Count V(D)J gene segments per repertoire, optionally grouped by locus. The method can count unique receptors or sum barcode/UMI counts. Use it to compare gene usage between groups, identify unusual gene patterns, and create repertoire-level features.

Usage

airr_desc_chains(
  idata,
  locus_col = NA,
  autojoin = getOption(IMMUNARCH_AUTOJOIN_OPTION, IMMUNARCH_AUTOJOIN_DEFAULT),
  format = c("long", "wide")
)

airr_desc_lengths(
  idata,
  seq_col = "cdr3_aa",
  by = NA_character_,
  autojoin = getOption(IMMUNARCH_AUTOJOIN_OPTION, IMMUNARCH_AUTOJOIN_DEFAULT),
  format = c("long", "wide")
)

airr_desc_genes(
  idata,
  gene_col = "v_call",
  level = c("receptor", "barcode"),
  by = NA_character_,
  autojoin = getOption(IMMUNARCH_AUTOJOIN_OPTION, IMMUNARCH_AUTOJOIN_DEFAULT),
  format = c("long", "wide")
)

Arguments

idata An immundata::ImmunData object.
locus_col A single character string, NA, or NULL. The default, NA, uses the canonical locus column when it is available. Supply NULL to combine loci, or a column name to group by that column.
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).
seq_col A single character string naming the sequence column.
by A character vector, NA, or NULL. The default NA automatically groups by the canonical locus column when it is available. Supply NULL to pool loci, or a character vector to group by and return those columns explicitly.
gene_col A single character string naming a gene-segment column calls (e.g., "v_call", "d_call", "j_call", "c_call"). Default is "v_call".
level A single character string: "receptor" or "barcode". If "receptor" (default), the function counts unique receptors (one per receptor ID) that carry a given gene segment. If "barcode", the function sums counts (e.g., cells/UMIs) per gene segment using the column defined by immundata::imd_schema("count").

Value

1) Chain counts (airr_desc_chains)

A tibble with columns:

  • imd_repertoire_id – internal repertoire identifier
  • repertoire-summary columns carried from idata, such as n_barcodes and n_receptors
  • locus – TRA, TRB, IGH, ...; NA when locus grouping is disabled
  • n_chains – number of chains

2) Sequence-length distribution (airr_desc_lengths)

A tibble with columns:

  • imd_repertoire_id – internal repertoire identifier
  • grouping columns requested through by (for example, locus)
  • seq_len – lengths of sequences
  • n – number of sequence rows
  • prop, pct – proportion and percentage within each repertoire and combination of grouping columns

3) Gene usage (airr_desc_genes)

A tibble with columns:

  • <gene_col> - the gene segment value (e.g., V gene)
  • imd_repertoire_id - internal repertoire identifier
  • grouping columns requested through by (for example, locus)
  • n - the measure:
    • if level = "receptor": number of receptors carrying the gene segment within the grouping
    • if level = "barcode": sum of counts across receptors for the segment

Visualisation

All three airr_desc_*() results can be passed directly to vis(). With autojoin = TRUE (the default), repertoire summaries and user metadata are included in the result and can be selected by the plotting arguments.

1) Chain counts (airr_desc_chains)

vis() plots one point per repertoire, with n_barcodes on the x-axis and n_receptors on the y-axis. Both axes use log scales. Dashed lines mark overall receptor-to-barcode ratios of 100%, 50%, and 25%. Locus-specific rows share the same repertoire totals, so they appear as one point. The plot accepts:

  • label selects one or more columns for point labels; the default is imd_repertoire_id.
  • colour colours points by a repertoire metadata column. fill is accepted as an alias for existing calls.
  • title replaces the default plot title.

For single-cell data, n_barcodes is the observed cell count; for bulk data, it is total abundance. The ratio shows repertoire-level richness, not how many receptors occur within each barcode.

2) Sequence-length distribution (airr_desc_lengths)

vis() plots seq_len on the x-axis and prop on the y-axis, filled by repertoire. Set fill to a metadata or grouping column to compare groups with box-and-violin distributions. facet accepts one column for a wrapped layout or two columns for a facet grid; dir = "h" or dir = "v" controls the wrapping direction.

3) Gene usage (airr_desc_genes)

vis() produces a dot plot with gene segments as rows, repertoires as columns, and n mapped to dot size and colour. Use row to select the gene column, col to select repertoire or metadata columns, and value to select the numeric measure. size_max_mm controls the largest dot; row_order and col_order set the display order.

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()

#
# Count chains in each repertoire.
chain_stats <- airr_desc_chains(immdata)

# Repertoire size versus distinct receptor count
vis(chain_stats)

# Label and colour by repertoire metadata when those columns are available.
# vis(chain_stats, label = "Sample", colour = "Tissue")


#
# Calculate CDR3 length distributions.
length_stats <- airr_desc_lengths(immdata)

# Default: CDR3 length proportions by repertoire
vis(length_stats)

# Compare distributions between repertoires and split them by locus.
vis(length_stats, fill = "imd_filename", facet = "locus")

airr_desc_lengths(immdata, by = "locus")
#> # A tibble: 28 × 7
#>    imd_repertoire_id locus seq_len     n    prop    pct imd_filename            
#>  *             <int> <chr>   <dbl> <int>   <dbl>  <dbl> <chr>                   
#>  1                 1 TCRB        5     1 0.00105  0.105 /home/runner/work/_temp…
#>  2                 1 TCRB        6     1 0.00105  0.105 /home/runner/work/_temp…
#>  3                 1 TCRB        7     2 0.00209  0.209 /home/runner/work/_temp…
#>  4                 1 TCRB        8     9 0.00942  0.942 /home/runner/work/_temp…
#>  5                 1 TCRB        9    38 0.0398   3.98  /home/runner/work/_temp…
#>  6                 1 TCRB       10   121 0.127   12.7   /home/runner/work/_temp…
#>  7                 1 TCRB       11   163 0.171   17.1   /home/runner/work/_temp…
#>  8                 1 TCRB       12   233 0.244   24.4   /home/runner/work/_temp…
#>  9                 1 TCRB       13   190 0.199   19.9   /home/runner/work/_temp…
#> 10                 1 TCRB       14   119 0.125   12.5   /home/runner/work/_temp…
#> # ℹ 18 more rows
airr_desc_lengths(immdata, by = NULL)
#> # A tibble: 28 × 6
#>    imd_repertoire_id seq_len     n    prop    pct imd_filename                  
#>  *             <int>   <dbl> <int>   <dbl>  <dbl> <chr>                         
#>  1                 1       5     1 0.00105  0.105 /home/runner/work/_temp/Libra…
#>  2                 1       6     1 0.00105  0.105 /home/runner/work/_temp/Libra…
#>  3                 1       7     2 0.00209  0.209 /home/runner/work/_temp/Libra…
#>  4                 1       8     9 0.00942  0.942 /home/runner/work/_temp/Libra…
#>  5                 1       9    38 0.0398   3.98  /home/runner/work/_temp/Libra…
#>  6                 1      10   121 0.127   12.7   /home/runner/work/_temp/Libra…
#>  7                 1      11   163 0.171   17.1   /home/runner/work/_temp/Libra…
#>  8                 1      12   233 0.244   24.4   /home/runner/work/_temp/Libra…
#>  9                 1      13   190 0.199   19.9   /home/runner/work/_temp/Libra…
#> 10                 1      14   119 0.125   12.5   /home/runner/work/_temp/Libra…
#> # ℹ 18 more rows
#
# Calculate V gene usage from receptor counts.
gene_stats <- airr_desc_genes(
  immdata,
  gene_col = "v_call",
  level = "receptor"
)

# Default: genes by repertoire, with counts mapped to dot size and colour
vis(gene_stats)

# Compare gene usage across repertoires and loci.
vis(
  gene_stats,
  row = "v_call",
  col = c("imd_filename", "locus"),
  value = "n",
  size_max_mm = 5
)

# V gene usage by summed cell/UMI counts (if a count column is present)
airr_desc_genes(immdata, gene_col = "v_call", level = "barcode")
#> # A tibble: 92 × 5
#>    v_call      imd_repertoire_id locus     n imd_filename                       
#>  * <chr>                   <int> <chr> <dbl> <chr>                              
#>  1 TRBV20-1*01                 1 TCRB    123 /home/runner/work/_temp/Library/im…
#>  2 TRBV29-1*01                 1 TCRB    122 /home/runner/work/_temp/Library/im…
#>  3 TRBV28*01                   1 TCRB     97 /home/runner/work/_temp/Library/im…
#>  4 TRBV18*01                   1 TCRB     86 /home/runner/work/_temp/Library/im…
#>  5 TRBV7-9*01                  1 TCRB     60 /home/runner/work/_temp/Library/im…
#>  6 TRBV7-2*01                  1 TCRB     44 /home/runner/work/_temp/Library/im…
#>  7 TRBV7-8*01                  1 TCRB     36 /home/runner/work/_temp/Library/im…
#>  8 TRBV12-3*01                 1 TCRB     36 /home/runner/work/_temp/Library/im…
#>  9 TRBV10-2*01                 1 TCRB     32 /home/runner/work/_temp/Library/im…
#> 10 TRBV6-5*01                  1 TCRB     28 /home/runner/work/_temp/Library/im…
#> # ℹ 82 more rows
# Split by locus (TRA/TRB/... if locus column exists)
airr_desc_genes(immdata, gene_col = "v_call", level = "receptor", by = "locus")
#> # A tibble: 92 × 5
#>    v_call      imd_repertoire_id locus     n imd_filename                       
#>  * <chr>                   <int> <chr> <int> <chr>                              
#>  1 TRBV20-1*01                 1 TCRB    112 /home/runner/work/_temp/Library/im…
#>  2 TRBV29-1*01                 1 TCRB    105 /home/runner/work/_temp/Library/im…
#>  3 TRBV28*01                   1 TCRB     91 /home/runner/work/_temp/Library/im…
#>  4 TRBV18*01                   1 TCRB     72 /home/runner/work/_temp/Library/im…
#>  5 TRBV7-9*01                  1 TCRB     53 /home/runner/work/_temp/Library/im…
#>  6 TRBV7-2*01                  1 TCRB     43 /home/runner/work/_temp/Library/im…
#>  7 TRBV12-3*01                 1 TCRB     36 /home/runner/work/_temp/Library/im…
#>  8 TRBV7-8*01                  1 TCRB     35 /home/runner/work/_temp/Library/im…
#>  9 TRBV6-5*01                  1 TCRB     28 /home/runner/work/_temp/Library/im…
#> 10 TRBV2*01                    1 TCRB     27 /home/runner/work/_temp/Library/im…
#> # ℹ 82 more rows
# Pool loci explicitly
airr_desc_genes(immdata, gene_col = "v_call", by = NULL)
#> # A tibble: 92 × 4
#>    v_call      imd_repertoire_id     n imd_filename                             
#>  * <chr>                   <int> <int> <chr>                                    
#>  1 TRBV20-1*01                 1   112 /home/runner/work/_temp/Library/immundat…
#>  2 TRBV29-1*01                 1   105 /home/runner/work/_temp/Library/immundat…
#>  3 TRBV28*01                   1    91 /home/runner/work/_temp/Library/immundat…
#>  4 TRBV18*01                   1    72 /home/runner/work/_temp/Library/immundat…
#>  5 TRBV7-9*01                  1    53 /home/runner/work/_temp/Library/immundat…
#>  6 TRBV7-2*01                  1    43 /home/runner/work/_temp/Library/immundat…
#>  7 TRBV12-3*01                 1    36 /home/runner/work/_temp/Library/immundat…
#>  8 TRBV7-8*01                  1    35 /home/runner/work/_temp/Library/immundat…
#>  9 TRBV6-5*01                  1    28 /home/runner/work/_temp/Library/immundat…
#> 10 TRBV2*01                    1    27 /home/runner/work/_temp/Library/immundat…
#> # ℹ 82 more rows