R/ms_run.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 MsRun
#' @description MsRun Class
#' @format An \code{R6Class} generator object
#' @field id  integer 
#'
#' @field name  character 
#'
#' @field location  character 
#'
#' @field instrument_ref  \link{Instrument} [optional]
#'
#' @field format  \link{Parameter} [optional]
#'
#' @field id_format  \link{Parameter} [optional]
#'
#' @field fragmentation_method  list( \link{Parameter} ) [optional]
#'
#' @field scan_polarity  list( \link{Parameter} ) [optional]
#'
#' @field hash  character [optional]
#'
#' @field hash_method  \link{Parameter} [optional]
#'
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
MsRun <- R6::R6Class(
  'MsRun',
  public = list(
    `id` = NULL,
    `name` = NULL,
    `location` = NULL,
    `instrument_ref` = NULL,
    `format` = NULL,
    `id_format` = NULL,
    `fragmentation_method` = NULL,
    `scan_polarity` = NULL,
    `hash` = NULL,
    `hash_method` = NULL,
    #' @description Create an MsRun
    #' @param id MsRun id.
    #' @param name MsRun name.
    #' @param location the location URI to retrieve the MsRun file.
    #' @param instrument_ref Link to the \link{Instrument} used to acquire this MsRun.
    #' @param format CV parameter defining the file format \link{Parameter}.
    #' @param id_format CV parameter defining the mass spec id format \link{Parameter}.
    #' @param fragmentation_method Fragementation methods list( \link{Parameter} ).
    #' @param scan_polarity Scan polarities present in the file list( \link{Parameter} ).
    #' @param hash Hash value of the file using the hash_method.
    #' @param hash_method \link{Parameter}
    #' @param ... local optional variable arguments
    #' 
    initialize = function(`id`, `name`, `location`, `instrument_ref`=NULL, `format`=NULL, `id_format`=NULL, `fragmentation_method`=NULL, `scan_polarity`=NULL, `hash`=NULL, `hash_method`=NULL, ...){
      local.optional.var <- list(...)
      if (!missing(`id`)) {
        stopifnot(is.numeric(`id`), length(`id`) == 1)
        self$`id` <- `id`
      }
      if (!missing(`name`)) {
        stopifnot(is.character(`name`), length(`name`) == 1)
        self$`name` <- `name`
      }
      if (!missing(`location`)) {
        stopifnot(is.character(`location`), length(`location`) == 1)
        self$`location` <- `location`
      }
      if (!is.null(`instrument_ref`)) {
        stopifnot(is.numeric(`instrument_ref`), length(`instrument_ref`) == 1)
        self$`instrument_ref` <- `instrument_ref`
      }
      if (!is.null(`format`)) {
        stopifnot(R6::is.R6(`format`))
        self$`format` <- `format`
      }
      if (!is.null(`id_format`)) {
        stopifnot(R6::is.R6(`id_format`))
        self$`id_format` <- `id_format`
      }
      if (!is.null(`fragmentation_method`)) {
        stopifnot(is.vector(`fragmentation_method`), length(`fragmentation_method`) != 0)
        sapply(`fragmentation_method`, function(x) stopifnot(R6::is.R6(x)))
        self$`fragmentation_method` <- `fragmentation_method`
      }
      if (!is.null(`scan_polarity`)) {
        stopifnot(is.vector(`scan_polarity`), length(`scan_polarity`) != 0)
        sapply(`scan_polarity`, function(x) stopifnot(R6::is.R6(x)))
        self$`scan_polarity` <- `scan_polarity`
      }
      if (!is.null(`hash`)) {
        stopifnot(is.character(`hash`), length(`hash`) == 1)
        self$`hash` <- `hash`
      }
      if (!is.null(`hash_method`)) {
        stopifnot(R6::is.R6(`hash_method`))
        self$`hash_method` <- `hash_method`
      }
    },
    #' @description Serialize to list object suitable for jsonlite
    toJSON = function() {
      MsRunObject <- list()
      if (!is.null(self$`id`)) {
        MsRunObject[['id']] <-
          rmzTabM::safe_unbox(self$`id`)
      }
      if (!is.null(self$`name`)) {
        MsRunObject[['name']] <-
          rmzTabM::safe_unbox(self$`name`)
      }
      if (!is.null(self$`location`)) {
        MsRunObject[['location']] <-
          rmzTabM::safe_unbox(self$`location`)
      }
      if (!is.null(self$`instrument_ref`)) {
        MsRunObject[['instrument_ref']] <-
          rmzTabM::safe_unbox(self$`instrument_ref`)
      }
      if (!is.null(self$`format`)) {
        MsRunObject[['format']] <-
          self$`format`$toJSON()
      }
      if (!is.null(self$`id_format`)) {
        MsRunObject[['id_format']] <-
          self$`id_format`$toJSON()
      }
      if (!is.null(self$`fragmentation_method`)) {
        MsRunObject[['fragmentation_method']] <-
          lapply(self$`fragmentation_method`, function(x) x$toJSON())
      }
      if (!is.null(self$`scan_polarity`)) {
        MsRunObject[['scan_polarity']] <-
          lapply(self$`scan_polarity`, function(x) x$toJSON())
      }
      if (!is.null(self$`hash`)) {
        MsRunObject[['hash']] <-
          rmzTabM::safe_unbox(self$`hash`)
      }
      if (!is.null(self$`hash_method`)) {
        MsRunObject[['hash_method']] <-
          self$`hash_method`$toJSON()
      }

      MsRunObject
    },
    #' @description Deserialize from jsonlite list object
    #' @param MsRunJson list object.
    fromJSON = function(MsRunJson) {
      MsRunObject <- jsonlite::fromJSON(MsRunJson)
      if (!is.null(MsRunObject$`id`)) {
        self$`id` <- MsRunObject$`id`
      }
      if (!is.null(MsRunObject$`name`)) {
        self$`name` <- MsRunObject$`name`
      }
      if (!is.null(MsRunObject$`location`)) {
        self$`location` <- MsRunObject$`location`
      }
      if (!is.null(MsRunObject$`instrument_ref`)) {
        self$`instrument_ref` <- MsRunObject$`instrument_ref`
      }
      if (!is.null(MsRunObject$`format`)) {
        formatObject <- Parameter$new()
        formatObject$fromJSON(jsonlite::toJSON(MsRunObject$format, auto_unbox = TRUE, null = "null", na = "null", digits = NA))
        self$`format` <- formatObject
      }
      if (!is.null(MsRunObject$`id_format`)) {
        id_formatObject <- Parameter$new()
        id_formatObject$fromJSON(jsonlite::toJSON(MsRunObject$id_format, auto_unbox = TRUE, null = "null", na = "null", digits = NA))
        self$`id_format` <- id_formatObject
      }
      if (!is.null(MsRunObject$`fragmentation_method`)) {
        self$`fragmentation_method` <- ApiClient$new()$deserializeObj(MsRunObject$`fragmentation_method`, "array[Parameter]", loadNamespace("rmzTabM"))
      }
      if (!is.null(MsRunObject$`scan_polarity`)) {
        self$`scan_polarity` <- ApiClient$new()$deserializeObj(MsRunObject$`scan_polarity`, "array[Parameter]", loadNamespace("rmzTabM"))
      }
      if (!is.null(MsRunObject$`hash`)) {
        self$`hash` <- MsRunObject$`hash`
      }
      if (!is.null(MsRunObject$`hash_method`)) {
        hash_methodObject <- Parameter$new()
        hash_methodObject$fromJSON(jsonlite::toJSON(MsRunObject$hash_method, auto_unbox = TRUE, null = "null", na = "null", digits = NA))
        self$`hash_method` <- hash_methodObject
      }
    },
    #' @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$`location`)) {
        sprintf(
        '"location":
          "%s"
                ',
        rmzTabM::safe_unbox(self$`location`)
        )},
        if (!is.null(self$`instrument_ref`)) {
        sprintf(
        '"instrument_ref":
        %s
        ',
        rmzTabM::safe_unbox(self$`instrument_ref`)
        )},
        if (!is.null(self$`format`)) {
        sprintf(
        '"format":
        %s
        ',
        jsonlite::toJSON(self$`format`$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)
        )},
        if (!is.null(self$`id_format`)) {
        sprintf(
        '"id_format":
        %s
        ',
        jsonlite::toJSON(self$`id_format`$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)
        )},
        if (!is.null(self$`fragmentation_method`)) {
        sprintf(
        '"fragmentation_method":
        %s
',
        paste(sapply(self$`fragmentation_method`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)), collapse=",")
        )},
        if (!is.null(self$`scan_polarity`)) {
        sprintf(
        '"scan_polarity":
        %s
',
        paste(sapply(self$`scan_polarity`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)), collapse=",")
        )},
        if (!is.null(self$`hash`)) {
        sprintf(
        '"hash":
          "%s"
                ',
        rmzTabM::safe_unbox(self$`hash`)
        )},
        if (!is.null(self$`hash_method`)) {
        sprintf(
        '"hash_method":
        %s
        ',
        jsonlite::toJSON(self$`hash_method`$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)
        )}
      )
      jsoncontent <- paste(jsoncontent, collapse = ",")
      paste('{', jsoncontent, '}', sep = "")
    },
    #' @description Deserialize from JSON string
    #' @param MsRunJson JSON string
    fromJSONString = function(MsRunJson) {
      MsRunObject <- jsonlite::fromJSON(MsRunJson)
      self$`id` <- MsRunObject$`id`
      self$`name` <- MsRunObject$`name`
      self$`location` <- MsRunObject$`location`
      self$`instrument_ref` <- MsRunObject$`instrument_ref`
      self$`format` <- Parameter$new()$fromJSONString(jsonlite::toJSON(MsRunObject$format, auto_unbox = TRUE, null = "null", na = "null", digits = NA))
      self$`id_format` <- Parameter$new()$fromJSONString(jsonlite::toJSON(MsRunObject$id_format, auto_unbox = TRUE, null = "null", na = "null", digits = NA))
      self$`fragmentation_method` <- ApiClient$new()$deserializeObj(MsRunObject$`fragmentation_method`, "array[Parameter]", loadNamespace("rmzTabM"))
      self$`scan_polarity` <- ApiClient$new()$deserializeObj(MsRunObject$`scan_polarity`, "array[Parameter]", loadNamespace("rmzTabM"))
      self$`hash` <- MsRunObject$`hash`
      self$`hash_method` <- Parameter$new()$fromJSONString(jsonlite::toJSON(MsRunObject$hash_method, auto_unbox = TRUE, null = "null", na = "null", digits = NA))
      self
    },
    #' @description Serialize to data frame
    toDataFrame = function() {
      idPrefix <- paste0("ms_run[", self$`id`, "]")
      elements <- data.frame(PREFIX=character(), KEY=character(), VALUE=character())
      if (!is.null(self$`name`)) {
        elements <-
          rbind(elements,
                list(
                  PREFIX = "MTD",
                  KEY = paste(idPrefix, "name", sep = "-"),
                  VALUE = self$`name`
                ),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`location`)) {
        elements <-
          rbind(elements,
                list(
                  PREFIX = "MTD",
                  KEY = paste(idPrefix, "location", sep="-"),
                  VALUE = self$`location`
                ),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`instrument_ref`)) {
        elements <-
          rbind(elements,
                list(
                  PREFIX = "MTD",
                  KEY = paste(idPrefix, "instrument_ref", sep = "-"),
                  VALUE = paste0("instrument[", self$`instrument_ref`, "]")
                ),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`format`)) {
        elements <-
          rbind(elements,
                list(
                  PREFIX = "MTD",
                  KEY = paste(idPrefix, "format", sep = "-"),
                  VALUE = self$`format`$toString()
                ),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`id_format`)) {
        elements <-
          rbind(elements,
                list(
                  PREFIX = "MTD",
                  KEY = paste(idPrefix, "id_format", sep = "-"),
                  VALUE = self$`id_format`$toString()
                ),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`fragmentation_method`)) {
        elements <-
          rbind(elements,
                lapply(seq_along(self$`fragmentation_method`), function(idx, elements, idPrefix) {
                  list(PREFIX = "MTD", KEY=paste(idPrefix, paste0("fragmentation_method[", idx, "]"), sep="-"), VALUE=elements[[idx]]$toString())
                }, elements=self$`fragmentation_method`, idPrefix=idPrefix) %>% dplyr::bind_rows(),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`scan_polarity`)) {
        elements <-
          rbind(elements,
                lapply(seq_along(self$`scan_polarity`), function(idx, elements, idPrefix) {
                  list(PREFIX = "MTD", KEY=paste(idPrefix, paste0("scan_polarity[", idx, "]"), sep="-"), VALUE=elements[[idx]]$toString())
                }, elements=self$`scan_polarity`, idPrefix=idPrefix) %>% dplyr::bind_rows(),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`hash`)) {
        elements <-
          rbind(elements,
                list(
                  PREFIX = "MTD",
                  KEY = paste(idPrefix, "hash", sep = "-"),
                  VALUE = self$`hash`
                ),
                stringsAsFactors = FALSE)
      }
      if (!is.null(self$`hash_method`)) {
        elements <-
          rbind(elements,
                list(
                  PREFIX = "MTD",
                  KEY = paste(idPrefix, "hash_method", sep = "-"),
                  VALUE = self$`hash_method`$toString()
                ),
                stringsAsFactors = FALSE)
      }
      elements
    },
    #' @description Deserialize from ms run data frame
    #' @param MsRunDataFrame MsRun data frame    
    fromDataFrame = function(MsRunDataFrame) {
      stopifnot(nrow(MsRunDataFrame)==1)
      columnNames <- colnames(MsRunDataFrame)
      if (rlang::has_name(MsRunDataFrame, "id")) {
        self$`id` <- as.numeric(MsRunDataFrame$`id`)
      }
      if (rlang::has_name(MsRunDataFrame, "name")) {
        self$`name` <- MsRunDataFrame$`name`
      }
      if (rlang::has_name(MsRunDataFrame, "location")) {
        self$`location` <- MsRunDataFrame$`location`
      }
      if (rlang::has_name(MsRunDataFrame, "instrument_ref")) { # single reference
        self$`instrument_ref` <- extractId(MsRunDataFrame$`instrument_ref`)
      }
      if (rlang::has_name(MsRunDataFrame, "format")) {
        param <- Parameter$new()
        self$`format` <- param$fromString(NULL, MsRunDataFrame$`format`)
      }
      if (rlang::has_name(MsRunDataFrame, "id_format")) {
        param <- Parameter$new()
        self$`id_format` <- param$fromString(NULL, MsRunDataFrame$`id_format`)
      }
      # extract potentially multiple columns with 'fragmentation_method' prefix
      fragmentationMethodColumns <- columnNames[grepl("^fragmentation\\_method", columnNames)]
      if (length(fragmentationMethodColumns) > 0) {
        self$`fragmentation_method` <- lapply(fragmentationMethodColumns, function(x) {
          param <- Parameter$new()
          param$fromString(NULL, MsRunDataFrame[[x]])
          param
        })
      }
      # extract potentially multiple columns with 'scan_polarity' prefix
      scanPolarityColumns <- columnNames[grepl("^scan\\_polarity", columnNames)]
      if (length(scanPolarityColumns) > 0) {
        self$`scan_polarity` <- lapply(scanPolarityColumns, function(x) {
          param <- Parameter$new()
          param$fromString(NULL, MsRunDataFrame[[x]])
          param
        })
      }
      if (rlang::has_name(MsRunDataFrame, "hash")) {
        self$`hash` <- MsRunDataFrame$`hash`
      }
      if (rlang::has_name(MsRunDataFrame, "hash_method")) {
        param <- Parameter$new()
        self$`hash_method` <- param$fromString(NULL, MsRunDataFrame$`hash_method`)
      }
      self
    }
  )
)
lifs-tools/rmzTab-m documentation built on Jan. 26, 2023, 4:45 p.m.