Deep generative models for T cell receptor protein sequences
Abstract
Probabilistic models of adaptive immune repertoire sequence distributions can be used to infer the expansion of immune cells in response to stimulus, differentiate genetic from environmental factors that determine repertoire sharing, and evaluate the suitability of various target immune sequences for stimulation via vaccination. Classically, these models are defined in terms of a probabilistic V(D)J recombination model which is sometimes combined with a selection model. In this paper we take a different approach, fitting variational autoencoder (VAE) models parameterized by deep neural networks to T cell receptor (TCR) repertoires. We show that simple VAE models can perform accurate cohort frequency estimation, learn the rules of VDJ recombination, and generalize well to unseen sequences. Further, we demonstrate that VAElike models can distinguish between real sequences and sequences generated according to a recombinationselection model, and that many characteristics of VAEgenerated sequences are similar to those of real sequences.
https://doi.org/10.7554/eLife.46935.001Introduction
T cell receptors (TCRs) are composed of an $\alpha $ and a $\beta $ protein chain, both originating from a random V(D)J recombination process, followed by selective steps that ensure functionality and limit autoreactivity. To generate diverse and functional TCRs, T cells combine a stochastic process for choosing from a pool of V, D and J genes with a process for selecting for expression and MHC recognition. The process first occurs for the $\beta $ chain, where first a D and a J gene are recombined using random trimming and joining with random nucleotides, then this DJ segment is recombined with a V gene via an analogous process. After the $\beta $ chain has been generated, a small cell expansion occurs followed by a similar $\alpha $ chain recombination, although without a D gene. For detailed reviews of V(D)J recombination see Bassing et al. (2002), and Schatz and Ji (2011). The naive T cell population consists of T cells that have undergone V(D)J recombination and MHC selection but not yet encountered antigen. In a system known as the clonal selection mechanism of immune memory, T cells that bind antigen increase in frequency, thus increasing the frequency of their corresponding TCR sequences. The resulting ensemble of protein sequences thus summarizes each individual’s previous immune exposures and largely determines their resistance to various infections. One can consider these protein sequences as a sample from a probability distribution, whether it is the distribution of receptors within an individual, or the distribution of receptors in a population. This article concerns fitting such probability distributions on TCR $\beta $ protein sequences (which will be called ‘TCR sequences’ for the rest of the paper).
Probability estimates from these models can be used to draw important biological conclusions. For example, observing sequences that are amplified in a repertoire indicates that they perform important functions like targeting yellow fewer or cytomegalovirus (Pogorelyy et al., 2018c; Pogorelyy et al., 2018d; Emerson et al., 2017). However, in order to properly define amplification, we must infer the frequency of such sequences appearing in the naive (i.e. postselection but preamplification) repertoire so that we do not mistake an inherently probable recombination scenario with functional selection. As another application, (Elhanati et al., 2018) used probability calculations to predict the frequency of shared TCR sequences between individuals, showing that biases of the V(D)J recombination process significantly explain the degree of sharing.
The appearance of a given TCR sequence in the blood of an individual means that it was generated by V(D)J recombination and subsequently passed thymic selection, which removes TCRs with improper binding to MHC as well as selfreactive TCRs. This series of two steps constitutes a sophisticated random process for generating protein sequences. Previous work (Elhanati et al., 2014; Pogorelyy et al., 2018c) approached the problem of inferring this process by calculating the probability of a sequence’s V(D)J recombination using a probabilistic graphical model, multiplying this probability by a thymic selection factor $Q$, and scaling accordingly. Although breaking the process into generation and selection steps parallels the biological process, we can instead fit a distribution to a mature TCR repertoire directly, and assess the advantages of either approach. Indeed, these considerations raise the question of how to model the distribution of TCR protein sequences from a given source in order to answer meaningful immunological questions.
In this paper we develop variants of Variational Autoencoder (VAE) models (Kingma et al., 2014b; Higgins et al., 2017) to fit the distribution of TCR protein sequences. Recent work on deep generative models of proteins inspired our approach (Sinai et al., 2017; Riesselman et al., 2018). We find that these models can predict cohort frequency with high accuracy, learn the rules of VDJ recombination, generalize to unseen sequences, and generate sequences with similar characteristics to observed TCR sequences.
Results
Methods overview
We briefly outline our methods in order to present results; further details can be found in the Materials and methods section. We model TCR sequences using simple variants of variational autoencoders (VAEs). Previous work using VAEs have found success when first, there is a vast amount of data available, and second, the data distribution is complicated, involving nonlinearities and interactions between covariates. There is indeed a vast amount of TCR repertoire data, and the TCR probability distributions are complex.
VAE models can be described as consisting of an $n$dimensional latent space, a prior ${p}_{\theta}(\mathbf{\mathbf{z}})$ on that latent space, and probabilistic maps parameterized by two neural networks: an encoder ${q}_{\varphi}(\mathbf{\mathbf{z}}\mathbf{\mathbf{x}})$ and a decoder ${p}_{\theta}(\widehat{\mathbf{\mathbf{x}}}\mathbf{\mathbf{z}})$ (Figure 1; Kingma et al., 2014b). For the models used in this paper the latent space is 20dimensional, and we use the conventional choice of a standard multivariate normal prior for ${p}_{\theta}(\mathbf{\mathbf{z}})$. The encoder ${q}_{\varphi}(\mathbf{\mathbf{z}}\mathbf{\mathbf{x}})$ is a multivariate normal distribution with mean and diagonal covariance determined by a neural network with input $\mathbf{\mathbf{x}}$ (see Materials and methods for how TCR protein sequences are transformed into appropriate input for a neural network). This choice of a normal distribution is primarily for mathematical convenience rather than being part of a specific modeling design; the normal ‘noise’ in the latent space gets processed by a neural network which introduces nonlinearities that ensure that the result is not normal. However, VAE variants do use other distributions in place of normal (Dilokthanakul et al., 2016; Davidson et al., 2018). The decoder ${p}_{\theta}(\widehat{\mathbf{\mathbf{x}}}\mathbf{\mathbf{z}})$ is a persite categorical distribution over amino acids and gaps parameterized by a neural network with input $\mathbf{\mathbf{z}}$.
Once the VAE is trained, one can sample new sequences by ‘decoding’ samples from ${p}_{\theta}(\mathbf{\mathbf{z}})$, that is, drawing from ${p}_{\theta}(\mathbf{\mathbf{z}})$, feeding those points through the decoder network, and then sampling from the resulting probabilities. In the case of TCRs, this final sampling step goes from categorical distributions on the TCR components (i.e. on the V gene, J gene, and the amino acids at the various positions) to an actual TCR sequence. One trains a VAE with a collection of observed sequences $\mathbf{\mathbf{x}}$ via the encoder ${q}_{\varphi}(\mathbf{\mathbf{z}}\mathbf{\mathbf{x}})$. VAE training has two goals, which are represented by two terms of the objective function: first, to be able to (probabilistically) encode and decode the sequences through the latent space with high fidelity, and second, to ensure that that the ${q}_{\varphi}(\mathbf{\mathbf{z}}\mathbf{\mathbf{x}})$ map is close to the prior ${p}_{\theta}(\mathbf{\mathbf{z}})$ on average across $\mathbf{\mathbf{x}}$. The second component of this objective encourages a structured mapping of input sequences to latent values, in hopes that the model learns meaningful sequence features rather than memorizing properties of the training data. The balance between these two components is important and is controlled by a parameter $\beta $ (Higgins et al., 2017). Once the VAE is trained (i.e. parameters $\varphi $ and $\theta $ are optimized according to the objective with respect to a particular dataset), we can calculate the probability of generating a given sequence $\mathbf{\mathbf{x}}$ via importance sampling.
We are interested in TCR $\beta $ protein sequences, which due to the process of VDJ recombination are uniquely identified by triples consisting of V gene, J gene, and CDR3 protein sequence (Woodsworth et al., 2013). We developed two VAE models for such protein sequences: a simple one, denoted basic and a more complex model, denoted count_match. The basic model does not have any information about the content of germline genes built into the model and was trained according to a simple loss function (Figure 1—figure supplement 1). The count_match model brings in information about the protein sequence of the germline genes and has a more complex loss function involving CDR3 length and the degree to which the protein sequences on the ends of the CDR3 match the corresponding germline gene sequences (Figure 1—figure supplement 2).
As a baseline for comparison, we combined OLGA, a sophisticated recombination model (Sethna et al., 2018), with a simplified version of the selection model used in Elhanati et al. (2014), together which we will denote OLGA.Q. Our selection component $Q$ is parameterized by triples consisting of V gene identity, J gene identity, and CDR3 length, resulting in a model with about 14,000 parameters. This is a simpler model than the general Elhanati et al. (2014) model, which allows for selection based on CDR3 amino acid composition. However, it is a richer model than any models used by the same group since the publication of Elhanati et al. (2014), such as the one used to find conditionassociated immune receptors in Pogorelyy et al. (2018b) and Pogorelyy et al. (2018c). Sethna et al. (2018) suggest probabilistically evaluating vaccine targets using OLGA directly and no selection model at all. In any case, a software implementation of the general Elhanati et al. (2014) model, for which training is highly involved, is not currently available.
VAE models predict cohort frequency
We wished to understand the ability of basic, count_match, and OLGA.Q to estimate the frequency with which a TCR appears in a given cohort, both when the TCR is contained in the training set (‘train’) and when it is not (‘test’). Here we define ‘cohort count’ for a collection of repertoires to be the number of times a given TCR amino acid sequence appeared in the output files from the ImmunoSEQ assay (Adaptive Biotechnologies, Seattle, WA, USA) for those repertoires (ignoring the template abundance column). Multiple nucleotide occurrences of a given TCR protein sequence contribute separately to this number. Define $c$ to be the cohort count vector for the data set of Emerson et al. (2017), indexed by the TCR protein sequences with values being these cohort counts.
To assess outofsample performance, we first partitioned $c$ into ${c}_{\mathrm{train}}$ and ${c}_{\mathrm{test}}$ with a 50/50 split irrespective of abundance. We emphasize that there is no overlap between these collections of TCRs. To obtain a training set, we drew 200,000 sequences from the multinomial distribution induced by ${c}_{\mathrm{train}}$. We then trained basic and OLGA.Q using these sequences. The trained models yield persequence probability distributions: ${P}_{\mathrm{VAE}}$ for the basic VAE and $P$_{OLGA.Q} for OLGA.Q. We evaluated each of these probabilities as well as the cohort frequency for 10,000 sequences drawn multinomially from ${c}_{\mathrm{test}}$ or ${c}_{\mathrm{train}}$.
We performed this procedure for the entire cohort, but also restricting the cohort for training to a randomlyselected, smaller number of subjects while still comparing to frequency estimates using the whole cohort.
We found that VAE models can predict cohort frequency for outofsample TCR sequences (Figure 2). As the number of samples increases, the scatter of points decreases and the difference between training and testing samples also decreases. With 666 samples, this results in an ${R}^{2}$ value for the bestfit line on the loglog scale of 0.258 for $P$_{OLGA.Q} and an ${R}^{2}$ value of 0.442 for ${P}_{\mathrm{VAE}}$ on the test set (Figure 2—figure supplement 1). When we increased the number of training sequences fivefold to 1 million, ${R}^{2}$s increased slightly to 0.268 for $P$_{OLGA.Q} and 0.474 for ${P}_{\mathrm{VAE}}$. Recall that these correlation measures include the full scale of frequencies, including very noisy frequency estimates on the lower end of the scale. Also, we make no efforts to account for sequencing error above the methods used in DeWitt et al. (2018). We note that higher correlations have been observed for an OLGA.Qtype model when calculating probability of CDR3 only, restricting to sequences found in an epitope database, and smoothing using a single amino acid mismatch (Pogorelyy et al., 2018a).
VAE models learn the rules of VDJ recombination
TCR $\beta $ chains are generated via VDJ recombination, a process in which germlineencoded genes are randomly chosen from a pool, trimmed a random amount, and then joined together with random nucleotide insertions in between. This recombination process leads to important structural characteristics in the generated sequences. Specifically, because the beginning of the CDR3 region is encoded by the V gene, and the end by the J gene, there is a strong correlation between the V and J gene identities and the CDR3 sequence.
The probabilistic models considered here differ in the extent to which they explicitly model this process. On one end of the spectrum, the OLGA.Q model is built on an explicit model of nucleotide VDJ recombination which emulates this process quite carefully, using our knowledge of the germline TCR nucleotide sequences and recombination mechanism (Murugan et al., 2012; Marcou et al., 2018; Sethna et al., 2018). The count_match model incorporates some of these aspects by making the germline V and J amino acid sequences for each input available to the decoder, and by scoring the degree to which the correct number of CDR3 amino acid positions of the reconstructed sequences match those of their corresponding V and J genes. The basic model predicts the germline genes and the CDR3 sequences as independent outputs of the VAE, and thus has no builtin prior information on the correlations between the germline genes and CDR3s.
We can understand the degree to which the models learn the VDJ recombination rules by evaluating them under the $P$_{OLGA.Q} recombinationselection model. If the VAE models respect the rules of VDJ recombination, they will generate sequences with a $P$_{OLGA.Q} comparable to that of real sequences, while if they do not respect these rules, they should get a low $P$_{OLGA.Q}. This is a stringent criterion: a single amino acid change towards the ${3}^{\prime}$ end of the CDR3 can cause $P$_{OLGA.Q} to drop precipitously. For example, OLGA gives (TRBV51, TRBJ26, CASSFSGSGANVLTF) a relatively high probability, while the same TCR with a single T switched to Q (TRBV51, TRBJ26, CASSFSGSGANVLQF) is assigned probability zero.
To test the models’ compliance with the rules of VDJ recombination, we used the data of De Neuter et al. (2019), which consists of TCR $\beta $ sequences from 33 subjects, as follows. We randomly split the data so that the repertoires of 22 subjects were used for training, and the remaining 11 subjects’ repertoires were used for testing (with one repertoire used for each subject). Each of the 22 training repertoires was randomly downsampled to 20,000 sequences to standardize the contribution of each repertoire to the training set; these samples were then pooled. 100,000 sequences from this pool were randomly selected to train the models, including the $Q$ factor of OLGA.Q. We then evaluated the distribution of $P$_{OLGA.Q} on 10,000 sequences from each of the heldout test repertoires as well as 10,000 sequences generated from each of the three models.
We found evidence that the VAE models do indeed learn the rules of VDJ recombination (Figure 3). Although there is slight left skew in the $P$_{OLGA.Q} distributions for VAEgenerated sequences compared to the $P$_{OLGA.Q} distributions of experimental repertoires, the behavior of the VAEgenerated distributions reasonably matches the behavior of the experimental distributions. In fact, the $P$_{OLGA.Q} distribution for OLGA.Qgenerated sequences seems to exhibit more left skew than the $P$_{OLGA.Q} distributions for VAEgenerated sequences, although the three distributions are for the most part comparable. Perhaps surprisingly, the count_match model that encodes germline amino acid information resulted in a very small improvement in terms of recombination probability compared to the basic model, which does not explicitly encode any dependence between a TCR’s germline gene usage and CDR3 sequence.
VAE models generalize to unseen sequences and learn more than a simple OLGA.Q
Next, we set out to determine whether the VAE models were simply memorizing and regurgitating training sequences. Such behavior is a persistent concern for deep generative models (Arora et al., 2017; Arora and Zhang, 2017). Although the close correspondence between test and train performance in the above frequency estimation suggests model generalization, it does not directly address this issue.
To evaluate outofsample probability estimation, we used the De Neuter et al. (2019) data as in the previous section to evaluate ${P}_{\mathrm{VAE}}$ under the basic model rather than $P$_{OLGA.Q}. If the VAE were regurgitating training sequences, it should consistently assign higher ${P}_{\mathrm{VAE}}$ to sequences it generates compared to heldout test sequences. Instead, we found that the ${P}_{\mathrm{VAE}}$ probabilities for VAEgenerated sequences closely follow probabilities for test sequences (Figure 4), for both basic and count_match. We also observed that the OLGA.Qgenerated sequences are consistently assigned a lower ${P}_{\mathrm{VAE}}$ on average than either test sequences or VAEgenerated sequences, indicating the VAE learns characteristics of real sequences not captured by the formulation of OLGA.Q used here.
VAE models generate sequences with similar characteristics to real sequences
We next sought to quantify the similarity of modelgenerated sequences to real sequences, for each of the three models in consideration. To accomplish this task, we used the sumrep package (Olson et al., 2019) (https://github.com/matsengrp/sumrep/), a collaborative effort of the AIRR (Breden et al., 2017; Rubelt et al., 2017) software working group. This package calculates many summary statistics on immune receptor sequence repertoires and provides functions for comparing these summaries. While these summaries are not of direct interest for this application, they comprise simple and relevant means of summarizing the abstract, highdimensional distribution of TCRs. Collectively, these summary comparisons allow for robust model validation without appealing to the models themselves for assessment. We found agreement between simulated and test repertoires in some respects, with the performance of the model depending on the summary statistic (Figure 5).
Above we showed how the VAE model learns the rules of VDJ recombination assessed by OLGA.Q likelihood distributions. Another assessment is to look directly at summary statistics like V/J gene usage, amino acid frequencies and CDR3 length. All models succeeded on J gene frequencies, with the VAE models performing worse in terms of V gene frequency, in particular the basic model. The models performed similarly in terms of CDR3 summaries, with OLGA.Q perhaps doing better in terms of CDR3 length, and the VAEs getting the correct distribution of nearestneighbor distances (Figure 5—figure supplement 1). The amino acid frequencies for the VAE models did not match those of the training data as closely as expected, although in some respects they appear better than OLGA.Q. Results were broadly consistent when analyzing a second data set (Figure 5—figure supplement 2).
The latent space embedding
We wished to understand the factors that determine the position of TCRs within the latent embedding. To uncover these determinants, we performed standard principal components analysis (PCA) on De Neuter test data embedded in the VAE latent space. This reduces the 20dimensional latent space embedding to the two dimensions which account for the largest variability in the data.
We found that this projection is structured according to V and J gene identity (Figure 6). In particular, the V gene determines one axis of the principal components projection, while the J gene determines another. In order to learn the next level of organization, we restricted the embedded TCR sequences to those using the most popular V and J genes — TCRBV3001 and TCRBJ0102 — and redid the projection. This additional projection showed that CDR3 length was an important determinant of embedding location (Figure 6—figure supplement 1).
Discussion
Probabilistic models of immune repertoires are powerful tools, with applications to finding diseaseresponsive TCRs (Pogorelyy et al., 2018c) and analyzing the forces dictating TCR sharing (Elhanati et al., 2018), among others. In this paper we applied deep learning to model TCR $\beta $ repertoires, and used the resultant models to gain meaningful insights. Specifically, we use a semiparametric method that makes a single weak assumption: that there exists some small number of latent parameters that can be used to generate to the observed distribution. We make no assumptions about the function mapping from these parameters to the highdimensional distribution space and learn it from the data. We have learned that this biologyagnostic approach can provide good results, even when compared to a previous approach that formalizes the considerable biological knowledge we have concerning the mechanism of VDJ recombination.
We find that these models have the following interesting features.
These models yield better insample and outofsample performance for cohort frequency estimation compared to an existing recombination and selection model.
They generalize well by learning features of real TCR repertoires, which allows them to differentiate between experimental repertoires and repertoires generated from the recombination and selection model.
They generate simulated repertoires that are similar to real TCR repertoires.
By leveraging powerful deep learning libraries, they can be expressed and implemented very simply with small amounts of specialized computer programming. The basic model, for example, is implemented in about 100 lines of Python code.
Furthermore, our efforts to inject biological knowledge into the deep learning framework did not significantly improve performance.
However, these models also have some important drawbacks. Most importantly, as is often the case for models parametrized by neural networks, these models are not directly interpretable. Although we have identified some structure in the latent space, further details may be difficult to ascertain. Besides the difficulty in interpreting the neural network weights, we did not engineer the model architectures with mechanism in mind. In addition, the models operate on amino acid sequences and thus cannot shed light on the VDJ recombination process, which operates at the nucleotide level. We also note that ${P}_{\mathrm{VAE}}$, which relies on importance sampling, is more expensive to compute than $P$_{OLGA.Q}.
The results presented here offer some interesting lessons concerning future development of deep probabilistic models for immune repertoires. Our model that had no a priori information about germline gene sequence performs very similarly, even when evaluated in terms of VDJ recombination likelihood, to one that deliberately attempts to recapitulate the amount of matching between germline gene and CDR3 amino acid sequences and includes germline CDR3 sequences in the untrained model. This may indicate that, given the volume of sequence data available, we should focus our efforts on the abstract problem of density estimation on the set of TCRs, rather than incorporating biological knowledge into our deep learning models.
Although we performed a preliminary analysis of the latent embedding, this exclusively involved sequence characteristics directly available to the model. In future work, we hope to further unravel this embedding by comparing repertoires in the latent space, and by comparing sequences labeled with external characteristics. We also plan to deliver a pretrained model that will enable biologists to evaluate the probability of seeing a naive B cell receptor (BCR) or TCR in a given population. Here we have restricted our attention to TCR $\beta $ sequences, however, our methods apply with no modification to TCR $\alpha $ chains. Contrasting the $\alpha $ and $\beta $ chains may yield interesting insights on the differences between the two generation processes. The most interesting insights will come from jointly modeling the two chains using largescale $\alpha \beta $ paired TCR sequencing (Howie et al., 2015), which is a more complex process.
Materials and methods
Data
Our goal was to model probability distributions on TCR $\beta $ chain protein sequences. By the process of VDJ recombination, these sequences are uniquely determined by V and J gene identities and CDR3 amino acid sequence. Thus, for the purpose of this paper, we exclusively used triples of V gene, J gene, CDR3 amino acid sequence to represent TCR protein sequences.
All data was downloaded from https://clients.adaptivebiotech.com/immuneaccess. We preprocessed the data to exclude sequences:
from an outofframe rearrangement
with a CDR3 that does not begin with the characteristic C or end with an F or YV
with a CDR3 longer than 30 amino acids
with an ambiguous V or J gene call.
We also excluded any TCRs with TCRBJ0205, which the internal Adaptive pipeline annotates incorrectly, and TCRBJ0207, to which the default OLGA model assigns artifactually low probabilities. Model design and parameter tuning, including the sizes of hidden layers and the dimension of the latent space, was performed using the data of DeWitt et al. (2018). On this data we endeavored to decrease model size without incurring loss on heldout data within this data set. We found that the model was relatively robust to parameter perturbations as long as the number of parameters was not too small. Model evaluation was performed using the data sets described in the Results section.
Encoding TCR sequences
Request a detailed protocolThe CDR3 sequences were padded with gaps in the middle so that they are a fixed length of 30 amino acid/gap characters. Thus there is an equal number of amino acids on either end of the gaps for even length CDR3s, with one extra on the left side for odd length CDR3s. This resulting sequence is ‘onehot encoded,’ meaning that each amino acid at each site is represented with 0/1 for absence/presence, with an additional dimension for ‘gap’ to make a 21dimensional space (Figure 7). V and J genes are similarly encoded in 67 and 13dimensional vectors, respectively, and all of these vectors are concatenated into a single large encoding vector. This vector is mapped to a latent embedding via a linear transformation that is learned during training (Biswas et al., 2018). In our case, there is one transformation for the V genes, one for the J genes, and one for amino acids. These transformations do not change dimension except for V gene identities, which are projected to a 30dimensional space (Figure 7).
Models
Request a detailed protocolHere we describe the basic and count_match models in detail. They are not exactly VAEs as originally defined in Kingma et al. (2014b) for two reasons. First, they are better categorized as $\beta $VAEs since they include a weight on the KullbackLeibler divergence term of the training objective (Higgins et al., 2017). Namely, the loss is
where $\mathcal{R}$ is the reconstruction loss for $\mathbf{\mathbf{x}}$ encoded as $\mathbf{\mathbf{z}}$ (details below).
Second, they have multiple outputs that are scored by separate reconstruction loss functions. Our reconstruction loss is a linear combination of these loss functions. For example, the simplest ‘basic’ model produces three outputs: one for the V gene, one for the J gene, and one for the CDR3 sequence. It has two denselyconnected layers for the encoder and two for the decoder (Figure 1—figure supplement 1). The V and J gene identities are scored using categorical crossentropy, while the CDR3 sequence is scored by the average categorical crossentropy across sites.
The count_match model includes TCR germline information in the untrained model in such a way that it can count the number of Vgermlinematching amino acids on the ${5}^{\prime}$ end of the CDR3 and the number of Jgermlinematching amino acids on the ${3}^{\prime}$ end of the CDR3 (Figure 1—figure supplement 2). Its loss function includes a component that scores these counts in terms of twodimensional squared loss. This model also contains an explicit loss component for CDR3 length, which is also evaluated via squared loss.
We combine the multiple losses within each model into a weighted linear combination which yields a single overall reconstruction loss function for optimization. Weights were determined by multivariate linear regression, minimizing the squared difference between the log likelihood and this reconstruction loss on a validation set (see next section for definition of the validation set used in training). This resulted in a marginal improvement in performance on the (DeWitt et al., 2018) data and fitting was not done again. Due to these modifications, our loss function cannot be interpreted in terms of the variational evidence lower bound (ELBO).
Training
Request a detailed protocolFor the purposes of fitting, the training data was split into truetraining and validation sets: the former was used for fitting, while the latter was used to assess error during training (which provided a stopping criterion). ‘Test’ data was completely held out from the training procedure.
Inspired by the work of Sønderby et al. (2016), we implemented a $\beta $ schedule during training such that training begins with $\beta =0$ and then linearly increases every training epoch until its final value. We extended this procedure by implementing a collection of pretraining phases that start with randomized weights and train for a fixed number of epochs using the $\beta $ schedule. The optimal weights, according to the validation loss, were used as the starting weights for a full optimization, which terminates when validation loss does not improve for a fixed number of epochs or until a maximum number of epochs is reached. Training was done using the Adam optimizer Kingma and Ba (2014a) implemented in Keras (Chollet, 2015).
Picking $\beta $
Request a detailed protocolAs described above, our complete loss function is a sum of a reconstruction loss plus $\beta $ times a KullbackLeibler (KL) divergence term ${D}_{\mathrm{KL}}({q}_{\varphi}(\mathbf{\mathbf{z}}\mathbf{\mathbf{x}})\parallel {p}_{\theta}(\mathbf{\mathbf{z}}))$ describing the divergence of the probabilistic encoder map $q$ to the prior $p$. This KL divergence term regularizes the optimization by encouraging a structured embedding of TCRs.
We tested the $\beta $ parameter in an evenly distributed range with seven values from 0.625 to 1 on the DeWitt et al. (2018) data set. We found that $\beta $ slightly impacted ${P}_{\mathrm{VAE}}$ when evaluated on test sequences, with larger values of $\beta $ being slightly preferred (Figure 8). On the other hand, $\beta $ strongly impacted the agreement of summary statistics of generated sequences with observed sequences in test repertoires (Figure 9). To balance these evaluative and generative objectives, we fixed $\beta $ to be 0.75. This choice was confirmed by running the same analysis on the data of Emerson et al. (2013) (Figure 9—figure supplement 1) and De Neuter et al. (2019) (Figure 9—figure supplement 2), both of which yielded similar results.
Importance sampling
Request a detailed protocol${P}_{\mathrm{VAE}}$ denotes the probability $p(\mathbf{\mathbf{x}})$ of the VAE generating $\mathbf{\mathbf{x}}$ when decoding a sample from the prior in the latent space. In principle we could calculate this as the expectation of $p(\mathbf{\mathbf{x}}\mathbf{\mathbf{z}})$ where $\mathbf{\mathbf{z}}$ is drawn from $p(\mathbf{\mathbf{z}})$, but this would be very inefficient.
Instead, we use importance sampling, calculating
Here
${q}_{\varphi}(\mathbf{\mathbf{z}}\mathbf{\mathbf{x}})$ is a sample from a multivariate normal with mean and variance determined by the encoder
${p}_{\theta}(\mathbf{\mathbf{x}}\mathbf{\mathbf{z}})$ is the probability of generating a given sequence from the decoded version of $\mathbf{\mathbf{z}}$: a product of categorical probabilities
${p}_{\theta}(\mathbf{\mathbf{z}})$ is the prior on the latent space
We found that 100 iterations of importance sampling yielded stable ${P}_{\mathrm{VAE}}$ estimates for our data, but used 500 iterations in the results presented here to ensure convergence.
OLGA and selection model
Request a detailed protocolWe used OLGA (Sethna et al., 2018) with its default model parameters to evaluate recombination probabilities. We layered a selection model on top of this recombination model via a multiplicative factor $Q$, parameterized in terms of triples consisting of V gene identity, J gene identity, and CDR3 length. The roughly 14,000 parameters of this selection model $Q$ were estimated from the same training data used to train the VAE in each case. As derived in the supplementary material of Elhanati et al. (2014), the maximum likelihood estimate of $Q$ for a given triple is the ratio of the empirical frequency of the triple in the data to the probability of observing the triple based on the recombination model. We truncated this ratio at 100 for numerical stability. We then used rejection sampling to sample from the corresponding $P$_{OLGA.Q} distribution. Code for estimating the OLGA.Q model parameters is included in our software package.
Implementation and pipeline
Request a detailed protocolWe implemented our models in a modular fashion with extensive comments so that others can understand, reproduce, and build upon our work. Code and pipelines are available at https://github.com/matsengrp/vampire/ (Matsen, 2019a; copy archived at https://github.com/elifesciencespublications/vampire), while scripts and Jupyter notebooks (Kluyver et al., 2016) specific to this paper are available at https://github.com/matsengrp/vampireanalysis1/ (Matsen, 2019b; copy archived at https://github.com/elifesciencespublications/vampireanalysis1). All models were implemented in Python 3.6 using Keras 2.2.4 (Chollet, 2015) and the Tensorflow 1.11.0 backend (Abadi et al., 2015). Our pipeline is written with SCons (https://scons.org) and nestly (https://pythonhosted.org/nestly/; McCoy et al., 2013). The sumrep package depends heavily on the Immcantation framework (https://immcantation.readthedocs.io/; Gupta et al., 2015).
The following tools were also especially helpful:
Biopython (Cock et al., 2009)
cowplot (Wilke, 2018)
ggplot2 (Wickham, 2016)
GNU parallel (Tange, 2018)
pandas (McKinney, 2010)
scikitlearn (Pedregosa et al., 2011).
Data availability
Raw data (TCR sequences) is available at immuneACCESS: https://clients.adaptivebiotech.com/pub/emerson2013jim, https://clients.adaptivebiotech.com/pub/emerson2017natgen, https://clients.adaptivebiotech.com/pub/seshadri2018journalofimmunology, https://clients.adaptivebiotech.com/pub/deneuter2018cmvserostatus. Processed data is available through Zenodo: https://zenodo.org/record/2619576#.XKElTrfYphE. Code and instructions for reproducing figures is available at: https://github.com/matsengrp/vampireanalysis1 (copy archived at https://github.com/elifesciencespublications/vampireanalysis1). Code to process data and run VAE is available at: https://github.com/matsengrp/vampire/ (copy archived at https://github.com/elifesciencespublications/vampire).

immuneACCESSEstimating the ratio of CD4+ to CD8+ T cells using highthroughput sequence data.https://doi.org/10.21417/B7H01M

immuneACCESSImmunosequencing identifies signatures of cytomegalovirus exposure history and HLAmediated effects on the Tcell repertoire.https://doi.org/10.21417/B7001Z

immuneACCESSA diverse lipid antigenspecific T cell receptor repertoire is clonally expanded during active tuberculosis.https://doi.org/10.21417/B7QG66

immuneACCESSMemory CD4+ T cell receptor repertoire data mining as a tool for identifying cytomegalovirus serostatus.https://doi.org/10.21417/B7R91W
References

TensorFlow: Largescale machine learning on heterogeneous systemsTensorFlow: Largescale machine learning on heterogeneous systems, https://www.tensorflow.org/.

Reproducibility and reuse of adaptive immune receptor repertoire dataFrontiers in Immunology 8:1418.https://doi.org/10.3389/fimmu.2017.01418

A diverse lipid AntigenSpecific TCR repertoire is clonally expanded during active tuberculosisThe Journal of Immunology 201:888–896.https://doi.org/10.4049/jimmunol.1800186

Predicting the spectrum of TCR repertoire sharing with a datadriven model of recombinationImmunological Reviews 284:167–179.https://doi.org/10.1111/imr.12665

Estimating the ratio of CD4+ to CD8+ T cells using highthroughput sequence dataJournal of Immunological Methods 391:14–21.https://doi.org/10.1016/j.jim.2013.02.002

Conferenceβvae: learning basic visual concepts with a constrained variational frameworkInternational Conference on Learning Representations.

Highthroughput pairing of T cell receptor α and β sequencesScience Translational Medicine 7:301ra131.https://doi.org/10.1126/scitranslmed.aac5624

BookJupyter Notebooks – A Publishing Format for Reproducible Computational WorkflowsIn: Loizides F, Schmidt B, editors. Positioning and Power in Academic Publishing: Players, Agents an Agendas. IOS Press. pp. 87–90.

Highthroughput immune repertoire analysis with IGoRNature Communications 9:561.https://doi.org/10.1038/s4146701802832w

SoftwareData structures for statistical computing in pythonProceedings of the 9th Python in Science.

Scikitlearn: machine learning in PythonJournal of Machine Learning Research 12:2825–2830.

Recombination centres and the orchestration of V(D)J recombinationNature Reviews Immunology 11:251–263.https://doi.org/10.1038/nri2941

BookGgplot2: Elegant Graphics for Data AnalysisNew York: SpringerVerlag .https://doi.org/10.1007/9780387981413

cowplot: Streamlined Plot Theme and Plot Annotations for ’ggplot2’cowplot: Streamlined Plot Theme and Plot Annotations for ’ggplot2’, r package version 0.9.3, https://CRAN.Rproject.org/package=cowplot.
Article and author information
Author details
Funding
National Institutes of Health (R01 GM113246)
 Kristian Davidsen
 Branden J Olson
 William S DeWitt III
 Frederick A Matsen IV
National Institutes of Health (U19 AI117891)
 Frederick A Matsen IV
National Institutes of Health (R01 AI120961)
 Elias Harkins
 Frederick A Matsen IV
Howard Hughes Medical Institute (Faculty Scholar grant)
 Kristian Davidsen
 Branden J Olson
 William S DeWitt III
 Frederick A Matsen IV
National Institutes of Health (R01 AI146028)
 Branden J Olson
 Elias Harkins
 Frederick A Matsen IV
National Institutes of Health (5T32HG00003523)
 William S DeWitt III
The funders had no role in study design, data collection and interpretation, or the decision to submit the work for publication.
Acknowledgements
We would like to thank Sam Sinai and Cheng Zhang for helpful discussions, Thierry Mora, Aleks Walczak, and Zachary Sethna for assistance with OLGA and the Q model, the AIRR software working group for their contributions to sumrep, Fred Hutch scientific computing, especially Michael Gutteridge and Dirk Petersen, and Adaptive Biotechnologies for hosting and sharing TCR data.
Version history
 Received: March 17, 2019
 Accepted: August 21, 2019
 Version of Record published: September 5, 2019 (version 1)
Copyright
© 2019, Davidsen et al.
This article is distributed under the terms of the Creative Commons Attribution License, which permits unrestricted use and redistribution provided that the original author and source are credited.
Metrics

 7,034
 views

 812
 downloads

 60
 citations
Views, downloads and citations are aggregated across all versions of this paper published by eLife.
Download links
Downloads (link to download the article as PDF)
Open citations (links to open the citations from this article in various online reference manager services)
Cite this article (links to download the citations from this article in formats compatible with various reference manager tools)
Further reading

 Computational and Systems Biology
 Genetics and Genomics
Runsofhomozygosity (ROH) segments, contiguous homozygous regions in a genome were traditionally linked to families and inbred populations. However, a growing literature suggests that ROHs are ubiquitous in outbred populations. Still, most existing genetic studies of ROH in populations are limited to aggregated ROH content across the genome, which does not offer the resolution for mapping causal loci. This limitation is mainly due to a lack of methods for the efficient identification of shared ROH diplotypes. Here, we present a new method, ROHDICE (runsofhomozygous diplotype cluster enumerator), to find large ROH diplotype clusters, sufficiently long ROHs shared by a sufficient number of individuals, in large cohorts. ROHDICE identified over 1 million ROH diplotypes that span over 100 single nucleotide polymorphisms (SNPs) and are shared by more than 100 UK Biobank participants. Moreover, we found significant associations of clustered ROH diplotypes across the genome with various selfreported diseases, with the strongest associations found between the extended human leukocyte antigen (HLA) region and autoimmune disorders. We found an association between a diplotype covering the homeostatic iron regulator (HFE) gene and hemochromatosis, even though the wellknown causal SNP was not directly genotyped or imputed. Using a genomewide scan, we identified a putative association between carriers of an ROH diplotype in chromosome 4 and an increase in mortality among COVID19 patients (pvalue = 1.82 × 10^{−11}). In summary, our ROHDICE method, by calling out large ROH diplotypes in a large outbred population, enables further population genetics into the demographic history of large populations. More importantly, our method enables a new genomewide mapping approach for finding diseasecausing loci with multimarker recessive effects at a population scale.

 Biochemistry and Chemical Biology
 Computational and Systems Biology
Previously, Tuller et al. found that the first 30–50 codons of the genes of yeast and other eukaryotes are slightly enriched for rare codons. They argued that this slowed translation, and was adaptive because it queued ribosomes to prevent collisions. Today, the translational speeds of different codons are known, and indeed rare codons are translated slowly. We reexamined this 5’ slow translation ‘ramp.’ We confirm that 5’ regions are slightly enriched for rare codons; in addition, they are depleted for downstream Start codons (which are fast), with both effects contributing to slow 5’ translation. However, we also find that the 5’ (and 3’) ends of yeast genes are poorly conserved in evolution, suggesting that they are unstable and turnover relatively rapidly. When a new 5’ end forms de novo, it is likely to include codons that would otherwise be rare. Because evolution has had a relatively short time to select against these codons, 5’ ends are typically slightly enriched for rare, slow codons. Opposite to the expectation of Tuller et al., we show by direct experiment that genes with slowly translated codons at the 5’ end are expressed relatively poorly, and that substituting faster synonymous codons improves expression. Direct experiment shows that slow codons do not prevent downstream ribosome collisions. Further informatic studies suggest that for natural genes, slow 5’ ends are correlated with poor gene expression, opposite to the expectation of Tuller et al. Thus, we conclude that slow 5’ translation is a ‘spandrel’a nonadaptive consequence of something else, in this case, the turnover of 5’ ends in evolution, and it does not improve translation.