vdj_pipe
pipeline for processing DNA sequence data
value_map.hpp
Go to the documentation of this file.
1 
7 #ifndef VALUE_MAP_HPP_
8 #define VALUE_MAP_HPP_
9 #include "boost/assert.hpp"
10 #include "boost/shared_ptr.hpp"
11 
13 #include "vdj_pipe/exception.hpp"
14 #include "vdj_pipe/object_ids.hpp"
18 
19 namespace vdj_pipe{
20 
23 class Value_map {
25  typedef boost::shared_ptr<map_type> map_ptr;
26  enum {
29  seq_fn_i = 3,
31  };
32 
33 public:
35  typedef iterator const_iterator;
36  struct Err : public base_exception{};
37 
39  : map_(new map_type())
40  {
41  const std::string rc = "read_count";
42  insert_new_name(rc);
43  BOOST_ASSERT(value_id(rc)() == read_count_i );
44 
45  const std::string frc = "file_read_count";
46  insert_new_name(frc);
47  BOOST_ASSERT(value_id(frc)() == file_read_count_i );
48 
49  const std::string sfn = "seq_file_name";
50  insert_new_name(sfn);
51  BOOST_ASSERT(value_id(sfn)() == seq_fn_i );
52 
53  const std::string ri = "read_id";
54  insert_new_name(ri);
55  BOOST_ASSERT(value_id(ri)() == read_id_i );
56 
59  }
60 
61  iterator begin() const {return map_->begin();}
62  iterator end() const {return map_->end();}
63  std::size_t size() const {return map_->size();}
64 
65  void clear_values() {
66  for(iterator i = Val_id(read_id_i); i != end(); ++i) {
67  (*this)[*i] = value_variant();
68  }
69  }
70 
71  std::string const& name(const Val_id vid) const {
72  return map_->name(vid);
73  }
74 
75  Val_id insert_name(std::string const& name) {
76  return map_->insert_name(name);
77  }
78 
79  Val_id insert_new_name(std::string const& name) {
80  return map_->insert_new_name(name);
81  }
82 
83  Val_id const* find_id(std::string const& name) const {
84  return map_->find_id(name);
85  }
86 
87  Val_id value_id(std::string const& name) const {
88  return map_->value_id(name);
89  }
90 
91  value_variant const& operator[](const Val_id vid) const {
92  return (*map_)[vid];
93  }
94 
95  value_variant& operator[](const Val_id vid) {
96  return (*map_)[vid];
97  }
98 
99  long read_count() const {
100  return value<long>(Val_id(read_count_i));
101  }
102 
104  (*this)[Val_id(read_count_i)] = (long)0;
105  }
106 
108  ++boost::get<long>((*map_)[Val_id(read_count_i)]);
109  }
110 
111  long file_read_count() const {
112  return value<long>(Val_id(file_read_count_i));
113  }
114 
116  (*this)[Val_id(file_read_count_i)] = (long)0;
117  }
118 
120  ++boost::get<long>((*map_)[Val_id(file_read_count_i)]);
121  }
122 
123  std::string const& seq_file_name() const {
124  return value<std::string>(Val_id(seq_fn_i));
125  }
126 
127  void seq_file_name(std::string const& fn) {
128  (*this)[Val_id(seq_fn_i)] = fn;
129  }
130 
131  std::string const& read_id_nothrow() const {
132  return str_nothrow(Val_id(read_id_i));
133  }
134 
135  std::string const& read_id() const {
136  return value<std::string>(Val_id(read_id_i));
137  }
138 
139  void read_id(std::string const& id) {
140  (*this)[Val_id(read_id_i)] = id;
141  }
142 
143  template<typename T> T const& value(std::string const& name) const {
144  const Val_id id = value_id(name);
145  if( ! id ) BOOST_THROW_EXCEPTION(
146  Err()
147  << Err::msg_t("unknown value name")
148  << Err::str1_t(sanitize(name))
149  );
150  return value<T>(id);
151  }
152 
153  template<typename T> T const* value_ptr(std::string const& name) const {
154  const Val_id id = value_id(name);
155  if( ! id ) BOOST_THROW_EXCEPTION(
156  Err()
157  << Err::msg_t("unknown value name")
158  << Err::str1_t(sanitize(name))
159  );
160  return value_ptr<T>(id);
161  }
162 
163  template<typename T> T const& value(const Val_id id) const {
164  try{
165  return boost::get<T>((*map_)[id]);
166  }catch(std::exception const&) {
167  BOOST_THROW_EXCEPTION(
168  Err()
169  << Err::msg_t("wrong value type")
171  << Err::str2_t(sanitize(name(id)))
172  << Err::nested_t(boost::current_exception())
173  );
174  }
175  }
176 
177  template<typename T> T const* value_ptr(const Val_id id) const {
178  return boost::get<T>(&(*map_)[id]);
179  }
180 
181  std::string const&
182  str_nothrow(const Val_id id) const {
183  if( std::string const* id_ptr = boost::get<std::string>(&(*map_)[id]) )
184  return *id_ptr;
185  static const std::string id_str = "not_available";
186  return id_str;
187  }
188 
189 protected:
190 
191  std::string sequence(
192  const Val_id seq_id,
193  const Val_id trim_id,
194  const bool trimmed,
195  const bool reverse_compl
196  ) const {
197  std::string const& seq = value<std::string>(seq_id);
198  return transform(
199  seq,
200  trimmed ?
201  value<sequence_interval>(trim_id) :
202  sequence_interval(0U, seq.size()),
203  reverse_compl
204  );
205  }
206 
208  const Val_id qual_id,
209  const Val_id trim_id,
210  const bool trimmed,
211  const bool reverse_compl
212  ) const {
213  Qual_record::quality const& q = value<Qual_record::quality>(qual_id);
214  std::size_t n1 = 0;
215  std::size_t n2 = q.size();
216  if( trimmed ) {
217  const sequence_interval si = value<sequence_interval>(trim_id);
218  n1 = lower(si);
219  n2 = upper(si);
220  }
221  if( n1 > q.size() || n2 > q.size() ) BOOST_THROW_EXCEPTION(
222  Err()
223  << Err::msg_t("sequence interval and quality score mismatch")
224  << Err::int1_t(std::max(n1, n2))
225  << Err::int2_t(q.size())
226  );
227 
228  Qual_record::quality q1(n2 - n1);
229  if( reverse_compl ) {
230  for(std::size_t i = 0, j = n2-1; i != q1.size(); ++i, --j) q1[i] = q[j];
231  } else {
232  for( std::size_t i = 0, j = n1; i != q1.size(); ++i, ++j) q1[i] = q[j];
233  }
234  return q1;
235  }
236 
237 private:
238  map_ptr map_;
239 };
240 
241 }//namespace vdj_pipe
242 #endif /* VALUE_MAP_HPP_ */
void read_id(std::string const &id)
Definition: value_map.hpp:139
long read_count() const
Definition: value_map.hpp:99
Definition: value_map.hpp:30
void read_count_reset()
Definition: value_map.hpp:103
std::string const & read_id_nothrow() const
Definition: value_map.hpp:131
T const * value_ptr(std::string const &name) const
Definition: value_map.hpp:153
map_type::iterator iterator
Definition: value_map.hpp:34
Definition: value_map.hpp:28
value_variant const & operator[](const Val_id vid) const
Definition: value_map.hpp:91
boost::shared_ptr< map_type > map_ptr
Definition: value_map.hpp:25
Definition: sequence_record.hpp:35
long file_read_count() const
Definition: value_map.hpp:111
std::string const & variable_type_str(const int which)
Definition: value_variant.hpp:54
std::string const & str_nothrow(const Val_id id) const
Definition: value_map.hpp:182
Val_id value_id(std::string const &name) const
Definition: value_map.hpp:87
void clear_values()
Definition: value_map.hpp:65
Definition: value_map.hpp:29
iterator begin() const
Definition: value_map.hpp:61
Store values mapped against name strings and value IDs.
Definition: named_value_map.hpp:22
std::string const & seq_file_name() const
Definition: value_map.hpp:123
boost::error_info< struct errinfo_int1_, int > int1_t
Definition: exception.hpp:28
void seq_file_name(std::string const &fn)
Definition: value_map.hpp:127
std::string const & name(const Val_id vid) const
Definition: value_map.hpp:71
Definition: value_map.hpp:36
Main namespace of vdj_pipe library.
Definition: keywords_variable.hpp:11
boost::errinfo_nested_exception nested_t
Definition: exception.hpp:32
Val_id insert_new_name(std::string const &name)
Definition: value_map.hpp:79
Qual_record::quality quality(const Val_id qual_id, const Val_id trim_id, const bool trimmed, const bool reverse_compl) const
Definition: value_map.hpp:207
boost::error_info< struct errinfo_str2_, std::string > str2_t
Definition: exception.hpp:26
T const * value_ptr(const Val_id id) const
Definition: value_map.hpp:177
boost::error_info< struct errinfo_int2_, int > int2_t
Definition: exception.hpp:29
Val_id insert_name(std::string const &name)
Definition: value_map.hpp:75
value_variant & operator[](const Val_id vid)
Definition: value_map.hpp:95
boost::error_info< struct errinfo_str1_, std::string > str1_t
Definition: exception.hpp:25
void read_count_incr()
Definition: value_map.hpp:107
void file_read_count_reset()
Definition: value_map.hpp:115
iterator const_iterator
Definition: value_map.hpp:35
Definition: value_map.hpp:27
boost::variant< Blank, bool, long, double, std::string, sequence_interval, Qual_record::quality > value_variant
Definition: value_variant.hpp:50
std::string const & read_id() const
Definition: value_map.hpp:135
Value_map()
Definition: value_map.hpp:38
std::size_t size() const
Definition: value_map.hpp:63
map_ptr map_
Definition: value_map.hpp:238
boost::numeric::interval< int, boost::numeric::interval_lib::policies< boost::numeric::interval_lib::rounded_math< int >, detail::Interval_checking_policy< int > > > sequence_interval
Definition: sequence_interval.hpp:40
T const & value(const Val_id id) const
Definition: value_map.hpp:163
Definition: exception.hpp:23
Definition: id_iterator.hpp:16
boost::error_info< struct errinfo_message_, std::string > msg_t
Definition: exception.hpp:24
detail::Named_value_map< Val_id, value_variant > map_type
Definition: value_map.hpp:24
std::string sanitize(const char c)
Definition: sanitize_string.cpp:53
std::string transform(const boost::string_ref seq, sequence_interval const &si, const bool reverse)
Reverse-complement (optionally) a portion of a sequence.
Definition: sequence_transform.hpp:34
void file_read_count_incr()
Definition: value_map.hpp:119
Val_id const * find_id(std::string const &name) const
Definition: value_map.hpp:83
Store values mapped against name strings and value IDs.
Definition: value_map.hpp:23
T const & value(std::string const &name) const
Definition: value_map.hpp:143
iterator end() const
Definition: value_map.hpp:62
std::string sequence(const Val_id seq_id, const Val_id trim_id, const bool trimmed, const bool reverse_compl) const
Definition: value_map.hpp:191