<?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=Sonia4</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=Sonia4"/>
	<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php/Special:Contributions/Sonia4"/>
	<updated>2026-04-30T14:18:01Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.40.1</generator>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1366</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1366"/>
		<updated>2019-08-07T10:53:51Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Plot the results */&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&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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1365</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1365"/>
		<updated>2019-08-07T10:52:35Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Infer selection along the genome */&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&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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1364</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1364"/>
		<updated>2019-08-07T10:50:26Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Download the input and sample 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&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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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;
pdf(&amp;quot;PCAngsdDemo2_2_1.pdf&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;
pdf(&amp;quot;PCAngsdDemo2_2_2.pdf&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1363</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1363"/>
		<updated>2019-08-07T10:47:16Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Plot the results */&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&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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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/pcangsd.py -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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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;
pdf(&amp;quot;PCAngsdDemo2_2_1.pdf&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;
pdf(&amp;quot;PCAngsdDemo2_2_2.pdf&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1362</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1362"/>
		<updated>2019-08-07T10:45:55Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Plot the results in R */&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&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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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/pcangsd.py -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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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;
## 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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1361</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1361"/>
		<updated>2019-08-07T10:44:19Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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/pcangsd.py -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;
 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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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;
## 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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1360</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1360"/>
		<updated>2019-08-07T10:44:00Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Download the input and sample 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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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/pcangsd.py -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;
 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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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;
## 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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1359</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1359"/>
		<updated>2019-08-07T10:32:43Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Run PCAngsd */&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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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/pcangsd.py -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;
 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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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;
## 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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1358</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1358"/>
		<updated>2019-08-07T10:22:21Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -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;
 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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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;
## 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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1357</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1357"/>
		<updated>2019-08-07T10:22:09Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
&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/pcangsd.py -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/pcangsd.py -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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -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;
 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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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;
## 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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1356</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1356"/>
		<updated>2019-08-07T10:18:33Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Inbreeding in the admixed individuals */&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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo2PCANGSD_3.K3.a50.0.qopt&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/pcangsd.py -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/pcangsd.py -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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -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;
 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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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;
## 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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1355</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1355"/>
		<updated>2019-08-07T10:18:21Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Inbreeding with 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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo2PCANGSD_3.K3.a50.0.qopt&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/pcangsd.py -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 | 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/pcangsd.py -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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -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;
 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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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;
## 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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1354</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1354"/>
		<updated>2019-08-07T10:11:16Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Infer selection along the genome */&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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo2PCANGSD_3.K3.a50.0.qopt&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/pcangsd.py -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 | 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/pcangsd.py -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 | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -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;
 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/pcangsd.py -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;
## 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;-scan(&amp;quot;Demo2PCANGSD_2.selection.gz&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;
## 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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1353</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1353"/>
		<updated>2019-08-07T10:09:51Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Plot the results in R */&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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo2PCANGSD_3.K3.a50.0.qopt&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/pcangsd.py -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 | 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/pcangsd.py -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 | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1352</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1352"/>
		<updated>2019-08-07T10:09:22Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Inbreeding with 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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo2PCANGSD_3.K3.a50.0.qopt&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/pcangsd.py -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 | 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/pcangsd.py -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 | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1351</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1351"/>
		<updated>2019-08-07T10:09:06Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Inbreeding with 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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo2PCANGSD_3.K3.a50.0.qopt&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/pcangsd.py -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 | 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/pcangsd.py -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 pop.info Demo1PCANGSD_5.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1350</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1350"/>
		<updated>2019-08-07T10:07:48Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Run PCAngsd */&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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo2PCANGSD_3.K3.a50.0.qopt&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/pcangsd.py -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 | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1349</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1349"/>
		<updated>2019-08-07T10:06:52Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Inbreeding in the admixed individuals */&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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo2PCANGSD_3.K3.a50.0.qopt&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/pcangsd.py -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 | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1348</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1348"/>
		<updated>2019-08-07T10:05:49Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo2PCANGSD_3.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo2PCANGSD_3.K3.a50.0.qopt&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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1347</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1347"/>
		<updated>2019-08-07T10:05:19Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Without 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&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 $IN_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/pcangsd.py -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/pcangsd.py -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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1346</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1346"/>
		<updated>2019-08-07T10:04:59Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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 $IN_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/pcangsd.py -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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;Demo2PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1345</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1345"/>
		<updated>2019-08-07T10:00:12Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;Demo2PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.admix.Q.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
#old NAME: &amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1344</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1344"/>
		<updated>2019-08-07T09:57:55Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;Demo2PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;Demo1pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1343</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1343"/>
		<updated>2019-08-07T09:55:58Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Without 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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;Demo2PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1342</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1342"/>
		<updated>2019-08-07T09:50:11Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1341</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1341"/>
		<updated>2019-08-07T09:48:18Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Infer selection along the genome */&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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;EUsmall.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;
 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/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1340</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1340"/>
		<updated>2019-08-07T09:47:47Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Run PCAngsd */&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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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; wc $IN_DIR/Demo2sample.info &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;python $PCANGSD/pcangsd.py -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;EUsmall.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;
 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1339</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1339"/>
		<updated>2019-08-07T09:43:50Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Inbreeding with 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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;EUsmall.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;
 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1338</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1338"/>
		<updated>2019-08-07T09:43:39Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Inbreeding in the admixed individuals */&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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;EUsmall.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;
 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1337</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1337"/>
		<updated>2019-08-07T09:43:27Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;EUsmall.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;
 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1336</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1336"/>
		<updated>2019-08-07T09:43:16Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Without 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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;EUsmall.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;
 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1335</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1335"/>
		<updated>2019-08-07T09:42:21Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Run PCAngsd */&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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;EUsmall.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;
 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1334</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1334"/>
		<updated>2019-08-07T09:41:39Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Set the path to PCAngsd */&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&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 $IN_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;PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;EUsmall.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;
 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1333</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1333"/>
		<updated>2019-08-07T09:40:50Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Set the path to PCAngsd */&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=python ~/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 $IN_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;PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;EUsmall.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;
 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1332</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1332"/>
		<updated>2019-08-07T09:40:20Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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 $IN_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;PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;EUsmall.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;
 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1331</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1331"/>
		<updated>2019-08-07T09:39:54Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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 $IN_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/pcangsd.py -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;EUsmall.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;
 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdv2&amp;diff=1330</id>
		<title>PCAngsdv2</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdv2&amp;diff=1330"/>
		<updated>2019-08-07T09:35:29Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Inbreeding */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&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 file&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/input.gz &lt;br /&gt;
maybe change it to: &lt;br /&gt;
wget popgen.dk/software/download/PCAngsd/data/input.gz&lt;br /&gt;
'''*ANDERS*'''&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 1 -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;
Population structure: [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;
HWE test: [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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdv2&amp;diff=1329</id>
		<title>PCAngsdv2</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdv2&amp;diff=1329"/>
		<updated>2019-08-07T09:31:39Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Output */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&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 file&lt;br /&gt;
'''*ANDERS*''' &lt;br /&gt;
wget popgen.dk/software/download/NGSadmix/data/input.gz &lt;br /&gt;
maybe change it to: &lt;br /&gt;
wget popgen.dk/software/download/PCAngsd/data/input.gz&lt;br /&gt;
'''*ANDERS*'''&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 1 -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;
; -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;
; -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;
; -inbreed_iter [int]&lt;br /&gt;
Maximum number of iterations for the EM algorithm methods. (Default: 200)&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;
&lt;br /&gt;
&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;
&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;
; -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;
Population structure: [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;
HWE test: [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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1328</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1328"/>
		<updated>2019-08-06T23:02:14Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Demo 2: Selection */&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&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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_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;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;EUsmall.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;
 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1327</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1327"/>
		<updated>2019-08-06T22:59:15Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Download the input and sample 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&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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;EUsmall.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;
 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 populating differences. However, PCA offers a good description of the differences between 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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1326</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1326"/>
		<updated>2019-08-06T22:56:31Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Download the input and sample 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&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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file &lt;br /&gt;
&lt;br /&gt;
(EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;
## R&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;EUsmall.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;
 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 populating differences. However, PCA offers a good description of the differences between individuals which out having the define disjoint groups.&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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1325</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1325"/>
		<updated>2019-08-06T22:56:03Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Download the beagle genotype likelihood input file */&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&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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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;
There is a file with the positions and sample information.&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file (EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;
## R&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;EUsmall.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;
 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 populating differences. However, PCA offers a good description of the differences between individuals which out having the define disjoint groups.&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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1324</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1324"/>
		<updated>2019-08-06T22:55:11Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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 beagle genotype likelihood input file ====&lt;br /&gt;
&lt;br /&gt;
There is a file with the positions and sample information.&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file (EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;
## R&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;EUsmall.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;
 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 populating differences. However, PCA offers a good description of the differences between individuals which out having the define disjoint groups.&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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1323</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1323"/>
		<updated>2019-08-06T22:54:24Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Demo 2: Selection */&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&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;
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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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 beagle genotype likelihood input file ====&lt;br /&gt;
&lt;br /&gt;
There is a file with the positions and sample information.&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file (EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;
## R&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;EUsmall.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;
 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 populating differences. However, PCA offers a good description of the differences between individuals which out having the define disjoint groups.&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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1322</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1322"/>
		<updated>2019-08-06T22:54:09Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Demo 2: Selection */&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&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;
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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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 individuals 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 beagle genotype likelihood input file ====&lt;br /&gt;
&lt;br /&gt;
There is a file with the positions and sample information.&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file (EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;
## R&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;EUsmall.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;
 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 populating differences. However, PCA offers a good description of the differences between individuals which out having the define disjoint groups.&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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1321</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1321"/>
		<updated>2019-08-06T22:53:56Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Demo 2: Selection */&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&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;
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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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 individuals 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 beagle genotype likelihood input file ====&lt;br /&gt;
&lt;br /&gt;
There is a file with the positions and sample information.&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file (EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;
## R&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;EUsmall.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;
 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 populating differences. However, PCA offers a good description of the differences between individuals which out having the define disjoint groups.&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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1320</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1320"/>
		<updated>2019-08-06T22:53:19Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Inbreeding with 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&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;
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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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 pop.info Demo4PCANGSD.inbreed | 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 individuals 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 beagle genotype likelihood input file ====&lt;br /&gt;
&lt;br /&gt;
There is a file with the positions and sample information.&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file (EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;
## R&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;EUsmall.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;
 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 populating differences. However, PCA offers a good description of the differences between individuals which out having the define disjoint groups.&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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1319</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1319"/>
		<updated>2019-08-06T22:52:08Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* Without 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&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;
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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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;
&amp;lt;code&amp;gt;paste pop.info Demo4PCANGSD.inbreed | 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 individuals 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 beagle genotype likelihood input file ====&lt;br /&gt;
&lt;br /&gt;
There is a file with the positions and sample information.&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file (EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;
## R&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;EUsmall.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;
 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 populating differences. However, PCA offers a good description of the differences between individuals which out having the define disjoint groups.&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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1318</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1318"/>
		<updated>2019-08-06T22:51:50Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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;
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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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:&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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;
&amp;lt;code&amp;gt;paste pop.info Demo4PCANGSD.inbreed | 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 individuals 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 beagle genotype likelihood input file ====&lt;br /&gt;
&lt;br /&gt;
There is a file with the positions and sample information.&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file (EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;
## R&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;EUsmall.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;
 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 populating differences. However, PCA offers a good description of the differences between individuals which out having the define disjoint groups.&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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
	<entry>
		<id>https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1317</id>
		<title>PCAngsdTutorial</title>
		<link rel="alternate" type="text/html" href="https://www.popgen.dk/software/index.php?title=PCAngsdTutorial&amp;diff=1317"/>
		<updated>2019-08-06T22:51:41Z</updated>

		<summary type="html">&lt;p&gt;Sonia4: /* 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&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;
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 $IN_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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD&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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo1PCANGSD.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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo2PCANGSD -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;pop.info&amp;quot;)&lt;br /&gt;
C &amp;lt;- as.matrix(read.table(&amp;quot;Demo2PCANGSD.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:&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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;
pop&amp;lt;-read.table(&amp;quot;pop.info&amp;quot;,as.is=T)&lt;br /&gt;
&lt;br /&gt;
###########CHECK NAME OF FILE###################&lt;br /&gt;
q&amp;lt;-read.table(&amp;quot;Demo3PCANGSD.K3.a50.0.qopt&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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo3PCANGSD -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 pop.info Demo3PCANGSD.inbreed | 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;$PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo4PCANGSD -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;
&amp;lt;code&amp;gt;paste pop.info Demo4PCANGSD.inbreed | 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 individuals 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 beagle genotype likelihood input file ====&lt;br /&gt;
&lt;br /&gt;
There is a file with the positions and sample information.&lt;br /&gt;
(cp $ThePath/PCangsd/data/eu1000g.sample.Info)&lt;br /&gt;
&lt;br /&gt;
And a beagle file (EU1000=$ThePath/PCangsd/data/eu1000g.small.beagle.gz)&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;
&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;
&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 $IN_DIR&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''*ANDERS*'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wc Demo2sample.info&lt;br /&gt;
N=424 #one line for header&lt;br /&gt;
&lt;br /&gt;
#run PCANGSD&lt;br /&gt;
$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -threads 4&lt;br /&gt;
#-n $N -threads 20 # from previous year...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&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;
## R&lt;br /&gt;
 cov &amp;lt;- as.matrix(read.table(&amp;quot;EUsmall.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;
 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 populating differences. However, PCA offers a good description of the differences between individuals which out having the define disjoint groups.&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;$PCANGSD -beagle $IN_DIR/Demo2input.gz -o EUsmall -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;
## 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;-scan(&amp;quot;EUsmall.selection.gz&amp;quot;)&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;EUsmall.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;Manhatten plot&amp;quot;)&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>Sonia4</name></author>
	</entry>
</feed>