-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApproximateAlignment.H
875 lines (737 loc) · 24.4 KB
/
ApproximateAlignment.H
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
/***********************************************************************
ApproximateAlignment.H
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
***********************************************************************/
#ifndef INCL_ApproximateAlignment_H
#define INCL_ApproximateAlignment_H
#include <iostream>
#include <fstream>
#include "SubstitutionMatrix.H"
#include "MatchType.H"
#include "Vector.H"
#include "Sequence.H"
#include "ProteinTrans.H"
#include "AminoAlphabet.H"
#include "DnaAlphabet.H"
#include "StringMap.H"
#include "MolecularSequenceType.H"
#include "Constants.H"
//#define SUM_BLOSUM_SCORES
/****************************************************************
class ApproxAlignCell
A single cell lying on the optimal path in the DP alignment matrix.
Stores length and match count information to allow rapid computation
of alignment scores of sub-regions of the alignment (for approximate
alignment purposes).
*/
namespace BOOM {
class ApproxAlignCell
{
public:
ApproxAlignCell();
ApproxAlignCell(int x,int y,MatchType,float matches,int length);
inline int getX() const;
inline int getY() const;
inline MatchType getMatchType() const;
inline float cumulativeMatches() const;
inline int cumulativeLength() const;
void setCumulatives(float matches,int length);
inline int lengthDiff(const ApproxAlignCell &other) const;
private:
int x, y;
MatchType matchType;
float matches; // sum of matches from beginning of alignment
int length; // alignment length up to this point
};
/****************************************************************
class ApproximateAlignment
Base class for ApproxNucAlignment and ApproxAminoAlignment. Con-
sists of a list of cells which are the cells in the DP alignment
matrix on the optimal solution. The score along any stretch of
the alignment can be computed in constant time once the two cells
delimiting the region are know, through simple subtraction. Can
also provide an approximate alignment score between two DP cells
not falling on the optimal path represented here, by adding in
the minimum number of indels needed to get onto the optimal path.
*/
template<class FLOAT_OR_DOUBLE>
class ApproximateAlignment
{
public:
ApproximateAlignment(const SubstitutionMatrix<FLOAT_OR_DOUBLE> &,
const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2,
MolecularSequenceType);
int getAlignmentLength() const;
FLOAT_OR_DOUBLE getMatches() const;
float getPercentIdentity()
{return getMatches()/(float)getAlignmentLength();}
int getBeginX() const;
int getEndX() const;
int getBeginY() const;
int getEndY() const;
const ApproxAlignCell &getCell(int index) const;
double score(const ApproxAlignCell &from,const ApproxAlignCell &to,
int extraIndels) const;
double score(int beginX,int beginY,int endX,int endY) const;
double show(int beginX,int beginY,int endX,int endY,
const Sequence &xSeq,const char *xSeqPtr,
const Sequence &ySeq,const char *ySeqPtr);//debugging
MolecularSequenceType getSequenceType() const {return seqType;}
protected:
const SubstitutionMatrix<FLOAT_OR_DOUBLE> &M;
Vector<ApproxAlignCell> cells;
MolecularSequenceType seqType;
virtual Symbol getResidue(const Sequence &,const char *sequenceString,
int index)=0;
virtual int getIncrement() const=0;
void load(const String &filename,
const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2,
int basesPerSymbol);
void load(istream &,
const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2,
int basesPerSymbol);
const ApproxAlignCell &findNearestViaX(int x,int y,int &distance) const;
const ApproxAlignCell &findNearestViaY(int x,int y,int &distance) const;
void boundsChecks(int beginX,int endX,int beginY,
int endY,int len1,int len2,int line);
void boundsCheck(int x,int maxVal,int line);
};
/****************************************************************
class ApproxNucAlignment
An approximate alignment in nucleotide space. Each cell represents
a pair of nucleotides aligned on the optimal DP alignment path.
*/
template<class FLOAT_OR_DOUBLE>
class ApproxNucAlignment : public ApproximateAlignment<FLOAT_OR_DOUBLE>
{
public:
ApproxNucAlignment(const String &filename,
const SubstitutionMatrix<FLOAT_OR_DOUBLE> &,
const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2);
ApproxNucAlignment(istream &,
const SubstitutionMatrix<FLOAT_OR_DOUBLE> &,
const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2);
protected:
virtual Symbol getResidue(const Sequence &,const char *seqStr,
int index);
virtual int getIncrement() const {return 3;}
};
/****************************************************************
class ApproxAminoAlignment
An approximate alignment in amino acid space. Each cell corres-
ponds to a pair of amino acids aligned in the optimal DP alignment
path. Note that while each cell corresponds to a pair of amino
acids, a cell's (x,y) coordinates are in nucleotide space -- ie.,
coordinates of adjacent cells differ by 0 or 3.
*/
template<class FLOAT_OR_DOUBLE>
class ApproxAminoAlignment : public ApproximateAlignment<FLOAT_OR_DOUBLE>
{
public:
ApproxAminoAlignment(const String &filename,
const SubstitutionMatrix<FLOAT_OR_DOUBLE> &,
const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2,
StringMap<char> &stopCodons);
ApproxAminoAlignment(istream &,
const SubstitutionMatrix<FLOAT_OR_DOUBLE> &,
const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2,
StringMap<char> &stopCodons);
static ApproxAminoAlignment *load(istream &,
const SubstitutionMatrix<FLOAT_OR_DOUBLE> &,
const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2,
StringMap<char> &stopCodons);
protected:
void extend(const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2,
StringMap<char> &stopCodons);
void extendLeft(const char *s1,const char *s2,
StringMap<char> &stopCodons);
void extendRight(const char *s1,int len1,const char *s2,int len2,
StringMap<char> &stopCodons);
void recomputeCumulatives(const Sequence &seq1,
const char *seqStr1,
const Sequence &seq2,
const char *seqStr2);
virtual Symbol getResidue(const Sequence &,const char *sequenceString,
int index);
virtual int getIncrement() const {return 3;}
};
}
using namespace BOOM;
/****************************************************************
inline ApproxAlignCell methods
*/
int ApproxAlignCell::getX() const
{
return x;
}
int ApproxAlignCell::getY() const
{
return y;
}
int ApproxAlignCell::lengthDiff(const ApproxAlignCell &other) const
{
return abs(other.length-length);
}
MatchType ApproxAlignCell::getMatchType() const
{
return matchType;
}
float ApproxAlignCell::cumulativeMatches() const
{
return matches;
}
int ApproxAlignCell::cumulativeLength() const
{
return length;
}
/****************************************************************
ApproximateAlignment methods
*/
template<class T>
ApproximateAlignment<T>::ApproximateAlignment(const SubstitutionMatrix<T> &M,
const Sequence &seq1,
const char *seqStr1,
const Sequence &seq2,
const char *seqStr2,
MolecularSequenceType t)
: M(M), seqType(t)
{
}
template<class T>
int ApproximateAlignment<T>::getAlignmentLength() const
{
return cells.size();
}
template<class T>
T ApproximateAlignment<T>::getMatches() const
{
return cells[cells.size()-1].cumulativeMatches();
}
template<class T>
int ApproximateAlignment<T>::getBeginX() const
{
return cells[0].getX();
}
template<class T>
int ApproximateAlignment<T>::getEndX() const
{
return cells[cells.size()-1].getX();
}
template<class T>
int ApproximateAlignment<T>::getBeginY() const
{
return cells[0].getY();
}
template<class T>
int ApproximateAlignment<T>::getEndY() const
{
return cells[cells.size()-1].getY();
}
template<class T>
const ApproxAlignCell &ApproximateAlignment<T>::getCell(int index) const
{
return cells[index];
}
template<class T>
double ApproximateAlignment<T>::score(const ApproxAlignCell &from,
const ApproxAlignCell &to,
int extraIndels) const
{
throw "I don't think this function actually gets called anywhere";
}
template<class T>
double ApproximateAlignment<T>::show(int beginX,int beginY,
int endX,int endY,
const Sequence &xSeq,
const char *xSeqPtr,
const Sequence &ySeq,
const char *ySeqPtr)
{
cout<<"ALIGN("<<beginX<<","<<beginY<<","<<endX<<","<<endY<<") using ("
<<cells[0].getX()<<","<<cells[0].getY()<<")-("
<<cells[cells.size()-1].getX()<<","<<cells[cells.size()-1].getY()
<<")"<<endl;
int indels, dA=0, dB=0, dC=0, dD=0;
//### can optimize these initializations away...
// Find the two nearest cells (vertically,horizontally) for start
const ApproxAlignCell &A=findNearestViaX(beginX,beginY,dA);
const ApproxAlignCell &B=findNearestViaY(beginX,beginY,dB);
// Find the two nearest cells for end
const ApproxAlignCell &C=findNearestViaX(endX,endY,dC);
const ApproxAlignCell &D=findNearestViaY(endX,endY,dD);
// Choose cell A or B, whichever is closer to C
// (or D...doesn't much matter)
int dAC=A.lengthDiff(C), dBC=B.lengthDiff(C);
const ApproxAlignCell *beginCell, *endCell;
if(dAC<dBC) {beginCell=&A; indels=dA;cout<<"\t+"<<dA<<" indels"<<endl;}
else {beginCell=&B; indels=dB;cout<<"\t+"<<dB<<" indels"<<endl;}
// Choose cell C or D, whichever is closer to A
int dAD=A.lengthDiff(D);
if(dAD<dAC) {endCell=&D; indels+=dD;cout<<"\t+"<<dD<<" indels"<<endl;}
else {endCell=&C; indels+=dC;cout<<"\t+"<<dC<<" indels"<<endl;}
cout<<"\tbeginCell=("<<beginCell->getX()<<","<<beginCell->getY()<<")"<<endl;
cout<<"\tendCell=("<<endCell->getX()<<","<<endCell->getY()<<")"<<endl;
cout<<"\tindels="<<indels<<endl;
// ### ARE THESE SUBTRACTIONS RIGHT, OR IS THERE A ONE-OFF ERROR?
double matches=
endCell->cumulativeMatches()-beginCell->cumulativeMatches();
double length=endCell->cumulativeLength()-beginCell->cumulativeLength();
cout<<"\tmatches="<<matches<<", length="<<length<<endl;
if(length<=0)
{
// These two points are too far from the optimal alignment path
// to use it for approximating their alignment score, so we'll
// just report the score expected from a random pair of sequences
int xLen=endX-beginX, yLen=endY-beginY;
int minLen, maxLen;
if(xLen<yLen) {minLen=xLen;maxLen=yLen;}
else {minLen=yLen;maxLen=xLen;}
matches=M.percentageGreaterThanZero()*minLen;
length=maxLen;
}
else
{
length+=indels;
int n=cells.size(), i, j;
Alphabet &alphabet=AminoAlphabet::global;
String topLine, bottomLine;
for(i=0 ; i<n ; ++i) if(&cells[i]==beginCell) break;
for(j=i ; j<n ; ++j) if(&cells[j]==endCell) break;
for(int k=i ; k<=j ; ++k)
{
ApproxAlignCell &cell=cells[k];
int x=cell.getX(), y=cell.getY();
cout<<"("<<x<<","<<y<<") ";
Symbol residueX=getResidue(xSeq,xSeqPtr,x);
Symbol residueY=getResidue(ySeq,ySeqPtr,y);
switch(cell.getMatchType())
{
case MATCH:
topLine+=alphabet.lookup(residueX);
bottomLine+=alphabet.lookup(residueY);
cout<<"MATCH cm="<<cell.cumulativeMatches()
<<" cl="<<cell.cumulativeLength()<<endl;
break;
case FIRST_UNMATCHED:
topLine+=alphabet.lookup(residueX);
bottomLine+='-';
cout<<"INS cm="<<cell.cumulativeMatches()
<<" cl="<<cell.cumulativeLength()<<endl;
break;
case SECOND_UNMATCHED:
topLine+='-';
bottomLine+=alphabet.lookup(residueY);
cout<<"DEL cm="<<cell.cumulativeMatches()
<<" cl="<<cell.cumulativeLength()<<endl;
break;
}
}
cout<<topLine<<"\n"<<bottomLine<<endl;
}
if(length==0) return 1;
return 0;//### make this void...
}
template<class T>
double ApproximateAlignment<T>::score(int beginX,int beginY,
int endX,int endY)
const
{
int indels, dA=0, dB=0, dC=0, dD=0, INC=getIncrement();
//### can optimize these initializations away...
// Find the two nearest cells (vertically,horizontally) for start
const ApproxAlignCell &A=findNearestViaX(beginX,beginY,dA);
const ApproxAlignCell &B=findNearestViaY(beginX,beginY,dB);
//###dA/=INC; dB/=INC;
// Find the two nearest cells for end
const ApproxAlignCell &C=findNearestViaX(endX,endY,dC);
const ApproxAlignCell &D=findNearestViaY(endX,endY,dD);
//### dC/=INC; dD/=INC;
// Choose cell A or B, whichever is closer to C
// (or D...doesn't much matter)
int dAC=A.lengthDiff(C), dBC=B.lengthDiff(C);
const ApproxAlignCell *beginCell, *endCell;
if(dAC<dBC) {beginCell=&A; indels=dA;}
else {beginCell=&B; indels=dB;}
// Choose cell C or D, whichever is closer to A
int dAD=A.lengthDiff(D);
if(dAD<dAC) {endCell=&D; indels+=dD;}
else {endCell=&C; indels+=dC;}
// ### ARE THESE SUBTRACTIONS RIGHT, OR IS THERE A ONE-OFF ERROR?
double matches=
endCell->cumulativeMatches()-beginCell->cumulativeMatches();
double length=endCell->cumulativeLength()-beginCell->cumulativeLength();
if(length<=0)
{
// These two points are too far from the optimal alignment path
// to use it for approximating their alignment score, so we'll
// just report the score expected from a random pair of sequences
int xLen=endX-beginX, yLen=endY-beginY;
int minLen, maxLen;
if(xLen<yLen) {minLen=xLen;maxLen=yLen;}
else {minLen=yLen;maxLen=xLen;}
#ifdef SUM_BLOSUM_SCORES
int numMatch, numMismatch;
if(seqType==DNA) numMatch=int(0.25*minLen+0.5);
else numMatch=int(0.05*minLen+0.5);
numMismatch=minLen-numMatch;
matches=numMatch*M.largestEntry()+numMismatch*M.smallestEntry();
return matches;
#else
matches=M.percentageGreaterThanZero()*minLen;
#endif
length=maxLen;
}
#ifdef SUM_BLOSUM_SCORES
return matches;
#else
else length+=indels;
if(length==0) return 1;
return matches/length;
#endif
}
template<class T>
const ApproxAlignCell &ApproximateAlignment<T>::findNearestViaX(int x,int y,
int &distance)
const
{
// First, perform a binary search for any cell at this x coordinate
const int n=cells.size();
int begin=0, end=n;
while(begin<end)
{
int mid=(begin+end)/2;
const ApproxAlignCell &midCell=cells[mid];
if(x>midCell.getX()) begin=mid+1;
else end=mid;
}
// Back up to the leftmost cell having this x coordinate
while(begin && cells[begin-1].getX()==x) --begin;
if(begin>=n) --begin;
// Now consider all the cells in this run having the same x
// coordinate, and choose the nearest
int bestDistance=abs(cells[begin].getY()-y), bestIndex=begin;
for(int i=begin+1 ; i<n ; ++i)
{
const ApproxAlignCell &thisCell=cells[i];
if(thisCell.getX()!=x) break;
int distance=abs(thisCell.getY()-y);
if(distance<bestDistance) {bestDistance=distance; bestIndex=i;}
}
distance=bestDistance;
return cells[bestIndex];
}
template<class T>
const ApproxAlignCell &ApproximateAlignment<T>::findNearestViaY(int x,int y,
int &distance)
const
{
// First, perform a binary search for any cell at this y coordinate
const int n=cells.size();
int begin=0, end=n;
while(begin<end)
{
int mid=(begin+end)/2;
const ApproxAlignCell &midCell=cells[mid];
if(y>midCell.getY()) begin=mid+1;
else end=mid;
}
// Back up to the leftmost cell having this y coordinate
while(begin && cells[begin-1].getY()==y) --begin;
if(begin>=n) --begin;
// Now consider all the cells in this run having the same
// x coordinate, and choose the nearest
int bestDistance=abs(cells[begin].getX()-x), bestIndex=begin;
for(int i=begin+1 ; i<n ; ++i)
{
const ApproxAlignCell &thisCell=cells[i];
if(thisCell.getY()!=y) break;
int distance=abs(thisCell.getX()-x);
if(distance<bestDistance) {bestDistance=distance; bestIndex=i;}
}
distance=bestDistance;
return cells[bestIndex];
}
template<class T>
void ApproximateAlignment<T>::load(const String &filename,
const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2,
int basesPerSymbol)
{
ifstream is(filename.c_str());
if(!is.good())
throw String("Error loading approximate alignment from file ")+
filename;
load(is,seq1,seqStr1,seq2,seqStr2,basesPerSymbol);
}
template<class T>
void ApproximateAlignment<T>::boundsChecks(int beginX,int endX,int beginY,
int endY,int len1,int len2,
int line)
{
boundsCheck(beginX,len1,line);
boundsCheck(endX,len1,line);
boundsCheck(beginY,len2,line);
boundsCheck(endY,len2,line);
}
template<class T>
void ApproximateAlignment<T>::boundsCheck(int x,int maxVal,int line)
{
if(x<0 || x>=maxVal) throw String(x)+" out of bounds in ApproximateAlignment (line "+line+"), max is "+maxVal+"\nHINT: DID YOU GIVE THE COMMAND-LINE ARGUMENTS IN THE WRONG ORDER?\n";
}
template<class T>
void ApproximateAlignment<T>::load(istream &is,
const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2,
int basesPerSymbol)
{
const int INC=basesPerSymbol;
int dummy, beginX, endX, beginY, endY;
is >> beginY;
if(is.eof()) return;
is >> endY >> beginX >> endX >> dummy >> dummy >> dummy;
int len1=seq1.getLength(), len2=seq2.getLength();
boundsChecks(beginX-1,endX-1,beginY-1,endY-1,len2,len1,__LINE__);
char op;
int len, x=beginX-1-INC, y=beginY-1-INC, dX, dY, cumLen=0;
T matches=0;
MatchType matchType;
while(!is.eof())
{
is >> op >> len; // NOTE: len is *always* in nucleotides!
if(is.eof()) break;
switch(op)
{
case 'i':
matchType=SECOND_UNMATCHED;
dX=0;
dY=INC;
break;
case 'd':
matchType=FIRST_UNMATCHED;
dX=INC;
dY=0;
break;
case 'm':
matchType=MATCH;
dX=INC;
dY=INC;
break;
case 'e': return;
}
for(int i=0 ; i<len ; i+=INC)
{
if(matchType==MATCH)
{
boundsCheck(y+INC,len1,__LINE__);
boundsCheck(x+INC,len2,__LINE__);
Symbol s1=getResidue(seq1,seqStr1,y+INC);
Symbol s2=getResidue(seq2,seqStr2,x+INC);
if(M(s1,s2)>0) ++matches;
}
cumLen++;
x+=dX;
y+=dY;
cells.push_back(ApproxAlignCell(y,x,matchType,matches,cumLen));
}
}
}
/****************************************************************
ApproxNucAlignment methods
*/
template<class T>
ApproxNucAlignment<T>::ApproxNucAlignment(const String &filename,
const SubstitutionMatrix<T> &M,
const Sequence &seq1,
const char *seqStr1,
const Sequence &seq2,
const char *seqStr2)
: ApproximateAlignment<T>(M,seq1,seqStr1,seq2,seqStr2,DNA)
{
load(filename,seq1,seqStr1,seq2,seqStr2,1);
}
template<class T>
ApproxNucAlignment<T>::ApproxNucAlignment(istream &is,
const SubstitutionMatrix<T> &M,
const Sequence &seq1,
const char *seqStr1,
const Sequence &seq2,
const char *seqStr2)
: ApproximateAlignment<T>(M,seq1,seqStr1,seq2,seqStr2)
{
load(is,seq1,seqStr1,seq2,seqStr2,1);
}
template<class T>
Symbol ApproxNucAlignment<T>::getResidue(const Sequence &seq,
const char *seqStr,
int index)
{
return seq[index];
}
/****************************************************************
ApproxAminoAlignment methods
*/
template<class T>
ApproxAminoAlignment<T>::ApproxAminoAlignment(const String &filename,
const SubstitutionMatrix<T> &M,
const Sequence &seq1,
const char *seqStr1,
const Sequence &seq2,
const char *seqStr2,
StringMap<char> &stopCodons)
: ApproximateAlignment<T>(M,seq1,seqStr1,seq2,seqStr2,PROTEIN)
{
ApproximateAlignment<T>::load(filename,seq1,seqStr1,seq2,seqStr2,3);
if(cells.size())
extend(seq1,seqStr1,seq2,seqStr2,stopCodons);
}
template<class T>
ApproxAminoAlignment<T>::ApproxAminoAlignment(istream &is,
const SubstitutionMatrix<T> &M,
const Sequence &seq1,
const char *seqStr1,
const Sequence &seq2,
const char *seqStr2,
StringMap<char> &
stopCodons)
: ApproximateAlignment<T>(M,seq1,seqStr1,seq2,seqStr2,PROTEIN)
{
ApproximateAlignment<T>::load(is,seq1,seqStr1,seq2,seqStr2,3);
if(cells.size())
extend(seq1,seqStr1,seq2,seqStr2,stopCodons);
}
template<class T>
Symbol ApproxAminoAlignment<T>::getResidue(const Sequence &seq,
const char *seqStr,
int index)
{
char buffer[4];
buffer[3]='\0';
memcpy(buffer,seqStr+index,3);
char aminoAcid=ProteinTrans::mapCodon(buffer);
return AminoAlphabet::global.lookup(aminoAcid);
}
template<class T>
ApproxAminoAlignment<T> *ApproxAminoAlignment<T>::load(istream &is,
const SubstitutionMatrix<T> &M,
const Sequence &seq1,const char *seqStr1,
const Sequence &seq2,const char *seqStr2,
StringMap<char> &stopCodons)
{
ApproxAminoAlignment<T> *a=
new ApproxAminoAlignment<T>(is,M,seq1,seqStr1,seq2,seqStr2,stopCodons);
if(a->cells.size()==0) {delete a; return NULL;}
return a;
}
template<class T>
void ApproxAminoAlignment<T>::extend(const Sequence &seq1,
const char *seqStr1,
const Sequence &seq2,
const char *seqStr2,
StringMap<char> &stopCodons)
{
// First, extend the alignment left and right as far as possible without
// passing any in-frame stop codons
extendLeft(seqStr1,seqStr2,stopCodons);
extendRight(seqStr1,seq1.getLength(),seqStr2,seq2.getLength(),stopCodons);
// Now process the cells that were added from left to right to compute
// the cumulative match count and alignment length.
recomputeCumulatives(seq1,seqStr1,seq2,seqStr2);
}
template<class T>
void ApproxAminoAlignment<T>::extendLeft(const char *s1,const char *s2,
StringMap<char> &stopCodons)
{
// Extend leftward, adding cells until either the beginning of either
// sequence is reached, or a stop codon is encountered in either genome
ApproxAlignCell &firstCell=cells[0];
int x=firstCell.getX(), y=firstCell.getY();
if(stopCodons.isDefined(s1,x,3) ||
stopCodons.isDefined(s2,y,3)) return;
while(x>2 && y>2)
{
x-=3;
y-=3;
if(stopCodons.isDefined(s1,x,3) ||
stopCodons.isDefined(s2,y,3))
break;//### check that x & y aren't reversed...
cells.push_front(ApproxAlignCell(x,y,MATCH,-1,-1));
}
}
template<class T>
void ApproxAminoAlignment<T>::extendRight(const char *s1,int len1,
const char *s2,int len2,
StringMap<char> &stopCodons)
{
// Extend rightward, adding cells until either the end of either
// sequence is reached or a stop codon is encountered in either genome
ApproxAlignCell &lastCell=cells[cells.size()-1];
int x=lastCell.getX(), y=lastCell.getY();
if(stopCodons.isDefined(s1,x,3) ||
stopCodons.isDefined(s2,y,3)) return;
int lastPos1=len1-5, lastPos2=len2-5;
while(x<lastPos1 && y<lastPos2)
{
x+=3;
y+=3;
if(stopCodons.isDefined(s1,x,3) ||
stopCodons.isDefined(s2,y,3))
break;//### check that x & y aren't reversed...
cells.push_back(ApproxAlignCell(x,y,MATCH,-1,-1));
}
}
template<class T>
void ApproxAminoAlignment<T>::recomputeCumulatives(const Sequence &seq1,
const char *seqStr1,
const Sequence &seq2,
const char *seqStr2)
{
int n=cells.size(), length=0;
T matches=0;
for(int i=0 ; i<n ; ++i)
{
ApproxAlignCell &cell=cells[i];
switch(cell.getMatchType())
{
case MATCH:
{
Symbol s1=getResidue(seq1,seqStr1,cell.getX());
Symbol s2=getResidue(seq2,seqStr2,cell.getY());
#ifdef SUM_BLOSUM_SCORES
matches+=M(s1,s2)*3;
#else
if(M(s1,s2)>0) ++matches;
#endif
}
break;
case FIRST_UNMATCHED:
#ifdef SUM_BLOSUM_SCORES
//###matches+=M.smallestEntry()*3;
#endif
break;
case SECOND_UNMATCHED:
#ifdef SUM_BLOSUM_SCORES
//###matches+=M.smallestEntry()*3;
#endif
break;
}
#ifdef SUM_BLOSUM_SCORES
length+=3;
#else
length++;
#endif
cell.setCumulatives(matches,length);
}
}
#endif