Learn R Programming

wingen

https://img.shields.io/badge/license-MIT-blue

Generate continuous maps of genetic diversity using moving windows with options for rarefaction, interpolation, and masking.

Citation

Please cite the original Bishop et al. (2023) paper if you use this package:

Bishop, A. P., Chambers, E. A., & Wang, I. J. (2023). Generating continuous maps of genetic diversity using moving windows. Methods in Ecology and Evolution, 14, 1175–1181. http://doi.org/10.1111/2041-210X.14090

Checkout our Methods blog post about wingen for a quick overview of the package and its uses.

Installation

Install wingen from GitHub with:

# install.packages("devtools")
devtools::install_github("AnushaPB/wingen", build_vignettes = TRUE)

Example

The following example demonstrates the basic functionality of wingen using a small subset (100 variant loci x 100 samples) of the simulated data from Bishop et al. (2023).

library(wingen)

# Load ggplot for plotting
library(ggplot2)

# Load example data
load_middle_earth_ex()

The core function of this package is window_gd(), which takes as inputs a vcfR object (or a path to a .vcf file), sample coordinates (as a data.frame, matrix, or sf object), and a raster layer (as a SpatRaster or RasterLayer) which the moving window will slide across. Users can control the genetic diversity statistic that is calculated (stat), the window dimensions (wdim), the aggregation factor to use on the raster (fact), whether to perform rarefaction (rarify), and other aspects of the moving window calculations. Additional arguments for this function are described in the vignette and function documentation.

# Run moving window calculations of pi with rarefaction
wgd <- window_gd(lotr_vcf,
  lotr_coords,
  lotr_lyr,
  stat = "pi",
  wdim = 7,
  fact = 3,
  rarify = TRUE
)

# Use ggplot_gd() to plot the genetic diversity layer and ggplot_count() to plot the sample counts layer
ggplot_gd(wgd) +
  ggtitle("Moving window pi")

ggplot_count(wgd) +
  ggtitle("Moving window sample counts")

Next, the output from window_gd() can be interpolated using kriging with the krig_gd() function.

# Krige genetic diversity (disaggregate grid to project across a smoother final surface)
kgd <- krig_gd(wgd, lotr_lyr, index = 1, disagg_grd = 2)

Finally, the output from krig_gd() (or window_gd()) can be masked to exclude areas that fall outside of the study area or that were undersampled.

# Mask results that fall outside of the "range"
mgd <- mask_gd(kgd, lotr_range)
# Plot results
ggplot_gd(kgd) +
  ggtitle("Kriged pi")

ggplot_gd(mgd) +
  ggtitle("Masked pi")

For an extended walk through, see the package vignette:

vignette("wingen-vignette")

A pdf of the vignette can also be found here

Example analyses from Bishop et al. (2023) can be found in the paperex directory.

Copy Link

Version

Install

install.packages('wingen')

Monthly Downloads

537

Version

2.1.1

License

MIT + file LICENSE

Maintainer

Anusha Bishop

Last Published

February 27th, 2024

Functions in wingen (2.1.1)

circle_gd

Create a moving window map of genetic diversity using a circle window
mini_vcf_NA

Mini middle earth example vcf with NA values
plot_count

Plot moving window map of sample counts
circle_general

General function for making circular moving window maps
get_geodist

Get a matrix of geographic distances for circle_gd
load_mini_ex

Mini middle earth example
get_resdist

Get a matrix of resistance distances for resist_gd
lotr_range

Middle earth example range polygon
lotr_vcf

Middle earth example vcf
resist_gd

Create a moving window map of genetic diversity based on resistance
resist_general

General function for making resistance-based maps
mini_lyr

Mini middle earth example raster
ggplot_count

Plot moving window map of sample counts
plot_gd

Plot moving window map of genetic diversity
ggplot_gd

Plot moving window map of genetic diversity
mini_vcf

Mini middle earth example vcf
window_general

General function for making moving window maps
preview_gd

Preview moving window and sample counts
wingen-package

wingen: Continuous Mapping of Genetic Diversity
krig_gd

Krige moving window maps
mask_gd

Mask moving window maps
lotr_coords

Middle earth example coordinates
mini_coords

Mini middle earth example coordinates
vcf_to_dosage

Convert a vcf to a dosage matrix
window_gd

Create a moving window map of genetic diversity
lotr_lyr

Middle earth example raster
coords_to_raster

Create a raster from coordinates
load_middle_earth_ex

Middle earth example