Skip to contents

Develop new function based on mass_dataset class

If you want to develop new functions based on the mass_dataset class in your own package, add massdataset as a package dependency.

DESCRIPTION

In DESCRIPTION, list massdataset in Imports.

Imports:
    massdataset

R code

Please set “mass_dataset” class as your new function primary input data. If the function is directory to process one component of “mass_dataset”, just get the component and then process it.

library(massdataset)
data("expression_data")
data("sample_info")
data("sample_info_note")
data("variable_info")
data("variable_info_note")
object =
  create_mass_dataset(
    expression_data = expression_data,
    sample_info = sample_info,
    variable_info = variable_info,
    sample_info_note = sample_info_note,
    variable_info_note = variable_info_note
  )
object
#> -------------------- 
#> massdataset version: 0.99.1 
#> -------------------- 
#> 1.expression_data:[ 1000 x 8 data.frame]
#> 2.sample_info:[ 8 x 4 data.frame]
#> 8 samples:Blank_3 Blank_4 QC_1 ... PS4P3 PS4P4
#> 3.variable_info:[ 1000 x 3 data.frame]
#> 1000 variables:M136T55_2_POS M79T35_POS M307T548_POS ... M232T937_POS M301T277_POS
#> 4.sample_info_note:[ 4 x 2 data.frame]
#> 5.variable_info_note:[ 3 x 2 data.frame]
#> 6.ms2_data:[ 0 variables x 0 MS2 spectra]
#> -------------------- 
#> Processing information
#> 1 processings in total
#> create_mass_dataset ---------- 
#>       Package         Function.used                Time
#> 1 massdataset create_mass_dataset() 2026-03-02 09:27:39

For example, we can create a function named get_class_test() to get “class” from “sample_info”.

get_class_test <- function(object){
  object@sample_info$class
}
get_class_test(object)
#> [1] "Blank"   "Blank"   "QC"      "QC"      "Subject" "Subject" "Subject"
#> [8] "Subject"

If the function is general, you can use the activate_mass_dataset() to tell you function which component you want to preocess.

For example, we can create a function named remove_blank() from “sample_info” or “expression_data”.

remove_blank <- function(object) {
  if (length(object@activated) == 0) {
    stop("activate_mass_dataset() first.\n")
  }
  
  if (object@activated == "sample_info") {
    sample_info <-
      object@sample_info
    sample_info <-
      sample_info %>%
      dplyr::filter(class != "Blank")
    object@sample_info <-
      sample_info
    object <-
      update_mass_dataset(object)
    return(object)
  }
  
  if (object@activated == "expression_data") {
    expression_data <-
      object@expression_data
    expression_data <-
      expression_data %>%
      dplyr::select(-dplyr::contains("Blank"))
    object@expression_data <-
      expression_data
    object <-
      update_mass_dataset(object)
    return(object)
  }
  
    if (object@activated != "expression_data" & object@activated != "sample_info") {
      stop("Only support sample_info or expression_data.\n")
  }
}
object %>% 
  remove_blank()
object %>% 
  activate_mass_dataset(what = "sample_info") %>% 
  remove_blank()
#> -------------------- 
#> massdataset version: 0.99.1 
#> -------------------- 
#> 1.expression_data:[ 1000 x 6 data.frame]
#> 2.sample_info:[ 6 x 4 data.frame]
#> 6 samples:QC_1 QC_2 PS4P1 ... PS4P3 PS4P4
#> 3.variable_info:[ 1000 x 3 data.frame]
#> 1000 variables:M136T55_2_POS M79T35_POS M307T548_POS ... M232T937_POS M301T277_POS
#> 4.sample_info_note:[ 4 x 2 data.frame]
#> 5.variable_info_note:[ 3 x 2 data.frame]
#> 6.ms2_data:[ 0 variables x 0 MS2 spectra]
#> -------------------- 
#> Processing information
#> 2 processings in total
#> create_mass_dataset ---------- 
#>       Package         Function.used                Time
#> 1 massdataset create_mass_dataset() 2026-03-02 09:27:39
#> update_mass_dataset ---------- 
#>       Package         Function.used                Time
#> 1 massdataset update_mass_dataset() 2026-03-02 09:27:39
object %>% 
  activate_mass_dataset(what = "expression_data") %>% 
  remove_blank()
#> -------------------- 
#> massdataset version: 0.99.1 
#> -------------------- 
#> 1.expression_data:[ 1000 x 6 data.frame]
#> 2.sample_info:[ 6 x 4 data.frame]
#> 6 samples:QC_1 QC_2 PS4P1 ... PS4P3 PS4P4
#> 3.variable_info:[ 1000 x 3 data.frame]
#> 1000 variables:M136T55_2_POS M79T35_POS M307T548_POS ... M232T937_POS M301T277_POS
#> 4.sample_info_note:[ 4 x 2 data.frame]
#> 5.variable_info_note:[ 3 x 2 data.frame]
#> 6.ms2_data:[ 0 variables x 0 MS2 spectra]
#> -------------------- 
#> Processing information
#> 2 processings in total
#> create_mass_dataset ---------- 
#>       Package         Function.used                Time
#> 1 massdataset create_mass_dataset() 2026-03-02 09:27:39
#> update_mass_dataset ---------- 
#>       Package         Function.used                Time
#> 1 massdataset update_mass_dataset() 2026-03-02 09:27:39
object %>% 
  activate_mass_dataset(what = "variable_info") %>% 
  remove_blank()

Make functions from other package support mass_dataset

If you want to make other functions from other package support “mass_dataset”. We have two examples here:

#' @title head
#' @method head mass_dataset
#' @param x x
#' @export
#' @rdname processing-mass_dataset
#' @return mass_dataset class object

head.mass_dataset = function(x, ...){
  x@expression_data = head(x@expression_data, ...)
  x = update_mass_dataset(x)
  return(x)
}
#' @title apply
#' @method apply mass_dataset
#' @param X X
#' @param MARGIN MARGIN
#' @param FUN FUN
#' @param ... ...
#' @param simplify simplify
#' @export
#' @rdname summary-mass_dataset
#' @return result

setMethod(f = "apply",
          signature(X = "mass_dataset"),
          function (X, MARGIN, FUN, ..., simplify = TRUE) {
            apply(as.matrix(X@expression_data),
                  MARGIN, FUN, ..., simplify = simplify)
          })

Session information

sessionInfo()
#> R version 4.5.2 (2025-10-31)
#> Platform: aarch64-apple-darwin20
#> Running under: macOS Tahoe 26.3
#> 
#> Matrix products: default
#> BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1
#> 
#> locale:
#> [1] C.UTF-8/C.UTF-8/C.UTF-8/C/C.UTF-8/C.UTF-8
#> 
#> time zone: Asia/Singapore
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] magrittr_2.0.3     dplyr_1.1.4        ggplot2_4.0.2      massdataset_0.99.1
#> 
#> loaded via a namespace (and not attached):
#>  [1] tidyselect_1.2.1            farver_2.1.2               
#>  [3] S7_0.2.0                    fastmap_1.2.0              
#>  [5] digest_0.6.37               lifecycle_1.0.4            
#>  [7] cluster_2.1.8.1             compiler_4.5.2             
#>  [9] rlang_1.1.6                 sass_0.4.10                
#> [11] tools_4.5.2                 yaml_2.3.10                
#> [13] knitr_1.50                  S4Arrays_1.8.1             
#> [15] htmlwidgets_1.6.4           DelayedArray_0.34.1        
#> [17] RColorBrewer_1.1-3          abind_1.4-8                
#> [19] withr_3.0.2                 purrr_1.1.0                
#> [21] BiocGenerics_0.54.0         desc_1.4.3                 
#> [23] grid_4.5.2                  stats4_4.5.2               
#> [25] colorspace_2.1-1            scales_1.4.0               
#> [27] iterators_1.0.14            dichromat_2.0-0.1          
#> [29] SummarizedExperiment_1.38.1 cli_3.6.5                  
#> [31] rmarkdown_2.29              crayon_1.5.3               
#> [33] ragg_1.4.0                  generics_0.1.4             
#> [35] rstudioapi_0.17.1           httr_1.4.7                 
#> [37] tzdb_0.5.0                  rjson_0.2.23               
#> [39] cachem_1.1.0                stringr_1.5.1              
#> [41] parallel_4.5.2              XVector_0.48.0             
#> [43] matrixStats_1.5.0           vctrs_0.6.5                
#> [45] Matrix_1.7-4                jsonlite_2.0.0             
#> [47] IRanges_2.42.0              hms_1.1.3                  
#> [49] GetoptLong_1.0.5            S4Vectors_0.48.0           
#> [51] clue_0.3-66                 systemfonts_1.2.3          
#> [53] foreach_1.5.2               tidyr_1.3.1                
#> [55] jquerylib_0.1.4             glue_1.8.0                 
#> [57] pkgdown_2.1.3               codetools_0.2-20           
#> [59] stringi_1.8.7               shape_1.4.6.1              
#> [61] gtable_0.3.6                GenomeInfoDb_1.44.2        
#> [63] GenomicRanges_1.60.0        UCSC.utils_1.4.0           
#> [65] ComplexHeatmap_2.24.1       tibble_3.3.0               
#> [67] pillar_1.11.0               htmltools_0.5.8.1          
#> [69] GenomeInfoDbData_1.2.14     circlize_0.4.16            
#> [71] R6_2.6.1                    textshaping_1.0.1          
#> [73] doParallel_1.0.17           evaluate_1.0.4             
#> [75] Biobase_2.68.0              lattice_0.22-7             
#> [77] readr_2.1.5                 png_0.1-8                  
#> [79] openxlsx_4.2.8              bslib_0.9.0                
#> [81] Rcpp_1.1.0                  zip_2.3.3                  
#> [83] SparseArray_1.8.1           xfun_0.53                  
#> [85] fs_1.6.6                    MatrixGenerics_1.20.0      
#> [87] pkgconfig_2.0.3             GlobalOptions_0.1.2