9.16 Calculating PBS

Using these \(\textrm{F}_{ST}\) values, we can calculate (for every SNP) the branch lengths (\(\mathbf{T}\)) that separate each population pair, and then calculate PBS.


The mutate() function

tidyverse’s mutate function is an easy way to perform calculations on tables. Its syntax is:

mutate(new_column = <formula for calculating column values>)

For example, if I wanted to create a new column that average each SNP’s allele frequencies across the three populations:

fst_results %>%
  mutate(avg_af = (rps.af + chb.af + png.af) / 3)

Filling in the gaps in the code block below, use mutate to calculate T and PBS on the FST values:

pbs <- fst_results %>%
  # calculate branch lengths between populations
  mutate(T_rps_chb = _________,
         T_rps_png = _________,
         T_png_chb = _________,) %>%
  # calculate pbs
  mutate(pbs = _________) %>%
  # sort by descending pbs value
  arrange(-pbs)

Solution
pbs <- fst_results %>%
  # calculate branch lengths between populations
  mutate(T_rps_chb = -log(1 - fst.rps.chb),
         T_rps_png = -log(1 - fst.rps.png),
         T_png_chb = -log(1 - fst.png.chb)) %>%
  # calculate pbs
  mutate(pbs = ((T_rps_png + T_rps_chb) - (T_png_chb)) / 2) %>%
  # sort by descending pbs value
  arrange(-pbs)

head(pbs)
##   chr       pos   rps.af   chb.af    png.af fst.rps.chb fst.rps.png fst.png.chb
## 1  11 126880301 0.833333 0.296117 0.0571429    0.734021    0.823884    0.216269
## 2  11 126883747 0.833333 0.296117 0.0571429    0.734021    0.823884    0.216269
## 3  11 126893266 0.833333 0.300971 0.0571429    0.729450    0.823884    0.222408
## 4  11 126883622 0.833333 0.300971 0.0571429    0.729227    0.823884    0.221887
## 5  11 126888750 0.833333 0.305825 0.0571429    0.724490    0.823884    0.227770
## 6  11 126885142 0.833333 0.320388 0.0571429    0.709721    0.823884    0.244928
##   T_rps_chb T_rps_png T_png_chb      pbs
## 1  1.324338  1.736612 0.2436894 1.408630
## 2  1.324338  1.736612 0.2436894 1.408630
## 3  1.307298  1.736612 0.2515533 1.396179
## 4  1.306474  1.736612 0.2508835 1.396102
## 5  1.289131  1.736612 0.2584728 1.383635
## 6  1.236913  1.736612 0.2809422 1.346291