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) {
24 m = MothurOut::getInstance();
25 alignment.resize(nRows); // For the Gotoh and Needleman-Wunsch we initialize the dynamic programming
26 for(int i=0;i<nRows;i++){ // matrix by initializing a matrix that is A x A. By default we will set A
27 alignment[i].resize(nCols); // at 2000 for 16S rRNA gene sequences
31 m->errorOut(e, "Alignment", "Alignment");
35 /**************************************************************************************************/
36 void Alignment::resize(int A) {
41 alignment.resize(nRows);
42 for(int i=0;i<nRows;i++){
43 alignment[i].resize(nCols);
47 m->errorOut(e, "Alignment", "resize");
51 /**************************************************************************************************/
53 void Alignment::traceBack(){ // This traceback routine is used by the dynamic programming algorithms
55 // to fill the values of seqAaln and seqBaln
63 AlignmentCell currentCell = alignment[row][column]; // Start the traceback from the bottom-right corner of the
66 if(currentCell.prevCell == 'x'){ seqAaln = seqBaln = "NOALIGNMENT"; }//If there's an 'x' in the bottom-
67 else{ // right corner bail out because it means nothing got aligned
68 while(currentCell.prevCell != 'x'){ // while the previous cell isn't an 'x', keep going...
70 if(currentCell.prevCell == 'u'){ // if the pointer to the previous cell is 'u', go up in the
71 seqAaln = '-' + seqAaln; // matrix. this indicates that we need to insert a gap in
72 seqBaln = seqB[row] + seqBaln; // seqA and a base in seqB
73 currentCell = alignment[--row][column];
75 else if(currentCell.prevCell == 'l'){ // if the pointer to the previous cell is 'l', go to the left
76 seqBaln = '-' + seqBaln; // in the matrix. this indicates that we need to insert a gap
77 seqAaln = seqA[column] + seqAaln; // in seqB and a base in seqA
78 currentCell = alignment[row][--column];
81 seqAaln = seqA[column] + seqAaln; // otherwise we need to go diagonally up and to the left,
82 seqBaln = seqB[row] + seqBaln; // here we add a base to both alignments
83 currentCell = alignment[--row][--column];
88 pairwiseLength = seqAaln.length();
89 seqAstart = 1; seqAend = 0;
90 seqBstart = 1; seqBend = 0;
92 for(int i=0;i<seqAaln.length();i++){
93 if(seqAaln[i] != '-' && seqBaln[i] == '-') { seqAstart++; }
94 else if(seqAaln[i] == '-' && seqBaln[i] != '-') { seqBstart++; }
98 pairwiseLength -= (seqAstart + seqBstart - 2);
100 for(int i=seqAaln.length()-1; i>=0;i--){
101 if(seqAaln[i] != '-' && seqBaln[i] == '-') { seqAend++; }
102 else if(seqAaln[i] == '-' && seqBaln[i] != '-') { seqBend++; }
105 pairwiseLength -= (seqAend + seqBend);
107 seqAend = seqA.length() - seqAend - 1;
108 seqBend = seqB.length() - seqBend - 1;
110 catch(exception& e) {
111 m->errorOut(e, "Alignment", "traceBack");
115 /**************************************************************************************************/
117 Alignment::~Alignment(){
119 for (int i = 0; i < alignment.size(); i++) {
120 for (int j = (alignment[i].size()-1); j >= 0; j--) { alignment[i].pop_back(); }
123 catch(exception& e) {
124 m->errorOut(e, "Alignment", "~Alignment");
129 /**************************************************************************************************/
131 string Alignment::getSeqAAln(){
132 return seqAaln; // this is called to get the alignment of seqA
135 /**************************************************************************************************/
137 string Alignment::getSeqBAln(){
138 return seqBaln; // this is called to get the alignment of seqB
141 /**************************************************************************************************/
143 int Alignment::getCandidateStartPos(){
144 return seqAstart; // this is called to report the quality of the alignment
147 /**************************************************************************************************/
149 int Alignment::getCandidateEndPos(){
150 return seqAend; // this is called to report the quality of the alignment
153 /**************************************************************************************************/
155 int Alignment::getTemplateStartPos(){
156 return seqBstart; // this is called to report the quality of the alignment
159 /**************************************************************************************************/
161 int Alignment::getTemplateEndPos(){
162 return seqBend; // this is called to report the quality of the alignment
165 /**************************************************************************************************/
167 int Alignment::getPairwiseLength(){
168 return pairwiseLength; // this is the pairwise alignment length
171 /**************************************************************************************************/
173 //int Alignment::getLongestTemplateGap(){
175 // int length = seqBaln.length();
177 // int gapLength = 0;
179 // int start = seqAstart;
180 // if(seqAstart < seqBstart){ start = seqBstart; }
181 // for(int i=seqAstart;i<length;i++){
182 // if(seqBaln[i] == '-'){
186 // if(gapLength > 0){
187 // if(gapLength > longGap){ longGap = gapLength; }
195 /**************************************************************************************************/