File: clonewisedp.c

package info (click to toggle)
wise 2.4.1-28
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,348 kB
  • sloc: ansic: 276,376; makefile: 1,021; perl: 886; lex: 93; yacc: 81; sh: 25
file content (4660 lines) | stat: -rw-r--r-- 178,364 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
#ifdef _cplusplus
extern "C" {
#endif
#include "clonewisedp.h"

# line 5 "clonewisedp.c"


  /*****************   C functions  ****************/
  /*             Written using dynamite            */
  /*            Sat Sep  8 09:05:31 2007           */
  /*            email birney@sanger.ac.uk          */
  /* http://www.sanger.ac.uk/Users/birney/dynamite */
  /*************************************************/


  /* Please report any problems or bugs to         */
  /* Ewan Birney, birney@sanger.ac.uk              */


/* basic set of macros to map states to numbers */ 
#define MATCH 0  
#define SKIP_QUERY 1 
#define SKIP_TARGET 2    


#define START 0  
#define TRUSTED_SPECIAL 1    
#define END 2    


#define CloneWise_EXPL_MATRIX(this_matrix,i,j,STATE) this_matrix->basematrix->matrix[((j+1)*3)+STATE][i+1]   
#define CloneWise_EXPL_SPECIAL(matrix,i,j,STATE) matrix->basematrix->specmatrix[STATE][j+1]  
#define CloneWise_READ_OFF_ERROR -3
 


#define CloneWise_VSMALL_MATRIX(mat,i,j,STATE) mat->basematrix->matrix[(j+2)%2][((i+1)*3)+STATE] 
#define CloneWise_VSMALL_SPECIAL(mat,i,j,STATE) mat->basematrix->specmatrix[(j+2)%2][STATE]  




#define CloneWise_SHATTER_SPECIAL(matrix,i,j,STATE) matrix->shatter->special[STATE][j]   
#define CloneWise_SHATTER_MATRIX(matrix,i,j,STATE)  fetch_cell_value_ShatterMatrix(mat->shatter,i,j,STATE)   


/* Function:  PackAln_read_Shatter_CloneWise(mat)
 *
 * Descrip:    Reads off PackAln from shatter matrix structure
 *
 *
 * Arg:        mat [UNKN ] Undocumented argument [CloneWise *]
 *
 * Return [UNKN ]  Undocumented return value [PackAln *]
 *
 */
PackAln * PackAln_read_Shatter_CloneWise(CloneWise * mat) 
{
    CloneWise_access_func_holder holder;     


    holder.access_main    = CloneWise_shatter_access_main;   
    holder.access_special = CloneWise_shatter_access_special;    
    assert(mat);     
    assert(mat->shatter);    
    return PackAln_read_generic_CloneWise(mat,holder);   
}    


/* Function:  CloneWise_shatter_access_main(mat,i,j,state)
 *
 * Descrip: No Description
 *
 * Arg:          mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:            i [UNKN ] Undocumented argument [int]
 * Arg:            j [UNKN ] Undocumented argument [int]
 * Arg:        state [UNKN ] Undocumented argument [int]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int CloneWise_shatter_access_main(CloneWise * mat,int i,int j,int state) 
{
    return CloneWise_SHATTER_MATRIX(mat,i,j,state);  
}    


/* Function:  CloneWise_shatter_access_special(mat,i,j,state)
 *
 * Descrip: No Description
 *
 * Arg:          mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:            i [UNKN ] Undocumented argument [int]
 * Arg:            j [UNKN ] Undocumented argument [int]
 * Arg:        state [UNKN ] Undocumented argument [int]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int CloneWise_shatter_access_special(CloneWise * mat,int i,int j,int state) 
{
    return CloneWise_SHATTER_SPECIAL(mat,i,j,state); 
}    


/* Function:  calculate_shatter_CloneWise(mat,dpenv)
 *
 * Descrip:    This function calculates the CloneWise matrix when in shatter mode
 *
 *
 * Arg:          mat [UNKN ] (null) [CloneWise *]
 * Arg:        dpenv [UNKN ] Undocumented argument [DPEnvelope *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean calculate_shatter_CloneWise(CloneWise * mat,DPEnvelope * dpenv) 
{
    int i;   
    int j;   
    int k;   
    int should_calc;     
    int leni;    
    int lenj;    
    int tot; 
    int num; 
    int starti;  
    int startj;  
    int endi;    
    int endj;    


    int * SIG_0_0;   
    int * SIG_1_1;   
    int * SIG_0_1;   
    int * SIG_1_0;   


    leni = mat->leni;    
    lenj = mat->lenj;    


    mat->shatter = new_ShatterMatrix(dpenv,3,lenj,3);    
    prepare_DPEnvelope(dpenv);   
    starti = dpenv->starti;  
    if( starti < 0 ) 
      starti = 0;    
    startj = dpenv->startj;  
    if( startj < 0 ) 
      startj = 0;    
    endi = dpenv->endi;  
    if( endi > mat->leni )   
      endi = mat->leni;  
    endj = dpenv->endj;  
    if( endj > mat->lenj )   
      endj = mat->lenj;  
    tot = (endi-starti) * (endj-startj); 
    num = 0; 


    start_reporting("CloneWise Matrix calculation: ");   
    for(j=startj;j<endj;j++) {  
      auto int score;    
      auto int temp;     
      for(i=starti;i<endi;i++)   {  
        /* Check if is in envelope - code identical to is_in_DPEnvelope, but aggressively inlined here for speed */ 
        should_calc = 0; 
        for(k=0;k<dpenv->len;k++)    {  
          auto DPUnit * u;   
          u = dpenv->dpu[k]; 
          switch(u->type)    {  
            case DPENV_RECT :    
              if( i >= u->starti && j >= u->startj && i < (u->starti+u->height) && j < (u->startj+u->length))    
                should_calc = 1;     
              break; 
            case DPENV_DIAG :    
              if(  abs( (i-j) - (u->starti-u->startj)) <= u->height && i+j >= u->starti+u->startj && i+j+u->length >= u->starti+u->startj)   
                should_calc = 1;     
              break; 
            }  
          if( should_calc == 1 ) 
            break;   
          }  
        if( should_calc == 0)    
          continue;  


        SIG_0_0 = fetch_cell_from_ShatterMatrix(mat->shatter,i,j);   
        SIG_1_1 = fetch_cell_from_ShatterMatrix(mat->shatter,i-1,j-1);   
        SIG_0_1 = fetch_cell_from_ShatterMatrix(mat->shatter,i-0,j-1);   
        SIG_1_0 = fetch_cell_from_ShatterMatrix(mat->shatter,i-1,j-0);   




        /* For state MATCH */ 
        /* setting first movement to score */ 
        score = SIG_1_1[MATCH] + (mat->match->matrix[i][j]+1);   
        /* From state MATCH to state MATCH */ 
        temp = SIG_0_1[MATCH] + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state MATCH to state MATCH */ 
        temp = SIG_1_0[MATCH] + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state MATCH */ 
        temp = SIG_1_1[SKIP_QUERY] + 0;  
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state MATCH */ 
        temp = SIG_1_1[SKIP_TARGET] + 0;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-1) == 0 && (j-1) == 0 )   {  
          /* From state START to state MATCH */ 
          temp = CloneWise_SHATTER_SPECIAL(mat,i-1,j-1,START) + 0;   
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for MATCH */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->matrix[i][j];  
         SIG_0_0[MATCH] = score; 


        /* state MATCH is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_SHATTER_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )  {  
          CloneWise_SHATTER_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;     
          }  




        /* Finished calculating state MATCH */ 


        /* For state SKIP_QUERY */ 
        /* setting first movement to score */ 
        score = SIG_1_0[MATCH] + mat->query_skip_start;  
        /* From state SKIP_TARGET to state SKIP_QUERY */ 
        temp = SIG_1_0[SKIP_TARGET] + mat->query_skip_start;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state SKIP_QUERY */ 
        temp = SIG_1_0[SKIP_QUERY] + 0;  
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-1) == 0 && (j-0) == 0 )   {  
          /* From state START to state SKIP_QUERY */ 
          temp = CloneWise_SHATTER_SPECIAL(mat,i-1,j-0,START) + mat->query_skip_start;   
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for SKIP_QUERY */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_iset[i];  
         SIG_0_0[SKIP_QUERY] = score;    


        /* state SKIP_QUERY is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_SHATTER_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )  {  
          CloneWise_SHATTER_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;     
          }  




        /* Finished calculating state SKIP_QUERY */ 


        /* For state SKIP_TARGET */ 
        /* setting first movement to score */ 
        score = SIG_0_1[MATCH] + mat->target_skip_start;     
        /* From state SKIP_QUERY to state SKIP_TARGET */ 
        temp = SIG_0_1[SKIP_QUERY] + mat->target_skip_start;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state SKIP_TARGET */ 
        temp = SIG_0_1[SKIP_TARGET] + 0;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-0) == 0 && (j-1) == 0 )   {  
          /* From state START to state SKIP_TARGET */ 
          temp = CloneWise_SHATTER_SPECIAL(mat,i-0,j-1,START) + mat->target_skip_start;  
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for SKIP_TARGET */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_jset[j];  
         SIG_0_0[SKIP_TARGET] = score;   


        /* state SKIP_TARGET is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_SHATTER_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )  {  
          CloneWise_SHATTER_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;     
          }  




        /* Finished calculating state SKIP_TARGET */ 
        }  


      /* Special state START has no special to special movements */ 


      /* Special state TRUSTED_SPECIAL has special to speical */ 
      /* Set score to current score (remember, state probably updated during main loop */ 
      score = CloneWise_SHATTER_SPECIAL(mat,0,j,TRUSTED_SPECIAL);    


      /* Source START is a special source for TRUSTED_SPECIAL */ 
      /* Has restricted position */ 
      if( (j-1) == 0  )  {  
        temp = CloneWise_SHATTER_SPECIAL(mat,0,j - 1,START) + (0) + (0);     
        if( temp > score )   
          score = temp;  
        }  


      /* Source TRUSTED_SPECIAL is a special source for TRUSTED_SPECIAL */ 
      temp = CloneWise_SHATTER_SPECIAL(mat,0,j - 1,TRUSTED_SPECIAL) + ((mat->match->skip_jset[j]-1)) + (0);  
      if( temp > score ) 
        score = temp;    


      /* Source MATCH for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Source SKIP_QUERY for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Source SKIP_TARGET for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Put back score... (now updated!) */ 
      CloneWise_SHATTER_SPECIAL(mat,0,j,TRUSTED_SPECIAL) = score;    
      /* Finished updating state TRUSTED_SPECIAL */ 




      /* Special state END has special to speical */ 
      /* Set score to current score (remember, state probably updated during main loop */ 
      score = CloneWise_SHATTER_SPECIAL(mat,0,j,END);    


      /* Source TRUSTED_SPECIAL is a special source for END */ 
      /* Has restricted position */ 
      if( j == mat->lenj-1 ) {  
        temp = CloneWise_SHATTER_SPECIAL(mat,0,j - 1,TRUSTED_SPECIAL) + (0) + (0);   
        if( temp > score )   
          score = temp;  
        }  


      /* Put back score... (now updated!) */ 
      CloneWise_SHATTER_SPECIAL(mat,0,j,END) = score;    
      /* Finished updating state END */ 


      }  
    stop_reporting();    
    return TRUE;     
}    


/* Function:  search_CloneWise(dbsi,out,q,t,match,target_skip_start,target_skip,query_skip_start,query_skip,spread,target_special_s)
 *
 * Descrip:    This function makes a database search of CloneWise
 *             It uses the dbsi structure to choose which implementation
 *             to use of the database searching. This way at run time you
 *             can switch between single threaded/multi-threaded or hardware
 *
 *
 * Arg:                     dbsi [UNKN ] Undocumented argument [DBSearchImpl *]
 * Arg:                      out [UNKN ] Undocumented argument [Hscore *]
 * Arg:                        q [UNKN ] Undocumented argument [MappedCloneSet *]
 * Arg:                        t [UNKN ] Undocumented argument [MappedCloneSet *]
 * Arg:                    match [UNKN ] Undocumented argument [MappedCloneMatch*]
 * Arg:        target_skip_start [UNKN ] Undocumented argument [Score]
 * Arg:              target_skip [UNKN ] Undocumented argument [Score]
 * Arg:         query_skip_start [UNKN ] Undocumented argument [Score]
 * Arg:               query_skip [UNKN ] Undocumented argument [Score]
 * Arg:                   spread [UNKN ] Undocumented argument [int]
 * Arg:         target_special_s [UNKN ] Undocumented argument [int]
 *
 * Return [UNKN ]  Undocumented return value [Search_Return_Type]
 *
 */
Search_Return_Type search_CloneWise(DBSearchImpl * dbsi,Hscore * out,MappedCloneSet * q,MappedCloneSet * t ,MappedCloneMatch* match,Score target_skip_start,Score target_skip,Score query_skip_start,Score query_skip,int spread,int target_special_s) 
{
#ifdef PTHREAD   
    int i;   
    int thr_no;  
    pthread_attr_t pat;  
    struct thread_pool_holder_CloneWise * holder;    
#endif   
    if( out == NULL )    {  
      warn("Passed in a null Hscore object into search_CloneWise. Can't process results!");  
      return SEARCH_ERROR;   
      }  
    if( dbsi == NULL )   {  
      warn("Passed in a null DBSearchImpl object into search_CloneWise. Can't process results!");    
      return SEARCH_ERROR;   
      }  
    if( dbsi->trace_level > 5 )  
      warn("Asking for trace level of %d in database search for CloneWise, but it was compiled with a trace level of -2139062144. Not all trace statements can be shown",dbsi->trace_level); 
    switch(dbsi->type)   { /*switch on implementation*/ 
      case DBSearchImpl_Serial : 
        return serial_search_CloneWise(out,q,t ,match,target_skip_start,target_skip,query_skip_start,query_skip,spread,target_special_s);    
      case DBSearchImpl_Pthreads :   
#ifdef PTHREAD   
        holder = (struct thread_pool_holder_CloneWise *) ckalloc(sizeof(struct thread_pool_holder_CloneWise));   
        if( holder == NULL )     {  
          warn("Unable to allocated thread pool datastructure...");  
          return SEARCH_ERROR;   
          }  
        holder->out = out;   
        holder->dbsi = dbsi; 
        holder->q = q;   
        holder->t = t;   
        holder->match = match;   
        holder->target_skip_start = target_skip_start;   
        holder->target_skip = target_skip;   
        holder->query_skip_start = query_skip_start; 
        holder->query_skip = query_skip; 
        holder->spread = spread; 
        holder->target_special_s = target_special_s; 
        if( pthread_mutex_init(&(holder->input_lock),NULL) != 0 )    
        fatal("Unable to iniated input mutex lock"); 
        if( pthread_mutex_init(&(holder->output_lock),NULL) != 0 )   
        fatal("Unable to iniated output mutex lock");    
        /* Let us rock! */ 
        thr_no = number_of_threads_DBSearchImpl(dbsi);   
        holder->pool = ckcalloc (thr_no,sizeof(pthread_t));  
        if( holder->pool == NULL )   {  
          warn("Unable to allocated thread pools");  
          return SEARCH_ERROR;   
          }  
        /* Build a thread attribute to make sure we get the most out of SMP boxes */ 
        pthread_attr_init(&pat);     
        /* Give thread libraries a hint that threads should be kernel threads */ 
#ifndef __sgi /* SGI can't set system scope ... */   
#ifdef  HAS_PTHREAD_SETSCOPE 
        pthread_attr_setscope(&pat, PTHREAD_SCOPE_SYSTEM);   
#endif /* set scope */   
#endif /* sgi */ 
        /* Give thread libraries a hint that there are num of threads to run */ 
#ifdef HAS_PTHREAD_SETCONCURRENCY    
        pthread_setconcurrency(thr_no+1);    
#endif /* set concurrency */ 
        for(i=0;i<thr_no;i++)    {  
          if( pthread_create(holder->pool+i,&pat,thread_loop_CloneWise,(void *)holder) ) 
            fatal("Unable to create a thread!"); 
          }  
        /* Now - wait for all the threads to exit */ 
        for(i=0;i<thr_no;i++)    {  
          if( pthread_join(holder->pool[i],NULL) != 0 )  
            fatal("Unable to join a thread!");   
          }  
        /* Deallocate the thread structures */ 
        ckfree(holder->pool);    
        ckfree(holder);  
        return SEARCH_OK;    
#else /* not compiled with threads */    
        warn("You did not specifiy the PTHREAD compile when compiled the C code for CloneWise"); 
#endif /* finished threads */    
      default :  
        warn("database search implementation %s was not provided in the compiled dynamite file from CloneWise",impl_string_DBSearchImpl(dbsi));  
        return SEARCH_ERROR; 
      } /* end of switch on implementation */ 


}    


/* Function:  thread_loop_CloneWise(ptr)
 *
 * Descrip:    dummy loop code foreach thread for CloneWise
 *
 *
 * Arg:        ptr [UNKN ] Undocumented argument [void *]
 *
 * Return [UNKN ]  Undocumented return value [void *]
 *
 */
void * thread_loop_CloneWise(void * ptr) 
{
    fatal("dummy thread loop function"); 
}    


/* Function:  serial_search_CloneWise(out,q,t,match,target_skip_start,target_skip,query_skip_start,query_skip,spread,target_special_s)
 *
 * Descrip:    This function makes a database search of CloneWise
 *             It is a single processor implementation
 *
 *
 * Arg:                      out [UNKN ] Undocumented argument [Hscore *]
 * Arg:                        q [UNKN ] Undocumented argument [MappedCloneSet *]
 * Arg:                        t [UNKN ] Undocumented argument [MappedCloneSet *]
 * Arg:                    match [UNKN ] Undocumented argument [MappedCloneMatch*]
 * Arg:        target_skip_start [UNKN ] Undocumented argument [Score]
 * Arg:              target_skip [UNKN ] Undocumented argument [Score]
 * Arg:         query_skip_start [UNKN ] Undocumented argument [Score]
 * Arg:               query_skip [UNKN ] Undocumented argument [Score]
 * Arg:                   spread [UNKN ] Undocumented argument [int]
 * Arg:         target_special_s [UNKN ] Undocumented argument [int]
 *
 * Return [UNKN ]  Undocumented return value [Search_Return_Type]
 *
 */
Search_Return_Type serial_search_CloneWise(Hscore * out,MappedCloneSet * q,MappedCloneSet * t ,MappedCloneMatch* match,Score target_skip_start,Score target_skip,Score query_skip_start,Score query_skip,int spread,int target_special_s) 
{
    int db_status;   
    int score;   
    int query_pos = 0;   
    int target_pos = 0;  
    DataScore * ds;  


    push_errormsg_stack("Before any actual search in db searching"); 


    target_pos = 0;  




    /* No maximum length - allocated on-the-fly */ 
    score = score_only_CloneWise(q, t , match, target_skip_start, target_skip, query_skip_start, query_skip, spread, target_special_s);  
    if( should_store_Hscore(out,score) == TRUE )     { /*if storing datascore*/ 
      ds = new_DataScore_from_storage(out);  
      if( ds == NULL )   {  
        warn("CloneWise search had a memory error in allocating a new_DataScore (?a leak somewhere - DataScore is a very small datastructure");  
        return SEARCH_ERROR; 
        }  
      /* Now: add query/target information to the entry */ 
      ds->score = score;     
      add_Hscore(out,ds);    
      } /* end of if storing datascore */ 
    pop_errormsg_stack();    
    push_errormsg_stack("DB searching: just finished [Query Pos: %d] [Target Pos: %d]",query_pos,target_pos);    


    pop_errormsg_stack();    
    return SEARCH_OK;    
}    


/* Function:  score_only_CloneWise(q,t,match,target_skip_start,target_skip,query_skip_start,query_skip,spread,target_special_s)
 *
 * Descrip:    This function just calculates the score for the matrix
 *             I am pretty sure we can do this better, but hey, for the moment...
 *             It calls /allocate_CloneWise_only
 *
 *
 * Arg:                        q [UNKN ] query data structure [MappedCloneSet *]
 * Arg:                        t [UNKN ] target data structure [MappedCloneSet *]
 * Arg:                    match [UNKN ] Resource [MappedCloneMatch*]
 * Arg:        target_skip_start [UNKN ] Resource [Score]
 * Arg:              target_skip [UNKN ] Resource [Score]
 * Arg:         query_skip_start [UNKN ] Resource [Score]
 * Arg:               query_skip [UNKN ] Resource [Score]
 * Arg:                   spread [UNKN ] Resource [int]
 * Arg:         target_special_s [UNKN ] Resource [int]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int score_only_CloneWise(MappedCloneSet * q,MappedCloneSet * t ,MappedCloneMatch* match,Score target_skip_start,Score target_skip,Score query_skip_start,Score query_skip,int spread,int target_special_s) 
{
    int bestscore = NEGI;    
    int i;   
    int j;   
    int k;   
    CloneWise * mat;     


    mat = allocate_CloneWise_only(q, t , match, target_skip_start, target_skip, query_skip_start, query_skip, spread, target_special_s); 
    if( mat == NULL )    {  
      warn("Memory allocation error in the db search - unable to communicate to calling function. this spells DIASTER!");    
      return NEGI;   
      }  
    if((mat->basematrix = BaseMatrix_alloc_matrix_and_specials(2,(mat->leni + 1) * 3,2,3)) == NULL)  {  
      warn("Score only matrix for CloneWise cannot be allocated, (asking for 1  by %d  cells)",mat->leni*3); 
      mat = free_CloneWise(mat);     
      return 0;  
      }  
    mat->basematrix->type = BASEMATRIX_TYPE_VERYSMALL;   


    /* Now, initiate matrix */ 
    for(j=0;j<3;j++) {  
      for(i=(-1);i<mat->leni;i++)    {  
        for(k=0;k<3;k++) 
          CloneWise_VSMALL_MATRIX(mat,i,j,k) = NEGI; 
        }  
      CloneWise_VSMALL_SPECIAL(mat,i,j,START) = 0;   
      CloneWise_VSMALL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = NEGI;  
      CloneWise_VSMALL_SPECIAL(mat,i,j,END) = NEGI;  
      }  


    /* Ok, lets do-o-o-o-o it */ 


    for(j=0;j<mat->lenj;j++) { /*for all target positions*/ 
      auto int score;    
      auto int temp;     
      for(i=0;i<mat->leni;i++)   { /*for all query positions*/ 


        /* For state MATCH */ 
        /* setting first movement to score */ 
        score = CloneWise_VSMALL_MATRIX(mat,i-1,j-1,MATCH) + (mat->match->matrix[i][j]+1);   
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_VSMALL_MATRIX(mat,i-0,j-1,MATCH) + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_VSMALL_MATRIX(mat,i-1,j-0,MATCH) + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state MATCH */ 
        temp = CloneWise_VSMALL_MATRIX(mat,i-1,j-1,SKIP_QUERY) + 0;  
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state MATCH */ 
        temp = CloneWise_VSMALL_MATRIX(mat,i-1,j-1,SKIP_TARGET) + 0;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-1) == 0 && (j-1) == 0 )   {  
          /* From state START to state MATCH */ 
          temp = CloneWise_VSMALL_SPECIAL(mat,i-1,j-1,START) + 0;    
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for MATCH */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->matrix[i][j];  
         CloneWise_VSMALL_MATRIX(mat,i,j,MATCH) = score; 


        /* state MATCH is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_VSMALL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )   {  
          CloneWise_VSMALL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;  
          }  




        /* Finished calculating state MATCH */ 


        /* For state SKIP_QUERY */ 
        /* setting first movement to score */ 
        score = CloneWise_VSMALL_MATRIX(mat,i-1,j-0,MATCH) + mat->query_skip_start;  
        /* From state SKIP_TARGET to state SKIP_QUERY */ 
        temp = CloneWise_VSMALL_MATRIX(mat,i-1,j-0,SKIP_TARGET) + mat->query_skip_start;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state SKIP_QUERY */ 
        temp = CloneWise_VSMALL_MATRIX(mat,i-1,j-0,SKIP_QUERY) + 0;  
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-1) == 0 && (j-0) == 0 )   {  
          /* From state START to state SKIP_QUERY */ 
          temp = CloneWise_VSMALL_SPECIAL(mat,i-1,j-0,START) + mat->query_skip_start;    
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for SKIP_QUERY */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_iset[i];  
         CloneWise_VSMALL_MATRIX(mat,i,j,SKIP_QUERY) = score;    


        /* state SKIP_QUERY is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_VSMALL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )   {  
          CloneWise_VSMALL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;  
          }  




        /* Finished calculating state SKIP_QUERY */ 


        /* For state SKIP_TARGET */ 
        /* setting first movement to score */ 
        score = CloneWise_VSMALL_MATRIX(mat,i-0,j-1,MATCH) + mat->target_skip_start;     
        /* From state SKIP_QUERY to state SKIP_TARGET */ 
        temp = CloneWise_VSMALL_MATRIX(mat,i-0,j-1,SKIP_QUERY) + mat->target_skip_start;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state SKIP_TARGET */ 
        temp = CloneWise_VSMALL_MATRIX(mat,i-0,j-1,SKIP_TARGET) + 0;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-0) == 0 && (j-1) == 0 )   {  
          /* From state START to state SKIP_TARGET */ 
          temp = CloneWise_VSMALL_SPECIAL(mat,i-0,j-1,START) + mat->target_skip_start;   
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for SKIP_TARGET */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_jset[j];  
         CloneWise_VSMALL_MATRIX(mat,i,j,SKIP_TARGET) = score;   


        /* state SKIP_TARGET is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_VSMALL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )   {  
          CloneWise_VSMALL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;  
          }  




        /* Finished calculating state SKIP_TARGET */ 
        } /* end of for all query positions */ 




      /* Special state START has no special to special movements */ 


      /* Special state TRUSTED_SPECIAL has special to speical */ 
      /* Set score to current score (remember, state probably updated during main loop */ 
      score = CloneWise_VSMALL_SPECIAL(mat,0,j,TRUSTED_SPECIAL); 


      /* Source START is a special source for TRUSTED_SPECIAL */ 
      /* Has restricted position */ 
      if( (j-1) == 0  )  {  
        temp = CloneWise_VSMALL_SPECIAL(mat,0,j - 1,START) + (0) + (0);  
        if( temp > score )   
          score = temp;  
        }  


      /* Source TRUSTED_SPECIAL is a special source for TRUSTED_SPECIAL */ 
      temp = CloneWise_VSMALL_SPECIAL(mat,0,j - 1,TRUSTED_SPECIAL) + ((mat->match->skip_jset[j]-1)) + (0);   
      if( temp > score ) 
        score = temp;    


      /* Source MATCH for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Source SKIP_QUERY for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Source SKIP_TARGET for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Put back score... (now updated!) */ 
      CloneWise_VSMALL_SPECIAL(mat,0,j,TRUSTED_SPECIAL) = score; 
      /* Finished updating state TRUSTED_SPECIAL */ 




      /* Special state END has special to speical */ 
      /* Set score to current score (remember, state probably updated during main loop */ 
      score = CloneWise_VSMALL_SPECIAL(mat,0,j,END); 


      /* Source TRUSTED_SPECIAL is a special source for END */ 
      /* Has restricted position */ 
      if( j == mat->lenj-1 ) {  
        temp = CloneWise_VSMALL_SPECIAL(mat,0,j - 1,TRUSTED_SPECIAL) + (0) + (0);    
        if( temp > score )   
          score = temp;  
        }  


      /* Put back score... (now updated!) */ 
      CloneWise_VSMALL_SPECIAL(mat,0,j,END) = score; 
      /* Finished updating state END */ 


      if( bestscore < CloneWise_VSMALL_SPECIAL(mat,0,j,END) )    
        bestscore = CloneWise_VSMALL_SPECIAL(mat,0,j,END);   
      } /* end of for all target positions */ 


    mat = free_CloneWise(mat);   
    return bestscore;    
}    


/* Function:  PackAln_bestmemory_CloneWise(q,t,match,target_skip_start,target_skip,query_skip_start,query_skip,spread,target_special_s,dpenv,dpri)
 *
 * Descrip:    This function chooses the best memory set-up for the alignment
 *             using calls to basematrix, and then implements either a large
 *             or small memory model.
 *
 *             It is the best function to use if you just want an alignment
 *
 *             If you want a label alignment, you will need
 *             /convert_PackAln_to_AlnBlock_CloneWise
 *
 *
 * Arg:                        q [UNKN ] query data structure [MappedCloneSet *]
 * Arg:                        t [UNKN ] target data structure [MappedCloneSet *]
 * Arg:                    match [UNKN ] Resource [MappedCloneMatch*]
 * Arg:        target_skip_start [UNKN ] Resource [Score]
 * Arg:              target_skip [UNKN ] Resource [Score]
 * Arg:         query_skip_start [UNKN ] Resource [Score]
 * Arg:               query_skip [UNKN ] Resource [Score]
 * Arg:                   spread [UNKN ] Resource [int]
 * Arg:         target_special_s [UNKN ] Resource [int]
 * Arg:                    dpenv [UNKN ] Undocumented argument [DPEnvelope *]
 * Arg:                     dpri [UNKN ] Undocumented argument [DPRunImpl *]
 *
 * Return [UNKN ]  Undocumented return value [PackAln *]
 *
 */
PackAln * PackAln_bestmemory_CloneWise(MappedCloneSet * q,MappedCloneSet * t ,MappedCloneMatch* match,Score target_skip_start,Score target_skip,Score query_skip_start,Score query_skip,int spread,int target_special_s,DPEnvelope * dpenv,DPRunImpl * dpri) 
{
    long long total; 
    CloneWise * mat; 
    PackAln * out;   
    DebugMatrix * de;    
    DPRunImplMemory strategy;    
    assert(dpri);    


    total = q->length * t->length;   
    if( dpri->memory == DPIM_Default )   {  
      if( (total * 3 * sizeof(int)) > 1000*dpri->kbyte_size) {  
        strategy = DPIM_Linear;  
        }  
      else   {  
        strategy = DPIM_Explicit;    
        }  
      }  
    else {  
      strategy = dpri->memory;   
      }  


    if( dpenv != NULL )  {  
      if( strategy == DPIM_Explicit) {  
        if( (mat=allocate_Expl_CloneWise(q, t , match, target_skip_start, target_skip, query_skip_start, query_skip, spread, target_special_s,dpri)) == NULL )   {  
          warn("Unable to allocate large CloneWise version");    
          return NULL;   
          }  
        calculate_dpenv_CloneWise(mat,dpenv);    
        out =  PackAln_read_Expl_CloneWise(mat); 
        }  
      else   {  
        mat = allocate_CloneWise_only(q, t , match, target_skip_start, target_skip, query_skip_start, query_skip, spread, target_special_s);     
        calculate_shatter_CloneWise(mat,dpenv);  
        out = PackAln_read_Shatter_CloneWise(mat);   
        }  
      }  
    else {  
      if( strategy == DPIM_Linear )  {  
        /* use small implementation */ 
        if( (mat=allocate_Small_CloneWise(q, t , match, target_skip_start, target_skip, query_skip_start, query_skip, spread, target_special_s)) == NULL )   {  
          warn("Unable to allocate small CloneWise version");    
          return NULL;   
          }  
        out = PackAln_calculate_Small_CloneWise(mat,dpenv);  
        }  
      else   {  
        /* use Large implementation */ 
        if( (mat=allocate_Expl_CloneWise(q, t , match, target_skip_start, target_skip, query_skip_start, query_skip, spread, target_special_s,dpri)) == NULL )   {  
          warn("Unable to allocate large CloneWise version");    
          return NULL;   
          }  
        if( dpri->debug == TRUE) {  
          fatal("Asked for dydebug, but dynamite file not compiled with -g. Need to recompile dynamite source"); 
          }  
        else {  
          calculate_CloneWise(mat);  
          out =  PackAln_read_Expl_CloneWise(mat);   
          }  
        }  
      }  


    mat = free_CloneWise(mat);   
    return out;  
}    


/* Function:  allocate_CloneWise_only(q,t,match,target_skip_start,target_skip,query_skip_start,query_skip,spread,target_special_s)
 *
 * Descrip:    This function only allocates the CloneWise structure
 *             checks types where possible and determines leni and lenj
 *             The basematrix area is delt with elsewhere
 *
 *
 * Arg:                        q [UNKN ] query data structure [MappedCloneSet *]
 * Arg:                        t [UNKN ] target data structure [MappedCloneSet *]
 * Arg:                    match [UNKN ] Resource [MappedCloneMatch*]
 * Arg:        target_skip_start [UNKN ] Resource [Score]
 * Arg:              target_skip [UNKN ] Resource [Score]
 * Arg:         query_skip_start [UNKN ] Resource [Score]
 * Arg:               query_skip [UNKN ] Resource [Score]
 * Arg:                   spread [UNKN ] Resource [int]
 * Arg:         target_special_s [UNKN ] Resource [int]
 *
 * Return [UNKN ]  Undocumented return value [CloneWise *]
 *
 */
CloneWise * allocate_CloneWise_only(MappedCloneSet * q,MappedCloneSet * t ,MappedCloneMatch* match,Score target_skip_start,Score target_skip,Score query_skip_start,Score query_skip,int spread,int target_special_s) 
{
    CloneWise * out;     


    if((out= CloneWise_alloc()) == NULL) {  
      warn("Allocation of basic CloneWise structure failed..."); 
      return NULL;   
      }  


    out->q = q;  
    out->t = t;  
    out->match = match;  
    out->target_skip_start = target_skip_start;  
    out->target_skip = target_skip;  
    out->query_skip_start = query_skip_start;    
    out->query_skip = query_skip;    
    out->spread = spread;    
    out->target_special_s = target_special_s;    
    out->leni = q->length;   
    out->lenj = t->length;   
    return out;  
}    


/* Function:  allocate_Expl_CloneWise(q,t,match,target_skip_start,target_skip,query_skip_start,query_skip,spread,target_special_s,dpri)
 *
 * Descrip:    This function allocates the CloneWise structure
 *             and the basematrix area for explicit memory implementations
 *             It calls /allocate_CloneWise_only
 *
 *
 * Arg:                        q [UNKN ] query data structure [MappedCloneSet *]
 * Arg:                        t [UNKN ] target data structure [MappedCloneSet *]
 * Arg:                    match [UNKN ] Resource [MappedCloneMatch*]
 * Arg:        target_skip_start [UNKN ] Resource [Score]
 * Arg:              target_skip [UNKN ] Resource [Score]
 * Arg:         query_skip_start [UNKN ] Resource [Score]
 * Arg:               query_skip [UNKN ] Resource [Score]
 * Arg:                   spread [UNKN ] Resource [int]
 * Arg:         target_special_s [UNKN ] Resource [int]
 * Arg:                     dpri [UNKN ] Undocumented argument [DPRunImpl *]
 *
 * Return [UNKN ]  Undocumented return value [CloneWise *]
 *
 */
CloneWise * allocate_Expl_CloneWise(MappedCloneSet * q,MappedCloneSet * t ,MappedCloneMatch* match,Score target_skip_start,Score target_skip,Score query_skip_start,Score query_skip,int spread,int target_special_s,DPRunImpl * dpri) 
{
    CloneWise * out; 


    out = allocate_CloneWise_only(q, t , match, target_skip_start, target_skip, query_skip_start, query_skip, spread, target_special_s); 
    if( out == NULL )    
      return NULL;   
    if( dpri->should_cache == TRUE ) {  
      if( dpri->cache != NULL )  {  
        if( dpri->cache->maxleni >= (out->lenj+1)*3 && dpri->cache->maxlenj >= (out->leni+1))    
          out->basematrix = hard_link_BaseMatrix(dpri->cache);   
        else 
          dpri->cache = free_BaseMatrix(dpri->cache);    
        }  
      }  
    if( out->basematrix == NULL )    {  
      if( (out->basematrix = BaseMatrix_alloc_matrix_and_specials((out->lenj+1)*3,(out->leni+1),3,out->lenj+1)) == NULL) {  
        warn("Explicit matrix CloneWise cannot be allocated, (asking for %d by %d main cells)",out->leni,out->lenj); 
        free_CloneWise(out);     
        return NULL; 
        }  
      }  
    if( dpri->should_cache == TRUE && dpri->cache == NULL)   
      dpri->cache = hard_link_BaseMatrix(out->basematrix);   
    out->basematrix->type = BASEMATRIX_TYPE_EXPLICIT;    
    init_CloneWise(out);     
    return out;  
}    


/* Function:  init_CloneWise(mat)
 *
 * Descrip:    This function initates CloneWise matrix when in explicit mode
 *             Called in /allocate_Expl_CloneWise
 *
 *
 * Arg:        mat [UNKN ] CloneWise which contains explicit basematrix memory [CloneWise *]
 *
 */
void init_CloneWise(CloneWise * mat) 
{
    register int i;  
    register int j;  
    if( mat->basematrix->type != BASEMATRIX_TYPE_EXPLICIT)   {  
      warn("Cannot iniate matrix, is not an explicit memory type and you have assumed that");   
      return;    
      }  


    for(i= (-1);i<mat->q->length;i++)    {  
      for(j= (-1);j<2;j++)   {  
        CloneWise_EXPL_MATRIX(mat,i,j,MATCH) = NEGI; 
        CloneWise_EXPL_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;    
        CloneWise_EXPL_MATRIX(mat,i,j,SKIP_TARGET) = NEGI;   
        }  
      }  
    for(j= (-1);j<mat->t->length;j++)    {  
      for(i= (-1);i<2;i++)   {  
        CloneWise_EXPL_MATRIX(mat,i,j,MATCH) = NEGI; 
        CloneWise_EXPL_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;    
        CloneWise_EXPL_MATRIX(mat,i,j,SKIP_TARGET) = NEGI;   
        }  
      CloneWise_EXPL_SPECIAL(mat,i,j,START) = 0; 
      CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = NEGI;    
      CloneWise_EXPL_SPECIAL(mat,i,j,END) = NEGI;    
      }  
    return;  
}    


/* Function:  recalculate_PackAln_CloneWise(pal,mat)
 *
 * Descrip:    This function recalculates the PackAln structure produced by CloneWise
 *             For example, in linear space methods this is used to score them
 *
 *
 * Arg:        pal [UNKN ] Undocumented argument [PackAln *]
 * Arg:        mat [UNKN ] Undocumented argument [CloneWise *]
 *
 */
void recalculate_PackAln_CloneWise(PackAln * pal,CloneWise * mat) 
{
    int i,j,k,offi,offj; 
    PackAlnUnit * prev;  
    PackAlnUnit * pau;   


    for(k=1,prev=pal->pau[0];k < pal->len;k++,prev=pau)  {  
      pau = pal->pau[k]; 
      i = pau->i;    
      j = pau->j;    
      offi = pau->i - prev->i;   
      offj = pau->j - prev->j;   
      switch(pau->state) {  
        case MATCH :     
          if( offi == 1 && offj == 1 && prev->state == MATCH )   {  
            pau->score = (mat->match->matrix[i][j]+1) + (mat->match->matrix[i][j]);  
            continue;    
            }  
          if( offi == 0 && offj == 1 && prev->state == MATCH )   {  
            pau->score = 0 + (mat->match->matrix[i][j]);     
            continue;    
            }  
          if( offi == 1 && offj == 0 && prev->state == MATCH )   {  
            pau->score = 0 + (mat->match->matrix[i][j]);     
            continue;    
            }  
          if( offi == 1 && offj == 1 && prev->state == SKIP_QUERY )  {  
            pau->score = 0 + (mat->match->matrix[i][j]);     
            continue;    
            }  
          if( offi == 1 && offj == 1 && prev->state == SKIP_TARGET ) {  
            pau->score = 0 + (mat->match->matrix[i][j]);     
            continue;    
            }  
          if( offj == 1 && prev->state == (START+3) )    {  
            pau->score = 0 + (mat->match->matrix[i][j]);     
            continue;    
            }  
          warn("In recaluclating PackAln with state MATCH, from [%d,%d,%d], got a bad source state. Error!",offi,offj,prev->state);  
          break; 
        case SKIP_QUERY :    
          if( offi == 1 && offj == 0 && prev->state == MATCH )   {  
            pau->score = mat->query_skip_start + (mat->match->skip_iset[i]);     
            continue;    
            }  
          if( offi == 1 && offj == 0 && prev->state == SKIP_TARGET ) {  
            pau->score = mat->query_skip_start + (mat->match->skip_iset[i]);     
            continue;    
            }  
          if( offi == 1 && offj == 0 && prev->state == SKIP_QUERY )  {  
            pau->score = 0 + (mat->match->skip_iset[i]);     
            continue;    
            }  
          if( offj == 0 && prev->state == (START+3) )    {  
            pau->score = mat->query_skip_start + (mat->match->skip_iset[i]);     
            continue;    
            }  
          warn("In recaluclating PackAln with state SKIP_QUERY, from [%d,%d,%d], got a bad source state. Error!",offi,offj,prev->state); 
          break; 
        case SKIP_TARGET :   
          if( offi == 0 && offj == 1 && prev->state == MATCH )   {  
            pau->score = mat->target_skip_start + (mat->match->skip_jset[j]);    
            continue;    
            }  
          if( offi == 0 && offj == 1 && prev->state == SKIP_QUERY )  {  
            pau->score = mat->target_skip_start + (mat->match->skip_jset[j]);    
            continue;    
            }  
          if( offi == 0 && offj == 1 && prev->state == SKIP_TARGET ) {  
            pau->score = 0 + (mat->match->skip_jset[j]);     
            continue;    
            }  
          if( offj == 1 && prev->state == (START+3) )    {  
            pau->score = mat->target_skip_start + (mat->match->skip_jset[j]);    
            continue;    
            }  
          warn("In recaluclating PackAln with state SKIP_TARGET, from [%d,%d,%d], got a bad source state. Error!",offi,offj,prev->state);    
          break; 
        case (START+3) :     
          warn("In recaluclating PackAln with state START, got a bad source state. Error!"); 
          break; 
        case (TRUSTED_SPECIAL+3) :   
          if( offj == 1 && prev->state == (START+3) )    {  
            pau->score = 0 + (0);    
            continue;    
            }  
          if( offj == 1 && prev->state == (TRUSTED_SPECIAL+3) )  {  
            pau->score = (mat->match->skip_jset[j]-1) + (0);     
            continue;    
            }  
          if( offj == 0 && prev->state == MATCH )    {  
            /* i here comes from the previous state ;) - not the real one */ 
            i = prev->i; 
            pau->score = mat->target_special_s + (0);    
            continue;    
            }  
          if( offj == 0 && prev->state == SKIP_QUERY )   {  
            /* i here comes from the previous state ;) - not the real one */ 
            i = prev->i; 
            pau->score = mat->target_special_s + (0);    
            continue;    
            }  
          if( offj == 0 && prev->state == SKIP_TARGET )  {  
            /* i here comes from the previous state ;) - not the real one */ 
            i = prev->i; 
            pau->score = mat->target_special_s + (0);    
            continue;    
            }  
          warn("In recaluclating PackAln with state TRUSTED_SPECIAL, got a bad source state. Error!");   
          break; 
        case (END+3) :   
          if( offj == 1 && prev->state == (TRUSTED_SPECIAL+3) )  {  
            pau->score = 0 + (0);    
            continue;    
            }  
          warn("In recaluclating PackAln with state END, got a bad source state. Error!");   
          break; 
        default :    
          warn("In recaluclating PackAln got a bad recipient state. Error!");    
        }  
      prev = pau;    
      }  
    return;  
}    
/* divide and conquor macros are next */ 
#define CloneWise_HIDDEN_MATRIX(thismatrix,i,j,state) (thismatrix->basematrix->matrix[(j-hiddenj+1)][(i+1)*3+state]) 
#define CloneWise_DC_SHADOW_MATRIX(thismatrix,i,j,state) (thismatrix->basematrix->matrix[((j+2)*8) % 16][(i+1)*3+state]) 
#define CloneWise_HIDDEN_SPECIAL(thismatrix,i,j,state) (thismatrix->basematrix->specmatrix[state][(j+1)])    
#define CloneWise_DC_SHADOW_SPECIAL(thismatrix,i,j,state) (thismatrix->basematrix->specmatrix[state*8][(j+1)])   
#define CloneWise_DC_SHADOW_MATRIX_SP(thismatrix,i,j,state,shadow) (thismatrix->basematrix->matrix[((((j+2)*8)+(shadow+1)) % 16)][(i+1)*3 + state])  
#define CloneWise_DC_SHADOW_SPECIAL_SP(thismatrix,i,j,state,shadow) (thismatrix->basematrix->specmatrix[state*8 +shadow+1][(j+1)])   
#define CloneWise_DC_OPT_SHADOW_MATRIX(thismatrix,i,j,state) (score_pointers[(((j+1)% 1) * (leni+1) * 3) + ((i+1) * 3) + (state)])   
#define CloneWise_DC_OPT_SHADOW_MATRIX_SP(thismatrix,i,j,state,shadow) (shadow_pointers[(((j+1)% 1) * (leni+1) * 24) + ((i+1) * 24) + (state * 8) + shadow+1])   
#define CloneWise_DC_OPT_SHADOW_SPECIAL(thismatrix,i,j,state) (thismatrix->basematrix->specmatrix[state*8][(j+1)])   
/* Function:  allocate_Small_CloneWise(q,t,match,target_skip_start,target_skip,query_skip_start,query_skip,spread,target_special_s)
 *
 * Descrip:    This function allocates the CloneWise structure
 *             and the basematrix area for a small memory implementations
 *             It calls /allocate_CloneWise_only
 *
 *
 * Arg:                        q [UNKN ] query data structure [MappedCloneSet *]
 * Arg:                        t [UNKN ] target data structure [MappedCloneSet *]
 * Arg:                    match [UNKN ] Resource [MappedCloneMatch*]
 * Arg:        target_skip_start [UNKN ] Resource [Score]
 * Arg:              target_skip [UNKN ] Resource [Score]
 * Arg:         query_skip_start [UNKN ] Resource [Score]
 * Arg:               query_skip [UNKN ] Resource [Score]
 * Arg:                   spread [UNKN ] Resource [int]
 * Arg:         target_special_s [UNKN ] Resource [int]
 *
 * Return [UNKN ]  Undocumented return value [CloneWise *]
 *
 */
#define CloneWise_DC_OPT_SHADOW_SPECIAL_SP(thismatrix,i,j,state,shadow) (thismatrix->basematrix->specmatrix[state*8 +shadow+1][(j+1)])   
CloneWise * allocate_Small_CloneWise(MappedCloneSet * q,MappedCloneSet * t ,MappedCloneMatch* match,Score target_skip_start,Score target_skip,Score query_skip_start,Score query_skip,int spread,int target_special_s) 
{
    CloneWise * out; 


    out = allocate_CloneWise_only(q, t , match, target_skip_start, target_skip, query_skip_start, query_skip, spread, target_special_s); 
    if( out == NULL )    
      return NULL;   
    out->basematrix = BaseMatrix_alloc_matrix_and_specials(16,(out->leni + 1) * 3,24,out->lenj+1);   
    if(out == NULL)  {  
      warn("Small shadow matrix CloneWise cannot be allocated, (asking for 2 by %d main cells)",out->leni+2);    
      free_CloneWise(out);   
      return NULL;   
      }  
    out->basematrix->type = BASEMATRIX_TYPE_SHADOW;  
    return out;  
}    


/* Function:  PackAln_calculate_Small_CloneWise(mat,dpenv)
 *
 * Descrip:    This function calculates an alignment for CloneWise structure in linear space
 *             If you want only the start/end points
 *             use /AlnRangeSet_calculate_Small_CloneWise 
 *
 *             The function basically
 *               finds start/end points 
 *               foreach start/end point 
 *                 calls /full_dc_CloneWise 
 *
 *
 * Arg:          mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:        dpenv [UNKN ] Undocumented argument [DPEnvelope *]
 *
 * Return [UNKN ]  Undocumented return value [PackAln *]
 *
 */
PackAln * PackAln_calculate_Small_CloneWise(CloneWise * mat,DPEnvelope * dpenv) 
{
    int endj;    
    int score;   
    PackAln * out;   
    PackAlnUnit * pau;   
    int starti;  
    int startj;  
    int startstate;  
    int stopi;   
    int stopj;   
    int stopstate;   
    int temp;    
    int donej;  /* This is for reporting, will be passed as a & arg in */ 
    int totalj; /* This also is for reporting, but as is not changed, can be passed by value */ 


    if( mat->basematrix->type != BASEMATRIX_TYPE_SHADOW )    {  
      warn("Could not calculate packaln small for CloneWise due to wrong type of matrix");   
      return NULL;   
      }  


    out = PackAln_alloc_std();   


    start_reporting("Find start end points: ");  
    dc_optimised_start_end_calc_CloneWise(mat,dpenv);    
    score = start_end_find_end_CloneWise(mat,&endj); 
    out->score = score;  
    stopstate = END;
    
    /* Special to specials: have to eat up in strip and then drop back to full_dc for intervening bits */ 
    log_full_error(REPORT,0,"End at %d Score %d",endj,score);    
    stop_reporting();    
    for(;;)  { /*while there are more special bits to recover*/ 
      start_reporting("Special cell aln end   %d:",endj);    
      if( read_special_strip_CloneWise(mat,0,endj,stopstate,&endj,&startstate,out) == FALSE )    {  
        warn("Problem in reading off special state system... going to return partial alignment");    
        break;   
        }  
      if( startstate == START || endj <= 0)  {  
        log_full_error(REPORT,0,"Recovered complete alignment"); 
        stop_reporting();    
        break;   
        }  


      log_full_error(REPORT,0,"Finished to %d",endj);    
      stop_reporting();  


      /* Ok... have to eat up another piece of matrix <sigh> */ 
      temp = startstate; 
      starti = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,endj,temp,0);    
      startj = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,endj,temp,1);    
      startstate = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,endj,temp,2);    
      stopi = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,endj,temp,3); 
      stopj = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,endj,temp,4); 
      stopstate = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,endj,temp,5); 


      /* Get out the score of this block. V. important! */ 
      temp = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,endj,temp,6);  
      totalj = stopj - startj;   
      donej  = 0;    
      start_reporting("Main matrix  aln [%d,%d]:",startj,stopj);     
      if(full_dc_CloneWise(mat,starti,startj,startstate,stopi,stopj,stopstate,out,&donej,totalj,dpenv) == FALSE) {  
        warn("In the alignment CloneWise [%d,%d][%d,%d], got a problem. Please report bug ... giving you back a partial alignment",starti,startj,stopi,stopj);   
        return out;  
        }  


      /* now have to figure out which special we came from... yikes */ 
      max_matrix_to_special_CloneWise(mat,starti,startj,startstate,temp,&stopi,&stopj,&stopstate,&temp,NULL);    
      if( stopi == CloneWise_READ_OFF_ERROR) {  
        warn("In CloneWise read off ending at %d ... got a bad matrix to special read off... returning partial alignment",startj);   
        invert_PackAln(out); 
        recalculate_PackAln_CloneWise(out,mat);  
        return out;  
        }  
      /* if at start, break, otherwise, back to eat another strip */ 
      if( stopstate == START)    {  
        log_full_error(REPORT,0,"Recovered complete alignment      ");   
        stop_reporting();    
        break;   
        }  
      log_full_error(REPORT,0,"Finished  alignment to %d           ",startj);    
      stop_reporting();  
      endj = stopj;  
      /* stopstate is correct as it is */ 
      } /* end of while there are more special bits to recover */ 
    invert_PackAln(out); 
    recalculate_PackAln_CloneWise(out,mat);  
    return out;  


}    


/* Function:  AlnRangeSet_calculate_Small_CloneWise(mat)
 *
 * Descrip:    This function calculates an alignment for CloneWise structure in linear space
 *             If you want the full alignment, use /PackAln_calculate_Small_CloneWise 
 *             If you have already got the full alignment, but want the range set, use /AlnRangeSet_from_PackAln_CloneWise
 *             If you have got the small matrix but not the alignment, use /AlnRangeSet_from_CloneWise 
 *
 *
 * Arg:        mat [UNKN ] Undocumented argument [CloneWise *]
 *
 * Return [UNKN ]  Undocumented return value [AlnRangeSet *]
 *
 */
AlnRangeSet * AlnRangeSet_calculate_Small_CloneWise(CloneWise * mat) 
{
    AlnRangeSet * out;   


    start_reporting("Find start end points: ");  
    dc_optimised_start_end_calc_CloneWise(mat,NULL); 
    log_full_error(REPORT,0,"Calculated");   


    out = AlnRangeSet_from_CloneWise(mat);   
    return out;  
}    


/* Function:  AlnRangeSet_from_CloneWise(mat)
 *
 * Descrip:    This function reads off a start/end structure
 *             for CloneWise structure in linear space
 *             If you want the full alignment use
 *             /PackAln_calculate_Small_CloneWise 
 *             If you have not calculated the matrix use
 *             /AlnRange_calculate_Small_CloneWise
 *
 *
 * Arg:        mat [UNKN ] Undocumented argument [CloneWise *]
 *
 * Return [UNKN ]  Undocumented return value [AlnRangeSet *]
 *
 */
AlnRangeSet * AlnRangeSet_from_CloneWise(CloneWise * mat) 
{
    AlnRangeSet * out;   
    AlnRange * temp; 
    int jpos;    
    int state;   


    if( mat->basematrix->type != BASEMATRIX_TYPE_SHADOW) {  
      warn("Bad error! - non shadow matrix type in AlnRangeSet_from_CloneWise"); 
      return NULL;   
      }  


    out = AlnRangeSet_alloc_std();   
    /* Find the end position */ 
    out->score = start_end_find_end_CloneWise(mat,&jpos);    
    state = END; 


    while( (temp = AlnRange_build_CloneWise(mat,jpos,state,&jpos,&state)) != NULL)   
      add_AlnRangeSet(out,temp); 
    return out;  
}    


/* Function:  AlnRange_build_CloneWise(mat,stopj,stopspecstate,startj,startspecstate)
 *
 * Descrip:    This function calculates a single start/end set in linear space
 *             Really a sub-routine for /AlnRangeSet_from_PackAln_CloneWise
 *
 *
 * Arg:                   mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:                 stopj [UNKN ] Undocumented argument [int]
 * Arg:         stopspecstate [UNKN ] Undocumented argument [int]
 * Arg:                startj [UNKN ] Undocumented argument [int *]
 * Arg:        startspecstate [UNKN ] Undocumented argument [int *]
 *
 * Return [UNKN ]  Undocumented return value [AlnRange *]
 *
 */
AlnRange * AlnRange_build_CloneWise(CloneWise * mat,int stopj,int stopspecstate,int * startj,int * startspecstate) 
{
    AlnRange * out;  
    int jpos;    
    int state;   


    if( mat->basematrix->type != BASEMATRIX_TYPE_SHADOW) {  
      warn("Bad error! - non shadow matrix type in AlnRangeSet_from_CloneWise"); 
      return NULL;   
      }  


    /* Assumme that we have specials (we should!). Read back along the specials till we have the finish point */ 
    if( read_special_strip_CloneWise(mat,0,stopj,stopspecstate,&jpos,&state,NULL) == FALSE)  {  
      warn("In AlnRanger_build_CloneWise alignment ending at %d, unable to read back specials. Will (evenutally) return a partial range set... BEWARE!",stopj);  
      return NULL;   
      }  
    if( state == START || jpos <= 0) 
      return NULL;   


    out = AlnRange_alloc();  


    out->starti = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,jpos,state,0);    
    out->startj = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,jpos,state,1);    
    out->startstate = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,jpos,state,2);    
    out->stopi = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,jpos,state,3); 
    out->stopj = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,jpos,state,4); 
    out->stopstate = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,jpos,state,5); 
    out->startscore = CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,jpos,state,6);    
    out->stopscore = CloneWise_DC_SHADOW_SPECIAL(mat,0,jpos,state);  


    /* Now, we have to figure out where this state came from in the specials */ 
    max_matrix_to_special_CloneWise(mat,out->starti,out->startj,out->startstate,out->startscore,&jpos,startj,startspecstate,&state,NULL);    
    if( jpos == CloneWise_READ_OFF_ERROR)    {  
      warn("In AlnRange_build_CloneWise alignment ending at %d, with aln range between %d-%d in j, unable to find source special, returning this range, but this could get tricky!",stopj,out->startj,out->stopj);   
      return out;    
      }  


    /* Put in the correct score for startstate, from the special */ 
    out->startscore = CloneWise_DC_SHADOW_SPECIAL(mat,0,*startj,*startspecstate);    
    /* The correct j coords have been put into startj, startspecstate... so just return out */ 
    return out;  
}    


/* Function:  read_hidden_CloneWise(mat,starti,startj,startstate,stopi,stopj,stopstate,out)
 *
 * Descrip: No Description
 *
 * Arg:               mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:            starti [UNKN ] Undocumented argument [int]
 * Arg:            startj [UNKN ] Undocumented argument [int]
 * Arg:        startstate [UNKN ] Undocumented argument [int]
 * Arg:             stopi [UNKN ] Undocumented argument [int]
 * Arg:             stopj [UNKN ] Undocumented argument [int]
 * Arg:         stopstate [UNKN ] Undocumented argument [int]
 * Arg:               out [UNKN ] Undocumented argument [PackAln *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean read_hidden_CloneWise(CloneWise * mat,int starti,int startj,int startstate,int stopi,int stopj,int stopstate,PackAln * out) 
{
    int i;   
    int j;   
    int state;   
    int cellscore;   
    int isspecial;   
    /* We don't need hiddenj here, 'cause matrix access handled by max funcs */ 
    PackAlnUnit * pau;   


    /* stop position is on the path */ 
    i = stopi;   
    j = stopj;   
    state= stopstate;    
    isspecial = FALSE;   


    while( i >= starti && j >= startj)   {  
      /* Put away current i,j,state */ 
      pau = PackAlnUnit_alloc();/* Should deal with memory overflow */ 
      pau->i = i;    
      pau->j = j;    
      pau->state =  state;   
      add_PackAln(out,pau);  


      max_hidden_CloneWise(mat,startj,i,j,state,isspecial,&i,&j,&state,&isspecial,&cellscore);   


      if( i == CloneWise_READ_OFF_ERROR) {  
        warn("In CloneWise hidden read off, between %d:%d,%d:%d - at got bad read off. Problem!",starti,startj,stopi,stopj); 
        return FALSE;    
        }  


      if( i == starti && j == startj && state == startstate) {  
/* Put away final state (start of this block) */ 
        pau = PackAlnUnit_alloc();  /* Should deal with memory overflow */ 
        pau->i = i;  
        pau->j = j;  
        pau->state =  state; 
        add_PackAln(out,pau);    
          return TRUE;   
        }  
      if( i == starti && j == startj)    {  
        warn("In CloneWise hidden read off, between %d:%d,%d:%d - hit start cell, but not in start state. Can't be good!.",starti,startj,stopi,stopj);   
        return FALSE;    
        }  
      }  
    warn("In CloneWise hidden read off, between %d:%d,%d:%d - gone past start cell (now in %d,%d,%d), can't be good news!.",starti,startj,stopi,stopj,i,j,state);    
    return FALSE;    
}    


/* Function:  max_hidden_CloneWise(mat,hiddenj,i,j,state,isspecial,reti,retj,retstate,retspecial,cellscore)
 *
 * Descrip: No Description
 *
 * Arg:               mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:           hiddenj [UNKN ] Undocumented argument [int]
 * Arg:                 i [UNKN ] Undocumented argument [int]
 * Arg:                 j [UNKN ] Undocumented argument [int]
 * Arg:             state [UNKN ] Undocumented argument [int]
 * Arg:         isspecial [UNKN ] Undocumented argument [boolean]
 * Arg:              reti [UNKN ] Undocumented argument [int *]
 * Arg:              retj [UNKN ] Undocumented argument [int *]
 * Arg:          retstate [UNKN ] Undocumented argument [int *]
 * Arg:        retspecial [UNKN ] Undocumented argument [boolean *]
 * Arg:         cellscore [UNKN ] Undocumented argument [int *]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int max_hidden_CloneWise(CloneWise * mat,int hiddenj,int i,int j,int state,boolean isspecial,int * reti,int * retj,int * retstate,boolean * retspecial,int * cellscore) 
{
    register int temp;   
    register int cscore; 


    *reti = (*retj) = (*retstate) = CloneWise_READ_OFF_ERROR;    


    if( i < 0 || j < 0 || i > mat->q->length || j > mat->t->length)  {  
      warn("In CloneWise matrix special read off - out of bounds on matrix [i,j is %d,%d state %d in standard matrix]",i,j,state);   
      return -1; 
      }  


    /* Then you have to select the correct switch statement to figure out the readoff      */ 
    /* Somewhat odd - reverse the order of calculation and return as soon as it is correct */ 
    cscore = CloneWise_HIDDEN_MATRIX(mat,i,j,state); 
    switch(state)    { /*Switch state */ 
      case MATCH :   
        /* Not allowing special sources.. skipping START */ 
        temp = cscore - (0) -  (mat->match->matrix[i][j]);   
        if( temp == CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 1,SKIP_TARGET) )   {  
          *reti = i - 1; 
          *retj = j - 1; 
          *retstate = SKIP_TARGET;   
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_HIDDEN_MATRIX(mat,i-1,j-1,SKIP_TARGET);  
            }  
          return CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 1,SKIP_TARGET);   
          }  
        temp = cscore - (0) -  (mat->match->matrix[i][j]);   
        if( temp == CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 1,SKIP_QUERY) )    {  
          *reti = i - 1; 
          *retj = j - 1; 
          *retstate = SKIP_QUERY;    
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_HIDDEN_MATRIX(mat,i-1,j-1,SKIP_QUERY);   
            }  
          return CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 1,SKIP_QUERY);    
          }  
        temp = cscore - (0) -  (mat->match->matrix[i][j]);   
        if( temp == CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 0,MATCH) ) {  
          *reti = i - 1; 
          *retj = j - 0; 
          *retstate = MATCH; 
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_HIDDEN_MATRIX(mat,i-1,j-0,MATCH);    
            }  
          return CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 0,MATCH);     
          }  
        temp = cscore - (0) -  (mat->match->matrix[i][j]);   
        if( temp == CloneWise_HIDDEN_MATRIX(mat,i - 0,j - 1,MATCH) ) {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = MATCH; 
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_HIDDEN_MATRIX(mat,i-0,j-1,MATCH);    
            }  
          return CloneWise_HIDDEN_MATRIX(mat,i - 0,j - 1,MATCH);     
          }  
        temp = cscore - ((mat->match->matrix[i][j]+1)) -  (mat->match->matrix[i][j]);    
        if( temp == CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 1,MATCH) ) {  
          *reti = i - 1; 
          *retj = j - 1; 
          *retstate = MATCH; 
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_HIDDEN_MATRIX(mat,i-1,j-1,MATCH);    
            }  
          return CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 1,MATCH);     
          }  
        warn("Major problem (!) - in CloneWise read off, position %d,%d state %d no source found!",i,j,state);   
        return (-1); 
      case SKIP_QUERY :  
        /* Not allowing special sources.. skipping START */ 
        temp = cscore - (0) -  (mat->match->skip_iset[i]);   
        if( temp == CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 0,SKIP_QUERY) )    {  
          *reti = i - 1; 
          *retj = j - 0; 
          *retstate = SKIP_QUERY;    
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_HIDDEN_MATRIX(mat,i-1,j-0,SKIP_QUERY);   
            }  
          return CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 0,SKIP_QUERY);    
          }  
        temp = cscore - (mat->query_skip_start) -  (mat->match->skip_iset[i]);   
        if( temp == CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 0,SKIP_TARGET) )   {  
          *reti = i - 1; 
          *retj = j - 0; 
          *retstate = SKIP_TARGET;   
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_HIDDEN_MATRIX(mat,i-1,j-0,SKIP_TARGET);  
            }  
          return CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 0,SKIP_TARGET);   
          }  
        temp = cscore - (mat->query_skip_start) -  (mat->match->skip_iset[i]);   
        if( temp == CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 0,MATCH) ) {  
          *reti = i - 1; 
          *retj = j - 0; 
          *retstate = MATCH; 
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_HIDDEN_MATRIX(mat,i-1,j-0,MATCH);    
            }  
          return CloneWise_HIDDEN_MATRIX(mat,i - 1,j - 0,MATCH);     
          }  
        warn("Major problem (!) - in CloneWise read off, position %d,%d state %d no source found!",i,j,state);   
        return (-1); 
      case SKIP_TARGET :     
        /* Not allowing special sources.. skipping START */ 
        temp = cscore - (0) -  (mat->match->skip_jset[j]);   
        if( temp == CloneWise_HIDDEN_MATRIX(mat,i - 0,j - 1,SKIP_TARGET) )   {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = SKIP_TARGET;   
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_HIDDEN_MATRIX(mat,i-0,j-1,SKIP_TARGET);  
            }  
          return CloneWise_HIDDEN_MATRIX(mat,i - 0,j - 1,SKIP_TARGET);   
          }  
        temp = cscore - (mat->target_skip_start) -  (mat->match->skip_jset[j]);  
        if( temp == CloneWise_HIDDEN_MATRIX(mat,i - 0,j - 1,SKIP_QUERY) )    {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = SKIP_QUERY;    
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_HIDDEN_MATRIX(mat,i-0,j-1,SKIP_QUERY);   
            }  
          return CloneWise_HIDDEN_MATRIX(mat,i - 0,j - 1,SKIP_QUERY);    
          }  
        temp = cscore - (mat->target_skip_start) -  (mat->match->skip_jset[j]);  
        if( temp == CloneWise_HIDDEN_MATRIX(mat,i - 0,j - 1,MATCH) ) {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = MATCH; 
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_HIDDEN_MATRIX(mat,i-0,j-1,MATCH);    
            }  
          return CloneWise_HIDDEN_MATRIX(mat,i - 0,j - 1,MATCH);     
          }  
        warn("Major problem (!) - in CloneWise read off, position %d,%d state %d no source found!",i,j,state);   
        return (-1); 
      default:   
        warn("Major problem (!) - in CloneWise read off, position %d,%d state %d no source found!",i,j,state);   
        return (-1); 
      } /* end of Switch state  */ 
}    


/* Function:  read_special_strip_CloneWise(mat,stopi,stopj,stopstate,startj,startstate,out)
 *
 * Descrip: No Description
 *
 * Arg:               mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:             stopi [UNKN ] Undocumented argument [int]
 * Arg:             stopj [UNKN ] Undocumented argument [int]
 * Arg:         stopstate [UNKN ] Undocumented argument [int]
 * Arg:            startj [UNKN ] Undocumented argument [int *]
 * Arg:        startstate [UNKN ] Undocumented argument [int *]
 * Arg:               out [UNKN ] Undocumented argument [PackAln *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean read_special_strip_CloneWise(CloneWise * mat,int stopi,int stopj,int stopstate,int * startj,int * startstate,PackAln * out) 
{
    int i;   
    int j;   
    int state;   
    int cellscore;   
    int isspecial;   
    PackAlnUnit * pau;   


    /* stop position is on the path */ 
    i = stopi;   
    j = stopj;   
    state= stopstate;    
    isspecial = TRUE;    


    /* Loop until state has the same j as its stop in shadow pointers */ 
    /* This will be the state is came out from, OR it has hit !start */ 
    /* We may not want to get the alignment, in which case out will be NULL */ 
    while( j > CloneWise_DC_SHADOW_SPECIAL_SP(mat,i,j,state,4) && state != START)    { /*while more specials to eat up*/ 
      /* Put away current state, if we should */ 
      if(out != NULL)    {  
        pau = PackAlnUnit_alloc();  /* Should deal with memory overflow */ 
        pau->i = i;  
        pau->j = j;  
        pau->state =  state + 3; 
        add_PackAln(out,pau);    
        }  


      max_special_strip_CloneWise(mat,i,j,state,isspecial,&i,&j,&state,&isspecial,&cellscore);   
      if( i == CloneWise_READ_OFF_ERROR) {  
        warn("In special strip read CloneWise, got a bad read off error. Sorry!");   
        return FALSE;    
        }  
      } /* end of while more specials to eat up */ 


    /* check to see we have not gone too far! */ 
    if( state != START && j < CloneWise_DC_SHADOW_SPECIAL_SP(mat,i,j,state,4))   {  
      warn("In special strip read CloneWise, at special [%d] state [%d] overshot!",j,state); 
      return FALSE;  
      }  
    /* Put away last state */ 
    if(out != NULL)  {  
      pau = PackAlnUnit_alloc();/* Should deal with memory overflow */ 
      pau->i = i;    
      pau->j = j;    
      pau->state =  state + 3;   
      add_PackAln(out,pau);  
      }  


    /* Put away where we are in startj and startstate */ 
    *startj = j; 
    *startstate = state; 
    return TRUE; 
}    


/* Function:  max_special_strip_CloneWise(mat,i,j,state,isspecial,reti,retj,retstate,retspecial,cellscore)
 *
 * Descrip:    A pretty intense internal function. Deals with read-off only in specials
 *
 *
 * Arg:               mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:                 i [UNKN ] Undocumented argument [int]
 * Arg:                 j [UNKN ] Undocumented argument [int]
 * Arg:             state [UNKN ] Undocumented argument [int]
 * Arg:         isspecial [UNKN ] Undocumented argument [boolean]
 * Arg:              reti [UNKN ] Undocumented argument [int *]
 * Arg:              retj [UNKN ] Undocumented argument [int *]
 * Arg:          retstate [UNKN ] Undocumented argument [int *]
 * Arg:        retspecial [UNKN ] Undocumented argument [boolean *]
 * Arg:         cellscore [UNKN ] Undocumented argument [int *]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int max_special_strip_CloneWise(CloneWise * mat,int i,int j,int state,boolean isspecial,int * reti,int * retj,int * retstate,boolean * retspecial,int * cellscore) 
{
    int temp;    
    int cscore;  


    *reti = (*retj) = (*retstate) = CloneWise_READ_OFF_ERROR;    
    if( isspecial == FALSE ) {  
      warn("In special strip max function for CloneWise, got a non special start point. Problem! (bad!)");   
      return (-1);   
      }  


    if( j < 0 || j > mat->t->length) {  
      warn("In CloneWise matrix special read off - out of bounds on matrix [j is %d in special]",j); 
      return -1; 
      }  


    cscore = CloneWise_DC_SHADOW_SPECIAL(mat,i,j,state); 
    switch(state)    { /*switch on special states*/ 
      case START :   
      case TRUSTED_SPECIAL :     
        /* Source SKIP_TARGET is not a special */ 
        /* Source SKIP_QUERY is not a special */ 
        /* Source MATCH is not a special */ 
        /* source TRUSTED_SPECIAL is a special */ 
        temp = cscore - ((mat->match->skip_jset[j]-1)) - (0);    
        if( temp == CloneWise_DC_SHADOW_SPECIAL(mat,i - 0,j - 1,TRUSTED_SPECIAL) )   {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = TRUSTED_SPECIAL;   
          *retspecial = TRUE;    
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_DC_SHADOW_SPECIAL(mat,i-0,j-1,TRUSTED_SPECIAL);  
            }  
          return CloneWise_DC_SHADOW_MATRIX(mat,i - 0,j - 1,TRUSTED_SPECIAL) ;   
          }  
        /* source START is a special */ 
        temp = cscore - (0) - (0);   
        if( temp == CloneWise_DC_SHADOW_SPECIAL(mat,i - 0,j - 1,START) ) {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = START; 
          *retspecial = TRUE;    
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_DC_SHADOW_SPECIAL(mat,i-0,j-1,START);    
            }  
          return CloneWise_DC_SHADOW_MATRIX(mat,i - 0,j - 1,START) ;     
          }  
      case END :     
        /* source TRUSTED_SPECIAL is a special */ 
        temp = cscore - (0) - (0);   
        if( temp == CloneWise_DC_SHADOW_SPECIAL(mat,i - 0,j - 1,TRUSTED_SPECIAL) )   {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = TRUSTED_SPECIAL;   
          *retspecial = TRUE;    
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_DC_SHADOW_SPECIAL(mat,i-0,j-1,TRUSTED_SPECIAL);  
            }  
          return CloneWise_DC_SHADOW_MATRIX(mat,i - 0,j - 1,TRUSTED_SPECIAL) ;   
          }  
      default:   
        warn("Major problem (!) - in CloneWise special strip read off, position %d,%d state %d no source found  dropped into default on source switch!",i,j,state);  
        return (-1); 
      } /* end of switch on special states */ 
}    


/* Function:  max_matrix_to_special_CloneWise(mat,i,j,state,cscore,reti,retj,retstate,retspecial,cellscore)
 *
 * Descrip: No Description
 *
 * Arg:               mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:                 i [UNKN ] Undocumented argument [int]
 * Arg:                 j [UNKN ] Undocumented argument [int]
 * Arg:             state [UNKN ] Undocumented argument [int]
 * Arg:            cscore [UNKN ] Undocumented argument [int]
 * Arg:              reti [UNKN ] Undocumented argument [int *]
 * Arg:              retj [UNKN ] Undocumented argument [int *]
 * Arg:          retstate [UNKN ] Undocumented argument [int *]
 * Arg:        retspecial [UNKN ] Undocumented argument [boolean *]
 * Arg:         cellscore [UNKN ] Undocumented argument [int *]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int max_matrix_to_special_CloneWise(CloneWise * mat,int i,int j,int state,int cscore,int * reti,int * retj,int * retstate,boolean * retspecial,int * cellscore) 
{
    int temp;    
    *reti = (*retj) = (*retstate) = CloneWise_READ_OFF_ERROR;    


    if( j < 0 || j > mat->lenj)  {  
      warn("In CloneWise matrix to special read off - out of bounds on matrix [j is %d in special]",j);  
      return -1; 
      }  


    switch(state)    { /*Switch state */ 
      case MATCH :   
        temp = cscore - (0) -  (mat->match->matrix[i][j]);   
        if( temp == CloneWise_DC_SHADOW_SPECIAL(mat,i - 1,j - 1,START) ) {  
          *reti = i - 1; 
          *retj = j - 1; 
          *retstate = START; 
          *retspecial = TRUE;    
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_DC_SHADOW_SPECIAL(mat,i-1,j-1,START);    
            }  
          return CloneWise_DC_SHADOW_MATRIX(mat,i - 1,j - 1,START) ;     
          }  
        /* Source SKIP_TARGET is not a special, should not get here! */ 
        /* Source SKIP_QUERY is not a special, should not get here! */ 
        /* Source MATCH is not a special, should not get here! */ 
        /* Source MATCH is not a special, should not get here! */ 
        /* Source MATCH is not a special, should not get here! */ 
        warn("Major problem (!) - in CloneWise matrix to special read off, position %d,%d state %d no source found!",i,j,state); 
        return (-1); 
      case SKIP_QUERY :  
        temp = cscore - (mat->query_skip_start) -  (mat->match->skip_iset[i]);   
        if( temp == CloneWise_DC_SHADOW_SPECIAL(mat,i - 1,j - 0,START) ) {  
          *reti = i - 1; 
          *retj = j - 0; 
          *retstate = START; 
          *retspecial = TRUE;    
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_DC_SHADOW_SPECIAL(mat,i-1,j-0,START);    
            }  
          return CloneWise_DC_SHADOW_MATRIX(mat,i - 1,j - 0,START) ;     
          }  
        /* Source SKIP_QUERY is not a special, should not get here! */ 
        /* Source SKIP_TARGET is not a special, should not get here! */ 
        /* Source MATCH is not a special, should not get here! */ 
        warn("Major problem (!) - in CloneWise matrix to special read off, position %d,%d state %d no source found!",i,j,state); 
        return (-1); 
      case SKIP_TARGET :     
        temp = cscore - (mat->target_skip_start) -  (mat->match->skip_jset[j]);  
        if( temp == CloneWise_DC_SHADOW_SPECIAL(mat,i - 0,j - 1,START) ) {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = START; 
          *retspecial = TRUE;    
          if( cellscore != NULL) {  
            *cellscore = cscore - CloneWise_DC_SHADOW_SPECIAL(mat,i-0,j-1,START);    
            }  
          return CloneWise_DC_SHADOW_MATRIX(mat,i - 0,j - 1,START) ;     
          }  
        /* Source SKIP_TARGET is not a special, should not get here! */ 
        /* Source SKIP_QUERY is not a special, should not get here! */ 
        /* Source MATCH is not a special, should not get here! */ 
        warn("Major problem (!) - in CloneWise matrix to special read off, position %d,%d state %d no source found!",i,j,state); 
        return (-1); 
      default:   
        warn("Major problem (!) - in CloneWise read off, position %d,%d state %d no source found!",i,j,state);   
        return (-1); 
      } /* end of Switch state  */ 


}    


/* Function:  calculate_hidden_CloneWise(mat,starti,startj,startstate,stopi,stopj,stopstate,dpenv)
 *
 * Descrip: No Description
 *
 * Arg:               mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:            starti [UNKN ] Undocumented argument [int]
 * Arg:            startj [UNKN ] Undocumented argument [int]
 * Arg:        startstate [UNKN ] Undocumented argument [int]
 * Arg:             stopi [UNKN ] Undocumented argument [int]
 * Arg:             stopj [UNKN ] Undocumented argument [int]
 * Arg:         stopstate [UNKN ] Undocumented argument [int]
 * Arg:             dpenv [UNKN ] Undocumented argument [DPEnvelope *]
 *
 */
void calculate_hidden_CloneWise(CloneWise * mat,int starti,int startj,int startstate,int stopi,int stopj,int stopstate,DPEnvelope * dpenv) 
{
    register int i;  
    register int j;  
    register int score;  
    register int temp;   
    register int hiddenj;    


    hiddenj = startj;    


    init_hidden_CloneWise(mat,starti,startj,stopi,stopj);    


    CloneWise_HIDDEN_MATRIX(mat,starti,startj,startstate) = 0;   


    for(j=startj;j<=stopj;j++)   {  
      for(i=starti;i<=stopi;i++) {  
        /* Should *not* do very first cell as this is the one set to zero in one state! */ 
        if( i == starti && j == startj ) 
          continue;  
        if( dpenv != NULL && is_in_DPEnvelope(dpenv,i,j) == FALSE )  { /*Is not in envelope*/ 
          CloneWise_HIDDEN_MATRIX(mat,i,j,MATCH) = NEGI;     
          CloneWise_HIDDEN_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;    
          CloneWise_HIDDEN_MATRIX(mat,i,j,SKIP_TARGET) = NEGI;   
          continue;  
          } /* end of Is not in envelope */ 


        /* For state MATCH */ 
        /* setting first movement to score */ 
        score = CloneWise_HIDDEN_MATRIX(mat,i-1,j-1,MATCH) + (mat->match->matrix[i][j]+1);   
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_HIDDEN_MATRIX(mat,i-0,j-1,MATCH) + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_HIDDEN_MATRIX(mat,i-1,j-0,MATCH) + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state MATCH */ 
        temp = CloneWise_HIDDEN_MATRIX(mat,i-1,j-1,SKIP_QUERY) + 0;  
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state MATCH */ 
        temp = CloneWise_HIDDEN_MATRIX(mat,i-1,j-1,SKIP_TARGET) + 0;     
        if( temp  > score )  {  
          score = temp;  
          }  


        /* Ok - finished max calculation for MATCH */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->matrix[i][j];  
         CloneWise_HIDDEN_MATRIX(mat,i,j,MATCH) = score; 
        /* Finished calculating state MATCH */ 


        /* For state SKIP_QUERY */ 
        /* setting first movement to score */ 
        score = CloneWise_HIDDEN_MATRIX(mat,i-1,j-0,MATCH) + mat->query_skip_start;  
        /* From state SKIP_TARGET to state SKIP_QUERY */ 
        temp = CloneWise_HIDDEN_MATRIX(mat,i-1,j-0,SKIP_TARGET) + mat->query_skip_start;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state SKIP_QUERY */ 
        temp = CloneWise_HIDDEN_MATRIX(mat,i-1,j-0,SKIP_QUERY) + 0;  
        if( temp  > score )  {  
          score = temp;  
          }  


        /* Ok - finished max calculation for SKIP_QUERY */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_iset[i];  
         CloneWise_HIDDEN_MATRIX(mat,i,j,SKIP_QUERY) = score;    
        /* Finished calculating state SKIP_QUERY */ 


        /* For state SKIP_TARGET */ 
        /* setting first movement to score */ 
        score = CloneWise_HIDDEN_MATRIX(mat,i-0,j-1,MATCH) + mat->target_skip_start;     
        /* From state SKIP_QUERY to state SKIP_TARGET */ 
        temp = CloneWise_HIDDEN_MATRIX(mat,i-0,j-1,SKIP_QUERY) + mat->target_skip_start;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state SKIP_TARGET */ 
        temp = CloneWise_HIDDEN_MATRIX(mat,i-0,j-1,SKIP_TARGET) + 0;     
        if( temp  > score )  {  
          score = temp;  
          }  


        /* Ok - finished max calculation for SKIP_TARGET */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_jset[j];  
         CloneWise_HIDDEN_MATRIX(mat,i,j,SKIP_TARGET) = score;   
        /* Finished calculating state SKIP_TARGET */ 
        }  
      }  


    return;  
}    


/* Function:  init_hidden_CloneWise(mat,starti,startj,stopi,stopj)
 *
 * Descrip: No Description
 *
 * Arg:           mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:        starti [UNKN ] Undocumented argument [int]
 * Arg:        startj [UNKN ] Undocumented argument [int]
 * Arg:         stopi [UNKN ] Undocumented argument [int]
 * Arg:         stopj [UNKN ] Undocumented argument [int]
 *
 */
void init_hidden_CloneWise(CloneWise * mat,int starti,int startj,int stopi,int stopj) 
{
    register int i;  
    register int j;  
    register int hiddenj;    


    hiddenj = startj;    
    for(j=(startj-1);j<=stopj;j++)   {  
      for(i=(starti-1);i<=stopi;i++) {  
        CloneWise_HIDDEN_MATRIX(mat,i,j,MATCH) = NEGI;
  
        CloneWise_HIDDEN_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;
 
        CloneWise_HIDDEN_MATRIX(mat,i,j,SKIP_TARGET) = NEGI;
    
        }  
      }  


    return;  
}    


/* Function:  full_dc_CloneWise(mat,starti,startj,startstate,stopi,stopj,stopstate,out,donej,totalj,dpenv)
 *
 * Descrip:    The main divide-and-conquor routine. Basically, call /PackAln_calculate_small_CloneWise
 *             Not this function, which is pretty hard core. 
 *             Function is given start/end points (in main matrix) for alignment
 *             It does some checks, decides whether start/end in j is small enough for explicit calc
 *               - if yes, calculates it, reads off into PackAln (out), adds the j distance to donej and returns TRUE
 *               - if no,  uses /do_dc_single_pass_CloneWise to get mid-point
 *                          saves midpoint, and calls itself to do right portion then left portion
 *             right then left ensures PackAln is added the 'right' way, ie, back-to-front
 *             returns FALSE on any error, with a warning
 *
 *
 * Arg:               mat [UNKN ] Matrix with small memory implementation [CloneWise *]
 * Arg:            starti [UNKN ] Start position in i [int]
 * Arg:            startj [UNKN ] Start position in j [int]
 * Arg:        startstate [UNKN ] Start position state number [int]
 * Arg:             stopi [UNKN ] Stop position in i [int]
 * Arg:             stopj [UNKN ] Stop position in j [int]
 * Arg:         stopstate [UNKN ] Stop position state number [int]
 * Arg:               out [UNKN ] PackAln structure to put alignment into [PackAln *]
 * Arg:             donej [UNKN ] pointer to a number with the amount of alignment done [int *]
 * Arg:            totalj [UNKN ] total amount of alignment to do (in j coordinates) [int]
 * Arg:             dpenv [UNKN ] Undocumented argument [DPEnvelope *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean full_dc_CloneWise(CloneWise * mat,int starti,int startj,int startstate,int stopi,int stopj,int stopstate,PackAln * out,int * donej,int totalj,DPEnvelope * dpenv) 
{
    int lstarti; 
    int lstartj; 
    int lstate;  


    if( mat->basematrix->type != BASEMATRIX_TYPE_SHADOW) {  
      warn("*Very* bad error! - non shadow matrix type in full_dc_CloneWise");   
      return FALSE;  
      }  


    if( starti == -1 || startj == -1 || startstate == -1 || stopi == -1 || stopstate == -1)  {  
      warn("In full dc program, passed bad indices, indices passed were %d:%d[%d] to %d:%d[%d]\n",starti,startj,startstate,stopi,stopj,stopstate);   
      return FALSE;  
      }  


    if( stopj - startj < 5)  {  
      log_full_error(REPORT,0,"[%d,%d][%d,%d] Explicit read off",starti,startj,stopi,stopj);/* Build hidden explicit matrix */ 
      calculate_hidden_CloneWise(mat,starti,startj,startstate,stopi,stopj,stopstate,dpenv);  
      *donej += (stopj - startj);   /* Now read it off into out */ 
      if( read_hidden_CloneWise(mat,starti,startj,startstate,stopi,stopj,stopstate,out) == FALSE)    {  
        warn("In full dc, at %d:%d,%d:%d got a bad hidden explicit read off... ",starti,startj,stopi,stopj); 
        return FALSE;    
        }  
      return TRUE;   
      }  


/* In actual divide and conquor */ 
    if( do_dc_single_pass_CloneWise(mat,starti,startj,startstate,stopi,stopj,stopstate,dpenv,(int)(*donej*100)/totalj) == FALSE) {  
      warn("In divide and conquor for CloneWise, at bound %d:%d to %d:%d, unable to calculate midpoint. Problem!",starti,startj,stopi,stopj);    
      return FALSE;  
      }  


/* Ok... now we have to call on each side of the matrix */ 
/* We have to retrieve left hand side positions, as they will be vapped by the time we call LHS */ 
    lstarti= CloneWise_DC_SHADOW_MATRIX_SP(mat,stopi,stopj,stopstate,0);     
    lstartj= CloneWise_DC_SHADOW_MATRIX_SP(mat,stopi,stopj,stopstate,1);     
    lstate = CloneWise_DC_SHADOW_MATRIX_SP(mat,stopi,stopj,stopstate,2);     


/* Call on right hand side: this lets us do the correct read off */ 
    if( full_dc_CloneWise(mat,CloneWise_DC_SHADOW_MATRIX_SP(mat,stopi,stopj,stopstate,3),CloneWise_DC_SHADOW_MATRIX_SP(mat,stopi,stopj,stopstate,4),CloneWise_DC_SHADOW_MATRIX_SP(mat,stopi,stopj,stopstate,5),stopi,stopj,stopstate,out,donej,totalj,dpenv) == FALSE)   {  
/* Warning already issued, simply chained back up to top */ 
      return FALSE;  
      }  
/* Call on left hand side */ 
    if( full_dc_CloneWise(mat,starti,startj,startstate,lstarti,lstartj,lstate,out,donej,totalj,dpenv) == FALSE)  {  
/* Warning already issued, simply chained back up to top */ 
      return FALSE;  
      }  


    return TRUE;     
}    


/* Function:  do_dc_single_pass_CloneWise(mat,starti,startj,startstate,stopi,stopj,stopstate,dpenv,perc_done)
 *
 * Descrip: No Description
 *
 * Arg:               mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:            starti [UNKN ] Undocumented argument [int]
 * Arg:            startj [UNKN ] Undocumented argument [int]
 * Arg:        startstate [UNKN ] Undocumented argument [int]
 * Arg:             stopi [UNKN ] Undocumented argument [int]
 * Arg:             stopj [UNKN ] Undocumented argument [int]
 * Arg:         stopstate [UNKN ] Undocumented argument [int]
 * Arg:             dpenv [UNKN ] Undocumented argument [DPEnvelope *]
 * Arg:         perc_done [UNKN ] Undocumented argument [int]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean do_dc_single_pass_CloneWise(CloneWise * mat,int starti,int startj,int startstate,int stopi,int stopj,int stopstate,DPEnvelope * dpenv,int perc_done) 
{
    int halfj;   
    halfj = startj + ((stopj - startj)/2);   


    init_dc_CloneWise(mat);  


    CloneWise_DC_SHADOW_MATRIX(mat,starti,startj,startstate) = 0;    
    run_up_dc_CloneWise(mat,starti,stopi,startj,halfj-1,dpenv,perc_done);    
    push_dc_at_merge_CloneWise(mat,starti,stopi,halfj,&halfj,dpenv);     
    follow_on_dc_CloneWise(mat,starti,stopi,halfj,stopj,dpenv,perc_done);    
    return TRUE; 
}    


/* Function:  push_dc_at_merge_CloneWise(mat,starti,stopi,startj,stopj,dpenv)
 *
 * Descrip: No Description
 *
 * Arg:           mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:        starti [UNKN ] Undocumented argument [int]
 * Arg:         stopi [UNKN ] Undocumented argument [int]
 * Arg:        startj [UNKN ] Undocumented argument [int]
 * Arg:         stopj [UNKN ] Undocumented argument [int *]
 * Arg:         dpenv [UNKN ] Undocumented argument [DPEnvelope *]
 *
 */
void push_dc_at_merge_CloneWise(CloneWise * mat,int starti,int stopi,int startj,int * stopj,DPEnvelope * dpenv) 
{
    register int i;  
    register int j;  
    register int k;  
    register int count;  
    register int mergej;/* Sources below this j will be stamped by triples */ 
    register int score;  
    register int temp;   


    mergej = startj -1;  
    for(count=0,j=startj;count<1;count++,j++)    {  
      for(i=starti;i<=stopi;i++) {  
        if( dpenv != NULL && is_in_DPEnvelope(dpenv,i,j) == FALSE )  { /*Is not in envelope*/ 
          CloneWise_DC_SHADOW_MATRIX(mat,i,j,MATCH) = NEGI;  
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,0) = (-100);   
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,1) = (-100);   
          CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;     
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,0) = (-100);  
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,1) = (-100);  
          CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_TARGET) = NEGI;    
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,0) = (-100); 
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,1) = (-100); 
          continue;  
          } /* end of Is not in envelope */ 


        /* For state MATCH, pushing when j - offj <= mergej */ 
        score = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-1,MATCH) + (mat->match->matrix[i][j]+1);    
        if( j - 1 <= mergej) {  
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,0) = i-1;  
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,1) = j-1;  
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,2) = MATCH;    
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,3) = i;    
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,4) = j;    
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,5) = MATCH;    
          }  
        else {  
          for(k=0;k<7;k++)   
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,k) = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 1,MATCH,k); 
          }  


        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,MATCH) + 0;    
        if( temp > score)    {  
          score = temp;  


          if( j - 1 <= mergej)   {  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,0) = i-0;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,1) = j-1;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,2) = MATCH;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,3) = i;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,4) = j;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,5) = MATCH;  
            }  
          else   {  
            for(k=0;k<7;k++) 
              CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,k) = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 0,j - 1,MATCH,k);   
            }  
          }  


        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,MATCH) + 0;    
        if( temp > score)    {  
          score = temp;  


          if( j - 0 <= mergej)   {  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,0) = i-1;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,1) = j-0;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,2) = MATCH;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,3) = i;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,4) = j;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,5) = MATCH;  
            }  
          else   {  
            for(k=0;k<7;k++) 
              CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,k) = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 0,MATCH,k);   
            }  
          }  


        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-1,SKIP_QUERY) + 0;   
        if( temp > score)    {  
          score = temp;  


          if( j - 1 <= mergej)   {  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,0) = i-1;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,1) = j-1;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,2) = SKIP_QUERY; 
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,3) = i;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,4) = j;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,5) = MATCH;  
            }  
          else   {  
            for(k=0;k<7;k++) 
              CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,k) = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 1,SKIP_QUERY,k);  
            }  
          }  


        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-1,SKIP_TARGET) + 0;  
        if( temp > score)    {  
          score = temp;  


          if( j - 1 <= mergej)   {  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,0) = i-1;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,1) = j-1;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,2) = SKIP_TARGET;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,3) = i;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,4) = j;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,5) = MATCH;  
            }  
          else   {  
            for(k=0;k<7;k++) 
              CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,k) = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 1,SKIP_TARGET,k); 
            }  
          }  
        /* Add any movement independant score */ 
        score += mat->match->matrix[i][j];   
        CloneWise_DC_SHADOW_MATRIX(mat,i,j,MATCH) = score;   
        /* Finished with state MATCH */ 


        /* For state SKIP_QUERY, pushing when j - offj <= mergej */ 
        score = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,MATCH) + mat->query_skip_start;   
        if( j - 0 <= mergej) {  
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,0) = i-1; 
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,1) = j-0; 
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,2) = MATCH;   
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,3) = i;   
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,4) = j;   
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,5) = SKIP_QUERY;  
          }  
        else {  
          for(k=0;k<7;k++)   
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,k) = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 0,MATCH,k);    
          }  


        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,SKIP_TARGET) + mat->query_skip_start;  
        if( temp > score)    {  
          score = temp;  


          if( j - 0 <= mergej)   {  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,0) = i-1;   
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,1) = j-0;   
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,2) = SKIP_TARGET;   
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,3) = i; 
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,4) = j; 
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,5) = SKIP_QUERY;    
            }  
          else   {  
            for(k=0;k<7;k++) 
              CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,k) = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 0,SKIP_TARGET,k);    
            }  
          }  


        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,SKIP_QUERY) + 0;   
        if( temp > score)    {  
          score = temp;  


          if( j - 0 <= mergej)   {  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,0) = i-1;   
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,1) = j-0;   
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,2) = SKIP_QUERY;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,3) = i; 
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,4) = j; 
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,5) = SKIP_QUERY;    
            }  
          else   {  
            for(k=0;k<7;k++) 
              CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,k) = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 0,SKIP_QUERY,k); 
            }  
          }  
        /* Add any movement independant score */ 
        score += mat->match->skip_iset[i];   
        CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_QUERY) = score;  
        /* Finished with state SKIP_QUERY */ 


        /* For state SKIP_TARGET, pushing when j - offj <= mergej */ 
        score = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,MATCH) + mat->target_skip_start;  
        if( j - 1 <= mergej) {  
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,0) = i-0;    
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,1) = j-1;    
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,2) = MATCH;  
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,3) = i;  
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,4) = j;  
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,5) = SKIP_TARGET;    
          }  
        else {  
          for(k=0;k<7;k++)   
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,k) = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 0,j - 1,MATCH,k);   
          }  


        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,SKIP_QUERY) + mat->target_skip_start;  
        if( temp > score)    {  
          score = temp;  


          if( j - 1 <= mergej)   {  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,0) = i-0;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,1) = j-1;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,2) = SKIP_QUERY;   
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,3) = i;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,4) = j;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,5) = SKIP_TARGET;  
            }  
          else   {  
            for(k=0;k<7;k++) 
              CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,k) = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 0,j - 1,SKIP_QUERY,k);    
            }  
          }  


        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,SKIP_TARGET) + 0;  
        if( temp > score)    {  
          score = temp;  


          if( j - 1 <= mergej)   {  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,0) = i-0;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,1) = j-1;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,2) = SKIP_TARGET;  
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,3) = i;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,4) = j;    
            CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,5) = SKIP_TARGET;  
            }  
          else   {  
            for(k=0;k<7;k++) 
              CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,k) = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 0,j - 1,SKIP_TARGET,k);   
            }  
          }  
        /* Add any movement independant score */ 
        score += mat->match->skip_jset[j];   
        CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_TARGET) = score;     
        /* Finished with state SKIP_TARGET */ 
        }  
      }  
    /* Put back j into * stop j so that calling function gets it correct */ 
    if( stopj == NULL)   
      warn("Bad news... NULL stopj pointer in push dc function. This means that calling function does not know how many cells I have done!");    
    else 
      *stopj = j;    


    return;  
}    


/* Function:  follow_on_dc_CloneWise(mat,starti,stopi,startj,stopj,dpenv,perc_done)
 *
 * Descrip: No Description
 *
 * Arg:              mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:           starti [UNKN ] Undocumented argument [int]
 * Arg:            stopi [UNKN ] Undocumented argument [int]
 * Arg:           startj [UNKN ] Undocumented argument [int]
 * Arg:            stopj [UNKN ] Undocumented argument [int]
 * Arg:            dpenv [UNKN ] Undocumented argument [DPEnvelope *]
 * Arg:        perc_done [UNKN ] Undocumented argument [int]
 *
 */
void follow_on_dc_CloneWise(CloneWise * mat,int starti,int stopi,int startj,int stopj,DPEnvelope * dpenv,int perc_done) 
{
    int i;   
    int j;   
    int k;   
    int score;   
    int temp;    
    int localshadow[7];  
    long int total;  
    long int num;    


    total = (stopi - starti+1) * (stopj - startj+1); 
    num = 0;     


    for(j=startj;j<=stopj;j++)   { /*for each valid j column*/ 
      for(i=starti;i<=stopi;i++) { /*this is strip*/ 
        num++;   
        if( dpenv != NULL && is_in_DPEnvelope(dpenv,i,j) == FALSE )  { /*Is not in envelope*/ 
          CloneWise_DC_SHADOW_MATRIX(mat,i,j,MATCH) = NEGI;  
          CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;     
          CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_TARGET) = NEGI;    
          continue;  
          } /* end of Is not in envelope */ 
        if( num % 1000 == 0 )    
          log_full_error(REPORT,0,"[%d%%%% done]After  mid-j %5d Cells done %d%%%%",perc_done,startj,(num*100)/total);   


        /* For state MATCH */ 
        /* setting first movement to score */ 
        score = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-1,MATCH) + (mat->match->matrix[i][j]+1);    
        /* shift first shadow numbers */ 
        for(k=0;k<7;k++) 
          localshadow[k] = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 1,MATCH,k);   
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,MATCH) + 0;    
        if( temp  > score )  {  
          score = temp;  
          for(k=0;k<7;k++)   
            localshadow[k] = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 0,j - 1,MATCH,k); 
          }  
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,MATCH) + 0;    
        if( temp  > score )  {  
          score = temp;  
          for(k=0;k<7;k++)   
            localshadow[k] = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 0,MATCH,k); 
          }  
        /* From state SKIP_QUERY to state MATCH */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-1,SKIP_QUERY) + 0;   
        if( temp  > score )  {  
          score = temp;  
          for(k=0;k<7;k++)   
            localshadow[k] = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 1,SKIP_QUERY,k);    
          }  
        /* From state SKIP_TARGET to state MATCH */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-1,SKIP_TARGET) + 0;  
        if( temp  > score )  {  
          score = temp;  
          for(k=0;k<7;k++)   
            localshadow[k] = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 1,SKIP_TARGET,k);   
          }  


        /* Ok - finished max calculation for MATCH */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->matrix[i][j];  
         CloneWise_DC_SHADOW_MATRIX(mat,i,j,MATCH) = score;  
        for(k=0;k<7;k++) 
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,k) = localshadow[k];   
        /* Now figure out if any specials need this score */ 
        /* Finished calculating state MATCH */ 


        /* For state SKIP_QUERY */ 
        /* setting first movement to score */ 
        score = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,MATCH) + mat->query_skip_start;   
        /* shift first shadow numbers */ 
        for(k=0;k<7;k++) 
          localshadow[k] = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 0,MATCH,k);   
        /* From state SKIP_TARGET to state SKIP_QUERY */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,SKIP_TARGET) + mat->query_skip_start;  
        if( temp  > score )  {  
          score = temp;  
          for(k=0;k<7;k++)   
            localshadow[k] = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 0,SKIP_TARGET,k);   
          }  
        /* From state SKIP_QUERY to state SKIP_QUERY */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,SKIP_QUERY) + 0;   
        if( temp  > score )  {  
          score = temp;  
          for(k=0;k<7;k++)   
            localshadow[k] = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 1,j - 0,SKIP_QUERY,k);    
          }  


        /* Ok - finished max calculation for SKIP_QUERY */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_iset[i];  
         CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_QUERY) = score; 
        for(k=0;k<7;k++) 
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,k) = localshadow[k];  
        /* Now figure out if any specials need this score */ 
        /* Finished calculating state SKIP_QUERY */ 


        /* For state SKIP_TARGET */ 
        /* setting first movement to score */ 
        score = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,MATCH) + mat->target_skip_start;  
        /* shift first shadow numbers */ 
        for(k=0;k<7;k++) 
          localshadow[k] = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 0,j - 1,MATCH,k);   
        /* From state SKIP_QUERY to state SKIP_TARGET */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,SKIP_QUERY) + mat->target_skip_start;  
        if( temp  > score )  {  
          score = temp;  
          for(k=0;k<7;k++)   
            localshadow[k] = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 0,j - 1,SKIP_QUERY,k);    
          }  
        /* From state SKIP_TARGET to state SKIP_TARGET */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,SKIP_TARGET) + 0;  
        if( temp  > score )  {  
          score = temp;  
          for(k=0;k<7;k++)   
            localshadow[k] = CloneWise_DC_SHADOW_MATRIX_SP(mat,i - 0,j - 1,SKIP_TARGET,k);   
          }  


        /* Ok - finished max calculation for SKIP_TARGET */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_jset[j];  
         CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_TARGET) = score;    
        for(k=0;k<7;k++) 
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,k) = localshadow[k]; 
        /* Now figure out if any specials need this score */ 
        /* Finished calculating state SKIP_TARGET */ 
        } /* end of this is strip */ 
      } /* end of for each valid j column */ 


/* Function:  run_up_dc_CloneWise(mat,starti,stopi,startj,stopj,dpenv,perc_done)
 *
 * Descrip: No Description
 *
 * Arg:              mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:           starti [UNKN ] Undocumented argument [int]
 * Arg:            stopi [UNKN ] Undocumented argument [int]
 * Arg:           startj [UNKN ] Undocumented argument [int]
 * Arg:            stopj [UNKN ] Undocumented argument [int]
 * Arg:            dpenv [UNKN ] Undocumented argument [DPEnvelope *]
 * Arg:        perc_done [UNKN ] Undocumented argument [int]
 *
 */
}    
void run_up_dc_CloneWise(CloneWise * mat,int starti,int stopi,int startj,int stopj,DPEnvelope * dpenv,int perc_done) 
{
    register int i;  
    register int j;  
    register int score;  
    register int temp;   
    long int total;  
    long int num;    


    total = (stopi - starti+1) * (stopj - startj+1); 
    if( total <= 0 ) 
      total = 1; 
    num = 0;     


    for(j=startj;j<=stopj;j++)   { /*for each valid j column*/ 
      for(i=starti;i<=stopi;i++) { /*this is strip*/ 
        if( j == startj && i == starti)  
          continue;  
        num++;   
        if( dpenv != NULL && is_in_DPEnvelope(dpenv,i,j) == FALSE )  { /*Is not in envelope*/ 
          CloneWise_DC_SHADOW_MATRIX(mat,i,j,MATCH) = NEGI;  
          CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;     
          CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_TARGET) = NEGI;    
          continue;  
          } /* end of Is not in envelope */ 
        if( num % 1000 == 0 )    
          log_full_error(REPORT,0,"[%d%%%% done]Before mid-j %5d Cells done %d%%%%",perc_done,stopj,(num*100)/total);    


        /* For state MATCH */ 
        /* setting first movement to score */ 
        score = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-1,MATCH) + (mat->match->matrix[i][j]+1);    
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,MATCH) + 0;    
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,MATCH) + 0;    
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state MATCH */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-1,SKIP_QUERY) + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state MATCH */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-1,SKIP_TARGET) + 0;  
        if( temp  > score )  {  
          score = temp;  
          }  


        /* Ok - finished max calculation for MATCH */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->matrix[i][j];  
         CloneWise_DC_SHADOW_MATRIX(mat,i,j,MATCH) = score;  
        /* Finished calculating state MATCH */ 


        /* For state SKIP_QUERY */ 
        /* setting first movement to score */ 
        score = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,MATCH) + mat->query_skip_start;   
        /* From state SKIP_TARGET to state SKIP_QUERY */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,SKIP_TARGET) + mat->query_skip_start;  
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state SKIP_QUERY */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-1,j-0,SKIP_QUERY) + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  


        /* Ok - finished max calculation for SKIP_QUERY */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_iset[i];  
         CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_QUERY) = score; 
        /* Finished calculating state SKIP_QUERY */ 


        /* For state SKIP_TARGET */ 
        /* setting first movement to score */ 
        score = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,MATCH) + mat->target_skip_start;  
        /* From state SKIP_QUERY to state SKIP_TARGET */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,SKIP_QUERY) + mat->target_skip_start;  
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state SKIP_TARGET */ 
        temp = CloneWise_DC_SHADOW_MATRIX(mat,i-0,j-1,SKIP_TARGET) + 0;  
        if( temp  > score )  {  
          score = temp;  
          }  


        /* Ok - finished max calculation for SKIP_TARGET */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_jset[j];  
         CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_TARGET) = score;    
        /* Finished calculating state SKIP_TARGET */ 
        } /* end of this is strip */ 
      } /* end of for each valid j column */ 


/* Function:  init_dc_CloneWise(mat)
 *
 * Descrip: No Description
 *
 * Arg:        mat [UNKN ] Undocumented argument [CloneWise *]
 *
 */
}    
void init_dc_CloneWise(CloneWise * mat) 
{
    register int i;  
    register int j;  
    register int k;  


    for(j=0;j<3;j++) {  
      for(i=(-1);i<mat->q->length;i++)   {  
        CloneWise_DC_SHADOW_MATRIX(mat,i,j,MATCH) = NEGI;    
        CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;   
        CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_TARGET) = NEGI;  
        for(k=0;k<7;k++) {  
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,k) = (-1); 
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,k) = (-1);    
          CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,k) = (-1);   
          }  
        }  
      }  


    return;  
}    


/* Function:  start_end_find_end_CloneWise(mat,endj)
 *
 * Descrip:    First function used to find end of the best path in the special state !end
 *
 *
 * Arg:         mat [UNKN ] Matrix in small mode [CloneWise *]
 * Arg:        endj [WRITE] position of end in j (meaningless in i) [int *]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int start_end_find_end_CloneWise(CloneWise * mat,int * endj) 
{
    register int j;  
    register int max;    
    register int maxj;   


    max = CloneWise_DC_SHADOW_SPECIAL(mat,0,mat->t->length-1,END);   
    maxj = mat->t->length-1;     
    for(j= mat->t->length-2 ;j >= 0 ;j--)    {  
      if( CloneWise_DC_SHADOW_SPECIAL(mat,0,j,END) > max )   {  
        max = CloneWise_DC_SHADOW_SPECIAL(mat,0,j,END);  
        maxj = j;    
        }  
      }  


    if( endj != NULL)    
      *endj = maxj;  


    return max;  
}    


/* Function:  dc_optimised_start_end_calc_CloneWise(*mat,dpenv)
 *
 * Descrip:    Calculates special strip, leaving start/end/score points in shadow matrix
 *             Works off specially laid out memory from steve searle
 *
 *
 * Arg:         *mat [UNKN ] Undocumented argument [CloneWise]
 * Arg:        dpenv [UNKN ] Undocumented argument [DPEnvelope *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean dc_optimised_start_end_calc_CloneWise(CloneWise *mat,DPEnvelope * dpenv) 
{
    int i;   
    int j;   
    int k;   
    int score;   
    int temp;    
    int leni;    
    int lenj;    
    int localshadow[7];  
    long int total;  
    long int num=0;  
    int * score_pointers;    
    int * shadow_pointers;   
    int * localsp;   
    leni = mat->q->length;   
    lenj = mat->t->length;   
    total = leni * lenj; 


    score_pointers = (int *) calloc (1 * (leni + 1) * 3,sizeof(int));    
    shadow_pointers = (int *) calloc (1 * (leni + 1) * 3 * 8,sizeof(int));   


    for(j=0;j<lenj;j++)  { /*for each j strip*/ 
      for(i=0;i<leni;i++)    { /*for each i position in strip*/ 
        num++;   
        if( dpenv != NULL && is_in_DPEnvelope(dpenv,i,j) == FALSE )  { /*Is not in envelope*/ 
          CloneWise_DC_OPT_SHADOW_MATRIX(mat,i,j,MATCH) = NEGI;  
          CloneWise_DC_OPT_SHADOW_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;     
          CloneWise_DC_OPT_SHADOW_MATRIX(mat,i,j,SKIP_TARGET) = NEGI;    
          continue;  
          } /* end of Is not in envelope */ 
        if( num%1000 == 0)   
          log_full_error(REPORT,0,"%6d Cells done [%2d%%%%]",num,num*100/total); 




        /* For state MATCH */ 
        /* setting first movement to score */ 
        score = CloneWise_DC_OPT_SHADOW_MATRIX(mat,i-1,j-1,MATCH) + (mat->match->matrix[i][j]+1) + (mat->match->matrix[i][j]);   
        /* assign local shadown pointer */ 
        localsp = &(CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i - 1,j - 1,MATCH,0)); 
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_DC_OPT_SHADOW_MATRIX(mat,i-0,j-1,MATCH) + 0 +(mat->match->matrix[i][j]);    
        if( temp  > score )  {  
          score = temp;  
          /* assign local shadown pointer */ 
          localsp = &(CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i - 0,j - 1,MATCH,0));   
          }  
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_DC_OPT_SHADOW_MATRIX(mat,i-1,j-0,MATCH) + 0 +(mat->match->matrix[i][j]);    
        if( temp  > score )  {  
          score = temp;  
          /* assign local shadown pointer */ 
          localsp = &(CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i - 1,j - 0,MATCH,0));   
          }  
        /* From state SKIP_QUERY to state MATCH */ 
        temp = CloneWise_DC_OPT_SHADOW_MATRIX(mat,i-1,j-1,SKIP_QUERY) + 0 +(mat->match->matrix[i][j]);   
        if( temp  > score )  {  
          score = temp;  
          /* assign local shadown pointer */ 
          localsp = &(CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i - 1,j - 1,SKIP_QUERY,0));  
          }  
        /* From state SKIP_TARGET to state MATCH */ 
        temp = CloneWise_DC_OPT_SHADOW_MATRIX(mat,i-1,j-1,SKIP_TARGET) + 0 +(mat->match->matrix[i][j]);  
        if( temp  > score )  {  
          score = temp;  
          /* assign local shadown pointer */ 
          localsp = &(CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i - 1,j - 1,SKIP_TARGET,0)); 
          }  
        /* From state START to state MATCH */ 
        temp = CloneWise_DC_OPT_SHADOW_SPECIAL(mat,i-1,j-1,START) + 0 + (mat->match->matrix[i][j]);  
        if( temp  > score )  {  
          score = temp;  
          /* This state [START] is a special for MATCH... push top shadow pointers here */ 
          localshadow[0]= i; 
          localshadow[1]= j; 
          localshadow[2]= MATCH; 
          localshadow[3]= (-1);  
          localshadow[4]= (-1);  
          localshadow[5]= (-1);  
          localshadow[6]= score; 
          localsp = localshadow; 
          }  


        /* Ok - finished max calculation for MATCH */ 
        /* Add any movement independant score and put away */ 
        /* Actually, already done inside scores */ 
         CloneWise_DC_OPT_SHADOW_MATRIX(mat,i,j,MATCH) = score;  
        for(k=0;k<7;k++) 
          CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i,j,MATCH,k) = localsp[k];   
        /* Now figure out if any specials need this score */ 


        /* state MATCH is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_DC_OPT_SHADOW_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )    {  
          CloneWise_DC_OPT_SHADOW_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;   
          /* Have to push only bottem half of system here */ 
          for(k=0;k<3;k++)   
            CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,k) = CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i,j,MATCH,k);  
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,6) = CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i,j,MATCH,6);    
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,3) = i; 
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,4) = j; 
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,5) = MATCH; 
          }  




        /* Finished calculating state MATCH */ 


        /* For state SKIP_QUERY */ 
        /* setting first movement to score */ 
        score = CloneWise_DC_OPT_SHADOW_MATRIX(mat,i-1,j-0,MATCH) + mat->query_skip_start + (mat->match->skip_iset[i]);  
        /* assign local shadown pointer */ 
        localsp = &(CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i - 1,j - 0,MATCH,0)); 
        /* From state SKIP_TARGET to state SKIP_QUERY */ 
        temp = CloneWise_DC_OPT_SHADOW_MATRIX(mat,i-1,j-0,SKIP_TARGET) + mat->query_skip_start +(mat->match->skip_iset[i]);  
        if( temp  > score )  {  
          score = temp;  
          /* assign local shadown pointer */ 
          localsp = &(CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i - 1,j - 0,SKIP_TARGET,0)); 
          }  
        /* From state SKIP_QUERY to state SKIP_QUERY */ 
        temp = CloneWise_DC_OPT_SHADOW_MATRIX(mat,i-1,j-0,SKIP_QUERY) + 0 +(mat->match->skip_iset[i]);   
        if( temp  > score )  {  
          score = temp;  
          /* assign local shadown pointer */ 
          localsp = &(CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i - 1,j - 0,SKIP_QUERY,0));  
          }  
        /* From state START to state SKIP_QUERY */ 
        temp = CloneWise_DC_OPT_SHADOW_SPECIAL(mat,i-1,j-0,START) + mat->query_skip_start + (mat->match->skip_iset[i]);  
        if( temp  > score )  {  
          score = temp;  
          /* This state [START] is a special for SKIP_QUERY... push top shadow pointers here */ 
          localshadow[0]= i; 
          localshadow[1]= j; 
          localshadow[2]= SKIP_QUERY;    
          localshadow[3]= (-1);  
          localshadow[4]= (-1);  
          localshadow[5]= (-1);  
          localshadow[6]= score; 
          localsp = localshadow; 
          }  


        /* Ok - finished max calculation for SKIP_QUERY */ 
        /* Add any movement independant score and put away */ 
        /* Actually, already done inside scores */ 
         CloneWise_DC_OPT_SHADOW_MATRIX(mat,i,j,SKIP_QUERY) = score; 
        for(k=0;k<7;k++) 
          CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,k) = localsp[k];  
        /* Now figure out if any specials need this score */ 


        /* state SKIP_QUERY is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_DC_OPT_SHADOW_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )    {  
          CloneWise_DC_OPT_SHADOW_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;   
          /* Have to push only bottem half of system here */ 
          for(k=0;k<3;k++)   
            CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,k) = CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,k); 
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,6) = CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,6);   
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,3) = i; 
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,4) = j; 
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,5) = SKIP_QUERY;    
          }  




        /* Finished calculating state SKIP_QUERY */ 


        /* For state SKIP_TARGET */ 
        /* setting first movement to score */ 
        score = CloneWise_DC_OPT_SHADOW_MATRIX(mat,i-0,j-1,MATCH) + mat->target_skip_start + (mat->match->skip_jset[j]);     
        /* assign local shadown pointer */ 
        localsp = &(CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i - 0,j - 1,MATCH,0)); 
        /* From state SKIP_QUERY to state SKIP_TARGET */ 
        temp = CloneWise_DC_OPT_SHADOW_MATRIX(mat,i-0,j-1,SKIP_QUERY) + mat->target_skip_start +(mat->match->skip_jset[j]);  
        if( temp  > score )  {  
          score = temp;  
          /* assign local shadown pointer */ 
          localsp = &(CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i - 0,j - 1,SKIP_QUERY,0));  
          }  
        /* From state SKIP_TARGET to state SKIP_TARGET */ 
        temp = CloneWise_DC_OPT_SHADOW_MATRIX(mat,i-0,j-1,SKIP_TARGET) + 0 +(mat->match->skip_jset[j]);  
        if( temp  > score )  {  
          score = temp;  
          /* assign local shadown pointer */ 
          localsp = &(CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i - 0,j - 1,SKIP_TARGET,0)); 
          }  
        /* From state START to state SKIP_TARGET */ 
        temp = CloneWise_DC_OPT_SHADOW_SPECIAL(mat,i-0,j-1,START) + mat->target_skip_start + (mat->match->skip_jset[j]);     
        if( temp  > score )  {  
          score = temp;  
          /* This state [START] is a special for SKIP_TARGET... push top shadow pointers here */ 
          localshadow[0]= i; 
          localshadow[1]= j; 
          localshadow[2]= SKIP_TARGET;   
          localshadow[3]= (-1);  
          localshadow[4]= (-1);  
          localshadow[5]= (-1);  
          localshadow[6]= score; 
          localsp = localshadow; 
          }  


        /* Ok - finished max calculation for SKIP_TARGET */ 
        /* Add any movement independant score and put away */ 
        /* Actually, already done inside scores */ 
         CloneWise_DC_OPT_SHADOW_MATRIX(mat,i,j,SKIP_TARGET) = score;    
        for(k=0;k<7;k++) 
          CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,k) = localsp[k]; 
        /* Now figure out if any specials need this score */ 


        /* state SKIP_TARGET is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_DC_OPT_SHADOW_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )    {  
          CloneWise_DC_OPT_SHADOW_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;   
          /* Have to push only bottem half of system here */ 
          for(k=0;k<3;k++)   
            CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,k) = CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,k);    
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,6) = CloneWise_DC_OPT_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,6);  
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,3) = i; 
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,4) = j; 
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,5) = SKIP_TARGET;   
          }  




        /* Finished calculating state SKIP_TARGET */ 


        } /* end of for each i position in strip */ 


      /* Special state START has no special to special movements */ 


      /* Special state TRUSTED_SPECIAL has special to speical */ 
      /* Set score to current score (remember, state probably updated during main loop */ 
      score = CloneWise_DC_OPT_SHADOW_SPECIAL(mat,0,j,TRUSTED_SPECIAL);  


      /* Source START is a special source for TRUSTED_SPECIAL */ 
      temp = CloneWise_DC_OPT_SHADOW_SPECIAL(mat,0,j - 1,START) + (0) + (0);     
      if( temp > score ) {  
        score = temp;    
        /* Also got to propagate shadows  */ 
        for(k=0;k<7;k++) 
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,k) = CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i - 0,j - 1,START,k);   
        }  


      /* Source TRUSTED_SPECIAL is a special source for TRUSTED_SPECIAL */ 
      temp = CloneWise_DC_OPT_SHADOW_SPECIAL(mat,0,j - 1,TRUSTED_SPECIAL) + ((mat->match->skip_jset[j]-1)) + (0);    
      if( temp > score ) {  
        score = temp;    
        /* Also got to propagate shadows  */ 
        for(k=0;k<7;k++) 
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,TRUSTED_SPECIAL,k) = CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i - 0,j - 1,TRUSTED_SPECIAL,k); 
        }  


      /* Source MATCH for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Source SKIP_QUERY for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Source SKIP_TARGET for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Put back score... (now updated!) */ 
      CloneWise_DC_OPT_SHADOW_SPECIAL(mat,0,j,TRUSTED_SPECIAL) = score;  
      /* Finished updating state TRUSTED_SPECIAL */ 




      /* Special state END has special to speical */ 
      /* Set score to current score (remember, state probably updated during main loop */ 
      score = CloneWise_DC_OPT_SHADOW_SPECIAL(mat,0,j,END);  


      /* Source TRUSTED_SPECIAL is a special source for END */ 
      temp = CloneWise_DC_OPT_SHADOW_SPECIAL(mat,0,j - 1,TRUSTED_SPECIAL) + (0) + (0);   
      if( temp > score ) {  
        score = temp;    
        /* Also got to propagate shadows  */ 
        for(k=0;k<7;k++) 
          CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i,j,END,k) = CloneWise_DC_OPT_SHADOW_SPECIAL_SP(mat,i - 0,j - 1,TRUSTED_SPECIAL,k); 
        }  


      /* Put back score... (now updated!) */ 
      CloneWise_DC_OPT_SHADOW_SPECIAL(mat,0,j,END) = score;  
      /* Finished updating state END */ 


      } /* end of for each j strip */ 
    free(score_pointers);    
    free(shadow_pointers);   
    return TRUE;     
}    


/* Function:  init_start_end_linear_CloneWise(mat)
 *
 * Descrip: No Description
 *
 * Arg:        mat [UNKN ] Undocumented argument [CloneWise *]
 *
 */
void init_start_end_linear_CloneWise(CloneWise * mat) 
{
    register int i;  
    register int j;  
    for(j=0;j<3;j++) {  
      for(i=(-1);i<mat->q->length;i++)   {  
        CloneWise_DC_SHADOW_MATRIX(mat,i,j,MATCH) = NEGI;    
        CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,MATCH,0) = (-1);   
        CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;   
        CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_QUERY,0) = (-1);  
        CloneWise_DC_SHADOW_MATRIX(mat,i,j,SKIP_TARGET) = NEGI;  
        CloneWise_DC_SHADOW_MATRIX_SP(mat,i,j,SKIP_TARGET,0) = (-1); 
        }  
      }  


    for(j=(-1);j<mat->t->length;j++) {  
      CloneWise_DC_SHADOW_SPECIAL(mat,0,j,START) = 0;    
      CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,j,START,0) = j;   
      CloneWise_DC_SHADOW_SPECIAL(mat,0,j,TRUSTED_SPECIAL) = NEGI;   
      CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,j,TRUSTED_SPECIAL,0) = (-1);  
      CloneWise_DC_SHADOW_SPECIAL(mat,0,j,END) = NEGI;   
      CloneWise_DC_SHADOW_SPECIAL_SP(mat,0,j,END,0) = (-1);  
      }  


    return;  
}    


/* Function:  convert_PackAln_to_AlnBlock_CloneWise(pal)
 *
 * Descrip:    Converts a path alignment to a label alignment
 *             The label alignment is probably much more useful than the path
 *
 *
 * Arg:        pal [UNKN ] Undocumented argument [PackAln *]
 *
 * Return [UNKN ]  Undocumented return value [AlnBlock *]
 *
 */
AlnBlock * convert_PackAln_to_AlnBlock_CloneWise(PackAln * pal) 
{
    AlnConvertSet * acs; 
    AlnBlock * alb;  


    acs = AlnConvertSet_CloneWise(); 
    alb = AlnBlock_from_PackAln(acs,pal);    
    free_AlnConvertSet(acs); 
    return alb;  
}    


 static char * query_label[] = { "QUERY_MATCH","QUERY_MATCH_PAUSE","QUERY_SKIP","QUERY_PAUSE","NO_QUERY_PAUSE","END" };  
/* Function:  AlnConvertSet_CloneWise(void)
 *
 * Descrip: No Description
 *
 *
 * Return [UNKN ]  Undocumented return value [AlnConvertSet *]
 *
 */
 static char * target_label[] = { "TARGET_MATCH","TARGET_MATCH_PAUSE","TARGET_PAUSE","TARGET_SKIP","END" };  
AlnConvertSet * AlnConvertSet_CloneWise(void) 
{
    AlnConvertUnit * acu;    
    AlnConvertSet  * out;    


    out = AlnConvertSet_alloc_std(); 


    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = MATCH; 
    acu->state2 = MATCH;     
    acu->offi = 1;   
    acu->offj = 1;   
    acu->label1 = query_label[0];    
    acu->label2 = target_label[0];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = MATCH; 
    acu->state2 = MATCH;     
    acu->offi = 0;   
    acu->offj = 1;   
    acu->label1 = query_label[1];    
    acu->label2 = target_label[0];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = MATCH; 
    acu->state2 = MATCH;     
    acu->offi = 1;   
    acu->offj = 0;   
    acu->label1 = query_label[0];    
    acu->label2 = target_label[1];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = SKIP_QUERY;    
    acu->state2 = MATCH;     
    acu->offi = 1;   
    acu->offj = 1;   
    acu->label1 = query_label[0];    
    acu->label2 = target_label[0];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = SKIP_TARGET;   
    acu->state2 = MATCH;     
    acu->offi = 1;   
    acu->offj = 1;   
    acu->label1 = query_label[0];    
    acu->label2 = target_label[0];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = START + 3; 
    acu->is_from_special = TRUE; 
    acu->state2 = MATCH;     
    acu->offi = (-1);    
    acu->offj = 1;   
    acu->label1 = query_label[0];    
    acu->label2 = target_label[0];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = MATCH; 
    acu->state2 = SKIP_QUERY;    
    acu->offi = 1;   
    acu->offj = 0;   
    acu->label1 = query_label[2];    
    acu->label2 = target_label[2];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = SKIP_TARGET;   
    acu->state2 = SKIP_QUERY;    
    acu->offi = 1;   
    acu->offj = 0;   
    acu->label1 = query_label[2];    
    acu->label2 = target_label[2];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = SKIP_QUERY;    
    acu->state2 = SKIP_QUERY;    
    acu->offi = 1;   
    acu->offj = 0;   
    acu->label1 = query_label[2];    
    acu->label2 = target_label[2];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = START + 3; 
    acu->is_from_special = TRUE; 
    acu->state2 = SKIP_QUERY;    
    acu->offi = (-1);    
    acu->offj = 0;   
    acu->label1 = query_label[2];    
    acu->label2 = target_label[2];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = MATCH; 
    acu->state2 = SKIP_TARGET;   
    acu->offi = 0;   
    acu->offj = 1;   
    acu->label1 = query_label[3];    
    acu->label2 = target_label[3];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = SKIP_QUERY;    
    acu->state2 = SKIP_TARGET;   
    acu->offi = 0;   
    acu->offj = 1;   
    acu->label1 = query_label[3];    
    acu->label2 = target_label[3];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = SKIP_TARGET;   
    acu->state2 = SKIP_TARGET;   
    acu->offi = 0;   
    acu->offj = 1;   
    acu->label1 = query_label[3];    
    acu->label2 = target_label[3];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = START + 3; 
    acu->is_from_special = TRUE; 
    acu->state2 = SKIP_TARGET;   
    acu->offi = (-1);    
    acu->offj = 1;   
    acu->label1 = query_label[3];    
    acu->label2 = target_label[3];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = START + 3; 
    acu->state2 = TRUSTED_SPECIAL + 3;   
    acu->offi = (-1);    
    acu->offj = 1;   
    acu->label1 = query_label[4];    
    acu->label2 = target_label[3];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = TRUSTED_SPECIAL + 3;   
    acu->state2 = TRUSTED_SPECIAL + 3;   
    acu->offi = (-1);    
    acu->offj = 1;   
    acu->label1 = query_label[4];    
    acu->label2 = target_label[3];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = MATCH; 
    acu->state2 = TRUSTED_SPECIAL + 3;   
    acu->offi = (-1);    
    acu->offj = 0;   
    acu->label1 = query_label[4];    
    acu->label2 = target_label[3];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = SKIP_QUERY;    
    acu->state2 = TRUSTED_SPECIAL + 3;   
    acu->offi = (-1);    
    acu->offj = 0;   
    acu->label1 = query_label[4];    
    acu->label2 = target_label[3];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = SKIP_TARGET;   
    acu->state2 = TRUSTED_SPECIAL + 3;   
    acu->offi = (-1);    
    acu->offj = 0;   
    acu->label1 = query_label[4];    
    acu->label2 = target_label[3];   
    acu = AlnConvertUnit_alloc();    
    add_AlnConvertSet(out,acu);  
    acu->state1 = TRUSTED_SPECIAL + 3;   
    acu->state2 = END + 3;   
    acu->offi = (-1);    
    acu->offj = 1;   
    acu->label1 = query_label[5];    
    acu->label2 = target_label[4];   
    return out;  
}    


/* Function:  PackAln_read_Expl_CloneWise(mat)
 *
 * Descrip:    Reads off PackAln from explicit matrix structure
 *
 *
 * Arg:        mat [UNKN ] Undocumented argument [CloneWise *]
 *
 * Return [UNKN ]  Undocumented return value [PackAln *]
 *
 */
PackAln * PackAln_read_Expl_CloneWise(CloneWise * mat) 
{
    CloneWise_access_func_holder holder;     


    holder.access_main    = CloneWise_explicit_access_main;  
    holder.access_special = CloneWise_explicit_access_special;   
    return PackAln_read_generic_CloneWise(mat,holder);   
}    


/* Function:  CloneWise_explicit_access_main(mat,i,j,state)
 *
 * Descrip: No Description
 *
 * Arg:          mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:            i [UNKN ] Undocumented argument [int]
 * Arg:            j [UNKN ] Undocumented argument [int]
 * Arg:        state [UNKN ] Undocumented argument [int]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int CloneWise_explicit_access_main(CloneWise * mat,int i,int j,int state) 
{
    return CloneWise_EXPL_MATRIX(mat,i,j,state); 
}    


/* Function:  CloneWise_explicit_access_special(mat,i,j,state)
 *
 * Descrip: No Description
 *
 * Arg:          mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:            i [UNKN ] Undocumented argument [int]
 * Arg:            j [UNKN ] Undocumented argument [int]
 * Arg:        state [UNKN ] Undocumented argument [int]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int CloneWise_explicit_access_special(CloneWise * mat,int i,int j,int state) 
{
    return CloneWise_EXPL_SPECIAL(mat,i,j,state);    
}    


/* Function:  PackAln_read_generic_CloneWise(mat,h)
 *
 * Descrip:    Reads off PackAln from explicit matrix structure
 *
 *
 * Arg:        mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:          h [UNKN ] Undocumented argument [CloneWise_access_func_holder]
 *
 * Return [UNKN ]  Undocumented return value [PackAln *]
 *
 */
PackAln * PackAln_read_generic_CloneWise(CloneWise * mat,CloneWise_access_func_holder h) 
{
    register PackAln * out;  
    int i;   
    int j;   
    int state;   
    int cellscore = (-1);    
    boolean isspecial;   
    PackAlnUnit * pau = NULL;    
    PackAlnUnit * prev = NULL;   


    assert(mat);     
    assert(h.access_main);   
    assert(h.access_special);    


    out = PackAln_alloc_std();   
    if( out == NULL )    
      return NULL;   


    out->score =  find_end_CloneWise(mat,&i,&j,&state,&isspecial,h); 


    /* Add final end transition (at the moment we have not got the score! */ 
    if( (pau= PackAlnUnit_alloc()) == NULL  || add_PackAln(out,pau) == FALSE )   {  
      warn("Failed the first PackAlnUnit alloc, %d length of Alignment in CloneWise_basic_read, returning a mess.(Sorry!)",out->len);    
      return out;    
      }  


    /* Put in positions for end trans. Remember that coordinates in C style */ 
    pau->i = i;  
    pau->j = j;  
    if( isspecial != TRUE)   
      pau->state = state;    
    else pau->state = state + 3;     
    prev=pau;    
    while( state != START || isspecial != TRUE)  { /*while state != START*/ 


      if( isspecial == TRUE )    
        max_calc_special_CloneWise(mat,i,j,state,isspecial,&i,&j,&state,&isspecial,&cellscore,h);    
      else   
        max_calc_CloneWise(mat,i,j,state,isspecial,&i,&j,&state,&isspecial,&cellscore,h);    
      if(i == CloneWise_READ_OFF_ERROR || j == CloneWise_READ_OFF_ERROR || state == CloneWise_READ_OFF_ERROR )   {  
        warn("Problem - hit bad read off system, exiting now");  
        break;   
        }  
      if( (pau= PackAlnUnit_alloc()) == NULL  || add_PackAln(out,pau) == FALSE ) {  
        warn("Failed a PackAlnUnit alloc, %d length of Alignment in CloneWise_basic_read, returning partial alignment",out->len);    
        break;   
        }  


      /* Put in positions for block. Remember that coordinates in C style */ 
      pau->i = i;    
      pau->j = j;    
      if( isspecial != TRUE)     
        pau->state = state;  
      else pau->state = state + 3;   
      prev->score = cellscore;   
      prev = pau;    
      } /* end of while state != START */ 


    invert_PackAln(out); 
    return out;  
}    


/* Function:  find_end_CloneWise(mat,ri,rj,state,isspecial,h)
 *
 * Descrip: No Description
 *
 * Arg:              mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:               ri [UNKN ] Undocumented argument [int *]
 * Arg:               rj [UNKN ] Undocumented argument [int *]
 * Arg:            state [UNKN ] Undocumented argument [int *]
 * Arg:        isspecial [UNKN ] Undocumented argument [boolean *]
 * Arg:                h [UNKN ] Undocumented argument [CloneWise_access_func_holder]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int find_end_CloneWise(CloneWise * mat,int * ri,int * rj,int * state,boolean * isspecial,CloneWise_access_func_holder h) 
{
    int j;   
    int max; 
    int maxj;    
    int temp;    


    max = (*h.access_special)(mat,0,mat->t->length-1,END);   
    maxj = mat->t->length-1;     
    for(j= mat->t->length-2 ;j >= 0 ;j--)    {  
      if( (temp =(*h.access_special)(mat,0,j,END)) > max )   {  
        max = temp;  
        maxj = j;    
        }  
      }  


    if( ri != NULL)  
       *ri = 0;  
    if( rj != NULL)  
       *rj = maxj;   
    if( state != NULL)   
       *state = END; 
    if( isspecial != NULL)   
       *isspecial = TRUE;    


    return max;  
}    


/* Function:  CloneWise_debug_show_matrix(mat,starti,stopi,startj,stopj,ofp)
 *
 * Descrip: No Description
 *
 * Arg:           mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:        starti [UNKN ] Undocumented argument [int]
 * Arg:         stopi [UNKN ] Undocumented argument [int]
 * Arg:        startj [UNKN ] Undocumented argument [int]
 * Arg:         stopj [UNKN ] Undocumented argument [int]
 * Arg:           ofp [UNKN ] Undocumented argument [FILE *]
 *
 */
void CloneWise_debug_show_matrix(CloneWise * mat,int starti,int stopi,int startj,int stopj,FILE * ofp) 
{
    register int i;  
    register int j;  


    for(i=starti;i<stopi && i < mat->q->length;i++)  {  
      for(j=startj;j<stopj && j < mat->t->length;j++)    {  
        fprintf(ofp,"Cell [%d - %d]\n",i,j);     
        fprintf(ofp,"State MATCH %d\n",CloneWise_EXPL_MATRIX(mat,i,j,MATCH));    
        fprintf(ofp,"State SKIP_QUERY %d\n",CloneWise_EXPL_MATRIX(mat,i,j,SKIP_QUERY));  
        fprintf(ofp,"State SKIP_TARGET %d\n",CloneWise_EXPL_MATRIX(mat,i,j,SKIP_TARGET));    
        fprintf(ofp,"\n\n"); 
        }  
      }  


}    


/* Function:  max_calc_CloneWise(mat,i,j,state,isspecial,reti,retj,retstate,retspecial,cellscore,h)
 *
 * Descrip: No Description
 *
 * Arg:               mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:                 i [UNKN ] Undocumented argument [int]
 * Arg:                 j [UNKN ] Undocumented argument [int]
 * Arg:             state [UNKN ] Undocumented argument [int]
 * Arg:         isspecial [UNKN ] Undocumented argument [boolean]
 * Arg:              reti [UNKN ] Undocumented argument [int *]
 * Arg:              retj [UNKN ] Undocumented argument [int *]
 * Arg:          retstate [UNKN ] Undocumented argument [int *]
 * Arg:        retspecial [UNKN ] Undocumented argument [boolean *]
 * Arg:         cellscore [UNKN ] Undocumented argument [int *]
 * Arg:                 h [UNKN ] Undocumented argument [CloneWise_access_func_holder]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int max_calc_CloneWise(CloneWise * mat,int i,int j,int state,boolean isspecial,int * reti,int * retj,int * retstate,boolean * retspecial,int * cellscore,CloneWise_access_func_holder h) 
{
    register int temp;   
    register int cscore; 


    *reti = (*retj) = (*retstate) = CloneWise_READ_OFF_ERROR;    


    if( i < 0 || j < 0 || i > mat->q->length || j > mat->t->length)  {  
      warn("In CloneWise matrix special read off - out of bounds on matrix [i,j is %d,%d state %d in standard matrix]",i,j,state);   
      return -1;     
      }  


    /* Then you have to select the correct switch statement to figure out the readoff      */ 
    /* Somewhat odd - reverse the order of calculation and return as soon as it is correct */ 
    cscore = (*h.access_main)(mat,i,j,state);    
    switch(state)    { /*Switch state */ 
      case MATCH :   
        /* Has restricted position */ 
        if( (i-1) == 0 && (j-1) == 0 )   {  
          temp = cscore - (0) -  (mat->match->matrix[i][j]); 
          if( temp == (*h.access_special)(mat,i - 1,j - 1,START) )   {  
            *reti = i - 1;   
            *retj = j - 1;   
            *retstate = START;   
            *retspecial = TRUE;  
            if( cellscore != NULL)   {  
              *cellscore = cscore - (*h.access_special)(mat,i-1,j-1,START);  
              }  
            return (*h.access_main)(mat,i - 1,j - 1,START);  
            }  
          }  
        temp = cscore - (0) -  (mat->match->matrix[i][j]);   
        if( temp == (*h.access_main)(mat,i - 1,j - 1,SKIP_TARGET) )  {  
          *reti = i - 1; 
          *retj = j - 1; 
          *retstate = SKIP_TARGET;   
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_main)(mat,i-1,j-1,SKIP_TARGET); 
            }  
          return (*h.access_main)(mat,i - 1,j - 1,SKIP_TARGET);  
          }  
        temp = cscore - (0) -  (mat->match->matrix[i][j]);   
        if( temp == (*h.access_main)(mat,i - 1,j - 1,SKIP_QUERY) )   {  
          *reti = i - 1; 
          *retj = j - 1; 
          *retstate = SKIP_QUERY;    
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_main)(mat,i-1,j-1,SKIP_QUERY);  
            }  
          return (*h.access_main)(mat,i - 1,j - 1,SKIP_QUERY);   
          }  
        temp = cscore - (0) -  (mat->match->matrix[i][j]);   
        if( temp == (*h.access_main)(mat,i - 1,j - 0,MATCH) )    {  
          *reti = i - 1; 
          *retj = j - 0; 
          *retstate = MATCH; 
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_main)(mat,i-1,j-0,MATCH);   
            }  
          return (*h.access_main)(mat,i - 1,j - 0,MATCH);    
          }  
        temp = cscore - (0) -  (mat->match->matrix[i][j]);   
        if( temp == (*h.access_main)(mat,i - 0,j - 1,MATCH) )    {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = MATCH; 
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_main)(mat,i-0,j-1,MATCH);   
            }  
          return (*h.access_main)(mat,i - 0,j - 1,MATCH);    
          }  
        temp = cscore - ((mat->match->matrix[i][j]+1)) -  (mat->match->matrix[i][j]);    
        if( temp == (*h.access_main)(mat,i - 1,j - 1,MATCH) )    {  
          *reti = i - 1; 
          *retj = j - 1; 
          *retstate = MATCH; 
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_main)(mat,i-1,j-1,MATCH);   
            }  
          return (*h.access_main)(mat,i - 1,j - 1,MATCH);    
          }  
        warn("Major problem (!) - in CloneWise read off, position %d,%d state %d no source found!",i,j,state);   
        return (-1); 
      case SKIP_QUERY :  
        /* Has restricted position */ 
        if( (i-1) == 0 && (j-0) == 0 )   {  
          temp = cscore - (mat->query_skip_start) -  (mat->match->skip_iset[i]); 
          if( temp == (*h.access_special)(mat,i - 1,j - 0,START) )   {  
            *reti = i - 1;   
            *retj = j - 0;   
            *retstate = START;   
            *retspecial = TRUE;  
            if( cellscore != NULL)   {  
              *cellscore = cscore - (*h.access_special)(mat,i-1,j-0,START);  
              }  
            return (*h.access_main)(mat,i - 1,j - 0,START);  
            }  
          }  
        temp = cscore - (0) -  (mat->match->skip_iset[i]);   
        if( temp == (*h.access_main)(mat,i - 1,j - 0,SKIP_QUERY) )   {  
          *reti = i - 1; 
          *retj = j - 0; 
          *retstate = SKIP_QUERY;    
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_main)(mat,i-1,j-0,SKIP_QUERY);  
            }  
          return (*h.access_main)(mat,i - 1,j - 0,SKIP_QUERY);   
          }  
        temp = cscore - (mat->query_skip_start) -  (mat->match->skip_iset[i]);   
        if( temp == (*h.access_main)(mat,i - 1,j - 0,SKIP_TARGET) )  {  
          *reti = i - 1; 
          *retj = j - 0; 
          *retstate = SKIP_TARGET;   
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_main)(mat,i-1,j-0,SKIP_TARGET); 
            }  
          return (*h.access_main)(mat,i - 1,j - 0,SKIP_TARGET);  
          }  
        temp = cscore - (mat->query_skip_start) -  (mat->match->skip_iset[i]);   
        if( temp == (*h.access_main)(mat,i - 1,j - 0,MATCH) )    {  
          *reti = i - 1; 
          *retj = j - 0; 
          *retstate = MATCH; 
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_main)(mat,i-1,j-0,MATCH);   
            }  
          return (*h.access_main)(mat,i - 1,j - 0,MATCH);    
          }  
        warn("Major problem (!) - in CloneWise read off, position %d,%d state %d no source found!",i,j,state);   
        return (-1); 
      case SKIP_TARGET :     
        /* Has restricted position */ 
        if( (i-0) == 0 && (j-1) == 0 )   {  
          temp = cscore - (mat->target_skip_start) -  (mat->match->skip_jset[j]);    
          if( temp == (*h.access_special)(mat,i - 0,j - 1,START) )   {  
            *reti = i - 0;   
            *retj = j - 1;   
            *retstate = START;   
            *retspecial = TRUE;  
            if( cellscore != NULL)   {  
              *cellscore = cscore - (*h.access_special)(mat,i-0,j-1,START);  
              }  
            return (*h.access_main)(mat,i - 0,j - 1,START);  
            }  
          }  
        temp = cscore - (0) -  (mat->match->skip_jset[j]);   
        if( temp == (*h.access_main)(mat,i - 0,j - 1,SKIP_TARGET) )  {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = SKIP_TARGET;   
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_main)(mat,i-0,j-1,SKIP_TARGET); 
            }  
          return (*h.access_main)(mat,i - 0,j - 1,SKIP_TARGET);  
          }  
        temp = cscore - (mat->target_skip_start) -  (mat->match->skip_jset[j]);  
        if( temp == (*h.access_main)(mat,i - 0,j - 1,SKIP_QUERY) )   {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = SKIP_QUERY;    
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_main)(mat,i-0,j-1,SKIP_QUERY);  
            }  
          return (*h.access_main)(mat,i - 0,j - 1,SKIP_QUERY);   
          }  
        temp = cscore - (mat->target_skip_start) -  (mat->match->skip_jset[j]);  
        if( temp == (*h.access_main)(mat,i - 0,j - 1,MATCH) )    {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = MATCH; 
          *retspecial = FALSE;   
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_main)(mat,i-0,j-1,MATCH);   
            }  
          return (*h.access_main)(mat,i - 0,j - 1,MATCH);    
          }  
        warn("Major problem (!) - in CloneWise read off, position %d,%d state %d no source found!",i,j,state);   
        return (-1); 
      default:   
        warn("Major problem (!) - in CloneWise read off, position %d,%d state %d no source found!",i,j,state);   
        return (-1); 
      } /* end of Switch state  */ 
}    


/* Function:  max_calc_special_CloneWise(mat,i,j,state,isspecial,reti,retj,retstate,retspecial,cellscore,h)
 *
 * Descrip: No Description
 *
 * Arg:               mat [UNKN ] Undocumented argument [CloneWise *]
 * Arg:                 i [UNKN ] Undocumented argument [int]
 * Arg:                 j [UNKN ] Undocumented argument [int]
 * Arg:             state [UNKN ] Undocumented argument [int]
 * Arg:         isspecial [UNKN ] Undocumented argument [boolean]
 * Arg:              reti [UNKN ] Undocumented argument [int *]
 * Arg:              retj [UNKN ] Undocumented argument [int *]
 * Arg:          retstate [UNKN ] Undocumented argument [int *]
 * Arg:        retspecial [UNKN ] Undocumented argument [boolean *]
 * Arg:         cellscore [UNKN ] Undocumented argument [int *]
 * Arg:                 h [UNKN ] Undocumented argument [CloneWise_access_func_holder]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int max_calc_special_CloneWise(CloneWise * mat,int i,int j,int state,boolean isspecial,int * reti,int * retj,int * retstate,boolean * retspecial,int * cellscore,CloneWise_access_func_holder h) 
{
    register int temp;   
    register int cscore; 


    *reti = (*retj) = (*retstate) = CloneWise_READ_OFF_ERROR;    


    if( j < 0 || j > mat->t->length) {  
      warn("In CloneWise matrix special read off - out of bounds on matrix [j is %d in special]",j); 
      return -1;     
      }  


    cscore = (*h.access_special)(mat,i,j,state); 
    switch(state)    { /*switch on special states*/ 
      case START :   
      case TRUSTED_SPECIAL :     
        /* source SKIP_TARGET is from main matrix */ 
        for(i= mat->q->length-1;i >= 0 ;i--) { /*for i >= 0*/ 
          temp = cscore - (mat->target_special_s) - (0);     
          if( temp == (*h.access_main)(mat,i - 0,j - 0,SKIP_TARGET) )    {  
            *reti = i - 0;   
            *retj = j - 0;   
            *retstate = SKIP_TARGET; 
            *retspecial = FALSE; 
            if( cellscore != NULL)   {  
              *cellscore = cscore - (*h.access_main)(mat,i-0,j-0,SKIP_TARGET);   
              }  
            return (*h.access_main)(mat,i - 0,j - 0,SKIP_TARGET) ;   
            }  
          } /* end of for i >= 0 */ 
        /* source SKIP_QUERY is from main matrix */ 
        for(i= mat->q->length-1;i >= 0 ;i--) { /*for i >= 0*/ 
          temp = cscore - (mat->target_special_s) - (0);     
          if( temp == (*h.access_main)(mat,i - 0,j - 0,SKIP_QUERY) ) {  
            *reti = i - 0;   
            *retj = j - 0;   
            *retstate = SKIP_QUERY;  
            *retspecial = FALSE; 
            if( cellscore != NULL)   {  
              *cellscore = cscore - (*h.access_main)(mat,i-0,j-0,SKIP_QUERY);    
              }  
            return (*h.access_main)(mat,i - 0,j - 0,SKIP_QUERY) ;    
            }  
          } /* end of for i >= 0 */ 
        /* source MATCH is from main matrix */ 
        for(i= mat->q->length-1;i >= 0 ;i--) { /*for i >= 0*/ 
          temp = cscore - (mat->target_special_s) - (0);     
          if( temp == (*h.access_main)(mat,i - 0,j - 0,MATCH) )  {  
            *reti = i - 0;   
            *retj = j - 0;   
            *retstate = MATCH;   
            *retspecial = FALSE; 
            if( cellscore != NULL)   {  
              *cellscore = cscore - (*h.access_main)(mat,i-0,j-0,MATCH);     
              }  
            return (*h.access_main)(mat,i - 0,j - 0,MATCH) ;     
            }  
          } /* end of for i >= 0 */ 
        /* source TRUSTED_SPECIAL is a special */ 
        temp = cscore - ((mat->match->skip_jset[j]-1)) - (0);    
        if( temp == (*h.access_special)(mat,i - 0,j - 1,TRUSTED_SPECIAL) )   {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = TRUSTED_SPECIAL;   
          *retspecial = TRUE;    
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_special)(mat,i-0,j-1,TRUSTED_SPECIAL);  
            }  
          return (*h.access_special)(mat,i - 0,j - 1,TRUSTED_SPECIAL) ;  
          }  
        /* source START is a special */ 
        temp = cscore - (0) - (0);   
        if( temp == (*h.access_special)(mat,i - 0,j - 1,START) ) {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = START; 
          *retspecial = TRUE;    
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_special)(mat,i-0,j-1,START);    
            }  
          return (*h.access_special)(mat,i - 0,j - 1,START) ;    
          }  
      case END :     
        /* source TRUSTED_SPECIAL is a special */ 
        temp = cscore - (0) - (0);   
        if( temp == (*h.access_special)(mat,i - 0,j - 1,TRUSTED_SPECIAL) )   {  
          *reti = i - 0; 
          *retj = j - 1; 
          *retstate = TRUSTED_SPECIAL;   
          *retspecial = TRUE;    
          if( cellscore != NULL) {  
            *cellscore = cscore - (*h.access_special)(mat,i-0,j-1,TRUSTED_SPECIAL);  
            }  
          return (*h.access_special)(mat,i - 0,j - 1,TRUSTED_SPECIAL) ;  
          }  
      default:   
        warn("Major problem (!) - in CloneWise read off, position %d,%d state %d no source found  dropped into default on source switch!",i,j,state);    
        return (-1); 
      } /* end of switch on special states */ 
}    


/* Function:  calculate_CloneWise(mat)
 *
 * Descrip:    This function calculates the CloneWise matrix when in explicit mode
 *             To allocate the matrix use /allocate_Expl_CloneWise
 *
 *
 * Arg:        mat [UNKN ] CloneWise which contains explicit basematrix memory [CloneWise *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean calculate_CloneWise(CloneWise * mat) 
{
    int i;   
    int j;   
    int leni;    
    int lenj;    
    int tot; 
    int num; 


    if( mat->basematrix->type != BASEMATRIX_TYPE_EXPLICIT )  {  
      warn("in calculate_CloneWise, passed a non Explicit matrix type, cannot calculate!");  
      return FALSE;  
      }  


    leni = mat->leni;    
    lenj = mat->lenj;    
    tot = leni * lenj;   
    num = 0; 


    start_reporting("CloneWise Matrix calculation: ");   
    for(j=0;j<lenj;j++)  {  
      auto int score;    
      auto int temp;     
      for(i=0;i<leni;i++)    {  
        if( num%1000 == 0 )  
          log_full_error(REPORT,0,"[%7d] Cells %2d%%%%",num,num*100/tot);    
        num++;   


        /* For state MATCH */ 
        /* setting first movement to score */ 
        score = CloneWise_EXPL_MATRIX(mat,i-1,j-1,MATCH) + (mat->match->matrix[i][j]+1);     
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-0,j-1,MATCH) + 0;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-1,j-0,MATCH) + 0;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state MATCH */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-1,j-1,SKIP_QUERY) + 0;    
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state MATCH */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-1,j-1,SKIP_TARGET) + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-1) == 0 && (j-1) == 0 )   {  
          /* From state START to state MATCH */ 
          temp = CloneWise_EXPL_SPECIAL(mat,i-1,j-1,START) + 0;  
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for MATCH */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->matrix[i][j];  
         CloneWise_EXPL_MATRIX(mat,i,j,MATCH) = score;   


        /* state MATCH is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )     {  
          CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;    
          }  




        /* Finished calculating state MATCH */ 


        /* For state SKIP_QUERY */ 
        /* setting first movement to score */ 
        score = CloneWise_EXPL_MATRIX(mat,i-1,j-0,MATCH) + mat->query_skip_start;    
        /* From state SKIP_TARGET to state SKIP_QUERY */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-1,j-0,SKIP_TARGET) + mat->query_skip_start;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state SKIP_QUERY */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-1,j-0,SKIP_QUERY) + 0;    
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-1) == 0 && (j-0) == 0 )   {  
          /* From state START to state SKIP_QUERY */ 
          temp = CloneWise_EXPL_SPECIAL(mat,i-1,j-0,START) + mat->query_skip_start;  
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for SKIP_QUERY */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_iset[i];  
         CloneWise_EXPL_MATRIX(mat,i,j,SKIP_QUERY) = score;  


        /* state SKIP_QUERY is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )     {  
          CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;    
          }  




        /* Finished calculating state SKIP_QUERY */ 


        /* For state SKIP_TARGET */ 
        /* setting first movement to score */ 
        score = CloneWise_EXPL_MATRIX(mat,i-0,j-1,MATCH) + mat->target_skip_start;   
        /* From state SKIP_QUERY to state SKIP_TARGET */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-0,j-1,SKIP_QUERY) + mat->target_skip_start;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state SKIP_TARGET */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-0,j-1,SKIP_TARGET) + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-0) == 0 && (j-1) == 0 )   {  
          /* From state START to state SKIP_TARGET */ 
          temp = CloneWise_EXPL_SPECIAL(mat,i-0,j-1,START) + mat->target_skip_start;     
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for SKIP_TARGET */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_jset[j];  
         CloneWise_EXPL_MATRIX(mat,i,j,SKIP_TARGET) = score; 


        /* state SKIP_TARGET is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )     {  
          CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;    
          }  




        /* Finished calculating state SKIP_TARGET */ 
        }  


      /* Special state START has no special to special movements */ 


      /* Special state TRUSTED_SPECIAL has special to speical */ 
      /* Set score to current score (remember, state probably updated during main loop */ 
      score = CloneWise_EXPL_SPECIAL(mat,0,j,TRUSTED_SPECIAL);   


      /* Source START is a special source for TRUSTED_SPECIAL */ 
      /* Has restricted position */ 
      if( (j-1) == 0  )  {  
        temp = CloneWise_EXPL_SPECIAL(mat,0,j - 1,START) + (0) + (0);    
        if( temp > score )   
          score = temp;  
        }  


      /* Source TRUSTED_SPECIAL is a special source for TRUSTED_SPECIAL */ 
      temp = CloneWise_EXPL_SPECIAL(mat,0,j - 1,TRUSTED_SPECIAL) + ((mat->match->skip_jset[j]-1)) + (0);     
      if( temp > score ) 
        score = temp;    


      /* Source MATCH for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Source SKIP_QUERY for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Source SKIP_TARGET for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Put back score... (now updated!) */ 
      CloneWise_EXPL_SPECIAL(mat,0,j,TRUSTED_SPECIAL) = score;   
      /* Finished updating state TRUSTED_SPECIAL */ 




      /* Special state END has special to speical */ 
      /* Set score to current score (remember, state probably updated during main loop */ 
      score = CloneWise_EXPL_SPECIAL(mat,0,j,END);   


      /* Source TRUSTED_SPECIAL is a special source for END */ 
      /* Has restricted position */ 
      if( j == mat->lenj-1 ) {  
        temp = CloneWise_EXPL_SPECIAL(mat,0,j - 1,TRUSTED_SPECIAL) + (0) + (0);  
        if( temp > score )   
          score = temp;  
        }  


      /* Put back score... (now updated!) */ 
      CloneWise_EXPL_SPECIAL(mat,0,j,END) = score;   
      /* Finished updating state END */ 


      }  
    stop_reporting();    
    return TRUE;     
}    


/* Function:  calculate_dpenv_CloneWise(mat,dpenv)
 *
 * Descrip:    This function calculates the CloneWise matrix when in explicit mode, subject to the envelope
 *
 *
 * Arg:          mat [UNKN ] CloneWise which contains explicit basematrix memory [CloneWise *]
 * Arg:        dpenv [UNKN ] Undocumented argument [DPEnvelope *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean calculate_dpenv_CloneWise(CloneWise * mat,DPEnvelope * dpenv) 
{
    int i;   
    int j;   
    int k;   
    int starti;  
    int startj;  
    int endi;    
    int endj;    
    int tot; 
    int num; 
    int should_calc; 


    if( mat->basematrix->type != BASEMATRIX_TYPE_EXPLICIT )  {  
      warn("in calculate_CloneWise, passed a non Explicit matrix type, cannot calculate!");  
      return FALSE;  
      }  


    prepare_DPEnvelope(dpenv);   
    starti = dpenv->starti;  
    if( starti < 0 ) 
      starti = 0;    
    startj = dpenv->startj;  
    if( startj < 0 ) 
      startj = 0;    
    endi = dpenv->endi;  
    if( endi > mat->leni )   
      endi = mat->leni;  
    endj = dpenv->endj;  
    if( endj > mat->lenj )   
      endj = mat->lenj;  
    tot = (endi-starti) * (endj-startj); 
    num = 0; 


    for(j=startj-1;j<endj;j++)   {  
      for(i=1;i<mat->leni;i++)   {  
        CloneWise_EXPL_MATRIX(mat,i,j,MATCH) = NEGI; 
        CloneWise_EXPL_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;    
        CloneWise_EXPL_MATRIX(mat,i,j,SKIP_TARGET) = NEGI;   
        }  
      }  
    for(j=-1;j<mat->lenj;j++)    {  
      CloneWise_EXPL_SPECIAL(mat,i,j,START) = 0; 
      CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = NEGI;    
      CloneWise_EXPL_SPECIAL(mat,i,j,END) = NEGI;    
      }  


    start_reporting("CloneWise Matrix calculation: ");   
    for(j=startj;j<endj;j++) {  
      auto int score;    
      auto int temp;     
      for(i=starti;i<endi;i++)   {  
        /* Check if is in envelope - code identical to is_in_DPEnvelope, but aggressively inlined here for speed */ 
        should_calc = 0; 
        for(k=0;k<dpenv->len;k++)    {  
          auto DPUnit * u;   
          u = dpenv->dpu[k]; 
          switch(u->type)    {  
            case DPENV_RECT :    
              if( i >= u->starti && j >= u->startj && i <= (u->starti+u->height) && j <= (u->startj+u->length))  
                should_calc = 1;     
              break; 
            case DPENV_DIAG :    
              if(  abs( (i-j) - (u->starti-u->startj)) <= u->height && i+j >= u->starti+u->startj && i+j+u->length >= u->starti+u->startj)   
                should_calc = 1;     
              break; 
            }  
          if( should_calc == 1 ) 
            break;   
          }  
        if( should_calc == 0)    {  
          CloneWise_EXPL_MATRIX(mat,i,j,MATCH) = NEGI;   
          CloneWise_EXPL_MATRIX(mat,i,j,SKIP_QUERY) = NEGI;  
          CloneWise_EXPL_MATRIX(mat,i,j,SKIP_TARGET) = NEGI; 
          continue;  
          }  


        if( num%1000 == 0 )  
          log_full_error(REPORT,0,"[%7d] Cells %2d%%%%",num,num*100/tot);    
        num++;   


        /* For state MATCH */ 
        /* setting first movement to score */ 
        score = CloneWise_EXPL_MATRIX(mat,i-1,j-1,MATCH) + (mat->match->matrix[i][j]+1);     
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-0,j-1,MATCH) + 0;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state MATCH to state MATCH */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-1,j-0,MATCH) + 0;     
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state MATCH */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-1,j-1,SKIP_QUERY) + 0;    
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state MATCH */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-1,j-1,SKIP_TARGET) + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-1) == 0 && (j-1) == 0 )   {  
          /* From state START to state MATCH */ 
          temp = CloneWise_EXPL_SPECIAL(mat,i-1,j-1,START) + 0;  
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for MATCH */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->matrix[i][j];  
         CloneWise_EXPL_MATRIX(mat,i,j,MATCH) = score;   


        /* state MATCH is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )     {  
          CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;    
          }  




        /* Finished calculating state MATCH */ 


        /* For state SKIP_QUERY */ 
        /* setting first movement to score */ 
        score = CloneWise_EXPL_MATRIX(mat,i-1,j-0,MATCH) + mat->query_skip_start;    
        /* From state SKIP_TARGET to state SKIP_QUERY */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-1,j-0,SKIP_TARGET) + mat->query_skip_start;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_QUERY to state SKIP_QUERY */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-1,j-0,SKIP_QUERY) + 0;    
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-1) == 0 && (j-0) == 0 )   {  
          /* From state START to state SKIP_QUERY */ 
          temp = CloneWise_EXPL_SPECIAL(mat,i-1,j-0,START) + mat->query_skip_start;  
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for SKIP_QUERY */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_iset[i];  
         CloneWise_EXPL_MATRIX(mat,i,j,SKIP_QUERY) = score;  


        /* state SKIP_QUERY is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )     {  
          CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;    
          }  




        /* Finished calculating state SKIP_QUERY */ 


        /* For state SKIP_TARGET */ 
        /* setting first movement to score */ 
        score = CloneWise_EXPL_MATRIX(mat,i-0,j-1,MATCH) + mat->target_skip_start;   
        /* From state SKIP_QUERY to state SKIP_TARGET */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-0,j-1,SKIP_QUERY) + mat->target_skip_start;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* From state SKIP_TARGET to state SKIP_TARGET */ 
        temp = CloneWise_EXPL_MATRIX(mat,i-0,j-1,SKIP_TARGET) + 0;   
        if( temp  > score )  {  
          score = temp;  
          }  
        /* Has restricted position */ 
        if( (i-0) == 0 && (j-1) == 0 )   {  
          /* From state START to state SKIP_TARGET */ 
          temp = CloneWise_EXPL_SPECIAL(mat,i-0,j-1,START) + mat->target_skip_start;     
          if( temp  > score )    {  
            score = temp;    
            }  
          }  


        /* Ok - finished max calculation for SKIP_TARGET */ 
        /* Add any movement independant score and put away */ 
         score += mat->match->skip_jset[j];  
         CloneWise_EXPL_MATRIX(mat,i,j,SKIP_TARGET) = score; 


        /* state SKIP_TARGET is a source for special TRUSTED_SPECIAL */ 
        temp = score + (mat->target_special_s) + (0) ;   
        if( temp > CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) )     {  
          CloneWise_EXPL_SPECIAL(mat,i,j,TRUSTED_SPECIAL) = temp;    
          }  




        /* Finished calculating state SKIP_TARGET */ 
        }  


      /* Special state START has no special to special movements */ 


      /* Special state TRUSTED_SPECIAL has special to speical */ 
      /* Set score to current score (remember, state probably updated during main loop */ 
      score = CloneWise_EXPL_SPECIAL(mat,0,j,TRUSTED_SPECIAL);   


      /* Source START is a special source for TRUSTED_SPECIAL */ 
      /* Has restricted position */ 
      if( (j-1) == 0  )  {  
        temp = CloneWise_EXPL_SPECIAL(mat,0,j - 1,START) + (0) + (0);    
        if( temp > score )   
          score = temp;  
        }  


      /* Source TRUSTED_SPECIAL is a special source for TRUSTED_SPECIAL */ 
      temp = CloneWise_EXPL_SPECIAL(mat,0,j - 1,TRUSTED_SPECIAL) + ((mat->match->skip_jset[j]-1)) + (0);     
      if( temp > score ) 
        score = temp;    


      /* Source MATCH for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Source SKIP_QUERY for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Source SKIP_TARGET for state TRUSTED_SPECIAL is not special... already calculated */ 
      /* Put back score... (now updated!) */ 
      CloneWise_EXPL_SPECIAL(mat,0,j,TRUSTED_SPECIAL) = score;   
      /* Finished updating state TRUSTED_SPECIAL */ 




      /* Special state END has special to speical */ 
      /* Set score to current score (remember, state probably updated during main loop */ 
      score = CloneWise_EXPL_SPECIAL(mat,0,j,END);   


      /* Source TRUSTED_SPECIAL is a special source for END */ 
      /* Has restricted position */ 
      if( j == mat->lenj-1 ) {  
        temp = CloneWise_EXPL_SPECIAL(mat,0,j - 1,TRUSTED_SPECIAL) + (0) + (0);  
        if( temp > score )   
          score = temp;  
        }  


      /* Put back score... (now updated!) */ 
      CloneWise_EXPL_SPECIAL(mat,0,j,END) = score;   
      /* Finished updating state END */ 


      }  
    stop_reporting();    
    return TRUE;     
}    


/* Function:  CloneWise_alloc(void)
 *
 * Descrip:    Allocates structure: assigns defaults if given 
 *
 *
 *
 * Return [UNKN ]  Undocumented return value [CloneWise *]
 *
 */
CloneWise * CloneWise_alloc(void) 
{
    CloneWise * out;/* out is exported at end of function */ 


    /* call ckalloc and see if NULL */ 
    if((out=(CloneWise *) ckalloc (sizeof(CloneWise))) == NULL)  {  
      warn("CloneWise_alloc failed ");   
      return NULL;  /* calling function should respond! */ 
      }  
    out->dynamite_hard_link = 1; 
#ifdef PTHREAD   
    pthread_mutex_init(&(out->dynamite_mutex),NULL);     
#endif   
    out->basematrix = NULL;  
    out->shatter = NULL; 
    out->leni = 0;   
    out->lenj = 0;   


    return out;  
}    


/* Function:  free_CloneWise(obj)
 *
 * Descrip:    Free Function: removes the memory held by obj
 *             Will chain up to owned members and clear all lists
 *
 *
 * Arg:        obj [UNKN ] Object that is free'd [CloneWise *]
 *
 * Return [UNKN ]  Undocumented return value [CloneWise *]
 *
 */
CloneWise * free_CloneWise(CloneWise * obj) 
{
    int return_early = 0;    


    if( obj == NULL) {  
      warn("Attempting to free a NULL pointer to a CloneWise obj. Should be trappable"); 
      return NULL;   
      }  


#ifdef PTHREAD   
    assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0); 
#endif   
    if( obj->dynamite_hard_link > 1)     {  
      return_early = 1;  
      obj->dynamite_hard_link--; 
      }  
#ifdef PTHREAD   
    assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);   
#endif   
    if( return_early == 1)   
      return NULL;   
    if( obj->basematrix != NULL) 
      free_BaseMatrix(obj->basematrix);  
    if( obj->shatter != NULL)    
      free_ShatterMatrix(obj->shatter);  
    /* obj->q is linked in */ 
    /* obj->t is linked in */ 
    /* obj->match is linked in */ 
    /* obj->target_skip_start is linked in */ 
    /* obj->target_skip is linked in */ 
    /* obj->query_skip_start is linked in */ 
    /* obj->query_skip is linked in */ 
    /* obj->spread is linked in */ 
    /* obj->target_special_s is linked in */ 


    ckfree(obj); 
    return NULL; 
}    





#ifdef _cplusplus
}
#endif