ProteoWizard
TextWriter.hpp
Go to the documentation of this file.
1 //
2 // $Id: TextWriter.hpp 1623 2009-12-17 20:58:35Z 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 _PROTEOME_TEXTWRITER_HPP_
24 #define _PROTEOME_TEXTWRITER_HPP_
25 
26 
28 #include "ProteomeData.hpp"
29 #include "boost/lexical_cast.hpp"
30 #include <iostream>
31 #include <string>
32 #include <vector>
33 
34 
35 namespace pwiz {
36 namespace proteome {
37 
38 
40 {
41  public:
42 
43  TextWriter(std::ostream& os, int depth = 0)
44  : os_(os), depth_(depth), indent_(depth*2, ' ')
45  {}
46 
47  TextWriter child() {return TextWriter(os_, depth_+1);}
48 
49  TextWriter& operator()(const std::string& text)
50  {
51  os_ << indent_ << text << std::endl;
52  return *this;
53  }
54 
55  template<typename object_type>
56  TextWriter& operator()(const std::string& label, const std::vector<object_type>& v)
57  {
58  (*this)(label);
59  for_each(v.begin(), v.end(), child());
60  return *this;
61  }
62 
63 
64  TextWriter& operator()(const ProteomeData& pd, bool metadata_only=false)
65  {
66  (*this)("ProteomeData:");
67  child()
68  ("id: " + pd.id);
69 
70  if (pd.proteinListPtr.get())
71  child()(*pd.proteinListPtr, metadata_only);
72 
73  return *this;
74  }
75 
76  TextWriter& operator()(const Protein& p)
77  {
78  (*this)("protein:");
79  child()
80  ("id: " + p.id)
81  ("index: " + p.index)
82  ("description: " + p.description)
83  ("sequence: " + p.sequence().substr(0, 10));
84  return *this;
85  }
86 
87  TextWriter& operator()(const ProteinList& proteinList, bool metadata_only=false)
88  {
89  std::string text("proteinList (" + boost::lexical_cast<std::string>(proteinList.size()) + " proteins)");
90  if (!metadata_only)
91  text += ":";
92 
93  (*this)(text);
94 
95  if (!metadata_only)
96  for (size_t index = 0; index < proteinList.size(); ++index)
97  child()
98  (*proteinList.protein(index, true));
99  return *this;
100  }
101 
102  // if no other overload matches, assume the object is a shared_ptr of a valid overloaded type
103  template<typename object_type>
104  TextWriter& operator()(const boost::shared_ptr<object_type>& p)
105  {
106  return p.get() ? (*this)(*p) : *this;
107  }
108 
109 
110  private:
111  std::ostream& os_;
112  int depth_;
113  std::string indent_;
114 };
115 
116 
117 } // namespace proteome
118 } // namespace pwiz
119 
120 
121 #endif // _PROTEOME_TEXTWRITER_HPP_