### Prepare the WORMS taxonomy
library(dplyr)
library(ggplot2)
library(finch)
mutate_cond <- function(.data, condition, ..., envir = parent.frame()) {
  condition <- eval(substitute(condition), .data, envir)
  .data[condition, ] <- .data[condition, ] %>% mutate(...)
  .data
}
#
setwd("E://Erlangen_2021/OBIS/data_and_code/data")
## read WORMS with finch
worm <- dwca_read("WoRMS_download_2021-01-01.zip", read = TRUE)
#
biv <- tbl_df(worm$data$taxon.txt %>% filter(class == "Bivalvia"))
biv <- biv %>% rename(parent = parentNameUsage, acname = acceptedNameUsage)
biv_all  <- biv
biv <- biv %>% filter(taxonomicStatus == "accepted")
#table(biv$scientificName == biv$acname)
#length(unique(biv$family))

# corrections to the data base: 
# 1 parent name of 

minfields <- c("outer0","outer1","middle","inner","aragonite","calcite","both","source")
# rank table
#table(biv$taxonRank)

subclasses <- (biv %>% filter(taxonRank == "subclass"))$acname 
orders <- tibble(order = (biv %>% filter(taxonRank == "order"))$order, parent = (biv %>% filter(taxonRank == "order"))$parent)
orders <- orders %>% mutate(subclass = parent)
orders <- orders %>% mutate_at(vars(subclass), funs(ifelse(. %in% subclasses, subclass, NA))) 

superorders <- (biv %>% filter(taxonRank == "superorder"))$acname 
orders <- orders %>% mutate(superorder = parent)
orders <- orders %>% mutate_at(vars(superorder), funs(ifelse(. %in% superorders, superorder, NA))) 

infraclasses <- (biv %>% filter(taxonRank == "infraclass"))$acname

orders <- orders %>% mutate(infraclass = parent)
orders <- orders %>% mutate_at(vars(infraclass), funs(ifelse(. %in% infraclasses, infraclass, NA))) 

orders <- orders %>% select(order,superorder,infraclass,subclass, parent)

# get parent of parent...
Imparidentia_parent <-  (biv %>% filter(acname %in% orders$superorder))$parent
(biv %>% filter(acname == Imparidentia_parent))$taxonRank
orders$infraclass[which(orders$superorder == "Imparidentia")] <- Imparidentia_parent 

infraclass_levels <- levels(as.factor(orders$infraclass))
infraclass_parent <-  (biv %>% filter(acname %in% infraclass_levels))$parent

(biv %>% filter(acname %in% infraclass_parent))$taxonRank

for (i in 1:length(infraclass_levels)) orders$subclass[which(orders$infraclass == infraclass_levels[i])] <- infraclass_parent[i]

# check manually of all used orders are also listed as taxon in the file

all_orders <- unique(biv$order)
all_orders <- all_orders[which(all_orders != "")]
length(all_orders)
length(orders$order)
all_orders %in% orders$order
# seems to have worked

# Superfamilies

all_superfamilies <- sort(unique(biv$acname[which(biv$taxonRank == "superfamily")]))



families <- tibble(family = (biv %>% filter(taxonRank == "family"))$family, superfamily = NA, 
                   order = (biv %>% filter(taxonRank == "family"))$order,
                   parent = (biv %>% filter(taxonRank == "family"))$parent,  parent_of_parent = NA, 
                   parent_of_parent_rank = NA, superorder = NA)

# View(biv[which(biv$acname=="Entolioidea"),])

families <- families[which(!(families$family=="Entoliidae")),]

for(i in 1:nrow(families)) {
  if (biv$taxonRank[which(biv$acname == families$parent[i])] == "superfamily") families$superfamily[i] <- families$parent[i]
}


for(i in 1:nrow(families)) {

  families$parent_of_parent[i] <- biv$parent[which(biv$acname == families$parent[i])] 
  families$parent_of_parent_rank[i] <- biv$taxonRank[which(biv$acname == families$parent_of_parent[i])] 
  
}

length(table((families %>% filter(parent_of_parent_rank == "Order"))$parent_of_parent))

table(families$parent_of_parent_rank)



# check manually of all used orders are also listed as taxon in the file

all_families <- unique(biv$family)
all_families <- all_families[which(all_families != "")]
length(all_families)

length(families$family)
mis_fam <- tibble(family = all_families[which(!(all_families %in% families$family))])

families <- bind_rows(families,mis_fam)

# 5 families were missing
# the family Bathyspinulidae is an invalid synonym for the subfamily Ledellinae, contained in Nuculanidae, so remove

families <- families[-which(families$family == "Bathyspinulidae"),]

#View(families %>% filter(family == "Hyriidae"))
families <- families %>%  mutate_cond(family == "Hyriidae", superfamily = "Hyrioidea", order = "Unionida")
families <- families %>%  mutate_cond(family == "Unionidae", superfamily = "Unionoidea", order = "Unionida")
families <- families %>%  mutate_cond(family == "Groeberellidae", superfamily = "Trigonioidea", order = "Trigoniida")
families <- families %>%  mutate_cond(family == "Etheriidae", superfamily = "Etherioidea", order = "Unionida")

# assign superorder
families <- families %>%  mutate_cond(parent_of_parent_rank == "Superorder" & !(is.na(parent_of_parent_rank)), 
                                           superorder = parent_of_parent)
families <- families %>% select(-parent_of_parent,-parent_of_parent_rank,-parent)


#families <- families %>% mutate_cond(order == "", order = NA)
families <- families %>% mutate(infraclass = NA, subclass = NA)

# assign superorders and up to the families based on orders
for(i in 1:nrow(families)) {
  if(!(is.na(families$order[i])) & families$order[i] != "") {
    families$superorder[i] <- orders$superorder[which(orders$order == families$order[i])]
    families$infraclass[i] <- orders$infraclass[which(orders$order == families$order[i])]
    families$subclass[i] <- orders$subclass[which(orders$order == families$order[i])]
  }
}

# manually assign infra- and subclass to the families of the two superorders which sometimes have no order
families <- families %>% mutate_cond(superorder == "Imparidentia" & !is.na(superorder), infraclass = "Euheterodonta", subclass = "Heterodonta")
families <- families %>% mutate_cond(superorder == "Anomalodesmata" & !is.na(superorder), infraclass = "Euheterodonta", subclass = "Heterodonta")

# sort by groups
families <- families %>% arrange(subclass,infraclass,superorder,order,superfamily,family)