Module 5 · Population structure · step 4 of 7
5.4Reference PCA and study projection
Commands tested on Windows
In short
We calculate ten principal components in the reference, each a number summarising one pattern of genetic variation. We then project both groups onto these fixed reference axes, meaning we place them on the same scale so we can compare them.
Do this
- Paste in the RStudio ConsoleThe R command in the Command section below
You should see: The study projection contains 1,844 samples.
Command
Module setup, needed before this step
R: module setup
source("R/guided.R")cfg <- guided_init()Blue: PLINK option · Orange: value you can change · Green: file name
R command
Type or paste into the RStudio console.
1reference <- file.path(cfg$data_dir, "pca_reference", "ref1000g_grch38")common <- guided_path(cfg, "5.2", "common.txt")fit <- guided_path(cfg, "5.4", "reference")2run_plink(cfg, c("--pfile", reference, "--extract", common, "--freq", "--pca", "10", "allele-wts", "--out", fit))3weights <- guided_table(paste0(fit, ".eigenvec.allele"))id_column <- match("ID", names(weights)); allele_column <- match("A1", names(weights))first_pc <- match("PC1", names(weights))4score <- c("--read-freq", paste0(fit, ".afreq"), "--score", paste0(fit, ".eigenvec.allele"), id_column, allele_column, "header-read", "no-mean-imputation", "variance-standardize", "--score-col-nums", paste0(first_pc, "-", first_pc + 9L))5run_plink(cfg, c("--pfile", reference, "--extract", common, score, "--out", guided_path(cfg, "5.4", "ref_scores")))6run_plink(cfg, c("--pfile", guided_path(cfg, "4.6"), "--extract", common, score, "--out", guided_path(cfg, "5.4", "study_scores")))# Course bookkeeping: records the counts for the next step. Do not change.guided_artifact_checkpoint(cfg, "5.4", c(paste0(fit, c(".afreq", ".eigenvec", ".eigenval", ".eigenvec.allele")), paste0(guided_path(cfg, "5.4", "ref_scores"), ".sscore"), paste0(guided_path(cfg, "5.4", "study_scores"), ".sscore")), parent = "5.2")- Note 1: Set the reference input, shared site list and output names.
- Note 2: Calculate ten reference components and save their allele contributions.
- Note 3: Find the columns containing site names, alleles and component contributions.
- Note 4: Prepare the same reference frequencies and scoring rules for both groups.
- Note 5: Place the reference samples on the fixed reference axes.
- Note 6: Place the study samples on the same reference axes.
You need
The checked shared sites from step 5.3, and the full saved state from step 4.6.
What we do
Calculate the PCs in the reference only. Then place the reference samples and the study samples on the same axes.
Why
If we ran one PCA on reference and study together, a much larger study group could pull the axes towards itself. So we fix the axes on the reference first. Placing new samples on fixed axes is called projection (opens in a new tab).
Each site has an allele weight (called a loading), which says how much that allele contributes to a PC. The score of a person is the sum of these weights over the alleles the person carries. The reference and the study scores must use the same allele frequencies, the same weights and the same standardisation. Standardisation means rescaling each genotype by the allele frequency, so that common and rare alleles count fairly. The scale and the sign (plus or minus) of a PC are arbitrary. Compare patterns only when both sets of scores use the same scale.
The weight file names the counted allele in a column called A1 (opens in a new tab). We switch off mean imputation. This means a missing call is not replaced by the average, so the missing calls stay visible in the score.
What each option does
Command 1: calculate reference components and allele weights
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the reference genotype files pca_reference/ref1000g_grch38.pgen, .pvar and .psam from the course data folder. |
| --extract | Uses only the 48,614 shared sites listed in s5-2_common.txt. |
| --freq | Saves how common each allele is in the reference to s5-4_reference.afreq. |
| --pca | Calculates ten principal components, each a number per person summarising one pattern of genetic variation. |
| Modifier of --pca: allele-wts | Saves how much each allele contributes to each component in s5-4_reference.eigenvec.allele. |
| --out | Starts the reference output file names with s5-4_reference. |
Command 2: place reference samples on the fixed axes
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the reference genotype files pca_reference/ref1000g_grch38.pgen, .pvar and .psam from the course data folder. |
| --extract | Uses only the 48,614 shared sites listed in s5-2_common.txt. |
| --read-freq | Reads the reference allele frequencies from s5-4_reference.afreq so both groups use the same scale. |
| --score | Uses s5-4_reference.eigenvec.allele to calculate reference scores, matching variant names in column 2 and counted alleles in column 5. |
| Modifier of --score: header-read | Reads the first row of the allele weight file as column names. |
| Modifier of --score: no-mean-imputation | A missing genotype adds nothing to a person's score, instead of being filled in from the allele frequency. |
| Modifier of --score: variance-standardize | Rescales genotypes using the reference allele frequencies so common and rare alleles contribute on a comparable scale. |
| --score-col-nums | Uses columns 6 to 15 of the allele weight file to calculate scores for PC1 to PC10. |
| --out | Starts the reference score output file names with s5-4_ref_scores. |
Command 3: place study samples on the fixed axes
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the cleaned study genotype files s4-6_state.pgen, .pvar and .psam from the results folder. |
| --extract | Uses only the 48,614 shared sites listed in s5-2_common.txt. |
| --read-freq | Reads the reference allele frequencies from s5-4_reference.afreq so both groups use the same scale. |
| --score | Uses s5-4_reference.eigenvec.allele to calculate study scores, matching variant names in column 2 and counted alleles in column 5. |
| Modifier of --score: header-read | Reads the first row of the allele weight file as column names. |
| Modifier of --score: no-mean-imputation | A missing genotype adds nothing to a person's score, instead of being filled in from the allele frequency. |
| Modifier of --score: variance-standardize | Rescales genotypes using the reference allele frequencies so common and rare alleles contribute on a comparable scale. |
| --score-col-nums | Uses columns 6 to 15 of the allele weight file to calculate scores for PC1 to PC10. |
| --out | Starts the study score output file names with s5-4_study_scores. |
Every command also uses --threads 2 (PLINK runs at most two tasks at the same time) and --memory 2048 (PLINK uses at most about 2 GB of memory).
PLINK terminal equivalent. Optional: you do not need it for the course.
Blue: PLINK option · Orange: value you can change · Green: file name
Terminal equivalent: plain PLINK 2
Select any option to see what it does. Do not paste terminal commands into the R console.
bin/plink2
bin/plink2
bin/plink2
No option selected. Each underlined part of the command is an option you can open.
Use bin\plink2.exe on Windows.
Expected result
Show the expected result
Reference eigenvectors and eigenvalues, allele weights, and two .sscore files (score tables) with the columns PC1_AVG to PC10_AVG. The study projection contains 1,844 samples.
The tested PLINK version is defined in config.R and recorded in
software_versions.txt. Validation: executable chunks and the expected counts passed on Windows.
If something goes wrong
If something goes wrong
Stop at the first error message. Check the input file named in the message and the previous step. Keep the command and the console log. Do not use files from a different dataset version. See troubleshooting (opens in a new tab).
Quick check
Why must the study projection use the reference frequencies, and not recalculate the standardisation from the study alone?
Show the answer
Otherwise the two sets of scores would not share the same coordinate system.
Files created
s5-4_reference.*, s5-4_ref_scores.sscore, s5-4_study_scores.sscore and logs.
Under the hood & literature
Under the hood & literature
What the tool computes. --pca 10 allele-wts finds the top ten principal components of the reference genotypes after each variant is shifted to mean 0 and scaled to variance 1 with its allele frequency. The .eigenvec.allele file has one line per allele. After the variant ID and A1, each PC column is the weight of that allele on that component. --score then gives each person the average of weight times standardised genotype over the shared alleles. The _AVG columns are averages, so their scale differs from the .eigenvec file. Use the projected scores for both groups.
Why these values. Ten components is PLINK's default and a course convention. Step 5.7 fixes how many enter the model. --read-freq gives both groups the same frequencies. no-mean-imputation leaves missing calls out of the average.
Pitfalls. Projected study samples shrink towards the origin, and the bias grows from PC5 onwards, so read distances on later PCs with care. An allele that does not match by name contributes nothing, so the score silently uses fewer sites. At an A/T or C/G site, a strand flip keeps the name but reverses the meaning and flips the sign of its contribution. This is why the shared sites are checked at step 5.3. A person from an ancestry absent from the reference tends to land near the closest reference group. The spelling variance-standardize is American; the course text says standardise.
References
- Patterson N et al. (2006) Population structure and eigenanalysis. PLoS Genet 2:e190. PMID 17194218.
- Price AL et al. (2006) Principal components analysis corrects for stratification in genome-wide association studies. Nat Genet 38:904-909. PMID 16862161.
- Privé F et al. (2020) Efficient toolkit implementing best practices for principal component analysis of population genetic data. Bioinformatics 36:4449-4457. PMID 32415959.
- The 1000 Genomes Project Consortium (2015) A global reference for human genetic variation. Nature 526:68-74. PMID 26432245.