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 {
27  read_count_i = 1,
28  file_read_count_i = 2,
29  seq_fn_i = 3,
30  read_id_i = 4
31  };
32 
33 public:
35  typedef iterator const_iterator;
36  struct Err : public base_exception{};
37 
38  Value_map()
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 
57  read_count_reset();
58  file_read_count_reset();
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 
103  void read_count_reset() {
104  (*this)[Val_id(read_count_i)] = (long)0;
105  }
106 
107  void read_count_incr() {
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 
115  void file_read_count_reset() {
116  (*this)[Val_id(file_read_count_i)] = (long)0;
117  }
118 
119  void file_read_count_incr() {
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")
170  << Err::str1_t(variable_type_str((*map_)[id]))
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 
207  Qual_record::quality quality(
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_ */
Definition: sequence_record.hpp:35
Store values mapped against name strings and value IDs.
Definition: named_value_map.hpp:22
Definition: value_map.hpp:36
Main namespace of vdj_pipe library.
Definition: sequence_file.hpp:14
Definition: exception.hpp:23
Definition: id_iterator.hpp:16
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
Store values mapped against name strings and value IDs.
Definition: value_map.hpp:23