Using the Fully Bayesian Model in rubias

Ben Moran

6/3/2019

The default model in rubias is a conditional model in which inference is done with the baseline (reference) allele counts fixed. The package now also includes a fully Bayesian GSI model, as is implemented in the software packages BAYES and cBayes, among others. This model differs from the conditional model in two main respects:

Ideally, updating the reference allele counts with mixture genotypes will refine the estimate of allele frequencies in that sample. This is especially useful in cases where the reference dataset is small relative to the number of mixture individuals, as well as those in which the mixture contains populations not present in the baseline. However, the fully Bayesian model is much more computationally intensive, and can exhibit pathological behavior when reference collections are not well-differentiated.

Running the Model with Baseline Resampling

Load the required packages first:

library(rubias)
library(tidyverse)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang

The basic way to invoke the fully Bayesian model is to use the infer_mixture function with the method option set to “BR” (for “baseline resampling”). For example:

full_model <- infer_mixture(
  reference = small_chinook_ref, 
  mixture = small_chinook_mix, 
  gen_start_col = 5, 
  method = "BR"
  )
## Collating data; compiling reference allele frequencies, etc.   time: 0.19 seconds
## Computing reference locus specific means and variances for computing mixture z-scores   time: 0.02 seconds
## Working on mixture collection: rec3 with 29 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##   performing 2000 sweeps of method "BR", 100 sweeps of which are burn-in.   time: 0.77 seconds
##   tidying output into a tibble.   time: 0.19 seconds
## Working on mixture collection: rec1 with 36 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##   performing 2000 sweeps of method "BR", 100 sweeps of which are burn-in.   time: 0.79 seconds
##   tidying output into a tibble.   time: 0.17 seconds
## Working on mixture collection: rec2 with 35 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##   performing 2000 sweeps of method "BR", 100 sweeps of which are burn-in.   time: 0.76 seconds
##   tidying output into a tibble.   time: 0.18 seconds

Note that the output generated by “BR” is still a list of four tidy data frames, but the bootstrapped_proportions data frame of methods “MCMC” and “PB” is replaced with an allele_frequencies data frame. This data frame contains the posterior mean allele frequencies (theta) for each population, updated based on the allocations of mixture individuals throughout MCMC. The first three columns specify the mixture collection, locus and allele in question, and all subsequent columns report the frequency for a particular population.

full_model$allele_frequencies
## # A tibble: 546 x 9
##    mixture_collect… locus allele Deer_Cr_sp Feather_H_fa Sacramento_H
##    <chr>            <chr> <chr>       <dbl>        <dbl>        <dbl>
##  1 rec3             AldB… 2          0.832       0.916       0.897   
##  2 rec3             AldB… 4          0.168       0.0841      0.103   
##  3 rec3             Aldo… 1          0.0327      0.00150     0.000902
##  4 rec3             Aldo… 4          0.967       0.999       0.999   
##  5 rec3             OTNA… 1          0.228       0.205       0.452   
##  6 rec3             OTNA… 3          0.772       0.795       0.548   
##  7 rec3             OTSB… 2          0.850       0.769       0.551   
##  8 rec3             OTSB… 4          0.150       0.231       0.449   
##  9 rec3             OTST… 3          0.890       0.889       0.683   
## 10 rec3             OTST… 4          0.110       0.111       0.317   
## # … with 536 more rows, and 3 more variables: Eel_R <dbl>,
## #   Klamath_IGH_fa <dbl>, Umpqua_sp <dbl>

Also note that the log-likelihoods and Z-scores included in the indiv_posteriors output data frame are calculated using only the reference allele counts prior to MCMC, and so do not utilize the full model results.

Let’s compare the results of this to those from the conditional model:

set.seed(15)
cond_model <- infer_mixture(
  reference = small_chinook_ref, 
  mixture = small_chinook_mix, 
  gen_start_col = 5, 
  method = "MCMC"
  )
## Collating data; compiling reference allele frequencies, etc.   time: 0.19 seconds
## Computing reference locus specific means and variances for computing mixture z-scores   time: 0.02 seconds
## Working on mixture collection: rec3 with 29 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##   performing 2000 total sweeps, 100 of which are burn-in and will not be used in computing averages in method "MCMC"   time: 0.02 seconds
##   tidying output into a tibble.   time: 0.01 seconds
## Working on mixture collection: rec1 with 36 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##   performing 2000 total sweeps, 100 of which are burn-in and will not be used in computing averages in method "MCMC"   time: 0.03 seconds
##   tidying output into a tibble.   time: 0.01 seconds
## Working on mixture collection: rec2 with 35 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##   performing 2000 total sweeps, 100 of which are burn-in and will not be used in computing averages in method "MCMC"   time: 0.03 seconds
##   tidying output into a tibble.   time: 0.01 seconds
comppi <- cond_model$mixing_proportions %>%
  mutate(cond_pi = pi, full_pi = full_model$mixing_proportions$pi)

ggplot(comppi, aes(x = cond_pi, y = full_pi, colour = collection)) +
  geom_point() +
  geom_abline(slope = 1, intercept = 0) +
  facet_wrap(~mixture_collection) +
  theme(legend.position = "bottom")

The two methods are largely in agreement for this small test set.

Initializing Mixture Proportions with the Conditional Model

When collections are poorly resolved, the fully Bayesian model may show pathological behaviors such as allocating all individuals to only one of the closely related populations. One way to reduce this behavior is to initialize the fully Bayesian model with the output from the conditional model. rubias explicitly supports this through the options prelim_reps and prelim_burn_in: if changed from the NULL default, these parameters specify conditional MCMC cycles to perform, the posterior mean mixing proportions of which are used as the initial mixing proportion estimates in the fully Bayesian model. For example:

## Collating data; compiling reference allele frequencies, etc.   time: 0.19 seconds
## Computing reference locus specific means and variances for computing mixture z-scores   time: 0.02 seconds
## Working on mixture collection: rec3 with 29 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##     performing 2000 initial sweeps, 100 of which are burn-in and will not be used in computing averages to initialize starting point for method "BR".   time: 0.03 seconds
##   performing 2000 sweeps of method "BR", 100 sweeps of which are burn-in.   time: 0.70 seconds
##   tidying output into a tibble.   time: 0.16 seconds
## Working on mixture collection: rec1 with 36 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##     performing 2000 initial sweeps, 100 of which are burn-in and will not be used in computing averages to initialize starting point for method "BR".   time: 0.03 seconds
##   performing 2000 sweeps of method "BR", 100 sweeps of which are burn-in.   time: 0.79 seconds
##   tidying output into a tibble.   time: 0.17 seconds
## Working on mixture collection: rec2 with 35 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##     performing 2000 initial sweeps, 100 of which are burn-in and will not be used in computing averages to initialize starting point for method "BR".   time: 0.03 seconds
##   performing 2000 sweeps of method "BR", 100 sweeps of which are burn-in.   time: 0.73 seconds
##   tidying output into a tibble.   time: 0.16 seconds

In this case, the results are largely identical to those from uniform initial proportions, potentially because the collections are well-resolved to begin with.

Managing Parallelization

To incorporate baseline updates while keeping runtimes reasonable, genotype likelihood calculations in the fully Bayesian model are parallelized using RcppParallel. By default, RcppParallel runs one thread per core on your machine; to check the number of cores available, use the detectCores() function from package parallel. However, this default behavior is dangerous on HPC systems, where a mismatch between the number of cores requested by the user and the number of threads demanded by RcppParallel might cause the job to abort. In these cases, the number of threads should be manually set with RcppParallel::setThreadOptions. For example, to set the number of threads used to 1:

RcppParallel::setThreadOptions(numThreads = 1)

full_model <- infer_mixture(
  reference = small_chinook_ref, 
  mixture = small_chinook_mix, 
  gen_start_col = 5, 
  method = "BR"
  )
## Collating data; compiling reference allele frequencies, etc.   time: 0.17 seconds
## Computing reference locus specific means and variances for computing mixture z-scores   time: 0.02 seconds
## Working on mixture collection: rec3 with 29 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##   performing 2000 sweeps of method "BR", 100 sweeps of which are burn-in.   time: 0.93 seconds
##   tidying output into a tibble.   time: 0.15 seconds
## Working on mixture collection: rec1 with 36 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##   performing 2000 sweeps of method "BR", 100 sweeps of which are burn-in.   time: 1.08 seconds
##   tidying output into a tibble.   time: 0.14 seconds
## Working on mixture collection: rec2 with 35 individuals
##   calculating log-likelihoods of the mixture individuals.   time: 0.00 seconds
##   performing 2000 sweeps of method "BR", 100 sweeps of which are burn-in.   time: 1.11 seconds
##   tidying output into a tibble.   time: 0.14 seconds

Note the slowdown even in this small dataset! To reset the threading options to utilize all available cores, just use:

RcppParallel::setThreadOptions(numThreads = RcppParallel::defaultNumThreads())