Module 5 · Population structure · step 7 of 7
5.7Cohort PCs and how many to include
Commands tested on Windows
In short
We select common sites with weaker correlations and calculate ten principal components, each a number summarising one pattern of genetic variation within the retained study group. We plot the variation each explains and save a complete checked starting point for module 6.
Do this
- Paste in the RStudio ConsoleThe R command in the Command section below
You should see: Ten cohort PCs, a scree plot and a complete saved state for step 5.7 with 1,811 samples and 238,513 variants.
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.
1regions <- guided_high_ld(cfg)prune <- guided_path(cfg, "5.7", "prune")2run_plink(cfg, c("--pfile", guided_path(cfg, "5.6"), "--chr", "1-22", "--maf", "0.05", "--exclude", "bed1", regions, "--indep-pairwise", "200kb", "0.2", "--out", prune))3report <- guided_path(cfg, "5.7", "pca")run_plink(cfg, c("--pfile", guided_path(cfg, "5.6"), "--extract", paste0(prune, ".prune.in"), "--pca", "10", "--out", report))4eigenvalues <- scan(paste0(report, ".eigenval"), quiet = TRUE)png(guided_path(cfg, "5.7", "scree.png"), width = 1000, height = 700, res = 130)plot(seq_along(eigenvalues), eigenvalues, type = "b", pch = 16, xlab = "PC number", ylab = "Eigenvalue", main = "Retained cohort: first ten PCs")5invisible(dev.off())cat("The plot was saved to ", guided_path(cfg, "5.7", "scree.png"), "\n", sep = "")# Course bookkeeping: records the counts for the next step. Do not change.guided_snapshot(cfg, "5.7", guided_path(cfg, "5.6"), 1811, 238513, parent = "5.6")file.copy(paste0(report, c(".eigenvec", ".eigenval")), file.path(cfg$out, "checkpoints", "5.7"), overwrite = TRUE)check_step("5.7", data_dir = cfg$out, work_dir = cfg$out)- Note 1: Prepare the regions to exclude and the site selection output name.
- Note 2: Select common sites on chromosomes 1 to 22 with weaker correlations with each other.
- Note 3: Calculate ten components within the retained study group using the selected sites.
- Note 4: Read how much variation each component explains and plot these values.
- Note 5: Finish saving the picture and print its location.
You need
The retained cohort from step 5.6, and the list of GRCh38 long-range LD regions to exclude.
What we do
Calculate new PCs in the retained study cohort, look at the eigenvalues and save the full state.
Why
The reference axes describe broad differences between world regions. PCs calculated inside the cohort can capture finer differences that matter for association. We prune the sites again, because the cohort has changed. We keep only common sites, which means a minor allele frequency (opens in a new tab) (MAF, the frequency of the rarer allele) of at least 0.05.
An eigenvalue (opens in a new tab) measures how much variation one PC explains. A scree plot (opens in a new tab) shows the eigenvalues in order, from the largest to the smallest.
The course fixes the number of PCs at ten for the main model. We do not choose the number to get attractive p-values. Too few PCs can leave confounding in the results. Too many PCs can reduce the precision of the estimates. In module 7 we compare other numbers of PCs as a sensitivity check. We do not use that check to search for the most significant result.
Illustration in preparation
What each option does
Command 1: select common sites with weaker correlations
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the retained study genotype files s5-6_state.pgen, .pvar and .psam from the results folder. |
| --chr | Uses only chromosomes 1 to 22, the autosomes. |
| --maf | Keeps sites whose less common allele has a frequency of at least 5%. |
| --exclude | Leaves out sites within the regions listed in s3-4_high_ld.bed1, where distant sites can carry similar genetic information. |
| Modifier of --exclude: bed1 | Reads region positions counted from 1 and includes both ends of each region. |
| --indep-pairwise | Selects sites with weaker correlations within windows of 200 kb (200,000 DNA bases). It drops one site of every pair with a squared correlation (r²) above 0.2. The value r² runs from 0 to 1, and a larger value means that the genotypes of the two sites vary together more closely. |
| --out | Starts the site selection output file names with s5-7_prune. |
Command 2: calculate components within the retained study group
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the retained study genotype files s5-6_state.pgen, .pvar and .psam from the results folder. |
| --extract | Uses only the sites listed in s5-7_prune.prune.in, selected to have weaker correlations with each other. |
| --pca | Calculates ten principal components, each a number per person summarising one pattern of genetic variation in the retained study group. |
| --out | Starts the component output file names with s5-7_pca. |
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
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
Ten cohort PCs, a scree plot and a complete saved state for step 5.7 with 1,811 samples and 238,513 variants. The first cohort eigenvalue is about 14. The signs of the eigenvectors can differ on your computer. This does not change how the PCs adjust the association. R saves the picture as a PNG file and prints its path. It does not show the picture in the console. To see it, open the file from the Files pane of RStudio (click the file name, then choose "View File").
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
Should we choose the number of PCs by trying many models and keeping the one with the most genome-wide significant hits?
Show the answer
No. Fix the main model before you see any association result. Report the sensitivity analyses openly.
Files created
Pruning lists, eigenvectors and eigenvalues, the scree plot (PNG), the working state and the full 5.7 checkpoint.