PCAngsd: Difference between revisions

From software
Jump to navigation Jump to search
No edit summary
No edit summary
 
(27 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This page contains information about the program PCAngsd, which estimates the covariance matrix for low depth NGS data in an iterative procedure based on genotype likelihoods. Based on population structure inference, PCAngsd is able to estimate individual allele frequencies. These individual allele frequencies can be used in various population genetic methods for heterogeneous populations, such that PCAngsd can perform PCA (estimate covariance matrix), call genotypes, estimate individual admixture proportions, estimate inbreeding coefficients (per-individual and per-site) and perform a genome selection scan using principal components. The entire program is written in Python 2.7 and is multithreaded to take advantage of several CPUs.


[[File:Pcangsd_admix.gif|frame]]
 
 
PCAngsd is a program that estimates the covariance matrix and individual allele frequencies for low-depth next-generation sequencing (NGS) data in structured/heterogeneous populations using principal component analysis (PCA) to perform multiple population genetic analyses using genotype likelihoods. Since version 0.98, PCAngsd was re-written to be based on Cython for computational bottlenecks and parallelization.
 
The main method was published in 2018 and can be found here: [https://www.genetics.org/content/210/2/719]
 
The HWE test was published in 2019 and can be found here: [https://onlinelibrary.wiley.com/doi/abs/10.1111/1755-0998.13019]
[[File:Pcangsd_admix3.gif|frame]]
 
[[File:Pcangsd_pca.png|thumb|400px|Simulated low depth NGS data of 3 populations]]
[[File:Pcangsd_pca.png|thumb|400px|Simulated low depth NGS data of 3 populations]]




=Download=
=Overview=
Framework for analyzing low-depth next-generation sequencing (NGS) data in heterogeneous/structured populations using principal component analysis (PCA). Population structure is inferred by estimating individual allele frequencies in an iterative approach using a truncated SVD model. The covariance matrix is estimated using the estimated individual allele frequencies as prior information for the unobserved genotypes in low-depth NGS data.


The program can be downloaded from Github:
The estimated individual allele frequencies can further be used to account for population structure in other probabilistic methods. PCAngsd can perform the following analyses:
https://github.com/Rosemeis/pcangsd
*Covariance matrix
*Admixture estimations
*Inbreeding coefficients (both per-individual and per-site)
*HWE test
*Genome-wide selection scan
*Genotype calling
*Estimate NJ tree of samples


Latest release of PCAngsd: 0.8
Older versions of PCAngsd can be found here [https://github.com/Rosemeis/pcangsd/releases/].


=Download and Installation=
PCAngsd should work on all platforms meeting the requirements but server-side usage is highly recommended. Installation has only been tested on Linux systems.
Get PCAngsd and build
<pre>
<pre>
git clone https://github.com/Rosemeis/pcangsd.git;
git clone https://github.com/Rosemeis/pcangsd.git
cd pcangsd/
cd pcangsd/
python setup.py build_ext --inplace
</pre>
</pre>
Install dependencies:


The following Python packages are needed to run PCAngsd:
The required set of Python packages are easily installed using the pip command and the 'requirements.txt file' included in the 'pcangsd' folder.
'''numpy''', '''scipy''', '''pandas''', '''sklearn''' and '''numba'''.
 
The packages and their dependencies can easily be installed using the following command inside the pcangsd folder:


<pre>
<pre>
pip install --user -r python_packages.txt
pip install --user -r requirements.txt
</pre>
</pre>


PCAngsd should work on all platforms meeting the requirements but server-side usage is highly recommended.
=Quick start=


 
PCAngsd is used by running the main caller file pcangsd.py. To see all available options use the following command:
==Quick start==
<pre>
<pre>
# See all options in PCAngsd
python pcangsd.py -h
python pcangsd.py -h


# Only estimate covariance matrix using 10 threads
# Genotype likelihoods using 64 threads
python pcangsd.py -beagle test.beagle.gz -n 100 -o test -threads 10
python pcangsd.py -beagle input.beagle.gz -out output -threads 64


# Estimate covariance matrix and individual admixture proportions
# PLINK files (using file-prefix, *.bed, *.bim, *.fam)
python pcangsd.py -beagle test.beagle.gz -n 100 -admix -o test -threads 10
python pcangsd.py -beagle input.plink -out output -threads 64
</pre>


# Estimate covariance matrix and inbreeding coefficients
PCAngsd accepts either genotype likelihoods in Beagle format or PLINK genotype files. Beagle files can be generated from BAM files using [http://popgen.dk/angsd ANGSD]. For inference of population structure in genotype data with non-random missigness, we recommend our [http://www.popgen.dk/software/index.php/EMU EMU] software that performs accelerated EM-PCA, however with fewer functionalities than PCAngsd (#soon).
python pcangsd.py -beagle test.beagle.gz -n 100 -inbreed 1 -o test -threads 10


# Estimate covariance matrix and perform selection scan
PCAngsd will mostly output files in binary Numpy format (.npy) with a few exceptions. In order to read files in python:
python pcangsd.py -beagle test.beagle.gz -n 100 -selection 1 -o test -threads 10
<pre>
import numpy as np
C = np.genfromtxt("output.cov") # Reads in estimated covariance matrix (text)
D = np.load("output.selection.npy") # Reads PC based selection statistics
</pre>
</pre>


=Input=
R can also read Numpy matrices using the "RcppCNPy" R library:
The only input PCAngsd needs and accepts are genotype likelihoods in [http://faculty.washington.edu/browning/beagle/beagle.html Beagle] format. [http://popgen.dk/angsd ANGSD] can be easily be used to compute genotype likelihoods and output them in the required Beagle format.
<pre>
library(RcppCNPy)
C <- as.matrix(read.table("output.cov")) # Reads in estimated covariance matrix
D <- npyLoad("output.selection.npy") # Reads PC based selection statistics
</pre>
 
An example of generating genotype likelihoods in [http://popgen.dk/angsd ANGSD] and output them in the required Beagle text format.


<pre>
<pre>
./angsd -GL 1 -out genoLikes -nThreads 10 -doGlf 2 -doMajorMinor 1 -doMaf 2 -SNP_pval 1e-6 -bam bam.filelist
./angsd -GL 2 -out input -nThreads 4 -doGlf 2 -doMajorMinor 1 -doMaf 2 -SNP_pval 1e-6 -bam bam.filelist
</pre>
</pre>


See [http://popgen.dk/angsd ANGSD] for more information on how to compute the genotype likelihoods and call SNPs.
=Tutorial=


=Using PCAngsd=
Please refer to the tutorial's page [http://www.popgen.dk/software/index.php/PCAngsdTutorial]


All the different options in PCAngsd are listed here. PCAngsd will always compute the covariance matrix, where it uses principal components to estimate individual allele frequencies in an iterative procedure. The estimated individual allele frequencies will then be used in any of the other specified options of PCAngsd.
=Options=
<pre>
# See all options in PCAngsd
python pcangsd.py -h
</pre>


 
==General usage==
==Estimation of individual allele frequencies==
; -beagle [Beagle file]
; -beagle [Beagle filename] '''Required'''
Input file of genotype likelihoods in Beagle format (.beagle.gz).
Path to file of the genotype likelihoods in Beagle format.
; -filter [Text file]
; -n [int] '''Required'''
Input file of 1's or 0's whether to keep individuals or not.
Specify the number of individuals in dataset.
; -plink [Prefix for binary PLINK files]
; -threads [int]
Path to PLINK files using their ONLY prefix (.bed, .bim, .fam).
Specify the number of thread(s) to use. (Default: 1)
; -plink_error [float]
Incorporate errors into genotypes by specifying rate as argument.
; -minMaf [float]
Minimum minor allele frequency threshold. (Default: 0.05)
; -maf_iter [int]
Maximum number of EM iterations for computing the population allele frequencies (Default: 200).
; -maf_tole [float]
Tolerance value in EM algorithm for population allele frequencies estimation (Default: 1e-4).
; -iter [int]
; -iter [int]
Maximum number of iterations for estimation of individual allele frequencies. (Default: 100)
Maximum number of iterations for estimation of individual allele frequencies (Default: 100).
; -tole [float]
; -tole [float]
Tolerance value for update in estimation of individual allele frequencies. (Default: 5e-5)
Tolerance value for update in estimation of individual allele frequencies (Default: 1e-5).
; -maf [int]
; -hwe [.lrt.npy file]
Maximum number of EM iterations for computing the population allele frequencies. (Default: 200)
Input file of LRT binary file from previous PCAngsd run to filter based on HWE.
; -maf_tole [float]
; -hwe_tole [float]
Tolerance value in EM algorithm for population allele frequencies estimation. (Default: 5e-5)
Threshold for HWE filtering of sites.  
; -e [int]
; -e [int]
Manually select the number of eigenvalues to use in the modelling of individual allele frequencies. (Default: Automatically tested using MAP test)
Manually select the number of eigenvalues to use in the modelling of individual allele frequencies (Default: Automatically tested using MAP test).
; -o [prefix]
; -pi [.pi.npy file]
Set the prefix for all output files created by PCAngsd (Default: "pcangsd").
Load previous estimation of individual allele frequencies to skip covariance estimation.
; -freq_save
; -maf_save
Choose to save estimated allele frequencies (both individual and population).
Choose to save estimated population allele frequencies (Binary). Numpy format (.npy).
; -pi_save
Choose to save estimated individual allele frequencies (Binary). Numpy format (.npy). Can be used with the '-pi' command.
; -dosage_save
Choose to save estimated genotype dosages (Binary). Numpy format (.npy).
; -post_save
Choose to save the posterior genotype probabilities. Beagle format (.beagle).
; -sites_save
; -sites_save
Choose to save the marker IDs after performing filtering using population allele frequencies. Useful for especially selection scans and per-site inbreeding coefficients.
Choose to save the kept sites after filtering which is useful for downstream analysis. Outputs a file of 1's and 0's for keeping a site or not, respectively.
; -threads [int]
Specify the number of thread(s) to use (Default: 1).
; -out [output prefix]
Fileprefix for all output files created by PCAngsd (Default: "pcangsd").
 
==Selection==
Perform PC-based genome-wide selection scans using posterior expectations of the genotypes (genotype dosages):
 
; -selection
Using an extended model of [http://www.cell.com/ajhg/abstract/S0002-9297(16)00003-3 FastPCA]. Performs a genome-wide selection scan along all significant PCs. Outputs the selection statistics and must be converted to p-values by user. Each column reflect the selection statistics along a tested PC and they are χ²-distributed with 1 degree of freedom.
 
; -pcadapt
Using an extended model of [https://onlinelibrary.wiley.com/doi/abs/10.1111/1755-0998.12592 pcadapt]. Performs a genome-wide selection scan across all significant PCs. Outputs the z-scores and must be converted to test statistics with the provided script 'pcangsd/scripts/pcadapt.R', and the test statistics are χ²-distributed with K degree of freedom.


; -snp_weights
Output the SNP weights of the significant K eigenvectors.
==Inbreeding==
; -inbreedSites
Estimate per-site inbreeding coefficients accounting for population structure and perform likehood ratio test for detecting sites deviating from HWE [https://onlinelibrary.wiley.com/doi/abs/10.1111/1755-0998.13019].
; -inbreedSamples
Estimate per-individual inbreeding coefficients accounting for population structure which is based on an extension of [http://genome.cshlp.org/content/23/11/1852.full ngsF] for structured populations.
; -inbreed_iter [int]
Maximum number of iterations for inbreeding EM algorithm. (Default: 200)
; -inbreed_tole [float]
Tolerance value for inbreeding EM algorithm in estimating inbreeding coefficients. (Default: 1e-4)


==Call genotypes==
==Call genotypes==
Genotypes can be called from posterior genotype probabilities incorporating the individual allele frequencies as prior information.
Genotypes can be called from posterior genotype probabilities by incorporating the individual allele frequencies as prior information.


; -geno [float]
; -geno [float]
Call genotypes with defined threshold.
Call genotypes with defined threshold.
; -genoInbreed [float]
; -genoInbreed [float]
Call genotypes with defined threshold also taking inbreeding into account. '''-inbreed [int]''' is required, since individual inbreeding coefficients must have been estimated prior to calling genotypes using that information.
Call genotypes with defined threshold also taking inbreeding into account. '-inbreedSamples' must also be called for using this option.
 


==Admixture==
==Admixture==
Individual admixture proportions and population-specific allele frequencies can be estimated based on assuming K ancestral populations using an accelerated mini-batch NMF method.
Individual admixture proportions and ancestral allele frequencies can be estimated assuming K ancestral populations using an accelerated mini-batch NMF method.


; -admix
; -admix
Toggles admixture estimations.
Toggles admixture estimations. Estimates admixture proportions and ancestral allele frequencies.
; -admix_alpha [int-list]
; -admix_K [int]
Specify alpha (sparseness regularization parameter). Can be specified as a sequence to try several alphas in a single run. Fully compatible with -admix_seed and -admix_K. (Default: 0)
Not recommended. Override the number of ancestry components (K) to use, instead of using K=e-1.
; -admix_seed [int-list]
Specify seed for random initializations of factor matrices in admixture estimations. Can be specified as a sequence to try several different seeds in a single run. Fully compatible with -admix_alpha and -admix_K.
; -admix_K [int-list]
Not recommended. Specify number of ancestral populations to use in admixture estimations. Can be specified as a sequence to try several K's in a single run.  Fully compatible with -admix_alpha and -admix_seed.
; -admix_iter [int]
; -admix_iter [int]
Maximum number of iterations for admixture estimations using NMF. (Default: 100)
Maximum number of iterations for admixture estimations using NMF. (Default: 200)
; -admix_tole [float]
; -admix_tole [float]
Tolerance value for update in admixture estimations using NMF. (Default: 1e-5)
Tolerance value for update in admixture estimations using NMF. (Default: 1e-5)  
; -admix_batch [int]
; -admix_alpha [float
Specify the number of mini-batches to use in NMF method. (Default: 20)
Specify alpha (sparseness regularization parameter). (Default: 0)
; -admix_save
; -admix_auto [float]
Choose to save the population-specific allele frequencies.
Enable automatic search for optimal alpha using likelihood measure, by giving soft upper search bound of alpha.
; -admix_seed [int]
Specify seed for random initializations of factor matrices in admixture estimations.


==Tree==
; -tree
Construct neighbour-joining tree of samples from estimated covariance matrix estimated based on indivdual allele frequencies.
; -tree_samples
Provide a list of sample names of all individuals to construct a beautiful tree.


==Inbreeding==
=Citation=
Per-individual inbreeding coefficients incorporating population structure can be computed using three different methods. However, -inbreed 2 is recommended for low depth cases.
Our methods for inferring population structure have been published in GENETICS:
 
; -inbreed 1
A maximum likelihood estimator computed by an EM algorithm. Only allows for F-values between 0 and 1. Based on [https://www.cambridge.org/core/journals/genetics-research/article/maximum-likelihood-estimation-of-individual-inbreeding-coefficients-and-null-allele-frequencies/2DEBA0C0C2B92DF0EE89BD27DFCAD3FB].
; -inbreed 2
Simple estimator also computed by an EM algorithm. Based on [http://genome.cshlp.org/content/23/11/1852.full ngsF].
; -inbreed 3
(Not recommended for low depth NGS data!) Estimator using the kinship matrix. Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate].
; -inbreed_iter [int]
Maximum number of iterations for the EM algorithm methods. (Default: 200)
; -inbreed_tole [float]
Tolerance value for the EM algorithms for inbreeding coefficients estimation. (Default: 5e-5)
 
 
Per-site inbreeding coefficients incorporating population structure alongside likehood ratio tests for HWE can be computed as follows:
 
; -inbreedSites
 
==Selection==
A genome selection scan can be computed using two different methods based on posterior expectations of the genotypes (genotype dosages):


; -selection 1
[http://www.genetics.org/content/early/2018/08/21/genetics.118.301336 Inferring Population Structure and Admixture Proportions in Low Depth NGS Data]
Using an extended model of [http://www.cell.com/ajhg/abstract/S0002-9297(16)00003-3 FastPCA]. Performs a genome selection scan along all significant PCs.
; -selection 2
Using an extended model of [http://onlinelibrary.wiley.com/doi/10.1111/1755-0998.12592/abstract PCAdapt].


==Relatedness==
'''Work in progress...'''


Estimate kinship matrix based on method Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate]:
Our method for testing for HWE in structured populations has been published in Molecular Ecology Resources:


; -kinship
[https://onlinelibrary.wiley.com/doi/abs/10.1111/1755-0998.13019 Testing for Hardy‐Weinberg Equilibrium in Structured Populations using Genotype or Low‐Depth NGS Data]
Automatically estimated if '''-inbreed 3''' has been selected.
 
=Citation=

Latest revision as of 12:26, 24 October 2023


PCAngsd is a program that estimates the covariance matrix and individual allele frequencies for low-depth next-generation sequencing (NGS) data in structured/heterogeneous populations using principal component analysis (PCA) to perform multiple population genetic analyses using genotype likelihoods. Since version 0.98, PCAngsd was re-written to be based on Cython for computational bottlenecks and parallelization.

The main method was published in 2018 and can be found here: [1]

The HWE test was published in 2019 and can be found here: [2]

Simulated low depth NGS data of 3 populations


Overview

Framework for analyzing low-depth next-generation sequencing (NGS) data in heterogeneous/structured populations using principal component analysis (PCA). Population structure is inferred by estimating individual allele frequencies in an iterative approach using a truncated SVD model. The covariance matrix is estimated using the estimated individual allele frequencies as prior information for the unobserved genotypes in low-depth NGS data.

The estimated individual allele frequencies can further be used to account for population structure in other probabilistic methods. PCAngsd can perform the following analyses:

  • Covariance matrix
  • Admixture estimations
  • Inbreeding coefficients (both per-individual and per-site)
  • HWE test
  • Genome-wide selection scan
  • Genotype calling
  • Estimate NJ tree of samples

Older versions of PCAngsd can be found here [3].

Download and Installation

PCAngsd should work on all platforms meeting the requirements but server-side usage is highly recommended. Installation has only been tested on Linux systems.

Get PCAngsd and build

git clone https://github.com/Rosemeis/pcangsd.git
cd pcangsd/
python setup.py build_ext --inplace

Install dependencies:

The required set of Python packages are easily installed using the pip command and the 'requirements.txt file' included in the 'pcangsd' folder.

pip install --user -r requirements.txt

Quick start

PCAngsd is used by running the main caller file pcangsd.py. To see all available options use the following command:

python pcangsd.py -h

# Genotype likelihoods using 64 threads
python pcangsd.py -beagle input.beagle.gz -out output -threads 64

# PLINK files (using file-prefix, *.bed, *.bim, *.fam)
python pcangsd.py -beagle input.plink -out output -threads 64

PCAngsd accepts either genotype likelihoods in Beagle format or PLINK genotype files. Beagle files can be generated from BAM files using ANGSD. For inference of population structure in genotype data with non-random missigness, we recommend our EMU software that performs accelerated EM-PCA, however with fewer functionalities than PCAngsd (#soon).

PCAngsd will mostly output files in binary Numpy format (.npy) with a few exceptions. In order to read files in python:

import numpy as np
C = np.genfromtxt("output.cov") # Reads in estimated covariance matrix (text)
D = np.load("output.selection.npy") # Reads PC based selection statistics

R can also read Numpy matrices using the "RcppCNPy" R library:

library(RcppCNPy)
C <- as.matrix(read.table("output.cov")) # Reads in estimated covariance matrix
D <- npyLoad("output.selection.npy") # Reads PC based selection statistics

An example of generating genotype likelihoods in ANGSD and output them in the required Beagle text format.

./angsd -GL 2 -out input -nThreads 4 -doGlf 2 -doMajorMinor 1 -doMaf 2 -SNP_pval 1e-6 -bam bam.filelist

Tutorial

Please refer to the tutorial's page [4]

Options

# See all options in PCAngsd
python pcangsd.py -h

General usage

-beagle [Beagle file]

Input file of genotype likelihoods in Beagle format (.beagle.gz).

-filter [Text file]

Input file of 1's or 0's whether to keep individuals or not.

-plink [Prefix for binary PLINK files]

Path to PLINK files using their ONLY prefix (.bed, .bim, .fam).

-plink_error [float]

Incorporate errors into genotypes by specifying rate as argument.

-minMaf [float]

Minimum minor allele frequency threshold. (Default: 0.05)

-maf_iter [int]

Maximum number of EM iterations for computing the population allele frequencies (Default: 200).

-maf_tole [float]

Tolerance value in EM algorithm for population allele frequencies estimation (Default: 1e-4).

-iter [int]

Maximum number of iterations for estimation of individual allele frequencies (Default: 100).

-tole [float]

Tolerance value for update in estimation of individual allele frequencies (Default: 1e-5).

-hwe [.lrt.npy file]

Input file of LRT binary file from previous PCAngsd run to filter based on HWE.

-hwe_tole [float]

Threshold for HWE filtering of sites.

-e [int]

Manually select the number of eigenvalues to use in the modelling of individual allele frequencies (Default: Automatically tested using MAP test).

-pi [.pi.npy file]

Load previous estimation of individual allele frequencies to skip covariance estimation.

-maf_save

Choose to save estimated population allele frequencies (Binary). Numpy format (.npy).

-pi_save

Choose to save estimated individual allele frequencies (Binary). Numpy format (.npy). Can be used with the '-pi' command.

-dosage_save

Choose to save estimated genotype dosages (Binary). Numpy format (.npy).

-post_save

Choose to save the posterior genotype probabilities. Beagle format (.beagle).

-sites_save

Choose to save the kept sites after filtering which is useful for downstream analysis. Outputs a file of 1's and 0's for keeping a site or not, respectively.

-threads [int]

Specify the number of thread(s) to use (Default: 1).

-out [output prefix]

Fileprefix for all output files created by PCAngsd (Default: "pcangsd").

Selection

Perform PC-based genome-wide selection scans using posterior expectations of the genotypes (genotype dosages):

-selection

Using an extended model of FastPCA. Performs a genome-wide selection scan along all significant PCs. Outputs the selection statistics and must be converted to p-values by user. Each column reflect the selection statistics along a tested PC and they are χ²-distributed with 1 degree of freedom.

-pcadapt

Using an extended model of pcadapt. Performs a genome-wide selection scan across all significant PCs. Outputs the z-scores and must be converted to test statistics with the provided script 'pcangsd/scripts/pcadapt.R', and the test statistics are χ²-distributed with K degree of freedom.

-snp_weights

Output the SNP weights of the significant K eigenvectors.

Inbreeding

-inbreedSites

Estimate per-site inbreeding coefficients accounting for population structure and perform likehood ratio test for detecting sites deviating from HWE [5].

-inbreedSamples

Estimate per-individual inbreeding coefficients accounting for population structure which is based on an extension of ngsF for structured populations.

-inbreed_iter [int]

Maximum number of iterations for inbreeding EM algorithm. (Default: 200)

-inbreed_tole [float]

Tolerance value for inbreeding EM algorithm in estimating inbreeding coefficients. (Default: 1e-4)

Call genotypes

Genotypes can be called from posterior genotype probabilities by incorporating the individual allele frequencies as prior information.

-geno [float]

Call genotypes with defined threshold.

-genoInbreed [float]

Call genotypes with defined threshold also taking inbreeding into account. '-inbreedSamples' must also be called for using this option.

Admixture

Individual admixture proportions and ancestral allele frequencies can be estimated assuming K ancestral populations using an accelerated mini-batch NMF method.

-admix

Toggles admixture estimations. Estimates admixture proportions and ancestral allele frequencies.

-admix_K [int]

Not recommended. Override the number of ancestry components (K) to use, instead of using K=e-1.

-admix_iter [int]

Maximum number of iterations for admixture estimations using NMF. (Default: 200)

-admix_tole [float]

Tolerance value for update in admixture estimations using NMF. (Default: 1e-5)

-admix_alpha [float

Specify alpha (sparseness regularization parameter). (Default: 0)

-admix_auto [float]

Enable automatic search for optimal alpha using likelihood measure, by giving soft upper search bound of alpha.

-admix_seed [int]

Specify seed for random initializations of factor matrices in admixture estimations.

Tree

-tree

Construct neighbour-joining tree of samples from estimated covariance matrix estimated based on indivdual allele frequencies.

-tree_samples

Provide a list of sample names of all individuals to construct a beautiful tree.

Citation

Our methods for inferring population structure have been published in GENETICS:

Inferring Population Structure and Admixture Proportions in Low Depth NGS Data


Our method for testing for HWE in structured populations has been published in Molecular Ecology Resources:

Testing for Hardy‐Weinberg Equilibrium in Structured Populations using Genotype or Low‐Depth NGS Data