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.6-4
library(ggplot2)
library(ggpubr)

Set the working directory

getwd()
## [1] "C:/Users/Fatsn/OneDrive - University of Pittsburgh/CBioFinal"

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: Scale The Data

TODO: The Standard deviation of the dataset to 0 in prepaeration of PCS analysis.

SNPs_scaled <- scale(SNPs_cleaned)

TODO: PCA

TODO: Run the PCA with prcomp()

pca_scaled <- prcomp(SNPs_scaled)

TODO: Screeplot

TODO: The Screeplot tells us how important each PCA is.

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

TODO: PC1 is the only relevant PC.

TODO: Variation

TODO: We can see the variation from the 10 PC.

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: PC1 has the greatest variance, which makes sense since it is the most important.

TODO: Biplot

TODO: biplot() is uses to create a biplot of the scaled PCA data.

biplot(pca_scaled)

TODO: Since there is alot of dimensions of the data, the data overlaps and the biplot doesnt give us any gppd information.

TODO: Extract

TODO: Extracting scores of the scaled PC data using the vegan package.

pca_scores <- vegan::scores(pca_scaled)

TODO: EXPLAIN

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: Assign population IDs to theextracted scores to then create a dataframe.

pca_scores2 <- data.frame(pop_id,
                              pca_scores)

TODO: PCA Plot

TODO: Use ggscatter to make a scatterplot with PC1 and PC2. The color and shape are set as the population ID.

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 = "GIVE ME A TITLE")

TODO: 3 distinc groups are formed. The Alt and Nel group form near -10 of Pc1 and go from 0 to 10 of PC2. The sub group is around -10 for both PCs and the Cau and Div group are all the way to the right of the scatterplot. The group is around 20 of PC1 and 0 for PC2.