| Title: | Taxonomic Hierarchy Distances and Lineage Analysis |
|---|---|
| Description: | Computes distances between taxonomic hierarchy nodes using lineage data retrieved from The Taxonomicon <http://taxonomicon.taxonomy.nl>. For distinct nodes, distance is defined as the reciprocal of the depth of their most recent common ancestor; identical nodes have distance zero. This definition yields an ultrametric within each connected hierarchy. Functions are provided for auditable name resolution, online or user-supplied lineage analysis, clade membership, pairwise, matrix, and cross-set distance calculation, classification trees with named clades, hierarchical clustering, principal coordinates analysis, portable and comparable JSON analysis bundles, and cache management. Distance matrices are returned as base R 'dist' objects. The distances represent classification depth rather than evolutionary time or phylogenetic branch length. |
| Authors: | Rodrigo Fonseca Villa [aut, cre] (ORCID: <https://orcid.org/0009-0005-2938-2270>) |
| Maintainer: | Rodrigo Fonseca Villa <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.9.0 |
| Built: | 2026-10-12 16:20:31 UTC |
| Source: | https://github.com/rodrigosqrt3/taxodist |
taxodist retrieves taxonomic lineages from The Taxonomicon (taxonomy.nl) or accepts user-supplied lineages, then computes distances from the depth of the most recent common ancestor. The resulting values describe separation within the source classification; they are not estimates of evolutionary time or phylogenetic branch length.
get_lineage() — retrieve the full lineage of any taxon
taxo_resolve() — resolve and audit a batch of names or IDs
taxo_from_lineages() — create an offline resolution from local lineages
taxo_distance() — compute the ultrametric distance between two taxa
mrca() — find the most recent common ancestor
distance_matrix() — compute all pairwise distances for a set of taxa
cross_distances() — compute distances between two sets of taxa
taxo_tree() — build a phylo tree whose nodes are named clades
closest_relative() — find the closest relative among candidates
compare_lineages() — print a side-by-side lineage comparison
shared_clades() — list clades shared between two taxa
is_member() — test clade membership
filter_clade() — filter taxa by clade membership
check_coverage() — check Taxonomicon coverage for a list of taxa
lineage_depth() — get the lineage depth of a taxon
taxo_bundle() — preserve an analysis and its provenance
compare_bundles() — report what changed between two analyses
save_cache() / load_cache() — persist retrieved lineages
clear_cache() — clear the session lineage cache
The distance metric is based on the depth of the most recent common ancestor (MRCA):
Here, h(A,B) denotes the depth of the MRCA. The deeper the shared
ancestor, the smaller the distance. Zero is reserved
for identical hierarchy nodes; distinct ancestor-descendant pairs therefore
have positive distance. The measure is an ultrametric on each connected
taxonomic hierarchy. Its numerical values depend on the resolution of the
classification returned by the data source.
Online lineage data is sourced from The Taxonomicon (taxonomy.nl), based on
Systema Naturae 2000 by S.J. Brands (1989 onwards). Please cite that
resource when using its classifications, or record the relevant source when
using taxo_from_lineages().
taxodist.max_tries: number of attempts made for each request to The
Taxonomicon (default 3). Network errors and server errors (5xx) are
retried with exponential backoff; client errors (4xx) are not.
Maintainer: Rodrigo Fonseca Villa [email protected] (ORCID)
Brands, S.J. (1989 onwards). Systema Naturae 2000. Amsterdam, The Netherlands. Retrieved from The Taxonomicon, http://taxonomicon.taxonomy.nl.
Useful links:
Report bugs at https://github.com/rodrigosqrt3/taxodist/issues
Reports the number of cached entries, their total memory footprint, and the names of all taxa whose lineages are stored in the current session. Useful for understanding what has already been retrieved before running further computations.
cache_info()cache_info()
Invisibly returns a list with elements:
n_lineagesInteger. Number of cached lineages.
n_idsInteger. Number of cached taxon IDs.
taxaCharacter vector of the taxa whose lineages are cached (the terminal node of each lineage).
idsCharacter vector of the Taxonomicon IDs of those lineages,
in the same order as taxa.
size_bytesNumeric. Total memory used by the cache.
save_cache(), load_cache(), clear_cache()
get_lineage("Tyrannosaurus") get_lineage("Velociraptor") cache_info()get_lineage("Tyrannosaurus") get_lineage("Velociraptor") cache_info()
Queries The Taxonomicon for a taxon name and returns a logical indicating whether the taxon was found. Useful for pre-screening a list of names before running distance computations.
check_coverage(taxa, verbose = FALSE)check_coverage(taxa, verbose = FALSE)
taxa |
A character vector of one or more taxon names. |
verbose |
Logical. If |
A named logical vector. TRUE indicates the taxon was found,
FALSE that it is absent from The Taxonomicon, and NA that the
answer is unknown because the server could not be reached.
taxo_resolve() for a complete audit of each name.
check_coverage(c("Tyrannosaurus", "Velociraptor", "Fakeosaurus"))check_coverage(c("Tyrannosaurus", "Velociraptor", "Fakeosaurus"))
Clears all cached lineages stored in the current R session. Useful when you suspect cached data is stale or want to force fresh retrieval.
clear_cache()clear_cache()
Invisibly returns NULL.
clear_cache()clear_cache()
Given a query taxon and a vector of candidate taxa, returns the candidate with the smallest taxonomic hierarchy distance to the query.
closest_relative(taxon, candidates, verbose = FALSE)closest_relative(taxon, candidates, verbose = FALSE)
taxon |
A character string giving the query taxon name. |
candidates |
A character vector of candidate taxon names to compare against. |
verbose |
Logical. If |
A data frame with columns taxon (candidate name) and distance
(ultrametric distance), sorted by distance ascending. Returns NULL if
the query taxon cannot be found. An empty candidate vector returns an
empty data frame with the same columns.
closest_relative("Tyrannosaurus", c("Velociraptor", "Triceratops", "Brachiosaurus", "Allosaurus"))closest_relative("Tyrannosaurus", c("Velociraptor", "Triceratops", "Brachiosaurus", "Allosaurus"))
Reports how the resolution of each taxon and the pairwise distances changed between two bundles. A typical use is to re-run an analysis months later and check whether updates to The Taxonomicon changed any lineage, selected identifier, or distance.
compare_bundles(old, new, tolerance = sqrt(.Machine$double.eps))compare_bundles(old, new, tolerance = sqrt(.Machine$double.eps))
old, new
|
|
tolerance |
Numeric tolerance below which two distances are considered equal. |
An object of class taxodist_bundle_comparison, a list with:
taxaA data frame with one row per input label found in either
bundle and columns input, change, status_old, status_new,
id_old, id_new, depth_old, and depth_new. change is one of
"unchanged", "added", "removed", "status_changed",
"id_changed", or "lineage_changed".
distancesA data frame of the pairs of inputs present in both
bundles whose distance changed, with columns taxon_a, taxon_b,
distance_old, and distance_new.
created_old, created_new
Creation timestamps of the bundles.
taxo_bundle(), read_taxodist_bundle()
before <- taxo_bundle(taxo_from_lineages(list( Alpha = c("Biota", "Animalia", "Alpha"), Beta = c("Biota", "Animalia", "Beta") )), progress = FALSE) after <- taxo_bundle(taxo_from_lineages(list( Alpha = c("Biota", "Animalia", "Chordata", "Alpha"), Beta = c("Biota", "Animalia", "Beta") )), progress = FALSE) compare_bundles(before, after)before <- taxo_bundle(taxo_from_lineages(list( Alpha = c("Biota", "Animalia", "Alpha"), Beta = c("Biota", "Animalia", "Beta") )), progress = FALSE) after <- taxo_bundle(taxo_from_lineages(list( Alpha = c("Biota", "Animalia", "Chordata", "Alpha"), Beta = c("Biota", "Animalia", "Beta") )), progress = FALSE) compare_bundles(before, after)
Prints the lineages of two taxa aligned at their most recent common ancestor, making the point of divergence easy to identify.
compare_lineages(taxon_a, taxon_b, verbose = FALSE)compare_lineages(taxon_a, taxon_b, verbose = FALSE)
taxon_a |
A character string giving the first taxon name. |
taxon_b |
A character string giving the second taxon name. |
verbose |
Logical. If |
Invisibly returns a list with elements lineage_a, lineage_b,
and mrca_depth.
compare_lineages("Tyrannosaurus", "Velociraptor") compare_lineages("Tyrannosaurus", "Triceratops")compare_lineages("Tyrannosaurus", "Velociraptor") compare_lineages("Tyrannosaurus", "Triceratops")
Computes the rectangular matrix of distances from every taxon in x to
every taxon in y, for example from the species of a community to a set
of reference taxa. This avoids computing the full square matrix of the
combined set.
cross_distances(x, y, verbose = FALSE, progress = TRUE)cross_distances(x, y, verbose = FALSE, progress = TRUE)
x, y
|
Character vectors of taxon names, or |
verbose |
Logical. If |
progress |
Logical. If |
A numeric matrix with one row per element of x and one column
per element of y, labeled with the inputs. Taxa that could not be
found have NA distances; taxa without a common ancestor have Inf.
distance_matrix(), focal_distances()
community <- taxo_from_lineages(list( Alpha = c("Biota", "Animalia", "Chordata", "Alpha"), Beta = c("Biota", "Animalia", "Arthropoda", "Beta") )) reference <- taxo_from_lineages(list( Gamma = c("Biota", "Animalia", "Chordata", "Gamma"), Delta = c("Biota", "Plantae", "Delta") )) cross_distances(community, reference)community <- taxo_from_lineages(list( Alpha = c("Biota", "Animalia", "Chordata", "Alpha"), Beta = c("Biota", "Animalia", "Arthropoda", "Beta") )) reference <- taxo_from_lineages(list( Gamma = c("Biota", "Animalia", "Chordata", "Gamma"), Delta = c("Biota", "Plantae", "Delta") )) cross_distances(community, reference)
Given a vector of taxon names, computes all pairwise taxonomic hierarchy distances and returns a symmetric distance matrix. Lineages are cached after first retrieval to minimise redundant network requests.
distance_matrix(taxa, verbose = FALSE, progress = TRUE)distance_matrix(taxa, verbose = FALSE, progress = TRUE)
taxa |
A character vector of taxon names, a |
verbose |
Logical. If |
progress |
Logical. If |
Distances are computed in vectorized form: every lineage node is first
replaced by an integer identifying its complete path from the root, so the
MRCA depth of two taxa is simply the number of depths at which these
identifiers agree. This gives exactly the same values as
taxo_distance() while scaling to thousands of taxa.
A symmetric numeric matrix of class "dist" containing pairwise
distances. Row and column names are set to the input taxon names.
Taxa that could not be found are included with NA distances.
An empty input returns an empty "dist" object.
taxo_distance(), cross_distances(), closest_relative(),
taxo_resolve(), taxo_bundle(), taxo_tree()
theropods <- c("Tyrannosaurus", "Velociraptor", "Spinosaurus", "Allosaurus", "Carnotaurus") mat <- distance_matrix(theropods) print(mat)theropods <- c("Tyrannosaurus", "Velociraptor", "Spinosaurus", "Allosaurus", "Carnotaurus") mat <- distance_matrix(theropods) print(mat)
Given a vector of taxon names and a clade name, returns only those taxa whose lineage includes the specified clade.
filter_clade(taxa, clade, verbose = FALSE)filter_clade(taxa, clade, verbose = FALSE)
taxa |
A character vector of taxon names. |
clade |
A character string giving the clade to filter by. |
verbose |
Logical. If |
A character vector of taxa that are members of the specified clade.
taxa <- c("Tyrannosaurus", "Triceratops", "Velociraptor", "Brachiosaurus", "Homo") filter_clade(taxa, "Theropoda") filter_clade(taxa, "Dinosauria")taxa <- c("Tyrannosaurus", "Triceratops", "Velociraptor", "Brachiosaurus", "Homo") filter_clade(taxa, "Theropoda") filter_clade(taxa, "Dinosauria")
Given a focal taxon and a vector of community taxa, retrieves all lineages and computes the taxonomic distance from the focal taxon to each member of the community. Returns a sorted data frame that also reports the most recent common ancestor (MRCA) for each pair, making it easy to interpret why a taxon is close or distant.
focal_distances(focal, community, verbose = FALSE, progress = TRUE)focal_distances(focal, community, verbose = FALSE, progress = TRUE)
focal |
A character string giving the focal taxon name. |
community |
A character vector of community taxon names to compare
against. The focal taxon may be included — it will receive a distance
of |
verbose |
Logical. If |
progress |
Logical. If |
A data frame of class "taxodist_focal" with columns:
taxonCharacter. Community taxon name.
distanceNumeric. Taxonomic distance to the focal taxon.
mrcaCharacter. Name of the most recent common ancestor.
mrca_depthInteger. Depth of the MRCA node.
Rows are sorted by distance ascending (closest relatives first).
Returns NULL if the focal taxon cannot be found. An empty community
returns an empty object with the same columns and class.
closest_relative(), cross_distances(), distance_matrix(),
taxo_distance()
community <- c("Velociraptor", "Triceratops", "Brachiosaurus", "Allosaurus", "Homo", "Quercus") focal_distances("Tyrannosaurus", community)community <- c("Velociraptor", "Triceratops", "Brachiosaurus", "Allosaurus", "Homo", "Quercus") focal_distances("Tyrannosaurus", community)
A convenience wrapper that combines get_taxonomicon_id() and
get_lineage_by_id() into a single call. Given a taxon name, returns
its complete lineage from root to tip.
get_lineage(taxon, clean = TRUE, verbose = FALSE)get_lineage(taxon, clean = TRUE, verbose = FALSE)
taxon |
A single character string giving the taxon name or numeric Taxonomicon ID. Leading, trailing, and repeated internal whitespace is ignored, and the queried name is matched case-insensitively when truncating the lineage. |
clean |
Logical. If |
verbose |
Logical. If |
A character vector of clade names ordered from root to tip, or
NULL if the taxon cannot be found.
get_lineage("Tyrannosaurus") get_lineage("Homo sapiens") get_lineage("Quercus robur")get_lineage("Tyrannosaurus") get_lineage("Homo sapiens") get_lineage("Quercus robur")
Given a Taxonomicon numeric ID, retrieves and parses the complete hierarchical lineage from root (Natura) to the taxon itself. The lineage is returned as a character vector ordered from root to tip.
get_lineage_by_id(taxon_id, clean = TRUE, verbose = FALSE)get_lineage_by_id(taxon_id, clean = TRUE, verbose = FALSE)
taxon_id |
A single numeric or character Taxonomicon ID.
Obtain this with |
clean |
Logical. If |
verbose |
Logical. If |
Lineage data is sourced from The Taxonomicon, which is based on Systema Naturae 2000 (Brands, S.J., 1989 onwards). Lineage resolution varies among taxonomic groups and may change when the source classification is updated. Because hierarchy distances depend on lineage depth, analyses should record the retrieval date and package version.
Cleaned and uncleaned lineages are cached separately, so the value of
clean used in an earlier call never affects later calls.
A character vector of clade names from root to tip, or NULL if
taxon_id is not a numeric ID or retrieval fails.
get_lineage(), taxo_distance()
id <- get_taxonomicon_id("Tyrannosaurus") lin <- get_lineage_by_id(id) print(lin)id <- get_taxonomicon_id("Tyrannosaurus") lin <- get_lineage_by_id(id) print(lin)
Queries The Taxonomicon (taxonomy.nl) to retrieve the internal numeric identifier for a given taxon name. The search filters out non-biological entities such as astronomical objects that may share the same name.
get_taxonomicon_id(taxon, verbose = FALSE)get_taxonomicon_id(taxon, verbose = FALSE)
taxon |
A character string giving the taxon name to search for.
Typically a genus name (e.g., |
verbose |
Logical. If |
The function queries the static search endpoint at
taxonomicon.taxonomy.nl/TaxonList.aspx and parses the resulting HTML
to extract the taxon ID from the hierarchy link. Only candidates whose
lineage descends from Biota are retained, which excludes astronomical
and other non-biological homonyms. When several biological candidates
remain, those whose lineage contains the queried name are preferred and a
warning lists the alternatives.
Transient network failures are retried (three attempts by default; see
the taxodist.max_tries option in taxodist-package).
A character string containing the Taxonomicon numeric ID, or NULL
if the taxon is not found or the server cannot be reached.
get_lineage(), taxo_resolve(), check_coverage()
get_taxonomicon_id("Tyrannosaurus") # returns "50841" get_taxonomicon_id("Homo") get_taxonomicon_id("Quercus")get_taxonomicon_id("Tyrannosaurus") # returns "50841" get_taxonomicon_id("Homo") get_taxonomicon_id("Quercus")
Returns TRUE if taxon is a member of clade — i.e., if the complete
clade name appears in the taxon's lineage. Matching is case-insensitive and
ignores leading and trailing whitespace.
is_member(taxon, clade, verbose = FALSE)is_member(taxon, clade, verbose = FALSE)
taxon |
A character string giving the taxon name to test. |
clade |
A character string giving the clade name to test membership in. |
verbose |
Logical. If |
A logical value, or NULL if the taxon cannot be found.
is_member("Tyrannosaurus", "Theropoda") # TRUE is_member("Triceratops", "Theropoda") # FALSE is_member("Homo", "Amniota") # TRUEis_member("Tyrannosaurus", "Theropoda") # TRUE is_member("Triceratops", "Theropoda") # FALSE is_member("Homo", "Amniota") # TRUE
Returns the number of nodes in the lineage of a taxon, from root to tip. This reflects how deeply nested the taxon is within the taxonomic hierarchy.
lineage_depth(taxon, verbose = FALSE)lineage_depth(taxon, verbose = FALSE)
taxon |
A character string giving the taxon name. |
verbose |
Logical. If |
An integer giving the lineage depth, or NULL if the taxon cannot
be found.
lineage_depth("Tyrannosaurus") # deep — many intermediate clades lineage_depth("Biota") # shallow — near rootlineage_depth("Tyrannosaurus") # deep — many intermediate clades lineage_depth("Biota") # shallow — near root
Restores lineage data saved with save_cache() into the current session
cache, avoiding network requests for taxa already retrieved in a previous
session. The file structure is validated before the current cache is
modified.
load_cache(file = NULL)load_cache(file = NULL)
file |
Path to an |
Invisibly returns NULL.
tmp <- tempfile(fileext = ".rds") save_cache(tmp) load_cache(tmp) unlink(tmp)tmp <- tempfile(fileext = ".rds") save_cache(tmp) load_cache(tmp) unlink(tmp)
Retrieves the lineages of two taxa and returns the name of their most recent common ancestor (MRCA) — the deepest node shared by both lineages.
mrca(taxon_a, taxon_b, verbose = FALSE)mrca(taxon_a, taxon_b, verbose = FALSE)
taxon_a |
A character string giving the first taxon name. |
taxon_b |
A character string giving the second taxon name. |
verbose |
Logical. If |
A character string giving the name of the MRCA, or NULL if
either taxon cannot be found or no common ancestor exists.
mrca("Tyrannosaurus", "Velociraptor") # "Tyrannoraptora" mrca("Tyrannosaurus", "Triceratops") # "Dinosauria" mrca("Tyrannosaurus", "Homo") # "Amniota"mrca("Tyrannosaurus", "Velociraptor") # "Tyrannoraptora" mrca("Tyrannosaurus", "Triceratops") # "Dinosauria" mrca("Tyrannosaurus", "Homo") # "Amniota"
Plots the hierarchical clustering dendrogram produced by taxo_cluster().
## S3 method for class 'taxodist_cluster' plot( x, main = "Taxonomic Clustering", xlab = "", sub = paste("Method:", if (!is.null(x$hclust)) x$hclust$method else "None"), ... )## S3 method for class 'taxodist_cluster' plot( x, main = "Taxonomic Clustering", xlab = "", sub = paste("Method:", if (!is.null(x$hclust)) x$hclust$method else "None"), ... )
x |
A |
main |
Plot title. Default: |
xlab |
X-axis label. Default: |
sub |
Subtitle. Default: clustering method used. |
... |
Additional arguments passed to |
Invisibly returns x. Called for side effects (plotting).
taxa <- c("Tyrannosaurus", "Velociraptor", "Triceratops", "Brachiosaurus") cl <- taxo_cluster(taxa) plot(cl)taxa <- c("Tyrannosaurus", "Velociraptor", "Triceratops", "Brachiosaurus") cl <- taxo_cluster(taxa) plot(cl)
Plots the PCoA ordination produced by taxo_ordinate().
## S3 method for class 'taxodist_ord' plot( x, main = "Taxonomic Ordination (PCoA)", xlab = "PC1", ylab = "PC2", labels = if (!is.null(x$points)) rownames(x$points) else NULL, ... )## S3 method for class 'taxodist_ord' plot( x, main = "Taxonomic Ordination (PCoA)", xlab = "PC1", ylab = "PC2", labels = if (!is.null(x$points)) rownames(x$points) else NULL, ... )
x |
A |
main |
Plot title. Default: |
xlab |
X-axis label. Default: |
ylab |
Y-axis label. Default: |
labels |
Character vector of labels. Default: rownames of points. |
... |
Additional arguments passed to |
Invisibly returns x. Called for side effects (plotting).
taxa <- c("Tyrannosaurus", "Velociraptor", "Triceratops", "Brachiosaurus") ord <- taxo_ordinate(taxa) plot(ord)taxa <- c("Tyrannosaurus", "Velociraptor", "Triceratops", "Brachiosaurus") ord <- taxo_ordinate(taxa) plot(ord)
Print a taxodist bundle
## S3 method for class 'taxodist_bundle' print(x, ...)## S3 method for class 'taxodist_bundle' print(x, ...)
x |
A |
... |
Additional arguments (currently ignored). |
Invisibly returns x.
Print a taxodist bundle comparison
## S3 method for class 'taxodist_bundle_comparison' print(x, ...)## S3 method for class 'taxodist_bundle_comparison' print(x, ...)
x |
A |
... |
Additional arguments (currently ignored). |
Invisibly returns x.
Print method for taxodist_focal objects
## S3 method for class 'taxodist_focal' print(x, ...)## S3 method for class 'taxodist_focal' print(x, ...)
x |
A |
... |
Additional arguments (ignored). |
Invisibly returns x. Called for side effects (printing).
Print method for taxodist_path objects
## S3 method for class 'taxodist_path' print(x, ...)## S3 method for class 'taxodist_path' print(x, ...)
x |
A |
... |
Additional arguments (ignored). |
Invisibly returns x. Called for side effects (printing).
Print an auditable taxon-resolution table
## S3 method for class 'taxodist_resolution' print(x, ...)## S3 method for class 'taxodist_resolution' print(x, ...)
x |
A |
... |
Additional arguments passed to |
Invisibly returns x.
Print method for taxodist distance results
## S3 method for class 'taxodist_result' print(x, ...)## S3 method for class 'taxodist_result' print(x, ...)
x |
A list returned by |
... |
Additional arguments (ignored). |
Invisibly returns x. Called for side effects (printing).
Read a portable taxodist JSON bundle
read_taxodist_bundle(file)read_taxodist_bundle(file)
file |
Path to a JSON file created by |
A reconstructed taxodist_bundle object.
Serialises the current session cache to an .rds file so it can be
restored in a future session with load_cache(). Useful for
reproducibility and for avoiding repeated network requests.
save_cache(file = NULL)save_cache(file = NULL)
file |
Path to the |
Invisibly returns the path of the written file.
tmp <- tempfile(fileext = ".rds") save_cache(tmp) unlink(tmp)tmp <- tempfile(fileext = ".rds") save_cache(tmp) unlink(tmp)
Prints a summary of the PCoA ordination, including the goodness-of-fit and the proportion of variance explained by each principal coordinate.
## S3 method for class 'taxodist_ord' summary(object, ...)## S3 method for class 'taxodist_ord' summary(object, ...)
object |
A |
... |
Additional arguments (ignored). |
Invisibly returns a data frame containing the eigenvalues and variance explained by each dimension.
Combines an auditable taxon-resolution table, its distance matrix, the
distance definition, and provenance metadata in one self-contained object.
The bundle can be written to a language-independent JSON file with
write_taxodist_bundle().
taxo_bundle( taxa, ambiguity = c("warn", "first", "error"), verbose = FALSE, progress = TRUE, within = NULL )taxo_bundle( taxa, ambiguity = c("warn", "first", "error"), verbose = FALSE, progress = TRUE, within = NULL )
taxa |
A character vector accepted by |
ambiguity, verbose, progress, within
|
Passed to |
An object of class taxodist_bundle containing schema_version,
created_at, source, software, metric, resolution, and matrix.
write_taxodist_bundle(), read_taxodist_bundle(),
compare_bundles()
bundle <- taxo_bundle(c("Tyrannosaurus", "Triceratops", "Homo")) distance_matrix(bundle)bundle <- taxo_bundle(c("Tyrannosaurus", "Triceratops", "Homo")) distance_matrix(bundle)
Computes pairwise taxonomic distances and performs hierarchical clustering.
taxo_cluster(taxa, method = "average", ...)taxo_cluster(taxa, method = "average", ...)
taxa |
A character vector of taxon names, or a |
method |
Clustering method passed to |
... |
Additional arguments passed to |
An object of class "taxodist_cluster" — a list with:
hclustThe stats::hclust() result.
distThe underlying distance matrix.
If fewer than two taxa are supplied, hclust is NULL and the distance
object is preserved.
taxo_tree() for a tree whose internal nodes are the named clades
of the classification, taxo_ordinate(), distance_matrix()
taxa <- c("Tyrannosaurus", "Velociraptor", "Triceratops", "Brachiosaurus") cl <- taxo_cluster(taxa) if (!is.null(cl$hclust)) { plot(cl) }taxa <- c("Tyrannosaurus", "Velociraptor", "Triceratops", "Brachiosaurus") cl <- taxo_cluster(taxa) if (!is.null(cl$hclust)) { plot(cl) }
Given two taxon names, retrieves their lineages from The Taxonomicon and computes a taxonomic distance based on the depth of their most recent common ancestor (MRCA):
taxo_distance(taxon_a, taxon_b, verbose = FALSE)taxo_distance(taxon_a, taxon_b, verbose = FALSE)
taxon_a |
A character string giving the first taxon name. |
taxon_b |
A character string giving the second taxon name. |
verbose |
Logical. If |
Here, h(A,B) is the depth of the MRCA of A and B. The deeper the
shared ancestor, the smaller (closer to zero) the distance.
Distinct nodes always have positive distance, including when one taxon is
an ancestor of the other. This makes the measure an ultrametric on each
connected taxonomic hierarchy. Use is_member() when the question is
whether one taxon belongs to a clade rather than how far apart the two
hierarchy nodes are.
A named list of class "taxodist_result" with the following elements:
distanceNumeric. The distance between the two taxa. Returns 0 only when their complete lineages are identical.
mrcaCharacter. The name of the most recent common ancestor.
mrca_depthInteger. The depth of the MRCA node.
depth_aInteger. The lineage depth of taxon A.
depth_bInteger. The lineage depth of taxon B.
taxon_aCharacter. Name of the first taxon.
taxon_bCharacter. Name of the second taxon.
Returns NULL if either taxon cannot be found.
Brands, S.J. (1989 onwards). Systema Naturae 2000. Amsterdam, The Netherlands. Retrieved from The Taxonomicon, http://taxonomicon.taxonomy.nl.
mrca(), distance_matrix(), get_lineage(), is_member(),
taxo_path()
# Distance between two theropods taxo_distance("Tyrannosaurus", "Velociraptor") # Distance between very distantly related taxa taxo_distance("Tyrannosaurus", "Quercus") # Distance between two oviraptorid genera taxo_distance("Nomingia", "Huanansaurus")# Distance between two theropods taxo_distance("Tyrannosaurus", "Velociraptor") # Distance between very distantly related taxa taxo_distance("Tyrannosaurus", "Quercus") # Distance between two oviraptorid genera taxo_distance("Nomingia", "Huanansaurus")
Builds a taxodist_resolution object without consulting an online
taxonomy service. This is useful for unpublished classifications, curated
local taxonomies, frozen analyses, and fully offline workflows.
taxo_from_lineages(lineages, ids = NULL, source = "user-supplied")taxo_from_lineages(lineages, ids = NULL, source = "user-supplied")
lineages |
A named list of character vectors ordered from root to the focal taxon. List names become the input labels. |
ids |
Optional character vector of unique identifiers, in the same
order as |
source |
A non-empty character string describing the lineage source. |
A taxodist_resolution object that can be passed to
distance_matrix() or taxo_bundle().
taxo_resolve(), distance_matrix(), taxo_bundle()
lineages <- list( Alpha = c("Biota", "Animalia", "Alpha"), Beta = c("Biota", "Animalia", "Beta") ) resolved <- taxo_from_lineages(lineages) distance_matrix(resolved)lineages <- list( Alpha = c("Biota", "Animalia", "Alpha"), Beta = c("Biota", "Animalia", "Beta") ) resolved <- taxo_from_lineages(lineages) distance_matrix(resolved)
Computes pairwise taxonomic distances and plots a heatmap with hierarchical clustering dendrograms on the margins. Darker/hotter colors typically represent smaller distances (closer relatives).
taxo_heatmap(taxa, ..., verbose = FALSE, progress = TRUE)taxo_heatmap(taxa, ..., verbose = FALSE, progress = TRUE)
taxa |
A character vector of taxon names, a |
... |
Additional graphical arguments passed to |
verbose, progress
|
Passed to |
Invisibly returns the underlying dist object.
Called primarily for its side effect (plotting).
If fewer than two taxa are supplied, plotting is skipped.
taxa <- c("Tyrannosaurus", "Velociraptor", "Homo", "Panthera", "Quercus") taxo_heatmap(taxa, col = hcl.colors(12, "YlOrRd"), progress = FALSE)taxa <- c("Tyrannosaurus", "Velociraptor", "Homo", "Panthera", "Quercus") taxo_heatmap(taxa, col = hcl.colors(12, "YlOrRd"), progress = FALSE)
Computes pairwise taxonomic distances and applies classical multidimensional scaling (PCoA) to project taxa into a low-dimensional space.
taxo_ordinate(taxa, k = 2, ...)taxo_ordinate(taxa, k = 2, ...)
taxa |
A character vector of taxon names, or a |
k |
Number of dimensions. Default |
... |
Additional arguments passed to |
An object of class "taxodist_ord" — a list with:
pointsA matrix of coordinates (taxa x k dimensions).
distThe underlying distance matrix.
GOFGoodness-of-fit from stats::cmdscale().
eigThe eigenvalues computed during PCoA.
At least two taxa are required. When k is greater than the maximum
possible dimension (n - 1), it is reduced automatically.
taxo_cluster(), distance_matrix()
taxa <- c("Tyrannosaurus", "Velociraptor", "Triceratops", "Brachiosaurus") ord <- taxo_ordinate(taxa) if (!is.null(ord$points)) { plot(ord$points, type = "n") text(ord$points, labels = rownames(ord$points)) }taxa <- c("Tyrannosaurus", "Velociraptor", "Triceratops", "Brachiosaurus") ord <- taxo_ordinate(taxa) if (!is.null(ord$points)) { plot(ord$points, type = "n") text(ord$points, labels = rownames(ord$points)) }
Returns the full node-by-node path from one taxon up to their most recent common ancestor (MRCA) and back down to the other taxon. The result is a data frame with one row per node, making it easy to inspect, filter, or pipe into other functions.
taxo_path(taxon_a, taxon_b, verbose = FALSE)taxo_path(taxon_a, taxon_b, verbose = FALSE)
taxon_a |
A character string giving the first taxon name. |
taxon_b |
A character string giving the second taxon name. |
verbose |
Logical. If |
A data frame of class "taxodist_path" with columns:
nodeCharacter. The clade or taxon name at this step.
depthInteger. The depth of this node in the full lineage of its side (or the shared lineage for the MRCA).
directionCharacter. One of "a" (ascending from taxon A to
MRCA), "mrca" (the shared ancestor), or "b" (descending from MRCA
to taxon B).
Returns NULL if either taxon cannot be found or if their lineages have no
common ancestor.
mrca(), shared_clades(), compare_lineages()
taxo_path("Tyrannosaurus", "Velociraptor") taxo_path("Tyrannosaurus", "Homo")taxo_path("Tyrannosaurus", "Velociraptor") taxo_path("Tyrannosaurus", "Homo")
Resolves taxon names or numeric Taxonomicon IDs while preserving the candidates considered, the selected identifier, the retrieved lineage, and the resolution status for every input. This is intended for reproducible workflows in which warnings emitted during a large matrix calculation would otherwise be difficult to audit afterwards.
taxo_resolve( taxa, ambiguity = c("warn", "first", "error"), verbose = FALSE, progress = TRUE, within = NULL )taxo_resolve( taxa, ambiguity = c("warn", "first", "error"), verbose = FALSE, progress = TRUE, within = NULL )
taxa |
A character vector of taxon names or numeric Taxonomicon IDs. |
ambiguity |
How to handle names with more than one valid biological
candidate. |
verbose |
Logical. If |
progress |
Logical. If |
within |
Optional character string naming a clade (for example
|
Ambiguous inputs retain status = "ambiguous" even when the first candidate
is selected. Consequently, downstream code can distinguish an unambiguous
match from a match chosen according to the requested policy. Numeric IDs
bypass name search and are treated as single candidates when their lineage
can be retrieved.
Names are matched after trimming and collapsing whitespace, and the stored
lineage is truncated at the queried taxon exactly as in get_lineage(), so
distance_matrix(taxo_resolve(x)) and distance_matrix(x) agree.
Objects returned by taxo_resolve() can be passed directly to
distance_matrix(), which reuses the stored lineages and preserves the
original input names as matrix labels.
A data frame of class taxodist_resolution with one row per input
and columns input, resolved_name, id, status, n_candidates,
lineage_depth, lineage, and candidates. The last two are list
columns. Status is one of "resolved", "ambiguous", "unresolved",
or "retrieval_error".
taxo_search(), distance_matrix(), check_coverage()
resolved <- taxo_resolve(c("Tyrannosaurus", "Nereis", "50841")) distance_matrix(resolved) # Restrict homonyms to animals taxo_resolve("Nereis", within = "Animalia")resolved <- taxo_resolve(c("Tyrannosaurus", "Nereis", "50841")) distance_matrix(resolved) # Restrict homonyms to animals taxo_resolve("Nereis", within = "Animalia")
Queries The Taxonomicon database and returns a data frame of all available biological entries matching the search string. This is particularly useful for exploring homonyms, historical ranks, or taxonomic synonyms before computing distances.
taxo_search(taxon, verbose = FALSE)taxo_search(taxon, verbose = FALSE)
taxon |
A character string giving the taxon name to search for. |
verbose |
Logical. If |
A data frame of class "data.frame" with columns:
idCharacter. The numeric Taxonomicon ID.
nameCharacter. The full taxon description, including rank and author.
Returns NULL if no matches are found.
taxo_search("Bacteria") taxo_search("Nereis") taxo_search("Tyrannosaurus")taxo_search("Bacteria") taxo_search("Nereis") taxo_search("Tyrannosaurus")
Builds a tree whose internal nodes are the named clades of the source
classification, directly from the lineages of the supplied taxa. Unlike
taxo_cluster(), which reconstructs a dendrogram from distances, the
topology here is the classification itself and internal nodes carry clade
names.
taxo_tree(taxa, verbose = FALSE, progress = TRUE)taxo_tree(taxa, verbose = FALSE, progress = TRUE)
taxa |
A character vector of taxon names, a |
verbose, progress
|
Passed to |
Clades with a single descendant among the supplied taxa are collapsed, so every internal node is a point where the taxa diverge and is labeled with the deepest clade shared by its descendants (their MRCA).
Branch lengths are chosen so that the tree is ultrametric and its
cophenetic (patristic) distances reproduce distance_matrix() exactly:
an internal node for a clade at depth h is placed at height 1 / (2h)
and tips at height zero. A taxon that is an ancestor of another supplied
taxon is attached to its clade node by a branch of length 1 / (2h), and
taxa with identical lineages are joined by zero-length branches.
All lineages must share the same root node; otherwise distances between some taxa are infinite and no single tree exists.
An object of class "phylo", compatible with the ape
package (for example ape::plot.phylo(), ape::write.tree(), and
ape::cophenetic.phylo()). Tip labels are the input labels and node
labels are clade names. Taxa without a lineage are dropped with a
warning.
taxo_cluster(), distance_matrix()
resolved <- taxo_from_lineages(list( Tyrannosaurus = c("Biota", "Dinosauria", "Theropoda", "Tyrannosaurus"), Velociraptor = c("Biota", "Dinosauria", "Theropoda", "Velociraptor"), Triceratops = c("Biota", "Dinosauria", "Ornithischia", "Triceratops"), Homo = c("Biota", "Mammalia", "Homo") )) tree <- taxo_tree(resolved) tree$node.label if (requireNamespace("ape", quietly = TRUE)) { plot(tree, show.node.label = TRUE) ape::write.tree(tree) }resolved <- taxo_from_lineages(list( Tyrannosaurus = c("Biota", "Dinosauria", "Theropoda", "Tyrannosaurus"), Velociraptor = c("Biota", "Dinosauria", "Theropoda", "Velociraptor"), Triceratops = c("Biota", "Dinosauria", "Ornithischia", "Triceratops"), Homo = c("Biota", "Mammalia", "Homo") )) tree <- taxo_tree(resolved) tree$node.label if (requireNamespace("ape", quietly = TRUE)) { plot(tree, show.node.label = TRUE) ape::write.tree(tree) }
A pre-computed reference dataset for 52 candidate genera distributed across major taxonomic groups. The data were retrieved from The Taxonomicon using the package functions and are intended for examples, documentation, and offline inspection of package output structures.
data(taxobase)data(taxobase)
A structured list containing the following pre-computed components:
Character vector of 52 candidate genera.
Character vector of candidates found in The Taxonomicon.
Named logical vector indicating database presence of each taxon.
A pre-computed taxonomic dist object for found_taxa.
A pre-computed distance result for Tyrannosaurus and Homo.
Taxonomic lineage returned for Homo.
Taxonomic lineage returned for Tyrannosaurus.
Result of a closest-relative query for Tyrannosaurus.
Taxa in found_taxa assigned to Theropoda.
Reference search result for the term "Bacteria".
Character vector used by the statistical applications vignette.
Pre-computed dist object for statistical_taxa.
Data source, generation date, package version, and distance definition.
http://taxonomicon.taxonomy.nl
data(taxobase) length(taxobase$found_taxa) head(labels(taxobase$matrix))data(taxobase) length(taxobase$found_taxa) head(labels(taxobase$matrix))
Write a taxodist bundle as portable JSON
write_taxodist_bundle(bundle, file, pretty = TRUE)write_taxodist_bundle(bundle, file, pretty = TRUE)
bundle |
A |
file |
Path to the JSON file to create. |
pretty |
Logical. If |
Invisibly returns the normalized output path.