Track selected immune receptors across time and space¶
Description¶
Use vdj_track() to follow receptors across samples, time
points, tissues, or cell populations. For example, select abundant
receptors before treatment and find their abundances in later samples.
You can also follow a set of sequences identified in another experiment.
Select receptors with the argument from, or use
n to select the most abundant receptors in each source
repertoire. Follow them throughout the target idata,
including repertoires where they are rare or unobserved. The input
ImmunData object is not changed.
Usage¶
vdj_track(idata, from = NULL, n = NULL, output = c("immundata", "df"))
Arguments¶
idata |
An immundata::ImmunData object with defined repertoires. These are the repertoires through which receptors will be followed. |
from |
NULL, a numeric vector of repertoire IDs, an
immundata::ImmunData object, or a data frame. NULL, the
default, uses every target repertoire as a source. Numeric values select
source imd_repertoire_id values in idata. An
ImmunData source supplies its observed receptor identities.
A data frame supplies exact column combinations to match, such as
junction alone or junction together with
v_call. Tibbles and duckplyr tables are also accepted.
tibble::tibble is recommended. |
n |
A non-negative whole number or NULL. Select up to
n receptors with the highest counts in each source
repertoire, then follow their combined set of identities. Equal counts
are resolved by receptor ID, as in immundata::top_n().
NULL, the default, selects all source receptors. This
argument applies only when from is NULL or
contains repertoire IDs. |
output |
A character scalar. "immundata", the default, returns a
new ImmunData object containing the selected receptors'
observed cells and chains. "df" returns an
abundance-summary tibble with one row per selected receptor and input
repertoire, including explicit zeros where unobserved. Both outputs
preserve the original target abundances. The summary retains input
receptor and repertoire IDs, receptor identity features, and repertoire
metadata as columns, and ready for plotting with
vis(). |
Details¶
An ImmunData source must use the same receptor features and
chain schema as the target. Complete feature combinations, including
both chains of a paired receptor, are matched
Column names same as in the idata must exist in the target
observations in the from argument. Matching is exact and
case-sensitive, with no sequence or gene-call normalization. Missing
query values in from are not allowed. Unmatched queries,
empty query tables, and n = 0 produce an empty result.
Value¶
With output = "immundata", a new immundata::ImmunData
object containing the selected receptors' observed rows, with
imd_orig_count and imd_orig_proportion.
With output = "df", an immunarch result tibble containing:
-
imd_receptor_idand receptor identity features from the input receptor dictionary. Paired-chain features are shown side by side. -
imd_receptor_label, a readable label combining the receptor features. Paired-chain labels include both loci and use+between chains. -
imd_repertoire_idand metadata from the input repertoire table. -
imd_orig_countandimd_orig_proportion, including zeros where the receptor was unobserved. Proportions use the original repertoire totals.
Each selected receptor has exactly one row in every input repertoire.
Receptor features sharing a name with repertoire metadata receive the
suffix _receptor. Barcode,
chain-observation, and other row-level fields are excluded. The tibble
is recognised by vis() and needs no attached repertoire
metadata. Empty selections produce a zero-row summary with the same
column structure.
Common use cases¶
- Follow receptors from one or several samples. Supply their repertoire
IDs through
from. Receptors are followed in every target repertoire, including those outside the source selection. - Follow the top receptors from one sample. Supply a repertoire ID and
n, for examplefrom = 5L, n = 3Lfor the top three from repertoire ID 5. - Follow the top receptors from every sample. Supply
nand leavefrom = NULL. A receptor selected in any source is followed everywhere, even where it is below the topn. The combined set can contain more thannidentities. - Follow an upstream selection. Pass an
ImmunDataobject containing receptors of interest, such as expanded clones or a selected cell population. The source can also come from an independent import. - Follow a sequence panel. Pass a table containing sequences and optional gene or locus columns. All columns in a row must match the same chain; rows are alternatives. Matching one chain keeps the complete receptor, including its partner for paired receptors.
Original abundances¶
imd_orig_count and imd_orig_proportion columns
in the output store the target receptors counts and proportions before
vdj_track filters the data. Use these columns to compare
abundances across the original samples. The ordinary
imd_count and imd_proportion describe the
filtered object, following normal immundata behavior, i.e.,
normalising the proportions. Please be careful - another
vdj_track call replaces the original-abundance columns
imd_orig_count and imd_orig_proportion.
The ImmunData result contains observed chains only and
follows normal immundata filtering, including rebuilding
IDs for the remaining repertoires. In other words, the repertoire ID
could change after calling vdj_track with output =
"immundata". This is temporary inconvenience, sorry for that. The
good news is that no repertoire metadata is missing so you still analyse
the data. The "df" summary uses the input IDs and contains
zero-count rows for unobserved combinations. These are abundance rows,
without barcode or observation-specific fields. A receptor not observed
in a sample is not necessarily biologically absent.
Visualisation¶
Pass vdj_track(..., output = "df") directly to
vis(). The summary tibble carries a result class that
selects the tracking plot. The default ImmunData output is
intended for further analysis, so pass output = "df"
whenever you want to plot the results.
vis() draws an alluvial plot of
imd_orig_proportion by imd_repertoire_id by
default. Receptors are distinguished by colour and labelled by their
receptor features in the legend, for example
AAA V01. For paired receptors,
each chain's locus and features are combined, for example
IGH AAA V01 + IGL BBB V02.
Receptor IDs remain the grouping keys, so pairs sharing one chain remain
separate trajectories. The plot accepts:
-
plot = "alluvial"(the default) draws stacked bars connected by smooth ribbons and requires the optional package ggalluvial. -
plot = "area"draws stacked areas. -
plot = "line"draws individual trajectories with points. -
xvalselects the repertoire ID or metadata column for the x-axis. The default is to use the numeric repertoire IDs. This allows you to select metadata columns such as "Timepoint". -
yvalselects"imd_orig_proportion"(the default) or"imd_orig_count". These preserve abundances from the original target repertoires. Proportions are not renormalised to the selected receptors and need not sum to 100%. -
facetselects one metadata column for separate panels. Each x value within a panel must identify exactly one repertoire. Define repertoires upstream to pool samples; plotting does not aggregate them. -
titlereplaces the default title,"Receptor tracking".
The summary already contains zero abundances for unobserved receptor-repertoire combinations. Every original target repertoire remains on the plot, including samples with no tracked observations.
Repertoire metadata is returned in regular columns for every target
sample, including samples with no tracked observations. Use those
columns for xval and facet, or prepare
additional display columns in the summary.
See Also¶
-
vis()for plotting the tracking abundance summary. -
immundata::top_n()for selecting abundant receptors upstream. -
immundata::filter_immundata()for filtering observations upstream.
Examples¶
library("immunarch")
library(immundata)
# A small bulk dataset sampled at four time points.
# At T3, only TTT is observed; neither AAA nor CCC is observed.
rows <- tibble::tibble(
sample = c(rep(c("T0", "T1", "T2"), each = 3), "T3"),
junction = c(
"AAA", "CCC", "GGG", "AAA", "CCC", "TTT",
"AAA", "GGG", "ACG", "TTT"
),
v_call = c("V1", "V2", "V1", "V1", "V2", "V3", "V1", "V1", "V4", "V3"),
abundance = c(8L, 2L, 1L, 1L, 9L, 4L, 2L, 3L, 5L, 5L)
)
input <- tempfile(fileext = ".tsv")
readr::write_tsv(rows, input)
idata <- read_repertoires(
input,
schema = c("junction", "v_call"), count_col = "abundance",
repertoire_schema = "sample", output_folder = tempfile("vdj-track-"),
preprocess = NULL, postprocess = NULL, rename_columns = NULL,
verbose = FALSE
)
source_id <- idata$repertoires |>
dplyr::filter(sample == "T0") |>
dplyr::pull(imd_repertoire_id)
# Follow every receptor observed at T0, or only its two most abundant ones.
from_t0 <- vdj_track(idata, from = source_id)
top_from_t0 <- vdj_track(idata, from = source_id, n = 2L)
# Follow any receptor that is the most abundant in at least one repertoire.
top_from_each <- vdj_track(idata, n = 1L)
# An upstream selection can also supply the receptors to follow.
selected <- immundata::top_n(idata, n = 1L)
from_selected <- vdj_track(idata, from = selected)
# Match sequences alone, or exact sequence/gene combinations.
from_sequences <- vdj_track(idata, from = tibble::tibble(
junction = c("AAA", "GGG")
))
from_panel <- vdj_track(idata, from = tibble::tibble(
junction = c("AAA", "CCC"), v_call = c("V1", "V2")
), output = "df")
# The summary already has one row per receptor and repertoire: 2 x 4 rows.
from_panel
#> # A tibble: 8 × 10
#> imd_receptor_id junction v_call imd_receptor_label imd_repertoire_id sample
#> * <int> <chr> <chr> <chr> <int> <chr>
#> 1 3 CCC V2 CCC V2 1 T0
#> 2 4 AAA V1 AAA V1 1 T0
#> 3 3 CCC V2 CCC V2 2 T1
#> 4 4 AAA V1 AAA V1 2 T1
#> 5 3 CCC V2 CCC V2 3 T2
#> 6 4 AAA V1 AAA V1 3 T2
#> 7 3 CCC V2 CCC V2 4 T3
#> 8 4 AAA V1 AAA V1 4 T3
#> # ℹ 4 more variables: n_barcodes <int>, n_receptors <int>,
#> # imd_orig_count <dbl>, imd_orig_proportion <dbl>
# Both selected receptors have explicit zero counts and proportions at T3.
from_panel |>
dplyr::filter(sample == "T3") |>
dplyr::select(sample, junction, imd_orig_count, imd_orig_proportion)
#> # A tibble: 2 × 4
#> sample junction imd_orig_count imd_orig_proportion
#> <chr> <chr> <dbl> <dbl>
#> 1 T3 CCC 0 0
#> 2 T3 AAA 0 0
# Select baseline receptors, then visualise their original abundances.
tracked <- vdj_track(idata, from = source_id, n = 2L, output = "df")
tracked$sample <- factor(tracked$sample, levels = c("T0", "T1", "T2", "T3"))
# T3 remains on each plot, with both receptor abundances at zero.
# Legend labels combine receptor features automatically, for example AAA V1.
vis(tracked, xval = "sample")



# Plot original counts instead of proportions.
vis(tracked, xval = "sample", plot = "line", yval = "imd_orig_count")
