7  Genetic Distance & Relatedness

This chapter covers inter-individual and inter-population distance estimation, pairwise relatedness, and spatial autocorrelation analysis.

library( gstudio )
library( dplyr )
library( ggplot2 )
data( arapat )
data( cornus_florida )

7.1 Genetic Distance

The genetic_distance() function computes distance matrices from genetic data. Some modes operate among individuals, others among populations (strata).

7.1.1 Available Distance Metrics

Mode Level Description
"AMOVA" Individual Inter-individual AMOVA distance
"Bray" Individual Proportion of shared alleles
"Euclidean" Population Euclidean frequency distance
"cGD" Population Conditional genetic distance
"dcGD" Population Directional conditional genetic distance; asymmetric
"Nei" Population Nei’s genetic distance (1978)
"Dps" Population Shared allele distance (1 - Ps)
"Jaccard" Population Jaccard set dissimilarity

7.1.2 Individual-Level Distances

AMOVA distance computes inter-individual genetic distances:

# The first 20 individuals, for a manageable example
D_amova <- arapat |>
  slice( 1:20 ) |>
  genetic_distance( mode = "AMOVA" )

dim( D_amova )
[1] 20 20
D_amova[1:5, 1:5]
          [,1]     [,2]     [,3]     [,4]     [,5]
[1,]  0.000000 7.272727 9.090909 3.636364 10.90909
[2,]  7.272727 0.000000 1.000000 2.000000  5.00000
[3,]  9.090909 1.000000 0.000000 3.000000  2.00000
[4,]  3.636364 2.000000 3.000000 0.000000  5.00000
[5,] 10.909091 5.000000 2.000000 5.000000  0.00000

7.1.3 Population-Level Distances

Population-level distances require a stratum column:

D_nei <- arapat |>
  genetic_distance( stratum = "Species", mode = "Nei" )
D_nei
               Cape  Mainland Peninsula
Cape      0.0000000 0.7267072 0.9233605
Mainland  0.7267072 0.0000000 0.6812596
Peninsula 0.9233605 0.6812596 0.0000000
D_cgd <- arapat |>
  genetic_distance( stratum = "Species", mode = "cGD" )
Warning in popgraph(x = mv, groups = factor(as.character(x[[stratum]]))): 1
variables are collinear and being dropped from the discriminant rotation.
D_cgd
              Cape  Mainland Peninsula
Cape      0.000000  9.913083  7.136108
Mainland  9.913083  0.000000 16.490315
Peninsula 7.136108 16.490315  0.000000

Directional conditional genetic distance (dcGD) splits each Population Graph edge’s cGD into two directed shares using the genetic-gravity neighbourhood weights, so the matrix is asymmetric: [i, j] is the distance from i to j, shorter in the direction of gene flow. The bandwidth multiplier is set with gamma (default 0.5); see vignette("genetic_gravity").

D_dcgd <- arapat |>
  genetic_distance( stratum = "Species", mode = "dcGD" )
Warning in popgraph(x = mv, groups = factor(as.character(x[[stratum]]))): 1
variables are collinear and being dropped from the discriminant rotation.
D_dcgd
              Cape Mainland Peninsula
Cape      0.000000 1.936519  3.214012
Mainland  7.976564 0.000000 11.190575
Peninsula 3.922096 4.155494  0.000000
D_euc <- arapat |>
  genetic_distance( stratum = "Species", mode = "Euclidean" )
Multilocus estimates of Euclidean distance are assumed to be additive.
D_euc
              Cape Mainland Peninsula
Cape      0.000000 2.418731  2.494205
Mainland  2.418731 0.000000  2.140258
Peninsula 2.494205 2.140258  0.000000

7.2 Genetic Relatedness

The genetic_relatedness() function computes pairwise relatedness among individuals.

7.2.1 Available Relatedness Estimators

Mode Description
"Nason" Nason’s kinship coefficient (Fij)
"LynchRitland" Lynch & Ritland estimator
"Ritland" Ritland estimator

7.2.2 Basic Usage

The Nason estimator divides by the polymorphic index (genetic_diversity(mode = "Pe")), so monomorphic loci (where Pe = 0) will produce NaN. In the arapat dataset, LTRS and ATPS can be monomorphic in small subsets. To avoid this, restrict the data to polymorphic loci:

# Use loci that are polymorphic in this subset
R <- arapat |>
  slice( 1:10 ) |>
  select( Species, WNT, EN, EF, ZMP, AML, MP20 ) |>
  genetic_relatedness( mode = "Nason" )
Some of your loci are missing, Fij will treat these as loci with all alleles with likelihood equal to the population allele frequency.
Some of your loci are missing, Fij will treat these as loci with all alleles with likelihood equal to the population allele frequency.
Some of your loci are missing, Fij will treat these as loci with all alleles with likelihood equal to the population allele frequency.
dim( R )
[1] 10 10
round( R[1:5, 1:5], 4 )
        [,1]    [,2]    [,3]    [,4]    [,5]
[1,]  1.0000 -0.5978 -0.8941 -0.4274 -1.0200
[2,] -0.5978  1.0000  0.6948  0.4308  0.2256
[3,] -0.8941  0.6948  1.0000  0.1345  1.1145
[4,] -0.4274  0.4308  0.1345  1.0000 -0.1038
[5,] -1.0200  0.2256  1.1145 -0.1038  1.0000

7.2.3 Single-Locus Relatedness

Relatedness at a single locus uses the same function, with loci =:

r_nason <- arapat |>
  slice( 1:10 ) |>
  genetic_relatedness( loci = "EN", mode = "Nason" )

round( r_nason[1:5, 1:5], 4 )
        [,1]    [,2]    [,3]    [,4]    [,5]
[1,]  1.0000 -0.9953 -0.9953  0.0271  0.0271
[2,] -0.9953  1.0000  0.9220 -0.1153 -0.1153
[3,] -0.9953  0.9220  1.0000 -0.1153 -0.1153
[4,]  0.0271 -0.1153 -0.1153  1.0000  0.2329
[5,]  0.0271 -0.1153 -0.1153  0.2329  1.0000

7.3 Spatial Autocorrelation

The genetic_autocorrelation() function performs the spatial autocorrelation analysis of Smouse & Peakall (1999). It requires two square distance matrices — one physical, one genetic — among the same individuals, and a vector of distance bin boundaries. Here we use the adult flowering dogwoods (Cornus florida, OffID == 0), which are mapped on a projected X/Y grid, so the physical distance is Euclidean. Using the individual ID as the stratum gives strata_distance() one point per tree:

adults <- cornus_florida |>
  filter( OffID == 0 )

# Physical distance among trees
P <- adults |>
  strata_distance( stratum = "ID",
                   longitude = "X",
                   latitude = "Y",
                   mode = "Euclidean" )

# Genetic distance among the same trees
G <- adults |>
  genetic_distance( mode = "AMOVA" )

# Distance classes: 200 units wide to 1200,
# then one class for the few long pairs
bins <- c( seq( 0, 1200, by = 200 ), 2600 )

set.seed( 1 )
result <- genetic_autocorrelation( P, G, bins = bins, perms = 999 )
result
  From   To           R  N p.value
1    0  200  0.11071023 31   0.001
2  200  400 -0.07586309 54   0.869
3  400  600 -0.11219847 26   0.969
4  600  800 -0.07495836 13   0.684
5  800 1000 -0.04885903 30   0.491
6 1000 1200 -0.04729521 27   0.493
7 1200 2600 -0.07681157 50   0.914

Each row is a distance class, from From up to (not including) To, with the autocorrelation coefficient R, the number of pairs of trees in the class N, and the one-sided permutation p.value for positive autocorrelation. The result plots directly:

ggplot( result, aes( x = To, y = R ) ) +
  geom_hline( yintercept = 0, linetype = "dashed" ) +
  geom_line() +
  geom_point( aes( colour = p.value < 0.05 ), size = 2.5 ) +
  scale_colour_manual( values = c( `TRUE` = "firebrick",
                                    `FALSE` = "grey40" ),
                       name = "p < 0.05" ) +
  labs( x = "Distance class (upper bound, map units)",
        y = "Autocorrelation (r)" )

7.4 Isolation by Distance

genetic_isolation() tests whether genetic distance among strata grows with their separation, starting from the genotypes. The default, mode = "IBD", correlates Nei’s genetic distance (change it with genetic =) with great-circle distance between strata and tests it with a Mantel permutation test:

set.seed( 1 )
arapat |>
  genetic_isolation( stratum = "Population", nperm = 999 )

    Isolation by distance (Nei genetic distance; Mantel permutation test)

data:  arapat by Population, and great-circle distance (km)
r = 0.56232, pairs = 741, nperm = 999, p-value = 0.001
alternative hypothesis: true r is greater than 0
sample estimates:
         R2       slope 
0.316201330 0.001337526 

mode = "IBGD" uses the conditional genetic distance through a Population Graph instead, and mode = "dcGD" asks whether a directional model fits better, given an axis (see Chapter 9). Separation can also come from your own matrix (spatial =, e.g. resistance distances) or positions along an axis (axis =). If you already have a Population Graph, ibgd() works on it directly.

7.5 Strata Distances

The strata_coordinates() function extracts per-stratum centroid coordinates, and strata_distance() computes physical (geographic) distances between them:

coords <- arapat |>
  strata_coordinates( stratum = "Species",
                      longitude = "Longitude",
                      latitude = "Latitude" )
coords
    Stratum Longitude Latitude
3 Peninsula -111.9226 26.18956
1      Cape -110.5805 24.25405
2  Mainland -109.6759 26.97435
coords |>
  strata_distance()
          Peninsula     Cape Mainland
Peninsula    0.0000 254.0525 239.8471
Cape       254.0525   0.0000 315.7868
Mainland   239.8471 315.7868   0.0000

strata_distance() also takes the raw data and finds the coordinates itself, so these are the same:

arapat |> strata_coordinates() |> strata_distance()
arapat |> strata_distance()

7.6 Without Pipes

D_amova <- genetic_distance( arapat[1:20, ], mode = "AMOVA" )
D_nei   <- genetic_distance( arapat, stratum = "Species", mode = "Nei" )
D_dcgd  <- genetic_distance( arapat,
                             stratum = "Species",
                             mode = "dcGD",
                             gamma = 0.5 )
loci    <- c( "Species", "WNT", "EN", "EF", "ZMP", "AML", "MP20" )
R       <- genetic_relatedness( arapat[1:10, loci],
                                mode = "Nason" )
coords  <- strata_coordinates( arapat, stratum = "Species" )
strata_distance( coords )
adults <- cornus_florida[cornus_florida$OffID == 0, ]
P      <- strata_distance( adults,
                           stratum = "ID",
                           longitude = "X",
                           latitude = "Y",
                           mode = "Euclidean" )
G      <- genetic_distance( adults, mode = "AMOVA" )
genetic_autocorrelation( P,
                         G,
                         bins = c( seq( 0, 1200, by = 200 ), 2600 ),
                         perms = 999 )
genetic_isolation( arapat,
                   stratum = "Population",
                   mode = "IBD",
                   nperm = 999 )