-
Notifications
You must be signed in to change notification settings - Fork 61
/
align.c
5946 lines (5215 loc) · 148 KB
/
align.c
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************************
*
* Fast alignment discovery and trace generation along with utilites for displaying alignments
* Based on previously unpublished ideas from 2005, subsequently refined in 2013-14. Basic
* idea is to keep a dynamically selected interval of the f.r. waves from my 1986 O(nd) paper.
* A recent cool idea is to not record all the details of an alignment while discovering it
* but simply record trace points through which the optimal alignment passes every 100bp,
* allowing rapid recomputation of the alignment details between trace points.
*
* Author : Gene Myers
* First : June 2013
* Current: June 1, 2014
*
********************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <limits.h>
#include "DB.h"
#include "align.h"
#undef DEBUG_PASSES // Show forward / backward extension termini for Local_Alignment
#undef DEBUG_POINTS // Show trace points
#undef DEBUG_WAVE // Show waves of Local_Alignment
#undef SHOW_MATCH_WAVE // For waves of Local_Alignment also show # of matches
#undef SHOW_TRAIL // Show trace at the end of forward and reverse passes
#undef SHOW_TPS // Show trace points as they are encountered in a wave
#undef DEBUG_EXTEND // Show waves of Extend_Until_Overlap
#undef DEBUG_ALIGN // Show division points of Compute_Trace
#undef DEBUG_TRACE // Show trace additions for Compute_Trace
#undef DEBUG_SCRIPT // Show script additions for Compute_Trace
#undef DEBUG_AWAVE // Show F/R waves of Compute_Trace
#undef SHOW_TRACE // Show full trace for Print_Alignment
#undef WAVE_STATS
/****************************************************************************************\
* *
* Working Storage Abstraction *
* *
\****************************************************************************************/
typedef struct // Hidden from the user, working space for each thread
{ int vecmax;
void *vector;
int celmax;
void *cells;
int pntmax;
void *points;
int tramax;
void *trace;
int alnmax;
void *alnpts;
} _Work_Data;
Work_Data *New_Work_Data()
{ _Work_Data *work;
work = (_Work_Data *) Malloc(sizeof(_Work_Data),"Allocating work data block");
if (work == NULL)
EXIT(NULL);
work->vecmax = 0;
work->vector = NULL;
work->pntmax = 0;
work->points = NULL;
work->tramax = 0;
work->trace = NULL;
work->alnmax = 0;
work->alnpts = NULL;
work->celmax = 0;
work->cells = NULL;
return ((Work_Data *) work);
}
static int enlarge_vector(_Work_Data *work, int newmax)
{ void *vec;
int max;
max = ((int) (newmax*1.2)) + 10000;
vec = Realloc(work->vector,max,"Enlarging DP vector");
if (vec == NULL)
EXIT(1);
work->vecmax = max;
work->vector = vec;
return (0);
}
static int enlarge_points(_Work_Data *work, int newmax)
{ void *vec;
int max;
max = ((int) (newmax*1.2)) + 10000;
vec = Realloc(work->points,max,"Enlarging point vector");
if (vec == NULL)
EXIT(1);
work->pntmax = max;
work->points = vec;
return (0);
}
static int enlarge_alnpts(_Work_Data *work, int newmax)
{ void *vec;
int max;
max = ((int) (newmax*1.2)) + 10000;
vec = Realloc(work->alnpts,max,"Enlarging point vector");
if (vec == NULL)
EXIT(1);
work->alnmax = max;
work->alnpts = vec;
return (0);
}
static int enlarge_trace(_Work_Data *work, int newmax)
{ void *vec;
int max;
max = ((int) (newmax*1.2)) + 10000;
vec = Realloc(work->trace,max,"Enlarging trace vector");
if (vec == NULL)
EXIT(1);
work->tramax = max;
work->trace = vec;
return (0);
}
void Free_Work_Data(Work_Data *ework)
{ _Work_Data *work = (_Work_Data *) ework;
if (work->vector != NULL)
free(work->vector);
if (work->cells != NULL)
free(work->cells);
if (work->trace != NULL)
free(work->trace);
if (work->points != NULL)
free(work->points);
if (work->alnpts != NULL)
free(work->alnpts);
free(work);
}
/****************************************************************************************\
* *
* ADAPTIVE PATH FINDING *
* *
\****************************************************************************************/
// Absolute/Fixed Parameters
#define BVEC uint64 // Can be uint32 if PATH_LEN <= 32
#define TRIM_LEN 15 // Report as the tip, the last wave maximum for which the last
// 2*TRIM_LEN edits are prefix-positive at rate ave_corr*f(bias)
// (max value is 20)
#define DUB_TRIM 45 // = 3*TRIM_LEN
#define PATH_LEN 60 // Follow the last PATH_LEN columns/edges (max value is 63)
// Derivative fixed parameters
#define PATH_TOP 0x1000000000000000ll // Must be 1 << PATH_LEN
#define PATH_INT 0x0fffffffffffffffll // Must be PATH_TOP-1
#define TRIM_MASK 0x7fff // Must be (1 << TRIM_LEN) - 1
#define TRIM_MLAG 250 // How far can last trim point be behind best point
#define WAVE_LAG 60 // How far can worst point be behind the best point
static double Bias_Factor[10] = { .690, .690, .690, .690, .780,
.850, .900, .933, .966, 1.000 };
// Adjustable paramters
typedef struct
{ double ave_corr;
int trace_space;
int reach;
float freq[4];
int ave_path;
int16 *score;
int16 *table;
} _Align_Spec;
/* Fill in bit table: TABLE[x] = 1 iff the alignment modeled by x (1 = match, 0 = mismatch)
has a non-negative score for every suffix of the alignment under the scoring scheme
where match = MATCH and mismatch = -1. MATCH is set so that an alignment with TRIM_PCT
matches has zero score ( (1-TRIM_PCT) / TRIM_PCT ). */
#define FRACTION 1000 // Implicit fractional part of scores, i.e. score = x/FRACTION
typedef struct
{ int mscore;
int dscore;
int16 *table;
int16 *score;
} Table_Bits;
static void set_table(int bit, int prefix, int score, int max, Table_Bits *parms)
{ if (bit >= TRIM_LEN)
{ parms->table[prefix] = (int16) (score-max);
parms->score[prefix] = (int16) score;
}
else
{ if (score > max)
max = score;
set_table(bit+1,(prefix<<1),score - parms->dscore,max,parms);
set_table(bit+1,(prefix<<1) | 1,score + parms->mscore,max,parms);
}
}
/* Create an alignment specification record including path tip tables & values */
Align_Spec *New_Align_Spec(double ave_corr, int trace_space, float *freq, int reach)
{ _Align_Spec *spec;
Table_Bits parms;
double match;
int bias;
spec = (_Align_Spec *) Malloc(sizeof(_Align_Spec),"Allocating alignment specification");
if (spec == NULL)
EXIT(NULL);
spec->ave_corr = ave_corr;
spec->trace_space = trace_space;
spec->reach = reach;
spec->freq[0] = freq[0];
spec->freq[1] = freq[1];
spec->freq[2] = freq[2];
spec->freq[3] = freq[3];
match = freq[0] + freq[3];
if ((match <= 0.) == (match > 0.)) // frequencies accidentally undefined?
match = .5;
if (match > .5)
match = 1.-match;
bias = (int) ((match+.025)*20.-1.);
if (match < .2)
{ fprintf(stderr,"Warning: Base bias worse than 80/20%% ! (New_Align_Spec)\n");
fprintf(stderr," Capping bias at this ratio.\n");
bias = 3;
}
spec->ave_path = (int) (PATH_LEN * (1. - Bias_Factor[bias] * (1. - ave_corr)));
parms.mscore = (int) (FRACTION * Bias_Factor[bias] * (1. - ave_corr));
parms.dscore = FRACTION - parms.mscore;
parms.score = (int16 *) Malloc(sizeof(int16)*(TRIM_MASK+1)*2,"Allocating trim table");
if (parms.score == NULL)
{ free(spec);
EXIT(NULL);
}
parms.table = parms.score + (TRIM_MASK+1);
set_table(0,0,0,0,&parms);
spec->table = parms.table;
spec->score = parms.score;
return ((Align_Spec *) spec);
}
void Free_Align_Spec(Align_Spec *espec)
{ _Align_Spec *spec = (_Align_Spec *) espec;
free(spec->score);
free(spec);
}
double Average_Correlation(Align_Spec *espec)
{ return (((_Align_Spec *) espec)->ave_corr); }
int Trace_Spacing(Align_Spec *espec)
{ return (((_Align_Spec *) espec)->trace_space); }
float *Base_Frequencies(Align_Spec *espec)
{ return (((_Align_Spec *) espec)->freq); }
int Overlap_If_Possible(Align_Spec *espec)
{ return (((_Align_Spec *) espec)->reach); }
/****************************************************************************************\
* *
* LOCAL ALIGNMENT FINDER: forward_/reverse_wave and Local_Alignment *
* *
\****************************************************************************************/
#ifdef WAVE_STATS
static int64 MAX, TOT, NWV;
static int64 RESTARTS;
void Init_Stats()
{ MAX = TOT = NWV = 0;
RESTARTS = 0;
}
void Print_Stats()
{ printf("\nMax = %lld Ave = %.1f # = %lld\n",MAX,(1.*TOT)/NWV,NWV);
printf("\nRestarts = %lld\n",RESTARTS);
}
#endif
#ifdef DEBUG_WAVE
static void print_wave(int *V, int *M, int low, int hgh, int besta)
{ int k, bestk;
(void) M;
printf(" [%6d,%6d]: ",low,hgh);
for (k = low; k <= hgh; k++)
{ if (besta == V[k])
bestk = k;
// printf(" %3d",(V[k]+k)/2);
printf(" %3d",besta-V[k]);
}
printf(" : %d (%d,%d)\n",besta,(besta+bestk)/2,(besta-bestk)/2);
#ifdef SHOW_MATCH_WAVE
printf(" ");
for (k = low; k <= hgh; k++)
printf(" %3d",M[k]);
printf("\n");
#endif
fflush(stdout);
}
#endif
/* At each furthest reaching point, keep a-coordinate of point (V), bitvector
recording the last TRIM_LEN columns of the implied alignment (T), and the
# of matches (1-bits) in the bitvector (M). */
typedef struct
{ int ptr;
int diag;
int diff;
int mark;
} Pebble;
static int VectorEl = 6*sizeof(int) + sizeof(BVEC);
static int forward_wave(_Work_Data *work, _Align_Spec *spec, Alignment *align, Path *bpath,
int *mind, int maxd, int mida, int minp, int maxp, int aoff, int boff)
{ char *aseq = align->aseq;
char *bseq = align->bseq;
Path *apath = align->path;
int hgh, low, dif;
int vlen, vmin, vmax;
int *V, *M;
int *_V, *_M;
BVEC *T;
BVEC *_T;
int *HA, *HB;
int *_HA, *_HB;
int *NA, *NB;
int *_NA, *_NB;
Pebble *cells;
int avail, cmax;
int TRACE_SPACE = spec->trace_space;
int PATH_AVE = spec->ave_path;
int REACH = spec->reach;
int16 *SCORE = spec->score;
int16 *TABLE = spec->table;
int besta, besty;
int trima, trimy, trimd;
int trimha, trimhb;
int morea, morey, mored;
int moreha, morehb;
int more, morem, lasta;
int aclip, bclip;
hgh = maxd;
low = *mind;
dif = 0;
{ int span, wing;
span = (hgh-low)+1;
vlen = work->vecmax/VectorEl;
wing = (vlen - span)/2;
vmin = low - wing;
vmax = hgh + wing;
_V = ((int *) work->vector);
_M = _V + vlen;
_HA = _M + vlen;
_HB = _HA + vlen;
_NA = _HB + vlen;
_NB = _NA + vlen;
_T = ((BVEC *) (_NB + vlen));
V = _V-vmin;
M = _M-vmin;
HA = _HA-vmin;
HB = _HB-vmin;
NA = _NA-vmin;
NB = _NB-vmin;
T = _T-vmin;
cells = (Pebble *) (work->cells);
cmax = work->celmax;
avail = 0;
}
/* Compute 0-wave starting from mid-line */
more = 1;
aclip = INT32_MAX;
bclip = -INT32_MAX;
besta = trima = morea = lasta = mida;
besty = trimy = morey = (mida-hgh) >> 1;
trimd = mored = 0;
trimha = moreha = 0;
trimhb = morehb = 1;
morem = -1;
{ int k;
char *a;
a = aseq + hgh;
for (k = hgh; k >= low; k--)
{ int y, c, d;
int ha, hb;
int na, nb;
Pebble *pb;
y = (mida-k) >> 1;
if (avail >= cmax-1)
{ cmax = ((int) (avail*1.2)) + 10000;
cells = (Pebble *) Realloc(cells,cmax*sizeof(Pebble),"Reallocating trace cells");
if (cells == NULL)
EXIT(1);
work->celmax = cmax;
work->cells = (void *) cells;
}
na = (((y+k)+(TRACE_SPACE-aoff))/TRACE_SPACE-1)*TRACE_SPACE+aoff;
#ifdef SHOW_TPS
printf(" A %d: %d,%d,0,%d\n",avail,-1,k,na); fflush(stdout);
#endif
pb = cells+avail;
pb->ptr = -1;
pb->diag = k;
pb->diff = 0;
pb->mark = na;
ha = avail++;
na += TRACE_SPACE;
nb = ((y+(TRACE_SPACE-boff))/TRACE_SPACE-1)*TRACE_SPACE+boff;
#ifdef SHOW_TPS
printf(" B %d: %d,%d,0,%d\n",avail,-1,k,nb); fflush(stdout);
#endif
pb = cells+avail;
pb->ptr = -1;
pb->diag = k;
pb->diff = 0;
pb->mark = nb;
hb = avail++;
nb += TRACE_SPACE;
while (1)
{ c = bseq[y];
if (c == 4)
{ more = 0;
if (bclip < k)
bclip = k;
break;
}
d = a[y];
if (c != d)
{ if (d == 4)
{ more = 0;
aclip = k;
}
break;
}
y += 1;
}
c = (y << 1) + k;
while (y+k >= na)
{ if (avail >= cmax)
{ cmax = ((int) (avail*1.2)) + 10000;
cells = (Pebble *) Realloc(cells,cmax*sizeof(Pebble),"Reallocating trace cells");
if (cells == NULL)
EXIT(1);
work->celmax = cmax;
work->cells = (void *) cells;
}
#ifdef SHOW_TPS
printf(" A %d: %d,%d,0,%d\n",avail,ha,k,na); fflush(stdout);
#endif
pb = cells+avail;
pb->ptr = ha;
pb->diag = k;
pb->diff = 0;
pb->mark = na;
ha = avail++;
na += TRACE_SPACE;
}
while (y >= nb)
{ if (avail >= cmax)
{ cmax = ((int) (avail*1.2)) + 10000;
cells = (Pebble *) Realloc(cells,cmax*sizeof(Pebble),"Reallocating trace cells");
if (cells == NULL)
EXIT(1);
work->celmax = cmax;
work->cells = (void *) cells;
}
#ifdef SHOW_TPS
printf(" B %d: %d,%d,0,%d\n",avail,hb,k,nb); fflush(stdout);
#endif
pb = cells+avail;
pb->ptr = hb;
pb->diag = k;
pb->diff = 0;
pb->mark = nb;
hb = avail++;
nb += TRACE_SPACE;
}
if (c > besta)
{ besta = trima = lasta = c;
besty = trimy = y;
trimha = ha;
trimhb = hb;
}
V[k] = c;
T[k] = PATH_INT;
M[k] = PATH_LEN;
HA[k] = ha;
HB[k] = hb;
NA[k] = na;
NB[k] = nb;
a -= 1;
}
}
if (more == 0)
{ if (bseq[besty] != 4 && aseq[besta - besty] != 4)
more = 1;
if (hgh >= aclip)
{ hgh = aclip-1;
if (morem <= M[aclip])
{ morem = M[aclip];
morea = V[aclip];
morey = (morea - aclip)/2;
moreha = HA[aclip];
morehb = HB[aclip];
}
}
if (low <= bclip)
{ low = bclip+1;
if (morem <= M[bclip])
{ morem = M[bclip];
morea = V[bclip];
morey = (morea - bclip)/2;
moreha = HA[bclip];
morehb = HB[bclip];
}
}
aclip = INT32_MAX;
bclip = -INT32_MAX;
}
#ifdef DEBUG_WAVE
printf("\nFORWARD WAVE:\n");
print_wave(V,M,low,hgh,besta);
#endif
/* Compute successive waves until no furthest reaching points remain */
while (more && lasta >= besta - TRIM_MLAG)
{ int k, n;
int ua, ub;
BVEC t;
int am, ac, ap;
char *a;
low -= 1;
hgh += 1;
if (low <= vmin || hgh >= vmax)
{ int span, wing;
int64 move;
int64 vd, md, had, hbd, nad, nbd, td;
span = (hgh-low)+1;
if (.8*vlen < span)
{ if (enlarge_vector(work,vlen*VectorEl))
EXIT(1);
move = ((void *) _V) - work->vector;
vlen = work->vecmax/VectorEl;
_V = (int *) work->vector;
_M = _V + vlen;
_HA = _M + vlen;
_HB = _HA + vlen;
_NA = _HB + vlen;
_NB = _NA + vlen;
_T = ((BVEC *) (_NB + vlen));
}
else
move = 0;
wing = (vlen - span)/2;
vd = ((void *) ( _V+wing)) - (((void *) ( V+low)) - move);
md = ((void *) ( _M+wing)) - (((void *) ( M+low)) - move);
had = ((void *) (_HA+wing)) - (((void *) (HA+low)) - move);
hbd = ((void *) (_HB+wing)) - (((void *) (HB+low)) - move);
nad = ((void *) (_NA+wing)) - (((void *) (NA+low)) - move);
nbd = ((void *) (_NB+wing)) - (((void *) (NB+low)) - move);
td = ((void *) ( _T+wing)) - (((void *) ( T+low)) - move);
if (vd < 0)
memmove( _V+wing, ((void *) ( V+low)) - move, span*sizeof(int));
if (md < 0)
memmove( _M+wing, ((void *) ( M+low)) - move, span*sizeof(int));
if (had < 0)
memmove(_HA+wing, ((void *) (HA+low)) - move, span*sizeof(int));
if (hbd < 0)
memmove(_HB+wing, ((void *) (HB+low)) - move, span*sizeof(int));
if (nad < 0)
memmove(_NA+wing, ((void *) (NA+low)) - move, span*sizeof(int));
if (nbd < 0)
memmove(_NB+wing, ((void *) (NB+low)) - move, span*sizeof(int));
if (td < 0)
memmove( _T+wing, ((void *) ( T+low)) - move, span*sizeof(BVEC));
if (td > 0)
memmove( _T+wing, ((void *) ( T+low)) - move, span*sizeof(BVEC));
if (nbd > 0)
memmove(_NB+wing, ((void *) (NB+low)) - move, span*sizeof(int));
if (nad > 0)
memmove(_NA+wing, ((void *) (NA+low)) - move, span*sizeof(int));
if (hbd > 0)
memmove(_HB+wing, ((void *) (HB+low)) - move, span*sizeof(int));
if (had > 0)
memmove(_HA+wing, ((void *) (HA+low)) - move, span*sizeof(int));
if (md > 0)
memmove( _M+wing, ((void *) ( M+low)) - move, span*sizeof(int));
if (vd > 0)
memmove( _V+wing, ((void *) ( V+low)) - move, span*sizeof(int));
vmin = low-wing;
vmax = hgh+wing;
V = _V-vmin;
M = _M-vmin;
HA = _HA-vmin;
HB = _HB-vmin;
NA = _NA-vmin;
NB = _NB-vmin;
T = _T-vmin;
}
if (low >= minp)
{ NA[low] = NA[low+1];
NB[low] = NB[low+1];
V[low] = -1;
}
else
low += 1;
if (hgh <= maxp)
{ NA[hgh] = NA[hgh-1];
NB[hgh] = NB[hgh-1];
V[hgh] = am = -1;
}
else
am = V[--hgh];
dif += 1;
ac = V[hgh+1] = V[low-1] = -1;
a = aseq + hgh;
t = PATH_INT;
n = PATH_LEN;
ua = ub = -1;
for (k = hgh; k >= low; k--)
{ int y, m;
int ha, hb;
int c, d;
BVEC b;
Pebble *pb;
ap = ac;
ac = am;
am = V[d = k-1];
if (ac < am)
if (am < ap)
{ c = ap+1;
m = n;
b = t;
ha = ua;
hb = ub;
}
else
{ c = am+1;
m = M[d];
b = T[d];
ha = HA[d];
hb = HB[d];
}
else
if (ac < ap)
{ c = ap+1;
m = n;
b = t;
ha = ua;
hb = ub;
}
else
{ c = ac+2;
m = M[k];
b = T[k];
ha = HA[k];
hb = HB[k];
}
if ((b & PATH_TOP) != 0)
m -= 1;
b <<= 1;
y = (c-k) >> 1;
while (1)
{ c = bseq[y];
if (c == 4)
{ more = 0;
if (bclip < k)
bclip = k;
break;
}
d = a[y];
if (c != d)
{ if (d == 4)
{ more = 0;
aclip = k;
}
break;
}
y += 1;
if ((b & PATH_TOP) == 0)
m += 1;
b = (b << 1) | 1;
}
c = (y << 1) + k;
while (y+k >= NA[k])
{ if (cells[ha].mark < NA[k])
{ if (avail >= cmax)
{ cmax = ((int) (avail*1.2)) + 10000;
cells = (Pebble *) Realloc(cells,cmax*sizeof(Pebble),
"Reallocating trace cells");
if (cells == NULL)
EXIT(1);
work->celmax = cmax;
work->cells = (void *) cells;
}
#ifdef SHOW_TPS
printf(" A %d: %d,%d,%d,%d\n",avail,ha,k,dif,NA[k]); fflush(stdout);
#endif
pb = cells+avail;
pb->ptr = ha;
pb->diag = k;
pb->diff = dif;
pb->mark = NA[k];
ha = avail++;
}
NA[k] += TRACE_SPACE;
}
while (y >= NB[k])
{ if (cells[hb].mark < NB[k])
{ if (avail >= cmax)
{ cmax = ((int) (avail*1.2)) + 10000;
cells = (Pebble *) Realloc(cells,cmax*sizeof(Pebble),
"Reallocating trace cells");
if (cells == NULL)
EXIT(1);
work->celmax = cmax;
work->cells = (void *) cells;
}
#ifdef SHOW_TPS
printf(" B %d: %d,%d,%d,%d\n",avail,hb,k,dif,NB[k]); fflush(stdout);
#endif
pb = cells+avail;
pb->ptr = hb;
pb->diag = k;
pb->diff = dif;
pb->mark = NB[k];
hb = avail++;
}
NB[k] += TRACE_SPACE;
}
if (c > besta)
{ besta = c;
besty = y;
if (m >= PATH_AVE)
{ lasta = c;
if (TABLE[b & TRIM_MASK] >= 0)
if (TABLE[(b >> TRIM_LEN) & TRIM_MASK] + SCORE[b & TRIM_MASK] >= 0)
{ trima = c;
trimy = y;
trimd = dif;
trimha = ha;
trimhb = hb;
}
}
}
t = T[k];
n = M[k];
ua = HA[k];
ub = HB[k];
V[k] = c;
T[k] = b;
M[k] = m;
HA[k] = ha;
HB[k] = hb;
a -= 1;
}
if (more == 0)
{ if (bseq[besty] != 4 && aseq[besta-besty] != 4)
more = 1;
if (hgh >= aclip)
{ hgh = aclip-1;
if (morem <= M[aclip])
{ morem = M[aclip];
morea = V[aclip];
morey = (morea - aclip)/2;
mored = dif;
moreha = HA[aclip];
morehb = HB[aclip];
}
}
if (low <= bclip)
{ low = bclip+1;
if (morem <= M[bclip])
{ morem = M[bclip];
morea = V[bclip];
morey = (morea - bclip)/2;
mored = dif;
moreha = HA[bclip];
morehb = HB[bclip];
}
}
aclip = INT32_MAX;
bclip = -INT32_MAX;
}
n = besta - WAVE_LAG;
while (hgh >= low)
if (V[hgh] < n)
hgh -= 1;
else
{ while (V[low] < n)
low += 1;
break;
}
#ifdef WAVE_STATS
k = (hgh-low)+1;
if (k > MAX)
MAX = k;
TOT += k;
NWV += 1;
#endif
#ifdef DEBUG_WAVE
print_wave(V,M,low,hgh,besta);
#endif
}
{ uint16 *atrace = (uint16 *) apath->trace;
uint16 *btrace = (uint16 *) bpath->trace;
int atlen, btlen;
int trimx;
int a, b, k, h;
int d, e;
if (morem >= 0 && REACH)
{ trimx = morea-morey;
trimy = morey;
trimd = mored;
trimha = moreha;
trimhb = morehb;
}
else
trimx = trima-trimy;
atlen = btlen = 0;
a = -1;
for (h = trimha; h >= 0; h = b)
{ b = cells[h].ptr;
cells[h].ptr = a;
a = h;
}
h = a;
k = cells[h].diag;
b = (mida-k)/2;
e = 0;
#ifdef SHOW_TRAIL
printf(" A path = (%5d,%5d)\n",(mida+k)/2,b); fflush(stdout);
#endif
for (h = cells[h].ptr; h >= 0; h = cells[h].ptr)
{ k = cells[h].diag;
a = cells[h].mark - k;
d = cells[h].diff;
atrace[atlen++] = (uint16) (d-e);
atrace[atlen++] = (uint16) (a-b);
#ifdef SHOW_TRAIL
printf(" %4d: (%5d,%5d): %3d / %3d\n",h,a+k,a,d-e,a-b); fflush(stdout);
#endif
b = a;
e = d;
}
if (b+k != trimx)
{ atrace[atlen++] = (uint16) (trimd-e);
atrace[atlen++] = (uint16) (trimy-b);
#ifdef SHOW_TRAIL
printf(" (%5d,%5d): %3d / %3d\n",trimx,trimy,trimd-e,trimy-b); fflush(stdout);
#endif
}
else if (b != trimy)
{ atrace[atlen-1] = (uint16) (atrace[atlen-1] + (trimy-b));
atrace[atlen-2] = (uint16) (atrace[atlen-2] + (trimd-e));
#ifdef SHOW_TRAIL
printf(" @ (%5d,%5d): %3d / %3d\n",trimx,trimy,trimd-e,trimy-b); fflush(stdout);
#endif
}
a = -1;
for (h = trimhb; h >= 0; h = b)
{ b = cells[h].ptr;
cells[h].ptr = a;
a = h;
}
h = a;
k = cells[h].diag;
b = (mida+k)/2;
e = 0;
low = k;
#ifdef SHOW_TRAIL
printf(" B path = (%5d,%5d)\n",b,(mida-k)/2); fflush(stdout);
#endif
for (h = cells[h].ptr; h >= 0; h = cells[h].ptr)
{ k = cells[h].diag;
a = cells[h].mark + k;
d = cells[h].diff;
btrace[btlen++] = (uint16) (d-e);
btrace[btlen++] = (uint16) (a-b);
#ifdef SHOW_TRAIL
printf(" %4d: (%5d,%5d): %3d / %3d\n",h,a,a-k,d-e,a-b); fflush(stdout);
#endif
b = a;
e = d;
}
if (b-k != trimy)
{ btrace[btlen++] = (uint16) (trimd-e);
btrace[btlen++] = (uint16) (trimx-b);
#ifdef SHOW_TRAIL
printf(" (%5d,%5d): %3d / %3d\n",trimx,trimy,trimd-e,trimx-b); fflush(stdout);
#endif
}
else if (b != trimx)
{ btrace[btlen-1] = (uint16) (btrace[btlen-1] + (trimx-b));
btrace[btlen-2] = (uint16) (btrace[btlen-2] + (trimd-e));
#ifdef SHOW_TRAIL
printf(" @ (%5d,%5d): %3d / %3d\n",trimx,trimy,trimd-e,trimx-b); fflush(stdout);
#endif
}