Module 7 · Plots and reporting · step 7 of 8
7.7Compare without PCs and without QC
Commands tested on Windows
In short
We repeat the association tests without genetic components, the numbers that summarise ancestry, and then without sample and variant quality checks as well. We compare counts and plots to see how these choices change the results.
Do this
- Paste in the RStudio ConsoleThe R command in the Command section below
You should see: Main analysis: 236,815 tests, eight hits, lambda about 1.016.
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.
1no_pc <- guided_path(cfg, "7.7", "no_pcs")run_plink(cfg, c("--pfile", guided_path(cfg, "5.7"), "--maf", "0.01", "--covar", guided_path(cfg, "6.3", "covariates.tsv"), "--covar-name", "Age", "--covar-variance-standardize", "--glm", "sex", "hide-covar", "firth-fallback", "--out", no_pc))2metadata <- guided_metadata(cfg)all_covar <- metadata[c("FID", "IID", "Age")]; names(all_covar)[1] <- "#FID"all_file <- guided_path(cfg, "7.7", "all_ages.tsv"); guided_write(all_covar, all_file)3no_qc <- guided_path(cfg, "7.7", "no_qc_no_pcs")baseline <- guided_path(cfg, "2.8")if (!file.exists(paste0(baseline, ".pgen"))) baseline <- file.path(cfg$data_dir, "checkpoints", "2.8", "state")4run_plink(cfg, c("--pfile", baseline, "--maf", "0.01", "--covar", all_file, "--covar-name", "Age", "--covar-variance-standardize", "--glm", "sex", "hide-covar", "firth-fallback", "--out", no_qc))5main <- guided_summary_stats(cfg)a <- guided_standardise_scan(guided_association(no_pc))b <- guided_standardise_scan(guided_association(no_qc))6k <- match(main$variant_id, a$variant_id)stopifnot(!anyNA(k), all(main$n == a$n[k]))7autosomal_p <- function(x) x$p_value[x$chromosome %in% as.character(1:22)]summarise <- function(x) c(finite_tests = sum(valid_p(x$p_value)), hits = sum(x$p_value[valid_p(x$p_value)] < 5e-8), lambda = lambda_gc(autosomal_p(x)))comparison <- rbind(main = summarise(main), no_PCs = summarise(a), no_sample_or_variant_QC_no_PCs = summarise(b))print(comparison)stopifnot(comparison[2, "hits"] == 15L, comparison[3, "hits"] == 19L)8guided_write(data.frame(model = rownames(comparison), comparison, row.names = NULL), guided_path(cfg, "7.7", "comparison.tsv"))# The order goes from the least adjusted analysis to the most adjusted one.9save_plot(guided_path(cfg, "7.7", "qq_comparison.png"), function() { par(mfrow = c(1, 3)) plotted <- list(autosomal_p(b), autosomal_p(a), autosomal_p(main)) limits <- c(0, max(-log10(unlist(plotted)))) plot_qq(autosomal_p(b), "No sample/variant QC, no PCs", ylim = limits) plot_qq(autosomal_p(a), "QC, no PCs", ylim = limits) plot_qq(autosomal_p(main), "QC + ten PCs", ylim = limits)}, width = 2100, height = 750)10manhattan_data <- function(x) { d <- data.frame(CHROM = suppressWarnings(as.numeric(x$chromosome)), POS = x$base_pair_location, P = x$p_value, LABEL = NA_character_) leads <- region_leads(d$CHROM, d$POS, d$P) d$LABEL[leads$index] <- guided_variant_labels(cfg, leads$chromosome, leads$position, x$variant_id[leads$index]) d}panels <- list(b = manhattan_data(b), a = manhattan_data(a), main = manhattan_data(main))11top <- max(8, -log10(unlist(lapply(panels, function(d) d$P[valid_p(d$P)]))))# Optional: suggestive = TRUE adds the dotted line at 5e-5 to every panel. max_labels sets how many regions get a label.12save_plot(guided_path(cfg, "7.7", "manhattan_comparison.png"), function() { par(mfrow = c(3, 1), mar = c(4, 4.5, 3, 1)) plot_manhattan(panels$b, "No sample/variant QC, no PCs", suggestive = FALSE, label_leads = TRUE, max_labels = 5, ylim = c(0, top)) plot_manhattan(panels$a, "QC, no PCs", suggestive = FALSE, label_leads = TRUE, max_labels = 5, ylim = c(0, top)) plot_manhattan(panels$main, "QC + ten PCs", suggestive = FALSE, label_leads = TRUE, max_labels = 5, ylim = c(0, top))}, width = 1800, height = 2400)- Note 1: Repeat the final cohort analysis using age and sex without genetic components.
- Note 2: Read the original sample information and save ages for all wells.
- Note 3: Choose the output name and find the cohort saved before quality checks.
- Note 4: Test the cohort before sample and variant quality checks using age and sex.
- Note 5: Read the main results and put the comparison results into matching columns.
- Note 6: Check that the main and cleaned comparison analyses use the same samples per variant.
- Note 7: Count valid tests and results below the genome-wide threshold and calculate lambda for each analysis.
- Note 8: Save the comparison table.
- Note 9: Save three QQ plots with the same scale to compare the analyses.
- Note 10: Prepare the same three analyses for Manhattan plots. Each lead variant gets its rs number as a label.
- Note 11: Use one vertical scale for all three Manhattan plots, so that the heights can be compared.
- Note 12: Save the Manhattan comparison, again from the least to the most adjusted analysis.
You need
The state from step 5.7, the state from step 2.8, the summary statistics file from step 6.8 for the main analysis, the covariates and the original sample information. The two comparison scans are new PLINK reports. R puts them into the same standard columns in memory. In this way, all three analyses are summarised in the same way.
What we do
Compare the main analysis, which we planned in advance, with two analyses that we deliberately make weaker.
Why
In the first comparison, we leave out the principal components (PCs) (opens in a new tab). PCs are the numbers that summarise ancestry. We keep the same cleaned cohort and the same covariates (opens in a new tab), age and sex. This shows the effect of the PC adjustment alone.
In the second comparison, we start from the state saved at module 2. It has the case and control labels and the corrected strands. We use no sample quality control (QC) (opens in a new tab), no variant QC and no PCs. This comparison changes the samples and the variants as well as the QC. We cannot give its differences to one single cause. The label "no QC" does not mean that we undid the strand correction or the case and control labels. When you interpret the differences, compare the shared variants and the number of samples for each variant.
Illustration in preparation
What each option does
Command 1: test the final cohort without genetic components
| Option or modifier | What it does |
|---|---|
| --pfile | Reads s5-7_state.pgen, s5-7_state.pvar and s5-7_state.psam, which hold the final cohort's DNA calls, variant information and sample information. |
| --maf | Tests only variants whose less common DNA version has a frequency of at least 0.01, or 1%, in the input cohort. |
| --covar | Reads s6-3_covariates.tsv and matches adjustment values to samples by their identifiers. |
| --covar-name | Selects only Age from the adjustment table, leaving out genetic component scores that summarise ancestry. |
| --covar-variance-standardize | Rescales age to have a mean of zero and a variance of one so the model calculation remains stable. |
| --glm | Fits a logistic model at each variant to estimate how each extra copy of the tested DNA version changes the odds of disease. The odds of disease are the chance of having the disease divided by the chance of not having it. |
| Modifier of --glm: sex | Adds recorded sex from the input sample file as an adjustment factor. |
| Modifier of --glm: hide-covar | Reports genetic tests without separate rows for the effects of age and sex. |
| Modifier of --glm: firth-fallback | Uses Firth correction, which gives usable estimates when very few people carry the allele, when the ordinary model calculation fails. |
| --out | Starts output file names with s7-7_no_pcs, including s7-7_no_pcs.Disease.glm.logistic.hybrid. |
Command 2: test the cohort before sample and variant quality checks
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the cohort before sample and variant quality checks from s2-8_state.pgen, s2-8_state.pvar and s2-8_state.psam. If the local PGEN file is absent, R uses checkpoints/2.8/state in the course data folder. |
| --maf | Tests only variants whose less common DNA version has a frequency of at least 0.01, or 1%, in the input cohort. |
| --covar | Reads s7-7_all_ages.tsv, which holds ages for all measured wells, and matches them to samples by their identifiers. |
| --covar-name | Selects only Age from the adjustment table, leaving out genetic component scores that summarise ancestry. |
| --covar-variance-standardize | Rescales age to have a mean of zero and a variance of one so the model calculation remains stable. |
| --glm | Fits a logistic model at each variant to estimate how each extra copy of the tested DNA version changes the odds of disease. The odds of disease are the chance of having the disease divided by the chance of not having it. |
| Modifier of --glm: sex | Adds recorded sex from the input sample file as an adjustment factor. |
| Modifier of --glm: hide-covar | Reports genetic tests without separate rows for the effects of age and sex. |
| Modifier of --glm: firth-fallback | Uses Firth correction, which gives usable estimates when very few people carry the allele, when the ordinary model calculation fails. |
| --out | Starts output file names with s7-7_no_qc_no_pcs, including s7-7_no_qc_no_pcs.Disease.glm.logistic.hybrid. |
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
Main analysis: 236,815 tests, eight hits, lambda about 1.016. Without PCs after QC: 236,815 tests, 15 hits, lambda about 1.546. Without sample or variant QC and without PCs: 239,647 tests, 19 hits, lambda about 1.655. The extra peaks do not show that worse QC finds more true causes.
The plots follow the order from the least adjusted analysis to the most adjusted one. All QQ plots share one vertical scale. All three Manhattan plots also share one vertical scale, so you can compare the heights of the peaks. Each plot has the genome-wide line at 5e-8, and the labels show the rs number of the lead variant of each region. The table below summarises the same numbers in that order.
| Analysis | Hits below 5e-8 | Lambda |
|---|---|---|
| No sample or variant QC, no PCs | 19 | about 1.655 |
| QC, no PCs | 15 | about 1.546 |
| QC and ten PCs (main analysis) | 8 | about 1.016 |
Without PCs, the same three peaks stay and some points reach a little higher. Without any QC, the plot also shows peaks on chromosomes 2 and 15 just above the line, and the lead variant on chromosome 12 changes to rs7310409. The label exm2265352 is an array name, because that variant has no rs number. A lead variant that appears only in the weaker analyses is a warning sign, not a discovery.
The tested PLINK version is set in config.R and recorded in
software_versions.txt. Validation: the executable chunks, the comparisons with the expected results and the final plot checks passed on Windows.
If something goes wrong
If something goes wrong
Stop at the first error. Check the input file that R names and the previous step. Keep the command and the messages from the console. Do not use files from a different version of the dataset. See troubleshooting (opens in a new tab).
Quick check
Can we interpret the comparison of the main analysis with the no-PC analysis more narrowly than the comparison with the no-QC analysis?
Show the answer
Yes. The first comparison keeps the same samples and the same number of samples for each variant. The second comparison also changes the samples and the tested sites.
Files created
Two comparison association outputs, the age table, s7-7_comparison.tsv, s7-7_qq_comparison.png and s7-7_manhattan_comparison.png.