LocARNA-1.9.2
src/LocARNA/aligner_n.hh
00001 #ifndef LOCARNA_ALIGNER_N_HH
00002 #define LOCARNA_ALIGNER_N_HH
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #include <config.h>
00006 #endif
00007 
00008 #include "sequence.hh"
00009 #include "basepairs.hh"
00010 #include "sparsification_mapper.hh"
00011 #include "arc_matches.hh"
00012 #include "alignment.hh"
00013 
00014 #include "params.hh"
00015 #include "scoring.hh"
00016 
00017 #include "matrix.hh"
00018 
00019 #include "aligner_restriction.hh"
00020 
00021 namespace LocARNA {
00022 
00036     class AlignerN {
00037     public:
00038         typedef BasePairs__Arc Arc; 
00039         typedef SparsificationMapper::ArcIdx ArcIdx; 
00040         typedef SparsificationMapper::ArcIdxVec
00041             ArcIdxVec; 
00042         typedef SparsificationMapper::matidx_t
00043             matidx_t; 
00044         typedef SparsificationMapper::seq_pos_t
00045             seq_pos_t; 
00046         typedef SparsificationMapper::index_t index_t; 
00047 
00049         typedef ScoreMatrix M_matrix_t;
00050 
00051     private:
00052     protected:
00053         const AlignerNParams *params; 
00054 
00055         const Scoring *scoring; 
00056         Scoring *mod_scoring;   
00057 
00058         const Sequence &seqA;   
00059         const Sequence &seqB;   
00060 
00061         const SparsificationMapper
00062             &mapperA; 
00063         const SparsificationMapper
00064             &mapperB; 
00065 
00066         const ArcMatches
00067             &arc_matches; 
00068 
00069         const BasePairs &bpsA; 
00070         const BasePairs &bpsB; 
00071 
00073         AlignerRestriction r;
00074 
00076         ScoreMatrix Dmat;
00077 
00080         ScoreMatrix IAmat;
00083         ScoreMatrix IBmat;
00084 
00087         ScoreMatrix IADmat;
00090         ScoreMatrix IBDmat;
00091 
00093         ScoreMatrix Emat;
00095         ScoreMatrix Fmat;
00096 
00103         M_matrix_t M;
00104 
00109         ScoreMatrix gapCostMatA;
00110 
00115         ScoreMatrix gapCostMatB;
00116 
00117         int min_i; 
00118         int min_j; 
00119         int max_i; 
00120         int max_j; 
00121 
00122         bool D_created; 
00123 
00124         Alignment alignment; 
00125 
00126         // ============================================================
00136         class UnmodifiedScoringViewN {
00137         protected:
00138             const AlignerN
00139                 *alignerN_; 
00140         public:
00146             explicit UnmodifiedScoringViewN(const AlignerN *alignerN)
00147                 : alignerN_(alignerN){};
00148 
00154             const Scoring *
00155             scoring() const {
00156                 return alignerN_->scoring;
00157             }
00158 
00167             infty_score_t
00168             D(const Arc &a, const Arc &b) const {
00169                 return alignerN_->Dmat(a.idx(), b.idx());
00170             }
00171 
00180             infty_score_t
00181             D(bool isA, const Arc &arcX, const Arc &arcY) {
00182                 if (isA)
00183                     return D(arcX, arcY);
00184                 else
00185                     return D(arcY, arcX);
00186             }
00187 
00195             infty_score_t
00196             D(const ArcMatch &am) const {
00197                 return D(am.arcA(), am.arcB());
00198             }
00199         };
00200 
00208         // TODO: required? ModifiedScoringView
00209         class ModifiedScoringViewN {
00210         protected:
00211             const AlignerN
00212                 *alignerN_; 
00213 
00214             score_t lambda_; 
00215 
00223             size_t
00224             arc_length(const Arc &a) const {
00225                 return a.right() - a.left() + 1;
00226             }
00227 
00228         public:
00237             explicit ModifiedScoringViewN(const AlignerN *alignerN)
00238                 : alignerN_(alignerN), lambda_(0) {}
00239 
00245             void
00246             set_lambda(score_t lambda) {
00247                 lambda_ = lambda;
00248             }
00249 
00255             const Scoring *
00256             scoring() const {
00257                 return alignerN_->mod_scoring;
00258             }
00259 
00268             infty_score_t
00269             D(const Arc &a, const Arc &b) const {
00270                 return alignerN_->Dmat(a.idx(), b.idx()) -
00271                     lambda_ * (arc_length(a) + arc_length(b));
00272             }
00273 
00281             infty_score_t
00282             D(const ArcMatch &am) const {
00283                 return alignerN_->Dmat(am.arcA().idx(), am.arcB().idx()) -
00284                     lambda_ * (arc_length(am.arcA()) + arc_length(am.arcB()));
00285             }
00286         };
00287 
00288         const UnmodifiedScoringViewN def_scoring_view; 
00289         ModifiedScoringViewN mod_scoring_view; 
00290 
00291 
00292         // ============================================================
00293 
00308         template <class ScoringView>
00309         void
00310         init_M_E_F(pos_type al,
00311                    pos_type ar,
00312                    pos_type bl,
00313                    pos_type br,
00314                    ScoringView sv);
00315 
00324         template <bool isA, class ScoringView>
00325         void
00326         initGapCostMat(ScoringView sv);
00327 
00336         template <bool isA>
00337         infty_score_t
00338         getGapCostBetween(pos_type leftSide, pos_type rightSide);
00339 
00351         template <bool isA, class ScoringView>
00352         infty_score_t
00353         compute_IX(pos_type xl, const Arc &arcY, pos_type i, ScoringView sv);
00354 
00364         void
00365         fill_IA_entries(pos_type al, Arc arcB, pos_type max_ar);
00366 
00376         void
00377         fill_IB_entries(Arc arcA, pos_type bl, pos_type max_br);
00378 
00391         template <class ScoringView>
00392         infty_score_t
00393         compute_E_entry(index_t al,
00394                         matidx_t i_index,
00395                         matidx_t j_index,
00396                         seq_pos_t i_seq_pos,
00397                         seq_pos_t i_prev_seq_pos,
00398                         ScoringView sv);
00399 
00412         template <class ScoringView>
00413         infty_score_t
00414         compute_F_entry(index_t bl,
00415                         matidx_t i_index,
00416                         matidx_t j_index,
00417                         seq_pos_t i_seq_pos,
00418                         seq_pos_t i_prev_seq_pos,
00419                         ScoringView sv);
00433         template <class ScoringView>
00434         infty_score_t
00435         compute_M_entry(index_t al,
00436                         index_t bl,
00437                         matidx_t index_i,
00438                         matidx_t index_j,
00439                         ScoringView sv);
00440         //---------------------------------------------------------------------------------
00441 
00456         void
00457         fill_M_entries(pos_type al, pos_type ar, pos_type bl, pos_type br);
00458 
00469         template <class ScoringView>
00470         void
00471         trace_E(pos_type al,
00472                 matidx_t i_index,
00473                 pos_type bl,
00474                 matidx_t j_index,
00475                 bool top_level,
00476                 ScoringView sv);
00477 
00488         template <class ScoringView>
00489         void
00490         trace_F(pos_type al,
00491                 matidx_t i_index,
00492                 pos_type bl,
00493                 matidx_t j_index,
00494                 bool top_level,
00495                 ScoringView sv);
00496 
00507         template <class ScoringView>
00508         void
00509         trace_M(pos_type al,
00510                 matidx_t i_index,
00511                 pos_type bl,
00512                 matidx_t j_index,
00513                 bool tl,
00514                 ScoringView sv);
00515 
00526         template <class ScoringView>
00527         void
00528         trace_M_noex(pos_type al,
00529                      pos_type i,
00530                      pos_type bl,
00531                      pos_type j,
00532                      bool top_level,
00533                      ScoringView sv);
00534 
00543         template <bool isA, class ScoringView>
00544         void
00545         trace_IXD(const Arc &arcA, const Arc &arcB, ScoringView sv);
00546 
00553         template <class ScoringView>
00554         void
00555         trace_D(const Arc &arcA, const Arc &arcB, ScoringView sv);
00556 
00557         // /**
00558         //  * trace D matrix
00559         //  * @param am the corresponding arcs which defines the D element
00560         //  * @param sv scoring view
00561         //  */
00562         // template <class ScoringView>
00563         // void trace_D(const ArcMatch &am, ScoringView sv);
00564 
00573         template <bool isA, class ScoringView>
00574         void
00575         trace_IX(pos_type xl, pos_type i, const Arc &arcY, ScoringView sv);
00576 
00581         void
00582         align_D();
00583 
00589         void
00590         fill_D_entries(pos_type al, pos_type bl);
00591 
00599         infty_score_t &
00600         D(const ArcMatch &am) {
00601             return Dmat(am.arcA().idx(), am.arcB().idx());
00602         }
00603 
00612         template <bool isA>
00613         infty_score_t &
00614         D(const Arc &arcX, const Arc &arcY) {
00615             if (isA)
00616                 return Dmat(arcX.idx(), arcY.idx());
00617             else
00618                 return Dmat(arcY.idx(), arcX.idx());
00619         }
00620 
00629         infty_score_t &
00630         D(const Arc &arcA, const Arc &arcB) {
00631             return Dmat(arcA.idx(), arcB.idx());
00632         }
00633 
00642         template <bool isA>
00643         infty_score_t &
00644         IX(const pos_type i, const Arc &arc) {
00645             if (isA)
00646                 return IAmat(i, arc.idx());
00647             else
00648                 return IBmat(arc.idx(), i);
00649         }
00650 
00659         template <bool isA>
00660         infty_score_t &
00661         IXD(const Arc &arc1, const Arc &arc2) {
00662             if (isA)
00663                 return IADmat(arc1.idx(), arc2.idx());
00664             else
00665                 return IBDmat(arc2.idx(), arc1.idx());
00666         }
00667 
00675         infty_score_t &
00676         IA(const pos_type i, const Arc &b) {
00677             return IAmat(i, b.idx());
00678         }
00679 
00687         infty_score_t &
00688         IB(const Arc &a, const pos_type k) {
00689             return IBmat(a.idx(), k);
00690         }
00691 
00697         template <class ScoringView>
00698         void
00699         trace(ScoringView sv);
00700 
00701     public:
00703         AlignerN(const AlignerN &a);
00704 
00711         AlignerN(const AlignerParams &ap);
00712 
00717         static AlignerNParams
00718         create() {
00719             return AlignerNParams();
00720         }
00721 
00723         ~AlignerN();
00724 
00726         Alignment const &
00727         get_alignment() const {
00728             return alignment;
00729         }
00730 
00732         infty_score_t
00733         align();
00734 
00736         void
00737         trace();
00738     };
00739 
00740 } // end namespace LocARNA
00741 
00742 #endif // LOCARNA_ALIGNER_N_HH
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends