Last updated: 2021-02-01

Checks: 7 0

Knit directory: PredictOutbredCrossVar/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). 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(20191123) 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 results in this page were generated with repository version 883b1d4. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

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:    analysis/.DS_Store
    Ignored:    code/.DS_Store
    Ignored:    data/.DS_Store
    Ignored:    output/.DS_Store
    Ignored:    output/crossRealizations/.DS_Store

Untracked files:
    Untracked:  .gitignore
    Untracked:  Icon
    Untracked:  PredictOutbredCrossVar.Rproj
    Untracked:  manuscript/
    Untracked:  output/crossPredictions/
    Untracked:  output/gblups_DirectionalDom_parentwise_crossVal_folds.rds
    Untracked:  output/gblups_geneticgroups.rds
    Untracked:  output/gblups_parentwise_crossVal_folds.rds

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 repository in which changes were made to the R Markdown (analysis/getPMVarComps.Rmd) and HTML (docs/getPMVarComps.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 883b1d4 wolfemd 2021-02-01 Update the syntax highlighting and code-block formatting throughout for
Rmd 6a10c30 wolfemd 2021-01-04 Submission and GitHub ready version.
html 6a10c30 wolfemd 2021-01-04 Submission and GitHub ready version.

For each of the genetic groups (GG, C1, C2, C3 , ALL):

Compute the posterior mean variances and covariances from the on-disk-stored, post-burnIn, thinned posterior samples of marker effects.

Models: A, AD, DirDom

For the directional dominance (DirDom) marker effects set. Add inbreeding/propHom effect to vector d.

  • Compute \(Var(GEBV)\) with allele sub. effects as: \(\alpha = a + d(q-p)\).
  • Compute \(Var(GETGV) = Var(Add) + Var(Dom)\)

Models A and AD

Set-up

# activate multithread OpenBLAS
export OMP_NUM_THREADS=112
rm(list=ls()); gc()
library(tidyverse); library(magrittr); library(predCrossVar); library(BGLR);
blups<-readRDS(here::here("data","blups_forawcdata.rds")) %>% 
  select(Trait,blups) %>% # BLUPs long-->wide for multivar analysis
  unnest(blups) %>% 
  select(Trait,germplasmName,drgBLUP) %>% 
  spread(Trait,drgBLUP) %>%  # choosing de-regressed BLUPs as responses despite unweighted analysis
  select(germplasmName,all_of(c("DM","logFYLD","MCMDS","TCHART"))) # precaution to ensure consistent column order

# SNP data ------------
snps<-readRDS(here::here("data","dosages_awc.rds")) %>% 
  remove_invariant(.); dim(snps) # [1] 5591 38093

# Training datasets -----------
geneticgroups<-blups %>% 
  filter(!grepl("TMS13|TMS14|TMS15",germplasmName)) %>% 
  mutate(Group="GG") %>% 
  bind_rows(blups %>% 
              filter(grepl("TMS13",germplasmName)) %>% 
              mutate(Group="TMS13")) %>% 
  bind_rows(blups %>% 
              filter(grepl("TMS14",germplasmName)) %>% 
              mutate(Group="TMS14")) %>% 
  bind_rows(blups %>% 
              filter(grepl("TMS15",germplasmName)) %>% 
              mutate(Group="TMS15")) %>% 
  bind_rows(blups %>% 
              mutate(Group="All")) %>% 
  nest(blups=-Group) %>% 
  crossing(Model=c("A","AD")) %>% 
  mutate(blups=map(blups,~filter(.,germplasmName %in% rownames(snps))),
         outName=paste0("mt_",Group,"_",Model))

# Parallelization specs ---------
require(furrr); options(future.globals.maxSize=50000*1024^2)
plan(multiprocess); options(mc.cores=10); 

# MCMC params ------
nIter<-30000; burnIn<-5000; thin<-5

# getVarComps function -----------
## Wrapper function for getMultiTraitPMVs_A and getMultiTraitPMVs_AD
## For a given Model / data chunk, load stored posterior marker effects
## Compute vars/covars
source(here::here("code","getVarComps.R"))

Compute var. comps

# cbsulm12 - Done!
geneticgroups %<>% 
  mutate(PMV=future_pmap(.,getVarComps,snps=snps,nIter=30000, burnIn=5000,thin=5))
saveRDS(geneticgroups,file=here::here("output","pmv_varcomps_geneticgroups.rds"))

Model DirDom

Set-up

# activate multithread OpenBLAS
export OMP_NUM_THREADS=112
rm(list=ls()); gc()
library(tidyverse); library(magrittr); library(predCrossVar); library(BGLR);
blups<-readRDS(here::here("data","blups_forawcdata.rds")) %>% 
  select(Trait,blups) %>% # BLUPs long-->wide for multivar analysis
  unnest(blups) %>% 
  select(Trait,germplasmName,drgBLUP) %>% 
  spread(Trait,drgBLUP) %>%  # choosing de-regressed BLUPs as responses despite unweighted analysis
  select(germplasmName,all_of(c("DM","logFYLD","MCMDS","TCHART"))) # precaution to ensure consistent column order

# SNP data ------------
snps<-readRDS(here::here("data","dosages_awc.rds")) %>% 
  remove_invariant(.); dim(snps) # [1] 5591 38093

# Training datasets -----------
geneticgroups<-blups %>% 
  filter(!grepl("TMS13|TMS14|TMS15",germplasmName)) %>% 
  mutate(Group="GG") %>% 
  bind_rows(blups %>% 
              filter(grepl("TMS13",germplasmName)) %>% 
              mutate(Group="TMS13")) %>% 
  bind_rows(blups %>% 
              filter(grepl("TMS14",germplasmName)) %>% 
              mutate(Group="TMS14")) %>% 
  bind_rows(blups %>% 
              filter(grepl("TMS15",germplasmName)) %>% 
              mutate(Group="TMS15")) %>% 
  bind_rows(blups %>% 
              mutate(Group="All")) %>% 
  nest(blups=-Group) %>% 
  crossing(Model=c("DirDomA","DirDomAD")) %>% 
  mutate(blups=map(blups,~filter(.,germplasmName %in% rownames(snps))),
         outName=paste0("mt_",Group,"_DirectionalDom"))

# Parallelization specs ---------
require(furrr); options(future.globals.maxSize=50000*1024^2)
plan(multiprocess); options(mc.cores=10); 

# MCMC params ------
nIter<-30000; burnIn<-5000; thin<-5

# getDirectionalDomVarComps function -----------
## Wrapper function for getMultiTraitPMVs_A and getMultiTraitPMVs_AD
## For a given Model / data chunk, load stored posterior marker effects
## Compute vars/covars
source(here::here("code","getDirectionalDomVarComps.R"))

Compute var. comps

# cbsulm12 - Done!
geneticgroups %<>% 
  mutate(PMV=future_pmap(.,getDirectionalDomVarComps,snps=snps,nIter=30000, burnIn=5000,thin=5))
saveRDS(geneticgroups,file=here::here("output","pmv_DirectionalDom_varcomps_geneticgroups.rds"))

Process results

Tidy VarComps

library(tidyverse); library(magrittr);
geneticgroups<-readRDS(here::here("output","pmv_varcomps_geneticgroups.rds")) %>% 
  bind_rows(readRDS(here::here("output","pmv_DirectionalDom_varcomps_geneticgroups.rds")))
geneticgroups %<>% 
  select(-blups) %>% 
  unnest_wider(PMV) %>% 
  select(-runtime) %>% 
  unnest(pmv) %>% 
  mutate_if(is.numeric,~round(.,6)) %>% 
  pivot_longer(cols=c(VPM,PMV),names_to = "VarMethod",values_to = "Var")

Compute SI variances

# Selection weights -----------
indices<-readRDS(file=here::here("data","selection_index_weights_4traits.rds"))
## Predicted Index Variances
geneticgroups_SI<-geneticgroups %>% 
  nest(varcovars=c(Trait1,Trait2,Var)) %>%
  mutate(varcovars=map(varcovars,
                       function(varcovars){
                         # pairwise to square symmetric matrix
                         gmat<-varcovars %>% 
                           spread(Trait2,Var) %>% 
                           column_to_rownames(var = "Trait1") %>% 
                           as.matrix %>% 
                           .[indices$Trait,indices$Trait]
                         gmat[lower.tri(gmat)]<-t(gmat)[lower.tri(gmat)]
                         return(gmat) }),
         # compute index variances
         stdSI=map_dbl(varcovars,~t(indices$stdSI)%*%.%*%indices$stdSI),
         biofortSI=map_dbl(varcovars,~t(indices$biofortSI)%*%.%*%indices$biofortSI)) %>% 
  # discard var-covar matrix
  select(-varcovars) %>% 
  pivot_longer(cols = c(stdSI,biofortSI),
               names_to = "Trait1", 
               values_to = "Var") %>% 
  mutate(Trait2=Trait1)

geneticgroups %<>% bind_rows(geneticgroups_SI)
rm(geneticgroups_SI)

–> Save

saveRDS(geneticgroups,file=here::here("output","pmv_varcomps_geneticgroups_tidy_includingSIvars.rds"))

Tidy inbreeding effect est. from DirDom model

library(tidyverse); library(magrittr); library(BGLR)
geneticgroups_dd<-readRDS(here::here("output","pmv_DirectionalDom_varcomps_geneticgroups.rds")) %>% 
  distinct(Group,outName) %>% 
  mutate(mtbrrFit=map(outName,~readRDS(here::here("output/mtMarkerEffects",paste0(.,".rds"))))) %>% 
  unnest_wider(mtbrrFit) %>% 
  select(-runtime,-snpIDs,-outName) %>% 
  mutate(Dataset="GeneticGroups")

parentfolds_dd<-readRDS(file = here::here("data","parentwise_crossVal_folds.rds")) %>% 
  rename(Repeat=id,Fold=id2) %>% 
  select(Repeat,Fold,trainset,testset) %>% 
  pivot_longer(c(trainset,testset),
               names_to = "Dataset",
               values_to = "sampleIDs") %>% 
  mutate(Model="DirectionalDom") %>% 
  arrange(desc(Dataset),Repeat,Fold) %>% 
  mutate(outName=paste0("mt_",Repeat,"_",Fold,"_",Dataset,"_",Model)) %>% 
  mutate(mtbrrFit=map(outName,~readRDS(here::here("output/mtMarkerEffects",paste0(.,".rds"))))) %>% 
  unnest_wider(mtbrrFit) %>% 
  select(-runtime,-snpIDs,-sampleIDs,-outName,-Model)
ddEffects<-bind_rows(geneticgroups_dd,parentfolds_dd) %>% 
  mutate(inbreff=map(mtbrrFit,function(mtbrrFit){
    traits<-colnames(mtbrrFit$yHat)
    beta<-mtbrrFit$ETA$GmeanD$beta
    SD.beta<-mtbrrFit$ETA$GmeanD$SD.beta
    colnames(beta)<-colnames(SD.beta)<-traits
    
    inbeffs<-bind_rows(as_tibble(beta),as_tibble(SD.beta)) %>% 
      t(.) %>% 
      as.data.frame %>% 
      rownames_to_column(var="Trait") %>% 
      rename(InbreedingEffect=V1,
             InbreedingEffectSD=V2)
    return(inbeffs) })) %>% 
  select(-mtbrrFit) %>% 
  unnest(inbreff)
saveRDS(ddEffects,file=here::here("output","ddEffects.rds"))

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6        whisker_0.4       knitr_1.31        magrittr_2.0.1   
 [5] workflowr_1.6.2   R6_2.5.0          rlang_0.4.10      stringr_1.4.0    
 [9] tools_4.0.2       xfun_0.20         git2r_0.28.0      htmltools_0.5.1.1
[13] ellipsis_0.3.1    yaml_2.2.1        digest_0.6.27     rprojroot_2.0.2  
[17] tibble_3.0.6      lifecycle_0.2.0   crayon_1.3.4      later_1.1.0.1    
[21] vctrs_0.3.6       fs_1.5.0          promises_1.1.1    glue_1.4.2       
[25] evaluate_0.14     rmarkdown_2.6     stringi_1.5.3     compiler_4.0.2   
[29] pillar_1.4.7      httpuv_1.5.5      pkgconfig_2.0.3