From 1c927855d01892374485914e8ad7565211ac9fed Mon Sep 17 00:00:00 2001 From: pribiller Date: Thu, 13 Aug 2026 15:40:20 +0900 Subject: [PATCH 1/6] Add divergence time function Add a function to retrieve the divergence time of two species via the TimeTree API. Add 'httr' as a new dependency to the package. --- DESCRIPTION | 4 +++- R/divergenceTimetree.R | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 R/divergenceTimetree.R diff --git a/DESCRIPTION b/DESCRIPTION index bd2b247..1ecd680 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -38,7 +38,8 @@ Imports: tidytree, treeio, purrr, - yaml + yaml, + httr VignetteBuilder: knitr Collate: 'ConvenientTblTree.R' @@ -51,6 +52,7 @@ Collate: 'averageResults.R' 'bootstrapTree.R' 'childSpecies.R' + 'divergenceTimeTree.R' 'ellipsePlot.R' 'enr.R' 'eog.R' diff --git a/R/divergenceTimetree.R b/R/divergenceTimetree.R new file mode 100644 index 0000000..1d7e783 --- /dev/null +++ b/R/divergenceTimetree.R @@ -0,0 +1,42 @@ +#' Divergence time - TimeTree +#' +#' Retrieves the divergence time of two species via the TimeTree API. +#' +#' @param taxid1 An integer: The NCBI id of one taxon +#' @param taxid2 An integer: The NCBI id of the other taxon +#' +#' @return A list in which the first item is the median divergence time (`divtimeMedian`), +#' and the last two items are the divergence time confidence interval +#' (`divtimeCI_low` and `divtimeCI_high`). +#' If the divergence time is not found in the TimeTree database, it returns `NA`. +#' +#' @author Priscila Biller +#' +#' @examples +#' \dontrun{ +#' getDivergenceTime(9555,9601)} +#' +#' @importFrom httr GET content +#' +#' @export +getDivergenceTime <- function(taxid1, taxid2) { + timetreePairwiseBase <- "http://timetree.temple.edu/api/pairwise/" + # Example of request to the TimeTree's API: + # - Request: http://timetree.temple.edu/api/pairwise/9555/9601 + # - Answer from TimeTree: + # taxon_a_id,taxon_b_id,scientific_name_a,scientific_name_b,all_total,precomputed_age,precomputed_ci_low,precomputed_ci_high,adjusted_age + # 9555,9601,Papio anubis,Pongo abelii,83,28.82,26.8,30.6,0 + timetreeRequest <- paste(timetreePairwiseBase, taxid1, "/", taxid2, sep="") + timetreeResponse <- GET(timetreeRequest) + lines <- content(timetreeResponse, as="text", encoding="UTF-8") + # Break the whole content into lines, and gets the second line (data). + lineData <- strsplit(lines, "\n", fixed = TRUE)[[1]][2] # First line: header; Second line: data. + # Break the line into data. + data <- strsplit(lineData, ",", fixed = TRUE)[[1]] + # Check if the request returned something valid. + if(length(data) >= 8) { + list(divtimeMedian=as.numeric(data[6]),divtimeCI_low=as.numeric(data[7]),divtimeCI_high=as.numeric(data[8])) + } else { + NA + } +} From 44921708f722c2badfe3a8b9f1b7c1aef8342bef Mon Sep 17 00:00:00 2001 From: pribiller Date: Thu, 13 Aug 2026 15:41:24 +0900 Subject: [PATCH 2/6] Update documentation Update documentation for existing functions. Add new documentation for new function 'getDivergenceTime'. --- DESCRIPTION | 2 +- NAMESPACE | 111 +++++++++++++++---------- R/{divergenceTimetree.R => timeTree.R} | 0 man/ConvenientTblTree.Rd | 5 -- man/Haloarchaea.Rd | 2 +- man/MRCA_2D_plot.Rd | 8 +- man/childSpecies.Rd | 8 +- man/ellipsePlot.Rd | 8 +- man/eog.Rd | 14 ++-- man/extractValues.Rd | 8 +- man/focalClade.Rd | 14 ++-- man/formatStats.Rd | 4 +- man/getDivergenceTime.Rd | 30 +++++++ man/oikData.Rd | 2 +- man/recordAncestor.Rd | 22 ++--- man/recordClades.Rd | 14 ++-- man/resultFiles.Rd | 4 +- man/subMatrix.Rd | 24 +++--- man/subTree.Rd | 14 ++-- man/treeHeatMap.Rd | 8 +- man/visualizeTree.Rd | 22 ++--- 21 files changed, 184 insertions(+), 140 deletions(-) rename R/{divergenceTimetree.R => timeTree.R} (100%) create mode 100644 man/getDivergenceTime.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 1ecd680..258ac96 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -52,7 +52,7 @@ Collate: 'averageResults.R' 'bootstrapTree.R' 'childSpecies.R' - 'divergenceTimeTree.R' + 'divergenceTimetree.R' 'ellipsePlot.R' 'enr.R' 'eog.R' diff --git a/NAMESPACE b/NAMESPACE index 9ac60ff..5fb91aa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,7 @@ # Generated by roxygen2: do not edit by hand +export("subTree,tbl_tree,double-method") +export("subTree,tbl_tree,integer-method") export(FocalCladeList) export(MRCA_2D_plot) export(MRCAs) @@ -12,6 +14,7 @@ export(eog) export(extractValues) export(focalClade) export(formatStats) +export(getDivergenceTime) export(makeMatrix) export(makeTidyTree) export(makeValueTibble) @@ -28,54 +31,70 @@ export(thinByMin) export(treeHeatMap) export(visualizeTree) import(S7) -importFrom(ape,as.phylo) -importFrom(ape,cophenetic.phylo) -importFrom(ape,prop.clades) -importFrom(ape,prop.part) -importFrom(dplyr,across) -importFrom(dplyr,arrange) -importFrom(dplyr,case_when) -importFrom(dplyr,desc) -importFrom(dplyr,filter) -importFrom(dplyr,group_by) -importFrom(dplyr,mutate) -importFrom(dplyr,n) -importFrom(dplyr,pull) -importFrom(dplyr,summarise) -importFrom(dplyr,summarize) -importFrom(dplyr,sym) -importFrom(dplyr,ungroup) -importFrom(dplyr,where) +importFrom(ape, + as.phylo, + cophenetic.phylo, + prop.clades, + prop.part +) +importFrom(dplyr, + across, + arrange, + case_when, + desc, + filter, + group_by, + mutate, + n, + pull, + summarise, + summarize, + sym, + ungroup, + where +) importFrom(ggforce,geom_mark_ellipse) -importFrom(ggplot2,aes) -importFrom(ggplot2,geom_errorbar) -importFrom(ggplot2,geom_errorbarh) -importFrom(ggplot2,geom_point) -importFrom(ggplot2,ggplot) -importFrom(ggplot2,labs) -importFrom(ggplot2,scale_color_manual) -importFrom(ggplot2,scale_fill_manual) -importFrom(ggplot2,scale_x_continuous) -importFrom(ggplot2,scale_y_continuous) -importFrom(ggplot2,theme_bw) -importFrom(ggplot2,theme_minimal) +importFrom(ggplot2, + aes, + geom_errorbar, + geom_errorbarh, + geom_point, + ggplot, + labs, + scale_color_manual, + scale_fill_manual, + scale_x_continuous, + scale_y_continuous, + theme_bw, + theme_minimal +) +importFrom(httr, + GET, + content +) importFrom(pheatmap,pheatmap) importFrom(rlang,.data) -importFrom(stats,as.dist) -importFrom(stats,complete.cases) -importFrom(stats,hclust) -importFrom(stats,loess) -importFrom(stats,mad) -importFrom(stats,na.omit) -importFrom(stats,predict) -importFrom(stats,quantile) -importFrom(stats,sd) -importFrom(tidytree,as.treedata) -importFrom(tidytree,as_tibble) -importFrom(tidytree,child) -importFrom(tidytree,offspring) -importFrom(tidytree,tree_subset) +importFrom(stats, + as.dist, + complete.cases, + hclust, + loess, + mad, + na.omit, + predict, + quantile, + sd +) +importFrom(tidytree, + as.treedata, + as_tibble, + child, + offspring, + tree_subset +) importFrom(treeio,MRCA) importFrom(utils,read.delim) -importFrom(yaml,read_yaml) -importFrom(yaml,yaml.load) +importFrom(yaml, + read_yaml, + yaml.load +) diff --git a/R/divergenceTimetree.R b/R/timeTree.R similarity index 100% rename from R/divergenceTimetree.R rename to R/timeTree.R diff --git a/man/ConvenientTblTree.Rd b/man/ConvenientTblTree.Rd index c072dea..a7d2050 100644 --- a/man/ConvenientTblTree.Rd +++ b/man/ConvenientTblTree.Rd @@ -1,12 +1,8 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/ConvenientTblTree.R -\docType{data} \name{ConvenientTblTree} \alias{ConvenientTblTree} \title{Convenient tbl_trees} -\format{ -An object of class \code{S7_S3_class} of length 3. -} \usage{ ConvenientTblTree } @@ -23,4 +19,3 @@ order in which the nodes would be plotted. This is needed by the \author{ Charles Plessy } -\keyword{datasets} diff --git a/man/Haloarchaea.Rd b/man/Haloarchaea.Rd index 98c52df..157c87d 100644 --- a/man/Haloarchaea.Rd +++ b/man/Haloarchaea.Rd @@ -32,7 +32,7 @@ Halo_FocalClades } \seealso{ -Other Lazy-loaded data: +Other Lazy-loaded data: \code{\link{oikData}} } \author{ diff --git a/man/MRCA_2D_plot.Rd b/man/MRCA_2D_plot.Rd index 7249bb6..fbc358a 100644 --- a/man/MRCA_2D_plot.Rd +++ b/man/MRCA_2D_plot.Rd @@ -26,10 +26,10 @@ p + ggplot2::geom_point( color = focalClade)) } \seealso{ -Other Plotting functions: -\code{\link{ellipsePlot}()}, -\code{\link{treeHeatMap}()}, -\code{\link{visualizeTree}()} +Other Plotting functions: +\code{\link[=ellipsePlot]{ellipsePlot()}}, +\code{\link[=treeHeatMap]{treeHeatMap()}}, +\code{\link[=visualizeTree]{visualizeTree()}} } \author{ Charles Plessy diff --git a/man/childSpecies.Rd b/man/childSpecies.Rd index bb0da69..cbd5611 100644 --- a/man/childSpecies.Rd +++ b/man/childSpecies.Rd @@ -22,10 +22,10 @@ childSpecies(Halo_Tree, 7) } \seealso{ -Other Functions for trees: -\code{\link{extractValues}()}, -\code{\link{recordAncestor}()}, -\code{\link{subMatrix}()} +Other Functions for trees: +\code{\link[=extractValues]{extractValues()}}, +\code{\link[=recordAncestor]{recordAncestor()}}, +\code{\link[=subMatrix]{subMatrix()}} } \author{ Noa Brenner diff --git a/man/ellipsePlot.Rd b/man/ellipsePlot.Rd index 674378d..ae1bbb1 100644 --- a/man/ellipsePlot.Rd +++ b/man/ellipsePlot.Rd @@ -28,10 +28,10 @@ clade. ellipsePlot(Halo_DF |> averageResults()) } \seealso{ -Other Plotting functions: -\code{\link{MRCA_2D_plot}()}, -\code{\link{treeHeatMap}()}, -\code{\link{visualizeTree}()} +Other Plotting functions: +\code{\link[=MRCA_2D_plot]{MRCA_2D_plot()}}, +\code{\link[=treeHeatMap]{treeHeatMap()}}, +\code{\link[=visualizeTree]{visualizeTree()}} } \author{ Charles Plessy diff --git a/man/eog.Rd b/man/eog.Rd index 1a9ba14..245b41a 100644 --- a/man/eog.Rd +++ b/man/eog.Rd @@ -25,13 +25,13 @@ eog(Halo_Tree, Halo_FocalClades$Haloferax) } \seealso{ -Other Focal clade functions: -\code{\link{focalClade}()}, -\code{\link{recordAncestor}()}, -\code{\link{recordClades}()}, -\code{\link{subMatrix}()}, -\code{\link{subTree}()}, -\code{\link{visualizeTree}()} +Other Focal clade functions: +\code{\link[=focalClade]{focalClade()}}, +\code{\link[=recordAncestor]{recordAncestor()}}, +\code{\link[=recordClades]{recordClades()}}, +\code{\link[=subMatrix]{subMatrix()}}, +\code{\link[=subTree]{subTree()}}, +\code{\link[=visualizeTree]{visualizeTree()}} } \author{ Charles Plessy diff --git a/man/extractValues.Rd b/man/extractValues.Rd index ea2910d..2b3cb89 100644 --- a/man/extractValues.Rd +++ b/man/extractValues.Rd @@ -26,9 +26,9 @@ unique(Halo_Tree$parent) |> sort() |> purrr::set_names() |> sapply(extractValues, Halo_Tree, Halo_PercentDiff) } \seealso{ -Other Functions for trees: -\code{\link{childSpecies}()}, -\code{\link{recordAncestor}()}, -\code{\link{subMatrix}()} +Other Functions for trees: +\code{\link[=childSpecies]{childSpecies()}}, +\code{\link[=recordAncestor]{recordAncestor()}}, +\code{\link[=subMatrix]{subMatrix()}} } \concept{Functions for trees} diff --git a/man/focalClade.Rd b/man/focalClade.Rd index 70ecc32..f56394e 100644 --- a/man/focalClade.Rd +++ b/man/focalClade.Rd @@ -29,13 +29,13 @@ Clades of interest to be plotted in color visualizeTree(Halo_Tree) + clades } \seealso{ -Other Focal clade functions: -\code{\link{eog}()}, -\code{\link{recordAncestor}()}, -\code{\link{recordClades}()}, -\code{\link{subMatrix}()}, -\code{\link{subTree}()}, -\code{\link{visualizeTree}()} +Other Focal clade functions: +\code{\link[=eog]{eog()}}, +\code{\link[=recordAncestor]{recordAncestor()}}, +\code{\link[=recordClades]{recordClades()}}, +\code{\link[=subMatrix]{subMatrix()}}, +\code{\link[=subTree]{subTree()}}, +\code{\link[=visualizeTree]{visualizeTree()}} } \author{ Charles Plessy diff --git a/man/formatStats.Rd b/man/formatStats.Rd index b000707..2eb32d4 100644 --- a/man/formatStats.Rd +++ b/man/formatStats.Rd @@ -33,8 +33,8 @@ exDataFrame <- formatStats(yamlFileData) exDataFrame[1:10,1:6] } \seealso{ -Other Data load functions: -\code{\link{resultFiles}()} +Other Data load functions: +\code{\link[=resultFiles]{resultFiles()}} } \author{ Noah Brenner diff --git a/man/getDivergenceTime.Rd b/man/getDivergenceTime.Rd new file mode 100644 index 0000000..c405320 --- /dev/null +++ b/man/getDivergenceTime.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/divergenceTimetree.R +\name{getDivergenceTime} +\alias{getDivergenceTime} +\title{Divergence time - TimeTree} +\usage{ +getDivergenceTime(taxid1, taxid2) +} +\arguments{ +\item{taxid1}{An integer: The NCBI id of one taxon} + +\item{taxid2}{An integer: The NCBI id of the other taxon} +} +\value{ +A list in which the first item is the median divergence time (\code{divtimeMedian}), +and the last two items are the divergence time confidence interval +(\code{divtimeCI_low} and \code{divtimeCI_high}). +If the divergence time is not found in the TimeTree database, it returns \code{NA}. +} +\description{ +Retrieves the divergence time of two species via the TimeTree API. +} +\examples{ +\dontrun{ +getDivergenceTime(9555,9601)} + +} +\author{ +Priscila Biller +} diff --git a/man/oikData.Rd b/man/oikData.Rd index 60fc056..5b47c94 100644 --- a/man/oikData.Rd +++ b/man/oikData.Rd @@ -28,7 +28,7 @@ by a beta version of the \url{https://nf-co.re/pairgenomealign} pipeline. oikData } \seealso{ -Other Lazy-loaded data: +Other Lazy-loaded data: \code{\link{Haloarchaea}} } \author{ diff --git a/man/recordAncestor.Rd b/man/recordAncestor.Rd index 7a66889..f062363 100644 --- a/man/recordAncestor.Rd +++ b/man/recordAncestor.Rd @@ -23,18 +23,18 @@ recordAncestor(Halo_DF, Halo_Tree)$MRCA } \seealso{ -Other Functions for trees: -\code{\link{childSpecies}()}, -\code{\link{extractValues}()}, -\code{\link{subMatrix}()} +Other Functions for trees: +\code{\link[=childSpecies]{childSpecies()}}, +\code{\link[=extractValues]{extractValues()}}, +\code{\link[=subMatrix]{subMatrix()}} -Other Focal clade functions: -\code{\link{eog}()}, -\code{\link{focalClade}()}, -\code{\link{recordClades}()}, -\code{\link{subMatrix}()}, -\code{\link{subTree}()}, -\code{\link{visualizeTree}()} +Other Focal clade functions: +\code{\link[=eog]{eog()}}, +\code{\link[=focalClade]{focalClade()}}, +\code{\link[=recordClades]{recordClades()}}, +\code{\link[=subMatrix]{subMatrix()}}, +\code{\link[=subTree]{subTree()}}, +\code{\link[=visualizeTree]{visualizeTree()}} } \author{ Noa Brenner diff --git a/man/recordClades.Rd b/man/recordClades.Rd index 9724c41..8d9857c 100644 --- a/man/recordClades.Rd +++ b/man/recordClades.Rd @@ -26,13 +26,13 @@ when it did not find it. recordClades(Halo_DF, Halo_FocalClades) } \seealso{ -Other Focal clade functions: -\code{\link{eog}()}, -\code{\link{focalClade}()}, -\code{\link{recordAncestor}()}, -\code{\link{subMatrix}()}, -\code{\link{subTree}()}, -\code{\link{visualizeTree}()} +Other Focal clade functions: +\code{\link[=eog]{eog()}}, +\code{\link[=focalClade]{focalClade()}}, +\code{\link[=recordAncestor]{recordAncestor()}}, +\code{\link[=subMatrix]{subMatrix()}}, +\code{\link[=subTree]{subTree()}}, +\code{\link[=visualizeTree]{visualizeTree()}} } \author{ Charles Plessy diff --git a/man/resultFiles.Rd b/man/resultFiles.Rd index 9483b17..f1fd465 100644 --- a/man/resultFiles.Rd +++ b/man/resultFiles.Rd @@ -30,8 +30,8 @@ system.file("extdata/yaml", package = "ScrambledTreeBuilder") |> resultFiles() } \seealso{ -Other Data load functions: -\code{\link{formatStats}()} +Other Data load functions: +\code{\link[=formatStats]{formatStats()}} } \author{ Charles Plessy diff --git a/man/subMatrix.Rd b/man/subMatrix.Rd index 6d34d0c..e649ffb 100644 --- a/man/subMatrix.Rd +++ b/man/subMatrix.Rd @@ -40,18 +40,18 @@ subTree(Halo_Tree, 9) |> subMatrix(Halo_PercentDiff) } \seealso{ -Other Functions for trees: -\code{\link{childSpecies}()}, -\code{\link{extractValues}()}, -\code{\link{recordAncestor}()} - -Other Focal clade functions: -\code{\link{eog}()}, -\code{\link{focalClade}()}, -\code{\link{recordAncestor}()}, -\code{\link{recordClades}()}, -\code{\link{subTree}()}, -\code{\link{visualizeTree}()} +Other Functions for trees: +\code{\link[=childSpecies]{childSpecies()}}, +\code{\link[=extractValues]{extractValues()}}, +\code{\link[=recordAncestor]{recordAncestor()}} + +Other Focal clade functions: +\code{\link[=eog]{eog()}}, +\code{\link[=focalClade]{focalClade()}}, +\code{\link[=recordAncestor]{recordAncestor()}}, +\code{\link[=recordClades]{recordClades()}}, +\code{\link[=subTree]{subTree()}}, +\code{\link[=visualizeTree]{visualizeTree()}} } \author{ Charles Plessy diff --git a/man/subTree.Rd b/man/subTree.Rd index 69ebffe..24dbe72 100644 --- a/man/subTree.Rd +++ b/man/subTree.Rd @@ -30,13 +30,13 @@ visualizeTree(subtree, subtree$node.orig) } \seealso{ -Other Focal clade functions: -\code{\link{eog}()}, -\code{\link{focalClade}()}, -\code{\link{recordAncestor}()}, -\code{\link{recordClades}()}, -\code{\link{subMatrix}()}, -\code{\link{visualizeTree}()} +Other Focal clade functions: +\code{\link[=eog]{eog()}}, +\code{\link[=focalClade]{focalClade()}}, +\code{\link[=recordAncestor]{recordAncestor()}}, +\code{\link[=recordClades]{recordClades()}}, +\code{\link[=subMatrix]{subMatrix()}}, +\code{\link[=visualizeTree]{visualizeTree()}} } \author{ Charles Plessy diff --git a/man/treeHeatMap.Rd b/man/treeHeatMap.Rd index 1d1e1a7..16ee2f1 100644 --- a/man/treeHeatMap.Rd +++ b/man/treeHeatMap.Rd @@ -30,10 +30,10 @@ treeHeatMap(Halo_PercentDiff, Halo_Tree, Halo_FocalClades) } \seealso{ -Other Plotting functions: -\code{\link{MRCA_2D_plot}()}, -\code{\link{ellipsePlot}()}, -\code{\link{visualizeTree}()} +Other Plotting functions: +\code{\link[=MRCA_2D_plot]{MRCA_2D_plot()}}, +\code{\link[=ellipsePlot]{ellipsePlot()}}, +\code{\link[=visualizeTree]{visualizeTree()}} } \author{ Charles Plessy diff --git a/man/visualizeTree.Rd b/man/visualizeTree.Rd index 29d4185..665e195 100644 --- a/man/visualizeTree.Rd +++ b/man/visualizeTree.Rd @@ -42,18 +42,18 @@ visualizeTree(Halo_Tree, value = Halo_Tree$Scrambling_index) visualizeTree(Halo_Tree, "Scrambling_index") # same } \seealso{ -Other Focal clade functions: -\code{\link{eog}()}, -\code{\link{focalClade}()}, -\code{\link{recordAncestor}()}, -\code{\link{recordClades}()}, -\code{\link{subMatrix}()}, -\code{\link{subTree}()} +Other Focal clade functions: +\code{\link[=eog]{eog()}}, +\code{\link[=focalClade]{focalClade()}}, +\code{\link[=recordAncestor]{recordAncestor()}}, +\code{\link[=recordClades]{recordClades()}}, +\code{\link[=subMatrix]{subMatrix()}}, +\code{\link[=subTree]{subTree()}} -Other Plotting functions: -\code{\link{MRCA_2D_plot}()}, -\code{\link{ellipsePlot}()}, -\code{\link{treeHeatMap}()} +Other Plotting functions: +\code{\link[=MRCA_2D_plot]{MRCA_2D_plot()}}, +\code{\link[=ellipsePlot]{ellipsePlot()}}, +\code{\link[=treeHeatMap]{treeHeatMap()}} } \author{ Noah Brenner From bf989f42887a7f4204a7e17afd1bac4d24367a52 Mon Sep 17 00:00:00 2001 From: pribiller Date: Thu, 10 Sep 2026 16:12:34 +0900 Subject: [PATCH 3/6] Update documentation Add documentation of new function. --- DESCRIPTION | 2 +- man/getDivergenceTime.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 258ac96..5d87247 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -52,7 +52,6 @@ Collate: 'averageResults.R' 'bootstrapTree.R' 'childSpecies.R' - 'divergenceTimetree.R' 'ellipsePlot.R' 'enr.R' 'eog.R' @@ -66,6 +65,7 @@ Collate: 'removeAssemblies.R' 'resultFiles.R' 'subMatrix.R' + 'timeTree.R' 'treeHeatMap.R' 'visualizeTree.R' 'zzz.R' diff --git a/man/getDivergenceTime.Rd b/man/getDivergenceTime.Rd index c405320..102743c 100644 --- a/man/getDivergenceTime.Rd +++ b/man/getDivergenceTime.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/divergenceTimetree.R +% Please edit documentation in R/timeTree.R \name{getDivergenceTime} \alias{getDivergenceTime} \title{Divergence time - TimeTree} From deb594042f2fcdade44b61cb7d5d1f469fa3c4c5 Mon Sep 17 00:00:00 2001 From: pribiller Date: Wed, 16 Sep 2026 13:51:39 +0900 Subject: [PATCH 4/6] Add timeline function Add a function that uses the TimeTree API to retrieve the divergence time of all species from the specified taxon to the common ancestor of all cellular organisms. --- NAMESPACE | 8 +++++--- R/timeTree.R | 54 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 5fb91aa..763a991 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,5 @@ # Generated by roxygen2: do not edit by hand -export("subTree,tbl_tree,double-method") -export("subTree,tbl_tree,integer-method") export(FocalCladeList) export(MRCA_2D_plot) export(MRCAs) @@ -15,6 +13,7 @@ export(extractValues) export(focalClade) export(formatStats) export(getDivergenceTime) +export(getTimeline) export(makeMatrix) export(makeTidyTree) export(makeValueTibble) @@ -93,7 +92,10 @@ importFrom(tidytree, tree_subset ) importFrom(treeio,MRCA) -importFrom(utils,read.delim) +importFrom(utils, + read.csv, + read.delim +) importFrom(yaml, read_yaml, yaml.load diff --git a/R/timeTree.R b/R/timeTree.R index 1d7e783..b954e4a 100644 --- a/R/timeTree.R +++ b/R/timeTree.R @@ -1,3 +1,49 @@ +#' Timeline - TimeTree +#' +#' Fetches a list of divergence times of nodes in TimeTree from the specified taxon to the common ancestor of all cellular organisms. +#' +#' @param taxid An integer: The NCBI id of a taxon +#' +#' @return A dataframe in which each row corresponds to an ancestor. +#' Each row contains the divergence time (`divtime`), its confidence interval +#' (`divtimeCI_low` and `divtimeCI_high`), and additional information, +#' including the scientific name (`name`) and taxonomic rank (`rank`). +#' If the divergence time is not found in the TimeTree database, it returns `NA`. +#' +#' @author Priscila Biller +#' +#' @examples +#' \dontrun{ +#' getTimeline(9555)} +#' +#' @importFrom httr GET content +#' @importFrom utils read.csv +#' +#' @export +getTimeline <- function(taxid) { + timetreeBaseTimeline <- "http://timetree.temple.edu/api/timeline/" + # Example of request to the TimeTree's API: + # - Request: http://timetree.temple.edu/api/timeline/9555 + # - Answer from TimeTree: + # ncbi-id,scientific_name,common_name,level,rank,branch_length,correction,ci_high,ci_low + # 84512,unnamed,,41,unknown,0.72,0,7.5,0.23 + # 84508,unnamed,,40,unknown,1.99,0,7.5,0.39 + # 84504,Papio,,39,genus,1.85,0,7.5,1.19 + # ... + timetreeRequest <- paste(timetreeBaseTimeline, taxid, sep="") + timetreeResponse <- GET(timetreeRequest) + lines <- content(timetreeResponse, as="text", encoding="UTF-8") + header <- lines |> strsplit(split="\n", fixed=TRUE) |> (\(x) x[[1]][1])() |> strsplit(split = ",", fixed = TRUE) |> (\(x) x[[1]])() # First line: header; Other lines: data. + if(length(header) >= 8) { + df <- read.csv(text=lines) + df <- df[ c("ncbi.id", "scientific_name", "rank", "branch_length", "ci_low", "ci_high")] + names(df) <- c("NCBIid", "name", "rank", "divtime", "divtimeCI_low", "divtimeCI_high") + df + } else { + NA + } +} + #' Divergence time - TimeTree #' #' Retrieves the divergence time of two species via the TimeTree API. @@ -5,7 +51,7 @@ #' @param taxid1 An integer: The NCBI id of one taxon #' @param taxid2 An integer: The NCBI id of the other taxon #' -#' @return A list in which the first item is the median divergence time (`divtimeMedian`), +#' @return A list in which the first item is the divergence time (`divtime`), #' and the last two items are the divergence time confidence interval #' (`divtimeCI_low` and `divtimeCI_high`). #' If the divergence time is not found in the TimeTree database, it returns `NA`. @@ -20,13 +66,13 @@ #' #' @export getDivergenceTime <- function(taxid1, taxid2) { - timetreePairwiseBase <- "http://timetree.temple.edu/api/pairwise/" + timetreeBasePairwise <- "http://timetree.temple.edu/api/pairwise/" # Example of request to the TimeTree's API: # - Request: http://timetree.temple.edu/api/pairwise/9555/9601 # - Answer from TimeTree: # taxon_a_id,taxon_b_id,scientific_name_a,scientific_name_b,all_total,precomputed_age,precomputed_ci_low,precomputed_ci_high,adjusted_age # 9555,9601,Papio anubis,Pongo abelii,83,28.82,26.8,30.6,0 - timetreeRequest <- paste(timetreePairwiseBase, taxid1, "/", taxid2, sep="") + timetreeRequest <- paste(timetreeBasePairwise, taxid1, "/", taxid2, sep="") timetreeResponse <- GET(timetreeRequest) lines <- content(timetreeResponse, as="text", encoding="UTF-8") # Break the whole content into lines, and gets the second line (data). @@ -35,7 +81,7 @@ getDivergenceTime <- function(taxid1, taxid2) { data <- strsplit(lineData, ",", fixed = TRUE)[[1]] # Check if the request returned something valid. if(length(data) >= 8) { - list(divtimeMedian=as.numeric(data[6]),divtimeCI_low=as.numeric(data[7]),divtimeCI_high=as.numeric(data[8])) + list(divtime=as.numeric(data[6]),divtimeCI_low=as.numeric(data[7]),divtimeCI_high=as.numeric(data[8])) } else { NA } From db102a22ca7cb6ca4095d1c8a3f0eb71e7444d05 Mon Sep 17 00:00:00 2001 From: pribiller Date: Wed, 16 Sep 2026 13:52:14 +0900 Subject: [PATCH 5/6] Update documentation Update the documentation for the two new functions, `getTimeline` and `getDivergenceTime`. --- man/getDivergenceTime.Rd | 2 +- man/getTimeline.Rd | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 man/getTimeline.Rd diff --git a/man/getDivergenceTime.Rd b/man/getDivergenceTime.Rd index 102743c..2aea125 100644 --- a/man/getDivergenceTime.Rd +++ b/man/getDivergenceTime.Rd @@ -12,7 +12,7 @@ getDivergenceTime(taxid1, taxid2) \item{taxid2}{An integer: The NCBI id of the other taxon} } \value{ -A list in which the first item is the median divergence time (\code{divtimeMedian}), +A list in which the first item is the divergence time (\code{divtime}), and the last two items are the divergence time confidence interval (\code{divtimeCI_low} and \code{divtimeCI_high}). If the divergence time is not found in the TimeTree database, it returns \code{NA}. diff --git a/man/getTimeline.Rd b/man/getTimeline.Rd new file mode 100644 index 0000000..b6a1af6 --- /dev/null +++ b/man/getTimeline.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/timeTree.R +\name{getTimeline} +\alias{getTimeline} +\title{Timeline - TimeTree} +\usage{ +getTimeline(taxid) +} +\arguments{ +\item{taxid}{An integer: The NCBI id of a taxon} +} +\value{ +A dataframe in which each row corresponds to an ancestor. +Each row contains the divergence time (\code{divtime}), its confidence interval +(\code{divtimeCI_low} and \code{divtimeCI_high}), and additional information, +including the scientific name (\code{name}) and taxonomic rank (\code{rank}). +If the divergence time is not found in the TimeTree database, it returns \code{NA}. +} +\description{ +Fetches a list of divergence times of nodes in TimeTree from the specified taxon to the common ancestor of all cellular organisms. +} +\examples{ +\dontrun{ +getTimeline(9555)} + +} +\author{ +Priscila Biller +} From 5cbc720d0719f8c36c36359d9a05199a5e64a7ae Mon Sep 17 00:00:00 2001 From: pribiller Date: Thu, 24 Sep 2026 12:34:42 +0900 Subject: [PATCH 6/6] Update in the TimeTree API As the TimeTree API was updated, the names of some fields changed (e.g. "branch_length" is now "summary_age"), so the code had to be modified accordingly. --- R/timeTree.R | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/R/timeTree.R b/R/timeTree.R index b954e4a..be31c76 100644 --- a/R/timeTree.R +++ b/R/timeTree.R @@ -25,10 +25,12 @@ getTimeline <- function(taxid) { # Example of request to the TimeTree's API: # - Request: http://timetree.temple.edu/api/timeline/9555 # - Answer from TimeTree: - # ncbi-id,scientific_name,common_name,level,rank,branch_length,correction,ci_high,ci_low - # 84512,unnamed,,41,unknown,0.72,0,7.5,0.23 - # 84508,unnamed,,40,unknown,1.99,0,7.5,0.39 - # 84504,Papio,,39,genus,1.85,0,7.5,1.19 + # ncbi-id,scientific_name,common_name,level,rank,summary_age,adjusted_age,ci_high,ci_low + # -1,unnamed,,46,unknown,1.1,1.1,1.48,0.72 + # -1,unnamed,,45,unknown,1.27,1.36,1.97,0.56 + # -1,unnamed,,44,unknown,1.27,1.58,1.97,0.56 + # 9554,Papio,,43,genus,1.9,1.9,2.31,1.48 + # -1,unnamed,,42,unknown,4.68,4.68,5.08,4.27 # ... timetreeRequest <- paste(timetreeBaseTimeline, taxid, sep="") timetreeResponse <- GET(timetreeRequest) @@ -36,8 +38,11 @@ getTimeline <- function(taxid) { header <- lines |> strsplit(split="\n", fixed=TRUE) |> (\(x) x[[1]][1])() |> strsplit(split = ",", fixed = TRUE) |> (\(x) x[[1]])() # First line: header; Other lines: data. if(length(header) >= 8) { df <- read.csv(text=lines) - df <- df[ c("ncbi.id", "scientific_name", "rank", "branch_length", "ci_low", "ci_high")] - names(df) <- c("NCBIid", "name", "rank", "divtime", "divtimeCI_low", "divtimeCI_high") + # In TimeTree there are two possibilities for the divergence time (here we go with "adjusted age"): + # - "Summary age" is the median of all studies. + # - "Adjusted age" is the adjusted time, computed in cases where a node is older than its parent in the global timetree reconstructed from individual timetrees. + df <- df[ c("ncbi.id", "scientific_name", "rank", "adjusted_age", "ci_low", "ci_high")] + names(df) <- c("NCBIid", "name", "rank", "divtime", "divtimeCI_low", "divtimeCI_high") df } else { NA