source(here::here("R",'PR2_init.R'), echo=FALSE)
PR2 version 5.0.0
Update pr2_traits to 9 ranks
Aim
Update pr2_traits for use of 9 ranks taxonomy
Initialization
Set up files
$date = format(Sys.time(), "%Y-%m-%d")
pr2.env<- here::here("5.0", "H - pr2_traits 9 levels")
dir_pr2_update
dir.create(dir_pr2_update)
<- function(file_name){
full_path str_c(dir_pr2_update,"/", file_name)
}
<- full_path(str_c("pr2_taxonomy_", pr2.env$date, ".xlsx"))
file_pr2_taxonomy
# create the directory for taxonomy output
dir.create(full_path("taxo"), showWarnings = FALSE)
Read pr2_taxonomy 8/9 levels and pr2_traits from database
<- db_info("pr2_google")
pr2_db <- db_connect(pr2_db)
pr2_db_con
<- tbl(pr2_db_con, "pr2_taxonomy_4.14") %>%
pr2_taxo filter (is.na(taxo_removed_version)) %>%
collect()
<- tbl(pr2_db_con, "pr2_traits") %>%
pr2_traits collect()
db_disconnect(pr2_db_con)
Create taxonomy lists for pr2 8 and 9 levels and compare
- Go from wide to long fro both 8 and 9 levels
- Join the 2 lists
- Filter those that are different
= "_8"
ending
<- pr2_taxo %>%
pr2_taxo_8 select(kingdom_8:species_8) %>%
rename_with(~ str_replace(.,ending, ""), contains(ending)) %>%
pivot_longer(cols = kingdom:species,
names_to = "taxon_level_8",
values_to = "taxon_name") %>%
distinct()
= "_9"
ending
<- pr2_taxo %>%
pr2_taxo_9 select(domain_9:species_9) %>%
rename_with(~ str_replace(.,ending, ""), contains(ending)) %>%
pivot_longer(cols = domain:species,
names_to = "taxon_level_9",
values_to = "taxon_name") %>%
distinct()
<- pr2_taxo_8 %>%
pr2_taxo_8_vs_9 left_join(pr2_taxo_9)
<- pr2_taxo_8_vs_9 %>%
pr2_taxo_8_vs_9_different filter(taxon_level_8 != taxon_level_9)
Join the 8 vs 9 list with pr2_traits
- Only keep the one that are different
- Save as Excel file
- Excel file is edited manually and used to update the pr2_traits table
- taxon_name have been edited to fit with the new names
- Edited: 167
- Added: 4
= join_by(taxon_name)
by
<- pr2_traits %>%
pr2_traits_updated left_join(pr2_taxo_8_vs_9, by) %>%
filter(taxon_level != taxon_level_9 | is.na(taxon_level_9))
::export(pr2_traits_updated, full_path("pr2_traits_5.0.xlsx")) rio