R/parameter.R

# mzTab-M reference implementation and validation API.
#
# This is the mzTab-M reference implementation and validation API service.
#
# The version of the OpenAPI document: 2.0.0
# Contact: nils.hoffmann@cebitec.uni-bielefeld.de
# Generated by: https://openapi-generator.tech

#' @docType class
#' @title Parameter
#' @description Parameter Class
#' @format An \code{R6Class} generator object
#' @field id  integer [optional]
#'
#' @field cv_label  character [optional]
#'
#' @field cv_accession  character [optional]
#'
#' @field name  character 
#'
#' @field value  character 
#'
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Parameter <- R6::R6Class(
  'Parameter',
  public = list(
    `id` = NULL,
    `cv_label` = NULL,
    `cv_accession` = NULL,
    `name` = NULL,
    `value` = NULL,
    #' @description Create a Parameter
    #' @param name Parameter name.
    #' @param value Parameter value.
    #' @param id Parameter id.
    #' @param cv_label The label of the controlled vocabulary.
    #' @param cv_accession The accession number of this Parameter.
    #' @param ... local optional variable arguments
    #'     
    initialize = function(`name`, `value`, `id`=NULL, `cv_label`='', `cv_accession`='', ...){
      local.optional.var <- list(...)
      if (!missing(`name`)) {
        stopifnot(is.character(`name`), length(`name`) == 1)
        self$`name` <- `name`
      }
      if (!missing(`value`)) {
        stopifnot(is.character(`value`), length(`value`) == 1)
        self$`value` <- `value`
      }
      if (!is.null(`id`)) {
        stopifnot(is.numeric(`id`), length(`id`) == 1)
        self$`id` <- `id`
      }
      if (!is.null(`cv_label`)) {
        stopifnot(is.character(`cv_label`), length(`cv_label`) == 1)
        self$`cv_label` <- `cv_label`
      }
      if (!is.null(`cv_accession`)) {
        stopifnot(is.character(`cv_accession`), length(`cv_accession`) == 1)
        self$`cv_accession` <- `cv_accession`
      }
    },
    #' @description Serialize to list object suitable for jsonlite
    toJSON = function() {
      ParameterObject <- list()
      if (is.null(self$`name`) || is.na(self$`name`)) {
        return(NULL)
      }
      if (!is.null(self$`id`)) {
        ParameterObject[['id']] <-
          rmzTabM::safe_unbox(self$`id`)
      }
      if (!is.null(self$`cv_label`)) {
        ParameterObject[['cv_label']] <-
          rmzTabM::safe_unbox(self$`cv_label`)
      }
      if (!is.null(self$`cv_accession`)) {
        ParameterObject[['cv_accession']] <-
          rmzTabM::safe_unbox(self$`cv_accession`)
      }
      if (!is.null(self$`name`)) {
        ParameterObject[['name']] <-
          rmzTabM::safe_unbox(self$`name`)
      }
      if (!is.null(self$`value`)) {
        ParameterObject[['value']] <-
          rmzTabM::safe_unbox(self$`value`)
      }

      ParameterObject
    },
    #' @description Deserialize from jsonlite list object
    #' @param ParameterJson list object.    
    fromJSON = function(ParameterJson) {
      ParameterObject <- jsonlite::fromJSON(ParameterJson)
      if (!is.null(ParameterObject$`id`)) {
        self$`id` <- ParameterObject$`id`
      }
      if (!is.null(ParameterObject$`cv_label`)) {
        self$`cv_label` <- ParameterObject$`cv_label`
      }
      if (!is.null(ParameterObject$`cv_accession`)) {
        self$`cv_accession` <- ParameterObject$`cv_accession`
      }
      if (!is.null(ParameterObject$`name`)) {
        self$`name` <- ParameterObject$`name`
      }
      if (!is.null(ParameterObject$`value`)) {
        self$`value` <- ParameterObject$`value`
      }
    },
    #' @description Serialize to JSON string.    
    toJSONString = function() {
      jsoncontent <- c(
        if (!is.null(self$`id`)) {
        sprintf(
        '"id":
          %d
                ',
        rmzTabM::safe_unbox(self$`id`)
        )},
        if (!is.null(self$`cv_label`)) {
        sprintf(
        '"cv_label":
          "%s"
                ',
        rmzTabM::safe_unbox(self$`cv_label`)
        )},
        if (!is.null(self$`cv_accession`)) {
        sprintf(
        '"cv_accession":
          "%s"
                ',
        rmzTabM::safe_unbox(self$`cv_accession`)
        )},
        if (!is.null(self$`name`)) {
        sprintf(
        '"name":
          "%s"
                ',
        rmzTabM::safe_unbox(self$`name`)
        )},
        if (!is.null(self$`value`)) {
        sprintf(
        '"value":
          "%s"
                ',
        rmzTabM::safe_unbox(self$`value`)
        )}
      )
      jsoncontent <- paste(jsoncontent, collapse = ",")
      paste('{', jsoncontent, '}', sep = "")
    },
    #' @description Deserialize from JSON string
    #' @param ParameterJson JSON string
    fromJSONString = function(ParameterJson) {
      ParameterObject <- jsonlite::fromJSON(ParameterJson)
      self$`id` <- ParameterObject$`id`
      self$`cv_label` <- ParameterObject$`cv_label`
      self$`cv_accession` <- ParameterObject$`cv_accession`
      self$`name` <- ParameterObject$`name`
      self$`value` <- ParameterObject$`value`
      self
    },
    #' @description Serialize to string
    toString = function() {
      name <- self$`name`
      if(grepl(",", name, fixed = TRUE)) {
        name <- paste0("\"", name, "\"")
      }
      value <- self$`value`
      if(!is.null(value) && grepl(",", value, fixed = TRUE)) {
        value <- paste0("\"", value, "\"")
      }
      paste0(
        "[",
        paste(
          self$`cv_label`,
          self$`cv_accession`,
          name,
          value,
          sep = ", "
        ),
        "]"
      )
    },
    #' @description Deserialize from string
    #' @param id Optional id of the Parameter.
    #' @param ParameterString String representation of the Parameter.
    fromString = function(id, ParameterString) {
      if (!is.null(id)) {
        self$`id` <- as.numeric(id)
      }
      paramExpr <- ',([^\\"]*"[^\\"]*\\")*[^\\"]*'
      paramContent <- gsub(", ", ",", gsub("]", "", gsub("[", "", ParameterString, fixed=TRUE), fixed=TRUE), fixed=TRUE)
      paramFields <- strsplit(paramContent, ",", perl=TRUE)
      self$`cv_label` <- trimws(paramFields[[1]][1], which="both")
      self$`cv_accession` <- trimws(paramFields[[1]][2], which="both")
      name <- trimws(paramFields[[1]][3], which="both")
      if (!is.null(name)) {
        self$`name` <- gsub("\"", "", name, fixed=TRUE)
      }
      value <- trimws(paramFields[[1]][4], which="both")
      if (!is.na(value) && !is.null(value) && value!="") {
        self$`value` <- gsub("\"", "", value, fixed=TRUE)
      } else {
        self$`value` <- NULL
      }
      self
    }
  )
)
lifs-tools/rmzTab-m documentation built on Jan. 26, 2023, 4:45 p.m.