Saturday, April 22, 2017

Simulating continuous space in SLiM 2

I've written before about SLiM 2, the genetically-explicit, individual-based evolutionary simulation software package that I work on nowadays with Philipp Messer at Cornell (see here and here).  SLiM is very flexible, because it's highly scriptable; you can customize and extend almost every aspect of its simulations.  It's also quite fast, as we showed in our paper on it (and it has gotten even faster since then).  It can run on Mac OS X or on Linux, including at the command line and on computing clusters.  And it includes a full-featured graphical modeling environment for OS X, allowing interactive model development and visual debugging.  And it's open-source (on GitHub) and free to use.  If you haven't checked it out already, those links above are a good place to start.

The SLiMgui modeling environment.

In this post I want to call attention to some new features I added recently: support for continuous space, and for spatial interactions.  These features are now released in version 2.3 of SLiM (downloadable from the SLiM home page).  SLiM 2 has always support spatially discrete subpopulations connected via migration, and it has always been possible to write script that governs interactions between individuals.  With these new additions, we now support continuous space as well as discrete space (and even both in the same model, if you wish), and we now provide built-in support for extremely fast evaluation of spatial interactions (i.e., individuals interacting with the other individuals nearest to them in space).

We'll look at three spatial models in this post.  Let's get right into it with a video of a continuous-space model running in SLiMgui:

What you see here, if you click play, are individuals (the little colored squares) in a 2D space (the black background).  SLiM now supports 1D, 2D, and 3D models, but since 2D is the easiest for us humans to visualize we'll stick with that here.  This model includes two spatial interactions: spatial competition, and local mate choice.  Individuals in this model are colored according to their fitness; when a dense cluster of individuals occurs, they compete with each other so strongly that their fitness is substantially decreased (the colors toward red).  On the other hand, it doesn't pay to be off by yourself in the wilderness; you might experience little competition, but you will also be unlikely to be chosen as a mate since mate choice is also local in this model.  These considerations thus trade off, leading to dynamically evolving spatial clusters that walk the line between being too dense and being too dispersed.  The number, size, and density of the clusters depend upon factors such dispersal distance (which is also local in this model), competition strength, and the mating kernel.

So we have a fairly sophisticated spatial model here, and it also includes all of the usual sophistication of SLiM models: individuals here have explicitly modeled chromosomes, with every mutation tracked through time, for example.  What does the SLiM script for a model like this look like?

initialize() {
   initializeMutationType("m1", 0.5, "f", 0.0);
   initializeGenomicElementType("g1", m1, 1.0);
   initializeGenomicElement(g1, 0, 99999);
   // spatial competition
   initializeInteractionType(1, "xy", reciprocal=T,
   i1.setInteractionFunction("n", 3.0, 0.1);
   // spatial mate choice
   initializeInteractionType(2, "xy", reciprocal=T,
   i2.setInteractionFunction("n", 1.0, 0.02);
1 late() {
   sim.addSubpop("p1", 500);
   for (ind in p1.individuals)
1: late() {
fitness(NULL) {
   totalStrength = i1.totalOfNeighborStrengths(individual);
   return 1.1 - totalStrength / p1.individualCount;
mateChoice() {
   // spatial mate choice
   return i2.strength(individual);
modifyChild() {
   do pos = parent1.spatialPosition + rnorm(2, 0, 0.01);
   while (!p1.pointInBounds(pos));
   return T;
2000 late() { sim.outputFixedMutations(); }

That's all it takes!  This is actually recipe 14.4 in the "SLiM cookbook", a compendium of example models that occupies the first half of SLiM's manual; for a more complete discussion of this model you can refer to that recipe.  Here, let's just walk through it very quickly.

The script is divided up into script blocks that do different things and run at different times; each script block is enclosed by braces {} and has indented script inside.  The first block is an initialize() callback, which SLiM runs when the simulation is first initialized.  It tells SLiM to run a 2D model using x-y coordinates, and sets up some basic genetic structure (mutation rate, chromosome length, recombination rate, background neutral mutations generated stochastically, etc.).  Then it initializes two interaction types: one for spatial competition, the other for spatial mate choice.  Without getting into the details too much, the calls here declare that these interactions both depend upon both the x and y coordinates of individuals, but they use different spatial interaction kernels and have different maximum distances.

Next comes a script block that runs at the end of generation 1.  It kicks off the simulation by adding a new subpopulation of 500 individuals, and gives each individual an initial position in space that is drawn randomly from within the spatial bounds of the subpopulation (we use the default bounds here, of [0,1] for x and y, but those bounds can be changed, of course).

The next block runs at the end of every generation from 1 onwards; that is the meaning of "1: late()".  It simply tells the two spatial interactions to evaluate themselves.  Interactions can be evaluated at any time; we have chosen this point in the generation cycle to do it.  (Since the positions of individuals can change at any point in time, it is necessary to choose such a "reference time" for interactions.)

Next comes a fitness() callback.  This is a script block that affects the fitness values for individuals, based upon genetics or upon any other simulation state.  Here we use the first interaction type that we defined, i1, to total up the interaction strengths between the focal individual and all other nearby individuals, and we decrease the focal individual's fitness accordingly.  This implements spatial competition: the more individuals are near us in space, and the nearer they are, the lower our fitness.

The next block is a mateChoice() callback.  This consults the second interaction type that we defined, i2, in order to obtain mating weights to use between the focal individual and all other individuals.  In essence, it says that the mating weight for each potential weight is equal to its interaction strength: the closer the potential mate is, then, the more likely it is to be chosen as a mate.

Then comes a modifyChild() callback; this is called whenever a new offspring individual is being generated by SLiM.  Its job is just to set the spatial position of offspring.  It does this by starting with the position of the first (i.e., maternal) parent, adding random deviations to x and y drawn from a normal distribution, looping until the new position is within spatial bounds (thus implementing a "reprising" boundary condition), and finally setting the position on the offspring.  It returns T (true) to tell SLiM to proceed with generating the proposed offspring.

Finally, an event at generation 2000 outputs a list of all of the mutations that fixed during the run of the model;  The model then terminates, since there are no events scheduled for generations beyond 2000.

Apologies if that description was eye-wateringly boring!  The point is just to show that it really is possible to set up a genetically and spatially explicit model, including both spatial competition and local mate choice, in less than a page of code.  SLiM is doing a whole lot of work behind the scenes, of course; the spatial competition interaction here, for example, is actually evaluated using an advanced data structure called a k-d tree, allowing very fast interaction calculations even for relatively large population sizes.  But all of that complexity is hidden, effectively encapsulated by SLiM's scripting interface.

We're going to look at two other spatial models here, but in much less depth; we won't look at more Eidos script, we'll just see videos and discuss the models a little.  These models are also recipes in SLiM's "cookbook", though, so you can look at their code in detail if you wish.

The second model we'll look at (recipe 14.10 in the cookbook) involves a "landscape map" that defines an environmental characteristic that varies across space.  For this somewhat silly model, we have created a landscape map that reflects the land/water dichotomy across the whole planet.  This video might get clipped on the right by the narrow width of the blog; you can open it in YouTube to see it at full size:

The model uses this landscape to limit the dispersal of individuals; new offspring can disperse onto land locations, but not water locations.  The whole population starts in a tight cluster in East Africa, and then disperses outward from there to reach Madagascar and the Middle East, India and Russia, and Western Europe.  If the model runs longer, the population finds its way across the Indonesian archipelago and into Australia; making it to the New World, however, is highly unlikely since it would require such a long jump across open ocean.  (The Bering Strait is not open to migration, since the left and right boundaries of the map are not connected in this model.)

Obviously this is just a toy model – the way that the map projection distorts the globe makes it unrealistic, just for starters – but it is a proof of concept, showing that any arbitrary landscape map can be defined and can then be used by the model to modify the simulation dynamics in any way desired.

The third and final model we'll look at (recipe 14.11 in the cookbook) generates a landscape map programmatically, rather than using a pre-fab map as the previous model did.  Each run of the model uses a different, random map, and this map defines what sort of phenotype is most fit at that point in space.  You could think of the mapped environmental trait as being elevation, for example; at a high elevation location, high-elevation-adapted individuals will be most fit, and mutatis mutandi for low elevations.  There is thus local, divergent selection pressure applied across the map, selecting for local adaptation.  And that is what we see:

The colors of individuals here reflect the environment for which they are best adapted; yellow individuals are most fit in yellow landscapes, red individuals in red landscapes.  The population starts out as all red (with occasional mutants), and those red individuals quickly die off in areas of the map that aren't red, leaving red populations in the redder areas of the map.  Mutants arise and manage to establish locally-adapted populations in several locations across the map, however; this is actually speciation occurring in the model.  This is a sexual model, and mate choice is both spatial and assortative; it is a "magic trait" model in which the trait under divergent selection is also used in assortative mating, facilitating divergence and speciation.  A longer runtime often leads to colonization and local adaptation to conditions across the map.  Although it is not the same model, this is in many ways similar to a model I wrote with Rupert Mazzucco and Ulf Dieckmann a couple of years ago; but that model was not written in SLiM, and so it involved a huge amount of code and took months to write (and it was not even genetically explicit, nor was it a sexual model!), whereas I coded this recipe in SLiM in about an hour, and it is about a page and a half of code!

You might wonder exactly how the individuals in this model are adapting; what is the genetics underlying the phenotype modeled here?  This is a QTL-based model, as it happens.  QTLs that affect the phenotype of individuals can arise randomly anywhere in the genome, with any random effect size.  Phenotypes are then calculated based upon the additive effects of all of the QTLs possessed by a given individual.  So it is essentially a quantitative genetics model, but instead of assuming an infinite number of loci of infinitesimal effect, as analytical quantitative genetics models typically do, it actually allows a QTL-based genetic structure to emerge dynamically.  One could then look at what that emergent QTL structure looks like – the distribution of effect sizes, for example – and how that is affected by the spatial structure of the model.  There are all sorts of fascinating theoretical questions in this area that can now be explored in SLiM, with just a few lines of script, in a graphical modeling environment.  Empirical questions, too, can be modeled since you can specify whatever genetic structure you wish (based on the genome map of your study organism, perhaps), set up whatever initial model state you desire (based upon empirical measurements), create whatever landscape maps you wish (based upon your field site), and so forth.

I must say I can't wait to see how people use these new features!  If you're interested, download SLiM from the SLiM home page, join the slim-announce mailing list to hear about new versions with new features like this, and/or join the slim-discuss mailing list to ask questions or discuss ideas.  Thanks for reading, and happy modeling!

Thursday, April 6, 2017

Rapid evolution and ecosystem services. What have you done for me lately?


Be honest now. How often does a cross-building collaboration between two grad students get launched? Our paper, now available on early view in TREE, is the result of a couple PhD students from different departments at UBC learning through the grapevine that they were thinking about the same idea: recent evolutionary processes that lead to ecosystem services. Seth (Zoology) already had a manuscript when Maayan (Resources, Environment, and Sustainability) heard about him through a mutual friend and they joined forces. We both had a remarkably similar take on the idea that rapid evolution could generate not just costs, but also services that benefit people. Though writing a paper that’s accessible for both conservation scientists and evolutionary biologists was a bit challenging, we had good representation of the two ‘sides’ between the two of us (as well as Kai and Dolph) so the battles were balanced and we ended up understanding more about each other’s disciplinary quirks and anxieties: Maayan now knows the fine differences between evolutionary and genetic rescue; Seth knows never to conflate ecosystem service supply with benefit. After 15 months of deciding on examples, wrangling terms, and creating and destroying framing categories, the paper is done, and it’s pretty much what we had in mind: an ‘idea’ piece squarely aimed at both conservation scientists and evolutionary ecologists exploring how sometimes rapidly evolving species can benefit people.

The authors benefitting from eco- and evo-system services. Clockwise from top left: Seth Rudman holding a beautiful and delicious Lake Michigan Lake Trout (Salvelinus namaycush), the abundance of which may have been buoyed by character displacement between alewife and bloater chub. Maayan Kreitzman picking an apple that was probably not too badly infested with the Apple Maggot (Rhagoletis pommonella) due to the cascading speciation of R. pomonella predators. Dolph Schluter clearly enjoying the sense of place imparted by getting close to squirrel monkeys on the Amazon River near Leticia, Colombia. Kai Chan glowing from the cultural and social cohesion generated with his daughter while kayaking on Campbell Lake near Parry Sound, Ontario.

Eco-evolutionary dynamics and people
Rapid evolution can alter ecological process, but how best to quantify this alteration?  Previous work has used measures of the effect size of intraspecific variation on the abundances of interacting species, metrics of community composition, and measures of ecosystem functions (a fantastic early example is [1]).  Yet, as biologists who are keenly aware of the goods and services that nature provides for human health and well-being, perhaps measuring or calculating the contribution of rapid evolution to ecosystem services is more informative in some cases.  The ecosystem services concept links the functioning of ecosystems and the material or nonmaterial benefits that humans derive from them. (As such, ecosystem services is an unabashedly anthropocentric concept - though not necessarily an exclusively materialistic one. So when we talk about benefits and costs, we’re doing so in the ecosystem services context, ie from a human perspective, not from the perspective of other species.) By assessing the way that rapid evolutionary change alters ecosystem services we could focus our efforts on measuring how evolution alters the components of communities and ecosystems we humans rely upon the most, thereby translating the importance of rapid evolution into units often used for conservation and management decisions.  In our TREE paper (freshly available on early view [2]), we hope to provide a foundation for such work by providing a framework for rapid evolution and ecosystem services, and describing some promising examples.

We have long known that rapid evolution can cause powerful negative impacts: antibiotic resistance and pesticide resistance are two classic areas of applied evolutionary biology.  However, there have not been attempts to document when rapid evolution might enhance ecosystem services.   To this end, we define the term contemporary evosystem services as “the maintenance or increase of an ecosystem service resulting from evolution occurring quickly enough to alter ecological processes”.  This term, and our opinion manuscript as a whole, owes quite a bit to previous work on evosystem services [3,4], which first recognized the role that evolution can play in ecosystem services. 

A history of the term ‘evosystem services’
This previous work, while it recognized the potentially beneficial role of rapid evolution, had a different focus and orientation: the aim of Faith et al. 2010 and Hendry et al. 2010 was to use the increasingly popular language of ecosystem services to draw attention to the importance of preserving biodiversity. In our initial submission we roundly criticized this term as being too meta-scale to be informative and we considered repurposing the term evosystem services to refer only to the contemporary contribution of evolution to ecosystem services. We came to our sense when we paid greater attention to the history of the term.  In part, this was due to the interesting history the proprietor of this blog (Andrew Hendry) shared with us  on his review for TREE of our second submission (reproduced with permission and a few small tweaks by Hendry, emphasis ours):

A brief history of the term evosystem services might be interesting to the authors, reviewers, and editor. I am not saying this history needs to be added to the paper. The idea came from concerns among evolutionary biologists involved in international biodiversity NGOs (DIVERSITAS, FUTURE EARTH, IPBES) that the justification of biodiversity conservation by reference to ecosystem services (the major trend in such NGOs) ran the risk of preserving only the few perceived "useful" components of biodiversity, rather than biodiversity per se. A group of us (including Faith) involved in these NGOs were concerned that evolutionary components of biodiversity were no longer of primary conservation concern, nor was rapid evolution being considered. Over discussions, we realized that ecosystem services were, in reality, a good justification for considering evolution per se once one recognized that ecosystem services were the product of evolution in the past, present, and future. Hence, the Faith definition as quoted in this MS was originally intended specifically to make evosystem services synonymous with ecosystem services - to make clear the importance of studying evolutionary diversity even when interested in ecosystem services. [A Hendry 4/9/2017: Stated more correctly, ecosystem services ARE evosystem services.] Thus, the original intent of the term was exactly that which authors of the present MS criticize - that it is all inclusive and, as the authors argue, therefore unhelpful). Personally, I am fine with the authors’ redefinition of the term to focus on contemporary evolution with ecological impacts (rapid evolution) on ecosystem services, but I think they should at least be aware that the original all-inclusiveness of the term was intentional a strategic attempt to increase attention paid to evolutionary patterns and processes when using ecosystem services as justification for biodiversity conservation. It might be valuable for the authors to at least point out that they are not questioning the importance of past evolution for ecosystem services - and that this link is an important point to recognize in biodiversity science (cite Faith). It is just that the present MS is focusing on the contemporary "rapid" aspect of this idea.

As we can see, the term evosystem services was originally meant to stress the inclusion of thinking about evolutionary diversity in conservation circles, and to mitigate against the idea that all of nature is appropriate fodder for tinkering and management, which the ecosystem services concept might seem to encourage. As stated above, Faith and Hendry et al. were arguing that all ecosystem services are evosystem services because past, present, and future evolution is at the root of all possible ecosystem services. While this is undoubtedly true, our critique of this approach is that it so general that is ends up being more rhetorical than operational: rather than providing an avenue to study and measure how evolution might affect specific ecosystem services, it simply gives evolution “credit” for all ecosystem services in hopes of (legitimately, in our opinion) highlighting the overall importance of diversity within the politics of international conservation. As Hendry mentions, an earlier draft of our paper proposed to redefine the term evosystem services, which we decided not to do in the end. Instead, we developed the framework in figure 1, which situates the contributions of different evolutionary processes to ecosystem services. This framework accepts Faith’s very inclusive definition of evosystem services (green), but also hones in on the nested subset of them that we believe to be the most operational and measurable (dotted line): those that are happening contemporarily on rapid timescales through the processes of local adaptation, gene flow and in one instance, speciation. We named these contemporary evosystem services. 

Figure 2: The nested and overlapping processes that produce evosystem services. 

There’s no conceptual conflict between the inclusive definition of evosystem services, as forwarded by Faith and Hendry, and our definition of contemporary evosystem services as a nested subset within that. But, there is perhaps a legitimate tension in the values encoded by these two terms - which has resulted in some debate and even misunderstanding as we’ve attempted to get this work published. The group that Hendry refers to was concerned that conservation science and policy was heading in a direction that put too much value on particular services that might be provided by low-diversity or otherwise artificial types of systems (for example carbon sequestration using fast-growing alien species) at the expense of a more traditional conservation approach based on the preservation of biodiversity and the intrinsic value of nature. We on the other hand wanted to lay the foundation to study particular cases where a specific evolutionary processes might be providing specific ecosystem service in order to better understand and manage those systems. Therefore, it might be fair to characterize the two approaches as supportive of macro-management vs micromanagement. The thing is, we think both are important. We are far from presuming that it’s possible or even desireable to micromanage all the places where rapid evolution is occurring in order to maximize units of service. But neither do we think that general insight about the potential value of biodiversity over the long term can replace system specific knowledge in management scenarios. There is a middle ground between the arrogant micromanaging of nature, and the too-general assertion that diversity is a good thing to conserve.

Why study contemporary evosystem services
We believe that studies exploring this rich middle ground in an evolutionary context by looking for a positive relationship between rapid evolution and ecosystem services are lacking, not because such cases don’t exist, but perhaps  because there hasn’t been a conceptual or quantitative framework in which to place them. In our paper, we therefore suggest a framework for assessing the contribution of rapid evolution to ecosystem services and provide a number of putative examples where rapid evolution might enhance ecosystem services. Although there are no iron-clad examples of contemporary evosystem services we outline some of the most promising potential examples.  We grouped these examples by the evolutionary process, namely directional selection and gene flow, that might maintain or enhance the ecosystem services. It’s important to note that these evolutionary mechanisms can function to either enhance or deplete ecosystem services, depending on the context. Our paper highlights potential benefits from an anthropocentric perspective because we felt that this emphasis was lacking in the applied evolution and conservation literature so far.

One of the most compelling potential examples of how rapid evolution might provide an ecosystem service comes from the literature on the rapid evolution by directional selection of Daphnia.  Some Daphnia species can evolve rapidly to grow faster when feeding on toxin-containing cyanobacteria [5–8].  This rapid evolution likely increases the total amount of cyanobacteria consumed, potentially reducing the intensity and duration of harmful algal blooms (HABs) associated with eutrophication.  Future work assessing the contribution of rapid evolution of Daphnia to the reduction of phytoplankton species that cause HABs could yield an idea of the value, in ecosystem service units (often dollars), of rapid evolution in Daphnia.

Gene flow could also provide contemporary evosystem services.  For example, several recent models [9,10] suggest that sufficient influx of susceptible genes from oceanic sea lice to salmon aquaculture net-pens delays or prevents the evolution of insecticide resistance on farms. The observation that insecticide-resistant sea lice are absent from salmon aquaculture located in the North Pacific (where large populations of wild salmon exist) compared to the prevalence of insecticide resistance in the South Pacific and Atlantic (where there are small or no wild salmon populations) seems to support this.  Other examples of putative contemporary evosystem services from gene flow are 1) cases of genetic rescue, when migration from another population provides an influx of genes that restores positive population growth to a population that would otherwise perish from inbreeding depression, and 2) mitigation of fishery size selection through gene flow from a marine protected area.

Environmental change necessitates that evolutionary biology and conservation be integrated and tremendous progress has been made in the past decade.  Our opinion is that measuring and understanding contemporary evosystem services could further contribute to this integration. Our end-goal is to spur research that comprehensively assesses how evolution alters ecosystem services (e.g. the services and the dis-services from rapid evolution), which we feel will yield an improved understanding of ecosystem services today and in the future.