ProteoWizard
TextWriter.hpp
Go to the documentation of this file.
1 //
2 // $Id: TextWriter.hpp 1605 2009-12-09 23:50:27Z chambm $
3 //
4 //
5 // Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6 //
7 // Copyright 2009 Vanderbilt University - Nashville, TN 37232
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 
23 #ifndef _TRADATA_TEXTWRITER_HPP_
24 #define _TRADATA_TEXTWRITER_HPP_
25 
26 
28 #include "TraData.hpp"
29 #include "boost/lexical_cast.hpp"
30 #include <iostream>
31 #include <string>
32 #include <vector>
33 
34 
35 namespace pwiz {
36 namespace tradata {
37 
38 
40 using std::string;
41 
42 
44 {
45  public:
46 
47  TextWriter(std::ostream& os, int depth = 0)
48  : os_(os), depth_(depth), indent_(depth*2, ' ')
49  {}
50 
51  TextWriter child() {return TextWriter(os_, depth_+1);}
52 
53  TextWriter& operator()(const std::string& text)
54  {
55  os_ << indent_ << text << std::endl;
56  return *this;
57  }
58 
59  TextWriter& operator()(const CVParam& cvParam)
60  {
61  os_ << indent_ << "cvParam: " << cvTermInfo(cvParam.cvid).name;
62  if (!cvParam.value.empty())
63  os_ << ", " << cvParam.value;
64  if (cvParam.units != CVID_Unknown)
65  os_ << ", " << cvParam.unitsName();
66  os_ << std::endl;
67  return *this;
68  }
69 
70  TextWriter& operator()(const UserParam& userParam)
71  {
72  os_ << indent_ << "userParam: " << userParam.name;
73  if (!userParam.value.empty()) os_ << ", " << userParam.value;
74  if (!userParam.type.empty()) os_ << ", " << userParam.type;
75  if (userParam.units != CVID_Unknown) os_ << ", " << cvTermInfo(userParam.units).name;
76  os_ << std::endl;
77  return *this;
78  }
79 
80  template<typename object_type>
81  TextWriter& operator()(const std::string& label, const std::vector<object_type>& v)
82  {
83  (*this)(label);
84  for_each(v.begin(), v.end(), child());
85  return *this;
86  }
87 
88  template<typename object_type>
89  TextWriter& operator()(const std::string& label, const object_type& v)
90  {
91  (*this)(label)(boost::lexical_cast<std::string>(v));
92  return *this;
93  }
94 
95 
96  TextWriter& operator()(const TraData& msd)
97  {
98  (*this)("tradata:");
99  child()("version: " + msd.version());
100  if (!msd.cvs.empty())
101  child()("cvList: ", msd.cvs);
102  if (!msd.contactPtrs.empty())
103  child()("contactList: ", msd.contactPtrs);
104  if (!msd.publications.empty())
105  child()("publicationList: ", msd.publications);
106  if (!msd.instrumentPtrs.empty())
107  child()("instrumentList: ", msd.instrumentPtrs);
108  if (!msd.softwarePtrs.empty())
109  child()("softwareList: ", msd.softwarePtrs);
110  if (!msd.proteinPtrs.empty())
111  child()("proteinList: ", msd.proteinPtrs);
112  if (!msd.peptidePtrs.empty())
113  child()("peptideList: ", msd.peptidePtrs);
114  if (!msd.compoundPtrs.empty())
115  child()("compoundList: ", msd.compoundPtrs);
116  if (!msd.transitions.empty())
117  child()("transitionList: ", msd.transitions);
118  if (!msd.targets.empty())
119  child()(msd.targets);
120 
121  return *this;
122  }
123 
124  TextWriter& operator()(const CV& cv)
125  {
126  (*this)("cv:");
127  child()
128  ("id: " + cv.id)
129  ("fullName: " + cv.fullName)
130  ("version: " + cv.version)
131  ("URI: " + cv.URI);
132  return *this;
133  }
134 
135  TextWriter& operator()(const ParamContainer& paramContainer)
136  {
137  for_each(paramContainer.cvParams.begin(), paramContainer.cvParams.end(), *this);
138  for_each(paramContainer.userParams.begin(), paramContainer.userParams.end(), *this);
139  return *this;
140  }
141 
142  TextWriter& operator()(const Publication& publication)
143  {
144  (*this)("publication:");
145  child()
146  ("id: " + publication.id)
147  (static_cast<const ParamContainer&>(publication));
148  return *this;
149  }
150 
151  TextWriter& operator()(const Software& software)
152  {
153  (*this)("software:");
154  child()
155  ("id: " + software.id)
156  ("version: " + software.version)
157  (static_cast<const ParamContainer&>(software));
158  return *this;
159  }
160 
161  TextWriter& operator()(const Contact& contact)
162  {
163  (*this)("contact:");
164  child()(static_cast<const ParamContainer&>(contact));
165  return *this;
166  }
167 
168  TextWriter& operator()(const RetentionTime& retentionTime)
169  {
170  (*this)("retentionTime:");
171  child()(static_cast<const ParamContainer&>(retentionTime));
172  if (retentionTime.softwarePtr.get() &&
173  !retentionTime.softwarePtr->empty())
174  child()("softwareRef: " + retentionTime.softwarePtr->id);
175  return *this;
176  }
177 
178  TextWriter& operator()(const Prediction& prediction)
179  {
180  (*this)("prediction:");
181  child()(static_cast<const ParamContainer&>(prediction));
182  return *this;
183  }
184 
185  TextWriter& operator()(const Evidence& evidence)
186  {
187  (*this)("evidence:");
188  child()(static_cast<const ParamContainer&>(evidence));
189  return *this;
190  }
191 
192  TextWriter& operator()(const Validation& validation)
193  {
194  (*this)("validation:");
195  child()(static_cast<const ParamContainer&>(validation));
196  return *this;
197  }
198 
199  TextWriter& operator()(const Protein& protein)
200  {
201  (*this)("protein:");
202  child()("id: " + protein.id)
203  ("sequence: " + protein.sequence);
204  child()(static_cast<const ParamContainer&>(protein));
205  return *this;
206  }
207 
209  {
210  (*this)("modification:");
211  child()("location: ", lexical_cast<string>(modification.location))
212  ("monoisotopicMassDelta: " + lexical_cast<string>(modification.monoisotopicMassDelta))
213  ("averageMassDelta: " + lexical_cast<string>(modification.averageMassDelta));
214  child()(static_cast<const ParamContainer&>(modification));
215  return *this;
216  }
217 
218  TextWriter& operator()(const Peptide& peptide)
219  {
220  (*this)("peptide:");
221  child()("id: " + peptide.id)
222  ("sequence: " + peptide.sequence)
223  (peptide.evidence);
224 
225  if (!peptide.proteinPtrs.empty())
226  child()("proteinRefs:", peptide.proteinPtrs);
227  if (!peptide.modifications.empty())
228  child()("modifications:", peptide.modifications);
229  if (!peptide.retentionTimes.empty())
230  child()("retentionTimes:", peptide.retentionTimes);
231 
232  child()(static_cast<const ParamContainer&>(peptide));
233  return *this;
234  }
235 
236  TextWriter& operator()(const Compound& compound)
237  {
238  (*this)("compound:");
239  child()("id: " + compound.id)
240  ("retentionTimes:", compound.retentionTimes);
241  child()(static_cast<const ParamContainer&>(compound));
242  return *this;
243  }
244 
245  TextWriter& operator()(const Precursor& precursor)
246  {
247  (*this)("precursor:");
248  child()(static_cast<const ParamContainer&>(precursor));
249  return *this;
250  }
251 
252  TextWriter& operator()(const Product& product)
253  {
254  (*this)("product:");
255  child()(static_cast<const ParamContainer&>(product));
256  return *this;
257  }
258 
259  TextWriter& operator()(const Transition& transition)
260  {
261  (*this)("transition:");
262  child()("id: ", transition.id);
263  if (!transition.precursor.empty())
264  child()(transition.precursor);
265  if (!transition.product.empty())
266  child()(transition.product);
267  if (!transition.prediction.empty())
268  child()(transition.prediction);
269  if (!transition.interpretationList.empty())
270  child()("interpretationList: ", transition.interpretationList);
271  if (!transition.configurationList.empty())
272  child()("configurationList: ", transition.configurationList);
273  if (transition.peptidePtr.get() && !transition.peptidePtr->empty())
274  child()("peptideRef: " + transition.peptidePtr->id);
275  if (transition.compoundPtr.get() && !transition.compoundPtr->empty())
276  child()("compoundRef: " + transition.compoundPtr->id);
277  return *this;
278  }
279 
280  TextWriter& operator()(const Target& target)
281  {
282  (*this)("target:");
283  child()("id: ", target.id);
284  if (!target.precursor.empty())
285  child()(target.precursor);
286  if (!target.configurationList.empty())
287  child()("configurationList: ", target.configurationList);
288  if (target.peptidePtr.get() && !target.peptidePtr->empty())
289  child()("peptideRef: " + target.peptidePtr->id);
290  if (target.compoundPtr.get() && !target.compoundPtr->empty())
291  child()("compoundRef: " + target.compoundPtr->id);
292  return *this;
293  }
294 
295  TextWriter& operator()(const TargetList& targetList)
296  {
297  (*this)("targetList:");
298  child()(static_cast<const ParamContainer&>(targetList));
299  if (!targetList.targetExcludeList.empty())
300  child()("targetExcludeList: ", targetList.targetExcludeList);
301  if (!targetList.targetIncludeList.empty())
302  child()("targetIncludeList: ", targetList.targetIncludeList);
303  return *this;
304  }
305 
306  // if no other overload matches, assume the object is a shared_ptr of a valid overloaded type
307  template<typename object_type>
308  TextWriter& operator()(const boost::shared_ptr<object_type>& p)
309  {
310  return p.get() ? (*this)(*p) : *this;
311  }
312 
313  private:
314  std::ostream& os_;
315  int depth_;
316  std::string indent_;
317 };
318 
319 
320 } // namespace tradata
321 } // namespace pwiz
322 
323 
324 #endif // _TRADATA_TEXTWRITER_HPP_
325