Skip to content

Instantly share code, notes, and snippets.

@valentinitnelav
Last active January 3, 2018 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valentinitnelav/4b4b039d36eff385fa625f60995dab12 to your computer and use it in GitHub Desktop.
Save valentinitnelav/4b4b039d36eff385fa625f60995dab12 to your computer and use it in GitHub Desktop.
Check some taxa names with Taxonstand & wikitaxa
# -----------------------------------------------------------------------------
# Example to check some taxa names with Taxonstand & wikitaxa
# -----------------------------------------------------------------------------
my_sp <- fread("Output/taxa_to_check_VS.csv")
my_sp
# taxa
# Thereianthus_spicatus
# Eriocnema_fulva
# Hebe_macrocarpa
# Aspalathus_cymbriformis
# Aspalathus_uniflora
# Silene_stockenii
# Cornus_kousa_subsp._kousa
# Davidia_involucrata
# replace _ with white space
my_sp[, taxa := gsub(pattern = "_", replacement = " ", x = taxa)]
# gives errors
# taxize::classification(x = my_sp$taxa, db = "tol")
# Connects to The Plant List (TPL) website and validates the names
my_tpl <- data.table(Taxonstand::TPL(splist = my_sp$taxa))
setnames(my_tpl, paste0(names(my_tpl), "_TPL"))
my_tpl
my_sp <- merge(x = my_sp,
y = my_tpl[, c("Taxon_TPL", "Taxonomic.status_TPL", "Family_TPL", "New.Genus_TPL",
"New.Species_TPL", "New.Taxonomic.status_TPL", "Typo_TPL")],
by.x = "taxa",
by.y = "Taxon_TPL",
all.x = TRUE, sort = FALSE)
my_families <- unique(my_sp$Family_TPL)
my_orders <- vector(mode = "character", length = length(my_families))
# Retrieve info from WikiSpecies about orders based on given families.
# Had to use WikiSpeceis because TPL does not return order.
# Try running several times if NA-s are returned - I guess it depends on the API connection somehow.
for (i in 1:length(my_families)){
my_dt <- data.table(wikitaxa::wt_wikispecies(name = my_families[i])$classification)
# some cases might not match properly and does not retrieve order information,
# so, assign NA for such cases
my_ord <- my_dt[rank == "Ordo", name]
my_orders[i] <- ifelse(length(my_ord) != 0, my_ord, NA)
}
my_orders_df <- data.frame(my_families, order_wikitaxa = my_orders)
my_sp <- merge(x = my_sp,
y = my_orders_df,
by.x = "Family_TPL",
by.y = "my_families",
all.x = TRUE, sort = FALSE)
# move "Family_TPL" column from first to last position
setcolorder(my_sp, c(setdiff(names(my_sp), "Family_TPL"), "Family_TPL"))
writexl::write_xlsx(my_sp, path = "Output/taxa_to_check_TPL_suggest_VS.xlsx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment