##Load packages

library(vcfR)
## Warning: package 'vcfR' was built under R version 4.2.2
## 
##    *****       ***   vcfR   ***       *****
##    This is vcfR 1.13.0 
##      browseVignettes('vcfR') # Documentation
##      citation('vcfR') # Citation
##    *****       *****      *****       *****
library(ggplot2)
library(ggpubr)
library(vegan)
## Warning: package 'vegan' was built under R version 4.2.2
## Loading required package: permute
## Warning: package 'permute' was built under R version 4.2.2
## Loading required package: lattice
## This is vegan 2.6-4

Extract Data

getwd()
## [1] "C:/Users/Sean/Documents/CompBio/Final"
snps <- vcfR::read.vcfR(
  "16.22083576-22323576.ALL.chr16_GRCh38.genotypes.20170504.vcf.gz", 
  convertNA  = T)
## Scanning file to determine attributes.
## File attributes:
##   meta lines: 130
##   header_line: 131
##   variant count: 6206
##   column count: 2513
## 
Meta line 130 read in.
## All meta lines processed.
## gt matrix initialized.
## Character matrix gt created.
##   Character matrix gt rows: 6206
##   Character matrix gt cols: 2513
##   skip: 0
##   nrows: 6206
##   row_num: 0
## 
Processed variant 1000
Processed variant 2000
Processed variant 3000
Processed variant 4000
Processed variant 5000
Processed variant 6000
Processed variant: 6206
## All variants processed
snps_num <- vcfR::extract.gt(snps, 
                             element = "GT",
                             IDtoRowNames  = F,
                             as.numeric = T,
                             convertNA = T,)
snps_num_t <- t(snps_num)
snps_num_df <- data.frame(snps_num_t) 
sample <- row.names(snps_num_df)
snps_num_df <- data.frame(sample, snps_num_df)

Data Merging

pop_meta <- read.csv("1000genomes_people_info2-1.csv")
snps_num_df2 <- merge(pop_meta,
                      snps_num_df,
                      by = "sample",)
invar_omit <- function(x){
  cat("Dataframe of dim",dim(x), "processed...\n")
  sds <- apply(x, 2, sd, na.rm = TRUE)
  i_var0 <- which(sds == 0)

  cat(length(i_var0),"columns removed\n")
  
  if(length(i_var0) > 0){
     x <- x[,-i_var0]
  }
  return(x)                      
}
snps_no_invar <- data.frame(snps_num_df2[,c(1:6)],
                            invar_omit(snps_num_df2[,-c(1:6)]),
                            row.names = NULL)
## Dataframe of dim 2504 6206 processed...
## 1733 columns removed
find_NAs <- function(x){
  NAs_TF <- is.na(x)
  i_NA <- which(NAs_TF == TRUE)
  N_NA <- length(i_NA)
  return(i_NA)
}
N_rows <- nrow(snps_no_invar)
N_NA <- rep(x=0, times = N_rows)
N_SNPs <- ncol(snps_no_invar)

for(i in 1:N_rows){
  i_NA <- find_NAs(snps_no_invar[i,])
  N_NA_i <- length(i_NA)
  N_NA[i] <- N_NA_i
}

cutoff50 <- N_SNPs*0.5
percent_NA <- N_NA/N_SNPs*100
any(percent_NA > 50)
## [1] FALSE
mean(percent_NA)
## [1] 0

mean imputation

mean_imputation <- function(df) {
  n_cols <- ncol(df)
  for (i in 1:n_cols) {
    column_i <- df[,i]
    mean_i <- mean(column_i,
                   na.rm = T)
    NAs_i <- which(is.na(column_i))
    N_NAs <- length(NAs_i)
    column_i[NAs_i] <- mean_i
    df[,i] <- column_i
  }
  return(df)
}

snps_no_NA <- data.frame(snps_no_invar[,c(1:4)],
                         mean_imputation(snps_no_invar[,-c(1:4)]),
                         row.names = NULL)
snps_scaled <- snps_no_NA
snps_scaled[,-c(1:4)] <- scale(snps_no_NA[,-c(1:4)])
dim(snps_scaled)
## [1] 2504 4479
write.csv(snps_scaled, 
          file = "SNPs_cleaned.csv",
          row.names = F)