lgatto / MSnbase

Base Classes and Functions for Mass Spectrometry and Proteomics
http://lgatto.github.io/MSnbase/
124 stars 50 forks source link

Toggle warnings from empty spectra for filterMz/trimMz_Spectrum #518

Closed jmorim closed 4 years ago

jmorim commented 4 years ago

Is there any way to turn off the warnings from using filterMz? I'm using TOF data and if I want to apply a function to only a very small range of ions, my console overflows with these warnings below:

Warning in trimMz_Spectrum(object, mzlim = mz, msLevel. = msLevel., ...) :
  No data points between 195.086063745358 and 195.090954798427 for spectrum with acquisition number 2580879. Returning empty spectrum.
jorainer commented 4 years ago

You could globally disable warnings with options(warn = -1) and to restore the default again with options(warn = 0).

lgatto commented 4 years ago

To specifically ignore the warnings from filterMz, you could wrap it in suppressWarnings:

suppressWarnings(filterMz(...))
jmorim commented 4 years ago

I still get the warnings even when wrapping functions in suppressWarnings(), e.g. eic <- suppressWarnings(chromatogram(peaks, mz = mz.range, rt = rt.range))

Additionally, a lot of times loading xcms or MSnbase will spam with repeated welcome messages, does anyone happen to know how to suppress that?

Attaching package: �BiocGenerics�

The following objects are masked from �package:parallel�:

    clusterApply, clusterApplyLB, clusterCall, clusterEvalQ, clusterExport, clusterMap, parApply, parCapply, parLapply, parLapplyLB,
    parRapply, parSapply, parSapplyLB

The following objects are masked from �package:stats�:

    IQR, mad, sd, var, xtabs

The following objects are masked from �package:base�:

    anyDuplicated, append, as.data.frame, basename, cbind, colnames, dirname, do.call, duplicated, eval, evalq, Filter, Find, get,
    grep, grepl, intersect, is.unsorted, lapply, Map, mapply, match, mget, order, paste, pmax, pmax.int, pmin, pmin.int, Position,
    rank, rbind, Reduce, rownames, sapply, setdiff, sort, table, tapply, union, unique, unsplit, which, which.max, which.min

Welcome to Bioconductor

    Vignettes contain introductory material; view with 'browseVignettes()'. To cite Bioconductor, see 'citation("Biobase")', and for
    packages 'citation("pkgname")'.

Attaching package: �S4Vectors�

The following object is masked from �package:base�:

    expand.grid

Attaching package: �ProtGenerics�

The following object is masked from �package:stats�:

    smooth

This is MSnbase version 2.14.2 
  Visit https://lgatto.github.io/MSnbase/ to get started.

Attaching package: �MSnbase�

The following object is masked from �package:base�:

    trimws

This is xcms version 3.10.1 

Attaching package: �xcms�

The following object is masked from �package:stats�:

    sigma

Edit: For anyone else, using suppressMessages instead of suppressWarnings has hidden the messages and also improves the time to run those functions, I guess since there's some overhead in printing those warnings for every empty scan. In my opinion suppressing that message should be the default, especially with high-res data where there will be many empty spectra for small m/z slices

lgatto commented 4 years ago

Messages and warnings are two different things. You suppress the former with suppressMessages() and the latter with suppressWarnings().

What you show above are startup messages and namespace conflicts. It isn't printing them that takes time but loading packages and dependencies.

If you could provide a more specific account of what is that you think should be hidden by default, we could look into this. But messages and warnings are printed/returned for reasons, and can't be ignored by default.

jmorim commented 4 years ago

I gave you the warning I was getting.

Warning in trimMz_Spectrum(object, mzlim = mz, msLevel. = msLevel., ...) :
  No data points between 195.086063745358 and 195.090954798427 for spectrum with acquisition number 2580879. Returning empty spectrum.

It's triggered by these lines in your code: https://github.com/lgatto/MSnbase/blob/48dd14fd788c467ba7cf8f67d94c865b3ebde811/R/functions-Spectrum.R#L255-L282 where you have the warning wrapped in warning(). It's not suppressed by suppressWarning(). What I think should be hidden by default are these warnings for high res data. Is that specific enough?

lgatto commented 4 years ago

It's not suppressed by suppressWarning().

Here's a reproducible example that show that the warning is suppressed.

> sp1 <- new("Spectrum2",
+            mz = 1:100,
+            intensity = abs(rnorm(length(mz))))
> sp2 <- trimMz(sp1, c(101, 110))
Warning message:
In trimMz_Spectrum(object, mzlim, ...) :
  No data points between 101 and 110 for spectrum with acquisition number
. Returning empty spectrum.
> suppressWarnings(sp2 <- trimMz(sp1, c(101, 110)))
jmorim commented 4 years ago

Can you try calling a function that calls trimMz?

eic <- suppressWarnings(chromatogram(ms.data, mz = c(195.0831, 195.0929))
# throws warnings

I don't understand why calling it at a higher level doesn't suppress warnings.

lgatto commented 4 years ago

Ok, thanks. Could you share some mock data for me to reproduce this?

jmorim commented 4 years ago

I'm not sure how to synthesize data but here's an example TOF data file https://github.com/jmorim/twoDxc/raw/master/inst/tea_data/tea_pool_1.mzML

lgatto commented 4 years ago

Thank you for the file. Here's what I get:

> x <- readMSData("~/Downloads/tea_pool_1.mzML", mode = "onDisk")
> eic <- suppressWarnings(chromatogram(x, mz = c(195.0831, 195.0929)))

No warning?!?

But then, the positive control doesn't return and warning either:

> x <- readMSData("~/Downloads/tea_pool_1.mzML", mode = "onDisk")
> eic <- chromatogram(x, mz = c(195.0831, 195.0929))

I am using

> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/libf77blas.so.3.10.3
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] MSnbase_2.14.2      ProtGenerics_1.20.0 S4Vectors_0.26.1   
[4] mzR_2.22.0          Rcpp_1.0.5          Biobase_2.48.0     
[7] BiocGenerics_0.34.0

loaded via a namespace (and not attached):
 [1] BiocManager_1.30.10   pillar_1.4.5          compiler_4.0.0       
 [4] plyr_1.8.6            tools_4.0.0           iterators_1.0.12     
 [7] zlibbioc_1.34.0       digest_0.6.25         ncdf4_1.17           
[10] MALDIquant_1.19.5     lifecycle_0.2.0       tibble_3.0.3         
[13] preprocessCore_1.50.0 gtable_0.3.0          lattice_0.20-41      
[16] pkgconfig_2.0.3       rlang_0.4.7           foreach_1.5.0        
[19] dplyr_1.0.0           IRanges_2.22.2        generics_0.0.2       
[22] vctrs_0.3.1           grid_4.0.0            tidyselect_1.1.0     
[25] glue_1.4.1            impute_1.62.0         R6_2.4.1             
[28] XML_3.99-0.4          BiocParallel_1.22.0   limma_3.44.3         
[31] ggplot2_3.3.2         purrr_0.3.4           magrittr_1.5         
[34] scales_1.1.1          pcaMethods_1.80.0     codetools_0.2-16     
[37] ellipsis_0.3.1        MASS_7.3-51.6         mzID_1.26.0          
[40] colorspace_1.4-1      affy_1.66.0           doParallel_1.0.15    
[43] munsell_0.5.0         vsn_3.56.0            crayon_1.3.4         
[46] affyio_1.58.0        

Could you please provide some code to reproduce your issue using the file that you shared.