PCAngsdTutorial: Difference between revisions

From software
Jump to navigation Jump to search
 
(86 intermediate revisions by 2 users not shown)
Line 1: Line 1:
We will go through a simple and more complex example on how to use PCAngsd with visualization of the data.
We will go through some examples on how to use PCAngsd with visualization of the data.


=PCA with admixture aware priors=
==== Set the path to PCAngsd ====
 
This example will perform a PCA analysis on 1000 genotype likelihoods.


Every time you open a new terminal window, set the paths to the program and the input file.
Every time you open a new terminal window, set the paths to the program and the input file.
===Set the path to PCAngsd===


<code>
<code>
PCAngsd=~/Software/PCAngsd
PCANGSD=~/Software/pcangsd/pcangsd.py
</code>
</code>


Line 19: Line 15:
</code>
</code>


===Create directories===
==== Create directories ====
Create the directories that will be used for working:
Create the directories that will be used for working:


<code>mkdir Demo</code>
<code>mkdir Demo</code>


<code>cd Demo</code>


<code>mkdir Data</code>
<code>mkdir Demo/Data</code>


<code>mkdir Results</code>
<code>mkdir Demo/Results</code>


===Set the paths to your local directories===
==== Set the paths to your local directories ====


<code>IN_DIR=Demo/Data</code>
<code>IN_DIR=Demo/Data</code>
Line 42: Line 37:
<code>ls $OUT_DIR</code>
<code>ls $OUT_DIR</code>


====Download the beagle genotype likelihood input file====
=== Demo 1: Allele Frequencies ===
 
This example will perform a PCA analysis on 1000 genotype likelihoods.
 
==== Download the input and population information files ====


PCAngsd uses Genotype Likelihoods (GLs) in .beagle format as input. The input file has been created for you.  
PCAngsd uses Genotype Likelihoods (GLs) in .beagle format as input. The input file has been created for you.  


Download the files and move them to your input folder (for example, $IN_DIR):
The population information file is also provided.
 
To download the files and move them to your input folder (for example, $IN_DIR):
 


'''*ANDERS*'''


<code>wget popgen.dk/software/download/NGSadmix/data/Demo1input.gz</code>
<code>wget popgen.dk/software/download/NGSadmix/data/Demo1input.gz</code>
Line 57: Line 58:


<code>mv Demo1pop.info $IN_DIR</code>
<code>mv Demo1pop.info $IN_DIR</code>
'''*ANDERS*'''


==== View the genotype likelihood beagle file ====
==== View the genotype likelihood beagle file ====
Line 66: Line 65:
*In order to see the first 10 columns and 10 lines of the input file, type:
*In order to see the first 10 columns and 10 lines of the input file, type:


<code>gunzip -c $IN_DIR/Demo1input.gz | head -n 10 | cut -f 1-10 | column -t</code>
:<code>gunzip -c $IN_DIR/Demo1input.gz | head -n 10 | cut -f 1-10 | column -t</code>


*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):
*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):


<code>gunzip -c $IN_DIR/Demo1input.gz | wc -l</code>
:<code>gunzip -c $IN_DIR/Demo1input.gz | wc -l</code>


==== View population information file ====
==== View population information file ====
Line 81: Line 80:


<code> cut -f1 -d" " $IN_DIR/Demo1pop.info  > $OUT_DIR/poplabel </code>
<code> cut -f1 -d" " $IN_DIR/Demo1pop.info  > $OUT_DIR/poplabel </code>
=== Run PCAngsd ===
The program estimates the covariance matrix that can be used for PCA.
==== Estimating Individual Allele Frequencies ====
<code>python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_1</code>
Plot the results in R
<pre>
#open R
pop<-read.table("Demo/Data/Demo1pop.info")
C <- as.matrix(read.table("Demo/Results/Demo1PCANGSD_1.cov"))
e <- eigen(C)
pdf("PCAngsd1.pdf")
plot(e$vectors[,1:2],col=pop[,1],xlab="PC1",ylab="PC2", main="individual allele frequency")
legend("top",fill=1:5,levels(pop[,1]))
dev.off()
## close R
</pre>
To view the plot, type:
<code>evince PCAngsd1.pdf</code>
==== Without Estimating Individual Allele Frequencies ====
Try the same analysis but without estimating individual allele frequencies.
This is the same as using the first iteration of the algorithm.
<code>python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_2 -iter 0</code>
Plot the results in R
<pre>
#open R
pop<-read.table("Demo1pop.info")
C <- as.matrix(read.table("Demo1PCANGSD_2.cov"))
e <- eigen(C)
pdf("PCAngsd2.pdf")
plot(e$vectors[,1:2],col=pop[,1],xlab="PC1",ylab="PC2",main="joint allele frequency")
legend("top",fill=1:5,levels(pop[,1]))
dev.off()
## close R
</pre>
To view the plot, type:
<code>evince PCAngsd2.pdf</code>
==== Admixture based on two PC ====
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).
<code>python $PCANGSD -beagle $IN_DIR/Demo1input.gz -e 2 -o $OUT_DIR/Demo2PCANGSD_3 -admix -admix_alpha 50</code>
Plot the results in R
<pre>
#open R
##Requires previous installation of the library RcppCNPy
library(RcppCNPy) # Numpy library for R
pop<-read.table("Demo/Data/Demo1pop.info",as.is=T)
q <- npyLoad("Demo/Results/Demo2PCANGSD_3.admix.Q.npy")
## order according to population
ord<-order(pop[,1])
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab="Individuals",ylab="Admixture proportions")
text(tapply(1:nrow(pop),pop[ord,1],mean),-0.05,unique(pop[ord,1]),xpd=T)
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)
## close R
</pre>
==== Inbreeding in the admixed individuals ====
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.
<code>python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_4 -inbreed 2 -iter 0</code>
Join names and results, sort the values and look at the results
<code>paste Demo1pop.info Demo1PCANGSD_4.inbreed.npy | LC_ALL=C sort -k3g</code>
The third column is an estimate of the inbreeding coefficient (allowing for negative)
==== Inbreeding with individual allele frequencies ====
Now let's try to estimate the inbreeding coefficient of the samples by using the individual allele frequencies predicted by the PCA
<code>python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_5 -inbreed 2 </code>
Join names and results, sort the values and look at the results
<code>paste Demo1pop.info Demo1PCANGSD_5.inbreed.npy | LC_ALL=C sort -k3g</code>
=== Demo 2: Selection ===
For very resent selection we can look within closely related individuals, for example within Europeans.
The objective is to use PCAngsd to estimate the covariance matrix while jointly estimating the individual allele frequencies.
Data file:
*Genotype likelihoods in Beagle format
*~150k random SNPs with maf > 5%
*Four EU populations with ~100 individuals in each
:<table class="muse-table" border="2" cellpadding="5">
    <tr>
      <td>CEU</td>
      <td>European (mostly British)</td>
    </tr>
    <tr>
      <td>GBR</td>
      <td>Great Britain</td>
    </tr>
    <tr>
      <td>IBS</td>
      <td>Iberian/Spain</td>
    </tr>
    <tr>
      <td>TSI</td>
      <td>Italien</td>
    </tr>
:</table>
==== Download the input and sample information files ====
A file with the positions and sample information, and a beagle file are provided:
Download the files and move them to your input folder (for example, $IN_DIR):
'''*ANDERS*'''
<code>wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz</code>
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.small.beagle.gz)
<code>wget popgen.dk/software/download/NGSadmix/data/Demo2sample.info</code>
(/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.sample.Info)
<code>mv Demo2input.gz $IN_DIR</code>
<code>mv Demo2sample.info $OUT_DIR</code>
'''*ANDERS*'''
==== Run PCAngsd ====
The objective is to show the differences among individuals.
<code>python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_1 -threads 4</code>
==== Plot the results in R ====
<pre>
## Open R
cov <- as.matrix(read.table("Demo2PCANGSD_1.cov"))
e<-eigen(cov)
ID<-read.table("Demo2sample.info",head=T)
pdf("PCAngsdDemo2_1.pdf")
plot(e$vectors[,1:2],col=ID$POP)
legend("topleft",fill=1:4,levels(ID$POP))
</pre>
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.
=== Infer selection along the genome ===
Now let's try to use the PC to infer selection along the genome based on the PCA
<code>python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_2 -selection -sites_save #-n $N </code>
==== Plot the results ====
<pre>
library(RcppCNPy) # Numpy library for R
## function for QQplot
qqchi<-function(x,...){
lambda<-round(median(x)/qchisq(0.5,1),2)
  qqplot(qchisq((1:length(x)-0.5)/(length(x)),1),x,ylab="Observed",xlab="Expected",...);abline(0,1,col=2,lwd=2)
legend("topleft",paste("lambda=",lambda))
}
### read in seleciton statistics (chi2 distributed)
s<-npyLoad("Demo2PCANGSD_2.selection.npy")
## make QQ plot to QC the test statistics
qqchi(s)
# convert test statistic to p-value
pval<-1-pchisq(s,1)
## read positions (hg38)
p<-read.table("Demo2PCANGSD_2.sites",colC=c("factor","integer"),sep="_")
names(p)<-c("chr","pos")
## make manhatten plot
plot(-log10(pval),col=p$chr,xlab="Chromosomes",main="Manhattan plot")
## zoom into region
w<-range(which(pval<1e-7)) + c(-100,100)
keep<-w[1]:w[2]
plot(p$pos[keep],-log10(pval[keep]),col=p$chr[keep],xlab="HG38 Position chr2")
## see the position of the most significant SNP
p$pos[which.max(s)]
</pre>

Latest revision as of 17:10, 29 May 2020

We will go through some examples on how to use PCAngsd with visualization of the data.

Set the path to PCAngsd

Every time you open a new terminal window, set the paths to the program and the input file.

PCANGSD=~/Software/pcangsd/pcangsd.py

Test the link

ls $PCAngsd

Create directories

Create the directories that will be used for working:

mkdir Demo


mkdir Demo/Data

mkdir Demo/Results

Set the paths to your local directories

IN_DIR=Demo/Data

OUT_DIR=Demo/Results

Test the links

ls $IN_DIR

ls $OUT_DIR

Demo 1: Allele Frequencies

This example will perform a PCA analysis on 1000 genotype likelihoods.

Download the input and population information files

PCAngsd uses Genotype Likelihoods (GLs) in .beagle format as input. The input file has been created for you.

The population information file is also provided.

To download the files and move them to your input folder (for example, $IN_DIR):


wget popgen.dk/software/download/NGSadmix/data/Demo1input.gz

wget popgen.dk/software/download/NGSadmix/data/Demo1pop.info

mv Demo1input.gz $IN_DIR

mv Demo1pop.info $IN_DIR

View the genotype likelihood beagle file

  • 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.
  • In order to see the first 10 columns and 10 lines of the input file, type:
gunzip -c $IN_DIR/Demo1input.gz | head -n 10 | cut -f 1-10 | column -t
  • 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):
gunzip -c $IN_DIR/Demo1input.gz | wc -l

View population information file

To view a summary of the population information file, cut the first column, sort and count:

cut -f 1 -d " " $IN_DIR/Demo1pop.info | sort | uniq -c

Lets make a population label file and place it in the output directory

cut -f1 -d" " $IN_DIR/Demo1pop.info > $OUT_DIR/poplabel

Run PCAngsd

The program estimates the covariance matrix that can be used for PCA.

Estimating Individual Allele Frequencies

python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_1

Plot the results in R

#open R
pop<-read.table("Demo/Data/Demo1pop.info")
C <- as.matrix(read.table("Demo/Results/Demo1PCANGSD_1.cov"))
e <- eigen(C)

pdf("PCAngsd1.pdf")
plot(e$vectors[,1:2],col=pop[,1],xlab="PC1",ylab="PC2", main="individual allele frequency")
legend("top",fill=1:5,levels(pop[,1]))
dev.off()
## close R

To view the plot, type:

evince PCAngsd1.pdf

Without Estimating Individual Allele Frequencies

Try the same analysis but without estimating individual allele frequencies. This is the same as using the first iteration of the algorithm.

python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_2 -iter 0

Plot the results in R

#open R
pop<-read.table("Demo1pop.info")
C <- as.matrix(read.table("Demo1PCANGSD_2.cov"))
e <- eigen(C)

pdf("PCAngsd2.pdf")
plot(e$vectors[,1:2],col=pop[,1],xlab="PC1",ylab="PC2",main="joint allele frequency")
legend("top",fill=1:5,levels(pop[,1]))
dev.off()
## close R

To view the plot, type:

evince PCAngsd2.pdf

Admixture based on two PC

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).

python $PCANGSD -beagle $IN_DIR/Demo1input.gz -e 2 -o $OUT_DIR/Demo2PCANGSD_3 -admix -admix_alpha 50

Plot the results in R

#open R
##Requires previous installation of the library RcppCNPy

library(RcppCNPy) # Numpy library for R

pop<-read.table("Demo/Data/Demo1pop.info",as.is=T)

q <- npyLoad("Demo/Results/Demo2PCANGSD_3.admix.Q.npy") 

## order according to population
ord<-order(pop[,1])
barplot(t(q)[,ord],col=2:10,space=0,border=NA,xlab="Individuals",ylab="Admixture proportions")
text(tapply(1:nrow(pop),pop[ord,1],mean),-0.05,unique(pop[ord,1]),xpd=T)
abline(v=cumsum(sapply(unique(pop[ord,1]),function(x){sum(pop[ord,1]==x)})),col=1,lwd=1.2)

## close R

Inbreeding in the admixed individuals

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.

python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_4 -inbreed 2 -iter 0

Join names and results, sort the values and look at the results

paste Demo1pop.info Demo1PCANGSD_4.inbreed.npy | LC_ALL=C sort -k3g

The third column is an estimate of the inbreeding coefficient (allowing for negative)

Inbreeding with individual allele frequencies

Now let's try to estimate the inbreeding coefficient of the samples by using the individual allele frequencies predicted by the PCA

python $PCANGSD -beagle $IN_DIR/Demo1input.gz -o $OUT_DIR/Demo1PCANGSD_5 -inbreed 2

Join names and results, sort the values and look at the results

paste Demo1pop.info Demo1PCANGSD_5.inbreed.npy | LC_ALL=C sort -k3g

Demo 2: Selection

For very resent selection we can look within closely related individuals, for example within Europeans.

The objective is to use PCAngsd to estimate the covariance matrix while jointly estimating the individual allele frequencies.

Data file:

  • Genotype likelihoods in Beagle format
  • ~150k random SNPs with maf > 5%
  • Four EU populations with ~100 individuals in each
CEU European (mostly British)
GBR Great Britain
IBS Iberian/Spain
TSI Italien

Download the input and sample information files

A file with the positions and sample information, and a beagle file are provided:

Download the files and move them to your input folder (for example, $IN_DIR):

*ANDERS*

wget popgen.dk/software/download/NGSadmix/data/Demo2input.gz (/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.small.beagle.gz)

wget popgen.dk/software/download/NGSadmix/data/Demo2sample.info (/home/albrecht/oldPhDCourse/PCangsd/data/eu1000g.sample.Info)

mv Demo2input.gz $IN_DIR

mv Demo2sample.info $OUT_DIR

*ANDERS*

Run PCAngsd

The objective is to show the differences among individuals.

python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_1 -threads 4

Plot the results in R

 ## Open R

 cov <- as.matrix(read.table("Demo2PCANGSD_1.cov"))

 e<-eigen(cov)
 ID<-read.table("Demo2sample.info",head=T)

 pdf("PCAngsdDemo2_1.pdf")

 plot(e$vectors[,1:2],col=ID$POP)

 legend("topleft",fill=1:4,levels(ID$POP))

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.

Infer selection along the genome

Now let's try to use the PC to infer selection along the genome based on the PCA

python $PCANGSD -beagle $IN_DIR/Demo2input.gz -o $OUT_DIR/Demo2PCANGSD_2 -selection -sites_save #-n $N


Plot the results


library(RcppCNPy) # Numpy library for R

## function for QQplot
qqchi<-function(x,...){
lambda<-round(median(x)/qchisq(0.5,1),2)
  qqplot(qchisq((1:length(x)-0.5)/(length(x)),1),x,ylab="Observed",xlab="Expected",...);abline(0,1,col=2,lwd=2)
legend("topleft",paste("lambda=",lambda))
}

### read in seleciton statistics (chi2 distributed)
s<-npyLoad("Demo2PCANGSD_2.selection.npy")

## make QQ plot to QC the test statistics
qqchi(s)

# convert test statistic to p-value
pval<-1-pchisq(s,1)

## read positions (hg38)
p<-read.table("Demo2PCANGSD_2.sites",colC=c("factor","integer"),sep="_")

names(p)<-c("chr","pos")

## make manhatten plot
plot(-log10(pval),col=p$chr,xlab="Chromosomes",main="Manhattan plot")


## zoom into region
 w<-range(which(pval<1e-7)) + c(-100,100)
 keep<-w[1]:w[2]
 plot(p$pos[keep],-log10(pval[keep]),col=p$chr[keep],xlab="HG38 Position chr2")

## see the position of the most significant SNP
 p$pos[which.max(s)]