Browse code

Merge remote-tracking branch 'upstream/master'

Bryson Gibbons authored on 23/05/2018 17:33:02
Showing 10 changed files

... ...
@@ -1,6 +1,8 @@
1 1
 CHANGES IN VERSION 2.15.1
2 2
 -------------------------
3
- o Fix type (see https://github.com/sneumann/mzR/pull/162)
3
+ o Fix typo (see https://github.com/sneumann/mzR/pull/162)
4
+ o New .hasSpectra and .hasChromatograms private function (see
5
+   https://github.com/lgatto/MSnbase/issues/343)
4 6
 
5 7
 CHANGES IN VERSION 2.15.0
6 8
 -------------------------
... ...
@@ -169,3 +169,13 @@ setMethod("show",
169 169
             invisible(NULL)
170 170
           })
171 171
 
172
+setMethod("chromatograms", "mzRnetCDF", function(object, chrom)
173
+    chromatogram(object, chrom))
174
+setMethod("chromatogram", "mzRnetCDF", function(object, chrom) {
175
+    warning("The mzRnetCdf backend does not support chromatographic data")
176
+    .empty_chromatogram()
177
+})
178
+setMethod("chromatogramHeader", "mzRnetCDF", function(object, chrom) {
179
+    warning("The mzRnetCdf backend does not support chromatographic data")
180
+    .empty_chromatogram_header()
181
+})
... ...
@@ -163,3 +163,14 @@ setMethod("show",
163 163
 
164 164
 setMethod("isolationWindow", "mzRramp",
165 165
           function(object, ...) .isolationWindow(fileName(object), ...))
166
+
167
+setMethod("chromatograms", "mzRramp", function(object, chrom)
168
+    chromatogram(object, chrom))
169
+setMethod("chromatogram", "mzRramp", function(object, chrom) {
170
+    warning("The mzRnetCdf backend does not support chromatographic data")
171
+    .empty_chromatogram()
172
+})
173
+setMethod("chromatogramHeader", "mzRramp", function(object, chrom) {
174
+    warning("The mzRnetCdf backend does not support chromatographic data")
175
+    .empty_chromatogram_header()
176
+})
... ...
@@ -40,3 +40,59 @@ setMethod("isolationWindow", "character",
40 40
     x <- lapply(x, base::unique)
41 41
     any(sapply(x, function(xx) nrow(xx) > 1))
42 42
 }
43
+
44
+
45
+.hasSpectra <- function(x) {
46
+    close_after <- FALSE
47
+    if (is.character(x) && file.exists(x)) {
48
+        x <- mzR::openMSfile(x)
49
+        ## Ensure we are closing the file later
50
+        close_after <- TRUE
51
+    }
52
+    stopifnot(inherits(x, "mzR"))
53
+    len <- length(x)
54
+    if (close_after)
55
+        close(x)
56
+    return(as.logical(len))
57
+}
58
+
59
+#' Create return data for MS backends not supporting chromatographic data. This
60
+#' function is supposed to be called by the chromatogram(s) methods for these
61
+#' backends
62
+#'
63
+#' @author Johannes Rainer
64
+#'
65
+#' @noRd
66
+.empty_chromatogram <- function() {
67
+    list()
68
+}
69
+
70
+#' Create return data for MS backends not supporting chromatographic data.
71
+#'
72
+#' @author Johannes Rainer
73
+#'
74
+#' @noRd
75
+.empty_chromatogram_header <- function() {
76
+    cn <- c("chromatogramId", "chromatogramIndex", "polarity",
77
+            "precursorIsolationWindowTargetMZ",
78
+            "precursorIsolationWindowLowerOffset",
79
+            "precursorIsolationWindowUpperOffset",
80
+            "precursorCollisionEnergy", "productIsolationWindowTargetMZ",
81
+            "productIsolationWindowLowerOffset",
82
+            "productIsolationWindowUpperOffset")
83
+    data.frame(matrix(nrow = 0, ncol = length(cn),
84
+                      dimnames = list(character(), cn)))
85
+}
86
+
87
+.hasChromatograms <- function(x) {
88
+    close_after <- FALSE
89
+    if (is.character(x) && file.exists(x)) {
90
+        x <- mzR::openMSfile(x)
91
+        close_after <- TRUE
92
+    }
93
+    stopifnot(inherits(x, "mzR"))
94
+    hdr <- chromatogramHeader(x)
95
+    if (close_after)
96
+        close(x)
97
+    as.logical(nrow(hdr))
98
+}
... ...
@@ -58,3 +58,27 @@ test_header <- function() {
58 58
 
59 59
   close(cdf)
60 60
 }
61
+
62
+test_chromatogram <- function() {
63
+    file <- system.file('cdf/ko15.CDF', package = "msdata")
64
+    x <- openMSfile(file, backend="netCDF")        
65
+    suppressWarnings(
66
+        chr <- chromatogram(x)
67
+    )
68
+    checkTrue(length(chr) == 0)
69
+    suppressWarnings(
70
+        chr <- chromatograms(x)
71
+    )
72
+    checkTrue(length(chr) == 0)
73
+    close(x)
74
+}
75
+
76
+test_chromatogramHeader <- function() {
77
+    file <- system.file('cdf/ko15.CDF', package = "msdata")
78
+    x <- openMSfile(file, backend="netCDF")        
79
+    suppressWarnings(
80
+        ch <- chromatogramHeader(x)
81
+    )
82
+    checkTrue(nrow(ch) == 0)
83
+    close(x)
84
+}
... ...
@@ -102,4 +102,32 @@ test_peaks_spectra <- function() {
102 102
     p <- peaks(x, 1:10)
103 103
     s <- spectra(x, 1:10)
104 104
     checkIdentical(p, s)
105
+    close(x)
105 106
 }
107
+
108
+test_chromatogram <- function() {
109
+    library("msdata")
110
+    f <- proteomics(full.names = TRUE)
111
+    x <- openMSfile(f[1], backend = "Ramp")
112
+    suppressWarnings(
113
+        chr <- chromatogram(x)
114
+    )
115
+    checkTrue(length(chr) == 0)
116
+    suppressWarnings(
117
+        chr <- chromatograms(x)
118
+    )
119
+    checkTrue(length(chr) == 0)
120
+    close(x)
121
+}
122
+
123
+test_chromatogramHeader <- function() {
124
+    library("msdata")
125
+    f <- proteomics(full.names = TRUE)
126
+    x <- openMSfile(f[1], backend = "Ramp")
127
+    suppressWarnings(
128
+        ch <- chromatogramHeader(x)
129
+    )
130
+    checkTrue(nrow(ch) == 0)
131
+    close(x)
132
+}
133
+
106 134
new file mode 100644
... ...
@@ -0,0 +1,20 @@
1
+test_hasChromatograms <- function() {
2
+    fl <- system.file("proteomics/MRM-standmix-5.mzML.gz", package = "msdata")
3
+    x <- mzR::openMSfile(fl, backend = "pwiz")
4
+    checkTrue(mzR:::.hasChromatograms(x))
5
+    checkTrue(mzR:::.hasChromatograms(fl))
6
+    close(x)
7
+    
8
+    fl <- system.file("cdf/ko15.CDF", package = "msdata")
9
+    x <- openMSfile(fl, backend = "netCDF")        
10
+    suppressWarnings(checkTrue(!mzR:::.hasChromatograms(x)))
11
+    suppressWarnings(checkTrue(!mzR:::.hasChromatograms(fl)))
12
+    close(x)
13
+
14
+    fl <- system.file("sciex/20171016_POOL_POS_1_105-134.mzML",
15
+                      package = "msdata")
16
+    x <- mzR::openMSfile(fl, backend = "pwiz")
17
+    checkTrue(!mzR:::.hasChromatograms(x))
18
+    checkTrue(!mzR:::.hasChromatograms(fl))
19
+    close(x)
20
+}
... ...
@@ -29,6 +29,12 @@
29 29
 \alias{chromatogram,mzRpwiz-method}
30 30
 \alias{chromatogramHeader,mzRpwiz-method}
31 31
 \alias{chromatograms,mzRpwiz-method}
32
+\alias{chromatogram,mzRramp-method}
33
+\alias{chromatogramHeader,mzRramp-method}
34
+\alias{chromatograms,mzRramp-method}
35
+\alias{chromatogram,mzRnetCDF-method}
36
+\alias{chromatogramHeader,mzRnetCDF-method}
37
+\alias{chromatograms,mzRnetCDF-method}
32 38
 \alias{tic,mzRpwiz-method}
33 39
 \alias{nChrom}
34 40
 \alias{chromatogram}
... ...
@@ -84,7 +84,7 @@ int RcppPwiz::getLastScan() const {
84 84
     SpectrumListPtr slp = msd->run.spectrumListPtr;
85 85
     return slp->size();
86 86
   }
87
-  Rprintf("Warning: pwiz not yet initialized.\n ");
87
+  Rf_warningcall(R_NilValue, "pwiz not yet initialized.");
88 88
   return -1;
89 89
 }
90 90
 
... ...
@@ -93,7 +93,7 @@ int RcppPwiz::getLastChrom() const {
93 93
     ChromatogramListPtr clp = msd->run.chromatogramListPtr;
94 94
     return clp->size();
95 95
   }
96
-  Rprintf("Warning: pwiz not yet initialized.\n ");
96
+  Rf_warningcall(R_NilValue, "pwiz not yet initialized.");
97 97
   return -1;
98 98
 }
99 99
 
... ...
@@ -160,7 +160,7 @@ Rcpp::List RcppPwiz::getInstrumentInfo ( )
160 160
         }
161 161
       return(instrumentInfo);
162 162
     }
163
-  Rprintf("Warning: pwiz not yet initialized.\n ");
163
+  Rf_warningcall(R_NilValue, "pwiz not yet initialized.");
164 164
   return instrumentInfo;
165 165
 }
166 166
 
... ...
@@ -306,7 +306,7 @@ Rcpp::DataFrame RcppPwiz::getScanHeaderInfo (Rcpp::IntegerVector whichScan)
306 306
       
307 307
       return header;
308 308
     }
309
-  Rprintf("Warning: pwiz not yet initialized.\n ");
309
+  Rf_warningcall(R_NilValue, "pwiz not yet initialized.");
310 310
   return Rcpp::DataFrame::create( );
311 311
 }
312 312
 
... ...
@@ -324,7 +324,7 @@ Rcpp::DataFrame RcppPwiz::getAllScanHeaderInfo ( )
324 324
         }
325 325
       return allScanHeaderInfo ;
326 326
     }
327
-  Rprintf("Warning: pwiz not yet initialized.\n ");
327
+  Rf_warningcall(R_NilValue, "pwiz not yet initialized.");
328 328
   return Rcpp::DataFrame::create( );
329 329
 }
330 330
 
... ...
@@ -362,7 +362,7 @@ Rcpp::List RcppPwiz::getPeakList ( int whichScan )
362 362
 				Rcpp::_["peaks"]  = peaks
363 363
 				) ;
364 364
     }
365
-  Rprintf("Warning: pwiz not yet initialized.\n ");
365
+  Rf_warningcall(R_NilValue, "pwiz not yet initialized.");
366 366
   return Rcpp::List::create( );
367 367
 }
368 368
 
... ...
@@ -756,10 +756,10 @@ Rcpp::DataFrame RcppPwiz::getChromatogramsInfo( int whichChrom )
756 756
   if (msd != NULL) {
757 757
     ChromatogramListPtr clp = msd->run.chromatogramListPtr;
758 758
     if (clp.get() == 0) {
759
-      Rcpp::Rcerr << "The direct support for chromatogram info is only available in mzML format." << std::endl;
759
+      Rf_warningcall(R_NilValue, "The direct support for chromatogram info is only available in mzML format.");
760 760
       return Rcpp::DataFrame::create();
761 761
     } else if (clp->size() == 0) {
762
-      Rcpp::Rcerr << "No available chromatogram info." << std::endl;
762
+      Rf_warningcall(R_NilValue, "No available chromatogram info.");
763 763
       return Rcpp::DataFrame::create();
764 764
     } else if ( (whichChrom < 0) || (whichChrom > clp->size()) ) {
765 765
       Rprintf("Index whichChrom out of bounds [0 ... %d].\n", (clp->size())-1);
... ...
@@ -783,7 +783,7 @@ Rcpp::DataFrame RcppPwiz::getChromatogramsInfo( int whichChrom )
783 783
     }
784 784
     return(chromatogramsInfo);
785 785
   }
786
-  Rprintf("Warning: pwiz not yet initialized.\n ");
786
+  Rf_warningcall(R_NilValue, "pwiz not yet initialized.");
787 787
   return Rcpp::DataFrame::create( );
788 788
 }
789 789
 
... ...
@@ -794,10 +794,10 @@ Rcpp::DataFrame RcppPwiz::getChromatogramHeaderInfo (Rcpp::IntegerVector whichCh
794 794
     CVID nativeIdFormat_ = id::getDefaultNativeIDFormat(*msd); // Ask CHRIS if I'm correctly dereferencing this...
795 795
     ChromatogramListPtr clp = msd->run.chromatogramListPtr;
796 796
     if (clp.get() == 0) {
797
-      Rcpp::Rcerr << "The direct support for chromatogram info is only available in mzML format." << std::endl;
797
+      Rf_warningcall(R_NilValue, "The direct support for chromatogram info is only available in mzML format.");
798 798
       return Rcpp::DataFrame::create();
799 799
     } else if (clp->size() == 0) {
800
-      Rcpp::Rcerr << "No available chromatogram info." << std::endl;
800
+      Rf_warningcall(R_NilValue, "No available chromatogram info.");
801 801
       return Rcpp::DataFrame::create();
802 802
     }
803 803
 
... ...
@@ -821,8 +821,8 @@ Rcpp::DataFrame RcppPwiz::getChromatogramHeaderInfo (Rcpp::IntegerVector whichCh
821 821
     for (int i = 0; i < N_chrom; i++) {
822 822
       int current_chrom = whichChrom[i];
823 823
       if (current_chrom < 0 || current_chrom > N) {
824
+	Rf_warningcall(R_NilValue, "Provided index out of bounds.");
824 825
 	Rcpp::Rcerr << "Provided index out of bounds" << std::endl;
825
-	return Rcpp::DataFrame::create();
826 826
       }
827 827
       ChromatogramPtr ch = clp->chromatogram(current_chrom - 1, false);
828 828
       chromatogramId[i] = ch->id;
... ...
@@ -877,18 +877,24 @@ Rcpp::DataFrame RcppPwiz::getChromatogramHeaderInfo (Rcpp::IntegerVector whichCh
877 877
     chromHeader.attr("names") = names;
878 878
     return chromHeader;
879 879
   }
880
-  Rprintf("Warning: pwiz not yet initialized.\n ");
880
+  Rf_warningcall(R_NilValue, "pwiz not yet initialized.");
881 881
   return Rcpp::DataFrame::create( );
882 882
 }
883 883
 
884 884
 Rcpp::DataFrame RcppPwiz::getAllChromatogramHeaderInfo ( ) {
885 885
   if (msd != NULL) {
886 886
     ChromatogramListPtr clp = msd->run.chromatogramListPtr;
887
+    if (clp.get() == 0) {
888
+      Rf_warningcall(R_NilValue, "The direct support for chromatogram info is only available in mzML format.");
889
+      return Rcpp::DataFrame::create();
890
+    }
887 891
     int N = clp->size();
888
-    
889
-    return getChromatogramHeaderInfo(Rcpp::seq(1, N));
892
+    if (N > 0) {
893
+      return getChromatogramHeaderInfo(Rcpp::seq(1, N));
894
+    } else {
895
+      Rf_warningcall(R_NilValue, "pwiz not yet initialized.");
896
+    }
890 897
   }
891
-  Rprintf("Warning: pwiz not yet initialized.\n ");
892 898
   return Rcpp::DataFrame::create( );
893 899
 }
894 900
 
... ...
@@ -915,7 +921,7 @@ Rcpp::NumericMatrix RcppPwiz::get3DMap ( std::vector<int> scanNumbers, double wh
915 921
         }
916 922
 
917 923
       int j=0;
918
-      Rprintf("%d\n",1);
924
+      //Rprintf("%d\n",1);
919 925
       for (int i = 0; i < scanNumbers.size(); i++)
920 926
         {
921 927
 	  SpectrumPtr s = slp->spectrum(scanNumbers[i] - 1, true);
... ...
@@ -939,7 +945,7 @@ Rcpp::NumericMatrix RcppPwiz::get3DMap ( std::vector<int> scanNumbers, double wh
939 945
       return(map3d);
940 946
     }
941 947
 
942
-  Rprintf("Warning: pwiz not yet initialized.\n ");
948
+  Rf_warningcall(R_NilValue, "pwiz not yet initialized.");
943 949
   return Rcpp::NumericMatrix(0,0);
944 950
 }
945 951
 
... ...
@@ -947,6 +953,6 @@ string RcppPwiz::getRunStartTimeStamp() {
947 953
   if (msd != NULL) {
948 954
     return msd->run.startTimeStamp;
949 955
   }
950
-  Rprintf("Warning: pwiz not yet initialized.\n ");
956
+  Rf_warningcall(R_NilValue, "pwiz not yet initialized.");
951 957
   return "";
952 958
 }
... ...
@@ -55,7 +55,7 @@ Rcpp::StringVector RcppRamp::getFilename (  )
55 55
     {
56 56
         return filename;
57 57
     }
58
-    Rprintf("Warning: Ramp not yet initialized.\n ");
58
+    Rf_warningcall(R_NilValue, "Ramp not yet initialized.");
59 59
     return filename;
60 60
 }
61 61
 
... ...
@@ -86,7 +86,7 @@ Rcpp::List RcppRamp::getRunInfo (  )
86 86
         }
87 87
         return runInfo;
88 88
     }
89
-    Rprintf("Warning: Ramp not yet initialized.\n");
89
+    Rf_warningcall(R_NilValue, "Ramp not yet initialized.");
90 90
     return runInfo;
91 91
 }
92 92
 
... ...
@@ -130,7 +130,7 @@ Rcpp::List RcppRamp::getInstrumentInfo ( )
130 130
         }
131 131
         return(instrumentInfo);
132 132
     }
133
-    Rprintf("Warning: Ramp not yet initialized.\n ");
133
+    Rf_warningcall(R_NilValue, "Ramp not yet initialized.");
134 134
     return instrumentInfo;
135 135
 }
136 136
 
... ...
@@ -200,7 +200,7 @@ Rcpp::List RcppRamp::getScanHeaderInfo ( int whichScan  )
200 200
 
201 201
         return  header;
202 202
     }
203
-    Rprintf("Warning: Ramp not yet initialized.\n ");
203
+    Rf_warningcall(R_NilValue, "Ramp not yet initialized.");
204 204
     return Rcpp::List::create( );
205 205
 }
206 206
 
... ...
@@ -322,7 +322,7 @@ Rcpp::DataFrame RcppRamp::getAllScanHeaderInfo ( )
322 322
         }
323 323
         return(allScanHeaderInfo);
324 324
     }
325
-    Rprintf("Warning: Ramp not yet initialized.\n ");
325
+    Rf_warningcall(R_NilValue, "Ramp not yet initialized.");
326 326
     return Rcpp::DataFrame::create( );
327 327
 }
328 328
 
... ...
@@ -361,7 +361,7 @@ Rcpp::List RcppRamp::getPeakList ( int whichScan )
361 361
                    Rcpp::_["peaks"]  = peaks
362 362
                ) ;
363 363
     }
364
-    Rprintf("Warning: Ramp not yet initialized.\n ");
364
+    Rf_warningcall(R_NilValue, "Ramp not yet initialized.");
365 365
     return Rcpp::List::create( );
366 366
 }
367 367
 
... ...
@@ -384,7 +384,7 @@ Rcpp::NumericMatrix RcppRamp::get3DMap ( std::vector<int> scanNumbers, double wh
384 384
         }
385 385
         // map3d = 0.0;
386 386
         int j=0;
387
-        Rprintf("%d\n",1);
387
+        //Rprintf("%d\n",1);
388 388
         for (int i = 0; i < scanNumbers.size(); i++)
389 389
         {
390 390
             rampPeakList *pl = ramp->getPeakList( scanNumbers[i] );
... ...
@@ -415,7 +415,7 @@ Rcpp::NumericMatrix RcppRamp::get3DMap ( std::vector<int> scanNumbers, double wh
415 415
         }
416 416
         return(map3d);
417 417
     }
418
-    Rprintf("Warning: Ramp not yet initialized.\n ");
418
+    Rf_warningcall(R_NilValue, "Ramp not yet initialized.");
419 419
     return Rcpp::NumericMatrix(0,0);
420 420
 }
421 421
 
... ...
@@ -425,7 +425,7 @@ int RcppRamp::getLastScan() const
425 425
     {
426 426
         return ramp->getLastScan();
427 427
     }
428
-    Rprintf("Warning: Ramp not yet initialized.\n ");
428
+    Rf_warningcall(R_NilValue, "Ramp not yet initialized.");
429 429
     return -1;
430 430
 }
431 431