File: asn2ff6.c

package info (click to toggle)
ncbi-tools6 6.1.20170106%2Bdfsg1-0%2Bdeb10u2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 468,492 kB
  • sloc: ansic: 1,474,204; pascal: 6,740; cpp: 6,248; xml: 3,390; sh: 2,137; perl: 1,084; csh: 508; makefile: 427; ruby: 93; lisp: 81
file content (3845 lines) | stat: -rw-r--r-- 102,860 bytes parent folder | download | duplicates (11)
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
/*   asn2ff6.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*            National Center for Biotechnology Information (NCBI)
*
*  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 do not place any restriction on its use or reproduction.
*  We would, however, appreciate having the NCBI and the author cited in
*  any work or product based on this material
*
*  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.
*
* ===========================================================================
*
* File Name:  asn2ff6.c
*
* Author:  Karl Sirotkin, Tom Madden, Tatiana Tatusov
*
* Version Creation Date:   7/15/95
*
* $Revision: 6.69 $
*
* File Description: 
*
* Modifications:  
* --------------------------------------------------------------------------
* $Log: asn2ff6.c,v $
* Revision 6.69  2006/07/13 17:06:38  bollin
* use Uint4 instead of Uint2 for itemID values
* removed unused variables
* resolved compiler warnings
*
* Revision 6.68  2002/02/15 18:30:55  kans
* no longer change snoRNA to misc_RNA
*
* Revision 6.67  2001/12/28 21:37:10  kans
* allow sfp->product to be SEQLOC_EQUIV
*
* Revision 6.66  2001/12/21 20:21:06  cavanaug
* old_locus_fmt now controls generated of *old* LOCUS line format
*
* Revision 6.65  2001/12/05 18:13:53  cavanaug
* Changes for new LOCUS line format
*
* Revision 6.64  2001/08/21 17:33:33  kans
* snoRNA can show /product
*
* Revision 6.63  2001/08/07 15:51:08  kans
* use NUM_SEQID, added third party annotation seqids
*
* Revision 6.62  2001/07/18 14:50:13  kans
* gather features with gsc.useSeqMgrIndexes if genpept, raw, indexing requested, and IndexedGetDescrForDiv to speed up finding division
*
* Revision 6.61  2001/07/03 20:01:41  kans
* AddGBQual ASN2GNBK_STRIP_NOTE_PERIODS trim trailing tilde first
*
* Revision 6.60  2001/07/03 00:05:51  kans
* TrimSpacesAndJunkFromEnds on genbankblock->source if ASN2GNBK_STRIP_NOTE_PERIODS
*
* Revision 6.59  2001/06/26 23:43:35  kans
* moved second period check to inside last period check
*
* Revision 6.58  2001/06/26 23:36:06  kans
* in AddGBQual if ASN2GNBK_STRIP_NOTE_PERIODS, trim one or two periods at end
*
* Revision 6.57  2001/06/13 14:41:58  yaschenk
* changing increment of 10 to 1024 in EnlargeSortList()
*
* Revision 6.56  2001/06/04 21:30:52  kans
* TrimSpacesAndSemicolons trims leading semicolons as well as leading spaces
*
* Revision 6.55  2001/06/01 18:46:26  tatiana
* NG_ added to ValidateAccession
*
* Revision 6.54  2001/05/31 23:45:48  kans
* if ASN2GNBK_STRIP_NOTE_PERIODS and IsEllipsis, do not strip period
*
* Revision 6.53  2001/05/29 23:27:47  kans
* added support for snoRNA - flatfile prints as misc_RNA for now
*
* Revision 6.52  2001/04/16 16:51:42  tatiana
* GetDivision(): CON division never use for aa
*
* Revision 6.51  2001/04/06 12:47:43  beloslyu
* missing flatloc declaration was added
*
* Revision 6.50  2001/04/05 21:41:26  tatiana
* REGION added in GetLocusPartsAwp()
*
* Revision 6.49  2001/04/04 22:05:16  kans
* In GB_PrintPubs under ASN2GNBK_STRIP_NOTE_PERIODS clean up comma/space/semicolon (TF)
*
* Revision 6.48  2001/04/04 21:46:56  kans
* TrimSpacesAndJunkFromEnds if ASN2GNBK_STRIP_NOTE_PERIODS (TF)
*
* Revision 6.47  2001/04/02 21:25:19  kans
* AddGBQual under ASN2GNBK_STRIP_NOTE_PERIODS also removes ; ; substrings
*
* Revision 6.46  2001/03/26 17:36:06  kans
* added NULL for endogenous-virus to genome prefix array
*
* Revision 6.45  2001/02/16 16:52:22  tatiana
* special case locus for NT_ records
*
* Revision 6.44  2001/01/26 19:21:48  kans
* extrachromosomal into source note, removed macronuclear, extrachrom, plasmid from organism line
*
* Revision 6.43  2001/01/19 21:51:04  kans
* finally got ASN2GNBK_STRIP_NOTE_PERIODS logic right
*
* Revision 6.42  2001/01/19 18:45:28  kans
* another attempt to use ASN2GNBK_STRIP_NOTE_PERIODS to remove extraneous asn2ff/asn2gnbk diffs
*
* Revision 6.41  2001/01/08 18:36:40  kans
* removed ASN2GNBK_STRIP_NOTE_PERIODS - this was not the right place
*
* Revision 6.40  2001/01/06 22:09:42  kans
* added ASN2GNBK_STRIP_NOTE_PERIODS to try to eliminate trivial note discrepancies
*
* Revision 6.39  2000/11/29 20:46:11  tatiana
* HTC division added for MI_TECH_htc
*
* Revision 6.38  2000/10/24 20:28:44  tatiana
* ValidateAccession accepts XP, XM
*
* Revision 6.37  2000/09/20 21:26:19  tatiana
* all organelles adde to ORGANISM line
*
* Revision 6.36  2000/09/11 18:52:59  tatiana
* PUBMED linetype is legal in release mode
*
* Revision 6.35  2000/08/25 16:16:46  kans
* ValidateLocus initializes num_of_digits even if > 1000 segments
*
* Revision 6.34  2000/08/01 21:09:39  tatiana
* ValidateVersion is colld in forgbrel option only
*
* Revision 6.33  2000/06/29 12:23:30  kans
* GenPept on Seq_repr_virtual shown only if is_www || ajp->mode != RELEASE_MODE, earlier kludge of ignoring get_www was probably too broad
*
* Revision 6.32  2000/06/28 19:31:22  kans
* in SeqToAwp always set is_www to TRUE, so virtual sequences show up on non-web applications
*
* Revision 6.31  2000/06/23 15:42:34  tatiana
* removed virion and proviral from ORGANISM line
*
* Revision 6.30  2000/06/21 15:04:57  tatiana
* space added to Virion
*
* Revision 6.29  2000/06/12 20:49:04  tatiana
* new organelles added to ORGANISM filed
*
* Revision 6.28  2000/06/05 17:51:53  tatiana
* increase size of feature arrays to Int4
*
* Revision 6.27  2000/02/09 19:34:39  kans
* added forgbrel flag to Asn2ffJobPtr, currently used to suppress PUBMED line, which was not formally announced in release notes
*
* Revision 6.26  2000/01/28 17:56:48  kans
* show_gi always FALSE to suppress NID and PID, added support for PUBMED line in GenBank format
*
* Revision 6.25  2000/01/18 17:09:24  tatiana
* NP added to ValidateAccession
*
* Revision 6.24  1999/10/06 20:20:24  bazhin
* Removed memory leaks in GeneStructContentFree() and GetPubsAwp()
* functions.
*
* Revision 6.23  1999/09/23 18:09:33  tatiana
* ValidateAccession modified for N*_ accession
*
* Revision 6.22  1999/09/15 18:17:12  tatiana
* GRAPHIK_FMT corrected
*
* Revision 6.18  1999/04/02 19:33:55  tatiana
* MI_TECH_htgs_0 added in BioseqGetGBDivCode()
*
* Revision 6.17  1999/04/01 20:44:12  kans
* Int2 lengths to Int4 to allow CountGapsInDeltaSeq with buffer > 32K
*
* Revision 6.16  1999/03/31 01:09:23  tatiana
* ValidateAccession accepts 3+5
*
* Revision 6.15  1999/03/30 21:00:45  tatiana
*  ValidateOtherAccession() added
*
* Revision 6.14  1999/03/22 23:22:32  tatiana
* accession.version modifications
*
* Revision 6.13  1999/01/12 16:57:55  kans
* SeqToAwp checks for null ep before dereferencing
*
* Revision 6.12  1998/11/24 20:15:03  kans
* seqid other has better priority than local so refgene id is used preferentially
*
* Revision 6.11  1998/10/30 01:12:00  kans
* GetPubsAwp GatherEntity filters out OBJ_SEQALIGN - this was being hit many times on big records, and there is no need for asn2ff to see alignments
*
* Revision 6.10  1998/09/24 17:46:00  kans
* fixed GetDBXrefFromGene problem (TT)
*
* Revision 6.9  1998/06/15 14:59:49  tatiana
* UNIX compiler warnings fixed
*
* Revision 6.8  1998/05/11 21:58:33  tatiana
* some functions moved from asn2ff1.c
*
* Revision 6.7  1998/05/05 19:53:50  tatiana
* SEQFEAT_RSITE supressed in GetNAFeatKey()
*
* Revision 6.6  1998/04/30 21:49:10  tatiana
* *** empty log message ***
*
* Revision 6.5  1998/02/10 17:01:14  tatiana
* AddGBQualEx() added
*
* Revision 6.4  1998/01/13 21:35:20  tatiana
*  AsnIoHash moved to asnio.c file
*
* Revision 6.3  1998/01/13 21:14:50  tatiana
* static AsnIoHash changed to AsnIoHash to avoid fubction name collision
*
* Revision 6.2  1997/12/15 15:53:29  tatiana
* features processing has been changed
*
* Revision 6.1  1997/09/16 15:41:49  kans
* added SEQFEAT_SITE case to GetNAFeatKey (TT)
*
* Revision 5.25  1997/07/28 19:03:59  vakatov
* [WIN32,MSVC++]  Restored lost "NCBIOBJ.LIB" pro-DLL modifications
*
 * Revision 5.24  1997/07/28 14:26:11  vakatov
 * BioseqGetGBDivCode() proto in-sync with its header-located declaration
 *
 * Revision 5.23  1997/07/24 23:57:41  tatiana
 * fixed sfp_order
 *
 * Revision 5.22  1997/07/24 15:59:06  tatiana
 * aaaaaaa bug fixed in Getscblknum
 *
 * Revision 5.21  1997/07/16 21:18:42  tatiana
 *  added sorting by feat type in CompareSfpForHeap()
 *
 * Revision 5.20  1997/06/19 18:37:17  vakatov
 * [WIN32,MSVC++]  Adopted for the "NCBIOBJ.LIB" DLL'ization
 *
 * Revision 5.19  1997/05/21 14:43:27  tatiana
 * fix empty /product in GetNAFeatKey
 *
 * Revision 5.17  1997/01/13  22:33:04  tatiana
 * added CompareGeneName()
 *
 * Revision 5.16  1996/12/17  22:47:56  tatiana
 * added StoreFeatFree()
 *
 * Revision 5.15  1996/10/25  22:12:10  tatiana
 * doesn't add empty ("") val if qual is translation
 *
 * Revision 5.14  1996/10/02  15:14:38  tatiana
 * a bug fixed
 *
 * Revision 5.13  1996/10/01  22:42:09  tatiana
 * fixed duplicated notes in NoteToCharPtrStack
 *
 * Revision 5.12  1996/09/09  13:36:02  kans
 * moved BioseqGetGBDivCode from toasn.[ch] to asn2ff.h/asn2ff6.c
 *
 * Revision 5.11  1996/09/03  19:52:49  tatiana
 * extra_loc added
 *
 * Revision 5.10  1996/08/28  21:40:35  tatiana
 * don't copy new location from gather
 *
 * Revision 5.9  1996/08/16  20:34:45  tatiana
 * GetNAFeatKey() changed
 *
 * Revision 5.7  1996/08/09  21:08:57  tatiana
 * a bug fixed in GetNAFeatKey
 *
 * Revision 5.6  1996/07/30  16:35:05  tatiana
 * Boolean new added to GetNaFeatKey()
 *
 * Revision 5.5  1996/07/19  21:38:15  tatiana
 * ERR_GI_No_GI_Number changed from 	ErrPostEx to	ErrPostStr
 *
 * Revision 5.3  1996/07/02  18:11:18  tatiana
 * calculate hash in StoreFeat
 *
 * Revision 5.2  1996/06/14  18:05:03  tatiana
 * GetNAFeatKey change
 *
 * Revision 5.1  1996/06/11  15:26:36  tatiana
 * GetGINumber is modified to get also embl NI
 *
 * Revision 4.17  1996/05/16  21:00:52  tatiana
 * RemoveRedundantFeats addded
 *
 * Revision 4.16  1996/04/29  18:51:42  tatiana
 * whole_book format added
 *
 * Revision 4.15  1996/04/15  14:36:23  tatiana
 * memory leaks cleaning
 *
 * Revision 4.13  1996/02/28  04:53:06  ostell
 * changes to support segmented master seeuquences
 *
 * Revision 4.12  1996/02/15  15:54:51  tatiana
 * minor clean ups
 *
 * Revision 4.11  1996/01/29  22:39:10  tatiana
 * error posting MODULE
 *
 * Revision 4.10  1995/12/20  22:41:56  tatiana
 * removed redundant functions
 *
 * Revision 4.9  1995/12/12  20:21:05  tatiana
 * CitSub validation fixed
 *
 * Revision 4.8  1995/12/10  22:19:31  tatiana
 * Imprint in CitSub became optional
 *
 * Revision 4.7  1995/11/17  21:28:35  kans
 * asn2ff now uses gather (Tatiana)
 *
 * Revision 4.2  1995/08/04  15:26:42  tatiana
 * bug fixed in GetPubDate (check for Null pointer).
 *
 * Revision 4.1  1995/08/01  14:53:08  tatiana
 * change SeqIdPrint to SeqIdWrite
 *
 * Revision 1.57  1995/07/17  19:33:20  kans
 * parameters combined into Asn2ffJobPtr structure
* ==========================================================================
*/

#include <asn2ff6.h>
#include <asn2ffp.h>
#include <a2ferrdf.h>
#include <asn2ffg.h>
#include <utilpub.h>
#include <ffprint.h>
#include <explore.h>
#include <sqnutils.h>

#define BUF_EXT_LENGTH 4

/*---------- order for other id FASTA_LONG (copied from SeqIdWrite) ------- */

static Uint1 fasta_order[NUM_SEQID] = {  
33, /* 0 = not set */
20, /* 1 = local Object-id */
15,  /* 2 = gibbsq */
16,  /* 3 = gibbmt */
30, /* 4 = giim Giimport-id */
10, /* 5 = genbank */
10, /* 6 = embl */
10, /* 7 = pir */
10, /* 8 = swissprot */
15,  /* 9 = patent */
10, /* 10 = other TextSeqId */
20, /* 11 = general Dbtag */
32,  /* 12 = gi */
10, /* 13 = ddbj */
10, /* 14 = prf */
12, /* 15 = pdb */
10,  /* 16 = tpg */
10,  /* 17 = tpe */
10   /* 18 = tpd */
};


static Uint1 sfp_order[21] = {0, 
2, /* SEQFEAT_GENE */
5, /* SEQFEAT_ORG */
3, /* SEQFEAT_CDREGION */
5, /* SEQFEAT_PROT */
1, /* SEQFEAT_RNA */
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5};

SeqFeatPtr MakeSyntheticSeqFeat PROTO ((void));
ValNodePtr LookForPubsOnFeat PROTO ((SeqFeatPtr sfp, ValNodePtr PubOnFeat));

Boolean asn2ff_flags[13];

NLM_EXTERN GeneStructPtr GeneStructNew (void)
{
	GeneStructPtr gsp;
	
	gsp = (GeneStructPtr) MemNew(sizeof(GeneStruct));
	gsp->gene = NULL;
	gsp->product = NULL;
	gsp->standard_name = NULL;
	gsp->map = (CharPtr PNTR) MemNew(sizeof(CharPtr));
	gsp->map_index = 0;
	gsp->map_size = 1;
	gsp->ECNum = NULL;
	gsp->activity = NULL;
	gsp->grp = NULL;

	return gsp;
}

NLM_EXTERN NoteStructPtr NoteStructNew (NoteStructPtr nsp)
{
	nsp = (NoteStructPtr) MemNew(sizeof(NoteStruct));

	nsp->note = (CharPtr PNTR) MemNew(5*sizeof(CharPtr));
	nsp->note_annot = (CharPtr PNTR) MemNew(5*sizeof(CharPtr));
	nsp->note_alloc = (Uint1 PNTR) MemNew(5*sizeof(Uint1));
	nsp->note_index = 0;
	nsp->note_size = 5;

	return nsp;
}

static void GeneStructContentFree(GeneStructPtr gsp)
{
	ValNodePtr v, vnext;
	
	if (gsp->gene) {
		if (gsp->gene->data.ptrvalue != NULL) {
			MemFree(gsp->gene->data.ptrvalue);
		}
		gsp->gene = ValNodeFree(gsp->gene);
	}
	for (v = gsp->product; v; v = vnext) {
		vnext = v->next;
		if (v->data.ptrvalue != NULL) {
			MemFree(v->data.ptrvalue);
		}
		MemFree(v);
	}
	for (v = gsp->standard_name; v != NULL; v = vnext) {
		vnext = v->next;
		if (v->data.ptrvalue != NULL) {
			MemFree(v->data.ptrvalue);
		}
		MemFree(v);
	}
	for (v = gsp->ECNum; v; v = vnext) {
		vnext = v->next;
		if (v->data.ptrvalue != NULL) {
			MemFree(v->data.ptrvalue);
		}
		MemFree(v);
	}
	for (v = gsp->activity; v; v = vnext) {
		vnext = v->next;
		if (v->data.ptrvalue != NULL) {
			MemFree(v->data.ptrvalue);
		}
		MemFree(v);
	}
	if (gsp->grp) {
		GeneRefFree(gsp->grp);
	}
	return;
}

NLM_EXTERN void GeneStructFree (GeneStructPtr gsp)
{
	if (gsp == NULL)
		return;
	gsp->map = MemFree(gsp->map);
	GeneStructContentFree(gsp);
	MemFree(gsp);
}

NLM_EXTERN void NoteStructFree (NoteStructPtr nsp)
{
	Int2 index;

	if (nsp == NULL) {
	return;
	}
	for (index=0; index < nsp->note_index; index++) {
			if (nsp->note_alloc[index] == ASN2FLAT_ALLOC)
				nsp->note[index] = MemFree(nsp->note[index]);
	}
	nsp->note = MemFree(nsp->note);
	nsp->note_annot = MemFree(nsp->note_annot);
	nsp->note_alloc = MemFree(nsp->note_alloc);
	MemFree(nsp);
}

NLM_EXTERN void NoteStructReset (NoteStructPtr nsp)
{
	Int2 index;

	if (nsp == NULL) {
		return;
	}
	for (index=0; index<nsp->note_index; index++)
	{
		if (nsp->note_alloc[index] == ASN2FLAT_ALLOC)
			nsp->note[index] = MemFree(nsp->note[index]);
		nsp->note[index] = NULL;
		nsp->note_annot[index] = NULL;
	}
	nsp->note_index = 0;
}


NLM_EXTERN void ListFree (SeqFeatPtr PNTR PNTR List, Int4 range)
{
	Int4 index;

	for (index=0; index < range; index++)
		MemFree(List[index]);

	MemFree(List);
}

/***********************************************************************
*SeqFeatPtr MakeSyntheticSeqFeat(void) 
*
*	This function allocates a "synthetic" SeqFeatPtr, which is
*	used to print the SeqFeats out.  To print out SeqFeats, they
*	are copied to this "synthetic" sfp, which is an ImpFeat, 
*	adjusted, validated, and then a function prints out this ImpFeat.
*************************************************************************/

NLM_EXTERN SeqFeatPtr MakeSyntheticSeqFeat(void) 
{
	ImpFeatPtr ifp;
	SeqFeatPtr sfp_out;

	sfp_out = SeqFeatNew();
	if (sfp_out)
	{
		sfp_out->data.choice = SEQFEAT_IMP;
		sfp_out->qual = NULL;
		ifp = sfp_out->data.value.ptrvalue = ImpFeatNew();
	/*	ifp->key = (CharPtr) MemNew(20*sizeof(Char)); */
/* key may be more than 20 char one day and cause segmentation fault */
		ifp->key = NULL;
		ifp->loc = NULL;
		sfp_out->comment = NULL;
		sfp_out->location = NULL;
		sfp_out->product = NULL;
	}

	return sfp_out;
}

NLM_EXTERN void CpNoteToCharPtrStack (NoteStructPtr nsp, CharPtr annot, CharPtr string)
{
	NoteToCharPtrStack(nsp, annot, string, ASN2FLAT_NOT_ALLOC);
	return;
}

NLM_EXTERN void SaveNoteToCharPtrStack (NoteStructPtr nsp, CharPtr annot, CharPtr string)
{
	NoteToCharPtrStack(nsp, annot, string, ASN2FLAT_ALLOC);
	return;
}


NLM_EXTERN void NoteToCharPtrStack (NoteStructPtr nsp, CharPtr annot, CharPtr string, Uint1 alloc)
{
	Int2 index, note_size;

	if (nsp)
	{
		note_size = nsp->note_size;
		index = nsp->note_index;
	}
	else
		return;
/*** add check for duplicated notes 9-27-96 ***/
/*	if (string[StringLen(string)-1] == '.')
		string[StringLen(string)-1] = '\0';
	for (i = 0; i < note_size; i++) {
		if (nsp->note[i] && StringStr(nsp->note[i], string) != NULL) {
			return;
		}
	}
*/
	if (index == note_size)
		EnlargeCharPtrStack(nsp, 5);

	nsp->note_annot[index] = annot;

	if (alloc == ASN2FLAT_NOT_ALLOC)
	{
		nsp->note_alloc[index] = ASN2FLAT_NOT_ALLOC;
		nsp->note[index] = string;
	}
	else if (alloc == ASN2FLAT_ALLOC)
	{
		nsp->note_alloc[index] = ASN2FLAT_ALLOC;
		nsp->note[index] = StringSave(string);
	}

	nsp->note_index++;

	return;
}

NLM_EXTERN void EnlargeCharPtrStack (NoteStructPtr nsp, Int2 enlarge)
{
	CharPtr PNTR newstr;
	CharPtr PNTR new_annot;
	Int2 index;
	Uint1 PNTR new_alloc;

	newstr = (CharPtr PNTR) MemNew((size_t) 
		((enlarge+(nsp->note_size))*sizeof(CharPtr)));
	new_annot = (CharPtr PNTR) MemNew((size_t) 
		((enlarge+(nsp->note_size))*sizeof(CharPtr)));
	new_alloc = (Uint1 PNTR) MemNew((size_t) 
		((enlarge+(nsp->note_size))*sizeof(Uint1)));

	for (index=0; index<(nsp->note_size); index++) {
		newstr[index] = nsp->note[index];
		new_annot[index] = nsp->note_annot[index];
		new_alloc[index] = nsp->note_alloc[index];
	}
	nsp->note_size += enlarge;
	nsp->note = MemFree(nsp->note);
	nsp->note_annot = MemFree(nsp->note_annot);
	nsp->note_alloc = MemFree(nsp->note_alloc);
	nsp->note = newstr;
	nsp->note_annot = new_annot;
	nsp->note_alloc = new_alloc;
}

NLM_EXTERN SortStructPtr EnlargeSortList(SortStructPtr List, Int4 size)
{
	SortStructPtr NewList;

	if (size % 1024 == 0) {
		NewList = (SortStructPtr) MemNew((size+1024)*sizeof(SortStruct));
		if (size > 0) {
			MemCopy(NewList, List, (size * sizeof(SortStruct)));
			MemFree(List);
		}
		return NewList;
	}
	return List;

}	/* EnlargeSortList */

NLM_EXTERN int LIBCALLBACK CompareSfpForHeap (VoidPtr vp1, VoidPtr vp2)
{

	SortStructPtr sp1 = vp1;
	SortStructPtr sp2 = vp2;
	BioseqPtr bsp;
	SeqFeatPtr sfp1, sfp2;
	Int2 status = 0;

	bsp = sp1->bsp;
	sfp1 = sp1->sfp;
	sfp2 = sp2->sfp;
	if (sfp1 == NULL || sfp2 == NULL) {
		return status;
	}

	status = SeqLocOrder(sfp1->location, sfp2->location, bsp);

	if (ABS(status) >= 2 && sp1->seg_bsp) {
		status = SeqLocOrder(sfp1->location, sfp2->location, sp1->seg_bsp);
	}
	if (status == 0 && sfp1->data.choice < 6 && sfp2->data.choice < 6) {
		status = sfp_order[sfp1->data.choice] - sfp_order[sfp2->data.choice];
	}
	return status;
}
NLM_EXTERN int LIBCALLBACK CompareGeneName (VoidPtr vp1, VoidPtr vp2)
{

	SortStructPtr sp1 = vp1;
	SortStructPtr sp2 = vp2;
	SeqFeatPtr sfp1, sfp2;
	GeneRefPtr gr1, gr2;
	Int2 status = 0;

	sfp1 = sp1->sfp;
	sfp2 = sp2->sfp;
	if (sfp1 == NULL || sfp2 == NULL) {
		return status;
	}
	if (sfp1->data.choice != SEQFEAT_GENE)
		return status;
	if (sfp2->data.choice != SEQFEAT_GENE)
		return status;
	gr1 = (GeneRefPtr) sfp1->data.value.ptrvalue;	
	gr2 = (GeneRefPtr) sfp2->data.value.ptrvalue;
	if (gr1 == NULL || gr2 == NULL)
		return status;
	status = StringCmp(gr1->locus, gr2->locus);

	return status;
}

/**************************************************************************
*	This function returns the gi number
*	If no gi number is found, -1 is returned  and a warning is
*	issued.
*	06-10-96
*	This fubction is changed to void. It will find NCBI gi and embl ni
*	and fill up gbp structure
**************************************************************************/

NLM_EXTERN void GetGINumber(GBEntryPtr gbp)
{
	Boolean 	found_gi;
	ValNodePtr 	vnp;
	Int4 		gi = -1;
	CharPtr 	ni = NULL;
    DbtagPtr	dbtag;
    ObjectIdPtr oid;

	found_gi = FALSE;
	if (gbp == NULL)
		return;
	if (gbp->bsp == NULL)
		return;
	for (vnp=gbp->bsp->id; vnp; vnp=vnp->next) {
		if (vnp->choice == SEQID_GI) {
			gi = vnp->data.intvalue;
			if (gi != 0) {
				found_gi = TRUE;
				break;
			} else {
				if (ASN2FF_SHOW_ERROR_MSG == TRUE) {
					ErrPostEx(SEV_WARNING, ERR_GI_No_GI_Number, 
									"Zero gi number: %d", gi);
				}
			}
		} else if (vnp->choice == SEQID_GENERAL) {
			dbtag = vnp->data.ptrvalue;
			if (StringCmp(dbtag->db, "NID") == 0) {
				oid = dbtag->tag;
				if (oid->str) {
					ni = StringSave(oid->str);
				}
			}
		}
	}
	if (! found_gi) {
		if (ASN2FF_SHOW_ERROR_MSG == TRUE) {
			ErrPostStr(SEV_WARNING, ERR_GI_No_GI_Number, "");
		}
		gi = -1;
	}
	gbp->gi = gi;
	gbp->ni = ni;
	return;
}

/***********************************************************************
*
*	GetGIs gets the GI's.
*
************************************************************************/
NLM_EXTERN void GetGIs (Asn2ffJobPtr ajp)
{
	GBEntryPtr gbp;
		
	for (gbp = ajp->asn2ffwep->gbp; gbp; gbp = gbp->next) {
		GetGINumber(gbp);
	}
	return;
}

NLM_EXTERN SeqIdPtr GetProductSeqId(ValNodePtr product)
{
	SeqIdPtr sip=NULL;
	SeqIntPtr seq_int;
	SeqLocPtr slp;

	if (product)
	{
		if (product->choice == SEQLOC_WHOLE)
		{
			sip = (SeqIdPtr) product->data.ptrvalue;
		} 
		else if (product->choice == SEQLOC_INT)
		{
			seq_int = (SeqIntPtr) product->data.ptrvalue;
			sip = seq_int->id;
		}
		else if (product->choice == SEQLOC_EQUIV)
		{
			for (slp = (SeqLocPtr) product->data.ptrvalue; slp != NULL; slp = slp->next) {
				sip = GetProductSeqId (slp);
				if (sip != NULL) return sip;
			}
		}
	}
	return sip;
}

/*****************************************************************************
*check_range
*
*	This function is called by the gbparse functions of Karl Sirotkin 
*	and determines if the length of a BioSeqPtr is sensible.
*	Pointer data is not used !! Tatiana !!
*	Tom Madden
*****************************************************************************/

NLM_EXTERN Int4 check_range(Pointer data, SeqIdPtr seq_id)

{
	BioseqPtr bsp;

	bsp = BioseqFind(seq_id);
	if (bsp)
		return bsp->length;
	else
		return 0;
}	/* check_range */

/****************************************************************************
*do_loc_errors
*
*	This function is called both by the gbparse functions of Karl Sirotkin
*	and by asn2ff.  If called by gbparse, error messages are stored in 
*	buffers and a flag is set; if called by asn2ff, the error messages
*	are retrieved and the flag reset.
*	
***************************************************************************/

NLM_EXTERN void do_loc_errors(CharPtr front, CharPtr details)
{
	ErrPostEx(SEV_INFO, ERR_FEATURE_Bad_location, "%s: %s\n", front, details);
}

/***************************************************************************
*do_no_loc_errors
*
*	Is used when no error messages are wanted.
****************************************************************************/

NLM_EXTERN void do_no_loc_errors(CharPtr front, CharPtr details)
{
	return;
}

/***************************************************************************
*Boolean GBQualPresent(CharPtr ptr, GBQualPtr gbqual)
*
*This function check that a qual, that is to be added to the list of qual
*isn't already present.
***************************************************************************/
NLM_EXTERN Boolean GBQualPresent(CharPtr ptr, GBQualPtr gbqual)

{
	Boolean present=FALSE;
	GBQualPtr qual;

	for (qual=gbqual; qual; qual=qual->next)
		if (StringCmp(ptr, qual->qual) == 0)
		{
			present = TRUE;
			break;
		}

	return present;
}	/* GBQualPresent */

/**********************************************************************
*Boolean GetNAFeatKey(CharPtr buffer, SeqFeatPtr sfp)
*
*	This function places the sfp "key" in buffer and returns TRUE
*	if successful, it returns FALSE if not successful.
*	This function only works for nucleic acid sequences, as the
*	keys (for corresponding numbers) are different for peptides.
***********************************************************************/

NLM_EXTERN Boolean GetNAFeatKey(Boolean is_new, CharPtr PNTR buffer, SeqFeatPtr sfp, SeqFeatPtr sfp_out)
{

	Boolean retval=TRUE;
	ImpFeatPtr ifp;
	RnaRefPtr rrp;
	CharPtr str = NULL;
	Int2 index;


	switch (sfp->data.choice)
	{
	case SEQFEAT_GENE:	/* gene becomes misc_feat for purposes of CheckNAFeat */
		if (is_new) {
			*buffer = StringSave("gene");
		} else {
			*buffer = StringSave("misc_feature");
		}
		break;
	case SEQFEAT_CDREGION:
		*buffer = StringSave("CDS");
		break;
	case SEQFEAT_RNA:
		rrp = sfp->data.value.ptrvalue;
		/* the following code was taken (almost) directly from Karl
		Sirotkin's code.					*/
		switch ( rrp -> type){ 
			case 1:
				*buffer =StringSave("precursor_RNA");
				break;
			case 2:
				*buffer = StringSave("mRNA");
				break;
			case 3:
				*buffer = StringSave("tRNA");
				break;
			case 4:
				*buffer = StringSave("rRNA");
				break;
			case 5:
				*buffer = StringSave("snRNA");
				break;
			case 6:
				*buffer = StringSave("scRNA");
				break;
			case 7:
				*buffer = StringSave("snoRNA"); /* snoRNA */
				break;
			case 255:
				*buffer = StringSave("misc_RNA");
				break;
		}
		switch ( rrp -> type){ 
			case 2:
			case 4:
			case 5:
			case 6:
			case 7:
			case 255:
				if (rrp ->ext.choice == 1 && sfp_out) {
					 str = rrp->ext.value.ptrvalue;
					 if (str != NULL && *str != '\0') {
					 	index = GBFeatKeyNameValid(buffer, FALSE);
					 	if (GBQualValidToAdd(index, "product")) {
							sfp_out->qual = AddGBQual(sfp_out->qual, 
									"product", str);
						}
					}
				}
				break;
		}
		break;
	case SEQFEAT_IMP:
		ifp = (ImpFeatPtr) sfp->data.value.ptrvalue;
		*buffer = StringSave(ifp->key); 
		break;
	case SEQFEAT_SEQ:
	case SEQFEAT_SITE:
	case SEQFEAT_REGION:
	case SEQFEAT_COMMENT:
		*buffer = StringSave("misc_feature");
		break;
	case SEQFEAT_BIOSRC:
		*buffer = StringSave("source");
		break;
	case SEQFEAT_RSITE:
	default:
		retval = FALSE;	
		break;
	}

	return retval;
}	/* GetNAFeatKey */

/**************************************************************************
*SeqIdPtr CheckXrefFeat (BioseqPtr bsp, SeqFeatPtr sfp)
*
* 	First the location of the xref is checked to see if it overlaps 
*	the sequence.  If this feature has a xref that is NOT of type 
*	genbank, embl, or ddbj, it is put out as a misc_feat.  If it's 
*	one of genbank, embl, or ddbj, it has been put out as a second 
*	accession.  If the feature should be put out as a misc_feat, then
*	the SeqIdPtr (xid) is returned, otherwise NULL.
**************************************************************************/

NLM_EXTERN SeqIdPtr CheckXrefFeat (BioseqPtr bsp, SeqFeatPtr sfp)
{

	SeqIdPtr xid=NULL;
	SeqIntPtr si;
	SeqLocPtr xref;
	ValNodePtr location;
			
	location = ValNodeNew(NULL);
	si = SeqIntNew();
	location->choice = SEQLOC_INT;
	location->data.ptrvalue = si;
	si->from = 0;
	si->to = bsp->length - 1;
	si->id = bsp->id;	/* Don't delete id!! */
	if (SeqLocCompare(sfp->location, location) != 0)
	{
		xref = (SeqLocPtr) sfp->data.value.ptrvalue;
		xid = (SeqIdPtr) xref->data.ptrvalue;
		if (xid->choice != 5 && xid->choice != 6 && xid->choice != 13)
			;
		else
			xid = NULL;
	}
	si->id = NULL;
	SeqIntFree(si);
	ValNodeFree(location);
	return xid;
}

NLM_EXTERN Int4 GetGINumFromSip (SeqIdPtr sip)
{
	Int4 gi = -1;
	ValNodePtr vnp;

	for (vnp=sip; vnp; vnp=vnp->next)
		if (vnp->choice == SEQID_GI)
			gi = vnp->data.intvalue;

	return gi;
}

/*****************************************************************************
*FlatRefBest
*
*	returns ValNodePtr to best (for FlatFile production) pub in a equiv set
*****************************************************************************/
NLM_EXTERN ValNodePtr FlatRefBest(ValNodePtr equiv, Boolean error_msgs, Boolean anything)
{
	ValNodePtr the_pub, retval = NULL, newpub;
	CitBookPtr cb;
	CitSubPtr cs;
	CitGenPtr cg;	
	CitArtPtr ca;
	MedlineEntryPtr ml;
	CitJourPtr jp;
	ImprintPtr ip;
	Boolean good_one;
	Int1 bad_one= 0;
	CharPtr str_ret;

	if (equiv->choice == PUB_Equiv) {
		newpub = equiv->data.ptrvalue;
	} else {
		newpub = equiv;
	}
	for (the_pub = newpub, good_one = FALSE; the_pub && ! good_one
			; the_pub = the_pub -> next) {

		switch ( the_pub -> choice) {

      case PUB_Sub:
         cs = (CitSubPtr) the_pub -> data.ptrvalue;
         if (cs) {
			if ( cs -> imp){
				ip = cs -> imp;
				if ( ip -> date) {
					retval = the_pub;
					good_one = TRUE; /* good for submitted */
				}
			} else if (cs->date) {
					retval = the_pub;
					good_one = TRUE; /* good for submitted */
			}
		}
         break;
		case PUB_Man:
		case PUB_Book:
			cb = (CitBookPtr) the_pub -> data.ptrvalue;
			if ( cb -> imp) {
				ip = cb -> imp;
				if ( ip -> date) {
					retval = the_pub;
					good_one = TRUE; /* good for thesis or book */
				}
			}
		break;
		case PUB_Patent:
			retval = the_pub;
			good_one = TRUE; /* might exclude later...*/
		break;
		case PUB_Article:
		case PUB_Medline:
			if ( the_pub -> choice == PUB_Medline) {
				ml = (MedlineEntryPtr) the_pub -> data.ptrvalue;
				ca = (CitArtPtr) ml -> cit;

			} else {
				ca = (CitArtPtr) the_pub -> data.ptrvalue;
			}
		if (ca -> fromptr) {
			if (ca -> from ==1) {
				jp = (CitJourPtr) ca -> fromptr;
				if ( jp -> imp) {
					ip = jp -> imp;
					if ( ip -> date) {
						retval = the_pub;
						good_one = TRUE; /* good as it gets */
					}
				}
			} else {
				CitBookPtr book = (CitBookPtr) ca -> fromptr;
					if ( book -> imp) {
						ip = book -> imp;
						if ( ip -> date) {
							retval = the_pub;
							good_one = TRUE; /* good for book */
						}
					}
				
			}
		}
			break;
		case PUB_Gen: 
			cg = (CitGenPtr) the_pub -> data.ptrvalue;
			if (cg -> cit) {
				str_ret = NULL;
				str_ret = StrStr(cg -> cit ,"Journal=\"");
				if ((str_ret) || (cg->title) || (cg->journal) || (cg->date)) {
					retval = the_pub;  /*unless something better */
				} else {
					if (StringNICmp("unpublished", cg->cit, 11) == 0)
						retval = the_pub;
					else if (StringNICmp("to be published", cg->cit, 15) == 0)
						retval = the_pub;
					else if (StringNICmp("in press", cg->cit, 8) == 0)
						retval = the_pub;
					else if (StringNICmp("submitted", cg->cit, 8) == 0)
						retval = the_pub;
				}
			} else if (cg -> journal) {
				retval = the_pub;  /*unless something better */
			}

			break;
		case PUB_Proc:
			bad_one = the_pub -> choice;
			break;
		}
	}

	if (! retval && anything) {
	   for (the_pub = newpub; the_pub; the_pub = the_pub -> next) {
		if (the_pub->choice == PUB_Muid)
			retval = the_pub;
	   }
	   if (! retval) /* Take anything left over now and hope for the best */
		retval = newpub;
	}

	if ( ! retval && bad_one != 0) {
		if (error_msgs == TRUE)
			ErrPostEx(SEV_WARNING, ERR_REFERENCE_Illegalreference,
			"FlatRefBest: Unimplemented pub type = %d", bad_one);
	}
	
	return retval;
}	/* FlatRefBest */

NLM_EXTERN Int4 StoreFeatTemp(SortStruct PNTR List, SeqFeatPtr sfp,
Int4 currentsize, BioseqPtr bsp, BioseqPtr seg, Uint2 entityID, Uint4 itemID, Uint2 itemtype,SeqLocPtr slp, SeqLocPtr PNTR extra_loc, Int2 extra_loc_cnt,
Boolean temp)
{
	SeqLocPtr PNTR slpp = NULL;
	
	List[currentsize].entityID = entityID;
	List[currentsize].itemID = itemID;
	List[currentsize].itemtype = itemtype;
	List[currentsize].sfp = sfp;
	List[currentsize].bsp = bsp;
	List[currentsize].seg_bsp = seg;
	List[currentsize].dup = FALSE;
	List[currentsize].hash = AsnIoHash(sfp, 
						(AsnWriteFunc) SeqFeatAsnWrite);
	List[currentsize].slp = slp;
	if (extra_loc_cnt > 0) {
		slpp = MemNew(extra_loc_cnt*(sizeof(SeqLocPtr)));
		MemCpy(slpp, extra_loc, extra_loc_cnt*(sizeof(SeqLocPtr)));
	} 
	List[currentsize].extra_loc = slpp;
	List[currentsize].extra_loc_cnt = extra_loc_cnt;
	List[currentsize].tempload = temp;
	List[currentsize].gsp = NULL;
	List[currentsize].nsp = NoteStructNew(List[currentsize].nsp);

	currentsize++;

	return currentsize;
}

NLM_EXTERN Int4 StoreFeat(SortStruct PNTR List, SeqFeatPtr sfp, Int4 currentsize, 
BioseqPtr bsp, BioseqPtr seg, Uint2 entityID, Uint4 itemID, Uint2 itemtype,
SeqLocPtr slp, SeqLocPtr PNTR extra_loc, Int2 extra_loc_cnt)
{
	return StoreFeatFree(List, sfp, currentsize, bsp, seg, entityID, itemID, 
					itemtype,slp, extra_loc, extra_loc_cnt, FALSE);
}

NLM_EXTERN Int4 StoreFeatFree(SortStruct PNTR List, SeqFeatPtr sfp, Int4 currentsize, 
BioseqPtr bsp, BioseqPtr seg, Uint2 entityID, Uint4 itemID, Uint2 itemtype,
SeqLocPtr slp, SeqLocPtr PNTR extra_loc, Int2 extra_loc_cnt, Boolean feat_free)
{
	SeqLocPtr PNTR slpp = NULL;
	
	List[currentsize].entityID = entityID;
	List[currentsize].itemID = itemID;
	List[currentsize].itemtype = itemtype;
	List[currentsize].sfp = sfp;
	List[currentsize].bsp = bsp;
	List[currentsize].seg_bsp = seg;
	List[currentsize].dup = FALSE;
	List[currentsize].hash = AsnIoHash(sfp, 
						(AsnWriteFunc) SeqFeatAsnWrite);
	List[currentsize].slp = slp;
	if (extra_loc_cnt > 0) {
		slpp = MemNew(extra_loc_cnt*(sizeof(SeqLocPtr)));
		MemCpy(slpp, extra_loc, extra_loc_cnt*(sizeof(SeqLocPtr)));
	} 
	List[currentsize].extra_loc = slpp;
	List[currentsize].extra_loc_cnt = extra_loc_cnt;
	List[currentsize].feat_free = feat_free;
	List[currentsize].gsp = NULL;
	List[currentsize].nsp = NoteStructNew(List[currentsize].nsp);

	currentsize++;

	return currentsize;
}
/****************************************************************************
*CharPtr Cat2Strings (CharPtr string1, CharPtr string2, CharPtr separator, Int2 num)
*
* Concatenates two strings (string1 and string2) and separates them by a
* "separator".  If num>0, takes num spaces off the end of string1 on
* concatenation; if num<0 takes all spaces off the end of the complete 
* string.
*****************************************************************************/
NLM_EXTERN CharPtr Cat2Strings (CharPtr string1, CharPtr string2, CharPtr separator, Int2 num)

{
	Boolean no_space=FALSE;
	Int4 length1=0, length2=0, length_sep=0, length_total;
	CharPtr newstring=NULL;

	if (num < 0)
	{
		num=0;
		no_space=TRUE;
	}

	if (string1 != NULL)
		length1 = StringLen(string1);
	if (string2 != NULL)
		length2 = StringLen(string2);
	if (separator != NULL)
		length_sep = StringLen(separator);

	length_total = length1+length2+length_sep-num+1;

	newstring = (CharPtr) MemNew(length_total*sizeof(Char));

	if (string1 != NULL)	
		newstring = StringCat(newstring, string1);
	if ((length1-num) >= 0)
		newstring[length1-num] = '\0';
	if (no_space && length1 > 0)
		while (length1 > 0 && newstring[length1-1] == ' ')
		{
			newstring[length1-1] = '\0';
			length1--;
		}
	if (separator != NULL)	
		newstring = StringCat(newstring, separator);
	if (string2 != NULL)	
		newstring = StringCat(newstring, string2);


	return newstring;
}

NLM_EXTERN GBQualPtr AddGBQualEx (CharPtr PNTR key, GBQualPtr gbqual, CharPtr qual, CharPtr val)
{
	Int2 index;
	
	index = GBFeatKeyNameValid(key, FALSE);
	if (GBQualValidToAdd(index,qual)) {
		return AddGBQual(gbqual, qual, val);
	}
	return gbqual;
}

/************************************************************************
*AddGBQual
*
*	This function makes a new GBQual and adds a "val" and a
*	a "qual".
*   doesn't add qual if it's already there /tatiana/
* 	doesn't add empty ("") val if qual is translation
***********************************************************************/
#ifdef ASN2GNBK_STRIP_NOTE_PERIODS
static Boolean IsEllipsis (
  CharPtr str
)

{
  size_t   len;
  CharPtr  ptr;

  if (StringHasNoText (str)) return FALSE;
  len = StringLen (str);
  if (len < 3) return FALSE;
  ptr = str + len - 3;
  return (Boolean) (ptr [0] == '.' && ptr [1] == '.' && ptr [2] == '.');
}
#endif

NLM_EXTERN GBQualPtr AddGBQual (GBQualPtr gbqual, CharPtr qual, CharPtr val)
{
	GBQualPtr curq, note = NULL;

	if (StringCmp(qual, "translation") == 0) {
		if (val == NULL) 
			return gbqual;
		if (*val == '\0')
			return gbqual;
	}
	if (gbqual) {
		if (CheckForQual(gbqual, qual, val) == 1) {
			return gbqual;
		}
		for (curq=gbqual; curq->next != NULL; curq=curq->next)
			continue;
		curq->next = GBQualNew();
		curq = curq->next;
		if (val)
			curq->val = StringSave(val);
		curq->qual = StringSave(qual);
		note = curq;
	} else {
		gbqual = GBQualNew();
		gbqual->next = NULL;
		if (val)
			gbqual->val = StringSave(val);
		gbqual->qual = StringSave(qual);
		note = gbqual;
	}

#ifdef ASN2GNBK_STRIP_NOTE_PERIODS
	if (note != NULL && StringICmp (qual, "note") == 0) {
		size_t len;
		CharPtr p, q;
		len = StringLen (note->val);
		if (len > 0 && note->val [len - 1] == '~') {
			note->val [len - 1] = '\0';
		}
		if (! IsEllipsis (note->val)) {
			len = StringLen (note->val);
			if (len > 0 && note->val [len - 1] == '.') {
 				note->val [len - 1] = '\0';
				if (len > 1 && note->val [len - 2] == '.') {
 					note->val [len - 2] = '\0';
				}
			}
		}
		TrimSpacesAndJunkFromEnds (note->val,TRUE);
		TrimSpacesAndSemicolons (note->val);
		p = note->val;
		q = note->val;
		while (*p) {
		  if (*p == ';' && p [1] == ' ' && p [2] == ';') {
		    p += 2;
		  } else {
		    *q = *p;
		    p++;
		    q++;
		  }
		}
		*q = '\0';
	}
#endif

	return gbqual;
}

/****************************************************************************
*	Int2 CheckForQual(GBQualPtr gbqual, CharPtr string_q, CharPtr string_v)
*
*	Compares string (a potential gbqual->val) against all gbquals.
*	If a match is found, "1" is returned; if not "0".
****************************************************************************/

NLM_EXTERN Int2 CheckForQual (GBQualPtr gbqual, CharPtr string_q, CharPtr string_v)
{
	GBQualPtr curq;

	for (curq=gbqual; curq; curq=curq->next) {
		if (StringCmp(string_q, curq->qual) == 0) {
			if (curq->val == NULL) {
				curq->val = StringSave(string_v);
				return 1;
			} 
			if (StringCmp(string_v, curq->val) == 0) {
				return 1;
			}
		}
	}
	return 0;
}


/****************************************************************************
*
*	MakeAnAccession is for last ditch efforts to get an accession
*	after all the normal things have failed.
*
****************************************************************************/

NLM_EXTERN CharPtr MakeAnAccession (CharPtr new_buf, SeqIdPtr seq_id, Int2 buflen)
{
	SeqIdPtr new_id;

	new_id = SeqIdFindBest(seq_id, SEQID_GENBANK);
	SeqIdWrite(new_id, new_buf, PRINTID_TEXTID_ACCESSION, buflen);
	return new_buf;

}

NLM_EXTERN CharPtr GetGBSourceLine (GBBlockPtr gb)
{
	CharPtr source = NULL;

	if(gb && gb->source)
		source = StringSave(gb->source);

#ifdef ASN2GNBK_STRIP_NOTE_PERIODS
	if (source != NULL) {
		TrimSpacesAndJunkFromEnds (source,TRUE);
	}
#endif
	return source;
}

NLM_EXTERN CharPtr FlatOrganelle(Asn2ffJobPtr ajp, GBEntryPtr gbp)
{
	CharPtr retval = NULL;
	ValNodePtr man, vnp=NULL;
	static char * organelle_names [] = {
		 "Mitochondrion " ,
    "Chloroplast " ,
    "Kinetoplast ",
    "Cyanelle "};
	BioSourcePtr biosp=NULL;
/*	
	static CharPtr genome[] = {
	NULL, NULL, "Chloroplast ", "Chromoplast ", "Kinetoplast ", "Mitochondrion ", "Plastid ", "Macronuclear ", "Extrachrom ", "Plasmid ", NULL, NULL, "Cyanelle ", "Proviral ", "Virion ", "Nucleomorph ", "Apicoplast ", "Leucoplast ", "Proplastid "};
*/	
	static CharPtr genome[] = {
	NULL, NULL, "Chloroplast ", "Chromoplast ", "Kinetoplast ", "Mitochondrion ", "Plastid ", NULL, NULL, NULL, NULL, NULL, "Cyanelle ", NULL, NULL, "Nucleomorph ", "Apicoplast ", "Leucoplast ", "Proplastid ", NULL};
	
/* try new first */
	if ((vnp=GatherDescrByChoice(ajp, gbp, Seq_descr_source)) != NULL) 
	{
		biosp = vnp->data.ptrvalue;
	/*	if (biosp->genome < 6 || biosp->genome > 12)*/
			retval = StringSave(genome[biosp->genome]);
	}
/* old next */
	if (biosp == NULL) {
		if ((vnp=GatherDescrByChoice(ajp, gbp, Seq_descr_modif)) != NULL) 
		{
			for (man = (ValNodePtr) vnp-> data.ptrvalue; man; man = man -> next)
			{
				switch (man -> data.intvalue){
					case 4: case 5: case 6: case 7:
					if (! retval )
						retval = StringSave(organelle_names
								[man->data.intvalue-4]);
						break;
					default:
						break;
					}
			}
		}
	}
	return retval;
}

NLM_EXTERN Int4 GetNumOfSeqBlks (Asn2ffJobPtr ajp, GBEntryPtr gbp)
{
        Int4 length, num_of_seqblks;

        length = BioseqGetLen(gbp->bsp);
		if (ajp->slp) {
			length = SeqLocLen(ajp->slp);
		}
        num_of_seqblks = ROUNDUP(length, SEQ_BLK_SIZE)/SEQ_BLK_SIZE;

        return num_of_seqblks;
}


/*************************************************************************
*	New asn.1 spec - division is in Orgname.div
*	check MolInfo.tech 
*	check GBBlock for PAT or SYN
*	get division from Orgname.div (in BioSource)
*	09-05-96
*************************************************************************/

static void IndexedGetDescrForDiv (BioseqPtr bsp, DivStructPtr PNTR dspp)

{
	SeqMgrDescContext context;
	ValNodePtr tmp;
	DivStructPtr	dsp;
	BioSourcePtr bsr;
	MolInfoPtr mol;
	CharPtr gb_div=NULL;
	GBBlockPtr gb;

	dsp = *dspp;
	tmp = SeqMgrGetNextDescriptor (bsp, NULL, Seq_descr_molinfo, &context);
			if (tmp != NULL) {
				if (tmp->data.ptrvalue != NULL) {
					mol = (MolInfoPtr) tmp->data.ptrvalue;
					if (mol->tech != 0) {
						if (dsp->tech == 0) {
							dsp->tech = mol->tech;
						} else if (mol->tech != dsp->tech) {
							dsp->was_err = TRUE;
							if (dsp->err_post) {
								ErrPostEx(SEV_WARNING, 0, 0, 
								"Different Molinfo in one entry: %d|%d", 
									mol->tech, dsp->tech);
							}
							dsp->tech = mol->tech;
						}
						dsp->techID = context.itemID;
						dsp->techtype = OBJ_SEQDESC;
						*dspp = dsp;
					}
				}
			}

	tmp = SeqMgrGetNextDescriptor (bsp, NULL, Seq_descr_source, &context);
			while (tmp != NULL && dsp->orgdiv == NULL) {
				bsr = (BioSourcePtr) tmp->data.ptrvalue;
				if (bsr && bsr->org) {
					if (bsr->org->orgname && bsr->org->orgname->div) {
						gb_div = bsr->org->orgname->div;
						if (dsp->orgdiv == NULL) {
							dsp->orgdiv = gb_div;
						} else if (StringCmp(gb_div, dsp->orgdiv) != 0) {
							dsp->was_err = TRUE;
							if (dsp->err_post) {
								ErrPostEx(SEV_WARNING, 0, 0, 
							"Different Taxonomy divisions in one entry: %s|%s", 
									gb_div, dsp->orgdiv);
							}
							dsp->orgdiv = gb_div;
						}
						dsp->biosrc = bsr;
						dsp->orgID = context.itemID;
						dsp->orgtype = OBJ_SEQDESC;
						*dspp = dsp;
					}
				}
				tmp = SeqMgrGetNextDescriptor (bsp, tmp, Seq_descr_source, &context);
			}

	tmp = SeqMgrGetNextDescriptor (bsp, NULL, Seq_descr_genbank, &context);
			if (tmp != NULL) {
				gb = (GBBlockPtr) tmp->data.ptrvalue;
				if (gb->div) {
					gb_div = gb->div;
					if (dsp->gbdiv == NULL) {
						dsp->gbdiv = gb_div;
					} else if (StringCmp(gb_div, dsp->gbdiv) != 0) {
						dsp->was_err = TRUE;
						if (dsp->err_post) {
							ErrPostEx(SEV_WARNING, 0, 0, 
							"Different GBBlock divisions in one entry: %s|%s", 
								gb_div, dsp->gbdiv);
						}
						dsp->gbdiv = gb_div;
					}
					dsp->gbID = context.itemID;
					dsp->gbtype = OBJ_SEQDESC;
					*dspp = dsp;
				}
			}
}

static Boolean GetDescrForDiv (GatherContextPtr gcp)
{
/* find only one (closest to the target!) vnp with given choice */
	ValNodePtr	tmp;
	DivStructPtr	PNTR dspp;
	DivStructPtr	dsp;
	BioSourcePtr bsr;
	MolInfoPtr mol;
	CharPtr gb_div=NULL;
	GBBlockPtr gb;
	
	dspp = gcp->userdata;
	dsp = *dspp;
	switch (gcp->thistype)
	{
		case OBJ_SEQDESC:
			tmp = (ValNodePtr) (gcp->thisitem);
			if (tmp->choice == Seq_descr_molinfo) {
				if (tmp->data.ptrvalue != NULL) {
					mol = (MolInfoPtr) tmp->data.ptrvalue;
					if (mol->tech != 0) {
						if (dsp->tech == 0) {
							dsp->tech = mol->tech;
						} else if (mol->tech != dsp->tech) {
							dsp->was_err = TRUE;
							if (dsp->err_post) {
								ErrPostEx(SEV_WARNING, 0, 0, 
								"Different Molinfo in one entry: %d|%d", 
									mol->tech, dsp->tech);
							}
							dsp->tech = mol->tech;
						}
						dsp->techID = gcp->itemID;
						dsp->techtype = gcp->thistype;
						*dspp = dsp;
					}
				}
			} else if (tmp->choice == Seq_descr_source) {
				bsr = (BioSourcePtr) tmp->data.ptrvalue;
				if (bsr && bsr->org) {
					if (bsr->org->orgname && bsr->org->orgname->div) {
						gb_div = bsr->org->orgname->div;
						if (dsp->orgdiv == NULL) {
							dsp->orgdiv = gb_div;
						} else if (StringCmp(gb_div, dsp->orgdiv) != 0) {
							dsp->was_err = TRUE;
							if (dsp->err_post) {
								ErrPostEx(SEV_WARNING, 0, 0, 
							"Different Taxonomy divisions in one entry: %s|%s", 
									gb_div, dsp->orgdiv);
							}
							dsp->orgdiv = gb_div;
						}
						dsp->biosrc = bsr;
						dsp->orgID = gcp->itemID;
						dsp->orgtype = gcp->thistype;
						*dspp = dsp;
					}
				}
			} else if (tmp->choice == Seq_descr_genbank) {
				gb = (GBBlockPtr) tmp->data.ptrvalue;
				if (gb->div) {
					gb_div = gb->div;
					if (dsp->gbdiv == NULL) {
						dsp->gbdiv = gb_div;
					} else if (StringCmp(gb_div, dsp->gbdiv) != 0) {
						dsp->was_err = TRUE;
						if (dsp->err_post) {
							ErrPostEx(SEV_WARNING, 0, 0, 
							"Different GBBlock divisions in one entry: %s|%s", 
								gb_div, dsp->gbdiv);
						}
						dsp->gbdiv = gb_div;
					}
					dsp->gbID = gcp->itemID;
					dsp->gbtype = gcp->thistype;
					*dspp = dsp;
				}
			}			
			break;
		default:
			break;
	}
	return TRUE;
}

/**************************************************************************
*	0 - nothing found
*	1 - return division code OK
*	2 - return division code but errors were found
**************************************************************************/
static Int2 BioseqGetGBDivCodeEx (BioseqPtr bsp, CharPtr buf, Int2 buflen, Boolean err_post, Boolean useFeatureIndexing)
{
	GatherScope gsc;
	SeqLocPtr slp = NULL;
	Uint2 bspID;
	DivStructPtr dsp;
	BioSourcePtr bsr = NULL;
	Int2 tech, /*UNUSED*/diff, retval = 0;
	CharPtr orgdiv, gbdiv;
	SeqIdPtr sip;

	if (buf == NULL)
		return 0;
	*buf = '\0';
/* check for Patent SeqId  */
	for (sip = bsp->id; sip; sip=sip->next) {
		if (sip->choice == SEQID_PATENT) {
			diff = LabelCopy(buf, "PAT", buflen);
			return 1;
		}
	}
	bspID = ObjMgrGetEntityIDForPointer(bsp);
	dsp = MemNew(sizeof(DivStruct));
	dsp->err_post = err_post;
	dsp->entityID = bspID;
	dsp->tech = 0;
	dsp->gbdiv = NULL;
	dsp->orgdiv = NULL;
	dsp->biosrc = NULL;
	dsp->was_err = FALSE;
  	MemSet ((Pointer) (&gsc), 0, sizeof (GatherScope));
	MemSet ((Pointer) (gsc.ignore), (int)(TRUE), (size_t) (OBJ_MAX * sizeof(Boolean)));
	gsc.ignore[OBJ_SEQDESC] = FALSE;
	slp = ValNodeNew(NULL);
	slp->choice = SEQLOC_WHOLE;
	slp->data.ptrvalue = (SeqIdPtr) SeqIdDup (SeqIdFindBest (bsp->id, 0));
	gsc.target = slp;

	if (useFeatureIndexing) {
		IndexedGetDescrForDiv (bsp, &dsp);
	} else {
		GatherEntity(bspID, &dsp, GetDescrForDiv, &gsc);
	}
	
	SeqLocFree(slp);
	orgdiv = dsp->orgdiv;
	gbdiv = dsp->gbdiv;
	tech = dsp->tech;
	bsr = dsp->biosrc;
	if (dsp->was_err) {
		retval = 2;
	} else {
		retval = 1;
	}
	MemFree(dsp);
	switch (tech) {
		case MI_TECH_est: 
			diff = LabelCopy(buf, "EST", buflen);
		break;
		case MI_TECH_sts:  /* Sequence Tagged Site */
			diff = LabelCopy(buf, "STS", buflen);
		break;
		case MI_TECH_survey:
			diff = LabelCopy(buf, "GSS", buflen);
		break;
		case MI_TECH_htc:
			diff = LabelCopy(buf, "HTC", buflen);
		break;
		case MI_TECH_htgs_0:
		case MI_TECH_htgs_1:
		case MI_TECH_htgs_2:
			diff = LabelCopy(buf, "HTG", buflen);
		break;
		default:
		break;
	}
	if (*buf != '\0') {
		return retval;
	}
/*  new slot for synthetic sequences */	
	if (bsr && bsr->origin == 5) {
		diff = LabelCopy(buf, "SYN", buflen);
		return retval;
	}
/***** division in GBBlock becomes obsolete  ********/	
	if (gbdiv != NULL) {
		if (StringCmp(gbdiv, "PAT") == 0 || 
					StringCmp(gbdiv, "SYN") == 0 || orgdiv == NULL) {
			diff = LabelCopy(buf, gbdiv, buflen);
		return retval;
		}
	}
	
/**********/	
	if (orgdiv != NULL) {
		diff = LabelCopy(buf, orgdiv, buflen);
		return retval;
	}
	return 0;
}

NLM_EXTERN Int2 BioseqGetGBDivCode(BioseqPtr bsp, CharPtr buf, Int2 buflen, Boolean err_post)

{
	return BioseqGetGBDivCodeEx (bsp, buf, buflen, err_post, FALSE);
}


/*============================================================================*\
 * Function:
 *	StrStripSpaces
 *
 * Purpose:
 *	Strips all spaces in string in following manner. If the function
 *	meet several spaces (spaces and tabs) in succession it replaces them
 *	with one space.
 *	Strips all spaces after '(' and before ')'
 *
\*----------------------------------------------------------------------------*/
static void StrStripSpaces(CharPtr str)
{
	CharPtr	new_str;

	if (str == NULL) {
		return;
	}

	new_str = str;
	while (*str != '\0') {
		*new_str++ = *str;
		if (*str == ' ' || *str == '\t' || *str == '(') {
			for (str++; *str == ' ' || *str == '\t'; str++) ;
			if (*str == ')' || *str == ',') {
				new_str--;
			}
		} else {
			str++;
		}
	}
	*new_str = '\0';
}

static CharPtr GetFlatRetract(ValNodePtr pub)
{
	CitArtPtr cit;
	CitJourPtr jour = NULL;
	CitRetractPtr ret = NULL;
	CharPtr buffer;
	Int2 len;
	
	if (pub == NULL)
		return NULL;
	if (pub->choice != PUB_Article)
		return NULL;
	cit = pub->data.ptrvalue;
	if (cit->from == 1) {
		jour = cit->fromptr;
		if (jour && jour->imp) {
			ret = jour->imp->retract;
			if (ret && ret->type == 3) { /* other types can be added later */
				len = StringLen(ret->exp) + 11;
				buffer = (CharPtr) MemNew(len*sizeof(Char));
				sprintf(buffer, "Erratum:[%s]", ret->exp);
				return buffer;
			}
		}
	}
	return NULL;
}

static CharPtr GetSubmitDescr(ValNodePtr pub)
{
	CitSubPtr cs;
	
	if (pub == NULL) {
		return NULL;
	}
	if (pub->choice != PUB_Sub) {
		return NULL;
	}
	cs = (CitSubPtr) pub->data.ptrvalue;
	if (cs->descr == NULL) {
		return NULL;
	}
	return (StringSave(cs->descr));
}

static Int4 GetMuid(ValNodePtr equiv)
{
	Int4 muid=0;
	ValNodePtr newpub, the_pub;
	MedlineEntryPtr ml;
	
	if (equiv->choice == PUB_Equiv)
		newpub = equiv->data.ptrvalue;
	else
		newpub = equiv;

	for (the_pub = newpub; the_pub; the_pub = the_pub -> next) {
		if (the_pub->choice == PUB_Muid) {
			muid = the_pub->data.intvalue;
			break;
		}
		if (the_pub->choice == PUB_Medline) {
			ml = (MedlineEntryPtr) the_pub -> data.ptrvalue;
			muid = ml->uid;
		}
	}

	return muid;

}	/* GetMuid */

static Int4 GetPmid(ValNodePtr equiv)
{
	Int4 pmid=0;
	ValNodePtr newpub, the_pub;
	MedlineEntryPtr ml;
	
	if (equiv->choice == PUB_Equiv)
		newpub = equiv->data.ptrvalue;
	else
		newpub = equiv;

	for (the_pub = newpub; the_pub; the_pub = the_pub -> next) {
		if (the_pub->choice == PUB_PMid) {
			pmid = the_pub->data.intvalue;
			break;
		}
		if (the_pub->choice == PUB_Medline) {
			ml = (MedlineEntryPtr) the_pub -> data.ptrvalue;
			pmid = ml->pmid;
		}
	}

	return pmid;

}	/* GetPmid */

/***************************************************************************
* SeqLocPtr GetBaseRangeForCitation (SeqLocPtr loc, SeqLocPtr slp, Int4Ptr start, Int4Ptr stop)
*
*
*	This function finds the start and stop Int4 values for a location.
*	If this is a cmplex location (e.g., SEQLOC_MIX), then the
*	function is called several times, with the returned slp used
*	as an argument on the next round.
*	
*	The first call should be with slp set to NULL.
*
****************************************************************************/

static SeqLocPtr GetBaseRangeForCitation (SeqLocPtr loc, SeqLocPtr slp, Int4Ptr start, Int4Ptr stop)
{
	Int4 tmp_start, tmp_stop, tmp_range;
	
	*start = 0;
	*stop = 0;

	switch (loc->choice)
	{
		case SEQLOC_BOND:   
       		case SEQLOC_FEAT:   
        	case SEQLOC_NULL:    
        	case SEQLOC_EMPTY:  
			slp = NULL;
                        break;
        	case SEQLOC_WHOLE:
        	case SEQLOC_INT:
			if ((tmp_start = SeqLocStart(loc)) >= 0  &&
					(tmp_stop = SeqLocStop(loc)) >= 0)
			{
				tmp_range = tmp_stop - tmp_start;
				if (tmp_range >= 0)
				{ /* +1 for Genbank format. */
					*start = tmp_start+1;
					*stop = tmp_stop+1;
				}
			}
			slp = NULL;
			break;
        	case SEQLOC_MIX:
        	case SEQLOC_EQUIV:
        	case SEQLOC_PACKED_INT:
			if (slp == NULL)
				slp = loc->data.ptrvalue;
			if (slp != NULL)
			{
				if ((tmp_start = SeqLocStart(slp)) >= 0  &&
						(tmp_stop = SeqLocStop(slp)) >= 0)
				{
					tmp_range = tmp_stop - tmp_start;
					if (tmp_range >= 0)
					{ /* +1 for Genbank format. */
						*start = tmp_start+1;
						*stop = tmp_stop+1;
					}
				}
				slp = slp->next;
			}
			break;
        	case SEQLOC_PACKED_PNT: 
       		case SEQLOC_PNT:
			slp = NULL;
			break;
		default:
			slp = NULL;
			break;
	}
	return slp;
}

/*************************************************************************
*GB_PrintPubs
*
*	"GB_PrintPubs" to dump pubs in Flat File (i.e., Genbank) format.
*
**************************************************************************/

void GB_PrintPubs (Asn2ffJobPtr ajp, GBEntryPtr gbp, PubStructPtr psp)

{

	BioseqPtr bsp=gbp->bsp;
	Boolean first_time, ignore_this=FALSE, submit=FALSE, tag;
	Char buffer[150];
	CharPtr authors=NULL,title=NULL,journal=NULL,string_start, string, retract;
	CharPtr descr = NULL;
	Int2 i;
	Int4 gibbsq, muid, pmid, pat_seqid=0, start=0, stop=0;
	PubdescPtr pdp;
	SeqFeatPtr sfp;
	SeqLocPtr loc, slp;
	ValNodePtr pub;

	if (ASN2FF_SHOW_ALL_PUBS) {
		pub = FlatRefBest(psp->pub, ajp->error_msgs, TRUE);
	} else {
		pub = FlatRefBest(psp->pub, ajp->error_msgs, FALSE);
	}
	if (pub == NULL)
	{
		if (ajp->error_msgs == TRUE)
			PostARefErrMessage (ajp, bsp, psp, NULL, -1, NULL);
		return;
	}
	ignore_this = FlatIgnoreThisPatentPub(bsp, pub, &pat_seqid);
	if (ajp->format != GENPEPT_FMT)
	{
		if (ignore_this == TRUE)
		{
			if (ajp->error_msgs == TRUE)
				PostARefErrMessage (ajp, bsp, psp, NULL, -1, NULL);
			return;
		}
	}

	ff_StartPrint(0, 12, ASN2FF_GB_MAX, NULL);
	ff_AddString("REFERENCE");
	TabToColumn(13);
	ff_AddInteger("%ld", (long) psp->number);
	if (psp->start == 1) {
		TabToColumn(16);
		if (psp->descr != NULL) {
			if (psp->descr->reftype != 0) {
				ff_AddString("(sites)");
			} else {
				if (ajp->format != GENPEPT_FMT) {
					ff_AddString("(bases ");
				} else {
					ff_AddString("(residues ");
				}
				if (ajp->slp) {
					ff_AddInteger("%ld", (long) (SeqLocStart(ajp->slp) + 1));
					ff_AddString(" to "); 
					ff_AddInteger("%ld", (long) (SeqLocStop(ajp->slp) + 1));
				} else {
					ff_AddString("1 to ");
					ff_AddInteger("%ld", (long) bsp->length);
				}
				ff_AddChar(')');
			}
		}
	}
	else if (psp->start == 2) {
		TabToColumn(16);
		if (ajp->format != GENPEPT_FMT)
			ff_AddString("(bases ");
		else
			ff_AddString("(residues ");
		for (i=0; i<psp->citcount; i++) {
			sfp = psp->citfeat[i];
			loc = (SeqLocPtr) sfp->location;
			slp = GetBaseRangeForCitation (loc, NULL, &start, &stop);
			if (start != 0 || stop != 0) {
				ff_AddInteger("%ld", (long) start);
				ff_AddString(" to ");
				ff_AddInteger("%ld", (long) stop);
				if (slp != NULL || i+1 != psp->citcount)
					ff_AddString("; ");
			}
			while (slp != NULL) {
				slp = GetBaseRangeForCitation (loc, slp, &start, &stop);
				if (start != 0 || stop != 0) {
					ff_AddInteger("%ld", (long) start);
					ff_AddString(" to ");
					ff_AddInteger("%ld", (long) stop);
					if (slp != NULL || i+1 != psp->citcount)
						ff_AddString("; ");
				}
			}
		}
		ff_AddChar(')');
	} else if (psp->start == 3) {
		TabToColumn(16);
		ff_AddString("(sites)");
	} else {
		if (ajp->error_msgs == TRUE)
			ErrPostEx(SEV_WARNING, CTX_NCBI2GB, 1, 
			 "Incorrect start value (%d) in PubStruct\n", psp->start);
	}
	ff_EndPrint();

	authors = FlatAuthor(ajp, pub);
	ff_StartPrint(2, 12, ASN2FF_GB_MAX, NULL);
	ff_AddString("AUTHORS");
	TabToColumn(13);

	if (authors && *authors != NULLB) {
		ff_AddString(authors);
	} else {
		ff_AddChar('.');
	}
	ff_EndPrint();

	title = FlatPubTitle(pub);
	if (title ) {
		if ( *title  != NULLB) {
			ff_StartPrint(2, 12, ASN2FF_GB_MAX, NULL);
			ff_AddString("TITLE");
			TabToColumn(13);
			StrStripSpaces(title);
			ff_AddString(title);
			ff_EndPrint();
		}
	}

	journal = FlatJournal(ajp, gbp, pub, pat_seqid, &submit, FALSE);
	ff_StartPrint(2, 12, ASN2FF_GB_MAX, NULL);
	ff_AddString("JOURNAL");
	TabToColumn(13);
	if (journal ) {
#ifdef ASN2GNBK_STRIP_NOTE_PERIODS
		CharPtr p, q;
		p = journal;
		q = journal;
		while (*p) {
		  if (*p == ',' && p [1] == ' ' && p [2] == ';') {
		    p += 2;
		  } else {
		    *q = *p;
		    p++;
		    q++;
		  }
		}
		*q = '\0';
#endif
		StrStripSpaces(journal);
		ff_AddString(journal);
	} else {
		ff_AddString("Unpublished");
	}
	ff_EndPrint();

	muid = GetMuid(psp->pub);
	if (muid > 0) {
		ff_StartPrint(2, 12, ASN2FF_GB_MAX, NULL);
		ff_AddString("MEDLINE");
		TabToColumn(13);
		www_muid(muid);
		ff_EndPrint();
	}
	pmid = GetPmid (psp->pub);
	if (pmid > 0) {
		ff_StartPrint(3, 12, ASN2FF_GB_MAX, NULL);
		ff_AddString("PUBMED");
		TabToColumn(13);
		www_muid(pmid);
		ff_EndPrint();
	}

	tag = FALSE;
	pdp = psp->descr;
	if (pdp != NULL && pdp->comment != NULL) {
		if (StringCmp(pdp->comment, "full automatic") != 0 &&
		  StringCmp(pdp->comment, "full staff_review") != 0 &&
		   StringCmp(pdp->comment, "full staff_entry") != 0 &&
		    StringCmp(pdp->comment, "simple staff_review") != 0 &&
		      StringCmp(pdp->comment, "simple staff_entry") != 0 &&
		       StringCmp(pdp->comment, "simple automatic") != 0 &&
		        StringCmp(pdp->comment, "unannotated automatic") != 0 &&
		         StringCmp(pdp->comment, "unannotated staff_review") != 0 &&
		          StringCmp(pdp->comment, "unannotated staff_entry") != 0)
		{
			ff_StartPrint(2, 12, ASN2FF_GB_MAX, NULL);
			ff_AddString("REMARK  ");
			TabToColumn(13);
			ff_AddStringWithTildes(pdp->comment);
			tag = TRUE;
		}
	}
	string = &buffer[0];
	gibbsq = GetGibbsqStatement(gbp, string);
	if (gibbsq > 0) {
		if (tag != TRUE) {
			ff_StartPrint(2, 12, ASN2FF_GB_MAX, NULL);
			ff_AddString("REMARK");
			TabToColumn(13);
		} else {
			NewContLine();
		}
		ff_AddStringWithTildes(string);
		tag = TRUE;
	}
	string = GetGibbsqComment(gbp);
	if (string) {
		string_start = string;
		if (tag != TRUE) {
			ff_StartPrint(2, 12, ASN2FF_GB_MAX, NULL);
			ff_AddString("REMARK");
			TabToColumn(13);
		} else {
			NewContLine();
		}
		first_time = TRUE;
/* Can't this be rewritten to use ff_AddString????  That would be faster! */
		while (*string != '\0') {
			if (*string == '~') {
				if (first_time == FALSE)
					NewContLine();
				else
					first_time = FALSE;
			} else if (*string == '\"') {
				*string = '\'';
				ff_AddChar(*string);
			} else {
				ff_AddChar(*string);
			}
			string++;
		}
		string_start = MemFree(string_start);
		tag=TRUE;
	}
	retract = GetFlatRetract(pub);
	if (retract) {
		if (tag != TRUE) {
			ff_StartPrint(2, 12, ASN2FF_GB_MAX, NULL);
			ff_AddString("REMARK");
			TabToColumn(13);
		} else {
			NewContLine();
		}
		ff_AddStringWithTildes(retract);
		tag = TRUE;
		MemFree(retract);
	}
	descr = GetSubmitDescr(pub);
	if (descr) {
		if (tag != TRUE) {
			ff_StartPrint(2, 12, ASN2FF_GB_MAX, NULL);
			ff_AddString("REMARK");
			TabToColumn(13);
		} else {
			NewContLine();
		}
		ff_AddStringWithTildes(descr);
		tag = TRUE;
	}
	if (tag == TRUE)
		ff_EndPrint();

	if (authors)
		MemFree(authors);

        MemFree(descr);
	MemFree(title);
	MemFree(journal);
}	/* GB_PrintPubs */

/*************************************************************************
*GR_PrintPubs
*
*	"GR_PrintPubs" to dump pubs in Flat File (i.e., Genbank) format.
*
**************************************************************************/

void GR_PrintPubs (Asn2ffJobPtr ajp, GBEntryPtr gbp, PubStructPtr psp)

{

	BioseqPtr bsp=gbp->bsp;
	Boolean ignore_this=FALSE, submit=FALSE;
	CharPtr authors=NULL,title=NULL,journal=NULL;
	CharPtr descr = NULL;
	Int4 muid, pmid, pat_seqid=0, start=0, stop=0;
	ValNodePtr pub;

	if (ASN2FF_SHOW_ALL_PUBS) {
		pub = FlatRefBest(psp->pub, ajp->error_msgs, TRUE);
	} else {
		pub = FlatRefBest(psp->pub, ajp->error_msgs, FALSE);
	}
	if (pub == NULL)
	{
		if (ajp->error_msgs == TRUE)
			PostARefErrMessage (ajp, bsp, psp, NULL, -1, NULL);
		return;
	}
	ignore_this = FlatIgnoreThisPatentPub(bsp, pub, &pat_seqid);
	if (ajp->format != GENPEPT_FMT)
	{
		if (ignore_this == TRUE)
		{
			if (ajp->error_msgs == TRUE)
				PostARefErrMessage (ajp, bsp, psp, NULL, -1, NULL);
			return;
		}
	}

	ff_StartPrint(0, 12, ASN2FF_GB_MAX, NULL);
	ff_AddString("<BR><BR>");
	title = FlatPubTitle(pub);
	if (title ) {
		if ( *title  != NULLB) {
			StrStripSpaces(title);
			ff_AddString("<B>");
			ff_AddString(title);
			ff_AddString("</B>");
			ff_EndPrint();
		}
	}
	authors = FlatAuthor(ajp, pub);

	if (authors && *authors != NULLB) {
		ff_AddString("<BR>");
		ff_AddString(authors);
	} else {
		ff_AddChar('.');
	}
	ff_EndPrint();


	journal = FlatJournal(ajp, gbp, pub, pat_seqid, &submit, FALSE);
	ff_StartPrint(2, 12, ASN2FF_GB_MAX, NULL);
	ff_AddString("<BR>");
	if (journal ) {
		StrStripSpaces(journal);
		ff_AddString(journal);
	} else {
		ff_AddString("Unpublished");
	}
	ff_EndPrint();

	muid = GetMuid(psp->pub);
	if (muid > 0) {
		ff_StartPrint(2, 12, ASN2FF_GB_MAX, NULL);
		ff_AddString("<BR>");
		TabToColumn(13);
		www_muid(muid);
		ff_EndPrint();
	}
	pmid = GetPmid (psp->pub); /* not sure what GR format should be generating */
	/*
	if (pmid > 0) {
		ff_StartPrint(3, 12, ASN2FF_GB_MAX, NULL);
		ff_AddString("<BR>");
		TabToColumn(13);
		www_muid(pmid);
		ff_EndPrint();
	}
	*/


	if (authors)
		MemFree(authors);

	MemFree(title);
	MemFree(journal);
	
}	/* GR_PrintPubs */

/*************************************************************************
*EMBL_PrintPubs
*
*	"EMBL_PrintPubs" to dump pubs in FlatFile (EMBL) format.
*
**************************************************************************/

void EMBL_PrintPubs (Asn2ffJobPtr ajp, GBEntryPtr gbp, PubStructPtr psp)

{

	BioseqPtr bsp=gbp->bsp;
	Boolean ignore_this=FALSE, submit=FALSE;
	CharPtr authors=NULL, title=NULL, journal=NULL, new_journal;
	Int2 i;
	Int4 pat_seqid=0;	
	Int4 start=0, stop=0, tmp_range, range;
	PubdescPtr descr=psp->descr;
	SeqFeatPtr sfp;
	SeqLocPtr loc, slp;
	ValNodePtr pub;
	Int4 muid;
	Char s[15];

	pub = FlatRefBest(psp->pub, ajp->error_msgs, FALSE);
	if (pub == NULL)
	{
		if (ajp->error_msgs == TRUE)
			ErrPostStr(SEV_WARNING, ERR_REFERENCE_Illegalreference, "FFDumpPubs: Invalid Pub found.");
		return;
	}
	ignore_this = FlatIgnoreThisPatentPub(bsp, pub, &pat_seqid);
	if (ignore_this == TRUE && ASN2FF_IGNORE_PATENT_PUBS != FALSE)
	{
		if (ajp->error_msgs == TRUE)
			ErrPostStr(SEV_WARNING, ERR_REFERENCE_Illegalreference, "FFDumpPubs: Invalid Patent Pub");
		return;
	}

	PrintXX();

	ff_StartPrint(5, 5, ASN2FF_EMBL_MAX, "RN");
	ff_AddChar('[');
	ff_AddInteger("%ld", (long) psp->number);
	ff_AddChar(']');
	ff_EndPrint();
	if (psp->start == 1)
	{
		ff_StartPrint(5, 5, ASN2FF_EMBL_MAX, "RP");
		ff_AddString("1-");
		ff_AddInteger("%ld", (long) bsp->length);
		ff_EndPrint();
	}
	else if (psp->start == 2)
	{
		range = 0;
		ff_StartPrint(5, 5, ASN2FF_EMBL_MAX, "RP");
		for (i=0; i<psp->citcount; i++)
		{
			sfp = psp->citfeat[i];
			loc = (SeqLocPtr) sfp->location;
			slp = GetBaseRangeForCitation (loc, NULL, &start, &stop);
			if (start != 0 || stop != 0)
			{ /* Why do I need the tmp_range test??? */
				tmp_range = stop - start;
				if (tmp_range >= range)
				{
					range = tmp_range;
					ff_AddInteger("%ld", (long) start);
					ff_AddChar('-');
					ff_AddInteger("%ld", (long) stop);
					if (slp != NULL || i+1 != psp->citcount)
						ff_AddString(", ");
				}
			}
			while (slp != NULL)
			{
				slp = GetBaseRangeForCitation (loc, slp, &start, &stop);
				if (start != 0 || stop != 0)
				{
					ff_AddInteger("%ld", (long) start);
					ff_AddChar('-');
					ff_AddInteger("%ld", (long) stop);
					if (slp != NULL || i+1 != psp->citcount)
						ff_AddString(", ");
				}
			}
		}
		ff_EndPrint();
	}
	else if (psp->start == 3 && ajp->pseudo == TRUE) 
	{ /* "sites" only for pseudo-embl.  */
		ff_StartPrint(5, 5, ASN2FF_EMBL_MAX, "RP");
		ff_AddString("(sites)");
		ff_EndPrint();
	}

	journal = FlatJournal(ajp, gbp, pub, pat_seqid, &submit, FALSE);

	if (descr && descr->comment)
	{
		ff_StartPrint(5, 5, ASN2FF_EMBL_MAX, "RC");
		ff_AddString(descr->comment);
		ff_EndPrint();
	}
	authors = FlatAuthor(ajp, pub);
	ff_StartPrint(5, 5, ASN2FF_EMBL_MAX, "RA");
	if (authors)
		ff_AddString(authors);
	ff_AddChar(';');
	ff_EndPrint();
	
	ff_StartPrint(5, 5, ASN2FF_EMBL_MAX, "RT");
	if (! submit)
	{
		title = FlatPubTitle(pub);
		if (title ){
			if ( *title )
			{
				ff_AddChar('\"');
				StrStripSpaces(title); 
				ff_AddString(title);
				ff_AddChar('\"');
			}
		}
	}
	ff_AddChar(';');
	ff_EndPrint();

	ff_StartPrint(5, 5, ASN2FF_EMBL_MAX, "RL");
	if (journal)
	{
		new_journal = CheckEndPunctuation(journal, '.');
		StrStripSpaces(new_journal);
		ff_AddString(new_journal);
		new_journal = MemFree(new_journal);
	}
	ff_EndPrint();
	
	muid = GetMuid(psp->pub);
	if (muid != 0) {
		sprintf(s, "%ld.", (long) muid);
		s[StringLen(s)] = '\0';
		ff_StartPrint(5, 5, ASN2FF_EMBL_MAX, "RX");
		ff_AddString("MEDLINE; ");
		ff_AddString(s);
		ff_EndPrint();
	}
	if (authors)
		MemFree(authors);
	MemFree(title);
	MemFree(journal);
}	/* EMBL_PrintPubs */

/***************************************************************************
*CharPtr CheckLocusLength (Boolean error_msgs, CharPtr locus, Int2 locus_max, Int2 total_segs)
*
*	Calculate the length of the locus; if it's too long, take characters
*	off the front.  If it's part of a segmented set and the locus ends 
*	in a number, add an "S".  If it appears to be an NCBI locus of the
*	form HSU00001, then take two letters off the front.
***************************************************************************/ 
static CharPtr CheckLocusLength (Boolean error_msgs, CharPtr locus, Int2 locus_max, Int2 total_segs)

{
	Boolean cut_two=FALSE;
	CharPtr buffer;
	Int2 length, surplus;

	length = StringLen(locus);
	buffer = MemNew((length+2)*sizeof(Char));
	buffer = StringCpy(buffer, locus);
	
	if (total_segs > 0 && IS_DIGIT(locus[length-1]) != 0)
		if (locus[length-1] != '0' || IS_DIGIT(locus[length-2]) != 0)
		{
			length++;
			buffer[length-1] = 'S';
			buffer[length] = '\0';
		}

	surplus = length - locus_max;

	if (surplus > 0)
	{
		if (surplus <= 2)
		{	/* Check if this is of the form HSU00001S */
			if (IS_ALPHA(buffer[0]) != 0 &&
				IS_ALPHA(buffer[1]) != 0 &&
				IS_ALPHA(buffer[2]) != 0 &&
	    		        IS_DIGIT(buffer[3]) != 0 &&
	        	        IS_DIGIT(buffer[4]) != 0 &&
	       	          	IS_DIGIT(buffer[5]) != 0 &&
	       	             	IS_DIGIT(buffer[6]) != 0 &&
	       	                IS_DIGIT(buffer[7]) != 0 &&
	       	                buffer[8] == 'S' &&
				buffer[9] == '\0')
					cut_two = TRUE;
		}
	
		if (cut_two == TRUE)
			locus = StringCpy(locus, buffer+2);
		else
			locus = StringCpy(locus, buffer+surplus);
		if (error_msgs == TRUE)
		{
			flat2asn_delete_locus_user_string();
			flat2asn_install_locus_user_string(buffer);
			ErrPostStr(SEV_INFO, ERR_LOCUS_ChangedLocusName, 
				"Locusname length is more than 16, locusname is truncated");
		}
	}

	buffer = MemFree(buffer);

	return locus;
}

NLM_EXTERN Int4 GetPubsAwp (Asn2ffJobPtr ajp, GBEntryPtr gbp)
{
	GatherScope gs;
	BioseqPtr bsp = NULL;
	ValNodePtr vnp, v;
	SeqLocPtr slp = NULL;
	SeqIdPtr isip;
	Int4 status, i;
	Char buffer[31];
	
	bsp = gbp->bsp;
	if (bsp == NULL) {
		return 0;
	}
	isip = bsp->id;
	vnp = NULL;
  	MemSet ((Pointer) (&gs), 0, sizeof (GatherScope));
  	gs.get_feats_location = TRUE;
  	if (ajp->genome_view == TRUE) {
		gs.seglevels = 0;
  	} else if (ajp->only_one) { 
		gs.seglevels = 2;
	} else {
		gs.seglevels = 1;
	}
/*	MemSet ((Pointer) (gs.ignore), (int)(TRUE), (size_t) (OBJ_MAX * sizeof(Boolean)));
	gs.ignore[OBJ_SEQDESC] = FALSE;
	gs.ignore[OBJ_SEQANNOT] = FALSE;
	gs.ignore[OBJ_SEQFEAT] = FALSE;
	gs.ignore[OBJ_SEQSUB] = FALSE;
	gs.ignore[OBJ_SEQSUB_CIT] = FALSE;*/

	MemSet ((Pointer) (gs.ignore), (int)(FALSE), (size_t) (OBJ_MAX * sizeof(Boolean)));
	gs.ignore[OBJ_SEQALIGN] = TRUE; /* this was being hit many times on big records */

	if (ajp->slp == NULL) {
		slp = ValNodeNew(NULL);
		slp->choice = SEQLOC_WHOLE;
		slp->data.ptrvalue = (SeqIdPtr) SeqIdDup (SeqIdFindBest (bsp->id, 0));
		gs.target = slp;
	} else {
		gs.target = ajp->slp;
	}
	GatherEntity(ajp->entityID, &vnp, get_pubs, &gs);
	if (slp)
		SeqLocFree(slp);
	if ((status = CheckPubs(ajp, bsp, &vnp)) <= 0) {
		if (ajp->error_msgs == TRUE) {
			MakeAnAccession(buffer, isip, 30);
			flat2asn_delete_locus_user_string();
			flat2asn_install_locus_user_string(buffer);
			flat2asn_delete_accession_user_string();
			flat2asn_install_accession_user_string(buffer);
			ErrPostStr(SEV_ERROR, ERR_REFERENCE_NoValidRefs, 
			"No refs found that would result in legal flatfile format");
		}
		/* found something. */
		if (status < 0) {
			ValNodeFree(vnp);
			vnp = NULL;
		}
	}
	gbp->Pub = OrganizePubList(vnp); 
	for (v = gbp->Pub, i=0; v != NULL; v= v->next, i++);
	
	return i;
}
/*************************************************************************
*	Check for EMBL format first
*	Call   to find div for Genbank records 
*	Allocate a buffer for division	
*	09-05-96
*************************************************************************/
static CharPtr GetDivision(Asn2ffJobPtr ajp, GBEntryPtr gbp)
{
	ValNodePtr vnp;
	MolInfoPtr mol = NULL;
	EMBLBlockPtr eb=NULL;
	BioseqPtr bsp = gbp->bsp;
	Int2 buflen=4;
	CharPtr buffer;
	static CharPtr embl_divs [] = {
	"FUN","INV","MAM","ORG","PHG","PLN","PRI","PRO","ROD","SYN","UNA","VRL",
	"VRT","PAT","EST","STS", "HUM", "HTC"
	};

	buffer = MemNew(buflen);
	buffer[0] = '\0';
	if (ajp->format == EMBL_FMT || ajp->format == PSEUDOEMBL_FMT ||
					ajp->format == EMBLPEPT_FMT) {
		if ((vnp=GatherDescrByChoice(ajp, gbp, Seq_descr_embl)) != NULL) {
			eb = (EMBLBlockPtr) vnp->data.ptrvalue;
		}
		if (eb ) {
			if (eb->div == 255) {
/* kludge for HUM division */
				if ((vnp=GatherDescrByChoice(ajp, gbp, Seq_descr_molinfo)) != NULL) {
					gbp->descr = MemFree(gbp->descr);	
					mol = (MolInfoPtr) vnp->data.ptrvalue;
				}
				if (mol) {
					if (mol->tech == MI_TECH_survey) {
						StringNCpy_0(buffer, "GSS", buflen);
						return buffer;
					} else if (mol->tech == MI_TECH_htc) {
						StringNCpy_0(buffer, "HTC", buflen);
						return buffer;
					} else if (mol->tech == MI_TECH_htgs_1
							 || mol->tech == MI_TECH_htgs_2) {
						StringNCpy_0(buffer, "HTG", buflen);
						return buffer;
					}
				} else {
					StringNCpy_0(buffer, embl_divs[16], buflen);  /*HUM */
					return buffer;
				}
			} else {
				StringNCpy_0(buffer, embl_divs[eb->div], buflen);
				return buffer;
			}
		}
		BioseqGetGBDivCodeEx (bsp, buffer, buflen, FALSE, ajp->useSeqMgrIndexes);
		if (buffer[0] == NULLB) {
			StringNCpy_0(buffer, "   ", buflen);
		}
		return buffer;
	}
	BioseqGetGBDivCodeEx (bsp, buffer, buflen, FALSE, ajp->useSeqMgrIndexes);
	if (buffer[0] == NULLB) {
		StringNCpy_0(buffer, "   ", buflen);
	}
	if (gbp->bsp && gbp->bsp->mol == Seq_mol_aa) {
		return buffer;
	}
	if (ajp->genome_view) {
		StringNCpy_0(buffer, "CON", buflen);
	}
	return buffer;
}

/***************************************************************************
*
*	UseGIforLocus to get the GI number for the locus and accession numbers.
*	and to get division using Gather
*
***************************************************************************/

NLM_EXTERN void UseGIforLocus (Asn2ffJobPtr ajp)
{
	CharPtr buffer;
	GBEntryPtr gbp;
	
	for (gbp=ajp->asn2ffwep->gbp; gbp; gbp=gbp->next) {
		if (ajp->show_gi) {	
			sprintf(gbp->accession, "%ld", (long) (gbp->gi));
			sprintf(gbp->locus, "%-10ld", (long) (gbp->gi));
		} else {
			MemSet((VoidPtr) gbp->accession, ' ', 10);
			MemSet((VoidPtr) gbp->locus, ' ', 10);
		}
		buffer = GetDivision(ajp, gbp);
		if (buffer[0] != NULLB) {
			StringCpy(gbp->div, "   ");
		} else {
			StringNCpy_0(gbp->div, buffer, 4);
		}
                MemFree(buffer);
	}
}

/*****************************************************************************
*
*	ValidateLocus takes a locus name and assures that the format is 
*	proper. if segmented set adds the segment number at the end and 
*	returns new locus
*****************************************************************************/
CharPtr ValidateLocus(Asn2ffJobPtr ajp, BioseqPtr bsp, CharPtr base_locus, Int2 total_segs, Int2 num_seg, CharPtr new_buf, CharPtr buf_locus, CharPtr orig_buf)

{
	Boolean collision=FALSE;
	static Boolean order_init=FALSE;
	Char buf_ext[BUF_EXT_LENGTH], buffer[30];
	DbtagPtr db;
	int dex;
	Int2 /*UNUSED*/base_locus_max, buf_index, exp, length, num_of_digits;
	ObjectIdPtr ob;
	SeqIdPtr best_id, id;
	static Uint1 rel_order[NUM_SEQID];

	if (! order_init)
	{
		for (dex=0; dex<18; dex++)
			rel_order[dex] = 255;
		rel_order[SEQID_GENERAL ] = 14;
	}
	order_init = TRUE;

	if (ASN2FF_AVOID_LOCUS_COLL || ASN2FF_REPORT_LOCUS_COLL)
	{	/* Check for LOCUS collisions with Karl's algorithm */
		id = bsp->id;
		best_id = SeqIdSelect( id, rel_order,NUM_SEQID);
		if (best_id != NULL) {
			if (best_id -> choice == SEQID_GENERAL){ /* always! */

			    db = (DbtagPtr) best_id -> data.ptrvalue;
			    if (StringCmp(db -> db, LOCUS_COLLISION_DB_NAME) == 0){
				ob = db -> tag;
				    if ( ob != NULL)
				    {
					if (ASN2FF_REPORT_LOCUS_COLL)
					{
					    MakeAnAccession(buffer, id, 30);
					    flat2asn_delete_locus_user_string();
					    flat2asn_install_locus_user_string(buffer);
					    flat2asn_delete_accession_user_string();
					    flat2asn_install_accession_user_string(buffer);
					     ErrPostStr(SEV_WARNING, ERR_LOCUS_LocusNameCollision, "");
					}
					if (ASN2FF_AVOID_LOCUS_COLL)
					{
					    collision=TRUE;
					    StringNCpy_0(new_buf, ob -> str, MAX_LOCUS_NAME_LEN+1);
					}
				    }
				}
			}
		}
	}

	if (! collision)
	{
		if (total_segs == 0)
		{	/* Not a segmented set. */
			if ((length=StringLen(buf_locus)) <= 0)
				new_buf = StringCpy(new_buf, orig_buf);
			else
				new_buf = StringCpy(new_buf, buf_locus);
				 
			new_buf = CheckLocusLength (ajp->error_msgs, new_buf, MAX_LOCUS_NAME_LEN, 0);
		}
		else
		{
			if (total_segs < 10)
				num_of_digits = 1;
			else if (total_segs < 100)
				num_of_digits = 2;
			else if (total_segs < 1000)
				num_of_digits = 3;
			else 
			{
				num_of_digits = 4;
				ErrPostStr(SEV_INFO, ERR_SEGMENT_MoreThan1000Segs, "");
			}
			if (num_seg < 10)
				exp = 1;
			else if (num_seg < 100)
				exp = 2;
			else if (num_seg < 1000)
				exp = 3;
			base_locus_max = MAX_LOCUS_NAME_LEN - num_of_digits;
			length = StringLen(base_locus);
			StringCpy(new_buf, base_locus);
			MemSet((VoidPtr) buf_ext, '\0', BUF_EXT_LENGTH);	
			MemSet((VoidPtr) buf_ext, '0', num_of_digits);	
			sprintf(buf_ext+num_of_digits-exp, "%ld", (long) num_seg);
			buf_index = 0;
			while (buf_ext[buf_index] != '\0')
			{
			     new_buf[length+buf_index] = buf_ext[buf_index];
			     buf_index++;
			 }
			 new_buf[length+buf_index] = '\0';
		}
	}
	
	return new_buf;
}	/* ValidateLocus */

/***************************************************************************
*	example: NM_000756
***************************************************************************/
static Int2 ValidateOtherAccession(CharPtr new_buf, CharPtr orig_buf)
{
	Int2 count;
	Boolean FirstLetter=FALSE, FiveNum = FALSE;

	if (orig_buf == NULL || orig_buf[0] == '\0') {
		return -3;
	}
	if (StringLen(orig_buf) >= 10) {
		return -4;
	}
	if (orig_buf[0] != 'N') {
		return -1;
	}
	if (orig_buf[2] != '_') {
		return -1;
	}
	for (count=3; count < 8; count++) {
		if(! IS_DIGIT(orig_buf[count]))
			break;
	}
	if (count == 8 && (orig_buf[count+1] == '\0' || orig_buf[count+1] == ' ')) {
		StringCpy(new_buf, orig_buf);
		return 0;

	} else {
		return -1;
	}
}


/****************************************************************************
*
*	ValidateAccession takes an accession number and makes sure it is
*	in the proper format (starts with a capital letter that is followed
*	by five numbers).
*
*	Return values are:
*	 0: no problem
*	-1: Accession did not start with a letter (or two letters)
*	-2: Accession did not contain five numbers (or six numbers after 2 letters)
*	-3: the original Accession number to be validated was NULL
*	-4: the original Accession number is too long (>10)
*
****************************************************************************/
Int2 ValidateAccession(CharPtr new_buf, CharPtr orig_buf)
{
	Int2 count, start_count, stop_count;
	Boolean FirstLetter=FALSE, FiveNum = FALSE;

	if (orig_buf == NULL || orig_buf[0] == '\0') {
		return -3;
	}
	if (StringLen(orig_buf) >= 10) {
		return -4;
	}
	if (orig_buf[0] < 'A' || orig_buf[0] > 'Z') {
		return -1;
	} else {
		FirstLetter = TRUE;
	}
	for (count=1; count < 5; count++) {
		if(! IS_DIGIT(orig_buf[count]))
			break;
	}
	if (count == 5 && (orig_buf[count+1] == '\0' || orig_buf[count+1] == ' '))
		FiveNum = TRUE;

	if (FirstLetter == TRUE) {
		if (FiveNum == TRUE) {           /* 1 + 5 accession*/
			StringCpy(new_buf, orig_buf);
			return 0;
		} else if (IS_ALPHA(orig_buf[1])) {      /* 2 + 6 accession */
			if (orig_buf[1] < 'A' || orig_buf[1] > 'Z') {
				return -1;
			}
			start_count = 2;
			stop_count = 7;
			if (orig_buf[0] == 'N' || orig_buf[0] == 'X') {
				if ((orig_buf[1] == 'M' || orig_buf[1] == 'C' 
						|| orig_buf[1] == 'T'  || orig_buf[1] == 'P' 
													 || orig_buf[1] == 'G') 
												&&  orig_buf[2] == '_') {
						start_count = 3;
						stop_count = 8;
				}
			}
			for (count=start_count; count < stop_count; count++) {
				if(! IS_DIGIT(orig_buf[count]))
					break;
			}			
			if (count == stop_count && (orig_buf[count+1] == '\0' || orig_buf[count+1] == ' ')) {
				StringCpy(new_buf, orig_buf);
				return 0;
			} else if (IS_ALPHA(orig_buf[2])) {      /* 3 + 5 accession */
				if (orig_buf[0] =='A' || orig_buf[0] == 'B' || orig_buf[0] == 'C') {
					for (count=3; count < 7; count++) {
						if(! IS_DIGIT(orig_buf[count]))
							break;
					}			
					if (count == 7 && (orig_buf[count+1] == '\0' || orig_buf[count+1] == ' ')) {
						StringCpy(new_buf, orig_buf);
						return 0;
					} else {
						return -2;
					}
				} else {
					return -2;
				}
			} else {
				return -2;
			}
		} else {
			return -2;
		}
	} else {
		return -1;
	}
}

/**************************************************************************
*MakeBaseAccession
*
*	GetBaseAccession takes a BioseqPtr bsp and returns an 
*	accession if 1.) the set is segmented, and 2.) there is
*	an accession at a higher level.  Otherwise NULL is returned.
*	The user should deallocate the CharPtr.
**************************************************************************/

CharPtr MakeBaseAccession (BioseqPtr bsp)

{
	Char buffer[MAX_ACCESSION_LEN+1];
	CharPtr buf_acc=buffer;
	Int2 status = -1;
	SeqIdPtr sip, isip;
	TextSeqIdPtr tsip;


	if (bsp == NULL)
		return NULL;
	isip = bsp->id;
	sip = SeqIdSelect(isip, fasta_order, NUM_SEQID);
	if (sip && (sip->choice == SEQID_GENBANK || 
		sip->choice == SEQID_EMBL || 
		sip->choice == SEQID_PIR || 
		sip->choice == SEQID_SWISSPROT || 
		sip->choice == SEQID_DDBJ || 
		sip->choice == SEQID_PRF ||
		sip->choice == SEQID_OTHER ||
		sip->choice == SEQID_TPG ||
		sip->choice == SEQID_TPE ||
		sip->choice == SEQID_TPD))
	{
		tsip = (TextSeqIdPtr) sip->data.ptrvalue;
		switch (sip->choice) {
			case SEQID_GENBANK:
			case SEQID_EMBL:
			case SEQID_DDBJ:
			case SEQID_TPG:
			case SEQID_TPE:
			case SEQID_TPD:
			case SEQID_PIR:
			case SEQID_SWISSPROT:
				status = ValidateAccession(buf_acc, tsip->accession);
		}
	}
	if (status < 0)
		return NULL;
		
	return (StringSave(buf_acc));
}

/***************************************************************************
*
*	MakeBaseLocus takes a Asn2ffJobPtr and a CharPtr (base_locus)
*	and returns a CharPtr which is the new base_locus.  Checking is 
*	done to assure suitability of the new base locus name (i.e., 
*	no more than 15 characters for less than 10 segments and no more
*	than 14 characters for 10 or more segments).
*
***************************************************************************/

CharPtr MakeBaseLocusAwp (Asn2ffJobPtr ajp, CharPtr base_locus)

{
	BioseqPtr bsp, bbsp = NULL;
	Int2  index, length, base_locus_max, name_len, num_of_digits, num_seg;
	SeqIdPtr sip, bsip=NULL, isip=NULL;
	TextSeqIdPtr tsip = NULL, btsip=NULL;
	ObjectIdPtr obj;
	Char buffer[21], temp_buf[21];
	CharPtr localbuf=buffer, name, ptr=temp_buf;
	CharPtr tmp = "SEG_";
	Asn2ffWEPtr awp;
	GBEntryPtr	gbp;
	
	base_locus[0] = '\0'; 
	awp = ajp->asn2ffwep;
	num_seg = awp->total_seg;
	if (num_seg < 10)
		num_of_digits = 1;
	else if (num_seg < 100)
		num_of_digits = 2;
	else if (num_seg < 1000)
		num_of_digits = 3;
	else 
	{
		ErrPostStr(SEV_INFO, ERR_SEGMENT_MoreThan1000Segs, "");
	}
	base_locus_max = MAX_LOCUS_NAME_LEN - num_of_digits;
/* look for base locus in segmented bioseq */
	awp = ajp->asn2ffwep;
	bbsp = awp->seg; /* segmented Bioseq in segmented set */
	if (bbsp) {
		bsip = SeqIdSelect(bbsp->id, fasta_order, NUM_SEQID);
	}
	if (bsip && (bsip->choice == SEQID_GENBANK || 
				bsip->choice == SEQID_EMBL ||
				bsip->choice == SEQID_DDBJ ||
				bsip->choice == SEQID_SWISSPROT ||
				bsip->choice == SEQID_PIR || 
				bsip->choice == SEQID_OTHER || 
				bsip->choice == SEQID_TPG || 
				bsip->choice == SEQID_TPE || 
				bsip->choice == SEQID_TPD)) {
		btsip = (TextSeqIdPtr) bsip->data.ptrvalue;
	}
	if (btsip && StringLen(btsip->name) > 0) {
		localbuf = StringCpy(localbuf, btsip->name);
		if (StringNCmp(localbuf, tmp, 4) == 0) {
		/* check if name starts with "SEG_", remove if it does. */
		    StringCpy(ptr, localbuf+4);
		    length = StringLen(ptr);
		    ptr[length] = '\0';
		    if (ptr[length-1] == '1') {
		       bsp = awp->gbp->bsp;
		       isip = bsp->id;
		       sip = SeqIdSelect(isip, fasta_order, NUM_SEQID);
		       if (sip &&
		       	(name=((TextSeqIdPtr)sip->data.ptrvalue)->name) != NULL) {
		          name_len = StringLen(name);
		          if (name_len == length) {
		             if (name[length-1] == ptr[length-1])
		    	       for (index=2; index >= num_of_digits; index++) {
		               /* The following is *really* '0'! */
		                  if (ptr[length-index] == '0') {
		                     if (ptr[length-index] == name[length-index]) {
		                        StringNCpy(base_locus, ptr, length-index);
		                        base_locus[length-index] = '\0';
		                     } else {
		                        StringNCpy(base_locus, ptr, length-index+1);
		                        base_locus[length-index+1] = '\0';
		                     }
		                  } else {
		                     StringNCpy(base_locus, ptr, length-index+1);
		                     base_locus[length-index+1] = '\0';
				     break;
		                  }
		               }
		            }
		         }
		    } 
		    /* If nothing else worked, use base locus anyway. */
		    if (base_locus[0] == '\0')
		    	StringCpy(base_locus, ptr);
		}
		if (base_locus[0] == '\0')
			StringCpy(base_locus, btsip->name);

		/*check for length, truncate if necessary.	*/
		base_locus = CheckLocusLength (ajp->error_msgs, base_locus, base_locus_max, num_seg);
		return base_locus;
	}

/* Look for at least one sensible locus in all segments. */
	for (gbp = awp->gbp; gbp; gbp=gbp->next) {
		bsp = gbp->bsp;
		isip = bsp->id;
		sip = SeqIdSelect(isip, fasta_order, NUM_SEQID);
		if (sip && (sip->choice == SEQID_GENBANK || 
					sip->choice == SEQID_EMBL ||
					sip->choice == SEQID_DDBJ ||
					sip->choice == SEQID_SWISSPROT ||
					sip->choice == SEQID_OTHER || 
					sip->choice == SEQID_PIR || 
					sip->choice == SEQID_TPG || 
					sip->choice == SEQID_TPE || 
					sip->choice == SEQID_TPD)) {
			tsip = (TextSeqIdPtr) sip->data.ptrvalue;
		}
		if (tsip && tsip->name && StringLen(tsip->name) > 0) {
			base_locus = StringCpy(base_locus, tsip->name);
			length = StringLen(base_locus);
			base_locus[length-num_of_digits] = '\0';
			base_locus = CheckLocusLength (ajp->error_msgs,
									base_locus, base_locus_max, num_seg);
			return base_locus;
		}
	}

/* No option left but to take the first locus name.*/
	bsp = awp->gbp->bsp;
	isip = bsp->id;
	sip = SeqIdSelect(isip, fasta_order, NUM_SEQID);
	if (sip && sip->choice == SEQID_LOCAL) {
		obj = (ObjectIdPtr) sip->data.ptrvalue;
		if ( obj->str == NULL) {
			sprintf(base_locus, "%ld", (long)(obj->id));
		} else {
			base_locus = StringCpy(base_locus, obj->str);
		}
	} else if (sip && (sip->choice == SEQID_GENBANK || 
				sip->choice == SEQID_EMBL ||
				sip->choice == SEQID_SWISSPROT ||
				sip->choice == SEQID_DDBJ ||
				sip->choice == SEQID_PRF ||
				sip->choice == SEQID_PDB ||
				sip->choice == SEQID_OTHER ||
				sip->choice == SEQID_PIR || 
				sip->choice == SEQID_TPG || 
				sip->choice == SEQID_TPE || 
				sip->choice == SEQID_TPD)) {
		tsip = (TextSeqIdPtr)sip->data.ptrvalue;
		base_locus = StringCpy(base_locus, tsip->name);
	}
	base_locus = CheckLocusLength (ajp->error_msgs, base_locus, base_locus_max, num_seg);
	return base_locus;
	
}	/* MakeBaseLocusAwp */	

static Boolean ValidateVersion(SeqIdPtr sid, Asn2ffJobPtr ajp)
{
	TextSeqIdPtr tsip;
		
	if (ajp->forgbrel == FALSE)
		return TRUE;
	switch (sid->choice) {
	case SEQID_GENBANK:
	case SEQID_EMBL:
	case SEQID_DDBJ:
	case SEQID_OTHER:
	case SEQID_TPG:
	case SEQID_TPE:
	case SEQID_TPD:
		tsip = (TextSeqIdPtr) sid->data.ptrvalue;
		if (tsip->version == 0 || tsip->version == INT2_MIN) {
			return FALSE;
		}
	}
	return TRUE;
}

NLM_EXTERN void GetLocusPartsAwp (Asn2ffJobPtr ajp)
{
	BioseqPtr bsp=NULL;
	Asn2ffWEPtr awp;
	SeqIdPtr sip, isip;
	Int2 num_seg=0, total_segs=0;
	TextSeqIdPtr tsip;
	Char buf_a[MAX_ACCESSION_LEN+1], buf_l[MAX_ACCESSION_LEN+1],
		 base_l[MAX_ACCESSION_LEN+1];
	CharPtr buffer, buf_acc=buf_a, buf_locus=buf_l, base_locus=base_l, base_a;
	GBEntryPtr gbp;
	CharPtr loc;
	Int2 acc_len;

	awp = ajp->asn2ffwep;
	if (ajp->slp) {
		for (gbp = awp->gbp; gbp; gbp = gbp->next) {
			buffer = GetDivision(ajp, gbp);
			if (buffer[0] != NULLB) {
				StringNCpy_0(gbp->div, buffer, 4);
				MemFree(buffer);
			}
			if ((bsp = BioseqFindFromSeqLoc(ajp->slp)) != NULL) {
				CharPtr flatloc;

				isip = SeqIdSelect(gbp->bsp->id, fasta_order, NUM_SEQID);
				if (isip == NULL)
					isip = gbp->bsp->id;
				SeqIdWrite(isip, 
					buf_acc, PRINTID_TEXTID_ACCESSION, MAX_ACCESSION_LEN);

				if (ajp->old_locus_fmt == TRUE)
				  sprintf(gbp->locus, "%-10s", buf_acc);
				else
				  sprintf(gbp->locus, "%-16s", buf_acc);
				
				flatloc =  FlatLoc(bsp, ajp->slp);
				sprintf(gbp->accession, "%s REGION: %s", buf_acc, flatloc);
				flatloc = MemFree(flatloc);
				if (ajp->show_version) {
					SeqIdWrite(isip,
					buf_acc, PRINTID_TEXTID_ACC_VER, MAX_ACCESSION_LEN+1);
					StringNCpy_0(gbp->version,
								 buf_acc, MAX_ACCESSION_LEN+1);
				}
			} else {
				loc = SeqLocPrint(ajp->slp);
				StringNCpy_0(gbp->locus,  loc, MAX_LOCUS_NAME_LEN+1); 
				acc_len = MIN(StringLen(loc), 60);
				StringNCpy_0(gbp->accession, loc, acc_len+1);
				MemFree(loc);
			}
		}
		return; 
	}
	if (ajp->only_one) {
		for (gbp = awp->gbp; gbp; gbp = gbp->next) {
			if (gbp->bsp == NULL) {
				continue;
			}
			bsp = gbp->bsp;
			GetGINumber(gbp);
			buffer = GetDivision(ajp, gbp);
			if (buffer[0] != NULLB) {
				StringNCpy_0(gbp->div, buffer, 4);
				MemFree(buffer);
			}
			isip = SeqIdSelect(gbp->bsp->id, fasta_order, NUM_SEQID);
			if (isip == NULL)
				isip = gbp->bsp->id;
			SeqIdWrite(isip, buf_acc, 
					PRINTID_TEXTID_ACCESSION, MAX_ACCESSION_LEN+1);
			StringNCpy_0(gbp->accession, buf_acc, MAX_ACCESSION_LEN+1);

			if (ajp->old_locus_fmt == TRUE)
			  sprintf(gbp->locus, "%-10s", buf_acc); 
			else
			  sprintf(gbp->locus, "%-16s", buf_acc); 

			if (ajp->show_version) {
				SeqIdWrite(isip, buf_acc, 
					PRINTID_TEXTID_ACC_VER, MAX_ACCESSION_LEN+1);
				StringNCpy_0(gbp->version, buf_acc, MAX_ACCESSION_LEN+1);
			}
		}
		return;
	}
	total_segs = awp->total_seg; 
	base_a = MakeBaseAccession(awp->seg);
	base_locus = MakeBaseLocusAwp(ajp, base_locus);
	StringNCpy_0(ajp->asn2ffwep->base_name, base_locus, 11);
	
	for (gbp = awp->gbp; gbp != NULL; gbp = gbp->next) {
		if (gbp->bsp == NULL) {
			continue;
		}
		bsp = gbp->bsp;
		if ((isip = gbp->bsp->id) == NULL) {
			continue;
		} 
		buffer = GetDivision(ajp, gbp);
		if (buffer[0] != NULLB) {
			StringNCpy_0(gbp->div, buffer, 4);
			MemFree(buffer);
		}
		num_seg = gbp->num_seg; 
		sip = SeqIdSelect(isip, fasta_order, NUM_SEQID);
		if (sip == NULL) {
			sip = isip;
		}
		switch (sip->choice) {
		    case SEQID_GENBANK:
	    	case SEQID_EMBL:
	    	case SEQID_DDBJ:
	    	case SEQID_OTHER:
			case SEQID_TPG:
			case SEQID_TPE:
			case SEQID_TPD:
				tsip = (TextSeqIdPtr) sip->data.ptrvalue;
				if ((ValidateAccession(buf_acc, tsip->accession)) < 0) {
					if (base_a != NULL) {
						StringNCpy_0(buf_acc, base_a, MAX_ACCESSION_LEN+1);
					} else {
						buf_acc = MakeAnAccession(buf_acc, isip, 
													MAX_ACCESSION_LEN+1);
					}
				}
				buf_locus = ValidateLocus(ajp, bsp, base_locus, 
					total_segs, num_seg, buf_locus, tsip->name, buf_acc); 
				StringNCpy_0(gbp->accession, 
					buf_acc, MAX_ACCESSION_LEN+1);
				if (sip->choice == SEQID_OTHER 
						&& StringNCmp(tsip->accession, "NT_", 3) == 0) {
					if (ajp->old_locus_fmt == TRUE)
					  sprintf(gbp->locus, "%-10s", buf_acc);
					else
					  sprintf(gbp->locus, "%-16s", buf_acc);
				} else {
					if (ajp->old_locus_fmt == TRUE)
					  sprintf(gbp->locus, "%-10s", buf_locus);
					else
					  sprintf(gbp->locus, "%-16s", buf_locus);
				}
				num_seg--;
			if (ajp->show_version) {
				if (ValidateVersion(sip, ajp) == FALSE) {
					gbp->bsp = NULL;
					ErrPostEx(SEV_ERROR, ERR_ACCESSION_No_VERSION_Number, "%s", gbp->accession);
					continue;
				}
				SeqIdWrite(sip, buf_acc, 
					PRINTID_TEXTID_ACC_VER, MAX_ACCESSION_LEN+6);
				StringNCpy_0(gbp->version, buf_acc, MAX_ACCESSION_LEN+6);
			}
				break;
		    case SEQID_LOCAL:
				if ((((ObjectIdPtr)sip->data.ptrvalue)->str) == NULL) {
					buf_acc[0] = 'X';
					sprintf(buf_acc+1, "%ld", 
						(long)((ObjectIdPtr)sip->data.ptrvalue)->id);
				} else {
					StringNCpy_0(buf_acc,
				 ((ObjectIdPtr)sip->data.ptrvalue)->str, MAX_ACCESSION_LEN+1);
				}
				buf_locus = ValidateLocus(ajp, bsp, base_locus, 
				total_segs, num_seg,buf_locus,  buf_acc, buf_acc); 
				StringNCpy_0(gbp->accession, buf_acc, MAX_ACCESSION_LEN+1);

				if (ajp->old_locus_fmt == TRUE)
				  sprintf(gbp->locus, "%-10s", buf_locus); 
				else
				  sprintf(gbp->locus, "%-16s", buf_locus); 

				num_seg--;
				break;
		   case SEQID_GI:
			sprintf(buf_acc, "%ld", (long) (sip->data.intvalue));
			buf_locus = ValidateLocus(ajp, bsp, base_locus, 
					total_segs, num_seg, buf_locus, buf_acc, buf_acc); 
			StringNCpy_0(gbp->accession, buf_acc, MAX_ACCESSION_LEN+1);

			if (ajp->old_locus_fmt == TRUE)
			  sprintf(gbp->locus, "%-10s", buf_locus); 
			else
			  sprintf(gbp->locus, "%-16s", buf_locus); 

			num_seg--;
			break;

		   case SEQID_PIR:
		   case SEQID_SWISSPROT:
			tsip = (TextSeqIdPtr) sip->data.ptrvalue;
			if ((ValidateAccession(buf_acc, tsip->accession)) < 0) {
				if (base_a != NULL) {
					StringNCpy_0(buf_acc, base_a, MAX_ACCESSION_LEN+1);
				} else {
					buf_acc = MakeAnAccession(buf_acc, 
						isip, MAX_ACCESSION_LEN);
				}
			}
			if (ajp->show_version) {
				SeqIdWrite(sip, buf_acc, 
					PRINTID_TEXTID_ACC_VER, MAX_ACCESSION_LEN+6);
				StringNCpy_0(gbp->version, buf_acc, MAX_ACCESSION_LEN+6);
			}
			buf_locus = ValidateLocus(ajp, bsp, base_locus, 
				total_segs, num_seg, buf_locus, tsip->name, buf_acc); 
			StringNCpy_0(gbp->accession, buf_acc, MAX_ACCESSION_LEN+1);
			if (sip->choice == SEQID_OTHER 
					&& StringNCmp(tsip->accession, "NT_", 3) == 0) {
				if (ajp->old_locus_fmt == TRUE)
				  sprintf(gbp->locus, "%-10s", buf_acc);
				else
				  sprintf(gbp->locus, "%-16s", buf_acc);
			} else {
				if (ajp->old_locus_fmt == TRUE)
				  sprintf(gbp->locus, "%-10s", buf_locus);
				else
				  sprintf(gbp->locus, "%-16s", buf_locus);
			}
			num_seg--;
			
			break;
		   default:
			buf_acc = MakeAnAccession(buf_acc, isip, MAX_ACCESSION_LEN+1);
			buf_locus = ValidateLocus(ajp, bsp, base_locus, 
				total_segs, num_seg, buf_locus, buf_acc, buf_acc); 
			StringNCpy_0(gbp->accession, buf_acc,
													 MAX_ACCESSION_LEN+1);
			if (ajp->old_locus_fmt == TRUE)
			  sprintf(gbp->locus, "%-10s", buf_locus); 
			else
			  sprintf(gbp->locus, "%-16s", buf_locus); 

			num_seg--;
			break;
		}
	}
	if (base_a != NULL)
		base_a = MemFree(base_a);
	
}
/**************************************************************************
*	Looks in the descriptor  and feature->xref for any extra-accessions.
**************************************************************************/

NLM_EXTERN void AddExtraAccessions(Asn2ffJobPtr ajp, GBEntryPtr gbp)

{
	BioseqPtr bsp;
	Char buffer[10];
	CharPtr ptr=buffer, ac;
	EMBLBlockPtr eb;
	GBBlockPtr gb;
	Int2 index, status;
	SeqFeatPtr sfp;
	SeqIdPtr xid;
	SeqIntPtr si;
	SeqLocPtr xref;
	TextSeqIdPtr text;
	ValNodePtr extra_access=NULL, location=NULL, vnp;
	SortStructPtr p;
	Boolean /*UNUSED*/ncbi = FALSE;

	if (gbp == NULL) {
		return;
	}
	if ((bsp = gbp->bsp) == NULL) {
		return;
	}
	ac = gbp->accession;
	if (ac && *ac == 'U') {
		ncbi = TRUE;
	}
	for (vnp = bsp->descr; vnp; vnp=vnp->next) {
		if (vnp->choice == Seq_descr_genbank) {
			break;
		}
	}
	if (vnp != NULL) {
		gb = (GBBlockPtr) vnp->data.ptrvalue;
		extra_access = gb->extra_accessions;
		if (extra_access != NULL) {
			for (vnp=extra_access; vnp != NULL; vnp=vnp->next) {
				status = ValidateAccession(ptr, vnp->data.ptrvalue);
				if (status == 0) {
					if (ajp->format == EMBL_FMT || ajp->format ==
						 PSEUDOEMBL_FMT || ajp->format == EMBLPEPT_FMT) {
						ff_AddChar(';');
					} else {
						ff_AddChar(' ');
					}
				/*	www_extra_acc(ptr, ncbi); */
					ff_AddString( ptr);
				}
			}
		}
	}
	for (vnp = bsp->descr; vnp; vnp=vnp->next) {
		if (vnp->choice == Seq_descr_embl) {
			break;
		}
	}

	if (vnp != NULL) {
		eb = (EMBLBlockPtr) vnp->data.ptrvalue;
		extra_access = eb->extra_acc;
		if (extra_access != NULL) {
			for (vnp=extra_access; vnp != NULL; vnp=vnp->next) {
				status = ValidateAccession(ptr, vnp->data.ptrvalue);
				if (status == 0) {
					if (ajp->format == EMBL_FMT || ajp->format ==
						 PSEUDOEMBL_FMT || ajp->format == EMBLPEPT_FMT) {
						ff_AddChar(';');
					} else {
						ff_AddChar(' ');
					}
				/*	www_extra_acc(ptr, ncbi); */
					ff_AddString( ptr);
				}
			}
		}
	}
	if (gbp->feat) {
		p = gbp->feat->Xreflist;
		for (index=0; index < gbp->feat->sfpXrefsize; index++, p++) {
			if (location == NULL) {
				location = ValNodeNew(NULL);
				si = SeqIntNew();
				location->choice = SEQLOC_INT;
				location->data.ptrvalue = si;
			}
			si->from = 0;
			bsp = gbp->bsp;
			si->to = bsp->length - 1;
			si->id = bsp->id;	/* Don't delete id!! */
			if ((sfp = p->sfp) == NULL) {
				GatherItemWithLock(p->entityID,
					p->itemID, p->itemtype, &sfp, find_item);
			}
			if (sfp == NULL) {
				continue;
			}
			if (SeqLocCompare(sfp->location, location) != 0) {
				xref = (SeqLocPtr) sfp->data.value.ptrvalue;
				xid = (SeqIdPtr) xref->data.ptrvalue;
				if (xid->choice == 5 || xid->choice == 6 ||
					xid->choice == 13) {
					text = (TextSeqIdPtr) xid->data.ptrvalue;
					status = ValidateAccession(ptr, text->accession);
					if (status == 0) {
						if (ajp->format == EMBL_FMT || ajp->format ==
							 PSEUDOEMBL_FMT || ajp->format == EMBLPEPT_FMT) {
							ff_AddChar(';');
						} else {
							ff_AddChar(' ');
						}
					/*	www_extra_acc(ptr, ncbi); */
						ff_AddString( ptr);
					}
				}
			}
		}
	}

	if (location) {
		si->id = NULL;
		SeqIntFree(si);
		ValNodeFree(location);
	}

	return;
}static Boolean CompareToAwpList (BioseqPtr bsp, Asn2ffWEPtr	awp)

{
	GBEntryPtr gbp;

	if (bsp == NULL) {
		return FALSE;
	}
	for (gbp = awp->gbp; gbp != NULL; gbp = gbp->next) {
		if (bsp == gbp->bsp) {
			return TRUE;
		}
	}
	return FALSE;	
}

static GBEntryPtr GBEntryNew(void)
{
	GBEntryPtr gbp;
	
	gbp = (GBEntryPtr) MemNew(sizeof(GBEntry));
	gbp->feat = NULL;
	gbp->descr = NULL;
	gbp->source_info = NULL;
	gbp->comm = NULL;
	gbp->map = FALSE;
	
	return gbp; 
}

static GBEntryPtr tie_next_gbp(GBEntryPtr head, GBEntryPtr next)
/*  ties next node to the end of the chain */
{
	GBEntryPtr v;

	if (head == NULL) {
		return next;
	}
	for (v = head; v->next != NULL; v = v->next) {
		v = v;
	}
	v->next = next;
	return head;
}

static GBEntryPtr CreateGBEntry(Asn2ffWEPtr awp, BioseqPtr bsp, 
Int2 eID, Int2 iID, Int2 itype)
{
	GBEntryPtr	gbep;

	gbep = GBEntryNew();
	gbep->bsp = bsp;
	gbep->length = bsp->length;
	gbep->entityID = eID;
	gbep->itemID = iID;
	gbep->itemtype = itype;
	awp->gbp = tie_next_gbp(awp->gbp, gbep);
	
	return gbep;
}

/************************************************************************
*	SeqToAwp()
*		gather callback to create a list of GenBank entries
*************************************************************************/

NLM_EXTERN Boolean SeqToAwp (GatherContextPtr gcp)

{
	BioseqPtr bsp;
	SeqEntryPtr ep;
	BioseqSetPtr bssp;
	SeqLocPtr slp;
	Asn2ffWEPtr	awp;
	Asn2ffJobPtr ajp;
	GBEntryPtr	gbep;
	SeqIdPtr isip, sip;
	Uint1 format;
	Boolean is_www = get_www();

	ajp = (Asn2ffJobPtr) gcp->userdata;
	awp = ajp->asn2ffwep;
	format = ajp->format;
	switch (gcp->thistype)
	{
		case OBJ_BIOSEQ:
			bsp = gcp->thisitem;
			if (bsp->repr == Seq_repr_seg) {
				if (ajp->genome_view || ajp->only_one) {
					gbep = CreateGBEntry(awp, bsp, gcp->entityID, 
						gcp->itemID, gcp->thistype);
						if (ajp->only_one && !ajp->map_view) {
							return FALSE;
						}
				}
				if (ISA_na(bsp->mol) && (format == GENBANK_FMT ||
					format == EMBL_FMT || format == PSEUDOEMBL_FMT
						|| format == GRAPHIK_FMT)) {
					awp->seg = bsp;
				} else if (ISA_aa(bsp->mol) && 
					(format == GENPEPT_FMT || format == EMBLPEPT_FMT
						|| format == GRAPHIK_FMT)) {
					awp->seg = bsp;
				}
			}
			if (ASN2FF_LOOK_FOR_SEQ == FALSE) {
				if (ajp->format == GENPEPT_FMT || ajp->format == EMBLPEPT_FMT
					|| (ISA_aa(bsp->mol) && format == GRAPHIK_FMT)) {
					if (ISA_aa(bsp->mol) && (bsp->repr == Seq_repr_raw 
		   	|| bsp->repr == Seq_repr_const || bsp->repr == Seq_repr_delta 
		   	|| 	((is_www || ajp->mode != RELEASE_MODE) && bsp->repr == Seq_repr_virtual))) {
						gbep = CreateGBEntry(awp, bsp, gcp->entityID, 
							gcp->itemID, gcp->thistype);
						++awp->total_seg;
						gbep->num_seg = awp->total_seg;
					}
				} else {
					if (ISA_na(bsp->mol) && (bsp->repr == Seq_repr_raw 
		   		|| bsp->repr == Seq_repr_const|| bsp->repr == Seq_repr_delta
		   		|| 	(is_www && bsp->repr == Seq_repr_virtual))) {
						if (ASN2FF_LOCAL_ID == FALSE) {
							sip = SeqIdSelect(bsp->id, fasta_order, NUM_SEQID);
							if (sip && sip->choice != SEQID_LOCAL) {
								gbep = CreateGBEntry(awp, bsp, gcp->entityID, 
									gcp->itemID, gcp->thistype);
								++awp->total_seg;
								gbep->num_seg = awp->total_seg;
							}
						} else {
							gbep = CreateGBEntry(awp, bsp, gcp->entityID, 
								gcp->itemID, gcp->thistype);
							++awp->total_seg;
							gbep->num_seg = awp->total_seg;
						}
					} else if (ISA_na(bsp->mol) && bsp->repr == Seq_repr_map &&
							ajp->map_view) {
							gbep = CreateGBEntry(awp, bsp, gcp->entityID, 
								gcp->itemID, gcp->thistype);
							gbep->map = TRUE;
					}
				}
			} else {
				if (bsp->seq_ext_type == 1) {
					slp = bsp->seq_ext;
					while (slp) {
						bsp = BioseqFind(SeqLocId(slp));
						if (bsp->repr == Seq_repr_raw || 
							bsp->repr == Seq_repr_const 
							|| bsp->repr == Seq_repr_delta 
							|| (is_www && bsp->repr == Seq_repr_virtual)) {
							if (CompareToAwpList(bsp, awp) == FALSE) {
								if (ASN2FF_LOCAL_ID == FALSE) {
									isip = bsp->id;
									sip = SeqIdSelect(isip, 
										fasta_order, NUM_SEQID);
									if (sip && sip->choice != SEQID_LOCAL) {
										gbep = CreateGBEntry(awp, bsp, 
											gcp->entityID, gcp->itemID, 
												gcp->thistype);
										++awp->total_seg;
										gbep->num_seg = awp->total_seg;
									} else if (sip->choice == SEQID_LOCAL && 
										(format == GENPEPT_FMT || 
												format == EMBLPEPT_FMT)) {
										gbep = CreateGBEntry(awp, bsp, 
											gcp->entityID, gcp->itemID, 
													gcp->thistype);
										++awp->total_seg;
										gbep->num_seg = awp->total_seg;
									} else {
										gbep = CreateGBEntry(awp, bsp, 
											gcp->entityID, gcp->itemID, 
												gcp->thistype);
										++awp->total_seg;
										gbep->num_seg = awp->total_seg;
								}
								}
							}
						}
						slp = slp->next;
					}
				} else if (ISA_na(bsp->mol) && (bsp->repr == Seq_repr_raw || 
						bsp->repr == Seq_repr_const 
							|| bsp->repr == Seq_repr_delta 
							|| (is_www && bsp->repr == Seq_repr_virtual))) {
						if (CompareToAwpList(bsp, awp) == FALSE) {
						if (ASN2FF_LOCAL_ID == FALSE) {
							isip = bsp->id;
							sip = SeqIdSelect(isip, fasta_order, NUM_SEQID);
							if (sip && sip->choice != SEQID_LOCAL) {
								gbep = CreateGBEntry(awp, bsp, gcp->entityID, 
											gcp->itemID, gcp->thistype);
								++awp->total_seg;
								gbep->num_seg = awp->total_seg;
							} else if (sip->choice == SEQID_LOCAL && 
									(format == GENPEPT_FMT || 
												format == EMBLPEPT_FMT)) {
								gbep = CreateGBEntry(awp, bsp, gcp->entityID, 
											gcp->itemID, gcp->thistype);
								++awp->total_seg;
								gbep->num_seg = awp->total_seg;
							} else {
								gbep = CreateGBEntry(awp, bsp, gcp->entityID, 
											gcp->itemID, gcp->thistype);
								++awp->total_seg;
								gbep->num_seg = awp->total_seg;
							}
						}
					}
				}
			}
			break;
		case OBJ_BIOSEQSET:
			bssp = (BioseqSetPtr) gcp->thisitem;
			if (bssp->_class == 4) {/*parts*/
				ep = bssp->seq_set;
				if (ep != NULL) {
					bsp = ep->data.ptrvalue;
					if (ISA_na(bsp->mol) && (format == GENBANK_FMT ||
						format == EMBL_FMT || format == PSEUDOEMBL_FMT)) {
						awp->parts = bssp;
					} else if (ISA_aa(bsp->mol) && 
						(format == GENPEPT_FMT || format == EMBLPEPT_FMT)) {
						awp->parts = bssp;
					}
				}
			}
			break;
		default:
			break;
			
	}
	return TRUE;

}