Portfolio › Barley PCA
Population Genomics · R

Principal Component Analysis of Genetic Variation in Barley

A reproducible R workflow to summarize genome-wide SNP variation and explore whether genetic variation in the analyzed barley panel aligns with row type.

VCFvcfRPCAggplot250K SNP

Principal component analysis (PCA) transforms correlated marker genotypes into orthogonal axes that summarize major patterns of genetic variation. In this project, each point represents a barley genotype; point colour represents its row type.

The workflow uses the filtered genotype matrix gt_qc produced by the shared VCF quality-control step. Its rows must be SNPs and its columns must be genotypes. Sample identifiers must match the phenotype table exactly before plotting.

ℹ️
Input requirement

Run the common genotype QC and sample-synchronization script first; refer to the LD project or the GAPIT GWAS project for the genotype QC and sample-alignment workflow.

01

Analysis workflow

From SNP matrix to interpretable ordination

SNP matrix
QC-filtered
→
PCA
prcomp()
→
Join traits
Row type
→
Plot
PC1 vs PC2
R

R code

Principal components and publication-style scatter plot

R · PCA
library(ggplot2)
library(dplyr)

# 1. Transpose the QC-filtered genotype matrix so individuals are rows and SNPs are columns
geno_pca <- t(gt_qc)

# 2. Calculate each SNP's standard deviation and remove invariant SNPs
#    because they cannot contribute to PCA and cause problems with scaling.
marker_sd <- apply(geno_pca, 2, sd, na.rm = TRUE)
geno_pca <- geno_pca[, is.finite(marker_sd) & marker_sd > 0, drop = FALSE]

# 3. Impute missing genotype values using the mean dosage for each SNP
for (j in seq_len(ncol(geno_pca))) {
  miss <- is.na(geno_pca[, j])
  if (any(miss)) geno_pca[miss, j] <- mean(geno_pca[, j], na.rm = TRUE)
}

# 4. Center and scale SNP dosages, then calculate principal components
pca <- prcomp(geno_pca, center = TRUE, scale. = TRUE)

# 5. Extract each individual's scores for the first two principal components
PC <- data.frame(
  Taxa = rownames(pca$x),
  PC1 = pca$x[, 1],
  PC2 = pca$x[, 2]
)

# 6. Calculate the percentage of total variance explained by each PC
pca_var <- summary(pca)$importance[2, ]
PC1_var <- round(100 * pca_var["PC1"], 2)
PC2_var <- round(100 * pca_var["PC2"], 2)

# 7. Standardize sample IDs and encode row type as a labeled factor
pheno_pca <- pheno %>%
  mutate(Taxa = as.character(Taxa),
         RowType = factor(RowType, levels = c(2, 6),
                          labels = c("Two-row", "Six-row")))

# 8. Join PCA scores to phenotype metadata by the individual ID
PC <- PC %>%
  mutate(Taxa = as.character(Taxa)) %>%
  inner_join(pheno_pca %>% select(Taxa, RowType), by = "Taxa")

# 9. Plot PC1 against PC2 and distinguish the two row types by colour
ggplot(PC, aes(PC1, PC2, colour = RowType)) +
  geom_point(size = 2.6, alpha = 0.85) +
  scale_colour_manual(values = c("Two-row" = "#2d5c42",
                                 "Six-row" = "#c8a84b")) +
  labs(title = "Population structure of the AM2017 barley panel",
       x = paste0("PC1 (", PC1_var, "%)"),
       y = paste0("PC2 (", PC2_var, "%)"),
       colour = "Barley row type") +
  theme_classic(base_size = 13) +
  theme(plot.title = element_text(hjust = 0.5, face = "bold"),
        axis.title = element_text(face = "bold"))
FIG

PCA results

Genetic differentiation by barley row type

Principal component analysis of the barley panel coloured by two-row and six-row type
Project PCA output for the supplied QC-filtered genotype matrix. In this plot, PC1 explains 9.73% and PC2 explains 4.15% of the variation.
02

Interpretation

What the PCA can and cannot establish

  • PC1 and PC2 labels show the percentage of marker variation explained by each axis.
  • The plot summarizes genome-wide genetic variation among the analyzed barley genotypes; any visible clustering is exploratory and should be interpreted cautiously.
  • Overlap between points does not prove that population structure is absent; additional PCs and relevant metadata may be examined for a fuller picture.
  • This PCA is a separate exploratory analysis. Row type was not included as a covariate in the GAPIT GWAS; the GWAS workflow uses the LEA sNMF ancestry estimate as its population-structure covariate.
REF

Reproducibility notes

⚠️
Check identifiers and scaling

Confirm that genotype sample names and pheno$Taxa match exactly. Scaling PCA variables can give rare or low-variance markers substantial influence; consider a centred-only PCA or a genetics-appropriate PCA method as a sensitivity analysis. Report the method actually used.