R/fishbase_ecology.R

Defines functions fishbase_ecology

## Requirement: 'rfishbase' + 'tibble'

# Info: Get from Fishbase if the species lives in marine environments, freshwater or both (brackish species taken into account)
# Info: Get from Fishbase if the species lives between 40 degree S and 40 degree N (i.e. tropical or subtropical), 
# above (i.e. tempererate, polar or subtropical) or in both types of climates.

fishbase_ecology = function(names){
  
  water = list()
  climate = list()
  
  print_time = function(start_time, end_time, message){
    
    time = round(end_time - start_time, 2)
    
    cat(paste0(message, " - Time taken : ", time, " seconds", "\n", ""))
    
    cat("\n")
    
  }
  
  zone = function(x, pattern){
    
    sapply(x, function(y){
      
      any(sapply(pattern, function(z) grepl(z, y, fixed = T)))
      
    })
    
  }
  
  salinity = function(data){
    
    if(length(data) == 0) output = NA
    
    else if(length(data) == 1 && !any(zone(data, "rackis"))) output = data
    
    else if(length(data) == 1 && any(zone(data, "rackis"))) output = "both"
    
    else{
      
      data = data[!data %in% c(" brackish", " Brackish", "brackish", "Brackish",
                               " brackish ", " Brackish ", "brackish ", "Brackish ")]
      
      if(any(zone(data, "reshwate")) && any(zone(data, "altwate"))) output = "both"
      
      else if(all(zone(data, "reshwate"))) output = "freshwater"
      
      else if(all(zone(data, "altwate"))) output = "marine"
      
      else output = NA
      
    }
    
    output
    
  }
  
  start_time_retrieving = Sys.time()
  
  data = ecosystem(names)
  
  end_time_retrieving = Sys.time()
  
  cat(paste0("Data retrieved in ", round(end_time_retrieving - start_time_retrieving, 2), " seconds\n"))
  
  start_time_processing = Sys.time()
  
  for(i in 1:length(names)){
    
    sub_df = subset(data, Species == names[i])
    
    environment = unique(sub_df$Salinity)
    
    environment = environment[!is.na(environment)]
    
    water[i] = salinity(environment)
    
    if(all(is.na(sub_df$Climate))) climate[i] = NA
    
    else if(any(zone(sub_df$Climate, "ropica")) && !any(zone(sub_df$Climate, c("emperat", "orea", "olar")))) climate[i] = "< 40"
    
    else if(!any(zone(sub_df$Climate, "ropica")) && any(zone(sub_df$Climate, c("emperat", "orea", "olar")))) climate[i] = "> 40"
    
    else climate[i] = "both"
    
  }
  
  end_time_processing = Sys.time()
  
  cat(paste0("Data processed in ", round(end_time_processing - start_time_processing, 2), " seconds\n"))
  
  tibble(data.frame(SPECIES = names, ENVIRONMENT = unlist(water), CLIMATE = unlist(climate)))
  
}
Eliot-RUIZ/eDNAevaluation documentation built on Dec. 17, 2021, 6:25 p.m.