rformassspectrometry / QFeatures

Quantitative features for mass spectrometry data
https://RforMassSpectrometry.github.io/QFeatures/
25 stars 7 forks source link

"Mutate"/assign/create new assays #183

Closed steveb-123 closed 1 year ago

steveb-123 commented 1 year ago

Firstly, thanks for developing this package, its looking really nice so far!

I would like to add a new assay in my experiment based on an existing one, which I create/modify with full freedom. Ideally I wanted to be able to duplicate an existing assay slot so that all linking info is preserved.

Perhaps to explain the logic, here is an example. This morning I wanted to apply this function which takes a matrix and returns a matrix to create the new slot, to test out the effects of normalisation within groups:

normalise_by_cols <- function(matrix,
                              col_group_list = list(c(1,2), c(3,4), c(5,6)),
                              normalise_function = "quantiles.robust") {
  lapply(
    col_group_list,
    function(x) {
      matrix[, unlist(x)] %>% ## take a piece
        MsCoreUtils::normalize_matrix(normalise_function) ## normalise
    }
  ) %>%
    do.call(cbind, .) ## put back together
}

I've been struggling to hack through to making this work with my QFeatures assays. I've tried addAssay and various assignment approaches but I always error out.

Apologies in advance for any foolishness.. I could be missing something obvious, it could be a missing functionality, or missing/unclear documentation, hence the issue/question here.

I have dug through MultiAssayExperiment and QFeatures docs (a little, and a lot, respectively), but with no luck. It may also be that I don't understand the philosophy behind the S4 Bioconductor classes, so please call me out if my expectations of freedom are off-target.

lgatto commented 1 year ago

What version of QFeatures are you using?

steveb-123 commented 1 year ago

1.6, I believe

> sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Arch Linux

Matrix products: default
BLAS:   /usr/lib/libopenblasp-r0.3.21.so
LAPACK: /usr/lib/liblapack.so.3.11.0

locale:
 [1] LC_CTYPE=C.UTF-8    LC_NUMERIC=C        LC_TIME=C          
 [4] LC_COLLATE=C        LC_MONETARY=C       LC_MESSAGES=C      
 [7] LC_PAPER=C          LC_NAME=C           LC_ADDRESS=C       
[10] LC_TELEPHONE=C      LC_MEASUREMENT=C    LC_IDENTIFICATION=C

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] shiny_1.7.4                 msqrob2_1.4.0              
 [3] QFeatures_1.6.0             MultiAssayExperiment_1.22.0
 [5] SummarizedExperiment_1.26.1 Biobase_2.58.0             
 [7] GenomicRanges_1.48.0        GenomeInfoDb_1.32.4        
 [9] IRanges_2.32.0              S4Vectors_0.36.1           
[11] BiocGenerics_0.44.0         MatrixGenerics_1.8.1       
[13] matrixStats_0.63.0          forcats_0.5.2              
[15] stringr_1.5.0               dplyr_1.0.10               
[17] purrr_1.0.1                 readr_2.1.3                
[19] tidyr_1.2.1                 tibble_3.1.8               
[21] ggplot2_3.4.0               tidyverse_1.3.2            
[23] patchwork_1.1.2            

loaded via a namespace (and not attached):
  [1] readxl_1.4.1            backports_1.4.1         systemfonts_1.0.4      
  [4] plyr_1.8.8              igraph_1.3.5            lazyeval_0.2.2         
  [7] shinydashboard_0.7.2    splines_4.2.2           crosstalk_1.2.0        
 [10] BiocParallel_1.32.5     digest_0.6.31           foreach_1.5.2          
 [13] htmltools_0.5.4         fansi_1.0.3             memoise_2.0.1          
 [16] magrittr_2.0.3          googlesheets4_1.0.1     cluster_2.1.4          
 [19] doParallel_1.0.17       tzdb_0.3.0              limma_3.54.0           
 [22] modelr_0.1.10           vroom_1.6.0             timechange_0.2.0       
 [25] colorspace_2.0-3        rvest_1.0.3             textshaping_0.3.6      
 [28] haven_2.5.1             crayon_1.5.2            RCurl_1.98-1.9         
 [31] jsonlite_1.8.4          lme4_1.1-31             impute_1.72.2          
 [34] iterators_1.0.14        glue_1.6.2              gtable_0.3.1           
 [37] gargle_1.2.1            zlibbioc_1.44.0         XVector_0.36.0         
 [40] DelayedArray_0.22.0     scales_1.2.1            vsn_3.66.0             
 [43] DBI_1.1.3               Rcpp_1.0.9              mzR_2.32.0             
 [46] xtable_1.8-4            clue_0.3-63             gridGraphics_0.5-1     
 [49] bit_4.0.5               preprocessCore_1.60.1   MsCoreUtils_1.10.0     
 [52] DT_0.26                 htmlwidgets_1.6.1       httr_1.4.4             
 [55] ellipsis_0.3.2          pkgconfig_2.0.3         XML_3.99-0.13          
 [58] farver_2.1.1            sass_0.4.4              dbplyr_2.2.1           
 [61] utf8_1.2.2              tidyselect_1.2.0        labeling_0.4.2         
 [64] rlang_1.0.6             later_1.3.0             munsell_0.5.0          
 [67] cellranger_1.1.0        tools_4.2.2             cachem_1.0.6           
 [70] cli_3.6.0               generics_0.1.3          broom_1.0.2            
 [73] fastmap_1.1.0           yaml_2.3.6              mzID_1.36.0            
 [76] ragg_1.2.5              bit64_4.0.5             fs_1.5.2               
 [79] AnnotationFilter_1.20.0 ncdf4_1.21              nlme_3.1-161           
 [82] mime_0.12               xml2_1.3.3              compiler_4.2.2         
 [85] affyio_1.68.0           reprex_2.0.2            bslib_0.4.2            
 [88] stringi_1.7.12          MSnbase_2.24.2          lattice_0.20-45        
 [91] ProtGenerics_1.30.0     Matrix_1.5-3            nloptr_2.0.3           
 [94] vctrs_0.5.1             pillar_1.8.1            lifecycle_1.0.3        
 [97] BiocManager_1.30.19     jquerylib_0.1.4         MALDIquant_1.22        
[100] bitops_1.0-7            httpuv_1.6.8            R6_2.5.1               
[103] pcaMethods_1.90.0       affy_1.76.0             promises_1.2.0.1       
[106] codetools_0.2-18        boot_1.3-28.1           MASS_7.3-58.1          
[109] assertthat_0.2.1        fontawesome_0.4.0       withr_2.5.0            
[112] GenomeInfoDbData_1.2.8  parallel_4.2.2          hms_1.1.2              
[115] grid_4.2.2              minqa_1.2.5             googledrive_2.0.0      
[118] lubridate_1.9.0        
>
cvanderaa commented 1 year ago

Actually we have this documented in the scp package, that heavily relies on QFeatures. See here. @lgatto, may be worth to include in QFeatures as well?

Here are two pieces of advice:

steveb-123 commented 1 year ago

@cvanderaa the vignette in scp was exactly what I was wondering about. Thank you! I was missing getWithColData() in this instance. And your code is much slicker than my replacment solution too btw!! Thanks again :)

@lgatto Oops, I hadn't noticed I was stuck on an older bioconductor until just now, thanks!