LocARNA-1.9.2
src/LocARNA/anchor_constraints.hh
00001 #ifndef LOCARNA_ANCHOR_CONSTRAINTS_HH
00002 #define LOCARNA_ANCHOR_CONSTRAINTS_HH
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #include <config.h>
00006 #endif
00007 
00008 #include <string>
00009 #include <vector>
00010 #include <map>
00011 
00012 #include <exception>
00013 
00014 #include <iosfwd>
00015 
00016 namespace LocARNA {
00017 
00066     class AnchorConstraints {
00067     public:
00068         typedef size_t size_type;                            
00069         typedef std::pair<size_type, size_type> size_pair_t; 
00070 
00071         typedef size_pair_t range_t; 
00072 
00073         // ------------------------------------------------------------
00074         // constructors
00075 
00110         AnchorConstraints(size_type lenA,
00111                           const std::vector<std::string> &seqCA,
00112                           size_type lenB,
00113                           const std::vector<std::string> &seqCB,
00114                           bool strict);
00115 
00128         AnchorConstraints(size_type lenA,
00129                           const std::string &seqCA,
00130                           size_type lenB,
00131                           const std::string &seqCB,
00132                           bool strict);
00133 
00134         // -----------------------------------------------------------
00135         // asking for constraint information
00136 
00157         bool
00158         allowed_match(size_type i, size_type j) const {
00159             assert(i >= 1);
00160             assert(i < ar_.size());
00161             return ar_[i].first <= j && j <= ar_[i].second;
00162         }
00163 
00183         bool
00184         allowed_del_unopt(size_type i, size_type j) const {
00185             assert(i<=lenA_);
00186             assert(j<=lenB_);
00187 
00188             if (is_anchored_a(i)) return false;
00189 
00190             if (strict_) {
00191                 size_type i0 = max_named_leq_a_[i];
00192                 size_type j0 = min_named_geq_b_[j+1];
00193                 size_type i1 = min_named_geq_a_[i];
00194                 size_type j1 = max_named_leq_b_[j];
00195 
00196                 return
00197                     names_a_[ i0 ] < names_b_[ j0 ]
00198                     &&
00199                     names_a_[ i1 ] > names_b_[ j1 ];
00200             } else {
00201                 return !conflicts_anchor(i,j);
00202             }
00203         }
00204 
00210         bool
00211         allowed_del(size_type i, size_type j) const {
00212             assert(1<=i); assert(i<=lenA_);
00213             assert(j<=lenB_);
00214             return adr_[i].first<=j && j<=adr_[i].second;
00215         }
00216 
00222         bool
00223         allowed_ins_unopt(size_type i, size_type j) const {
00224             assert(i<=lenA_);
00225             assert(j<=lenB_);
00226 
00227             if (is_anchored_b(j)) return false;
00228 
00229             if (strict_) {
00230                 size_type i0 = max_named_leq_a_[i];
00231                 size_type j0 = min_named_geq_b_[j];
00232                 size_type i1 = min_named_geq_a_[i+1];
00233                 size_type j1 = max_named_leq_b_[j];
00234 
00235                 return
00236                     names_a_[ i0 ] < names_b_[ j0 ]
00237                     &&
00238                     names_a_[ i1 ] > names_b_[ j1 ];
00239             } else {
00240                 return !conflicts_anchor(i,j);
00241             }
00242         }
00243 
00249         bool
00250         allowed_ins(size_type i, size_type j) const {
00251             assert(i<=lenA_);
00252             assert(1<=j); assert(j<=lenB_);
00253             return air_[j].first<=i && i<=air_[j].second;
00254         }
00255 
00257         std::string
00258         get_name_a(size_type i) const {
00259             return names_a_[i];
00260         }
00261 
00263         std::string
00264         get_name_b(size_type j) const {
00265             return names_b_[j];
00266         }
00267 
00269         size_type
00270         name_size() const {
00271             return name_size_;
00272         };
00273 
00275         bool
00276         empty() const {
00277             return name_size() == 0;
00278         }
00279 
00286         size_pair_t
00287         rightmost_anchor() const {
00288             for (size_type i = lenA_; i >= 1; --i) {
00289                 if (anchors_a_[i] > 0)
00290                     return size_pair_t(i, anchors_a_[i]);
00291             }
00292             return size_pair_t(0, 0);
00293         }
00294 
00301         size_pair_t
00302         leftmost_anchor() const {
00303             for (size_type i = 0; i <= lenA_; i++) {
00304                 if (anchors_a_[i] > 0)
00305                     return size_pair_t(i, anchors_a_[i]);
00306             }
00307             return size_pair_t(lenA_ + 1, lenB_ + 1);
00308         }
00309 
00316         bool
00317         is_anchored_a(size_type i) const {
00318             return is_anchored(lenA_,anchors_a_,i);
00319         };
00320 
00325         bool
00326         is_anchored_b(size_type i) const {
00327             return is_anchored(lenB_,anchors_b_,i);
00328         };
00329 
00335         bool
00336         is_named_a(size_type i) const {
00337             return is_named(lenA_,anchors_a_,i);
00338         };
00339 
00344         bool
00345         is_named_b(size_type i) const {
00346             return is_named(lenB_,anchors_b_,i);
00347         };
00348 
00349 
00351         void
00352         print_debug();
00353 
00354     private:
00355         typedef std::map<std::string, size_type> name_tab_t;
00356         typedef std::vector<int> int_vec_t;
00357         typedef std::vector<size_type> size_vec_t;
00358         typedef std::vector<std::string> name_vec_t;
00359         typedef std::vector<range_t> range_vec_t; 
00360 
00362         const bool strict_;
00363 
00365         size_type lenA_;
00366 
00368         size_type lenB_;
00369 
00378         int_vec_t anchors_a_;
00379 
00381         int_vec_t anchors_b_;
00382 
00392         range_vec_t ar_;
00393 
00395         range_vec_t adr_;
00397         range_vec_t air_;
00398 
00400         name_vec_t names_a_;
00402         name_vec_t names_b_;
00403 
00405         size_type name_size_;
00406 
00411         size_vec_t max_anchored_left_a_;
00412 
00417         size_vec_t min_anchored_right_a_;
00418 
00423         size_vec_t max_named_leq_a_;
00424 
00429         size_vec_t min_named_geq_a_;
00430 
00432         size_vec_t max_anchored_left_b_;
00433 
00435         size_vec_t min_anchored_right_b_;
00436 
00438         size_vec_t max_named_leq_b_;
00439 
00441         size_vec_t min_named_geq_b_;
00442 
00450         static
00451         bool
00452         is_named(size_type len, const int_vec_t &anchors, size_type i) {
00453             assert(i<=len+1);
00454             return i==0 || i==len+1 || anchors[i]!=0;
00455         };
00456 
00457 
00465         static
00466         bool
00467         is_anchored(size_type len, const int_vec_t &anchors, size_type i) {
00468             assert(i<=len);
00469             return i==0 || i==len+1 || anchors[i]>0;
00470         }
00471 
00485         bool
00486         conflicts_anchor(size_type i, size_type j) const {
00487             return
00488                 !(static_cast<size_type>(anchors_a_[ max_anchored_left_a_[i] ]) < j
00489                   &&
00490                   j < static_cast<size_type>(anchors_a_[ min_anchored_right_a_[i] ])) ;
00491         }
00492 
00505         bool
00506         is_anchor(size_type i, size_type j) const {
00507             return is_named_a(i) && static_cast<size_type>(anchors_a_[i]) == j;
00508         }
00509 
00510         // ------------------------------------------------------------
00511         // construction helper
00512 
00526         static void
00527         transform_input(name_tab_t &nameTab,
00528                         size_type len,
00529                         const std::vector<std::string> &seq,
00530                         bool strict);
00531 
00552         void
00553         init_tables(const name_tab_t &nameTabA, const name_tab_t &nameTabB);
00554 
00565         void static init_anchored_tables(size_type len,
00566                                          const int_vec_t &anchors,
00567                                          size_vec_t &max_anchored_left,
00568                                          size_vec_t &min_anchored_right);
00569 
00580         void static init_named_tables(size_type len,
00581                                       const int_vec_t &anchors,
00582                                       size_vec_t &max_named_leq,
00583                                       size_vec_t &min_named_geq);
00584 
00599         static void
00600         init_anchors(int_vec_t &seq_tab,
00601                        name_vec_t &name_vec_tab,
00602                        const name_tab_t &nameTabA,
00603                        const name_tab_t &nameTabB);
00604 
00607         static bool
00608         only_dont_care(const std::string &s);
00609     };
00610 }
00611 
00612 #endif // LOCARNA_ANCHOR_CONSTRAINTS_HH
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends