00001 #ifndef DYNALN_H
00002 #define DYNALN_H
00003
00004 #include "bioseq.h"
00005 #include "matrix.h"
00006 #include <list>
00007 #include <vector>
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00039 enum ALNTYPE {GLOBAL, LOCAL};
00040
00041
00054 class Dynaln {
00055 public:
00056 Dynaln() : ST(), gapi(-8), gape(-2), seq1(0),
00057 seq2(0),alnidx(), S(0), M(0),
00058 IX(0), IY(0), Smax(0), idencnt(0), simcnt(0), numgaps1(0),
00059 numgaps2(0), gaplen1(0), gaplen2(0),
00060 seq1begin(-1), seq1end(-1), seq2begin(-1), seq2end(-1),
00061 topaln(), middle(), buttomaln(), topruler(), buttomruler() { }
00079 Dynaln(const bioseq &s1, const bioseq &s2)
00080 : ST(), gapi(-8), gape(-2), seq1(&s1), seq2(&s2),
00081 alnidx(), S(0), M(0), IX(0), IY(0),
00082 Smax(0), idencnt(0), simcnt(0), numgaps1(0),
00083 numgaps2(0), gaplen1(0), gaplen2(0),
00084 seq1begin(-1), seq1end(-1), seq2begin(-1), seq2end(-1),
00085 topaln(), middle(), buttomaln(), topruler(),buttomruler()
00086 { if ( (s1.getSequenceType() & NUCLEIC_ACID & s2.getSequenceType()) > 0)
00087 ST.setMatrix("NUC.4.4");
00088 }
00096 Dynaln(const bioseq &s1, const bioseq &s2, const string &ma)
00097 : ST(ma), seq1(&s1), seq2(&s2),
00098 alnidx(), S(0), M(0), IX(0), IY(0),
00099 Smax(0), idencnt(0), simcnt(0), numgaps1(0),
00100 numgaps2(0), gaplen1(0), gaplen2(0),
00101 seq1begin(-1), seq1end(-1), seq2begin(-1), seq2end(-1),
00102 topaln(), middle(), buttomaln(), topruler(), buttomruler()
00103 {
00104 gapi=2*ST.getMinScore();
00105 gape=ST.getMinScore()/3;
00106 if (gape > -1) gape = -1;
00107 if (gape < -3) gape = -3; }
00115 int global();
00120 int runglobal() {
00121 int s=global(); buildResult(); return s; }
00124 int local();
00128 int runlocal() {
00129 int s=local(); buildResult(); return s; }
00130
00131 ~Dynaln();
00132
00148 void buildResult();
00156 void buildAlnInfo();
00157
00159
00162 void printAlign(ostream &ous, const int w=70) const;
00165 const string& getTopAln() const { return topaln; }
00168 const string& getButtomAln() const { return buttomaln; }
00182 void getNgMatchArray(vector<int> &ngmarr, bool append=false, bool countsim=true) const;
00188 void getMatchArray(vector<int> &marr, bool countsim=true) const;
00205 string toDelimitedString(const string &dl=",", int ibase=0) const;
00206
00211 void showseq() const {
00212 cout << seq1->toString() << "\n"
00213 << seq2->toString() << endl; }
00214
00216 int getScore() { return Smax; }
00217 int getAlnlen() const { return alnidx.size(); }
00218 float getIdentity() const { return (float)idencnt/getAlnlen(); }
00219 float getSimilarity() const { return (float)(idencnt+simcnt)/getAlnlen(); }
00224 pair<int,int> numgaps() const {
00225 return make_pair(numgaps1, numgaps2); }
00226 static string headers(const string &dl);
00227
00228 const Matrix* getScoringMatrix() const { return &ST; }
00231 int topBeginIndex() const { return seq1begin; }
00232 int buttomBeginIndex() const { return seq2begin; }
00233 int topEndIndex() const { return seq1end; }
00234 int buttomEndIndex() const { return seq2end; }
00235
00237 void setMatrix(const string &mat) { ST.setMatrix(mat); }
00241 void setMatrix(const Matrix &mat) { ST=mat; }
00242
00246 void setSeq1(const bioseq &sq) { seq1 = &sq; }
00247 void setSeq2(const bioseq &sq) { seq2 = &sq; }
00248 void setSeq(const bioseq &sq1, const bioseq &sq2) {
00249 seq1=&sq1; seq2=&sq2; }
00261 void setseq(const bioseq &sq1, const bioseq &sq2) {
00262 seq1=&sq1; seq2=&sq2; }
00263
00264
00265
00266 void setGapInsert(int ins) { gapi=ins; }
00267 void setGapExtend(int ext) { gape=ext; }
00268 void setGapParameter(const int go, const int ge) {
00269 gapi=go; gape=ge; }
00270 void setAllParameter(const Matrix& mat, const int go, const int ge)
00271 { ST=mat; gapi=go; gape=ge; }
00272
00274 static const char gapchar='-';
00275 static const char idenchar='|';
00276 static const char simchar=':';
00277 static const int simcut=0;
00284 static const int PTRNULL=0;
00285 static const int PTRDIAG=1;
00286 static const int PTRTOP=2;
00287 static const int PTRLEFT=4;
00288
00290 void debug_showmatrix(ostream &ous) const;
00291
00292 protected:
00294 void countnumgaps();
00307 void clearResult();
00308
00309
00310
00311
00312
00313
00322 void allocmem();
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00340 void tracepointer(int &i, int &j);
00343 void findAlnBoundary();
00344
00345 int gapi, gape;
00346
00347
00351 Matrix ST;
00352
00354 const bioseq *seq1;
00355 const bioseq *seq2;
00356
00357 const int* C1;
00358 const int* C2;
00361 int* S;
00362
00364 int Ssize;
00365 int *M, *IX, *IY;
00366
00367 int numcol;
00368
00369
00370
00371
00379 list<pair<int, int> > alnidx;
00380
00381 int Smax;
00382
00383
00389 int Smaxi, Smaxj;
00395 int seq1begin, seq1end, seq2begin, seq2end;
00396
00397
00398
00399 int idencnt;
00400 int simcnt;
00401 int numgaps1;
00402 int numgaps2;
00403 int gaplen1;
00404 int gaplen2;
00405
00406
00407
00416 string topaln, middle, buttomaln;
00417 string topruler, buttomruler;
00418 ALNTYPE alntype;
00419 };
00429 class LSDynaln : public Dynaln {
00430 public:
00431 LSDynaln() : Dynaln() { }
00432 LSDynaln(const bioseq &s1, const bioseq &s2)
00433 : Dynaln(s1,s2) { }
00434 LSDynaln(const bioseq &s1, const bioseq &s2, const string &mat)
00435 : Dynaln(s1,s2,mat) { }
00436 ~LSDynaln();
00442 int global();
00443 int local();
00446 int runlocal() {
00447 int s=local(); buildResult(); return s; }
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 pair<int, int> computeScoreFW(int b1, int e1, int b2, int e2);
00475 pair<int, int> computeScoreBW(int b1, int e1, int b2, int e2);
00476
00485 int path(int b1, int e1, int b2, int e2);
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00510 list<pair<int,int> > tracebackFW(pair<int,int> mrow, int b1, int &e1, int b2, int &e2);
00520 list<pair<int,int> > tracebackBW(pair<int,int> mrow, int b1, int &e1, int b2, int &e2);
00521
00522
00527 void buildResult();
00528
00529 void debug_showmatrixForward(pair<int,int> &rows, const int col);
00530 void debug_showmatrixBackward(pair<int,int> &rows, const int col);
00531
00532 private:
00538 void allocmem();
00539 int *MR, *IXR, *IYR;
00540
00541
00542 };
00543
00544 #endif