## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(ape)
library(delimtools)
library(dplyr)
library(here)

## ----eval=FALSE---------------------------------------------------------------
# path_to_file <- system.file("extdata/geophagus.fasta", package = "delimtools")
# 
# geophagus <- ape::read.FASTA(file = path_to_file)
# 

## -----------------------------------------------------------------------------
geophagus

## -----------------------------------------------------------------------------
dplyr::glimpse(geophagus_info)

## -----------------------------------------------------------------------------
check_identifiers(dna = geophagus, identifier = "gbAccession", data = geophagus_info)

## ----error= TRUE--------------------------------------------------------------
try({
check_identifiers(geophagus[11:354], "gbAccession", geophagus_info)
})

## ----error= TRUE--------------------------------------------------------------
try({
check_identifiers(geophagus, "gbAccession", geophagus_info[5:354,])
})

## -----------------------------------------------------------------------------
geophagus_clean <- clean_dna(geophagus)
# and display
geophagus_clean

## -----------------------------------------------------------------------------
geophagus_collapsed <- hap_collapse(geophagus)
# and display
geophagus_collapsed

## -----------------------------------------------------------------------------
geophagus_collapsed <- hap_collapse(geophagus, collapseSubstrings = FALSE)
# and display
geophagus_collapsed

## -----------------------------------------------------------------------------
haplotype_tbl(geophagus, verbose = FALSE)

## -----------------------------------------------------------------------------
haps_df <- haplotype_tbl(geophagus, verbose = FALSE)

collapse_others(data = geophagus_info, 
                hap_tbl = haps_df, 
                labels = "gbAccession", 
                cols = c("scientificName", "country"))

## -----------------------------------------------------------------------------
# geophagus - the `ape::DNAbin` object supplied with this package

abgd_delim <- abgd(geophagus)
abgd_df <- abgd_tbl(abgd_delim)

# print the dataframe
print(abgd_df)

## -----------------------------------------------------------------------------
# geophagus - the `ape::DNAbin` object supplied with this package

asap_delim <- asap(geophagus)
asap_df <- asap_tbl(asap_delim)

# print the dataframe
print(asap_df)

## -----------------------------------------------------------------------------
dist_mat <- ape::dist.dna(geophagus, model = "raw", pairwise.deletion = TRUE)

locmin <- spider::localMinima(dist_mat)


## ----fig.height= 3.5, fig.width= 6.9------------------------------------------
plot(locmin) |> 
  abline(v=locmin$localMinima[1],col="red")

## -----------------------------------------------------------------------------
locmin_df <- locmin_tbl(dist_mat, threshold = locmin$localMinima[1])

print(locmin_df)

## ----fig.height= 3.5, fig.width= 6.9------------------------------------------
dist_mat_tr <- ape::cophenetic.phylo(as.phylo(geophagus_beast)) |> 
  as.dist()

locmin_tr <- spider::localMinima(dist_mat_tr)

plot(locmin_tr) |> 
  abline(v=locmin_tr$localMinima[1],col="red")

treedist_df <- locmin_tbl(dist_mat_tr,
                          threshold = locmin_tr$localMinima[1],
                          delimname = "treedist")

print(treedist_df)

## -----------------------------------------------------------------------------
percent_df <- locmin_tbl(dist_mat, threshold = 0.02, delimname = "percent")

print(percent_df)

## -----------------------------------------------------------------------------
# beast_tree - the Nexus formatted ultrametric phylogenetic tree object supplied with this package

path_to_file <- system.file("extdata/geophagus_beast.nex", package = "delimtools")

beast_tree <- treeio::read.beast(path_to_file) |>
  ape::as.phylo()

## -----------------------------------------------------------------------------
gmyc_delim <- gmyc(beast_tree)

## -----------------------------------------------------------------------------
summary(gmyc_delim)

## -----------------------------------------------------------------------------
gmyc_df <- gmyc_tbl(gmyc_delim)

# print the dataframe
print(gmyc_df)

## -----------------------------------------------------------------------------
# beast_tree already loaded and converted

bgmyc_delim <- bgmyc(beast_tree)

## -----------------------------------------------------------------------------
bgmyc_df <- bgmyc_tbl(bgmyc_delim)

print(bgmyc_df)

## -----------------------------------------------------------------------------
# raxml_tree - the Newick formatted phylogenetic tree object supplied with this package

path_to_file <- system.file("extdata/geophagus_raxml.nwk", package = "delimtools")

raxml_tree <- treeio::read.newick(path_to_file)

## -----------------------------------------------------------------------------
mptp_delim <- mptp(raxml_tree)
mptp_df <- mptp_tbl(mptp_delim)

print(mptp_df)

## -----------------------------------------------------------------------------
# raxml_tree already loaded and converted

ptp_delim <- ptp(raxml_tree)
ptp_df <- ptp_tbl(ptp_delim)

print(ptp_df)

## -----------------------------------------------------------------------------
min_brlen(raxml_tree)

## -----------------------------------------------------------------------------
morph_df <- morph_tbl(labels = geophagus_info$gbAccession, 
                      sppVector = geophagus_info$scientificName) |>
  dplyr::filter(labels %in% haps_df$labels)

print(morph_df)

## ----message= FALSE-----------------------------------------------------------
all_delims <- list(abgd_df, asap_df, locmin_df, percent_df, treedist_df,
                   bgmyc_df, gmyc_df,
                   mptp_df, ptp_df,
                   morph_df)

all_delims_df <- delim_join(all_delims)

## -----------------------------------------------------------------------------
report_delim(all_delims_df)

## -----------------------------------------------------------------------------
match_ratio(all_delims_df) |> 
  dplyr::arrange(dplyr::desc(match_ratio)) |> 
            print(n=Inf)

## -----------------------------------------------------------------------------
all_delims_df <- dplyr::select(all_delims_df, -treedist)

report_delim(all_delims_df)

## ----warning=FALSE, message= FALSE, fig.height= 15, fig.width= 12-------------
# geophagus_beast - the Nexus formatted ultrametric phylogenetic tree object used for visualization

path_to_file <- system.file("extdata/geophagus_beast.nex", package = "delimtools")

geophagus_tree <- treeio::read.beast(path_to_file)

# customize tip labels of the tree
tip_tab <- geophagus_info |> 
  dplyr::filter(gbAccession %in% geophagus_beast@phylo$tip.label) |>
  dplyr::mutate(labs = glue::glue("{gbAccession} | {scientificName}")) |> 
  dplyr::select(gbAccession, labs, scientificName)

# create a customized color palette
cols <- delim_brewer(delim = all_delims_df, package = "randomcoloR", seed = 42)

# plot
delim_autoplot(all_delims_df, 
               geophagus_beast,
               consensus = TRUE,
               n_match = 5,
               tbl_labs = tip_tab,
               col_vec = cols,
               hexpand = 0.7,
               widths = c(0.5, 0.5))

