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) {
27 pairwiseAlignSeqs(); // This is part A in Fig. 2 of DeSantis et al.
28 regapSequences(); // This is parts B-F in Fig. 2 of DeSantis et al.
32 errorOut(e, "Nast", "Nast");
38 /**************************************************************************************************/
40 void Nast::pairwiseAlignSeqs(){ // Here we call one of the pairwise alignment methods to align our unaligned candidate
41 // and template sequences
44 alignment->align(candidateSeq->getUnaligned(), templateSeq->getUnaligned());
46 string candAln = alignment->getSeqAAln();
47 string tempAln = alignment->getSeqBAln();
51 candidateSeq->setPairwise("");
52 templateSeq->setPairwise(templateSeq->getUnaligned());
56 if(tempAln[0] == '-'){
57 int pairwiseAlignmentLength = tempAln.length(); // we need to make sure that the candidate sequence alignment
58 for(int i=0;i<pairwiseAlignmentLength;i++){ // starts where the template sequence alignment starts, if it
59 if(isalpha(tempAln[i])){ // starts early, we nuke the beginning of the candidate
60 candAln = candAln.substr(i); // sequence
61 tempAln = tempAln.substr(i);
66 int pairwiseAlignmentLength = tempAln.length();
67 if(tempAln[pairwiseAlignmentLength-1] == '-'){ // we need to make sure that the candidate sequence alignment
68 for(int i=pairwiseAlignmentLength-1; i>=0; i--){// ends where the template sequence alignment ends, if it runs
69 if(isalpha(tempAln[i])){ // long, we nuke the end of the candidate sequence
70 candAln = candAln.substr(0,i+1);
71 tempAln = tempAln.substr(0,i+1);
79 candidateSeq->setPairwise(candAln); // set the pairwise sequences in the Sequence objects for
80 templateSeq->setPairwise(tempAln); // the candidate and template sequences
84 errorOut(e, "Nast", "pairwiseAlignSeqs");
89 /**************************************************************************************************/
91 void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAlign){
93 // here we do steps C-F of Fig. 2 from DeSantis et al.
96 int longAlignmentLength = newTemplateAlign.length();
98 for(int i=0; i<longAlignmentLength; i++){ // use the long alignment as the standard
99 int rightIndex, rightRoom, leftIndex, leftRoom;
101 // Part C of Fig. 2 from DeSantis et al.
102 if((isalpha(newTemplateAlign[i]) != isalpha(tempAln[i]))){ //if there is a discrepancy between the regapped
104 rightRoom = 0; leftRoom = 0;
106 // Part D of Fig. 2 from DeSantis et al. // template sequence and the official template sequence
107 for(leftIndex=i-1;leftIndex>0;leftIndex--){ // then we've got problems...
108 if(!isalpha(candAln[leftIndex])){
109 leftRoom = 1; //count how far it is to the nearest gap on the LEFT side of the anomaly
110 while(leftIndex-leftRoom>=0 && !isalpha(candAln[leftIndex-leftRoom])) { leftRoom++; }
115 for(rightIndex=i+1;rightIndex<longAlignmentLength-1;rightIndex++){
116 if(!isalpha(candAln[rightIndex])){
117 rightRoom = 1; //count how far it is to the nearest gap on the RIGHT side of the anomaly
118 while(rightIndex+rightRoom<longAlignmentLength && !isalpha(candAln[rightIndex+rightRoom])) { rightRoom++; }
123 int insertLength = 0; // figure out how long the anomaly is
124 while(!isalpha(newTemplateAlign[i + insertLength])) { insertLength++; }
125 if(insertLength > maxInsertLength){ maxInsertLength = insertLength; }
127 if((leftRoom + rightRoom) >= insertLength){
129 // Parts D & E from Fig. 2 of DeSantis et al.
130 if((i-leftIndex) <= (rightIndex-i)){ // the left gap is closer - > move stuff left there's
132 if(leftRoom >= insertLength){ // enough room to the left to move
134 string leftTemplateString = newTemplateAlign.substr(0,i);
135 string rightTemplateString = newTemplateAlign.substr(i+insertLength);
136 newTemplateAlign = leftTemplateString + rightTemplateString;
137 longAlignmentLength = newTemplateAlign.length();
139 string leftCandidateString = candAln.substr(0,leftIndex-insertLength+1);
140 string rightCandidateString = candAln.substr(leftIndex+1);
141 candAln = leftCandidateString + rightCandidateString;
144 else{ // not enough room to the left, have to steal some space to
145 string leftTemplateString = newTemplateAlign.substr(0,i); // the right
146 string rightTemplateString = newTemplateAlign.substr(i+insertLength);
147 newTemplateAlign = leftTemplateString + rightTemplateString;
148 longAlignmentLength = newTemplateAlign.length();
150 string leftCandidateString = candAln.substr(0,leftIndex-leftRoom+1);
151 string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
152 string rightCandidateString = candAln.substr(rightIndex+(insertLength-leftRoom));
153 candAln = leftCandidateString + insertString + rightCandidateString;
157 else{ // the right gap is closer - > move stuff right there's
158 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
172 // space to the left lets move left and then right...
173 string leftTemplateString = newTemplateAlign.substr(0,i);
174 string rightTemplateString = newTemplateAlign.substr(i+insertLength);
175 newTemplateAlign = leftTemplateString + rightTemplateString;
176 longAlignmentLength = newTemplateAlign.length();
178 string leftCandidateString = candAln.substr(0,leftIndex-(insertLength-rightRoom)+1);
179 string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
180 string rightCandidateString = candAln.substr(rightIndex+rightRoom);
181 candAln = leftCandidateString + insertString + rightCandidateString;
187 // there could be a case where there isn't enough room in
188 string leftTemplateString = newTemplateAlign.substr(0,i); // either direction to move stuff
189 string rightTemplateString = newTemplateAlign.substr(i+leftRoom+rightRoom);
190 newTemplateAlign = leftTemplateString + rightTemplateString;
191 longAlignmentLength = newTemplateAlign.length();
193 string leftCandidateString = candAln.substr(0,leftIndex-leftRoom+1);
194 string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
195 string rightCandidateString = candAln.substr(rightIndex+rightRoom);
196 candAln = leftCandidateString + insertString + rightCandidateString;
204 catch(exception& e) {
205 errorOut(e, "Nast", "removeExtraGaps");
210 /**************************************************************************************************/
212 void Nast::regapSequences(){ //This is essentially part B in Fig 2. of DeSantis et al.
215 string candPair = candidateSeq->getPairwise();
218 string tempPair = templateSeq->getPairwise();
219 string tempAln = templateSeq->getAligned(); // we use the template aligned sequence as our guide
221 int pairwiseLength = candPair.length();
222 int fullAlignLength = tempAln.length();
225 for(int i=0;i<fullAlignLength;i++) { candAln += '.'; }
226 candidateSeq->setAligned(candAln);
230 int fullAlignIndex = 0;
231 int pairwiseAlignIndex = 0;
232 string newTemplateAlign = ""; // this is going to be messy so we want a temporary template
234 while(tempAln[fullAlignIndex] == '.' || tempAln[fullAlignIndex] == '-'){
235 candAln += '.'; // add the initial '-' and '.' to the candidate and template
236 newTemplateAlign += tempAln[fullAlignIndex];// pairwise sequences
240 string lastLoop = "";
242 while(pairwiseAlignIndex<pairwiseLength){
243 if(isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
244 && isalpha(candPair[pairwiseAlignIndex])){
245 // the template and candidate pairwise and template aligned have characters
246 // need to add character onto the candidatSeq.aligned sequence
248 candAln += candPair[pairwiseAlignIndex];
249 newTemplateAlign += tempPair[pairwiseAlignIndex];//
251 pairwiseAlignIndex++;
254 else if(isalpha(tempPair[pairwiseAlignIndex]) && !isalpha(tempAln[fullAlignIndex])
255 && isalpha(candPair[pairwiseAlignIndex])){
256 // the template pairwise and candidate pairwise are characters and the template aligned is a gap
257 // need to insert gaps into the candidateSeq.aligned sequence
260 newTemplateAlign += '-';//
263 else if(!isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
264 && isalpha(candPair[pairwiseAlignIndex])){
265 // the template pairwise is a gap and the template aligned and pairwise sequences have characters
266 // this is the alpha scenario. add character to the candidateSeq.aligned sequence without progressing
267 // further through the tempAln sequence.
269 candAln += candPair[pairwiseAlignIndex];
270 newTemplateAlign += '-';//
271 pairwiseAlignIndex++;
273 else if(isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
274 && !isalpha(candPair[pairwiseAlignIndex])){
275 // the template pairwise and full alignment are characters and the candidate sequence has a gap
276 // should not be a big deal, just add the gap position to the candidateSeq.aligned sequence;
278 candAln += candPair[pairwiseAlignIndex];
279 newTemplateAlign += tempAln[fullAlignIndex];//
281 pairwiseAlignIndex++;
283 else if(!isalpha(tempPair[pairwiseAlignIndex]) && !isalpha(tempAln[fullAlignIndex])
284 && isalpha(candPair[pairwiseAlignIndex])){
285 // the template pairwise and aligned are gaps while the candidate pairwise has a character
286 // this would be an insertion, go ahead and add the character->seems to be the opposite of the alpha scenario
288 candAln += candPair[pairwiseAlignIndex];
289 newTemplateAlign += tempAln[fullAlignIndex];//
290 pairwiseAlignIndex++;
293 else if(isalpha(tempPair[pairwiseAlignIndex]) && !isalpha(tempAln[fullAlignIndex])
294 && !isalpha(candPair[pairwiseAlignIndex])){
295 // template pairwise has a character, but its full aligned sequence and candidate sequence have gaps
296 // this would happen like we need to add a gap. basically the opposite of the alpha situation
298 newTemplateAlign += tempAln[fullAlignIndex];//
302 else if(!isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
303 && !isalpha(candPair[pairwiseAlignIndex])){
304 // template and candidate pairwise are gaps and the template aligned is not a gap this should not be possible
305 // would skip the gaps and not progress through full alignment sequence
308 mothurOut("We're into D " + toString(fullAlignIndex) + " " + toString(pairwiseAlignIndex)); mothurOutEndLine();
309 pairwiseAlignIndex++;
312 // everything has a gap - not possible
315 mothurOut("We're into F " + toString(fullAlignIndex) + " " + toString(pairwiseAlignIndex)); mothurOutEndLine();
316 pairwiseAlignIndex++;
321 for(int i=fullAlignIndex;i<fullAlignLength;i++){
323 newTemplateAlign += tempAln[i];//
327 int end = candAln.length()-1;
329 for(int i=0;i<candAln.length();i++){
330 if(candAln[i] == 'Z' || !isalnum(candAln[i])) { candAln[i] = '.'; } // if we padded the alignemnt from
331 else{ start = i; break; } // blast with Z's, change them to
334 for(int i=candAln.length()-1;i>=0;i--){ // ditto.
335 if(candAln[i] == 'Z' || !isalnum(candAln[i])) { candAln[i] = '.'; }
336 else{ end = i; break; }
339 for(int i=start;i<=end;i++){ // go through the candidate alignment sequence and make sure that
340 candAln[i] = toupper(candAln[i]); // everything is upper case
344 if(candAln.length() != tempAln.length()){ // if the regapped candidate sequence is longer than the official
345 removeExtraGaps(candAln, tempAln, newTemplateAlign);// template alignment then we need to do steps C-F in Fig.
346 } // 2 of Desantis et al.
348 candidateSeq->setAligned(candAln);
350 catch(exception& e) {
351 errorOut(e, "Nast", "regapSequences");
356 /**************************************************************************************************/
358 float Nast::getSimilarityScore(){
361 string cand = candidateSeq->getAligned();
362 string temp = templateSeq->getAligned();
363 int alignmentLength = temp.length();
367 for(int i=0;i<alignmentLength;i++){
368 if(cand[i] == '-' && temp[i] == '-'){
371 else if(cand[i] != '.' && temp[i] != '.'){
374 if(cand[i] != temp[i]){
379 float similarity = 100 * (1. - mismatch / (float)denominator);
380 if(denominator == 0){ similarity = 0.0000; }
385 catch(exception& e) {
386 errorOut(e, "Nast", "getSimilarityScore");
391 /**************************************************************************************************/
393 int Nast::getMaxInsertLength(){
395 return maxInsertLength;
399 /**************************************************************************************************/