R/software.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 Software
#' @description Software Class
#' @format An \code{R6Class} generator object
#' @field id  integer [optional]
#'
#' @field parameter  \link{Parameter} [optional]
#'
#' @field setting  list( character ) [optional]
#'
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Software <- R6::R6Class(
  'Software',
  public = list(
    `id` = NULL,
    `parameter` = NULL,
    `setting` = NULL,
    #' @description Create a Software
    #' @param id Software id.
    #' @param parameter CV parameter for software \link{Parameter}.
    #' @param setting Software settings list( character ).
    #' @param ... local optional variable arguments
    #' 
    initialize = function(`id`=NULL, `parameter`=NULL, `setting`=NULL, ...){
      local.optional.var <- list(...)
      if (!is.null(`id`)) {
        stopifnot(is.numeric(`id`), length(`id`) == 1)
        self$`id` <- `id`
      }
      if (!is.null(`parameter`)) {
        stopifnot(R6::is.R6(`parameter`))
        self$`parameter` <- `parameter`
      }
      if (!is.null(`setting`)) {
        stopifnot(is.vector(`setting`), length(`setting`) != 0)
        sapply(`setting`, function(x) stopifnot(is.character(x)))
        self$`setting` <- `setting`
      }
    },
    #' @description Serialize to list object suitable for jsonlite    
    toJSON = function() {
      SoftwareObject <- list()
      if (!is.null(self$`id`)) {
        SoftwareObject[['id']] <-
          rmzTabM::safe_unbox(self$`id`)
      }
      if (!is.null(self$`parameter`)) {
        SoftwareObject[['parameter']] <-
          self$`parameter`$toJSON()
      }
      if (!is.null(self$`setting`)) {
        SoftwareObject[['setting']] <-
          unlist(self$`setting`)
      }

      SoftwareObject
    },
    #' @description Deserialize from jsonlite list object
    #' @param SoftwareJson list object.    
    fromJSON = function(SoftwareJson) {
      SoftwareObject <- jsonlite::fromJSON(SoftwareJson)
      if (!is.null(SoftwareObject$`id`)) {
        self$`id` <- SoftwareObject$`id`
      }
      if (!is.null(SoftwareObject$`parameter`)) {
        parameterObject <- Parameter$new()
        parameterObject$fromJSON(jsonlite::toJSON(SoftwareObject$parameter, auto_unbox = TRUE, null = "null", na = "null", digits = NA))
        self$`parameter` <- parameterObject
      }
      if (!is.null(SoftwareObject$`setting`)) {
        self$`setting` <- ApiClient$new()$deserializeObj(SoftwareObject$`setting`, "array[character]", loadNamespace("rmzTabM"))
      }
    },
    #' @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$`parameter`)) {
        sprintf(
        '"parameter":
        %s
        ',
        jsonlite::toJSON(self$`parameter`$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)
        )},
        if (!is.null(self$`setting`)) {
        sprintf(
        '"setting":
           %s
        ',
        jsonlite::toJSON(unlist(self$`setting`), auto_unbox = FALSE, null = "null", na = "null", digits = NA)
        )}
      )
      jsoncontent <- paste(jsoncontent, collapse = ",")
      paste('{', jsoncontent, '}', sep = "")
    },
    #' @description Deserialize from JSON string
    #' @param SoftwareJson JSON string
    fromJSONString = function(SoftwareJson) {
      SoftwareObject <- jsonlite::fromJSON(SoftwareJson)
      self$`id` <- SoftwareObject$`id`
      self$`parameter` <- Parameter$new()$fromJSONString(jsonlite::toJSON(SoftwareObject$parameter, auto_unbox = TRUE, null = "null", na = "null", digits = NA))
      self$`setting` <- ApiClient$new()$deserializeObj(SoftwareObject$`setting`, "array[character]", loadNamespace("rmzTabM"))
      self
    },
    #' @description Serialize to data frame
    toDataFrame = function() {
      idPrefix <- paste0("software[", self$`id`, "]")
      elements <- data.frame(PREFIX=character(), KEY=character(), VALUE=character())
      if (!is.null(self$`parameter`)) {
        elements <-
          rbind(elements,
                list(
                  PREFIX = "MTD",
                  KEY = idPrefix,
                  VALUE = self$`parameter`$toString()
                ),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`setting`)) {
        elements <-
          rbind(elements,
                lapply(seq_along(self$`setting`), function(idx, elements, idPrefix) {
                  lapply(seq_along(elements[[idx]]), function(eidx, idx, element, idPrefix) {
                    list(PREFIX = "MTD", KEY=paste(idPrefix, paste0("setting[", eidx, "]"), sep="-"), VALUE=element[[eidx]])  
                  }, idx=idx, element=elements[[idx]], idPrefix=idPrefix)
                }, elements=self$`setting`, idPrefix=idPrefix) %>% dplyr::bind_rows(),
                stringsAsFactors = FALSE)
      }
    },
    #' @description Deserialize from software data frame
    #' @param SoftwareDataFrame Software data frame
    fromDataFrame = function(SoftwareDataFrame) {
      stopifnot(nrow(SoftwareDataFrame)==1)
      columnNames <- colnames(SoftwareDataFrame)
      if (rlang::has_name(SoftwareDataFrame, "id")) {
        self$`id` <- as.numeric(SoftwareDataFrame$`id`)
      }
      if (rlang::has_name(SoftwareDataFrame, "parameter")) {
        param <- Parameter$new()
        self$`parameter` <- param$fromString(NULL, SoftwareDataFrame$`parameter`)
      }
      # extract potentially multiple columns with 'custom' prefix
      customColumns <- columnNames[grepl("^setting", columnNames)]
      if (length(customColumns) > 0) {
        self$`setting` <- lapply(customColumns, function(x) {
          SoftwareDataFrame[[x]]
        })
      }
      if (rlang::has_name(SoftwareDataFrame, "setting")) { # list of references
        assayRefList <- splitList(SoftwareDataFrame$`setting`)
        self$`assay_refs` <- lapply(assayRefList, function(x) {
          extractId(x)  
        })
      }
      self
    }
  )
)
lifs-tools/rmzTab-m documentation built on Jan. 26, 2023, 4:45 p.m.