Browse code

Merge pull request #204 from jorainer/master

Add scanWindowLowerLimit and scanWindowUpperLimit to header

Steffen Neumann authored on 31/08/2019 13:39:13 • GitHub committed on 31/08/2019 13:39:13
Showing 12 changed files

... ...
@@ -2,7 +2,7 @@ Package: mzR
2 2
 Type: Package
3 3
 Title: parser for netCDF, mzXML, mzData and mzML and mzIdentML files
4 4
        (mass spectrometry data)
5
-Version: 2.19.3
5
+Version: 2.19.4
6 6
 Author: Bernd Fischer, Steffen Neumann, Laurent Gatto, Qiang Kou, Johannes Rainer
7 7
 Maintainer: Steffen Neumann <sneumann@ipb-halle.de>,
8 8
 	    Laurent Gatto <lg390@cam.ac.uk>,
... ...
@@ -1,3 +1,7 @@
1
+CHANGES IN VERSION 2.19.4
2
+-------------------------
3
+ o Add header columns scanWindowLowerLimit and scanWindowUpperLimit
4
+
1 5
 CHANGES IN VERSION 2.19.3
2 6
 -------------------------
3 7
  o use ProtGenerics::tolerance generic <2019-08-16 Fri>
... ...
@@ -41,7 +41,9 @@
41 41
                   ionMobilityDriftTime = "numeric",
42 42
                   isolationWindowTargetMZ = "numeric",
43 43
                   isolationWindowLowerOffset = "numeric",
44
-                  isolationWindowUpperOffset = "numeric"
44
+                  isolationWindowUpperOffset = "numeric",
45
+                  scanWindowLowerLimit = "numeric",
46
+                  scanWindowUpperLimit = "numeric"
45 47
                   )
46 48
     if (!is.data.frame(x))
47 49
         return("'x' is supposed to be a data.frame")
... ...
@@ -53,6 +55,10 @@
53 55
         x$isolationWindowLowerOffset <- NA_real_
54 56
     if (!any(colnames(x) == "isolationWindowUpperOffset"))
55 57
         x$isolationWindowUpperOffset <- NA_real_
58
+    if (!any(colnames(x) == "scanWindowLowerLimit"))
59
+        x$scanWindowLowerLimit <- NA_real_
60
+    if (!any(colnames(x) == "scanWindowUpperLimit"))
61
+        x$scanWindowUpperLimit <- NA_real_
56 62
     if (!(all(names(req_cols) %in% colnames(x))))
57 63
         return(paste0("'x' is missing one or more required columns: ",
58 64
                       paste(names(req_cols), collapse = ", ")))
... ...
@@ -82,6 +82,8 @@ setMethod("header",
82 82
             result$isolationWindowTargetMZ <- NA_real_
83 83
             result$isolationWindowLowerOffset <- NA_real_
84 84
             result$isolationWindowUpperOffset <- NA_real_
85
+            result$scanWindowLowerLimit <- NA_real_
86
+            result$scanWindowUpperLimit <- NA_real_
85 87
             return(result)
86 88
           })
87 89
 
... ...
@@ -51,6 +51,8 @@ setMethod("header",
51 51
               res$isolationWindowTargetMZ <- NA_real_
52 52
               res$isolationWindowLowerOffset <- NA_real_
53 53
               res$isolationWindowUpperOffset <- NA_real_
54
+              res$scanWindowLowerLimit <- NA_real_
55
+              res$scanWindowUpperLimit <- NA_real_
54 56
               res
55 57
 })
56 58
 
... ...
@@ -71,6 +73,8 @@ setMethod("header",
71 73
               res$isolationWindowTargetMZ <- NA_real_
72 74
               res$isolationWindowLowerOffset <- NA_real_
73 75
               res$isolationWindowUpperOffset <- NA_real_
76
+              res$scanWindowLowerLimit <- NA_real_
77
+              res$scanWindowUpperLimit <- NA_real_
74 78
               res
75 79
           })
76 80
 
... ...
@@ -77,7 +77,8 @@ setMethod("isolationWindow", "character",
77 77
             "precursorIsolationWindowTargetMZ",
78 78
             "precursorIsolationWindowLowerOffset",
79 79
             "precursorIsolationWindowUpperOffset",
80
-            "precursorCollisionEnergy", "productIsolationWindowTargetMZ",
80
+            "precursorCollisionEnergy",
81
+            "productIsolationWindowTargetMZ",
81 82
             "productIsolationWindowLowerOffset",
82 83
             "productIsolationWindowUpperOffset")
83 84
     data.frame(matrix(nrow = 0, ncol = length(cn),
... ...
@@ -64,7 +64,7 @@ test_header <- function() {
64 64
   cdf <- openMSfile(file, backend="netCDF")        
65 65
 
66 66
   h <- header(cdf)
67
-  checkEquals(ncol(h), 26)
67
+  checkEquals(ncol(h), 28)
68 68
   checkEquals(nrow(h), 1278)
69 69
   checkTrue(any(colnames(h) == "centroided"))
70 70
   checkTrue(all(is.na(h$centroided)))
... ...
@@ -74,12 +74,17 @@ test_header <- function() {
74 74
   checkTrue(any(colnames(h) == "spectrumId"))
75 75
   checkEquals(h$spectrumId, paste0("scan=", h$acquisitionNum))
76 76
   
77
+  checkTrue(any(colnames(h) == "scanWindowLowerLimit"))
78
+  checkTrue(any(colnames(h) == "scanWindowUpperLimit"))
79
+  checkTrue(all(is.na(h$scanWindowLowerLimit)))
80
+  checkTrue(all(is.na(h$scanWindowUpperLimit)))
81
+
77 82
   h <- header(cdf, 1)
78
-  checkEquals(ncol(h), 26)
83
+  checkEquals(ncol(h), 28)
79 84
   checkEquals(nrow(h), 1)
80 85
 
81 86
   h <- header(cdf, 2:3)
82
-  checkEquals(ncol(h), 26)
87
+  checkEquals(ncol(h), 28)
83 88
   checkEquals(nrow(h), 2)
84 89
 
85 90
   close(cdf)
... ...
@@ -15,13 +15,17 @@ test_mzXML <- function() {
15 15
     hdr <- header(mzxml)
16 16
     checkTrue(any(colnames(hdr) == "spectrumId"))
17 17
     checkTrue(all(hdr$centroided))
18
+    checkTrue(any(colnames(hdr) == "scanWindowLowerLimit"))
19
+    checkTrue(any(colnames(hdr) == "scanWindowUpperLimit"))
20
+    checkTrue(all(is.na(hdr$scanWindowLowerLimit)))
21
+    checkTrue(all(is.na(hdr$scanWindowUpperLimit)))
18 22
     hdr <- header(mzxml,1)
19 23
     checkTrue(is.list(hdr))
20 24
     hdr <- header(mzxml, 2:3)
21 25
     checkTrue(is.data.frame(hdr))
22 26
     checkTrue(nrow(hdr) == 2)
23 27
     fileName(mzxml)
24
-    close(mzxml)    
28
+    close(mzxml)
25 29
 }
26 30
 
27 31
 test_mzML <- function() {
... ...
@@ -40,6 +44,10 @@ test_mzML <- function() {
40 44
     checkTrue(any(colnames(hdr) == "spectrumId"))
41 45
     checkTrue(all(hdr$centroided))
42 46
     checkEquals(hdr$spectrumId, paste0("spectrum=", hdr$acquisitionNum))
47
+    checkTrue(any(colnames(hdr) == "scanWindowLowerLimit"))
48
+    checkTrue(any(colnames(hdr) == "scanWindowUpperLimit"))
49
+    checkTrue(all(!is.na(hdr$scanWindowLowerLimit)))
50
+    checkTrue(all(!is.na(hdr$scanWindowUpperLimit)))
43 51
     hdr <- header(mzml,1)
44 52
     checkTrue(is.list(hdr))
45 53
     hdr <- header(mzml, 2:3)
... ...
@@ -76,7 +84,8 @@ test_getScanHeaderInfo <- function() {
76 84
     ## Read single scan header.
77 85
     scan_3 <- header(mzml, scans = 3)
78 86
     cn <- names(scan_3)
79
-    cn <- cn[cn != "spectrumId"]
87
+    cn <- cn[!(cn %in% c("spectrumId", "scanWindowLowerLimit",
88
+                         "scanWindowUpperLimit"))]
80 89
     scan_3_ramp <- header(ramp, scans = 3)
81 90
     ## Ramp does not read polarity
82 91
     scan_3$polarity <- 0
... ...
@@ -19,6 +19,10 @@ test_mzXML <- function() {
19 19
     checkTrue(all(is.na(hdr$centroided)))
20 20
     checkTrue(any(colnames(hdr) == "ionMobilityDriftTime"))
21 21
     checkTrue(all(is.na(hdr$ionMobilityDriftTime)))
22
+    checkTrue(any(colnames(hdr) == "scanWindowLowerLimit"))
23
+    checkTrue(any(colnames(hdr) == "scanWindowUpperLimit"))
24
+    checkTrue(all(is.na(hdr$scanWindowLowerLimit)))
25
+    checkTrue(all(is.na(hdr$scanWindowUpperLimit)))
22 26
     close(mzxml)
23 27
 }
24 28
 
... ...
@@ -41,6 +45,10 @@ test_mzML <- function() {
41 45
     header(mzml,2:3)
42 46
     checkTrue(any(colnames(hdr) == "ionMobilityDriftTime"))
43 47
     checkTrue(all(is.na(hdr$ionMobilityDriftTime)))
48
+    checkTrue(any(colnames(hdr) == "scanWindowLowerLimit"))
49
+    checkTrue(any(colnames(hdr) == "scanWindowUpperLimit"))
50
+    checkTrue(all(is.na(hdr$scanWindowLowerLimit)))
51
+    checkTrue(all(is.na(hdr$scanWindowUpperLimit)))
44 52
 
45 53
     checkTrue(ncol(header(mzml))>4)
46 54
     checkTrue(length(header(mzml,1))>4)
... ...
@@ -186,7 +186,9 @@ test_copyWriteMSData <- function() {
186 186
     checkEquals(hdr_mod[, rt_col], hdr_new[, rt_col], tolerance = 0.01)
187 187
     cn <- colnames(hdr)[!(colnames(hdr) %in% c("injectionTime", "retentionTime",
188 188
                                                "filterString", "spectrumId",
189
-                                               "isolationWindowTargetMZ"))]
189
+                                               "isolationWindowTargetMZ",
190
+                                               "scanWindowLowerLimit",
191
+                                               "scanWindowUpperLimit"))]
190 192
     checkEquals(hdr_mod[, cn], hdr_new[, cn])
191 193
     ## checkEquals(ii, ii_new)
192 194
 
... ...
@@ -245,7 +247,9 @@ test_copyWriteMSData <- function() {
245 247
                                                        "retentionTime",
246 248
                                                        "filterString",
247 249
                                                        "spectrumId",
248
-                                                       "isolationWindowTargetMZ"))]
250
+                                                       "isolationWindowTargetMZ",
251
+                                                       "scanWindowLowerLimit",
252
+                                                       "scanWindowUpperLimit"))]
249 253
     checkEquals(hdr_sub[, cn], hdr_new[, cn])    
250 254
     
251 255
     ## Other mzML:
... ...
@@ -279,8 +283,9 @@ test_copyWriteMSData <- function() {
279 283
     hdr_2 <- header(in_file)
280 284
     pks_2 <- peaks(in_file)
281 285
     mzR::close(in_file)
282
-    checkEquals(hdr[, colnames(hdr) != "spectrumId"],
283
-                hdr_2[, colnames(hdr_2) != "spectrumId"])
286
+    cn <- c("spectrumId", "scanWindowLowerLimit", "scanWindowUpperLimit")
287
+    checkEquals(hdr[, !(colnames(hdr) %in% cn)],
288
+                hdr_2[, !(colnames(hdr_2) %in% cn)])
284 289
     checkEquals(pks, pks_2)
285 290
     checkEquals(ii, ii_2)
286 291
 }
... ...
@@ -444,7 +449,9 @@ test_writeMSData <- function() {
444 449
                                                        "injectionTime",
445 450
                                                        "filterString",
446 451
                                                        "spectrumId",
447
-                                                       "isolationWindowTargetMZ"))]
452
+                                                       "isolationWindowTargetMZ",
453
+                                                       "scanWindowLowerLimit",
454
+                                                       "scanWindowUpperLimit"))]
448 455
     checkEquals(hdr_mod[, cn], hdr_2[, cn])
449 456
     
450 457
     ## Subset. These checks ensure that the scan - precursor scan are mapped
... ...
@@ -512,7 +519,9 @@ test_writeMSData <- function() {
512 519
                                                        "retentionTime",
513 520
                                                        "filterString",
514 521
                                                        "spectrumId",
515
-                                                       "isolationWindowTargetMZ"))]
522
+                                                       "isolationWindowTargetMZ",
523
+                                                       "scanWindowLowerLimit",
524
+                                                       "scanWindowUpperLimit"))]
516 525
     checkEquals(hdr_sub[, cn], hdr_new[, cn])
517 526
     
518 527
     ## Other mzML:
... ...
@@ -545,9 +554,10 @@ test_writeMSData <- function() {
545 554
     pks_2 <- peaks(in_file)
546 555
     mzR::close(in_file)
547 556
     checkEquals(pks, pks_2)
548
-    checkEquals(hdr[, colnames(hdr_2) != "spectrumId"],
549
-                hdr_2[, colnames(hdr_2) != "spectrumId"])
550
-
557
+    cn <- c("spectrumId", "scanWindowLowerLimit", "scanWindowUpperLimit")
558
+    checkEquals(hdr[, !(colnames(hdr_2) %in% cn)],
559
+                hdr_2[, !(colnames(hdr_2) %in% cn)])
560
+    
551 561
     ## mzData:
552 562
     test_file <- system.file("iontrap", "extracted.mzData", package = "msdata")
553 563
     in_file <- openMSfile(test_file, backend = "Ramp")
... ...
@@ -124,8 +124,9 @@
124 124
   or profile mode; only for pwiz backend), \code{injectionTime} (ion
125 125
   injection time, in milliseconds), \code{ionMobilityDriftTime} (in
126 126
   milliseconds), \code{isolationWindowTargetMZ},
127
-  \code{isolationWindowLowerOffset} and
128
-  \code{isolationWindowUpperOffset}. If multiple scans are queried, a
127
+  \code{isolationWindowLowerOffset},
128
+  \code{isolationWindowUpperOffset}, \code{scanWindowLowerLimit} and
129
+  \code{scanWindowUpperLimit}. If multiple scans are queried, a
129 130
   \code{data.frame} is returned with the scans reported along the
130 131
   rows.
131 132
 
... ...
@@ -210,6 +210,8 @@ Rcpp::DataFrame RcppPwiz::getScanHeaderInfo (Rcpp::IntegerVector whichScan)
210 210
       Rcpp::NumericVector isolationWindowTargetMZ(N_scans);
211 211
       Rcpp::NumericVector isolationWindowLowerOffset(N_scans);
212 212
       Rcpp::NumericVector isolationWindowUpperOffset(N_scans);
213
+      Rcpp::NumericVector scanWindowLowerLimit(N_scans);
214
+      Rcpp::NumericVector scanWindowUpperLimit(N_scans);
213 215
       
214 216
       for (int i = 0; i < N_scans; i++)
215 217
 	{
... ...
@@ -228,6 +230,14 @@ Rcpp::DataFrame RcppPwiz::getScanHeaderInfo (Rcpp::IntegerVector whichScan)
228 230
 	  ionInjectionTime[i] = (scan.cvParam(MS_ion_injection_time).timeInSeconds() * 1000);
229 231
 	  filterString[i] = scan.cvParam(MS_filter_string).value.empty() ? NA_STRING : Rcpp::String(scan.cvParam(MS_filter_string).value);
230 232
 	  ionMobilityDriftTime[i] = scan.cvParam(MS_ion_mobility_drift_time).value.empty() ? NA_REAL : (scan.cvParam(MS_ion_mobility_drift_time).timeInSeconds() * 1000);
233
+
234
+	  if (!scan.scanWindows.empty()) {
235
+	    scanWindowLowerLimit[i] = scan.scanWindows[0].cvParam(MS_scan_window_lower_limit).valueAs<double>();
236
+	    scanWindowUpperLimit[i] = scan.scanWindows[0].cvParam(MS_scan_window_upper_limit).valueAs<double>();
237
+	  } else {
238
+	    scanWindowLowerLimit[i] = NA_REAL;
239
+	    scanWindowUpperLimit[i] = NA_REAL;
240
+	  }
231 241
 	  
232 242
 	  if (!sp->precursors.empty()) {
233 243
 	    IsolationWindow iwin = sp->precursors[0].isolationWindow;
... ...
@@ -270,7 +280,7 @@ Rcpp::DataFrame RcppPwiz::getScanHeaderInfo (Rcpp::IntegerVector whichScan)
270 280
       delete adapter;
271 281
       adapter = NULL;
272 282
 
273
-      Rcpp::List header(29);
283
+      Rcpp::List header(31);
274 284
       std::vector<std::string> names;
275 285
       int i = 0;
276 286
       names.push_back("seqNum");
... ...
@@ -331,6 +341,10 @@ Rcpp::DataFrame RcppPwiz::getScanHeaderInfo (Rcpp::IntegerVector whichScan)
331 341
       header[i++] = Rcpp::wrap(isolationWindowLowerOffset);      
332 342
       names.push_back("isolationWindowUpperOffset");
333 343
       header[i++] = Rcpp::wrap(isolationWindowUpperOffset);      
344
+      names.push_back("scanWindowLowerLimit");
345
+      header[i++] = Rcpp::wrap(scanWindowLowerLimit);      
346
+      names.push_back("scanWindowUpperLimit");
347
+      header[i++] = Rcpp::wrap(scanWindowUpperLimit);      
334 348
       header.attr("names") = names;
335 349
       
336 350
       return header;
... ...
@@ -651,6 +665,8 @@ void RcppPwiz::addSpectrumList(MSData& msd,
651 665
   Rcpp::NumericVector isolationWindowTargetMZ = spctr_header["isolationWindowTargetMZ"];
652 666
   Rcpp::NumericVector isolationWindowLowerOffset = spctr_header["isolationWindowLowerOffset"];
653 667
   Rcpp::NumericVector isolationWindowUpperOffset = spctr_header["isolationWindowUpperOffset"];
668
+  Rcpp::NumericVector scanWindowLowerLimit = spctr_header["scanWindowLowerLimit"];
669
+  Rcpp::NumericVector scanWindowUpperLimit = spctr_header["scanWindowUpperLimit"];
654 670
   
655 671
   // From MSnbase::Spectrum        Column in the header
656 672
   // msLevel integer               $msLevel
... ...
@@ -727,7 +743,11 @@ void RcppPwiz::addSpectrumList(MSData& msd,
727 743
     if (ionMobilityDriftTime[i] != NA_REAL)
728 744
       spct_scan.set(MS_ion_mobility_drift_time, ionMobilityDriftTime[i],
729 745
 		    UO_millisecond);
730
-    
746
+
747
+    // scanWindow
748
+    if (scanWindowLowerLimit[i] != NA_REAL && scanWindowUpperLimit[i] != NA_REAL) {
749
+      spct_scan.scanWindows.push_back(ScanWindow(scanWindowLowerLimit[i], scanWindowUpperLimit[i], MS_m_z));
750
+    }
731 751
     // MSn - precursor:
732 752
     if (precursorScanNum[i] > 0 | precursorMZ[i] > 0) {
733 753
       // Fill precursor data. This preserves the precursor data even if the
... ...
@@ -834,7 +854,7 @@ Rcpp::DataFrame RcppPwiz::getChromatogramsInfo( int whichChrom )
834 854
 Rcpp::DataFrame RcppPwiz::getChromatogramHeaderInfo (Rcpp::IntegerVector whichChrom)
835 855
 {
836 856
   if (msd != NULL) {
837
-    CVID nativeIdFormat_ = id::getDefaultNativeIDFormat(*msd); // Ask CHRIS if I'm correctly dereferencing this...
857
+    CVID nativeIdFormat_ = id::getDefaultNativeIDFormat(*msd);
838 858
     ChromatogramListPtr clp = msd->run.chromatogramListPtr;
839 859
     if (clp.get() == 0) {
840 860
       Rf_warningcall(R_NilValue, "The direct support for chromatogram info is only available in mzML format.");
... ...
@@ -874,8 +894,8 @@ Rcpp::DataFrame RcppPwiz::getChromatogramHeaderInfo (Rcpp::IntegerVector whichCh
874 894
       polarity[i] = (param.cvid==MS_negative_scan ? 0 : (param.cvid==MS_positive_scan ? +1 : -1 ) );
875 895
       if (!ch->precursor.empty()) {
876 896
 	precursorIsolationWindowTargetMZ[i] = ch->precursor.isolationWindow.cvParam(MS_isolation_window_target_m_z).value.empty() ? NA_REAL : ch->precursor.isolationWindow.cvParam(MS_isolation_window_target_m_z).valueAs<double>();
877
-	precursorIsolationWindowLowerOffset[i] = ch->precursor.isolationWindow.cvParam(MS_isolation_window_lower_offset).value.empty() ? 0 : ch->precursor.isolationWindow.cvParam(MS_isolation_window_lower_offset).valueAs<double>();
878
-	precursorIsolationWindowUpperOffset[i] = ch->precursor.isolationWindow.cvParam(MS_isolation_window_upper_offset).value.empty() ? 0 : ch->precursor.isolationWindow.cvParam(MS_isolation_window_upper_offset).valueAs<double>();
897
+	precursorIsolationWindowLowerOffset[i] = ch->precursor.isolationWindow.cvParam(MS_isolation_window_lower_offset).value.empty() ? NA_REAL : ch->precursor.isolationWindow.cvParam(MS_isolation_window_lower_offset).valueAs<double>();
898
+	precursorIsolationWindowUpperOffset[i] = ch->precursor.isolationWindow.cvParam(MS_isolation_window_upper_offset).value.empty() ? NA_REAL : ch->precursor.isolationWindow.cvParam(MS_isolation_window_upper_offset).valueAs<double>();
879 899
 	precursorCollisionEnergy[i] = ch->precursor.activation.cvParam(MS_collision_energy).value.empty() ? NA_REAL : ch->precursor.activation.cvParam(MS_collision_energy).valueAs<double>(); 
880 900
       } else {
881 901
 	precursorIsolationWindowTargetMZ[i] = NA_REAL;
... ...
@@ -885,8 +905,8 @@ Rcpp::DataFrame RcppPwiz::getChromatogramHeaderInfo (Rcpp::IntegerVector whichCh
885 905
       }
886 906
       if (!ch->product.empty()) {
887 907
 	productIsolationWindowTargetMZ[i] = ch->product.isolationWindow.cvParam(MS_isolation_window_target_m_z).value.empty() ? NA_REAL : ch->product.isolationWindow.cvParam(MS_isolation_window_target_m_z).valueAs<double>();
888
-	productIsolationWindowLowerOffset[i] = ch->product.isolationWindow.cvParam(MS_isolation_window_lower_offset).value.empty() ? 0 : ch->product.isolationWindow.cvParam(MS_isolation_window_lower_offset).valueAs<double>();
889
-	productIsolationWindowUpperOffset[i] = ch->product.isolationWindow.cvParam(MS_isolation_window_upper_offset).value.empty() ? 0 : ch->product.isolationWindow.cvParam(MS_isolation_window_upper_offset).valueAs<double>();
908
+	productIsolationWindowLowerOffset[i] = ch->product.isolationWindow.cvParam(MS_isolation_window_lower_offset).value.empty() ? NA_REAL : ch->product.isolationWindow.cvParam(MS_isolation_window_lower_offset).valueAs<double>();
909
+	productIsolationWindowUpperOffset[i] = ch->product.isolationWindow.cvParam(MS_isolation_window_upper_offset).value.empty() ? NA_REAL : ch->product.isolationWindow.cvParam(MS_isolation_window_upper_offset).valueAs<double>();
890 910
       } else {
891 911
 	productIsolationWindowTargetMZ[i] = NA_REAL;
892 912
 	productIsolationWindowLowerOffset[i] = NA_REAL;