knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(taxadb)

source(here::here("data-raw", "helper_functions.R"))

Get id's for amphibio database from itis

#original data from https://figshare.com/articles/Oliveira_et_al_AmphiBIO_v1/4644424

amph <- read_csv(system.file("extdata", "amphibio/AmphiBIO_v1.csv", package = "biodivTS")) %>%
  select(-id) %>%
  rename_all(tolower) %>%
  rename(diet_leaves = leaves, diet_flowers = flowers, diet_seed = seeds, diet_fruit = fruits, diet_inv = arthro, diet_vert = vert,
         diurnal = diu, nocturnal = noc, crepuscular = crepu, bodymass_value = body_mass_g) %>%
  mutate(diet_seed = as.numeric(diet_seed))

amphibio <- amph %>%
  mutate(id = get_ids(species, "itis")) %>%
  filter(!is.na(id)) %>%
  mutate(scientificName = get_names(id, "itis")) %>%
  select(id, scientificName, species) %>%
  right_join(amph, by = "species") %>%
  rename(sourceName = species) %>%
  select(-c("order", "family", "genus")) 

#two entries need to be resolved by hand
add_ids <- amphibio %>%
  filter(is.na(id)) %>%
  pull(sourceName) %>%
  filter_name(., "itis") %>%
  filter(!is.na(acceptedNameUsageID)) %>%
  select(scientificName, id = acceptedNameUsageID) %>%
  distinct() %>%
  left_join(amph, by = c("scientificName" = "species")) %>%
  select(-c("order", "family", "genus")) %>%
  mutate(sourceName = scientificName)

amphibio <- amphibio %>% 
  filter(!sourceName %in% add_ids$sourceName) %>%
  bind_rows(add_ids)

Check for matches from other providers

syn_res <- match_providers(amphibio, "itis")

#names that resolve to more than one id
alt_dupes <- syn_res %>%
  select(acceptedNameUsageID, input) %>%
  distinct() %>%
  group_by(input) %>%
  filter(n() > 1)

add_syn_res <- syn_res %>% 
  filter(!input %in% alt_dupes$input) %>% #excluding duplicate matched species
  select(id = acceptedNameUsageID, sourceName = input) %>%
  distinct() %>%
  mutate(scientificName = get_names(id, "itis")) %>%
  left_join(amphibio %>% select(-c(id, scientificName)), by = "sourceName", na_matches =  "never") %>%
  distinct()

amphibio <- amphibio %>% 
  filter(!sourceName %in% add_syn_res$sourceName) %>%
  bind_rows(add_syn_res)

Select relevant trait columns

amphibio <- amphibio %>%
  select(-obs)

Check for multiple matches to the same ID

amphibio <- undupe_ids(amphibio)
#save locally
usethis::use_data(amphibio)


karinorman/biodivTS documentation built on Dec. 23, 2024, 10 p.m.