Last updated: 2020-02-14

Checks: 7 0

Knit directory: IITA_2019GS/

This reproducible R Markdown analysis was created with workflowr (version 1.5.0.9000). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20191121) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    data/.DS_Store
    Ignored:    output/.DS_Store

Untracked files:
    Untracked:  analysis/GetGainEst.Rmd
    Untracked:  workflowr_log.R

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
Rmd 6a4d569 wolfemd 2020-02-14 Add box and barplots of genetic gain based on GEBVs.
html f6e2d1f wolfemd 2020-02-13 Build site.
html 84cab28 wolfemd 2019-11-21 Build site.
html 57b19a9 wolfemd 2019-11-21 Build site.
html cb61b89 wolfemd 2019-11-21 Build site.
html 70242a6 wolfemd 2019-11-21 Build site.
html dacbcf9 wolfemd 2019-11-21 Build site.
html 43e9d5d wolfemd 2019-11-21 Build site.
html a869b9e wolfemd 2019-11-21 Build site.
Rmd bfffb51 wolfemd 2019-11-21 Publish the first set of analyses and files for IITA 2019 GS,

Prep. data for genomic prediction

Load snps

rm(list=ls()); gc()
library(tidyverse); library(magrittr); 
snps<-readRDS(file=paste0("/workdir/ImputationStageIII_72619/",
                          "DosageMatrix_RefPanelAndGSprogeny_ReadyForGP_73019.rds"))

Load blups

blupsNoOut<-readRDS(file="data/iita_blupsForCrossVal_outliersRemoved_73019.rds")
blups<-readRDS(file="data/iita_blupsForCrossVal_72619.rds")

blups<-blupsNoOut %>% 
  dplyr::select(Trait,blups) %>% 
  mutate(OutliersRemoved=TRUE) %>% 
  bind_rows(
    blups %>% 
      filter(Dataset=="HistoricalDataIncluded" | Trait %in% c("BRNHT1","PLTHT")) %>% 
      dplyr::select(Trait,blups) %>% 
      mutate(OutliersRemoved=FALSE))

Match TMS18F germNames to DArT genos

For the TMS18F, there are some phenotypes available now.

gidsInPhenos<-tibble(GID=blups %>% unnest() %$% GID %>% unique)
table(gidsInPhenos$GID %in% rownames(snps)) # 6629 (that only includes GG through C3)

# gidsInPhenos$GID %>% grep("TMS18",.,value=T) %>% length # 1452
# rownames(snps) %>% grep("TMS18",.,value=T) %>% length # 2420
gidsInPhenos %<>% 
  left_join(
    tibble(dartName=rownames(snps) %>% grep("TMS18",.,value=T)) %>% 
      separate(dartName,c("GID","dartID"),"_",extra = 'merge',remove = F) %>% 
      semi_join(gidsInPhenos) %>% 
      group_by(GID) %>% 
      slice(1) %>% 
      ungroup()) %>% 
  mutate(NewName=ifelse(!is.na(dartName),dartName,GID))

blups %<>% 
  unnest(blups) %>% 
  left_join(gidsInPhenos) %>% 
  mutate(GID=NewName) %>% 
  dplyr::select(-dartName,-dartID,-NewName) %>% 
  group_by(Trait,OutliersRemoved) %>% 
  nest(.key = "trainingData")

Make a kinship matrix including C4

Subset snps

table(gidsInPhenos$NewName %in% rownames(snps))
iitaSamples2keep<-union(rownames(snps) %>% .[. %in% gidsInPhenos$NewName],
                        rownames(snps) %>% grep("TMS18",.,value = T)) #%>% length # 9049
snps<-snps[iitaSamples2keep,]

MAF>1% filter

maf_filter<-function(snps,thresh){
  freq<-colMeans(snps, na.rm=T)/2; maf<-freq;
  maf[which(maf > 0.5)]<-1-maf[which(maf > 0.5)]
  snps1<-snps[,which(maf>thresh)];
  return(snps1) }
snps %<>% maf_filter(.,0.01)
dim(snps) # [1]  9049 68029

Make kinships

library(tidyverse); library(magrittr); library(rrBLUP)
K<-rrBLUP::A.mat(snps-1)

saveRDS(K,file=paste0("/workdir/marnin/IITA_2019GS/",
                      "Kinship_IITA_SamplesForGP_73119.rds"))
saveRDS(snps,file=paste0("/workdir/marnin/IITA_2019GS/",
                         "DosageMatrix_IITA_SamplesForGP_73119.rds"))
rm(snps); gc()

PCA

On cbsurobbins

pca<-prcomp(snps, scale=T, center=T) 
pc_scores<-pca$x %>% 
  .[,1:15] %>% 
  as.data.frame %>% 
  rownames_to_column(var="FullSampleName") %>% 
  mutate(GeneticGroup=NA,
         GeneticGroup=ifelse(grepl("TMS13F|2013_",FullSampleName),"TMS13F",
                             ifelse(grepl("TMS14F",FullSampleName),"TMS14F",
                                    ifelse(grepl("TMS15F",FullSampleName),"TMS15F",
                                           ifelse(grepl("TMS18F",FullSampleName),"TMS18F","GGetc")))))
summary(pca)$importance[,1:10] 

First two PCs only explain about 9% of the genetic variance.

Save PCA results

saveRDS(pc_scores,file="output/PCscores_IITA_SamplesForGP_73119.rds")
saveRDS(pca,file="/workdir/marnin/IITA_2019GS/PCA_IITA_SamplesForGP_73119.rds")
rm(pca); gc()
rm(list=ls());gc()
          used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
Ncells  605648 32.4    1271975   68         NA  1062694 56.8
Vcells 1157548  8.9    8388608   64     102400  2071651 15.9
library(tidyverse); library(magrittr); library(cowplot); 
pc_scores<-readRDS("output/PCscores_IITA_SamplesForGP_73119.rds")
library(viridis)
pc1v2<-pc_scores %>% 
  ggplot(.,aes(x=PC1,y=PC2,color=GeneticGroup)) + 
  geom_point() + 
  theme_bw() + scale_color_viridis_d() + 
  labs(x="PC1 (6%)",y="PC2 (3%)") + theme(legend.position = 'none')
pc3v4<-pc_scores %>% 
  ggplot(.,aes(x=PC3,y=PC4,color=GeneticGroup)) + 
  geom_point() + 
  theme_bw() + scale_color_viridis_d() + 
  labs(x="PC3 (3%)",y="PC4 (3%)")
plot_grid(pc1v2,pc3v4)

Version Author Date
a869b9e wolfemd 2019-11-21

Prediction

On cbsulm16

Load data for pred

rm(list=ls()); gc()
library(tidyverse); library(magrittr); 
K<-readRDS(file=paste0("/workdir/IITA_2019GS/Kinship_IITA_SamplesForGP_73119.rds"))
blupsNoOut<-readRDS(file="data/iita_blupsForCrossVal_outliersRemoved_73019.rds")
blups<-readRDS(file="data/iita_blupsForCrossVal_72619.rds")

blups<-blupsNoOut %>% 
  dplyr::select(Trait,blups) %>% 
  mutate(OutliersRemoved=TRUE) %>% 
  bind_rows(
    blups %>% 
      filter(Dataset=="HistoricalDataIncluded" | Trait %in% c("BRNHT1","PLTHT")) %>% 
      dplyr::select(Trait,blups) %>% 
      mutate(OutliersRemoved=FALSE))
gidsInPhenos<-tibble(GID=blups %>% unnest() %$% GID %>% unique)
gidsInPhenos %<>% 
  left_join(
    tibble(dartName=rownames(K) %>% grep("TMS18",.,value=T)) %>% 
      separate(dartName,c("GID","dartID"),"_",extra = 'merge',remove = F) %>% 
      semi_join(gidsInPhenos) %>% 
      group_by(GID) %>% 
      slice(1) %>% 
      ungroup()) %>% 
  mutate(NewName=ifelse(!is.na(dartName),dartName,GID))
blups %<>% 
  unnest(blups) %>% 
  left_join(gidsInPhenos) %>% 
  mutate(GID=NewName) %>% 
  dplyr::select(-dartName,-dartID,-NewName) %>% 
  filter(GID %in% rownames(K)) %>% 
  group_by(Trait,OutliersRemoved) %>% 
  nest(.key = "trainingData")

Get GEBVs

require(sommer)
require(furrr)
options(future.globals.maxSize= 1500*1024^2)
options(mc.cores=18)
plan(multiprocess)
proctime<-proc.time()
blups %>% 
  mutate(GEBV=future_pmap(.,function(Trait,trainingData,OutliersRemoved,...){
    trndata<-trainingData %>% 
      mutate(GID=factor(GID,levels=rownames(K)))
    fit <- mmer(fixed = drgBLUP ~1,
                random = ~vs(GID,Gu=K),
                weights = WT,
                data=trndata)
    x<-fit$U$`u:GID`$drgBLUP 
    gebvs<-tibble(GID=names(x),
                  GEBV=as.numeric(x))
    saveRDS(gebvs,file=paste0("output/",
                              "GEBV_",Trait,"_OutliersEqual",OutliersRemoved,"_IITA_73119.rds"))
    rm(fit); gc()}))
proc.time() - proctime

Format GEBV

gebvs<-list.files(path = "output/") %>% 
  grep("GEBV_",.,value = T) %>% 
  grep("_IITA_73119.rds",.,value = T) %>% 
  tibble(Files=paste0("output/",.)) %>% 
  mutate(Trait=gsub("output/GEBV_","",Files),
         Trait=gsub("_IITA_73119.rds","",Trait)) %>% 
  separate(Trait,c("Trait","OutliersRemoved"),"_OutliersEqual") %>% 
  mutate(Files=map(Files,~readRDS(.))) %>% 
  unnest(Files) %>% 
  mutate(GeneticGroup=NA,
         GeneticGroup=ifelse(grepl("TMS13F|2013_",GID),"TMS13F",
                             ifelse(grepl("TMS14F",GID),"TMS14F",
                                    ifelse(grepl("TMS15F",GID),"TMS15F",
                                           ifelse(grepl("TMS18F",GID),"TMS18F","GGetc")))))

Write GEBVs

gebvs %>% 
  spread(Trait,GEBV) %>% 
  group_by(OutliersRemoved) %>% 
  nest() %>% 
  mutate(data=map2(data,OutliersRemoved,function(data,OutliersRemoved){
    write.csv(data,
              file=paste0("output/GEBV_IITA_OutliersRemoved",OutliersRemoved,"_73119.csv"),
              row.names = F) }))

Next step

Get Genetic Gain Estimate


sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] viridis_0.5.1     viridisLite_0.3.0 cowplot_1.0.0     magrittr_1.5     
 [5] forcats_0.4.0     stringr_1.4.0     dplyr_0.8.3       purrr_0.3.3      
 [9] readr_1.3.1       tidyr_1.0.0       tibble_2.1.3      ggplot2_3.2.1    
[13] tidyverse_1.2.1  

loaded via a namespace (and not attached):
 [1] tidyselect_0.2.5     xfun_0.11            haven_2.2.0         
 [4] lattice_0.20-38      colorspace_1.4-1     vctrs_0.2.0         
 [7] generics_0.0.2       htmltools_0.4.0      yaml_2.2.0          
[10] rlang_0.4.1          later_1.0.0          pillar_1.4.2        
[13] withr_2.1.2          glue_1.3.1           modelr_0.1.5        
[16] readxl_1.3.1         lifecycle_0.1.0      munsell_0.5.0       
[19] gtable_0.3.0         workflowr_1.5.0.9000 cellranger_1.1.0    
[22] rvest_0.3.5          evaluate_0.14        labeling_0.3        
[25] knitr_1.26           httpuv_1.5.2         broom_0.5.2         
[28] Rcpp_1.0.3           promises_1.1.0       backports_1.1.5     
[31] scales_1.1.0         jsonlite_1.6         farver_2.0.1        
[34] fs_1.3.1             gridExtra_2.3        hms_0.5.2           
[37] digest_0.6.22        stringi_1.4.3        grid_3.6.1          
[40] rprojroot_1.3-2      cli_1.1.0            tools_3.6.1         
[43] lazyeval_0.2.2       crayon_1.3.4         whisker_0.4         
[46] pkgconfig_2.0.3      zeallot_0.1.0        xml2_1.2.2          
[49] lubridate_1.7.4      assertthat_0.2.1     rmarkdown_1.17      
[52] httr_1.4.1           rstudioapi_0.10      R6_2.4.1            
[55] nlme_3.1-142         git2r_0.26.1         compiler_3.6.1