<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.popgen.dk/software/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Albrecht</id>
	<title>software - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.popgen.dk/software/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Albrecht"/>
	<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php/Special:Contributions/Albrecht"/>
	<updated>2026-05-01T18:31:53Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.40.1</generator>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=AsaMap&amp;diff=1489</id>
		<title>AsaMap</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=AsaMap&amp;diff=1489"/>
		<updated>2026-03-24T07:32:33Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: Replaced content with &amp;quot;The program is available and described on github:  https://github.com/e-jorsboe/asaMap&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The program is available and described on github:&lt;br /&gt;
&lt;br /&gt;
https://github.com/e-jorsboe/asaMap&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=NgsAdmix&amp;diff=1488</id>
		<title>NgsAdmix</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=NgsAdmix&amp;diff=1488"/>
		<updated>2026-03-24T07:28:40Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: Replaced content with &amp;quot;NGSadmix is a tool for estimating individual admixture proportions low depth sequencing data based on genotype likelihoods   The software including tutorials can be found here https://github.com/aalbrechtsen/NGSadmix&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;NGSadmix is a tool for estimating individual admixture proportions low depth sequencing data based on genotype likelihoods &lt;br /&gt;
&lt;br /&gt;
The software including tutorials can be found here&lt;br /&gt;
https://github.com/aalbrechtsen/NGSadmix&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1487</id>
		<title>EvalAdmix</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1487"/>
		<updated>2024-03-04T17:57:07Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''IMPORTANT''': version 0.95 (updated on 30/06/2021) fixes a bug in the implementation for genotype data, which caused displacement of genotypes between samples when a site had missing data. When all sites have some missingness this would result in the last samples from the analyses having a correlation of nan with all other samples; but might have some more subtle effects whenever there is some level of missingness. If you have analyses from previous versions based on genotype data with any missingness might be a good idea to re-run them after updating. The bug did not affect the genotype likelihoods implementation so if you based the analyses on genotype likelihoods you do not need to worry. If you applied it to gentoype data without any missingness you also do not need to worry.&lt;br /&gt;
&lt;br /&gt;
evalAdmix allows to evaluate the results of an admixture analysis (i.e. the result of applying [https://genome.cshlp.org/content/19/9/1655.long ADMIXTURE], [https://web.stanford.edu/group/pritchardlab/structure.html STRUCTURE], [http://www.popgen.dk/software/index.php/NgsAdmix NGSadmix] and similar). It only needs the input genotype data used for the previous admixture analysis and the output of that analysis (admixture proportions and ancestral population frequencies). The genotype input data can either be called genotypes in [https://www.cog-genomics.org/plink/1.9/formats#bed binary plink format] or genotype likelihoods in [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Beagle_format beagle format].&lt;br /&gt;
&lt;br /&gt;
The output is a pairwise correlation of residuals matrix between individuals. The correlation will be close to 0 in case of a good fit of the data to the admixture model. When individuals do not fit the model, individuals with similar demographic histories (i.e. usually individuals from the same population) will be positively correlated; and individuals with different histories but that are modelled as sharing one or more ancestral populations as admixture sources will have a negative correlation. Positive correlation between a pair of individuals might also be due to relatedness.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:evalAdmix.jpg|thumb|550px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Download and Installation==&lt;br /&gt;
&lt;br /&gt;
evalAdmix can be installed from [https://github.com/GenisGE/evalAdmix github] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;git clone https://github.com/GenisGE/evalAdmix.git&lt;br /&gt;
cd evalAdmix&lt;br /&gt;
make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quick start==&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -beagle inputBeagleFile.gz  -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -plink inputPlinkPrefix  -fname inputPlinkPrefix.K.P -qname inputPlinkPrefix.K.Q -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''-beagle''' beagle file of genotype likelihoods&lt;br /&gt;
* '''-plink''' binary plink file prefix with genotype data&lt;br /&gt;
* '''-fname''' file with ancestral frequencies (space delimited, rows are sites and columns ancestral populations)&lt;br /&gt;
* '''-qname''' file with admixture proportions (space delimited, rows are individuals and columns ancestral populations)&lt;br /&gt;
* '''-o''' prefix of output file names&lt;br /&gt;
* '''-P''' Number of threads used&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Arguments:&lt;br /&gt;
	Required:&lt;br /&gt;
		-plink path to binary plink file (excluding the .bed)&lt;br /&gt;
		or&lt;br /&gt;
	      	-beagle path to beagle file containing genotype likelihoods (alternative to -plink)&lt;br /&gt;
		&lt;br /&gt;
		-fname path to ancestral population frequencies file&lt;br /&gt;
	       	-qname path to admixture proportions file&lt;br /&gt;
		&lt;br /&gt;
	Optional:       &lt;br /&gt;
	&lt;br /&gt;
	       -o name of the output file&lt;br /&gt;
	       &lt;br /&gt;
	 Setup (optional):&lt;br /&gt;
	 &lt;br /&gt;
	       -P 1 number of threads&lt;br /&gt;
	       -autosomeMax 23	 autosome ends with this chromsome&lt;br /&gt;
	       -nIts 5	 number of iterations to do for frequency correction; if set to 0 calculates correlation without correction (fast but biased)&lt;br /&gt;
	       -useSites 1.0	 proportion of sites to use to calculate correlation of residuals&lt;br /&gt;
	       -useInds filename     path to tab delimited file with first column containing all individuals ID and second column with 1 to include individual in analysis and 0 otherwise (default all individuals are included)&lt;br /&gt;
	       -misTol 0.05 	 tolerance for considering site as missing when using genotype likelihoods. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
	       -minMaf 0.05 	 minimum minor allele frequency to keep site. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Input Files==&lt;br /&gt;
&lt;br /&gt;
===Plink===&lt;br /&gt;
Genotype data files in binary PLINK format (.bed .fam .bim).&lt;br /&gt;
===Beagle genotype likelhoods===&lt;br /&gt;
The input file contains genotype likelihoods in a .beagle file format [http://faculty.washington.edu/browning/beagle/beagle.html].&lt;br /&gt;
and can be compressed with gzip.&lt;br /&gt;
==== BAM files  ====&lt;br /&gt;
If you have BAM files you can use [[ANGSD]] to produce genotype likelihoods in .beagle format. Please &lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Beagle_input Creation of Beagle files with ANGSD]&lt;br /&gt;
&lt;br /&gt;
==== VCF files ====&lt;br /&gt;
If you already have made a VCF file that contains genotype likehood information then  it should be possible to convert .vcf files with genotype likelihoods to .beagle file via vcftools [https://vcftools.github.io/man_latest.html] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vcftools --vcf input.vcf --out test --BEAGLE-GL --chr 1,2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Chromosome has to be specified.&lt;br /&gt;
&lt;br /&gt;
You can also use bcftools' [https://samtools.github.io/bcftools/bcftools.html] 'query' option for generating a .beagle file from a .vcf file.&lt;br /&gt;
&lt;br /&gt;
==Output File==&lt;br /&gt;
The analysis performed by evalAdmix produces one file, containing a tab delimited N times N symmetric correlation matrix, where column i in line j contains the correlation of residuals between individual i and j, and the diagonal values (self-correlation) are set to NA:&lt;br /&gt;
&lt;br /&gt;
NA      0.008609        -0.006919       0.002731        0.020224&amp;lt;br /&amp;gt;&lt;br /&gt;
0.008609        NA      0.000033        0.004968        -0.008470&amp;lt;br /&amp;gt;&lt;br /&gt;
-0.006919       0.000033        NA      0.006982        0.005664&amp;lt;br /&amp;gt;&lt;br /&gt;
0.002731        0.004968        0.006982        NA      0.000521&amp;lt;br /&amp;gt;&lt;br /&gt;
0.020224        -0.008470       0.005664        0.000521        NA&lt;br /&gt;
&lt;br /&gt;
==Run command example==&lt;br /&gt;
&lt;br /&gt;
=== Genotype data ===&lt;br /&gt;
Download the input file containing genotypes in binary plink format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget https://popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bed&lt;br /&gt;
wget https://popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bim&lt;br /&gt;
wget https://popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.fam&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run ADMIXTURE [http://software.genetics.ucla.edu/admixture/] to obtain admixture proprotions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;admixture admixTjeck2.bed 3 -j20&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2.bed&lt;br /&gt;
::Number of Ancestral Populations K = 3&lt;br /&gt;
::Number of CPU threads = 20&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -plink admixTjeck2 -fname admixTjeck2.3.P -qname admixTjeck2.3.Q -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2 (-plink admixTjeck2).&lt;br /&gt;
::Ancestral Populations frequency file admixTjeck2.3.P (-fname admixTjeck2.3.P).&lt;br /&gt;
::Admixture proportions file admixTjeck2.3.Q (-qname admixTjeck2.3.Q).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
[[File:evalAdmixK3.Q.png|thumb|frame|admixture proportions]]&lt;br /&gt;
[[File:evalAdmixK3.cor.png|thumb|frame|evalAdmix correlations]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;admixTjeck2.fam&amp;quot;)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;admixTjeck2.3.Q&amp;quot;,stringsAsFactors=T)&lt;br /&gt;
&lt;br /&gt;
palette(c(&amp;quot;#A6CEE3&amp;quot;,&amp;quot;#1F78B4&amp;quot;,&amp;quot;#B2DF8A&amp;quot;,&amp;quot;#33A02C&amp;quot;,&amp;quot;#FB9A99&amp;quot;,&amp;quot;#E31A1C&amp;quot;,&amp;quot;#FDBF6F&amp;quot;,&amp;quot;#FF7F00&amp;quot;,&amp;quot;#CAB2D6&amp;quot;,&amp;quot;#6A3D9A&amp;quot;,&amp;quot;#FFFF99&amp;quot;,&amp;quot;#B15928&amp;quot;,&amp;quot;#1B9E77&amp;quot;,&amp;quot;#999999&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the ADMIXTURE reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,2]), q = q)&lt;br /&gt;
&lt;br /&gt;
#make barplot&lt;br /&gt;
plotAdmix(q,ord=ord,pop=pop[,2])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-as.matrix(read.table(&amp;quot;output.corres.txt&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,2]), ord=ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Low depth sequencing data ===&lt;br /&gt;
Download the input file containing genotype likelihoods in beagle format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2pop.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Execute [[NgsAdmix |NGSadmix]] to obtain admixture proportions&lt;br /&gt;
&amp;lt;pre&amp;gt;./NGSadmix -likes Demo2input.gz -K 3 -P 20 -o myoutfiles -minMaf 0.05&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Input file = Demo2input.gz&lt;br /&gt;
::Ancestral Populations K=3&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
::Output prefix = myoutfiles (-o myoutfiles) &lt;br /&gt;
::SNPs with MAF &amp;gt; 5%  (-minMaf 0.05)&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -beagle Demo2input.gz -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotype likelihoods file Demo2input.gz (-beagle Demo2input.gz).&lt;br /&gt;
::Ancestral Populations frequency file myoutfiles.fopt.gz (-fname myoutfiles.fopt.gz).&lt;br /&gt;
::Admixture proportions file  myoutfiles.qopt(-qname myoutfiles.qopt).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo2pop.info&amp;quot;,as.is=T)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;myoutfiles.qopt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the NGSadmix reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,1]), q = q)&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Demo2 Admixture proportions for K=3&amp;quot;)&lt;br /&gt;
text(sort(tapply(1:nrow(pop),pop[ord,1],mean)),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-read.table(&amp;quot;output.corres.txt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,1]), ord = ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Citation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://doi.org/10.1111/1755-0998.13171 Evaluation of Model Fit of Inferred Admixture Proportions]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=File:EvalAdmix.jpg&amp;diff=1486</id>
		<title>File:EvalAdmix.jpg</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=File:EvalAdmix.jpg&amp;diff=1486"/>
		<updated>2024-01-11T10:10:26Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1485</id>
		<title>EvalAdmix</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1485"/>
		<updated>2024-01-11T10:09:38Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''IMPORTANT''': version 0.95 (updated on 30/06/2021) fixes a bug in the implementation for genotype data, which caused displacement of genotypes between samples when a site had missing data. When all sites have some missingness this would result in the last samples from the analyses having a correlation of nan with all other samples; but might have some more subtle effects whenever there is some level of missingness. If you have analyses from previous versions based on genotype data with any missingness might be a good idea to re-run them after updating. The bug did not affect the genotype likelihoods implementation so if you based the analyses on genotype likelihoods you do not need to worry. If you applied it to gentoype data without any missingness you also do not need to worry.&lt;br /&gt;
&lt;br /&gt;
evalAdmix allows to evaluate the results of an admixture analysis (i.e. the result of applying [https://genome.cshlp.org/content/19/9/1655.long ADMIXTURE], [https://web.stanford.edu/group/pritchardlab/structure.html STRUCTURE], [http://www.popgen.dk/software/index.php/NgsAdmix NGSadmix] and similar). It only needs the input genotype data used for the previous admixture analysis and the output of that analysis (admixture proportions and ancestral population frequencies). The genotype input data can either be called genotypes in [https://www.cog-genomics.org/plink/1.9/formats#bed binary plink format] or genotype likelihoods in [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Beagle_format beagle format].&lt;br /&gt;
&lt;br /&gt;
The output is a pairwise correlation of residuals matrix between individuals. The correlation will be close to 0 in case of a good fit of the data to the admixture model. When individuals do not fit the model, individuals with similar demographic histories (i.e. usually individuals from the same population) will be positively correlated; and individuals with different histories but that are modelled as sharing one or more ancestral populations as admixture sources will have a negative correlation. Positive correlation between a pair of individuals might also be due to relatedness.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:evalAdmix.jpg|thumb|550px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Download and Installation==&lt;br /&gt;
&lt;br /&gt;
evalAdmix can be installed from [https://github.com/GenisGE/evalAdmix github] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;git clone https://github.com/GenisGE/evalAdmix.git&lt;br /&gt;
cd evalAdmix&lt;br /&gt;
make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quick start==&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -beagle inputBeagleFile.gz  -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -plink inputPlinkPrefix  -fname inputPlinkPrefix.K.P -qname inputPlinkPrefix.K.Q -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''-beagle''' beagle file of genotype likelihoods&lt;br /&gt;
* '''-plink''' binary plink file prefix with genotype data&lt;br /&gt;
* '''-fname''' file with ancestral frequencies (space delimited, rows are sites and columns ancestral populations)&lt;br /&gt;
* '''-qname''' file with admixture proportions (space delimited, rows are individuals and columns ancestral populations)&lt;br /&gt;
* '''-o''' prefix of output file names&lt;br /&gt;
* '''-P''' Number of threads used&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Arguments:&lt;br /&gt;
	Required:&lt;br /&gt;
		-plink path to binary plink file (excluding the .bed)&lt;br /&gt;
		or&lt;br /&gt;
	      	-beagle path to beagle file containing genotype likelihoods (alternative to -plink)&lt;br /&gt;
		&lt;br /&gt;
		-fname path to ancestral population frequencies file&lt;br /&gt;
	       	-qname path to admixture proportions file&lt;br /&gt;
		&lt;br /&gt;
	Optional:       &lt;br /&gt;
	&lt;br /&gt;
	       -o name of the output file&lt;br /&gt;
	       &lt;br /&gt;
	 Setup (optional):&lt;br /&gt;
	 &lt;br /&gt;
	       -P 1 number of threads&lt;br /&gt;
	       -autosomeMax 23	 autosome ends with this chromsome&lt;br /&gt;
	       -nIts 5	 number of iterations to do for frequency correction; if set to 0 calculates correlation without correction (fast but biased)&lt;br /&gt;
	       -useSites 1.0	 proportion of sites to use to calculate correlation of residuals&lt;br /&gt;
	       -useInds filename     path to tab delimited file with first column containing all individuals ID and second column with 1 to include individual in analysis and 0 otherwise (default all individuals are included)&lt;br /&gt;
	       -misTol 0.05 	 tolerance for considering site as missing when using genotype likelihoods. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
	       -minMaf 0.05 	 minimum minor allele frequency to keep site. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Input Files==&lt;br /&gt;
&lt;br /&gt;
===Plink===&lt;br /&gt;
Genotype data files in binary PLINK format (.bed .fam .bim).&lt;br /&gt;
===Beagle genotype likelhoods===&lt;br /&gt;
The input file contains genotype likelihoods in a .beagle file format [http://faculty.washington.edu/browning/beagle/beagle.html].&lt;br /&gt;
and can be compressed with gzip.&lt;br /&gt;
==== BAM files  ====&lt;br /&gt;
If you have BAM files you can use [[ANGSD]] to produce genotype likelihoods in .beagle format. Please &lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Beagle_input Creation of Beagle files with ANGSD]&lt;br /&gt;
&lt;br /&gt;
==== VCF files ====&lt;br /&gt;
If you already have made a VCF file that contains genotype likehood information then  it should be possible to convert .vcf files with genotype likelihoods to .beagle file via vcftools [https://vcftools.github.io/man_latest.html] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vcftools --vcf input.vcf --out test --BEAGLE-GL --chr 1,2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Chromosome has to be specified.&lt;br /&gt;
&lt;br /&gt;
You can also use bcftools' [https://samtools.github.io/bcftools/bcftools.html] 'query' option for generating a .beagle file from a .vcf file.&lt;br /&gt;
&lt;br /&gt;
==Output File==&lt;br /&gt;
The analysis performed by evalAdmix produces one file, containing a tab delimited N times N symmetric correlation matrix, where column i in line j contains the correlation of residuals between individual i and j, and the diagonal values (self-correlation) are set to NA:&lt;br /&gt;
&lt;br /&gt;
NA      0.008609        -0.006919       0.002731        0.020224&amp;lt;br /&amp;gt;&lt;br /&gt;
0.008609        NA      0.000033        0.004968        -0.008470&amp;lt;br /&amp;gt;&lt;br /&gt;
-0.006919       0.000033        NA      0.006982        0.005664&amp;lt;br /&amp;gt;&lt;br /&gt;
0.002731        0.004968        0.006982        NA      0.000521&amp;lt;br /&amp;gt;&lt;br /&gt;
0.020224        -0.008470       0.005664        0.000521        NA&lt;br /&gt;
&lt;br /&gt;
==Run command example==&lt;br /&gt;
&lt;br /&gt;
=== Genotype data ===&lt;br /&gt;
Download the input file containing genotypes in binary plink format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bed&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bim&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.fam&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run ADMIXTURE [http://software.genetics.ucla.edu/admixture/] to obtain admixture proprotions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;admixture admixTjeck2.bed 3 -j20&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2.bed&lt;br /&gt;
::Number of Ancestral Populations K = 3&lt;br /&gt;
::Number of CPU threads = 20&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -plink admixTjeck2 -fname admixTjeck2.3.P -qname admixTjeck2.3.Q -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2 (-plink admixTjeck2).&lt;br /&gt;
::Ancestral Populations frequency file admixTjeck2.3.P (-fname admixTjeck2.3.P).&lt;br /&gt;
::Admixture proportions file admixTjeck2.3.Q (-qname admixTjeck2.3.Q).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
[[File:evalAdmixK3.Q.png|thumb|frame|admixture proportions]]&lt;br /&gt;
[[File:evalAdmixK3.cor.png|thumb|frame|evalAdmix correlations]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;admixTjeck2.fam&amp;quot;)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;admixTjeck2.3.Q&amp;quot;,stringsAsFactors=T)&lt;br /&gt;
&lt;br /&gt;
palette(c(&amp;quot;#A6CEE3&amp;quot;,&amp;quot;#1F78B4&amp;quot;,&amp;quot;#B2DF8A&amp;quot;,&amp;quot;#33A02C&amp;quot;,&amp;quot;#FB9A99&amp;quot;,&amp;quot;#E31A1C&amp;quot;,&amp;quot;#FDBF6F&amp;quot;,&amp;quot;#FF7F00&amp;quot;,&amp;quot;#CAB2D6&amp;quot;,&amp;quot;#6A3D9A&amp;quot;,&amp;quot;#FFFF99&amp;quot;,&amp;quot;#B15928&amp;quot;,&amp;quot;#1B9E77&amp;quot;,&amp;quot;#999999&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the ADMIXTURE reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,2]), q = q)&lt;br /&gt;
&lt;br /&gt;
#make barplot&lt;br /&gt;
plotAdmix(q,ord=ord,pop=pop[,2])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-as.matrix(read.table(&amp;quot;output.corres.txt&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,2]), ord=ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Low depth sequencing data ===&lt;br /&gt;
Download the input file containing genotype likelihoods in beagle format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2pop.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Execute [[NgsAdmix |NGSadmix]] to obtain admixture proportions&lt;br /&gt;
&amp;lt;pre&amp;gt;./NGSadmix -likes Demo2input.gz -K 3 -P 20 -o myoutfiles -minMaf 0.05&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Input file = Demo2input.gz&lt;br /&gt;
::Ancestral Populations K=3&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
::Output prefix = myoutfiles (-o myoutfiles) &lt;br /&gt;
::SNPs with MAF &amp;gt; 5%  (-minMaf 0.05)&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -beagle Demo2input.gz -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotype likelihoods file Demo2input.gz (-beagle Demo2input.gz).&lt;br /&gt;
::Ancestral Populations frequency file myoutfiles.fopt.gz (-fname myoutfiles.fopt.gz).&lt;br /&gt;
::Admixture proportions file  myoutfiles.qopt(-qname myoutfiles.qopt).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo2pop.info&amp;quot;,as.is=T)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;myoutfiles.qopt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the NGSadmix reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,1]), q = q)&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Demo2 Admixture proportions for K=3&amp;quot;)&lt;br /&gt;
text(sort(tapply(1:nrow(pop),pop[ord,1],mean)),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-read.table(&amp;quot;output.corres.txt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,1]), ord = ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Citation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://doi.org/10.1111/1755-0998.13171 Evaluation of Model Fit of Inferred Admixture Proportions]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=File:Pcangsd_pca.png&amp;diff=1484</id>
		<title>File:Pcangsd pca.png</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=File:Pcangsd_pca.png&amp;diff=1484"/>
		<updated>2023-10-24T11:32:12Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1482</id>
		<title>PCAngsd</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1482"/>
		<updated>2023-10-24T11:26:25Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
The main method was published in 2018 and can be found here: [https://www.genetics.org/content/210/2/719]&lt;br /&gt;
&lt;br /&gt;
The HWE test was published in 2019 and can be found here: [https://onlinelibrary.wiley.com/doi/abs/10.1111/1755-0998.13019]&lt;br /&gt;
[[File:Pcangsd_admix3.gif|frame]]&lt;br /&gt;
&lt;br /&gt;
[[File:Pcangsd_pca.png|thumb|400px|Simulated low depth NGS data of 3 populations]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
The estimated individual allele frequencies can further be used to account for population structure in other probabilistic methods. PCAngsd can perform the following analyses:&lt;br /&gt;
*Covariance matrix&lt;br /&gt;
*Admixture estimations&lt;br /&gt;
*Inbreeding coefficients (both per-individual and per-site)&lt;br /&gt;
*HWE test&lt;br /&gt;
*Genome-wide selection scan&lt;br /&gt;
*Genotype calling&lt;br /&gt;
*Estimate NJ tree of samples&lt;br /&gt;
&lt;br /&gt;
Older versions of PCAngsd can be found here [https://github.com/Rosemeis/pcangsd/releases/].&lt;br /&gt;
&lt;br /&gt;
=Download and Installation=&lt;br /&gt;
&lt;br /&gt;
PCAngsd should work on all platforms meeting the requirements but server-side usage is highly recommended. Installation has only been tested on Linux systems.&lt;br /&gt;
&lt;br /&gt;
Get PCAngsd and build&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
git clone https://github.com/Rosemeis/pcangsd.git&lt;br /&gt;
cd pcangsd/&lt;br /&gt;
python setup.py build_ext --inplace&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Install dependencies:&lt;br /&gt;
&lt;br /&gt;
The required set of Python packages are easily installed using the pip command and the 'requirements.txt file' included in the 'pcangsd' folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pip install --user -r requirements.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Quick start=&lt;br /&gt;
&lt;br /&gt;
PCAngsd is used by running the main caller file pcangsd.py. To see all available options use the following command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
python pcangsd.py -h&lt;br /&gt;
&lt;br /&gt;
# Genotype likelihoods using 64 threads&lt;br /&gt;
python pcangsd.py -beagle input.beagle.gz -out output -threads 64&lt;br /&gt;
&lt;br /&gt;
# PLINK files (using file-prefix, *.bed, *.bim, *.fam)&lt;br /&gt;
python pcangsd.py -beagle input.plink -out output -threads 64&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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).&lt;br /&gt;
&lt;br /&gt;
PCAngsd will mostly output files in binary Numpy format (.npy) with a few exceptions. In order to read files in python:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
C = np.genfromtxt(&amp;quot;output.cov&amp;quot;) # Reads in estimated covariance matrix (text)&lt;br /&gt;
D = np.load(&amp;quot;output.selection.npy&amp;quot;) # Reads PC based selection statistics&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
R can also read Numpy matrices using the &amp;quot;RcppCNPy&amp;quot; R library:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
library(RcppCNPy)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;output.cov&amp;quot;)) # Reads in estimated covariance matrix&lt;br /&gt;
D &amp;lt;- npyLoad(&amp;quot;output.selection.npy&amp;quot;) # Reads PC based selection statistics&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example of generating genotype likelihoods in [http://popgen.dk/angsd ANGSD] and output them in the required Beagle text format.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
./angsd -GL 2 -out input -nThreads 4 -doGlf 2 -doMajorMinor 1 -doMaf 2 -SNP_pval 1e-6 -bam bam.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Tutorial=&lt;br /&gt;
&lt;br /&gt;
Please refer to the tutorial's page [http://www.popgen.dk/software/index.php/PCAngsdTutorial]&lt;br /&gt;
&lt;br /&gt;
=Options=&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# See all options in PCAngsd&lt;br /&gt;
python pcangsd.py -h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==General usage==&lt;br /&gt;
; -beagle [Beagle file]&lt;br /&gt;
Input file of genotype likelihoods in Beagle format (.beagle.gz).&lt;br /&gt;
; -filter [Text file]&lt;br /&gt;
Input file of 1's or 0's whether to keep individuals or not.&lt;br /&gt;
; -plink [Prefix for binary PLINK files]&lt;br /&gt;
Path to PLINK files using their ONLY prefix (.bed, .bim, .fam).&lt;br /&gt;
; -plink_error [float]&lt;br /&gt;
Incorporate errors into genotypes by specifying rate as argument.&lt;br /&gt;
; -minMaf [float]&lt;br /&gt;
Minimum minor allele frequency threshold. (Default: 0.05)&lt;br /&gt;
; -maf_iter [int]&lt;br /&gt;
Maximum number of EM iterations for computing the population allele frequencies (Default: 200).&lt;br /&gt;
; -maf_tole [float]&lt;br /&gt;
Tolerance value in EM algorithm for population allele frequencies estimation (Default: 1e-4).&lt;br /&gt;
; -iter [int]&lt;br /&gt;
Maximum number of iterations for estimation of individual allele frequencies (Default: 100).&lt;br /&gt;
; -tole [float]&lt;br /&gt;
Tolerance value for update in estimation of individual allele frequencies (Default: 1e-5).&lt;br /&gt;
; -hwe [.lrt.npy file]&lt;br /&gt;
Input file of LRT binary file from previous PCAngsd run to filter based on HWE.&lt;br /&gt;
; -hwe_tole [float]&lt;br /&gt;
Threshold for HWE filtering of sites. &lt;br /&gt;
; -e [int]&lt;br /&gt;
Manually select the number of eigenvalues to use in the modelling of individual allele frequencies (Default: Automatically tested using MAP test).&lt;br /&gt;
; -pi [.pi.npy file]&lt;br /&gt;
Load previous estimation of individual allele frequencies to skip covariance estimation.&lt;br /&gt;
; -maf_save&lt;br /&gt;
Choose to save estimated population allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
; -pi_save&lt;br /&gt;
Choose to save estimated individual allele frequencies (Binary). Numpy format (.npy). Can be used with the '-pi' command.&lt;br /&gt;
; -dosage_save&lt;br /&gt;
Choose to save estimated genotype dosages (Binary). Numpy format (.npy).&lt;br /&gt;
; -post_save&lt;br /&gt;
Choose to save the posterior genotype probabilities. Beagle format (.beagle).&lt;br /&gt;
; -sites_save&lt;br /&gt;
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.&lt;br /&gt;
; -threads [int]&lt;br /&gt;
Specify the number of thread(s) to use (Default: 1).&lt;br /&gt;
; -out [output prefix]&lt;br /&gt;
Fileprefix for all output files created by PCAngsd (Default: &amp;quot;pcangsd&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
==Selection==&lt;br /&gt;
Perform PC-based genome-wide selection scans using posterior expectations of the genotypes (genotype dosages):&lt;br /&gt;
&lt;br /&gt;
; -selection&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
; -pcadapt&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
; -snp_weights&lt;br /&gt;
Output the SNP weights of the significant K eigenvectors.&lt;br /&gt;
&lt;br /&gt;
==Inbreeding==&lt;br /&gt;
; -inbreedSites&lt;br /&gt;
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].&lt;br /&gt;
&lt;br /&gt;
; -inbreedSamples&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
; -inbreed_iter [int]&lt;br /&gt;
Maximum number of iterations for inbreeding EM algorithm. (Default: 200)&lt;br /&gt;
&lt;br /&gt;
; -inbreed_tole [float]&lt;br /&gt;
Tolerance value for inbreeding EM algorithm in estimating inbreeding coefficients. (Default: 1e-4)&lt;br /&gt;
&lt;br /&gt;
==Call genotypes==&lt;br /&gt;
Genotypes can be called from posterior genotype probabilities by incorporating the individual allele frequencies as prior information.&lt;br /&gt;
&lt;br /&gt;
; -geno [float]&lt;br /&gt;
Call genotypes with defined threshold.&lt;br /&gt;
; -genoInbreed [float]&lt;br /&gt;
Call genotypes with defined threshold also taking inbreeding into account. '-inbreedSamples' must also be called for using this option.&lt;br /&gt;
&lt;br /&gt;
==Admixture==&lt;br /&gt;
Individual admixture proportions and ancestral allele frequencies can be estimated assuming K ancestral populations using an accelerated mini-batch NMF method.&lt;br /&gt;
&lt;br /&gt;
; -admix&lt;br /&gt;
Toggles admixture estimations. Estimates admixture proportions and ancestral allele frequencies.&lt;br /&gt;
; -admix_K [int]&lt;br /&gt;
Not recommended. Override the number of ancestry components (K) to use, instead of using K=e-1.&lt;br /&gt;
; -admix_iter [int]&lt;br /&gt;
Maximum number of iterations for admixture estimations using NMF. (Default: 200)&lt;br /&gt;
; -admix_tole [float]&lt;br /&gt;
Tolerance value for update in admixture estimations using NMF. (Default: 1e-5) &lt;br /&gt;
; -admix_alpha [float&lt;br /&gt;
Specify alpha (sparseness regularization parameter). (Default: 0)&lt;br /&gt;
; -admix_auto [float]&lt;br /&gt;
Enable automatic search for optimal alpha using likelihood measure, by giving soft upper search bound of alpha.&lt;br /&gt;
; -admix_seed [int]&lt;br /&gt;
Specify seed for random initializations of factor matrices in admixture estimations.&lt;br /&gt;
&lt;br /&gt;
==Tree==&lt;br /&gt;
; -tree&lt;br /&gt;
Construct neighbour-joining tree of samples from estimated covariance matrix estimated based on indivdual allele frequencies.&lt;br /&gt;
; -tree_samples&lt;br /&gt;
Provide a list of sample names of all individuals to construct a beautiful tree.&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Our methods for inferring population structure have been published in GENETICS:&lt;br /&gt;
&lt;br /&gt;
[http://www.genetics.org/content/early/2018/08/21/genetics.118.301336 Inferring Population Structure and Admixture Proportions in Low Depth NGS Data]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Our method for testing for HWE in structured populations has been published in Molecular Ecology Resources:&lt;br /&gt;
&lt;br /&gt;
[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]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=File:Pcangsd_admix3.gif&amp;diff=1481</id>
		<title>File:Pcangsd admix3.gif</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=File:Pcangsd_admix3.gif&amp;diff=1481"/>
		<updated>2023-10-24T11:25:27Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1480</id>
		<title>PCAngsd</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1480"/>
		<updated>2023-10-24T11:12:40Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
The main method was published in 2018 and can be found here: [https://www.genetics.org/content/210/2/719]&lt;br /&gt;
&lt;br /&gt;
The HWE test was published in 2019 and can be found here: [https://onlinelibrary.wiley.com/doi/abs/10.1111/1755-0998.13019]&lt;br /&gt;
[[File:Pcangsd_admix.gif|frame]]&lt;br /&gt;
[[File:Pcangsd_admix3.gif|frame]]&lt;br /&gt;
&lt;br /&gt;
[[File:Pcangsd_pca.png|thumb|400px|Simulated low depth NGS data of 3 populations]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
The estimated individual allele frequencies can further be used to account for population structure in other probabilistic methods. PCAngsd can perform the following analyses:&lt;br /&gt;
*Covariance matrix&lt;br /&gt;
*Admixture estimations&lt;br /&gt;
*Inbreeding coefficients (both per-individual and per-site)&lt;br /&gt;
*HWE test&lt;br /&gt;
*Genome-wide selection scan&lt;br /&gt;
*Genotype calling&lt;br /&gt;
*Estimate NJ tree of samples&lt;br /&gt;
&lt;br /&gt;
Older versions of PCAngsd can be found here [https://github.com/Rosemeis/pcangsd/releases/].&lt;br /&gt;
&lt;br /&gt;
=Download and Installation=&lt;br /&gt;
&lt;br /&gt;
PCAngsd should work on all platforms meeting the requirements but server-side usage is highly recommended. Installation has only been tested on Linux systems.&lt;br /&gt;
&lt;br /&gt;
Get PCAngsd and build&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
git clone https://github.com/Rosemeis/pcangsd.git&lt;br /&gt;
cd pcangsd/&lt;br /&gt;
python setup.py build_ext --inplace&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Install dependencies:&lt;br /&gt;
&lt;br /&gt;
The required set of Python packages are easily installed using the pip command and the 'requirements.txt file' included in the 'pcangsd' folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pip install --user -r requirements.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Quick start=&lt;br /&gt;
&lt;br /&gt;
PCAngsd is used by running the main caller file pcangsd.py. To see all available options use the following command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
python pcangsd.py -h&lt;br /&gt;
&lt;br /&gt;
# Genotype likelihoods using 64 threads&lt;br /&gt;
python pcangsd.py -beagle input.beagle.gz -out output -threads 64&lt;br /&gt;
&lt;br /&gt;
# PLINK files (using file-prefix, *.bed, *.bim, *.fam)&lt;br /&gt;
python pcangsd.py -beagle input.plink -out output -threads 64&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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).&lt;br /&gt;
&lt;br /&gt;
PCAngsd will mostly output files in binary Numpy format (.npy) with a few exceptions. In order to read files in python:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
C = np.genfromtxt(&amp;quot;output.cov&amp;quot;) # Reads in estimated covariance matrix (text)&lt;br /&gt;
D = np.load(&amp;quot;output.selection.npy&amp;quot;) # Reads PC based selection statistics&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
R can also read Numpy matrices using the &amp;quot;RcppCNPy&amp;quot; R library:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
library(RcppCNPy)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;output.cov&amp;quot;)) # Reads in estimated covariance matrix&lt;br /&gt;
D &amp;lt;- npyLoad(&amp;quot;output.selection.npy&amp;quot;) # Reads PC based selection statistics&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example of generating genotype likelihoods in [http://popgen.dk/angsd ANGSD] and output them in the required Beagle text format.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
./angsd -GL 2 -out input -nThreads 4 -doGlf 2 -doMajorMinor 1 -doMaf 2 -SNP_pval 1e-6 -bam bam.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Tutorial=&lt;br /&gt;
&lt;br /&gt;
Please refer to the tutorial's page [http://www.popgen.dk/software/index.php/PCAngsdTutorial]&lt;br /&gt;
&lt;br /&gt;
=Options=&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# See all options in PCAngsd&lt;br /&gt;
python pcangsd.py -h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==General usage==&lt;br /&gt;
; -beagle [Beagle file]&lt;br /&gt;
Input file of genotype likelihoods in Beagle format (.beagle.gz).&lt;br /&gt;
; -filter [Text file]&lt;br /&gt;
Input file of 1's or 0's whether to keep individuals or not.&lt;br /&gt;
; -plink [Prefix for binary PLINK files]&lt;br /&gt;
Path to PLINK files using their ONLY prefix (.bed, .bim, .fam).&lt;br /&gt;
; -plink_error [float]&lt;br /&gt;
Incorporate errors into genotypes by specifying rate as argument.&lt;br /&gt;
; -minMaf [float]&lt;br /&gt;
Minimum minor allele frequency threshold. (Default: 0.05)&lt;br /&gt;
; -maf_iter [int]&lt;br /&gt;
Maximum number of EM iterations for computing the population allele frequencies (Default: 200).&lt;br /&gt;
; -maf_tole [float]&lt;br /&gt;
Tolerance value in EM algorithm for population allele frequencies estimation (Default: 1e-4).&lt;br /&gt;
; -iter [int]&lt;br /&gt;
Maximum number of iterations for estimation of individual allele frequencies (Default: 100).&lt;br /&gt;
; -tole [float]&lt;br /&gt;
Tolerance value for update in estimation of individual allele frequencies (Default: 1e-5).&lt;br /&gt;
; -hwe [.lrt.npy file]&lt;br /&gt;
Input file of LRT binary file from previous PCAngsd run to filter based on HWE.&lt;br /&gt;
; -hwe_tole [float]&lt;br /&gt;
Threshold for HWE filtering of sites. &lt;br /&gt;
; -e [int]&lt;br /&gt;
Manually select the number of eigenvalues to use in the modelling of individual allele frequencies (Default: Automatically tested using MAP test).&lt;br /&gt;
; -pi [.pi.npy file]&lt;br /&gt;
Load previous estimation of individual allele frequencies to skip covariance estimation.&lt;br /&gt;
; -maf_save&lt;br /&gt;
Choose to save estimated population allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
; -pi_save&lt;br /&gt;
Choose to save estimated individual allele frequencies (Binary). Numpy format (.npy). Can be used with the '-pi' command.&lt;br /&gt;
; -dosage_save&lt;br /&gt;
Choose to save estimated genotype dosages (Binary). Numpy format (.npy).&lt;br /&gt;
; -post_save&lt;br /&gt;
Choose to save the posterior genotype probabilities. Beagle format (.beagle).&lt;br /&gt;
; -sites_save&lt;br /&gt;
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.&lt;br /&gt;
; -threads [int]&lt;br /&gt;
Specify the number of thread(s) to use (Default: 1).&lt;br /&gt;
; -out [output prefix]&lt;br /&gt;
Fileprefix for all output files created by PCAngsd (Default: &amp;quot;pcangsd&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
==Selection==&lt;br /&gt;
Perform PC-based genome-wide selection scans using posterior expectations of the genotypes (genotype dosages):&lt;br /&gt;
&lt;br /&gt;
; -selection&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
; -pcadapt&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
; -snp_weights&lt;br /&gt;
Output the SNP weights of the significant K eigenvectors.&lt;br /&gt;
&lt;br /&gt;
==Inbreeding==&lt;br /&gt;
; -inbreedSites&lt;br /&gt;
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].&lt;br /&gt;
&lt;br /&gt;
; -inbreedSamples&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
; -inbreed_iter [int]&lt;br /&gt;
Maximum number of iterations for inbreeding EM algorithm. (Default: 200)&lt;br /&gt;
&lt;br /&gt;
; -inbreed_tole [float]&lt;br /&gt;
Tolerance value for inbreeding EM algorithm in estimating inbreeding coefficients. (Default: 1e-4)&lt;br /&gt;
&lt;br /&gt;
==Call genotypes==&lt;br /&gt;
Genotypes can be called from posterior genotype probabilities by incorporating the individual allele frequencies as prior information.&lt;br /&gt;
&lt;br /&gt;
; -geno [float]&lt;br /&gt;
Call genotypes with defined threshold.&lt;br /&gt;
; -genoInbreed [float]&lt;br /&gt;
Call genotypes with defined threshold also taking inbreeding into account. '-inbreedSamples' must also be called for using this option.&lt;br /&gt;
&lt;br /&gt;
==Admixture==&lt;br /&gt;
Individual admixture proportions and ancestral allele frequencies can be estimated assuming K ancestral populations using an accelerated mini-batch NMF method.&lt;br /&gt;
&lt;br /&gt;
; -admix&lt;br /&gt;
Toggles admixture estimations. Estimates admixture proportions and ancestral allele frequencies.&lt;br /&gt;
; -admix_K [int]&lt;br /&gt;
Not recommended. Override the number of ancestry components (K) to use, instead of using K=e-1.&lt;br /&gt;
; -admix_iter [int]&lt;br /&gt;
Maximum number of iterations for admixture estimations using NMF. (Default: 200)&lt;br /&gt;
; -admix_tole [float]&lt;br /&gt;
Tolerance value for update in admixture estimations using NMF. (Default: 1e-5) &lt;br /&gt;
; -admix_alpha [float&lt;br /&gt;
Specify alpha (sparseness regularization parameter). (Default: 0)&lt;br /&gt;
; -admix_auto [float]&lt;br /&gt;
Enable automatic search for optimal alpha using likelihood measure, by giving soft upper search bound of alpha.&lt;br /&gt;
; -admix_seed [int]&lt;br /&gt;
Specify seed for random initializations of factor matrices in admixture estimations.&lt;br /&gt;
&lt;br /&gt;
==Tree==&lt;br /&gt;
; -tree&lt;br /&gt;
Construct neighbour-joining tree of samples from estimated covariance matrix estimated based on indivdual allele frequencies.&lt;br /&gt;
; -tree_samples&lt;br /&gt;
Provide a list of sample names of all individuals to construct a beautiful tree.&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Our methods for inferring population structure have been published in GENETICS:&lt;br /&gt;
&lt;br /&gt;
[http://www.genetics.org/content/early/2018/08/21/genetics.118.301336 Inferring Population Structure and Admixture Proportions in Low Depth NGS Data]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Our method for testing for HWE in structured populations has been published in Molecular Ecology Resources:&lt;br /&gt;
&lt;br /&gt;
[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]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1477</id>
		<title>EvalAdmix</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1477"/>
		<updated>2022-12-27T09:27:51Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Genotype data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''IMPORTANT''': version 0.95 (updated on 30/06/2021) fixes a bug in the implementation for genotype data, which caused displacement of genotypes between samples when a site had missing data. When all sites have some missingness this would result in the last samples from the analyses having a correlation of nan with all other samples; but might have some more subtle effects whenever there is some level of missingness. If you have analyses from previous versions based on genotype data with any missingness might be a good idea to re-run them after updating. The bug did not affect the genotype likelihoods implementation so if you based the analyses on genotype likelihoods you do not need to worry. If you applied it to gentoype data without any missingness you also do not need to worry.&lt;br /&gt;
&lt;br /&gt;
evalAdmix allows to evaluate the results of an admixture analysis (i.e. the result of applying [https://genome.cshlp.org/content/19/9/1655.long ADMIXTURE], [https://web.stanford.edu/group/pritchardlab/structure.html STRUCTURE], [http://www.popgen.dk/software/index.php/NgsAdmix NGSadmix] and similar). It only needs the input genotype data used for the previous admixture analysis and the output of that analysis (admixture proportions and ancestral population frequencies). The genotype input data can either be called genotypes in [https://www.cog-genomics.org/plink/1.9/formats#bed binary plink format] or genotype likelihoods in [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Beagle_format beagle format].&lt;br /&gt;
&lt;br /&gt;
The output is a pairwise correlation of residuals matrix between individuals. The correlation will be close to 0 in case of a good fit of the data to the admixture model. When individuals do not fit the model, individuals with similar demographic histories (i.e. usually individuals from the same population) will be positively correlated; and individuals with different histories but that are modelled as sharing one or more ancestral populations as admixture sources will have a negative correlation. Positive correlation between a pair of individuals might also be due to relatedness.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:evalAdmix.png|thumb|550px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Download and Installation==&lt;br /&gt;
&lt;br /&gt;
evalAdmix can be installed from [https://github.com/GenisGE/evalAdmix github] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;git clone https://github.com/GenisGE/evalAdmix.git&lt;br /&gt;
cd evalAdmix&lt;br /&gt;
make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quick start==&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -beagle inputBeagleFile.gz  -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -plink inputPlinkPrefix  -fname inputPlinkPrefix.K.P -qname inputPlinkPrefix.K.Q -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''-beagle''' beagle file of genotype likelihoods&lt;br /&gt;
* '''-plink''' binary plink file prefix with genotype data&lt;br /&gt;
* '''-fname''' file with ancestral frequencies (space delimited, rows are sites and columns ancestral populations)&lt;br /&gt;
* '''-qname''' file with admixture proportions (space delimited, rows are individuals and columns ancestral populations)&lt;br /&gt;
* '''-o''' prefix of output file names&lt;br /&gt;
* '''-P''' Number of threads used&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Arguments:&lt;br /&gt;
	Required:&lt;br /&gt;
		-plink path to binary plink file (excluding the .bed)&lt;br /&gt;
		or&lt;br /&gt;
	      	-beagle path to beagle file containing genotype likelihoods (alternative to -plink)&lt;br /&gt;
		&lt;br /&gt;
		-fname path to ancestral population frequencies file&lt;br /&gt;
	       	-qname path to admixture proportions file&lt;br /&gt;
		&lt;br /&gt;
	Optional:       &lt;br /&gt;
	&lt;br /&gt;
	       -o name of the output file&lt;br /&gt;
	       &lt;br /&gt;
	 Setup (optional):&lt;br /&gt;
	 &lt;br /&gt;
	       -P 1 number of threads&lt;br /&gt;
	       -autosomeMax 23	 autosome ends with this chromsome&lt;br /&gt;
	       -nIts 5	 number of iterations to do for frequency correction; if set to 0 calculates correlation without correction (fast but biased)&lt;br /&gt;
	       -useSites 1.0	 proportion of sites to use to calculate correlation of residuals&lt;br /&gt;
	       -useInds filename     path to tab delimited file with first column containing all individuals ID and second column with 1 to include individual in analysis and 0 otherwise (default all individuals are included)&lt;br /&gt;
	       -misTol 0.05 	 tolerance for considering site as missing when using genotype likelihoods. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
	       -minMaf 0.05 	 minimum minor allele frequency to keep site. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Input Files==&lt;br /&gt;
&lt;br /&gt;
===Plink===&lt;br /&gt;
Genotype data files in binary PLINK format (.bed .fam .bim).&lt;br /&gt;
===Beagle genotype likelhoods===&lt;br /&gt;
The input file contains genotype likelihoods in a .beagle file format [http://faculty.washington.edu/browning/beagle/beagle.html].&lt;br /&gt;
and can be compressed with gzip.&lt;br /&gt;
==== BAM files  ====&lt;br /&gt;
If you have BAM files you can use [[ANGSD]] to produce genotype likelihoods in .beagle format. Please &lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Beagle_input Creation of Beagle files with ANGSD]&lt;br /&gt;
&lt;br /&gt;
==== VCF files ====&lt;br /&gt;
If you already have made a VCF file that contains genotype likehood information then  it should be possible to convert .vcf files with genotype likelihoods to .beagle file via vcftools [https://vcftools.github.io/man_latest.html] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vcftools --vcf input.vcf --out test --BEAGLE-GL --chr 1,2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Chromosome has to be specified.&lt;br /&gt;
&lt;br /&gt;
You can also use bcftools' [https://samtools.github.io/bcftools/bcftools.html] 'query' option for generating a .beagle file from a .vcf file.&lt;br /&gt;
&lt;br /&gt;
==Output File==&lt;br /&gt;
The analysis performed by evalAdmix produces one file, containing a tab delimited N times N symmetric correlation matrix, where column i in line j contains the correlation of residuals between individual i and j, and the diagonal values (self-correlation) are set to NA:&lt;br /&gt;
&lt;br /&gt;
NA      0.008609        -0.006919       0.002731        0.020224&amp;lt;br /&amp;gt;&lt;br /&gt;
0.008609        NA      0.000033        0.004968        -0.008470&amp;lt;br /&amp;gt;&lt;br /&gt;
-0.006919       0.000033        NA      0.006982        0.005664&amp;lt;br /&amp;gt;&lt;br /&gt;
0.002731        0.004968        0.006982        NA      0.000521&amp;lt;br /&amp;gt;&lt;br /&gt;
0.020224        -0.008470       0.005664        0.000521        NA&lt;br /&gt;
&lt;br /&gt;
==Run command example==&lt;br /&gt;
&lt;br /&gt;
=== Genotype data ===&lt;br /&gt;
Download the input file containing genotypes in binary plink format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bed&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bim&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.fam&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run ADMIXTURE [http://software.genetics.ucla.edu/admixture/] to obtain admixture proprotions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;admixture admixTjeck2.bed 3 -j20&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2.bed&lt;br /&gt;
::Number of Ancestral Populations K = 3&lt;br /&gt;
::Number of CPU threads = 20&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -plink admixTjeck2 -fname admixTjeck2.3.P -qname admixTjeck2.3.Q -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2 (-plink admixTjeck2).&lt;br /&gt;
::Ancestral Populations frequency file admixTjeck2.3.P (-fname admixTjeck2.3.P).&lt;br /&gt;
::Admixture proportions file admixTjeck2.3.Q (-qname admixTjeck2.3.Q).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
[[File:evalAdmixK3.Q.png|thumb|frame|admixture proportions]]&lt;br /&gt;
[[File:evalAdmixK3.cor.png|thumb|frame|evalAdmix correlations]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;admixTjeck2.fam&amp;quot;)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;admixTjeck2.3.Q&amp;quot;,stringsAsFactors=T)&lt;br /&gt;
&lt;br /&gt;
palette(c(&amp;quot;#A6CEE3&amp;quot;,&amp;quot;#1F78B4&amp;quot;,&amp;quot;#B2DF8A&amp;quot;,&amp;quot;#33A02C&amp;quot;,&amp;quot;#FB9A99&amp;quot;,&amp;quot;#E31A1C&amp;quot;,&amp;quot;#FDBF6F&amp;quot;,&amp;quot;#FF7F00&amp;quot;,&amp;quot;#CAB2D6&amp;quot;,&amp;quot;#6A3D9A&amp;quot;,&amp;quot;#FFFF99&amp;quot;,&amp;quot;#B15928&amp;quot;,&amp;quot;#1B9E77&amp;quot;,&amp;quot;#999999&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the ADMIXTURE reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,2]), q = q)&lt;br /&gt;
&lt;br /&gt;
#make barplot&lt;br /&gt;
plotAdmix(q,ord=ord,pop=pop[,2])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-as.matrix(read.table(&amp;quot;output.corres.txt&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,2]), ord=ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Low depth sequencing data ===&lt;br /&gt;
Download the input file containing genotype likelihoods in beagle format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2pop.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Execute [[NgsAdmix |NGSadmix]] to obtain admixture proportions&lt;br /&gt;
&amp;lt;pre&amp;gt;./NGSadmix -likes Demo2input.gz -K 3 -P 20 -o myoutfiles -minMaf 0.05&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Input file = Demo2input.gz&lt;br /&gt;
::Ancestral Populations K=3&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
::Output prefix = myoutfiles (-o myoutfiles) &lt;br /&gt;
::SNPs with MAF &amp;gt; 5%  (-minMaf 0.05)&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -beagle Demo2input.gz -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotype likelihoods file Demo2input.gz (-beagle Demo2input.gz).&lt;br /&gt;
::Ancestral Populations frequency file myoutfiles.fopt.gz (-fname myoutfiles.fopt.gz).&lt;br /&gt;
::Admixture proportions file  myoutfiles.qopt(-qname myoutfiles.qopt).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo2pop.info&amp;quot;,as.is=T)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;myoutfiles.qopt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the NGSadmix reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,1]), q = q)&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Demo2 Admixture proportions for K=3&amp;quot;)&lt;br /&gt;
text(sort(tapply(1:nrow(pop),pop[ord,1],mean)),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-read.table(&amp;quot;output.corres.txt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,1]), ord = ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Citation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://doi.org/10.1111/1755-0998.13171 Evaluation of Model Fit of Inferred Admixture Proportions]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=File:EvalAdmixK3.Q.png&amp;diff=1476</id>
		<title>File:EvalAdmixK3.Q.png</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=File:EvalAdmixK3.Q.png&amp;diff=1476"/>
		<updated>2022-12-27T09:26:56Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: Albrecht uploaded a new version of File:EvalAdmixK3.Q.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=File:EvalAdmixK3.cor.png&amp;diff=1475</id>
		<title>File:EvalAdmixK3.cor.png</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=File:EvalAdmixK3.cor.png&amp;diff=1475"/>
		<updated>2022-12-27T09:25:56Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=File:EvalAdmixK3.Q.png&amp;diff=1474</id>
		<title>File:EvalAdmixK3.Q.png</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=File:EvalAdmixK3.Q.png&amp;diff=1474"/>
		<updated>2022-12-27T09:25:35Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1473</id>
		<title>EvalAdmix</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1473"/>
		<updated>2022-12-27T09:25:20Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Genotype data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''IMPORTANT''': version 0.95 (updated on 30/06/2021) fixes a bug in the implementation for genotype data, which caused displacement of genotypes between samples when a site had missing data. When all sites have some missingness this would result in the last samples from the analyses having a correlation of nan with all other samples; but might have some more subtle effects whenever there is some level of missingness. If you have analyses from previous versions based on genotype data with any missingness might be a good idea to re-run them after updating. The bug did not affect the genotype likelihoods implementation so if you based the analyses on genotype likelihoods you do not need to worry. If you applied it to gentoype data without any missingness you also do not need to worry.&lt;br /&gt;
&lt;br /&gt;
evalAdmix allows to evaluate the results of an admixture analysis (i.e. the result of applying [https://genome.cshlp.org/content/19/9/1655.long ADMIXTURE], [https://web.stanford.edu/group/pritchardlab/structure.html STRUCTURE], [http://www.popgen.dk/software/index.php/NgsAdmix NGSadmix] and similar). It only needs the input genotype data used for the previous admixture analysis and the output of that analysis (admixture proportions and ancestral population frequencies). The genotype input data can either be called genotypes in [https://www.cog-genomics.org/plink/1.9/formats#bed binary plink format] or genotype likelihoods in [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Beagle_format beagle format].&lt;br /&gt;
&lt;br /&gt;
The output is a pairwise correlation of residuals matrix between individuals. The correlation will be close to 0 in case of a good fit of the data to the admixture model. When individuals do not fit the model, individuals with similar demographic histories (i.e. usually individuals from the same population) will be positively correlated; and individuals with different histories but that are modelled as sharing one or more ancestral populations as admixture sources will have a negative correlation. Positive correlation between a pair of individuals might also be due to relatedness.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:evalAdmix.png|thumb|550px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Download and Installation==&lt;br /&gt;
&lt;br /&gt;
evalAdmix can be installed from [https://github.com/GenisGE/evalAdmix github] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;git clone https://github.com/GenisGE/evalAdmix.git&lt;br /&gt;
cd evalAdmix&lt;br /&gt;
make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quick start==&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -beagle inputBeagleFile.gz  -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -plink inputPlinkPrefix  -fname inputPlinkPrefix.K.P -qname inputPlinkPrefix.K.Q -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''-beagle''' beagle file of genotype likelihoods&lt;br /&gt;
* '''-plink''' binary plink file prefix with genotype data&lt;br /&gt;
* '''-fname''' file with ancestral frequencies (space delimited, rows are sites and columns ancestral populations)&lt;br /&gt;
* '''-qname''' file with admixture proportions (space delimited, rows are individuals and columns ancestral populations)&lt;br /&gt;
* '''-o''' prefix of output file names&lt;br /&gt;
* '''-P''' Number of threads used&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Arguments:&lt;br /&gt;
	Required:&lt;br /&gt;
		-plink path to binary plink file (excluding the .bed)&lt;br /&gt;
		or&lt;br /&gt;
	      	-beagle path to beagle file containing genotype likelihoods (alternative to -plink)&lt;br /&gt;
		&lt;br /&gt;
		-fname path to ancestral population frequencies file&lt;br /&gt;
	       	-qname path to admixture proportions file&lt;br /&gt;
		&lt;br /&gt;
	Optional:       &lt;br /&gt;
	&lt;br /&gt;
	       -o name of the output file&lt;br /&gt;
	       &lt;br /&gt;
	 Setup (optional):&lt;br /&gt;
	 &lt;br /&gt;
	       -P 1 number of threads&lt;br /&gt;
	       -autosomeMax 23	 autosome ends with this chromsome&lt;br /&gt;
	       -nIts 5	 number of iterations to do for frequency correction; if set to 0 calculates correlation without correction (fast but biased)&lt;br /&gt;
	       -useSites 1.0	 proportion of sites to use to calculate correlation of residuals&lt;br /&gt;
	       -useInds filename     path to tab delimited file with first column containing all individuals ID and second column with 1 to include individual in analysis and 0 otherwise (default all individuals are included)&lt;br /&gt;
	       -misTol 0.05 	 tolerance for considering site as missing when using genotype likelihoods. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
	       -minMaf 0.05 	 minimum minor allele frequency to keep site. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Input Files==&lt;br /&gt;
&lt;br /&gt;
===Plink===&lt;br /&gt;
Genotype data files in binary PLINK format (.bed .fam .bim).&lt;br /&gt;
===Beagle genotype likelhoods===&lt;br /&gt;
The input file contains genotype likelihoods in a .beagle file format [http://faculty.washington.edu/browning/beagle/beagle.html].&lt;br /&gt;
and can be compressed with gzip.&lt;br /&gt;
==== BAM files  ====&lt;br /&gt;
If you have BAM files you can use [[ANGSD]] to produce genotype likelihoods in .beagle format. Please &lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Beagle_input Creation of Beagle files with ANGSD]&lt;br /&gt;
&lt;br /&gt;
==== VCF files ====&lt;br /&gt;
If you already have made a VCF file that contains genotype likehood information then  it should be possible to convert .vcf files with genotype likelihoods to .beagle file via vcftools [https://vcftools.github.io/man_latest.html] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vcftools --vcf input.vcf --out test --BEAGLE-GL --chr 1,2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Chromosome has to be specified.&lt;br /&gt;
&lt;br /&gt;
You can also use bcftools' [https://samtools.github.io/bcftools/bcftools.html] 'query' option for generating a .beagle file from a .vcf file.&lt;br /&gt;
&lt;br /&gt;
==Output File==&lt;br /&gt;
The analysis performed by evalAdmix produces one file, containing a tab delimited N times N symmetric correlation matrix, where column i in line j contains the correlation of residuals between individual i and j, and the diagonal values (self-correlation) are set to NA:&lt;br /&gt;
&lt;br /&gt;
NA      0.008609        -0.006919       0.002731        0.020224&amp;lt;br /&amp;gt;&lt;br /&gt;
0.008609        NA      0.000033        0.004968        -0.008470&amp;lt;br /&amp;gt;&lt;br /&gt;
-0.006919       0.000033        NA      0.006982        0.005664&amp;lt;br /&amp;gt;&lt;br /&gt;
0.002731        0.004968        0.006982        NA      0.000521&amp;lt;br /&amp;gt;&lt;br /&gt;
0.020224        -0.008470       0.005664        0.000521        NA&lt;br /&gt;
&lt;br /&gt;
==Run command example==&lt;br /&gt;
&lt;br /&gt;
=== Genotype data ===&lt;br /&gt;
Download the input file containing genotypes in binary plink format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bed&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bim&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.fam&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run ADMIXTURE [http://software.genetics.ucla.edu/admixture/] to obtain admixture proprotions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;admixture admixTjeck2.bed 3 -j20&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2.bed&lt;br /&gt;
::Number of Ancestral Populations K = 3&lt;br /&gt;
::Number of CPU threads = 20&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -plink admixTjeck2 -fname admixTjeck2.3.P -qname admixTjeck2.3.Q -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2 (-plink admixTjeck2).&lt;br /&gt;
::Ancestral Populations frequency file admixTjeck2.3.P (-fname admixTjeck2.3.P).&lt;br /&gt;
::Admixture proportions file admixTjeck2.3.Q (-qname admixTjeck2.3.Q).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;admixTjeck2.fam&amp;quot;)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;admixTjeck2.3.Q&amp;quot;,stringsAsFactors=T)&lt;br /&gt;
&lt;br /&gt;
palette(c(&amp;quot;#A6CEE3&amp;quot;,&amp;quot;#1F78B4&amp;quot;,&amp;quot;#B2DF8A&amp;quot;,&amp;quot;#33A02C&amp;quot;,&amp;quot;#FB9A99&amp;quot;,&amp;quot;#E31A1C&amp;quot;,&amp;quot;#FDBF6F&amp;quot;,&amp;quot;#FF7F00&amp;quot;,&amp;quot;#CAB2D6&amp;quot;,&amp;quot;#6A3D9A&amp;quot;,&amp;quot;#FFFF99&amp;quot;,&amp;quot;#B15928&amp;quot;,&amp;quot;#1B9E77&amp;quot;,&amp;quot;#999999&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the ADMIXTURE reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,2]), q = q)&lt;br /&gt;
&lt;br /&gt;
#make barplot&lt;br /&gt;
plotAdmix(q,ord=ord,pop=pop[,2])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-as.matrix(read.table(&amp;quot;output.corres.txt&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,2]), ord=ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
[[File:evalAdmixK3.Q.png|thumb|frame|admixture proportions]]&lt;br /&gt;
[[File:evalAdmixK3.cor.png|thumb|frame|evalAdmix correlations]]&lt;br /&gt;
&lt;br /&gt;
=== Low depth sequencing data ===&lt;br /&gt;
Download the input file containing genotype likelihoods in beagle format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2pop.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Execute [[NgsAdmix |NGSadmix]] to obtain admixture proportions&lt;br /&gt;
&amp;lt;pre&amp;gt;./NGSadmix -likes Demo2input.gz -K 3 -P 20 -o myoutfiles -minMaf 0.05&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Input file = Demo2input.gz&lt;br /&gt;
::Ancestral Populations K=3&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
::Output prefix = myoutfiles (-o myoutfiles) &lt;br /&gt;
::SNPs with MAF &amp;gt; 5%  (-minMaf 0.05)&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -beagle Demo2input.gz -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotype likelihoods file Demo2input.gz (-beagle Demo2input.gz).&lt;br /&gt;
::Ancestral Populations frequency file myoutfiles.fopt.gz (-fname myoutfiles.fopt.gz).&lt;br /&gt;
::Admixture proportions file  myoutfiles.qopt(-qname myoutfiles.qopt).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo2pop.info&amp;quot;,as.is=T)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;myoutfiles.qopt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the NGSadmix reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,1]), q = q)&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Demo2 Admixture proportions for K=3&amp;quot;)&lt;br /&gt;
text(sort(tapply(1:nrow(pop),pop[ord,1],mean)),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-read.table(&amp;quot;output.corres.txt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,1]), ord = ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Citation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://doi.org/10.1111/1755-0998.13171 Evaluation of Model Fit of Inferred Admixture Proportions]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1472</id>
		<title>EvalAdmix</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1472"/>
		<updated>2022-12-27T09:16:14Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Run command example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''IMPORTANT''': version 0.95 (updated on 30/06/2021) fixes a bug in the implementation for genotype data, which caused displacement of genotypes between samples when a site had missing data. When all sites have some missingness this would result in the last samples from the analyses having a correlation of nan with all other samples; but might have some more subtle effects whenever there is some level of missingness. If you have analyses from previous versions based on genotype data with any missingness might be a good idea to re-run them after updating. The bug did not affect the genotype likelihoods implementation so if you based the analyses on genotype likelihoods you do not need to worry. If you applied it to gentoype data without any missingness you also do not need to worry.&lt;br /&gt;
&lt;br /&gt;
evalAdmix allows to evaluate the results of an admixture analysis (i.e. the result of applying [https://genome.cshlp.org/content/19/9/1655.long ADMIXTURE], [https://web.stanford.edu/group/pritchardlab/structure.html STRUCTURE], [http://www.popgen.dk/software/index.php/NgsAdmix NGSadmix] and similar). It only needs the input genotype data used for the previous admixture analysis and the output of that analysis (admixture proportions and ancestral population frequencies). The genotype input data can either be called genotypes in [https://www.cog-genomics.org/plink/1.9/formats#bed binary plink format] or genotype likelihoods in [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Beagle_format beagle format].&lt;br /&gt;
&lt;br /&gt;
The output is a pairwise correlation of residuals matrix between individuals. The correlation will be close to 0 in case of a good fit of the data to the admixture model. When individuals do not fit the model, individuals with similar demographic histories (i.e. usually individuals from the same population) will be positively correlated; and individuals with different histories but that are modelled as sharing one or more ancestral populations as admixture sources will have a negative correlation. Positive correlation between a pair of individuals might also be due to relatedness.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:evalAdmix.png|thumb|550px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Download and Installation==&lt;br /&gt;
&lt;br /&gt;
evalAdmix can be installed from [https://github.com/GenisGE/evalAdmix github] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;git clone https://github.com/GenisGE/evalAdmix.git&lt;br /&gt;
cd evalAdmix&lt;br /&gt;
make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quick start==&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -beagle inputBeagleFile.gz  -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -plink inputPlinkPrefix  -fname inputPlinkPrefix.K.P -qname inputPlinkPrefix.K.Q -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''-beagle''' beagle file of genotype likelihoods&lt;br /&gt;
* '''-plink''' binary plink file prefix with genotype data&lt;br /&gt;
* '''-fname''' file with ancestral frequencies (space delimited, rows are sites and columns ancestral populations)&lt;br /&gt;
* '''-qname''' file with admixture proportions (space delimited, rows are individuals and columns ancestral populations)&lt;br /&gt;
* '''-o''' prefix of output file names&lt;br /&gt;
* '''-P''' Number of threads used&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Arguments:&lt;br /&gt;
	Required:&lt;br /&gt;
		-plink path to binary plink file (excluding the .bed)&lt;br /&gt;
		or&lt;br /&gt;
	      	-beagle path to beagle file containing genotype likelihoods (alternative to -plink)&lt;br /&gt;
		&lt;br /&gt;
		-fname path to ancestral population frequencies file&lt;br /&gt;
	       	-qname path to admixture proportions file&lt;br /&gt;
		&lt;br /&gt;
	Optional:       &lt;br /&gt;
	&lt;br /&gt;
	       -o name of the output file&lt;br /&gt;
	       &lt;br /&gt;
	 Setup (optional):&lt;br /&gt;
	 &lt;br /&gt;
	       -P 1 number of threads&lt;br /&gt;
	       -autosomeMax 23	 autosome ends with this chromsome&lt;br /&gt;
	       -nIts 5	 number of iterations to do for frequency correction; if set to 0 calculates correlation without correction (fast but biased)&lt;br /&gt;
	       -useSites 1.0	 proportion of sites to use to calculate correlation of residuals&lt;br /&gt;
	       -useInds filename     path to tab delimited file with first column containing all individuals ID and second column with 1 to include individual in analysis and 0 otherwise (default all individuals are included)&lt;br /&gt;
	       -misTol 0.05 	 tolerance for considering site as missing when using genotype likelihoods. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
	       -minMaf 0.05 	 minimum minor allele frequency to keep site. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Input Files==&lt;br /&gt;
&lt;br /&gt;
===Plink===&lt;br /&gt;
Genotype data files in binary PLINK format (.bed .fam .bim).&lt;br /&gt;
===Beagle genotype likelhoods===&lt;br /&gt;
The input file contains genotype likelihoods in a .beagle file format [http://faculty.washington.edu/browning/beagle/beagle.html].&lt;br /&gt;
and can be compressed with gzip.&lt;br /&gt;
==== BAM files  ====&lt;br /&gt;
If you have BAM files you can use [[ANGSD]] to produce genotype likelihoods in .beagle format. Please &lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Beagle_input Creation of Beagle files with ANGSD]&lt;br /&gt;
&lt;br /&gt;
==== VCF files ====&lt;br /&gt;
If you already have made a VCF file that contains genotype likehood information then  it should be possible to convert .vcf files with genotype likelihoods to .beagle file via vcftools [https://vcftools.github.io/man_latest.html] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vcftools --vcf input.vcf --out test --BEAGLE-GL --chr 1,2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Chromosome has to be specified.&lt;br /&gt;
&lt;br /&gt;
You can also use bcftools' [https://samtools.github.io/bcftools/bcftools.html] 'query' option for generating a .beagle file from a .vcf file.&lt;br /&gt;
&lt;br /&gt;
==Output File==&lt;br /&gt;
The analysis performed by evalAdmix produces one file, containing a tab delimited N times N symmetric correlation matrix, where column i in line j contains the correlation of residuals between individual i and j, and the diagonal values (self-correlation) are set to NA:&lt;br /&gt;
&lt;br /&gt;
NA      0.008609        -0.006919       0.002731        0.020224&amp;lt;br /&amp;gt;&lt;br /&gt;
0.008609        NA      0.000033        0.004968        -0.008470&amp;lt;br /&amp;gt;&lt;br /&gt;
-0.006919       0.000033        NA      0.006982        0.005664&amp;lt;br /&amp;gt;&lt;br /&gt;
0.002731        0.004968        0.006982        NA      0.000521&amp;lt;br /&amp;gt;&lt;br /&gt;
0.020224        -0.008470       0.005664        0.000521        NA&lt;br /&gt;
&lt;br /&gt;
==Run command example==&lt;br /&gt;
&lt;br /&gt;
=== Genotype data ===&lt;br /&gt;
Download the input file containing genotypes in binary plink format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bed&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bim&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.fam&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run ADMIXTURE [http://software.genetics.ucla.edu/admixture/] to obtain admixture proprotions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;admixture admixTjeck2.bed 3 -j20&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2.bed&lt;br /&gt;
::Number of Ancestral Populations K = 3&lt;br /&gt;
::Number of CPU threads = 20&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -plink admixTjeck2 -fname admixTjeck2.3.P -qname admixTjeck2.3.Q -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2 (-plink admixTjeck2).&lt;br /&gt;
::Ancestral Populations frequency file admixTjeck2.3.P (-fname admixTjeck2.3.P).&lt;br /&gt;
::Admixture proportions file admixTjeck2.3.Q (-qname admixTjeck2.3.Q).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;admixTjeck2.fam&amp;quot;)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;admixTjeck2.3.Q&amp;quot;,stringsAsFactors=T)&lt;br /&gt;
&lt;br /&gt;
palette(c(&amp;quot;#A6CEE3&amp;quot;,&amp;quot;#1F78B4&amp;quot;,&amp;quot;#B2DF8A&amp;quot;,&amp;quot;#33A02C&amp;quot;,&amp;quot;#FB9A99&amp;quot;,&amp;quot;#E31A1C&amp;quot;,&amp;quot;#FDBF6F&amp;quot;,&amp;quot;#FF7F00&amp;quot;,&amp;quot;#CAB2D6&amp;quot;,&amp;quot;#6A3D9A&amp;quot;,&amp;quot;#FFFF99&amp;quot;,&amp;quot;#B15928&amp;quot;,&amp;quot;#1B9E77&amp;quot;,&amp;quot;#999999&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the ADMIXTURE reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,2]), q = q)&lt;br /&gt;
&lt;br /&gt;
#make barplot&lt;br /&gt;
plotAdmix(q,ord=ord,pop=pop[,2])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-as.matrix(read.table(&amp;quot;output.corres.txt&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,2]), ord=ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Low depth sequencing data ===&lt;br /&gt;
Download the input file containing genotype likelihoods in beagle format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2pop.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Execute [[NgsAdmix |NGSadmix]] to obtain admixture proportions&lt;br /&gt;
&amp;lt;pre&amp;gt;./NGSadmix -likes Demo2input.gz -K 3 -P 20 -o myoutfiles -minMaf 0.05&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Input file = Demo2input.gz&lt;br /&gt;
::Ancestral Populations K=3&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
::Output prefix = myoutfiles (-o myoutfiles) &lt;br /&gt;
::SNPs with MAF &amp;gt; 5%  (-minMaf 0.05)&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -beagle Demo2input.gz -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotype likelihoods file Demo2input.gz (-beagle Demo2input.gz).&lt;br /&gt;
::Ancestral Populations frequency file myoutfiles.fopt.gz (-fname myoutfiles.fopt.gz).&lt;br /&gt;
::Admixture proportions file  myoutfiles.qopt(-qname myoutfiles.qopt).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo2pop.info&amp;quot;,as.is=T)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;myoutfiles.qopt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the NGSadmix reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,1]), q = q)&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Demo2 Admixture proportions for K=3&amp;quot;)&lt;br /&gt;
text(sort(tapply(1:nrow(pop),pop[ord,1],mean)),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-read.table(&amp;quot;output.corres.txt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,1]), ord = ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Citation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://doi.org/10.1111/1755-0998.13171 Evaluation of Model Fit of Inferred Admixture Proportions]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1471</id>
		<title>EvalAdmix</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1471"/>
		<updated>2022-12-27T09:13:09Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Genotype data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''IMPORTANT''': version 0.95 (updated on 30/06/2021) fixes a bug in the implementation for genotype data, which caused displacement of genotypes between samples when a site had missing data. When all sites have some missingness this would result in the last samples from the analyses having a correlation of nan with all other samples; but might have some more subtle effects whenever there is some level of missingness. If you have analyses from previous versions based on genotype data with any missingness might be a good idea to re-run them after updating. The bug did not affect the genotype likelihoods implementation so if you based the analyses on genotype likelihoods you do not need to worry. If you applied it to gentoype data without any missingness you also do not need to worry.&lt;br /&gt;
&lt;br /&gt;
evalAdmix allows to evaluate the results of an admixture analysis (i.e. the result of applying [https://genome.cshlp.org/content/19/9/1655.long ADMIXTURE], [https://web.stanford.edu/group/pritchardlab/structure.html STRUCTURE], [http://www.popgen.dk/software/index.php/NgsAdmix NGSadmix] and similar). It only needs the input genotype data used for the previous admixture analysis and the output of that analysis (admixture proportions and ancestral population frequencies). The genotype input data can either be called genotypes in [https://www.cog-genomics.org/plink/1.9/formats#bed binary plink format] or genotype likelihoods in [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Beagle_format beagle format].&lt;br /&gt;
&lt;br /&gt;
The output is a pairwise correlation of residuals matrix between individuals. The correlation will be close to 0 in case of a good fit of the data to the admixture model. When individuals do not fit the model, individuals with similar demographic histories (i.e. usually individuals from the same population) will be positively correlated; and individuals with different histories but that are modelled as sharing one or more ancestral populations as admixture sources will have a negative correlation. Positive correlation between a pair of individuals might also be due to relatedness.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:evalAdmix.png|thumb|550px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Download and Installation==&lt;br /&gt;
&lt;br /&gt;
evalAdmix can be installed from [https://github.com/GenisGE/evalAdmix github] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;git clone https://github.com/GenisGE/evalAdmix.git&lt;br /&gt;
cd evalAdmix&lt;br /&gt;
make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quick start==&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -beagle inputBeagleFile.gz  -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -plink inputPlinkPrefix  -fname inputPlinkPrefix.K.P -qname inputPlinkPrefix.K.Q -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''-beagle''' beagle file of genotype likelihoods&lt;br /&gt;
* '''-plink''' binary plink file prefix with genotype data&lt;br /&gt;
* '''-fname''' file with ancestral frequencies (space delimited, rows are sites and columns ancestral populations)&lt;br /&gt;
* '''-qname''' file with admixture proportions (space delimited, rows are individuals and columns ancestral populations)&lt;br /&gt;
* '''-o''' prefix of output file names&lt;br /&gt;
* '''-P''' Number of threads used&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Arguments:&lt;br /&gt;
	Required:&lt;br /&gt;
		-plink path to binary plink file (excluding the .bed)&lt;br /&gt;
		or&lt;br /&gt;
	      	-beagle path to beagle file containing genotype likelihoods (alternative to -plink)&lt;br /&gt;
		&lt;br /&gt;
		-fname path to ancestral population frequencies file&lt;br /&gt;
	       	-qname path to admixture proportions file&lt;br /&gt;
		&lt;br /&gt;
	Optional:       &lt;br /&gt;
	&lt;br /&gt;
	       -o name of the output file&lt;br /&gt;
	       &lt;br /&gt;
	 Setup (optional):&lt;br /&gt;
	 &lt;br /&gt;
	       -P 1 number of threads&lt;br /&gt;
	       -autosomeMax 23	 autosome ends with this chromsome&lt;br /&gt;
	       -nIts 5	 number of iterations to do for frequency correction; if set to 0 calculates correlation without correction (fast but biased)&lt;br /&gt;
	       -useSites 1.0	 proportion of sites to use to calculate correlation of residuals&lt;br /&gt;
	       -useInds filename     path to tab delimited file with first column containing all individuals ID and second column with 1 to include individual in analysis and 0 otherwise (default all individuals are included)&lt;br /&gt;
	       -misTol 0.05 	 tolerance for considering site as missing when using genotype likelihoods. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
	       -minMaf 0.05 	 minimum minor allele frequency to keep site. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Input Files==&lt;br /&gt;
&lt;br /&gt;
===Plink===&lt;br /&gt;
Genotype data files in binary PLINK format (.bed .fam .bim).&lt;br /&gt;
===Beagle genotype likelhoods===&lt;br /&gt;
The input file contains genotype likelihoods in a .beagle file format [http://faculty.washington.edu/browning/beagle/beagle.html].&lt;br /&gt;
and can be compressed with gzip.&lt;br /&gt;
==== BAM files  ====&lt;br /&gt;
If you have BAM files you can use [[ANGSD]] to produce genotype likelihoods in .beagle format. Please &lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Beagle_input Creation of Beagle files with ANGSD]&lt;br /&gt;
&lt;br /&gt;
==== VCF files ====&lt;br /&gt;
If you already have made a VCF file that contains genotype likehood information then  it should be possible to convert .vcf files with genotype likelihoods to .beagle file via vcftools [https://vcftools.github.io/man_latest.html] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vcftools --vcf input.vcf --out test --BEAGLE-GL --chr 1,2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Chromosome has to be specified.&lt;br /&gt;
&lt;br /&gt;
You can also use bcftools' [https://samtools.github.io/bcftools/bcftools.html] 'query' option for generating a .beagle file from a .vcf file.&lt;br /&gt;
&lt;br /&gt;
==Output File==&lt;br /&gt;
The analysis performed by evalAdmix produces one file, containing a tab delimited N times N symmetric correlation matrix, where column i in line j contains the correlation of residuals between individual i and j, and the diagonal values (self-correlation) are set to NA:&lt;br /&gt;
&lt;br /&gt;
NA      0.008609        -0.006919       0.002731        0.020224&amp;lt;br /&amp;gt;&lt;br /&gt;
0.008609        NA      0.000033        0.004968        -0.008470&amp;lt;br /&amp;gt;&lt;br /&gt;
-0.006919       0.000033        NA      0.006982        0.005664&amp;lt;br /&amp;gt;&lt;br /&gt;
0.002731        0.004968        0.006982        NA      0.000521&amp;lt;br /&amp;gt;&lt;br /&gt;
0.020224        -0.008470       0.005664        0.000521        NA&lt;br /&gt;
&lt;br /&gt;
==Run command example==&lt;br /&gt;
&lt;br /&gt;
=== Genotype data ===&lt;br /&gt;
Download the input file containing genotypes in binary plink format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bed&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bim&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.fam&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run ADMIXTURE [http://software.genetics.ucla.edu/admixture/] to obtain admixture proprotions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;admixture admixTjeck2.bed 3 -j20&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2.bed&lt;br /&gt;
::Number of Ancestral Populations K = 3&lt;br /&gt;
::Number of CPU threads = 20&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -plink admixTjeck2 -fname admixTjeck2.3.P -qname admixTjeck2.3.Q -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2 (-plink admixTjeck2).&lt;br /&gt;
::Ancestral Populations frequency file admixTjeck2.3.P (-fname admixTjeck2.3.P).&lt;br /&gt;
::Admixture proportions file admixTjeck2.3.Q (-qname admixTjeck2.3.Q).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;admixTjeck2.fam&amp;quot;)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;admixTjeck2.3.Q&amp;quot;,stringsAsFactors=T)&lt;br /&gt;
&lt;br /&gt;
palette(c(&amp;quot;#A6CEE3&amp;quot;,&amp;quot;#1F78B4&amp;quot;,&amp;quot;#B2DF8A&amp;quot;,&amp;quot;#33A02C&amp;quot;,&amp;quot;#FB9A99&amp;quot;,&amp;quot;#E31A1C&amp;quot;,&amp;quot;#FDBF6F&amp;quot;,&amp;quot;#FF7F00&amp;quot;,&amp;quot;#CAB2D6&amp;quot;,&amp;quot;#6A3D9A&amp;quot;,&amp;quot;#FFFF99&amp;quot;,&amp;quot;#B15928&amp;quot;,&amp;quot;#1B9E77&amp;quot;,&amp;quot;#999999&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the ADMIXTURE reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,2]), q = q)&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Demo2 Admixture proportions for K=3&amp;quot;)&lt;br /&gt;
text(sort(tapply(1:nrow(pop),pop[ord,2],mean)),-0.05,unique(pop[ord,2]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,2]),function(x){sum(pop[ord,2]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-as.matrix(read.table(&amp;quot;output.corres.txt&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,2]), ord=ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Low depth sequencing data ===&lt;br /&gt;
Download the input file containing genotype likelihoods in beagle format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2pop.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Execute [[NgsAdmix |NGSadmix]] to obtain admixture proportions&lt;br /&gt;
&amp;lt;pre&amp;gt;./NGSadmix -likes Demo2input.gz -K 3 -P 20 -o myoutfiles -minMaf 0.05&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Input file = Demo2input.gz&lt;br /&gt;
::Ancestral Populations K=3&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
::Output prefix = myoutfiles (-o myoutfiles) &lt;br /&gt;
::SNPs with MAF &amp;gt; 5%  (-minMaf 0.05)&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -beagle Demo2input.gz -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotype likelihoods file Demo2input.gz (-beagle Demo2input.gz).&lt;br /&gt;
::Ancestral Populations frequency file myoutfiles.fopt.gz (-fname myoutfiles.fopt.gz).&lt;br /&gt;
::Admixture proportions file  myoutfiles.qopt(-qname myoutfiles.qopt).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo2pop.info&amp;quot;,as.is=T)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;myoutfiles.qopt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the NGSadmix reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,1]), q = q)&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Demo2 Admixture proportions for K=3&amp;quot;)&lt;br /&gt;
text(sort(tapply(1:nrow(pop),pop[ord,1],mean)),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-read.table(&amp;quot;output.corres.txt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,1]), ord = ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Citation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://doi.org/10.1111/1755-0998.13171 Evaluation of Model Fit of Inferred Admixture Proportions]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1470</id>
		<title>EvalAdmix</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1470"/>
		<updated>2022-12-27T09:02:57Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Genotype data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''IMPORTANT''': version 0.95 (updated on 30/06/2021) fixes a bug in the implementation for genotype data, which caused displacement of genotypes between samples when a site had missing data. When all sites have some missingness this would result in the last samples from the analyses having a correlation of nan with all other samples; but might have some more subtle effects whenever there is some level of missingness. If you have analyses from previous versions based on genotype data with any missingness might be a good idea to re-run them after updating. The bug did not affect the genotype likelihoods implementation so if you based the analyses on genotype likelihoods you do not need to worry. If you applied it to gentoype data without any missingness you also do not need to worry.&lt;br /&gt;
&lt;br /&gt;
evalAdmix allows to evaluate the results of an admixture analysis (i.e. the result of applying [https://genome.cshlp.org/content/19/9/1655.long ADMIXTURE], [https://web.stanford.edu/group/pritchardlab/structure.html STRUCTURE], [http://www.popgen.dk/software/index.php/NgsAdmix NGSadmix] and similar). It only needs the input genotype data used for the previous admixture analysis and the output of that analysis (admixture proportions and ancestral population frequencies). The genotype input data can either be called genotypes in [https://www.cog-genomics.org/plink/1.9/formats#bed binary plink format] or genotype likelihoods in [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Beagle_format beagle format].&lt;br /&gt;
&lt;br /&gt;
The output is a pairwise correlation of residuals matrix between individuals. The correlation will be close to 0 in case of a good fit of the data to the admixture model. When individuals do not fit the model, individuals with similar demographic histories (i.e. usually individuals from the same population) will be positively correlated; and individuals with different histories but that are modelled as sharing one or more ancestral populations as admixture sources will have a negative correlation. Positive correlation between a pair of individuals might also be due to relatedness.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:evalAdmix.png|thumb|550px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Download and Installation==&lt;br /&gt;
&lt;br /&gt;
evalAdmix can be installed from [https://github.com/GenisGE/evalAdmix github] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;git clone https://github.com/GenisGE/evalAdmix.git&lt;br /&gt;
cd evalAdmix&lt;br /&gt;
make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quick start==&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -beagle inputBeagleFile.gz  -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -plink inputPlinkPrefix  -fname inputPlinkPrefix.K.P -qname inputPlinkPrefix.K.Q -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''-beagle''' beagle file of genotype likelihoods&lt;br /&gt;
* '''-plink''' binary plink file prefix with genotype data&lt;br /&gt;
* '''-fname''' file with ancestral frequencies (space delimited, rows are sites and columns ancestral populations)&lt;br /&gt;
* '''-qname''' file with admixture proportions (space delimited, rows are individuals and columns ancestral populations)&lt;br /&gt;
* '''-o''' prefix of output file names&lt;br /&gt;
* '''-P''' Number of threads used&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Arguments:&lt;br /&gt;
	Required:&lt;br /&gt;
		-plink path to binary plink file (excluding the .bed)&lt;br /&gt;
		or&lt;br /&gt;
	      	-beagle path to beagle file containing genotype likelihoods (alternative to -plink)&lt;br /&gt;
		&lt;br /&gt;
		-fname path to ancestral population frequencies file&lt;br /&gt;
	       	-qname path to admixture proportions file&lt;br /&gt;
		&lt;br /&gt;
	Optional:       &lt;br /&gt;
	&lt;br /&gt;
	       -o name of the output file&lt;br /&gt;
	       &lt;br /&gt;
	 Setup (optional):&lt;br /&gt;
	 &lt;br /&gt;
	       -P 1 number of threads&lt;br /&gt;
	       -autosomeMax 23	 autosome ends with this chromsome&lt;br /&gt;
	       -nIts 5	 number of iterations to do for frequency correction; if set to 0 calculates correlation without correction (fast but biased)&lt;br /&gt;
	       -useSites 1.0	 proportion of sites to use to calculate correlation of residuals&lt;br /&gt;
	       -useInds filename     path to tab delimited file with first column containing all individuals ID and second column with 1 to include individual in analysis and 0 otherwise (default all individuals are included)&lt;br /&gt;
	       -misTol 0.05 	 tolerance for considering site as missing when using genotype likelihoods. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
	       -minMaf 0.05 	 minimum minor allele frequency to keep site. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Input Files==&lt;br /&gt;
&lt;br /&gt;
===Plink===&lt;br /&gt;
Genotype data files in binary PLINK format (.bed .fam .bim).&lt;br /&gt;
===Beagle genotype likelhoods===&lt;br /&gt;
The input file contains genotype likelihoods in a .beagle file format [http://faculty.washington.edu/browning/beagle/beagle.html].&lt;br /&gt;
and can be compressed with gzip.&lt;br /&gt;
==== BAM files  ====&lt;br /&gt;
If you have BAM files you can use [[ANGSD]] to produce genotype likelihoods in .beagle format. Please &lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Beagle_input Creation of Beagle files with ANGSD]&lt;br /&gt;
&lt;br /&gt;
==== VCF files ====&lt;br /&gt;
If you already have made a VCF file that contains genotype likehood information then  it should be possible to convert .vcf files with genotype likelihoods to .beagle file via vcftools [https://vcftools.github.io/man_latest.html] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vcftools --vcf input.vcf --out test --BEAGLE-GL --chr 1,2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Chromosome has to be specified.&lt;br /&gt;
&lt;br /&gt;
You can also use bcftools' [https://samtools.github.io/bcftools/bcftools.html] 'query' option for generating a .beagle file from a .vcf file.&lt;br /&gt;
&lt;br /&gt;
==Output File==&lt;br /&gt;
The analysis performed by evalAdmix produces one file, containing a tab delimited N times N symmetric correlation matrix, where column i in line j contains the correlation of residuals between individual i and j, and the diagonal values (self-correlation) are set to NA:&lt;br /&gt;
&lt;br /&gt;
NA      0.008609        -0.006919       0.002731        0.020224&amp;lt;br /&amp;gt;&lt;br /&gt;
0.008609        NA      0.000033        0.004968        -0.008470&amp;lt;br /&amp;gt;&lt;br /&gt;
-0.006919       0.000033        NA      0.006982        0.005664&amp;lt;br /&amp;gt;&lt;br /&gt;
0.002731        0.004968        0.006982        NA      0.000521&amp;lt;br /&amp;gt;&lt;br /&gt;
0.020224        -0.008470       0.005664        0.000521        NA&lt;br /&gt;
&lt;br /&gt;
==Run command example==&lt;br /&gt;
&lt;br /&gt;
=== Genotype data ===&lt;br /&gt;
Download the input file containing genotypes in binary plink format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bed&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bim&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.fam&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run ADMIXTURE [http://software.genetics.ucla.edu/admixture/] to obtain admixture proprotions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;admixture admixTjeck2.bed 3 -j20&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2.bed&lt;br /&gt;
::Number of Ancestral Populations K = 3&lt;br /&gt;
::Number of CPU threads = 20&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -plink admixTjeck2 -fname admixTjeck2.3.P -qname admixTjeck2.3.Q -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2 (-plink admixTjeck2).&lt;br /&gt;
::Ancestral Populations frequency file admixTjeck2.3.P (-fname admixTjeck2.3.P).&lt;br /&gt;
::Admixture proportions file admixTjeck2.3.Q (-qname admixTjeck2.3.Q).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;admixTjeck2.fam&amp;quot;)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;admixTjeck2.3.Q&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the ADMIXTURE reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,2]), q = q)&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Demo2 Admixture proportions for K=3&amp;quot;)&lt;br /&gt;
text(sort(tapply(1:nrow(pop),pop[ord,2],mean)),-0.05,unique(pop[ord,2]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,2]),function(x){sum(pop[ord,2]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-as.matrix(read.table(&amp;quot;output.corres.txt&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,2]), ord=ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Low depth sequencing data ===&lt;br /&gt;
Download the input file containing genotype likelihoods in beagle format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2pop.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Execute [[NgsAdmix |NGSadmix]] to obtain admixture proportions&lt;br /&gt;
&amp;lt;pre&amp;gt;./NGSadmix -likes Demo2input.gz -K 3 -P 20 -o myoutfiles -minMaf 0.05&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Input file = Demo2input.gz&lt;br /&gt;
::Ancestral Populations K=3&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
::Output prefix = myoutfiles (-o myoutfiles) &lt;br /&gt;
::SNPs with MAF &amp;gt; 5%  (-minMaf 0.05)&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -beagle Demo2input.gz -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotype likelihoods file Demo2input.gz (-beagle Demo2input.gz).&lt;br /&gt;
::Ancestral Populations frequency file myoutfiles.fopt.gz (-fname myoutfiles.fopt.gz).&lt;br /&gt;
::Admixture proportions file  myoutfiles.qopt(-qname myoutfiles.qopt).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo2pop.info&amp;quot;,as.is=T)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;myoutfiles.qopt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the NGSadmix reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,1]), q = q)&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Demo2 Admixture proportions for K=3&amp;quot;)&lt;br /&gt;
text(sort(tapply(1:nrow(pop),pop[ord,1],mean)),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-read.table(&amp;quot;output.corres.txt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,1]), ord = ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Citation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://doi.org/10.1111/1755-0998.13171 Evaluation of Model Fit of Inferred Admixture Proportions]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1469</id>
		<title>EvalAdmix</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=EvalAdmix&amp;diff=1469"/>
		<updated>2022-12-27T09:02:24Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Genotype data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''IMPORTANT''': version 0.95 (updated on 30/06/2021) fixes a bug in the implementation for genotype data, which caused displacement of genotypes between samples when a site had missing data. When all sites have some missingness this would result in the last samples from the analyses having a correlation of nan with all other samples; but might have some more subtle effects whenever there is some level of missingness. If you have analyses from previous versions based on genotype data with any missingness might be a good idea to re-run them after updating. The bug did not affect the genotype likelihoods implementation so if you based the analyses on genotype likelihoods you do not need to worry. If you applied it to gentoype data without any missingness you also do not need to worry.&lt;br /&gt;
&lt;br /&gt;
evalAdmix allows to evaluate the results of an admixture analysis (i.e. the result of applying [https://genome.cshlp.org/content/19/9/1655.long ADMIXTURE], [https://web.stanford.edu/group/pritchardlab/structure.html STRUCTURE], [http://www.popgen.dk/software/index.php/NgsAdmix NGSadmix] and similar). It only needs the input genotype data used for the previous admixture analysis and the output of that analysis (admixture proportions and ancestral population frequencies). The genotype input data can either be called genotypes in [https://www.cog-genomics.org/plink/1.9/formats#bed binary plink format] or genotype likelihoods in [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Beagle_format beagle format].&lt;br /&gt;
&lt;br /&gt;
The output is a pairwise correlation of residuals matrix between individuals. The correlation will be close to 0 in case of a good fit of the data to the admixture model. When individuals do not fit the model, individuals with similar demographic histories (i.e. usually individuals from the same population) will be positively correlated; and individuals with different histories but that are modelled as sharing one or more ancestral populations as admixture sources will have a negative correlation. Positive correlation between a pair of individuals might also be due to relatedness.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:evalAdmix.png|thumb|550px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Download and Installation==&lt;br /&gt;
&lt;br /&gt;
evalAdmix can be installed from [https://github.com/GenisGE/evalAdmix github] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;git clone https://github.com/GenisGE/evalAdmix.git&lt;br /&gt;
cd evalAdmix&lt;br /&gt;
make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quick start==&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -beagle inputBeagleFile.gz  -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
:&amp;lt;code&amp;gt; ./evalAdmix -plink inputPlinkPrefix  -fname inputPlinkPrefix.K.P -qname inputPlinkPrefix.K.Q -o evaladmixOut.corres -P 10  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''-beagle''' beagle file of genotype likelihoods&lt;br /&gt;
* '''-plink''' binary plink file prefix with genotype data&lt;br /&gt;
* '''-fname''' file with ancestral frequencies (space delimited, rows are sites and columns ancestral populations)&lt;br /&gt;
* '''-qname''' file with admixture proportions (space delimited, rows are individuals and columns ancestral populations)&lt;br /&gt;
* '''-o''' prefix of output file names&lt;br /&gt;
* '''-P''' Number of threads used&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Arguments:&lt;br /&gt;
	Required:&lt;br /&gt;
		-plink path to binary plink file (excluding the .bed)&lt;br /&gt;
		or&lt;br /&gt;
	      	-beagle path to beagle file containing genotype likelihoods (alternative to -plink)&lt;br /&gt;
		&lt;br /&gt;
		-fname path to ancestral population frequencies file&lt;br /&gt;
	       	-qname path to admixture proportions file&lt;br /&gt;
		&lt;br /&gt;
	Optional:       &lt;br /&gt;
	&lt;br /&gt;
	       -o name of the output file&lt;br /&gt;
	       &lt;br /&gt;
	 Setup (optional):&lt;br /&gt;
	 &lt;br /&gt;
	       -P 1 number of threads&lt;br /&gt;
	       -autosomeMax 23	 autosome ends with this chromsome&lt;br /&gt;
	       -nIts 5	 number of iterations to do for frequency correction; if set to 0 calculates correlation without correction (fast but biased)&lt;br /&gt;
	       -useSites 1.0	 proportion of sites to use to calculate correlation of residuals&lt;br /&gt;
	       -useInds filename     path to tab delimited file with first column containing all individuals ID and second column with 1 to include individual in analysis and 0 otherwise (default all individuals are included)&lt;br /&gt;
	       -misTol 0.05 	 tolerance for considering site as missing when using genotype likelihoods. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
	       -minMaf 0.05 	 minimum minor allele frequency to keep site. Use same value as used in NGSadmix to keep compatibility when using genotype likelihoods (-beagle)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Input Files==&lt;br /&gt;
&lt;br /&gt;
===Plink===&lt;br /&gt;
Genotype data files in binary PLINK format (.bed .fam .bim).&lt;br /&gt;
===Beagle genotype likelhoods===&lt;br /&gt;
The input file contains genotype likelihoods in a .beagle file format [http://faculty.washington.edu/browning/beagle/beagle.html].&lt;br /&gt;
and can be compressed with gzip.&lt;br /&gt;
==== BAM files  ====&lt;br /&gt;
If you have BAM files you can use [[ANGSD]] to produce genotype likelihoods in .beagle format. Please &lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Beagle_input Creation of Beagle files with ANGSD]&lt;br /&gt;
&lt;br /&gt;
==== VCF files ====&lt;br /&gt;
If you already have made a VCF file that contains genotype likehood information then  it should be possible to convert .vcf files with genotype likelihoods to .beagle file via vcftools [https://vcftools.github.io/man_latest.html] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vcftools --vcf input.vcf --out test --BEAGLE-GL --chr 1,2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Chromosome has to be specified.&lt;br /&gt;
&lt;br /&gt;
You can also use bcftools' [https://samtools.github.io/bcftools/bcftools.html] 'query' option for generating a .beagle file from a .vcf file.&lt;br /&gt;
&lt;br /&gt;
==Output File==&lt;br /&gt;
The analysis performed by evalAdmix produces one file, containing a tab delimited N times N symmetric correlation matrix, where column i in line j contains the correlation of residuals between individual i and j, and the diagonal values (self-correlation) are set to NA:&lt;br /&gt;
&lt;br /&gt;
NA      0.008609        -0.006919       0.002731        0.020224&amp;lt;br /&amp;gt;&lt;br /&gt;
0.008609        NA      0.000033        0.004968        -0.008470&amp;lt;br /&amp;gt;&lt;br /&gt;
-0.006919       0.000033        NA      0.006982        0.005664&amp;lt;br /&amp;gt;&lt;br /&gt;
0.002731        0.004968        0.006982        NA      0.000521&amp;lt;br /&amp;gt;&lt;br /&gt;
0.020224        -0.008470       0.005664        0.000521        NA&lt;br /&gt;
&lt;br /&gt;
==Run command example==&lt;br /&gt;
&lt;br /&gt;
=== Genotype data ===&lt;br /&gt;
Download the input file containing genotypes in binary plink format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bed&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.bim&lt;br /&gt;
wget http://pontus.popgen.dk/albrecht/open/admixTjeck/plink/admixTjeck2.fam&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run ADMIXTURE [http://software.genetics.ucla.edu/admixture/] to obtain admixture proprotions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;admixture admixTjeck2.bed 3 -j20&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2.bed&lt;br /&gt;
::Ancestral Populations K = 3&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -plink admixTjeck2 -fname admixTjeck2.3.P -qname admixTjeck2.3.Q -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotypes file prefix admixTjeck2 (-plink admixTjeck2).&lt;br /&gt;
::Ancestral Populations frequency file admixTjeck2.3.P (-fname admixTjeck2.3.P).&lt;br /&gt;
::Admixture proportions file admixTjeck2.3.Q (-qname admixTjeck2.3.Q).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;admixTjeck2.fam&amp;quot;)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;admixTjeck2.3.Q&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the ADMIXTURE reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,2]), q = q)&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Demo2 Admixture proportions for K=3&amp;quot;)&lt;br /&gt;
text(sort(tapply(1:nrow(pop),pop[ord,2],mean)),-0.05,unique(pop[ord,2]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,2]),function(x){sum(pop[ord,2]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-as.matrix(read.table(&amp;quot;output.corres.txt&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,2]), ord=ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Low depth sequencing data ===&lt;br /&gt;
Download the input file containing genotype likelihoods in beagle format&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/Demo2pop.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Execute [[NgsAdmix |NGSadmix]] to obtain admixture proportions&lt;br /&gt;
&amp;lt;pre&amp;gt;./NGSadmix -likes Demo2input.gz -K 3 -P 20 -o myoutfiles -minMaf 0.05&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Input file = Demo2input.gz&lt;br /&gt;
::Ancestral Populations K=3&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
::Output prefix = myoutfiles (-o myoutfiles) &lt;br /&gt;
::SNPs with MAF &amp;gt; 5%  (-minMaf 0.05)&lt;br /&gt;
&lt;br /&gt;
Run evalAdmix&lt;br /&gt;
&amp;lt;pre&amp;gt;./evalAdmix -beagle Demo2input.gz -fname myoutfiles.fopt.gz -qname myoutfiles.qopt -P 20 &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Genotype likelihoods file Demo2input.gz (-beagle Demo2input.gz).&lt;br /&gt;
::Ancestral Populations frequency file myoutfiles.fopt.gz (-fname myoutfiles.fopt.gz).&lt;br /&gt;
::Admixture proportions file  myoutfiles.qopt(-qname myoutfiles.qopt).&lt;br /&gt;
::Computer cores = 20 (-P 20). &lt;br /&gt;
&lt;br /&gt;
Plot results in R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source(&amp;quot;visFuns.R&amp;quot;)&lt;br /&gt;
# read population labels and estimated admixture proportions&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo2pop.info&amp;quot;,as.is=T)&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;myoutfiles.qopt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# order according to population and plot the NGSadmix reults&lt;br /&gt;
ord&amp;lt;-orderInds(pop = as.vector(pop[,1]), q = q)&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Demo2 Admixture proportions for K=3&amp;quot;)&lt;br /&gt;
text(sort(tapply(1:nrow(pop),pop[ord,1],mean)),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
r&amp;lt;-read.table(&amp;quot;output.corres.txt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Plot correlation of residuals&lt;br /&gt;
&lt;br /&gt;
plotCorRes(cor_mat = r, pop = as.vector(pop[,1]), ord = ord, title=&amp;quot;Evaluation of 1000G admixture proportions with K=3&amp;quot;, max_z=0.1, min_z=-0.1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Citation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://doi.org/10.1111/1755-0998.13171 Evaluation of Model Fit of Inferred Admixture Proportions]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1454</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1454"/>
		<updated>2020-05-29T16:10:28Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Admixture based on two PC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;We will go through some examples on how to use PCAngsd with visualization of the data.&lt;br /&gt;
&lt;br /&gt;
==== Set the path to PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
Every time you open a new terminal window, set the paths to the program and the input file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PCANGSD=~/Software/pcangsd/pcangsd.py&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test the link&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
ls $PCAngsd&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Create directories ====&lt;br /&gt;
Create the directories that will be used for working:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Results&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Set the paths to your local directories ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;IN_DIR=Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;OUT_DIR=Demo/Results&amp;lt;/code&amp;gt;	&lt;br /&gt;
&lt;br /&gt;
Test the links&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 1: Allele Frequencies ===&lt;br /&gt;
&lt;br /&gt;
This example will perform a PCA analysis on 1000 genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
==== Download the input and population information files ====&lt;br /&gt;
&lt;br /&gt;
PCAngsd uses Genotype Likelihoods (GLs) in .beagle format as input. The input file has been created for you. &lt;br /&gt;
&lt;br /&gt;
The population information file is also provided.&lt;br /&gt;
&lt;br /&gt;
To download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1pop.info&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1pop.info $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View the genotype likelihood beagle file ====&lt;br /&gt;
&lt;br /&gt;
*In general, the first three columns of a beagle file contain marker name and the two alleles, allele1 and allele2, present in the locus (in beagle A=0, C=1, G=2, T=3). All following columns contain genotype likelihoods (three columns for each individual: first GL for homozygote for allele1, then GL for heterozygote and then GL for homozygote for allele2). Note that the GL values sum to one per site for each individual. This is just a normalization of the genotype likelihoods in order to avoid underflow problems in the beagle software, but it does not mean that they are genotype probabilities. &lt;br /&gt;
&lt;br /&gt;
*In order to see the first 10 columns and 10 lines of the input file, type:&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | head -n 10 | cut -f 1-10 | column -t&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Use this command to count the number of lines of the input file. The number of lines, indicates the number of loci for which there are GLs plus one (as the command includes the count of the header line):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | wc -l&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View population information file ====&lt;br /&gt;
&lt;br /&gt;
To view a summary of the population information file, cut the first column, sort and count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;cut -f 1 -d &amp;quot; &amp;quot; $IN_DIR/Demo1pop.info | sort | uniq -c&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lets make a population label file and place it in the output directory&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; cut -f1 -d&amp;quot; &amp;quot; $IN_DIR/Demo1pop.info  &amp;gt; $OUT_DIR/poplabel &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Run PCAngsd ===&lt;br /&gt;
&lt;br /&gt;
The program estimates the covariance matrix that can be used for PCA.&lt;br /&gt;
&lt;br /&gt;
==== Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo/Data/Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo/Results/Demo1PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd1.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;, main=&amp;quot;individual allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd1.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Without Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
Try the same analysis but without estimating individual allele frequencies. &lt;br /&gt;
This is the same as using the first iteration of the algorithm.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_2.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd2.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;,main=&amp;quot;joint allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd2.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Admixture based on two PC ====&lt;br /&gt;
Let's try to use the PCA to infer admixture proportions based on the first 2 principal components. For the optimization we will use a small penalty on the admixture proportions (alpha).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -e 2 -o $OUT_DIR/Demo2PCANGSD_3 -admix -admix_alpha 50&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
##Requires previous installation of the library RcppCNPy&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo/Data/Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q &amp;lt;- npyLoad(&amp;quot;Demo/Results/Demo2PCANGSD_3.admix.Q.npy&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
## order according to population&lt;br /&gt;
ord&amp;lt;-order(pop[,1])&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Admixture proportions&amp;quot;)&lt;br /&gt;
text(tapply(1:nrow(pop),pop[ord,1],mean),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding in the admixed individuals ====&lt;br /&gt;
Inbreeding in admixed samples is usually not possible to estimate using standard approaches. Let's try to estimate the inbreeding coefficient of the samples using the average allele frequency.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_4 -inbreed 2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_4.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The third column is an estimate of the inbreeding coefficient (allowing for negative)&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding with individual allele frequencies ====&lt;br /&gt;
&lt;br /&gt;
Now let's try to estimate the inbreeding coefficient of the samples by using the individual allele frequencies predicted by the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_5 -inbreed 2 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_5.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 2: Selection ===&lt;br /&gt;
&lt;br /&gt;
For very resent selection we can look within closely related individuals, for example within Europeans.&lt;br /&gt;
&lt;br /&gt;
The objective is to use PCAngsd to estimate the covariance matrix while jointly estimating the individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
Data file:&lt;br /&gt;
&lt;br /&gt;
*Genotype likelihoods in Beagle format&lt;br /&gt;
*~150k random SNPs with maf &amp;gt; 5%&lt;br /&gt;
*Four EU populations with ~100 individuals in each&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;table class=&amp;quot;muse-table&amp;quot; border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;CEU&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;European (mostly British)&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;GBR&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Great Britain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;IBS&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Iberian/Spain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;TSI&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Italien&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
:&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Download the input and sample information files ====&lt;br /&gt;
&lt;br /&gt;
A file with the positions and sample information, and a beagle file are provided:&lt;br /&gt;
&lt;br /&gt;
Download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.small.beagle.gz)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2sample.info&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2sample.info $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
==== Run PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
The objective is to show the differences among individuals.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_1 -threads 4&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plot the results in R ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 ## Open R&lt;br /&gt;
&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
 e&amp;lt;-eigen(cov)&lt;br /&gt;
 ID&amp;lt;-read.table(&amp;quot;Demo2sample.info&amp;quot;,head=T)&lt;br /&gt;
&lt;br /&gt;
 pdf(&amp;quot;PCAngsdDemo2_1.pdf&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
 plot(e$vectors[,1:2],col=ID$POP)&lt;br /&gt;
&lt;br /&gt;
 legend(&amp;quot;topleft&amp;quot;,fill=1:4,levels(ID$POP))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the European individuals in 1000G are not simple homogeneous disjoint populations, it is hard to use PBS/FST or similar statistics to infer selection based on population differences. However, PCAngsd offers a good description of the differences among individuals without having to define disjoint groups.&lt;br /&gt;
&lt;br /&gt;
=== Infer selection along the genome ===&lt;br /&gt;
&lt;br /&gt;
Now let's try to use the PC to infer selection along the genome based on the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_2 -selection -sites_save #-n $N &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Plot the results ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
## function for QQplot&lt;br /&gt;
qqchi&amp;lt;-function(x,...){&lt;br /&gt;
lambda&amp;lt;-round(median(x)/qchisq(0.5,1),2)&lt;br /&gt;
  qqplot(qchisq((1:length(x)-0.5)/(length(x)),1),x,ylab=&amp;quot;Observed&amp;quot;,xlab=&amp;quot;Expected&amp;quot;,...);abline(0,1,col=2,lwd=2)&lt;br /&gt;
legend(&amp;quot;topleft&amp;quot;,paste(&amp;quot;lambda=&amp;quot;,lambda))&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
### read in seleciton statistics (chi2 distributed)&lt;br /&gt;
s&amp;lt;-npyLoad(&amp;quot;Demo2PCANGSD_2.selection.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make QQ plot to QC the test statistics&lt;br /&gt;
qqchi(s)&lt;br /&gt;
&lt;br /&gt;
# convert test statistic to p-value&lt;br /&gt;
pval&amp;lt;-1-pchisq(s,1)&lt;br /&gt;
&lt;br /&gt;
## read positions (hg38)&lt;br /&gt;
p&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_2.sites&amp;quot;,colC=c(&amp;quot;factor&amp;quot;,&amp;quot;integer&amp;quot;),sep=&amp;quot;_&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
names(p)&amp;lt;-c(&amp;quot;chr&amp;quot;,&amp;quot;pos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make manhatten plot&lt;br /&gt;
plot(-log10(pval),col=p$chr,xlab=&amp;quot;Chromosomes&amp;quot;,main=&amp;quot;Manhattan plot&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
## zoom into region&lt;br /&gt;
 w&amp;lt;-range(which(pval&amp;lt;1e-7)) + c(-100,100)&lt;br /&gt;
 keep&amp;lt;-w[1]:w[2]&lt;br /&gt;
 plot(p$pos[keep],-log10(pval[keep]),col=p$chr[keep],xlab=&amp;quot;HG38 Position chr2&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## see the position of the most significant SNP&lt;br /&gt;
 p$pos[which.max(s)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1453</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1453"/>
		<updated>2020-05-29T16:09:49Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Admixture based on 1st two PC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;We will go through some examples on how to use PCAngsd with visualization of the data.&lt;br /&gt;
&lt;br /&gt;
==== Set the path to PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
Every time you open a new terminal window, set the paths to the program and the input file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PCANGSD=~/Software/pcangsd/pcangsd.py&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test the link&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
ls $PCAngsd&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Create directories ====&lt;br /&gt;
Create the directories that will be used for working:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Results&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Set the paths to your local directories ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;IN_DIR=Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;OUT_DIR=Demo/Results&amp;lt;/code&amp;gt;	&lt;br /&gt;
&lt;br /&gt;
Test the links&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 1: Allele Frequencies ===&lt;br /&gt;
&lt;br /&gt;
This example will perform a PCA analysis on 1000 genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
==== Download the input and population information files ====&lt;br /&gt;
&lt;br /&gt;
PCAngsd uses Genotype Likelihoods (GLs) in .beagle format as input. The input file has been created for you. &lt;br /&gt;
&lt;br /&gt;
The population information file is also provided.&lt;br /&gt;
&lt;br /&gt;
To download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1pop.info&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1pop.info $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View the genotype likelihood beagle file ====&lt;br /&gt;
&lt;br /&gt;
*In general, the first three columns of a beagle file contain marker name and the two alleles, allele1 and allele2, present in the locus (in beagle A=0, C=1, G=2, T=3). All following columns contain genotype likelihoods (three columns for each individual: first GL for homozygote for allele1, then GL for heterozygote and then GL for homozygote for allele2). Note that the GL values sum to one per site for each individual. This is just a normalization of the genotype likelihoods in order to avoid underflow problems in the beagle software, but it does not mean that they are genotype probabilities. &lt;br /&gt;
&lt;br /&gt;
*In order to see the first 10 columns and 10 lines of the input file, type:&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | head -n 10 | cut -f 1-10 | column -t&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Use this command to count the number of lines of the input file. The number of lines, indicates the number of loci for which there are GLs plus one (as the command includes the count of the header line):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | wc -l&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View population information file ====&lt;br /&gt;
&lt;br /&gt;
To view a summary of the population information file, cut the first column, sort and count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;cut -f 1 -d &amp;quot; &amp;quot; $IN_DIR/Demo1pop.info | sort | uniq -c&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lets make a population label file and place it in the output directory&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; cut -f1 -d&amp;quot; &amp;quot; $IN_DIR/Demo1pop.info  &amp;gt; $OUT_DIR/poplabel &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Run PCAngsd ===&lt;br /&gt;
&lt;br /&gt;
The program estimates the covariance matrix that can be used for PCA.&lt;br /&gt;
&lt;br /&gt;
==== Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo/Data/Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo/Results/Demo1PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd1.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;, main=&amp;quot;individual allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd1.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Without Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
Try the same analysis but without estimating individual allele frequencies. &lt;br /&gt;
This is the same as using the first iteration of the algorithm.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_2.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd2.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;,main=&amp;quot;joint allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd2.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Admixture based on two PC ====&lt;br /&gt;
Let's try to use the PCA to infer admixture proportions based on the first 2 principal components. For the optimization we will use a small penalty on the admixture proportions (alpha).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD_3 -admix -admix_alpha 50&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
##Requires previous installation of the library RcppCNPy&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo/Data/Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q &amp;lt;- npyLoad(&amp;quot;Demo/Results/Demo2PCANGSD_3.admix.Q.npy&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
## order according to population&lt;br /&gt;
ord&amp;lt;-order(pop[,1])&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Admixture proportions&amp;quot;)&lt;br /&gt;
text(tapply(1:nrow(pop),pop[ord,1],mean),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding in the admixed individuals ====&lt;br /&gt;
Inbreeding in admixed samples is usually not possible to estimate using standard approaches. Let's try to estimate the inbreeding coefficient of the samples using the average allele frequency.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_4 -inbreed 2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_4.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The third column is an estimate of the inbreeding coefficient (allowing for negative)&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding with individual allele frequencies ====&lt;br /&gt;
&lt;br /&gt;
Now let's try to estimate the inbreeding coefficient of the samples by using the individual allele frequencies predicted by the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_5 -inbreed 2 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_5.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 2: Selection ===&lt;br /&gt;
&lt;br /&gt;
For very resent selection we can look within closely related individuals, for example within Europeans.&lt;br /&gt;
&lt;br /&gt;
The objective is to use PCAngsd to estimate the covariance matrix while jointly estimating the individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
Data file:&lt;br /&gt;
&lt;br /&gt;
*Genotype likelihoods in Beagle format&lt;br /&gt;
*~150k random SNPs with maf &amp;gt; 5%&lt;br /&gt;
*Four EU populations with ~100 individuals in each&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;table class=&amp;quot;muse-table&amp;quot; border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;CEU&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;European (mostly British)&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;GBR&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Great Britain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;IBS&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Iberian/Spain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;TSI&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Italien&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
:&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Download the input and sample information files ====&lt;br /&gt;
&lt;br /&gt;
A file with the positions and sample information, and a beagle file are provided:&lt;br /&gt;
&lt;br /&gt;
Download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.small.beagle.gz)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2sample.info&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2sample.info $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
==== Run PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
The objective is to show the differences among individuals.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_1 -threads 4&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plot the results in R ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 ## Open R&lt;br /&gt;
&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
 e&amp;lt;-eigen(cov)&lt;br /&gt;
 ID&amp;lt;-read.table(&amp;quot;Demo2sample.info&amp;quot;,head=T)&lt;br /&gt;
&lt;br /&gt;
 pdf(&amp;quot;PCAngsdDemo2_1.pdf&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
 plot(e$vectors[,1:2],col=ID$POP)&lt;br /&gt;
&lt;br /&gt;
 legend(&amp;quot;topleft&amp;quot;,fill=1:4,levels(ID$POP))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the European individuals in 1000G are not simple homogeneous disjoint populations, it is hard to use PBS/FST or similar statistics to infer selection based on population differences. However, PCAngsd offers a good description of the differences among individuals without having to define disjoint groups.&lt;br /&gt;
&lt;br /&gt;
=== Infer selection along the genome ===&lt;br /&gt;
&lt;br /&gt;
Now let's try to use the PC to infer selection along the genome based on the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_2 -selection -sites_save #-n $N &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Plot the results ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
## function for QQplot&lt;br /&gt;
qqchi&amp;lt;-function(x,...){&lt;br /&gt;
lambda&amp;lt;-round(median(x)/qchisq(0.5,1),2)&lt;br /&gt;
  qqplot(qchisq((1:length(x)-0.5)/(length(x)),1),x,ylab=&amp;quot;Observed&amp;quot;,xlab=&amp;quot;Expected&amp;quot;,...);abline(0,1,col=2,lwd=2)&lt;br /&gt;
legend(&amp;quot;topleft&amp;quot;,paste(&amp;quot;lambda=&amp;quot;,lambda))&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
### read in seleciton statistics (chi2 distributed)&lt;br /&gt;
s&amp;lt;-npyLoad(&amp;quot;Demo2PCANGSD_2.selection.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make QQ plot to QC the test statistics&lt;br /&gt;
qqchi(s)&lt;br /&gt;
&lt;br /&gt;
# convert test statistic to p-value&lt;br /&gt;
pval&amp;lt;-1-pchisq(s,1)&lt;br /&gt;
&lt;br /&gt;
## read positions (hg38)&lt;br /&gt;
p&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_2.sites&amp;quot;,colC=c(&amp;quot;factor&amp;quot;,&amp;quot;integer&amp;quot;),sep=&amp;quot;_&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
names(p)&amp;lt;-c(&amp;quot;chr&amp;quot;,&amp;quot;pos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make manhatten plot&lt;br /&gt;
plot(-log10(pval),col=p$chr,xlab=&amp;quot;Chromosomes&amp;quot;,main=&amp;quot;Manhattan plot&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
## zoom into region&lt;br /&gt;
 w&amp;lt;-range(which(pval&amp;lt;1e-7)) + c(-100,100)&lt;br /&gt;
 keep&amp;lt;-w[1]:w[2]&lt;br /&gt;
 plot(p$pos[keep],-log10(pval[keep]),col=p$chr[keep],xlab=&amp;quot;HG38 Position chr2&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## see the position of the most significant SNP&lt;br /&gt;
 p$pos[which.max(s)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1452</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1452"/>
		<updated>2020-05-29T16:08:27Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Admixture based on 1st two PC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;We will go through some examples on how to use PCAngsd with visualization of the data.&lt;br /&gt;
&lt;br /&gt;
==== Set the path to PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
Every time you open a new terminal window, set the paths to the program and the input file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PCANGSD=~/Software/pcangsd/pcangsd.py&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test the link&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
ls $PCAngsd&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Create directories ====&lt;br /&gt;
Create the directories that will be used for working:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Results&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Set the paths to your local directories ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;IN_DIR=Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;OUT_DIR=Demo/Results&amp;lt;/code&amp;gt;	&lt;br /&gt;
&lt;br /&gt;
Test the links&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 1: Allele Frequencies ===&lt;br /&gt;
&lt;br /&gt;
This example will perform a PCA analysis on 1000 genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
==== Download the input and population information files ====&lt;br /&gt;
&lt;br /&gt;
PCAngsd uses Genotype Likelihoods (GLs) in .beagle format as input. The input file has been created for you. &lt;br /&gt;
&lt;br /&gt;
The population information file is also provided.&lt;br /&gt;
&lt;br /&gt;
To download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1pop.info&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1pop.info $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View the genotype likelihood beagle file ====&lt;br /&gt;
&lt;br /&gt;
*In general, the first three columns of a beagle file contain marker name and the two alleles, allele1 and allele2, present in the locus (in beagle A=0, C=1, G=2, T=3). All following columns contain genotype likelihoods (three columns for each individual: first GL for homozygote for allele1, then GL for heterozygote and then GL for homozygote for allele2). Note that the GL values sum to one per site for each individual. This is just a normalization of the genotype likelihoods in order to avoid underflow problems in the beagle software, but it does not mean that they are genotype probabilities. &lt;br /&gt;
&lt;br /&gt;
*In order to see the first 10 columns and 10 lines of the input file, type:&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | head -n 10 | cut -f 1-10 | column -t&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Use this command to count the number of lines of the input file. The number of lines, indicates the number of loci for which there are GLs plus one (as the command includes the count of the header line):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | wc -l&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View population information file ====&lt;br /&gt;
&lt;br /&gt;
To view a summary of the population information file, cut the first column, sort and count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;cut -f 1 -d &amp;quot; &amp;quot; $IN_DIR/Demo1pop.info | sort | uniq -c&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lets make a population label file and place it in the output directory&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; cut -f1 -d&amp;quot; &amp;quot; $IN_DIR/Demo1pop.info  &amp;gt; $OUT_DIR/poplabel &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Run PCAngsd ===&lt;br /&gt;
&lt;br /&gt;
The program estimates the covariance matrix that can be used for PCA.&lt;br /&gt;
&lt;br /&gt;
==== Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo/Data/Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo/Results/Demo1PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd1.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;, main=&amp;quot;individual allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd1.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Without Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
Try the same analysis but without estimating individual allele frequencies. &lt;br /&gt;
This is the same as using the first iteration of the algorithm.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_2.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd2.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;,main=&amp;quot;joint allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd2.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Admixture based on 1st two PC ====&lt;br /&gt;
Let's try to use the PCA to infer admixture proportions based on the first 2 principal components. For the optimization we will use a small penalty on the admixture proportions (alpha).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD_3 -admix -admix_alpha 50&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
##Requires previous installation of the library RcppCNPy&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo/Data/Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q &amp;lt;- npyLoad(&amp;quot;Demo/Results/Demo2PCANGSD_3.admix.Q.npy&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
## order according to population&lt;br /&gt;
ord&amp;lt;-order(pop[,1])&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Admixture proportions&amp;quot;)&lt;br /&gt;
text(tapply(1:nrow(pop),pop[ord,1],mean),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding in the admixed individuals ====&lt;br /&gt;
Inbreeding in admixed samples is usually not possible to estimate using standard approaches. Let's try to estimate the inbreeding coefficient of the samples using the average allele frequency.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_4 -inbreed 2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_4.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The third column is an estimate of the inbreeding coefficient (allowing for negative)&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding with individual allele frequencies ====&lt;br /&gt;
&lt;br /&gt;
Now let's try to estimate the inbreeding coefficient of the samples by using the individual allele frequencies predicted by the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_5 -inbreed 2 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_5.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 2: Selection ===&lt;br /&gt;
&lt;br /&gt;
For very resent selection we can look within closely related individuals, for example within Europeans.&lt;br /&gt;
&lt;br /&gt;
The objective is to use PCAngsd to estimate the covariance matrix while jointly estimating the individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
Data file:&lt;br /&gt;
&lt;br /&gt;
*Genotype likelihoods in Beagle format&lt;br /&gt;
*~150k random SNPs with maf &amp;gt; 5%&lt;br /&gt;
*Four EU populations with ~100 individuals in each&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;table class=&amp;quot;muse-table&amp;quot; border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;CEU&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;European (mostly British)&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;GBR&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Great Britain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;IBS&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Iberian/Spain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;TSI&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Italien&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
:&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Download the input and sample information files ====&lt;br /&gt;
&lt;br /&gt;
A file with the positions and sample information, and a beagle file are provided:&lt;br /&gt;
&lt;br /&gt;
Download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.small.beagle.gz)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2sample.info&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2sample.info $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
==== Run PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
The objective is to show the differences among individuals.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_1 -threads 4&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plot the results in R ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 ## Open R&lt;br /&gt;
&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
 e&amp;lt;-eigen(cov)&lt;br /&gt;
 ID&amp;lt;-read.table(&amp;quot;Demo2sample.info&amp;quot;,head=T)&lt;br /&gt;
&lt;br /&gt;
 pdf(&amp;quot;PCAngsdDemo2_1.pdf&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
 plot(e$vectors[,1:2],col=ID$POP)&lt;br /&gt;
&lt;br /&gt;
 legend(&amp;quot;topleft&amp;quot;,fill=1:4,levels(ID$POP))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the European individuals in 1000G are not simple homogeneous disjoint populations, it is hard to use PBS/FST or similar statistics to infer selection based on population differences. However, PCAngsd offers a good description of the differences among individuals without having to define disjoint groups.&lt;br /&gt;
&lt;br /&gt;
=== Infer selection along the genome ===&lt;br /&gt;
&lt;br /&gt;
Now let's try to use the PC to infer selection along the genome based on the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_2 -selection -sites_save #-n $N &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Plot the results ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
## function for QQplot&lt;br /&gt;
qqchi&amp;lt;-function(x,...){&lt;br /&gt;
lambda&amp;lt;-round(median(x)/qchisq(0.5,1),2)&lt;br /&gt;
  qqplot(qchisq((1:length(x)-0.5)/(length(x)),1),x,ylab=&amp;quot;Observed&amp;quot;,xlab=&amp;quot;Expected&amp;quot;,...);abline(0,1,col=2,lwd=2)&lt;br /&gt;
legend(&amp;quot;topleft&amp;quot;,paste(&amp;quot;lambda=&amp;quot;,lambda))&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
### read in seleciton statistics (chi2 distributed)&lt;br /&gt;
s&amp;lt;-npyLoad(&amp;quot;Demo2PCANGSD_2.selection.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make QQ plot to QC the test statistics&lt;br /&gt;
qqchi(s)&lt;br /&gt;
&lt;br /&gt;
# convert test statistic to p-value&lt;br /&gt;
pval&amp;lt;-1-pchisq(s,1)&lt;br /&gt;
&lt;br /&gt;
## read positions (hg38)&lt;br /&gt;
p&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_2.sites&amp;quot;,colC=c(&amp;quot;factor&amp;quot;,&amp;quot;integer&amp;quot;),sep=&amp;quot;_&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
names(p)&amp;lt;-c(&amp;quot;chr&amp;quot;,&amp;quot;pos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make manhatten plot&lt;br /&gt;
plot(-log10(pval),col=p$chr,xlab=&amp;quot;Chromosomes&amp;quot;,main=&amp;quot;Manhattan plot&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
## zoom into region&lt;br /&gt;
 w&amp;lt;-range(which(pval&amp;lt;1e-7)) + c(-100,100)&lt;br /&gt;
 keep&amp;lt;-w[1]:w[2]&lt;br /&gt;
 plot(p$pos[keep],-log10(pval[keep]),col=p$chr[keep],xlab=&amp;quot;HG38 Position chr2&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## see the position of the most significant SNP&lt;br /&gt;
 p$pos[which.max(s)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1451</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1451"/>
		<updated>2020-05-29T16:07:11Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Estimating Individual Allele Frequencies */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;We will go through some examples on how to use PCAngsd with visualization of the data.&lt;br /&gt;
&lt;br /&gt;
==== Set the path to PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
Every time you open a new terminal window, set the paths to the program and the input file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PCANGSD=~/Software/pcangsd/pcangsd.py&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test the link&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
ls $PCAngsd&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Create directories ====&lt;br /&gt;
Create the directories that will be used for working:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Results&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Set the paths to your local directories ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;IN_DIR=Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;OUT_DIR=Demo/Results&amp;lt;/code&amp;gt;	&lt;br /&gt;
&lt;br /&gt;
Test the links&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 1: Allele Frequencies ===&lt;br /&gt;
&lt;br /&gt;
This example will perform a PCA analysis on 1000 genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
==== Download the input and population information files ====&lt;br /&gt;
&lt;br /&gt;
PCAngsd uses Genotype Likelihoods (GLs) in .beagle format as input. The input file has been created for you. &lt;br /&gt;
&lt;br /&gt;
The population information file is also provided.&lt;br /&gt;
&lt;br /&gt;
To download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1pop.info&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1pop.info $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View the genotype likelihood beagle file ====&lt;br /&gt;
&lt;br /&gt;
*In general, the first three columns of a beagle file contain marker name and the two alleles, allele1 and allele2, present in the locus (in beagle A=0, C=1, G=2, T=3). All following columns contain genotype likelihoods (three columns for each individual: first GL for homozygote for allele1, then GL for heterozygote and then GL for homozygote for allele2). Note that the GL values sum to one per site for each individual. This is just a normalization of the genotype likelihoods in order to avoid underflow problems in the beagle software, but it does not mean that they are genotype probabilities. &lt;br /&gt;
&lt;br /&gt;
*In order to see the first 10 columns and 10 lines of the input file, type:&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | head -n 10 | cut -f 1-10 | column -t&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Use this command to count the number of lines of the input file. The number of lines, indicates the number of loci for which there are GLs plus one (as the command includes the count of the header line):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | wc -l&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View population information file ====&lt;br /&gt;
&lt;br /&gt;
To view a summary of the population information file, cut the first column, sort and count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;cut -f 1 -d &amp;quot; &amp;quot; $IN_DIR/Demo1pop.info | sort | uniq -c&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lets make a population label file and place it in the output directory&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; cut -f1 -d&amp;quot; &amp;quot; $IN_DIR/Demo1pop.info  &amp;gt; $OUT_DIR/poplabel &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Run PCAngsd ===&lt;br /&gt;
&lt;br /&gt;
The program estimates the covariance matrix that can be used for PCA.&lt;br /&gt;
&lt;br /&gt;
==== Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo/Data/Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo/Results/Demo1PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd1.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;, main=&amp;quot;individual allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd1.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Without Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
Try the same analysis but without estimating individual allele frequencies. &lt;br /&gt;
This is the same as using the first iteration of the algorithm.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_2.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd2.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;,main=&amp;quot;joint allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd2.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Admixture based on 1st two PC ====&lt;br /&gt;
Let's try to use the PCA to infer admixture proportions based on the first 2 principal components. For the optimization we will use a small penalty on the admixture proportions (alpha).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD_3 -admix -admix_alpha 50&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
##Requires previous installation of the library RcppCNPy&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q &amp;lt;- npyLoad(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
## order according to population&lt;br /&gt;
ord&amp;lt;-order(pop[,1])&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Admixture proportions&amp;quot;)&lt;br /&gt;
text(tapply(1:nrow(pop),pop[ord,1],mean),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding in the admixed individuals ====&lt;br /&gt;
Inbreeding in admixed samples is usually not possible to estimate using standard approaches. Let's try to estimate the inbreeding coefficient of the samples using the average allele frequency.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_4 -inbreed 2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_4.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The third column is an estimate of the inbreeding coefficient (allowing for negative)&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding with individual allele frequencies ====&lt;br /&gt;
&lt;br /&gt;
Now let's try to estimate the inbreeding coefficient of the samples by using the individual allele frequencies predicted by the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_5 -inbreed 2 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_5.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 2: Selection ===&lt;br /&gt;
&lt;br /&gt;
For very resent selection we can look within closely related individuals, for example within Europeans.&lt;br /&gt;
&lt;br /&gt;
The objective is to use PCAngsd to estimate the covariance matrix while jointly estimating the individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
Data file:&lt;br /&gt;
&lt;br /&gt;
*Genotype likelihoods in Beagle format&lt;br /&gt;
*~150k random SNPs with maf &amp;gt; 5%&lt;br /&gt;
*Four EU populations with ~100 individuals in each&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;table class=&amp;quot;muse-table&amp;quot; border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;CEU&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;European (mostly British)&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;GBR&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Great Britain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;IBS&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Iberian/Spain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;TSI&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Italien&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
:&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Download the input and sample information files ====&lt;br /&gt;
&lt;br /&gt;
A file with the positions and sample information, and a beagle file are provided:&lt;br /&gt;
&lt;br /&gt;
Download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.small.beagle.gz)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2sample.info&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2sample.info $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
==== Run PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
The objective is to show the differences among individuals.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_1 -threads 4&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plot the results in R ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 ## Open R&lt;br /&gt;
&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
 e&amp;lt;-eigen(cov)&lt;br /&gt;
 ID&amp;lt;-read.table(&amp;quot;Demo2sample.info&amp;quot;,head=T)&lt;br /&gt;
&lt;br /&gt;
 pdf(&amp;quot;PCAngsdDemo2_1.pdf&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
 plot(e$vectors[,1:2],col=ID$POP)&lt;br /&gt;
&lt;br /&gt;
 legend(&amp;quot;topleft&amp;quot;,fill=1:4,levels(ID$POP))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the European individuals in 1000G are not simple homogeneous disjoint populations, it is hard to use PBS/FST or similar statistics to infer selection based on population differences. However, PCAngsd offers a good description of the differences among individuals without having to define disjoint groups.&lt;br /&gt;
&lt;br /&gt;
=== Infer selection along the genome ===&lt;br /&gt;
&lt;br /&gt;
Now let's try to use the PC to infer selection along the genome based on the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_2 -selection -sites_save #-n $N &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Plot the results ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
## function for QQplot&lt;br /&gt;
qqchi&amp;lt;-function(x,...){&lt;br /&gt;
lambda&amp;lt;-round(median(x)/qchisq(0.5,1),2)&lt;br /&gt;
  qqplot(qchisq((1:length(x)-0.5)/(length(x)),1),x,ylab=&amp;quot;Observed&amp;quot;,xlab=&amp;quot;Expected&amp;quot;,...);abline(0,1,col=2,lwd=2)&lt;br /&gt;
legend(&amp;quot;topleft&amp;quot;,paste(&amp;quot;lambda=&amp;quot;,lambda))&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
### read in seleciton statistics (chi2 distributed)&lt;br /&gt;
s&amp;lt;-npyLoad(&amp;quot;Demo2PCANGSD_2.selection.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make QQ plot to QC the test statistics&lt;br /&gt;
qqchi(s)&lt;br /&gt;
&lt;br /&gt;
# convert test statistic to p-value&lt;br /&gt;
pval&amp;lt;-1-pchisq(s,1)&lt;br /&gt;
&lt;br /&gt;
## read positions (hg38)&lt;br /&gt;
p&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_2.sites&amp;quot;,colC=c(&amp;quot;factor&amp;quot;,&amp;quot;integer&amp;quot;),sep=&amp;quot;_&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
names(p)&amp;lt;-c(&amp;quot;chr&amp;quot;,&amp;quot;pos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make manhatten plot&lt;br /&gt;
plot(-log10(pval),col=p$chr,xlab=&amp;quot;Chromosomes&amp;quot;,main=&amp;quot;Manhattan plot&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
## zoom into region&lt;br /&gt;
 w&amp;lt;-range(which(pval&amp;lt;1e-7)) + c(-100,100)&lt;br /&gt;
 keep&amp;lt;-w[1]:w[2]&lt;br /&gt;
 plot(p$pos[keep],-log10(pval[keep]),col=p$chr[keep],xlab=&amp;quot;HG38 Position chr2&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## see the position of the most significant SNP&lt;br /&gt;
 p$pos[which.max(s)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1450</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1450"/>
		<updated>2020-05-29T16:06:03Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Demo 1: Allele Frequencies */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;We will go through some examples on how to use PCAngsd with visualization of the data.&lt;br /&gt;
&lt;br /&gt;
==== Set the path to PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
Every time you open a new terminal window, set the paths to the program and the input file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PCANGSD=~/Software/pcangsd/pcangsd.py&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test the link&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
ls $PCAngsd&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Create directories ====&lt;br /&gt;
Create the directories that will be used for working:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Results&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Set the paths to your local directories ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;IN_DIR=Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;OUT_DIR=Demo/Results&amp;lt;/code&amp;gt;	&lt;br /&gt;
&lt;br /&gt;
Test the links&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 1: Allele Frequencies ===&lt;br /&gt;
&lt;br /&gt;
This example will perform a PCA analysis on 1000 genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
==== Download the input and population information files ====&lt;br /&gt;
&lt;br /&gt;
PCAngsd uses Genotype Likelihoods (GLs) in .beagle format as input. The input file has been created for you. &lt;br /&gt;
&lt;br /&gt;
The population information file is also provided.&lt;br /&gt;
&lt;br /&gt;
To download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1pop.info&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1pop.info $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View the genotype likelihood beagle file ====&lt;br /&gt;
&lt;br /&gt;
*In general, the first three columns of a beagle file contain marker name and the two alleles, allele1 and allele2, present in the locus (in beagle A=0, C=1, G=2, T=3). All following columns contain genotype likelihoods (three columns for each individual: first GL for homozygote for allele1, then GL for heterozygote and then GL for homozygote for allele2). Note that the GL values sum to one per site for each individual. This is just a normalization of the genotype likelihoods in order to avoid underflow problems in the beagle software, but it does not mean that they are genotype probabilities. &lt;br /&gt;
&lt;br /&gt;
*In order to see the first 10 columns and 10 lines of the input file, type:&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | head -n 10 | cut -f 1-10 | column -t&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Use this command to count the number of lines of the input file. The number of lines, indicates the number of loci for which there are GLs plus one (as the command includes the count of the header line):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | wc -l&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View population information file ====&lt;br /&gt;
&lt;br /&gt;
To view a summary of the population information file, cut the first column, sort and count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;cut -f 1 -d &amp;quot; &amp;quot; $IN_DIR/Demo1pop.info | sort | uniq -c&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lets make a population label file and place it in the output directory&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; cut -f1 -d&amp;quot; &amp;quot; $IN_DIR/Demo1pop.info  &amp;gt; $OUT_DIR/poplabel &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Run PCAngsd ===&lt;br /&gt;
&lt;br /&gt;
The program estimates the covariance matrix that can be used for PCA.&lt;br /&gt;
&lt;br /&gt;
==== Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd1.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;, main=&amp;quot;individual allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd1.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Without Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
Try the same analysis but without estimating individual allele frequencies. &lt;br /&gt;
This is the same as using the first iteration of the algorithm.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_2.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd2.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;,main=&amp;quot;joint allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd2.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Admixture based on 1st two PC ====&lt;br /&gt;
Let's try to use the PCA to infer admixture proportions based on the first 2 principal components. For the optimization we will use a small penalty on the admixture proportions (alpha).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD_3 -admix -admix_alpha 50&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
##Requires previous installation of the library RcppCNPy&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q &amp;lt;- npyLoad(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
## order according to population&lt;br /&gt;
ord&amp;lt;-order(pop[,1])&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Admixture proportions&amp;quot;)&lt;br /&gt;
text(tapply(1:nrow(pop),pop[ord,1],mean),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding in the admixed individuals ====&lt;br /&gt;
Inbreeding in admixed samples is usually not possible to estimate using standard approaches. Let's try to estimate the inbreeding coefficient of the samples using the average allele frequency.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_4 -inbreed 2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_4.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The third column is an estimate of the inbreeding coefficient (allowing for negative)&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding with individual allele frequencies ====&lt;br /&gt;
&lt;br /&gt;
Now let's try to estimate the inbreeding coefficient of the samples by using the individual allele frequencies predicted by the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_5 -inbreed 2 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_5.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 2: Selection ===&lt;br /&gt;
&lt;br /&gt;
For very resent selection we can look within closely related individuals, for example within Europeans.&lt;br /&gt;
&lt;br /&gt;
The objective is to use PCAngsd to estimate the covariance matrix while jointly estimating the individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
Data file:&lt;br /&gt;
&lt;br /&gt;
*Genotype likelihoods in Beagle format&lt;br /&gt;
*~150k random SNPs with maf &amp;gt; 5%&lt;br /&gt;
*Four EU populations with ~100 individuals in each&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;table class=&amp;quot;muse-table&amp;quot; border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;CEU&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;European (mostly British)&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;GBR&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Great Britain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;IBS&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Iberian/Spain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;TSI&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Italien&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
:&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Download the input and sample information files ====&lt;br /&gt;
&lt;br /&gt;
A file with the positions and sample information, and a beagle file are provided:&lt;br /&gt;
&lt;br /&gt;
Download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.small.beagle.gz)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2sample.info&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2sample.info $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
==== Run PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
The objective is to show the differences among individuals.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_1 -threads 4&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plot the results in R ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 ## Open R&lt;br /&gt;
&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
 e&amp;lt;-eigen(cov)&lt;br /&gt;
 ID&amp;lt;-read.table(&amp;quot;Demo2sample.info&amp;quot;,head=T)&lt;br /&gt;
&lt;br /&gt;
 pdf(&amp;quot;PCAngsdDemo2_1.pdf&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
 plot(e$vectors[,1:2],col=ID$POP)&lt;br /&gt;
&lt;br /&gt;
 legend(&amp;quot;topleft&amp;quot;,fill=1:4,levels(ID$POP))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the European individuals in 1000G are not simple homogeneous disjoint populations, it is hard to use PBS/FST or similar statistics to infer selection based on population differences. However, PCAngsd offers a good description of the differences among individuals without having to define disjoint groups.&lt;br /&gt;
&lt;br /&gt;
=== Infer selection along the genome ===&lt;br /&gt;
&lt;br /&gt;
Now let's try to use the PC to infer selection along the genome based on the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_2 -selection -sites_save #-n $N &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Plot the results ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
## function for QQplot&lt;br /&gt;
qqchi&amp;lt;-function(x,...){&lt;br /&gt;
lambda&amp;lt;-round(median(x)/qchisq(0.5,1),2)&lt;br /&gt;
  qqplot(qchisq((1:length(x)-0.5)/(length(x)),1),x,ylab=&amp;quot;Observed&amp;quot;,xlab=&amp;quot;Expected&amp;quot;,...);abline(0,1,col=2,lwd=2)&lt;br /&gt;
legend(&amp;quot;topleft&amp;quot;,paste(&amp;quot;lambda=&amp;quot;,lambda))&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
### read in seleciton statistics (chi2 distributed)&lt;br /&gt;
s&amp;lt;-npyLoad(&amp;quot;Demo2PCANGSD_2.selection.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make QQ plot to QC the test statistics&lt;br /&gt;
qqchi(s)&lt;br /&gt;
&lt;br /&gt;
# convert test statistic to p-value&lt;br /&gt;
pval&amp;lt;-1-pchisq(s,1)&lt;br /&gt;
&lt;br /&gt;
## read positions (hg38)&lt;br /&gt;
p&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_2.sites&amp;quot;,colC=c(&amp;quot;factor&amp;quot;,&amp;quot;integer&amp;quot;),sep=&amp;quot;_&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
names(p)&amp;lt;-c(&amp;quot;chr&amp;quot;,&amp;quot;pos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make manhatten plot&lt;br /&gt;
plot(-log10(pval),col=p$chr,xlab=&amp;quot;Chromosomes&amp;quot;,main=&amp;quot;Manhattan plot&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
## zoom into region&lt;br /&gt;
 w&amp;lt;-range(which(pval&amp;lt;1e-7)) + c(-100,100)&lt;br /&gt;
 keep&amp;lt;-w[1]:w[2]&lt;br /&gt;
 plot(p$pos[keep],-log10(pval[keep]),col=p$chr[keep],xlab=&amp;quot;HG38 Position chr2&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## see the position of the most significant SNP&lt;br /&gt;
 p$pos[which.max(s)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1449</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1449"/>
		<updated>2020-05-29T16:04:40Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Download the input and population information files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;We will go through some examples on how to use PCAngsd with visualization of the data.&lt;br /&gt;
&lt;br /&gt;
==== Set the path to PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
Every time you open a new terminal window, set the paths to the program and the input file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PCANGSD=~/Software/pcangsd/pcangsd.py&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test the link&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
ls $PCAngsd&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Create directories ====&lt;br /&gt;
Create the directories that will be used for working:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Results&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Set the paths to your local directories ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;IN_DIR=Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;OUT_DIR=Demo/Results&amp;lt;/code&amp;gt;	&lt;br /&gt;
&lt;br /&gt;
Test the links&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 1: Allele Frequencies ===&lt;br /&gt;
&lt;br /&gt;
This example will perform a PCA analysis on 1000 genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
==== Download the input and population information files ====&lt;br /&gt;
&lt;br /&gt;
PCAngsd uses Genotype Likelihoods (GLs) in .beagle format as input. The input file has been created for you. &lt;br /&gt;
&lt;br /&gt;
The population information file is also provided.&lt;br /&gt;
&lt;br /&gt;
To download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1pop.info&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1pop.info $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View the genotype likelihood beagle file ====&lt;br /&gt;
&lt;br /&gt;
*In general, the first three columns of a beagle file contain marker name and the two alleles, allele1 and allele2, present in the locus (in beagle A=0, C=1, G=2, T=3). All following columns contain genotype likelihoods (three columns for each individual: first GL for homozygote for allele1, then GL for heterozygote and then GL for homozygote for allele2). Note that the GL values sum to one per site for each individual. This is just a normalization of the genotype likelihoods in order to avoid underflow problems in the beagle software, but it does not mean that they are genotype probabilities. &lt;br /&gt;
&lt;br /&gt;
*In order to see the first 10 columns and 10 lines of the input file, type:&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | head -n 10 | cut -f 1-10 | column -t&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Use this command to count the number of lines of the input file. The number of lines, indicates the number of loci for which there are GLs plus one (as the command includes the count of the header line):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | wc -l&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View population information file ====&lt;br /&gt;
&lt;br /&gt;
To view a summary of the population information file, cut the first column, sort and count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;cut -f 1 -d &amp;quot; &amp;quot; $IN_DIR/Demo1pop.info | sort | uniq -c&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lets make a population label file and place it in the output directory&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; cut -f1 -d&amp;quot; &amp;quot; $IN_DIR/Demo1pop.info  &amp;gt; $OUT_DIR/poplabel &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Run PCAngsd ===&lt;br /&gt;
&lt;br /&gt;
The program estimates the covariance matrix that can be used for PCA.&lt;br /&gt;
&lt;br /&gt;
==== Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd1.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;, main=&amp;quot;individual allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd1.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Without Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
Try the same analysis but without estimating individual allele frequencies. &lt;br /&gt;
This is the same as using the first iteration of the algorithm.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_2.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd2.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;,main=&amp;quot;joint allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd2.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Admixture based on 1st two PC ====&lt;br /&gt;
Let's try to use the PCA to infer admixture proportions based on the first 2 principal components. For the optimization we will use a small penalty on the admixture proportions (alpha).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD_3 -admix -admix_alpha 50&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
##Requires previous installation of the library RcppCNPy&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q &amp;lt;- npyLoad(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
## order according to population&lt;br /&gt;
ord&amp;lt;-order(pop[,1])&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Admixture proportions&amp;quot;)&lt;br /&gt;
text(tapply(1:nrow(pop),pop[ord,1],mean),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding in the admixed individuals ====&lt;br /&gt;
Inbreeding in admixed samples is usually not possible to estimate using standard approaches. Let's try to estimate the inbreeding coefficient of the samples using the average allele frequency.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_4 -inbreed 2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_4.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The third column is an estimate of the inbreeding coefficient (allowing for negative)&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding with individual allele frequencies ====&lt;br /&gt;
&lt;br /&gt;
Now let's try to estimate the inbreeding coefficient of the samples by using the individual allele frequencies predicted by the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_5 -inbreed 2 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_5.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 2: Selection ===&lt;br /&gt;
&lt;br /&gt;
For very resent selection we can look within closely related individuals, for example within Europeans.&lt;br /&gt;
&lt;br /&gt;
The objective is to use PCAngsd to estimate the covariance matrix while jointly estimating the individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
Data file:&lt;br /&gt;
&lt;br /&gt;
*Genotype likelihoods in Beagle format&lt;br /&gt;
*~150k random SNPs with maf &amp;gt; 5%&lt;br /&gt;
*Four EU populations with ~100 individuals in each&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;table class=&amp;quot;muse-table&amp;quot; border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;CEU&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;European (mostly British)&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;GBR&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Great Britain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;IBS&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Iberian/Spain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;TSI&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Italien&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
:&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Download the input and sample information files ====&lt;br /&gt;
&lt;br /&gt;
A file with the positions and sample information, and a beagle file are provided:&lt;br /&gt;
&lt;br /&gt;
Download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.small.beagle.gz)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2sample.info&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2sample.info $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
==== Run PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
The objective is to show the differences among individuals.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_1 -threads 4&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plot the results in R ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 ## Open R&lt;br /&gt;
&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
 e&amp;lt;-eigen(cov)&lt;br /&gt;
 ID&amp;lt;-read.table(&amp;quot;Demo2sample.info&amp;quot;,head=T)&lt;br /&gt;
&lt;br /&gt;
 pdf(&amp;quot;PCAngsdDemo2_1.pdf&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
 plot(e$vectors[,1:2],col=ID$POP)&lt;br /&gt;
&lt;br /&gt;
 legend(&amp;quot;topleft&amp;quot;,fill=1:4,levels(ID$POP))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the European individuals in 1000G are not simple homogeneous disjoint populations, it is hard to use PBS/FST or similar statistics to infer selection based on population differences. However, PCAngsd offers a good description of the differences among individuals without having to define disjoint groups.&lt;br /&gt;
&lt;br /&gt;
=== Infer selection along the genome ===&lt;br /&gt;
&lt;br /&gt;
Now let's try to use the PC to infer selection along the genome based on the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_2 -selection -sites_save #-n $N &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Plot the results ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
## function for QQplot&lt;br /&gt;
qqchi&amp;lt;-function(x,...){&lt;br /&gt;
lambda&amp;lt;-round(median(x)/qchisq(0.5,1),2)&lt;br /&gt;
  qqplot(qchisq((1:length(x)-0.5)/(length(x)),1),x,ylab=&amp;quot;Observed&amp;quot;,xlab=&amp;quot;Expected&amp;quot;,...);abline(0,1,col=2,lwd=2)&lt;br /&gt;
legend(&amp;quot;topleft&amp;quot;,paste(&amp;quot;lambda=&amp;quot;,lambda))&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
### read in seleciton statistics (chi2 distributed)&lt;br /&gt;
s&amp;lt;-npyLoad(&amp;quot;Demo2PCANGSD_2.selection.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make QQ plot to QC the test statistics&lt;br /&gt;
qqchi(s)&lt;br /&gt;
&lt;br /&gt;
# convert test statistic to p-value&lt;br /&gt;
pval&amp;lt;-1-pchisq(s,1)&lt;br /&gt;
&lt;br /&gt;
## read positions (hg38)&lt;br /&gt;
p&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_2.sites&amp;quot;,colC=c(&amp;quot;factor&amp;quot;,&amp;quot;integer&amp;quot;),sep=&amp;quot;_&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
names(p)&amp;lt;-c(&amp;quot;chr&amp;quot;,&amp;quot;pos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make manhatten plot&lt;br /&gt;
plot(-log10(pval),col=p$chr,xlab=&amp;quot;Chromosomes&amp;quot;,main=&amp;quot;Manhattan plot&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
## zoom into region&lt;br /&gt;
 w&amp;lt;-range(which(pval&amp;lt;1e-7)) + c(-100,100)&lt;br /&gt;
 keep&amp;lt;-w[1]:w[2]&lt;br /&gt;
 plot(p$pos[keep],-log10(pval[keep]),col=p$chr[keep],xlab=&amp;quot;HG38 Position chr2&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## see the position of the most significant SNP&lt;br /&gt;
 p$pos[which.max(s)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1448</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1448"/>
		<updated>2020-05-29T16:03:48Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Create directories */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;We will go through some examples on how to use PCAngsd with visualization of the data.&lt;br /&gt;
&lt;br /&gt;
==== Set the path to PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
Every time you open a new terminal window, set the paths to the program and the input file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PCANGSD=~/Software/pcangsd/pcangsd.py&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test the link&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
ls $PCAngsd&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Create directories ====&lt;br /&gt;
Create the directories that will be used for working:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo/Results&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Set the paths to your local directories ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;IN_DIR=Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;OUT_DIR=Demo/Results&amp;lt;/code&amp;gt;	&lt;br /&gt;
&lt;br /&gt;
Test the links&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 1: Allele Frequencies ===&lt;br /&gt;
&lt;br /&gt;
This example will perform a PCA analysis on 1000 genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
==== Download the input and population information files ====&lt;br /&gt;
&lt;br /&gt;
PCAngsd uses Genotype Likelihoods (GLs) in .beagle format as input. The input file has been created for you. &lt;br /&gt;
&lt;br /&gt;
The population information file is also provided.&lt;br /&gt;
&lt;br /&gt;
To download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1pop.info&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1pop.info $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
==== View the genotype likelihood beagle file ====&lt;br /&gt;
&lt;br /&gt;
*In general, the first three columns of a beagle file contain marker name and the two alleles, allele1 and allele2, present in the locus (in beagle A=0, C=1, G=2, T=3). All following columns contain genotype likelihoods (three columns for each individual: first GL for homozygote for allele1, then GL for heterozygote and then GL for homozygote for allele2). Note that the GL values sum to one per site for each individual. This is just a normalization of the genotype likelihoods in order to avoid underflow problems in the beagle software, but it does not mean that they are genotype probabilities. &lt;br /&gt;
&lt;br /&gt;
*In order to see the first 10 columns and 10 lines of the input file, type:&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | head -n 10 | cut -f 1-10 | column -t&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Use this command to count the number of lines of the input file. The number of lines, indicates the number of loci for which there are GLs plus one (as the command includes the count of the header line):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | wc -l&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View population information file ====&lt;br /&gt;
&lt;br /&gt;
To view a summary of the population information file, cut the first column, sort and count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;cut -f 1 -d &amp;quot; &amp;quot; $IN_DIR/Demo1pop.info | sort | uniq -c&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lets make a population label file and place it in the output directory&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; cut -f1 -d&amp;quot; &amp;quot; $IN_DIR/Demo1pop.info  &amp;gt; $OUT_DIR/poplabel &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Run PCAngsd ===&lt;br /&gt;
&lt;br /&gt;
The program estimates the covariance matrix that can be used for PCA.&lt;br /&gt;
&lt;br /&gt;
==== Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd1.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;, main=&amp;quot;individual allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd1.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Without Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
Try the same analysis but without estimating individual allele frequencies. &lt;br /&gt;
This is the same as using the first iteration of the algorithm.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_2.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd2.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;,main=&amp;quot;joint allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd2.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Admixture based on 1st two PC ====&lt;br /&gt;
Let's try to use the PCA to infer admixture proportions based on the first 2 principal components. For the optimization we will use a small penalty on the admixture proportions (alpha).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD_3 -admix -admix_alpha 50&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
##Requires previous installation of the library RcppCNPy&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q &amp;lt;- npyLoad(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
## order according to population&lt;br /&gt;
ord&amp;lt;-order(pop[,1])&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Admixture proportions&amp;quot;)&lt;br /&gt;
text(tapply(1:nrow(pop),pop[ord,1],mean),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding in the admixed individuals ====&lt;br /&gt;
Inbreeding in admixed samples is usually not possible to estimate using standard approaches. Let's try to estimate the inbreeding coefficient of the samples using the average allele frequency.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_4 -inbreed 2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_4.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The third column is an estimate of the inbreeding coefficient (allowing for negative)&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding with individual allele frequencies ====&lt;br /&gt;
&lt;br /&gt;
Now let's try to estimate the inbreeding coefficient of the samples by using the individual allele frequencies predicted by the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_5 -inbreed 2 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_5.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 2: Selection ===&lt;br /&gt;
&lt;br /&gt;
For very resent selection we can look within closely related individuals, for example within Europeans.&lt;br /&gt;
&lt;br /&gt;
The objective is to use PCAngsd to estimate the covariance matrix while jointly estimating the individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
Data file:&lt;br /&gt;
&lt;br /&gt;
*Genotype likelihoods in Beagle format&lt;br /&gt;
*~150k random SNPs with maf &amp;gt; 5%&lt;br /&gt;
*Four EU populations with ~100 individuals in each&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;table class=&amp;quot;muse-table&amp;quot; border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;CEU&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;European (mostly British)&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;GBR&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Great Britain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;IBS&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Iberian/Spain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;TSI&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Italien&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
:&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Download the input and sample information files ====&lt;br /&gt;
&lt;br /&gt;
A file with the positions and sample information, and a beagle file are provided:&lt;br /&gt;
&lt;br /&gt;
Download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.small.beagle.gz)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2sample.info&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2sample.info $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
==== Run PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
The objective is to show the differences among individuals.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_1 -threads 4&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plot the results in R ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 ## Open R&lt;br /&gt;
&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
 e&amp;lt;-eigen(cov)&lt;br /&gt;
 ID&amp;lt;-read.table(&amp;quot;Demo2sample.info&amp;quot;,head=T)&lt;br /&gt;
&lt;br /&gt;
 pdf(&amp;quot;PCAngsdDemo2_1.pdf&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
 plot(e$vectors[,1:2],col=ID$POP)&lt;br /&gt;
&lt;br /&gt;
 legend(&amp;quot;topleft&amp;quot;,fill=1:4,levels(ID$POP))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the European individuals in 1000G are not simple homogeneous disjoint populations, it is hard to use PBS/FST or similar statistics to infer selection based on population differences. However, PCAngsd offers a good description of the differences among individuals without having to define disjoint groups.&lt;br /&gt;
&lt;br /&gt;
=== Infer selection along the genome ===&lt;br /&gt;
&lt;br /&gt;
Now let's try to use the PC to infer selection along the genome based on the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_2 -selection -sites_save #-n $N &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Plot the results ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
## function for QQplot&lt;br /&gt;
qqchi&amp;lt;-function(x,...){&lt;br /&gt;
lambda&amp;lt;-round(median(x)/qchisq(0.5,1),2)&lt;br /&gt;
  qqplot(qchisq((1:length(x)-0.5)/(length(x)),1),x,ylab=&amp;quot;Observed&amp;quot;,xlab=&amp;quot;Expected&amp;quot;,...);abline(0,1,col=2,lwd=2)&lt;br /&gt;
legend(&amp;quot;topleft&amp;quot;,paste(&amp;quot;lambda=&amp;quot;,lambda))&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
### read in seleciton statistics (chi2 distributed)&lt;br /&gt;
s&amp;lt;-npyLoad(&amp;quot;Demo2PCANGSD_2.selection.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make QQ plot to QC the test statistics&lt;br /&gt;
qqchi(s)&lt;br /&gt;
&lt;br /&gt;
# convert test statistic to p-value&lt;br /&gt;
pval&amp;lt;-1-pchisq(s,1)&lt;br /&gt;
&lt;br /&gt;
## read positions (hg38)&lt;br /&gt;
p&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_2.sites&amp;quot;,colC=c(&amp;quot;factor&amp;quot;,&amp;quot;integer&amp;quot;),sep=&amp;quot;_&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
names(p)&amp;lt;-c(&amp;quot;chr&amp;quot;,&amp;quot;pos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make manhatten plot&lt;br /&gt;
plot(-log10(pval),col=p$chr,xlab=&amp;quot;Chromosomes&amp;quot;,main=&amp;quot;Manhattan plot&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
## zoom into region&lt;br /&gt;
 w&amp;lt;-range(which(pval&amp;lt;1e-7)) + c(-100,100)&lt;br /&gt;
 keep&amp;lt;-w[1]:w[2]&lt;br /&gt;
 plot(p$pos[keep],-log10(pval[keep]),col=p$chr[keep],xlab=&amp;quot;HG38 Position chr2&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## see the position of the most significant SNP&lt;br /&gt;
 p$pos[which.max(s)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1447</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1447"/>
		<updated>2020-05-29T16:02:52Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;We will go through some examples on how to use PCAngsd with visualization of the data.&lt;br /&gt;
&lt;br /&gt;
==== Set the path to PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
Every time you open a new terminal window, set the paths to the program and the input file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PCANGSD=~/Software/pcangsd/pcangsd.py&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test the link&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
ls $PCAngsd&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Create directories ====&lt;br /&gt;
Create the directories that will be used for working:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Demo&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;cd Demo&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mkdir Results&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Set the paths to your local directories ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;IN_DIR=Demo/Data&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;OUT_DIR=Demo/Results&amp;lt;/code&amp;gt;	&lt;br /&gt;
&lt;br /&gt;
Test the links&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 1: Allele Frequencies ===&lt;br /&gt;
&lt;br /&gt;
This example will perform a PCA analysis on 1000 genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
==== Download the input and population information files ====&lt;br /&gt;
&lt;br /&gt;
PCAngsd uses Genotype Likelihoods (GLs) in .beagle format as input. The input file has been created for you. &lt;br /&gt;
&lt;br /&gt;
The population information file is also provided.&lt;br /&gt;
&lt;br /&gt;
To download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo1pop.info&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo1pop.info $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
==== View the genotype likelihood beagle file ====&lt;br /&gt;
&lt;br /&gt;
*In general, the first three columns of a beagle file contain marker name and the two alleles, allele1 and allele2, present in the locus (in beagle A=0, C=1, G=2, T=3). All following columns contain genotype likelihoods (three columns for each individual: first GL for homozygote for allele1, then GL for heterozygote and then GL for homozygote for allele2). Note that the GL values sum to one per site for each individual. This is just a normalization of the genotype likelihoods in order to avoid underflow problems in the beagle software, but it does not mean that they are genotype probabilities. &lt;br /&gt;
&lt;br /&gt;
*In order to see the first 10 columns and 10 lines of the input file, type:&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | head -n 10 | cut -f 1-10 | column -t&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Use this command to count the number of lines of the input file. The number of lines, indicates the number of loci for which there are GLs plus one (as the command includes the count of the header line):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;gunzip -c $IN_DIR/Demo1input.gz | wc -l&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== View population information file ====&lt;br /&gt;
&lt;br /&gt;
To view a summary of the population information file, cut the first column, sort and count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;cut -f 1 -d &amp;quot; &amp;quot; $IN_DIR/Demo1pop.info | sort | uniq -c&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lets make a population label file and place it in the output directory&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; cut -f1 -d&amp;quot; &amp;quot; $IN_DIR/Demo1pop.info  &amp;gt; $OUT_DIR/poplabel &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Run PCAngsd ===&lt;br /&gt;
&lt;br /&gt;
The program estimates the covariance matrix that can be used for PCA.&lt;br /&gt;
&lt;br /&gt;
==== Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd1.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;, main=&amp;quot;individual allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd1.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Without Estimating Individual Allele Frequencies ====&lt;br /&gt;
&lt;br /&gt;
Try the same analysis but without estimating individual allele frequencies. &lt;br /&gt;
This is the same as using the first iteration of the algorithm.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD_2.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
&lt;br /&gt;
pdf(&amp;quot;PCAngsd2.pdf&amp;quot;)&lt;br /&gt;
plot(e$vectors[,1:2],col=pop[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;,main=&amp;quot;joint allele frequency&amp;quot;)&lt;br /&gt;
legend(&amp;quot;top&amp;quot;,fill=1:5,levels(pop[,1]))&lt;br /&gt;
dev.off()&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view the plot, type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;evince PCAngsd2.pdf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Admixture based on 1st two PC ====&lt;br /&gt;
Let's try to use the PCA to infer admixture proportions based on the first 2 principal components. For the optimization we will use a small penalty on the admixture proportions (alpha).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD_3 -admix -admix_alpha 50&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plot the results in R&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#open R&lt;br /&gt;
##Requires previous installation of the library RcppCNPy&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q &amp;lt;- npyLoad(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
## order according to population&lt;br /&gt;
ord&amp;lt;-order(pop[,1])&lt;br /&gt;
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab=&amp;quot;Individuals&amp;quot;,ylab=&amp;quot;Admixture proportions&amp;quot;)&lt;br /&gt;
text(tapply(1:nrow(pop),pop[ord,1],mean),-0.05,unique(pop[ord,1]),xpd=T)&lt;br /&gt;
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)&lt;br /&gt;
&lt;br /&gt;
## close R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding in the admixed individuals ====&lt;br /&gt;
Inbreeding in admixed samples is usually not possible to estimate using standard approaches. Let's try to estimate the inbreeding coefficient of the samples using the average allele frequency.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_4 -inbreed 2 -iter 0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_4.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The third column is an estimate of the inbreeding coefficient (allowing for negative)&lt;br /&gt;
&lt;br /&gt;
==== Inbreeding with individual allele frequencies ====&lt;br /&gt;
&lt;br /&gt;
Now let's try to estimate the inbreeding coefficient of the samples by using the individual allele frequencies predicted by the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_5 -inbreed 2 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Join names and results, sort the values and look at the results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;paste Demo1pop.info Demo1PCANGSD_5.inbreed.npy | LC_ALL=C sort -k3g&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Demo 2: Selection ===&lt;br /&gt;
&lt;br /&gt;
For very resent selection we can look within closely related individuals, for example within Europeans.&lt;br /&gt;
&lt;br /&gt;
The objective is to use PCAngsd to estimate the covariance matrix while jointly estimating the individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
Data file:&lt;br /&gt;
&lt;br /&gt;
*Genotype likelihoods in Beagle format&lt;br /&gt;
*~150k random SNPs with maf &amp;gt; 5%&lt;br /&gt;
*Four EU populations with ~100 individuals in each&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;table class=&amp;quot;muse-table&amp;quot; border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;CEU&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;European (mostly British)&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;GBR&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Great Britain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;IBS&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Iberian/Spain&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;TSI&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;Italien&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
:&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Download the input and sample information files ====&lt;br /&gt;
&lt;br /&gt;
A file with the positions and sample information, and a beagle file are provided:&lt;br /&gt;
&lt;br /&gt;
Download the files and move them to your input folder (for example, $IN_DIR):&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.small.beagle.gz)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;wget popgen.dk/software/download/NGSadmix/data/Demo2sample.info&amp;lt;/code&amp;gt;&lt;br /&gt;
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2input.gz $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mv Demo2sample.info $OUT_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
==== Run PCAngsd ====&lt;br /&gt;
&lt;br /&gt;
The objective is to show the differences among individuals.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_1 -threads 4&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plot the results in R ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 ## Open R&lt;br /&gt;
&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD_1.cov&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
 e&amp;lt;-eigen(cov)&lt;br /&gt;
 ID&amp;lt;-read.table(&amp;quot;Demo2sample.info&amp;quot;,head=T)&lt;br /&gt;
&lt;br /&gt;
 pdf(&amp;quot;PCAngsdDemo2_1.pdf&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
 plot(e$vectors[,1:2],col=ID$POP)&lt;br /&gt;
&lt;br /&gt;
 legend(&amp;quot;topleft&amp;quot;,fill=1:4,levels(ID$POP))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the European individuals in 1000G are not simple homogeneous disjoint populations, it is hard to use PBS/FST or similar statistics to infer selection based on population differences. However, PCAngsd offers a good description of the differences among individuals without having to define disjoint groups.&lt;br /&gt;
&lt;br /&gt;
=== Infer selection along the genome ===&lt;br /&gt;
&lt;br /&gt;
Now let's try to use the PC to infer selection along the genome based on the PCA&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_2 -selection -sites_save #-n $N &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Plot the results ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
library(RcppCNPy) # Numpy library for R&lt;br /&gt;
&lt;br /&gt;
## function for QQplot&lt;br /&gt;
qqchi&amp;lt;-function(x,...){&lt;br /&gt;
lambda&amp;lt;-round(median(x)/qchisq(0.5,1),2)&lt;br /&gt;
  qqplot(qchisq((1:length(x)-0.5)/(length(x)),1),x,ylab=&amp;quot;Observed&amp;quot;,xlab=&amp;quot;Expected&amp;quot;,...);abline(0,1,col=2,lwd=2)&lt;br /&gt;
legend(&amp;quot;topleft&amp;quot;,paste(&amp;quot;lambda=&amp;quot;,lambda))&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
### read in seleciton statistics (chi2 distributed)&lt;br /&gt;
s&amp;lt;-npyLoad(&amp;quot;Demo2PCANGSD_2.selection.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make QQ plot to QC the test statistics&lt;br /&gt;
qqchi(s)&lt;br /&gt;
&lt;br /&gt;
# convert test statistic to p-value&lt;br /&gt;
pval&amp;lt;-1-pchisq(s,1)&lt;br /&gt;
&lt;br /&gt;
## read positions (hg38)&lt;br /&gt;
p&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_2.sites&amp;quot;,colC=c(&amp;quot;factor&amp;quot;,&amp;quot;integer&amp;quot;),sep=&amp;quot;_&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
names(p)&amp;lt;-c(&amp;quot;chr&amp;quot;,&amp;quot;pos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## make manhatten plot&lt;br /&gt;
plot(-log10(pval),col=p$chr,xlab=&amp;quot;Chromosomes&amp;quot;,main=&amp;quot;Manhattan plot&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
## zoom into region&lt;br /&gt;
 w&amp;lt;-range(which(pval&amp;lt;1e-7)) + c(-100,100)&lt;br /&gt;
 keep&amp;lt;-w[1]:w[2]&lt;br /&gt;
 plot(p$pos[keep],-log10(pval[keep]),col=p$chr[keep],xlab=&amp;quot;HG38 Position chr2&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## see the position of the most significant SNP&lt;br /&gt;
 p$pos[which.max(s)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1446</id>
		<title>PCAngsd</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1446"/>
		<updated>2020-05-29T15:59:32Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Quick start */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
PCAngsd is a program that estimates the covariance matrix for low depth next-generation sequencing (NGS) data in structured/heterogeneous populations using principal component analysis (PCA) to perform multiple population genetic analyses using an iterative procedure based on genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
Since version 0.98, PCAngsd was re-written to be based on Cython for computational bottlenecks and parallelization and is now compatible with any newer Python version.&lt;br /&gt;
&lt;br /&gt;
The method was published in 2018 and can be found here: [https://www.genetics.org/content/210/2/719]&lt;br /&gt;
[[File:Pcangsd_admix.gif|frame]]&lt;br /&gt;
&lt;br /&gt;
[[File:Pcangsd_pca.png|thumb|400px|Simulated low depth NGS data of 3 populations]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
Framework for analyzing low depth next-generation sequencing (NGS) data in heterogeneous populations using principal component analysis (PCA). Population structure is inferred to detect the number of significant principal components which is used to estimate individual allele frequencies using genotype dosages in a SVD model. The estimated individual allele frequencies are then used in an probabilistic framework to update the genotype dosages such that an updated set of individual allele frequencies can be estimated iteratively based on inferred population structure. A covariance matrix can be estimated using the updated prior information of the estimated individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
The estimated individual allele frequencies and principal components can be used as prior knowledge in other probabilistic methods based on a same Bayesian principle. PCAngsd can perform the following analyses:&lt;br /&gt;
*Covariance matrix&lt;br /&gt;
*Genotype calling&lt;br /&gt;
*Admixture&lt;br /&gt;
*Inbreeding coefficients (both per-individual and per-site)&lt;br /&gt;
*HWE test&lt;br /&gt;
*Genome selection scan&lt;br /&gt;
*Kinship matrix&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The older version, based on the Numba library (only working with Python 2.7) is still available in version 0.973 and can be found here [https://github.com/Rosemeis/pcangsd/releases/tag/0.973].&lt;br /&gt;
&lt;br /&gt;
=Download and Installation=&lt;br /&gt;
&lt;br /&gt;
PCAngsd should work on all platforms meeting the requirements but server-side usage is highly recommended. &lt;br /&gt;
Installation has only been tested on Linux systems.&lt;br /&gt;
&lt;br /&gt;
Get PCAngsd and build&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
git clone https://github.com/Rosemeis/pcangsd.git&lt;br /&gt;
cd pcangsd/&lt;br /&gt;
python setup.py build_ext --inplace&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Install dependencies:&lt;br /&gt;
&lt;br /&gt;
The required set of Python packages are easily installed using the pip command and the requirements.txt file included in the pcangsd folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;pip install --user -r requirements.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Quick start=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Download the input beagle file with genotype likelihoods&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/input.gz &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Only estimate covariance matrix using 10 threads&lt;br /&gt;
python pcangsd.py -beagle input.gz -o test1 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and individual admixture proportions&lt;br /&gt;
python pcangsd.py -beagle input.gz -admix -o test2 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and inbreeding coefficients&lt;br /&gt;
python pcangsd.py -beagle input.gz -inbreed 2 -o test3 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and perform selection scan&lt;br /&gt;
python pcangsd.py -beagle input.gz -selection -o test4 -threads 10&lt;br /&gt;
&lt;br /&gt;
# perform PCA on covarince matrix in R&lt;br /&gt;
## open R&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;test1.cov&amp;quot;))&lt;br /&gt;
e &amp;lt;- eigen(C)&lt;br /&gt;
plot(e$vectors[,1:2],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Detailed Examples and Tutorial==&lt;br /&gt;
&lt;br /&gt;
Please refer to the tutorial's page [http://www.popgen.dk/software/index.php/PCAngsdTutorial]&lt;br /&gt;
&lt;br /&gt;
=Input=&lt;br /&gt;
The only input PCAngsd needs and accepts are genotype likelihoods in  [http://faculty.washington.edu/browning/beagle/beagle.html Beagle] format. New functionality for using PLINK files has been added (version 0.9). Genotypes are automatically converted into a genotype likelihood matrix where the user can incorporate an error model.&lt;br /&gt;
&lt;br /&gt;
[http://popgen.dk/angsd ANGSD] can be easily be used to compute genotype likelihoods and output them in the required Beagle format.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
./angsd -GL 2 -out data -nThreads 10 -doGlf 2 -doMajorMinor 1 -doMaf 2 -SNP_pval 1e-6 -bam bam.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See [http://popgen.dk/angsd ANGSD] for more information on how to compute the genotype likelihoods and call SNPs.&lt;br /&gt;
&lt;br /&gt;
=Output=&lt;br /&gt;
&lt;br /&gt;
Since version 0.98, PCAngsd's output is only in binary Numpy format (.npy) except for the covariance matrix. &lt;br /&gt;
&lt;br /&gt;
In order to read files in python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
S = np.load(&amp;quot;output.selection.npy&amp;quot;) # Reads results from selection scan&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
R can also read Numpy matrices using the &amp;quot;RcppCNPy&amp;quot; library:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
library(RcppCNPy)&lt;br /&gt;
S &amp;lt;- npyLoad(&amp;quot;output.selection.npy&amp;quot;) # Reads results from selection scan&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Using PCAngsd=&lt;br /&gt;
&lt;br /&gt;
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.´&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# See all options in PCAngsd&lt;br /&gt;
python pcangsd.py -h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Estimation of individual allele frequencies==&lt;br /&gt;
; -beagle [Beagle filename]&lt;br /&gt;
Input file of genotype likelihoods in Beagle format (.beagle.gz).&lt;br /&gt;
; -plink [Prefix for binary PLINK files]&lt;br /&gt;
Path to PLINK files using their prefix (.bed, .bim, .fam).&lt;br /&gt;
; -plink_error [float]&lt;br /&gt;
Incorporate error model for PLINK genotypes.&lt;br /&gt;
; -minMaf [float]&lt;br /&gt;
Minimum minor allele frequency threshold. (Default: 0.05)&lt;br /&gt;
; -iter [int]&lt;br /&gt;
Maximum number of iterations for estimation of individual allele frequencies (Default: 100).&lt;br /&gt;
; -tole [float]&lt;br /&gt;
Tolerance value for update in estimation of individual allele frequencies (Default: 1e-5).&lt;br /&gt;
; -maf_iter [int]&lt;br /&gt;
Maximum number of EM iterations for computing the population allele frequencies (Default: 100).&lt;br /&gt;
; -maf_tole [float]&lt;br /&gt;
Tolerance value in EM algorithm for population allele frequencies estimation (Default: 1e-4).&lt;br /&gt;
; -e [int]&lt;br /&gt;
Manually select the number of eigenvalues to use in the modelling of individual allele frequencies (Default: Automatically tested using MAP test).&lt;br /&gt;
; -o [prefix]&lt;br /&gt;
Set the prefix for all output files created by PCAngsd (Default: &amp;quot;pcangsd&amp;quot;).&lt;br /&gt;
; -indf_save&lt;br /&gt;
Choose to save estimated individual allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
; -dosage_save&lt;br /&gt;
Choose to save estimated genotype dosages (Binary). Numpy format (.npy).&lt;br /&gt;
; -sites_save&lt;br /&gt;
Choose to save the marker IDs after performing filtering using population allele frequencies. Useful for especially selection scans and per-site inbreeding coefficients.&lt;br /&gt;
; -post_save&lt;br /&gt;
Choose to save the posterior genotype probabilities. Beagle format (.beagle).&lt;br /&gt;
; -threads [int]&lt;br /&gt;
Specify the number of thread(s) to use (Default: 1).&lt;br /&gt;
&lt;br /&gt;
==Call genotypes==&lt;br /&gt;
Genotypes can be called from posterior genotype probabilities incorporating the individual allele frequencies as prior information.&lt;br /&gt;
&lt;br /&gt;
; -geno [float]&lt;br /&gt;
Call genotypes with defined threshold.&lt;br /&gt;
; -genoInbreed [float]&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Admixture==&lt;br /&gt;
Individual admixture proportions and population-specific allele frequencies can be estimated based on assuming K ancestral populations using an accelerated mini-batch NMF method.&lt;br /&gt;
&lt;br /&gt;
; -admix&lt;br /&gt;
Toggles admixture estimations. Individual ancestry proportions are saved (Binary). Numpy format (.npy).&lt;br /&gt;
; -admix_alpha [float-list]&lt;br /&gt;
Specify alpha (sparseness regularization parameter). Can be specified as a sequence to try several alpha's in a single run (Default: 0).&lt;br /&gt;
; -admix_auto [float]&lt;br /&gt;
Enable automatic search for optimal alpha using likelihood measure, by giving soft upper search bound of alpha.&lt;br /&gt;
; -admix_seed [int-list]&lt;br /&gt;
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.&lt;br /&gt;
; -admix_K [int]&lt;br /&gt;
Not recommended. Manually specify the number of ancestral populations to use in admixture estimations (overrides number chosen from '''-e'''). Structure explained by individual allele frequencies may therefore not reflect the manually chosen K. It is recommended to adjust '''-e''' instead of '''-admix_K'''.&lt;br /&gt;
; -admix_iter [int]&lt;br /&gt;
Maximum number of iterations for admixture estimations using NMF. (Default: 200)&lt;br /&gt;
; -admix_tole [float]&lt;br /&gt;
Tolerance value for update in admixture estimations using NMF. (Default: 1e-5)&lt;br /&gt;
; -admix_batch [int]&lt;br /&gt;
Specify the number of batches to use in NMF method. (Default: 10)&lt;br /&gt;
; -admix_save&lt;br /&gt;
Choose to save the population-specific allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
&lt;br /&gt;
==Inbreeding==&lt;br /&gt;
Per-individual inbreeding coefficients incorporating population structure can be computed using three different methods. However, -inbreed 1 is recommended for low depth cases.&lt;br /&gt;
&lt;br /&gt;
; -inbreed 1&lt;br /&gt;
Simple estimator computed by an EM algorithm. Allows for F-values between -1 and 1. Based on [http://genome.cshlp.org/content/23/11/1852.full ngsF].&lt;br /&gt;
&lt;br /&gt;
; -inbreed 2&lt;br /&gt;
A maximum likelihood estimator also 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].&lt;br /&gt;
&lt;br /&gt;
; -inbreed 3&lt;br /&gt;
Estimator using an estimated kinship matrix. Allows for F-values between -1 and 1. Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate].  &lt;br /&gt;
&lt;br /&gt;
; -inbreed_iter [int]&lt;br /&gt;
Maximum number of iterations for the EM algorithm methods. (Default: 200)&lt;br /&gt;
&lt;br /&gt;
; -inbreed_tole [float]&lt;br /&gt;
Tolerance value for the EM algorithms for inbreeding coefficients estimation. (Default: 1e-4)&lt;br /&gt;
Per-site inbreeding coefficients incorporating population structure alongside likehood ratio tests for HWE can be computed as follows:&lt;br /&gt;
&lt;br /&gt;
; -inbreedSites&lt;br /&gt;
Use likelihood ratio tests (.lrt.sites.gz) generated from '''-inbreedSites''' to filter out variable sites using a given threshold for HWE test p-value:&lt;br /&gt;
&lt;br /&gt;
; -hwe [LRT filename]&lt;br /&gt;
&lt;br /&gt;
; -hwe_tole [float]&lt;br /&gt;
Tolerance value for HWE test. (Default: 1e-6)&lt;br /&gt;
&lt;br /&gt;
==Selection==&lt;br /&gt;
A genome selection scan can be computed based on posterior expectations of the genotypes (genotype dosages):&lt;br /&gt;
&lt;br /&gt;
; -selection&lt;br /&gt;
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. 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.&lt;br /&gt;
&lt;br /&gt;
==Relatedness==&lt;br /&gt;
Estimate kinship matrix based on method Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate]:&lt;br /&gt;
&lt;br /&gt;
; -kinship&lt;br /&gt;
Automatically estimated if '''-inbreed 3''' has been selected.&lt;br /&gt;
&lt;br /&gt;
Remove related individuals based on kinhsip matrix of previous run:&lt;br /&gt;
; -relate [Kinship filename]&lt;br /&gt;
; -relate_tole [float]&lt;br /&gt;
Threshold for kinship coefficients for removing individuals (Default: 0.0625).&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Our methods for inferring population structure have been published in GENETICS:&lt;br /&gt;
&lt;br /&gt;
[http://www.genetics.org/content/early/2018/08/21/genetics.118.301336 Inferring Population Structure and Admixture Proportions in Low Depth NGS Data]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Our method for testing for HWE in structured populations has been published in Molecular Ecology Resources:&lt;br /&gt;
&lt;br /&gt;
[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]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1445</id>
		<title>PCAngsd</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1445"/>
		<updated>2020-05-29T15:56:45Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Output */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
PCAngsd is a program that estimates the covariance matrix for low depth next-generation sequencing (NGS) data in structured/heterogeneous populations using principal component analysis (PCA) to perform multiple population genetic analyses using an iterative procedure based on genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
Since version 0.98, PCAngsd was re-written to be based on Cython for computational bottlenecks and parallelization and is now compatible with any newer Python version.&lt;br /&gt;
&lt;br /&gt;
The method was published in 2018 and can be found here: [https://www.genetics.org/content/210/2/719]&lt;br /&gt;
[[File:Pcangsd_admix.gif|frame]]&lt;br /&gt;
&lt;br /&gt;
[[File:Pcangsd_pca.png|thumb|400px|Simulated low depth NGS data of 3 populations]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
Framework for analyzing low depth next-generation sequencing (NGS) data in heterogeneous populations using principal component analysis (PCA). Population structure is inferred to detect the number of significant principal components which is used to estimate individual allele frequencies using genotype dosages in a SVD model. The estimated individual allele frequencies are then used in an probabilistic framework to update the genotype dosages such that an updated set of individual allele frequencies can be estimated iteratively based on inferred population structure. A covariance matrix can be estimated using the updated prior information of the estimated individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
The estimated individual allele frequencies and principal components can be used as prior knowledge in other probabilistic methods based on a same Bayesian principle. PCAngsd can perform the following analyses:&lt;br /&gt;
*Covariance matrix&lt;br /&gt;
*Genotype calling&lt;br /&gt;
*Admixture&lt;br /&gt;
*Inbreeding coefficients (both per-individual and per-site)&lt;br /&gt;
*HWE test&lt;br /&gt;
*Genome selection scan&lt;br /&gt;
*Kinship matrix&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The older version, based on the Numba library (only working with Python 2.7) is still available in version 0.973 and can be found here [https://github.com/Rosemeis/pcangsd/releases/tag/0.973].&lt;br /&gt;
&lt;br /&gt;
=Download and Installation=&lt;br /&gt;
&lt;br /&gt;
PCAngsd should work on all platforms meeting the requirements but server-side usage is highly recommended. &lt;br /&gt;
Installation has only been tested on Linux systems.&lt;br /&gt;
&lt;br /&gt;
Get PCAngsd and build&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
git clone https://github.com/Rosemeis/pcangsd.git&lt;br /&gt;
cd pcangsd/&lt;br /&gt;
python setup.py build_ext --inplace&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Install dependencies:&lt;br /&gt;
&lt;br /&gt;
The required set of Python packages are easily installed using the pip command and the requirements.txt file included in the pcangsd folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;pip install --user -r requirements.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Quick start=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Download the input beagle file with genotype likelihoods&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/input.gz &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Only estimate covariance matrix using 10 threads&lt;br /&gt;
python pcangsd.py -beagle input.gz -o test1 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and individual admixture proportions&lt;br /&gt;
python pcangsd.py -beagle input.gz -admix -o test2 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and inbreeding coefficients&lt;br /&gt;
python pcangsd.py -beagle input.gz -inbreed 2 -o test3 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and perform selection scan&lt;br /&gt;
python pcangsd.py -beagle input.gz -selection -o test4 -threads 10&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Detailed Examples and Tutorial==&lt;br /&gt;
&lt;br /&gt;
Please refer to the tutorial's page [http://www.popgen.dk/software/index.php/PCAngsdTutorial]&lt;br /&gt;
&lt;br /&gt;
=Input=&lt;br /&gt;
The only input PCAngsd needs and accepts are genotype likelihoods in  [http://faculty.washington.edu/browning/beagle/beagle.html Beagle] format. New functionality for using PLINK files has been added (version 0.9). Genotypes are automatically converted into a genotype likelihood matrix where the user can incorporate an error model.&lt;br /&gt;
&lt;br /&gt;
[http://popgen.dk/angsd ANGSD] can be easily be used to compute genotype likelihoods and output them in the required Beagle format.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
./angsd -GL 2 -out data -nThreads 10 -doGlf 2 -doMajorMinor 1 -doMaf 2 -SNP_pval 1e-6 -bam bam.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See [http://popgen.dk/angsd ANGSD] for more information on how to compute the genotype likelihoods and call SNPs.&lt;br /&gt;
&lt;br /&gt;
=Output=&lt;br /&gt;
&lt;br /&gt;
Since version 0.98, PCAngsd's output is only in binary Numpy format (.npy) except for the covariance matrix. &lt;br /&gt;
&lt;br /&gt;
In order to read files in python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
S = np.load(&amp;quot;output.selection.npy&amp;quot;) # Reads results from selection scan&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
R can also read Numpy matrices using the &amp;quot;RcppCNPy&amp;quot; library:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
library(RcppCNPy)&lt;br /&gt;
S &amp;lt;- npyLoad(&amp;quot;output.selection.npy&amp;quot;) # Reads results from selection scan&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Using PCAngsd=&lt;br /&gt;
&lt;br /&gt;
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.´&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# See all options in PCAngsd&lt;br /&gt;
python pcangsd.py -h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Estimation of individual allele frequencies==&lt;br /&gt;
; -beagle [Beagle filename]&lt;br /&gt;
Input file of genotype likelihoods in Beagle format (.beagle.gz).&lt;br /&gt;
; -plink [Prefix for binary PLINK files]&lt;br /&gt;
Path to PLINK files using their prefix (.bed, .bim, .fam).&lt;br /&gt;
; -plink_error [float]&lt;br /&gt;
Incorporate error model for PLINK genotypes.&lt;br /&gt;
; -minMaf [float]&lt;br /&gt;
Minimum minor allele frequency threshold. (Default: 0.05)&lt;br /&gt;
; -iter [int]&lt;br /&gt;
Maximum number of iterations for estimation of individual allele frequencies (Default: 100).&lt;br /&gt;
; -tole [float]&lt;br /&gt;
Tolerance value for update in estimation of individual allele frequencies (Default: 1e-5).&lt;br /&gt;
; -maf_iter [int]&lt;br /&gt;
Maximum number of EM iterations for computing the population allele frequencies (Default: 100).&lt;br /&gt;
; -maf_tole [float]&lt;br /&gt;
Tolerance value in EM algorithm for population allele frequencies estimation (Default: 1e-4).&lt;br /&gt;
; -e [int]&lt;br /&gt;
Manually select the number of eigenvalues to use in the modelling of individual allele frequencies (Default: Automatically tested using MAP test).&lt;br /&gt;
; -o [prefix]&lt;br /&gt;
Set the prefix for all output files created by PCAngsd (Default: &amp;quot;pcangsd&amp;quot;).&lt;br /&gt;
; -indf_save&lt;br /&gt;
Choose to save estimated individual allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
; -dosage_save&lt;br /&gt;
Choose to save estimated genotype dosages (Binary). Numpy format (.npy).&lt;br /&gt;
; -sites_save&lt;br /&gt;
Choose to save the marker IDs after performing filtering using population allele frequencies. Useful for especially selection scans and per-site inbreeding coefficients.&lt;br /&gt;
; -post_save&lt;br /&gt;
Choose to save the posterior genotype probabilities. Beagle format (.beagle).&lt;br /&gt;
; -threads [int]&lt;br /&gt;
Specify the number of thread(s) to use (Default: 1).&lt;br /&gt;
&lt;br /&gt;
==Call genotypes==&lt;br /&gt;
Genotypes can be called from posterior genotype probabilities incorporating the individual allele frequencies as prior information.&lt;br /&gt;
&lt;br /&gt;
; -geno [float]&lt;br /&gt;
Call genotypes with defined threshold.&lt;br /&gt;
; -genoInbreed [float]&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Admixture==&lt;br /&gt;
Individual admixture proportions and population-specific allele frequencies can be estimated based on assuming K ancestral populations using an accelerated mini-batch NMF method.&lt;br /&gt;
&lt;br /&gt;
; -admix&lt;br /&gt;
Toggles admixture estimations. Individual ancestry proportions are saved (Binary). Numpy format (.npy).&lt;br /&gt;
; -admix_alpha [float-list]&lt;br /&gt;
Specify alpha (sparseness regularization parameter). Can be specified as a sequence to try several alpha's in a single run (Default: 0).&lt;br /&gt;
; -admix_auto [float]&lt;br /&gt;
Enable automatic search for optimal alpha using likelihood measure, by giving soft upper search bound of alpha.&lt;br /&gt;
; -admix_seed [int-list]&lt;br /&gt;
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.&lt;br /&gt;
; -admix_K [int]&lt;br /&gt;
Not recommended. Manually specify the number of ancestral populations to use in admixture estimations (overrides number chosen from '''-e'''). Structure explained by individual allele frequencies may therefore not reflect the manually chosen K. It is recommended to adjust '''-e''' instead of '''-admix_K'''.&lt;br /&gt;
; -admix_iter [int]&lt;br /&gt;
Maximum number of iterations for admixture estimations using NMF. (Default: 200)&lt;br /&gt;
; -admix_tole [float]&lt;br /&gt;
Tolerance value for update in admixture estimations using NMF. (Default: 1e-5)&lt;br /&gt;
; -admix_batch [int]&lt;br /&gt;
Specify the number of batches to use in NMF method. (Default: 10)&lt;br /&gt;
; -admix_save&lt;br /&gt;
Choose to save the population-specific allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
&lt;br /&gt;
==Inbreeding==&lt;br /&gt;
Per-individual inbreeding coefficients incorporating population structure can be computed using three different methods. However, -inbreed 1 is recommended for low depth cases.&lt;br /&gt;
&lt;br /&gt;
; -inbreed 1&lt;br /&gt;
Simple estimator computed by an EM algorithm. Allows for F-values between -1 and 1. Based on [http://genome.cshlp.org/content/23/11/1852.full ngsF].&lt;br /&gt;
&lt;br /&gt;
; -inbreed 2&lt;br /&gt;
A maximum likelihood estimator also 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].&lt;br /&gt;
&lt;br /&gt;
; -inbreed 3&lt;br /&gt;
Estimator using an estimated kinship matrix. Allows for F-values between -1 and 1. Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate].  &lt;br /&gt;
&lt;br /&gt;
; -inbreed_iter [int]&lt;br /&gt;
Maximum number of iterations for the EM algorithm methods. (Default: 200)&lt;br /&gt;
&lt;br /&gt;
; -inbreed_tole [float]&lt;br /&gt;
Tolerance value for the EM algorithms for inbreeding coefficients estimation. (Default: 1e-4)&lt;br /&gt;
Per-site inbreeding coefficients incorporating population structure alongside likehood ratio tests for HWE can be computed as follows:&lt;br /&gt;
&lt;br /&gt;
; -inbreedSites&lt;br /&gt;
Use likelihood ratio tests (.lrt.sites.gz) generated from '''-inbreedSites''' to filter out variable sites using a given threshold for HWE test p-value:&lt;br /&gt;
&lt;br /&gt;
; -hwe [LRT filename]&lt;br /&gt;
&lt;br /&gt;
; -hwe_tole [float]&lt;br /&gt;
Tolerance value for HWE test. (Default: 1e-6)&lt;br /&gt;
&lt;br /&gt;
==Selection==&lt;br /&gt;
A genome selection scan can be computed based on posterior expectations of the genotypes (genotype dosages):&lt;br /&gt;
&lt;br /&gt;
; -selection&lt;br /&gt;
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. 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.&lt;br /&gt;
&lt;br /&gt;
==Relatedness==&lt;br /&gt;
Estimate kinship matrix based on method Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate]:&lt;br /&gt;
&lt;br /&gt;
; -kinship&lt;br /&gt;
Automatically estimated if '''-inbreed 3''' has been selected.&lt;br /&gt;
&lt;br /&gt;
Remove related individuals based on kinhsip matrix of previous run:&lt;br /&gt;
; -relate [Kinship filename]&lt;br /&gt;
; -relate_tole [float]&lt;br /&gt;
Threshold for kinship coefficients for removing individuals (Default: 0.0625).&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Our methods for inferring population structure have been published in GENETICS:&lt;br /&gt;
&lt;br /&gt;
[http://www.genetics.org/content/early/2018/08/21/genetics.118.301336 Inferring Population Structure and Admixture Proportions in Low Depth NGS Data]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Our method for testing for HWE in structured populations has been published in Molecular Ecology Resources:&lt;br /&gt;
&lt;br /&gt;
[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]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1444</id>
		<title>PCAngsd</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1444"/>
		<updated>2020-05-29T15:51:56Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Download and Installation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
PCAngsd is a program that estimates the covariance matrix for low depth next-generation sequencing (NGS) data in structured/heterogeneous populations using principal component analysis (PCA) to perform multiple population genetic analyses using an iterative procedure based on genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
Since version 0.98, PCAngsd was re-written to be based on Cython for computational bottlenecks and parallelization and is now compatible with any newer Python version.&lt;br /&gt;
&lt;br /&gt;
The method was published in 2018 and can be found here: [https://www.genetics.org/content/210/2/719]&lt;br /&gt;
[[File:Pcangsd_admix.gif|frame]]&lt;br /&gt;
&lt;br /&gt;
[[File:Pcangsd_pca.png|thumb|400px|Simulated low depth NGS data of 3 populations]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
Framework for analyzing low depth next-generation sequencing (NGS) data in heterogeneous populations using principal component analysis (PCA). Population structure is inferred to detect the number of significant principal components which is used to estimate individual allele frequencies using genotype dosages in a SVD model. The estimated individual allele frequencies are then used in an probabilistic framework to update the genotype dosages such that an updated set of individual allele frequencies can be estimated iteratively based on inferred population structure. A covariance matrix can be estimated using the updated prior information of the estimated individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
The estimated individual allele frequencies and principal components can be used as prior knowledge in other probabilistic methods based on a same Bayesian principle. PCAngsd can perform the following analyses:&lt;br /&gt;
*Covariance matrix&lt;br /&gt;
*Genotype calling&lt;br /&gt;
*Admixture&lt;br /&gt;
*Inbreeding coefficients (both per-individual and per-site)&lt;br /&gt;
*HWE test&lt;br /&gt;
*Genome selection scan&lt;br /&gt;
*Kinship matrix&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The older version, based on the Numba library (only working with Python 2.7) is still available in version 0.973 and can be found here [https://github.com/Rosemeis/pcangsd/releases/tag/0.973].&lt;br /&gt;
&lt;br /&gt;
=Download and Installation=&lt;br /&gt;
&lt;br /&gt;
PCAngsd should work on all platforms meeting the requirements but server-side usage is highly recommended. &lt;br /&gt;
Installation has only been tested on Linux systems.&lt;br /&gt;
&lt;br /&gt;
Get PCAngsd and build&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
git clone https://github.com/Rosemeis/pcangsd.git&lt;br /&gt;
cd pcangsd/&lt;br /&gt;
python setup.py build_ext --inplace&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Install dependencies:&lt;br /&gt;
&lt;br /&gt;
The required set of Python packages are easily installed using the pip command and the requirements.txt file included in the pcangsd folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;pip install --user -r requirements.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Quick start=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Download the input beagle file with genotype likelihoods&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/input.gz &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Only estimate covariance matrix using 10 threads&lt;br /&gt;
python pcangsd.py -beagle input.gz -o test1 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and individual admixture proportions&lt;br /&gt;
python pcangsd.py -beagle input.gz -admix -o test2 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and inbreeding coefficients&lt;br /&gt;
python pcangsd.py -beagle input.gz -inbreed 2 -o test3 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and perform selection scan&lt;br /&gt;
python pcangsd.py -beagle input.gz -selection -o test4 -threads 10&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Detailed Examples and Tutorial==&lt;br /&gt;
&lt;br /&gt;
Please refer to the tutorial's page [http://www.popgen.dk/software/index.php/PCAngsdTutorial]&lt;br /&gt;
&lt;br /&gt;
=Input=&lt;br /&gt;
The only input PCAngsd needs and accepts are genotype likelihoods in  [http://faculty.washington.edu/browning/beagle/beagle.html Beagle] format. New functionality for using PLINK files has been added (version 0.9). Genotypes are automatically converted into a genotype likelihood matrix where the user can incorporate an error model.&lt;br /&gt;
&lt;br /&gt;
[http://popgen.dk/angsd ANGSD] can be easily be used to compute genotype likelihoods and output them in the required Beagle format.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
./angsd -GL 2 -out data -nThreads 10 -doGlf 2 -doMajorMinor 1 -doMaf 2 -SNP_pval 1e-6 -bam bam.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See [http://popgen.dk/angsd ANGSD] for more information on how to compute the genotype likelihoods and call SNPs.&lt;br /&gt;
&lt;br /&gt;
=Output=&lt;br /&gt;
&lt;br /&gt;
Since version 0.98, PCAngsd's output is only in binary Numpy format (.npy) except for the covariance matrix. &lt;br /&gt;
&lt;br /&gt;
In order to read files in python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
C = np.genfromtxt(&amp;quot;output.cov&amp;quot;) # Reads in estimated covariance matrix&lt;br /&gt;
S = np.load(&amp;quot;output.selection.npy&amp;quot;) # Reads results from selection scan&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
R can also read Numpy matrices using the &amp;quot;RcppCNPy&amp;quot; library:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
library(RcppCNPy)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;output.cov&amp;quot;)) # Reads in estimated covariance matrix&lt;br /&gt;
S &amp;lt;- npyLoad(&amp;quot;output.selection.npy&amp;quot;) # Reads results from selection scan&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Using PCAngsd=&lt;br /&gt;
&lt;br /&gt;
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.´&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# See all options in PCAngsd&lt;br /&gt;
python pcangsd.py -h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Estimation of individual allele frequencies==&lt;br /&gt;
; -beagle [Beagle filename]&lt;br /&gt;
Input file of genotype likelihoods in Beagle format (.beagle.gz).&lt;br /&gt;
; -plink [Prefix for binary PLINK files]&lt;br /&gt;
Path to PLINK files using their prefix (.bed, .bim, .fam).&lt;br /&gt;
; -plink_error [float]&lt;br /&gt;
Incorporate error model for PLINK genotypes.&lt;br /&gt;
; -minMaf [float]&lt;br /&gt;
Minimum minor allele frequency threshold. (Default: 0.05)&lt;br /&gt;
; -iter [int]&lt;br /&gt;
Maximum number of iterations for estimation of individual allele frequencies (Default: 100).&lt;br /&gt;
; -tole [float]&lt;br /&gt;
Tolerance value for update in estimation of individual allele frequencies (Default: 1e-5).&lt;br /&gt;
; -maf_iter [int]&lt;br /&gt;
Maximum number of EM iterations for computing the population allele frequencies (Default: 100).&lt;br /&gt;
; -maf_tole [float]&lt;br /&gt;
Tolerance value in EM algorithm for population allele frequencies estimation (Default: 1e-4).&lt;br /&gt;
; -e [int]&lt;br /&gt;
Manually select the number of eigenvalues to use in the modelling of individual allele frequencies (Default: Automatically tested using MAP test).&lt;br /&gt;
; -o [prefix]&lt;br /&gt;
Set the prefix for all output files created by PCAngsd (Default: &amp;quot;pcangsd&amp;quot;).&lt;br /&gt;
; -indf_save&lt;br /&gt;
Choose to save estimated individual allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
; -dosage_save&lt;br /&gt;
Choose to save estimated genotype dosages (Binary). Numpy format (.npy).&lt;br /&gt;
; -sites_save&lt;br /&gt;
Choose to save the marker IDs after performing filtering using population allele frequencies. Useful for especially selection scans and per-site inbreeding coefficients.&lt;br /&gt;
; -post_save&lt;br /&gt;
Choose to save the posterior genotype probabilities. Beagle format (.beagle).&lt;br /&gt;
; -threads [int]&lt;br /&gt;
Specify the number of thread(s) to use (Default: 1).&lt;br /&gt;
&lt;br /&gt;
==Call genotypes==&lt;br /&gt;
Genotypes can be called from posterior genotype probabilities incorporating the individual allele frequencies as prior information.&lt;br /&gt;
&lt;br /&gt;
; -geno [float]&lt;br /&gt;
Call genotypes with defined threshold.&lt;br /&gt;
; -genoInbreed [float]&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Admixture==&lt;br /&gt;
Individual admixture proportions and population-specific allele frequencies can be estimated based on assuming K ancestral populations using an accelerated mini-batch NMF method.&lt;br /&gt;
&lt;br /&gt;
; -admix&lt;br /&gt;
Toggles admixture estimations. Individual ancestry proportions are saved (Binary). Numpy format (.npy).&lt;br /&gt;
; -admix_alpha [float-list]&lt;br /&gt;
Specify alpha (sparseness regularization parameter). Can be specified as a sequence to try several alpha's in a single run (Default: 0).&lt;br /&gt;
; -admix_auto [float]&lt;br /&gt;
Enable automatic search for optimal alpha using likelihood measure, by giving soft upper search bound of alpha.&lt;br /&gt;
; -admix_seed [int-list]&lt;br /&gt;
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.&lt;br /&gt;
; -admix_K [int]&lt;br /&gt;
Not recommended. Manually specify the number of ancestral populations to use in admixture estimations (overrides number chosen from '''-e'''). Structure explained by individual allele frequencies may therefore not reflect the manually chosen K. It is recommended to adjust '''-e''' instead of '''-admix_K'''.&lt;br /&gt;
; -admix_iter [int]&lt;br /&gt;
Maximum number of iterations for admixture estimations using NMF. (Default: 200)&lt;br /&gt;
; -admix_tole [float]&lt;br /&gt;
Tolerance value for update in admixture estimations using NMF. (Default: 1e-5)&lt;br /&gt;
; -admix_batch [int]&lt;br /&gt;
Specify the number of batches to use in NMF method. (Default: 10)&lt;br /&gt;
; -admix_save&lt;br /&gt;
Choose to save the population-specific allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
&lt;br /&gt;
==Inbreeding==&lt;br /&gt;
Per-individual inbreeding coefficients incorporating population structure can be computed using three different methods. However, -inbreed 1 is recommended for low depth cases.&lt;br /&gt;
&lt;br /&gt;
; -inbreed 1&lt;br /&gt;
Simple estimator computed by an EM algorithm. Allows for F-values between -1 and 1. Based on [http://genome.cshlp.org/content/23/11/1852.full ngsF].&lt;br /&gt;
&lt;br /&gt;
; -inbreed 2&lt;br /&gt;
A maximum likelihood estimator also 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].&lt;br /&gt;
&lt;br /&gt;
; -inbreed 3&lt;br /&gt;
Estimator using an estimated kinship matrix. Allows for F-values between -1 and 1. Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate].  &lt;br /&gt;
&lt;br /&gt;
; -inbreed_iter [int]&lt;br /&gt;
Maximum number of iterations for the EM algorithm methods. (Default: 200)&lt;br /&gt;
&lt;br /&gt;
; -inbreed_tole [float]&lt;br /&gt;
Tolerance value for the EM algorithms for inbreeding coefficients estimation. (Default: 1e-4)&lt;br /&gt;
Per-site inbreeding coefficients incorporating population structure alongside likehood ratio tests for HWE can be computed as follows:&lt;br /&gt;
&lt;br /&gt;
; -inbreedSites&lt;br /&gt;
Use likelihood ratio tests (.lrt.sites.gz) generated from '''-inbreedSites''' to filter out variable sites using a given threshold for HWE test p-value:&lt;br /&gt;
&lt;br /&gt;
; -hwe [LRT filename]&lt;br /&gt;
&lt;br /&gt;
; -hwe_tole [float]&lt;br /&gt;
Tolerance value for HWE test. (Default: 1e-6)&lt;br /&gt;
&lt;br /&gt;
==Selection==&lt;br /&gt;
A genome selection scan can be computed based on posterior expectations of the genotypes (genotype dosages):&lt;br /&gt;
&lt;br /&gt;
; -selection&lt;br /&gt;
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. 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.&lt;br /&gt;
&lt;br /&gt;
==Relatedness==&lt;br /&gt;
Estimate kinship matrix based on method Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate]:&lt;br /&gt;
&lt;br /&gt;
; -kinship&lt;br /&gt;
Automatically estimated if '''-inbreed 3''' has been selected.&lt;br /&gt;
&lt;br /&gt;
Remove related individuals based on kinhsip matrix of previous run:&lt;br /&gt;
; -relate [Kinship filename]&lt;br /&gt;
; -relate_tole [float]&lt;br /&gt;
Threshold for kinship coefficients for removing individuals (Default: 0.0625).&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Our methods for inferring population structure have been published in GENETICS:&lt;br /&gt;
&lt;br /&gt;
[http://www.genetics.org/content/early/2018/08/21/genetics.118.301336 Inferring Population Structure and Admixture Proportions in Low Depth NGS Data]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Our method for testing for HWE in structured populations has been published in Molecular Ecology Resources:&lt;br /&gt;
&lt;br /&gt;
[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]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1443</id>
		<title>PCAngsd</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1443"/>
		<updated>2020-05-29T15:50:32Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
PCAngsd is a program that estimates the covariance matrix for low depth next-generation sequencing (NGS) data in structured/heterogeneous populations using principal component analysis (PCA) to perform multiple population genetic analyses using an iterative procedure based on genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
Since version 0.98, PCAngsd was re-written to be based on Cython for computational bottlenecks and parallelization and is now compatible with any newer Python version.&lt;br /&gt;
&lt;br /&gt;
The method was published in 2018 and can be found here: [https://www.genetics.org/content/210/2/719]&lt;br /&gt;
[[File:Pcangsd_admix.gif|frame]]&lt;br /&gt;
&lt;br /&gt;
[[File:Pcangsd_pca.png|thumb|400px|Simulated low depth NGS data of 3 populations]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
Framework for analyzing low depth next-generation sequencing (NGS) data in heterogeneous populations using principal component analysis (PCA). Population structure is inferred to detect the number of significant principal components which is used to estimate individual allele frequencies using genotype dosages in a SVD model. The estimated individual allele frequencies are then used in an probabilistic framework to update the genotype dosages such that an updated set of individual allele frequencies can be estimated iteratively based on inferred population structure. A covariance matrix can be estimated using the updated prior information of the estimated individual allele frequencies.&lt;br /&gt;
&lt;br /&gt;
The estimated individual allele frequencies and principal components can be used as prior knowledge in other probabilistic methods based on a same Bayesian principle. PCAngsd can perform the following analyses:&lt;br /&gt;
*Covariance matrix&lt;br /&gt;
*Genotype calling&lt;br /&gt;
*Admixture&lt;br /&gt;
*Inbreeding coefficients (both per-individual and per-site)&lt;br /&gt;
*HWE test&lt;br /&gt;
*Genome selection scan&lt;br /&gt;
*Kinship matrix&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The older version, based on the Numba library (only working with Python 2.7) is still available in version 0.973 and can be found here [https://github.com/Rosemeis/pcangsd/releases/tag/0.973].&lt;br /&gt;
&lt;br /&gt;
=Download and Installation=&lt;br /&gt;
&lt;br /&gt;
PCAngsd should work on all platforms meeting the requirements but server-side usage is highly recommended. &lt;br /&gt;
Installation has only been tested on Linux systems.&lt;br /&gt;
&lt;br /&gt;
It is assumed that OpenMP is installed [https://www.openmp.org/].&lt;br /&gt;
&lt;br /&gt;
1. Login to your server using ssh on your terminal window.&lt;br /&gt;
&lt;br /&gt;
2. Create the directory where you will install your software and enter it, such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
mkdir ~/Software&lt;br /&gt;
cd ~/Software&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
3. Download the source code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
git clone https://github.com/Rosemeis/pcangsd.git&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4. Configure, Compile and Install:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cd pcangsd/&lt;br /&gt;
python setup.py build_ext --inplace&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
5. Install dependencies:&lt;br /&gt;
&lt;br /&gt;
The required set of Python packages are easily installed using the pip command and the requirements.txt file included in the pcangsd folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;pip install --user -r requirements.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Quick start=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Download the input beagle file with genotype likelihoods&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/input.gz &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Only estimate covariance matrix using 10 threads&lt;br /&gt;
python pcangsd.py -beagle input.gz -o test1 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and individual admixture proportions&lt;br /&gt;
python pcangsd.py -beagle input.gz -admix -o test2 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and inbreeding coefficients&lt;br /&gt;
python pcangsd.py -beagle input.gz -inbreed 2 -o test3 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and perform selection scan&lt;br /&gt;
python pcangsd.py -beagle input.gz -selection -o test4 -threads 10&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Detailed Examples and Tutorial==&lt;br /&gt;
&lt;br /&gt;
Please refer to the tutorial's page [http://www.popgen.dk/software/index.php/PCAngsdTutorial]&lt;br /&gt;
&lt;br /&gt;
=Input=&lt;br /&gt;
The only input PCAngsd needs and accepts are genotype likelihoods in  [http://faculty.washington.edu/browning/beagle/beagle.html Beagle] format. New functionality for using PLINK files has been added (version 0.9). Genotypes are automatically converted into a genotype likelihood matrix where the user can incorporate an error model.&lt;br /&gt;
&lt;br /&gt;
[http://popgen.dk/angsd ANGSD] can be easily be used to compute genotype likelihoods and output them in the required Beagle format.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
./angsd -GL 2 -out data -nThreads 10 -doGlf 2 -doMajorMinor 1 -doMaf 2 -SNP_pval 1e-6 -bam bam.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See [http://popgen.dk/angsd ANGSD] for more information on how to compute the genotype likelihoods and call SNPs.&lt;br /&gt;
&lt;br /&gt;
=Output=&lt;br /&gt;
&lt;br /&gt;
Since version 0.98, PCAngsd's output is only in binary Numpy format (.npy) except for the covariance matrix. &lt;br /&gt;
&lt;br /&gt;
In order to read files in python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
C = np.genfromtxt(&amp;quot;output.cov&amp;quot;) # Reads in estimated covariance matrix&lt;br /&gt;
S = np.load(&amp;quot;output.selection.npy&amp;quot;) # Reads results from selection scan&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
R can also read Numpy matrices using the &amp;quot;RcppCNPy&amp;quot; library:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
library(RcppCNPy)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;output.cov&amp;quot;)) # Reads in estimated covariance matrix&lt;br /&gt;
S &amp;lt;- npyLoad(&amp;quot;output.selection.npy&amp;quot;) # Reads results from selection scan&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Using PCAngsd=&lt;br /&gt;
&lt;br /&gt;
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.´&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# See all options in PCAngsd&lt;br /&gt;
python pcangsd.py -h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Estimation of individual allele frequencies==&lt;br /&gt;
; -beagle [Beagle filename]&lt;br /&gt;
Input file of genotype likelihoods in Beagle format (.beagle.gz).&lt;br /&gt;
; -plink [Prefix for binary PLINK files]&lt;br /&gt;
Path to PLINK files using their prefix (.bed, .bim, .fam).&lt;br /&gt;
; -plink_error [float]&lt;br /&gt;
Incorporate error model for PLINK genotypes.&lt;br /&gt;
; -minMaf [float]&lt;br /&gt;
Minimum minor allele frequency threshold. (Default: 0.05)&lt;br /&gt;
; -iter [int]&lt;br /&gt;
Maximum number of iterations for estimation of individual allele frequencies (Default: 100).&lt;br /&gt;
; -tole [float]&lt;br /&gt;
Tolerance value for update in estimation of individual allele frequencies (Default: 1e-5).&lt;br /&gt;
; -maf_iter [int]&lt;br /&gt;
Maximum number of EM iterations for computing the population allele frequencies (Default: 100).&lt;br /&gt;
; -maf_tole [float]&lt;br /&gt;
Tolerance value in EM algorithm for population allele frequencies estimation (Default: 1e-4).&lt;br /&gt;
; -e [int]&lt;br /&gt;
Manually select the number of eigenvalues to use in the modelling of individual allele frequencies (Default: Automatically tested using MAP test).&lt;br /&gt;
; -o [prefix]&lt;br /&gt;
Set the prefix for all output files created by PCAngsd (Default: &amp;quot;pcangsd&amp;quot;).&lt;br /&gt;
; -indf_save&lt;br /&gt;
Choose to save estimated individual allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
; -dosage_save&lt;br /&gt;
Choose to save estimated genotype dosages (Binary). Numpy format (.npy).&lt;br /&gt;
; -sites_save&lt;br /&gt;
Choose to save the marker IDs after performing filtering using population allele frequencies. Useful for especially selection scans and per-site inbreeding coefficients.&lt;br /&gt;
; -post_save&lt;br /&gt;
Choose to save the posterior genotype probabilities. Beagle format (.beagle).&lt;br /&gt;
; -threads [int]&lt;br /&gt;
Specify the number of thread(s) to use (Default: 1).&lt;br /&gt;
&lt;br /&gt;
==Call genotypes==&lt;br /&gt;
Genotypes can be called from posterior genotype probabilities incorporating the individual allele frequencies as prior information.&lt;br /&gt;
&lt;br /&gt;
; -geno [float]&lt;br /&gt;
Call genotypes with defined threshold.&lt;br /&gt;
; -genoInbreed [float]&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Admixture==&lt;br /&gt;
Individual admixture proportions and population-specific allele frequencies can be estimated based on assuming K ancestral populations using an accelerated mini-batch NMF method.&lt;br /&gt;
&lt;br /&gt;
; -admix&lt;br /&gt;
Toggles admixture estimations. Individual ancestry proportions are saved (Binary). Numpy format (.npy).&lt;br /&gt;
; -admix_alpha [float-list]&lt;br /&gt;
Specify alpha (sparseness regularization parameter). Can be specified as a sequence to try several alpha's in a single run (Default: 0).&lt;br /&gt;
; -admix_auto [float]&lt;br /&gt;
Enable automatic search for optimal alpha using likelihood measure, by giving soft upper search bound of alpha.&lt;br /&gt;
; -admix_seed [int-list]&lt;br /&gt;
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.&lt;br /&gt;
; -admix_K [int]&lt;br /&gt;
Not recommended. Manually specify the number of ancestral populations to use in admixture estimations (overrides number chosen from '''-e'''). Structure explained by individual allele frequencies may therefore not reflect the manually chosen K. It is recommended to adjust '''-e''' instead of '''-admix_K'''.&lt;br /&gt;
; -admix_iter [int]&lt;br /&gt;
Maximum number of iterations for admixture estimations using NMF. (Default: 200)&lt;br /&gt;
; -admix_tole [float]&lt;br /&gt;
Tolerance value for update in admixture estimations using NMF. (Default: 1e-5)&lt;br /&gt;
; -admix_batch [int]&lt;br /&gt;
Specify the number of batches to use in NMF method. (Default: 10)&lt;br /&gt;
; -admix_save&lt;br /&gt;
Choose to save the population-specific allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
&lt;br /&gt;
==Inbreeding==&lt;br /&gt;
Per-individual inbreeding coefficients incorporating population structure can be computed using three different methods. However, -inbreed 1 is recommended for low depth cases.&lt;br /&gt;
&lt;br /&gt;
; -inbreed 1&lt;br /&gt;
Simple estimator computed by an EM algorithm. Allows for F-values between -1 and 1. Based on [http://genome.cshlp.org/content/23/11/1852.full ngsF].&lt;br /&gt;
&lt;br /&gt;
; -inbreed 2&lt;br /&gt;
A maximum likelihood estimator also 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].&lt;br /&gt;
&lt;br /&gt;
; -inbreed 3&lt;br /&gt;
Estimator using an estimated kinship matrix. Allows for F-values between -1 and 1. Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate].  &lt;br /&gt;
&lt;br /&gt;
; -inbreed_iter [int]&lt;br /&gt;
Maximum number of iterations for the EM algorithm methods. (Default: 200)&lt;br /&gt;
&lt;br /&gt;
; -inbreed_tole [float]&lt;br /&gt;
Tolerance value for the EM algorithms for inbreeding coefficients estimation. (Default: 1e-4)&lt;br /&gt;
Per-site inbreeding coefficients incorporating population structure alongside likehood ratio tests for HWE can be computed as follows:&lt;br /&gt;
&lt;br /&gt;
; -inbreedSites&lt;br /&gt;
Use likelihood ratio tests (.lrt.sites.gz) generated from '''-inbreedSites''' to filter out variable sites using a given threshold for HWE test p-value:&lt;br /&gt;
&lt;br /&gt;
; -hwe [LRT filename]&lt;br /&gt;
&lt;br /&gt;
; -hwe_tole [float]&lt;br /&gt;
Tolerance value for HWE test. (Default: 1e-6)&lt;br /&gt;
&lt;br /&gt;
==Selection==&lt;br /&gt;
A genome selection scan can be computed based on posterior expectations of the genotypes (genotype dosages):&lt;br /&gt;
&lt;br /&gt;
; -selection&lt;br /&gt;
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. 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.&lt;br /&gt;
&lt;br /&gt;
==Relatedness==&lt;br /&gt;
Estimate kinship matrix based on method Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate]:&lt;br /&gt;
&lt;br /&gt;
; -kinship&lt;br /&gt;
Automatically estimated if '''-inbreed 3''' has been selected.&lt;br /&gt;
&lt;br /&gt;
Remove related individuals based on kinhsip matrix of previous run:&lt;br /&gt;
; -relate [Kinship filename]&lt;br /&gt;
; -relate_tole [float]&lt;br /&gt;
Threshold for kinship coefficients for removing individuals (Default: 0.0625).&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Our methods for inferring population structure have been published in GENETICS:&lt;br /&gt;
&lt;br /&gt;
[http://www.genetics.org/content/early/2018/08/21/genetics.118.301336 Inferring Population Structure and Admixture Proportions in Low Depth NGS Data]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Our method for testing for HWE in structured populations has been published in Molecular Ecology Resources:&lt;br /&gt;
&lt;br /&gt;
[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]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1442</id>
		<title>PCAngsd</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsd&amp;diff=1442"/>
		<updated>2020-05-29T15:49:16Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
PCAngsd is a program that estimates the covariance matrix for low depth next-generation sequencing (NGS) data in structured/heterogeneous populations using principal component analysis (PCA) to perform multiple population genetic analyses using an iterative procedure based on genotype likelihoods. &lt;br /&gt;
&lt;br /&gt;
Since version 0.98, PCAngsd was re-written to be based on Cython for computational bottlenecks and parallelization and is now compatible with any newer Python version.&lt;br /&gt;
&lt;br /&gt;
The method was published in 2018 and can be found here: [https://www.genetics.org/content/210/2/719]&lt;br /&gt;
[[File:Pcangsd_admix.gif|frame]]&lt;br /&gt;
&lt;br /&gt;
[[File:Pcangsd_pca.png|thumb|400px|Simulated low depth NGS data of 3 populations]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Overview=&lt;br /&gt;
&lt;br /&gt;
Based on population structure inference, PCAngsd is able to detect the number of significant principal components which is then used to estimate individual allele frequencies using genotype dosages in a SVD model. 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.&lt;br /&gt;
The estimated individual allele frequencies and principal components can be used as prior knowledge in other probabilistic methods based on a same Bayesian principle. PCAngsd can perform the following analyses:&lt;br /&gt;
*Covariance matrix&lt;br /&gt;
*Genotype calling&lt;br /&gt;
*Admixture&lt;br /&gt;
*Inbreeding coefficients (both per-individual and per-site)&lt;br /&gt;
*HWE test&lt;br /&gt;
*Genome selection scan&lt;br /&gt;
*Kinship matrix&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The older version, based on the Numba library (only working with Python 2.7) is still available in version 0.973 and can be found here [https://github.com/Rosemeis/pcangsd/releases/tag/0.973].&lt;br /&gt;
&lt;br /&gt;
=Download and Installation=&lt;br /&gt;
&lt;br /&gt;
PCAngsd should work on all platforms meeting the requirements but server-side usage is highly recommended. &lt;br /&gt;
Installation has only been tested on Linux systems.&lt;br /&gt;
&lt;br /&gt;
It is assumed that OpenMP is installed [https://www.openmp.org/].&lt;br /&gt;
&lt;br /&gt;
1. Login to your server using ssh on your terminal window.&lt;br /&gt;
&lt;br /&gt;
2. Create the directory where you will install your software and enter it, such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
mkdir ~/Software&lt;br /&gt;
cd ~/Software&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
3. Download the source code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
git clone https://github.com/Rosemeis/pcangsd.git&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4. Configure, Compile and Install:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cd pcangsd/&lt;br /&gt;
python setup.py build_ext --inplace&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
5. Install dependencies:&lt;br /&gt;
&lt;br /&gt;
The required set of Python packages are easily installed using the pip command and the requirements.txt file included in the pcangsd folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;pip install --user -r requirements.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Quick start=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Download the input beagle file with genotype likelihoods&lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/input.gz &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Only estimate covariance matrix using 10 threads&lt;br /&gt;
python pcangsd.py -beagle input.gz -o test1 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and individual admixture proportions&lt;br /&gt;
python pcangsd.py -beagle input.gz -admix -o test2 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and inbreeding coefficients&lt;br /&gt;
python pcangsd.py -beagle input.gz -inbreed 2 -o test3 -threads 10&lt;br /&gt;
&lt;br /&gt;
# Estimate covariance matrix and perform selection scan&lt;br /&gt;
python pcangsd.py -beagle input.gz -selection -o test4 -threads 10&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Detailed Examples and Tutorial==&lt;br /&gt;
&lt;br /&gt;
Please refer to the tutorial's page [http://www.popgen.dk/software/index.php/PCAngsdTutorial]&lt;br /&gt;
&lt;br /&gt;
=Input=&lt;br /&gt;
The only input PCAngsd needs and accepts are genotype likelihoods in  [http://faculty.washington.edu/browning/beagle/beagle.html Beagle] format. New functionality for using PLINK files has been added (version 0.9). Genotypes are automatically converted into a genotype likelihood matrix where the user can incorporate an error model.&lt;br /&gt;
&lt;br /&gt;
[http://popgen.dk/angsd ANGSD] can be easily be used to compute genotype likelihoods and output them in the required Beagle format.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
./angsd -GL 2 -out data -nThreads 10 -doGlf 2 -doMajorMinor 1 -doMaf 2 -SNP_pval 1e-6 -bam bam.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See [http://popgen.dk/angsd ANGSD] for more information on how to compute the genotype likelihoods and call SNPs.&lt;br /&gt;
&lt;br /&gt;
=Output=&lt;br /&gt;
&lt;br /&gt;
Since version 0.98, PCAngsd's output is only in binary Numpy format (.npy) except for the covariance matrix. &lt;br /&gt;
&lt;br /&gt;
In order to read files in python:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
C = np.genfromtxt(&amp;quot;output.cov&amp;quot;) # Reads in estimated covariance matrix&lt;br /&gt;
S = np.load(&amp;quot;output.selection.npy&amp;quot;) # Reads results from selection scan&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
R can also read Numpy matrices using the &amp;quot;RcppCNPy&amp;quot; library:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
library(RcppCNPy)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;output.cov&amp;quot;)) # Reads in estimated covariance matrix&lt;br /&gt;
S &amp;lt;- npyLoad(&amp;quot;output.selection.npy&amp;quot;) # Reads results from selection scan&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Using PCAngsd=&lt;br /&gt;
&lt;br /&gt;
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.´&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# See all options in PCAngsd&lt;br /&gt;
python pcangsd.py -h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Estimation of individual allele frequencies==&lt;br /&gt;
; -beagle [Beagle filename]&lt;br /&gt;
Input file of genotype likelihoods in Beagle format (.beagle.gz).&lt;br /&gt;
; -plink [Prefix for binary PLINK files]&lt;br /&gt;
Path to PLINK files using their prefix (.bed, .bim, .fam).&lt;br /&gt;
; -plink_error [float]&lt;br /&gt;
Incorporate error model for PLINK genotypes.&lt;br /&gt;
; -minMaf [float]&lt;br /&gt;
Minimum minor allele frequency threshold. (Default: 0.05)&lt;br /&gt;
; -iter [int]&lt;br /&gt;
Maximum number of iterations for estimation of individual allele frequencies (Default: 100).&lt;br /&gt;
; -tole [float]&lt;br /&gt;
Tolerance value for update in estimation of individual allele frequencies (Default: 1e-5).&lt;br /&gt;
; -maf_iter [int]&lt;br /&gt;
Maximum number of EM iterations for computing the population allele frequencies (Default: 100).&lt;br /&gt;
; -maf_tole [float]&lt;br /&gt;
Tolerance value in EM algorithm for population allele frequencies estimation (Default: 1e-4).&lt;br /&gt;
; -e [int]&lt;br /&gt;
Manually select the number of eigenvalues to use in the modelling of individual allele frequencies (Default: Automatically tested using MAP test).&lt;br /&gt;
; -o [prefix]&lt;br /&gt;
Set the prefix for all output files created by PCAngsd (Default: &amp;quot;pcangsd&amp;quot;).&lt;br /&gt;
; -indf_save&lt;br /&gt;
Choose to save estimated individual allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
; -dosage_save&lt;br /&gt;
Choose to save estimated genotype dosages (Binary). Numpy format (.npy).&lt;br /&gt;
; -sites_save&lt;br /&gt;
Choose to save the marker IDs after performing filtering using population allele frequencies. Useful for especially selection scans and per-site inbreeding coefficients.&lt;br /&gt;
; -post_save&lt;br /&gt;
Choose to save the posterior genotype probabilities. Beagle format (.beagle).&lt;br /&gt;
; -threads [int]&lt;br /&gt;
Specify the number of thread(s) to use (Default: 1).&lt;br /&gt;
&lt;br /&gt;
==Call genotypes==&lt;br /&gt;
Genotypes can be called from posterior genotype probabilities incorporating the individual allele frequencies as prior information.&lt;br /&gt;
&lt;br /&gt;
; -geno [float]&lt;br /&gt;
Call genotypes with defined threshold.&lt;br /&gt;
; -genoInbreed [float]&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Admixture==&lt;br /&gt;
Individual admixture proportions and population-specific allele frequencies can be estimated based on assuming K ancestral populations using an accelerated mini-batch NMF method.&lt;br /&gt;
&lt;br /&gt;
; -admix&lt;br /&gt;
Toggles admixture estimations. Individual ancestry proportions are saved (Binary). Numpy format (.npy).&lt;br /&gt;
; -admix_alpha [float-list]&lt;br /&gt;
Specify alpha (sparseness regularization parameter). Can be specified as a sequence to try several alpha's in a single run (Default: 0).&lt;br /&gt;
; -admix_auto [float]&lt;br /&gt;
Enable automatic search for optimal alpha using likelihood measure, by giving soft upper search bound of alpha.&lt;br /&gt;
; -admix_seed [int-list]&lt;br /&gt;
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.&lt;br /&gt;
; -admix_K [int]&lt;br /&gt;
Not recommended. Manually specify the number of ancestral populations to use in admixture estimations (overrides number chosen from '''-e'''). Structure explained by individual allele frequencies may therefore not reflect the manually chosen K. It is recommended to adjust '''-e''' instead of '''-admix_K'''.&lt;br /&gt;
; -admix_iter [int]&lt;br /&gt;
Maximum number of iterations for admixture estimations using NMF. (Default: 200)&lt;br /&gt;
; -admix_tole [float]&lt;br /&gt;
Tolerance value for update in admixture estimations using NMF. (Default: 1e-5)&lt;br /&gt;
; -admix_batch [int]&lt;br /&gt;
Specify the number of batches to use in NMF method. (Default: 10)&lt;br /&gt;
; -admix_save&lt;br /&gt;
Choose to save the population-specific allele frequencies (Binary). Numpy format (.npy).&lt;br /&gt;
&lt;br /&gt;
==Inbreeding==&lt;br /&gt;
Per-individual inbreeding coefficients incorporating population structure can be computed using three different methods. However, -inbreed 1 is recommended for low depth cases.&lt;br /&gt;
&lt;br /&gt;
; -inbreed 1&lt;br /&gt;
Simple estimator computed by an EM algorithm. Allows for F-values between -1 and 1. Based on [http://genome.cshlp.org/content/23/11/1852.full ngsF].&lt;br /&gt;
&lt;br /&gt;
; -inbreed 2&lt;br /&gt;
A maximum likelihood estimator also 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].&lt;br /&gt;
&lt;br /&gt;
; -inbreed 3&lt;br /&gt;
Estimator using an estimated kinship matrix. Allows for F-values between -1 and 1. Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate].  &lt;br /&gt;
&lt;br /&gt;
; -inbreed_iter [int]&lt;br /&gt;
Maximum number of iterations for the EM algorithm methods. (Default: 200)&lt;br /&gt;
&lt;br /&gt;
; -inbreed_tole [float]&lt;br /&gt;
Tolerance value for the EM algorithms for inbreeding coefficients estimation. (Default: 1e-4)&lt;br /&gt;
Per-site inbreeding coefficients incorporating population structure alongside likehood ratio tests for HWE can be computed as follows:&lt;br /&gt;
&lt;br /&gt;
; -inbreedSites&lt;br /&gt;
Use likelihood ratio tests (.lrt.sites.gz) generated from '''-inbreedSites''' to filter out variable sites using a given threshold for HWE test p-value:&lt;br /&gt;
&lt;br /&gt;
; -hwe [LRT filename]&lt;br /&gt;
&lt;br /&gt;
; -hwe_tole [float]&lt;br /&gt;
Tolerance value for HWE test. (Default: 1e-6)&lt;br /&gt;
&lt;br /&gt;
==Selection==&lt;br /&gt;
A genome selection scan can be computed based on posterior expectations of the genotypes (genotype dosages):&lt;br /&gt;
&lt;br /&gt;
; -selection&lt;br /&gt;
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. 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.&lt;br /&gt;
&lt;br /&gt;
==Relatedness==&lt;br /&gt;
Estimate kinship matrix based on method Based on [http://www.cell.com/ajhg/abstract/S0002-9297(15)00493-0 PC-Relate]:&lt;br /&gt;
&lt;br /&gt;
; -kinship&lt;br /&gt;
Automatically estimated if '''-inbreed 3''' has been selected.&lt;br /&gt;
&lt;br /&gt;
Remove related individuals based on kinhsip matrix of previous run:&lt;br /&gt;
; -relate [Kinship filename]&lt;br /&gt;
; -relate_tole [float]&lt;br /&gt;
Threshold for kinship coefficients for removing individuals (Default: 0.0625).&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Our methods for inferring population structure have been published in GENETICS:&lt;br /&gt;
&lt;br /&gt;
[http://www.genetics.org/content/early/2018/08/21/genetics.118.301336 Inferring Population Structure and Admixture Proportions in Low Depth NGS Data]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Our method for testing for HWE in structured populations has been published in Molecular Ecology Resources:&lt;br /&gt;
&lt;br /&gt;
[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]&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=EMU&amp;diff=1439</id>
		<title>EMU</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=EMU&amp;diff=1439"/>
		<updated>2020-04-27T09:48:57Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Run example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about EMU (EM-PCA for Ultra-low Coverage Sequencing Data). EMU infers population structure in the presence of missingness and works for both haploid, psuedo-haploid and diploid genotype datasets. Due to EMUs iterative nature, it is able to infer population structure even for datasets of ultra-low coverage sequencing data with very high missingness rates in addition to being able to handle non-random missingness patterns where other existing methods fail. We use a procedure of low-rank approximations based on randomized PCA to iteratively update population structure in a very efficient manner. &lt;br /&gt;
&lt;br /&gt;
EMU is written in Python and Cython and is freely available on Github. We have also implemented a very memory-efficient variant of EMU (EMU-mem) for large-scale datasets that uses the 2-bit data structures of PLINK binary file formats.&lt;br /&gt;
&lt;br /&gt;
=Download=&lt;br /&gt;
&lt;br /&gt;
The program can be downloaded from Github:&lt;br /&gt;
https://github.com/Rosemeis/emu&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
git clone https://github.com/Rosemeis/emu.git&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See github for more information regarding installation.&lt;br /&gt;
Server-side usage is recommended.&lt;br /&gt;
&lt;br /&gt;
==Quick start==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# See all options in EMU&lt;br /&gt;
python emu.py -h&lt;br /&gt;
&lt;br /&gt;
# Infer population structure using 2 eigenvectors and 64 threads from binary PLINK files (.bed, .bim, .fam)&lt;br /&gt;
python emu.py -plink plink_prefix -e 2 -t 64 -accel -o plink_emu&lt;br /&gt;
&lt;br /&gt;
# Or directly from NumPy array input&lt;br /&gt;
python emu.py -npy matrix.npy -e 2 -t 64 -accel -o npy_emu&lt;br /&gt;
&lt;br /&gt;
# Use EMU-mem variant&lt;br /&gt;
python emu_mem.py -plink plink_prefix -e 2 -t 64 -accel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Input=&lt;br /&gt;
EMU use either binary PLINK files as input (RECOMMENDED!) or saved NumPy genotype matrices in 8-bit format (numpy.int8). EMU-mem will only accept PLINK files as input due to the 2-bit data structures. If NumPy format should be preferred, you can use the script provided on Github for conversion (convertMat.py).&lt;br /&gt;
&lt;br /&gt;
=Using EMU=&lt;br /&gt;
We highly recommend to use EM acceleration at all times (-accel). You can save factor matrices (-indf_save) from a run to use as starting point in a new run (-w, -s, -u). Due to convenience we have also implemented the PC-based selection scan of Galinsky et al. 2016 (-selection). MAF filtering is possible but it is recommended (and ASSUMED!) to do beforehand. &lt;br /&gt;
&lt;br /&gt;
==Options==&lt;br /&gt;
; -plink [Prefix for binary PLINK files]&lt;br /&gt;
Path to PLINK files using their prefix (.bed, .bim, .fam).&lt;br /&gt;
; -npy [Numpy.int8 matrix format]&lt;br /&gt;
Path to NumPy matrix (.npy).&lt;br /&gt;
; -e [int]&lt;br /&gt;
Number of eigenvectors to use in optimization.&lt;br /&gt;
; -k [int]&lt;br /&gt;
Number of eigenvectors to output if user wants different than -e.&lt;br /&gt;
; -m [int]&lt;br /&gt;
Maximum number of iterations (Default: 100).&lt;br /&gt;
; -m_tole [float]&lt;br /&gt;
Tolerance for covergence of iterative procedure (Default: 5e-7).&lt;br /&gt;
; -t [int]&lt;br /&gt;
Number of threads to use (Default: 1).&lt;br /&gt;
; -maf [float]&lt;br /&gt;
Minimum minor allele frequency threshold (Default: 0.00).&lt;br /&gt;
; -selection&lt;br /&gt;
Perform genome-wide PC-based selection scan (Galinsky et al. 2016).&lt;br /&gt;
; -maf_save&lt;br /&gt;
Save the estimated minor allele frequencies.&lt;br /&gt;
; -bool_save&lt;br /&gt;
Save boolean vector of filtered sites based on MAF.&lt;br /&gt;
; -indf_save&lt;br /&gt;
Save estimated factor matrices (W, S, U).&lt;br /&gt;
; -index [file]&lt;br /&gt;
Provide index of individuals for guiding initialization (np.int8 format).&lt;br /&gt;
; -svd [string]&lt;br /&gt;
Select which low-rank SVD method to use, halko/arpack (Default: 'halko').&lt;br /&gt;
; -svd_power [int]&lt;br /&gt;
Number of power iterations to use in low-rank SVD (Default: 3).&lt;br /&gt;
; -w [file]&lt;br /&gt;
Provide starting point, left singular matrix (.w.npy).&lt;br /&gt;
; -s [file]&lt;br /&gt;
Provide starting point, singular values (.s.npy).&lt;br /&gt;
; -u [file]&lt;br /&gt;
Provide starting point, right singular matrix (.u.npy).&lt;br /&gt;
; -accel&lt;br /&gt;
Use EM acceleration (Highly recommended!).&lt;br /&gt;
; -o [string]&lt;br /&gt;
Prefix for all output files (Default: 'emu').&lt;br /&gt;
; -cost&lt;br /&gt;
Output Frobenius each iteration (DEBUG).&lt;br /&gt;
; -cost_step&lt;br /&gt;
Use acceleration based on Frobenius (DEBUG).&lt;br /&gt;
&lt;br /&gt;
==Options in EMU-mem==&lt;br /&gt;
-maf, -bool_save, -svd, -cost, -cost_step functions are not available for EMU-mem. MAF filtering has to be performed beforehand, which is easily done in PLINK (--maf 0.05).&lt;br /&gt;
&lt;br /&gt;
=Run example=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Download data&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget popgen.dk/software/download/fastNGSadmix/data.tar.gz&lt;br /&gt;
tar -xzf data.tar.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
run&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
python emu.py -plink data/humanOrigins_7worldPops -e 4 -t 4 -accel -o plink_emu&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
plot&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 library(RcppCNPy)&lt;br /&gt;
vec &amp;lt;- npyLoad(&amp;quot;plink_emu.eigenvecs.npy&amp;quot;) # Reads in eigen vectors&lt;br /&gt;
fam &amp;lt;- read.table(&amp;quot;data/humanOrigins_7worldPops.fam&amp;quot;,head=F)&lt;br /&gt;
plot(vec[,1:2],col=fam[,1],xlab=&amp;quot;PC1&amp;quot;,ylab=&amp;quot;PC2&amp;quot;)&lt;br /&gt;
legend(&amp;quot;center&amp;quot;,fill=1:7,levels(fam[,1]))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
TBA&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=EMU&amp;diff=1438</id>
		<title>EMU</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=EMU&amp;diff=1438"/>
		<updated>2020-04-27T09:30:36Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about EMU (EM-PCA for Ultra-low Coverage Sequencing Data). EMU infers population structure in the presence of missingness and works for both haploid, psuedo-haploid and diploid genotype datasets. Due to EMUs iterative nature, it is able to infer population structure even for datasets of ultra-low coverage sequencing data with very high missingness rates in addition to being able to handle non-random missingness patterns where other existing methods fail. We use a procedure of low-rank approximations based on randomized PCA to iteratively update population structure in a very efficient manner. &lt;br /&gt;
&lt;br /&gt;
EMU is written in Python and Cython and is freely available on Github. We have also implemented a very memory-efficient variant of EMU (EMU-mem) for large-scale datasets that uses the 2-bit data structures of PLINK binary file formats.&lt;br /&gt;
&lt;br /&gt;
=Download=&lt;br /&gt;
&lt;br /&gt;
The program can be downloaded from Github:&lt;br /&gt;
https://github.com/Rosemeis/emu&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
git clone https://github.com/Rosemeis/emu.git&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See github for more information regarding installation.&lt;br /&gt;
Server-side usage is recommended.&lt;br /&gt;
&lt;br /&gt;
==Quick start==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# See all options in EMU&lt;br /&gt;
python emu.py -h&lt;br /&gt;
&lt;br /&gt;
# Infer population structure using 2 eigenvectors and 64 threads from binary PLINK files (.bed, .bim, .fam)&lt;br /&gt;
python emu.py -plink plink_prefix -e 2 -t 64 -accel -o plink_emu&lt;br /&gt;
&lt;br /&gt;
# Or directly from NumPy array input&lt;br /&gt;
python emu.py -npy matrix.npy -e 2 -t 64 -accel -o npy_emu&lt;br /&gt;
&lt;br /&gt;
# Use EMU-mem variant&lt;br /&gt;
python emu_mem.py -plink plink_prefix -e 2 -t 64 -accel&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Input=&lt;br /&gt;
EMU use either binary PLINK files as input (RECOMMENDED!) or saved NumPy genotype matrices in 8-bit format (numpy.int8). EMU-mem will only accept PLINK files as input due to the 2-bit data structures. If NumPy format should be preferred, you can use the script provided on Github for conversion (convertMat.py).&lt;br /&gt;
&lt;br /&gt;
=Using EMU=&lt;br /&gt;
We highly recommend to use EM acceleration at all times (-accel). You can save factor matrices (-indf_save) from a run to use as starting point in a new run (-w, -s, -u). Due to convenience we have also implemented the PC-based selection scan of Galinsky et al. 2016 (-selection). MAF filtering is possible but it is recommended (and ASSUMED!) to do beforehand. &lt;br /&gt;
&lt;br /&gt;
==Options==&lt;br /&gt;
; -plink [Prefix for binary PLINK files]&lt;br /&gt;
Path to PLINK files using their prefix (.bed, .bim, .fam).&lt;br /&gt;
; -npy [Numpy.int8 matrix format]&lt;br /&gt;
Path to NumPy matrix (.npy).&lt;br /&gt;
; -e [int]&lt;br /&gt;
Number of eigenvectors to use in optimization.&lt;br /&gt;
; -k [int]&lt;br /&gt;
Number of eigenvectors to output if user wants different than -e.&lt;br /&gt;
; -m [int]&lt;br /&gt;
Maximum number of iterations (Default: 100).&lt;br /&gt;
; -m_tole [float]&lt;br /&gt;
Tolerance for covergence of iterative procedure (Default: 5e-7).&lt;br /&gt;
; -t [int]&lt;br /&gt;
Number of threads to use (Default: 1).&lt;br /&gt;
; -maf [float]&lt;br /&gt;
Minimum minor allele frequency threshold (Default: 0.00).&lt;br /&gt;
; -selection&lt;br /&gt;
Perform genome-wide PC-based selection scan (Galinsky et al. 2016).&lt;br /&gt;
; -maf_save&lt;br /&gt;
Save the estimated minor allele frequencies.&lt;br /&gt;
; -bool_save&lt;br /&gt;
Save boolean vector of filtered sites based on MAF.&lt;br /&gt;
; -indf_save&lt;br /&gt;
Save estimated factor matrices (W, S, U).&lt;br /&gt;
; -index [file]&lt;br /&gt;
Provide index of individuals for guiding initialization (np.int8 format).&lt;br /&gt;
; -svd [string]&lt;br /&gt;
Select which low-rank SVD method to use, halko/arpack (Default: 'halko').&lt;br /&gt;
; -svd_power [int]&lt;br /&gt;
Number of power iterations to use in low-rank SVD (Default: 3).&lt;br /&gt;
; -w [file]&lt;br /&gt;
Provide starting point, left singular matrix (.w.npy).&lt;br /&gt;
; -s [file]&lt;br /&gt;
Provide starting point, singular values (.s.npy).&lt;br /&gt;
; -u [file]&lt;br /&gt;
Provide starting point, right singular matrix (.u.npy).&lt;br /&gt;
; -accel&lt;br /&gt;
Use EM acceleration (Highly recommended!).&lt;br /&gt;
; -o [string]&lt;br /&gt;
Prefix for all output files (Default: 'emu').&lt;br /&gt;
; -cost&lt;br /&gt;
Output Frobenius each iteration (DEBUG).&lt;br /&gt;
; -cost_step&lt;br /&gt;
Use acceleration based on Frobenius (DEBUG).&lt;br /&gt;
&lt;br /&gt;
==Options in EMU-mem==&lt;br /&gt;
-maf, -bool_save, -svd, -cost, -cost_step functions are not available for EMU-mem. MAF filtering has to be performed beforehand, which is easily done in PLINK (--maf 0.05).&lt;br /&gt;
&lt;br /&gt;
=Run example=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget popgen.dk/software/download/fastNGSadmix/data.tar.gz&lt;br /&gt;
tar -xzf data.tar.gz&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
TBA&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1429</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1429"/>
		<updated>2019-09-24T12:36:56Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Examine the results */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
The man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the IBS results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 method citation]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block. However, if your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
Yes, point estimates of R0, R1, or KING-robust kinship do not require a reference genome that is assembled into chromosomes.  If you have enough RAM, you can use all of your data at once, otherwise you will need to split your data into pieces, analyze each piece, and combine them afterwards.&lt;br /&gt;
&lt;br /&gt;
== How can the quality of the reference genome assembly affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Probably in multiple, complicated ways! &lt;br /&gt;
&lt;br /&gt;
But one to watch out for are regions of your reference genome that are attracting aligned reads originating from multiple, distinct parts of the genome. These regions can exhibit elevated heterozygosity across all individuals.  This is particularly problematic for our analyses, as shared heterozygosity between a pair of individuals (sites where both individuals are heterozygotes) are the strongest signals we have that the individuals are related. This is because shared heterozygosity shows two important things about a site: 1) the site is variable (segregating) in the population, and 2) that the pair of individuals has the maximum possible allelic identity at the site. A upward bias in shared heterozygosity will result in KING-robust kinship estimates biased toward 0.5, elevated values of R1, and lower values of R0.&lt;br /&gt;
&lt;br /&gt;
If possible these regions of the reference genome should be excluded from the analysis. With access to multiple individuals, it may be possible to identify them by estimating heterozygosity across all individuals, or with a HWE test (see [https://onlinelibrary.wiley.com/doi/full/10.1111/1755-0998.13019 here] for a method of testing HWE on low depth data that is robust to population structure).  Without access to multiple individuals, it will be difficult to resolve these issues except through a better reference genome.&lt;br /&gt;
&lt;br /&gt;
One thing we found with the human data in the paper was that applying a &amp;quot;mappability&amp;quot; filter improved our estimates of R0, R1, or KING-robust kinship.  This is essentially a scan of the reference genome that reports where in the genome it is possible to uniquely align reads, and needs to be calibrated to your read lengths.  We used [https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0030377 GEM] &lt;br /&gt;
But there are also [https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6237805 new programs] that claim to do the same.&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
Inbreeding within one or both individuals of a pair can affect estimates of R0, R1, or KING-robust kinship, but there isn't a simple answer that covers all possibilities. &lt;br /&gt;
&lt;br /&gt;
If only one of the individuals is inbred, the pair of individuals may appear less related than otherwise, as the pair of individuals may have an elevated number of alternate homozygous genotypes and a reduced number of shared heterozygous genotypes.  &lt;br /&gt;
&lt;br /&gt;
When considering inbreeding, it can be useful to compare the heterozygosities of a pair of individuals.  This is possible from the output of realSFS or IBS, and is facilitated by the parsing scripts included above.&lt;br /&gt;
&lt;br /&gt;
== What data type is required for these analysis? ==&lt;br /&gt;
The best starting point are aligned sequencing reads (bam files).  The above example shows how to process these into all of the intermediate and final outputs. &lt;br /&gt;
&lt;br /&gt;
The program [http://academic.oup.com/gigascience/article-abstract/8/5/giz034/5481763 ngsRelateV2] can also be used to estimate R0, R1, or KING-robust kinship.  It can take multiple formats including BAM, VCF and GLF. Please notice that ngsRelateV2 has its own implementation of the something very close to the SFS-based method described in the paper, and so it assumes that you know one of the alleles that is present at each site.&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1428</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1428"/>
		<updated>2019-09-24T12:36:44Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* run IBS, this will analyse each pair of individuals */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
The man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 method citation]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block. However, if your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
Yes, point estimates of R0, R1, or KING-robust kinship do not require a reference genome that is assembled into chromosomes.  If you have enough RAM, you can use all of your data at once, otherwise you will need to split your data into pieces, analyze each piece, and combine them afterwards.&lt;br /&gt;
&lt;br /&gt;
== How can the quality of the reference genome assembly affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Probably in multiple, complicated ways! &lt;br /&gt;
&lt;br /&gt;
But one to watch out for are regions of your reference genome that are attracting aligned reads originating from multiple, distinct parts of the genome. These regions can exhibit elevated heterozygosity across all individuals.  This is particularly problematic for our analyses, as shared heterozygosity between a pair of individuals (sites where both individuals are heterozygotes) are the strongest signals we have that the individuals are related. This is because shared heterozygosity shows two important things about a site: 1) the site is variable (segregating) in the population, and 2) that the pair of individuals has the maximum possible allelic identity at the site. A upward bias in shared heterozygosity will result in KING-robust kinship estimates biased toward 0.5, elevated values of R1, and lower values of R0.&lt;br /&gt;
&lt;br /&gt;
If possible these regions of the reference genome should be excluded from the analysis. With access to multiple individuals, it may be possible to identify them by estimating heterozygosity across all individuals, or with a HWE test (see [https://onlinelibrary.wiley.com/doi/full/10.1111/1755-0998.13019 here] for a method of testing HWE on low depth data that is robust to population structure).  Without access to multiple individuals, it will be difficult to resolve these issues except through a better reference genome.&lt;br /&gt;
&lt;br /&gt;
One thing we found with the human data in the paper was that applying a &amp;quot;mappability&amp;quot; filter improved our estimates of R0, R1, or KING-robust kinship.  This is essentially a scan of the reference genome that reports where in the genome it is possible to uniquely align reads, and needs to be calibrated to your read lengths.  We used [https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0030377 GEM] &lt;br /&gt;
But there are also [https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6237805 new programs] that claim to do the same.&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
Inbreeding within one or both individuals of a pair can affect estimates of R0, R1, or KING-robust kinship, but there isn't a simple answer that covers all possibilities. &lt;br /&gt;
&lt;br /&gt;
If only one of the individuals is inbred, the pair of individuals may appear less related than otherwise, as the pair of individuals may have an elevated number of alternate homozygous genotypes and a reduced number of shared heterozygous genotypes.  &lt;br /&gt;
&lt;br /&gt;
When considering inbreeding, it can be useful to compare the heterozygosities of a pair of individuals.  This is possible from the output of realSFS or IBS, and is facilitated by the parsing scripts included above.&lt;br /&gt;
&lt;br /&gt;
== What data type is required for these analysis? ==&lt;br /&gt;
The best starting point are aligned sequencing reads (bam files).  The above example shows how to process these into all of the intermediate and final outputs. &lt;br /&gt;
&lt;br /&gt;
The program [http://academic.oup.com/gigascience/article-abstract/8/5/giz034/5481763 ngsRelateV2] can also be used to estimate R0, R1, or KING-robust kinship.  It can take multiple formats including BAM, VCF and GLF. Please notice that ngsRelateV2 has its own implementation of the something very close to the SFS-based method described in the paper, and so it assumes that you know one of the alleles that is present at each site.&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1427</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1427"/>
		<updated>2019-09-24T12:35:12Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* What data type is required for these analysis? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 method citation]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block. However, if your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
Yes, point estimates of R0, R1, or KING-robust kinship do not require a reference genome that is assembled into chromosomes.  If you have enough RAM, you can use all of your data at once, otherwise you will need to split your data into pieces, analyze each piece, and combine them afterwards.&lt;br /&gt;
&lt;br /&gt;
== How can the quality of the reference genome assembly affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Probably in multiple, complicated ways! &lt;br /&gt;
&lt;br /&gt;
But one to watch out for are regions of your reference genome that are attracting aligned reads originating from multiple, distinct parts of the genome. These regions can exhibit elevated heterozygosity across all individuals.  This is particularly problematic for our analyses, as shared heterozygosity between a pair of individuals (sites where both individuals are heterozygotes) are the strongest signals we have that the individuals are related. This is because shared heterozygosity shows two important things about a site: 1) the site is variable (segregating) in the population, and 2) that the pair of individuals has the maximum possible allelic identity at the site. A upward bias in shared heterozygosity will result in KING-robust kinship estimates biased toward 0.5, elevated values of R1, and lower values of R0.&lt;br /&gt;
&lt;br /&gt;
If possible these regions of the reference genome should be excluded from the analysis. With access to multiple individuals, it may be possible to identify them by estimating heterozygosity across all individuals, or with a HWE test (see [https://onlinelibrary.wiley.com/doi/full/10.1111/1755-0998.13019 here] for a method of testing HWE on low depth data that is robust to population structure).  Without access to multiple individuals, it will be difficult to resolve these issues except through a better reference genome.&lt;br /&gt;
&lt;br /&gt;
One thing we found with the human data in the paper was that applying a &amp;quot;mappability&amp;quot; filter improved our estimates of R0, R1, or KING-robust kinship.  This is essentially a scan of the reference genome that reports where in the genome it is possible to uniquely align reads, and needs to be calibrated to your read lengths.  We used [https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0030377 GEM] &lt;br /&gt;
But there are also [https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6237805 new programs] that claim to do the same.&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
Inbreeding within one or both individuals of a pair can affect estimates of R0, R1, or KING-robust kinship, but there isn't a simple answer that covers all possibilities. &lt;br /&gt;
&lt;br /&gt;
If only one of the individuals is inbred, the pair of individuals may appear less related than otherwise, as the pair of individuals may have an elevated number of alternate homozygous genotypes and a reduced number of shared heterozygous genotypes.  &lt;br /&gt;
&lt;br /&gt;
When considering inbreeding, it can be useful to compare the heterozygosities of a pair of individuals.  This is possible from the output of realSFS or IBS, and is facilitated by the parsing scripts included above.&lt;br /&gt;
&lt;br /&gt;
== What data type is required for these analysis? ==&lt;br /&gt;
The best starting point are aligned sequencing reads (bam files).  The above example shows how to process these into all of the intermediate and final outputs. &lt;br /&gt;
&lt;br /&gt;
The program [http://academic.oup.com/gigascience/article-abstract/8/5/giz034/5481763 ngsRelateV2] can also be used to estimate R0, R1, or KING-robust kinship.  It can take multiple formats including BAM, VCF and GLF. Please notice that ngsRelateV2 has its own implementation of the something very close to the SFS-based method described in the paper, and so it assumes that you know one of the alleles that is present at each site.&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1426</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1426"/>
		<updated>2019-09-24T12:34:21Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Frequently asked Questions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 method citation]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block. However, if your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
Yes, point estimates of R0, R1, or KING-robust kinship do not require a reference genome that is assembled into chromosomes.  If you have enough RAM, you can use all of your data at once, otherwise you will need to split your data into pieces, analyze each piece, and combine them afterwards.&lt;br /&gt;
&lt;br /&gt;
== How can the quality of the reference genome assembly affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Probably in multiple, complicated ways! &lt;br /&gt;
&lt;br /&gt;
But one to watch out for are regions of your reference genome that are attracting aligned reads originating from multiple, distinct parts of the genome. These regions can exhibit elevated heterozygosity across all individuals.  This is particularly problematic for our analyses, as shared heterozygosity between a pair of individuals (sites where both individuals are heterozygotes) are the strongest signals we have that the individuals are related. This is because shared heterozygosity shows two important things about a site: 1) the site is variable (segregating) in the population, and 2) that the pair of individuals has the maximum possible allelic identity at the site. A upward bias in shared heterozygosity will result in KING-robust kinship estimates biased toward 0.5, elevated values of R1, and lower values of R0.&lt;br /&gt;
&lt;br /&gt;
If possible these regions of the reference genome should be excluded from the analysis. With access to multiple individuals, it may be possible to identify them by estimating heterozygosity across all individuals, or with a HWE test (see [https://onlinelibrary.wiley.com/doi/full/10.1111/1755-0998.13019 here] for a method of testing HWE on low depth data that is robust to population structure).  Without access to multiple individuals, it will be difficult to resolve these issues except through a better reference genome.&lt;br /&gt;
&lt;br /&gt;
One thing we found with the human data in the paper was that applying a &amp;quot;mappability&amp;quot; filter improved our estimates of R0, R1, or KING-robust kinship.  This is essentially a scan of the reference genome that reports where in the genome it is possible to uniquely align reads, and needs to be calibrated to your read lengths.  We used [https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0030377 GEM] &lt;br /&gt;
But there are also [https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6237805 new programs] that claim to do the same.&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
Inbreeding within one or both individuals of a pair can affect estimates of R0, R1, or KING-robust kinship, but there isn't a simple answer that covers all possibilities. &lt;br /&gt;
&lt;br /&gt;
If only one of the individuals is inbred, the pair of individuals may appear less related than otherwise, as the pair of individuals may have an elevated number of alternate homozygous genotypes and a reduced number of shared heterozygous genotypes.  &lt;br /&gt;
&lt;br /&gt;
When considering inbreeding, it can be useful to compare the heterozygosities of a pair of individuals.  This is possible from the output of realSFS or IBS, and is facilitated by the parsing scripts included above.&lt;br /&gt;
&lt;br /&gt;
== What data type is required for these analysis? ==&lt;br /&gt;
The best starting point are aligned sequencing reads (bam files).  The above example shows how to process these into all of the intermediate and final outputs. &lt;br /&gt;
&lt;br /&gt;
The program [http://academic.oup.com/gigascience/article-abstract/8/5/giz034/5481763 ngsRelateV2] can also be used to estimate R0, R1, or KING-robust kinship.  It can take multiple formats including GLF, BAM, and VCF. Please notice that ngsRelateV2 has its own implementation of the something very close to the SFS-based method described in the paper, and so it assumes that you know one of the alleles that is present at each site.&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1425</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1425"/>
		<updated>2019-09-24T12:27:41Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* How will inbreeding affects the estimates produces here? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 method citation]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block. However, if your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
Yes, point estimates of R0, R1, or KING-robust kinship do not require a reference genome that is assembled into chromosomes.  If you have enough RAM, you can use all of your data at once, otherwise you will need to split your data into pieces, analyze each piece, and combine them afterwards.&lt;br /&gt;
&lt;br /&gt;
== How can the quality of the reference genome assembly affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Probably in multiple, complicated ways! &lt;br /&gt;
&lt;br /&gt;
But one to watch out for are regions of your reference genome that are attracting aligned reads originating from multiple, distinct parts of the genome. These regions can exhibit elevated heterozygosity across all individuals.  This is particularly problematic for our analyses, as shared heterozygosity between a pair of individuals (sites where both individuals are heterozygotes) are the strongest signals we have that the individuals are related. This is because shared heterozygosity shows two important things about a site: 1) the site is variable (segregating) in the population, and 2) that the pair of individuals has the maximum possible allelic identity at the site. A upward bias in shared heterozygosity will result in KING-robust kinship estimates biased toward 0.5, elevated values of R1, and lower values of R0.&lt;br /&gt;
&lt;br /&gt;
If possible these regions of the reference genome should be excluded from the analysis. With access to multiple individuals, it may be possible to identify them by estimating heterozygosity across all individuals, or with a HWE test (see [https://onlinelibrary.wiley.com/doi/full/10.1111/1755-0998.13019 here] for a method of testing HWE on low depth data that is robust to population structure).  Without access to multiple individuals, it will be difficult to resolve these issues except through a better reference genome.&lt;br /&gt;
&lt;br /&gt;
One thing we found with the human data in the paper was that applying a &amp;quot;mappability&amp;quot; filter improved our estimates of R0, R1, or KING-robust kinship.  This is essentially a scan of the reference genome that reports where in the genome it is possible to uniquely align reads, and needs to be calibrated to your read lengths.  We used [https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0030377 GEM] &lt;br /&gt;
But there are also [https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6237805 new programs] that claim to do the same.&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
Inbreeding within one or both individuals of a pair can affect estimates of R0, R1, or KING-robust kinship, but there isn't a simple answer that covers all possibilities. &lt;br /&gt;
&lt;br /&gt;
If only one of the individuals is inbred, the pair of individuals may appear less related than otherwise, as the pair of individuals may have an elevated number of alternate homozygous genotypes and a reduced number of shared heterozygous genotypes.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When considering inbreeding, it can be useful to compare the heterozygosities of a pair of individuals.  This is possible from the output of realSFS or IBS, and is facilitated by the parsing scripts included above.&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1424</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1424"/>
		<updated>2019-09-24T12:03:59Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* How can the quality of the reference genome assembly affect R0, R1, or KING-robust kinship estimates? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 method citation]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block. However, if your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
Yes, point estimates of R0, R1, or KING-robust kinship do not require a reference genome that is assembled into chromosomes.  If you have enough RAM, you can use all of your data at once, otherwise you will need to split your data into pieces, analyze each piece, and combine them afterwards.&lt;br /&gt;
&lt;br /&gt;
== How can the quality of the reference genome assembly affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Probably in multiple, complicated ways! &lt;br /&gt;
&lt;br /&gt;
But one to watch out for are regions of your reference genome that are attracting aligned reads originating from multiple, distinct parts of the genome. These regions can exhibit elevated heterozygosity across all individuals.  This is particularly problematic for our analyses, as shared heterozygosity between a pair of individuals (sites where both individuals are heterozygotes) are the strongest signals we have that the individuals are related. This is because shared heterozygosity shows two important things about a site: 1) the site is variable (segregating) in the population, and 2) that the pair of individuals has the maximum possible allelic identity at the site. A upward bias in shared heterozygosity will result in KING-robust kinship estimates biased toward 0.5, elevated values of R1, and lower values of R0.&lt;br /&gt;
&lt;br /&gt;
If possible these regions of the reference genome should be excluded from the analysis. With access to multiple individuals, it may be possible to identify them by estimating heterozygosity across all individuals, or with a HWE test (see [https://onlinelibrary.wiley.com/doi/full/10.1111/1755-0998.13019 here] for a method of testing HWE on low depth data that is robust to population structure).  Without access to multiple individuals, it will be difficult to resolve these issues except through a better reference genome.&lt;br /&gt;
&lt;br /&gt;
One thing we found with the human data in the paper was that applying a &amp;quot;mappability&amp;quot; filter improved our estimates of R0, R1, or KING-robust kinship.  This is essentially a scan of the reference genome that reports where in the genome it is possible to uniquely align reads, and needs to be calibrated to your read lengths.  We used [https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0030377 GEM] &lt;br /&gt;
But there are also [https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6237805 new programs] that claim to do the same.&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1423</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1423"/>
		<updated>2019-09-24T12:03:48Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 method citation]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block. However, if your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
Yes, point estimates of R0, R1, or KING-robust kinship do not require a reference genome that is assembled into chromosomes.  If you have enough RAM, you can use all of your data at once, otherwise you will need to split your data into pieces, analyze each piece, and combine them afterwards.&lt;br /&gt;
&lt;br /&gt;
== How can the quality of the reference genome assembly affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Probably in multiple, complicated ways! &lt;br /&gt;
&lt;br /&gt;
But one to watch out for are regions of your reference genome that are attracting aligned reads originating from multiple, distinct parts of the genome. These regions can exhibit elevated heterozygosity across all individuals.  This is particularly problematic for our analyses, as shared heterozygosity between a pair of individuals (sites where both individuals are heterozygotes) are the strongest signals we have that the individuals are related. This is because shared heterozygosity shows two important things about a site: 1) the site is variable (segregating) in the population, and 2) that the pair of individuals has the maximum possible allelic identity at the site. A upward bias in shared heterozygosity will result in KING-robust kinship estimates biased toward 0.5, elevated values of R1, and lower values of R0.&lt;br /&gt;
&lt;br /&gt;
If possible these regions of the reference genome should be excluded from the analysis. With access to multiple individuals, it may be possible to identify them by estimating heterozygosity across all individuals, or with a HWE test (see [https://onlinelibrary.wiley.com/doi/full/10.1111/1755-0998.13019 here] for a method of testing HWE on low depth data that is robust to population structure).  Without access to multiple individuals, it will be difficult to resolve these issues except through a better reference genome.&lt;br /&gt;
&lt;br /&gt;
One thing we found with the human data in the paper was that applying a &amp;quot;mappability&amp;quot; filter improved our estimates of R0, R1, or KING-robust kinship.  This is essentially a scan of the reference genome that reports where in the genome it is possible to uniquely align reads, and needs to be calibrated to your read lengths.  We used [https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0030377 GEM] &lt;br /&gt;
But there are also [https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6237805 new programs] that claim to do the same  .&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1422</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1422"/>
		<updated>2019-09-24T11:54:07Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 method citation]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block. However, if your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
Yes, point estimates of R0, R1, or KING-robust kinship do not require a reference genome that is assembled into chromosomes.  If you have enough RAM, you can use all of your data at once, otherwise you will need to split your data into pieces, analyze each piece, and combine them afterwards.&lt;br /&gt;
&lt;br /&gt;
== How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Probably in multiple, complicated ways! &lt;br /&gt;
&lt;br /&gt;
But one to watch out for are regions of your reference genome that are attracting aligned reads originating from multiple, distinct parts of the genome. These regions can exhibit elevated heterozygosity across all individuals.  This is particularly problematic for our analyses, as shared heterozygosity between a pair of individuals (sites where both individuals are heterozygotes) are the strongest signals we have that the individuals are related. This is because shared heterozygosity shows two important things about a site: 1) the site is variable (segregating) in the population, and 2) that the pair of individuals has the maximum possible allelic identity at the site. A upward bias in shared heterozygosity will result in KING-robust kinship estimates biased toward 0.5, elevated values of R1, and lower values of R0.&lt;br /&gt;
&lt;br /&gt;
If possible these regions of the reference genome should be excluded from the analysis. With access to multiple individuals, it may be possible to identify them by estimating heterozygosity across all individuals, or with a HWE test (see [https://onlinelibrary.wiley.com/doi/full/10.1111/1755-0998.13019 here] for a method of testing HWE on low depth data that is robust to population structure).  Without access to multiple individuals, it will be difficult to resolve these issues except through a better reference genome.&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1421</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1421"/>
		<updated>2019-09-24T11:32:38Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 method citation]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block. However, if your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
Yes, point estimates of R0, R1, or KING-robust kinship do not require a reference genome that is assembled into chromosomes.  If you have enough RAM, you can use all of your data at once, otherwise you will need to split your data into pieces, analyze each piece, and combine them afterwards.&lt;br /&gt;
&lt;br /&gt;
== How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1420</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1420"/>
		<updated>2019-09-24T11:29:39Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 method citation]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block. However, if your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
&lt;br /&gt;
== How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1419</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1419"/>
		<updated>2019-09-24T11:29:04Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 citation ]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block. However, if your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
&lt;br /&gt;
== How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1418</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1418"/>
		<updated>2019-09-24T11:24:18Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' blocks that can be of unequal sizes.  Confidence intervals are then generated by leaving out each block and computing R0, R1, and KING-robust kinship on these '''X''' distinct sets [https://link.springer.com/article/10.1023/A:1008800423698 see here]. This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the '''X''' blocks are independent from the observations in all the other pieces. For relatives, this implies that identity-by-descent (IBD) tracts should not extend into multiple blocks, as blocks within the same IBD tract will not be independent.  One easy solution if your reference genome contains chromosomes is to assign each chromosome to a distinct block.  If your reference genome only has smaller contigs, it will be difficult to interpret the confidence intervals produced by a block jackknife procedure like this as observations may not be fully independent. This issue is also discussed in the paper.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
&lt;br /&gt;
== How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1417</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1417"/>
		<updated>2019-09-24T11:15:42Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into '''X''' pieces, that can be of unequal sizes.  Confidence intervals are then generated by leaving out each piece and computing R0, R1, and KING-robust kinship on these X distinct sets [https://link.springer.com/article/10.1023/A:1008800423698].  This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.  &lt;br /&gt;
&lt;br /&gt;
One important difference here is that this procedure assumes that the observations within each of the X pieces are independent of&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
&lt;br /&gt;
== How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1416</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1416"/>
		<updated>2019-09-24T11:14:32Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a weighted block jackknife procedure.  This involves dividing your data set into X pieces, that can be of unequal sizes.  Confidence intervals are then generated by leaving out each piece and computing R0, R1, and KING-robust kinship on these X distinct sets [https://link.springer.com/article/10.1023/A:1008800423698].  This is a similar approach to generating Z-scores for D-statistics[https://www.genetics.org/content/192/3/1065.short] that are informative about admixture.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
&lt;br /&gt;
== How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1415</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1415"/>
		<updated>2019-09-24T11:06:36Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
Confidence intervals around R0, R1, and KING-robust kinship can be generated with a [https://link.springer.com/article/10.1023/A:1008800423698 block jackknife] procedure.&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
&lt;br /&gt;
== How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1414</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1414"/>
		<updated>2019-09-24T11:01:42Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: /* I run out of RAM running IBS or realSFS on my entire data set, what should I do? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
You can split the data set up by chromosome (or contig), run the IBS or SFS method on each chromosome, and then combine afterwards by summing the values ['A' - 'I'] across chromosomes.  &lt;br /&gt;
&lt;br /&gt;
There a few possible concerns with this approach.  1) The optimization routines work best with more data, so be careful of breaking up your data into too many small groups. This is especially true as R0, R1, and KING-robust kinship are all ratios and can be sensitive to small absolute errors.    2) Also, without chromosomes, it becomes difficult to generate meaningful confidence intervals around R0, R1, and KING-robust kinship, as it important to account for correlations between nearby sites when generating the confidence intervals with something like a jackknife.  You can jackknife by leaving one contig out each time, but if the correlations (i.e. the identity-by-descent (IBD) blocks present between relatives) extend beyond the contigs, this procedure will produce confidence intervals that are smaller than they should be.&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
&lt;br /&gt;
== How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1413</id>
		<title>IBSrelate</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=IBSrelate&amp;diff=1413"/>
		<updated>2019-09-24T09:34:33Z</updated>

		<summary type="html">&lt;p&gt;Albrecht: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about the method '''IBSrelate''', a method to identify pairs of related individuals without requiring population allele frequencies. &lt;br /&gt;
&lt;br /&gt;
On this page we will show you how to estimate the R0, R1 and KING-robust kinship statistics for a pair (or more!) of individuals from aligned sequencing data. These statistics are informative about relatedness, but can also be useful for quality-control (QC). &lt;br /&gt;
&lt;br /&gt;
For further details, including the interpretation of R0, R1 and KING-robust kinship, please see our paper in Molecular Ecology at: https://doi.org/10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Calculating statistics from the output of IBS and realSFS =&lt;br /&gt;
'''IBS''' and '''realSFS''' are two methods implemented in ANGSD [http://www.popgen.dk/angsd/index.php/ANGSD] that can be used to estimate the allele sharing ''genotype distribution'' for a pair of individuals.  The paper describes and examines the differences between the two methods, but we expect they both will perform comparably well for most applications.  Below are links to two R scripts that can be used to load the output of '''IBS''' and '''realSFS''' and produce estimates of '''R0''', '''R1''' and '''KING-robust kinship'''.&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
https://github.com/rwaples/freqfree_suppl/blob/master/read_realSFS.R&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Application to the ANGSD example data =&lt;br /&gt;
The shell commands given below are available in a text file here: https://github.com/rwaples/freqfree_suppl/blob/master/example_data.sh .&lt;br /&gt;
&lt;br /&gt;
They are available in a Jupyter notebook here (slightly older version): https://nbviewer.jupyter.org/github/rwaples/freqfree_suppl/blob/master/example_data.ipynb .&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
You will need installations and both [http://www.popgen.dk/angsd/index.php/ANGSD ANGSD] and [http://www.htslib.org/ samtools], as well as Rscript (part of [https://www.r-project.org/ R]). &lt;br /&gt;
&lt;br /&gt;
The commands below will download files to your current directory, as well as create a few sub-directories.  In total the analysis will download and generate files with a total size of about 1.5 GB.&lt;br /&gt;
&lt;br /&gt;
=== Set up shell variables ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set paths to the analysis programs, may need to be replaced your local installation(s)&lt;br /&gt;
ANGSD=&amp;quot;$HOME/programs/angsd/angsd&amp;quot;&lt;br /&gt;
realSFS=&amp;quot;$HOME/programs/angsd/misc/realSFS&amp;quot;&lt;br /&gt;
IBS=&amp;quot;$HOME/programs/angsd/misc/ibs&amp;quot;&lt;br /&gt;
SAMTOOLS=&amp;quot;samtools&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get the example data ===&lt;br /&gt;
The example data set has small bam files from ten individuals.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download the example data&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/bams.tar.gz&lt;br /&gt;
&lt;br /&gt;
# unzip/untar and index the bam files&lt;br /&gt;
tar xf bams.tar.gz&lt;br /&gt;
for i in bams/*.bam;do samtools index $i;done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== realSFS method ==&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_realsfs&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the realSFS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_realSFS.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specify an allele at each site ===&lt;br /&gt;
For the realSFS method, one of the alleles at each site must be specified. Here we will use an ancestral state file (fasta format).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# download and index the ancestral state fasta file&lt;br /&gt;
wget http://popgen.dk/software/download/angsd/hg19ancNoChr.fa.gz&lt;br /&gt;
$SAMTOOLS faidx hg19ancNoChr.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a saf (site allele frequency likelihood) file for each individual ===&lt;br /&gt;
In the commands below, I apply minMapQ (-minMapQ 30) and minQ (-minQ 20) filters, as well as specify a specific genotype likelihood model (-GL 2).  These values worked well for this data set and seem to be reasonable defaults, but the best values may vary by data set. I also generate summaries of sequencing depth (-doDepth) and allele counts (-doCounts).  The output of these are not evaluated here, but they should be examined be part of a general QC process for NGS data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a separate bam filelist for each individual&lt;br /&gt;
# also create a SAMPLES array for use below&lt;br /&gt;
BAMS=./bams/*.bam&lt;br /&gt;
SAMPLES=()&lt;br /&gt;
for b in $BAMS; do&lt;br /&gt;
  # parse out the sample name&lt;br /&gt;
  base=&amp;quot;$(basename -- $b)&amp;quot;&lt;br /&gt;
  sample=&amp;quot;${base%%.mapped.*}&amp;quot;&lt;br /&gt;
  SAMPLES+=(&amp;quot;$sample&amp;quot;)&lt;br /&gt;
  echo $sample&lt;br /&gt;
  echo $b &amp;gt; ${sample}.filelist.ind&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# run doSAF on each individual&lt;br /&gt;
for s in &amp;quot;${SAMPLES[@]}&amp;quot;; do&lt;br /&gt;
  $ANGSD -b ${s}.filelist.ind \&lt;br /&gt;
  -anc hg19ancNoChr.fa.gz \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doSaf 1 -doDepth 1 -doCounts 1 \&lt;br /&gt;
  -out ${s}&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run realSFS on each pair of indiviudals ===&lt;br /&gt;
Here we have 10 individuals, and want to consider each pair just once.  We index the SAMPLES array created above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i in {0..9}; do&lt;br /&gt;
  for j in {0..9}; do&lt;br /&gt;
    if (( i &amp;lt; j)); then&lt;br /&gt;
      sample1=${SAMPLES[i]}&lt;br /&gt;
      sample2=${SAMPLES[j]}&lt;br /&gt;
      $realSFS ${sample1}.saf.idx ${sample2}.saf.idx &amp;gt; ./results_realsfs/${sample1}_${sample2}.2dsfs&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parse the results for a single pair of individuals ===&lt;br /&gt;
Below shows how to use the read_realSFS() function in R to parse the output 2dsfs file generated by realSFS.  It will be easier to open an interactive R session, source read_realSFS.R, and use the function read_realSFS() to parse the file into data frame to then explore, plot, or export.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_realSFS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = read_realSFS('results_realsfs/smallNA06985_smallNA11830.2dsfs')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res['sample1'] = 'smallNA06985'; res['sample2'] = 'smallNA11830'&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[,c('sample1', 'sample2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IBS Method == &lt;br /&gt;
&lt;br /&gt;
=== Setup ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# make a directory for the results&lt;br /&gt;
mkdir results_IBS&lt;br /&gt;
&lt;br /&gt;
# get the R script to parse the IBS output&lt;br /&gt;
wget https://raw.githubusercontent.com/rwaples/freqfree_suppl/master/read_IBS.R&lt;br /&gt;
&lt;br /&gt;
## make a file with paths to the bam(s) for all individuals&lt;br /&gt;
ls bams/*.bam &amp;gt; all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== make a genotype likelihood file (glf) containing all individuals ===&lt;br /&gt;
see [http://www.popgen.dk/angsd/index.php/Genotype_Likelihoods#Output_genotype_likelihoods here] for a description of the file format produced by -doGLF 1.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ANGSD -b all.filelist \&lt;br /&gt;
  -minMapQ 30 -minQ 20 -GL 2 \&lt;br /&gt;
  -doGlf 1 \&lt;br /&gt;
  -out example&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== run IBS, this will analyse each pair of individuals ===&lt;br /&gt;
Here we have specified &amp;quot;model 0&amp;quot;, so the command will generate expected values for each of the 100 possible pairwise 2-allele genotypes for each pair of individuals (10 unique genotypes per individual).&lt;br /&gt;
man page for misc/ibs [http://www.popgen.dk/angsd/index.php/Genotype_Distribution here]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$IBS -glf example.glf.gz \&lt;br /&gt;
  -model 0 \&lt;br /&gt;
  -nInd 10 -allpairs 1 \&lt;br /&gt;
  -outFileName results_IBS/ibs.model0.results&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Examine the results ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rscript \&lt;br /&gt;
  -e &amp;quot;source('./read_IBS.R')&amp;quot; \&lt;br /&gt;
  -e &amp;quot;res = do_derived_stats(read_ibspair_model0('results_IBS/ibs.model0.results.ibspair'))&amp;quot; \&lt;br /&gt;
  -e &amp;quot;print(res[6,c('ind1', 'ind2', 'nSites', 'Kin', 'R0', 'R1') ])&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# the IBS method in ANGSD indexes individuals as they appear in the filelist&lt;br /&gt;
# (zero-indexed)&lt;br /&gt;
cat all.filelist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Frequently asked Questions=&lt;br /&gt;
&lt;br /&gt;
== I run out of RAM running IBS or realSFS on my entire data set, what should I do?  ==&lt;br /&gt;
&lt;br /&gt;
== How can I estimate confidence intervals around my R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
&lt;br /&gt;
== Can I estimate R0, R1, or KING-robust kinship even if my reference genome is not assembled into chromosomes?  ==&lt;br /&gt;
&lt;br /&gt;
== How can reference genome assembly errors affect R0, R1, or KING-robust kinship estimates? ==&lt;br /&gt;
&lt;br /&gt;
== How will inbreeding affects the estimates produces here? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Citation=&lt;br /&gt;
Waples, R. K., Albrechtsen, A. and Moltke, I. (2018), Allele frequency‐free inference of close familial relationships from genotypes or low depth sequencing data. Mol Ecol. doi:10.1111/mec.14954&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Bibtex==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@article{doi:10.1111/mec.14954,&lt;br /&gt;
author = {Waples, Ryan K and Albrechtsen, Anders and Moltke, Ida},&lt;br /&gt;
title = {Allele frequency-free inference of close familial relationships from genotypes or low depth sequencing data},&lt;br /&gt;
journal = {Molecular Ecology},&lt;br /&gt;
volume = {0},&lt;br /&gt;
number = {ja},&lt;br /&gt;
pages = {},&lt;br /&gt;
doi = {10.1111/mec.14954},&lt;br /&gt;
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/mec.14954},&lt;br /&gt;
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/mec.14954},&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Albrecht</name></author>
	</entry>
</feed>