4 * Created by Pat Schloss on 12/15/08.
5 * Copyright 2008 Patrick D. Schloss. All rights reserved.
7 * This is a class for an abstract datatype for classes that implement various types of alignment algorithms.
8 * As of 12/18/08 these included alignments based on blastn, needleman-wunsch, and the Gotoh algorithms
12 #include "alignmentcell.hpp"
13 #include "alignment.hpp"
16 /**************************************************************************************************/
18 Alignment::Alignment() { /* do nothing */ }
20 /**************************************************************************************************/
22 Alignment::Alignment(int A) : nCols(A), nRows(A) {
25 m = MothurOut::getInstance();
26 alignment.resize(nRows); // For the Gotoh and Needleman-Wunsch we initialize the dynamic programming
27 for(int i=0;i<nRows;i++){ // matrix by initializing a matrix that is A x A. By default we will set A
28 alignment[i].resize(nCols); // at 2000 for 16S rRNA gene sequences
32 m->errorOut(e, "Alignment", "Alignment");
36 /**************************************************************************************************/
37 void Alignment::resize(int A) {
42 alignment.resize(nRows);
43 for(int i=0;i<nRows;i++){
44 alignment[i].resize(nCols);
48 m->errorOut(e, "Alignment", "resize");
52 /**************************************************************************************************/
54 void Alignment::traceBack(){ // This traceback routine is used by the dynamic programming algorithms
56 // to fill the values of seqAaln and seqBaln
64 AlignmentCell currentCell = alignment[row][column]; // Start the traceback from the bottom-right corner of the
67 if(currentCell.prevCell == 'x'){ seqAaln = seqBaln = "NOALIGNMENT"; }//If there's an 'x' in the bottom-
68 else{ // right corner bail out because it means nothing got aligned
69 while(currentCell.prevCell != 'x'){ // while the previous cell isn't an 'x', keep going...
71 if(currentCell.prevCell == 'u'){ // if the pointer to the previous cell is 'u', go up in the
72 seqAaln = '-' + seqAaln; // matrix. this indicates that we need to insert a gap in
73 seqBaln = seqB[row] + seqBaln; // seqA and a base in seqB
74 currentCell = alignment[--row][column];
76 else if(currentCell.prevCell == 'l'){ // if the pointer to the previous cell is 'l', go to the left
77 seqBaln = '-' + seqBaln; // in the matrix. this indicates that we need to insert a gap
78 seqAaln = seqA[column] + seqAaln; // in seqB and a base in seqA
79 currentCell = alignment[row][--column];
82 seqAaln = seqA[column] + seqAaln; // otherwise we need to go diagonally up and to the left,
83 seqBaln = seqB[row] + seqBaln; // here we add a base to both alignments
84 currentCell = alignment[--row][--column];
89 pairwiseLength = seqAaln.length();
90 seqAstart = 1; seqAend = 0;
91 seqBstart = 1; seqBend = 0;
93 for(int i=0;i<seqAaln.length();i++){
94 if(seqAaln[i] != '-' && seqBaln[i] == '-') { seqAstart++; }
95 else if(seqAaln[i] == '-' && seqBaln[i] != '-') { seqBstart++; }
99 pairwiseLength -= (seqAstart + seqBstart - 2);
101 for(int i=seqAaln.length()-1; i>=0;i--){
102 if(seqAaln[i] != '-' && seqBaln[i] == '-') { seqAend++; }
103 else if(seqAaln[i] == '-' && seqBaln[i] != '-') { seqBend++; }
106 pairwiseLength -= (seqAend + seqBend);
108 seqAend = seqA.length() - seqAend - 1;
109 seqBend = seqB.length() - seqBend - 1;
111 catch(exception& e) {
112 m->errorOut(e, "Alignment", "traceBack");
116 /**************************************************************************************************/
118 Alignment::~Alignment(){
120 for (int i = 0; i < alignment.size(); i++) {
121 for (int j = (alignment[i].size()-1); j >= 0; j--) { alignment[i].pop_back(); }
124 catch(exception& e) {
125 m->errorOut(e, "Alignment", "~Alignment");
130 /**************************************************************************************************/
132 string Alignment::getSeqAAln(){
133 return seqAaln; // this is called to get the alignment of seqA
136 /**************************************************************************************************/
138 string Alignment::getSeqBAln(){
139 return seqBaln; // this is called to get the alignment of seqB
142 /**************************************************************************************************/
144 int Alignment::getCandidateStartPos(){
145 return seqAstart; // this is called to report the quality of the alignment
148 /**************************************************************************************************/
150 int Alignment::getCandidateEndPos(){
151 return seqAend; // this is called to report the quality of the alignment
154 /**************************************************************************************************/
156 int Alignment::getTemplateStartPos(){
157 return seqBstart; // this is called to report the quality of the alignment
160 /**************************************************************************************************/
162 int Alignment::getTemplateEndPos(){
163 return seqBend; // this is called to report the quality of the alignment
166 /**************************************************************************************************/
168 int Alignment::getPairwiseLength(){
169 return pairwiseLength; // this is the pairwise alignment length
172 /**************************************************************************************************/
174 //int Alignment::getLongestTemplateGap(){
176 // int length = seqBaln.length();
178 // int gapLength = 0;
180 // int start = seqAstart;
181 // if(seqAstart < seqBstart){ start = seqBstart; }
182 // for(int i=seqAstart;i<length;i++){
183 // if(seqBaln[i] == '-'){
187 // if(gapLength > 0){
188 // if(gapLength > longGap){ longGap = gapLength; }
196 /**************************************************************************************************/