R/Taxon.r

# Netherlands Biodiversity API
#
# Access to the digitised Natural History collection at the Naturalis Biodiversity Center
#
# OpenAPI spec version: v2
# Contact: support@naturalis.nl
# Generated by: https://github.com/swagger-api/swagger-codegen.git

#'
#' @docType class
#'
#' @format R6 class
#'
#' @usage
#' # Taxon$new()
#'
#' @format
#' R6 class
#'
#' @title Taxon Class
#'
#' @description
#' For more information on the NBA object model, please refer to the
#' official NBA documentation at
#' \href{https://docs.biodiversitydata.nl}{https://docs.biodiversitydata.nl} and
#' the NBA model and endpoints reference at
#' \href{https://docs.biodiversitydata.nl/endpoints-reference}{https://docs.biodiversitydata.nl/endpoints-reference}.
#'
#' @details Model class for Taxon objects. A taxon record stores the hierarchical classification of a taxon, its scientific names and synonyms, and other relevant data retrieved from the respective source system. Taxonomic information is sourced from two species checklists: The Catalogue of Life (COL) and the Dutch Species Register (NSR).
#'
#'
#' @field sourceSystem  SourceSystem
#'
#' @field sourceSystemId  character
#'
#' @field recordURI  character
#'
#' @field id  character
#'
#' @field sourceSystemParentId  character
#'
#' @field taxonRank  character
#'
#' @field taxonRemarks  character
#'
#' @field occurrenceStatusVerbatim  character
#'
#' @field acceptedName  ScientificName
#'
#' @field defaultClassification  DefaultClassification
#'
#' @field systemClassification  list(Monomial)
#'
#' @field synonyms  list(ScientificName)
#'
#' @field vernacularNames  list(VernacularName)
#'
#' @field descriptions  list(TaxonDescription)
#'
#' @field references  list(Reference)
#'
#' @field experts  list(Expert)
#'
#' @field validName  ScientificName
#'
#'
#'
#' @section Methods:
#' \describe{
#'
#' \item{\code{$new()}}{
#'
#'   Constructor Taxon object.
#'
#' }
#' \item{\code{$fromList(TaxonList)}}{
#'
#'   Create Taxon object from list.
#'
#' }
#'
#' \item{\code{$toList()}}{
#'
#'   Get list representation of Taxon.
#'
#' }
#' \item{\code{fromJSONString(TaxonJson)}}{
#'
#'   Create Taxon object from JSON.
#'
#' }
#' \item{\code{toJSONString(pretty=TRUE)}}{
#'
#'   Get JSON representation of Taxon.
#'
#' }
#' }
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Taxon <- R6::R6Class(
  "Taxon",
  public = list(
    `sourceSystem` = NULL,
    `sourceSystemId` = NULL,
    `recordURI` = NULL,
    `id` = NULL,
    `sourceSystemParentId` = NULL,
    `taxonRank` = NULL,
    `taxonRemarks` = NULL,
    `occurrenceStatusVerbatim` = NULL,
    `acceptedName` = NULL,
    `defaultClassification` = NULL,
    `systemClassification` = NULL,
    `synonyms` = NULL,
    `vernacularNames` = NULL,
    `descriptions` = NULL,
    `references` = NULL,
    `experts` = NULL,
    `validName` = NULL,
    initialize = function(
                              `sourceSystem`,
                              `sourceSystemId`,
                              `recordURI`,
                              `id`,
                              `sourceSystemParentId`,
                              `taxonRank`,
                              `taxonRemarks`,
                              `occurrenceStatusVerbatim`,
                              `acceptedName`,
                              `defaultClassification`,
                              `systemClassification`,
                              `synonyms`,
                              `vernacularNames`,
                              `descriptions`,
                              `references`,
                              `experts`,
                              `validName`) {
      if (!missing(`sourceSystem`)) {
        stopifnot(R6::is.R6(`sourceSystem`))
        self[["sourceSystem"]] <- `sourceSystem`
      }
      if (!missing(`sourceSystemId`)) {
        stopifnot(
          is.character(`sourceSystemId`),
          length(`sourceSystemId`) == 1
        )
        self[["sourceSystemId"]] <- `sourceSystemId`
      }
      if (!missing(`recordURI`)) {
        stopifnot(
          is.character(`recordURI`),
          length(`recordURI`) == 1
        )
        self[["recordURI"]] <- `recordURI`
      }
      if (!missing(`id`)) {
        stopifnot(
          is.character(`id`),
          length(`id`) == 1
        )
        self[["id"]] <- `id`
      }
      if (!missing(`sourceSystemParentId`)) {
        stopifnot(
          is.character(`sourceSystemParentId`),
          length(`sourceSystemParentId`) == 1
        )
        self[["sourceSystemParentId"]] <- `sourceSystemParentId`
      }
      if (!missing(`taxonRank`)) {
        stopifnot(
          is.character(`taxonRank`),
          length(`taxonRank`) == 1
        )
        self[["taxonRank"]] <- `taxonRank`
      }
      if (!missing(`taxonRemarks`)) {
        stopifnot(
          is.character(`taxonRemarks`),
          length(`taxonRemarks`) == 1
        )
        self[["taxonRemarks"]] <- `taxonRemarks`
      }
      if (!missing(`occurrenceStatusVerbatim`)) {
        stopifnot(
          is.character(`occurrenceStatusVerbatim`),
          length(`occurrenceStatusVerbatim`) == 1
        )
        self[["occurrenceStatusVerbatim"]] <- `occurrenceStatusVerbatim`
      }
      if (!missing(`acceptedName`)) {
        stopifnot(R6::is.R6(`acceptedName`))
        self[["acceptedName"]] <- `acceptedName`
      }
      if (!missing(`defaultClassification`)) {
        stopifnot(R6::is.R6(`defaultClassification`))
        self[["defaultClassification"]] <- `defaultClassification`
      }
      if (!missing(`systemClassification`)) {
        stopifnot(
          is.list(`systemClassification`),
          length(`systemClassification`) != 0
        )
        lapply(
          `systemClassification`,
          function(x) stopifnot(R6::is.R6(x))
        )
        ## omit names as they should not be part of JSON representation
        self[["systemClassification"]] <- unname(`systemClassification`)
      }
      if (!missing(`synonyms`)) {
        stopifnot(
          is.list(`synonyms`),
          length(`synonyms`) != 0
        )
        lapply(
          `synonyms`,
          function(x) stopifnot(R6::is.R6(x))
        )
        ## omit names as they should not be part of JSON representation
        self[["synonyms"]] <- unname(`synonyms`)
      }
      if (!missing(`vernacularNames`)) {
        stopifnot(
          is.list(`vernacularNames`),
          length(`vernacularNames`) != 0
        )
        lapply(
          `vernacularNames`,
          function(x) stopifnot(R6::is.R6(x))
        )
        ## omit names as they should not be part of JSON representation
        self[["vernacularNames"]] <- unname(`vernacularNames`)
      }
      if (!missing(`descriptions`)) {
        stopifnot(
          is.list(`descriptions`),
          length(`descriptions`) != 0
        )
        lapply(
          `descriptions`,
          function(x) stopifnot(R6::is.R6(x))
        )
        ## omit names as they should not be part of JSON representation
        self[["descriptions"]] <- unname(`descriptions`)
      }
      if (!missing(`references`)) {
        stopifnot(
          is.list(`references`),
          length(`references`) != 0
        )
        lapply(
          `references`,
          function(x) stopifnot(R6::is.R6(x))
        )
        ## omit names as they should not be part of JSON representation
        self[["references"]] <- unname(`references`)
      }
      if (!missing(`experts`)) {
        stopifnot(
          is.list(`experts`),
          length(`experts`) != 0
        )
        lapply(
          `experts`,
          function(x) stopifnot(R6::is.R6(x))
        )
        ## omit names as they should not be part of JSON representation
        self[["experts"]] <- unname(`experts`)
      }
      if (!missing(`validName`)) {
        stopifnot(R6::is.R6(`validName`))
        self[["validName"]] <- `validName`
      }
    },
    toList = function() {
      TaxonList <- list()
      if (!is.null(self[["sourceSystem"]])) {
        TaxonList[["sourceSystem"]] <-
          self[["sourceSystem"]]$toList()
      }
      if (!is.null(self[["sourceSystemId"]])) {
        TaxonList[["sourceSystemId"]] <-
          self[["sourceSystemId"]]
      }
      if (!is.null(self[["recordURI"]])) {
        TaxonList[["recordURI"]] <-
          self[["recordURI"]]
      }
      if (!is.null(self[["id"]])) {
        TaxonList[["id"]] <-
          self[["id"]]
      }
      if (!is.null(self[["sourceSystemParentId"]])) {
        TaxonList[["sourceSystemParentId"]] <-
          self[["sourceSystemParentId"]]
      }
      if (!is.null(self[["taxonRank"]])) {
        TaxonList[["taxonRank"]] <-
          self[["taxonRank"]]
      }
      if (!is.null(self[["taxonRemarks"]])) {
        TaxonList[["taxonRemarks"]] <-
          self[["taxonRemarks"]]
      }
      if (!is.null(self[["occurrenceStatusVerbatim"]])) {
        TaxonList[["occurrenceStatusVerbatim"]] <-
          self[["occurrenceStatusVerbatim"]]
      }
      if (!is.null(self[["acceptedName"]])) {
        TaxonList[["acceptedName"]] <-
          self[["acceptedName"]]$toList()
      }
      if (!is.null(self[["defaultClassification"]])) {
        TaxonList[["defaultClassification"]] <-
          self[["defaultClassification"]]$toList()
      }
      if (!is.null(self[["systemClassification"]])) {
        TaxonList[["systemClassification"]] <-
          lapply(self[["systemClassification"]], function(x) x$toList())
      }
      if (!is.null(self[["synonyms"]])) {
        TaxonList[["synonyms"]] <-
          lapply(self[["synonyms"]], function(x) x$toList())
      }
      if (!is.null(self[["vernacularNames"]])) {
        TaxonList[["vernacularNames"]] <-
          lapply(self[["vernacularNames"]], function(x) x$toList())
      }
      if (!is.null(self[["descriptions"]])) {
        TaxonList[["descriptions"]] <-
          lapply(self[["descriptions"]], function(x) x$toList())
      }
      if (!is.null(self[["references"]])) {
        TaxonList[["references"]] <-
          lapply(self[["references"]], function(x) x$toList())
      }
      if (!is.null(self[["experts"]])) {
        TaxonList[["experts"]] <-
          lapply(self[["experts"]], function(x) x$toList())
      }
      if (!is.null(self[["validName"]])) {
        TaxonList[["validName"]] <-
          self[["validName"]]$toList()
      }
      ## omit empty nested lists in returned list
      TaxonList[vapply(
        TaxonList,
        length,
        FUN.VALUE = integer(1)
      ) > 0]
    },
    fromList = function(TaxonList,
                            typeMapping = NULL) {
      self[["sourceSystem"]] <- SourceSystem$new()$fromList(
        TaxonList[["sourceSystem"]],
        typeMapping = typeMapping
      )
      self[["sourceSystemId"]] <-
        TaxonList[["sourceSystemId"]]
      self[["recordURI"]] <-
        TaxonList[["recordURI"]]
      self[["id"]] <-
        TaxonList[["id"]]
      self[["sourceSystemParentId"]] <-
        TaxonList[["sourceSystemParentId"]]
      self[["taxonRank"]] <-
        TaxonList[["taxonRank"]]
      self[["taxonRemarks"]] <-
        TaxonList[["taxonRemarks"]]
      self[["occurrenceStatusVerbatim"]] <-
        TaxonList[["occurrenceStatusVerbatim"]]
      self[["acceptedName"]] <- ScientificName$new()$fromList(
        TaxonList[["acceptedName"]],
        typeMapping = typeMapping
      )
      self[["defaultClassification"]] <- DefaultClassification$new()$fromList(
        TaxonList[["defaultClassification"]],
        typeMapping = typeMapping
      )
      self[["systemClassification"]] <- lapply(
        TaxonList[["systemClassification"]],
        function(x) {
          Monomial$new()$fromList(x,
            typeMapping = typeMapping
          )
        }
      )
      self[["synonyms"]] <- lapply(
        TaxonList[["synonyms"]],
        function(x) {
          ScientificName$new()$fromList(x,
            typeMapping = typeMapping
          )
        }
      )
      self[["vernacularNames"]] <- lapply(
        TaxonList[["vernacularNames"]],
        function(x) {
          VernacularName$new()$fromList(x,
            typeMapping = typeMapping
          )
        }
      )
      self[["descriptions"]] <- lapply(
        TaxonList[["descriptions"]],
        function(x) {
          TaxonDescription$new()$fromList(x,
            typeMapping = typeMapping
          )
        }
      )
      self[["references"]] <- lapply(
        TaxonList[["references"]],
        function(x) {
          Reference$new()$fromList(x,
            typeMapping = typeMapping
          )
        }
      )
      self[["experts"]] <- lapply(
        TaxonList[["experts"]],
        function(x) {
          Expert$new()$fromList(x,
            typeMapping = typeMapping
          )
        }
      )
      self[["validName"]] <- ScientificName$new()$fromList(
        TaxonList[["validName"]],
        typeMapping = typeMapping
      )
      invisible(self)
    },
    toJSONString = function(pretty = TRUE) {
      jsonlite::toJSON(
        self$toList(),
        simplifyVector = TRUE,
        auto_unbox = TRUE,
        pretty = pretty
      )
    },
    fromJSONString = function(TaxonJson,
                                  typeMapping = NULL) {
      TaxonList <- jsonlite::fromJSON(
        TaxonJson,
        simplifyVector = FALSE
      )
      self <- self$fromList(TaxonList)
      invisible(self)
    },
    print = function(...) {
      ## print class name
      cat("<Taxon>\n")
      ## print all members with values
      cat("Fields:\n")
      if (typeof(self$sourceSystem) == "environment") {
        cat("\tsourceSystem:\tobject of class", paste0("<", class(self$sourceSystem)[1], ">"), "\n")
      }
      else if (typeof(self$sourceSystem) == "list") {
        cat("\tsourceSystem:\tlist of length", length(self$sourceSystem), "\n")
      }
      else {
        cat("\tsourceSystem:\t", self$sourceSystem, "\n")
      }
      if (typeof(self$sourceSystemId) == "environment") {
        cat("\tsourceSystemId:\tobject of class", paste0("<", class(self$sourceSystemId)[1], ">"), "\n")
      }
      else if (typeof(self$sourceSystemId) == "list") {
        cat("\tsourceSystemId:\tlist of length", length(self$sourceSystemId), "\n")
      }
      else {
        cat("\tsourceSystemId:\t", self$sourceSystemId, "\n")
      }
      if (typeof(self$recordURI) == "environment") {
        cat("\trecordURI:\tobject of class", paste0("<", class(self$recordURI)[1], ">"), "\n")
      }
      else if (typeof(self$recordURI) == "list") {
        cat("\trecordURI:\tlist of length", length(self$recordURI), "\n")
      }
      else {
        cat("\trecordURI:\t", self$recordURI, "\n")
      }
      if (typeof(self$id) == "environment") {
        cat("\tid:\tobject of class", paste0("<", class(self$id)[1], ">"), "\n")
      }
      else if (typeof(self$id) == "list") {
        cat("\tid:\tlist of length", length(self$id), "\n")
      }
      else {
        cat("\tid:\t", self$id, "\n")
      }
      if (typeof(self$sourceSystemParentId) == "environment") {
        cat("\tsourceSystemParentId:\tobject of class", paste0("<", class(self$sourceSystemParentId)[1], ">"), "\n")
      }
      else if (typeof(self$sourceSystemParentId) == "list") {
        cat("\tsourceSystemParentId:\tlist of length", length(self$sourceSystemParentId), "\n")
      }
      else {
        cat("\tsourceSystemParentId:\t", self$sourceSystemParentId, "\n")
      }
      if (typeof(self$taxonRank) == "environment") {
        cat("\ttaxonRank:\tobject of class", paste0("<", class(self$taxonRank)[1], ">"), "\n")
      }
      else if (typeof(self$taxonRank) == "list") {
        cat("\ttaxonRank:\tlist of length", length(self$taxonRank), "\n")
      }
      else {
        cat("\ttaxonRank:\t", self$taxonRank, "\n")
      }
      if (typeof(self$taxonRemarks) == "environment") {
        cat("\ttaxonRemarks:\tobject of class", paste0("<", class(self$taxonRemarks)[1], ">"), "\n")
      }
      else if (typeof(self$taxonRemarks) == "list") {
        cat("\ttaxonRemarks:\tlist of length", length(self$taxonRemarks), "\n")
      }
      else {
        cat("\ttaxonRemarks:\t", self$taxonRemarks, "\n")
      }
      if (typeof(self$occurrenceStatusVerbatim) == "environment") {
        cat("\toccurrenceStatusVerbatim:\tobject of class", paste0("<", class(self$occurrenceStatusVerbatim)[1], ">"), "\n")
      }
      else if (typeof(self$occurrenceStatusVerbatim) == "list") {
        cat("\toccurrenceStatusVerbatim:\tlist of length", length(self$occurrenceStatusVerbatim), "\n")
      }
      else {
        cat("\toccurrenceStatusVerbatim:\t", self$occurrenceStatusVerbatim, "\n")
      }
      if (typeof(self$acceptedName) == "environment") {
        cat("\tacceptedName:\tobject of class", paste0("<", class(self$acceptedName)[1], ">"), "\n")
      }
      else if (typeof(self$acceptedName) == "list") {
        cat("\tacceptedName:\tlist of length", length(self$acceptedName), "\n")
      }
      else {
        cat("\tacceptedName:\t", self$acceptedName, "\n")
      }
      if (typeof(self$defaultClassification) == "environment") {
        cat("\tdefaultClassification:\tobject of class", paste0("<", class(self$defaultClassification)[1], ">"), "\n")
      }
      else if (typeof(self$defaultClassification) == "list") {
        cat("\tdefaultClassification:\tlist of length", length(self$defaultClassification), "\n")
      }
      else {
        cat("\tdefaultClassification:\t", self$defaultClassification, "\n")
      }
      if (typeof(self$systemClassification) == "environment") {
        cat("\tsystemClassification:\tobject of class", paste0("<", class(self$systemClassification)[1], ">"), "\n")
      }
      else if (typeof(self$systemClassification) == "list") {
        cat("\tsystemClassification:\tlist of length", length(self$systemClassification), "\n")
      }
      else {
        cat("\tsystemClassification:\t", self$systemClassification, "\n")
      }
      if (typeof(self$synonyms) == "environment") {
        cat("\tsynonyms:\tobject of class", paste0("<", class(self$synonyms)[1], ">"), "\n")
      }
      else if (typeof(self$synonyms) == "list") {
        cat("\tsynonyms:\tlist of length", length(self$synonyms), "\n")
      }
      else {
        cat("\tsynonyms:\t", self$synonyms, "\n")
      }
      if (typeof(self$vernacularNames) == "environment") {
        cat("\tvernacularNames:\tobject of class", paste0("<", class(self$vernacularNames)[1], ">"), "\n")
      }
      else if (typeof(self$vernacularNames) == "list") {
        cat("\tvernacularNames:\tlist of length", length(self$vernacularNames), "\n")
      }
      else {
        cat("\tvernacularNames:\t", self$vernacularNames, "\n")
      }
      if (typeof(self$descriptions) == "environment") {
        cat("\tdescriptions:\tobject of class", paste0("<", class(self$descriptions)[1], ">"), "\n")
      }
      else if (typeof(self$descriptions) == "list") {
        cat("\tdescriptions:\tlist of length", length(self$descriptions), "\n")
      }
      else {
        cat("\tdescriptions:\t", self$descriptions, "\n")
      }
      if (typeof(self$references) == "environment") {
        cat("\treferences:\tobject of class", paste0("<", class(self$references)[1], ">"), "\n")
      }
      else if (typeof(self$references) == "list") {
        cat("\treferences:\tlist of length", length(self$references), "\n")
      }
      else {
        cat("\treferences:\t", self$references, "\n")
      }
      if (typeof(self$experts) == "environment") {
        cat("\texperts:\tobject of class", paste0("<", class(self$experts)[1], ">"), "\n")
      }
      else if (typeof(self$experts) == "list") {
        cat("\texperts:\tlist of length", length(self$experts), "\n")
      }
      else {
        cat("\texperts:\t", self$experts, "\n")
      }
      if (typeof(self$validName) == "environment") {
        cat("\tvalidName:\tobject of class", paste0("<", class(self$validName)[1], ">"), "\n")
      }
      else if (typeof(self$validName) == "list") {
        cat("\tvalidName:\tlist of length", length(self$validName), "\n")
      }
      else {
        cat("\tvalidName:\t", self$validName, "\n")
      }
      ## print all methods
      cat("Methods:\n")
      cat("\tfromJSONString\n")
      cat("\ttoJSONString\n")
      cat("\tfromList\n")
      cat("\ttoList\n")
      cat("\tprint\n")
      invisible(self)
    }
  )
)
naturalis/nbaR documentation built on Nov. 12, 2023, 4:47 p.m.