# How is a principal component analysis for population structure made from a bam or other alignment file?

I'm curious how a PCA for population structure is built from an alignment/BAM or SAM file. How do programs like angsd and ngsCovar actually build a PCA? What exactly makes things come out as different and how can this be determined? If I have 3 alignments that look like this:

Locus1:

>Indiv1
ACGT
>Indiv2
ACGC
>Indiv3
ACGA


Locus2:

>Indiv1
GACT
>Indiv2
GACA
>Indiv3
GAGT


Locus3:

>Indiv1
GGTC
>Indiv2
GGCT
>Indiv3
GCTA


(These example alignments leave out quality scores for simplicity, assume that each has high quality across the board.)

What algorithm or system would be used to graph them on a PCA? How many dimensions would that PCA have? Is it possible to model this by hand so that I can understand what's going on/how it is built?

• Can you update your post to somewhere mention that this is related to population structure? There are other uses of PCA using BAM files that use completely unrelated methods (e.g., deepTools). Commented Dec 16, 2017 at 20:05

In general, the process works as such:

1. Variants are found at a number of loci in each sample (missing loci are often imputed).
2. Each sample is scored at each locus, often according to the number of copies of the reference allele that it has (so, 0-2).
3. The resulting matrix is used for PCA.
• @terdon "...according to the number of copies of the reference allele that it has (so, 0-2)." So the PCA is related to heterozygosity?
– 5r9n
Commented Dec 17, 2017 at 19:10
• You said each locus was scored often based on number of copies- what else could it be scored on?
– 5r9n
Commented Dec 17, 2017 at 19:13
• So you build a matrix (often) based on allele copy (0, 1, or 2) then make a covariance matrix and do associated eigendecomposition stuff to generate a PCA?
– 5r9n
Commented Dec 17, 2017 at 20:24
• One could consider making a numeric score for the genotype, such that each non-reference genotype got a different value (instead of them being all lumped into 0/1). I've not seen someone try that, so I don't know whether it's useful. Usually PCA is done via SVD these days, but really however you want to do it should lead to similarish results. Commented Dec 17, 2017 at 20:31