summaryrefslogtreecommitdiff
path: root/api/jzcoll.c
blob: cd7ede9977766bca93c96fcf514d90db9edd96ab (plain)
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
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
/* $Id: jzcoll.c,v 6.18 2006/07/13 17:06:38 bollin Exp $
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*               National Center for Biotechnology Information
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government have not placed any restriction on its use or reproduction.
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
*  Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* File Name:  $RCSfile: jzcoll.c,v $
*
* Author:  Jinghui Zhang
*
* Initial Version Creation Date: 03/24/97
*
* $Revision: 6.18 $
*
* File Description:
*         File for various alignments
*
* $Log: jzcoll.c,v $
* Revision 6.18  2006/07/13 17:06:38  bollin
* use Uint4 instead of Uint2 for itemID values
* removed unused variables
* resolved compiler warnings
*
* Revision 6.17  2001/06/26 16:42:58  vakatov
* POINT --> BAND_POINT  (to avoid conflicts with MS-Win standard headers)
*
* Revision 6.16  2000/11/16 22:10:37  shavirin
* Moved many functions from txalign.c - due to move of txalign.c to
* distrib/tools directory and libncbitool.a library.
*
*
* ==========================================================================
*/

#include <jzcoll.h>
#include <txalign.h>
#include <codon.h>

static Char pchars[] = "ARNDCQEGHILKMFPSTWYVBZX";	/* amino acid names */
static Int4 webb_blosum62[WEBB_asize][WEBB_asize] = {
   { 4,-1,-2,-2, 0,-1,-1, 0,-2,-1,-1,-1,-1,-2,-1, 1, 0,-3,-2, 0,-2,-1, 0 },
   {-1, 5, 0,-2,-3, 1, 0,-2, 0,-3,-2, 2,-1,-3,-2,-1,-1,-3,-2,-3,-1, 0,-1 },
   {-2, 0, 6, 1,-3, 0, 0, 0, 1,-3,-3, 0,-2,-3,-2, 1, 0,-4,-2,-3, 3, 0,-1 },
   {-2,-2, 1, 6,-3, 0, 2,-1,-1,-3,-4,-1,-3,-3,-1, 0,-1,-4,-3,-3, 4, 1,-1 },
   { 0,-3,-3,-3, 9,-3,-4,-3,-3,-1,-1,-3,-1,-2,-3,-1,-1,-2,-2,-1,-3,-3,-2 },
   {-1, 1, 0, 0,-3, 5, 2,-2, 0,-3,-2, 1, 0,-3,-1, 0,-1,-2,-1,-2, 0, 3,-1 },
   {-1, 0, 0, 2,-4, 2, 5,-2, 0,-3,-3, 1,-2,-3,-1, 0,-1,-3,-2,-2, 1, 4,-1 },
   { 0,-2, 0,-1,-3,-2,-2, 6,-2,-4,-4,-2,-3,-3,-2, 0,-2,-2,-3,-3,-1,-2,-1 },
   {-2, 0, 1,-1,-3, 0, 0,-2, 8,-3,-3,-1,-2,-1,-2,-1,-2,-2, 2,-3, 0, 0,-1 },
   {-1,-3,-3,-3,-1,-3,-3,-4,-3, 4, 2,-3, 1, 0,-3,-2,-1,-3,-1, 3,-3,-3,-1 },
   {-1,-2,-3,-4,-1,-2,-3,-4,-3, 2, 4,-2, 2, 0,-3,-2,-1,-2,-1, 1,-4,-3,-1 },
   {-1, 2, 0,-1,-3, 1, 1,-2,-1,-3,-2, 5,-1,-3,-1, 0,-1,-3,-2,-2, 0, 1,-1 },
   {-1,-1,-2,-3,-1, 0,-2,-3,-2, 1, 2,-1, 5, 0,-2,-1,-1,-1,-1, 1,-3,-1,-1 },
   {-2,-3,-3,-3,-2,-3,-3,-3,-1, 0, 0,-3, 0, 6,-4,-2,-2, 1, 3,-1,-3,-3,-1 },
   {-1,-2,-2,-1,-3,-1,-1,-2,-2,-3,-3,-1,-2,-4, 7,-1,-1,-4,-3,-2,-2,-1,-2 },
   { 1,-1, 1, 0,-1, 0, 0, 0,-1,-2,-2, 0,-1,-2,-1, 4, 1,-3,-2,-2, 0, 0, 0 },
   { 0,-1, 0,-1,-1,-1,-1,-2,-2,-1,-1,-1,-1,-2,-1, 1, 5,-2,-2, 0,-1,-1, 0 },
   {-3,-3,-4,-4,-2,-2,-3,-2,-2,-3,-2,-3,-1, 1,-4,-3,-2,11, 2,-3,-4,-3,-2 },
   {-2,-2,-2,-3,-2,-1,-2,-3, 2,-1,-1,-2,-1, 3,-3,-2,-2, 2, 7,-1,-3,-2,-1 },
   { 0,-3,-3,-3,-1,-2,-2,-3,-3, 3, 1,-2, 1,-1,-2,-2, 0,-3,-1, 4,-3,-2,-1 },
   {-2,-1, 3, 4,-3, 0, 1,-1, 0,-3,-4, 0,-3,-3,-2, 0,-1,-4,-3,-3, 4, 1,-1 },
   {-1, 0, 0, 1,-3, 3, 4,-2, 0,-3,-3, 1,-1,-3,-1, 0,-1,-3,-2,-2, 1, 4,-1 },
   { 0,-1,-1,-1,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-2, 0, 0,-2,-1,-1,-1,-1,-1 },
 };

NLM_EXTERN Int4Ptr PNTR load_default_matrix (void)
{
	Int4Ptr PNTR ss;
	Int2 i, j;

	ss = (Int4Ptr PNTR) MemNew((size_t)TX_MATRIX_SIZE * sizeof (Int4Ptr));
	for(i = 0; i<TX_MATRIX_SIZE; ++i)
		ss[i] = (Int4Ptr) MemNew((size_t)TX_MATRIX_SIZE * sizeof (Int4));

	for(i = 0; i < TX_MATRIX_SIZE; i++) 
		for(j = 0; j < TX_MATRIX_SIZE;j++) 
			ss[i][j] = -1000;
	for(i = 0; i < WEBB_asize; ++i)
		for(j = 0; j < WEBB_asize; ++j)
			ss[pchars[i]][pchars[j]] = webb_blosum62[i][j];
	for(i = 0; i < WEBB_asize; ++i)
		ss[pchars[i]]['*'] = ss['*'][pchars[i]] = -4;
	ss['*']['*'] = 1;
	return ss;
}

NLM_EXTERN void free_default_matrix (Int4Ptr PNTR matrix)
{
	Int2 i;

	for(i = 0; i<TX_MATRIX_SIZE; ++i)
		MemFree(matrix[i]);
	MemFree(matrix);
}

NLM_EXTERN SeqIdPtr LIBCALL
ScorePtrUseThisGi (ScorePtr sp)

{
    ObjectIdPtr obid;
    ScorePtr scrp;
    SeqIdPtr gilist=NULL;
    
    for (scrp=sp; scrp; scrp = scrp->next) {
        obid = scrp->id;
        if(obid && obid->str) {
            if (StringICmp(obid->str, "use_this_gi") == 0) {
                ValNodeAddInt(&gilist, SEQID_GI, scrp->value.intvalue);
            }
        }
    }
    
    return gilist;
}

/*
  GetUseThisGi(SeqAlignPtr) looks for the "use_this_gi" flag in the ScorePtr.
*/

NLM_EXTERN SeqIdPtr LIBCALL
GetUseThisGi(SeqAlignPtr seqalign)

{
	Boolean retval=FALSE;
        DenseDiagPtr ddp;
        DenseSegPtr dsp;
        ScorePtr sp;
	SeqIdPtr gilist=NULL;
        StdSegPtr ssp;
	
	sp = seqalign->score;
	if (sp == NULL)
	{
		switch (seqalign->segtype)
		{
			case 1: /*Dense-diag*/
				ddp = (DenseDiagPtr) seqalign->segs;
				while (ddp)
				{
					sp = ddp->scores;
					if (sp)
						break;
					ddp = ddp->next;
				}
				break;
			case 2:
				dsp = ( DenseSegPtr) seqalign->segs;
				if (dsp)
				{
					sp = dsp->scores;
				}
				break;
			case 3:
				ssp = (StdSegPtr) seqalign->segs;
				while (ssp)
				{
					sp = ssp->scores;
					if (sp)
						break;
					ssp = ssp->next;
				}
				break;
			default:
				break;
		}
	}


	gilist = ScorePtrUseThisGi(sp);
	return gilist;
}

/*************************************************************************
*
*	functions and structure related to create a text buffer for the 
*	alignment
*
*************************************************************************/

NLM_EXTERN ValNodePtr FreeTextAlignList(ValNodePtr tdp_list)
{
	TextAlignBufPtr tdp;
	ValNodePtr next;
	Int2 i;

	while(tdp_list)
	{
		next = tdp_list->next;
		tdp_list->next = NULL;
		tdp = (TextAlignBufPtr) tdp_list->data.ptrvalue;
		if(tdp->label)
			MemFree(tdp->label);
		if(tdp->buf)
			MemFree(tdp->buf);
		if(tdp->matrix_val)
			MemFree(tdp->matrix_val);
		if(tdp->exonCount > 0)
		{
			for(i =0; i<3; ++i)
				MemFree(tdp->codon[i]);
		}
		MemFree(tdp);
		MemFree(tdp_list);
		tdp_list = next;
	}

	return NULL;
}


/*######################################################################
#
#	functions related to ProcessTextAlignNode
#
#######################################################################*/


/******************************************************************************
*
*	load_text(bsp, pos1, pos2, l_seq, l_pos, mbuf, maxlen)
*	load the sequence into text
*	bsp: the Bioseq
*	pos1: the first position on the sequence. 
*	pos2: the second position on the sequence. 
*		if(pos1 and pos2 are negative val, indicate the region in on the*	minus strand
*	l_seq: the buffer for loading the sequence
*	l_pos: the current position in l_seq. Will be updated after the sequence
*		is loaded
*	mbuf: buffer from the master sequence. For checking mismatches and positive scores
*	maxlen: the maximum length per line. Used to determine the special 
*	format used for long insertions
*	spacing is the space between the two adjacent residues
*	mismatch: if TRUE, show the identical residue with 
*
*****************************************************************************/

static Boolean load_text(BioseqPtr bsp, Int4 pos1, Int4 pos2, CharPtr l_seq, Int4Ptr l_pos, CharPtr mbuf, Int2 maxlen, Int2 spacing, Boolean translate, Boolean mismatch, Int2Ptr matrix_val, Int4Ptr PNTR matrix, Uint1 strand, Int4Ptr PNTR posMatrix, Int4 q_start)
{
    SeqPortPtr spp = NULL;
    ByteStorePtr b_store = NULL;
    Uint1 code;
    Int4 start, stop;
    Uint1 m_res, t_res, stdaa_res;
    Int2 i;
    Int2 val;
    Int4 length, s_len;
    Int2 c_pos;
    Char temp[100];
    Boolean protein;
    Boolean overflow;
    Boolean reverse;
    Boolean is_real;
    SeqFeatPtr fake_cds;
    Boolean check_neg;	/*if aa is negative, load it as lower case char*/
    SeqMapTablePtr smtp;
    
    if(*l_pos >= maxlen )
        return FALSE;

    /* posMatrix uses NCBIstdaa encoding */
    
    if(posMatrix != NULL) { 
        if((smtp = SeqMapTableFindObj(Seq_code_ncbistdaa, 
                                      Seq_code_ncbieaa)) == NULL)
            return FALSE;
    }

    protein = (bsp->mol == Seq_mol_aa);
    reverse = FALSE;
    if(protein)
        code = Seq_code_ncbieaa;
    else
        code = Seq_code_iupacna;
    check_neg = (matrix_val == NULL && matrix != NULL);
    if(strand == Seq_strand_minus) {	/*on the minus strand*/

        start = -pos2;
        stop = -pos1;
        
        if(protein) {
            strand = Seq_strand_plus;
            reverse = TRUE;
        }
        
    } else {
        start = pos1;
        stop = pos2;
    }
    if(translate) {
        fake_cds = make_fake_cds(bsp, start, stop, strand);
        b_store = ProteinFromCdRegionEx(fake_cds, TRUE, FALSE);
        SeqFeatFree(fake_cds);
        if(b_store == NULL)
            return FALSE;
        length = (stop - start +1)/3;
        BSSeek(b_store, 0, SEEK_SET);
    } else {
        spp = SeqPortNew(bsp, start, stop, strand, code);
        length = stop - start +1;
    }
    c_pos = (Int2)(*l_pos);
    overflow = (c_pos >= maxlen);
    if(maxlen>0 && (length > maxlen)) {	/*large insertions*/
	
        for(i =0; i<5 && !overflow; ++i) {
            if(translate)
                l_seq[c_pos++] = (Uint1)BSGetByte(b_store);
            else {
                if(reverse)
                    SeqPortSeek(spp, length-1 -i, SEEK_SET);
                l_seq[c_pos++] = SeqPortGetResidue(spp);
            }
            overflow = (c_pos >= maxlen);
        }
        for(i =0; i<3 && !overflow; ++i) {
            l_seq[c_pos++] = '.';
            overflow = (c_pos >= maxlen);
        }
        if(!overflow) {
            if(translate)
                BSSeek(b_store, length-1, SEEK_SET);
            else if(!reverse)
                SeqPortSeek(spp, length-5, SEEK_SET);
            for(i =0; i<5 && !overflow; ++i) {
                if(translate)
                    l_seq[c_pos++] = (Uint1)BSGetByte(b_store);
                else {
                    if(reverse)
                        SeqPortSeek(spp, 4-i, SEEK_SET);
                    l_seq[c_pos++] = SeqPortGetResidue(spp);
                }
                overflow = (c_pos >= maxlen);
            }
        }
        if(overflow)
            l_seq[maxlen-1] = '\0';
        else
            l_seq[c_pos] = '\0';
        sprintf(temp, "(length=%ld)", (long) length);
        s_len = StringLen(temp);
        StringCat(l_seq, temp);
        *l_pos = c_pos+s_len;
    } else {
        if(translate) {
            while((val = BSGetByte(b_store)) != EOF) {
                t_res = (Uint1)val;
                l_seq[c_pos]= t_res;
                if(mbuf != NULL) {
                    m_res = mbuf[c_pos];
                    if(matrix_val && matrix)
                        matrix_val[c_pos] = (Int2)matrix[m_res][t_res];
                    if(mismatch && t_res == m_res)
                        l_seq[c_pos] = '.';
                    else if(check_neg && matrix[t_res][m_res] < 0)
                        l_seq[c_pos] = TO_LOWER(t_res);
                }
                c_pos += spacing;
                if(c_pos >= maxlen) {
                    c_pos = maxlen;
                    break;
                }
            }
        } else {
            if(reverse)
                SeqPortSeek(spp, length-1, SEEK_SET);
            s_len = 0;
            while((t_res = SeqPortGetResidue(spp)) != SEQPORT_EOF) {
                is_real = IS_ALPHA(t_res);
                if(is_real || t_res == '*' || t_res == '-') {
                    if(is_real && !protein)
                        t_res = TO_LOWER(t_res);
                    l_seq[c_pos] = t_res;
                    if(mbuf != NULL) {
                        m_res = mbuf[c_pos];
                        if(matrix_val) {
                            if(matrix) {
                                if(posMatrix != NULL) {
                                    if(t_res == m_res) /* complete match */
                                        matrix_val[c_pos] = INT2_MAX;
                                    else {
                                        stdaa_res = SeqMapTableConvert(smtp, t_res);
                                        matrix_val[c_pos] = (Int2)posMatrix[c_pos + q_start][stdaa_res];

                                        /* 
                                     if(posMatrix[c_pos + q_start][t_res] == 
                                     matrix[t_res][t_res]) {
                                     printf("Got it!");
                                     } */

                                    }
                                } else {
                                    matrix_val[c_pos] = (Int2)matrix[m_res][t_res];
                                }
                                
                            } else if(t_res == m_res)
                                matrix_val[c_pos] = '|';
                        }
                        
                        if(mismatch && t_res == m_res)
                            l_seq[c_pos] = '.';
                        else if(posMatrix != NULL) {
                            stdaa_res = SeqMapTableConvert(smtp, m_res);
                            if(check_neg && posMatrix[c_pos + q_start][stdaa_res] < 0)
                                l_seq[c_pos] = TO_LOWER(t_res);
                        } else { /*regular BLOSSUM62*/
                            if(check_neg && matrix[t_res][m_res] < 0)
                                l_seq[c_pos] = TO_LOWER(t_res);
                        }
                    }
                    c_pos += spacing;
                    if(c_pos >= maxlen) {
                        c_pos = maxlen;
                        break;
                    }
                    ++s_len;
                }
                if(reverse) {
                    if(s_len == length)
                        break;
                    else
                        SeqPortSeek(spp, length -1 - s_len, SEEK_SET);
                }
            }
        }
        *l_pos = c_pos;
    }
    
    if(translate)
        BSFree(b_store);
    else
        SeqPortFree(spp);
    return TRUE;
}

/*##########################################################################
#
#	functions related to add the features to the alignment
#
###########################################################################*/


typedef struct protbuf{	/*for loading the translation of a CDs*/
	CharPtr buf;	/*load the protein sequence*/
	Int4 start;	/*start position in CDs*/
	Int4 stop;	/*stop position in CDs*/
	Int4 pos;	/*position for the feature*/
	Boolean load_codon;	/*load the codon data for aa sequence*/
	ValNodePtr cvp_list;	/*list for loading the codon of an aa*/
}ProtBuf, PNTR ProtBufPtr;

		
			
/************************************************************************
*
*	check the protein sequence from CDs feature into the buffer
*
*************************************************************************/
static Boolean load_prot_seq(GatherContextPtr gcp)
{
	SeqFeatPtr sfp;
	ProtBufPtr pbp;
	SeqLocPtr loc;

	if(gcp->thistype != OBJ_SEQFEAT)
		return FALSE;
	sfp = (SeqFeatPtr)(gcp->thisitem);
	if(sfp->data.choice !=3)
		return FALSE;

	pbp = (ProtBufPtr)(gcp->userdata);
	if(pbp->load_codon)	/*looking for codon in aa sequence*/
	{
		pbp->cvp_list = aa_to_codon(sfp, pbp->start, pbp->stop);
		return (pbp->cvp_list !=NULL);
	}
		

	if(pbp->start <0)/*minus strand*/
		loc = SeqLocIntNew((-pbp->stop), (-pbp->start), Seq_strand_minus, SeqLocId(sfp->location));
	else
		loc = SeqLocIntNew(pbp->start, pbp->stop, Seq_strand_plus, SeqLocId(sfp->location));

	pbp->pos = print_protein_for_cds(sfp, pbp->buf, loc, TRUE);
	SeqLocFree(loc);
	return (pbp->pos != -1);
}



static Boolean buffer_for_feature(Int4 c_left, Int4 c_right, Int4 seq_start, Int4 seq_stop, ValNodePtr fnp_node, Boolean load_codon, ProtBufPtr pbp)
{
	FeatNodePtr fnp;
	Uint2 itemtype;
	CharPtr buf = NULL;
	Int2 i=0;
	Char symbol;
	ValNodePtr curr;
	IvalNodePtr inp;
	Int4 i_left, i_right;
	Int4 f_len;


	itemtype = (Uint2)(fnp_node->choice);

	if(itemtype!= OBJ_SEQFEAT)
		return FALSE;
	fnp = (FeatNodePtr) fnp_node->data.ptrvalue;
	f_len = seq_stop - seq_start +1; 
	if(load_codon)
		pbp->buf = NULL;
	else
		pbp->buf = (CharPtr) MemNew((size_t)(f_len +1)*sizeof(Char));
	pbp->start = seq_start;
	pbp->stop = seq_stop;
	pbp->pos = -1;
	pbp->load_codon= load_codon;
	pbp->cvp_list = NULL;

	buf = pbp->buf;
	if(buf !=NULL)
		MemSet((Pointer)buf,  '~', (size_t)(f_len) * sizeof(Char));
	switch(fnp->feattype)/*check symbol for different features*/
	{
		case FEATDEF_GENE:
			symbol = '+';
			break;
		case FEATDEF_mRNA:
			symbol = '^';
			break;
		case FEATDEF_CDS:
			symbol = '$';
			break;
		default:
			symbol = '*';
			break;
	}
	if(fnp->feattype ==FEATDEF_CDS)

		GatherItem(fnp->entityID, fnp->itemID, itemtype, (Pointer)(pbp), load_prot_seq);
	else
	{
		if(fnp->interval !=NULL)
		{
			for(curr = fnp->interval; curr !=NULL; curr = curr->next)
			{
				inp = (IvalNodePtr) curr->data.ptrvalue;
				i_left = inp->gr.left;
				i_right = inp->gr.right;
				if(!(i_left > c_right || i_right < c_left))
				{
					i_left = MAX(i_left, c_left);
					i_right = MIN(i_right, c_right);
					i_left -= c_left;
					i_right -=c_left;
					for(; i_left<=i_right; ++i_left)
						buf[i_left] = symbol;
				}
			}
		}
		else
		{
			i_left = fnp->extremes.left;
			i_right = fnp->extremes.right;
			if(!(i_left > c_right || i_right < c_left))
			{
				i_left = MAX(i_left, c_left);
				i_right = MIN(i_right, c_right);
				i_left -= c_left;
				i_right -=c_left;
				for(; i_left<=i_right; ++i_left)
					buf[i_left] = symbol;
			}
		}
			
	}
	if(buf!=NULL)
		buf[f_len]= '\0';
	if(pbp->pos == -1)
		pbp->pos = ABS(seq_start);

	if(pbp->buf != NULL || pbp->cvp_list !=NULL)
		return TRUE;
	else
		return FALSE;
}


	
static Boolean load_feature_data(ProtBufPtr pbp, FeatNodePtr fnp, Int4 pos, Int4 maxlen, ValNodePtr PNTR fbp_head)
{
	Boolean found;
	TextAlignBufPtr fbp;
	ValNodePtr curr, pcvp;
	CodonVectorPtr cvp;
	Boolean load_codon;
	CharPtr PNTR codon;
	Int2 i;
	Int4 f_len;
	Char label[100];
	CharPtr buf;
	Boolean locus = FALSE;
	
	if(pbp == NULL)
		return FALSE;
	if(pbp->buf == NULL && pbp->cvp_list == NULL)
		return FALSE;
	load_codon = (pbp->cvp_list !=NULL);
	f_len = pbp->stop - pbp->start +1;
	
	found = FALSE;
	for(curr = *fbp_head; curr !=NULL; curr = curr->next)
	{
	   fbp = (TextAlignBufPtr) curr->data.ptrvalue;
	   if(fbp->itemID == fnp->itemID)
	   {
	     if(load_codon)
	     {
		for(pcvp = pbp->cvp_list; pcvp!=NULL; pcvp= pcvp->next)
		{
		   cvp = (CodonVectorPtr) pcvp->data.ptrvalue;
		   if(cvp->exonCount == fbp->exonCount)
		   {
			codon = fbp->codon;
			for(i =0; i<3; ++i)
			{
			   if(pos > fbp->f_pos)
				make_empty(codon[i] + fbp->f_pos, (Int2)(pos - fbp->f_pos));
			   StringCat(codon[i], (cvp->buf[i]+cvp->aa_index));
			}
			cvp->exonCount = 0;
			fbp->f_pos = pos + f_len;
		   }
		}/*end of for*/
	     }
	     else
	     {
		if(fbp->pos == -1)
		   fbp->pos = pbp->pos+1;
		if(pos > fbp->f_pos)
		   make_empty(fbp->buf+fbp->f_pos, (Int2)(pos - fbp->f_pos));	
		StringCat(fbp->buf, pbp->buf);
		fbp->f_pos = pos + f_len;
		found = TRUE;
	     }
	   }
	}


	if(load_codon)
	{
	   for(pcvp = pbp->cvp_list; pcvp!=NULL; pcvp= pcvp->next)
	   {
		cvp = (CodonVectorPtr) pcvp->data.ptrvalue;
		if(cvp->exonCount !=0)
		{
		   fbp = (TextAlignBufPtr) MemNew(sizeof(TextAlignBuf));
		   fbp->seqEntityID = fnp->entityID;
		   fbp->pos = cvp->dna_pos +1;
		   fbp->strand = cvp->strand;
		   seqid_name(cvp->sip, label, locus, FALSE);
		   fbp->label = StringSave(label);
		   fbp->buf = NULL;
		   for(i =0; i<3; ++i)
		   {
		   	buf = (CharPtr) MemNew((size_t)(maxlen+1+1+1) * sizeof(Char));
			/*1 for partial start, 1 for partial stop*/
			if(pos > 0)
				make_empty(buf, (Int2)pos);
			StringCat(buf, cvp->buf[i]+cvp->aa_index);
			fbp->codon[i] = buf;
		   }
		   fbp->frame = cvp->frame;
		   fbp->f_pos = pos+f_len;
		   fbp->exonCount = cvp->exonCount;
		   fbp->itemID = fnp->itemID;
		   fbp->itemID = fnp->itemID;
		   fbp->feattype = fnp->feattype;
		   fbp->subtype = fnp->subtype;
		   fbp->entityID = fnp->entityID;
		   fbp->extra_space = (cvp->aa_index == 0);
		   ValNodeAddPointer(fbp_head, 0, fbp);
		
		}
	     }
	}
	else
	{
	   	if(!found)	/*create a new node*/
		{
		   fbp = (TextAlignBufPtr) MemNew(sizeof(TextAlignBuf));
		   buf = (CharPtr) MemNew((size_t)(maxlen+1) * sizeof(Char));
		   if(pos > 0)
			make_empty(buf, (Int2)pos);
		   StringCat(buf, pbp->buf);
		   fbp->seqEntityID = fnp->entityID;
		   fbp->f_pos = pos + f_len;
		   fbp->itemID = fnp->itemID;
		   fbp->buf = buf;
		   fbp->pos = pbp->pos+1;
		   if(fnp->label !=NULL)
			fbp->label = StringSave(fnp->label);
		   fbp->strand = fnp->extremes.strand;
		   fbp->itemID = fnp->itemID;
		   fbp->feattype = fnp->feattype;
		   fbp->subtype = fnp->subtype;
		   fbp->entityID = fnp->entityID;
		   fbp->exonCount = 0;
		   ValNodeAddPointer(fbp_head, 0, fbp);
		}
	}
	if(pbp->buf)
		MemFree(pbp->buf);
	if(pbp->cvp_list)
		free_cvp_list(pbp->cvp_list);
	return TRUE;
}


	
/**************************************************************************
*
*	collect_feature_buf(fnp_list, g_left, g_right, seq_start, l_pos, 
*	fbp_head, max_len)
*	collect the features to be shown together with the alignment
*	fnp_list: a list of FeatNode associated with the current segment
*	g_left: the left position 
*
***************************************************************************/
static ValNodePtr collect_feature_buf(ValNodePtr fnp_list, Int4 g_left, Int4 g_right, Int4 seq_start, Int4 l_pos, ValNodePtr fbp_head, Int4 maxlen, Boolean is_aa)
{
	ProtBuf pb;
	FeatNodePtr fnp;
	Int4 c_left, c_right;
	Int4 pos;
	Int4 fseq_start, fseq_stop;	/*map sequence start stop to the feature*/
	Int4 f_len;		/*length of the feature*/
	Boolean load_codon;

	if(fnp_list == NULL)
		return fbp_head;

	
	while(fnp_list)
	{
	   fnp = (FeatNodePtr) fnp_list->data.ptrvalue;
	   c_left = fnp->extremes.left;
	   c_right = fnp->extremes.right;
	   load_codon = (is_aa && fnp->feattype == FEATDEF_CDS);
	   if(!(c_left > g_right || c_right < g_left))
	   {
		if(c_left > g_left)	/*map the seq pos from the graphic pos*/
			fseq_start = seq_start + (c_left-g_left);
		else
			fseq_start = seq_start;
		c_left = MAX(c_left, g_left);
		c_right = MIN(c_right, g_right);
		f_len = c_right - c_left+1;
		fseq_stop = fseq_start+f_len-1;

		if(c_left > g_left)
		   pos = l_pos + (c_left - g_left);
		else
		   pos = l_pos;

		if(buffer_for_feature(c_left, c_right, fseq_start, fseq_stop, fnp_list, load_codon, &pb))
		
			load_feature_data(&pb, fnp, pos, maxlen, &fbp_head);
	   }
	   fnp_list = fnp_list->next;
	}

	return fbp_head;
}
	
static Int4 map_position_by_spacing(Int4 distance, Int4 spacing, Boolean is_head)
{
	Int4 pos, left_over;

	if(spacing == 1)
		return distance;

	pos = distance/spacing;
	left_over = distance%spacing;

	if(left_over == 0 && !is_head)
		pos = MAX(pos-1, 0);
	else if(left_over == 2 && is_head)
		++pos;
	return pos;
}

static void add_empty_space(CharPtr buf, Int4 maxlen)
{
	Int4 buf_len;

	buf_len = StringLen(buf);	
	if(buf_len < maxlen)
		make_empty(buf+buf_len, (Int2)(maxlen-buf_len));
}
static void copy_insertion_bar(CharPtr buf, CharPtr ins_2, Int2 sym_pos, Int4 len)
{
	Int2 k;

	if(buf == NULL || ins_2 == NULL)
		return;
	add_empty_space(buf, len);
	for(k = 0; k<sym_pos; ++k)
		if(ins_2[k] == '|' && buf[k] == ' ')
			buf[k] = '|';
}

static Int4 get_long_insert_len(Int4 length)
{
	Char temp[50];

	sprintf(temp, "(length=%ld)", (long) length);
	return (StringLen(temp) + 13);
}

static ValNodePtr load_tdp_data(ValNodePtr PNTR head, CharPtr label, CharPtr text, Uint4 itemID, Uint2 entityID, Uint2 seqEntityID, Uint4 bsp_itemID)
{
	TextAlignBufPtr tdp;

	tdp = (TextAlignBufPtr) MemNew(sizeof(TextAlignBuf));
	tdp->pos = -1;
	tdp->label = label;
	tdp->buf= text;
	tdp->itemID = itemID;
	tdp->entityID = entityID;
	tdp->seqEntityID = seqEntityID;
	tdp->bsp_itemID = bsp_itemID;

	return ValNodeAddPointer(head, 0, (Pointer)tdp);
}
			 
/******************************************************************************
*
*	ProcessTextInsertion(anp, m_left, m_right, bsp)
*	convert the insertions that are located within [m_left, m_right] into
*	text buffer (a list of TextDrawPtr)
*	anp: AlignNodePtr
*	m_left, m_right: the current region for selection
*	bsp: the BioseqPtr for this anp
*
*	return a list of TextDrawPtr
*
******************************************************************************/
static ValNodePtr ProcessTextInsertion(AlignNodePtr anp, Int4 m_left, Int4 m_right, BioseqPtr bsp, Int4 line_len, Int1 m_frame)
{
	AlignSegPtr asp;
	Int4 inslen;		/*length of insertion*/
	Int2 insnum;		/*the number of insertions*/
	Int2 i, j;
	Int4Ptr inslevel;	/*for layout the level of insertions*/
	Int4 level;
	Int4 inspos;		/*position for insertion*/
	Int4 left;
	Int4 len;
	Int4 last_ins;

	CharPtr ins_1;	/* \ symbols for insertions*/
	CharPtr ins_2;	/*| symbols for insertion*/
	CharPtr ins_seq;
	Int4 sym_pos;
	Int4 l_pos;
	Int4 seq_offset, seq_start, seq_stop;
	ValNodePtr head = NULL;
	ValNodePtr fbuf_list = NULL, curr;
	TextAlignBufPtr fbp;
	Int4 g_left, g_right;
	Boolean is_aa;
	Int4 seq_expand;
	Int4 spacing;
	Boolean translate;
	Uint1 strand;

	strand = Seq_strand_plus;
	if(anp->seqpos < 0)
		strand = Seq_strand_minus;
	else if(anp->seqpos == 0 && anp->extremes.strand == Seq_strand_minus)
		strand = Seq_strand_minus;
	spacing = 1;
	if(m_frame > 0)
		spacing = 3;
	if(m_frame  == -1)
	{
		translate = TRUE;
		seq_expand = 3;
	}
	else
	{
		seq_expand = 1;
		translate = FALSE;
	}
	is_aa = (bsp->mol == Seq_mol_aa);
	insnum = 0;
	for(asp = anp->segs; asp !=NULL; asp = asp->next)
	/*checking the insertion numbers*/
	{ 
	   if(asp->type == INS_SEG)
	   {
	   	inspos = asp->ins_pos;
		if (inspos >= m_left && inspos<=m_right)
		{
			++insnum;
			asp->line = 0;
		}
		else
			asp->line = -1;
	   }
	}
	if(insnum == 0)
		return head;

	/*layout the insertions*/
	inslevel = (Int4Ptr) MemNew((size_t)(2*insnum) * sizeof(Int4));	/*layout insert*/
	level = 0;
	len = MAX(m_right - m_left +1, line_len);
	for(asp = anp->segs; asp !=NULL; asp = asp->next)
	{
	   if(asp->type == INS_SEG && asp->line == 0)
	   {
	   	inspos = asp->ins_pos;
		inslen = asp->gr.right/seq_expand;
		/* if(inslen > (m_right-m_left+1)) */
		if(inslen > len)
			inslen = get_long_insert_len(inslen);
		inspos -= m_left;
		asp->line = find_insert_ypos(&left, inslen, inspos, 0, len-1, inslevel, 2, insnum);
		asp->gr.left = left;
		level = MAX(asp->line, level);
	   }	
	}
	MemFree(inslevel);
	

	/*comput the insertion text*/
	for(j = 0; j< (level+1); ++j)
	{
	   l_pos = 0;
	   sym_pos = 0;
	   fbuf_list = NULL;
	   ins_seq = (CharPtr) MemNew((size_t)(len+1) * sizeof(Char));
	   ins_2 = (CharPtr) MemNew((size_t)(len+1) * sizeof(Char));
	   if(j == 0)
		ins_1 = (CharPtr) MemNew((size_t)(len+1) * sizeof(Char));
	   seq_offset = 0;
	   for(asp = anp->segs; asp !=NULL; asp = asp->next)
	   {
	   	if(asp->type == INS_SEG && asp->line >=j)
		{
	
			inspos = asp->ins_pos - m_left;
			if(inspos > sym_pos)
			{
			   if(j == 0)	/*the first level*/
				make_empty(ins_1+sym_pos, (Int2)(inspos-sym_pos));
			   make_empty(ins_2+sym_pos, (Int2)(inspos-sym_pos));
			   sym_pos = inspos;
			}
			if(j == 0)
				ins_1[sym_pos] = '\\';
			ins_2[sym_pos] = '|';
			if(asp->line == j)
				last_ins = inspos+1;
			++sym_pos;
			
			if(asp->line == j)
			{
			   seq_start = anp->seqpos + seq_offset;
			   seq_stop = seq_start + asp->gr.right -1;
			   /* seq_stop = seq_start + map_position_by_spacing(asp->gr.right, spacing, FALSE) * seq_expand + seq_expand -1; */
			   if(asp->gr.left > l_pos)
			   {
				make_empty(ins_seq+l_pos, (Int2)(asp->gr.left-l_pos));
				l_pos = asp->gr.left;
			   }

			   g_left = asp->ins_pos;
			   g_right = asp->ins_pos + asp->gr.right -1;
			   /* g_left = asp->gr.left;
			   g_right = g_left + asp->gr.right -1;*/

			   if((seq_stop - seq_start+1)>len)/*long insertions*/
			   {
		   	      fbuf_list = collect_feature_buf(asp->cnp, g_left, (g_left+4), seq_start, l_pos, fbuf_list, len, is_aa);	/*check the features first*/
		   	      fbuf_list = collect_feature_buf(asp->cnp, g_left, (g_left+4), seq_stop-4, l_pos+8, fbuf_list, len, is_aa);	/*check the features ffirst. 3 is the 3 dots*/
			   }
			   else
			      fbuf_list = collect_feature_buf(asp->cnp, g_left, g_right, seq_start, l_pos, fbuf_list, len, is_aa);

			   load_text(bsp, seq_start, seq_stop, ins_seq, &l_pos, NULL, (Int2)len, 1, translate, FALSE, NULL, NULL, strand, NULL, 0);
			}

		}
		if(asp->type == INS_SEG)
			seq_offset += asp->gr.right;
		if(asp->type == DIAG_SEG || asp->type == REG_SEG || asp->type == STD_SEG)
			seq_offset += map_position_by_spacing(asp->gr.right - asp->gr.left +1, 
				spacing, TRUE) * seq_expand; 
			/* seq_offset += (asp->gr.right - asp->gr.left +1) * seq_expand; */
	   }

	   ins_2[sym_pos] = '\0';
	   ins_seq[l_pos] = '\0';
	   if(j == 0)
	   {
		ins_1[sym_pos] = '\0';	
		load_tdp_data(&head, NULL, ins_1, 0, 0, 0, 0);
	   }

	   for(curr = head; curr !=NULL; curr = curr->next)
	   /*for(curr = fbuf_list; curr !=NULL; curr = curr->next)*/
	   {
		fbp = (TextAlignBufPtr) curr->data.ptrvalue;
		if(fbp->buf != NULL)
			copy_insertion_bar(fbp->buf, ins_2, (Int2)sym_pos, len);
		else
		{
			for(i =0; i<3; ++i)
				copy_insertion_bar(fbp->codon[i], ins_2, (Int2)sym_pos, len);
		}
	   }

	   copy_insertion_bar(ins_seq, ins_2, (Int2)sym_pos, len);
	   load_tdp_data(&head, NULL, ins_2, 0, 0, 0, 0);
	   load_tdp_data(&head, NULL, ins_seq, anp->itemID, anp->entityID, anp->seq_entityID, anp->bsp_itemID);
	   ValNodeLink(&head, fbuf_list);
	   fbuf_list = head;
	}

	return head;

}	

/***********************************************************************
*
*	ProcessTextAlignNode(anp, m_left, m_right, p_stop, m_buf, locus)
*	Process the AlignNode to make a list of text buffer on the 
*	current region
*	anp: AlignNodePtr
*	m_left, m_right: the region on the alignment. Mapped in response
*	to anp->extremes.left, and anp->extremes.right
*	p_stop: the stop position of the previous segment. Used to label
*	the position of a line composed entirely of gaps
*	m_buf: buffer for the master sequence. Used to compare mismatches
*	locus: if TRUE, show the locus name of the alignment
*
*	frame: frame >0, those are the hits from blastx. So, the 
*	protein need to be displayed to the proper frame
*	frame 1-3: match to the plus strand of the master
*	frame 4-6: match to the minus strand of the master
*	frame 0:   no tranlsation, no frame match to the master
*	frame -1:  translate the DNA sequence
*	option:    option for display the alignments
*   matrix:	   the protein alignment matrix
*
*
************************************************************************/

NLM_EXTERN ValNodePtr ProcessTextAlignNode2(AlignNodePtr anp, Int4 m_left, Int4 m_right, Int4Ptr p_stop, CharPtr m_buf, Int4 line_len, Int1 m_frame, Uint4 option, Int4Ptr PNTR matrix, Int4Ptr PNTR posMatrix, Int4 q_start)
{
    Int4 maxlen;
    Int4 g_left, g_right;
    Int4 len;		/*length of the segment*/
    CharPtr l_seq;	/*the buffer for the sequence*/
    Int2Ptr matrix_val;	/*value of each residue in alignment matrix*/
    Int4 l_pos;		/*the start position on the line*/	
    Int4 offset;
    BioseqPtr bsp;
    SeqEntryPtr sep;
    
    AlignSegPtr asp;
    Int4 seq_offset, off_len;
    Int4 seq_start, seq_stop;
    Int4 s_start, s_stop;	/*for marking the position on one line*/
    CharPtr str;
    
    ValNodePtr head = NULL, ins_node;
    ValNodePtr fbuf_list = NULL;
    TextAlignBufPtr tdp;
    Boolean is_aa;
    Int4 spacing;
    Boolean translate;
    Int4 seq_expand;
    Boolean show_mismatch;
    Boolean set_matrix;
    Uint1 strand;
    
    
    if(m_frame > 6 || m_frame < -1)	/*check the m_frame. -1 for translate the hits*/
        return NULL;
    
    
    g_left = anp->extremes.left;
    g_right = anp->extremes.right;
    if(m_left > g_right || m_right < g_left)/*no overlap*/ {
        if(m_frame > 0) {
            if(anp->m_frame != m_frame)
                return NULL;
            if(m_buf == NULL)
                return NULL;
        }
        if(option & TXALIGN_BLUNT_END) {
            maxlen = m_right - m_left +1;
            l_seq = (CharPtr) MemGet((size_t)(maxlen+1)*sizeof(Char), MGET_ERRPOST);
            MemSet((Pointer)l_seq, '-',(size_t)(maxlen) * sizeof(Char));
            l_seq[maxlen] = '\0';
            tdp = (TextAlignBufPtr) MemNew(sizeof(TextAlignBuf));
            tdp->pos = *p_stop;
            tdp->strand = anp->extremes.strand;
            tdp->label = StringSave(anp->label);
            tdp->buf = l_seq;
            tdp->matrix_val = NULL;
            tdp->itemID = anp->itemID;
            tdp->feattype = 0;
            tdp->subtype = 0;
            tdp->entityID = anp->entityID;
            tdp->seqEntityID = anp->seq_entityID;
            tdp->bsp_itemID = anp->bsp_itemID;
            ValNodeAddPointer(&head, 0, tdp);
            return head;
        }
        else
            return NULL;
    }
    
    strand = Seq_strand_plus;
    if(anp->seqpos < 0)
        strand = Seq_strand_minus;
    else if(anp->seqpos == 0 && anp->extremes.strand == Seq_strand_minus)
        strand = Seq_strand_minus;
    
    l_pos = 0;
    spacing = 1;
    offset = 0;
    if(m_frame > 0) {
        if(anp->m_frame != m_frame)
            return NULL;
        if(m_buf == NULL)
            return NULL;
        /*add the empty space to reflect the reading frame*/
        for(str = m_buf; *str != '\n' && *str != '\0'; ++str) {
            if(IS_WHITESP(*str))
                ++offset;
            else
                break;
        }
        spacing = 3;
    }
    if(m_left < g_left) {
        l_pos += (g_left - m_left);
        if(m_frame > 0)
            ++l_pos;
    } else
        l_pos += offset;
    
    bsp = BioseqLockById(anp->sip);
    if(bsp == NULL)
        return NULL;
    is_aa = (bsp->mol == Seq_mol_aa);
    if((m_frame > 0 && !is_aa) || (m_frame == -1 && is_aa)) {
        BioseqUnlock(bsp);
        return NULL;
    }
    if(anp->seq_entityID == 0) {
        sep = SeqEntryFind(bsp->id);
        anp->seq_entityID = SeqMgrGetEntityIDForSeqEntry(sep);
    }
    if(anp->bsp_itemID == 0)
        anp->bsp_itemID = get_bioseq_itemID(bsp, anp->seq_entityID);
    
    if(m_frame == -1) {
        translate = TRUE;
        seq_expand = 3;
    } else {
        translate = FALSE;
        seq_expand = 1;
    }
    
    maxlen = m_right - m_left +1;
    l_seq = (CharPtr) MemGet((size_t)(maxlen+1)*sizeof(Char), 
                             MGET_ERRPOST);
    if(option & TXALIGN_BLUNT_END)
        MemSet((Pointer)l_seq, '-',(size_t)maxlen * sizeof(Char));
    else
        MemSet((Pointer)l_seq, ' ',(size_t)maxlen * sizeof(Char));
    l_seq[maxlen] = '\0';
    
    
    set_matrix = FALSE;
    if(m_frame == 0 && bsp->mol != Seq_mol_aa) { /*DNA-DNA alignment*/
        if(option & TXALIGN_MATRIX_VAL)
            set_matrix = TRUE;
    } else {
        if(matrix != NULL && (option & TXALIGN_MATRIX_VAL))
            set_matrix = TRUE;
    }
    if(set_matrix) {
        matrix_val = (Int2Ptr) MemGet((size_t)(maxlen+1)*sizeof(Int2), MGET_ERRPOST);
        MemSet((Pointer)matrix_val, 0,(size_t)maxlen * sizeof(Int2));
    } else
        matrix_val = NULL;
    show_mismatch = (Boolean)(option & TXALIGN_MISMATCH);
    
    
    /*process  the GAPs and the DIAGs segs*/
    s_start = -1;
    s_stop = -1;
    off_len = 0;
    for(asp = anp->segs; asp !=NULL; asp = asp->next) { 
        g_left = asp->gr.left;
        g_right = asp->gr.right;
        if(!(g_left > m_right || g_right < m_left)) {
            switch(asp->type) {
            case GAP_SEG:
                g_left = MAX(m_left, g_left);
                g_right = MIN(m_right, g_right);
                len = g_right - g_left +1;
                MemSet((Pointer)(l_seq +l_pos), '-',(size_t)len * sizeof(Char));
                l_pos += len;
                break;
                
            case REG_SEG:
            case DIAG_SEG:
            case STD_SEG:	/* Std-seg only works if the m_frame != 0 */
                if(m_left > g_left)
                    len = off_len + m_left - g_left;
                else
                    len = off_len;
                seq_offset = map_position_by_spacing(len, spacing, TRUE) * seq_expand;
                seq_start = anp->seqpos + seq_offset;
                g_left = MAX(m_left, g_left);
                g_right = MIN(m_right, g_right);
                len += (g_right - g_left);
                seq_stop = anp->seqpos + map_position_by_spacing(len, spacing, FALSE) * seq_expand + seq_expand -1;
                
                if(seq_start <= seq_stop) {	/*the order of start and stop is reversed*/
                    if(s_start == -1)	/*record the end point*/
                        s_start = ABS(seq_start);
                    s_stop = ABS(seq_stop);
                    
                    if(m_frame == 0)
                        fbuf_list = collect_feature_buf(asp->cnp, g_left, g_right, seq_start, l_pos, fbuf_list, maxlen, is_aa);	/*check the features first*/
                    load_text(bsp, seq_start, seq_stop, l_seq, &l_pos, m_buf, (Int2)maxlen, 
                              (Int2)spacing, translate, show_mismatch, matrix_val, matrix, strand, posMatrix, q_start);
                    
                }
                break;
                
            default:
                break;
            }
        }
        if(asp->type == INS_SEG)
            off_len += (asp->gr.right * spacing);
        if(asp->type == REG_SEG || asp->type == DIAG_SEG || asp->type == STD_SEG)
            off_len+=(asp->gr.right - asp->gr.left +1);
    }
    
    
    /*the first segment in the layout is a gap segment*/
    if(s_start == -1)
        s_start = *p_stop;
    if(s_stop == -1)	/*gap across the entire region*/
        s_stop = *p_stop;
    *p_stop = s_stop	/*update the stop value*/;
    tdp = (TextAlignBufPtr) MemNew(sizeof(TextAlignBuf));
    tdp->pos = s_start+1;
    tdp->strand = anp->extremes.strand;
    tdp->label = StringSave(anp->label);
    tdp->buf = l_seq;
    tdp->matrix_val = matrix_val;
    tdp->itemID = anp->itemID;
    tdp->feattype = 0;
    tdp->subtype = 0;
    tdp->entityID = anp->entityID;
    tdp->seqEntityID = anp->seq_entityID;
    tdp->bsp_itemID = anp->bsp_itemID;
    ValNodeAddPointer(&head, 0, tdp);
    ValNodeLink(&head, fbuf_list);
    
    ins_node = ProcessTextInsertion(anp, m_left, m_right, bsp, line_len, m_frame);
    ValNodeLink(&head, ins_node);
    BioseqUnlock(bsp);
    return head;
}

NLM_EXTERN ValNodePtr ProcessTextAlignNode(AlignNodePtr anp, Int4 m_left, Int4 m_right, Int4Ptr p_stop, CharPtr m_buf, Int4 line_len, Int1 m_frame, Uint4 option, Int4Ptr PNTR matrix)
{
    return ProcessTextAlignNode2(anp, m_left, m_right, p_stop, m_buf, line_len, m_frame, option, matrix, NULL, 0);
}

NLM_EXTERN ValNodePtr clean_annot_for_anp(ValNodePtr PNTR head)
{
	ValNodePtr prev, next, anp_list;
	
	prev = NULL;
	anp_list = *head;
	while(anp_list)
	{
		next = anp_list->next;
		if(anp_list->choice == OBJ_SEQANNOT)
		{
			if(prev == NULL)
				*head = next;
			else
				prev->next = next;
			anp_list->next = NULL;
			FreeAlignNode(anp_list);
		}
		else
			prev = anp_list;
		anp_list = next;
	}
	
	return (*head);
}



/***********************************************************************
*
*	FreeFeatureList(list)
*	free a list of FeatNode
*
***********************************************************************/
NLM_EXTERN ValNodePtr FreeFeatureList  (ValNodePtr list)
{
	FeatNodePtr  fnp;
	ValNodePtr   next;

 	while (list != NULL) 
 	{
		next = list->next;
		fnp = list->data.ptrvalue;
		if (fnp != NULL) 
		{
			ValNodeFreeData (fnp->interval);
			MemFree (fnp->label);
			MemFree(fnp->pos_label);
			if(fnp->supress_node != NULL)	/*hidden features*/
				FreeFeatureList(fnp->supress_node);
			MemFree (fnp);
		}
		MemFree (list);

		list = next;
	}
	return NULL;
}

/*********************************************************************
*
*	extract_node_list(head, itemType, entityID, feattype, subtype, 
*	label_type)
*	extract a list of featnode from head which will have the 
*	selected itemType, entityID, feattye, subtype, label_type. 
*	set values to 0 if it is not considered in the selection
*
*********************************************************************/
static Boolean do_collect(ValNodePtr vnp, Uint1 itemType, Uint2 entityID, Uint1 feattype, Uint1 label_type)
{
	Boolean is_num;		/*is the gene mark a number*/
	FeatNodePtr fnp;

	if(vnp->choice != itemType)
		return FALSE;

	fnp = (FeatNodePtr)(vnp->data.ptrvalue);
	if(fnp == NULL)
		return FALSE;

	if(entityID !=0)
		if(fnp->entityID !=entityID)
			return FALSE;


	if(itemType == OBJ_SEQFEAT)
	{
		if((feattype == 0) || (fnp->feattype == feattype))
		{
			if(label_type == ALL_LABEL)
				return TRUE;
			is_num = IS_NUM_GENE(fnp->label);
			if(label_type == STR_LABEL)
				return (is_num == FALSE);
			if(label_type == NUM_LABEL)
				return (is_num == TRUE);
		}
		else
			return FALSE;
	}

	return TRUE;
	
}



NLM_EXTERN ValNodePtr extract_node_list(ValNodePtr PNTR head, Uint1 itemType, Uint2 entityID, Uint1 feattype, Uint1 label_type)
{
	ValNodePtr vnp, prev, list, next;


	list = NULL;
	prev = NULL;
	vnp = *head;
	while(vnp)
	{
		next = vnp->next;
		if(do_collect(vnp, itemType, entityID, feattype, label_type))
		{
			if(prev == NULL)
				*head = vnp->next;
			else
				prev->next = vnp->next;
			vnp->next = NULL;
			ValNodeLink(&list, vnp);
		}
		else
			prev = vnp;
		vnp = next;
	}
	
	return list;

}

NLM_EXTERN ValNodePtr extract_lollipop_feature(ValNodePtr PNTR head, Int4 scale, BoolPtr lolli_feature)
{
	ValNodePtr vnp, prev, list, next;
	FeatNodePtr fnp;
	Boolean extract = FALSE;


	list = NULL;
	prev = NULL;
	vnp = *head;
	while(vnp)
	{
		next = vnp->next;
		extract = FALSE;
		if(vnp->choice == OBJ_SEQFEAT)
		{
			fnp = vnp->data.ptrvalue;
			if((fnp->extremes.right - fnp->extremes.left +1) <= scale)
				extract = TRUE;
			else if(lolli_feature != NULL)
				extract = lolli_feature[fnp->feattype];
		}
		if(extract)
		{
			if(prev == NULL)
				*head = vnp->next;
			else
				prev->next = vnp->next;
			vnp->next = NULL;
			ValNodeLink(&list, vnp);
		}
		else
			prev = vnp;
		vnp = next;
	}
	
	return list;

}

/*deside whether the alignment is of different molecules */
NLM_EXTERN Uint1 get_alignment_type(AnnotInfoPtr annot_info)
{
	if(annot_info->blast_type == ALIGN_BLASTX)
		return ALIGN_DNA_TO_PROT;
	if(annot_info->blast_type == ALIGN_TBLASTN)
		return ALIGN_PROT_TO_DNA;
        if(annot_info->blast_type == ALIGN_PSITBLASTN)
                return ALIGN_PROT_TO_DNA;
	if(annot_info->blast_type == ALIGN_TBLASTX)
		return ALIGN_TDNA_TO_TDNA;
	return 0;
}


/*********************************************************************
*
*	FreeAlignNode(list)
*	free a list of AlignNodePtr
*
*********************************************************************/
NLM_EXTERN ValNodePtr FreeAlignNode(ValNodePtr list)
{
	AlignNodePtr anp;
	AlignSegPtr asp, aspnext;
	ValNodePtr   next;
	AlignBlockPtr abp, abpnext;
	AnnotInfoPtr annot_info;

 	while (list != NULL) 
 	{
		next = list->next;
		if(list->choice == OBJ_SEQANNOT)
		{
			annot_info = list->data.ptrvalue;
			MemFree(annot_info);
		}
		else
		{
			anp = list->data.ptrvalue;
			if (anp != NULL) 
			{
				asp = anp->segs;
				while(asp !=NULL)
				{
					aspnext = asp->next;
					asp->next = NULL;
					if(asp->cnp != NULL)
						FreeFeatureList(asp->cnp);
					if(asp->mismatch)
						ValNodeFree(asp->mismatch);
					MemFree(asp);
					asp = aspnext;
				}
				abp = anp->blocks;
				while(abp != NULL)
				{
					abpnext = abp->next;
					MemFree(abp);
					abp = abpnext;
				}
				if(anp->pop_sap !=NULL)
					SeqAnnotFree(anp->pop_sap);
				SeqIdFree(anp->sip);
				MemFree (anp->label);
				MemFree (anp->clone_id);
				MemFree(anp);
			}
		}
		MemFree (list);

		list = next;
	}
	return NULL;
}




/***********************************************************************
*
* 	CollectSegmentSeq(bsp, slp, seqID, offset, head)
*		collect the segments in Bioseq
*	bsp: Bioseq
*	slp: the location on bsp to be collected
*	seqID: the order of bsp in the current list
*	offset: the offset to the graphic
*	head: the head of the previous list
*	return the head of new list
*	if bsp is a segmented sequence, the corresponding segments are recorded
*	in inp. Otherwise there is only one inp for slp.
*	
************************************************************************/


typedef struct collectheader{	/*for collecting data of a sequence display*/
	CollectSeqOptionPtr csop;	/*option for the sequences+features*/
	ValNodePtr features;		/*a list of FeatNode for storing the feature data*/
	ValNodePtr prev_feat;		/*previous node, for speed it up */
	CollectAlignOptionPtr caop;	/*option for the alignment*/
	ValNodePtr aligns;			/*a list of AlignNode for storing alignment data*/
	ValNodePtr prev_align;		/*the previous node for alignment*/	
	SeqLocPtr slp;				/*target Seq-loc*/
	SeqIdPtr maybe_mapid;			/*a possible mapid*/
	
	ObjMgrPtr omp;				/*for save some space in the collection*/
	Char thislabel[101];
	Char ftype[101];
	Uint2 subtype;
	Int2 filter_level;
	GeneDataPtr gdata;
	Uint2 priority;
	Boolean take_all_annot;		/*take everything in a Seq-annot*/
	Boolean load_align;
	Boolean skip_feature;
	Uint1 index;
	Char annotDB[21];
	Boolean is_lod_score;
}CollectHeader, PNTR CollectHeaderPtr;



static void link_data_for_collect (ValNodePtr PNTR head, ValNodePtr PNTR prev, Pointer data, Uint1 type)
{
	ValNodePtr curr;

	curr = ValNodeNew(NULL);
	curr->choice = type;
	curr->data.ptrvalue = data;

	if(*prev == NULL)
		*head = curr;
	else
		(*prev)->next = curr;

	*prev = curr;
}

static ValNodePtr get_last_node (ValNodePtr head)
{
	if(head == NULL)
		return NULL;

	while(head->next != NULL)
		head = head->next;
	return head;
}

static FeatNodePtr CreateFeatNode (ValNodePtr PNTR f_head, ValNodePtr PNTR prev, Uint2 itemType, Uint4 itemID, Uint2 entityID, Uint2 feattype)
{
	FeatNodePtr  fnp;

	fnp = MemNew (sizeof (FeatNode));
	fnp->itemID = itemID;
	fnp->entityID = entityID;
	fnp->feattype = (Uint1)feattype;
	link_data_for_collect(f_head, prev, (Pointer)fnp, (Uint1)(itemType));
	return fnp;
}

static Boolean collect_feature_label(Uint1 format)
{
	return (format <=OM_LABEL_SUMMARY);
}

static Boolean collect_sequence_label(Uint1 format)
{
	return (format >= PRINTID_FASTA_SHORT && format <=PRINTID_REPORT);
}

/*#####################################################################
#
#	functions related to the collection of the features of alignment
#
#####################################################################*/


/****************************************************************
*
*	satcollfunc()
*	callback function for collecting features on Sequence 
*	alignment. It recalculates the feature intervals based on 
*	the intervals in the aligned segments
*
****************************************************************/
typedef struct alignfeat
{
	ObjMgrPtr omp;
	AlignNodePtr anp;
	CollectSeqOptionPtr csop;
	Int2 filter_level;
	Boolean all_feature;
}AlignFeat, PNTR AlignFeatPtr;

static Boolean is_powerblast_feature(SeqAnnotPtr annot)
{
	ValNodePtr desc;

	if(annot->type != 1)
		return FALSE;
	for(desc = annot->desc; desc != NULL; desc = desc->next)
	{
		if(desc->choice == Annot_descr_name)
		{
			if(StringICmp(desc->data.ptrvalue, "powblast") == 0)
				return TRUE;
			if(StringICmp(desc->data.ptrvalue, "powerblast") == 0)
				return TRUE;
			/*powerBlast feature*/
			if(StringNCmp(desc->data.ptrvalue, "PB:", 3) == 0)
				return TRUE;
		}
	}
	return FALSE;
}


static Boolean satcollfunc(GatherContextPtr gcp)
{
	SeqFeatPtr sfp;
	AlignFeatPtr afp;
	CollectSeqOptionPtr csop;
	
	AlignNodePtr anp;
	Uint2 feat_subtype;	/*types defined by objfdef.h*/
	SeqLocPtr slp = NULL;
	Char label[101];
	ObjMgrTypePtr omtp;
	IvalNodePtr new;
	FeatNodePtr fnp;
	AlignSegPtr asp;
	Int4 current_pos;
	Uint1 strand;
	Int4 seglen;
	SeqLocPtr head;
	Int2 label_size;
	Int4 left, right, e_left, e_right;
	Int4 i_left, i_right;
	Int4 ins_len, gap_len;
	GatherRangePtr grp;
	Int2 i;
	ValNodePtr prev;
	
	afp= (AlignFeatPtr)(gcp->userdata);
	if(afp == NULL || afp->csop == NULL)
		return FALSE;

	if(gcp->thistype == OBJ_SEQANNOT)
	{
		afp->all_feature = is_powerblast_feature((SeqAnnotPtr)(gcp->thisitem));
		return TRUE;
	}
	if(gcp->thistype != OBJ_SEQFEAT)
		return TRUE;
		
	if(afp->filter_level == gcp->seglevel+1)
		return TRUE;
	csop = afp->csop;
	label_size = MIN(100, csop->label_size);
	if(csop->features == NULL && afp->all_feature == FALSE)
		return FALSE;

	omtp=ObjMgrTypeFind(afp->omp, OBJ_SEQFEAT, NULL, NULL);
	if(omtp == NULL)
		return TRUE;

	feat_subtype = 0;
	if(omtp->subtypefunc !=NULL)
		feat_subtype =  (*(omtp->subtypefunc)) (gcp->thisitem); 
	if((afp->all_feature == FALSE) && 
		(csop->features[feat_subtype] == FALSE))	/*do not collect the current feature*/
		return TRUE;


	anp = afp->anp;
	current_pos = anp->seqpos;
	if(anp->seqpos < 0)
		strand = Seq_strand_minus;
	else
		strand = Seq_strand_plus;
	sfp = gcp->thisitem;
	label[0] = '\0';
	if(collect_feature_label(csop->flabel_format[feat_subtype]))
		if(omtp->labelfunc !=NULL)
			(*(omtp->labelfunc))(sfp, label, label_size, csop->flabel_format[feat_subtype]);

	/*map to the location of aligned segs*/
	if(gcp->product)	/*for protein sequence alignment*/
		head = sfp->product;
	else
		head = sfp->location;
	left = anp->extremes.left;
	ins_len = 0;
	gap_len = 0;
	e_left = gcp->extremes.left;
	e_right = gcp->extremes.right;
	for(asp = anp->segs; asp !=NULL; asp = asp->next)
	{
		if(asp->type != GAP_SEG)
		{
			prev = get_last_node (asp->cnp);
			if(asp->type == INS_SEG)
			{
				seglen = asp->gr.right;
				/*ins_len += seglen;*/
			}
			else
				seglen = asp->gr.right - asp->gr.left +1;
			right = left + seglen -1;
			if(!(left > e_right || right < e_left))
			{
				fnp = CreateFeatNode (&(asp->cnp), &prev, OBJ_SEQFEAT, gcp->itemID, gcp->entityID, feat_subtype);
				fnp->extremes.left = MAX(left, e_left)  + gap_len;
				fnp->extremes.right = MIN(right, e_right) + gap_len;
				fnp->extremes.left -=ins_len;
				fnp->extremes.right -= ins_len;
				fnp->extremes.strand = gcp->extremes.strand;
				if(label[0] != '\0')
					fnp->label = StringSave(label);
				grp = gcp->rdp;
				for(i=0; (grp!=NULL) && i<gcp->num_interval; ++i)
				{
					i_left = grp->left;
					i_right = grp->right;
					if(!(left > i_right || right < i_left))
					{
						new = MemNew(sizeof(IvalNode));
						new->gr.left = MAX(left, i_left) - ins_len + gap_len;
						new->gr.right = MIN(right, i_right) - ins_len + gap_len;
						new->gr.strand = grp->strand;
						ValNodeAddPointer(&(fnp->interval), 0, new);
					}
					++grp;
				}
			}
			left = right +1;
			if(asp->type == INS_SEG)
				ins_len += seglen;
		}
		else
			gap_len += (asp->gr.right - asp->gr.left +1);
	}
	return TRUE;
}
	
		
/******************************************************************
*
*	CollectFeatureForAlignNode(slp, anp, csop)
*	collect feature for the alignment
*	slp: the target Seq-loc
*	anp: the AlignNode belong to the target Seq-loc
*	csop: the option for gathering the features
*	
******************************************************************/			
NLM_EXTERN Boolean CollectFeatureForAlignNode(SeqLocPtr slp, AlignNodePtr anp, CollectSeqOptionPtr csop)
{
	GatherScope gs;
	AlignFeat af;
	BioseqPtr bsp;
	
	if(slp == NULL || anp == NULL || csop == NULL)
		return FALSE;
		
	if(anp->seq_entityID == 0)
		return FALSE;
	bsp = BioseqLockById(SeqLocId(slp));


	MemSet((Pointer)&gs, 0, sizeof (GatherScope));
	gs.get_feats_location = TRUE;
	gs.get_feats_product =( bsp->mol == Seq_mol_aa);
	MemSet((Pointer)(gs.ignore), (int)TRUE, (size_t)(OBJ_MAX)*sizeof(Boolean));

	gs.ignore[OBJ_SEQANNOT] = FALSE;
	gs.ignore[OBJ_SEQFEAT] = FALSE;

	gs.nointervals = FALSE;	/*need to recalculate the intervals*/
	/* gs.seglevels = 1;
	gs.seglevels = 1;
	gs.stop_on_annot = TRUE;*/
	gs.ignore_top = FALSE;
	gs.currlevel = 0;
	gs.offset = anp->extremes.left;
	gs.target = slp;

	af.anp = anp;
	af.csop = csop;
	af.omp = ObjMgrGet();
	af.filter_level = 0;

	GatherEntity(anp->seq_entityID, (Pointer)(&af), satcollfunc, &gs);
	BioseqUnlock(bsp);
	return TRUE;
}


/******************************************************************
*
*	CollectFeatureForAlign(slp, anp, featureOrder, groupOrder)
*	collect feature for the alignment
*	slp: the target Seq-loc
*	anp: the AlignNode belong to the target Seq-loc
*	featureOrder: the order of features
*	groupOrder: the order of the groups
*	it takes the anp->seq_entityID and searches for the features
*	
******************************************************************/			

static Boolean CollectAlignFeature(SeqLocPtr slp, AlignNodePtr anp, Uint1Ptr featureOrder, Uint1Ptr groupOrder, Uint1Ptr flabel_format) 
{
	CollectSeqOption cs_option;
	Boolean show_feature, collect = FALSE;
	Int2 i;
	ValNode vn;
	
	if(featureOrder == NULL || groupOrder == NULL || slp == NULL || anp == NULL)
		return FALSE;
	
	cs_option.nointerval = FALSE;
	cs_option.slabel_format = PRINTID_TEXTID_ACCESSION;
	cs_option.seglevels = 0;
	cs_option.label_size = 10;
	for( i =0; i<FEATDEF_ANY; ++i)	/*for checking the features to load*/
	{
		show_feature = (featureOrder[i] != 0);
		cs_option.features[i] = show_feature;
		if(show_feature)
			collect = TRUE;
	}
	if(collect)
	{
		if(flabel_format == NULL)
			MemSet((Pointer)(cs_option.flabel_format), OM_LABEL_CONTENT, (size_t)FEATDEF_ANY*sizeof(Uint1));
		else
			MemCopy(&(cs_option.flabel_format), &flabel_format, (size_t)FEATDEF_ANY*sizeof(Uint1));
		CollectFeatureForAlignNode(slp, anp, &cs_option);
		vn.choice = OBJ_SEQALIGN;
		vn.data.ptrvalue = anp;
		vn.next = NULL;
		SortAlignmentFeature(&vn, featureOrder, groupOrder);
		return TRUE;
	}
	else
		return FALSE;
}

NLM_EXTERN Boolean CollectFeatureForAlign(SeqLocPtr slp, AlignNodePtr anp, Uint1Ptr featureOrder, Uint1Ptr groupOrder) 
{
	return CollectAlignFeature(slp, anp, featureOrder, groupOrder, NULL);
}

/******************************************************************
*
*	SortAlignmentFeature(anp_node, featureOrder, groupOrder)
*	sort the list of FeatNode in aligned segment (asp->cnp) to the 
*	proper order of featureOrder and groupOrder
*
*******************************************************************/
NLM_EXTERN void SortAlignmentFeature(ValNodePtr anp_node, Uint1Ptr featureOrder, Uint1Ptr groupOrder)
{
	AlignNodePtr anp;
	AlignSegPtr asp;
	
	while(anp_node)
	{
		if(anp_node->choice != OBJ_SEQANNOT)
		{
			anp = anp_node->data.ptrvalue;
			for(asp = anp->segs; asp !=NULL; asp = asp->next)
					if(asp->cnp !=NULL)
						asp->cnp = SortFeatNode(asp->cnp, featureOrder, groupOrder);
		}
		anp_node = anp_node->next;
	}
}
		

static SeqPortPtr make_current_seqport(SeqLocPtr masterloc, Int4 offset, Uint1 code)
{
	SeqLocPtr slp;
	Int4 start, stop;
	Uint1 strand;
	SeqPortPtr spp;


	start = SeqLocStart(masterloc);
	stop = SeqLocStop(masterloc);
	strand = SeqLocStrand(masterloc);

	if(strand == Seq_strand_minus)
		stop -= offset;
	else
		start += offset;
	slp = SeqLocIntNew(start, stop, strand, SeqLocId(masterloc));
	spp = SeqPortNewByLoc(slp, code);
	SeqLocFree(slp);

	return spp;
}


static void add_int_to_node (ValNodePtr PNTR head, ValNodePtr PNTR prev, Int4 val, Uint1 choice)
{
	ValNodePtr curr;

	curr = ValNodeNew(NULL);	
	curr->choice = choice;
	curr->data.intvalue = val;

	if(*prev == NULL)
		*head = curr;
	else
		(*prev)->next = curr;
	*prev = curr;
}


static Boolean CollectMismatchForAlign(AlignNodePtr anp, SeqLocPtr masterloc, BioseqPtr bsp, Int4 offset)
{
	
	AlignSegPtr asp;
	SeqPortPtr spp, mspp;
	Uint1 code;
	Uint1 res, mres;
	Int4 start = 0, stop = 0;
	Uint1 strand;
	Int4 current_pos;
	Int4 seglen, j;
	ValNodePtr prev;


		
	if(anp->is_master || SeqIdForSameBioseq(SeqLocId(masterloc), anp->sip))
		return FALSE;
	if(bsp->mol == Seq_mol_aa)
		code = Seq_code_ncbieaa;
	else
		code = Seq_code_iupacna;
	/* mspp = SeqPortNewByLoc(masterloc, code); */

	current_pos = anp->seqpos;
	if(anp->seqpos < 0)
	{
		strand = Seq_strand_minus;
		if(ABS(anp->seqpos) < bsp->length-1)
			anp->extremes.l_trunc = TRUE;
	}
	else
	{
		strand = Seq_strand_plus;
		if(anp->seqpos > 0)
			anp->extremes.l_trunc = TRUE;
	}
	for(asp = anp->segs; asp !=NULL; asp = asp->next)
	{
		if(asp->type == INS_SEG)
			seglen = asp->gr.right;
		else
			seglen = asp->gr.right - asp->gr.left + 1;
		switch(asp->type)
		{
			case GAP_SEG:
				break;
				
			case INS_SEG:
				current_pos += seglen;
				break;
				
			case REG_SEG:
			case DIAG_SEG:
				prev = get_last_node (asp->mismatch);
				if(strand == Seq_strand_minus)
				{
					stop = - current_pos;
					start = stop - (seglen-1);
				}
				else
				{
					start = current_pos;
					stop = start + (seglen -1);
				}
				spp = SeqPortNew(bsp, start, stop, strand, code);
				
				mspp = make_current_seqport(masterloc, (asp->gr.left - offset), code);
				/* SeqPortSeek(mspp, (asp->gr.left - offset), SEEK_SET); */

				for(j =0; j<seglen; ++j)
				{
					res = SeqPortGetResidue(spp);
					mres = SeqPortGetResidue(mspp);
					while(res == SEQPORT_EOS || res == SEQPORT_VIRT)
						res = SeqPortGetResidue(spp);
					while(mres == SEQPORT_EOS || mres == SEQPORT_VIRT)
						mres = SeqPortGetResidue(mspp);
					if(IS_ALPHA(res) && IS_ALPHA(mres))
					{
						if(res != mres)
						{
							if(bsp->mol != Seq_mol_aa && !StrChr("acgtACGT", res))
								add_int_to_node (&(asp->mismatch), &prev, (j+asp->gr.left), MISMATCH_AMB);
							else
								add_int_to_node (&(asp->mismatch), &prev, (j+asp->gr.left), MISMATCH_LINE);
							/* ValNodeAddInt(&(asp->mismatch), 0, (j+asp->gr.left)); */
						}
					}
					else if(res == SEQPORT_EOF || mres == SEQPORT_EOF)
						break;
					
				}
				current_pos += seglen;
				
				SeqPortFree(spp);
				SeqPortFree(mspp);
				break;
				
			default:
				break;
		}
		/*current_pos += seglen;*/
	}
	/* SeqPortFree(mspp); */
	if(strand == Seq_strand_minus)
	{
		if(start > 0)
			anp->extremes.r_trunc = TRUE;
	}
	else
	{
		if(stop < bsp->length-1)
			anp->extremes.r_trunc = TRUE;
	}
	
	return TRUE;
}


static AlignBlockPtr make_one_block(SeqRangePtr srp, Int4 seq_start, Int4 seq_stop, Int4 left, Int4 right, Int2 order, AlignNodePtr anp)
{
	AlignBlockPtr abp;
	Int4 off_left, off_right;
	
	if(srp == NULL || anp == NULL)
		return NULL;
	if(srp->start > seq_stop || srp->stop < seq_start)
		return NULL;

	if(srp->strand == Seq_strand_minus)
	{
		off_left = MAX(0, (srp->stop - seq_stop));
		off_right = MAX(0, (seq_start - srp->start));
	}
	else
	{
		off_left = MAX(0, (seq_start - srp->start));
		off_right = MAX(0, (srp->stop - seq_stop));
	}
	
	abp = MemNew(sizeof(AlignBlock));
	abp->gr.left = left + off_left;
	abp->gr.right = right - off_right;
	abp->gr.strand = 0;
	if(abp->gr.left == anp->extremes.left&& anp->extremes.strand == Seq_strand_minus)
		abp->gr.strand = Seq_strand_minus;
	if(abp->gr.right == anp->extremes.right && anp->extremes.strand == Seq_strand_plus)
		abp->gr.strand = Seq_strand_plus;
	abp->order = order;
	return abp;
}


static AlignBlockPtr link_align_blocks(AlignBlockPtr PNTR head, AlignBlockPtr new)
{
	AlignBlockPtr curr;

	if(*head == NULL)
		*head = new;
	else
	{
		curr = *head;
		while(curr->next != NULL)
			curr = curr->next;
		curr->next = new;
	}
	return new;
}


static Boolean make_blocks(AlignDataPtr adp, Int4 seq_start, Int4 seq_stop, Int2 order, AlignNodePtr anp)
{
	AlignBlockPtr abp = NULL;
	AlignRangePtr arp;
	

	if(adp == NULL || anp == NULL)
		return FALSE;
	if(adp->arp == NULL)
	{
		abp = make_one_block(&(adp->seqends), seq_start, seq_stop, anp->extremes.left, anp->extremes.right, order, anp);
		if(abp != NULL)
			link_align_blocks(&(anp->blocks), abp);
	}
	else
	{
		for(arp = adp->arp; arp != NULL; arp = arp->next)
		{
			if(arp->segtype == REG_SEG)
			{
				abp = make_one_block(&(arp->sr), seq_start, seq_stop, arp->gr.left, arp->gr.right, order, anp); 
				if(abp != NULL)
				{
					link_align_blocks(&(anp->blocks), abp);
					break;
				}
			}
		}
	}
	return (abp != NULL);
}
		

static Boolean sequence_has_alignment(ValNodePtr align_id_list, SeqIdPtr sip)
{
	Uint1 kludge_factor;
	Int4 gi;
	
	
	kludge_factor = (Uint1)get_kludge_factor(sip, &gi);
	if(gi == -1)
		return FALSE;
		
	while(align_id_list)
	{
		if(align_id_list->choice == kludge_factor)
		{
			if(align_id_list->data.intvalue == gi)
				return TRUE;
		}
		
		align_id_list = align_id_list->next;
	}
	
	return FALSE;
}



static Boolean add_sequence_alignment_info(ValNodePtr align_id_list, ValNodePtr anp_list)
{

	AlignNodePtr anp;
	
	if(align_id_list == NULL || anp_list == NULL)
		return FALSE;
		
	while(anp_list)
	{
		if(anp_list->choice != OBJ_SEQANNOT)
		{
			anp = anp_list->data.ptrvalue;
			if(anp->seq_has_align == FALSE)
				anp->seq_has_align = sequence_has_alignment(align_id_list, anp->sip);
		}
		anp_list = anp_list->next;
	}
	
	return TRUE;
}


typedef struct temp_bsp_data{
	BioseqPtr bsp;
	Uint4 itemID;
	Boolean found;
}TempBsp, PNTR TempBspPtr;

static Boolean bspcountfunc(GatherContextPtr gcp)
{
	TempBspPtr tbp;
	BioseqPtr bsp;

	if(gcp == NULL)
		return FALSE;
	tbp = (TempBspPtr)(gcp->userdata);
	if(tbp == NULL || tbp->bsp == NULL)
		return FALSE;
	if(tbp->found)
		return FALSE;
	bsp = (BioseqPtr)(gcp->thisitem);
	if(tbp->bsp == bsp)
	{
		tbp->itemID= gcp->itemID;
		tbp->found = TRUE;
		return FALSE;
	}
	else
		return TRUE;
}
	
/*****************************************************************
*
*       given the bioseq and its entityID, figure out the 
*       itemID for the Bioseq
*
*****************************************************************/
NLM_EXTERN Uint4 get_bioseq_itemID(BioseqPtr bsp, Uint2 entityID)
{
	GatherScope gs;
	TempBsp tb;


	if(bsp == NULL || entityID == 0)
		return 0;

	tb.bsp = bsp;
	tb.itemID= 0;
	tb.found = FALSE;

	MemSet((Pointer)(&gs), 0, sizeof(GatherScope));
	MemSet((Pointer)(gs.ignore), (int)(TRUE), (size_t)OBJ_MAX * sizeof(Boolean));
	gs.ignore[OBJ_BIOSEQ] = FALSE;
	GatherEntity(entityID, &tb, bspcountfunc, &gs);

	return tb.itemID;
}
	
	
static Boolean stop_collecting_alignment(ValNodePtr anp_list, Int4 max_num)
{
	Int2 i;

	i = 0 ;
	while(anp_list)
	{
		++i;
		if(i > max_num)
		{
			if(anp_list->next == NULL)
				return TRUE;
		}
		anp_list = anp_list->next;
	}

	return FALSE;
}

static void FindCloneCallback(SeqEntryPtr sep, Pointer data, Int4 index, Int2 indent)
{
	AlignNodePtr anp;
	BioseqPtr bsp;
	BioseqSetPtr bssp;
	ValNodePtr descr;
	ValNodePtr curr;
	OrgRefPtr orp;
	ValNodePtr mod;
	CharPtr str;

	BioSourcePtr source;
	SubSourcePtr ssp;

	anp = (AlignNodePtr)data;
	if(anp->clone_id != NULL)
		return;


	if(sep->choice == 1)
	{
		bsp = (BioseqPtr)(sep->data.ptrvalue);
		descr = bsp->descr;
	}
	else
	{
		bssp = (BioseqSetPtr)(sep->data.ptrvalue);
		descr = bssp->descr;
	}

	for(curr = descr; curr != NULL; curr = curr->next)
	{
		if(curr->choice == Seq_descr_source)
		{
			source = curr->data.ptrvalue;
			/* search for /chromosome= */
			for(ssp = source->subtype; ssp != NULL; ssp = ssp->next)
			{
				if(ssp->subtype == 3 && ssp->name != NULL)
				{	/* 3 == clone */
					anp->clone_id = StringSave(ssp->name);
					return;
				}
			}
		}
		else if(curr->choice == Seq_descr_org)
		{
			orp = curr->data.ptrvalue;
			if(orp)
			{
				for(mod = orp->mod; mod != NULL; mod = mod->next)
				{
					str = mod->data.ptrvalue;
					if(StringNCmp(str, "clone=", 6) == 0)
					{
						anp->clone_id = StringSave(str+6);
					}
				}
			}
		}
	}
}

static Boolean LoadIndexLabelBlock(AlignNodePtr anp)

{
	AlignBlockPtr abp;
	SeqIdPtr sip;
	ObjectIdPtr oip;
	DbtagPtr db_tag;

	sip = anp->sip;
	if(sip == NULL || sip->choice != SEQID_GENERAL)
		return FALSE;

	db_tag = sip->data.ptrvalue;
	if(db_tag == NULL || db_tag->db == NULL)
		return FALSE;
	oip = db_tag->tag;
	if(oip== NULL || oip->id <= 0)
		return FALSE;
	
	
	abp = MemNew(sizeof(AlignBlock));
	MemCopy((Pointer)(&(abp->gr)), (Pointer)(&(anp->extremes)), sizeof(GatherRange));
	abp->order = (Uint2)oip->id;
	anp->blocks = abp;

	anp->label = StringSave(db_tag->db);
	return TRUE;
}


/***********************************************************************
*
*	coll_align_data(align, m_sip, adp, clone, featureOrder, show_mismatch, 
*	itemID, entityID, anp_list)
*	convert all the alignment data stored in adp into the drawing 
*	structure AlignNode
*
*	align: the current Seq-align
*	m_sip: the master sequence, also the target sequence in gather
*	adp: the collected structure from gather
*	clone: for filtering out unwanted clone type. set to NULL for all
*	featureOrder: for features to be displayed together with alignment
*	show_mismatch: show the mismatched base-pairs
*	itemID: itemID for the current align
*	entityID: entityID for the Seq-entry of m_sip
*
*	NOTE: if either show_mismatch or featureOrder is selected, it puts 
*	the newly retrieved sequence for itemID and entityID 
*	
*	anp_list: the list of AlignNodePtr to stored the coverted result
*
************************************************************************/	

static Boolean coll_align_data(SeqAlignPtr align, Uint1 index, AlignDataPtr adp, CollectAlignOptionPtr caop, Uint4 itemID, Int2 entityID, Int2 itemType, SeqLocPtr mloc, ValNodePtr PNTR anp_list, ValNodePtr PNTR prev)
{
	Char label[41];
	
	SeqLocPtr slp, extloc;
	SeqIdPtr sip;
	Boolean feat;	/*collect any features?*/
	Boolean show_mismatch;
	Boolean is_master;
	
	AlignRangePtr arp;
	AlignNodePtr anp;
	AlignSegPtr asp, pasp;

	BioseqPtr bsp;
	Uint2 order;
	Int4 e_left = 0, e_right = 0;
	Boolean match_seg;
	Int2 label_size;
	Int4 offset = 0;
	SeqIdPtr best_id;
	SeqEntryPtr sep;

#ifdef NONO
        if(align->segtype == 5) /* Discontinuous aligment not collected */
            return TRUE;
#endif
	label_size = MIN(caop->label_size, 100);
	feat = caop->show_feature;
	if(align->segtype == 3)	/*for std-seg, no feature or mismatch*/
	{
		show_mismatch = FALSE;
		feat = FALSE;
	}
	else
		show_mismatch = caop->show_mismatch;

	if(align->segtype == 2)	/*for Dense-seg or Dense-diag only*/
	{
		if(caop->align_num != -1)
		{
			/* if(stop_collecting_alignment(*anp_list, caop->align_num)) */
			if(caop->curr_align_num > caop->align_num)
			{
				/* ErrPostEx (SEV_WARNING, 0, 0, "The top %ld alignments are displayed. The rest are truncated", caop->align_num);
				return FALSE; */
				feat = FALSE;
				show_mismatch = FALSE;
			}
		}
	}

	while(adp)
	{
		++(caop->curr_align_num);
		anp = MemNew(sizeof (AlignNode));
		anp->pop_sap = NULL;
		anp->itemID = itemID;
		anp->entityID = entityID;
		anp->seqOrder = adp->order;
		anp->chain = adp->chain;
		anp->seq_has_align = FALSE;
		anp->index = index;
		anp->keep_label = FALSE;
		MemCopy(&(anp->extremes), &(adp->extremes), sizeof(GatherRange));

		if(adp->seqends.strand == Seq_strand_minus)
			anp->seqpos = -(adp->seqends.stop);
		else
			anp->seqpos = adp->seqends.start;
			
		pasp = NULL;
		for(arp = adp->arp; arp !=NULL; arp = arp->next)
		{
			asp = MemNew(sizeof(AlignSeg));
			MemCopy(&(asp->gr), &(arp->gr), sizeof(GatherRange));
			asp->type = arp->segtype;
			if(asp->type == INS_SEG)
				asp->ins_pos = asp->gr.left;
			if(pasp == NULL)
				anp->segs = asp;
			else
				pasp->next = asp;
			pasp = asp;
		}
		
		if(index == ALIGN_NON_INDEX  && (feat|| show_mismatch))
		{
			bsp = BioseqLockById(adp->sip);
			if(bsp != NULL)
			{
				if(adp->sip->choice == SEQID_GI)
				{
					sep = SeqEntryFind(adp->sip);
					if(sep != NULL)
						SeqEntryExplore(sep, (Pointer)anp, FindCloneCallback);
				}
				if(bsp->hist && bsp->hist->assembly)
					anp->seq_has_align = TRUE;
				anp->seq_entityID = ObjMgrGetEntityIDForPointer((Pointer)bsp);
				anp->bsp_itemID = get_bioseq_itemID(bsp, anp->seq_entityID);
				best_id = SeqIdFindBest(bsp->id, SEQID_GI);
				if(best_id == NULL)
					best_id = bsp->id;
				anp->sip = SeqIdDup(best_id);
				
				if(feat)
				{
					if(BioseqHasFeature(bsp))
						caop->csop->seglevels = 0;
					else
						caop->csop->seglevels = 1;
					slp = SeqLocIntNew(adp->seqends.start, adp->seqends.stop, adp->seqends.strand, best_id);
					CollectFeatureForAlignNode(slp, anp, caop->csop);
					SeqLocFree(slp);
				}
				if(show_mismatch && bsp->repr != Seq_repr_map)
					CollectMismatchForAlign(anp, mloc, bsp, offset+caop->graphic_offset);

				BioseqUnlock(bsp);
			}
			/* else
				printf("fail to get sequence for %ld\n", adp->sip->data.intvalue);  */
		}
		if(anp->sip == NULL)
			anp->sip = SeqIdDup(adp->sip);
			


		/*collecting matching piece to show the content of a segmented sequence*/			
		if(caop->segloc != NULL && index == ALIGN_NON_INDEX )	
		{
			is_master = SeqIdForSameBioseq(adp->sip, SeqLocId(mloc));
			if(is_master)
			{
				e_left = 0;
				e_right = -1;
			}
			order = 0;
			for(extloc = caop->segloc; extloc != NULL; extloc = extloc->next)
			{
				++order;
				match_seg = FALSE;
				if(is_master)
				{
					e_right += SeqLocLen(extloc);
					match_seg = TRUE;
				}
				else
				{
					sip = SeqLocId(extloc);
					match_seg = SeqIdForSameBioseq(sip, anp->sip);
						
				}
				if(match_seg)
				{
					if(!is_master)
					{
						e_left = SeqLocStart(extloc);
						e_right = SeqLocStop(extloc);
					}
					match_seg = make_blocks(adp, e_left, e_right, order, anp);
				}
				if(is_master)
					e_left = e_right +1;
				if(match_seg)
					if(e_right > adp->seqends.stop)
						break;
			}
		}

		/*store the index information in the blocks*/
		if(index != ALIGN_NON_INDEX)
			LoadIndexLabelBlock(anp);
		else if(label_size > 0)
		{
			if(MuskSeqIdWrite (anp->sip, label, label_size, caop->slabel_format, TRUE, TRUE)) {
			    SeqIdPtr	gilist = GetUseThisGi(align);
			    if (gilist) {
				Char	buf[1024];
				sprintf(buf, "%d", gilist->data.intvalue);
				anp->label = StringSave(buf);
				anp->keep_label = TRUE;
				gilist = SeqIdSetFree(gilist);
			    } else {
				anp->label = StringSave(label);
			    }
			}
		}

		link_data_for_collect (anp_list, prev, (Pointer)anp, (Uint1)itemType);
		adp = adp->next;
	}
	return TRUE;
}
	
static Boolean does_annot_match_target (SeqLocPtr target, SeqAnnotPtr annot)
{
    SeqAlignPtr sap;
    SeqIdPtr sip;
    DenseDiagPtr ddp;
    DenseSegPtr dsp;
    StdSegPtr ssp;
    SeqIdPtr target_id;
    SeqLocPtr slp;
    Boolean result;

    if(target == NULL || annot == NULL || annot->type != 2)
        return FALSE;
    target_id = SeqLocId(target);
    sap = annot->data;

    if(sap == NULL)
        return FALSE;

    switch(sap->segtype) {
    case 1:
        ddp = sap->segs;
        for(sip = ddp->id; sip != NULL; sip = sip->next)
            if(SeqIdForSameBioseq(sip, target_id))
                return TRUE;
        break;
    case 2:
        dsp = sap->segs;
        for(sip = dsp->ids; sip != NULL; sip = sip->next)
            if(SeqIdForSameBioseq(sip, target_id))
                return TRUE;
        break;
    case 3:
        ssp = sap->segs;
        for(slp = ssp->loc; slp != NULL; slp = slp->next)
            if(SeqIdForSameBioseq(SeqLocId(slp), target_id))
                return TRUE;
        break;
    case 5:

        annot->data = (SeqAlignPtr) sap->segs;
        result =  does_annot_match_target (target, annot);
        annot->data = sap;
        return result;

    default:
        break;
    }
    
    return FALSE;
}
			
static Boolean collalignfunc(GatherContextPtr gcp)
{
	SeqAnnotPtr annot;
	CollectHeaderPtr chp;
	AnnotInfoPtr info;
	SeqAlignPtr align;
	Uint1 annot_type;
	
	
	chp= (CollectHeaderPtr)(gcp->userdata);

	switch(gcp->thistype)
	{
	case OBJ_SEQANNOT:
		annot = (SeqAnnotPtr)(gcp->thisitem);
		if(annot->type == 2)
		{
		chp->caop->curr_align_num = 0;
		chp->load_align = TRUE;
		chp->index = 0;
		if(!chp->take_all_annot)
		{
			if(!is_annot_for_hist_alignment(annot))
			{
				chp->load_align = FALSE;
				return TRUE;
			}
		}
		info = MemNew(sizeof(AnnotInfo));
		info->annotDB[0] = '\0';
		info->displayOrder = get_align_annot_qual(annot, info->annotDB, 20, &annot_type);
		info->annot_type = annot_type;
		if(annot_type == ANNOT_BLAST)
			info->blast_type = info->displayOrder;
		/*load the index values*/
		if(info->annotDB[0] != '\0')
		{
			if(StringCmp(info->annotDB, "Sequencing Status") == 0)
				chp->index = ALIGN_SEQ_INDEX;
			else if(StringCmp(info->annotDB, "Mapping Status") == 0)
				chp->index = ALING_MAP_INDEX;
		}

		/* Eric Green's un-aligned guys */
		if(annot_type == ANNOT_CONSIST)
		{
			info->consistent = info->displayOrder;
			if(info->consistent == ALIGN_UNKNOWN)
			{	/*un-aligned guys, check if the Seq-loc matches */
				if(!does_annot_match_target (chp->slp, annot))
					info = MemFree(info);
				chp->load_align = FALSE;
			}
		}
		else if(annot_type == ANNOT_FISH)
			info->is_fish_align= TRUE;
		if(info != NULL)
		{
			info->entityID = gcp->entityID;
			info->itemID = gcp->itemID;
			link_data_for_collect (&(chp->aligns), &(chp->prev_align), (Pointer)info, (Uint1)(gcp->thistype));
		}
		}
		return TRUE;

	case OBJ_SEQALIGN:
		align = (SeqAlignPtr)(gcp->thisitem);
		if(chp->load_align)
			return coll_align_data(align, chp->index, gcp->adp, chp->caop, gcp->itemID, gcp->entityID, gcp->thistype, chp->slp, &(chp->aligns), &(chp->prev_align));
		else
			return TRUE;
	case OBJ_SEQHIST_ALIGN:
		align = (SeqAlignPtr)(gcp->thisitem);
		return coll_align_data(align, chp->index, gcp->adp, chp->caop, gcp->itemID, gcp->entityID, gcp->thistype, chp->slp, &(chp->aligns), &(chp->prev_align));
	case OBJ_SEQHIST:
		chp->caop->curr_align_num = 0;
		return TRUE;
	default:
		return TRUE;
	}
}

	
/*********************************************************************
*
*	CollectItemForAlignment(slp, entityID, left, caop)
*	return a list of AlignNode for the alignment in the target seqloc
*	slp: the target Seq-loc
*	entityID: the entity source for collection
*	left: the left offset on the graphic
*	caop: the option for alignment collection
*
**********************************************************************/
NLM_EXTERN ValNodePtr CollectItemForAlignment(SeqLocPtr slp, Uint2 entityID, Int4 left, CollectAlignOptionPtr caop, Boolean take_all_annot)
{
	GatherScope gs;
	CollectHeader ch;
	BioseqPtr mbsp;
	SeqIdPtr sip;
	ValNodePtr align_id_list = NULL;
	SeqLocPtr curr, next;

	if(slp == NULL || entityID == 0 || caop == NULL)
		return NULL;

	sip = SeqLocId(slp);
	ch.aligns = NULL;
	ch.caop = caop;
	ch.take_all_annot = take_all_annot;
	ch.load_align = TRUE;
	ch.prev_feat = NULL;
	ch.prev_align = NULL;
	ch.index = 0;
	/*ch.slp = slp;*/
	

	MemSet((Pointer)&gs, 0, sizeof (GatherScope));
	MemSet((Pointer)(gs.ignore), (int)TRUE, (size_t)(OBJ_MAX)*sizeof(Boolean));

	if(caop->only_history == FALSE)
	{
		gs.ignore[OBJ_SEQANNOT] = FALSE;
		gs.ignore[OBJ_SEQALIGN] = FALSE;
	}
	gs.ignore[OBJ_SEQHIST] = FALSE;
	gs.ignore[OBJ_SEQHIST_ALIGN] = FALSE;
	

	gs.nointervals = caop->nointerval;
	gs.seglevels = 0;
	gs.currlevel = 0;
	gs.split_packed_pnt = FALSE;
	gs.mapinsert = caop->map_insert;


	curr = slp;
	while(curr)
	{
		next = curr->next;
		curr->next = NULL;
		gs.offset = left;
		gs.target = curr;
		ch.slp = curr;
		caop->graphic_offset = left;
		GatherEntity(entityID, (Pointer)(&ch), collalignfunc, &gs);
		left += SeqLocLen(curr);
		curr->next = next;
		curr = next;
	}
	
	if(ch.aligns != NULL)
	{
		mbsp = BioseqLockById(sip);
		align_id_list = get_seqids_with_alignment(mbsp);
		if(align_id_list != NULL)
		{
			add_sequence_alignment_info(align_id_list, ch.aligns);
			ValNodeFree(align_id_list);
		}
		BioseqUnlock(mbsp);
	}
			
	
	return ch.aligns;
}


static void merge_master_head(ValNodePtr head, ValNodePtr new_node)
{
	AlignNodePtr anp_head, anp;
	AlignSegPtr asp;
	AlignBlockPtr block;

	if(head == NULL || new_node == NULL)
		return;
	anp_head = head->data.ptrvalue;
	anp = new_node->data.ptrvalue;
	
	if(anp_head == NULL || anp == NULL)
		return;

	anp_head->extremes.right = anp->extremes.right;
	asp = anp_head->segs;
	if(asp == NULL)
		anp_head->segs = anp->segs;
	else
	{
		while(asp->next != NULL)
			asp = asp->next;
		asp->next = anp->segs;
	}
	anp->segs = NULL;

	block = anp_head->blocks;
	if(block == NULL)
		anp_head->blocks = anp->blocks;
	else
	{
		while(block->next != NULL)
			block = block->next;
		if(anp->blocks != NULL)
			block->gr.strand = 0;
		block->next = anp->blocks;
	}
	anp->blocks = NULL;

	FreeAlignNode(new_node);
}
	

/*****************************************************************************
*
*	cllect_master_align_node(m_loc, featureOrder, groupOrder)
*	in the master-slave alignment, a fake Seq-align is created for the 
*	master sequence where the master is aligned to itself. The AlignNode
*	can be computed for this faked alignment. When this is done, the fake
*	Seq-align will be freed
*
*	m_loc: the Seq-loc for the master sequence
*	featureOrder: the selected features
*
*******************************************************************************/
NLM_EXTERN ValNodePtr collect_master_align_node(CollectAlignOptionPtr caop, SeqLocPtr m_loc, Uint1 obj_type, Uint2 entityID)
{
	SeqAlignPtr align;
	DenseSegPtr dsp;
	SeqIdPtr m_sip;
	ValNodePtr anp_node, anp_head = NULL, curr;
	ValNodePtr prev = NULL;
	AlignNodePtr anp;
	AlignDataPtr adp;
	Int4 left =0;
	Boolean show_mismatch;

	if(caop == NULL || m_loc == NULL)
		return NULL;
		
	show_mismatch = caop->show_mismatch;
	caop->show_mismatch = FALSE;
   while(m_loc)
   {
	m_sip = SeqLocId(m_loc);

	dsp = DenseSegNew();
	dsp->dim = 2;
	dsp->numseg =1;
	dsp->strands = MemNew((size_t)2*sizeof(Uint1));
	dsp->strands[0] = Seq_strand_plus;
	dsp->strands[1] = SeqLocStrand(m_loc);
	dsp->ids = SeqIdDup(m_sip);
	dsp->ids->next = SeqIdDup(m_sip);
	dsp->starts = MemNew((size_t)2*sizeof(Int4));
	dsp->starts[0] = SeqLocStart(m_loc);
	dsp->starts[1] = SeqLocStart(m_loc);
	dsp->lens = MemNew(sizeof(Int4));
	dsp->lens[0] = SeqLocLen(m_loc);

	align = SeqAlignNew();
	align->type = 3;
	align->segtype = 2;
	align->dim = 2;
	align->segs = dsp;

	anp_node = NULL;
	adp = gather_align_data(m_loc, align, left, TRUE, TRUE);
	if(adp !=NULL)
	{
		coll_align_data(align, 0, adp, caop, 0, entityID, obj_type, m_loc, &anp_node, &prev);
		FreeAlignData(adp);
	}
	if(anp_head == NULL)
		anp_head = anp_node;
	else
		merge_master_head(anp_head, anp_node);
	SeqAlignFree(align);
	left = SeqLocLen(m_loc);
	m_loc = m_loc->next;
   }
   for(curr = anp_head; curr != NULL; curr = curr->next)
   {
	anp = curr->data.ptrvalue;
	anp->use_seq_ids = TRUE; /*use the Seq-id as the itemID for graphic display*/
	anp->is_master = TRUE;
   }
	caop->show_mismatch = show_mismatch;
   return anp_head;
}

NLM_EXTERN Boolean set_option_for_collect_align(CollectAlignOptionPtr caop, Int2 label_size, Uint1 style)
{
	if(caop == NULL)
		return FALSE;
		
	MemSet((Pointer)caop, 0, sizeof(CollectAlignOption));
	if(style < COLLECT_HISTORY || style > COLLECT_FIXED)
	{
		Message(MSG_ERROR, "Illegal style for alignment display %d", (int)style);
		return FALSE;
	}
	
	caop->nointerval = FALSE;
	caop->label_size= label_size;
	if(style == COLLECT_MD || style == COLLECT_FIXED)
	{
		caop->only_history = FALSE;
		caop->map_insert = FALSE;
	}
	else
	{
		caop->only_history = TRUE;
		caop->map_insert = TRUE;
	}
	caop->map_graphic = (style != COLLECT_FIXED);
	caop->show_mismatch = (style != COLLECT_HISTORY);
	caop->show_feature = FALSE;
	caop->slabel_format = PRINTID_TEXTID_ACCESSION;
	caop->segloc = NULL;
	caop->align_num = DEFAULT_ALIGN_NUM;
	caop->graphic_offset = 0;
	return TRUE;
}

static Boolean alignment_are_blast_hits(BioseqPtr bsp)
{
	SeqAnnotPtr annot;
	Char label[101];
	Uint1 annot_type;

	if(bsp == NULL || bsp->annot == NULL)
		return FALSE;
	for(annot = bsp->annot; annot != NULL; annot = annot->next)
	{
		if(annot->type == 2)
		{
			label[0] = '\0';
			get_align_annot_qual(annot, label, 20, &annot_type);
			if(label[0] != '\0' && StringNCmp(label, "BLAST", 5) == 0)
				return TRUE;
		}
	}
	return FALSE;
}

NLM_EXTERN ValNodePtr collect_anpnode_with_option(CollectAlignOptionPtr caop, SeqLocPtr m_loc, Uint2 entityID, Int4 style, Uint1 itemType, Uint1Ptr f_order, Uint1Ptr g_order, Boolean take_all_annot)
{
	ValNodePtr anp_list = NULL, list;
	BioseqPtr mbsp;
	Uint1 featureOrder[FEATDEF_ANY];
	Uint1 groupOrder[FEATDEF_ANY];
	Int2 i;
	CollectSeqOptionPtr csop = NULL;
	ValNodePtr align_id_list = NULL;
	AlignNodePtr anp;
	Boolean show_feature;
	ValNodePtr prev = NULL;



	if(caop == NULL || m_loc == NULL || entityID == 0)
		return NULL;
	if(style < COLLECT_HISTORY || style > COLLECT_MD)
		return NULL;
	mbsp = BioseqLockById(SeqLocId(m_loc));
	if(mbsp == NULL)
		return NULL;

	if(mbsp->repr == Seq_repr_seg)
		caop->segloc  = (SeqLocPtr)(mbsp->seq_ext);
	else
		caop->segloc = NULL;

	show_feature = FALSE;
	if(style != COLLECT_HISTORY)
	{
		if(f_order != NULL && g_order != NULL)
		{
			MemCopy((Pointer)(featureOrder), (Pointer)f_order, (size_t)(FEATDEF_ANY* sizeof(Uint1)));
			MemCopy((Pointer)(groupOrder), (Pointer)g_order, (size_t)(FEATDEF_ANY* sizeof(Uint1)));
		}
		else	/*use the default features*/
		{
			if(mbsp->mol == Seq_mol_aa)
			{
				MemSet((Pointer)(featureOrder), 1, (size_t)(FEATDEF_ANY* sizeof(Uint1)));
				MemSet((Pointer)(groupOrder), 1, (size_t)(FEATDEF_ANY* sizeof(Uint1)));
				featureOrder[FEATDEF_BAD] = 0;
				/* featureOrder[FEATDEF_ANY] = 0; */ /* out of bounds */
				featureOrder[FEATDEF_PUB] = 0;
				featureOrder[FEATDEF_source] = 0;
				featureOrder[FEATDEF_NUM] = 0;
				featureOrder[FEATDEF_BIOSRC] = 0;
				featureOrder[FEATDEF_ORG] = 0;
				featureOrder[FEATDEF_CDS] =0;
				featureOrder[FEATDEF_PROT] =0;
			}
			else
			{
				MemSet((Pointer)(featureOrder), 0, (size_t)(FEATDEF_ANY* sizeof(Uint1)));
				MemSet((Pointer)(groupOrder), 0, (size_t)(FEATDEF_ANY* sizeof(Uint1)));
				featureOrder[FEATDEF_Imp_CDS] = 1;
				groupOrder[FEATDEF_Imp_CDS] = 1;
				featureOrder[FEATDEF_CDS] = 1;
				groupOrder[FEATDEF_CDS] = 1;
			}
		}

		csop = caop->csop;
		for(i =0; i<FEATDEF_ANY; ++i)
		{
			if(featureOrder[i] != 0)
			{
				csop->features[i] = TRUE;
				show_feature = TRUE;
			}
			else
				csop->features[i] = FALSE;
		}
	}
	else
		caop->show_mismatch = FALSE;



	if(style == COLLECT_MP)
	{
		if(csop->features[FEATDEF_repeat_region] == FALSE || 
			csop->features[FEATDEF_repeat_unit] == FALSE)
		{
			if(mbsp->repr == Seq_repr_seg || mbsp->repr == Seq_repr_raw 
				|| mbsp->repr == Seq_repr_const)
			{
	
				if(alignment_are_blast_hits(mbsp))
				{
					csop->features[FEATDEF_repeat_region] = TRUE;
					csop->features[FEATDEF_repeat_unit] = TRUE;
					csop->features[FEATDEF_repeat_region] = TRUE;
					csop->features[FEATDEF_repeat_unit] = TRUE;
					caop->show_feature = TRUE;
				}
			}
		} 
		anp_list = collect_master_align_node(caop, m_loc, itemType, entityID);
		if(anp_list == NULL)
		{
			BioseqUnlock(mbsp);
			Message(MSG_ERROR, "Fail to make AlignNode for the master sequence");
			return NULL;
		}
		/* if(caop->map_graphic == FALSE)
		{
			csop->features[FEATDEF_repeat_region] = FALSE;
			csop->features[FEATDEF_repeat_unit] = FALSE;
		} */

	}
	caop->show_feature = show_feature;
	
	
	list = CollectItemForAlignment(m_loc, entityID, 0, caop, take_all_annot);
	if(caop->no_sort == FALSE)
		list = SortAlignNode(list);
	ValNodeLink(&anp_list, list);
	if(style == COLLECT_MD)
	{
		for(list = anp_list; list != NULL; list = list->next)
		{
			if(list->choice != OBJ_SEQANNOT)
			{
				anp = list->data.ptrvalue;
				if(anp != NULL)
					anp->use_seq_ids = TRUE;
			}
		}
	}

		
	if(caop->show_feature)
		SortAlignmentFeature(anp_list, featureOrder, groupOrder);
	align_id_list = get_seqids_with_alignment(mbsp);
	if(align_id_list != NULL)
	{
		add_sequence_alignment_info(align_id_list, anp_list);
		ValNodeFree(align_id_list);
	}
	if(style == COLLECT_MP && caop->flat_insert)
		FlatAlignNode(anp_list);

	BioseqUnlock(mbsp);
	return anp_list;
}



/***************************************************************
*
*	CollAlignFromSeqAnnot(annot, m_loc, featureOrder, groupOrder, 
*	style,graphic)
*
*	collect the AlignNode for Seq-aligns stored in Seq-annot
*	annot: the Seq-annot
*	m_loc: the target sequence
*	left: the offset of the leftmost position
*	featureOrder, groupOrde: the features selected to be displayed together 
*	with alignment
*	style: the style of the display. Only valid for multiple-pairwise 
*	and multiple dimension for now
*	graphic: if TRUE, it is designed to show the display on graphic, 
*	so the mismatch data will be collected. Otherwise, it will not 
*	collect mismatch data
*
****************************************************************/
NLM_EXTERN ValNodePtr CollAlignFromSeqAnnot(SeqAnnotPtr annot, SeqLocPtr m_loc, Uint1Ptr featureOrder, Uint1Ptr groupOrder, Uint1 style, Boolean graphic, Boolean sort, Boolean flat_insert)
{
	Uint2 entityID;

	CollectAlignOption ca_option;
	CollectSeqOption cs_option;
	Int2 label_size = 32;

	if(annot->type !=2)	/*it is not an alignment*/
		return NULL;
		
	entityID = ObjMgrRegister(OBJ_SEQANNOT, (Pointer)annot);
	if(entityID == 0)
		return NULL;

		
	if(style == COLLECT_MP || style == COLLECT_MD)
	{
		set_option_for_collect_align(&ca_option, label_size, style);
		cs_option.nointerval = FALSE;
		cs_option.slabel_format = PRINTID_TEXTID_ACCESSION;
		MemSet((Pointer)&(cs_option.flabel_format), OM_LABEL_CONTENT, (size_t)FEATDEF_ANY * sizeof(Uint1));
		cs_option.label_size = label_size;
		cs_option.seglevels = 0;
		ca_option.csop = &cs_option;
		ca_option.no_sort = 1- sort;
		if(style == COLLECT_MP && flat_insert)
			ca_option.flat_insert = TRUE;
		else
			ca_option.flat_insert = FALSE;

		ca_option.only_history = FALSE;
		if(!graphic)
		{
			ca_option.show_mismatch = FALSE;
			ca_option.align_num = -1;
		}
		return collect_anpnode_with_option(&ca_option, m_loc, entityID, style, OBJ_SEQALIGN, featureOrder, groupOrder, TRUE);
	}
	else
		return NULL;


}
	
/*###################################################################
#
#	functions related to collect Seq-feat, Bioseq and Bioseq-seg
#
###################################################################*/


static void get_mapmarker_info(UserObjectPtr uop, Uint4Ptr extra, Uint2Ptr bin_order)
{
	ObjectIdPtr oip;
	Int4 val;
	Uint4 temp;
	UserFieldPtr ufp;

	temp = *extra;

	while(uop)
	{
		oip = uop->type;
		if(oip && oip->str != NULL)
		{
			if(StringCmp(oip->str, "MapMarkerInfo") == 0)
			{
				ufp = uop->data;
				while(ufp)
				{
					oip = ufp->label;
					if(StringCmp(oip->str, "Marker Type") == 0)
					{
						if(ufp->choice == 2)
						{
							val = ufp->data.intvalue;
							switch(val)
							{
								case FRAME_WORK:
									temp |= EXTRA_FRAME_WORK;
									break;
								case RECMIN:
									temp |= EXTRA_RECMIN;
									break;
								case LIKELY:
									temp |= EXTRA_LIKELY;
									break;
								case MDUP:
									temp |= EXTRA_MDUP;
									break;
								case DUP:
									temp |= EXTRA_DUP;
									break;

								case CONTIG_STS:
									temp |= EXTRA_CONTIG_STS;
									break;
								default:
									break;
							}
							*extra = temp;
						}
					}
					if(StringCmp(oip->str, "Bin Order") == 0)
					{
						if(ufp->choice == 2)
							*bin_order = (Uint2)(ufp->data.intvalue);
					}

					if(StringCmp(oip->str, "Marker Category") == 0)
					{
						if(ufp->choice == 2)
						{
							val = ufp->data.intvalue;
							switch(val)
							{
								case EG_YAC_END:
									temp |= EXTRA_YAC_END;
									break;
								case EG_RANDOME:
									temp |= EXTRA_RANDOM;
									break;
	
								case EG_GENETIC:
									temp |= EXTRA_GENETIC;
									break;
	
								case EG_GENE:
									temp |= EXTRA_GENE;
									break;
								case EG_EST:
									temp |= EXTRA_EST;
									break;
								case EG_MISC:
									temp |= EXTRA_MISC;
									break;
								default:
									break;
							}
						}
					}
					ufp = ufp->next;
				}
			}
			else if(StringCmp(oip->str, "Marker Category") == 0)
			{
				ufp = uop->data;
				while(ufp)
				{
					if(ufp->choice == 2)
					{
						val = ufp->data.intvalue;
						switch(val)
						{
							case EG_YAC_END:
								temp |= EXTRA_YAC_END;
								break;
							case EG_RANDOME:
								temp |= EXTRA_RANDOM;
								break;

							case EG_GENETIC:
								temp |= EXTRA_GENETIC;
								break;

							case EG_GENE:
								temp |= EXTRA_GENE;
								break;
							case EG_EST:
								temp |= EXTRA_EST;
								break;
							case EG_MISC:
								temp |= EXTRA_MISC;
								break;
							default:
								break;
						}
					}
					ufp = ufp->next;
				}
			}
		}
		uop = uop->next;
	}
	*extra = temp;
}
	

/*******************************************************************
*
*	ck_seqfeat_extra: check if there is  extra data, such as 
*	Genbank accessions assocated with a GeneRef or Medlines 
*	associated with a Seq-feat
*
*******************************************************************/
NLM_EXTERN Uint4 ck_seqfeat_extra(SeqFeatPtr sfp)
{
	GeneRefPtr grp;
	ValNodePtr db;
	DbtagPtr db_tag;
	ValNodePtr cit;
	ValNodePtr pub;
	Boolean has_gb = FALSE, has_med = FALSE;
	Uint4 extra_data = 0;
	

	if(sfp->data.choice == 1)
	{
		grp = sfp->data.value.ptrvalue;

		for(db = grp->db; db!=NULL; db = db->next)
		{
			db_tag = db->data.ptrvalue;
			if(StringICmp(db_tag->db, "GenBank") == 0)
			{
				extra_data |= EXTRA_GENBANK;
				break;
			}
		}
	}
	for(cit = sfp->cit; !has_med && cit!=NULL; cit = cit->next)
	{
		if(cit->choice == 3)
			has_med = TRUE;
		if(cit->choice ==1)
		{
			pub = (ValNodePtr)(cit->data.ptrvalue);
			while(pub)
			{
				if(pub->choice == PUB_Muid)
				{
					has_med = TRUE;
					break;
				}
				pub = pub->next;
			}
		}
	}

	if(has_med)
		extra_data |= EXTRA_MEDLINE;
	return extra_data;
}
		
			 
		
/******************************************************************
*
*	get_bin_order(sfp)
*	get the 1000:1 bin data()
*
*******************************************************************/
static Uint2 get_bin_order(SeqFeatPtr sfp)
{
	GeneRefPtr grp;
	ValNodePtr db;
	DbtagPtr db_tag;
	ObjectIdPtr oip;

	if(sfp->data.choice != 1)
		return 0;
	grp = sfp->data.value.ptrvalue;
	if(grp == NULL)
		return 0;

	for(db = grp->db; db != NULL; db = db->next)
	{
		db_tag = db->data.ptrvalue;
		if(db_tag != NULL && StringCmp(db_tag->db, "1000:1 Bin") ==0)
		{
			oip = db_tag->tag;
			return (Uint2)(oip->id);
		}
	}

	return 0;
}
	
		
static Boolean load_annot_name(SeqAnnotPtr annot, CharPtr annot_db)
{
	ValNodePtr desc;
	CharPtr name, title;
	Int4 len;

	annot_db[0] = '\0';
	if(annot == NULL)
		return FALSE;
	name = NULL;
	title = NULL;
	for(desc = annot->desc; desc != NULL; desc = desc->next)
	{
		if(desc->choice == Annot_descr_name)
		{
			if(name == NULL)
				name = (CharPtr)(desc->data.ptrvalue);
		}
		if(desc->choice == Annot_descr_title)
		{
			if(title == NULL)
				title = (CharPtr)(desc->data.ptrvalue);
		}
	}

	if(name != NULL)
		StringNCpy_0(annot_db, name, 20);
	len = StringLen(annot_db);
	if(title != NULL && len < 19)
	{
		StringCat(annot_db, ":");
		++len;
		StringNCpy_0(annot_db+len, title, 20-len);
	}

	return (annot_db[0] != '\0');
}


static Boolean check_feature_for_landmark(CharPtr label, GeneDataPtr gdata, SeqFeatPtr sfp, GatherContextPtr gcp, Uint2 priority)
{
	Boolean found;
	GeneDataPtr c_gdp;

	if(gdata == NULL || sfp == NULL)
		return FALSE;
	found = FALSE;
	c_gdp = NULL;
	if(label[0] != '\0')
	{
		for(c_gdp = gdata; c_gdp != NULL; c_gdp = c_gdp->next)
		{
			if(StringICmp(c_gdp->symbol, label) == 0)
			{
				found = TRUE;
				break;
			}
		}
	}
					
	if(!found && sfp->data.choice == 1)
	{
		for(c_gdp = gdata; c_gdp != NULL; c_gdp = c_gdp->next)
		{
			if(check_landmark(sfp, c_gdp->symbol))
			{
				found = TRUE;
				break;
			}
		}
	}

	if(!found)
		return FALSE;
	
	if(c_gdp->priority == 0 || priority < c_gdp->priority)
	{
		c_gdp->entityID = gcp->entityID;
		c_gdp->itemID = gcp->itemID;
		c_gdp->itemType = gcp->thistype;
		c_gdp->priority = priority;
	}

	StringCpy(label, c_gdp->symbol);
	return TRUE;
}



/*******************************************************************
*
*	collseqfunc( )
*	callback function for collecting sequence related data in 
*	gather, such as segments, features
*
*******************************************************************/
static Boolean collseqfunc(GatherContextPtr gcp)
{
	SeqFeatPtr sfp;
	SeqLocPtr slp;
	BioseqPtr bsp;
	CollectHeaderPtr chp;
	FeatNodePtr fnp;
	CollectSeqOptionPtr csop;
	ObjMgrTypePtr   omtp;
	
	UserObjectPtr uop;
	UserFieldPtr ufp;
	GatherRangePtr grp;
	IvalNodePtr inp;
	Uint1 band;
	Int2 i;
	Int2 label_size;
	ValNodePtr delta_node;
	Boolean is_gap;
	SeqLitPtr slitp;


	chp= (CollectHeaderPtr)(gcp->userdata);
	chp->subtype = 0;
	chp->thislabel[0] = '\0';
	chp->ftype [0] = '\0';
	csop = chp->csop;
	label_size = MIN(100, (Int2)(csop->label_size));
		
		
	switch (gcp->thistype)
	{
		case OBJ_SEQANNOT:	/*for the cytogenetic map, skip certain 
						Seq-annot*/
			chp->annotDB[0] = '\0';
			chp->is_lod_score = is_lod_score_annot((SeqAnnotPtr)(gcp->thisitem));
			load_annot_name((SeqAnnotPtr)(gcp->thisitem), chp->annotDB);
			if(csop->bsp_type == CYTO_MAP)
			{
				if(!annot_is_user_defined((SeqAnnotPtr)(gcp->thisitem)))
					chp->skip_feature = TRUE;
				else
					chp->skip_feature = FALSE;
			}
			break;
				
		case OBJ_BIOSEQ_SEG:
			slp = (SeqLocPtr)(gcp->thisitem);
			if(is_map_segment(slp))	/*not very reliable*/
				return TRUE;
			if(chp->maybe_mapid != NULL)
				if(SeqIdMatch(chp->maybe_mapid, SeqLocId(slp)))
					return TRUE;
			fnp = CreateFeatNode (&(chp->features), &(chp->prev_feat), gcp->thistype, gcp->itemID, gcp->entityID, 0);
			MemCopy(&(fnp->extremes), &(gcp->extremes), sizeof(GatherRange));
			if(slp->choice == SEQLOC_NULL || slp->choice == SEQLOC_EMPTY)
				fnp->follower = TRUE;	/*used to present the empty Seq-loc*/
			else
			{
				if(MuskSeqIdWrite (SeqLocId(slp), chp->thislabel, label_size, csop->slabel_format, TRUE, TRUE))
					fnp->label = StringSave(chp->thislabel);
			}
			break;
		case OBJ_BIOSEQ_DELTA:
			delta_node = (ValNodePtr)(gcp->thisitem);
			is_gap = FALSE;
			chp->thislabel[0] = '\0';
			if(delta_node->choice ==1)
			{
				slp = delta_node->data.ptrvalue;
				if(slp->choice == SEQLOC_NULL || slp->choice == SEQLOC_EMPTY)
					is_gap = TRUE;
				else
					MuskSeqIdWrite (SeqLocId(slp), chp->thislabel, label_size, csop->slabel_format, TRUE, TRUE);

			}
			else
			{
				slitp = delta_node->data.ptrvalue;
				if(slitp->length == 0 || slitp->seq_data == NULL)
				{
					is_gap = TRUE;
					if(slitp->length > 0)
						return TRUE;
				}
			}

			fnp = CreateFeatNode (&(chp->features), &(chp->prev_feat), gcp->thistype, gcp->itemID, gcp
->entityID, 0);
			MemCopy(&(fnp->extremes), &(gcp->extremes), sizeof(GatherRange));
			if(is_gap)
				fnp->follower = TRUE;
			else if(chp->thislabel[0] != '\0')
				fnp->label = StringSave(chp->thislabel);
			break;
		
		case OBJ_BIOSEQ:
			fnp = CreateFeatNode (&(chp->features), &(chp->prev_feat), gcp->thistype, gcp->itemID, gcp->entityID, 0);
			MemCopy(&(fnp->extremes), &(gcp->extremes), sizeof(GatherRange));
			bsp = (BioseqPtr) gcp->thisitem;
			if(MuskSeqIdWrite(bsp->id, chp->thislabel, label_size, csop->slabel_format, TRUE, FALSE))
				fnp->label = StringSave(chp->thislabel);
			break;
			
		case OBJ_BIOSEQ_MAPFEAT:
		case OBJ_SEQFEAT:
			if(gcp->thistype == OBJ_SEQFEAT && chp->skip_feature)
				return TRUE;
			sfp = (SeqFeatPtr) gcp->thisitem;
			/* if(gcp->thistype == OBJ_SEQFEAT)
			{
				if(chp->filter_level == gcp->seglevel +1)
					return TRUE;
			} */
			omtp = ObjMgrTypeFind (chp->omp, OBJ_SEQFEAT, NULL, NULL);
			if(omtp == NULL)
				return TRUE;
			if (omtp->subtypefunc != NULL)
				chp->subtype = (*(omtp->subtypefunc)) (gcp->thisitem);
			else
				chp->subtype = 0;
			if(gcp->thistype == OBJ_SEQFEAT && 
				csop->features[chp->subtype] == 0)	/*filter unwanted features*/
				return TRUE;
				
			/*tolerate the unknown band*/
			/*
			if(sfp->data.choice == 14 && gcp->thistype == OBJ_BIOSEQ_MAPFEAT)
			{
				uop = sfp->data.value.ptrvalue;
				band = get_band_type(uop);
				if(band == 0)
					return TRUE;
			}
			*/
			fnp = CreateFeatNode (&(chp->features), &(chp->prev_feat), gcp->thistype, gcp->itemID, gcp->entityID, chp->subtype);

			/*special collection for the LOD scores*/
			if(chp->is_lod_score && gcp->thistype == OBJ_SEQFEAT)	/*it is the LOD score data*/
			{
				fnp->extra_data = EXTRA_LOD_SCORE;
				fnp->bin_order = GetLODScoreBitValue(sfp);
				StringCpy(fnp->annotDB, chp->annotDB);
				MemCopy(&(fnp->extremes), &(gcp->extremes), sizeof(GatherRange));
				return TRUE;
			}

			fnp->has_product = (sfp->product !=NULL);
			fnp->extra_data = ck_seqfeat_extra(sfp);	/*extra data associated with a Gene-ref*/
			get_mapmarker_info(sfp->ext, &(fnp->extra_data), &(fnp->bin_order));
			if(fnp->bin_order == 0)	/*just as a backup*/
				fnp->bin_order = get_bin_order(sfp);
			if(gcp->thistype == OBJ_SEQFEAT && chp->annotDB[0] != '\0')
				StringCpy(fnp->annotDB, chp->annotDB);
			else
				fnp->annotDB[0]= '\0';
			MemCopy(&(fnp->extremes), &(gcp->extremes), sizeof(GatherRange));

			/*special collection for cytogenetic band*/			
			if(sfp->data.choice == 14 && gcp->thistype == OBJ_BIOSEQ_MAPFEAT)
			{
				uop = sfp->data.value.ptrvalue;
				band = get_band_type(uop);
				fnp->band = band;
				fnp->label = StringSave(get_band_name(uop));
				if(band < BAND_POINT)	/*for flybase*/
				{
					for(ufp = uop->data; ufp!=NULL; ufp=ufp->next)
					{
						if(is_label_match(ufp->label, "Subdivision"))
							fnp->pos_label = StringSave(ufp->data.ptrvalue);
					}	
				}
			}
			else	/*for non-cyto band*/
			{
				/* if((gcp->thistype == OBJ_BIOSEQ_MAPFEAT) || collect_feature_label(csop->flabel_format[chp->subtype])) */
				/* check the landmark genes*/

				if(collect_feature_label(csop->flabel_format[chp->subtype]))
				{
					if(omtp->labelfunc !=NULL)
						(*(omtp->labelfunc)) (gcp->thisitem, chp->thislabel, label_size, OM_LABEL_CONTENT);
						fnp->landmark = check_feature_for_landmark(chp->thislabel, chp->gdata, sfp, gcp, chp->priority);
					if(fnp->landmark == FALSE && omtp->labelfunc != NULL && 
						csop->flabel_format[chp->subtype] != OM_LABEL_CONTENT)
						(*(omtp->labelfunc)) (gcp->thisitem, chp->thislabel, label_size, csop->flabel_format[chp->subtype]);

				}					

				if(chp->thislabel[0] != '\0')
					fnp->label = StringSave(chp->thislabel);
				slp = sfp->location;	/*collect the intervals*/
				if(slp->choice == SEQLOC_PACKED_PNT || csop->nointerval == FALSE)
				{
					grp = gcp->rdp;
					for(i=0; (grp!=NULL) && i<gcp->num_interval; ++i)
					{
						inp = MemNew(sizeof(IvalNode));
						MemCopy(&(inp->gr), grp, sizeof(GatherRange));
						ValNodeAddPointer(&(fnp->interval), (Uint1)(i+1), (Pointer)inp);
						++grp;
					}
				}
			}
			break;
		default:
			break;
	}
	
	return TRUE;
}


static Boolean ignore_feature(BoolPtr f_list)
{
	Int2 i;

	if(f_list == NULL)
		return TRUE;
	for(i = 0; i<FEATDEF_ANY; ++i)
		if(f_list[i])
			return FALSE;
	return TRUE;
}
			
/***********************************************************************
*
*	CollectItemForSeqLoc(slp, entityID, left, is_aa, csop)
*	Collect sequences, features for a Seq-loc
*	slp: the target Seq-loc
*	entityID: the top level entityID for the current sequence
*	left: the left offset on the graph
*	is_aa: if TRUE, set get_feats_product flag to TRUE
*	csop: the collection option
*
*
***********************************************************************/	
NLM_EXTERN ValNodePtr CollectItemForSeqLocEx(SeqLocPtr slp, Uint2 entityID, Int4 left, Boolean is_aa, SeqIdPtr maybe_mapid, CollectSeqOptionPtr csop, GeneDataPtr gdata, Uint2 priority, Boolean forceSeglevelsTo1)
{
	GatherScope gs;
	CollectHeader ch;

	if(slp == NULL || entityID == 0 || csop == NULL)
		return NULL;
	ch.omp = ObjMgrGet();	/*set up the options*/
	ch.features = NULL;
	ch.csop = csop;
	ch.maybe_mapid = maybe_mapid;
	ch.filter_level = csop->filter_level;
	ch.gdata = gdata;
	ch.priority = priority;
	ch.skip_feature = FALSE;
	ch.prev_feat = NULL;
	ch.prev_align = NULL;
	ch.index = 0;
	ch.annotDB[0] = '\0';
	ch.is_lod_score = FALSE;
	

	MemSet((Pointer)&gs, 0, sizeof (GatherScope));
	gs.get_feats_location = TRUE;
	gs.get_feats_product = is_aa;
	MemSet((Pointer)(gs.ignore), (int)TRUE, (size_t)(OBJ_MAX)*sizeof(Boolean));

	gs.ignore[OBJ_SEQENTRY] = FALSE;
	gs.ignore[OBJ_BIOSEQ_SEG] = FALSE;
	gs.ignore[OBJ_BIOSEQ] = FALSE;
	gs.ignore[OBJ_BIOSEQ_MAPFEAT] = FALSE;
	gs.ignore[OBJ_BIOSEQ_DELTA] = FALSE;
	if(!ignore_feature(csop->features))
	{
		gs.ignore[OBJ_SEQANNOT] = FALSE;
		gs.ignore[OBJ_SEQFEAT] = FALSE;
	}

	gs.nointervals = csop->nointerval;
	gs.seglevels = csop->seglevels;
	/*gs.stop_on_annot = TRUE;*/
	if(gs.seglevels == 0)
	{
		gs.ignore_top = FALSE;
		gs.stop_on_annot = FALSE;
		/* gs.ignore_top = FALSE;
		gs.stop_on_annot = TRUE; */
	}
	else if (forceSeglevelsTo1)
	{
		gs.ignore_top = TRUE; /* JK */
		gs.stop_on_annot = FALSE; /* JK */
	}
	else
	{
		gs.ignore_top = FALSE;
		gs.stop_on_annot = TRUE;
	}
	gs.currlevel = 0;
	gs.split_packed_pnt = TRUE;


	for(; slp!= NULL; slp = slp->next)
	{
		gs.offset = left;
		gs.target = slp;
		GatherEntity(entityID, (Pointer)(&ch), collseqfunc, &gs);
		left += SeqLocLen(slp);
	}
	return ch.features;
}

NLM_EXTERN ValNodePtr CollectItemForSeqLoc(SeqLocPtr slp, Uint2 entityID, Int4 left, Boolean is_aa, SeqIdPtr maybe_mapid, CollectSeqOptionPtr csop, GeneDataPtr gdata, Uint2 priority)
{
	return CollectItemForSeqLocEx(slp, entityID, left, is_aa, maybe_mapid, csop, gdata, priority, FALSE);
}

static Uint1 is_segmap_align_annot(SeqAnnotPtr annot)
{
	UserObjectPtr uop;
	ValNodePtr desc;
	ObjectIdPtr oip;
	UserFieldPtr ufp;
	
	if(annot == NULL)
		return 0;
	if(annot->type != 2)
		return 0;
	if(is_annot_for_hist_alignment(annot))
		return 0;

	desc =annot->desc;
	while(desc)
	{
		if(desc->choice == Annot_descr_user)
		{
			uop = desc->data.ptrvalue;
			if(uop->type)
			{
				oip = uop->type;
				if(StringCmp(oip->str, "SegMap STS Alignment") == 0)
				{
					ufp = uop->data;
					if(ufp && ufp->choice == 2)
						return (Uint1)(ufp->data.intvalue);
				}
			}
		}
		desc = desc->next;
	}
	return 0;
}

static GatherRangePtr create_gr_data(SeqAlignPtr align, SeqLocPtr m_loc, Int4 m_left)
{
	Int2 i, num;
	SeqAlignPtr curr;
	GatherRangePtr grp;

	for(num = 0, curr = align; curr!= NULL; curr = curr->next)
		++num;
	if( num == 0)
		return NULL;

	grp = MemNew((size_t)num * sizeof(GatherRange));
	for(i =0, curr = align; curr != NULL; curr = curr->next, ++i)
	{
		if(!SeqLocOffset (m_loc, curr->bounds, &(grp[i]), m_left))
		{
			grp[i].left = -1;
			grp[i].right = -1;
		}
	}
	return grp;
}

static void add_int_with_order(ValNodePtr PNTR head, Uint1 type, Int4 pos)
{
	ValNodePtr prev, curr;
	ValNodePtr cnew;

	if(*head == NULL)
		ValNodeAddInt(head, type, pos);
	else
	{
		prev = NULL;
		curr = *head;
		cnew = ValNodeNew(NULL);
		cnew->choice = type;
		cnew->data.intvalue = pos;
		while(curr)
		{
			if(pos < curr->data.intvalue)
			{
				if(prev == NULL)
					*head = cnew;
				else
					prev->next = cnew;
				cnew->next = curr;
				return;
			}

			prev = curr;
			curr = curr->next;
		}
		if(prev != NULL)
			prev->next = cnew;
	}
}
					
	
					
static void load_open_close_sts_mark(SeqAlignPtr halign, ValNodePtr anp_list, Uint1 annot_type, SeqLocPtr m_loc, Int4 m_left)
{
	SeqAlignPtr align;
	AlignNodePtr anp;
	AlignSegPtr asp;
	SeqIdPtr sip;
	SeqLocPtr slp;
	GatherRange gr;
	GatherRangePtr grp;
	Int4 start, stop;
	Uint1 strand;
	SeqInt sint;
	SeqLoc sl;
	Boolean collected = FALSE;
	StdSegPtr ssp;
	Int4 e_left, e_right;
	Int2 i;
	
	grp = create_gr_data(halign, m_loc, m_left);
	if(grp == NULL)
		return;
	while(anp_list)
	{
		if(anp_list->choice != OBJ_SEQANNOT)
		{
			anp = anp_list->data.ptrvalue;
			sip = anp->sip;
			if(!SeqIdForSameBioseq(sip, SeqLocId(m_loc)))
			{
			if(anp->seqpos < 0)	/*minus strand*/
			{
				stop = ABS(anp->seqpos);
				start = stop - (anp->extremes.right - anp->extremes.left);
				strand = Seq_strand_minus;
			}
			else
			{
				start = anp->seqpos;
				stop = start + (anp->extremes.right - anp->extremes.left);
				strand = Seq_strand_plus;
			}

			sint.from = start;
			sint.to = stop;
			sint.strand = strand;
			sint.id = sip;
			sl.choice = SEQLOC_INT;
			sl.data.ptrvalue = &sint;
			sl.next = NULL;

			e_left = anp->extremes.left;
			e_right = anp->extremes.right;
			asp = anp->segs;
			for(align = halign, i=0; align != NULL; align = align->next, ++i)
			{
				if(grp[i].left != -1)
				{
					if(!(e_left > grp[i].right || e_right < grp[i].left))
					{
						for(ssp = align->segs; ssp != NULL; ssp = ssp->next)
						{
							/*slp = ssp->loc->next;*/
							slp = ssp->loc;
							while(slp)
							{
								if(SeqIdMatch(SeqLocId(slp), sip))
									break;
								else
									slp = slp->next;
							}
					
							if(slp != NULL)
							{
								if(SeqLocOffset (&sl, slp, &gr, e_left))
								{
									if(asp == NULL)
									{
										asp = MemNew(sizeof(AlignSeg));
										MemCopy(&(asp->gr), &(anp->extremes), sizeof(GatherRange));
										asp->type = REG_SEG;
										anp->segs = asp;
									}
											
									/*ValNodeAddInt(&(asp->mismatch), annot_type, gr.left);*/
									add_int_with_order(&(asp->mismatch), annot_type, gr.left);
									collected = TRUE;
								}
							}
				
						}
					}
					/*if(grp[i].left> e_right)
						break;*/
				}
			}
			}
		}
		anp_list = anp_list->next;
	}
	MemFree(grp);
	
}
					
					
typedef struct segmap_data{
	ValNodePtr anp_list;
	SeqLocPtr m_loc;
	Int4 left;
}SegMapData, PNTR SegMapDataPtr;

static Boolean coll_segmap_func(GatherContextPtr gcp)
{
	ValNodePtr anp_list;
	SeqAnnotPtr annot;
	Uint1 annot_type;
	SeqAlignPtr align;
	SegMapDataPtr smdp;
	
	smdp = (SegMapDataPtr)(gcp->userdata);
	if(smdp == NULL || smdp->anp_list == NULL || smdp->m_loc == NULL)
		return FALSE;
	anp_list = smdp->anp_list;

	annot = (SeqAnnotPtr)(gcp->thisitem);
	if(annot == NULL || annot->type != 2)
		return TRUE;
	annot_type = is_segmap_align_annot(annot);
	if(annot_type == 0)
		return TRUE;
	align = annot->data;
	load_open_close_sts_mark(align, anp_list, annot_type, smdp->m_loc, smdp->left);
	
	return TRUE;
}


/*******************************************************************
*
*	void CollectSegMapSTSAlign( entityID, anp_list)
*	look for the sts alignment from segmap stored as Seq-annot in 
*	in entityID. Add the alignment as the mismatch marker in the 
*	AlignSeg of the anp_list
*
*******************************************************************/
NLM_EXTERN void CollectSegMapSTSAlign( Uint2 entityID, ValNodePtr anp_list, SeqLocPtr m_loc, Int4 m_left)
{
	GatherScope gs;
	SegMapData smd;

	if(entityID == 0 || anp_list == NULL)
		return;

	MemSet((Pointer)&gs, 0, sizeof (GatherScope));
	MemSet((Pointer)(gs.ignore), (int)TRUE, (size_t)(OBJ_MAX)*sizeof(Boolean));
	gs.ignore[OBJ_SEQANNOT] = FALSE;

	smd.anp_list = anp_list;
	smd.m_loc = m_loc;
	smd.left = m_left;
	GatherEntity(entityID, (Pointer)(&smd), coll_segmap_func, &gs);
}


/*#####################################################################
#
#	functions related to the layout for FeatNode
#
######################################################################*/



/***********************************************************************
*  SortFeatNode(list)
*	HeapSort the FeatNode list to the accending order of fnp->left
*	return the head of the sorted list
*	
************************************************************************/
static Uint1Ptr featureSortOrder;
static Uint1Ptr groupSortOrder;

static int FeatNodeIntervalCompare (ValNodePtr vnp1, ValNodePtr vnp2)

{
  GatherRangePtr  grp1, grp2;

  while (vnp1 != NULL && vnp2 != NULL) {
    grp1 = (GatherRangePtr) vnp1->data.ptrvalue;
    grp2 = (GatherRangePtr) vnp2->data.ptrvalue;
    if (grp1 != NULL && grp2 != NULL) {
      /*
      if (grp1->left > grp2->left) {
        return 1;
      } else if (grp1->left < grp2->left) {
        return -1;
      } else if (grp1->right > grp2->right) {
        return 1;
      } else if (grp1->right < grp2->right) {
        return -1;
      }
      */
      if (grp1->left > grp2->left) {
        return -1;
      } else if (grp1->left < grp2->left) {
        return 1;
      } else if (grp1->right > grp2->right) {
        return -1;
      } else if (grp1->right < grp2->right) {
        return 1;
      }
    }
    vnp1 = vnp1->next;
    vnp2 = vnp2->next;
  }
  if (vnp1 != NULL) {
    return -1;
  } else if (vnp2 != NULL) {
    return 1;
  } else {
    return 0;
  }
}

static int LIBCALLBACK FeatNodeCompProc (VoidPtr ptr1, VoidPtr ptr2)
{
  FeatNodePtr  fnp1;
  FeatNodePtr  fnp2;
  ValNodePtr   vnp1;
  ValNodePtr   vnp2;
  GatherRange	gr1, gr2;
  Uint1 group1, group2;
  Uint1 order1, order2;
  int rsult;

  if (ptr1 != NULL && ptr2 != NULL) {
    vnp1 = *((ValNodePtr PNTR) ptr1);
    vnp2 = *((ValNodePtr PNTR) ptr2);
    if (vnp1 != NULL && vnp2 != NULL) {
      fnp1 = (FeatNodePtr) vnp1->data.ptrvalue;
      fnp2 = (FeatNodePtr) vnp2->data.ptrvalue;
      if (fnp1 != NULL && fnp2 != NULL) {
	gr1 = fnp1->extremes;
	gr2 = fnp2->extremes;
        /*
        if (gr1.left > gr2.left) {
          return 1;
        } else if (gr1.left < gr2.left) {
          return -1;
        } else if (gr1.right > gr2.right) {
          return 1;
        } else if (gr1.right < gr2.right) {
          return -1;
        } else */
        if ((rsult = FeatNodeIntervalCompare (fnp1->interval, fnp2->interval)) != 0) {
          return rsult;
        } else {
	  if(featureSortOrder == NULL || groupSortOrder == NULL)
          	return 0;
	  else
	  {
		group1 = groupSortOrder[fnp1->feattype];
		group2 = groupSortOrder[fnp2->feattype];
		if(group1 !=group2)
			return -1;
		order1 = featureSortOrder[fnp1->feattype];
		order2 = featureSortOrder[fnp2->feattype];
		if(order1 < order2)
			return -1;
		if(order1 > order2)
			return 1;
		return 0;
	  }
        }
      } else {
        return 0;
      }
    } else {
      return 0;
    }
  } else {
    return 0;
  }
}




/***********************************************************************
*
*	SortFeatNode(list)
*	sort a list of FeatNode to the ascending order of (extremes.left, 
*	extremes.right)
*
**********************************************************************/

NLM_EXTERN ValNodePtr SortFeatNode(ValNodePtr fnp_list, Uint1Ptr featureOrder, Uint1Ptr groupOrder)
{
	featureSortOrder = featureOrder;
	groupSortOrder = groupOrder;
	return SortValNode(fnp_list, FeatNodeCompProc);
}


static int LIBCALLBACK AlignNodeCompProc (VoidPtr ptr1, VoidPtr ptr2)
{
  AlignNodePtr  anp1, anp2;
  ValNodePtr   vnp1;
  ValNodePtr   vnp2;
  GatherRange	gr1, gr2;

  if (ptr1 != NULL && ptr2 != NULL) {
    vnp1 = *((ValNodePtr PNTR) ptr1);
    vnp2 = *((ValNodePtr PNTR) ptr2);
    if (vnp1 != NULL && vnp2 != NULL) {
      anp1 = (AlignNodePtr) vnp1->data.ptrvalue;
      anp2 = (AlignNodePtr) vnp2->data.ptrvalue;
      if (anp1 != NULL && anp2 != NULL) {
	gr1 = anp1->extremes;
	gr2 = anp2->extremes;
	/* len1 = anp1->extremes.right - anp1->extremes.left;
	len2 = anp2->extremes.right - anp2->extremes.left;
        if(len1 > len2)
          return -1;
        else if(len1 < len2)
          return 1; */

        if (gr1.left > gr2.left) {
          return 1;
        } else if (gr1.left < gr2.left) {
          return -1;
        } else if (gr1.right < gr2.right) {
          return 1;
        } else if (gr1.right > gr2.right) {
          return -1;
        } else {
          return 0;
        }
      } else {
        return 0;
      }
    } else {
      return 0;
    }
  } else {
    return 0;
  }
}

/*sort to make the display showing the alignment that are 
* consistuent of the master sequence first
*/
static ValNodePtr modify_align_node_block(ValNodePtr anp_list)
{
	ValNodePtr block_list = NULL;
	ValNodePtr curr, next, prev = NULL;
	AlignNodePtr anp;

	if(anp_list == NULL)
		return NULL;

	curr = anp_list;
	while(curr)
	{
		next = curr->next;
		anp = curr->data.ptrvalue;
		if(anp->blocks != NULL)
		{
			if(prev == NULL)
				anp_list = curr->next;
			else
				prev->next = curr->next;
			curr->next = NULL;
			ValNodeLink(&block_list, curr);
		}
		else
			prev = curr;
		curr = next;
	}

	if(block_list == NULL)
		return anp_list;
	else
	{
		ValNodeLink(&block_list, anp_list);
		return block_list;
	}
}

		

/***********************************************************************
*
*	SortAlignNode(anp_list)
*	sort a list of AlignNode to the ascending order of (extremes.left, 
*	extremes.right)
*
**********************************************************************/
NLM_EXTERN ValNodePtr SortAlignNode(ValNodePtr anp_list)
{
	ValNodePtr list, curr, prev, last, next;
	ValNodePtr head;

	if(anp_list == NULL)
		return NULL;
	list = anp_list;
	prev = NULL;
	head = NULL;
	while(list != NULL)
	{
		if(prev != NULL)
			prev->next = list;
		while(list && list->choice == OBJ_SEQANNOT)
		{
			if(head == NULL)
				head = list;
			prev = list;
			list = list->next;
		}
		if(list != NULL)
		{
			curr = list;
			last = NULL;
			while(curr && curr->choice != OBJ_SEQANNOT)
			{
				last = curr;
				curr = curr->next;
			}
			next = last->next;
			last->next = NULL;
			list = SortValNode(list, AlignNodeCompProc);
			list = modify_align_node_block(list);
			if(prev == NULL)
				head = 	list;
			else
				prev->next = list;
			while(list->next != NULL)
				list = list->next;
			prev = list;
			list = next;
		}
	}

	return head;
}



/*#######################################################################
#
#	function related to Layout of AlignNode
#
########################################################################*/


/***********************************************************************
*
*	find_insert_ypos(left, seglen, ins, l_bound, r_bound, p_pos, space
*	num)
*	find the level for placing the insertions. Used in both the layout
*	for text and graphic
*	left: to store the left-most position calculated for an insertion
*	seglen: length of the insertion
*	ins: the position for insertions
*	l_bound: the leftmost position in the current line
*	r_bound: the rightmost position in the current line
*	p_pos: position for storing all the layout info
*	num: number of elements in p_pos
*	return the current level found for an insertion
*
***********************************************************************/
NLM_EXTERN Int2 find_insert_ypos(Int4Ptr left, Int4 seglen, Int4 ins, Int4 l_bound, Int4 r_bound, Int4Ptr p_pos, Int4 space, Int2 num)
{
 	Int2 i =0;
	Int4 start, stop;

	--seglen;
	*left = MAX(l_bound, (ins-seglen));
	start = *left;
        for(i =0; i<num; ++i)
	{
		if(p_pos[i] == 0)
		{
			p_pos[i] = (*left + seglen);
             		return i;
		}

		if(ins > (p_pos[i]+space))
		{
			start = (*left);
			start +=MAX(0, (seglen - (ins - (p_pos[i]+space))));
			stop = start+seglen;
			if(stop <= r_bound)
			{
				*left = start;
				p_pos[i] = (*left) + seglen;
				return i;
			}
		}


	}

	return -1;
}


		
/************************************************************************
*
*	convert_gdata_for_featnode(gdata, cyto_loc, offset)
*	gdata: the GeneDataPtr
*	cyto_loc: the current location on the cytogenetic map
*	offset: the offset of cyto_loc to the graphic viewer1
*	for human cytogenetic map, the markers are not shown. But for 
*	the markers that were queried, it will display the interval for 
*	gene data
*
************************************************************************/

NLM_EXTERN ValNodePtr convert_gdata_to_featnode (GeneDataPtr gdata, SeqLocPtr cyto_loc, Int4 offset)
{
	ValNodePtr fnp_node;
	ValNodePtr prev;
	FeatNodePtr fnp;
	SeqLocPtr slp;
	SeqPntPtr spp;
	Boolean mod_fuzz;	/*for the old style of FeatNode. To modify the 
					fuzziness on a point*/
	IntFuzzPtr fuzz;
	GatherRange gr;
	SeqFeatPtr sfp;


	if(gdata == NULL || cyto_loc == NULL)
		return NULL;
	fnp_node = NULL;
	prev = NULL;
	while(gdata)
	{
		slp = NULL;
		sfp = gdata->sfp;
		if(sfp != NULL && sfp->location != NULL)
		{
			mod_fuzz = FALSE;
			if(sfp->location->choice == SEQLOC_PNT)
			{
				spp = sfp->location->data.ptrvalue;
				if(spp->fuzz != NULL)
				{
					fuzz = spp->fuzz;
					if(fuzz->choice == 2)	/*range */
					{
						mod_fuzz = TRUE;
						slp = SeqLocIntNew(fuzz->b, fuzz->a, 0, spp->id);
					}
				}
			}
			if(!mod_fuzz)
				slp = sfp->location;
			if(SeqLocOffset(cyto_loc, slp, &gr, offset))
			{
				fnp = CreateFeatNode (&fnp_node, &prev, gdata->itemType, gdata->itemID, gdata->entityID, gdata->subtype);
				MemCopy(&(fnp->extremes), &gr,  sizeof(GatherRange));
				fnp->label = StringSave(gdata->symbol);
				fnp->landmark = TRUE;
				if(gdata->sfp != NULL)
				{
					fnp->extra_data = ck_seqfeat_extra(gdata->sfp);
					get_mapmarker_info(gdata->sfp->ext, &(fnp->extra_data), &(fnp->bin_order));
				}
			}


			if(mod_fuzz)
				SeqLocFree(slp);
		}
		gdata = gdata->next;
	}

	return fnp_node;
}

				
/*
*	for AlignNode that includes insertions, map the insertion 
*	to gaps on the master sequence
*/

/*the data structure for storing the insertion information*/
typedef struct insert_list {
	Int4 max_size;
	Int4 master_pos;	/*position on the master sequence*/
	Boolean  used;	/* this position is acturally at an inserted segment*/
	Boolean after;	/*does the insertion occurs after the master_pos*/
	struct insert_list PNTR next;
}InsertList, PNTR InsertListPtr;


static void load_insertion_list(InsertListPtr PNTR head, Int4 insert_pos, Int4 insert_size, Boolean after)
{
	InsertListPtr curr, prev, ilp;

	prev = NULL;
	curr = *head;
	while(curr)
	{
		if(curr->master_pos == insert_pos)
		{
			curr->max_size = MAX(curr->max_size, insert_size);
			return;
		}
		if(curr->master_pos > insert_pos)
			break;
		else
			prev = curr;
		curr = curr->next;
	}

	ilp = MemNew(sizeof(InsertList));
	ilp->max_size = insert_size;
	ilp->master_pos = insert_pos;
	ilp->next = curr;
	ilp->after = after;

	if(prev == NULL)
		*head = ilp;
	else
		prev->next = ilp;
}

static void add_offset_to_featnode(ValNodePtr fnp_node, Int4 offset)
{
	FeatNodePtr fnp;
	ValNodePtr interval;
	IvalNodePtr inp;

	while(fnp_node)
	{
		fnp = fnp_node->data.ptrvalue;
		fnp->extremes.left += offset;
		fnp->extremes.right += offset;

		for (interval = fnp->interval; interval != NULL; interval = interval->next)
		{
			inp = interval->data.ptrvalue;
			inp->gr.left += offset;
			inp->gr.right += offset;
		}
		fnp_node = fnp_node->next;
	}
}

NLM_EXTERN void AddOffsetToAlignNode(AlignNodePtr anp, Int4 offset)
{
	AlignSegPtr asp;
	AlignBlockPtr abp;

	anp->extremes.left += offset;
	anp->extremes.right += offset;
	for(abp = anp->blocks; abp != NULL; abp = abp->next)
	{
		abp->gr.left += offset;
		abp->gr.right += offset;
	}

	for(asp = anp->segs; asp != NULL; asp = asp->next)
	{
		if(asp->type == INS_SEG)
		{
			asp->ins_pos += offset;
			asp->gr.left += offset;
		}
		else
		{
			asp->gr.left += offset;
			asp->gr.right += offset;
		}
		if(asp->cnp != NULL)
			add_offset_to_featnode(asp->cnp, offset);
	}
}

static ValNodePtr split_feature_interval(ValNodePtr PNTR p_interval, Int4 offset, 
										 Int4 ins_pos, Int4 ins_size)
{
	ValNodePtr interval, next, prev;
	ValNodePtr second_list = NULL;
	IvalNodePtr inp, new_inp;


	prev = NULL;
	interval = *p_interval; 
	while(interval != NULL)
	{
		next = interval->next;
		inp = interval->data.ptrvalue;
		if(inp->gr.right <= ins_pos)
		{
			inp->gr.left += offset;
			inp->gr.right += offset;
			prev = interval;
		}
		else if(inp->gr.left > ins_pos)
		{
			if(prev == NULL)
				*p_interval = NULL;
			else
				prev->next = NULL;
			return interval;
		}
		else
		{	/*there is overlap */
			new_inp = MemNew(sizeof(IvalNode));
			new_inp->gr.strand = inp->gr.strand;
			/* new_inp->gr.right = inp->gr.right + offset + ins_size;
			new_inp->gr.left = ins_pos + offset + ins_size;	 */
			new_inp->gr.right = inp->gr.right;
			new_inp->gr.left = ins_pos +1;

			inp->gr.left += offset;
			inp->gr.right = ins_pos + offset;
			interval->next = NULL;

			ValNodeAddPointer(&second_list, 0, new_inp);
			ValNodeLink(&second_list, next);
			return second_list;
		}
		interval = next;
	}

	return NULL;
}

static ValNodePtr add_insertion_to_featnode(ValNodePtr PNTR pfnp_node, Int4 offset, 
											Int4 ins_pos, Int4 ins_size)
{
	ValNodePtr fnp_node, next, prev;
	ValNodePtr second_list;
	FeatNodePtr fnp, new_fnp;
	ValNodePtr interval;
	IvalNodePtr inp;

	fnp_node = *pfnp_node;
	second_list = NULL;
	prev = NULL;
	while(fnp_node)
	{
		next = fnp_node->next;
		fnp = fnp_node->data.ptrvalue;
		if(fnp->extremes.right <= ins_pos)
		{
			for (interval = fnp->interval; interval != NULL; interval = interval->next)
			{
				inp = interval->data.ptrvalue;
				inp->gr.left += offset;
				inp->gr.right += offset;
			}
			fnp->extremes.left += offset;
			fnp->extremes.right += offset;
			prev = fnp_node;
		}
		else if(fnp->extremes.left > ins_pos)
		{
			/* fnp->extremes.left += offset + ins_size;
			fnp->extremes.right += offset + ins_size;

			for (interval = fnp->interval; interval != NULL; interval = interval->next)
			{
				inp = interval->data.ptrvalue;
				inp->gr.left += offset + ins_size;
				inp->gr.right += offset + ins_size;
			} */

			fnp_node->next = NULL;
			ValNodeLink(&second_list, fnp_node);
			if(prev == NULL)
				*pfnp_node = next;
			else
				prev->next = next;
		}
		else	/*resides between the insertion points, needs to split the featnode*/
		{
			new_fnp = MemNew(sizeof(FeatNode));
			MemCopy((Pointer)new_fnp, fnp, sizeof(FeatNode));
			if(fnp->label != NULL)
				new_fnp->label = StringSave	(fnp->label);
			if(fnp->pos_label != NULL)
				new_fnp->pos_label = StringSave	(fnp->pos_label);
			if(fnp->annotDB[0] != '\0')
				StringCpy(new_fnp->annotDB, fnp->annotDB);
			/* new_fnp->extremes.right = fnp->extremes.right + offset + ins_size;
			new_fnp->extremes.left = ins_pos + ins_size + offset; */
			new_fnp->extremes.right = fnp->extremes.right;
			new_fnp->extremes.left = ins_pos + 1;
			new_fnp->extremes.strand = fnp->extremes.strand;

			ValNodeAddPointer(&second_list, fnp_node->choice, new_fnp);

			fnp->extremes.left += offset;
			fnp->extremes.right = ins_pos + offset;

			new_fnp->interval = split_feature_interval(&(fnp->interval), offset, 
										 ins_pos, ins_size);

			prev = fnp_node;
		}
		fnp_node = next;
	}

	return second_list;
}


static Int4 find_insertion_size (InsertListPtr ilp, Int4Ptr ins_pos)
{
	while(ilp)
	{
		if(ilp->master_pos == *ins_pos)
		{
			if(ilp->after == FALSE)
				-- (*ins_pos);
			return ilp->max_size;
		}
		ilp = ilp->next;
	}

	return 0;
}
static Int4 get_max_insert_size (InsertListPtr ilp, Int4 from, Int4 to, Int4Ptr insert_pos)
{
	Int4 t_from, t_to;

	while(ilp)
	{
		if(ilp->used == FALSE)	/*it is not used by insertion and mapping*/
		{
			t_from = from;
			t_to = to;
			if(ilp->after == FALSE)
			{
				t_from +=1;
				t_to += 1;
			}
			if(ilp->master_pos >= from && ilp->master_pos <= to)
			{
				*insert_pos = ilp->master_pos;
				if(ilp->after == FALSE)
					--(*insert_pos);
				ilp->used = TRUE;
				return ilp->max_size;
			}
		}
		else if(ilp->master_pos > to)
			return -1;
		ilp = ilp->next;
	}

	return -1;
}

static Int4 get_max_gap_size(InsertListPtr ilp, Int4 from, Int4 to)
{
	Int4 max_size = 0;

	while(ilp)
	{
		if(ilp->used == FALSE)
		{
			if(ilp->master_pos >= from && ilp->master_pos <= to)
			{
				ilp->used = TRUE;
				max_size += ilp->max_size;
			}
		}
		if(ilp->master_pos > to)
			return max_size;

		ilp = ilp->next;
	}
	return max_size;
}


static ValNodePtr add_offset_to_mismatch(ValNodePtr PNTR mismatch, Int4 offset, Int4 ins_pos, Int4 ins_size)
{
	ValNodePtr second_list, prev, curr;

	second_list = NULL;
	curr = *mismatch;
	prev = NULL;
	while(curr)
	{
		if(curr->data.intvalue <= ins_pos || ins_pos == -1)
			curr->data.intvalue += offset;
		else
		{
			ValNodeLink(&second_list, curr);
			if(prev == NULL)
				*mismatch = NULL;
			else
				prev->next = NULL;
			return second_list;

		}
		prev = curr;
		curr = curr->next;
	}
	return second_list;
}

static void reset_insertion_list(InsertListPtr ilp, AlignSegPtr asp)
{
	AlignSegPtr curr;

	while(ilp)
	{
		ilp->used = FALSE;
		for(curr = asp; curr != NULL; curr = curr->next)
		{
			if(curr->ins_pos == ilp->master_pos)
			{
				ilp->used = TRUE;
				break;
			}
		}

		ilp = ilp->next;
	}
}

static void refresh_insertion_list(InsertListPtr ilp)
{
	while(ilp)
	{
		ilp->used = FALSE;
		ilp = ilp->next;
	}
}

static Int4 get_offset_of_insertion(InsertListPtr ilp, Int4 left)
{
	Int4 offset = 0;

	while(ilp)
	{
		if(ilp->master_pos >= left)
			return offset;
		else
			offset += ilp->max_size;
		ilp = ilp->next;
	}

	return offset;
}

static void modify_anp_with_insertion(AlignNodePtr anp, InsertListPtr ilp)
{
	Int4 offset;
	Int4 leftover;
	AlignSegPtr asp, next, new_asp, t_asp, prev;
	Int4 max_insert_size;
	Int4 insert_pos;
	AlignBlockPtr abp, new_abp, next_abp;
	ValNodePtr second_ms_list;


	asp = anp->segs;
	prev = NULL;
	reset_insertion_list(ilp, asp);
	offset = get_offset_of_insertion(ilp, anp->extremes.left);
	anp->extremes.left += offset;
	while(asp)
	{
		next = asp->next;
		if(asp->type == INS_SEG)
		{
			max_insert_size = find_insertion_size (ilp, &(asp->ins_pos));
			if(max_insert_size >0)
			{
				leftover = max_insert_size - asp->gr.right;	/*gr.right is the size of the insertion*/
				/*convert the insertion into a REG_SEG */
				asp->gr.left = asp->ins_pos + offset + 1;	/*insert after */
				asp->gr.right += (asp->gr.left -1);
				asp->type = REG_SEG;
				add_offset_to_featnode(asp->cnp, offset +1);

				/*insert the additional one for gaps*/
				if(leftover > 0)
				{
					new_asp = MemNew(sizeof(AlignSeg));
					new_asp->type = GAP_SEG;
					new_asp->gr.left = asp->gr.right + 1;
					new_asp->gr.right = new_asp->gr.left + leftover -1;
					asp->next = new_asp;
					new_asp->next = next;
				}
				offset += max_insert_size;
			}
			prev = asp;
		}
		else if(asp->type == GAP_SEG)
		{ /*a gap */
			max_insert_size = get_max_gap_size(ilp, asp->gr.left, asp->gr.right);
			asp->gr.left += offset;
			asp->gr.right += max_insert_size + offset;
			offset += max_insert_size;
			prev = asp;
		}
		else if(asp->type == REG_SEG)
		{	/* a diagnol */
			while( asp != NULL && (max_insert_size = 
				get_max_insert_size (ilp, asp->gr.left, 
				asp->gr.right, &insert_pos)) >0)
			{
				/*insertion at the very begining */
				if(insert_pos == -1)
				{
					new_asp = MemNew(sizeof(AlignSeg));
					new_asp->type = GAP_SEG;
					new_asp->gr.left = asp->gr.left;
					new_asp->gr.right = asp->gr.left + max_insert_size -1;
					if(prev == NULL)
						anp->segs = new_asp;
					else
						prev->next = new_asp;
					prev = new_asp;
					new_asp->next = asp;
				}
				else
				{
				if(asp->mismatch != NULL)
					second_ms_list = add_offset_to_mismatch(&(asp->mismatch), offset, 
						insert_pos, max_insert_size);
				else
					second_ms_list = NULL;

				leftover = asp->gr.right - insert_pos;
				asp->gr.left += offset;
				asp->gr.right = insert_pos + offset;

				new_asp = MemNew(sizeof(AlignSeg));
				new_asp->type = GAP_SEG;
				new_asp->gr.left = asp->gr.right + 1;
				new_asp->gr.right = insert_pos + offset + max_insert_size;
				new_asp->next = next;
				t_asp = asp;
				asp->next = new_asp;
				asp = new_asp;

				if(leftover > 0)
				{
					new_asp = MemNew(sizeof(AlignSeg));
					new_asp->type = REG_SEG;
					new_asp->gr.left = insert_pos +1;
					new_asp->gr.right = insert_pos + leftover;
					asp->next = new_asp;
					new_asp->next = next;
					new_asp->cnp = add_insertion_to_featnode(&(t_asp->cnp), offset, 
										insert_pos, max_insert_size);
					new_asp->mismatch = second_ms_list;
					new_asp->next = next;
					asp->next = new_asp;
					asp = new_asp;
					prev = new_asp;
				}
				else
				{
					add_offset_to_featnode(asp->cnp, offset);
					prev = asp;
					asp = NULL;
					break;
				}
				}

				offset += max_insert_size;
			}	/*end of while*/
			if(asp != NULL)
			{

				asp->gr.left += offset;
				asp->gr.right += offset;
				if(asp->cnp)
					add_offset_to_featnode(asp->cnp, offset);
				if(asp->mismatch != NULL)
					add_offset_to_mismatch(&(asp->mismatch), offset, -1, -1);
				prev = asp;
			}
		}
		asp = next;
	}
	anp->extremes.right += offset;

	if(offset > 0 && anp->blocks != NULL)
	{
		refresh_insertion_list(ilp);
		abp = anp->blocks;
		while(abp)
		{
			next_abp = abp->next;
			abp->next = NULL;
			offset = get_offset_of_insertion(ilp, abp->gr.left);
			while( abp && (max_insert_size = get_max_insert_size (ilp, 
					abp->gr.left, abp->gr.right, &insert_pos)) > 0)
			{
				/*insertion at the very begining */
				if(insert_pos == -1)
					offset += max_insert_size;
				else
				{
					leftover = abp->gr.right - insert_pos;
					if(leftover > 0)
					{
						new_abp = MemNew(sizeof(AlignBlock));
						new_abp->gr.left = insert_pos + 1;
						new_abp->gr.right = abp->gr.right;
						new_abp->order = abp->order;
						new_abp->next = next_abp;

						abp->gr.left += offset;
						abp->gr.right = insert_pos + offset;
						abp->next = new_abp;
						if(abp->gr.strand != Seq_strand_minus)
						{
							new_abp->gr.strand = abp->gr.strand;
							abp->gr.strand = 0;
						}
						abp = new_abp;
						offset += max_insert_size;
					}
					else	/*reach the end */
						break;
				}
			}
			if(abp != NULL)
			{
				abp->gr.left += offset;
				abp->gr.right += offset;
				abp->next = next_abp;
			}
			abp = next_abp;
		}
	}
}

static void free_insert_list(InsertListPtr ilp)
{
	InsertListPtr next;

	while(ilp)
	{
		next = ilp->next;
		MemFree(ilp);
		ilp = next;
	}
}


NLM_EXTERN Boolean FlatAlignNode(ValNodePtr anp_list)
{
	ValNodePtr curr;
	AlignNodePtr master_anp, anp;
	AnnotInfoPtr annot_info;
	Uint1 align_type;
	InsertListPtr ilp;
	AlignSegPtr asp;
	Int4 p_pos;

	
	master_anp = NULL;
	for(curr = anp_list; curr != NULL; curr = curr->next)
	{
		if(curr->choice == OBJ_SEQANNOT)
		{
			annot_info = curr->data.ptrvalue;
			align_type = get_alignment_type (annot_info);
			if(align_type == ALIGN_DNA_TO_PROT || 
				align_type == ALIGN_PROT_TO_DNA || align_type == ALIGN_TDNA_TO_TDNA)
				return FALSE;
		}
		else
		{
			anp = curr->data.ptrvalue;
			if(anp->is_master)
				master_anp = anp;
		}
	}
	if(master_anp == NULL)
		return FALSE;

	/*load all the insertions in the alignments*/
	ilp = NULL;
	for(curr = anp_list; curr != NULL; curr = curr->next)
	{
		if(curr->choice != OBJ_SEQANNOT)
		{
			anp = curr->data.ptrvalue;
			if(anp != master_anp)
			{
				p_pos = -1;
				for(asp = anp->segs; asp != NULL; asp = asp->next)
				{
					if(asp->type == INS_SEG)
						load_insertion_list(&ilp, asp->ins_pos, asp->gr.right, (Boolean)(p_pos == asp->ins_pos));
					else if(asp->type != GAP_SEG)
						p_pos = asp->gr.right;
				}
			}
		}
	}

	if(ilp == NULL)
		return FALSE;

	/*do the real flatting*/
	for(curr = anp_list; curr != NULL; curr = curr->next)
	{
		if(curr->choice != OBJ_SEQANNOT)
		{
			anp = curr->data.ptrvalue;
			modify_anp_with_insertion(anp, ilp);
		}
	}


	free_insert_list(ilp);
	return TRUE;
}


/*
*	Delete all the bad YACs from the list
*	anything on the NHGRI map that is recorded inconsistent will 
*	be considered inconsistent. For the Whitehead map, the 
*	inconsistent+ambiguous is inconsistent. Inconsistent alone 
*	is not considered inconsistent
*/
static Boolean is_ambiguous_annot(AnnotInfoPtr info, Uint1 db)
{
	if(info == NULL)
		return FALSE;
	if(info->annot_type == ANNOT_CONSIST)
	{
		if(info->consistent == ALIGN_CONSISTENT)
			return FALSE;
		else
		{
			if(info->consistent == ALIGN_INCONSISTENT)
			{
				if(db == YAC_NHGRI)
					return TRUE;
				else if(db == YAC_MIT)
				{	/*inconsistent and ambiguous are different*/
					if(StringCmp(info->annotDB, "Ambiguous") == 0)
					
						return TRUE;
					else
						return FALSE;
				}
			}
		}
	}

	return FALSE;
}

/*
*	delete any of the whitehead yacs that only contains 
*	ambiguous STS hits
*/
static Boolean delete_alignnode (AlignNodePtr anp, Uint1 db)
{
	AlignSegPtr asp;
	ValNodePtr curr;
	Boolean has_sts_hits;

	has_sts_hits = FALSE;
	for(asp = anp->segs; asp != NULL; asp = asp->next)
	{
		if(asp->mismatch != NULL)
		{
			for(curr = asp->mismatch; curr != NULL; curr = curr->next)
			{
				if(curr->choice == MISMATCH_CLOSE)	/*unambiguous hits*/
					return FALSE;
				if(curr->choice == MISMATCH_SQUARE)
				{
					if(db == YAC_NHGRI)
						return FALSE;
				}
			}
			has_sts_hits = TRUE;
		}
	}

	return (has_sts_hits == FALSE);
}


/*
*	Delete all the bad YACs from the list
*	anything on the NHGRI map that is recorded inconsistent will 
*	be considered inconsistent. For the Whitehead map, the 
*	inconsistent+ambiguous is inconsistent. Inconsistent alone 
*	is not considered inconsistent
*/
NLM_EXTERN void CleanUpAmbiguousYAC (ValNodePtr PNTR anp_node, Uint1 db, SeqIdPtr chr_id)
{
	AnnotInfoPtr info;
	AlignNodePtr anp;
	ValNodePtr curr, prev, next;
	Boolean del_annot;
	Boolean del;

	prev = NULL;
	del_annot = FALSE;

	curr = *anp_node;
	while(curr)
	{
		next = curr->next;
		del = FALSE;
		if(curr->choice == OBJ_SEQANNOT)
		{
			info = curr->data.ptrvalue;
			del_annot = is_ambiguous_annot(info, db);
		}
		else if(!del_annot || chr_id != NULL)
		{
			anp = curr->data.ptrvalue;
			if(chr_id != NULL)
			{
				if(anp->sip != NULL)
				{
					if(SeqIdMatch(chr_id, anp->sip))
						del = TRUE;
				}
			}
			if(!del)
				del = delete_alignnode (anp, db);
		}
		if(del_annot)
			del = TRUE;

		if(del)
		{
			if(prev == NULL)
				*anp_node = next;
			else
				prev->next = next;
			curr->next = NULL;
			FreeAlignNode(curr);
		}
		else
			prev = curr;
		curr = next;
	}
}


	
/*****************************************************************
*
*	check if the AlignNode only contains Seq-annot or it 
*	has real sequence alignment. 
*	the empty Seq-annot may be the unaligned contigs in 
*	Eric Green's map
*
******************************************************************/
NLM_EXTERN Boolean alignode_has_alignments(ValNodePtr aligns)
{
	while(aligns)
	{
		if(aligns->choice != OBJ_SEQANNOT)
			return TRUE;
		aligns = aligns->next;
	}

	return FALSE;
}