File: needlemanoverlap.cpp

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (270 lines) | stat: -rwxr-xr-x 11,048 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 *  needleman.cpp
 *  
 *
 *  Created by Pat Schloss on 12/15/08.
 *  Copyright 2008 Patrick D. Schloss. All rights reserved.
 *
 *	This class is an Alignment child class that implements the Gotoh pairwise alignment algorithm as described in:
 *		
 *		Gotoh O. 1982.  An improved algorithm for matching biological sequences.  J. Mol. Biol.  162:705-8.
 *		Myers, EW & Miller, W.  1988.  Optimal alignments in linear space.  Comput Appl Biosci. 4:11-7.
 *
 *	This method is nice because it allows for an affine gap penalty to be assessed, which is analogous to what is used
 *	in blast and is an alternative to Needleman-Wunsch, which only charges the same penalty for each gap position.
 *	Because this method typically has problems at the ends when two sequences do not full overlap, we employ a separate
 *	method to fix the ends (see Overlap class documentation)
 *
 */

#include "alignmentcell.hpp"
#include "alignment.hpp"
#include "overlap.hpp"
#include "needlemanoverlap.hpp"

/**************************************************************************************************/

NeedlemanOverlap::NeedlemanOverlap(float gO, float f, float mm, int r) ://	note that we don't have a gap extend
gap(gO), match(f), mismatch(mm), Alignment(r) {							//	the gap openning penalty is assessed for
	try {																	//	every gapped position
		for(int i=1;i<nCols;i++){
			alignment[0][i].prevCell = 'l';					//	initialize first row by pointing all poiters to the left
			alignment[0][i].cValue = 0;						//	and the score to zero
		}
		
		for(int i=1;i<nRows;i++){
			alignment[i][0].prevCell = 'u';					//	initialize first column by pointing all poiters upwards
			alignment[i][0].cValue = 0;						//	and the score to zero
		}
	
	}
	catch(exception& e) {
		m->errorOut(e, "NeedlemanOverlap", "NeedlemanOverlap");
		exit(1);
	}
}
/**************************************************************************************************/

NeedlemanOverlap::~NeedlemanOverlap(){	/*	do nothing	*/	}

/**************************************************************************************************/

void NeedlemanOverlap::align(string A, string B, bool createBaseMap){
	try {
	
		seqA = ' ' + A;	lA = seqA.length();		//	algorithm requires a dummy space at the beginning of each string
		seqB = ' ' + B;	lB = seqB.length();		//	algorithm requires a dummy space at the beginning of each string

		if (lA > nRows) { m->mothurOut("One of your candidate sequences is longer than you longest template sequence. Your longest template sequence is " + toString(nRows) + ". Your candidate is " + toString(lA) + ".\n");   }
		
		for(int i=1;i<lB;i++){					//	This code was largely translated from Perl code provided in Ex 3.1 
		
			for(int j=1;j<lA;j++){				//	of the O'Reilly BLAST book.  I found that the example output had a
	
				//	number of errors
				float diagonal;
				if(seqB[i] == seqA[j])	{	diagonal = alignment[i-1][j-1].cValue + match;		}
				else					{	diagonal = alignment[i-1][j-1].cValue + mismatch;	}
			
				float up	= alignment[i-1][j].cValue + gap;
				float left	= alignment[i][j-1].cValue + gap;
				
				if(diagonal >= up){
					if(diagonal >= left){
						alignment[i][j].cValue = diagonal;
						alignment[i][j].prevCell = 'd';
					}
					else{
						alignment[i][j].cValue = left;
						alignment[i][j].prevCell = 'l';
					}
				}
				else{
					if(up >= left){
						alignment[i][j].cValue = up;
						alignment[i][j].prevCell = 'u';
					}
					else{
						alignment[i][j].cValue = left;
						alignment[i][j].prevCell = 'l';
					}
				}
			}
		}

		Overlap over;						
		over.setOverlap(alignment, lA, lB, 0);		//	Fix gaps at the beginning and end of the sequences
		traceBack(createBaseMap);								//	Traceback the alignment to populate seqAaln and seqBaln
	
	}
	catch(exception& e) {
		m->errorOut(e, "NeedlemanOverlap", "align");
		exit(1);
	}

}
/**************************************************************************************************/
//A is dna, B is protein
void NeedlemanOverlap::align(Sequence A, Protein B){
    try {
        string seq = A.getUnaligned();
        vector<string> seqA; seqA.push_back(" ");
        int extentionSize = 3 - (seq.length() % 3);
        for (int i = 0; i < extentionSize; i++) { seq += "."; } //add gaps
        
        for(int j = 0; j<seq.length();){
            string temp = ""; temp += seq[j]; j++; temp += seq[j]; j++; temp += seq[j]; j++;
            seqA.push_back(temp);
        }
        
        lA = seqA.size();
        
        vector<AminoAcid> seqB = B.getAligned();
        AminoAcid dummy('.');
        seqB.insert(seqB.begin(), dummy); lB = seqB.size();

        if (lA > nRows) { m->mothurOut("One of your unaligned sequence is longer than your protein sequence. Your longest protein sequence is " + toString(nRows) + ". Your candidate is " + toString(lA) + ".\n");   }
        
        for(int i=1;i<lB;i++){                    //    This code was largely translated from Perl code provided in Ex 3.1
        
            for(int j=1;j<lA;j++){                //    of the O'Reilly BLAST book.  I found that the example output had a
    
                //    number of errors
                float diagonal;
                
                AminoAcid codon(seqA[j]);
                if(seqB[i].getNum() == codon.getNum())      {    diagonal = alignment[i-1][j-1].cValue + match;        }
                else                                        {    diagonal = alignment[i-1][j-1].cValue + mismatch;      }
            
                float up    = alignment[i-1][j].cValue + gap;
                float left    = alignment[i][j-1].cValue + gap;
                
                if(diagonal >= up){
                    if(diagonal >= left){
                        alignment[i][j].cValue = diagonal;
                        alignment[i][j].prevCell = 'd';
                    }
                    else{
                        alignment[i][j].cValue = left;
                        alignment[i][j].prevCell = 'l';
                    }
                }
                else{
                    if(up >= left){
                        alignment[i][j].cValue = up;
                        alignment[i][j].prevCell = 'u';
                    }
                    else{
                        alignment[i][j].cValue = left;
                        alignment[i][j].prevCell = 'l';
                    }
                }
            }
        }

        Overlap over;
        over.setOverlap(alignment, lA, lB, 0);        //    Fix gaps at the beginning and end of the sequences
        
        proteinTraceBack(seqA, seqB);
    }
    catch(exception& e) {
        m->errorOut(e, "NeedlemanOverlap", "align");
        exit(1);
    }

}
/**************************************************************************************************/

void NeedlemanOverlap::alignPrimer(string A, string B){
	try {
        
		seqA = ' ' + A;	lA = seqA.length();		//	algorithm requires a dummy space at the beginning of each string
		seqB = ' ' + B;	lB = seqB.length();		//	algorithm requires a dummy space at the beginning of each string
        
		if (lA > nRows) { m->mothurOut("One of your candidate sequences is longer than you longest template sequence. Your longest template sequence is " + toString(nRows) + ". Your candidate is " + toString(lA) + ".\n");   }
		
		for(int i=1;i<lB;i++){					//	This code was largely translated from Perl code provided in Ex 3.1
            
			for(int j=1;j<lA;j++){				//	of the O'Reilly BLAST book.  I found that the example output had a
                
				//	number of errors
				float diagonal;
				if(isEquivalent(seqB[i],seqA[j]))	{	diagonal = alignment[i-1][j-1].cValue + match;		}
				else                                {	diagonal = alignment[i-1][j-1].cValue + mismatch;	}
                
				float up	= alignment[i-1][j].cValue + gap;
				float left	= alignment[i][j-1].cValue + gap;
				
				if(diagonal >= up){
					if(diagonal >= left){
						alignment[i][j].cValue = diagonal;
						alignment[i][j].prevCell = 'd';
					}
					else{
						alignment[i][j].cValue = left;
						alignment[i][j].prevCell = 'l';
					}
				}
				else{
					if(up >= left){
						alignment[i][j].cValue = up;
						alignment[i][j].prevCell = 'u';
					}
					else{
						alignment[i][j].cValue = left;
						alignment[i][j].prevCell = 'l';
					}
				}
			}
		}
        
		Overlap over;
		over.setOverlap(alignment, lA, lB, 0);		//	Fix gaps at the beginning and end of the sequences
		traceBack(false);								//	Traceback the alignment to populate seqAaln and seqBaln
        
	}
	catch(exception& e) {
		m->errorOut(e, "NeedlemanOverlap", "alignPrimer");
		exit(1);
	}
    
}
//********************************************************************/
bool NeedlemanOverlap::isEquivalent(char oligo, char seq){
	try {
		
        bool same = true;
					
        oligo = toupper(oligo);
        seq = toupper(seq);
        
        if(oligo != seq){
            if(oligo == 'A' && (seq != 'A' && seq != 'M' && seq != 'R' && seq != 'W' && seq != 'D' && seq != 'H' && seq != 'V'))       {	same = false;	}
            else if(oligo == 'C' && (seq != 'C' && seq != 'Y' && seq != 'M' && seq != 'S' && seq != 'B' && seq != 'H' && seq != 'V'))       {	same = false;	}
            else if(oligo == 'G' && (seq != 'G' && seq != 'R' && seq != 'K' && seq != 'S' && seq != 'B' && seq != 'D' && seq != 'V'))       {	same = false;	}
            else if(oligo == 'T' && (seq != 'T' && seq != 'Y' && seq != 'K' && seq != 'W' && seq != 'B' && seq != 'D' && seq != 'H'))       {	same = false;	}
            else if((oligo == '.' || oligo == '-'))           {	same = false;	}
            else if((oligo == 'N' || oligo == 'I') && (seq == 'N'))                         {	same = false;	}
            else if(oligo == 'R' && (seq != 'A' && seq != 'G'))                        {	same = false;	}
            else if(oligo == 'Y' && (seq != 'C' && seq != 'T'))                        {	same = false;	}
            else if(oligo == 'M' && (seq != 'C' && seq != 'A'))                        {	same = false;	}
            else if(oligo == 'K' && (seq != 'T' && seq != 'G'))                        {	same = false;	}
            else if(oligo == 'W' && (seq != 'T' && seq != 'A'))                        {	same = false;	}
            else if(oligo == 'S' && (seq != 'C' && seq != 'G'))                        {	same = false;	}
            else if(oligo == 'B' && (seq != 'C' && seq != 'T' && seq != 'G'))       {	same = false;	}
            else if(oligo == 'D' && (seq != 'A' && seq != 'T' && seq != 'G'))       {	same = false;	}
            else if(oligo == 'H' && (seq != 'A' && seq != 'T' && seq != 'C'))       {	same = false;	}
            else if(oligo == 'V' && (seq != 'A' && seq != 'C' && seq != 'G'))       {	same = false;	}
        }

		
		
		return same;
	}
	catch(exception& e) {
		m->errorOut(e, "TrimOligos", "countDiffs");
		exit(1);
	}
}
/**************************************************************************************************/