bodkan / admixr

An R package for reproducible and automated ADMIXTOOLS analyses
https://bodkan.net/admixr
Other
28 stars 9 forks source link

Note on the pre-screening functionality #62

Closed bodkan closed 4 years ago

bodkan commented 4 years ago

Would something like this do the trick? Is that even useful given the expanded functionality in the rotation procedure?

#' Perform a 'pre-screening' of the 'right' populations before
#' a qpAdm analysis based on Harney et al., 2020 (bioRxiv)
#'
#' @param data EIGENSTRAT dataset
#' @param candidates Character vector with potential 'outgroup' populations
#' @param left Character vector with target and source populations
#'
#' @return Data frame of all combinations of f4(L_i, L_j; R_k, R_l),
#'   ordered by Zscore of the f4 statistic
#'
#' @export
qpAdm_prescreen <- function(data, candidates, left, Zcutoff = 2) {
  leftcomb <- t(combn(left, 2))
  rightcomb <- t(combn(candidates, 2))

  i <- 1
  quartets <- list()
  for (l in 1:nrow(leftcomb)) {
    for (r in 1:nrow(rightcomb)) {
      quartets[[i]] <- c(leftcomb[l, ], rightcomb[r, ])
      i <- i + 1
    }
  }

  result <- f4(data, quartets = quartets) %>% dplyr::arrange(abs(Zscore))

  right <- dplyr::filter(result, abs(Zscore) > Zcutoff) %>%
    .[, c("Y", "Z")] %>%
    as.matrix %>%
    as.vector %>%
    unique

  list(outgroups = right, screening = result)
}