ProteoWizard
RegionAnalyzerTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: RegionAnalyzerTest.cpp 4136 2012-11-21 21:17:24Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2008 Spielberg Family Center for Applied Proteomics
8 // Cedars-Sinai Medical Center, Los Angeles, California 90048
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //
22 
23 
24 #include "RegionAnalyzer.hpp"
28 #include <cstring>
29 #include <boost/algorithm/string/split.hpp>
30 
31 
32 using namespace pwiz;
33 using namespace pwiz::util;
34 using namespace pwiz::analysis;
35 
36 
37 ostream* os_ = 0;
38 
39 
40 // s0 .......
41 // s1 |.-^-.| (+.1 m/z)
42 // s2 |.-^-.| (-.1 m/z, x2 intensity)
43 // s3 |.-^-.| (+.1 m/z)
44 // s4 .......
45 
46 
47 double data_[5][14] =
48 {
49  {0,9, 1,9, 2,9, 3,9, 4,9, 5,9, 6,9},
50  {0.1,9, 1.1,0, 2.1,1.5, 3.1,2, 4.1,1.5, 5.1,0, 6.1,9},
51  {-.1,9, .9,0, 1.9,3, /*2.9,4,*/ 3.9,3, 4.9,0, 5.9,9, 6,0}, // we still find interpolated peak
52  {0.1,9, 1.1,0, 2.1,1.5, 3.1,2, 4.1,1.5, 5.1,0, 6.1,9},
53  {0,9, 1,9, 2,9, 3,9, 4,9, 5,9, 6,9}
54 };
55 
56 
57 void initialize(MSData& msd)
58 {
60  msd.run.spectrumListPtr = sl;
61 
62  for (size_t i=0; i<5; i++)
63  {
64  SpectrumPtr spectrum(new Spectrum);
65  sl->spectra.push_back(spectrum);
66  spectrum->index = i;
67  spectrum->id = "scan=" + lexical_cast<string>(18+(int)i);
68  spectrum->scanList.scans.push_back(Scan());
69  spectrum->scanList.scans.back().cvParams.push_back(CVParam(MS_scan_start_time, 420+int(i), UO_second));
70  spectrum->setMZIntensityPairs((MZIntensityPair*)data_[i], 7, MS_number_of_counts);
71  }
72 }
73 
74 
75 const double epsilon_ = 1e-14;
76 
77 
78 ostream& operator<<(ostream& os, const RegionAnalyzer::SpectrumStats& ss)
79 {
80  os << ss.sumIntensity << " "
81  << "(" << ss.max.mz << "," << ss.max.intensity << ")" << " "
82  << "(" << ss.peak.mz << "," << ss.peak.intensity << ")";
83  return os;
84 }
85 
86 
87 ostream& operator<<(ostream& os, const RegionAnalyzer::Stats& stats)
88 {
89  os << "nonzeroCount: " << stats.nonzeroCount << endl
90  << "sum_sumIntensity: " << stats.sum_sumIntensity << endl
91  << "sum_peak_intensity: " << stats.sum_peak_intensity << endl
92  << "mean_peak_mz: " << stats.mean_peak_mz << endl
93  << "variance_peak_mz: " << stats.variance_peak_mz << endl
94  << "sd_peak_mz: " << stats.sd_peak_mz << endl
95  << "indexApex: " << stats.indexApex << endl;
96  return os;
97 }
98 
99 
101 {
102  MSData msd;
103  initialize(msd);
104 
105  shared_ptr<MSDataCache> cache(new MSDataCache);
106  shared_ptr<RegionAnalyzer> regionAnalyzer(new RegionAnalyzer(config, *cache));
107 
108  MSDataAnalyzerContainer analyzers;
109  analyzers.push_back(cache);
110  analyzers.push_back(regionAnalyzer);
111 
112  MSDataAnalyzerDriver driver(analyzers);
113  driver.analyze(msd);
114 
115  unit_assert(regionAnalyzer->spectrumStats().size() == 5);
116 
117  if (os_) *os_ << "sumIntensity (max) (peak):\n";
118 
119  vector<RegionAnalyzer::SpectrumStats>::const_iterator it =
120  regionAnalyzer->spectrumStats().begin();
121  if (os_) *os_ << *it << endl;
122  unit_assert(it->sumIntensity == 0);
123  unit_assert_equal(it->max.mz, 0, epsilon_);
124  unit_assert_equal(it->max.intensity, 0, epsilon_);
125  unit_assert_equal(it->peak.mz, 0, epsilon_);
126  unit_assert_equal(it->peak.intensity, 0, epsilon_);
127 
128  ++it;
129  if (os_) *os_ << *it << endl;
130  unit_assert(it->sumIntensity == 5);
131  unit_assert_equal(it->max.mz, 3.1, epsilon_);
132  unit_assert_equal(it->max.intensity, 2, epsilon_);
133  unit_assert_equal(it->peak.mz, 3.1, epsilon_);
134  unit_assert_equal(it->peak.intensity, 2, epsilon_);
135 
136  ++it;
137  if (os_) *os_ << *it << endl;
138  unit_assert(it->sumIntensity == 6); // with omitted sample intensity 4
139  unit_assert_equal(it->max.mz, 1.9, epsilon_);
140  unit_assert_equal(it->max.intensity, 3, epsilon_);
141  unit_assert_equal(it->peak.mz, 2.9, epsilon_); // found the peak by interpolation
142  unit_assert_equal(it->peak.intensity, 4, epsilon_);
143 
144  ++it;
145  if (os_) *os_ << *it << endl;
146  unit_assert(it->sumIntensity == 5);
147  unit_assert_equal(it->max.mz, 3.1, epsilon_);
148  unit_assert_equal(it->max.intensity, 2, epsilon_);
149  unit_assert_equal(it->peak.mz, 3.1, epsilon_);
150  unit_assert_equal(it->peak.intensity, 2, epsilon_);
151 
152  ++it;
153  if (os_) *os_ << *it << endl;
154  unit_assert(it->sumIntensity == 0);
155  unit_assert_equal(it->max.mz, 0, epsilon_);
156  unit_assert_equal(it->max.intensity, 0, epsilon_);
157  unit_assert_equal(it->peak.mz, 0, epsilon_);
158  unit_assert_equal(it->peak.intensity, 0, epsilon_);
159 
160  // Stats
161 
162  const RegionAnalyzer::Stats& stats = regionAnalyzer->stats();
163  if (os_) *os_ << stats << endl;
164  unit_assert(stats.nonzeroCount == 3);
170  unit_assert(stats.indexApex == 2);
171 }
172 
173 
174 void test()
175 {
176  // test for each output delimiter type
177  std::vector<std::string> options;
178  std::vector<std::string> outputs;
179  std::string delimiter_help(TABULARCONFIG_DELIMITER_OPTIONS_STR);
180  boost::algorithm::split(options, delimiter_help, boost::algorithm::is_any_of("=|") );
181 
182  for (int n=1; n<(int)options.size(); n++)
183  {
184  std::ostringstream txtstream;
185  std::string delim = options[0] + "=" + options[n];
186  if (os_) *os_ << "test with delimiter style " << delim <<":\n";
187 
188  if (os_) *os_ << "test index:\n";
189  RegionAnalyzer::Config config;
190  unit_assert(config.checkDelimiter(delim)); // set output delimiter type
191  config.dumpRegionData = true;
192  config.osDump = &txtstream; // dump to this stream
193  config.mzRange = make_pair(.5, 5.5);
194  config.indexRange = make_pair(1,3);
195  testConfig(config);
196 
197  if (os_) *os_ << "test scanNumber:\n";
198  config = RegionAnalyzer::Config();
199  unit_assert(config.checkDelimiter(delim)); // set output delimiter type
200  config.dumpRegionData = true;
201  config.osDump = &txtstream; // dump to this stream
202  config.mzRange = make_pair(.5, 5.5);
203  config.scanNumberRange = make_pair(19,21);
204  testConfig(config);
205 
206  if (os_) *os_ << "test retentionTime:\n";
207  config = RegionAnalyzer::Config();
208  unit_assert(config.checkDelimiter(delim)); // set output delimiter type
209  config.dumpRegionData = true;
210  config.osDump = &txtstream; // dump to this stream
211  config.mzRange = make_pair(.5, 5.5);
212  config.rtRange = make_pair(420.5, 423.5);
213  testConfig(config);
214 
215  // save each output style
216  outputs.push_back(txtstream.str());
217  }
218 
219  // all the outputs should be different
220  for (int m=(int)outputs.size();m-->1;)
221  {
222  unit_assert(outputs[m]!=outputs[m-1]);
223  }
224  // now convert all delimiters back to a single space
225  for (int mm=(int)outputs.size();mm--;)
226  {
227  // reduce double spaces to single
228  // no, replace_all(outputs[mm],std::string(" "),std::string(" ")); doesn't do it
229  for (int n=outputs[mm].size();n-->1;)
230  {
231  if ((outputs[mm][n]==' ') && (outputs[mm][n-1]==' '))
232  {
233  outputs[mm] = outputs[mm].substr(0,n-1)+outputs[mm].substr(n);
234  }
235  if ((outputs[mm][n]==' ') && (outputs[mm][n-1]=='\n'))
236  {
237  // watch for lines with leading spaces
238  outputs[mm] = outputs[mm].substr(0,n-1)+outputs[mm].substr(n);
239  outputs[mm][n-1]='\n';
240  }
241  }
242  // and replace single character delimiters
243  boost::algorithm::replace_all(outputs[mm],std::string("\t"),std::string(" "));
244  boost::algorithm::replace_all(outputs[mm],std::string(","),std::string(" "));
245  }
246  // all the outputs should now be identical
247  for (int delimtype=(int)outputs.size();delimtype-->1;)
248  {
249  unit_assert(outputs[delimtype]==outputs[delimtype-1]);
250  }
251 }
252 
253 
254 int main(int argc, char* argv[])
255 {
256  TEST_PROLOG(argc, argv)
257 
258  try
259  {
260  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
261  test();
262  }
263  catch (exception& e)
264  {
265  TEST_FAILED(e.what())
266  }
267  catch (...)
268  {
269  TEST_FAILED("Caught unknown exception.")
270  }
271 
273 }
274