R/mz_tab.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 MzTab
#' @description MzTab Class
#' @format An \code{R6Class} generator object
#' @field metadata  \link{Metadata} 
#'
#' @field smallMoleculeSummary  list( \link{SmallMoleculeSummary} ) 
#'
#' @field smallMoleculeFeature  list( \link{SmallMoleculeFeature} ) 
#'
#' @field smallMoleculeEvidence  list( \link{SmallMoleculeEvidence} ) 
#'
#' @field comment  list( \link{Comment} ) [optional]
#'
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @importFrom dplyr %>%
#' @export
MzTab <- R6::R6Class(
  'MzTab',
  public = list(
    `metadata` = NULL,
    `smallMoleculeSummary` = NULL,
    `smallMoleculeFeature` = NULL,
    `smallMoleculeEvidence` = NULL,
    `comment` = NULL,
    #' @description Create an MzTab object.
    #' @param metadata The metadata \link{Metadata}.
    #' @param smallMoleculeSummary The list( \link{SmallMoleculeSummary} ).
    #' @param smallMoleculeFeature The list( \link{SmallMoleculeFeature} ).
    #' @param smallMoleculeEvidence The list( \link{SmallMoleculeEvidence} ).
    #' @param comment The list( \link{Comment} ).
    #' @param ... local optional variable arguments
    #'     
    initialize = function(`metadata`, `smallMoleculeSummary`, `smallMoleculeFeature`, `smallMoleculeEvidence`, `comment`=NULL, ...){
      local.optional.var <- list(...)
      if (!missing(`metadata`)) {
        stopifnot(R6::is.R6(`metadata`))
        self$`metadata` <- `metadata`
      }
      if (!missing(`smallMoleculeSummary`)) {
        stopifnot(is.vector(`smallMoleculeSummary`), length(`smallMoleculeSummary`) != 0)
        sapply(`smallMoleculeSummary`, function(x) stopifnot(R6::is.R6(x)))
        self$`smallMoleculeSummary` <- `smallMoleculeSummary`
      }
      if (!missing(`smallMoleculeFeature`)) {
        stopifnot(is.vector(`smallMoleculeFeature`), length(`smallMoleculeFeature`) != 0)
        sapply(`smallMoleculeFeature`, function(x) stopifnot(R6::is.R6(x)))
        self$`smallMoleculeFeature` <- `smallMoleculeFeature`
      }
      if (!missing(`smallMoleculeEvidence`)) {
        stopifnot(is.vector(`smallMoleculeEvidence`), length(`smallMoleculeEvidence`) != 0)
        sapply(`smallMoleculeEvidence`, function(x) stopifnot(R6::is.R6(x)))
        self$`smallMoleculeEvidence` <- `smallMoleculeEvidence`
      }
      if (!is.null(`comment`)) {
        stopifnot(is.vector(`comment`), length(`comment`) != 0)
        sapply(`comment`, function(x) stopifnot(R6::is.R6(x)))
        self$`comment` <- `comment`
      }
    },
    #' @description Serialize to list object suitable for jsonlite
    toJSON = function() {
      MzTabObject <- list()
      if (!is.null(self$`metadata`)) {
        MzTabObject[['metadata']] <-
          self$`metadata`$toJSON()
      }
      if (!is.null(self$`smallMoleculeSummary`)) {
        MzTabObject[['smallMoleculeSummary']] <-
          lapply(self$`smallMoleculeSummary`, function(x) x$toJSON())
      }
      if (!is.null(self$`smallMoleculeFeature`)) {
        MzTabObject[['smallMoleculeFeature']] <-
          lapply(self$`smallMoleculeFeature`, function(x) x$toJSON())
      }
      if (!is.null(self$`smallMoleculeEvidence`)) {
        MzTabObject[['smallMoleculeEvidence']] <-
          lapply(self$`smallMoleculeEvidence`, function(x) x$toJSON())
      }
      if (!is.null(self$`comment`)) {
        MzTabObject[['comment']] <-
          lapply(self$`comment`, function(x) x$toJSON())
      }

      MzTabObject
    },
    #' @description Deserialize from jsonlite list object
    #' @param MzTabJson list object.
    fromJSON = function(MzTabJson) {
      MzTabObject <- jsonlite::fromJSON(MzTabJson)
      if (!is.null(MzTabObject$`metadata`)) {
        metadataObject <- Metadata$new()
        metadataObject$fromJSON(jsonlite::toJSON(MzTabObject$metadata, auto_unbox = TRUE, null = "null", na = "null", digits = NA))
        self$`metadata` <- metadataObject
      }
      if (!is.null(MzTabObject$`smallMoleculeSummary`)) {
        self$`smallMoleculeSummary` <- ApiClient$new()$deserializeObj(MzTabObject$`smallMoleculeSummary`, "array[SmallMoleculeSummary]", loadNamespace("rmzTabM"))
      }
      if (!is.null(MzTabObject$`smallMoleculeFeature`)) {
        self$`smallMoleculeFeature` <- ApiClient$new()$deserializeObj(MzTabObject$`smallMoleculeFeature`, "array[SmallMoleculeFeature]", loadNamespace("rmzTabM"))
      }
      if (!is.null(MzTabObject$`smallMoleculeEvidence`)) {
        self$`smallMoleculeEvidence` <- ApiClient$new()$deserializeObj(MzTabObject$`smallMoleculeEvidence`, "array[SmallMoleculeEvidence]", loadNamespace("rmzTabM"))
      }
      if (!is.null(MzTabObject$`comment`)) {
        self$`comment` <- ApiClient$new()$deserializeObj(MzTabObject$`comment`, "array[Comment]", loadNamespace("rmzTabM"))
      }
    },
    #' @description Serialize to JSON string.
    toJSONString = function() {
      jsoncontent <- c(
        if (!is.null(self$`metadata`)) {
        sprintf(
        '"metadata":
        %s
        ',
        jsonlite::toJSON(self$`metadata`$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)
        )},
        if (!is.null(self$`smallMoleculeSummary`)) {
        sprintf(
        '"smallMoleculeSummary":
        [%s]
',
        paste(sapply(self$`smallMoleculeSummary`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)), collapse=",")
        )},
        if (!is.null(self$`smallMoleculeFeature`)) {
        sprintf(
        '"smallMoleculeFeature":
        [%s]
',
        paste(sapply(self$`smallMoleculeFeature`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)), collapse=",")
        )},
        if (!is.null(self$`smallMoleculeEvidence`)) {
        sprintf(
        '"smallMoleculeEvidence":
        [%s]
',
        paste(sapply(self$`smallMoleculeEvidence`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)), collapse=",")
        )},
        if (!is.null(self$`comment`)) {
        sprintf(
        '"comment":
        [%s]
',
        paste(sapply(self$`comment`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=FALSE, null = "null", na = "null", digits = NA)), collapse=",")
        )}
      )
      jsoncontent <- paste(jsoncontent, collapse = ",")
      paste('{', jsoncontent, '}', sep = "")
    },
    #' @description Deserialize from JSON string
    #' @param MzTabJson JSON string
    fromJSONString = function(MzTabJson) {
      MzTabObject <- jsonlite::fromJSON(MzTabJson)
      self$`metadata` <- Metadata$new()$fromJSONString(jsonlite::toJSON(MzTabObject$metadata, auto_unbox = TRUE, null = "null", na = "null", digits = NA))
      self$`smallMoleculeSummary` <- ApiClient$new()$deserializeObj(MzTabObject$`smallMoleculeSummary`, "array[SmallMoleculeSummary]", loadNamespace("rmzTabM"))
      self$`smallMoleculeFeature` <- ApiClient$new()$deserializeObj(MzTabObject$`smallMoleculeFeature`, "array[SmallMoleculeFeature]", loadNamespace("rmzTabM"))
      self$`smallMoleculeEvidence` <- ApiClient$new()$deserializeObj(MzTabObject$`smallMoleculeEvidence`, "array[SmallMoleculeEvidence]", loadNamespace("rmzTabM"))
      self$`comment` <- ApiClient$new()$deserializeObj(MzTabObject$`comment`, "array[Comment]", loadNamespace("rmzTabM"))
      self
    },
    #' @description Serialize to data frame
    toDataFrame = function() {
      # create individual tables
      metadataTable <- self$`metadata`$toDataFrame()
      smlTable <- lapply(self$`smallMoleculeSummary`, function(x) {x$toDataFrame()}) %>% dplyr::bind_rows()
      smfTable <- lapply(self$`smallMoleculeFeature`, function(x) {x$toDataFrame()}) %>% dplyr::bind_rows()
      smeTable <- lapply(self$`smallMoleculeEvidence`, function(x) {x$toDataFrame()}) %>% dplyr::bind_rows()
      
      # bind ragged to pad with missing columns
      df <- metadataTable %>% 
        rbind.ragged(., t(colnames(smlTable))) %>% 
        rbind.ragged(., smlTable)
      if(nrow(smfTable)>0) {
        df <- df %>% 
        rbind.ragged(., t(colnames(smfTable))) %>% 
        rbind.ragged(., smfTable)
      }
      if(nrow(smeTable)>0) {
       df <- df %>% 
        rbind.ragged(., t(colnames(smeTable))) %>% 
        rbind.ragged(., smeTable)
      }
      df
    },
    #' @description Deserialize from MzTab data frame
    #' @param MzTabDataFrame MzTab data frame
    fromDataFrame = function(MzTabDataFrame) {
      # self <- MzTab$new()
      self$`metadata` <- Metadata$new()$fromDataFrame(extractMetadata(MzTabDataFrame))
      # summary
      smlDataFrame <- extractSmallMoleculeSummary(MzTabDataFrame)
      if (!is.null(smlDataFrame)) {
        sml.list <- split(smlDataFrame, seq(nrow(smlDataFrame)))
        self$`smallMoleculeSummary` <- lapply(sml.list, FUN=function(x) {SmallMoleculeSummary$new()$fromDataFrame(x)})
      }
      # features
      smfDataFrame <- extractSmallMoleculeFeatures(MzTabDataFrame)
      if (!is.null(smfDataFrame)) {
        smf.list <- split(smfDataFrame, seq(nrow(smfDataFrame)))
        self$`smallMoleculeFeature` <- lapply(smf.list, FUN=function(x) {SmallMoleculeFeature$new()$fromDataFrame(x)})
      }
      # evidence
      smeDataFrame <- extractSmallMoleculeEvidence(MzTabDataFrame)
      if (!is.null(smeDataFrame)) {
        sme.list <- split(smeDataFrame, seq(nrow(smeDataFrame)))
        self$`smallMoleculeEvidence` <- lapply(sme.list, FUN=function(x) {SmallMoleculeEvidence$new()$fromDataFrame(x)})
    }
      # comments, if any exist
      commentsDataFrame <- extractComments(MzTabDataFrame)
      if (nrow(commentsDataFrame>0)) {
        comments.list <- split(commentsDataFrame, seq(nrow(commentsDataFrame)))
        self$`comment` <- lapply(comments.list, FUN=function(x) {Comment$new()$fromDataFrame(x)})
      }
      self
    }
  )
)
lifs-tools/rmzTab-m documentation built on Jan. 26, 2023, 4:45 p.m.