For one mass_dataset class object, we can add some new information to mass_dataset class.

Data preparation

library(massdataset)
library(tidyverse)

data("expression_data")
data("sample_info")
data("variable_info")

object =
  create_mass_dataset(
    expression_data = expression_data,
    sample_info = sample_info,
    variable_info = variable_info
  )

Add new information to sample_info

Use mutate_xxx() functions

library(tidyverse)
###add NA frequency (%) in sample_info
object2 =
object %>% 
  mutate_sample_na_freq()

extract_sample_info(object2) %>% head()
#>   sample_id injection.order   class   group na_freq
#> 1   Blank_3               1   Blank   Blank   0.682
#> 2   Blank_4               2   Blank   Blank   0.702
#> 3      QC_1               3      QC      QC   0.397
#> 4      QC_2               4      QC      QC   0.381
#> 5     PS4P1               5 Subject Subject   0.424
#> 6     PS4P2               6 Subject Subject   0.427

###add NA number (%) in sample_info
object2 =
object %>% 
  mutate_sample_na_number()

extract_sample_info(object2) %>% head()
#>   sample_id injection.order   class   group na_number
#> 1   Blank_3               1   Blank   Blank       682
#> 2   Blank_4               2   Blank   Blank       702
#> 3      QC_1               3      QC      QC       397
#> 4      QC_2               4      QC      QC       381
#> 5     PS4P1               5 Subject Subject       424
#> 6     PS4P2               6 Subject Subject       427

If you want to add NA number/frequency based on specific variables, just provide the variable IDs.

###add NA number (%) in sample_info based on variables with mz > 200
variable_id = 
object %>% 
  activate_mass_dataset(what = "variable_info") %>% 
  filter(mz > 200) %>% 
  pull(variable_id)

object2 =
object %>% 
  mutate_sample_na_number(according_to_variables = variable_id)

extract_sample_info(object2) %>% head()
#>   sample_id injection.order   class   group na_number
#> 1   Blank_3               1   Blank   Blank       457
#> 2   Blank_4               2   Blank   Blank       474
#> 3      QC_1               3      QC      QC       248
#> 4      QC_2               4      QC      QC       236
#> 5     PS4P1               5 Subject Subject       240
#> 6     PS4P2               6 Subject Subject       241

Use mutate() from tidyverse

mass_dataset support tidyverse functions. So we can just use the mutate() to add new columns in sample_info.

###add a new column named as sample_id2 in sample_info
object2 =
object %>% 
  activate_mass_dataset(what = "sample_info") %>% 
  mutate(sample_id2 = sample_id)

extract_sample_info(object2) %>% head()
#>   sample_id injection.order   class   group sample_id2
#> 1   Blank_3               1   Blank   Blank    Blank_3
#> 2   Blank_4               2   Blank   Blank    Blank_4
#> 3      QC_1               3      QC      QC       QC_1
#> 4      QC_2               4      QC      QC       QC_2
#> 5     PS4P1               5 Subject Subject      PS4P1
#> 6     PS4P2               6 Subject Subject      PS4P2

Use left_join() from tidyverse


new_sample_info = 
  data.frame(sample_id = c("PS4P1", "PS4P2"), 
             BMI = c(20, 22))

object2 =
object %>% 
  activate_mass_dataset(what = "sample_info") %>% 
  left_join(new_sample_info, by = "sample_id")

extract_sample_info(object2) %>% head()
#>   sample_id injection.order   class   group BMI
#> 1   Blank_3               1   Blank   Blank  NA
#> 2   Blank_4               2   Blank   Blank  NA
#> 3      QC_1               3      QC      QC  NA
#> 4      QC_2               4      QC      QC  NA
#> 5     PS4P1               5 Subject Subject  20
#> 6     PS4P2               6 Subject Subject  22

Add new information to variable_info

Use mutate_xxx() functions

library(tidyverse)
###add mean intensity in variable_info
object2 =
object %>% 
  mutate_mean_intensity()

extract_variable_info(object2) %>% head()
#>                 variable_id        mz        rt mean_intensity
#> M136T55_2_POS M136T55_2_POS 136.06140  54.97902      1808605.7
#> M79T35_POS       M79T35_POS  79.05394  35.36550      2671173.4
#> M307T548_POS   M307T548_POS 307.14035 547.56641       276256.3
#> M183T224_POS   M183T224_POS 183.06209 224.32777      5103244.9
#> M349T47_POS     M349T47_POS 349.01584  47.00262      6789862.4
#> M182T828_POS   M182T828_POS 181.99775 828.35712      4326865.8

###add median intensity in variable_info
object2 =
object %>% 
  mutate_median_intensity()

extract_variable_info(object2) %>% head()
#>                 variable_id        mz        rt median_intensity
#> M136T55_2_POS M136T55_2_POS 136.06140  54.97902        1676180.4
#> M79T35_POS       M79T35_POS  79.05394  35.36550        2777897.0
#> M307T548_POS   M307T548_POS 307.14035 547.56641         273687.8
#> M183T224_POS   M183T224_POS 183.06209 224.32777        5103244.9
#> M349T47_POS     M349T47_POS 349.01584  47.00262        7169041.1
#> M182T828_POS   M182T828_POS 181.99775 828.35712        4433034.2

If you want to add mean/median intensity based on specific samples, just provide the sample IDs.

library(tidyverse)
###add mean intensity in variable_info based on QC sample
qc_id =
  object %>% 
  activate_mass_dataset(what = "sample_info") %>% 
  pull(sample_id)

object2 =
object %>% 
  mutate_mean_intensity(according_to_samples = qc_id, na.rm = TRUE)

extract_variable_info(object2) %>% head()
#>                 variable_id        mz        rt mean_intensity
#> M136T55_2_POS M136T55_2_POS 136.06140  54.97902      1808605.7
#> M79T35_POS       M79T35_POS  79.05394  35.36550      2671173.4
#> M307T548_POS   M307T548_POS 307.14035 547.56641       276256.3
#> M183T224_POS   M183T224_POS 183.06209 224.32777      5103244.9
#> M349T47_POS     M349T47_POS 349.01584  47.00262      6789862.4
#> M182T828_POS   M182T828_POS 181.99775 828.35712      4326865.8
###add RSD for each variable
object2 =
object %>% 
  mutate_rsd()

extract_variable_info(object2) %>% head()
#>                 variable_id        mz        rt       rsd
#> M136T55_2_POS M136T55_2_POS 136.06140  54.97902 50.756560
#> M79T35_POS       M79T35_POS  79.05394  35.36550 28.257007
#> M307T548_POS   M307T548_POS 307.14035 547.56641 35.041286
#> M183T224_POS   M183T224_POS 183.06209 224.32777  1.224228
#> M349T47_POS     M349T47_POS 349.01584  47.00262 27.715030
#> M182T828_POS   M182T828_POS 181.99775 828.35712 25.534063
###add na
object2 =
object %>% 
  mutate_sample_na_freq()

extract_variable_info(object2) %>% head()
#>     variable_id        mz        rt
#> 1 M136T55_2_POS 136.06140  54.97902
#> 2    M79T35_POS  79.05394  35.36550
#> 3  M307T548_POS 307.14035 547.56641
#> 4  M183T224_POS 183.06209 224.32777
#> 5   M349T47_POS 349.01584  47.00262
#> 6  M182T828_POS 181.99775 828.35712

###add na
object2 =
object %>% 
  mutate_sample_na_number()

extract_variable_info(object2) %>% head()
#>     variable_id        mz        rt
#> 1 M136T55_2_POS 136.06140  54.97902
#> 2    M79T35_POS  79.05394  35.36550
#> 3  M307T548_POS 307.14035 547.56641
#> 4  M183T224_POS 183.06209 224.32777
#> 5   M349T47_POS 349.01584  47.00262
#> 6  M182T828_POS 181.99775 828.35712

Use mutate() from tidyverse

###add a new column named as variable_id2 in variable_info
object2 =
object %>% 
  activate_mass_dataset(what = "variable_info") %>% 
  mutate(variable_id2 = variable_id)

extract_sample_info(object2) %>% head()
#>   sample_id injection.order   class   group
#> 1   Blank_3               1   Blank   Blank
#> 2   Blank_4               2   Blank   Blank
#> 3      QC_1               3      QC      QC
#> 4      QC_2               4      QC      QC
#> 5     PS4P1               5 Subject Subject
#> 6     PS4P2               6 Subject Subject

Use left_join() from tidyverse


new_variable_info = 
  data.frame(variable_id = c("M136T55_2_POS", "M79T35_POS"), 
             marker = c("yes", "no"))

object2 =
object %>% 
  activate_mass_dataset(what = "variable_info") %>% 
  left_join(new_variable_info, by = "variable_id")

extract_variable_info(object2) %>% head()
#>     variable_id        mz        rt marker
#> 1 M136T55_2_POS 136.06140  54.97902    yes
#> 2    M79T35_POS  79.05394  35.36550     no
#> 3  M307T548_POS 307.14035 547.56641   <NA>
#> 4  M183T224_POS 183.06209 224.32777   <NA>
#> 5   M349T47_POS 349.01584  47.00262   <NA>
#> 6  M182T828_POS 181.99775 828.35712   <NA>

Add new samples to expression_data

Just use the mutate() to add new samples to expression_data.

colnames(object)
#> [1] "Blank_3" "Blank_4" "QC_1"    "QC_2"    "PS4P1"   "PS4P2"   "PS4P3"  
#> [8] "PS4P4"
object2 = 
object %>% 
  activate_mass_dataset(what = "expression_data") %>% 
  mutate(QC_3 = QC_2,
         QC_4 = 1:1000)

colnames(object2)
#>  [1] "Blank_3" "Blank_4" "QC_1"    "QC_2"    "PS4P1"   "PS4P2"   "PS4P3"  
#>  [8] "PS4P4"   "QC_3"    "QC_4"

plot(object2$QC_2, object2$QC_3)

extract_sample_info(object2)
#>    sample_id injection.order   class   group
#> 1    Blank_3               1   Blank   Blank
#> 2    Blank_4               2   Blank   Blank
#> 3       QC_1               3      QC      QC
#> 4       QC_2               4      QC      QC
#> 5      PS4P1               5 Subject Subject
#> 6      PS4P2               6 Subject Subject
#> 7      PS4P3               7 Subject Subject
#> 8      PS4P4               8 Subject Subject
#> 9       QC_3               4      QC      QC
#> 10      QC_4              NA    <NA>    <NA>

Session information

sessionInfo()
#> R version 4.2.1 (2022-06-23)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS Big Sur ... 10.16
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] masstools_1.0.2    forcats_0.5.1.9000 stringr_1.4.0      dplyr_1.0.9       
#>  [5] purrr_0.3.4        readr_2.1.2        tidyr_1.2.0        tibble_3.1.7      
#>  [9] ggplot2_3.3.6      tidyverse_1.3.1    magrittr_2.0.3     tinytools_0.9.1   
#> [13] massdataset_1.0.12
#> 
#> loaded via a namespace (and not attached):
#>   [1] readxl_1.4.0                backports_1.4.1            
#>   [3] circlize_0.4.15             systemfonts_1.0.4          
#>   [5] plyr_1.8.7                  lazyeval_0.2.2             
#>   [7] BiocParallel_1.30.3         GenomeInfoDb_1.32.2        
#>   [9] Rdisop_1.56.0               digest_0.6.29              
#>  [11] foreach_1.5.2               yulab.utils_0.0.5          
#>  [13] htmltools_0.5.2             fansi_1.0.3                
#>  [15] memoise_2.0.1               cluster_2.1.3              
#>  [17] doParallel_1.0.17           tzdb_0.3.0                 
#>  [19] openxlsx_4.2.5              limma_3.52.2               
#>  [21] ComplexHeatmap_2.12.0       modelr_0.1.8               
#>  [23] matrixStats_0.62.0          pkgdown_2.0.5              
#>  [25] colorspace_2.0-3            rvest_1.0.2                
#>  [27] textshaping_0.3.6           haven_2.5.0                
#>  [29] xfun_0.31                   crayon_1.5.1               
#>  [31] RCurl_1.98-1.7              jsonlite_1.8.0             
#>  [33] impute_1.70.0               iterators_1.0.14           
#>  [35] glue_1.6.2                  gtable_0.3.0               
#>  [37] zlibbioc_1.42.0             XVector_0.36.0             
#>  [39] GetoptLong_1.0.5            DelayedArray_0.22.0        
#>  [41] shape_1.4.6                 BiocGenerics_0.42.0        
#>  [43] scales_1.2.0                vsn_3.64.0                 
#>  [45] DBI_1.1.3                   Rcpp_1.0.8.3               
#>  [47] mzR_2.30.0                  viridisLite_0.4.0          
#>  [49] clue_0.3-61                 gridGraphics_0.5-1         
#>  [51] preprocessCore_1.58.0       stats4_4.2.1               
#>  [53] MsCoreUtils_1.8.0           htmlwidgets_1.5.4          
#>  [55] httr_1.4.3                  RColorBrewer_1.1-3         
#>  [57] ellipsis_0.3.2              pkgconfig_2.0.3            
#>  [59] XML_3.99-0.10               sass_0.4.1                 
#>  [61] dbplyr_2.2.1                utf8_1.2.2                 
#>  [63] ggplotify_0.1.0             tidyselect_1.1.2           
#>  [65] rlang_1.0.3                 munsell_0.5.0              
#>  [67] cellranger_1.1.0            tools_4.2.1                
#>  [69] cachem_1.0.6                cli_3.3.0                  
#>  [71] generics_0.1.3              broom_1.0.0                
#>  [73] evaluate_0.15               fastmap_1.1.0              
#>  [75] mzID_1.34.0                 yaml_2.3.5                 
#>  [77] ragg_1.2.2                  knitr_1.39                 
#>  [79] fs_1.5.2                    zip_2.2.0                  
#>  [81] ncdf4_1.19                  pbapply_1.5-0              
#>  [83] xml2_1.3.3                  compiler_4.2.1             
#>  [85] rstudioapi_0.13             plotly_4.10.0              
#>  [87] png_0.1-7                   affyio_1.66.0              
#>  [89] reprex_2.0.1                bslib_0.3.1                
#>  [91] stringi_1.7.6               highr_0.9                  
#>  [93] desc_1.4.1                  MSnbase_2.22.0             
#>  [95] lattice_0.20-45             ProtGenerics_1.28.0        
#>  [97] Matrix_1.4-1                ggsci_2.9                  
#>  [99] vctrs_0.4.1                 pillar_1.7.0               
#> [101] lifecycle_1.0.1             BiocManager_1.30.18        
#> [103] jquerylib_0.1.4             MALDIquant_1.21            
#> [105] GlobalOptions_0.1.2         data.table_1.14.2          
#> [107] bitops_1.0-7                GenomicRanges_1.48.0       
#> [109] R6_2.5.1                    pcaMethods_1.88.0          
#> [111] affy_1.74.0                 IRanges_2.30.0             
#> [113] codetools_0.2-18            MASS_7.3-57                
#> [115] assertthat_0.2.1            SummarizedExperiment_1.26.1
#> [117] rprojroot_2.0.3             rjson_0.2.21               
#> [119] withr_2.5.0                 S4Vectors_0.34.0           
#> [121] GenomeInfoDbData_1.2.8      parallel_4.2.1             
#> [123] hms_1.1.1                   grid_4.2.1                 
#> [125] rmarkdown_2.14              MatrixGenerics_1.8.1       
#> [127] Biobase_2.56.0              lubridate_1.8.0