R/Local_phylo_dist.R

Defines functions make_local_phylo_dist

Documented in make_local_phylo_dist

#' Make Local Scale Phylogenetic Distance Matrix
#' 
#' @description Function for creating a phylogenetic distance matrix
#' from data that does not require you to subset first. 
#' 
#' @param focal.species The focal species or plot identifier that
#' you want to use to create a the phylogeny
#' @param community.data The community data file. Each observation
#' of a species in a community should be its own row. For now,
#' the column specifiy which community is which is called 
#' \code{exotic_species}, but this will be generalized later
#' using an argument to specifiy which column marks community 
#' separators and which column holds the actual species names
#' @param phylo A larger phylogeny that contains all of the 
#' species in the \code{community.data} file. 
#' @param square_root Square root transform branch lengths in distance matrix?
#' 
#' @return A phylogenetic distance matrix in the form of a
#' \code{data.frame}
#' 
#' @author Sam Levin
#' 
#' @importFrom magrittr %>%
#' @importFrom dplyr filter
#' @importFrom ape drop.tip
#' @importFrom ape cophenetic.phylo
#' @export

make_local_phylo_dist <- function(focal.species, community.data,
                                  phylo, square_root = TRUE){
  local.com <- dplyr::filter(community.data,
                             exotic_species == focal.species) %>%
    .$community %>% as.character()
  
  out <- ape::drop.tip(phylo, setdiff(phylo$tip.label, local.com)) %>%
    ape::cophenetic.phylo() 
  
  if(square_root) {
    out <- sqrt(out)
  }
  
  out <- data.frame(out)
  
  return(out)
  
}
levisc8/Fun_Phylo_Package documentation built on June 2, 2020, 8:41 a.m.