|
| 1 | +#' Search GRSciColl collections |
| 2 | +#' |
| 3 | +#' @param query Simple full text search parameter. The value for this parameter |
| 4 | +#' can be a simple word or a phrase. Wildcards are not supported. |
| 5 | +#' @param name Name of a GrSciColl institution or collection. |
| 6 | +#' @param fuzzyName It searches by name fuzzily so the parameter doesn't have |
| 7 | +#' to be the exact name. |
| 8 | +#' @param preservationType Preservation type of a GrSciColl collection. Accepts |
| 9 | +#' multiple values. |
| 10 | +#' @param contentType Content type of a GrSciColl collection. See here for |
| 11 | +#' accepted values : |
| 12 | +#' https://techdocs.gbif.org/en/openapi/v1/registry#/Collections/listCollections |
| 13 | +#' @param numberSpecimens Number of specimens. It supports ranges and a `*` can |
| 14 | +#' be used as a wildcard. |
| 15 | +#' @param accessionStatus Accession status of a GrSciColl collection. Accepted |
| 16 | +#' values : INSTITUTIONAL, PROJECT |
| 17 | +#' @param personalCollection Flag for personal GRSciColl collections. |
| 18 | +#' @param sourceId sourceId of MasterSourceMetadata. |
| 19 | +#' @param source Source attribute of MasterSourceMetadata. Accepted values : |
| 20 | +#' DATASET, ORGANIZATION, IH_IRN |
| 21 | +#' @param code Code of a GrSciColl institution or collection. |
| 22 | +#' @param alternativeCode Alternative code of a GrSciColl institution or |
| 23 | +#' collection. |
| 24 | +#' @param contact Filters collections and institutions whose contacts contain |
| 25 | +#' the person key specified. |
| 26 | +#' @param institutionKey Keys of institutions to filter by. |
| 27 | +#' @param country Filters by country given as a ISO 639-1 (2 letter) country |
| 28 | +#' code. |
| 29 | +#' @param city Filters by the city of the address. It searches in both the |
| 30 | +#' physical and the mailing address. |
| 31 | +#' @param gbifRegion Filters by a gbif region |
| 32 | +#' Available values : AFRICA, ASIA, EUROPE, NORTH_AMERICA, OCEANIA, |
| 33 | +#' LATIN_AMERICA, ANTARCTICA. |
| 34 | +#' @param machineTagNamespace Filters for entities with a machine tag in the |
| 35 | +#' specified namespace. |
| 36 | +#' @param machineTagName Filters for entities with a machine tag with the |
| 37 | +#' specified name (use in combination with the machineTagNamespace parameter). |
| 38 | +#' @param machineTagValue Filters for entities with a machine tag with the |
| 39 | +#' specified value (use in combination with the machineTagNamespace and |
| 40 | +#' machineTagName parameters). |
| 41 | +#' @param identifier An identifier of the type given by the identifierType |
| 42 | +#' parameter, for example a DOI or UUID. |
| 43 | +#' @param identifierType An identifier type for the identifier parameter. |
| 44 | +#' Available values : URL, LSID, HANDLER, DOI, UUID, FTP, URI, UNKNOWN, |
| 45 | +#' GBIF_PORTAL, GBIF_NODE, GBIF_PARTICIPANT, GRSCICOLL_ID, GRSCICOLL_URI, |
| 46 | +#' IH_IRN, ROR, GRID, CITES, SYMBIOTA_UUID, WIKIDATA, NCBI_BIOCOLLECTION, |
| 47 | +#' ISIL, CLB_DATASET_KEY. |
| 48 | +#' @param active Active status of a GrSciColl institution or collection. |
| 49 | +#' @param displayOnNHCPortal Flag to show this record in the NHC portal. |
| 50 | +#' @param alternativeCode Alternative code of a GrSciColl institution or |
| 51 | +#' collection. |
| 52 | +#' @param masterSourceType The master source type of a GRSciColl institution. |
| 53 | +#' or collection. Available values : GRSCICOLL, GBIF_REGISTRY, IH. |
| 54 | +#' @param replacedBy Key of the entity that replaced another entity. |
| 55 | +#' @param sortBy Field to sort the results by. It only supports the fields |
| 56 | +#' contained in the enum. Available values : NUMBER_SPECIMENS. |
| 57 | +#' @param sortOrder Sort order to use with the sortBy parameter. |
| 58 | +#' Available values : ASC, DESC. |
| 59 | +#' @param offset Determines the offset for the search results. |
| 60 | +#' @param limit Controls the number of results in the page. Default 20. |
| 61 | +#' @param format (character) Format of the export. Default is "TSV". |
| 62 | +#' Only used for [collection_export]. |
| 63 | +#' @param curlopts curlopts options passed on to [crul::HttpClient]. |
| 64 | +#' |
| 65 | +#' @details Will return GRSciColl collections data. [collection_export] will |
| 66 | +#' return all of the results in a single `tibble`, while [collection_search] will |
| 67 | +#' return a sample of results. |
| 68 | +#' |
| 69 | +#' @return a `list` |
| 70 | +#' @export |
| 71 | +#' |
| 72 | +#' @references |
| 73 | +#' https://scientific-collections.gbif.org/connected-systems#grscicoll-data-coming-from-other-sources |
| 74 | +#' @examples \dontrun{ |
| 75 | +#' collection_search(query="insect",limit=2) |
| 76 | +#' collection_search(name="Insects;Entomology", limit=2) |
| 77 | +#' collection_search(numberSpecimens = "0,100", limit=1) |
| 78 | +#' collection_search(institutionKey = "6a6ac6c5-1b8a-48db-91a2-f8661274ff80" |
| 79 | +#' , limit = 1) |
| 80 | +#' collection_search(query = "insect", country = "US;GB", limit=1) |
| 81 | +#' } |
| 82 | +collection_search <- function( |
| 83 | + query = NULL, |
| 84 | + name = NULL, |
| 85 | + fuzzyName = NULL, |
| 86 | + preservationType = NULL, |
| 87 | + contentType = NULL, |
| 88 | + numberSpecimens = NULL, |
| 89 | + accessionStatus = NULL, |
| 90 | + personalCollection = NULL, |
| 91 | + sourceId = NULL, |
| 92 | + source = NULL, |
| 93 | + code = NULL, |
| 94 | + alternativeCode = NULL, |
| 95 | + contact = NULL, |
| 96 | + institutionKey = NULL, |
| 97 | + country = NULL, |
| 98 | + city = NULL, |
| 99 | + gbifRegion = NULL, |
| 100 | + machineTagNamespace = NULL, |
| 101 | + machineTagName = NULL, |
| 102 | + machineTagValue = NULL, |
| 103 | + identifier = NULL, |
| 104 | + identifierType = NULL, |
| 105 | + active = NULL, |
| 106 | + displayOnNHCPortal = NULL, |
| 107 | + masterSourceType = NULL, |
| 108 | + replacedBy = NULL, |
| 109 | + sortBy = NULL, |
| 110 | + sortOrder = NULL, |
| 111 | + offset = NULL, |
| 112 | + limit = NULL, |
| 113 | + format = NULL, |
| 114 | + curlopts = list(http_version=2) |
| 115 | + ) { |
| 116 | + |
| 117 | + assert(query,"character") |
| 118 | + assert(name,"character") |
| 119 | + assert(fuzzyName,"character") |
| 120 | + assert(preservationType,"character") |
| 121 | + assert(contentType, "character") |
| 122 | + assert(accessionStatus,"character") |
| 123 | + assert(personalCollection,"logical") |
| 124 | + assert(source,"character") |
| 125 | + assert(code,"character") |
| 126 | + assert(alternativeCode,"character") |
| 127 | + assert(contact,"character") |
| 128 | + assert(institutionKey,"character") |
| 129 | + assert(country,"character") |
| 130 | + assert(city,"character") |
| 131 | + assert(gbifRegion,"character") |
| 132 | + assert(machineTagNamespace,"character") |
| 133 | + assert(machineTagName,"character") |
| 134 | + assert(identifierType,"character") |
| 135 | + assert(active,"logical") |
| 136 | + assert(displayOnNHCPortal,"logical") |
| 137 | + assert(alternativeCode,"character") |
| 138 | + assert(masterSourceType,"character") |
| 139 | + assert(replacedBy,"character") |
| 140 | + assert(sortBy,"character") |
| 141 | + assert(limit,"numeric") |
| 142 | + assert(offset,"numeric") |
| 143 | + |
| 144 | + # check for valid values |
| 145 | + if(!is.null(sortBy)) { |
| 146 | + match.arg(sortBy, c("NUMBER_SPECIMENS")) |
| 147 | + } |
| 148 | + if(!is.null(sortOrder)) { |
| 149 | + match.arg(sortOrder, c("ASC", "DESC")) |
| 150 | + } |
| 151 | + |
| 152 | + args <- as.list( |
| 153 | + rgbif_compact(c( |
| 154 | + q = query, |
| 155 | + numberSpecimens = numberSpecimens, |
| 156 | + accessionStatus = accessionStatus, |
| 157 | + active = active, |
| 158 | + displayOnNHCPortal = displayOnNHCPortal, |
| 159 | + replacedBy = replacedBy, |
| 160 | + sortBy = sortBy, |
| 161 | + sortOrder = sortOrder, |
| 162 | + offset = offset, |
| 163 | + limit = limit |
| 164 | + ))) |
| 165 | + |
| 166 | + args <- rgbif_compact(c( |
| 167 | + args, |
| 168 | + convmany(name), |
| 169 | + convmany(fuzzyName), |
| 170 | + convmany(preservationType), |
| 171 | + convmany(contentType), |
| 172 | + convmany(personalCollection), |
| 173 | + convmany(sourceId), |
| 174 | + convmany(source), |
| 175 | + convmany(code), |
| 176 | + convmany(alternativeCode), |
| 177 | + convmany(contact), |
| 178 | + convmany(institutionKey), |
| 179 | + convmany(country), |
| 180 | + convmany(city), |
| 181 | + convmany(gbifRegion), |
| 182 | + convmany(machineTagNamespace), |
| 183 | + convmany(machineTagName), |
| 184 | + convmany(machineTagValue), |
| 185 | + convmany(identifier), |
| 186 | + convmany(identifierType), |
| 187 | + convmany(masterSourceType) |
| 188 | + )) |
| 189 | + |
| 190 | + url <- paste0(gbif_base(), '/grscicoll/collection') |
| 191 | + tt <- gbif_GET(url, args, TRUE, curlopts) |
| 192 | + |
| 193 | + meta <- tt[c('offset','limit','endOfRecords','count')] |
| 194 | + data <- tibble::as_tibble(tt$results) |
| 195 | + |
| 196 | + list(meta = as.data.frame(meta), data = data) |
| 197 | +} |
| 198 | + |
| 199 | +#' @export |
| 200 | +#' @rdname collection_search |
| 201 | +collection_export <- function( |
| 202 | + query = NULL, |
| 203 | + name = NULL, |
| 204 | + fuzzyName = NULL, |
| 205 | + preservationType = NULL, |
| 206 | + contentType = NULL, |
| 207 | + numberSpecimens = NULL, |
| 208 | + accessionStatus = NULL, |
| 209 | + personalCollection = NULL, |
| 210 | + sourceId = NULL, |
| 211 | + source = NULL, |
| 212 | + code = NULL, |
| 213 | + alternativeCode = NULL, |
| 214 | + contact = NULL, |
| 215 | + institutionKey = NULL, |
| 216 | + country = NULL, |
| 217 | + city = NULL, |
| 218 | + gbifRegion = NULL, |
| 219 | + machineTagNamespace = NULL, |
| 220 | + machineTagName = NULL, |
| 221 | + machineTagValue = NULL, |
| 222 | + identifier = NULL, |
| 223 | + identifierType = NULL, |
| 224 | + active = NULL, |
| 225 | + displayOnNHCPortal = NULL, |
| 226 | + masterSourceType = NULL, |
| 227 | + replacedBy = NULL, |
| 228 | + sortBy = NULL, |
| 229 | + sortOrder = NULL, |
| 230 | + offset = NULL, |
| 231 | + limit = NULL, |
| 232 | + format = "TSV", |
| 233 | + curlopts = list() |
| 234 | +) { |
| 235 | + |
| 236 | + # https://api.gbif.org/v1/grscicoll/collection/export?format=TSV&displayOnNHCPortal=true |
| 237 | + assert(query,"character") |
| 238 | + assert(name,"character") |
| 239 | + assert(fuzzyName,"character") |
| 240 | + assert(preservationType,"character") |
| 241 | + assert(contentType, "character") |
| 242 | + assert(accessionStatus,"character") |
| 243 | + assert(personalCollection,"logical") |
| 244 | + assert(source,"character") |
| 245 | + assert(code,"character") |
| 246 | + assert(alternativeCode,"character") |
| 247 | + assert(contact,"character") |
| 248 | + assert(institutionKey,"character") |
| 249 | + assert(country,"character") |
| 250 | + assert(city,"character") |
| 251 | + assert(gbifRegion,"character") |
| 252 | + assert(machineTagNamespace,"character") |
| 253 | + assert(machineTagName,"character") |
| 254 | + assert(identifierType,"character") |
| 255 | + assert(active,"logical") |
| 256 | + assert(displayOnNHCPortal,"logical") |
| 257 | + assert(alternativeCode,"character") |
| 258 | + assert(masterSourceType,"character") |
| 259 | + assert(replacedBy,"character") |
| 260 | + assert(sortBy,"character") |
| 261 | + assert(limit,"numeric") |
| 262 | + assert(offset,"numeric") |
| 263 | + |
| 264 | + if(format != "TSV") { |
| 265 | + warning("Only 'TSV' format is supported for collection_export") |
| 266 | + } |
| 267 | + if(!is.null(limit) | !is.null(offset)) { |
| 268 | + warning("Limit and offset are ignored for collection_export. The full export |
| 269 | + is returned.") |
| 270 | + } |
| 271 | + |
| 272 | + args <- as.list( |
| 273 | + rgbif_compact(c( |
| 274 | + q = query, |
| 275 | + numberSpecimens = numberSpecimens, |
| 276 | + accessionStatus = accessionStatus, |
| 277 | + active = active, |
| 278 | + displayOnNHCPortal = displayOnNHCPortal, |
| 279 | + replacedBy = replacedBy, |
| 280 | + sortBy = sortBy, |
| 281 | + sortOrder = sortOrder, |
| 282 | + offset = offset, |
| 283 | + limit = limit, |
| 284 | + format = format |
| 285 | + ))) |
| 286 | + |
| 287 | + args <- rgbif_compact(c( |
| 288 | + args, |
| 289 | + convmany(name), |
| 290 | + convmany(fuzzyName), |
| 291 | + convmany(preservationType), |
| 292 | + convmany(contentType), |
| 293 | + convmany(personalCollection), |
| 294 | + convmany(sourceId), |
| 295 | + convmany(source), |
| 296 | + convmany(code), |
| 297 | + convmany(alternativeCode), |
| 298 | + convmany(contact), |
| 299 | + convmany(institutionKey), |
| 300 | + convmany(country), |
| 301 | + convmany(city), |
| 302 | + convmany(gbifRegion), |
| 303 | + convmany(machineTagNamespace), |
| 304 | + convmany(machineTagName), |
| 305 | + convmany(machineTagValue), |
| 306 | + convmany(identifier), |
| 307 | + convmany(identifierType), |
| 308 | + convmany(masterSourceType) |
| 309 | + )) |
| 310 | + |
| 311 | + url_query <- paste0(names(args),"=",args,collapse="&") |
| 312 | + url_query <- utils::URLencode(url_query) |
| 313 | + url <- paste0(gbif_base(),"/grscicoll/collection/export?",url_query) |
| 314 | + temp_file <- tempfile() |
| 315 | + utils::download.file(url,destfile=temp_file,quiet=TRUE) |
| 316 | + out <- tibble::as_tibble(data.table::fread(temp_file, showProgress=FALSE)) |
| 317 | + colnames(out) <- to_camel(colnames(out)) |
| 318 | + out |
| 319 | +} |
| 320 | + |
0 commit comments