From f60309759eb6e51b31ec5381aa893cf58c28ac2c Mon Sep 17 00:00:00 2001 From: Yohann Chauvier <43674773+8Ginette8@users.noreply.github.com> Date: Tue, 19 Mar 2024 10:58:29 +0100 Subject: [PATCH] update GBIF --- R/get_gbif.R | 4 +- R/get_status.R | 320 +++++++++++++++++++++++++------------------------ 2 files changed, 165 insertions(+), 159 deletions(-) diff --git a/R/get_gbif.R b/R/get_gbif.R index fa08ba1..18e3863 100644 --- a/R/get_gbif.R +++ b/R/get_gbif.R @@ -149,8 +149,8 @@ get_gbif = function(sp_name = NULL, ###################################################### - if (is.null(sp_name)) { - stop("'sp_name' must be provided to search for species records...") + if (is.null(sp_name)|is.na(sp_name)) { + stop("Given 'sp_name' is NA or NULL, a string must be provided...") } diff --git a/R/get_status.R b/R/get_status.R index 08b2c13..6474160 100644 --- a/R/get_status.R +++ b/R/get_status.R @@ -61,183 +61,189 @@ get_status=function(sp_name = NULL, conf_match = 80, all = FALSE) { - if (!search){ - # Search input name via fuzzy match and direct search - bone.search = rgbif::name_backbone(sp_name, - rank = rank, - phylum = phylum, - class = class, - order = order, - family = family, - verbose = FALSE, - strict = FALSE) - } else { - # Search input name via strict match and refined search - bone.search = rgbif::name_backbone(sp_name, - verbose = TRUE, - strict = TRUE) - - q.crit = !sapply(list(rank,phylum,class,order,family),is.null) - - # Filter by given criterias if results - if (!bone.search$matchType[1]%in%"NONE"){ - if (any(q.crit)){ - id.crit = c("rank","phylum","class","order","family")[q.crit] - p.crit = unlist(list(rank,phylum,class,order,family)[q.crit]) - n.test = id.crit%in%names(bone.search) - if (any(n.test)){ - # selecting which - id.crit2 = id.crit[n.test] - p.crit2 = p.crit[n.test] - # Apply the rigth criterias - for (i in 1:length(id.crit2)){ - bone.search = bone.search[c(bone.search[,id.crit2[i]])[[1]]%in%p.crit2[i],] - if (nrow(bone.search)==0){ - bone.search = data.frame(matchType="NONE") - } + # Error message + if (is.na(sp_name)|is.null(sp_name)){ + stop("Given 'sp_name' is NA or NULL, a string must be provided...") + } + + # Search + if (!search){ + # Search input name via fuzzy match and direct search + bone.search = rgbif::name_backbone(sp_name, + rank = rank, + phylum = phylum, + class = class, + order = order, + family = family, + verbose = FALSE, + strict = FALSE) + } else { + # Search input name via strict match and refined search + bone.search = rgbif::name_backbone(sp_name, + verbose = TRUE, + strict = TRUE) + + q.crit = !sapply(list(rank,phylum,class,order,family),is.null) + + # Filter by given criterias if results + if (!bone.search$matchType[1]%in%"NONE"){ + if (any(q.crit)){ + id.crit = c("rank","phylum","class","order","family")[q.crit] + p.crit = unlist(list(rank,phylum,class,order,family)[q.crit]) + n.test = id.crit%in%names(bone.search) + if (any(n.test)){ + # selecting which + id.crit2 = id.crit[n.test] + p.crit2 = p.crit[n.test] + # Apply the rigth criterias + for (i in 1:length(id.crit2)){ + bone.search = bone.search[c(bone.search[,id.crit2[i]])[[1]]%in%p.crit2[i],] + if (nrow(bone.search)==0){ + bone.search = data.frame(matchType="NONE") } } - if (!all(n.test)){ - pp = paste(id.crit[!n.test],collapse=", ") - warning(paste0("'",pp,"' level(s) not available for this taxa in GBIF, could not be employed...")) - } + } + if (!all(n.test)){ + pp = paste(id.crit[!n.test],collapse=", ") + warning(paste0("'",pp,"' level(s) not available for this taxa in GBIF, could not be employed...")) } } - - # Normal procedure with or without criterias - if (nrow(bone.search)>1){ - if (all(!bone.search$rank%in%c("SPECIES","SUBSPECIES","VARIETY"))){ + } + + # Normal procedure with or without criterias + if (nrow(bone.search)>1){ + if (all(!bone.search$rank%in%c("SPECIES","SUBSPECIES","VARIETY"))){ + cat("Not match found...","\n") + return(data.frame(NULL)) + + } else { + s.keep = bone.search[bone.search$rank%in%c("SPECIES","SUBSPECIES","VARIETY"),] + s.keep = s.keep[s.keep$status%in%c("ACCEPTED","SYNONYM"),] + s.keep = s.keep[s.keep$matchType%in%"EXACT",] + if (nrow(s.keep)==0){ cat("Not match found...","\n") return(data.frame(NULL)) - } else { - s.keep = bone.search[bone.search$rank%in%c("SPECIES","SUBSPECIES","VARIETY"),] - s.keep = s.keep[s.keep$status%in%c("ACCEPTED","SYNONYM"),] - s.keep = s.keep[s.keep$matchType%in%"EXACT",] - if (nrow(s.keep)==0){ - cat("Not match found...","\n") - return(data.frame(NULL)) - - } else if (nrow(s.keep)>1){ - - # If we only find subpsecies and variety, we need to (default) prioritize - if (all(s.keep$rank%in%c("VARIETY","SUBSPECIES"))){ - if ("var."%in%strsplit(sp_name," ")[[1]]){ - bone.search = s.keep[s.keep$rank%in%"VARIETY",] - if (nrow(bone.search)==0){ - bone.search = s.keep[s.keep$rank%in%"SUBSPECIES",] - } - - } else { + } else if (nrow(s.keep)>1){ + + # If we only find subpsecies and variety, we need to (default) prioritize + if (all(s.keep$rank%in%c("VARIETY","SUBSPECIES"))){ + if ("var."%in%strsplit(sp_name," ")[[1]]){ + bone.search = s.keep[s.keep$rank%in%"VARIETY",] + if (nrow(bone.search)==0){ bone.search = s.keep[s.keep$rank%in%"SUBSPECIES",] } } else { - bone.search = s.keep[s.keep$rank%in%"SPECIES",] - } - - coltax = c("familyKey","orderKey","classKey","phylumKey")%in%colnames(bone.search) - key.test = bone.search[,c("familyKey","orderKey","classKey","phylumKey")[coltax]] - - if (any(bone.search$status%in%"ACCEPTED") & length(unique(key.test[,1]))==1){ - bone.search = bone.search[bone.search$status%in%"ACCEPTED",] + bone.search = s.keep[s.keep$rank%in%"SUBSPECIES",] } } else { - bone.search = s.keep + bone.search = s.keep[s.keep$rank%in%"SPECIES",] } - # If not the same species overall return NULL - s.usp = length(unique(bone.search$speciesKey))==1 - if (!s.usp){ - cat("No synonyms distinction could be made. Consider using phylum/class/order/family...","\n") - return(data.frame(NULL)) - - } else { - bone.search = bone.search[1,] - } - } - } - } - if (bone.search$matchType%in%"NONE") { - cat("No species name found...","\n") - return(data.frame(NULL)) - } + coltax = c("familyKey","orderKey","classKey","phylumKey")%in%colnames(bone.search) + key.test = bone.search[,c("familyKey","orderKey","classKey","phylumKey")[coltax]] - if (bone.search$confidence[1]