ropensci / taxize

A taxonomic toolbelt for R
https://docs.ropensci.org/taxize
Other
269 stars 61 forks source link

gbif_downstream return extant species #837

Closed ocstringham closed 4 years ago

ocstringham commented 4 years ago

I was wondering if there is a way to remove extinct species and only keep extant species when using gbif_downstream?

For example, Camelidae (gbif ID 5716) has only seven extant species but gbif_downstream returns 165 species.

Example code: camels = gbif_downstream(5716, downto = 'species')

sckott commented 4 years ago

thanks for your question @ocstringham - having a look

sckott commented 4 years ago

We use the /species/5716/children/ API route, e.g, https://api.gbif.org/v1/species/5716/children to get data for this function. It does not allow query parameters for extinction status, AND does not return data for that status.

Another route /species/search/, e.g,. https://api.gbif.org/v1/species/search?q=Camelidae&limit=20&isExtinct=false does allow filtering by extinct status and returns the data, but only allows querying by a name string rather than a taxonomic ID, so you can't do a precise query with a known taxonomic ID

opened an issue in their forum https://discourse.gbif.org/t/extinction-status-and-the-species-api-routes/2312

ocstringham commented 4 years ago

OK, thank you for the update! Looking forward to hearing back.

sckott commented 4 years ago

https://discourse.gbif.org/t/extinction-status-and-the-species-api-routes/2312/2 reply, may be at a later point though - see also https://github.com/CatalogueOfLife/backend/issues/837

sckott commented 4 years ago

I found out that No, there' no way to filter extinct/extant status with the routes we're using for gbif_downstream. An alternative could be to add that data after, something like:

library(taxize)
library(rgbif)
x <- gbif_downstream(5716, downto = 'species')
dskey <- "d7dddbf4-2cf0-4f39-9b2a-bb099caae36c"
is_extinct <- function(name, key) {
  res <- data.frame(name_lookup(name, datasetKey=dskey)$data)
  if (NROW(res) == 0) return(NA)
  if (NROW(res) > 1) {
    res <- res[res$key == key, ]
  }
  res$extinct %||% NA
}
ext_vec <- Map(is_extinct, x$name, x$key)
x$extinct <- unlist(unname(ext_vec))
na.omit(x)[!na.omit(x)$extinct,]
ocstringham commented 4 years ago

Thanks for the update @sckott. I appreciate the alternative way of adding in the data after!

sckott commented 4 years ago

hopefully that works for you

sckott commented 4 years ago

@ocstringham does that solution work for you, adding in the extinct status after?

ocstringham commented 4 years ago

@sckott No, not yet. I'm running into issues where taxize::gbif_downstream is returning synonyms so then rgbif::name_lookup is not returning extinction status. I'll keep trying and get back to you. Thanks.

ocstringham commented 4 years ago

@sckott It's actually that rgbif::name_lookup sometimes doesn't return an extinction status. For example: test = name_lookup("Camelops navadanus")$data doesn't have an extinction column. Any idea why this may be?

sckott commented 4 years ago

No i don't know why exinct would be missing.

Asked a question in their forum https://discourse.gbif.org/t/why-would-extinct-be-missing-from-species-search/2366

sckott commented 4 years ago

@ocstringham see the answer in the forum link above. When there is no extinct field they just don't know if it's extinct or not. I guess that sounds a bit silly but there it is. If you want to ask in that forum follow up questions go for it

ocstringham commented 4 years ago

Got it, makes sense. In that case, yes I can now code to get extinction status using rgbif::name_lookup, given if no extinction is returned its status is unknown. Thanks!

sckott commented 4 years ago

great, sounds good

Andreas-Bio commented 2 years ago

possibly related to #https://github.com/gbif/portal-feedback/issues/3432