Skip to content

Immune repertoire similarity

Description

[Experimental]

A family of functions to compare shared receptors between repertoires. These methods help you measure repertoire similarity, identify public receptors, compare biological groups, and check whether replicate samples have similar receptor composition.

Available functions

The following methods are available.

1) Shared-receptor counts (repsim_intersection). Count the unique receptors shared by each pair of repertoires. Use this method to find related samples, check replicate similarity, or identify receptors shared across many repertoires.

2) Jaccard similarity (repsim_jaccard). Compare the sets of unique receptors in each pair of repertoires. The score accounts for the total number of unique receptors in both repertoires. Use it to compare receptor overlap when repertoire sizes differ.

$J(A, B) = \frac{|A \cap B|}{|A \cup B|}$ 3) Chao-Jaccard similarity (repsim_chao_jaccard). Compare receptor overlap using receptor counts. The method estimates similarity after allowing for shared receptors that may be missing because one or both repertoires were sampled at limited depth. Use it when sampling depth may affect the observed overlap. 4) Morisita-Horn similarity (repsim_morisita_horn). Compare receptor abundance profiles. Abundant receptors have more influence on the score than rare receptors. Use it to determine whether the same expanded receptors dominate two repertoires. The method uses imd_proportion as the abundance measure.

C\_{MH} = {(\_i + \_j) N_i N_j} 5) Bray-Curtis similarity (repsim_bray). Measure how similar two receptor abundance profiles are. This is one minus Bray-Curtis dissimilarity: 0 means no overlap, and 1 means identical abundance composition. The method uses imd_proportion as the abundance measure. Set transform = "log1p" to reduce the influence of the most abundant receptors and give lower-abundance receptors more influence on the result. This can reveal broader repertoire similarity when a few expanded receptors would otherwise dominate the comparison. The transformation is applied inside the database query and does not modify idata. Immune-repertoire studies commonly calculate Bray-Curtis measures from clonotype proportions without an additional transformation. Therefore, transform = "none" remains the default. The "log1p" option is an exploratory sensitivity analysis based on a common ecological pre-treatment for reducing the effect of dominant taxa. After transformation, the values no longer sum to one, and the result no longer has the simple interpretation of the fraction of original repertoire abundance that overlaps. ## Usage

repsim_intersection(idata)

repsim_jaccard(idata)

repsim_chao_jaccard(idata)

repsim_morisita_horn(idata)

repsim_bray(idata, transform = c("none", "log1p"))
## Arguments
idata An immundata::ImmunData object.
transform A single character string selecting the transformation applied to imd_proportion before calculating similarity. Use "none" (the default) for the original proportions or "log1p" for log(1 + imd_proportion).

S_{BC,ij} = 1 - \frac{\sum_k |a_{ik} - a_{jk}|} {\sum_k (a_{ik} + a_{jk})} = \frac{2\sum_k \min(a_{ik}, a_{jk})}{\sum_k (a_{ik} + a_{jk})} where $a_{ik}$ is the original or transformed abundance of receptor $k$ in repertoire $i$.

## Value

1) Shared-receptor counts (repsim_intersection)

A symmetric numeric matrix where rows/columns are repertoire labels derived from schema_repertoire. Each cell is the count of shared unique receptors. The diagonal contains per-repertoire richness (total unique receptors).

2) Jaccard similarity (repsim_jaccard)

A symmetric numeric matrix where rows/columns are repertoire labels derived from schema_repertoire. Each cell is the Jaccard similarity in \[0, 1\]. The diagonal is 1.

3) Chao-Jaccard similarity (repsim_chao_jaccard)

A symmetric numeric matrix where rows/columns are repertoire labels derived from schema_repertoire. Each cell is the Chao-Jaccard similarity in \[0, 1\] (1 = maximum estimated overlap, 0 = no observed shared receptors). The diagonal is 1.

4) Morisita-Horn similarity (repsim_morisita_horn)

A symmetric numeric matrix whose rows/columns are repertoire labels derived from schema_repertoire, with values in \[0, 1\] (0 = no shared abundance, 1 = identical abundance composition). The diagonal is 1. Comparisons involving an empty repertoire are NA.

5) Bray-Curtis similarity (repsim_bray)

A symmetric numeric matrix where rows/columns are repertoire labels derived from schema_repertoire. Each cell is Bray-Curtis similarity in \[0, 1\] (0 = no overlap, 1 = identical). The diagonal is 1. ## Visualisation All five repsim\_\*() results can be passed directly to vis(). vis() displays the result as a dot plot, with repertoire labels on both axes. Each dot represents one pair of repertoires; its size and fill show the value for that pair.

1) Shared-receptor counts (repsim_intersection)

Larger and brighter dots mean that the repertoires share more unique receptors. The matrix diagonal contains repertoire richness; the plot omits self-comparisons.

2) Jaccard similarity (repsim_jaccard)

Larger and brighter dots mean greater similarity between the sets of unique receptors. Values range from 0 (no shared receptors) to 1 (identical receptor sets).

3) Chao-Jaccard similarity (repsim_chao_jaccard)

Larger and brighter dots mean greater estimated similarity after correction for receptors that may be missing because of limited sampling. Values range from 0 (no observed shared receptors) to 1 (maximum estimated similarity).

4) Morisita-Horn similarity (repsim_morisita_horn)

Larger and brighter dots mean greater similarity between receptor abundance profiles. Values range from 0 (no overlap) to 1 (identical abundance composition).

5) Bray-Curtis similarity (repsim_bray)

Larger and brighter dots mean greater similarity between receptor abundance profiles. Values range from 0 (no overlap) to 1 (identical abundance composition). All plots omit self-comparisons. Their color legends mark the smallest, midpoint, and largest plotted pairwise values. All repertoire-similarity plots accept:
  • size_max_mm to set the maximum dot diameter.
  • row_order and col_order to set the repertoire order on each axis.
## References Chao A, Chazdon RL, Colwell RK, Shen TJ (2005). A new statistical approach for assessing similarity of species composition with incidence and abundance data. Ecology Letters, 8(2), 148-159. [doi:10.1111/j.1461-0248.2004.00707.x](https://doi.org/10.1111/j.1461-0248.2004.00707.x) Horn HS (1966). Measurement of overlap in comparative ecological studies. The American Naturalist, 100(914), 419-424. [doi:10.1086/282436](https://doi.org/10.1086/282436) Clarke KR, Chapman MG, Somerfield PJ, Needham HR (2006). Dispersion-based weighting of species counts in assemblage analyses. Marine Ecology Progress Series, 320, 11-27. [doi:10.3354/meps320011](https://doi.org/10.3354/meps320011) The TCR assigns naive T cells to a preferred lymph node (2024). Science Advances. [doi:10.1126/sciadv.adl0796](https://doi.org/10.1126/sciadv.adl0796) \*On the feasibility of using TCR sequencing to follow a vaccination response
  • lessons learned* (2023). Frontiers in Immunology. [doi:10.3389/fimmu.2023.1210168](https://doi.org/10.3389/fimmu.2023.1210168)
## 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 receptors shared by each pair of repertoires.
m_pub <- repsim_intersection(immdata)

# Visualise the shared-receptor counts.
vis(m_pub)
![](repsim.markdown_strict_files/figure-markdown_strict/unnamed-chunk-1-1.png)
#
# Calculate Jaccard similarity.
m_jac <- repsim_jaccard(immdata)
vis(m_jac)
![](repsim.markdown_strict_files/figure-markdown_strict/unnamed-chunk-1-2.png)
#
# Calculate sampling-corrected Jaccard similarity.
m_chao_jac <- repsim_chao_jaccard(immdata)
vis(m_chao_jac)
![](repsim.markdown_strict_files/figure-markdown_strict/unnamed-chunk-1-3.png)
#
# Calculate Morisita-Horn similarity.
m_mh <- repsim_morisita_horn(immdata)
vis(m_mh)
![](repsim.markdown_strict_files/figure-markdown_strict/unnamed-chunk-1-4.png)
#
# Calculate Bray-Curtis similarity.
m_bray <- repsim_bray(immdata)
vis(m_bray)
![](repsim.markdown_strict_files/figure-markdown_strict/unnamed-chunk-1-5.png)
# Reduce the influence of highly expanded receptors.
m_bray_log <- repsim_bray(
  immdata,
  transform = "log1p"
)
vis(m_bray_log)
![](repsim.markdown_strict_files/figure-markdown_strict/unnamed-chunk-1-6.png)