Introduction

An important question in immunology concerns the ability of the adaptive immune system to perform self-nonself discrimination (SNSD). The problem can be formulated as follows. Given a set with different self-peptides S, is it possible to define a population of T cells with different receptors that is capable of reacting specifically against any foreign peptide f that was not originally in S? A positive answer seems doubtful because self-peptides can be extremely diverse, their number very large and because self and nonself peptides can be almost indistinguishable at the level of the detection mechanisms involved. For instance, a self-peptide in one individual can be nonself in another. Ultimately, the classification between self and nonself depends only on whether peptides have or have not been available during repertoire education.

So far mathematical or computational models suggest that perfect and efficient SNSD would require an exponentially large T cell repertoire1,2,3,4 and an enormous time for peptides to find their cognate receptors. In particular, it is unclear how T cells should move to guarantee that potentially dangerous peptides have been screened as, for short time scales, their movements in lymph nodes are simply Brownian5.

Almost all immunology textbooks refer to the ability of the adaptive immune system to discriminate self from nonself6,7,8 and yet the topic is highly controversial9,10,11,12. Alternative scenarios have been proposed arguing that the adaptive immune system evolved instead to detect evolutionary conserved dangerous patterns13 or to maintain homeostasis14. These scenarios agree with some circumstantial evidence, but lack predictive quantitative power.

Notwithstanding, a remarkable set of experimental facts still require convincing explanations. One concerns positive selection. Which evolutionary force placed positive selection at the beginning of the T cell education process is still unknown. The current view is that by eliminating thymocytes that bind weakly to MHC ligands, MHC restriction is established7,15,16,17. This, however, does not agree with the extensive alloreactivity observed in practice18. Furthermore, it is difficult to understand why this process could not take place in the periphery all along an individual's lifetime.

The ability to generate enormous diversity of T and B cells receptors is another striking phenomenon, which is regarded as a cornerstone in the evolution of adaptive immune system19. It is commonly argued that this mechanism guarantees that any foreign peptide is recognized. This explanation seems incomplete because this enormous diversity generation has implications on the recognition of both, self and nonself peptides. Increasing receptors diversity allows discriminating finer details. However, this should work both ways and it is unclear how a high reactivity towards nonself is consistent with perfect tolerance towards self.

T cell costimulation and anergy are another two enigmatic phenomena awaiting explanation. Costimulation provides what is known as the second signal required for T cell activation. The first signal corresponds to the recognition of peptide-MHC molecules. Costimulation is provided by a nonspecific second signal and, most puzzling, in its absence T cells become anergic. Under physiological conditions, the second signal never precedes the first7,16,20 and a third signal is also not required. This suggests that the second signal does not provide additive information that could be strengthened by adding further stimuli.

Anergy seems an even harder phenomenon to explain. Anergy has been understood as a tolerance mechanism6,7,8, because T cells are sent to a resting state even after recognizing potentially immunogenic peptides as signal one. However, if this explanation is correct, why should not T cells continue searching for other APCs that properly present foreign peptides?

This work demonstrates that if lymphocytes use a set of simple interaction rules then they can fulfill an important immunological function, namely that of detecting any ligand not presented during thymic education. Our model requires that T cells should be selected to establish the shortest conjugation lifetimes corresponding to a maximally frustrated dynamics in the thymus21. As a result, APCs displaying nonself peptides in lymph nodes, will inevitably engage in a less frustrated dynamics. This change can be detected by an increase in conjugation lifetimes involving the pathogenic APC. For populations displaying an arbitrary large diversity of self-peptides, frustration maximization will never be perfect. A small number of T cells can still perform large conjugation times with some self-presenting APCs. However, we will show that costimulation and anergy arise as natural mechanisms to render the accuracy of the discrimination task perfect. This is because in this model a macroscopic set of T cells is capable of performing less frustrated interactions with pathogenic APCs, while only a residual number of T cells performs long contacts with self APCs. Costimulation complemented with anergy allows APCs to “count” the number of long contacts performed with different T cells and in this way evaluate its pathogenicity.

Results

Model

The adaptive immune system is modeled at two different phases, T cell repertoire education in the thymus and T cell activation in the periphery. In both cases two cell types are considered, T cells and APCs. It is assumed that their number is kept constant and that they engage in a decision dynamics as described in Figure 1 (see Methods). Here we consider the simplest version of the model, in which T cells are divided in two subtypes depending on the ligand they present on their surface. Accordingly, APCs also belong to two different subtypes depending on how they interact with T cell ligands. Since our arguments are very general, no specific cell names will be associated to each T cell or APC subtype.

Figure 1
figure 1

a) Representation of the model with N cells of two cell types, APCs and T cells.All cells present ligands to the other cell type. APCs present a diverse set of peptide-MHC complexes, . T cell ligands l1 and l2 are less diverse and determine the T cell subtype i. All APCs of the same subtype share the same receptor. Receptors encode all information on how they interact with the other cell type ligands. This information is represented by an interaction list (IList). APCs can only have two types of receptors: either they interact with higher affinity with ligand l1 or the inverse. T cell receptors ILists are more complex. In principle all T cells have different receptors and their associated ILists rank APC ligands pi in completely different orders. b) When two cells interact they perform a decision that determines their next state. A new conjugate is formed whenever both cells involved receive a stronger stimulus from the new cell. In the case represented here, receptors of the T cell with ligand l2 interact with higher affinity with ligand pN than p9. Consequently, the T cell tends to depolarize its previous synapse towards the new APC, in accordance with the observations in28. In this case the APC receptors also promote the interaction with ligand l2 and consequently a new conjugation is formed because both cells promote the new conjugation. In that case, cells involved in previous conjugations are freed. In this framework, it is assumed that cells always promote interactions with cells displaying ligands ranked higher in their IList. In particular, non-conjugated cells always favor forming a conjugate than to remain alone.

APCs display a rich diversity of ligands. The set of ligands presented by APCs during education defines the self-set S. In lymph nodes APCs present either self or foreign peptides. The affinity of interactions between T cells receptors and APC ligands can be extremely complex and varied. A simple way of grouping this information is by defining interaction lists (ILists). ILists rank the ligands a T cell can interact with in order of decreasing affinity (Figure 1). T cell receptors have a decisive impact on the system dynamics. All cells perform a decision dynamics, in which they polarize towards cells delivering the stronger stimulus and in this case release previous conjugations. It is assumed that cells can only maintain a stable conjugation at a time. If T cells have higher affinity towards APCs that rank higher T cells of the other subtype, then conjugations are never stable and are called frustrated. In that case, there is always a cell in a conjugate that can find another cell delivering a stronger stimulus. In that case effector functions, like induced apoptosis or cellular activation, are never triggered because synapses do not have time to mature. Otherwise, stable conjugations could induce apoptosis in the thymus and T cell activation in lymph nodes. Similarly, death by neglect is triggered whenever T cells cannot form conjugations for a long time.

In this work we study how repertoire education and T cell activation should be performed to achieve perfect self nonself discrimination. Two main approaches are used. Simulations with cellular automata provide precise measurements of detection rates (see SM1). In particular, we show that starting with a population of APCs with arbitrary ligands and T cells with randomly drawn ILists, it is possible to select a T cell repertoire that promptly detects any foreign peptide and simultaneously maintains perfect tolerance towards self. Cellular automata are the best method to prove these results because they capture every stochastic effect.

Using rate equations for the mean conjugate frequencies nij on a simplified version of the model (see Methods) we gain an additional complementary understanding. Here they were used especially to understand the effect of positive selection. These equations are of the form:

where {creation} and {destruction} are respectively the contributions to the creation and destruction of conjugates involving APCs with ligand i and T cell receptor j. Destruction terms are of the form , where is the inverse of the conjugation lifetime (the rate at which the AiTj conjugate is destroyed)21. This analytical approach can also incorporate positive and negative selection mechanisms.

Negative selection maximizes frustration

Starting with an initially random population of APC ligands and T cell receptors successive replacement of T cells performing the longest conjugations with T cells with random receptors reduces conjugation times and, equivalently, increases frustration. This can be appreciated in Figure 2, where the frequency of conjugations lasting for a time τ is plotted for all APCs before (Fig. 2a) and after (Fig. 2b) education. This result demonstrates that conjugation lifetimes can be reduced through a non-directed selection process operating on T cell receptors.

Figure 2
figure 2

Frequency of conjugations lasting longer than τ iteration steps for each APC in the population for the dynamics of a) a non-educated population; b) an educated population; c) an educated population and in which an APC presents a foreign peptide (line in red).

From a) and b) it is clear that negative selection reduces considerably conjugation lifetimes, demonstrating the dynamical plasticity of the system. By comparing b) and c) it can be concluded that the APC displaying a foreign peptide engages in a less frustrated dynamics, with a larger number of stable conjugations and it disturbs only slightly the dynamics of the other cells in the population. These results were obtained with populations with 60 cells of each type, after W = 5000 iterations. All APCs displayed different ligands.

Prompt self-nonself discrimination arises on maximally frustrated populations with diverse and flexible T cell receptors

Negative selection successively eliminates all T cells performing the longest conjugations and replaces them by T cells with random receptors. Only T cells frustrating interactions survive this continuous selection process. Their T cells receptors have higher affinity towards APCs that have low affinity towards their ligands. In our model (Figure 1), ILists from subtype I T cells should avoid having on top positions ligands displayed by subtype I APCs, as it is indeed confirmed in Figure 3. However, peptides not presented during the education process cannot shape T cells ILists. All Nf foreign peptides have the same 1/Np uniform probability of appearing in any position and in top positions in particular. As a result during T cell activation APCs presenting foreign peptides have a higher probability of establishing stable contacts with a finite fraction (a macroscopic number) of T cells. Consequently, the response can be quick and independent of the number of different specificities in the population.

Figure 3
figure 3

Frequency of ligands displayed by subtype i APCs on a subtype i T cell IList (i = I or II), as a function of their position in the IList.

These results were obtained for a population of 60 T cells with (Black) a non-educated T cell population and (Blue) an educated T cell population. Negative selection eliminates T cells whose ILists have on top positions ligands displayed by APCs of the same subtype, as the corresponding conjugations are the most stable. Note that the dispersion of results is considerably smaller in the top 6 positions, where selection pressure is bigger.

In Figure 2c we show the number of conjugations lasting for a time τ in a population where an APC presents a nonself peptide. The APC presenting the foreign peptide establishes a larger number of stable conjugations than the other APCs. The dynamics of self APCs remains almost unperturbed, implying that nonself discrimination does not induce autoimmunity.

Maximal frustration alone does not guarantee perfect discrimination

In order to discriminate self from nonself, conjugation lifetimes involving all self APCs should be markedly different from those involving pathogenic APCs. While the previous argument supports that pathogenic APCs should engage in a less frustrated dynamics, it does not guarantee that there is not a single self APC performing long contacts following the education process. This is indeed difficult to prevent, as it would be necessary to guarantee that all T cells interacted with all top ranked ligands. This is impossible to guarantee on all cells simultaneously. In practice, on populations with 60 APCs displaying different ligands, about 40% of pathogens evade detection (Figure 4a).

Figure 4
figure 4

Effect of anergy on the probability of detection of foreign ligands.

Detection takes place whenever R>1. If only one population of T cells is used, the probability of detection is poor (black line in a) and first point in b)). If several educated T cell populations are available, T cells can be replaced by equivalent T cells from another educated population in the repertoire. This is done using the mechanism of anergy. Only 1 in 10000 foreign peptides escaped detection when the T cell repertoire included 20 populations (i.e., 60x12 = 720 different T cells). The remaining results in this work report experiments using Npop = 40.

Maximal frustration of the full T cell repertoire

The thymus works throughout a considerable lifetime. During this time new naïve T cells are put into circulation and come eventually to a lymph node where they meet T cells they have never seen before. It is important to know whether T cells educated at different times still form a frustrated set with the same dynamical properties. This was indeed confirmed by building a broaden repertoire which adds independently educated T cell populations. The dynamics of a population with a random sample of T cells selected from the broaden repertoire, exhibits the similar conjugation lifetime distributions as it was observed for each educated T cell population separately.

Costimulation and anergy lead to perfect self-nonself discrimination

Since many different T cells in the repertoire can be used indistinctively without changing the collective dynamics (see SM2), less frustrated T cells could be replaced to improve the accuracy of the discrimination task. The distinctive nature of pathogenic and self APCs is that the former can establish long lasting conjugations with a finite fraction of the repertoire, while the latter can only establish stable conjugations with a residual number of T cells. Then, APCs presenting self-peptides have a low probability of repeatedly forming stable conjugations, while the same does not happen for pathogenic APCs. However, it could happen that an APC establishes consecutive stable interactions with a same T cell. Anergy naturally emerges as a mechanism to decrease the probability of these events as if APCs put T cells in anergy after terminating a contact (see Methods and discussion in SM3), stable contacts with the same T cell can be prevented. Costimulation then appears as the signal that is triggered whenever an APC contacts T cells at a distinctive high rate. In that case the APC activates any T cell forming stable conjugations. All activated T cells have a high affinity towards the foreign peptide, although they may differ significantly on the affinities towards the different self-peptides.

In our simulations costimulation is introduced by assuming that if APCs establish a higher rate of long contacts than observed during education (i.e. R>1, see Methods), then pathogens are detected. This agrees with the view that the rate of stable contacts determines T cell activation22,23,24,25,26.

The impact of anergy and costimulation was particularly noticeable in our numerical simulations. If no anergy is used, then pathogen detection rates are poor (~40%). However, if each T cell is replaced within other Npop alternative T cells, then a dramatic increase in detection rates is observed (Figure 4). Perfect detection is achieved with a reasonable repertoire size (Npop~20). The mechanism is robust since good detection rates are achieved even with non-exhaustively educated repertoires (see SM4).

Limited connectivity allows scalability

Negative selection selects T cells ILists with ordered top positions. However, perfect orderings cannot be expected because, in principle, APC ligands are arbitrary and consequently T cell receptors must be randomly generated to account for all possibilities.

The probability that the top n self-peptides ranked in a T cell IList are strictly of a given subtype, decreases as (1/2)n. If n is equal to 10, this happens with a small probability, ~0.1%. Anergy allows loosening this stringent condition by not requiring that exactly all these n peptides are of a given subtype. Also lengthy education processes make available rarer ILists. In any case, only a small fraction of top ranked self-peptides get ordered in T cells' ILists in educated populations. Since the difficulty in ordering ILists is intrinsically probabilistic, the fraction of properly ordered self-ligands represents an increasingly smaller fraction of the total number of self-peptides. As a result non detection rates increase with the self-peptide's diversity.

In order to maximize frustration in large populations, T cells should interact with a smaller number of self-peptides. This can be achieved if T cell interactions have limited connectivity and interact only with a subset of ligands. This can be done either by imposing spatial or affinity interaction constraints. Given the highly structured nature of lymphoid organs, it is possible that both coexist. Our simulations indeed show that constraining cells connectivity allows maximizing frustration and even reduce conjugation lifetimes in larger populations (Figure SM5). Moreover, the mechanism that led to perfect SNSD still applies. However, since each APC interacts effectively with a smaller number of cells, responses could become slower as diversity increases. This difficulty can nevertheless be circumvented if cells connectivity correlates with spatial organization, so that cells contact only with those cells they can interact with. This is plausible, given the highly structured organization of lymphoid organs. We modeled limited connectivity using this strategy. In that case, prompt perfect SNSD was achieved (see SM6).

Positive selection is necessary to maximize frustration

The simplified version of our model in Figure 5 highlights the importance of positive selection on repertoire education. Consider that all subtype I APCs present the same ligand, while subtype II APCs can present two different ligands. To illustrate the impact of limited connectivity in repertoire education, connectivity is limited to 2 self-ligands per T cell. Note that the number of nonself ligands on each T cell IList can still be arbitrarily large; however, this will not be the focus of the following discussion.

Figure 5
figure 5

Representation of a simple model that highlights the importance of positive selection in systems with limited connectivity.

APCs of the first subtype present peptide p1, while APCs of subtype II present either peptide p2 or p3. It is assumed that connectivity is limited to 2 self-peptides per cell. As a result there are only 12 kinds of T cells, depending on their subtype and on how they order self-ligands. For instance, the T cell denoted TI(21) belongs to the first T cell subtype - because it presents ligand l1 to APCs – and it can interact with self-ligands p2 and p1, having higher affinity towards ligand p2. T cells with ILists represented in red establish stable pairs and consequently are eliminated by negative selection. T cells represented in black form a frustrated set: and conjugation involving these cells can always be destroyed either by an APC or a frustrated T cell. Similarly T cells with ILists represented in blue do not establish long contacts with APCs 2 or 3, because any T cell of subtype II destabilizes the pair. However, the rate of formation of new conjugates involving these cells is more reduced since ligands are not on the top of any APC's IList they can interact with (APCs 2 and 3). As a result they only establish interactions with non-conjugated APCs 2 and 3.

Twelve different T cells can be defined (Figure 5). We denote them using indices for their subtype and, in parenthesis, indices of the ligands in their ILists. Three main types of cells can be defined. Cells with ILists represented in red engage in stable conjugations: TI(32), TI(23), TI(21), TI(31), TII(12), TII(13); cells with ILists in black engage in frustrated interactions: TI(21), TI(31), TII(12) and TII(13); and cells with ILists in blue only interact with subtype II APCs, but without stable contacts: TI(23) and TI(32).

In Methods we derive a set of differential equations to model the evolution of the population under positive and negative selection. These equations were integrated numerically in the presence of negative selection (Figure 6a), or of both, positive and negative selection (Figure 6a). Since in both cases, T cells leading to stable conjugations are straightforwardly eliminated, only conjugation lifetimes for the remaining cells are shown in Figure 6c,d.

Figure 6
figure 6

Impact of positive and negative selection on the evolution of T cell frequencies for the simplified model in Figure 5.

In a) and c) only negative selection is applied. In b) and d) positive and negative selection are applied simultaneously. Initially there are only non-conjugated cells with frequencies , , , (i = 1,12) and . In a) and c), , while in b) and d), . Lines in blue in a) and b) represent the total number of T cells with ILists represented in blue in Figure 5; in red are represented the total number of T cells that establish stable conjugations; in black are represented the total number of T cells engaging in frustrated interactions. In c) and d) are displayed conjugation lifetimes for the most relevant conjugates in the population. As stable cells are eliminated, their lifetimes are not represented. The conjugate with the largest lifetime in c) corresponds to conjugate A1TI(21), whereas in d) these conjugates A2TI(21) and A2TI(23). These results show that if no positive selection is applied, neglected T cells accumulate and the maximum conjugation lifetimes are larger than in the model in which positive selection is applied and these cells are eliminated. These results agree with numerical results obtained for cellular automata with more ligands (see SM8).

It is clear that the maximum conjugation lifetimes are considerably smaller when positive and negative selections are applied simultaneously. These results are also consistent with results obtained with the cellular automata model for populations with greater diversity and in which connectivity constraints are not as severe (see SM8). In more general terms one can argue that these results are typical of heterogeneous populations, i.e., populations in which ligands appear with very different frequencies.

To understand why positive selection plays such a crucial role, assume, to simplify the analysis, that stable pairs are straightforwardly eliminated by negative selection. Then only the other kinds of T cells remain (with ILists in black and blue in Figure 5). In that case simple expressions for conjugation lifetimes can be derived (Table 1). In order to account for all feedback effects involved in the numerical integration, these expressions depend on steady state frequencies.

Table 1 Conjugation lifetimes involving some subtype I T cells in the absence of stable pairs

Some conclusions can be drawn from their analysis. Given that frustration requires the two T cell subtypes to be present, we start by analyzing how subtype I T cells evolve. Given that and , we can conclude that TI(23) cells are certainly not eliminated at a higher rate than TI(21) cells by negative selection. In case , then TI(23) cells should grow in numbers. This can happen if ligands p2 and p3 are present in different numbers. For instance, if ligand p2 is in small numbers, then can increase considerably because the TI(21) finds difficult to form A2TI(21) conjugations, while the same does not happen for . This is confirmed from the numerical integration of the differential equations (Figure 6). In that case, TI(21) cells are eliminated at a higher rate and consequently TI(23) and TI(32) cells grow.

We conclude that to minimize conjugation lifetimes it is necessary to eliminate TI(23) and TI(32) cells. This can naturally be done by eliminating T cells according to the time they spend unbound since, in the absence of stable pairs (see Table I and Methods).

Discussion

The cellular frustration framework presents an alternative view on how the immune system works. Traditionally, the notion of high reactivity is tightly connected with the notion of high affinity (or avidity) of cellular interactions. This had important implications. To maintain tolerance towards self, the affinity range of T cell receptors had to be limited. Negative selection was thus expected to shape and decrease the affinity of T cell interactions.

The cellular frustration scenario is in stark contrast with this view. In this case, tolerance can be achieved even when T cell receptors establish interactions with high affinity, provided interactions are directed towards ligands displayed by cells that frustrate interactions. Hence, education does not need to modulate interactions' strength, only the targets. We believe that this view agrees better with the extensive potential autoimmunity that exists in the healthy and which led some researchers to propose that the immune system regulates autoimmunity14.

As discussed in the introduction, a clear advantage of the present framework is that perfect SNSD can be robustly achieved. Most strikingly, this is only achieved in the presence of positive selection, costimulation and anergy, a range of phenomena that still lacked convincing explanations and which consistently fit in the present framework. Another important issue concerns the flexibility of information in ILists. Perfect SNSD depends crucially on the assumption that T cells interact with APC ligands using extremely flexible patterns of interaction. This allows one T cell receptor to have high affinity towards two peptides while another T cell receptor could have high affinity towards one, but very low towards the other one. In this way T cells can have high affinity towards foreign peptides and simultaneously low and very different affinities towards self-peptides. This means that T cells measure interactions using different metrics. In practice these flexible interactions could result from the fact that T cell receptors bind only strongly to a subset of amino acids in the peptide-MHC complex27 and this subset could differ from one T cell receptor to the other.

Here we considered the simplest model to put in evidence the main mechanisms involved. However, the model should not be taken literally. Certainly, the number of cell subtypes can be larger and cell interactions could be modeled differently. For instance, it is likely that APCs could interact with more than two cells at a time, or that the affinity of interactions could be related to some less general functions of amino acid sequences. The main point however, is that there should always be restrictions on the number of cells one cell can interact with. This necessarily imposes an optimization decision process as it has been experimentally observed28. Also, what our construction of ILists shows is that their flexibility is important. Modeling affinity interactions using more realistic metrics, like based on the similarity on the number of contiguous amino acids does not change our results as it will be shown elsewhere.

Finally, another point that is important to discuss concerns the frequency and timing at which positive and negative selection take place. This, we believe, depends substantially on how the sequence of events in the thymus is modeled. In the present work we assumed that every eliminated T cell would be replaced by another cell with random ligands and receptors. This is certainly an oversimplification of the sequence of selection events taking place in the thymus. It is more likely that, at later stages in the education, the newly introduced lymphocytes have already gone through several selection stages and consequently their ligands and receptors could depend on their education stage. Indeed we verified that negative selection maximizes frustration more efficiently – in terms of speed and conjugation lifetimes reduction – when the education process was divided in two main stages: during an initial stage, positive and negative selection would take place; on a second stage only negative selection would be used and in this case, only T cells ILists would be replaced by new randomly generated ILists. This assumes that T cell subtype frequencies and connectivities would be regulated (through cytokine communication and the thymic medula micro-environment), so that any new incoming T cell would already encode this information. Clearly there seems to be an advantage in placing positive selection in initial stages, as when τp is large, frustration maximization is not effective. However, from our current model it is not possible to predict whether positive and negative selections are disjoint or coexisting.

To conclude, this work showed that the adaptive immune system should be capable of performing perfect SNSD. This is an important result because it identifies a new type of complex system organization with an underlying principle - the Principle of Maximal Frustration - that can confer a reliable protection mechanism and an important evolutionary advantage. Using an analogy, these results show that the immune system can work as a sophisticated proofreader, capable of promptly detecting any misspelling in a text. The ability of detecting uncommon word associations is also possible and will be discussed in another publication. Finally, these results do not imply that the adaptive immune system protects the host perfectly as it still relies on what is presented by the innate immune system and also on how self-peptides remain homeostatically equilibrated (see SM9).

Methods

Cellular frustration model

The model has two cell types, APCs and T cells and each cell type has two subtypes, I and II. APCs from cell type I favor interactions with T cells of subtype I and the reverse happens for cells of the other APC subtype. APCs present peptides depending on their MHC molecules, which can vary from individual to individual. Here we associated each APC with a random number ni, selected from 1 to Np, the maximum number of peptides (see also SM10); cell index i varies between 1 and N, the total number of APCs. Np can be arbitrarily large. In fact, our results will not depend on how large this value is. We considered populations with the same number of APCs and T cells. Each T cell receptor is associated an IList, where all Np numbers (i.e., the APC ligands) are listed in decreasing order of affinity. In case limited connectivity is considered, T cell ILists contain only a randomly drawn subset of the Np numbers. In the results presented here we assumed that all T cells interacted with the same number of different self APC ligands.

A discrete time dynamics was considered (see SM1). For each time step each cell is put in interaction in a random order with a randomly drawn cell from the other cell type. Cells that are put in interaction form a new conjugate if both cells favor the interaction. In that case either cells were previously alone or formed conjugates with cells ranked lower in their ILists. Conjugation lifetimes are measured as the number of iterations required to separate a conjugate. Neglect times are measured as the number of consecutive iterations a T cell stays non-conjugated.

Repertoire education

Positive and negative selection processes were defined so that, after a fixed number of iterations (W~5000) T cells that: 1) did not establish conjugations for a time greater than a threshold time, τp, were eliminated by neglect; 2) established conjugations lasting longer than a threshold time τn, were eliminated by induced apoptosis. In either case cells were replaced by new incoming T cells with randomly generated ILists. Depending on the simulation, the new T cell subtype was randomly drawn, or it could be equal to the cell subtype of the eliminated cell. If after W iterations no cells exceeded threshold times τp and τn, then thresholds were updated to the maximum times cells stayed either, not conjugated or conjugated, respectively. At this moment the T cell population was recorded. In some studies positive and negative selection processes were considered separately. In particular, it was verified that, for systems with limited connectivity, the best results were obtained when positive and negative selection were performed at initial stages simultaneously, during which the newly introduced T cells had both, random ILists and belonged to an arbitrary subtype. On a second stage, only negative selection was applied and the introduced T cells inherited the T cell subtype of the eliminated cell. When τn reaches a target value or the number of iterations exceeds a target value (for instance, 107 iterations), then the last recorded population is added to the T cell repertoire and the education process is restarted with a new randomly drawn T cell population. In populations with limited connectivity, the education of populations after the first registered population does not change each cell's connectivity. See the full algorithm in SM1.

Cellular activation and response to pathogens

Modeling T cell activation in lymph nodes follows after the creation of the repertoire of T cell populations. The same cellular decision dynamics is applied as during repertoire education, except that anergy is introduced. Then, all T cells that terminate a conjugation and are left non-conjugated are replaced by equivalent cells in the repertoire. For a matter of consistency with models with limited connectivity, an equivalent cell is a cell with the same index but originating from different educated populations. In models with limited connectivity, equivalent cells have the same connectivity. To establish a T cell activation criterion, the cellular decision dynamics in the presence of anergy is run for W iterations (W = 5000). This is called a calibration stage and could take place in the thymus. Then, the number of conjugations living for at least τact iterations is registered for APC i in the absence of pathogens. This defines a reference for the largest rate of long conjugations in the healthy state: . Here we introduced a tolerance factor f (typically f~1.05) to avoid autoimmunity.

To study the response of the system in the presence of a pathogen, a randomly drawn APC cell presents a random number between 1 and Np, not contained in the initial self-set. It is considered that the system can mount an immune reaction if the ratio of long conjugations established by the pathogenic APC (i = P) and the reference rate:

is greater than 1.

Modelling dynamical frustration with differential equations

A set of differential equations was derived to gain additional insight on the importance of positive selection during repertoire education. The simplified version of the model has APCs with 3 different ligands, p1, p2 and p3. T cells can belong to two different subtypes, I and II and can have 6 different types of ILists with connectivity 2 (i.e., they have only 2 self-ligands). Twelve different kinds of T cells can be associated an integer index between 1 and 12, or equivalently represented by their subtype and IList (Figure 5). For instance, TI(23) denotes subtype I T cell with an IList that ranks first ligand p2 and next ligand p3. To write compact differential equations APCs and T cell indices were extended to 0, to account for their absence. In this way, ni0 and n0j denote respectively, the frequency of non-conjugated APCs and T cells. It is assumed that n00 = 0 and that the normalization condition holds. The dynamical differential equations can be written as:

For example, C0ji0 = 1, because any pair of conjugated cells will form a conjugate. One of these equations is derived in the SM7. Conjugation lifetimes can then be obtained as21:

where represents steady state frequencies. T cells TI(12), TI(13), TII(31), TII(21), TII(32) and TII(23) establish stable conjugations with APCs displaying ligands that are top ranked in their ILists. In that case conjugation lifetimes diverge and consequently they are eliminated by negative selection. Then, in their absence, the expressions for conjugation lifetimes in Table 1 are valid.

Dynamical equations including repertoire selection

During repertoire selection, cell death by induced apoptosis or neglect is triggered with a probability that depends on the time duration of the conjugated or non-conjugated state, respectively. There are several possible methods to account for duration dependent transition probabilities. One possibility consists in distinguishing states depending on their duration, as in Ref. [29]. Here we take a simpler approach which assumes that the rate of negative selection events is proportional to the probability that a conjugate is not destabilized for a time duration τn. Considering negative selection, an additional contribution was added to the right hand side of equation (4), given by:

At the same time, is added to equations, for all j, as all kinds of non-conjugated T cells can replace eliminated T cells. Also, is added to , as APCs with ligand i become non-conjugated. Similarly, for positive selection:

The first contribution arises from elimination by neglect of APCs with ligand j, while the second contribution accounts for T cell replacement due to elimination by neglect of all APCs.