Skip to content

Measure genotype correlation and prune markers

Nearby variants can carry similar genotype patterns because chromosome segments share a recombination history. Their association statistics can therefore move together. Counting each significant marker as independent biological evidence would ignore that dependence.

This lesson calculates squared correlations between unphased ALT dosage vectors. PLINK calls the same statistic --r2-unphased. The source genotypes retain phase, but neither calculation below estimates LD from phased haplotype frequencies.

The generating variant is the first eligible chromosome 22 SNP at or above GRCh38 position 30,000,000, with REF and ALT as lexical tie breakers. The workflow freezes that choice before phenotype generation. The region extends 250 kb on each side and contains only variants from the complete association universe.

Both languages calculate correlation with that anchor for every regional marker and compare PLINK’s report. The heatmap shows the 31 closest eligible markers in coordinate order. Their selection uses distance, not an association result.

The calculations pool the retained people. Their allele frequencies and correlations depend on this cohort’s composition; the displayed LD is not a universal reference for every source population.

The genotype data come from Byrska-Bishop and colleagues and NYGC. The collection acknowledgment credits Coriell samples and NHGRI grants 3UM1HG008901-03S1 and 3UM1HG008901-04S2. This lesson calculates its own teaching summaries from the declared source selection.

source("/shared/population_utils.R")
accepted <- ReadAnalysis()
variants <- ReadTable("variant_universe.tsv.gz")
dosage <- ReadTable("regional_dosages.tsv.gz")
region <- MarkerIds("regional_markers.txt")
generating <- accepted$panel$generating_variant
stopifnot(generating %in% region)
genotypes <- as.matrix(dosage[region])
stopifnot(all(genotypes %in% 0:2))
anchor <- genotypes[, generating]
r_squared <- as.numeric(cor(genotypes, anchor)^2)
result <- data.frame(ID = region, r_squared = r_squared)
primary <- ReadTable("regional_ld.vcor")
stopifnot(all(primary$ID_A == generating | primary$ID_B == generating))
other <- ifelse(primary$ID_A == generating, primary$ID_B, primary$ID_A)
stopifnot(!anyDuplicated(other), setequal(other, setdiff(region, generating)))
matched <- result$r_squared[match(other, result$ID)]
stopifnot(all(abs(matched - primary$UNPHASED_R2) < 5e-6 * matched + 1e-8))
region_variants <- variants[match(region, variants$ID), ]
distance <- abs(region_variants$POS - accepted$panel$generating_position)
nearest <- order(distance, region_variants$POS,
region_variants$REF, region_variants$ALT)[seq_len(31)]
nearest <- nearest[order(region_variants$POS[nearest],
region_variants$REF[nearest],
region_variants$ALT[nearest])]
heatmap <- cor(genotypes[, nearest, drop = FALSE])^2
dir.create("outputs", showWarnings = FALSE)
write.table(result, "outputs/regional_ld_r.tsv", sep = "\t",
row.names = FALSE, quote = FALSE)
write.table(heatmap, "outputs/heatmap_r.tsv", sep = "\t", quote = FALSE,
col.names = NA)
palette <- hcl.colors(100, "Viridis")
png("outputs/linkage_r.png", width = 1050, height = 850, res = 130)
layout(matrix(c(1, 2), nrow = 1), widths = c(5, 1))
par(mar = c(4, 4, 4, 1))
image(seq_len(31), seq_len(31), heatmap, col = palette, zlim = c(0, 1),
xlab = "Ordered local marker index", ylab = "Ordered local marker index",
main = "The 31 closest eligible chromosome 22 markers")
par(mar = c(4, 1, 4, 4))
image(1, seq(0, 1, length.out = 100),
matrix(seq(0, 1, length.out = 100), nrow = 1),
col = palette, axes = FALSE, xlab = "", ylab = "", main = expression(r^2))
axis(4, at = seq(0, 1, by = 0.25))
invisible(dev.off())
candidates <- MarkerIds("pca_candidates.txt")
retained <- MarkerIds("ld_pruning.prune.in")
excluded <- MarkerIds("ld_pruning.prune.out")
stopifnot(!length(intersect(retained, excluded)))
stopifnot(setequal(c(retained, excluded), candidates))
chromosome <- variants$CHROM[match(candidates, variants$ID)]
stopifnot(all(chromosome %in% 1:21))
before <- tabulate(chromosome, nbins = 21)
after <- tabulate(variants$CHROM[match(retained, variants$ID)], nbins = 21)
pruning <- data.frame(CHROM = 1:21, before = before, retained = after)
write.table(pruning, "outputs/pruning_r.tsv", sep = "\t", row.names = FALSE,
quote = FALSE)
png("outputs/pruning_r.png", width = 1300, height = 720, res = 130)
barplot(rbind(after, before - after), names.arg = 1:21,
col = c("#167c80", "#cbd1d5"), border = NA,
xlab = "Chromosome", ylab = "Sparse markers",
main = "PCA markers before and after the declared LD rule")
legend("topright", c("Retained", "Pruned"),
fill = c("#167c80", "#cbd1d5"), bty = "n")
invisible(dev.off())
metrics <- list(regional_markers = length(region),
candidate_markers = length(candidates),
retained_markers = length(retained),
excluded_markers = length(excluded),
maximum_ld = max(r_squared), heatmap_markers = 31)
WriteMetrics(metrics)
cat(sprintf("The ALT dosage comparison checks %d regional markers.\n",
length(region)))
cat(sprintf("The PCA rule retains %d of %d candidate markers.\n",
length(retained), length(candidates)))
cat("The PCA candidate set contains chromosomes 1 through 21 only.\n")
The ALT dosage comparison checks 965 regional markers.
The PCA rule retains 23356 of 27007 candidate markers.
The PCA candidate set contains chromosomes 1 through 21 only.

The R heatmap shows unphased dosage correlation among 31 preselected local markers.

The diagonal equals one because each marker is correlated with itself. The matrix preserves coordinate order, and its colors represent squared correlation.

The R pruning figure counts retained and removed markers by chromosome.

The primary rule uses a 1,000-kb window, one-marker steps, and an r squared threshold of 0.2. It does not promise that every pair across the genome has correlation below that threshold.

  • PLINK LD and marker pruning

    The manual distinguishes unphased dosage correlation from phased haplotype estimates. It also explains the window, step, and threshold arguments that produce separate retained and excluded marker lists.

  • PLINK population structure

    The PCA documentation explains why correlated markers can dominate a component. This motivates the separate pruned covariate panel without changing the association universe.