5 * Created by Pat Schloss on 12/17/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
8 * This is my implementation of the NAST (nearest alignment space termination) algorithm as described in:
10 * DeSantis TZ, Hugenholtz P, Keller K, Brodie EL, Larsen N, Piceno YM, Phan R, & Anderson GL. 2006. NAST: a multiple
11 * sequence alignment server for comparative analysis of 16S rRNA genes. Nucleic Acids Research. 34:W394-9.
13 * To construct an object one needs to provide a method of getting a pairwise alignment (alignment) and the template
14 * and candidate sequence that are to be aligned to each other.
18 #include "sequence.hpp"
19 #include "alignment.hpp"
22 /**************************************************************************************************/
24 Nast::Nast(Alignment* method, Sequence* cand, Sequence* temp) : alignment(method), candidateSeq(cand), templateSeq(temp) {
26 m = MothurOut::getInstance();
28 pairwiseAlignSeqs(); // This is part A in Fig. 2 of DeSantis et al.
29 regapSequences(); // This is parts B-F in Fig. 2 of DeSantis et al.
32 m->errorOut(e, "Nast", "Nast");
37 /**************************************************************************************************/
39 void Nast::pairwiseAlignSeqs(){ // Here we call one of the pairwise alignment methods to align our unaligned candidate
40 // and template sequences
42 alignment->align(candidateSeq->getUnaligned(), templateSeq->getUnaligned());
44 string candAln = alignment->getSeqAAln();
45 string tempAln = alignment->getSeqBAln();
49 candidateSeq->setPairwise("");
50 templateSeq->setPairwise(templateSeq->getUnaligned());
54 if(tempAln[0] == '-'){
55 int pairwiseAlignmentLength = tempAln.length(); // we need to make sure that the candidate sequence alignment
56 for(int i=0;i<pairwiseAlignmentLength;i++){ // starts where the template sequence alignment starts, if it
57 if(isalpha(tempAln[i])){ // starts early, we nuke the beginning of the candidate
58 candAln = candAln.substr(i); // sequence
59 tempAln = tempAln.substr(i);
64 int pairwiseAlignmentLength = tempAln.length();
65 if(tempAln[pairwiseAlignmentLength-1] == '-'){ // we need to make sure that the candidate sequence alignment
66 for(int i=pairwiseAlignmentLength-1; i>=0; i--){// ends where the template sequence alignment ends, if it runs
67 if(isalpha(tempAln[i])){ // long, we nuke the end of the candidate sequence
68 candAln = candAln.substr(0,i+1);
69 tempAln = tempAln.substr(0,i+1);
77 candidateSeq->setPairwise(candAln); // set the pairwise sequences in the Sequence objects for
78 templateSeq->setPairwise(tempAln); // the candidate and template sequences
81 m->errorOut(e, "Nast", "pairwiseAlignSeqs");
86 /**************************************************************************************************/
88 void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAlign){
90 // here we do steps C-F of Fig. 2 from DeSantis et al.
93 //cout << candAln << endl;
94 //cout << tempAln << endl;
95 //cout << newTemplateAlign << endl;
98 int longAlignmentLength = newTemplateAlign.length();
100 for(int i=0; i<longAlignmentLength; i++){ // use the long alignment as the standard
101 int rightIndex, rightRoom, leftIndex, leftRoom;
103 // Part C of Fig. 2 from DeSantis et al.
104 if((isalpha(newTemplateAlign[i]) != isalpha(tempAln[i]))){ //if there is a discrepancy between the regapped
106 rightRoom = 0; leftRoom = 0;
108 // Part D of Fig. 2 from DeSantis et al. // template sequence and the official template sequence
109 for(leftIndex=i-1;leftIndex>0;leftIndex--){ // then we've got problems...
110 if(!isalpha(candAln[leftIndex])){
111 leftRoom = 1; //count how far it is to the nearest gap on the LEFT side of the anomaly
112 while(leftIndex-leftRoom>=0 && !isalpha(candAln[leftIndex-leftRoom])) { leftRoom++; }
117 for(rightIndex=i+1;rightIndex<longAlignmentLength-1;rightIndex++){
118 if(!isalpha(candAln[rightIndex])){
119 rightRoom = 1; //count how far it is to the nearest gap on the RIGHT side of the anomaly
120 while(rightIndex+rightRoom<longAlignmentLength && !isalpha(candAln[rightIndex+rightRoom])) { rightRoom++; }
125 int insertLength = 0; // figure out how long the anomaly is
126 while(!isalpha(newTemplateAlign[i + insertLength])) { insertLength++; }
127 if(insertLength > maxInsertLength){ maxInsertLength = insertLength; }
129 if((leftRoom + rightRoom) >= insertLength){
131 // Parts D & E from Fig. 2 of DeSantis et al.
132 if((i-leftIndex) <= (rightIndex-i)){ // the left gap is closer - > move stuff left there's
134 if(leftRoom >= insertLength){ // enough room to the left to move
135 string leftTemplateString = newTemplateAlign.substr(0,i);
136 string rightTemplateString = newTemplateAlign.substr(i+insertLength);
137 newTemplateAlign = leftTemplateString + rightTemplateString;
138 longAlignmentLength = newTemplateAlign.length();
140 string leftCandidateString = candAln.substr(0,leftIndex-insertLength+1);
141 string rightCandidateString = candAln.substr(leftIndex+1);
142 candAln = leftCandidateString + rightCandidateString;
145 else{ // not enough room to the left, have to steal some space to
146 string leftTemplateString = newTemplateAlign.substr(0,i); // the right
147 string rightTemplateString = newTemplateAlign.substr(i+insertLength);
148 newTemplateAlign = leftTemplateString + rightTemplateString;
149 longAlignmentLength = newTemplateAlign.length();
151 string leftCandidateString = candAln.substr(0,leftIndex-leftRoom+1);
152 string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
153 string rightCandidateString = candAln.substr(rightIndex+(insertLength-leftRoom));
154 candAln = leftCandidateString + insertString + rightCandidateString;
158 else{ // the right gap is closer - > move stuff right there's
159 if(rightRoom >= insertLength){ // enough room to the right to move
160 string leftTemplateString = newTemplateAlign.substr(0,i);
161 string rightTemplateString = newTemplateAlign.substr(i+insertLength);
162 newTemplateAlign = leftTemplateString + rightTemplateString;
163 longAlignmentLength = newTemplateAlign.length();
165 string leftCandidateString = candAln.substr(0,rightIndex);
166 string rightCandidateString = candAln.substr(rightIndex+insertLength);
167 candAln = leftCandidateString + rightCandidateString;
170 else{ // not enough room to the right, have to steal some
171 // space to the left lets move left and then right...
172 string leftTemplateString = newTemplateAlign.substr(0,i);
173 string rightTemplateString = newTemplateAlign.substr(i+insertLength);
174 newTemplateAlign = leftTemplateString + rightTemplateString;
175 longAlignmentLength = newTemplateAlign.length();
177 string leftCandidateString = candAln.substr(0,leftIndex-(insertLength-rightRoom)+1);
178 string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
179 string rightCandidateString = candAln.substr(rightIndex+rightRoom);
180 candAln = leftCandidateString + insertString + rightCandidateString;
188 // there could be a case where there isn't enough room in either direction to move stuff
190 string leftTemplateString = newTemplateAlign.substr(0,i);
191 string rightTemplateString = newTemplateAlign.substr(i+leftRoom+rightRoom);
192 newTemplateAlign = leftTemplateString + rightTemplateString;
193 longAlignmentLength = newTemplateAlign.length();
195 string leftCandidateString = candAln.substr(0,leftIndex-leftRoom+1);
196 string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
197 string rightCandidateString = candAln.substr(rightIndex+rightRoom);
198 candAln = leftCandidateString + insertString + rightCandidateString;
200 i -= (leftRoom + rightRoom);
203 // i -= insertLength;
207 catch(exception& e) {
208 m->errorOut(e, "Nast", "removeExtraGaps");
213 /**************************************************************************************************/
215 void Nast::regapSequences(){ //This is essentially part B in Fig 2. of DeSantis et al.
218 string candPair = candidateSeq->getPairwise();
221 string tempPair = templateSeq->getPairwise();
222 string tempAln = templateSeq->getAligned(); // we use the template aligned sequence as our guide
224 int pairwiseLength = candPair.length();
225 int fullAlignLength = tempAln.length();
228 for(int i=0;i<fullAlignLength;i++) { candAln += '.'; }
229 candidateSeq->setAligned(candAln);
233 int fullAlignIndex = 0;
234 int pairwiseAlignIndex = 0;
235 string newTemplateAlign = ""; // this is going to be messy so we want a temporary template
237 while(tempAln[fullAlignIndex] == '.' || tempAln[fullAlignIndex] == '-'){
238 candAln += '.'; // add the initial '-' and '.' to the candidate and template
239 newTemplateAlign += tempAln[fullAlignIndex];// pairwise sequences
243 string lastLoop = "";
245 while(pairwiseAlignIndex<pairwiseLength){
246 if(isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
247 && isalpha(candPair[pairwiseAlignIndex])){
248 // the template and candidate pairwise and template aligned have characters
249 // need to add character onto the candidatSeq.aligned sequence
251 candAln += candPair[pairwiseAlignIndex];
252 newTemplateAlign += tempPair[pairwiseAlignIndex];//
254 pairwiseAlignIndex++;
257 else if(isalpha(tempPair[pairwiseAlignIndex]) && !isalpha(tempAln[fullAlignIndex])
258 && isalpha(candPair[pairwiseAlignIndex])){
259 // the template pairwise and candidate pairwise are characters and the template aligned is a gap
260 // need to insert gaps into the candidateSeq.aligned sequence
263 newTemplateAlign += '-';//
266 else if(!isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
267 && isalpha(candPair[pairwiseAlignIndex])){
268 // the template pairwise is a gap and the template aligned and pairwise sequences have characters
269 // this is the alpha scenario. add character to the candidateSeq.aligned sequence without progressing
270 // further through the tempAln sequence.
272 candAln += candPair[pairwiseAlignIndex];
273 newTemplateAlign += '-';//
274 pairwiseAlignIndex++;
276 else if(isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
277 && !isalpha(candPair[pairwiseAlignIndex])){
278 // the template pairwise and full alignment are characters and the candidate sequence has a gap
279 // should not be a big deal, just add the gap position to the candidateSeq.aligned sequence;
281 candAln += candPair[pairwiseAlignIndex];
282 newTemplateAlign += tempAln[fullAlignIndex];//
284 pairwiseAlignIndex++;
286 else if(!isalpha(tempPair[pairwiseAlignIndex]) && !isalpha(tempAln[fullAlignIndex])
287 && isalpha(candPair[pairwiseAlignIndex])){
288 // the template pairwise and aligned are gaps while the candidate pairwise has a character
289 // this would be an insertion, go ahead and add the character->seems to be the opposite of the alpha scenario
291 candAln += candPair[pairwiseAlignIndex];
292 newTemplateAlign += tempAln[fullAlignIndex];//
293 pairwiseAlignIndex++;
296 else if(isalpha(tempPair[pairwiseAlignIndex]) && !isalpha(tempAln[fullAlignIndex])
297 && !isalpha(candPair[pairwiseAlignIndex])){
298 // template pairwise has a character, but its full aligned sequence and candidate sequence have gaps
299 // this would happen like we need to add a gap. basically the opposite of the alpha situation
301 newTemplateAlign += tempAln[fullAlignIndex];//
305 else if(!isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
306 && !isalpha(candPair[pairwiseAlignIndex])){
307 // template and candidate pairwise are gaps and the template aligned is not a gap this should not be possible
308 // would skip the gaps and not progress through full alignment sequence
311 m->mothurOut("We're into D " + toString(fullAlignIndex) + " " + toString(pairwiseAlignIndex)); m->mothurOutEndLine();
312 pairwiseAlignIndex++;
315 // everything has a gap - not possible
318 m->mothurOut("We're into F " + toString(fullAlignIndex) + " " + toString(pairwiseAlignIndex)); m->mothurOutEndLine();
319 pairwiseAlignIndex++;
324 for(int i=fullAlignIndex;i<fullAlignLength;i++){
326 newTemplateAlign += tempAln[i];//
330 int end = candAln.length()-1;
332 for(int i=0;i<candAln.length();i++){
333 if(candAln[i] == 'Z' || !isalnum(candAln[i])) { candAln[i] = '.'; } // if we padded the alignemnt from
334 else{ start = i; break; } // blast with Z's, change them to
337 for(int i=candAln.length()-1;i>=0;i--){ // ditto.
338 if(candAln[i] == 'Z' || !isalnum(candAln[i])) { candAln[i] = '.'; }
339 else{ end = i; break; }
342 for(int i=start;i<=end;i++){ // go through the candidate alignment sequence and make sure that
343 candAln[i] = toupper(candAln[i]); // everything is upper case
347 if(candAln.length() != tempAln.length()){ // if the regapped candidate sequence is longer than the official
348 removeExtraGaps(candAln, tempAln, newTemplateAlign);// template alignment then we need to do steps C-F in Fig.
349 } // 2 of Desantis et al.
351 candidateSeq->setAligned(candAln);
353 catch(exception& e) {
354 m->errorOut(e, "Nast", "regapSequences");
359 /**************************************************************************************************/
361 float Nast::getSimilarityScore(){
364 string cand = candidateSeq->getAligned();
365 string temp = templateSeq->getAligned();
366 int alignmentLength = temp.length();
370 for(int i=0;i<alignmentLength;i++){
371 if(cand[i] == '-' && temp[i] == '-'){
374 else if(cand[i] != '.' && temp[i] != '.'){
377 if(cand[i] != temp[i]){
382 float similarity = 100 * (1. - mismatch / (float)denominator);
383 if(denominator == 0){ similarity = 0.0000; }
388 catch(exception& e) {
389 m->errorOut(e, "Nast", "getSimilarityScore");
394 /**************************************************************************************************/
396 int Nast::getMaxInsertLength(){
398 return maxInsertLength;
402 /**************************************************************************************************/