Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

V.PhyloMaker2 phylo tree error #3

Closed
fab4ap opened this issue Sep 26, 2023 · 6 comments
Closed

V.PhyloMaker2 phylo tree error #3

fab4ap opened this issue Sep 26, 2023 · 6 comments

Comments

@fab4ap
Copy link

fab4ap commented Sep 26, 2023

I'm trying to use spat.alpha/spat.beta on phylo tree generated by V.PhyloMaker2 and getting errors. I don't see any guidance in the divraster documentation on what type of tree to use for tree/plant species. Here is the code

library(V.PhyloMaker2) ; library(dplyr); library(ape); library(divraster); library(terra)
natree = phylo.maker(sp.list = naspp, tree = GBOTB.extended.TPL, output.tree=T, nodes = nodes.info.1.TPL)
write.tree(natree$scenario.3, "natree.tre")

tf = read.tree("natree.tre")
tf = as.phylo(tf)

 p = rast("na_sumPres_actual_stack.tif")
pg = project(p, "+proj=longlat +datum=WGS84")
z = vect("canusmex_geog.gpkg")
ce = ext(z)
pge = crop(pg, ce)
g = spat.alpha(pge) # Works

 g = spat.alpha(pge, tf)

```**Error: [app] cannot use this function
Error in comm %*% A: non-conformable arguments**

gb = spat.beta(pge, tf)
Error: [focal] test failed

@flaviomoc
Copy link
Owner

Thanks for your feedback! It seems there might be a problem because the 'rast' and 'phylo' objects have different numbers of species. I will try to make them equal. If that does not work, could you please provide a reproducible example? This way, I can better understand and fix the issue.

@fab4ap
Copy link
Author

fab4ap commented Sep 26, 2023

Appreciate the quick response! If your fix does not work, I'll try to give you a reproducible example (325 tree species of North America!).

@fab4ap
Copy link
Author

fab4ap commented Oct 2, 2023

Hope you have been able to make progress. Attached are the files that can reproduce the error.
Thanks!!

library(terra); library(divraster); library(ape)
pge = rast("na_sumPres_ActualStack.tif")
tf = read.tree("natree.tre") # Or "natree2c.tre"
tf = as.phylo(tf)

g = spat.alpha(pge, tf)
gb = spat.beta(pge, tf)
divraster_phylo_Error.zip

@flaviomoc
Copy link
Owner

flaviomoc commented Oct 2, 2023

After checking your data, it appears that the issue is indeed the different species names between the spatraster and phylo objects. The phylo object contains the actual species names, while the spatraster object labels the species as "rivmf_nfif3_20k_sXXX". The updated version of the package now specifies this error, and I am including this information in the functions' documentation. You might want to consider running the code below for further details. Thank you!

install updated version from github

devtools::install_github("flaviomoc/divraster", force = TRUE)

load packages

library(terra); library(divraster); library(ape)

load data

pge = rast("na_sumPres_ActualStack.tif")
tf = ape::read.tree("natree2c.tre") # Or "natree2c.tre"
tf = as.phylo(tf)

apply functions

g = spat.alpha(pge, tf)
gb = spat.beta(pge, tf)

Error in inputs_chk(bin1 = bin, tree = tree) :

Species names in 'bin1' and 'tree' must match.

check objects

sort(names(pge))
sort(tf$tip.label)

Number of species and their names do not match!

check divraster examples

bin1 <- terra::rast(system.file("extdata", "ref.tif",
package = "divraster"))
tree <- ape::read.tree(system.file("extdata", "tree.tre",
package = "divraster"))
sort(names(bin1))
sort(tree$tip.label)
spat.alpha(bin1, tree)
spat.beta(bin1, tree)

@fab4ap
Copy link
Author

fab4ap commented Oct 2, 2023

Thanks! Must have checked that myself - was under the impression that the numbers was also recorded in the tre file. Appreciate your help and the improved error message!

@flaviomoc
Copy link
Owner

Great!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants