LocARNA-2.0.0
anchor_constraints.hh
1 #ifndef LOCARNA_ANCHOR_CONSTRAINTS_HH
2 #define LOCARNA_ANCHOR_CONSTRAINTS_HH
3 
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7 
8 #include <string>
9 #include <vector>
10 #include <map>
11 
12 #include <exception>
13 
14 #include <iosfwd>
15 
16 namespace LocARNA {
17 
67  public:
68  typedef size_t size_type;
69  typedef std::pair<size_type, size_type> size_pair_t;
70 
71  typedef size_pair_t range_t;
72 
73  // ------------------------------------------------------------
74  // constructors
75 
111  std::vector<std::string> seqCA,
112  size_type lenB,
113  std::vector<std::string> seqCB,
114  bool strict);
115 
129  const std::string &seqCA,
130  size_type lenB,
131  const std::string &seqCB,
132  bool strict);
133 
134  // -----------------------------------------------------------
135  // asking for constraint information
136 
157  bool
159  assert(i >= 1);
160  assert(i < ar_.size());
161  return ar_[i].first <= j && j <= ar_[i].second;
162  }
163 
183  bool
185  assert(i<=lenA_);
186  assert(j<=lenB_);
187 
188  if (is_anchored_a(i)) return false;
189 
190  if (strict_) {
191  size_type i0 = max_named_leq_a_[i];
192  size_type j0 = min_named_geq_b_[j+1];
193  size_type i1 = min_named_geq_a_[i];
194  size_type j1 = max_named_leq_b_[j];
195 
196  return
197  names_a_[ i0 ] < names_b_[ j0 ]
198  &&
199  names_a_[ i1 ] > names_b_[ j1 ];
200  } else {
201  return !conflicts_anchor(i,j);
202  }
203  }
204 
210  bool
212  assert(1<=i); assert(i<=lenA_);
213  assert(j<=lenB_);
214  return adr_[i].first<=j && j<=adr_[i].second;
215  }
216 
222  bool
224  assert(i<=lenA_);
225  assert(j<=lenB_);
226 
227  if (is_anchored_b(j)) return false;
228 
229  if (strict_) {
230  size_type i0 = max_named_leq_a_[i];
231  size_type j0 = min_named_geq_b_[j];
232  size_type i1 = min_named_geq_a_[i+1];
233  size_type j1 = max_named_leq_b_[j];
234 
235  return
236  names_a_[ i0 ] < names_b_[ j0 ]
237  &&
238  names_a_[ i1 ] > names_b_[ j1 ];
239  } else {
240  return !conflicts_anchor(i,j);
241  }
242  }
243 
249  bool
251  assert(i<=lenA_);
252  assert(1<=j); assert(j<=lenB_);
253  return air_[j].first<=i && i<=air_[j].second;
254  }
255 
257  std::string
259  return names_a_[i];
260  }
261 
263  std::string
265  return names_b_[j];
266  }
267 
269  size_type
270  name_size() const {
271  return name_size_;
272  };
273 
275  bool
276  empty() const {
277  return name_size() == 0;
278  }
279 
288  for (size_type i = lenA_; i >= 1; --i) {
289  if (anchors_a_[i] > 0)
290  return size_pair_t(i, anchors_a_[i]);
291  }
292  return size_pair_t(0, 0);
293  }
294 
302  leftmost_anchor() const {
303  for (size_type i = 0; i <= lenA_; i++) {
304  if (anchors_a_[i] > 0)
305  return size_pair_t(i, anchors_a_[i]);
306  }
307  return size_pair_t(lenA_ + 1, lenB_ + 1);
308  }
309 
316  bool
318  return is_anchored(lenA_,anchors_a_,i);
319  };
320 
325  bool
327  return is_anchored(lenB_,anchors_b_,i);
328  };
329 
335  bool
337  return is_named(lenA_,anchors_a_,i);
338  };
339 
344  bool
346  return is_named(lenB_,anchors_b_,i);
347  };
348 
349 
351  void
353 
354  private:
355  typedef std::map<std::string, size_type> name_tab_t;
356  typedef std::vector<int> int_vec_t;
357  typedef std::vector<size_type> size_vec_t;
358  typedef std::vector<std::string> name_vec_t;
359  typedef std::vector<range_t> range_vec_t;
360 
362  const bool strict_;
363 
365  size_type lenA_;
366 
368  size_type lenB_;
369 
378  int_vec_t anchors_a_;
379 
381  int_vec_t anchors_b_;
382 
392  range_vec_t ar_;
393 
395  range_vec_t adr_;
397  range_vec_t air_;
398 
400  name_vec_t names_a_;
402  name_vec_t names_b_;
403 
405  size_type name_size_;
406 
411  size_vec_t max_anchored_left_a_;
412 
417  size_vec_t min_anchored_right_a_;
418 
423  size_vec_t max_named_leq_a_;
424 
429  size_vec_t min_named_geq_a_;
430 
432  size_vec_t max_anchored_left_b_;
433 
435  size_vec_t min_anchored_right_b_;
436 
438  size_vec_t max_named_leq_b_;
439 
441  size_vec_t min_named_geq_b_;
442 
450  static
451  bool
452  is_named(size_type len, const int_vec_t &anchors, size_type i) {
453  assert(i<=len+1);
454  return i==0 || i==len+1 || anchors[i]!=0;
455  };
456 
457 
465  static
466  bool
467  is_anchored(size_type len, const int_vec_t &anchors, size_type i) {
468  assert(i<=len);
469  return i==0 || i==len+1 || anchors[i]>0;
470  }
471 
485  bool
486  conflicts_anchor(size_type i, size_type j) const {
487  return
488  !(static_cast<size_type>(anchors_a_[ max_anchored_left_a_[i] ]) < j
489  &&
490  j < static_cast<size_type>(anchors_a_[ min_anchored_right_a_[i] ])) ;
491  }
492 
505  bool
506  is_anchor(size_type i, size_type j) const {
507  return is_named_a(i) && static_cast<size_type>(anchors_a_[i]) == j;
508  }
509 
510  // ------------------------------------------------------------
511  // construction helper
512 
526  static void
527  transform_input(name_tab_t &nameTab,
528  size_type len,
529  const std::vector<std::string> &seq,
530  bool strict);
531 
552  void
553  init_tables(const name_tab_t &nameTabA, const name_tab_t &nameTabB);
554 
565  void static init_anchored_tables(size_type len,
566  const int_vec_t &anchors,
567  size_vec_t &max_anchored_left,
568  size_vec_t &min_anchored_right);
569 
580  void static init_named_tables(size_type len,
581  const int_vec_t &anchors,
582  size_vec_t &max_named_leq,
583  size_vec_t &min_named_geq);
584 
599  static void
600  init_anchors(int_vec_t &seq_tab,
601  name_vec_t &name_vec_tab,
602  const name_tab_t &nameTabA,
603  const name_tab_t &nameTabB);
604 
607  static bool
608  only_dont_care(const std::string &s);
609  };
610 }
611 
612 #endif // LOCARNA_ANCHOR_CONSTRAINTS_HH
Represents anchor constraints between two sequences.
Definition: anchor_constraints.hh:66
std::pair< size_type, size_type > size_pair_t
size pair
Definition: anchor_constraints.hh:69
std::string get_name_b(size_type j) const
get the name of position j in B
Definition: anchor_constraints.hh:264
bool allowed_del_unopt(size_type i, size_type j) const
is deletion allowed? (unoptimized)
Definition: anchor_constraints.hh:184
bool is_named_a(size_type i) const
Is position in A named?
Definition: anchor_constraints.hh:336
bool allowed_match(size_type i, size_type j) const
is match allowed
Definition: anchor_constraints.hh:158
size_type name_size() const
returns length/size of the names
Definition: anchor_constraints.hh:270
bool allowed_del(size_type i, size_type j) const
is deletion allowed?
Definition: anchor_constraints.hh:211
bool allowed_ins_unopt(size_type i, size_type j) const
is insertion allowed? (unoptimized)
Definition: anchor_constraints.hh:223
bool is_named_b(size_type i) const
Is position in B named?
Definition: anchor_constraints.hh:345
size_pair_t rightmost_anchor() const
Get rightmost anchor.
Definition: anchor_constraints.hh:287
size_pair_t leftmost_anchor() const
Get leftmost anchor.
Definition: anchor_constraints.hh:302
bool is_anchored_b(size_type i) const
Is position in B anchored?
Definition: anchor_constraints.hh:326
size_t size_type
size type
Definition: anchor_constraints.hh:68
bool allowed_ins(size_type i, size_type j) const
is insertion allowed?
Definition: anchor_constraints.hh:250
std::string get_name_a(size_type i) const
get the name of position i in A
Definition: anchor_constraints.hh:258
void print_debug()
write some debug information to stderr
size_pair_t range_t
type of range
Definition: anchor_constraints.hh:71
AnchorConstraints(size_type lenA, std::vector< std::string > seqCA, size_type lenB, std::vector< std::string > seqCB, bool strict)
Construct from sequence lengths and anchor names.
Definition: anchor_constraints.cc:12
bool empty() const
is the constraint declaration empty
Definition: anchor_constraints.hh:276
bool is_anchored_a(size_type i) const
Is position in A anchored?
Definition: anchor_constraints.hh:317
Definition: aligner.cc:15
size_t size_type
general size type
Definition: aux.hh:120