joshwlambert / DAISIEprep

Extracts phylogenetic island community data from phylogenetic trees
https://joshwlambert.github.io/DAISIEprep
GNU General Public License v3.0
6 stars 3 forks source link

Duplicated extracted species from `extract_island_species()` #34

Closed joshwlambert closed 4 months ago

joshwlambert commented 5 months ago

When a species, for this example let's call this species "bird_a", is extracted using the asr algorithm and if first stored as a colonist, it can then be duplicated in the island_tbl if another clade has an ancestral node on the island that also includes "bird_a" as one of it's descendants. It is not excluded in the same way a descendant that is not present on the island would be.

Here is a reprex to show the bug:

library(DAISIEprep)
set.seed(
  1,
  kind = "Mersenne-Twister",
  normal.kind = "Inversion",
  sample.kind = "Rejection"
)
phylo <- ape::rcoal(6)
phylo$tip.label <- c("bird_a", "bird_b", "bird_c", "bird_d", "bird_e",
                     "bird_f")
phylo <- add_outgroup(phylo = phylo)
phylo <- phylobase::phylo4(phylo)

endemicity_status <- c(
  "not_present",
  "endemic",
  "not_present",
  "nonendemic",
  rep("endemic", 2),
  "not_present"
)
phylod <- phylobase::phylo4d(phylo, as.data.frame(endemicity_status))

plot_phylod(phylod)


phylod <- add_asr_node_states(
  phylod = phylod, 
  asr_method = "mk",
  tie_preference = "mainland",
  rate_model = matrix(
    data = c(
      0, 1, 0,
      2, 0, 3,
      0, 0, 0
    ),
    nrow = 3,
    byrow = TRUE
  )
)

plot_phylod(phylod)


# augment node state of bird a ancestor to not present to reproduce bug
phylod@data$island_status[11] <- "not_present"

plot_phylod(phylod)


it <- extract_island_species(phylod, extraction_method = "asr")
it
#> Class:  Island_tbl 
#>   clade_name  status missing_species   col_time col_max_age branching_times
#> 1     bird_a endemic               0 0.05034546       FALSE              NA
#> 2     bird_c endemic               0 0.67546123       FALSE    0.239392....
#>   min_age      species clade_type
#> 1      NA       bird_a          1
#> 2      NA bird_a, ....          1
it@island_tbl$species
#> [[1]]
#> [1] "bird_a"
#> 
#> [[2]]
#> [1] "bird_a" "bird_c" "bird_d" "bird_e"

Created on 2024-05-06 with reprex v2.1.0