
Extract biogeographic range data mapped on a phylogeny at a given time in the past
Source:R/extract_most_likely_trait_values_for_focal_time.R
extract_most_likely_ranges_from_densityMaps_for_focal_time.RdExtracts the most likely ranges found along branches
at a specific time in the past (i.e. the focal_time).
Optionally, the function can update the mapped phylogeny (densityMaps)
such as branches overlapping the focal_time are shorten to the focal_time,
and the range mapping for the cut off branches are removed
by updating the $tree$maps and $tree$mapped.edge elements.
Usage
extract_most_likely_ranges_from_densityMaps_for_focal_time(
densityMaps,
ace = NULL,
tip_data = NULL,
focal_time,
update_densityMaps = FALSE,
keep_tip_labels = TRUE
)Arguments
- densityMaps
List of objects of class
"densityMap", typically generated withprepare_trait_data(), that contains a phylogenetic tree and associated posterior probability of being in a given range along branches. Each object (i.e.,densityMap) corresponds to a range. The phylogenetic tree must be rooted and fully resolved/dichotomous, but it does not need to be ultrametric (it can includes fossils).- ace
(Optional) Numerical matrix that record the posterior probabilities of ancestral ranges at internal nodes, obtained with
prepare_trait_data()as output in the$aceslot. Rows are internal nodes_ID. Columns are ranges. Values are posterior probabilities of each range per node. Needed to provide accurate estimates of ancestral ranges.- tip_data
(Optional) Named character string vector of tip ranges. Names are nodes_ID of the internal nodes. Needed to provide accurate tip values.
- focal_time
Integer. The time, in terms of time distance from the present, at which the tree and mapping must be cut. It must be smaller than the root age of the phylogeny.
- update_densityMaps
Logical. Specify whether the mapped phylogeny (
densityMaps) provided as input should be updated for visualization and returned among the outputs. Default isFALSE. The update consists in cutting off branches and mapping that are younger than thefocal_time.- keep_tip_labels
Logical. Specify whether terminal branches with a single descendant tip must retained their initial
tip.labelon the updated densityMaps. Default isTRUE. Used only ifupdate_map = TRUE.
Value
By default, the function returns a list with three elements.
$trait_dataA named character string vector with ML ranges found along branches overlapping thefocal_time. Names are the tip.label/tipward node ID.$focal_timeInteger. The time, in terms of time distance from the present, at which the trait data were extracted.$trait_data_typeCharacter string. Define the type of trait data as "biogeographic". Used in downstream analyses to select appropriate statistical processing.
If update_densityMaps = TRUE, the output is a list with four elements: $trait_data, $focal_time, $trait_data_type, and $densityMaps.
$densityMapsA list of objects of class"densityMap"that contains the updateddensityMapof each range/range, with branches and mapping that are younger than thefocal_timecut off. The function also adds multiple useful sub-elements to the$densityMaps$treeelements.$root_ageInteger. Stores the age of the root of the tree.$nodes_ID_dfData.frame with two columns. Provides the conversion from thenew_node_IDto theinitial_node_ID. Each row is a node.$initial_nodes_IDVector of character strings. Provides the initial ID of internal nodes. Used to plot internal node IDs as labels withape::nodelabels().$edges_ID_dfData.frame with two columns. Provides the conversion from thenew_edge_IDto theinitial_edge_ID. Each row is an edge/branch.$initial_edges_IDVector of character strings. Provides the initial ID of edges/branches. Used to plot edge/branch IDs as labels withape::edgelabels().
Details
The mapped phylogeny (densityMaps) is cut at a specific time in the past
(i.e. the focal_time) and the current trait values of the overlapping edges/branches are extracted.
—– Extract trait_data —–
Most likely ranges are extracted from the posterior probabilities displayed in the densityMaps.
The range with the highest probability is assigned to each tip and cut branches at focal_time.
True ML estimates will be used if tip_data and/or ace are provided as optional inputs.
In practice the discrepancy is negligible.
—– Update the densityMaps —–
To obtain updated densityMaps alongside the trait data, set update_densityMaps = TRUE.
The update consists in cutting off branches and mapping that are younger than the focal_time.
When a branch with a single descendant tip is cut and
keep_tip_labels = TRUE, the leaf left is labeled with the tip.label of the unique descendant tip.When a branch with a single descendant tip is cut and
keep_tip_labels = FALSE, the leaf left is labeled with the node ID of the unique descendant tip.In all cases, when a branch with multiple descendant tips (i.e., a clade) is cut, the leaf left is labeled with the node ID of the MRCA of the cut-off clade.
The ancestral range mapping in densityMap ($tree$maps and $tree$mapped.edge) is updated accordingly by removing mapping associated with the cut off branches.
See also
cut_phylo_for_focal_time() cut_densityMaps_for_focal_time()
Associated main function: extract_most_likely_trait_values_for_focal_time()
Sub-functions for other types of trait data:
extract_most_likely_trait_values_from_contMap_for_focal_time()
extract_most_likely_states_from_densityMaps_for_focal_time()
Examples
## Load biogeographic range data mapped on a phylogeny
data(eel_biogeo_data, package = "deepSTRAPP")
# Explore data
str(eel_biogeo_data, 1)
#> List of 9
#> $ densityMaps :List of 2
#> $ densityMaps_all_ranges:List of 3
#> $ trait_data_type : chr "biogeographic"
#> $ ace : num [1:60, 1:2] 0.361 0.524 0.623 0.334 0.47 ...
#> ..- attr(*, "dimnames")=List of 2
#> $ ace_all_ranges : num [1:60, 1:3] 0.174 0.373 0.53 0.244 0.411 ...
#> ..- attr(*, "dimnames")=List of 2
#> $ BSM_output :List of 2
#> $ simmaps :Class "multiPhylo"
#> List of 100
#> $ best_model_fit :List of 13
#> ..- attr(*, "class")= chr "calc_loglike_sp_results"
#> $ model_selection_df :'data.frame': 6 obs. of 8 variables:
eel_biogeo_data$densityMaps # Two density maps: one per unique area: A, B.
#> $Density_map_A
#> Object of class "densityMap" containing:
#>
#> (1) A phylogenetic tree with 61 tips and 60 internal nodes.
#>
#> (2) The mapped posterior density of a discrete binary character
#> with states (Not A, A).
#>
#>
#> $Density_map_B
#> Object of class "densityMap" containing:
#>
#> (1) A phylogenetic tree with 61 tips and 60 internal nodes.
#>
#> (2) The mapped posterior density of a discrete binary character
#> with states (Not B, B).
#>
#>
eel_biogeo_data$densityMaps_all_ranges # Three density maps: one per range: A, B, and AB.
#> $Density_map_A
#> Object of class "densityMap" containing:
#>
#> (1) A phylogenetic tree with 61 tips and 60 internal nodes.
#>
#> (2) The mapped posterior density of a discrete binary character
#> with states (Not A, A).
#>
#>
#> $Density_map_B
#> Object of class "densityMap" containing:
#>
#> (1) A phylogenetic tree with 61 tips and 60 internal nodes.
#>
#> (2) The mapped posterior density of a discrete binary character
#> with states (Not B, B).
#>
#>
#> $Density_map_AB
#> Object of class "densityMap" containing:
#>
#> (1) A phylogenetic tree with 61 tips and 60 internal nodes.
#>
#> (2) The mapped posterior density of a discrete binary character
#> with states (Not AB, AB).
#>
#>
# Set focal time to 10 Mya
focal_time <- 10
# ----- Example 1: Using only unique areas ----- #
## Extract trait data and update densityMaps for the given focal_time
# Extract from the densityMaps
eel_biogeo_data_10My <- extract_most_likely_ranges_from_densityMaps_for_focal_time(
densityMaps = eel_biogeo_data$densityMaps,
# ace = eel_biogeo_data$ace,
focal_time = focal_time,
update_densityMaps = TRUE)
#> WARNING: No ancestral character estimates (ace) for internal nodes have been provided. Using most likely ranges extracted from the densityMaps instead.
#> WARNING: No tip data have been provided. Using ranges extracted from the densityMaps instead.
## Print trait data
str(eel_biogeo_data_10My, 1)
#> List of 4
#> $ trait_data : Named chr [1:52] "B" "B" "B" "A" ...
#> ..- attr(*, "names")= chr [1:52] "Moringua_edwardsi" "Kaupichthys_nuchalis" "69" "Venefica_proboscidea" ...
#> $ focal_time : num 10
#> $ trait_data_type: chr "biogeographic"
#> $ densityMaps :List of 2
eel_biogeo_data_10My$trait_data
#> Moringua_edwardsi Kaupichthys_nuchalis
#> "B" "B"
#> 69 Venefica_proboscidea
#> "B" "A"
#> 74 Anguilla_bicolor
#> "B" "B"
#> Anguilla_japonica Serrivomer_beanii
#> "B" "A"
#> Nemichthys_scolopaceus Kaupichthys_hyoproroides
#> "A" "A"
#> Dysomma_anguillare Simenchelys_parasitica
#> "A" "A"
#> Gnathophis_longicauda Facciolella_gilbertii
#> "B" "A"
#> Nettastoma_melanurum Gavialiceps_taeniola
#> "A" "A"
#> Uroconger_lepturus Bathyuroconger_vicinus
#> "A" "A"
#> Rhynchoconger_flavus Saurenchelys_fierasfer
#> "A" "A"
#> 91 94
#> "B" "B"
#> Scolecenchelys_breviceps Myrichthys_maculosus
#> "B" "B"
#> 99 Myrichthys_breviceps
#> "B" "B"
#> Brachysomophis_crocodilinus Pisodonophis_cancrivorus
#> "A" "A"
#> Ichthyapus_ophioneus Myrichthys_magnificus
#> "A" "B"
#> Oxyconger_leptognathus Gymnothorax_moringa
#> "A" "A"
#> Gymnothorax_castaneus Gymnothorax_pseudothyrsoideus
#> "A" "A"
#> 111 Gymnothorax_kidako
#> "A" "A"
#> Gymnothorax_flavimarginatus Uropterygius_micropterus
#> "A" "A"
#> Scuticaria_tigrina Congresox_talabonoides
#> "A" "A"
#> 115 Cynoponticus_ferox
#> "A" "A"
#> Ariosoma_anago Parabathymyrus_macrophthalmus
#> "B" "B"
#> Ariosoma_balearicum 119
#> "B" "B"
#> Serrivomer_sector Paraconger_notialis
#> "A" "B"
#> Moringua_javanica Elops_saurus
#> "A" "B"
#> Megalops_cyprinoides Albula_vulpes
#> "B" "B"
## Plot density maps as overlay of all range posterior probabilities
# Plot initial density maps with ACE pies
plot_densityMaps_overlay(densityMaps = eel_biogeo_data$densityMaps, fsize = 0.7)
abline(v = max(phytools::nodeHeights(eel_biogeo_data$densityMaps[[1]]$tree)[,2]) - focal_time,
col = "red", lty = 2, lwd = 2)
# Plot updated densityMaps with ACE pies
plot_densityMaps_overlay(eel_biogeo_data_10My$densityMaps, fsize = 0.7)
# ----- Example 2: Using all ranges ----- #
## Extract trait data and update densityMaps_all_ranges for the given focal_time
# Extract from the densityMaps
eel_biogeo_data_10My <- extract_most_likely_ranges_from_densityMaps_for_focal_time(
densityMaps = eel_biogeo_data$densityMaps_all_ranges,
# ace = eel_biogeo_data$ace_all_ranges,
focal_time = focal_time,
update_densityMaps = TRUE)
#> WARNING: No ancestral character estimates (ace) for internal nodes have been provided. Using most likely ranges extracted from the densityMaps instead.
#> WARNING: No tip data have been provided. Using ranges extracted from the densityMaps instead.
## Print trait data
str(eel_biogeo_data_10My, 1)
#> List of 4
#> $ trait_data : Named chr [1:52] "AB" "AB" "B" "A" ...
#> ..- attr(*, "names")= chr [1:52] "Moringua_edwardsi" "Kaupichthys_nuchalis" "69" "Venefica_proboscidea" ...
#> $ focal_time : num 10
#> $ trait_data_type: chr "biogeographic"
#> $ densityMaps :List of 3
eel_biogeo_data_10My$trait_data
#> Moringua_edwardsi Kaupichthys_nuchalis
#> "AB" "AB"
#> 69 Venefica_proboscidea
#> "B" "A"
#> 74 Anguilla_bicolor
#> "AB" "B"
#> Anguilla_japonica Serrivomer_beanii
#> "B" "A"
#> Nemichthys_scolopaceus Kaupichthys_hyoproroides
#> "A" "A"
#> Dysomma_anguillare Simenchelys_parasitica
#> "A" "AB"
#> Gnathophis_longicauda Facciolella_gilbertii
#> "B" "A"
#> Nettastoma_melanurum Gavialiceps_taeniola
#> "A" "A"
#> Uroconger_lepturus Bathyuroconger_vicinus
#> "A" "A"
#> Rhynchoconger_flavus Saurenchelys_fierasfer
#> "AB" "A"
#> 91 94
#> "B" "B"
#> Scolecenchelys_breviceps Myrichthys_maculosus
#> "B" "B"
#> 99 Myrichthys_breviceps
#> "B" "B"
#> Brachysomophis_crocodilinus Pisodonophis_cancrivorus
#> "A" "A"
#> Ichthyapus_ophioneus Myrichthys_magnificus
#> "A" "B"
#> Oxyconger_leptognathus Gymnothorax_moringa
#> "A" "A"
#> Gymnothorax_castaneus Gymnothorax_pseudothyrsoideus
#> "A" "A"
#> 111 Gymnothorax_kidako
#> "A" "A"
#> Gymnothorax_flavimarginatus Uropterygius_micropterus
#> "A" "A"
#> Scuticaria_tigrina Congresox_talabonoides
#> "A" "A"
#> 115 Cynoponticus_ferox
#> "A" "A"
#> Ariosoma_anago Parabathymyrus_macrophthalmus
#> "AB" "B"
#> Ariosoma_balearicum 119
#> "AB" "B"
#> Serrivomer_sector Paraconger_notialis
#> "AB" "B"
#> Moringua_javanica Elops_saurus
#> "A" "B"
#> Megalops_cyprinoides Albula_vulpes
#> "AB" "B"
## Plot density maps as overlay of all range posterior probabilities
# Plot initial density maps with ACE pies
root_age <- max(phytools::nodeHeights(eel_biogeo_data$densityMaps_all_ranges[[1]]$tree)[,2])
plot_densityMaps_overlay(densityMaps = eel_biogeo_data$densityMaps_all_ranges, fsize = 0.7)
abline(v = root_age - focal_time,
col = "red", lty = 2, lwd = 2)
# Plot updated densityMaps with ACE pies
plot_densityMaps_overlay(eel_biogeo_data_10My$densityMaps, fsize = 0.7)