Immune receptor distances¶
Description¶
A family of functions to calculate sequence distances between immune receptors. These methods help you explore receptor similarity, select thresholds for Immunoglobulin clone assignment, and construct sparse receptor-similarity graphs.
Available functions
The following methods are available.
1) Hamming distances (dist_hamm). Compute
an upper-triangular sparse edge table of Hamming distances between
receptor IDs represented in the receptor annotations in
idata. Comparisons are restricted to equal sequence lengths
and equal values of by. The data are not grouped by
repertoire automatically. For paired-chain receptor schemas, the
canonical locus column is added to by automatically, so
distances are computed separately per locus. Add it explicitly if you
have a non-AIRR-C standard locus column name.
Usage¶
dist_hamm(
idata,
seq_col = "cdr3_aa",
by = NULL,
max_dist = NULL,
min_sim = NULL,
sample_n = 0L,
sample_by = "<auto>",
mode = c("pairs", "graph")
)
Arguments¶
idata |
An immundata::ImmunData object. |
seq_col |
A single character string naming a sequence feature in the receptor schema. |
by |
A character vector or NULL. These optional annotation
columns must be equal within a receptor pair. For B-cell clone
assignment, a typical choice is c("v_call", "j_call"); add
imd_repertoire_id when comparisons must stay within
repertoires, or subject_id if your data has such a
dedicated column for subject-specific metadata. Locus grouping is also
always applied for paired-chain receptor schemas. |
max_dist |
A single nonnegative number, a keyed threshold table, or
NULL. Scalar values in (0, 1) bound normalized Hamming
distance; integer values which are >=1 bound raw Hamming distance.
NULL (the default) and the legacy scalar value
0 disable bounding. A threshold table must contain one or
more key columns plus a numeric threshold column strictly
inside (0, 1). Its key columns
must be included in by and must exactly cover the
corresponding input key combinations. Mutually exclusive with
min_sim. |
min_sim |
A single number in [0, 1] or
NULL, giving the minimum normalized Hamming similarity. It
is mutually exclusive with max_dist. |
sample_n |
A nonnegative whole number giving the maximum number of distinct
receptor IDs to sample before distance calculation within each
sample_by group. 0 (the default) disables
sampling. |
sample_by |
A single character string or NULL selecting the column
to sample by. The default, "<auto>", uses the first
available column among subject_id, donor,
donor_id, patient_id, patient,
subject, and imd_repertoire_id. The resolved
column must also be supplied through by. Set to
NULL to sample from the whole dataset without accounting
for groups. This argument is ignored when
sample_n = 0. |
mode |
Output representation. "pairs" (the default) returns
the receptor-pair table described below. "graph" returns
contracted sequence-node edges and receptor-to-node membership. Grouping
columns cannot use names reserved for structural columns in the selected
output. |
Value¶
1) Hamming distances (dist_hamm)
With mode = "pairs", a lazy duckplyr table with:
- grouping columns supplied through
by -
locusfor paired-chain receptor schemas -
imd_receptor_id_1,imd_receptor_id_2– canonical receptor identifiers, withimd_receptor_id_1 < imd_receptor_id_2 -
seq_len -
dist– raw Hamming distance -
norm_dist–dist / seq_len -
sim– normalized Hamming similarity,1 - norm_dist
With mode = "graph", an
immunarch_sequence_graph object containing two lazy
duckplyr tables. edges contains the effective grouping
columns, globally unique from and to node IDs,
and seq_len, dist, norm_dist, and
sim. membership contains the effective
grouping columns, the canonical receptor ID, and node_id. A
receptor can have multiple membership rows, and membership includes
nodes without edges. Nodes represent distinct combinations of the full
effective comparison group and a non-missing, non-empty sequence;
sequences themselves are not returned.
Call dplyr::collect() only when a returned table is small
enough for memory.
Visualisation¶
Distance results can be passed directly to vis().
1) Hamming distances (dist_hamm)
vis() plots the normalized distance to the nearest
non-identical neighbor for each receptor by default. This is the
distribution used for manual IG clone-threshold selection. When a
subject column can be inferred using the same priority as
sample_by = "<auto>", one panel is drawn per subject;
otherwise, a single pooled distribution is drawn.
Use mode = "all" to plot every pairwise distance instead of
nearest neighbors. Pass one of the following values to xval
to change what will be plotted: "norm_dist" (the default),
"dist" (non-normalised distnaces), or "sim"
(similarity). Set facet = NULL to have one plot per all
input samples, or name a result column to facet by it.
binwidth and title customize the histogram.
See Also¶
immundata::ImmunData
Examples¶
library("immunarch")
idata <- get_test_idata()
# Build the computing query
res <- dist_hamm(idata)
# Check how results would look like
res
# Run the computation query and upload the results to the memory
res <- res |> collect()
# Bound Hamming distance.
# Non-normalised -- no receptors with more than 1 mismatch in the output
res <- dist_hamm(idata, max_dist = 1) |> collect()
res
# Normalised -- no receptors with more than .2 normalised distance in the output
res <- dist_hamm(idata, max_dist = .2) |> collect()
res
# Similarity, inverse to normalised distance -- no receptors with
# less than 0.9 similarity in the output
# The results are the same as for the previous normalised run
res <- dist_hamm(idata, min_sim = .8) |> collect()
res
# Plot normalized nearest-non-identical-neighbor distances.
vis(dist_hamm(idata))
# Plot all raw pairwise distances instead.
vis(dist_hamm(idata), mode = "all", xval = "dist")
#
# Typical use case for BCR data
#
# BCR-style V/J/length blocking with at most three mismatches, inside patients
clone_edges <- dist_hamm(
idata_bcr,
seq_col = "junction",
by = c("patient", "v_call", "j_call"),
max_dist = 3
)