File: FastPairwiseAlign.cpp

package info (click to toggle)
clustalx 2.1%2Blgpl-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,320 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 16
file content (661 lines) | stat: -rw-r--r-- 17,148 bytes parent folder | download | duplicates (6)
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
/**
 * Author: Mark Larkin
 * 
 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
 */
#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif
#include <math.h>
#include "FastPairwiseAlign.h"

namespace clustalw
{

FastPairwiseAlign::FastPairwiseAlign()
{
    _maxAlnLength = 0;
}

void FastPairwiseAlign::pairwiseAlign(Alignment *alignPtr, DistMatrix *distMat, int iStart, 
                                      int iEnd, int jStart, int jEnd)
{
    try
    {
        if(distMat->getSize() != alignPtr->getNumSeqs() + 1)
        {
            cerr << "The distance matrix is not the right size!\n"
                 << "Need to terminate program.\n";
            exit(1);
        }
        if((iStart < 0) || (iEnd < iStart) || (jStart < 0) || (jEnd < jStart))
        {
            cout << "The range for pairwise Alignment is incorrect.\n"
                 << "Need to terminate program.\n";
            exit(1);
        }
    
        int i, j, dsr;
        double calcScore;
        bool _DNAFlag = userParameters->getDNAFlag();
        _maxAlnLength = alignPtr->getMaxAlnLength();
        int num = (2 * _maxAlnLength) + 1;
        accum.ResizeRect(5, num);
    
        displ.resize(num);
        slopes.resize(num);
        diagIndex.resize(num);

        zza.resize(_maxAlnLength + 1);
        zzb.resize(_maxAlnLength + 1);
        zzc.resize(_maxAlnLength + 1);
        zzd.resize(_maxAlnLength + 1);
    
        if (_DNAFlag)
        {
            userParameters->setDNAParams();
        }
        else
        {
            userParameters->setProtParams();
        }

        cout << "\n\n";
    
        for (i = iStart + 1; i <= iEnd; ++i)
        {
            const vector<int>* _seqIPtr = alignPtr->getSequence(i);
            int _seqILength = alignPtr->getSeqLength(i);
            if (_DNAFlag)
            {
                makeNPtrs(zza, zzc, _seqIPtr, _seqILength);
            }
            else
            {
                makePPtrs(zza, zzc, _seqIPtr, _seqILength);
            }
            double _score;
	    for (j = jStart + 2 > i+1 ? jStart + 2 : i+1; j <= jEnd; ++j)
            {
                const vector<int>* _seqJPtr = alignPtr->getSequence(j);
                int _seqJLength = alignPtr->getSeqLength(j);
                if (_DNAFlag)
                {
                    makeNPtrs(zzb, zzd, _seqJPtr, _seqJLength);
                }
                else
                {
                    makePPtrs(zzb, zzd, _seqJPtr, _seqJLength);
                }
                pairAlign(_seqIPtr, _seqILength, _seqJLength);
                if (!maxSoFar)
                {
                    calcScore = 0.0;
                }
                else
                {
                    calcScore = (double)accum[0][maxSoFar];
                    if (userParameters->getPercent())
                    {
                        dsr = (_seqILength < _seqJLength) ? _seqILength
                            : _seqJLength;
                        calcScore = (calcScore / (double)dsr) *100.0;
                    }
                }
                _score = (100.0 - calcScore) / 100.0;
                distMat->SetAt(i, j, _score);
                //distMat->SetAt(j, i, _score); /* distMat symmetric, FS, 2009-04-06 */

            
                if(userParameters->getDisplayInfo())
                {
                    if (calcScore > 0.1)
                    {
                        utilityObject->info("Sequences (%d:%d) Aligned. Score: %lg",
                                            i, j, calcScore);     
                    }
                    else
                    {
                        utilityObject->info("Sequences (%d:%d) Not Aligned", i, j);
                    }
                }
            }
        }
        accum.clearArray();    
        displ.clear();
        slopes.clear();
        diagIndex.clear();

        zza.clear();
        zzb.clear();
        zzc.clear();
        zzd.clear();
    }
    catch(const exception& e)
    {
        cerr << "An exception has occurred in the FastPairwiseAlign class.\n"
             << e.what() << "\n";
        exit(1);    
    }    
}


/*
 * Note: There is a problem with the treatment of DNA/RNA. 
 * During file reading all residues are encoded as AminoAcids, 
 * even before it has been established if they are AA or not. 
 * This is bad and will have to be changed (later). 
 * 'A' is assigned code 0, C is assigned 2, G = 6, T=18, U=19. 
 * However, the fast alignment routines require that 
 * A=0, C=1, G=2, T=U=3. In the best case the results of the 
 * fast alignment (of DNA) will simply be meaningless, the 
 * worst case is a core dump. 
 * As a quick fix I implemented the following mask, that 
 * (for DNA/RNA) translates 0->0, 2->1, 6->2, 18->3, 19->3. 
 * This is awfull (it is not OO compliant) but it was quick, 
 * uses (much) less memory than a second DNA array, and is 
 * (much) faster than calling a translation function. 
 * Ideally this will be removed, but this requires changes 
 * to (i) the sequence encoding during file-reading AND 
 * (ii) all the DNA substitution matrices. (Fabian, 2009-02-25)
 *
 *                A  B  C  D  E  F  G  H  I  K  L  M  0  N  P  Q  R  S 
 *                T  U  W  X  Y  Z (goodmeasuregoodmeasuregood) */
int ziAA2DNA[] = {0,-1, 1,-1,-1,-1, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
		  3, 3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
const int ciMaxResID = 3;




void FastPairwiseAlign::pairAlign(const vector<int>* seq, int l1, int l2)
{
    int pot[8], i, j, l, m, limit, pos, tl1, vn1, vn2, flen, osptr, fs;
    int tv1, tv2, encrypt, subt1, subt2, rmndr;
    bool flag;
    int residue; 
    bool _DNAFlag = userParameters->getDNAFlag();
    int _ktup = userParameters->getKtup();
    int _maxAA = userParameters->getMaxAA();
    int _windowGap = userParameters->getWindowGap();
    int _window = userParameters->getWindow();
    int _signif = userParameters->getSignif();
        
    if (_DNAFlag)
    {
        for (i = 1; i <= _ktup; ++i)
        {
            pot[i] = (int)pow((double)4, (double)(i - 1));
        }
        limit = (int)pow((double)4, (double)_ktup);
    }
    else
    {
        for (i = 1; i <= _ktup; i++)
        {
            pot[i] = (int)pow((double)(_maxAA + 1), (double)(i - 1));
        }
        limit = (int)pow((double)(_maxAA + 1), (double)_ktup);
    }
    tl1 = (l1 + l2) - 1;

    for (i = 1; i <= tl1; ++i)
    {
        slopes[i] = displ[i] = 0;
        diagIndex[i] = i;
    }


    // increment diagonal score for each k_tuple match 

    for (i = 1; i <= limit; ++i)
    {
        vn1 = zzc[i];
        while (true)
        {
            if (!vn1)
            {
                break;
            }
            vn2 = zzd[i];
            while (vn2 != 0)
            {
                osptr = vn1 - vn2 + l2;
                ++displ[osptr];
                vn2 = zzb[vn2];
            }
            vn1 = zza[vn1];
        }
    }

    // choose the top SIGNIF diagonals 

    desQuickSort(displ, diagIndex, tl1);

    j = tl1 - _signif + 1;
    if (j < 1)
    {
        j = 1;
    }

    // flag all diagonals within WINDOW of a top diagonal

    for (i = tl1; i >= j; i--)
    if (displ[i] > 0)
    {
        pos = diagIndex[i];
        l = (1 > pos - _window) ? 1 : pos - _window;
        m = (tl1 < pos + _window) ? tl1 : pos + _window;
        for (; l <= m; l++)
        {
            slopes[l] = 1;
        }
    }

    for (i = 1; i <= tl1; i++)
    {
        displ[i] = 0;
    }


    currFrag = maxSoFar = 0;

    for (i = 1; i <= (l1 - _ktup + 1); ++i)
    {
        encrypt = flag = 0;
	if (_DNAFlag){
	  for (j = 1; j <= _ktup; ++j)
	    {
	      residue = ziAA2DNA[(*seq)[i + j - 1]];
	      if ((residue < 0) || (residue > ciMaxResID))
		{
		  flag = true;
		  break;
		}
	      encrypt += ((residue) * pot[j]);
	    }
	}
	else {
	  for (j = 1; j <= _ktup; ++j)
	    {
	      residue = (*seq)[i + j - 1];
	      if ((residue < 0) || (residue > _maxAA))
		{
		  flag = true;
		  break;
		}
	      encrypt += ((residue) * pot[j]);
	    }
	}

        if (flag)
        {
            continue;
        }
        ++encrypt;

        vn2 = zzd[encrypt];

        flag = false;
        while (true)
        {
            if (!vn2)
            {
                flag = true;
                break;
            }
            osptr = i - vn2 + l2;
            if (slopes[osptr] != 1)
            {
                vn2 = zzb[vn2];
                continue;
            }
            flen = 0;
            fs = _ktup;
            next = maxSoFar;

            
            //  A-loop
             

            while (true)
            {
                if (!next)
                {
                    ++currFrag;
                    if (currFrag >= 2 * _maxAlnLength)
                    {
                        utilityObject->info("(Partial alignment)");
                        vatend = 1;
                        return ;
                    }
                    displ[osptr] = currFrag;
                    putFrag(fs, i, vn2, flen);
                }
                else
                {
                    tv1 = accum[1][next];
                    tv2 = accum[2][next];
                    if (fragRelPos(i, vn2, tv1, tv2))
                    {
                        if (i - vn2 == accum[1][next] - accum[2][next])
                        {
                            if (i > accum[1][next] + (_ktup - 1))
                            {
                                fs = accum[0][next] + _ktup;
                            }
                            else
                            {
                                rmndr = i - accum[1][next];
                                fs = accum[0][next] + rmndr;
                            }
                            flen = next;
                            next = 0;
                            continue;
                        }
                        else
                        {
                            if (displ[osptr] == 0)
                            {
                                subt1 = _ktup;
                            }
                            else
                            {
                                if (i > accum[1][displ[osptr]] + (_ktup - 1))
                                {
                                    subt1 = accum[0][displ[osptr]] + _ktup;
                                }
                                else
                                {
                                    rmndr = i - accum[1][displ[osptr]];
                                    subt1 = accum[0][displ[osptr]] + rmndr;
                                }
                            }
                            subt2 = accum[0][next] - _windowGap + _ktup;
                            if (subt2 > subt1)
                            {
                                flen = next;
                                fs = subt2;
                            }
                            else
                            {
                                flen = displ[osptr];
                                fs = subt1;
                            }
                            next = 0;
                            continue;
                        }
                    }
                    else
                    {
                        next = accum[4][next];
                        continue;
                    }
                }
                break;
            }
            // End of Aloop
            vn2 = zzb[vn2];
        }
    }
    vatend = 0;
}

void FastPairwiseAlign::makePPtrs(vector<int>& tptr, vector<int>& pl, const vector<int>* seq, int length)
{
    int a[10];
    int i, j, limit, code;
    bool flag;
    int residue;
    int _ktup = userParameters->getKtup();
    int _maxAA = userParameters->getMaxAA();
    
    for (i = 1; i <= _ktup; i++)
    {
        a[i] = (int)pow((double)(_maxAA + 1), (double)(i - 1));
    }

    limit = (int)pow((double)(_maxAA + 1), (double)_ktup);
    if(limit >= (int)pl.size())
    {
        pl.resize(limit + 1);
    }
    if(length >= (int)tptr.size())
    {
        tptr.resize(length + 1);
    }
    
    for (i = 1; i <= limit; ++i)
    {
        pl[i] = 0;
    }
    for (i = 1; i <= length; ++i) // NOTE changed this
    {
        tptr[i] = 0;
    }

    for (i = 1; i <= (length - _ktup + 1); ++i)
    {
        code = 0;
        flag = false;
        for (j = 1; j <= _ktup; ++j)
        {
            residue = (*seq)[i + j - 1];
            if ((residue < 0) || (residue > _maxAA))
            {
                flag = true;
                break;
            }
            code += ((residue) * a[j]);
        }
        if (flag)
        {
            continue;
        }
        ++code;
        if (pl[code] != 0)
        {
            tptr[i] = pl[code];
        }
        pl[code] = i; //Ktuple code is at position i in sequence
    }
}

void FastPairwiseAlign::makeNPtrs(vector<int>& tptr,vector<int>& pl, const vector<int>* seq, int length)
{
    int pot[] =
    {
        0, 1, 4, 16, 64, 256, 1024, 4096
    };
    int i, j, limit, code;
    bool flag;
    int residue;
    int _ktup = userParameters->getKtup();
    
    limit = (int)pow((double)4, (double)_ktup);

    if(limit >= (int)pl.size())
    {
        pl.resize(limit + 1);
    }
    if(length >= (int)tptr.size())
    {
        tptr.resize(length + 1);
    }
    
    for (i = 1; i <= limit; ++i)
    {
        pl[i] = 0;
    }
    for (i = 1; i <= length; ++i)
    {
        tptr[i] = 0;
    }

    for (i = 1; i <= length - _ktup + 1; ++i)
    {
        code = 0;
        flag = false;
	for (j = 1; j <= _ktup; ++j)
	  {
	    residue = ziAA2DNA[(*seq)[i + j - 1]];
	    if ((residue < 0) || (residue > ciMaxResID))
	      {
		flag = true;
		break;
	      }
	    code += ((residue) * pot[j]);
	  }
        if (flag)
        {
            continue;
        }
        ++code;
        if (pl[code] != 0)
        {
            tptr[i] = pl[code];
        }
        pl[code] = i;
    }

}

void FastPairwiseAlign::putFrag(int fs, int v1, int v2, int flen)
{
    int end;
    accum[0][currFrag] = fs;
    accum[1][currFrag] = v1;
    accum[2][currFrag] = v2;
    accum[3][currFrag] = flen;

    if (!maxSoFar)
    {
        maxSoFar = 1;
        accum[4][currFrag] = 0;
        return ;
    }

    if (fs >= accum[0][maxSoFar])
    {
        accum[4][currFrag] = maxSoFar;
        maxSoFar = currFrag;
        return ;
    }
    else
    {
        next = maxSoFar;
        while (true)
        {
            end = next;
            next = accum[4][next];
            if (fs >= accum[0][next])
            {
                break;
            }
        }
        accum[4][currFrag] = next;
        accum[4][end] = currFrag;
    }
}

inline int FastPairwiseAlign::fragRelPos(int a1, int b1, int a2, int b2)
{
    int ret;
    int _ktup = userParameters->getKtup();
    
    ret = false;
    if (a1 - b1 == a2 - b2)
    {
        if (a2 < a1)
        {
            ret = true;
        }
    }
    else
    {
        if (a2 + _ktup - 1 < a1 && b2 + _ktup - 1 < b1)
        {
            ret = true;
        }
    }
    return ret;
}

void FastPairwiseAlign::desQuickSort(vector<int>& array1, vector<int>& array2, int arraySize)
{
    // Quicksort routine, adapted from chapter 4, page 115 of software tools 
    // by Kernighan and Plauger, (1986) 
    // Sort the elements of array1 and sort the 
    // elements of array2 accordingly
    int temp1, temp2;
    int p, pivlin;
    int i, j;
    int lst[50], ust[50]; // the maximum no. of elements must be
                            // < log(base2) of 50 

    lst[1] = 1;
    ust[1] = arraySize - 1;
    p = 1;

    while (p > 0)
    {
        if (lst[p] >= ust[p])
        {
            p--;
        }
        else
        {
            i = lst[p] - 1;
            j = ust[p];
            pivlin = array1[j];
            while (i < j)
            {
                for (i = i + 1; array1[i] < pivlin; i++)
                    ;
                for (j = j - 1; j > i; j--)
                    if (array1[j] <= pivlin)
                    {
                        break;
                    }
                if (i < j)
                {
                    temp1 = array1[i];
                    array1[i] = array1[j];
                    array1[j] = temp1;

                    temp2 = array2[i];
                    array2[i] = array2[j];
                    array2[j] = temp2;
                }
            }

            j = ust[p];

            temp1 = array1[i];
            array1[i] = array1[j];
            array1[j] = temp1;

            temp2 = array2[i];
            array2[i] = array2[j];
            array2[j] = temp2;

            if (i - lst[p] < ust[p] - i)
            {
                lst[p + 1] = lst[p];
                ust[p + 1] = i - 1;
                lst[p] = i + 1;
            }
            else
            {
                lst[p + 1] = i + 1;
                ust[p + 1] = ust[p];
                ust[p] = i - 1;
            }
            p = p + 1;
        }
    }
    return ;

}

}