Tidy Population Genetics

We provide a tidy grammar of population genetics, facilitating the manipulation and analysis of data on biallelic single nucleotide polymorphisms (SNPs). 'tidypopgen' scales to very large genetic datasets by storing genotypes on disk, and performing operations on them in chunks, without ever loading all data in memory. The full functionalities of the package are described in Carter et al. (2025) .


tidypopgen

R-CMD-check codecov CRAN status CRAN downloads

The goal of tidypopgen is to provide a tidy grammar of population genetics, facilitating the manipulation and analysis of biallelic single nucleotide polymorphisms (SNPs). tidypopgen scales to very large genetic datasets by storing genotypes on disk, and performing operations on them in chunks, without ever loading all data in memory. The full functionalities of the package are described in Carter et al. (2025). Please cite this paper if you use tidypopgen in your research.

Installation

You can install the release version of tidypopgen from CRAN:

install.packages("tidypopgen")

You can install the latest development version directly from r-universe (recommended):

install.packages('tidypopgen', repos = c('https://evolecolgroup.r-universe.dev',
                 'https://cloud.r-project.org'))

Alternatively, you can install tidypopgenusing devtools (but you might need to set up your development environment, which can be a bit more complex):

install.packages("pak")
pak::pak("EvolEcolGroup/tidypopgen@dev")

Examples

There are several vignettes designed to teach you how to use tidypopgen. A short introduction to the package is available in the 'introduction' vignette. A more detailed and technical description of the grammar of population genetics, explaining how to manipulate individuals and loci, is available in the 'grammar' vignette.

The 'quality control' vignette illustrates the tidypopgen functions that help running a full QC of a dataset before analysis.

The 'population genetic analysis' vignette provides a fully annotated example of how to run various population genetics analyses with tidypopgen.

We also provide a 'PLINK cheatsheet' aimed at translating common tasks performed in PLINK into tidypopgen commands.

There is also an article showing how to manage aDNA samples that have been coded as pseudohaploids, including how to project ancient DNA data onto a PCA fitted to modern data and prepare data for admixtools: 'aDNA pseudohaploids' article.

Other articles cover specific analyses:

  • the 'spatial interpolation' article interpolates principal component scores across geographic space, using the integration of tidypopgen with sf;
  • the 'runs of homozygosity' article finds and summarises the runs of homozygosity of the HGDP, with the same algorithm and results as PLINK 1.9 (--homozyg);
  • the 'site frequency spectra' article computes single and joint site frequency spectra, dealing with missing genotypes by projection (as easySFS does), as the input of demographic inference with dadi, moments or fastsimcoal2.

The original version of tidypopgen relied on imputing missing genotypes for a number of analyses, but the latest version can now handle missing genotypes in most workflows, including PCA, Fst and site frequency spectra. See the 'missing genotypes' article for an explanation of why it is better to keep missing genotypes rather than imputing them, and how to do so in tidypopgen.

Finally, tidypopgen is fast and can handle large datasets easily. See a 'benchmark' article using the HGDP, a dataset of over 1000 individuals typed for 650k SNPs. We can load the data, clean it, run imputation, PCA and pairwise Fst among 51 populations in less than 20 seconds on a powerful desktop (and less than a minute on a laptop). For even larger datasets, the 'large VCFs' article merges many whole-genome VCF files into a single gen_tibble, storing the names of the loci as integers to save memory, and the 'large RAD-seq datasets' article handles de novo RAD-seq assemblies, in which every RAD locus is its own chromosome, by storing the chromosomes as integers too.

When something does not work

If something does not work, check the issues on GitHub to see whether the problem has already been reported. If not, feel free to create an new issue. Please make sure you have updated to the latest version of tidypopgen on r-universe/Github, as well as updating all other packages on your system, and provide a reproducible example for the developers to investigate the problem. Ideally, try to create a minimalistic dataset that reproduces the error, as it will be much easier (and thus faster!) for the developers to track down the problem.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("tidypopgen")

1.1.0 by Andrea Manica, 6 days ago


https://github.com/EvolEcolGroup/tidypopgen, https://evolecolgroup.github.io/tidypopgen/


Report a bug at https://github.com/EvolEcolGroup/tidypopgen/issues


Browse source code at https://github.com/cran/tidypopgen


Authors: Evie Carter [aut] , Eirlys Tysall [aut] , Andrea Manica [aut, cre, cph] (ORCID: , Max Carter-Brown [ctb] , Jason Hodgson [ctb] , Christopher Chang [ctb, cph] (Author of PLINK 1.90: Hardy-Weinberg exact test code used in loci_hwe() and qc_report_loci(); runs of homozygosity algorithm followed by windows_indiv_roh(); LD pruning algorithm followed by loci_ld_prune()) , Shaun Purcell [ctb, cph] (Author of PLINK 1.90: Hardy-Weinberg exact test code used in loci_hwe() and qc_report_loci(); runs of homozygosity algorithm followed by windows_indiv_roh(); LD pruning algorithm followed by loci_ld_prune()) , Henrik Bengtsson [ctb, cph] (Author of countLines() in R.utils , modified for .vcf in count_vcf_variants()) , Jan Wigginton [ctb, cph] (Author of the original Hardy-Weinberg exact test code (SNPHWE) , on which the PLINK 1.90 code used in loci_hwe() is based) , Florian Privé [ctb, cph] (Author of bigsnpr and bigstatsr , code from which is adapted in read_packedancestry_to_fbm() , gt_as_plink() , predict.gt_pca() , autoplot.gt_dapc() and loci_ld_clump()) , Jérôme Goudet [ctb, cph] (Author of hierfstat , code from which is adapted in pop_global_stats() , pop_het_obs() , pop_het_exp() and indiv_inbreeding()) , Robert Maier [ctb, cph] (Author of admixtools , code from which is adapted in gt_extract_f2()) , Ryan Gutenkunst [ctb, cph] (Author of dadi , whose Spectrum class is translated to R in the tests of the site frequency spectra) , Isaac Overcast [ctb, cph] (Author of easySFS , translated to R in the tests of the site frequency spectra)


Documentation:   PDF Manual  


GPL (>= 3) license


Imports bigparallelr, bigsnpr, bigstatsr, data.table, foreach, generics, lifecycle, ggplot2, methods, MASS, patchwork, runner, rlang, RSpectra, sf, stats, tidyselect, tidyr, utils, Rcpp, vctrs, gtable

Depends on dplyr, tibble

Suggests adegenet, admixtools, ape, broom, hexbin, hierfstat, knitr, detectRUNS, LEA, pcadapt, RhpcBLASctl, rmarkdown, rnaturalearth, rnaturalearthdata, readr, reticulate, testthat, vcfR, withr, xgboost, spelling

Linking to Rcpp, RcppEigen, bigstatsr, rmio


Suggested by dartR.base.


See at CRAN