ProteoWizard
TextWriter.hpp
Go to the documentation of this file.
1 //
2 // $Id: TextWriter.hpp 1880 2010-03-08 22:52:43Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2007 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 #ifndef _TEXTWRITER_HPP_
25 #define _TEXTWRITER_HPP_
26 
27 
29 #include "MSData.hpp"
30 #include "boost/lexical_cast.hpp"
31 #include <iostream>
32 #include <string>
33 #include <vector>
34 
35 
36 namespace pwiz {
37 namespace msdata {
38 
39 
41 {
42  public:
43 
44  /// constructs a TextWriter for MSData types
45  /// @param os The ostream to write to.
46  /// @param depth The number of indentations to prefix to each output line.
47  /// @param arrayExampleCount The number of example values to print for arrays (i.e. m/z and intensity); -1 for unlimited
48  TextWriter(std::ostream& os, int depth = 0, int arrayExampleCount = 3)
49  : os_(os),
50  depth_(depth),
51  arrayExampleCount_(arrayExampleCount < 0 ? std::numeric_limits<size_t>::max()
52  : (size_t) arrayExampleCount),
53  indent_(depth*2, ' ')
54  {}
55 
56  TextWriter child() {return TextWriter(os_, depth_+1, arrayExampleCount_);}
57 
58  TextWriter& operator()(const std::string& text)
59  {
60  os_ << indent_ << text << std::endl;
61  return *this;
62  }
63 
64  TextWriter& operator()(const CVParam& cvParam)
65  {
66  os_ << indent_ << "cvParam: " << cvTermInfo(cvParam.cvid).name;
67  if (!cvParam.value.empty())
68  os_ << ", " << cvParam.value;
69  if (cvParam.units != CVID_Unknown)
70  os_ << ", " << cvParam.unitsName();
71  os_ << std::endl;
72  return *this;
73  }
74 
75  TextWriter& operator()(const UserParam& userParam)
76  {
77  os_ << indent_ << "userParam: " << userParam.name;
78  if (!userParam.value.empty()) os_ << ", " << userParam.value;
79  if (!userParam.type.empty()) os_ << ", " << userParam.type;
80  if (userParam.units != CVID_Unknown) os_ << ", " << cvTermInfo(userParam.units).name;
81  os_ << std::endl;
82  return *this;
83  }
84 
85  template<typename object_type>
86  TextWriter& operator()(const std::string& label, const std::vector<object_type>& v)
87  {
88  (*this)(label);
89  for_each(v.begin(), v.end(), child());
90  return *this;
91  }
92 
93 
94  TextWriter& operator()(const MSData& msd, bool metadata_only=false)
95  {
96  (*this)("msdata:");
97  child()
98  ("id: " + msd.id);
99  if (!msd.accession.empty())
100  child()("accession: " + msd.accession);
101  if (!msd.version().empty())
102  child()("version: " + msd.version());
103  if (!msd.cvs.empty())
104  child()("cvList: ", msd.cvs);
105  if (!msd.fileDescription.empty())
106  child()(msd.fileDescription);
107  if (!msd.paramGroupPtrs.empty())
108  child()("paramGroupList: ", msd.paramGroupPtrs);
109  if (!msd.samplePtrs.empty())
110  child()("sampleList: " , msd.samplePtrs);
111  if (!msd.softwarePtrs.empty())
112  child()("softwareList: ", msd.softwarePtrs);
113  if (!msd.scanSettingsPtrs.empty())
114  child()("scanSettingsList: ", msd.scanSettingsPtrs);
115  if (!msd.instrumentConfigurationPtrs.empty())
116  child()("instrumentConfigurationList: ", msd.instrumentConfigurationPtrs);
117  if (!msd.dataProcessingPtrs.empty())
118  child()("dataProcessingList: ", msd.dataProcessingPtrs);
119 
120  if (!msd.run.empty())
121  child()(msd.run, metadata_only);
122 
123  return *this;
124  }
125 
126  TextWriter& operator()(const CV& cv)
127  {
128  (*this)("cv:");
129  child()
130  ("id: " + cv.id)
131  ("fullName: " + cv.fullName)
132  ("version: " + cv.version)
133  ("URI: " + cv.URI);
134  return *this;
135  }
136 
137  TextWriter& operator()(const FileDescription& fd)
138  {
139  (*this)("fileDescription:");
140  child()
141  (fd.fileContent)
142  ("sourceFileList: ", fd.sourceFilePtrs);
143  for_each(fd.contacts.begin(), fd.contacts.end(), child());
144  return *this;
145  }
146 
147  TextWriter& operator()(const ParamContainer& paramContainer)
148  {
149  for (std::vector<ParamGroupPtr>::const_iterator it=paramContainer.paramGroupPtrs.begin();
150  it!=paramContainer.paramGroupPtrs.end(); ++it)
151  (*this)("referenceableParamGroupRef: " + (*it)->id);
152  for_each(paramContainer.cvParams.begin(), paramContainer.cvParams.end(), *this);
153  for_each(paramContainer.userParams.begin(), paramContainer.userParams.end(), *this);
154  return *this;
155  }
156 
157  TextWriter& operator()(const FileContent& fileContent)
158  {
159  (*this)("fileContent:");
160  child()(static_cast<const ParamContainer&>(fileContent));
161  return *this;
162  }
163 
164  TextWriter& operator()(const SourceFile& sf)
165  {
166  (*this)("sourceFile:");
167  child()
168  ("id: " + sf.id)
169  ("name: " + sf.name)
170  ("location: " + sf.location)
171  (static_cast<const ParamContainer&>(sf));
172  return *this;
173  }
174 
175  TextWriter& operator()(const Contact& contact)
176  {
177  (*this)("contact:");
178  child()(static_cast<const ParamContainer&>(contact));
179  return *this;
180  }
181 
182  TextWriter& operator()(const ParamGroup& paramGroup)
183  {
184  (*this)("paramGroup:");
185  child()
186  ("id: " + paramGroup.id)
187  (static_cast<const ParamContainer&>(paramGroup));
188  return *this;
189  }
190 
191  TextWriter& operator()(const Sample& sample)
192  {
193  (*this)("sample:");
194  child()
195  ("id: " + sample.id)
196  ("name: " + sample.name)
197  (static_cast<const ParamContainer&>(sample));
198  return *this;
199  }
200 
201  TextWriter& operator()(const InstrumentConfiguration& instrumentConfiguration)
202  {
203  (*this)("instrumentConfiguration:");
204  child()
205  ("id: " + instrumentConfiguration.id)
206  (static_cast<const ParamContainer&>(instrumentConfiguration));
207  if (!instrumentConfiguration.componentList.empty())
208  child()(instrumentConfiguration.componentList);
209  if (instrumentConfiguration.softwarePtr.get() && !instrumentConfiguration.softwarePtr->empty())
210  child()("softwareRef: " + instrumentConfiguration.softwarePtr->id);
211  return *this;
212  }
213 
214  TextWriter& operator()(const ComponentList& componentList)
215  {
216  (*this)("componentList:");
217  for (size_t i=0; i < componentList.size(); ++i)
218  child()(componentList[i]);
219  return *this;
220  }
221 
222  TextWriter& operator()(const Component& component)
223  {
224  switch(component.type)
225  {
227  (*this)("source: ");
228  break;
230  (*this)("analyzer: ");
231  break;
233  (*this)("detector: ");
234  break;
235  default:
236  break;
237  }
238  child()
239  ("order: " + boost::lexical_cast<std::string>(component.order))
240  (static_cast<const ParamContainer&>(component));
241  return *this;
242  }
243 
244  TextWriter& operator()(const Software& software)
245  {
246  (*this)("software:");
247  child()
248  ("id: " + software.id)
249  ("version: " + software.version)
250  (static_cast<const ParamContainer&>(software));
251  return *this;
252  }
253 
254  TextWriter& operator()(const ProcessingMethod& processingMethod)
255  {
256  (*this)("processingMethod:");
257  child()
258  ("order: " + boost::lexical_cast<std::string>(processingMethod.order));
259  if (processingMethod.softwarePtr.get() && !processingMethod.softwarePtr->empty())
260  child()("softwareRef: " + processingMethod.softwarePtr->id);
261  child()
262  (static_cast<const ParamContainer&>(processingMethod));
263  return *this;
264  }
265 
266  TextWriter& operator()(const DataProcessing& dp)
267  {
268  (*this)("dataProcessing:");
269  child()
270  ("id: " + dp.id);
271  for_each(dp.processingMethods.begin(), dp.processingMethods.end(), child());
272  return *this;
273  }
274 
275  TextWriter& operator()(const Target& target)
276  {
277  (*this)("target:");
278  child()(static_cast<const ParamContainer&>(target));
279  return *this;
280  }
281 
282  TextWriter& operator()(const ScanSettings& as)
283  {
284  (*this)("scanSettings:");
285  child()
286  ("id: " + as.id);
287  for_each(as.targets.begin(), as.targets.end(), child());
288  child()("sourceFileList: ", as.sourceFilePtrs);
289  return *this;
290  }
291 
292  TextWriter& operator()(const Run& run, bool metadata_only=false)
293  {
294  (*this)("run:");
295  child()("id: " + run.id);
296  if (run.defaultInstrumentConfigurationPtr.get())
297  child()("defaultInstrumentConfigurationRef: " + run.defaultInstrumentConfigurationPtr->id);
298  if (run.samplePtr.get())
299  child()("sampleRef: " + run.samplePtr->id);
300  if (!run.startTimeStamp.empty())
301  child()("startTimeStamp: " + run.startTimeStamp);
302  child()(static_cast<const ParamContainer&>(run));
303  if (run.defaultSourceFilePtr.get())
304  child()("defaultSourceFileRef: " + run.defaultSourceFilePtr->id);
305  if (run.spectrumListPtr.get())
306  child()(run.spectrumListPtr, metadata_only);
307  if (run.chromatogramListPtr.get())
308  child()(run.chromatogramListPtr, metadata_only);
309  return *this;
310  }
311 
312  TextWriter& operator()(const SpectrumList& spectrumList, bool metadata_only=false)
313  {
314  std::string text("spectrumList (" + boost::lexical_cast<std::string>(spectrumList.size()) + " spectra)");
315  if (!metadata_only)
316  text += ":";
317 
318  (*this)(text);
319 
320  if (spectrumList.dataProcessingPtr().get())
321  child()(*spectrumList.dataProcessingPtr());
322 
323  if (!metadata_only)
324  for (size_t index=0; index<spectrumList.size(); ++index)
325  child()
326  (*spectrumList.spectrum(index, true));
327  return *this;
328  }
329 
330  TextWriter& operator()(const SpectrumListPtr& p, bool metadata_only=false)
331  {
332  return p.get() ? (*this)(*p, metadata_only) : *this;
333  }
334 
335  TextWriter& operator()(const ChromatogramList& chromatogramList, bool metadata_only=false)
336  {
337  std::string text("chromatogramList (" + boost::lexical_cast<std::string>(chromatogramList.size()) + " chromatograms)");
338  if (!metadata_only)
339  text += ":";
340 
341  (*this)(text);
342 
343  if (chromatogramList.dataProcessingPtr().get())
344  child()(*chromatogramList.dataProcessingPtr());
345 
346  if (!metadata_only)
347  for (size_t index=0; index<chromatogramList.size(); ++index)
348  child()
349  (*chromatogramList.chromatogram(index, true));
350  return *this;
351  }
352 
353  TextWriter& operator()(const ChromatogramListPtr& p, bool metadata_only=false)
354  {
355  return p.get() ? (*this)(*p, metadata_only) : *this;
356  }
357 
358  TextWriter& operator()(const Spectrum& spectrum)
359  {
360  (*this)("spectrum:");
361  child()
362  ("index: " + boost::lexical_cast<std::string>(spectrum.index))
363  ("id: " + spectrum.id);
364  if (!spectrum.spotID.empty())
365  child()("spotID: " + spectrum.spotID);
366  if (spectrum.sourceFilePtr.get())
367  child()(spectrum.sourceFilePtr);
368  child()
369  ("defaultArrayLength: " + boost::lexical_cast<std::string>(spectrum.defaultArrayLength))
370  (spectrum.dataProcessingPtr)
371  (static_cast<const ParamContainer&>(spectrum));
372  if (!spectrum.scanList.empty())
373  child()(spectrum.scanList);
374  if (!spectrum.precursors.empty())
375  child()("precursorList: ", spectrum.precursors);
376  for_each(spectrum.binaryDataArrayPtrs.begin(), spectrum.binaryDataArrayPtrs.end(), child());
377  return *this;
378  }
379 
380  TextWriter& operator()(const Chromatogram& chromatogram)
381  {
382  (*this)("chromatogram:");
383  child()
384  ("index: " + boost::lexical_cast<std::string>(chromatogram.index))
385  ("id: " + chromatogram.id)
386  ("defaultArrayLength: " + boost::lexical_cast<std::string>(chromatogram.defaultArrayLength))
387  (chromatogram.dataProcessingPtr)
388  (static_cast<const ParamContainer&>(chromatogram));
389  for_each(chromatogram.binaryDataArrayPtrs.begin(), chromatogram.binaryDataArrayPtrs.end(), child());
390  return *this;
391  }
392 
393  TextWriter& operator()(const Scan& scan)
394  {
395  (*this)("scan:");
396  if (scan.instrumentConfigurationPtr.get()) child()(*scan.instrumentConfigurationPtr);
397  child()(static_cast<const ParamContainer&>(scan));
398  if (!scan.scanWindows.empty())
399  child()("scanWindowList: ", scan.scanWindows);
400  return *this;
401  }
402 
403  TextWriter& operator()(const ScanWindow& window)
404  {
405  (*this)("scanWindow:");
406  for_each(window.cvParams.begin(), window.cvParams.end(), child());
407  return *this;
408  }
409 
410  TextWriter& operator()(const BinaryDataArrayPtr& p)
411  {
412  if (!p.get() || p->empty()) return *this;
413 
414  std::stringstream oss;
415  oss << "[" << boost::lexical_cast<std::string>(p->data.size()) << "] ";
416  oss.precision(12);
417  for (size_t i=0; i < arrayExampleCount_ && i < p->data.size(); i++)
418  oss << p->data[i] << " ";
419  if (p->data.size() > arrayExampleCount_)
420  oss << "...";
421 
422  (*this)("binaryDataArray:");
423  child() (static_cast<const ParamContainer&>(*p));
424  if (p->dataProcessingPtr.get() && !p->dataProcessingPtr->empty())
425  child()(p->dataProcessingPtr);
426  if (!p->data.empty())
427  child()("binary: " + oss.str());
428  return *this;
429  }
430 
431  TextWriter& operator()(const SelectedIon& selectedIon)
432  {
433  (*this)("selectedIon:");
434  child()(static_cast<const ParamContainer&>(selectedIon));
435  return *this;
436  }
437 
438  TextWriter& operator()(const Precursor& precursor)
439  {
440  (*this)("precursor:");
441  child()
442  ("spectrumRef: " + precursor.spectrumID)
443  (static_cast<const ParamContainer&>(precursor));
444 
445  if (!precursor.isolationWindow.empty())
446  {
447  child()("isolationWindow:");
448  child().child()(precursor.isolationWindow);
449  }
450 
451  if (!precursor.selectedIons.empty())
452  {
453  child()("selectedIons:", precursor.selectedIons);
454  }
455 
456  if (!precursor.activation.empty())
457  {
458  child()("activation:");
459  child().child()(precursor.activation);
460  }
461 
462  return *this;
463  }
464 
465  TextWriter& operator()(const Product& product)
466  {
467  (*this)("product:");
468 
469  if (!product.isolationWindow.empty())
470  {
471  child()("isolationWindow:");
472  child().child()(product.isolationWindow);
473  }
474 
475  return *this;
476  }
477 
478  TextWriter& operator()(const ScanList& scanList)
479  {
480  (*this)
481  (static_cast<const ParamContainer&>(scanList))
482  ("scanList:", scanList.scans);
483  return *this;
484  }
485 
486  // if no other overload matches, assume the object is a shared_ptr of a valid overloaded type
487  template<typename object_type>
488  TextWriter& operator()(const boost::shared_ptr<object_type>& p)
489  {
490  return p.get() ? (*this)(*p) : *this;
491  }
492 
493 
494  private:
495  std::ostream& os_;
496  int depth_;
498  std::string indent_;
499 };
500 
501 
502 } // namespace msdata
503 } // namespace pwiz
504 
505 
506 #endif // _TEXTWRITER_HPP_
507