Introduction

The example is split into 2 Parts:

Part 1 must be completed first to create a file, SNPs_cleaned.csv, that has been completely prepared for analysis.

Now in Part 2, you will analyze the data with PCA. The steps here will be:

  1. Center the data (scale())
  2. Run a PCA analysis (prcomp())
  3. Evaluate the scree plot from the PCA (screeplot())
  4. Evaluate the amount of variation explained by the first 2 PCs.
  5. Extract the PCA scores for plotting (vegan::scores())
  6. Plot the data

Tasks

In the code below all code is provided. Your tasks will be to do 4 things:

  1. Give a meaningful title to all sections marked “TODO: TITLE”
  2. Write 1 to 2 sentences describing what is being done and why in all sections marked “TODO: EXPLAIN”
  3. Add titles and axes to plots in all sections marked “TODO: UPDATE PLOT”
  4. Write 1 or 2 sentences interpreting the output from R in all sections marked “TODO: INTERPRET”

Preliminaries

Load the vcfR package with library()

library(vcfR) # KEY
## 
##    *****       ***   vcfR   ***       *****
##    This is vcfR 1.13.0 
##      browseVignettes('vcfR') # Documentation
##      citation('vcfR') # Citation
##    *****       *****      *****       *****
library(vegan)
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.5-7
library(ggplot2)
library(ggpubr)

Set the working directory

getwd()
## [1] "/Users/austineastmure/Desktop/comp_bio/Final Project"

Load the data

SNPs_cleaned <- read.csv(file = "SNPs_cleaned.csv")

warning("If this didn't work, its may be because you didn't set your working directory.")
## Warning: If this didn't work, its may be because you didn't set your working
## directory.

Data analysis

TODO: TITLE

TITLE: Scaling the data for PCA TODO: EXPLAIN EXPLAIN: Scales the data by centering it so it can be used for PCA analysis.

SNPs_scaled <- scale(SNPs_cleaned)

TODO: TITLE

TITLE: PCA analysis TODO: EXPLAIN EXPLAIN: Runs a PCA analysis on the data. This reduces the dimensionality of the dataset so it can be viewed in a cleaner manner.

pca_scaled <- prcomp(SNPs_scaled)

TODO: TITLE

TITLE: Create a screeplot TODO: EXPLAIN EXPLAIN: Creating a screeplot gives the user an idea of what PC dimensions would be considered relevant for analysis. TODO: UPDATE PLOT WITH TITLE

screeplot(pca_scaled, 
          ylab  = "Relative importance",
          main = "PCA dimensions")

TODO: INTERPRET SCREEPLOT Screeplot shows that only PC1 and PC2 are relevant for analysis ### TODO: TITLE TITLE: Evaluating the variance with PC1 and PC2 TODO: EXPLAIN EXPLAIN: Evaluation of variance is important because it allows users to understand what PC variables are relevant for analysis.

summary_out_scaled <- summary(pca_scaled)
PCA_variation <- function(pca_summary, PCs = 2){
  var_explained <- pca_summary$importance[2,1:PCs]*100
  var_explained <- round(var_explained,1)
  return(var_explained)
}
var_out <- PCA_variation(summary_out_scaled,PCs = 10)
N_columns <- ncol(SNPs_scaled)
barplot(var_out,
        main = "Percent variation Scree plot",
        ylab = "Percent variation explained")
abline(h = 1/N_columns*100, col = 2, lwd = 2)

TODO: INTERPRET VARIANCE EXPLAINED

TODO: TITLE

Graphing the PCA analysis TODO: EXPLAIN This will create a biplot of the data found in PC1 and PC2. This is done to identify any correlations within the data.

biplot(pca_scaled)

TODO: EXPLAIN WHY THIS IS A BAD IDEA This is a bad idea because, since there is so much data, it creates an unreadable graph.

TODO: TITLE

TITLE: Generating PCA scores for Plotting TODO: EXPLAIN EXPLAIN: Scores will be used for plotting, scores are generated here.

pca_scores <- vegan::scores(pca_scaled)

TODO: EXPLAIN EXPLAIN:Population id vector is generated to match with scores

pop_id <- c("Nel","Nel","Nel","Nel","Nel","Nel","Nel","Nel",
"Nel", "Nel", "Nel", "Nel", "Nel", "Nel", "Nel", "Alt",
"Alt", "Alt", "Alt", "Alt", "Alt", "Alt", "Alt", "Alt",
"Alt", "Alt", "Alt", "Alt", "Alt", "Alt", "Sub", "Sub",
"Sub", "Sub", "Sub", "Sub", "Sub", "Sub", "Sub", "Sub",
"Sub", "Cau", "Cau", "Cau", "Cau", "Cau", "Cau", "Cau",
"Cau", "Cau", "Cau", "Cau", "Cau", "Div", "Div", "Div",
"Div", "Div", "Div", "Div", "Div", "Div", "Div", "Div",
"Div", "Div", "Div", "Div")

TODO: EXPLAIN EXPLAIN: Object created that has population ids and their respective PCA scores

pca_scores2 <- data.frame(pop_id,
                              pca_scores)

TODO: TITLE

TITLE: Plotting the PCA scores TODO: EXPLAIN EXPLAIN: The PCA scores are plotted (PC1 and PC2) with their matching population ID. This is a cleaner version of the biplot and allows the user to draw conclusions from the data. TODO: UPDATE PLOT WITH TITLE TODO: UPDATE X and Y AXES WITH AMOUNT OF VARIATION EXPLAINED

ggpubr::ggscatter(data = pca_scores2,
                  y = "PC2",
                  x = "PC1",
                  color = "pop_id",
                  shape = "pop_id",
                  xlab = "PC1 (19.9% variation)",
                  ylab = "PC2 (2.3% variation)",
                  main = "SNP Relation Between Major Populations of Songbirds")

TODO: INTERPRET PLOT Songbird populations Div and Cau are more related than other groups of songbirds.