Compute inter-clonal CDR3 amino acid convergence scores per neighbourhood
Source:R/nhood_cdr3_convergence.R
nhood_cdr3_convergence.RdComputes pairwise complementary determining region 3 (CDR3) amino acid sequence similarities between different B cell receptor (BCR) or T cell receptor (TCR) clones within a neighbourhood, and summarises this score per neighbourhood.
Usage
nhood_cdr3_convergence(
object,
nhood_col = NULL,
receptor_type = c("BCR", "TCR"),
chain = c("VDJ", "VJ", "both"),
aa_col,
clone_id_col = NULL,
min_n_contigs = 20L,
min_clones = NULL,
substitution_weighting = c("levenshtein", "blosum62", "custom"),
substitution_matrix = NULL,
summary_metric = "converge_score",
knn_k = NULL,
downsample = TRUE,
n_boots = 100,
seed = NULL,
BPPARAM = BiocParallel::SerialParam(),
result_name = "nhood_cdr3_convergence"
)Arguments
- object
A
MiloorSingleCellExperimentobject. Seurat objects must first be converted viaas.SingleCellExperiment.- nhood_col
Character string. For plain
SingleCellExperimentobjects (i.e. notMilo), the name of thecolDatacolumn containing neighbourhood assignments. Not required forMiloobjects.- receptor_type
Character string; one of
"BCR"(default) or"TCR". Controls clone-level representative selection: for BCRs, CDR3 sequences belonging to the same clone may have different sequences due to somatic hypermutation (SHM), hence a centroid sequence is chosen per clone (the sequence with the lowest mean pairwise distance to all other members of the same clone). For TCRs, unique sequences can be used directly as representatives. Ignored ifclone_id_colisNULL.- chain
Character string; one of
"VDJ"(default),"VJ", or"both". Determines which receptor chain(s) are analysed. Whenchain = "both",aa_colmust be a named character vector with elements"VDJ"and"VJ"(seeaa_col).- aa_col
Character string or named character vector specifying the
colDatacolumn(s) containing CDR3 amino acid sequences. Whenchainis"VDJ"or"VJ", supply a single column name. Whenchain = "both", supply a named vector with elements"VDJ"and"VJ", e.g.:aa_col = c(VDJ = "cdr3_aa_heavy", VJ = "cdr3_aa_light"). Cells withNA, empty string,"No_contig", or"None"in the relevant column are excluded from analysis.- clone_id_col
Character string; name of the
colDatacolumn containing clone identifiers. When supplied andreceptor_type = "BCR", one centroid sequence is selected per clone per neighbourhood. WhenNULL(default), all unique CDR3 sequences are treated as individual representatives regardless ofreceptor_type. If the column is not found incolData, a warning is issued and the function falls back to unique-sequence mode.- min_n_contigs
Integer; minimum number of cells with valid contig annotation required for a neighbourhood to be included in the analysis. Neighbourhoods below this threshold are assigned
NAfor all distance metrics andconverge_score_reliable = FALSE. Default is20. Whenmin_clonesis also supplied, settingmin_n_contigsbelow that value gives it no independent gating effect; a warning is issued in this case.- min_clones
Integer or
NULL(default). When supplied, neighbourhoods with fewer representative sequences than this threshold are excluded: all metrics are set toNAandconverge_score_reliable = FALSE. WhenNULL, all neighbourhoods that passmin_n_contigsare processed regardless of representative count. Must be at least equal tomin_n_contigsto have any independent gating effect; a warning is issued otherwise.converge_scoremay be unstable whenn_clonesis small relative to \(k\): when the adaptive \(k\) approachesn_clones, each sequence's nearest neighbours span nearly the entire neighbourhood and the kNN score converges toward the global mean. If this is a concern, setmin_clonesto a value comfortably above the adaptive \(k\) floor (e.g.20) to exclude such neighbourhoods, or inspect<chain>_n_clonesin the output to identify small neighbourhoods post-hoc.- substitution_weighting
Character string; one of
"levenshtein"(default),"blosum62", or"custom"."levenshtein"Flat substitution cost: 0 for identical amino acids, 1 for any mismatch.
"blosum62"Substitution cost derived from BLOSUM62 via \(c(i,j) = (s(i,i) + s(j,j)) / 2 - s(i,j)\), normalised to \([0, 1]\) by the maximum off-diagonal cost. Physicochemically similar amino acids incur lower cost than dissimilar ones.
"custom"User-supplied substitution weightings passed as a matrix to
substitution_matrix.
- substitution_matrix
A square numeric matrix with single-letter amino acid codes as named rows and columns. The matrix must be bounded -1-0, where higher values indicate more similar amino acids. Used when
substitution_weighting = "custom".- summary_metric
Character vector; one or more of
"converge_score"(default),"converge_score_global", and"converge_score_global_median". Controls which summary convergence metrics are calculated:"converge_score"Local convergence score computed via a kNN-based approach.
NAwhenn_clones <= resolved_k, or whenmin_clonesis supplied and not met."converge_score_global"Mean pairwise CDR3 convergence score across the upper triangle of the representative distance matrix.
"converge_score_global_median"Median pairwise CDR3 convergence score across the upper triangle of the representative distance matrix.
Any combination of the three values may be supplied. The default is
"converge_score".- knn_k
Integer or
NULL(default). Number of nearest neighbours to use when computingconverge_score. WhenNULL, \(k\) is set adaptively to \(\lfloor 0.25 \times n_{\text{reps}} \rfloor\) per neighbourhood. 3 is the minimum permitted adaptive \(k\) to ensure the metric captures more than just the nearest pairwise comparisons (not applied to a fixed \(k\)). Within each bootstrap subsample \(k\) is additionally capped at \(n - 1\) to guard against subsamples smaller than \(k\).- downsample
Logical; whether to downsample large neighbourhoods to a common representative depth before computing distances. The downsampling depth is set to the median number of unique representatives across all passing neighbourhoods (excluding singletons), computed separately per chain. Downsampling is performed via bootstrapping (
n_bootsiterations). Default isTRUE. WhenFALSE, the full representative matrix is used directly and_sdcolumns are not reported.- n_boots
Integer; number of bootstrap iterations used during downsampling. Each iteration subsamples
downsample_depthrepresentatives without replacement and computes distance summaries; reported values are the mean across iterations and the corresponding_sdcolumn reports the standard deviation across iterations for each requested metric. Default is100. Ignored whendownsample = FALSEor when a neighbourhood has fewer representatives than the downsampling depth. Whendownsample = TRUEand"converge_score"is insummary_metric, the effective sample size for kNN computation within each bootstrap iteration is the downsampling depth rather than the full representative count.- seed
Integer or
NULL(default). Random seed for reproducibility of the bootstrap downsampling procedure. When supplied, a neighbourhood-specific seed is derived asseed + i(whereiis the neighbourhood index), ensuring that each neighbourhood's bootstrap is independently reproducible while the full run remains deterministic. Set this parameter whenever results will be reported or compared across runs. Has no effect whendownsample = FALSE.- BPPARAM
A
BiocParallelParaminstance controlling parallelisation. The default,SerialParam(), runs sequentially with per-neighbourhood progress messages. Parallel backends (e.g.MulticoreParam,SnowParam) suppress per-neighbourhood messages but respect all other arguments. Note that when using process-based parallel backends, all data objects are serialised to each worker, which may increase peak memory usage substantially for large datasets.- result_name
Character string; the key under which results are stored in
metadata(object). If an entry with this name already exists a warning is emitted and the entry is overwritten. Default is"nhood_cdr3_convergence".
Value
The input object with a data.frame added to
metadata(object)[[result_name]]. The data.frame has one row per
neighbourhood and the following columns:
nhoodNeighbourhood identifier.
nhood_sizeTotal number of cells in the neighbourhood, including cells without a contig. Used as the denominator for
contig_capture_rate.<chain>_n_contigsNumber of cells with a contig for this chain.
<chain>_contig_capture_rateProportion of neighbourhood cells with a contig (
n_contigs / nhood_size).NAifnhood_sizeis zero.<chain>_n_clonesNumber of representative sequences used for distance computation (unique sequences for contigs without clone labels; one centroid per clone for BCR with clone labels).
<chain>_converge_score_reliableLogical;
FALSEwhenn_contigs < min_n_contigs(all metricsNA), or whenmin_clonesis supplied andn_clonesdoes not meet it (all metricsNA).TRUEfor all other neighbourhoods, including those whereconverge_scoreisNAdue to insufficient sequences for kNN.CDR3_converge_score_<chain>kNN convergence score per neighbourhood, averaged across all representatives. Captures local sequence density. When downsampling, this is the mean across bootstrap iterations.
NAwhenn_clones <= resolved_k(insufficient sequences for kNN), or whenmin_clonesis supplied andn_clonesdoes not meet it. Only present when"converge_score"is insummary_metric. Example:CDR3_converge_score_VDJ.CDR3_converge_score_sd_<chain>Standard deviation of
CDR3_converge_score_<chain>across bootstrap iterations. Only present whendownsample = TRUEand"converge_score"is insummary_metric.NAwhenconverge_scoreisNA(kNN could not be computed). Example:CDR3_converge_score_sd_VDJ.CDR3_converge_score_global_<chain>Mean pairwise CDR3 convergence score across the upper triangle of the representative distance matrix. When downsampling, this is the mean across bootstrap iterations. Only present when
"converge_score_global"is insummary_metric. Example:CDR3_converge_score_global_VDJ.CDR3_converge_score_global_sd_<chain>Standard deviation of
CDR3_converge_score_global_<chain>across bootstrap iterations. Only present whendownsample = TRUEand"converge_score_global"is insummary_metric. Example:CDR3_converge_score_global_sd_VDJ.CDR3_converge_score_global_median_<chain>Median pairwise CDR3 convergence score across the upper triangle of the representative distance matrix. When downsampling, this is the mean of bootstrap-iteration medians. Only present when
"converge_score_global_median"is insummary_metric. Example:CDR3_converge_score_global_median_VJ.CDR3_converge_score_global_median_sd_<chain>Standard deviation of
CDR3_converge_score_global_median_<chain>across bootstrap iterations. Only present whendownsample = TRUEand"converge_score_global_median"is insummary_metric. Example:CDR3_converge_score_global_median_sd_VDJ.
Where chain = "both", columns are present for both VDJ and
VJ chains.
The returned data.frame also carries the following attributes
recording the analysis parameters: receptor_type,
substitution_weighting, chains, downsample_depths,
min_n_contigs, min_clones, downsample,
n_boots, summary_metric.
Details
Summary of steps
For each neighbourhood the following steps are performed:
A single pairwise distance matrix is computed over all unique CDR3 sequences per neighbourhood.
For BCR data with clone labels, one centroid sequence is selected per clone as the sequence with the lowest mean pairwise distance to all other unique sequences within that clone in the neighbourhood. The raw pairwise distance matrix is filtered to these centroids.
If
downsample = TRUEand the number of representatives exceeds the target depth, bootstrapped subsampling is performed over the pre-computed distance matrix (no re-alignment), and summary statistics are averaged across iterations.The distance matrix is summarised per neighbourhood using the requested metric
Object type handling
Milo objects: Neighbourhood membership is read directly from the sparse
nhoodsmatrix vianhoods. Row names of this matrix must matchcolnames(object); a mismatch raises an error to prevent silent misassignment.Plain SingleCellExperiment objects: A binary neighbourhood membership matrix is constructed internally from the column specified by
nhood_col. Cells withNAassignments are excluded with a warning.
Distance calculation methods
Pairwise distances are computed using
pwalign::stringDist()
across all unique CDR3 amino acid sequences per neighbourhood requested
exceeding min_n_contigs and optionally min_clones. CDR3 sequences can differ
via insertions, deletions or substitutions. In all cases, insertions and
deletions are assigned a cost of 1. When substitution_weighting = "levenshtein", all
substitutions also incur a cost of 1. Substitutions can also be weighted
according to their physicochemical significance when substitution_weighting = "blosum62".
The raw BLOSUM substitution matrix is converted to a cost matrix via
\(c(i,j) = (s(i,i) + s(j,j))/2 - s(i,j)\). This cost matrix is then
normalised to 0-1 by dividing each cost by the maximum off-diagonal cost.
The normalised cost matrix is then negated such that the most costly amino
acid substitutions score -1 and the least significant substitutions score 0.
If the user wishes to use an alternative method of substitution weighting via substitution_weighting = "custom",
they must provide their own substitution matrix which matches the same logic
(bounded -1-0, more similar amino acids have a higher score). Overall,
independent of the substitution scoring used, the maximum distance that can be
achieved is the length of the longest sequence (all amino acids substituted
incurrring the maximum possible cost if blosum-weighted or custom
substitution scores have been used, insertion of the extra amino acids
necessary to account for sequence length variation). All distances are
subsequently normalised to 0-1 by dividing by the length of the longer
sequence.
Selecting a representative sequence for BCRs
Pairwise distances are computed between all unique CDR3 amino acid sequences per aggregate using the above method. However, for BCRs, some of these unique sequences belong to the same clone due to the effects of somatic hypermutation. In this scenario, the CDR3 sequence with the lowest average pairwise distance to all other sequences in the clone is selected. All other sequences are discarded from the per aggregate distance matrix, such that it contains only one sequence per clone.
Downsampling depth
Downsampling is recommended where neighbourhood sizes differ substantially
to ensure distances are comparable across neighbourhoods.The downsampling
depth is the median number of unique representatives across all
neighbourhoods that pass min_n_contigs, excluding singleton-
representative neighbourhoods. This is computed separately per chain when
chain = "both", as VDJ and VJ clone counts can differ. The depth is
reported in the progress messages emitted to the console.
Summarising the distance matrix and obtaining a convergence score
The distance matrix is then summarised to obtain a singular convergence score representing the similarity of CDR3 sequences of different clones per aggregate. This can be a global convergence score, defined as the mean or median pairwise distance across all representative CDR3 sequences, or a local score. Local convergence scores are computed as the mean distance of each CDR3 sequence to its k nearest neighbours, averaged across all representatives in the aggregate. This metric is more sensitive to repertoire structure than a global average. For example, a neighbourhood containing clones that respond to two distinct epitopes would contain two clusters of highly similar CDR3s that are dissimilar from one another. Here, global convergence would be high due to the difference between these two antigens, whilst local convergence would reflect sequence similarity within these two groups. By default, k is set adaptively at n/4,where n is the number of representatives, with a minimum k of 3; a fixed k can also be supplied. Local convergence is not calculated when the number of representatives does not exceed k. All distance-based metrics are converted to convergence scores via 1 - distance.
Parallelisation
Each neighbourhood is processed independently and dispatched as a separate
task via bplapply. With
SerialParam (default), progress is reported at
every 10\
progress is suppressed; a completion summary is printed after collation.
Neighbourhood size and capture rate
nhood_size and contig_capture_rate are based on all cells
assigned to the neighbourhood in the binary nhoods matrix, regardless of
contig annotation status. This reflects the overall cellular composition of
the neighbourhood rather than immune cell abundance alone, and is the
intended denominator for capture rate.
See also
pwalign::stringDist()
for the underlying alignment engine.
BiocParallelParam for parallelisation options.
nhood_diversity, nhood_shm,
nhood_annotation, nhood_annotation_props
Examples
if (FALSE) { # \dontrun{
# --- Milo object, BCR, VDJ chain only, Levenshtein distance -------------
milo <- nhood_cdr3_convergence(
object = milo_object,
receptor_type = "BCR",
chain = "VDJ",
aa_col = "cdr3_aa_heavy",
clone_id_col = "clone_id",
seed = 42
)
results <- S4Vectors::metadata(milo)$nhood_cdr3_convergence
# --- Milo object, TCR, both chains, BLOSUM62, all metrics ----------------
milo <- nhood_cdr3_convergence(
object = milo_object,
receptor_type = "TCR",
chain = "both",
aa_col = c(VDJ = "cdr3_aa_TRB", VJ = "cdr3_aa_TRA"),
substitution_weighting = "blosum62",
summary_metric = c("converge_score", "converge_score_global", "converge_score_global_median"),
min_n_contigs = 5,
seed = 42,
BPPARAM = BiocParallel::MulticoreParam(workers = 4)
)
# --- SingleCellExperiment object, BCR, custom substitution matrix --------
custom_mat <- matrix(...) # 20x20 numeric, named by single-letter AA code
sce <- nhood_cdr3_convergence(
object = sce_object,
receptor_type = "BCR",
chain = "VDJ",
aa_col = "cdr3_aa_heavy",
clone_id_col = "clone_id",
nhood_col = "nhood_id",
substitution_weighting = "custom",
substitution_matrix = custom_mat,
seed = 42
)
} # }