## Overview

A B-cell receptor (BCR) consists of an immunoglobulin molecule and a СD79 molecule. BCR includes two identical heavy chains (generated by recombination of V, D, and J segments), and two identical light chains (generated by recombination of V and J segments). Rapid improvements in high-throughput sequencing provide an opportunity to study B-cell immunoglobulin receptors on the cell surface [1] via the process called BCR repertoire sequencing. This pipeline describes how to work with BCR repertoire data after preprocessing raw sequenced data with MiXCR (https://mixcr.readthedocs.io/en/master/) or any other programs.

The pipeline involves five steps:

The step includes loading the data and checking whether there is enough information for the pipeline to work.

2. Reconstructing clonal lineages.

At this step, all BCR sequences are divided into clonal lineages — sets of sequences that presumably share a common ancestor.

3. Building germline.

At this step, the algorithm generates a sequence that represents the ancestral sequence for each BCR in a clonal lineage.

4. Aligning sequences within clonal lineages.

This step involves preparation for phylogenetic and somatic hypermutation analysis.

5. Phylogenetic analysis.

This step provides phylogeny reconstruction and trunk length calculation (by running the PHYLIP package).

6. Somatic hypermutation analysis.

At this step, we compare clonotype and germline sequences to detect and count the number of mutations in clonotype sequence.

BCR data are loaded with repLoad functions, like any other data.

Check that BCR data has the following:

• positions of CDR3 region start and end,
• number of mutations per CDR3 region. In MiXCR output files, this information is located in the column ‘refPoints

Look at the sample BCR data implemented in Immunarch:

#load the package into the R environment:
library(immunarch)

data(bcrdata)

## Reconstructing clonal lineages

B-cell clonal lineage represents a set of B cells that presumably have a common origin (arising from the same VDJ rearrangement event) and a common ancestor. BCR sequences are clustered according to their similarity, and sets of sequences in one cluster are named clonal lineages. Clustering involves two steps.

An example of reconstructing clonal lineages using default Immunarch options:

#calulate distance matrix
distBCR <- seqDist(bcrdata$data %>% top(500)) #find clusters bcrdata$data <- seqCluster(bcrdata$data %>% top(500), distBCR, .perc_similarity = 0.9) ## Building germline Each clonal lineage has its own germline sequence that represents the ancestral sequence for each BCR in the clonal lineage. In other words, germline sequence is a sequence of B-cells immediately after VDJ recombination, before B-cell maturation and hypermutation process. Germline sequence is useful for assessing the degree of mutation and maturity of the repertoire. In Immunarch, repGermline() function generates germline for each sequence: #generate germline bcrdata$data %>%
repGermline(.threads = 1)

A germline is represented via sequences of V gene - N…N (CDR3 length) - J gene:

#germline example
bcrdata$data %>% top(1) %>% repGermline(.threads = 1) %>% .$full_clones %>% .Germline.sequence ## [1] "CAGCTGCAGCTGCAGGAGTCGGGCCCAGGACTGGTGAAGCCTTCGGAGACCCTGTCCCTCACCTGCACTGTCTCTGGTGGCTCCATCAGCAGTAGTAGTTACTACTGGGGCTGGATCCGCCAGCCCCCAGGGAAGGGGCTGGAGTGGATTGGGAGTATCTATTATAGTGGGAGCACCTACTACAACCCGTCCCTCAAGAGTCGAGTCACCATATCCGTAGACACGTCCAAGAACCAGTTCTCCCTGAAGCTGAGCTCTGTGACCGCCGCAGACACGGCTGTGTATTACNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNGGCCAAGGAACCCTGGTCACCGTCTCCTCAG" Оptions: .data - The data to be processed. Can be data.frame, data.table, or a list of these objects. .species - Specifies species from which reference V and J are taken. Available species: “HomoSapiens” (default), “MusMusculus”, “BosTaurus”, “CamelusDromedarius”, “CanisLupusFamiliaris”, “DanioRerio”, “MacacaMulatta”, “MusMusculusDomesticus”, “MusMusculusCastaneus”, “MusMusculusMolossinus”, “MusMusculusMusculus”, “MusSpretus”, “OncorhynchusMykiss”, “OrnithorhynchusAnatinus”, “OryctolagusCuniculus”, “RattusNorvegicus”, “SusScrofa”. .min_nuc_outside_cdr3 — this parameter sets how many nucleotides should have V or J chain outside of CDR3 to be considered good for further alignment. Reads with too short chains are filtered out .align_j_gene - if the germline sequence does not assemble correctly in the region of the J gene, then set this parameter to True. This will slow down the algorithm, but the assembly of the germline sequence will be more accurate. ## Aligning sequences within a clonal lineage After building clonal lineage and germline, we can start analyzing the degree of mutation and maturity of each clonal lineage. This allows us to find cells with a large number of mutated clones. The phylogenetic analysis will find mutations that influence the affinity of BCRs. Aligning the sequence is the first step toward sequence phylogenetic analysis. In the Immunarch package, the function repAlignLineage aligns sequences within clonal lineages. This function requires Clustal W app to be installed. The app could be downloaded here: http://www.clustal.org/download/current/, or installed via your system package manager (such as apt or dnf). sudo apt update sudo apt install clustalw • For Windows, download and run clustalw-x.x-win.msi repAlignLineage usage example: data(bcrdata) bcr_data <- bcrdatadata %>% top(500)
bcr_data %>%
seqCluster(seqDist(bcr_data), .fixed_threshold = 3) %>%
repAlignLineage(.min_lineage_sequences = 2)

The function has several parameters:

• .min_lineage_sequences — Filters clusters (clonal lineages) with the number of clonotypes lower than the threshold. Aligning clonal lineages with few sequences is of little use.
# take clusters that contain at least 1 sequence
bcr_data <- bcrdatadata align_dt <- bcr_data %>% seqCluster(seqDist(bcr_data, .col = 'CDR3.nt', .group_by_seqLength = TRUE), .perc_similarity = 0.6) %>% repGermline(.threads = 1) %>% repAlignLineage(.min_lineage_sequences = 6, .align_threads = 2, .nofail = TRUE) ## Warning in merge_reference_sequences(., reference, "V", species, sample_name): Alleles IGHV4-55*01 from sample full_clones not found in the reference and will be dropped! ## Probably, species argument is wrong (current value: HomoSapiens) or the data contains non-BCR genes. • .prepare_threads — the number of threads to prepare results table. A high number can cause memory overload! • .align_threads — the number of threads for lineage alignment. Requirements for the input table for repAlignLineage() • must have columns in the immunarch compatible format immunarch_data_format, • must contain the ‘Cluster’ column generated by seqCluster() function, • must contain the ‘Sequence.germline’ column generated by repGermline() function. Align sequences in a cluster can be visualized using standard functions: # A name of the first cluster align_dtfull_clonesCluster[[1]] ## [1] "IGHV1-69/IGHJ6_length_63_cluster_1" # Alignment of sequences from the first cluster image(align_dtfull_clonesAlignment[[1]], grid = TRUE) ## Phylogenetic analysis For BCR phylogenetic analysis, we use the maximum parsimony method. The maximum parsimony method reconstructs a phylogenetic tree for the lineage by minimizing the total number of mutation events [2]. This method also enables the reconstruction of intermediate sequences that may have existed between BCR and germline sequence. These simulated sequences contain mutations that were ancestral to the clonotype group selected with the maximum parsimony analysis. In Immunarch, function repClonalFamily builds a phylogeny of B-cells, generates a common ancestor, and calculates a trunk length. The mean trunk length represents the distance between the most recent common ancestor and germline sequence [3]. Mean trunk length serves as a measure of the maturity of a lineage. The trunk length of the lineage tree approximates the maturation state of the initiating B cell for each clone [4]. This function requires PHYLIP app to be installed. The app can be downloaded here: https://evolution.genetics.washington.edu/phylip/getme-new1.html, or could be installed with your system package manager (such as apt or dnf). sudo apt-get update -y sudo apt-get install -y phylip repClonalFamily usage example: bcr <- align_dt %>% repClonalFamily(.threads = 2, .nofail = TRUE) #plot visualization of the first tree vis(bcr[["full_clones"]][["TreeStats"]][[1]]) For each cluster tree is represented as table (The default number of clones for CommonAncestor, Germline, Presumable is 1): #example fot first tree bcr[["full_clones"]][["TreeStats"]][[1]] You can recolor leaves. For example, we recolor leaves where number of AA mutations is not 0: #take sequence where number of AA mutations is not 0 f <- bcr[["full_clones"]][["TreeStats"]][[1]] #rename these leaves f[fDistanceAA != 0, ]['Type'] = 'mutationAA'
#new tree
vis(f)

We have found 4 clusters:

bcr$full_clones$Cluster %>% unique()
## [1] "IGHV1-69/IGHJ6_length_63_cluster_1" "IGHV1-69/IGHJ6_length_75_cluster_2"
## [3] "IGHV3-23/IGHJ4_length_45_cluster_1" "IGHV3-23/IGHJ4_length_48_cluster_1"

We have found mismatches between a germline and an ancestor sequence. Dots represent nucleotides matches between the sequences, letters represent mismatches between the sequences:

# the example of common ancestor sequence
bcr$full_clones$Common.Ancestor[1]
## [1] "GGGAGGGATCATCCCTATCTTTGGTACAGCAAACTACGCACAGAAGTTCCAGGGCAGAGTCACGATTACCGCGGACGAATCCACGAGCACAGCCTACATGGAGCTGAGCAGCCTGAGATCTGAGGACACGGCCGTGTATTACTGTGCGAGAGW-----TYCGKRTAGYAGTGGMTRGTCKCTGG----ACTACTACTACGGTATGGACGTCTGGGGCCAAGGGACCACGGTCACCG"
# the example of mismatches between a germline and an ancestor sequence
bcr$full_clones$Germline.Output[1]
## [1] "GGGAGGGATCATCCCTATCTTTGGTACAGCAAACTACGCACAGAAGTTCCAGGGCAGAGTCACGATTACCGCGGACGAATCCACGAGCACAGCCTACATGGAGCTGAGCAGCCTGAGATCTGAGGACACGGCCGTGTATTACNNNNNNNNNNN-----NNNNNNNNNNNNNNNNNNNNNNNNNN----NNNNNNNNNNNNNNNNNNNNNNNNNNGGGCAAGGGACCACGGTCACCG"

We have calculated a trunk length for each cluster:

bcr$full_clones[ , c('Cluster', 'Trunk.Length') ] ## Cluster Trunk.Length ## 1 IGHV1-69/IGHJ6_length_63_cluster_1 1 ## 2 IGHV1-69/IGHJ6_length_75_cluster_2 1 ## 3 IGHV3-23/IGHJ4_length_45_cluster_1 2 ## 4 IGHV3-23/IGHJ4_length_48_cluster_1 1 Also trunk length specified in “TreeStats” table in column “DistanceNT”. #example fot first tree bcr[["full_clones"]][["TreeStats"]][[1]][1, ] ## Somatic hypermutation analysis The rate of somatic hypermutation allows us to estimate repertoire maturation and detect the type of mutation contributing to the emergence of high affinity antibodies. This makes somatic hypermutation analysis a valuable asset for B-cell repertoire analysis. In Immunarch, repSomaticHypermutation() function is designed for hypermutation analysis: bcr_data <- bcrdata$data

shm_data <- bcr %>% repSomaticHypermutation(.threads = 2, .nofail = TRUE)

The function repSomaticHypermutation() takes V and J germline sequences and V and J clonotype sequences as an input. Then the function aligns germline and clonotype sequences to detect and calculate occurring mutations.

Examples of germline and clonotype sequences:

# the example of germline V sequence
shm_data$full_clones$V.germline.nt[1]
## [1] "CAGGTGCAGCTGGTGCAGTCTGGGGCTGAGGTGAAGAAGCCTGGGTCCTCGGTGAAGGTCTCCTGCAAGGCTTCTGGAGGCACCTTCAGCAGCTATGCTATCAGCTGGGTGCGACAGGCCCCTGGACAAGGGCTTGAGTGGATGGGAGGGATCATCCCTATCTTTGGTACAGCAAACTACGCACAGAAGTTCCAGGGCAGAGTCACGATTACCGCGGACGAATCCACGAGCACAGCCTACATGGAGCTGAGCAGCCTGAGATCTGAGGACACGGCCGTGTATTAC"
# the example of germline J sequence
shm_data$full_clones$J.germline.nt[1]
## [1] "GGGCAAGGGACCACGGTCACCGTCTCCTCAG"
# the example of clonotype V sequence
cols <- c('FR1.nt', 'CDR1.nt', 'FR2.nt', 'CDR2.nt', 'FR3.nt')
apply(shm_data$full_clones[1,][ , cols ] , 1 , paste , collapse = "" )[[1]] ## [1] "caggtgcagctggtgcagtctggggctgaggtgaagaagcctgggtcctcggtgaaggtctcctgcaaggcttctggaggcaccttcagcagctatgctatcagctgggtgcgacaggcccctggacaagggcttgagtgGATGGGAGGGATCATCCCTATCTTTGGTACAGCAAACTACGCACAGAAGTTCCAGGGCAGAGTCACGATTACCGCGGACGAATCCACGAGCACAGCCTACATGGAGCTGAGCAGCCTGAGATCTGAGGACACGGCCGTGTATTAC" # the example of clonotype J sequence shm_data$full_clonesFR4.nt[1] ## [1] "GGCCAAGGGACCACGGTCACCGTCTCCTCAG" Example: aligning germline and clonotype V sequences: image(shm_datafull_clonesGermline.Alignment.V[[3]], grid = TRUE) Example: aligning germline and clonotype J sequences: image(shm_datafull_clones$Germline.Alignment.J[[3]], grid = TRUE) The number of mutations for each clonotype sequence: cols <- c('Clone.ID', 'Substitutions', 'Insertions', 'Deletions', 'Mutations') shm_data$full_clones[ , cols ]
##    Clone.ID Substitutions Insertions Deletions Mutations
## 1       851             1          0         0         1
## 2       921             1          0         0         1
## 3       214             3          0         0         3
## 4       159             2          0         0         2
## 5       222             1          0         0         1
## 6       270             1          0         0         1
## 7       250             1          0         0         1
## 8       330             1          0         0         1
## 9       966             2          0         0         2
## 10      672             2          0         0         2
## 11      401             1          0         0         1
## 12      386             1          0         0         1
## 13      736             1          0         0         1
## 14      295             1          0         0         1
## 15      202             1          0         0         1
## 16      716             2          0         0         2
## 17      827             1          0         0         1
## 18      591            16          0         0        16
## 19      433             1          0         0         1
## 20      163            22          0         0        22
## 21      914            24          0         0        24
## 22      524            12          0         0        12
## 23       24             6          0         0         6
## 24      342             6          0         0         6
## 25      940            19          0         0        19
## 26      238             6          0         0         6
## 27      428            15          0         0        15
## 28      484            13          0         0        13
## 29      932             1          0         0         1
## 30      517             4          0         0         4
## 31      522             4          0         0         4
## 32      933            12          0         0        12

Then you could easily estimate the mutation rate:

# estimate mutation rate
shm_data\$full_clones %>%
mutate(Mutation.Rate = Mutations / (nchar(Common.Ancestor) - CDR3.germline.length)) %>%
select(Clone.ID, Mutation.Rate)
##    Clone.ID Mutation.Rate
## 1       851   0.005780347
## 2       921   0.005780347
## 3       214   0.017341040
## 4       159   0.011560694
## 5       222   0.005780347
## 6       270   0.005780347
## 7       250   0.005780347
## 8       330   0.005780347
## 9       966   0.011560694
## 10      672   0.011560694
## 11      401   0.007751938
## 12      386   0.007751938
## 13      736   0.007751938
## 14      295   0.007751938
## 15      202   0.007751938
## 16      716   0.015503876
## 17      827   0.004926108
## 18      591   0.078817734
## 19      433   0.004926108
## 20      163   0.108374384
## 21      914   0.118226601
## 22      524   0.059113300
## 23       24   0.029268293
## 24      342   0.029268293
## 25      940   0.092682927
## 26      238   0.029268293
## 27      428   0.073170732
## 28      484   0.063414634
## 29      932   0.004878049
## 30      517   0.019512195
## 31      522   0.019512195
## 32      933   0.058536585