R/assay.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 Assay
#' @description Assay Class
#' @format An \code{R6Class} generator object
#' @field id  integer [optional]
#'
#' @field name  character 
#'
#' @field custom  list( \link{Parameter} ) [optional]
#'
#' @field external_uri  character [optional]
#'
#' @field sample_ref  \link{Sample} [optional]
#'
#' @field ms_run_ref  list( \link{MsRun} ) 
#'
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Assay <- R6::R6Class(
  'Assay',
  public = list(
    `id` = NULL,
    `name` = NULL,
    `custom` = NULL,
    `external_uri` = NULL,
    `sample_ref` = NULL,
    `ms_run_ref` = NULL,
    #' @description Create an Assay
    #' @param name Assay name.
    #' @param ms_run_ref Reference to \link{MsRun}s.
    #' @param id Assay id.
    #' @param custom Custom parameters.
    #' @param external_uri External URI.
    #' @param sample_ref Reference to \link{Sample}.
    #' @param ... local optional variable arguments
    #' 
    initialize = function(`name`, `ms_run_ref`, `id`=NULL, `custom`=NULL, `external_uri`=NULL, `sample_ref`=NULL, ...){
      local.optional.var <- list(...)
      if (!missing(`name`)) {
        stopifnot(is.character(`name`), length(`name`) == 1)
        self$`name` <- `name`
      }
      if (!missing(`ms_run_ref`)) {
        stopifnot(is.vector(`ms_run_ref`), length(`ms_run_ref`) != 0)
        sapply(`ms_run_ref`, function(x) stopifnot(is.character(x)))
        self$`ms_run_ref` <- `ms_run_ref`
      }
      if (!is.null(`id`)) {
        stopifnot(is.numeric(`id`), length(`id`) == 1)
        self$`id` <- `id`
      }
      if (!is.null(`custom`)) {
        stopifnot(is.vector(`custom`), length(`custom`) != 0)
        sapply(`custom`, function(x) stopifnot(R6::is.R6(x)))
        self$`custom` <- `custom`
      }
      if (!is.null(`external_uri`)) {
        stopifnot(is.character(`external_uri`), length(`external_uri`) == 1)
        self$`external_uri` <- `external_uri`
      }
      if (!is.null(`sample_ref`)) {
        stopifnot(is.numeric(`sample_ref`), length(`sample_ref`) == 1)
        self$`sample_ref` <- `sample_ref`
      }
    },
    #' @description Serialize to list object suitable for jsonlite
    toJSON = function() {
      AssayObject <- list()
      if (!is.null(self$`id`)) {
        AssayObject[['id']] <-
          rmzTabM::safe_unbox(self$`id`)
      }
      if (!is.null(self$`name`)) {
        AssayObject[['name']] <-
          rmzTabM::safe_unbox(self$`name`)
      }
      if (!is.null(self$`custom`)) {
        AssayObject[['custom']] <-
          rmzTabM::safe_unbox(lapply(self$`custom`, function(x) x$toJSON()))
      }
      if (!is.null(self$`external_uri`)) {
        AssayObject[['external_uri']] <-
          rmzTabM::safe_unbox(self$`external_uri`)
      }
      if (!is.null(self$`sample_ref`)) {
        AssayObject[['sample_ref']] <-
          rmzTabM::safe_unbox(self$`sample_ref`)
      }
      if (!is.null(self$`ms_run_ref`)) {
        AssayObject[['ms_run_ref']] <-
          rmzTabM::safe_unbox(self$`ms_run_ref`)
      }

      AssayObject
    },
    #' @description Deserialize from jsonlite list object
    #' @param AssayJson list object.
    fromJSON = function(AssayJson) {
      AssayObject <- jsonlite::fromJSON(AssayJson)
      if (!is.null(AssayObject$`id`)) {
        self$`id` <- AssayObject$`id`
      }
      if (!is.null(AssayObject$`name`)) {
        self$`name` <- AssayObject$`name`
      }
      if (!is.null(AssayObject$`custom`)) {
        self$`custom` <- ApiClient$new()$deserializeObj(AssayObject$`custom`, "array[Parameter]", loadNamespace("rmzTabM"))
      }
      if (!is.null(AssayObject$`external_uri`)) {
        self$`external_uri` <- AssayObject$`external_uri`
      }
      if (!is.null(AssayObject$`sample_ref`)) {
        self$`sample_ref` <- AssayObject$`sample_ref`
      }
      if (!is.null(AssayObject$`ms_run_ref`)) {
        self$`ms_run_ref` <- ApiClient$new()$deserializeObj(AssayObject$`ms_run_ref`, "array[integer]", 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$`name`)) {
        sprintf(
        '"name":
          "%s"
                ',
        rmzTabM::safe_unbox(self$`name`)
        )},
        if (!is.null(self$`custom`)) {
        sprintf(
        '"custom":
        %s
',
        paste(sapply(self$`custom`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)), collapse=",")
        )},
        if (!is.null(self$`external_uri`)) {
        sprintf(
        '"external_uri":
          "%s"
                ',
        self$`external_uri`
        )},
        if (!is.null(self$`sample_ref`)) {
        sprintf(
        '"sample_ref":
        %s
        ',
        paste(unlist(lapply(self$`sample_ref`, function(x) paste0(x))), collapse=",")
        )},
        if (!is.null(self$`ms_run_ref`)) {
        sprintf(
        '"ms_run_ref":
        %s
',
        paste(unlist(lapply(self$`ms_run_ref`, function(x) paste0(x))), collapse=",")
        )}
      )
      jsoncontent <- paste(jsoncontent, collapse = ",")
      paste('{', jsoncontent, '}', sep = "")
    },
    #' @description Deserialize from JSON string
    #' @param AssayJson JSON string
    fromJSONString = function(AssayJson) {
      AssayObject <- jsonlite::fromJSON(AssayJson)
      self$`id` <- AssayObject$`id`
      self$`name` <- AssayObject$`name`
      self$`custom` <- ApiClient$new()$deserializeObj(AssayObject$`custom`, "array[Parameter]", loadNamespace("rmzTabM"))
      self$`external_uri` <- AssayObject$`external_uri`
      self$`sample_ref` <- AssayObject$`sample_ref`
      self$`ms_run_ref` <- ApiClient$new()$deserializeObj(AssayObject$`ms_run_ref`, "array[integer]", loadNamespace("rmzTabM"))
      self
    },
    #' @description Serialize to data frame
    toDataFrame = function() {
      idPrefix <- paste0("assay[", self$`id`, "]")
      elements <- data.frame(PREFIX=character(), KEY=character(), VALUE=character())
      if (!is.null(self$`name`)) {
        elements <-
          rbind(elements,
                list(
                  PREFIX = "MTD",
                  KEY = idPrefix,
                  VALUE = self$`name`
                ),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`custom`)) { # list of parameters
        elements <-
          rbind(elements,
                lapply(seq_along(self$`custom`), function(idx, elements, idPrefix) {
                  list(PREFIX = "MTD", KEY=paste(idPrefix, paste0("custom[", idx, "]"), sep="-"), VALUE=elements[[idx]]$toString())
                }, elements=self$`custom`, idPrefix=idPrefix) %>% dplyr::bind_rows(),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`external_uri`)) {
        elements <-
          rbind(elements,
                list(
                  PREFIX = "MTD",
                  KEY = paste(idPrefix, "external_uri", sep="-"),
                  VALUE = self$`external_uri`
                ),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`sample_ref`)) {
        elements <-
          rbind(elements,
                list(
                  PREFIX = "MTD",
                  KEY = paste(idPrefix, "sample_ref", sep="-"),
                  VALUE = paste0("sample[", self$`sample_ref`, "]")
                ),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`ms_run_ref`)) { # list of references
        elements <-
          rbind(elements,
                lapply(seq_along(self$`ms_run_ref`), function(idx, elements, idPrefix) {
                  list(PREFIX = "MTD", KEY=paste(idPrefix, "ms_run_ref", sep="-"), VALUE=paste0("ms_run[", elements[[idx]], "]"))
                }, elements=self$`ms_run_ref`, idPrefix=idPrefix) %>% dplyr::bind_rows(),
                stringsAsFactors = FALSE)
      }
      elements
    },
    #' @description Deserialize from assay data frame
    #' @param AssayDataFrame Assay data frame
    fromDataFrame = function(AssayDataFrame) {
      stopifnot(nrow(AssayDataFrame)==1)
      columnNames <- colnames(AssayDataFrame)
      if (rlang::has_name(AssayDataFrame, "id")) {
        self$`id` <- as.numeric(AssayDataFrame$`id`)
      }
      if (rlang::has_name(AssayDataFrame, "name")) {
        self$`name` <- AssayDataFrame$`name`
      }
      # extract potentially multiple columns with 'custom' prefix
      customColumns <- columnNames[grepl("^custom", columnNames)]
      if (length(customColumns) > 0) {
        self$`custom` <- lapply(customColumns, function(x) {
          param <- Parameter$new()
          param$fromString(NULL, AssayDataFrame[[x]])
          param
        })
      }
      if (rlang::has_name(AssayDataFrame, "external_uri")) {
        self$`uri` <- AssayDataFrame$`external_uri`
      }
      if (rlang::has_name(AssayDataFrame, "sample_ref")) { # single reference
        self$`sample_ref` <- extractId(AssayDataFrame$`sample_ref`)
      }
      if (rlang::has_name(AssayDataFrame, "ms_run_ref")) { # list of references
        msRunRefList <- splitList(AssayDataFrame$`ms_run_ref`)
        self$`ms_run_ref` <- lapply(msRunRefList, function(x) {
          extractId(x)  
        })
      }
      self
    }
  )
)
lifs-tools/rmzTab-m documentation built on Jan. 26, 2023, 4:45 p.m.