Introduction

The purpose of this vignette is to illustrate a new outlier detection method that uses genetic data and spatial coordinates to detect loci potentially under selection (Wagner et al. 2016). This method, which we call Moran spectral outlier detection (MSOD), uses the power spectrum of Moran eigenvector maps (MEM) to quantify how variation in the frequency of an allele is distributed across a range of spatial scales defined by MEM spatial eigenvectors.

The power spectrum for a locus is the squared correlation coefficient obtained by correlation of that locus with the MEM eigenvectors. By comparing the power spectrum of an individual locus to the average power spectrum of all of the loci, we can identify outlier loci that show a power spectrum that deviates from the average. This deviation reflects the spatial signature of selection, while the average power specturm (composed primarily of neutral loci) mainly reflects a spatial signature of gene flow. This approach can be used on individual or population based data sets.

If environmental predictors are available, MSOD can be followed by a second step that uses Moran spectral randomization (MSR) to test associations between outlier loci and environmental predictors, while accounting for spatial autocorrelation.

Assumptions

MSOD relies on an assumption of normality under the null hypothesis that the distributions of loci are shaped by the same process (gene flow). Deviations from normality are likely under non-equilibrium and isolation by distance scenarios, which may increase false positive rates. MSR relies on stationarity assumptions, which means that p-values may be inflated if both variables have a linear trend. See Wagner et al. 2016 for more discussion of these assumptions, and how they impact outlier detection in a variety of simulation scenarios.

Data & packages

In this vignette, we’ll walk through an MSOD/MSR example using part of an individual-based simulation data set from Forester et al. 2016. In this case, the genetic data are input as allele counts (i.e. 0/1/2) for each locus. For population-based data, you can input the genetic data as allele frequencies within demes.

The example data provided here are simplified from a CDPOP (Landguth & Cushman 2010) simulation output file. This particular simulation used an aggregated habitat surface (plotted below), a selection coefficient of 5%, and individual dispersal of 5% of the surface per generation. These data are from the first replicate of 10 for these simulations. The full simulation data set is available at Dryad doi: 10.5061/dryad.v0c77, with more information on the simulation scenarios in Forester et al. 2016.

Begin by installing the necessary packages, if you don’t already have them:

Analysis

Section 1: Load and inspect the data

The dimensions of our data frame are 500 rows and 103 columns. Every row in the data frame contains information for one individual, so in this example we will be analyzing 500 individuals. Columns 1 and 2 are the X and Y UTM coordinates for each individual. Column 3 is a factor (“Env”) indicating what type of habitat an individual is located in. Columns 4 through 103 are genotypes at 100 loci for each individual. These genotypes are provided as allele counts (0/1/2). Locus 1 (L1) is the locus under selection (responsive to the habitat factor “Env”). The remainder of the loci are neutral (L2 - L100).

Now that we have loaded the data, our next task is to calculate the Moran eigenvector maps (MEM) from the XY coordinates of our 500 individuals. MEM are orthogonal synthetic variables that provide a decomposition of the spatial relationships among sampling sites based on a spatial weighting matrix (see Dray et al. 2006 and Wagner et al. 2016 for more information).

In the code below, we begin by defining neighbors with a Gabriel graph using function graph2nb. Then we create a spatial weights matrix (object listW) so that neighbors receive a weight that is proportional to the inverse distance between a location and that neighbor. We use function nbdists to calculate Euclidean distances along the links. MEM are then extracted by eigenanalysis. This produces a set of eigenvalues (one for each MEM axis) that quantifies the spatial scale of that MEM. We also obtain the MEM eigenvectors, which are the MEM axes we’ll use for our analysis. In this case, we have 500 individuals sampled at 500 sets of XY coordinates, so we end up with 499 MEM axes.

Let’s plot the habitat selection surface, the 500 sampled individuals, and the Gabriel graph used to calculate the spatial weights matrix and MEM variables. First we need to download the ascii Habitat file from the Dryad upload:

Section 2: Moran spectral outlier detection (MSOD)

Now that we have all of the necessary data, we’ll calculate a MEM power spectrum for each of our 100 loci. We do this by calculating correlations R.YV between allele counts (or frequencies) at each locus and all 499 MEM eigenvectors. Note that the square of R.YV for each locus is its power spectrum.

Compare the power spectra plots for the locus under selection (top) and a neutral locus (middle). The average power spectrum across all 100 loci is shown at the bottom.

Next, we’ll calculate a z-score for each locus, based on deviation from the average power spectrum. Then we’ll compare outlier detection based on three cutoff options.

This plot shows the standardized z-scores for the 100 loci (locus under selection in red), along with the three outlier cutoffs. You can see that two loci would be detected as false positives using the most liberal cutoff of 0.05. We’ll continue and just use the middle cutoff of 0.01:

## [1] "True positive rate:  1"
## [1] "False positive rate:  0.01"
## [1] "Falsly identified outlier loci (ID):  L90"

Notice how true and false positive rates change based on the cutoff. Setting a cutoff and deciding on a correction for multiple tests depends on your question and your tolerance for false positives and false negatives.

Section 3: Moran spectral randomization (MSR)

This step is used as an optional post-MSOD test for association between candidate outliers and environmental predictors. MSR calculates correlations between loci and an environmental predictor while accounting for spatial autocorrelation in both data sets. A randomization approach is used to build a null hypothesis of no correlation, given the power spectra of the candidate outlier and the environmental predictor. See Wagner et al. 2016 for more information on MSR and relevant citations.

For these simulations, we use the UTM coordinates as spurious environmental predictors. You can think of them as environmental variables that are correlated with latitude and longitude. The correct predictor driving selection at the locus under selection (L1) is “Env”.

## [1] "Loci significantly associated with Env:  L1"
## [1] "Loci significantly associated with X:  L90"
## [1] "Loci significantly associated with Y:  L1"

For this simulation and cutoff, MSOD (Section 2) detected two loci as potentially under selection - one was a correct true positive detection (L1), while the other was a false positive detection (L90). Using MSR (Section 3), we found that our false positive was significantly associated with the spurious “X” predictor, while the true positive detection (L1) was significantly associated with both the driving environmental predictor, “Env”, as well as the spurious “Y” predictor.

Let’s plot the two detected loci and a random neutral locus to visualize the spatial distribution of alleles across the Gabriel graph. In these plots, individuals with no copies of the reference allele (“0”) are purple, indivdiduals with one copy (heterozygotes, coded “1”) are teal, and individuals with two copies (“2”) are yellow.

The association of L1 with habitat is clear (left plot). The correlation of habitat (and allele counts) with the y-axis illustrates why the spurious predictor “Y” is also detected as significantly associated with L1. Note also how individual dispersal (up to 5% of the surface per generation in this simulation) means that individuals with maladapted genotypes will sometimes end up in the wrong habitat type; this is not unusual when selection is moderate and habitat is fragmented.

The distribution of alleles for L90 (center plot) is coincident with the x-axis, illustrating how detection associated with the spurious predictor “X” produced a false positive in this case. Lastly, the clearly neutral L2 locus (right plot) shows no association with either habitat, latitude, or longitude.

Conclusions

In summary, Moran spectral outlier detection (MSOD) can be used to reliably identify outlier loci based on their unusual spatial signature, without reference to environmental data. Follow-up analyses using Moran spectral randomization (MSR) can be used to identify potential drivers of selection at these candidate loci, while accounting for spatial autocorrelation. These methods can be used on either individual or population-level samples, and are robust to a variety of sampling designs and sample sizes (Wagner et al. 2016).

Contributors

References

Dray S, Legendre P, Peres-Neto PR (2006) Spatial modelling: a comprehensive framework for principal coordinate analysis of neighbour matrices (PCNM). Ecological Modelling, 196, 483-493.

Forester BR, Jones MR, Joost S, Landguth EL, Lasky JR (2016) Detecting spatial genetic signatures of local adaptation in heterogeneous landscapes. Molecular Ecology, 25, 104-120.

Landguth EL, Cushman SA (2010) cdpop: a spatially explicit cost distance population genetics program. Molecular Ecology Resources, 10, 156-161.

Wagner HH, Chávez-Pesqueira M, Forester BR (In press) Spatial detection of outlier loci with Moran eigenvector maps (MEM). Molecular Ecology Resources.

Session Information

## ─ Session info ───────────────────────────────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 3.6.1 (2019-07-05)
##  os       Debian GNU/Linux 9 (stretch)
##  system   x86_64, linux-gnu           
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  ctype    en_US.UTF-8                 
##  tz       Etc/UTC                     
##  date     2019-09-16                  
## 
## ─ Packages ───────────────────────────────────────────────────────────────────────────────────────
##  package      * version   date       lib source        
##  ade4           1.7-13    2018-08-31 [1] CRAN (R 3.6.1)
##  adegenet       2.1.1     2018-02-02 [1] CRAN (R 3.6.1)
##  adegraphics    1.0-15    2018-12-18 [1] CRAN (R 3.6.1)
##  adephylo       1.1-11    2017-12-18 [1] CRAN (R 3.6.1)
##  adespatial   * 0.3-7     2019-06-04 [1] CRAN (R 3.6.1)
##  ape            5.3       2019-03-17 [1] CRAN (R 3.6.1)
##  assertthat     0.2.1     2019-03-21 [1] CRAN (R 3.6.1)
##  backports      1.1.4     2019-04-10 [1] CRAN (R 3.6.1)
##  boot           1.3-22    2019-04-02 [2] CRAN (R 3.6.1)
##  callr          3.3.1     2019-07-18 [1] CRAN (R 3.6.1)
##  class          7.3-15    2019-01-01 [2] CRAN (R 3.6.1)
##  classInt       0.4-1     2019-08-06 [1] CRAN (R 3.6.1)
##  cli            1.1.0     2019-03-19 [1] CRAN (R 3.6.1)
##  cluster        2.1.0     2019-06-19 [2] CRAN (R 3.6.1)
##  coda           0.19-3    2019-07-05 [1] CRAN (R 3.6.1)
##  codetools      0.2-16    2018-12-24 [2] CRAN (R 3.6.1)
##  colorspace     1.4-1     2019-03-18 [1] CRAN (R 3.6.1)
##  crayon         1.3.4     2017-09-16 [1] CRAN (R 3.6.1)
##  DBI            1.0.0     2018-05-02 [1] CRAN (R 3.6.1)
##  deldir         0.1-23    2019-07-31 [1] CRAN (R 3.6.1)
##  desc           1.2.0     2018-05-01 [1] CRAN (R 3.6.1)
##  devtools       2.2.0     2019-09-07 [1] CRAN (R 3.6.1)
##  digest         0.6.20    2019-07-04 [1] CRAN (R 3.6.1)
##  dplyr          0.8.3     2019-07-04 [1] CRAN (R 3.6.1)
##  DT             0.8       2019-08-07 [1] CRAN (R 3.6.1)
##  e1071          1.7-2     2019-06-05 [1] CRAN (R 3.6.1)
##  ellipsis       0.2.0.1   2019-07-02 [1] CRAN (R 3.6.1)
##  evaluate       0.14      2019-05-28 [1] CRAN (R 3.6.1)
##  expm           0.999-4   2019-03-21 [1] CRAN (R 3.6.1)
##  fs             1.3.1     2019-05-06 [1] CRAN (R 3.6.1)
##  gdata          2.18.0    2017-06-06 [1] CRAN (R 3.6.1)
##  ggplot2        3.2.1     2019-08-10 [1] CRAN (R 3.6.1)
##  glue           1.3.1     2019-03-12 [1] CRAN (R 3.6.1)
##  gmodels        2.18.1    2018-06-25 [1] CRAN (R 3.6.1)
##  gridExtra      2.3       2017-09-09 [1] CRAN (R 3.6.1)
##  gtable         0.3.0     2019-03-25 [1] CRAN (R 3.6.1)
##  gtools         3.8.1     2018-06-26 [1] CRAN (R 3.6.1)
##  hms            0.5.1     2019-08-23 [1] CRAN (R 3.6.1)
##  htmltools      0.3.6     2017-04-28 [1] CRAN (R 3.6.1)
##  htmlwidgets    1.3       2018-09-30 [1] CRAN (R 3.6.1)
##  httpuv         1.5.2     2019-09-11 [1] CRAN (R 3.6.1)
##  httr           1.4.1     2019-08-05 [1] CRAN (R 3.6.1)
##  igraph         1.2.4.1   2019-04-22 [1] CRAN (R 3.6.1)
##  KernSmooth     2.23-15   2015-06-29 [2] CRAN (R 3.6.1)
##  knitr          1.24      2019-08-08 [1] CRAN (R 3.6.1)
##  later          0.8.0     2019-02-11 [1] CRAN (R 3.6.1)
##  lattice        0.20-38   2018-11-04 [2] CRAN (R 3.6.1)
##  latticeExtra   0.6-28    2016-02-09 [1] CRAN (R 3.6.1)
##  lazyeval       0.2.2     2019-03-15 [1] CRAN (R 3.6.1)
##  LearnBayes     2.15.1    2018-03-18 [1] CRAN (R 3.6.1)
##  lifecycle      0.1.0     2019-08-01 [1] CRAN (R 3.6.1)
##  magrittr       1.5       2014-11-22 [1] CRAN (R 3.6.1)
##  MASS           7.3-51.4  2019-03-31 [2] CRAN (R 3.6.1)
##  Matrix         1.2-17    2019-03-22 [2] CRAN (R 3.6.1)
##  memoise        1.1.0     2017-04-21 [1] CRAN (R 3.6.1)
##  mgcv           1.8-28    2019-03-21 [2] CRAN (R 3.6.1)
##  mime           0.7       2019-06-11 [1] CRAN (R 3.6.1)
##  munsell        0.5.0     2018-06-12 [1] CRAN (R 3.6.1)
##  nlme           3.1-140   2019-05-12 [2] CRAN (R 3.6.1)
##  permute        0.9-5     2019-03-12 [1] CRAN (R 3.6.1)
##  phylobase      0.8.6     2019-02-02 [1] CRAN (R 3.6.1)
##  pillar         1.4.2     2019-06-29 [1] CRAN (R 3.6.1)
##  pkgbuild       1.0.5     2019-08-26 [1] CRAN (R 3.6.1)
##  pkgconfig      2.0.2     2018-08-16 [1] CRAN (R 3.6.1)
##  pkgload        1.0.2     2018-10-29 [1] CRAN (R 3.6.1)
##  plyr           1.8.4     2016-06-08 [1] CRAN (R 3.6.1)
##  prettyunits    1.0.2     2015-07-13 [1] CRAN (R 3.6.1)
##  processx       3.4.1     2019-07-18 [1] CRAN (R 3.6.1)
##  progress       1.2.2     2019-05-16 [1] CRAN (R 3.6.1)
##  promises       1.0.1     2018-04-13 [1] CRAN (R 3.6.1)
##  ps             1.3.0     2018-12-21 [1] CRAN (R 3.6.1)
##  purrr          0.3.2     2019-03-15 [1] CRAN (R 3.6.1)
##  R6             2.4.0     2019-02-14 [1] CRAN (R 3.6.1)
##  raster       * 3.0-2     2019-08-22 [1] CRAN (R 3.6.1)
##  RColorBrewer   1.1-2     2014-12-07 [1] CRAN (R 3.6.1)
##  Rcpp           1.0.2     2019-07-25 [1] CRAN (R 3.6.1)
##  remotes        2.1.0     2019-06-24 [1] CRAN (R 3.6.1)
##  reshape2       1.4.3     2017-12-11 [1] CRAN (R 3.6.1)
##  rlang          0.4.0     2019-06-25 [1] CRAN (R 3.6.1)
##  rmarkdown      1.15      2019-08-21 [1] CRAN (R 3.6.1)
##  rncl           0.8.3     2018-07-27 [1] CRAN (R 3.6.1)
##  RNeXML         2.3.0     2019-01-24 [1] CRAN (R 3.6.1)
##  rprojroot      1.3-2     2018-01-03 [1] CRAN (R 3.6.1)
##  scales         1.0.0     2018-08-09 [1] CRAN (R 3.6.1)
##  seqinr         3.6-1     2019-09-07 [1] CRAN (R 3.6.1)
##  sessioninfo    1.1.1     2018-11-05 [1] CRAN (R 3.6.1)
##  sf           * 0.7-7     2019-07-24 [1] CRAN (R 3.6.1)
##  shiny          1.3.2     2019-04-22 [1] CRAN (R 3.6.1)
##  sp           * 1.3-1     2018-06-05 [1] CRAN (R 3.6.1)
##  spData       * 0.3.0     2019-01-07 [1] CRAN (R 3.6.1)
##  spdep        * 1.1-2     2019-04-05 [1] CRAN (R 3.6.1)
##  stringi        1.4.3     2019-03-12 [1] CRAN (R 3.6.1)
##  stringr        1.4.0     2019-02-10 [1] CRAN (R 3.6.1)
##  testthat       2.2.1     2019-07-25 [1] CRAN (R 3.6.1)
##  tibble         2.1.3     2019-06-06 [1] CRAN (R 3.6.1)
##  tidyr          1.0.0     2019-09-11 [1] CRAN (R 3.6.1)
##  tidyselect     0.2.5     2018-10-11 [1] CRAN (R 3.6.1)
##  units          0.6-4     2019-08-22 [1] CRAN (R 3.6.1)
##  usethis        1.5.1     2019-07-04 [1] CRAN (R 3.6.1)
##  uuid           0.1-2     2015-07-28 [1] CRAN (R 3.6.1)
##  vctrs          0.2.0     2019-07-05 [1] CRAN (R 3.6.1)
##  vegan          2.5-6     2019-09-01 [1] CRAN (R 3.6.1)
##  viridis      * 0.5.1     2018-03-29 [1] CRAN (R 3.6.1)
##  viridisLite  * 0.3.0     2018-02-01 [1] CRAN (R 3.6.1)
##  withr          2.1.2     2018-03-15 [1] CRAN (R 3.6.1)
##  xfun           0.9       2019-08-21 [1] CRAN (R 3.6.1)
##  XML            3.98-1.20 2019-06-06 [1] CRAN (R 3.6.1)
##  xml2           1.2.2     2019-08-09 [1] CRAN (R 3.6.1)
##  xtable         1.8-4     2019-04-21 [1] CRAN (R 3.6.1)
##  yaml           2.2.0     2018-07-25 [1] CRAN (R 3.6.1)
##  zeallot        0.1.0     2018-01-28 [1] CRAN (R 3.6.1)
## 
## [1] /usr/local/lib/R/site-library
## [2] /usr/local/lib/R/library