File: valid.c

package info (click to toggle)
ncbi-tools6 6.0.2-1.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 33,932 kB
  • ctags: 50,922
  • sloc: ansic: 590,557; sh: 716; makefile: 375; csh: 234; lisp: 81; cpp: 57; perl: 48
file content (3956 lines) | stat: -rw-r--r-- 107,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
/*  valid.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE                          
*               National Center for Biotechnology Information
*                                                                          
*  This software/database is a "United States Government Work" under the   
*  terms of the United States Copyright Act.  It was written as part of    
*  the author's official duties as a United States Government employee and 
*  thus cannot be copyrighted.  This software/database is freely available 
*  to the public for use. The National Library of Medicine and the U.S.    
*  Government have not placed any restriction on its use or reproduction.  
*                                                                          
*  Although all reasonable efforts have been taken to ensure the accuracy  
*  and reliability of the software and data, the NLM and the U.S.          
*  Government do not and cannot warrant the performance or results that    
*  may be obtained by using this software or data. The NLM and the U.S.    
*  Government disclaim all warranties, express or implied, including       
*  warranties of performance, merchantability or fitness for any particular
*  purpose.                                                                
*                                                                          
*  Please cite the author in any work or product based on this material.   
*
* ===========================================================================
*
* File Name:  valid.c
*
* Author:  James Ostell
*   
* Version Creation Date: 1/1/94
*
* $Revision: 6.48 $
*
* File Description:  Sequence editing utilities
*
* Modifications:  
* --------------------------------------------------------------------------
* Date	   Name        Description of modification
* -------  ----------  -----------------------------------------------------
*
* $Log: valid.c,v $
* Revision 6.48  1999/01/05 23:20:50  kans
* SpliceCheckEx does not check exon junction if partial
*
* Revision 6.47  1998/12/14 22:27:28  kans
* CdTransCheck now deals with termination by polyA
*
* Revision 6.46  1998/12/07 20:00:56  kans
* meant to set bcp = NULL, not bsp = NULL, crashed with segmented protein
*
* Revision 6.45  1998/10/26 20:57:45  kans
* check gene and prot db fields for IllegalDbXref
*
* Revision 6.44  1998/10/23 15:25:57  kans
* added FarLocation warning
*
* Revision 6.43  1998/10/22 16:05:57  kans
* removed labeltype parameter from SeqMgrIndexFeatures, changed index parameter/field to Uint2
*
* Revision 6.42  1998/10/21 14:32:11  kans
* on invalid feature for bioseq, restore itemid itemid and itemtype to avoid weird(er) click association - need to rewrite valid with new index functions, which will give proper items
*
* Revision 6.41  1998/10/20 20:18:10  kans
* mRNA feature is invalid on an mRNA (cDNA) bioseq
*
* Revision 6.40  1998/10/20 18:12:54  kans
* invalid for type (e.g., intron on mRNA) now coerces gcp to have feature itemtype, itemID for selection
*
* Revision 6.39  1998/10/15 17:29:18  kans
* import feature of mat_, sig_, and transit_peptide now flagged as invalid for type
*
* Revision 6.38  1998/09/22 13:12:01  kans
* locationFilter parameter to explore features function
*
* Revision 6.37  1998/09/21 17:29:35  kans
* precursor rna can have intron feature
*
* Revision 6.36  1998/09/17 16:38:14  kans
* added ERR_SEQ_DESCR_NoMolInfoFound
*
* Revision 6.35  1998/09/01 19:25:27  kans
* context parameter in get best protein, get cds/rna given product
*
* Revision 6.34  1998/08/28 22:25:56  kans
* keep track of last biomol, tech, completeness in multiple molinfo descriptors
*
* Revision 6.33  1998/08/26 21:07:48  kans
* added check for ERR_SEQ_INST_ConflictingIdsOnBioseq
*
* Revision 6.32  1998/08/10 16:05:15  kans
* copy some old descriptor checks to Molinfo
*
* Revision 6.31  1998/07/23 14:25:38  kans
* intron and CAAT_signal are illegal on mRNA - first checks molinfo, then resorts to Seq_mol_rna as mRNA criterion
*
* Revision 6.30  1998/07/16 16:06:56  kans
* use ObjMgrGetEntityIDForChoice instead of ObjMgrGetEntityIDForPointer for SeqEntryPtr
*
* Revision 6.29  1998/07/14 18:10:33  kans
* invalid feature for nucleotide now says nucleotide, not protein
*
* Revision 6.28  1998/07/06 18:01:52  kans
* added LIBCALLBACK to SeqMgrExplore function callbacks
*
* Revision 6.27  1998/07/02 17:53:43  kans
* useSeqMgrIndexes field added to ValidStructPtr, validator can use either old (nested gathers) or new (SeqMgr indexing) method
*
* Revision 6.26  1998/06/24 18:49:15  kans
* added missing BioseqContextFree
*
* Revision 6.25  1998/06/22 20:13:21  kans
* gencode mismatch reports biosource and cds codes
*
* Revision 6.24  1998/06/12 20:05:53  kans
* fixed unix compiler warnings
*
* Revision 6.23  1998/04/16 15:12:15  kans
* slight fix to frame > 1 and not at splice site test
*
* Revision 6.22  1998/04/15 21:59:25  kans
* added ERR_SEQ_FEAT_IllegalDbXref
*
* Revision 6.21  1998/04/14 20:57:36  kans
* check for mixed bioseqs in segset, parts set, and for sets within parts set
*
* Revision 6.20  1998/04/14 19:11:25  kans
* improvements to PartialAtSpliceSite and frame > 1 check
*
* Revision 6.19  1998/04/14 18:55:56  kans
* cds frame > 1 but not 5prime partial now also checks for PartialAtSpliceSite
*
* Revision 6.18  1998/04/13 18:10:38  kans
* warn if CDS frame > 1 but not 5prime partial
*
* Revision 6.17  1998/04/02 15:45:51  kans
* MolInfoPtr had not been obtained for Seq_repr_raw for HTGS test on long sequences
*
* Revision 6.16  1998/03/30 17:35:22  kans
* check raw bioseq for htgs flags if greater than 350kb
*
* Revision 6.15  1998/03/18 20:41:50  kans
* SpliceCheck only on mRNA (not all RNAs) and CDS
*
* Revision 6.14  1998/03/09 17:48:46  kans
* OBJ_SEQSUB_CIT now satisfies need for publication
*
* Revision 6.13  1998/02/19 17:21:15  shavirin
* Added check for NULL in ValidErr() function
*
* Revision 6.12  1998/02/18 20:34:55  kans
* added ERR_GENERIC_MissingPubInfo
*
* Revision 6.11  1998/02/09 20:35:35  kans
* calls ERR_SEQ_FEAT_PseudoCdsHasProduct
*
* Revision 6.10  1998/01/30 21:05:54  kans
* check for ERR_SEQ_DESCR_MultipleBioSources
*
* Revision 6.9  1998/01/30 20:29:48  kans
* added PartialAtSpliceSite check
*
* Revision 6.8  1998/01/13 15:34:50  kans
* gbqual_citation satisfied by sfp->cit
*
* Revision 6.7  1998/01/10 00:05:36  kans
* added ValidateImpFeat
*
* Revision 6.6  1998/01/06 03:07:57  ostell
* in comparison of cdregion genetic code to biosource genetic code, set defaults
* to 0 instead of -1 to fix default behavior on building submission.
*
* Revision 6.5  1997/12/18 21:51:43  kans
* warn on cds/biosource genetic code conflict, rna type 0
*
* Revision 6.4  1997/11/14 17:10:13  kans
* added checks for bioseq length > 350K (based on Cavanaugh request)
*
* Revision 6.3  1997/08/27 20:11:02  kans
* order gene should in fact have partial flag set
*
* Revision 6.2  1997/08/27 19:48:32  kans
* print feature product seqloc
*
* Revision 6.1  1997/08/27 14:15:51  kans
* gene of order should not cause partial error
*
* Revision 6.0  1997/08/25 18:08:25  madden
* Revision changed to 6.0
*
* Revision 5.24  1997/08/13 18:52:51  kans
* new packaging errors set to SEV_FATAL
*
* Revision 5.23  1997/08/13 15:36:53  kans
* added NucProtNotSegSet and SegSetNotParts (Bazhin)
*
* Revision 5.22  1997/07/07 21:28:11  kans
* existing bad start codon check was being bypassed, so new one was added
*
* Revision 5.21  1997/07/07 15:00:28  kans
* signal or transit peptide do not need names
*
* Revision 5.20  1997/07/02 19:44:09  kans
* added check for et al, changed symbol names for empty gene and prot feature
*
* Revision 5.19  1997/06/24 16:39:12  kans
* fixed Digital Unix compiler complaint
*
* Revision 5.18  1997/06/19 18:39:51  vakatov
* [WIN32,MSVC++]  Adopted for the "NCBIOBJ.LIB" DLL'ization
*
* Revision 5.17  1997/05/29 17:25:16  kans
* splice check and trans check not done if excpt
*
* Revision 5.16  1997/05/28 19:10:32  kans
* added check for empty protref
*
* Revision 5.15  1997/05/20 21:11:38  kans
* warnings for delta seq not htgs1 or 2, cds orf with product, gene with no fields, cds exception gbqual without excpt
*
* Revision 5.14  1997/04/24 20:39:20  kans
* invalid splice sites are warning level unless app property forces to error
*
 * Revision 5.13  1997/03/17  21:43:28  kans
 * added closing bracket to bioseq length indication
 *
 * Revision 5.12  1997/02/20  13:50:33  ostell
 * added length check on segmented sequence back
 *
 * Revision 5.11  1996/11/22  17:23:20  kans
 * splice errors on exon imp-feats are now severity warning, since there is
 * no way of knowing which are the unspliced ends of the first and last exon
 *
 * Revision 5.10  1996/11/04  16:29:55  kans
 * app property allows splice check for exon features, and rare GC splice
 * donor has separate warning message
 *
 * Revision 5.9  1996/10/16  20:31:16  ostell
 * added length check for delta sequences
 * added CdTrnsCheck for exception and pseudo
 *
 * Revision 5.8  1996/08/21  14:08:26  ostell
 * rmoved kludge for big sequences
 *
 * Revision 5.7  1996/08/19  02:45:49  ostell
 * added check in BioseqContect for more than 30n bioseqs to control
 * feature checkes
 *
 * Revision 5.6  1996/08/06  19:56:03  kans
 * for SEQLOC_WHOLE, must call SeqIdFindBest on bsp->id
 *
 * Revision 5.5  1996/08/01  18:58:00  kans
 * on pseudo cds, suppress CdTransCheck, SpliceCheck
 *
 * Revision 5.4  1996/06/19  00:35:32  ostell
 * added check for ragged end of CdRegion
 *
 * Revision 5.1  1996/06/16  04:16:05  ostell
 * added support for delta seq
 *
 * Revision 5.0  1996/05/28  13:23:23  ostell
 * Set to revision 5.0
 *
 * Revision 4.19  1996/05/03  18:59:13  kans
 * up to 5 stops still allows mismatch report, which includes nuc position
 *
 * Revision 4.18  1996/04/01  16:31:47  ostell
 * fix to preserver eror message count between invocations
 *
 * Revision 4.17  1996/03/15  20:01:14  ostell
 * in SpliceCheck, give accession of sequence with bad junction
 *
 * Revision 4.16  1996/03/08  14:48:02  kans
 * fixed typos in ValidateSeqEntry scope memset, use as parameter
 *
 * Revision 4.15  1996/03/06  20:43:59  ostell
 * added scoping to validation
 *
 * Revision 4.14  1996/03/05  19:54:29  kans
 * added biosource to two switch statements
 *
 * Revision 4.13  1996/03/03  16:59:34  ostell
 * added SpellCheckPub() to look at more Pub types
 *
 * Revision 4.12  1996/03/02  03:41:43  ostell
 * fix to correctly identigy splice junctions on minus strand
 *
 * Revision 4.11  1996/02/26  22:06:37  ostell
 * finished gatherized version of spell check on descriptors
 *
 * Revision 4.10  1996/02/19  19:58:05  ostell
 * added support for Code-break and tRNA.anticodon
 *
 * Revision 4.9  1996/01/23  23:10:10  kans
 * implemented onlyspell and justwarnonspell code
 *
 * Revision 4.8  1995/12/07  01:55:37  ostell
 * fix to check for NULL on bioseqset parent
 *
 * Revision 4.7  1995/12/07  01:38:56  ostell
 * added Splice error flag
 *
 * Revision 4.6  1995/12/06  22:11:23  ostell
 * changed wording of SpliceCheck message
 *
 * Revision 4.5  1995/12/06  06:08:57  ostell
 * lowered warning levels on partial messages
 * added SpliceCheck()
 *
 * Revision 4.4  1995/08/16  18:21:52  epstein
 * correct declaration of static functions to be consistent with function prototypes
 *
 * Revision 4.3  1995/08/04  18:41:02  madden
 * removed "|SpellErr|" SpellCallBack.
 *
 * Revision 4.2  1995/08/03  12:45:56  madden
 * Set ValNodePtr in SpellCheckBioseqDescr; added "SpellErr" to ErrPosting.
 *
 * Revision 4.1  1995/08/02  22:21:50  madden
 * gatherized the spell functions.
 *
 * Revision 4.0  1995/07/26  13:49:01  ostell
 * force revision to 4.0
 *
 * Revision 1.14  1995/06/03  13:45:47  ostell
 * changes made in valid to use gather functions and ErrPostItem instead
 * of previous custom functions
 *
 * Revision 1.13  1995/05/15  21:46:05  ostell
 * added Log line
 *
*
*
* ==========================================================================
*/

static char *this_module = "valid";
#define THIS_MODULE this_module

static char *this_file = __FILE__;
#define THIS_FILE this_file

#include <ncbi.h>
#include <objfdef.h>
#include <valid.h>
#include <validerr.h>
#include <sqnutils.h>
#include <gbftdef.h>
#include <gbfeat.h>
#include <objsub.h>
#include <asn2ffp.h>
#include <explore.h>
#include <subutil.h>

/*****************************************************************************
*
*   NOTE: look at all the ValidErr calls with severity=0. Some should be
*   bumped up later. Look also for string "PARSER"
*
*****************************************************************************/



#ifdef VAR_ARGS
#include <varargs.h>
#else
#include <stdarg.h>
#endif

static ValidStructPtr globalvsp;  /* for spell checker */

NLM_EXTERN void CDECL  ValidErr VPROTO((ValidStructPtr vsp, int severity, int code1, int code2, const char *fmt, ...));
static void ValidateBioseqInst (GatherContextPtr gcp);
static void ValidateBioseqContext (GatherContextPtr gcp);
static void ValidateBioseqSet (GatherContextPtr gcp);
static void SpellCheckSeqDescr(GatherContextPtr gcp);
NLM_EXTERN void CdTransCheck(ValidStructPtr vsp, SeqFeatPtr sfp);
void ValidateFeatureTable (SeqEntryPtr sep, Pointer data, Int4 index, Int2 indent);
NLM_EXTERN void ValidateSeqFeat(GatherContextPtr gcp);
NLM_EXTERN void ValidateSeqLoc(ValidStructPtr vsp, SeqLocPtr slp, CharPtr prefix);
NLM_EXTERN Boolean PatchBadSequence(BioseqPtr bsp);
NLM_EXTERN CharPtr FindIDForEntry (SeqEntryPtr sep, CharPtr buf);
NLM_EXTERN void SpellCheckSeqFeat(GatherContextPtr gcp);
NLM_EXTERN void SpellCheckString (ValidStructPtr vsp, CharPtr str);
NLM_EXTERN void SpliceCheck(ValidStructPtr vsp, SeqFeatPtr sfp);
static void SpliceCheckEx(ValidStructPtr vsp, SeqFeatPtr sfp, Boolean checkAll);

/*****************************************************************************
*
*   Perform Validation Checks on a SeqEntry
*
*****************************************************************************/

NLM_EXTERN void ValidStructClear (ValidStructPtr vsp)  /* 0 out a ValidStruct */
{
	CharPtr errbuf;
	Int2 cutoff;
	Boolean patch_seq;
	SpellCheckFunc spellfunc;
	SpellCallBackFunc spellcallback;
	Boolean onlyspell;
	Boolean justwarnonspell;
	Boolean useSeqMgrIndexes;

	if (vsp == NULL) return;

	errbuf = vsp->errbuf;
	cutoff = vsp->cutoff;
	patch_seq = vsp->patch_seq;
	spellfunc = vsp->spellfunc;
	spellcallback = vsp->spellcallback;
	onlyspell = vsp->onlyspell;
	justwarnonspell = vsp->justwarnonspell;
	useSeqMgrIndexes = vsp->useSeqMgrIndexes;
	MemSet((VoidPtr)vsp, 0, sizeof(ValidStruct));
	vsp->errbuf = errbuf;
	vsp->cutoff = cutoff;
	vsp->patch_seq = patch_seq;
	vsp->spellfunc = spellfunc;
	vsp->spellcallback = spellcallback;
	vsp->onlyspell = onlyspell;
	vsp->justwarnonspell = justwarnonspell;
	vsp->useSeqMgrIndexes = useSeqMgrIndexes;
	return;
}

NLM_EXTERN ValidStructPtr ValidStructNew (void)
{
	ValidStructPtr vsp;

	vsp = (ValidStructPtr)MemNew(sizeof(ValidStruct));
	return vsp;
}

NLM_EXTERN ValidStructPtr ValidStructFree (ValidStructPtr vsp)
{
	if (vsp == NULL) return vsp;

	MemFree(vsp->errbuf);
	return (ValidStructPtr)MemFree(vsp);
}

/*****************************************************************************
*
*   ValidErr()
*
*****************************************************************************/

#ifdef VAR_ARGS
NLM_EXTERN void CDECL  ValidErr (vsp, severity, code1, code2, fmt, va_alist)
  ValidStructPtr vsp;
  int severity;
  int code1;
  int code2;
  const char *fmt;
  va_dcl
#else
NLM_EXTERN void CDECL  ValidErr (ValidStructPtr vsp, int severity, int code1, int code2, const char *fmt, ...)
#endif
{
    va_list  args;
	GatherContextPtr gcp;
	CharPtr tmp, ctmp;
	Int2 buflen, diff;
	BioseqPtr bsp;
	SeqIdPtr sip;

	if (vsp == NULL || severity < vsp->cutoff) return;

	if (vsp->errbuf == NULL)
	{
		vsp->errbuf = MemNew(1024);
		if (vsp->errbuf == NULL)
			AbnormalExit(1);
	}
	tmp = vsp->errbuf;

	vsp->errors[severity]++;

#ifdef VAR_ARGS
    va_start (args);
#else
    va_start (args, fmt);
#endif

	gcp = vsp->gcp;
	buflen = 1023;
    vsprintf (tmp, fmt, args);
	while (*tmp != '\0')
	{
		buflen--;
		tmp++;
	}

	va_end (args);

	if (vsp->sfp != NULL)
	{
		diff = LabelCopy(tmp, " FEATURE: ", buflen);
		buflen -= diff;
		tmp += diff;

		diff = FeatDefLabel(vsp->sfp, tmp, buflen, OM_LABEL_BOTH);
		buflen -= diff;
		tmp += diff;

		ctmp = SeqLocPrint(vsp->sfp->location);
		if (ctmp != NULL)
		{
			diff = LabelCopyExtra(tmp, ctmp, buflen, " [", "]");
			buflen -= diff;
			tmp += diff;
			MemFree(ctmp);
		}

		sip = SeqLocId(vsp->sfp->location);
		if (sip != NULL)
		{
			bsp = BioseqFind(sip);
			if (bsp != NULL)
			{
				diff = LabelCopy(tmp, " [", buflen);
				buflen -= diff;
				tmp += diff;

				diff = BioseqLabel(bsp, tmp, buflen, OM_LABEL_BOTH);
				buflen -= diff;
				tmp += diff;

				diff = LabelCopy(tmp, "]", buflen);
				buflen -= diff;
				tmp += diff;
			}
		}
		if (vsp->sfp->product != NULL) {
			ctmp = SeqLocPrint(vsp->sfp->product);
			if (ctmp != NULL)
			{
				diff = LabelCopyExtra(tmp, ctmp, buflen, " -> [", "]");
				buflen -= diff;
				tmp += diff;
				MemFree(ctmp);
			}
		}
	}
	else if (vsp->descr != NULL)
	{
		diff = LabelCopy(tmp, " DESCRIPTOR: ", buflen);
		buflen -= diff;
		tmp += diff;

		diff = SeqDescLabel(vsp->descr, tmp, buflen, OM_LABEL_BOTH);
		buflen -= diff;
		tmp += diff;
	}

	if (vsp->sfp == NULL)    /* sfp adds its own context */
	{
		if (vsp->bsp != NULL)
		{
			diff = LabelCopy(tmp, " BIOSEQ: ", buflen);
			buflen -= diff;
			tmp += diff;

			diff = BioseqLabel(vsp->bsp, tmp, buflen, OM_LABEL_BOTH);
			buflen -= diff;
			tmp += diff;
		}
		else if (vsp->bssp != NULL)
		{
			diff = LabelCopy(tmp, " BIOSEQ-SET: ", buflen);
			buflen -= diff;
			tmp += diff;
		
			diff = BioseqSetLabel(vsp->bssp, tmp, buflen, OM_LABEL_BOTH);
			buflen -= diff;
			tmp += diff;
		}
	}

	ErrPostItem ((ErrSev) (severity), code1, code2, "%s", vsp->errbuf);
	vsp->errbuf[0] = '\0';

	return;
}

/*****************************************************************************
*
*   Valid1GatherProc(gcp)
*     top level gather callback
*     dispatches to other levels
*
*****************************************************************************/
static Boolean Valid1GatherProc (GatherContextPtr gcp)
{
	ValidStructPtr vsp;

	vsp = (ValidStructPtr)(gcp->userdata);
	vsp->gcp = gcp;     /* needed for ValidErr */

	switch (gcp->thistype)
	{
		case OBJ_BIOSEQ:
			if (! vsp->onlyspell) {
				ValidateBioseqInst (gcp);
				ValidateBioseqContext (gcp);
			}
			break;
		case OBJ_BIOSEQSET:
			if (! vsp->onlyspell) {
				ValidateBioseqSet (gcp);
			}
			break;
		case OBJ_SEQFEAT:
			if (! vsp->onlyspell) {
				ValidateSeqFeat (gcp);
			}
			SpellCheckSeqFeat(gcp);
			break;
		case OBJ_SEQDESC:
			SpellCheckSeqDescr(gcp);
			/**
			ValidateSeqDescr (gcp);
		    **/
			break;
		default:
			break;
		
	}
	return TRUE;
}

NLM_EXTERN Boolean ValidateSeqEntry(SeqEntryPtr sep, ValidStructPtr vsp)
{
	Uint2 entityID;
	GatherScope gs;
	BioseqSetPtr bssp;
	Boolean do_many = FALSE;
	Int2 errors[6], i;

	for (i =0; i < 6; i++)  /* keep errors between clears */
		errors[i] = 0;

	if (IS_Bioseq_set(sep))
	{
		bssp = (BioseqSetPtr)(sep->data.ptrvalue);
		switch (bssp->_class)
		{
			case BioseqseqSet_class_genbank:
			case BioseqseqSet_class_pir:
			case BioseqseqSet_class_gibb:
			case BioseqseqSet_class_gi:
			case BioseqseqSet_class_swissprot:
				sep = bssp->seq_set;
				do_many = TRUE;
				break;
			default:
				break;
		}
	}
					  
	globalvsp = vsp;   /* for spell checker */

	while (sep != NULL)
	{
		MemSet(&gs, 0, sizeof(GatherScope));
		gs.scope = sep;    /* default is to scope to this set */

		ValidStructClear(vsp);
      	vsp->sep = sep;

		/* build seqmgr feature indices if not already done */

		if (vsp->useSeqMgrIndexes) {
			entityID = ObjMgrGetEntityIDForChoice (sep);

			/* do not trust SeqMgrFeaturesAreIndexed time, always reindex */

			SeqMgrIndexFeatures (entityID, NULL);
		}

      	GatherSeqEntry(sep, (Pointer)vsp, Valid1GatherProc, &gs);

		if (do_many)
		{
			for (i = 0; i < 6; i++)
				errors[i] += vsp->errors[i];
			sep = sep->next;
		}
		else
			sep = NULL;
	}

	if (do_many)
	{
		for (i =0; i < 6; i++)
			vsp->errors[i] = errors[i];
	}
      
   return TRUE;
}

static void ValidateSetContents (SeqEntryPtr sep, Pointer data, Int4 index, Int2 indent)
{
    BioseqPtr bsp;
    ValidStructPtr vsp;
      
    vsp = (ValidStructPtr)data;
      
	if (IS_Bioseq(sep))
	{
		bsp = (BioseqPtr)(sep->data.ptrvalue);
		if (ISA_aa(bsp->mol))
			vsp->protcnt++;
		else
			vsp->nuccnt++;
		if (bsp->repr == Seq_repr_seg)
			vsp->segcnt++;

	}
	return;
}


static CharPtr GetBioseqSetClass(Uint1 cl)
{
    if(cl == BioseqseqSet_class_nuc_prot)
        return("nuc-prot");
    if(cl == BioseqseqSet_class_segset)
        return("segset");
    if(cl == BioseqseqSet_class_conset)
        return("conset");
    if(cl == BioseqseqSet_class_parts)
        return("parts");
    if(cl == BioseqseqSet_class_gibb)
        return("gibb");
    if(cl == BioseqseqSet_class_gi)
        return("gi");
    if(cl == BioseqseqSet_class_genbank)
        return("genbank");
    if(cl == BioseqseqSet_class_pir)
        return("pir");
    if(cl == BioseqseqSet_class_pub_set)
        return("pub-set");
    if(cl == BioseqseqSet_class_equiv)
        return("equiv");
    if(cl == BioseqseqSet_class_swissprot)
        return("swissprot");
    if(cl == BioseqseqSet_class_pdb_entry)
        return("pdb-entry");
    if(cl == BioseqseqSet_class_mut_set)
        return("mut-set");
    if(cl == BioseqseqSet_class_pop_set)
        return("pop-set");
    if(cl == BioseqseqSet_class_phy_set)
        return("phy-set");
    if(cl == BioseqseqSet_class_other)
        return("other");
    return("not-set");
}

static void ValidateNucProtSet(BioseqSetPtr bssp, ValidStructPtr vsp)
{
    SeqEntryPtr  sep;
    BioseqSetPtr bssp1;

    if(bssp->_class != BioseqseqSet_class_nuc_prot)
        return;

    for(sep = bssp->seq_set; sep != NULL; sep = sep->next)
    {
        if(!IS_Bioseq_set(sep))
            continue;

        bssp1 = sep->data.ptrvalue;
        if(bssp1 == NULL)
            continue;

        if(bssp1->_class != BioseqseqSet_class_segset)
        {
            ValidErr(vsp, SEV_FATAL, ERR_SEQ_PKG_NucProtNotSegSet,
                     "Nuc-prot Bioseq-set contains wrong Bioseq-set, its class is \"%s\".",
                     GetBioseqSetClass(bssp1->_class));
            break;
        }
    }
}

static void ValidateSegmentedSet(BioseqSetPtr bssp, ValidStructPtr vsp)
{
    SeqEntryPtr  sep;
    BioseqSetPtr bssp1;
    BioseqPtr    bsp;
    Uint1        mol = 0;

    if(bssp->_class != BioseqseqSet_class_segset)
        return;

    for(sep = bssp->seq_set; sep != NULL; sep = sep->next)
    {
        if (IS_Bioseq (sep)) {
        	bsp = (BioseqPtr) sep->data.ptrvalue;
        	if (bsp != NULL) {
        		if (mol == 0 || mol == Seq_mol_other) {
        			mol = bsp->mol;
        		} else if (bsp->mol != Seq_mol_other) {
        			if (ISA_na (bsp->mol) != ISA_na (mol)) {
        			    ValidErr(vsp, SEV_FATAL, ERR_SEQ_PKG_SegSetMixedBioseqs,
                  			   "Segmented set contains mixture of nucleotides and proteins");
        			}
        		}
        	}
        }

        if(!IS_Bioseq_set(sep))
            continue;

        bssp1 = sep->data.ptrvalue;
        if(bssp1 == NULL)
            continue;

        if(bssp1->_class != BioseqseqSet_class_parts)
        {
            ValidErr(vsp, SEV_FATAL, ERR_SEQ_PKG_SegSetNotParts,
                     "Segmented set contains wrong Bioseq-set, its class is \"%s\".",
                     GetBioseqSetClass(bssp1->_class));
            break;
        }
    }
}

static void ValidatePartsSet(BioseqSetPtr bssp, ValidStructPtr vsp)
{
    SeqEntryPtr  sep;
    BioseqSetPtr bssp1;
    BioseqPtr    bsp;
    Uint1        mol = 0;

    if(bssp->_class != BioseqseqSet_class_parts)
        return;

    for(sep = bssp->seq_set; sep != NULL; sep = sep->next)
    {
        if (IS_Bioseq (sep)) {
        	bsp = (BioseqPtr) sep->data.ptrvalue;
        	if (bsp != NULL) {
        		if (mol == 0 || mol == Seq_mol_other) {
        			mol = bsp->mol;
        		} else if (bsp->mol != Seq_mol_other) {
        			if (ISA_na (bsp->mol) != ISA_na (mol)) {
        			    ValidErr(vsp, SEV_FATAL, ERR_SEQ_PKG_PartsSetMixedBioseqs,
                  			   "Parts set contains mixture of nucleotides and proteins");
                  		break;
        			}
        		}
        	}
        }
    }

    for(sep = bssp->seq_set; sep != NULL; sep = sep->next)
    {
        if (IS_Bioseq_set(sep)) {
    	    bssp1 = sep->data.ptrvalue;
     	   if(bssp1 == NULL)
         	   continue;

        	 ValidErr(vsp, SEV_FATAL, ERR_SEQ_PKG_PartsSetHasSets,
                     "Parts set contains unwanted Bioseq-set, its class is \"%s\".",
                     GetBioseqSetClass(bssp1->_class));
            break;
        }
    }
}

static void ValidateBioseqSet (GatherContextPtr gcp)
{
	BioseqSetPtr bssp;
	ValidStructPtr vsp;
	SeqEntryPtr sep;

	vsp = (ValidStructPtr)(gcp->userdata);
	bssp = (BioseqSetPtr)(gcp->thisitem);
	vsp->bssp = bssp;
	vsp->bsp = NULL;
	vsp->descr = NULL;
	vsp->sfp = NULL;

	if (vsp->non_ascii_chars)  /* non_ascii chars in AsnRead step */
	{
		ValidErr(vsp, SEV_ERROR, ERR_GENERIC_NonAsciiAsn, "Non-ascii chars in input ASN.1 strings");
		vsp->non_ascii_chars = FALSE;   /* only do once */
	}

	vsp->nuccnt = 0;
	vsp->segcnt = 0;
	vsp->protcnt = 0;

	sep = gcp->sep;

	SeqEntryExplore(sep, (Pointer)vsp, ValidateSetContents);

	switch (bssp->_class)
	{
		case 1:     /* nuc-prot */
		    if (vsp->nuccnt == 0)
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_PKG_NucProtProblem, "No nucleotides in nuc-prot set");
			if (vsp->protcnt == 0)
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_PKG_NucProtProblem, "No proteins in nuc-prot set");
			ValidateNucProtSet(bssp, vsp);
			break;
		case 2:     /* seg set */
			if (vsp->segcnt == 0)
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_PKG_SegSetProblem, "No segmented Bioseq in segset");
			ValidateSegmentedSet(bssp, vsp);
		case 4:     /* seg set */
			ValidatePartsSet(bssp, vsp);
		default:
			if (! ((vsp->nuccnt) || (vsp->protcnt)))
				ValidErr(vsp, SEV_WARNING, ERR_SEQ_PKG_EmptySet, "No Bioseqs in this set");
			break;
	}
	return;
}

/*****************************************************************************
*
*   ValidateBioseqInst(gcp)
*      Validate one Bioseq Seq-inst
*
*****************************************************************************/
static void ValidateBioseqInst (GatherContextPtr gcp)
{
	Boolean retval = TRUE;
	Int2 i, start_at, num;
	Boolean errors[4], check_alphabet;
	static char * repr [8] = {
		"virtual", "raw", "segmented", "constructed",
		"reference", "consensus", "map", "delta" };
	SeqPortPtr spp;
	Int2 residue, x, termination;
	Int4 len, divisor = 1, len2;
	ValNode head;
	ValNodePtr vnp, vnp2;
	BioseqContextPtr bcp;
	Boolean got_partial, is_invalid;
	int seqtype, terminations;
	ValidStructPtr vsp;
	BioseqPtr bsp;
	SeqIdPtr sip1, sip2;
	Char buf1 [41], buf2 [41];
	SeqLitPtr slitp;
	SeqCodeTablePtr sctp;
	MolInfoPtr mip;
	Boolean litHasData;
	SeqMgrDescContext context;

							/* set up data structures */

	vsp = (ValidStructPtr)(gcp->userdata);
	bsp = (BioseqPtr)(gcp->thisitem);
	vsp->bsp = bsp;
	vsp->descr = NULL;
	vsp->sfp = NULL;
	vsp->bssp = (BioseqSetPtr)(gcp->parentitem);
	vsp->bsp_partial_val = 0;

	if (vsp->non_ascii_chars)  /* non_ascii chars in AsnRead step */
	{
		ValidErr(vsp, SEV_FATAL, ERR_GENERIC_NonAsciiAsn, "Non-ascii chars in input ASN.1 strings");
		vsp->non_ascii_chars = FALSE;   /* only do once */
	}

	if (bsp->id == NULL)
	{
		ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_NoIdOnBioseq, "No ids on a Bioseq");
		return;
	}

	for (sip1 = bsp->id; sip1 != NULL; sip1 = sip1->next) {
		for (sip2 = sip1->next; sip2 != NULL; sip2 = sip2->next) {
			if (SeqIdComp (sip1, sip2) != SIC_DIFF) {
				SeqIdWrite (sip1, buf1, PRINTID_FASTA_SHORT, 40);
				SeqIdWrite (sip2, buf2, PRINTID_FASTA_SHORT, 40);
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_ConflictingIdsOnBioseq,
					"Conflicting ids on a Bioseq: (%s - %s)", buf1, buf2);
			}
		}
	}

	for (i = 0; i < 4; i++)
		errors[i] = FALSE;

	switch (bsp->repr)
	{
		case Seq_repr_virtual:
			if ((bsp->seq_ext_type) || (bsp->seq_ext != NULL))
				errors[0] = TRUE;
			if ((bsp->seq_data_type) || (bsp->seq_data != NULL))
				errors[3] = TRUE;
			break;
		case Seq_repr_map:
			if ((bsp->seq_ext_type != 3) || (bsp->seq_ext == NULL))
				errors[1] = TRUE;
			if ((bsp->seq_data_type) || (bsp->seq_data != NULL))
				errors[3] = TRUE;
			break;
		case Seq_repr_ref:
			if ((bsp->seq_ext_type != 2) || (bsp->seq_ext == NULL))
				errors[1] = TRUE;
			if ((bsp->seq_data_type) || (bsp->seq_data != NULL))
				errors[3] = TRUE;
			break;
		case Seq_repr_seg:
			if ((bsp->seq_ext_type != 1) || (bsp->seq_ext == NULL))
				errors[1] = TRUE;
			if ((bsp->seq_data_type) || (bsp->seq_data != NULL))
				errors[3] = TRUE;
			break;
		case Seq_repr_raw:
		case Seq_repr_const:
			if ((bsp->seq_ext_type) || (bsp->seq_ext != NULL))
				errors[0] = TRUE;
			if ((bsp->seq_data_type < 1) || (bsp->seq_data_type > 11)
				|| (bsp->seq_data == NULL))
				errors[2] = TRUE;
			break;
		case Seq_repr_delta:
			if ((bsp->seq_ext_type != 4) || (bsp->seq_ext == NULL))
				errors[1] = TRUE;
			if ((bsp->seq_data_type) || (bsp->seq_data != NULL))
				errors[3] = TRUE;
			break;
		default:
			ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_ReprInvalid, "Invalid Bioseq->repr = %d", (int)(bsp->repr));
			return;
	}

	if (errors[0] == TRUE)
	{
		ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_ExtNotAllowed, "Bioseq-ext not allowed on %s Bioseq", repr[bsp->repr - 1]);
		retval = FALSE;
	}

	if (errors[1] == TRUE)
	{
		ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_ExtBadOrMissing, "Missing or incorrect Bioseq-ext on %s Bioseq", repr[bsp->repr - 1]);
		retval = FALSE;
	}

	if (errors[2] == TRUE)
	{
		ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_SeqDataNotFound, "Missing Seq-data on %s Bioseq", repr[bsp->repr - 1]);
		retval = FALSE;
	}

	if (errors[3] == TRUE)
	{
		ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_SeqDataNotAllowed, "Seq-data not allowed on %s Bioseq", repr[bsp->repr - 1]);
		retval = FALSE;
	}

	if (! retval) return;

	if (ISA_aa(bsp->mol))
	{
		if (bsp->topology > 1)   /* not linear */
		{
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_CircularProtein, "Non-linear topology set on protein");
		}
		if (bsp->strand > 1)
		{
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_DSProtein, "Protein not single stranded");
		}

	}
	else
	{
		if (! bsp->mol)
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_MolNotSet, "Bioseq.mol is 0");
		else if (bsp->mol == Seq_mol_other)
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_MolOther, "Bioseq.mol is type other");
	}
	                                          /* check sequence alphabet */
	if ((bsp->repr == Seq_repr_raw) || (bsp->repr == Seq_repr_const))
	{
		if (bsp->fuzz != NULL)
		{
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_FuzzyLen, "Fuzzy length on %s Bioseq", repr[bsp->repr - 1]);
		}

		if (bsp->length < 1)
		{
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_InvalidLen, "Invalid Bioseq length [%ld]", (long)bsp->length);
		}

		seqtype = (int)(bsp->seq_data_type);
		switch (seqtype)
		{
			case Seq_code_iupacna:
			case Seq_code_ncbi2na:
			case Seq_code_ncbi4na:
			case Seq_code_ncbi8na:
			case Seq_code_ncbipna:
				if (ISA_aa(bsp->mol))
				{
					ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_InvalidAlphabet,"Using a nucleic acid alphabet on a protein sequence");
					return;
				}
				break;
			case Seq_code_iupacaa:
			case Seq_code_ncbi8aa:
			case Seq_code_ncbieaa:
			case Seq_code_ncbipaa:
			case Seq_code_ncbistdaa:
				if (ISA_na(bsp->mol))
				{
					ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_InvalidAlphabet,"Using a protein alphabet on a nucleic acid");
					return;
				}
				break;
			default:
				ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_InvalidAlphabet, "Using illegal sequence alphabet [%d]",(int)bsp->seq_data_type);
				return;
		}

		check_alphabet = FALSE;
		switch (seqtype)
		{
			case Seq_code_iupacaa:
			case Seq_code_iupacna:
			case Seq_code_ncbieaa:
			case Seq_code_ncbistdaa:
				check_alphabet = TRUE;

			case Seq_code_ncbi8na:
			case Seq_code_ncbi8aa:
				divisor = 1;
				break;

			case Seq_code_ncbi4na:
				divisor = 2;
				break;

			case Seq_code_ncbi2na:
				divisor = 4;
				break;

			case Seq_code_ncbipna:
				divisor = 5;
				break;

			case Seq_code_ncbipaa:
				divisor = 21;
				break;
		}

		len = bsp->length;
		if (len % divisor) len += divisor;
		len /= divisor;
		len2 = BSLen(bsp->seq_data);
		if (len > len2)
		{
			ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_SeqDataLenWrong,"Bioseq.seq_data too short [%ld] for given length [%ld]",	(long)(len2 * divisor), (long)bsp->length);
			return;
		}
		else if (len < len2)
		{
			ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_SeqDataLenWrong,"Bioseq.seq_data is larger [%ld] than given length [%ld]",(long)(len2 * divisor), (long)bsp->length);
		}

		if (check_alphabet)				  /* check 1 letter alphabets */
		{
			switch (seqtype)
			{
				case Seq_code_iupacaa:
				case Seq_code_ncbieaa:
					termination = '*';
					break;
				case Seq_code_ncbistdaa:
					termination = 25;
					break;
                default:
                    termination = '\0';
                    break;
			}
			spp = SeqPortNew(bsp, 0, -1, 0, 0);
			if (spp == NULL)
			{
				ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_SeqPortFail, "Can't open SeqPort");
				return;
			}

			i = 0;
			terminations = 0;
			for (len=0; len < bsp->length; len++)
			{
				residue = SeqPortGetResidue(spp);
				if (! IS_residue(residue))
				{
					i++;
					if (i > 10)
					{
						ValidErr(vsp, SEV_FATAL,ERR_SEQ_INST_InvalidResidue,"More than 10 invalid residues. Checking stopped");
						SeqPortFree(spp);
						if (vsp->patch_seq)
							PatchBadSequence(bsp);
						return;
			 		}
					else
					{
						BSSeek(bsp->seq_data, len, SEEK_SET);
						x = BSGetByte(bsp->seq_data);
						if (bsp->seq_data_type == Seq_code_ncbistdaa)
							ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_InvalidResidue, "Invalid residue [%d] in position [%ld]",	(int)x, (long)(len+1));
						else
							ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_InvalidResidue, "Invalid residue [%c] in position [%ld]",	(char)x, (long)(len+1));
					}
				}
				else if (residue == termination)
					terminations++;
			}
			SeqPortFree(spp);
			if (terminations)
			{
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_StopInProtein,"[%d] termination symbols in protein sequence", terminations);
				if (! i)
					return;
			}
			if (i)
			{
				if (vsp->patch_seq)
					PatchBadSequence(bsp);
				return;
			}

		}
	}

	if ((bsp->repr == Seq_repr_seg) ||
		(bsp->repr == Seq_repr_ref))/* check segmented sequence */
	{
		head.choice = SEQLOC_MIX;
		head.data.ptrvalue = bsp->seq_ext;
		head.next = NULL;
		ValidateSeqLoc(vsp, (SeqLocPtr)&head, "Segmented Bioseq");
		                            /* check the length */
		len = 0;
		vnp = NULL;
		while ((vnp = SeqLocFindNext(&head, vnp)) != NULL)
		{
			len2 = SeqLocLen(vnp);
			if (len2 > 0)
				len += len2;
		}
		if (bsp->length > len)
		{
			ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_SeqDataLenWrong,"Bioseq.seq_data too short [%ld] for given length [%ld]",	(long)(len), (long)bsp->length);
		}
		else if (bsp->length < len)
		{
			ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_SeqDataLenWrong,"Bioseq.seq_data is larger [%ld] than given length [%ld]",(long)(len), (long)bsp->length);
		}
		

		vsp->bsp_partial_val = SeqLocPartialCheck((SeqLocPtr)(&head));
		if ((vsp->bsp_partial_val) && (ISA_aa(bsp->mol)))
		{
			bcp = NULL;
			vnp = NULL;
			got_partial = FALSE;
			if (vsp->useSeqMgrIndexes) {
			  vnp = SeqMgrGetNextDescriptor(bsp, vnp, Seq_descr_modif, &context);
			} else {
			  bcp = BioseqContextNew(bsp);
			  vnp = BioseqContextGetSeqDescr(bcp, Seq_descr_modif, vnp, NULL);
			}
			while (vnp != NULL)
			{
				for (vnp2 = (ValNodePtr)(vnp->data.ptrvalue); vnp2 != NULL; vnp2 = vnp2->next)
				{
					switch(vnp2->data.intvalue)
					{
						case 10:   /* partial */
							got_partial = TRUE;
							break;
						case 16:   /* no-left */
							if (! (vsp->bsp_partial_val & SLP_START))
								ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_PartialInconsistent, "GIBB-mod no-left inconsistent with segmented SeqLoc");
							got_partial = TRUE;
							break;
						case 17:   /* no-right */
							if (! (vsp->bsp_partial_val & SLP_STOP))
								ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_PartialInconsistent, "GIBB-mod no-right inconsistent with segmented SeqLoc");
							got_partial = TRUE;
							break;

					}
				}
				if (vsp->useSeqMgrIndexes) {
				  vnp = SeqMgrGetNextDescriptor(bsp, vnp, Seq_descr_modif, &context);
				} else {
				  vnp = BioseqContextGetSeqDescr(bcp, Seq_descr_modif, vnp, NULL);
				}
			}
			if (! vsp->useSeqMgrIndexes) {
			  	BioseqContextFree(bcp);
			}
			if (! got_partial)
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_PartialInconsistent, "Partial segmented sequence without GIBB-mod");
		}
	}

	mip = NULL;

	if (bsp->repr == Seq_repr_delta)
	{
		len = 0;
		for (vnp = (ValNodePtr)(bsp->seq_ext); vnp != NULL; vnp = vnp->next)
		{
			if (vnp->data.ptrvalue == NULL)
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_SeqDataLenWrong, "NULL pointer in delta seq_ext valnode");
			else
			{
				switch (vnp->choice)
				{
					case 1:		 /* SeqLocPtr */
						len2 = SeqLocLen((SeqLocPtr)(vnp->data.ptrvalue));
						if (len2 < 0)
							  ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_SeqDataLenWrong, "-1 length on seq-loc of delta seq_ext");
						else
							len += len2;
						break;
					case 2:     /* SeqLitPtr */
						slitp = (SeqLitPtr)(vnp->data.ptrvalue);
						if (slitp->seq_data != NULL)
						{
							sctp = SeqCodeTableFind(slitp->seq_data_type);
							if (sctp == NULL)
							{
                                ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_InvalidAlphabet, "Using illegal sequence alphabet [%d] in SeqLitPtr",
									(int)slitp->seq_data_type);
								len += slitp->length;
								break;
							}

							start_at = (Int2)(sctp->start_at);
							num = (Int2)(sctp->num);

							switch (slitp->seq_data_type)
							{
								case Seq_code_iupacaa:
								case Seq_code_iupacna:
								case Seq_code_ncbieaa:
								case Seq_code_ncbistdaa:
									BSSeek(slitp->seq_data, 0, SEEK_SET);
									for (len2 = 1; len2 <= (slitp->length); len2++)
									{
										is_invalid = FALSE;
										residue = BSGetByte(slitp->seq_data);
										i = residue - start_at;
										if ((i < 0) || (i >= num))
											is_invalid = TRUE;
										else if (*(sctp->names[i]) == '\0')
											is_invalid = TRUE;
										if (is_invalid)
										{
											if (slitp->seq_data_type == Seq_code_ncbistdaa)
												ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_InvalidResidue, "Invalid residue [%d] in position [%ld]",	(int)residue, (long)(len+len2));
											else
												ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_InvalidResidue, "Invalid residue [%c] in position [%ld]",	(char)residue, (long)(len+len2));
										}
									}
									break;
								default:
									break;
							}
						}
						len += slitp->length;
						break;
					default:
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_ExtNotAllowed, "Illegal choice [%d] in delta chain",
							(int)(vnp->choice));
						break;
				}
			}
		}
		if (bsp->length > len)
		{
			ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_SeqDataLenWrong,"Bioseq.seq_data too short [%ld] for given length [%ld]",	(long)(len), (long)bsp->length);
		}
		else if (bsp->length < len)
		{
			ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_SeqDataLenWrong,"Bioseq.seq_data is larger [%ld] than given length [%ld]",(long)(len), (long)bsp->length);
		}
		vnp = NULL;
		if (vsp->useSeqMgrIndexes) {
		  vnp = SeqMgrGetNextDescriptor (bsp, NULL, Seq_descr_molinfo, &context);
		} else {
		  bcp = BioseqContextNew(bsp);
		  vnp = BioseqContextGetSeqDescr (bcp, Seq_descr_molinfo, NULL, NULL);
		  BioseqContextFree (bcp);
		}
		if (vnp != NULL) {
			mip = (MolInfoPtr) vnp->data.ptrvalue;
			if (mip != NULL) {
				if (mip->tech != MI_TECH_htgs_1 && mip->tech != MI_TECH_htgs_2) {
					ValidErr(vsp, SEV_ERROR, ERR_SEQ_INST_BadDeltaSeq,"Delta seq technique should not be [%d]",(int)(mip->tech));
				}
			}
		}
	}

	if (ISA_aa(bsp->mol))
	{
		if ((bsp->length <= 3) && (bsp->length >= 0))
		{
			ValidErr(vsp, SEV_WARNING, ERR_SEQ_INST_ShortSeq, "Sequence only %ld residues", (long)(bsp->length));
		}

	}
	else
	{
		if ((bsp->length <= 10) && (bsp->length >= 0))
		{
			ValidErr(vsp, SEV_WARNING, ERR_SEQ_INST_ShortSeq, "Sequence only %ld residues", (long)(bsp->length));
		}
	}

	if (bsp->length > 350000)
	{
		if (bsp->repr == Seq_repr_delta) {
			if (mip != NULL) {
				if (mip->tech == MI_TECH_htgs_1 || mip->tech == MI_TECH_htgs_2) {
					ValidErr(vsp, SEV_WARNING, ERR_SEQ_INST_LongHtgsSequence,"Phase 1 or 2 HTGS sequence exceeds 350kbp limit");
				} else if (mip->tech == MI_TECH_htgs_3) {
					ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_SequenceTooLong,"Phase 3 HTGS sequence exceeds 350kbp limit");
				} else {
					len = 0;
					litHasData = FALSE;
					for (vnp = (ValNodePtr)(bsp->seq_ext); vnp != NULL; vnp = vnp->next) {
						if (vnp->choice == 2) {
							slitp = (SeqLitPtr)(vnp->data.ptrvalue);
							if (slitp != NULL) {
								if (slitp->seq_data != NULL) {
									litHasData = TRUE;
								}
								len += slitp->length;
							}
						}
					}
					if (len > 350000 && litHasData) {
						ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_LongLiteralSequence,"Length of sequence literals exceeds 350kbp limit");
					}
				}
			}
		} else if (bsp->repr == Seq_repr_raw) {
			vnp = NULL;
			if (vsp->useSeqMgrIndexes) {
				vnp = SeqMgrGetNextDescriptor (bsp, NULL, Seq_descr_molinfo, &context);
			} else {
				bcp = BioseqContextNew (bsp);
				vnp = BioseqContextGetSeqDescr (bcp, Seq_descr_molinfo, NULL, NULL);
				BioseqContextFree (bcp);
			}
			if (vnp != NULL) {
				mip = (MolInfoPtr) vnp->data.ptrvalue;
			}
			if (mip != NULL) {
				if (mip->tech == MI_TECH_htgs_1 || mip->tech == MI_TECH_htgs_2) {
					ValidErr(vsp, SEV_WARNING, ERR_SEQ_INST_LongHtgsSequence,"Phase 1 or 2 HTGS sequence exceeds 350kbp limit");
				} else if (mip->tech == MI_TECH_htgs_3) {
					ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_SequenceTooLong,"Phase 3 HTGS sequence exceeds 350kbp limit");
				} else {
					ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_SequenceTooLong,"Length of sequence exceeds 350kbp limit");
				}
			} else {
				ValidErr(vsp, SEV_FATAL, ERR_SEQ_INST_SequenceTooLong,"Length of sequence exceeds 350kbp limit");
			}
		} else {
			/* Could be a segset header bioseq that is > 350kbp */
			/* No-op for now? Or generate a warning? */
		}
	}

	return;
}

/*****************************************************************************
*
*   ValidatePubdesc(gcp)
*      Check pubdesc for missing information
*
*****************************************************************************/
static Boolean HasNoText (CharPtr str)

{
  Char  ch;

  if (str != NULL) {
    ch = *str;
    while (ch != '\0') {
      if (ch > ' ') {
        return FALSE;
      }
      str++;
      ch = *str;
    }
  }
  return TRUE;
}

static Boolean HasNoName (ValNodePtr name)

{
	AuthorPtr  ap;
	NameStdPtr  nsp;
	PersonIdPtr  pid;

	if (name != NULL) {
		ap = name->data.ptrvalue;
		if (ap != NULL) {
			pid = ap->name;
			if (pid != NULL) {
				if (pid->choice == 2) {
					nsp = pid->data;
					if (nsp != NULL) {
						if (! HasNoText (nsp->names [0])) {
							return FALSE;
						}
					}
				}
			}
		}
	}
	return TRUE;
}

static void ValidatePubdesc (ValidStructPtr vsp, PubdescPtr pdp)

{
	AuthListPtr  alp;
	CitArtPtr  cap;
	Boolean  hasName, hasTitle;
	ValNodePtr  name;
	ValNodePtr  title;
	ValNodePtr  vnp;

	if (vsp == NULL || pdp == NULL) return;
	for (vnp = pdp->pub; vnp != NULL; vnp = vnp->next) {
		switch (vnp->choice) {
			case PUB_Article :
				cap = (CitArtPtr) vnp->data.ptrvalue;
				hasName = FALSE;
				hasTitle = FALSE;
				if (cap != NULL) {
					for (title = cap->title; title != NULL; title = title->next) {
						if (! HasNoText ((CharPtr) title->data.ptrvalue)) {
							hasTitle = TRUE;
						}
					}
					if (! hasTitle) {
						ValidErr (vsp, SEV_ERROR, ERR_GENERIC_MissingPubInfo,
							"Publication has no title");
					}
					alp = cap->authors;
					if (alp != NULL) {
						if (alp->choice == 1) {
							for (name = alp->names; name != NULL; name = name->next) {
								if (! HasNoName (name)) {
									hasName = TRUE;
								}
							}
						} else if (alp->choice == 2 || alp->choice == 3) {
							for (name = alp->names; name != NULL; name = name->next) {
								if (! HasNoText ((CharPtr) name->data.ptrvalue)) {
									hasName = TRUE;
								}
							}
						}
					}
					if (! hasName) {
						ValidErr (vsp, SEV_ERROR, ERR_GENERIC_MissingPubInfo,
							"Publication has no author names");
					}
				}
				break;
			default :
				break;
		}
	}
}

typedef struct bioseqvalid {
	ValidStructPtr vsp;
	Boolean is_aa;                 /* bioseq is protein? */
	Boolean is_mrna;               /* molinfo is mrna? */
	Boolean is_prerna;             /* molinfo is precursor rna? */
	Boolean got_a_pub;
	int last_na_mol,
		last_na_mod,
		last_organelle,
		last_partialness,
		last_left_right,
		last_biomol,
		last_tech,
		last_completeness,
		num_full_length_src_feat,   /* number full length src feats */
	    num_full_length_prot_ref;
	ValNodePtr last_gb,
		last_embl,
		last_prf,
		last_pir,
		last_sp,
		last_pdb,
		last_create,
		last_update,
		last_biosrc,
		last_orgref;
	OrgRefPtr last_org;
	GatherContextPtr gcp;
} BioseqValidStr, PNTR BioseqValidStrPtr;

/*****************************************************************************
*
*   ValidateSeqFeatContext(gcp)
*      Gather callback helper function for validating context on a Bioseq
*
*****************************************************************************/
static Boolean ValidateSeqFeatCommon (SeqFeatPtr sfp, BioseqValidStrPtr bvsp, ValidStructPtr vsp,
                                      Int4 left, Int4 right, Uint2 featitemid, Boolean farloc)
{
	GatherContextPtr gcp = NULL;
	ImpFeatPtr ifp;
	Uint2 olditemtype;
	Uint2 olditemid;
	RnaRefPtr rrp;

	vsp->descr = NULL;
	vsp->sfp = sfp;

	if (featitemid > 0) {
		gcp = vsp->gcp;
		if (gcp != NULL) {
			olditemid = gcp->itemID;
			olditemtype = gcp->thistype;
			gcp->itemID = featitemid;
			gcp->thistype = OBJ_SEQFEAT;
		}
	}

	if (bvsp->is_aa)
	{
		if (sfp->data.choice == SEQFEAT_PROT)
		{
			if ((left == 0) &&
				(right == ((vsp->bsp->length) -1 )))
				bvsp->num_full_length_prot_ref++;
		}

		switch (sfp->data.choice)
		{
			case SEQFEAT_CDREGION:
			case SEQFEAT_RNA:
			case SEQFEAT_RSITE:
			case SEQFEAT_TXINIT:
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_InvalidForType, "Invalid feature for a protein Bioseq.");
				break;
			default:
				break;
		}

	}
	else
	{
		switch (sfp->data.choice)
		{
			case SEQFEAT_PROT:
			case SEQFEAT_PSEC_STR:
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_InvalidForType, "Invalid feature for a nucleotide Bioseq.");
				break;
			default:
				break;
		}

	}

	if (bvsp->is_mrna) {
		switch (sfp->data.choice) {
			case SEQFEAT_RNA:
				rrp = (RnaRefPtr) sfp->data.value.ptrvalue;
				if (rrp != NULL && rrp->type == 2) {
					ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_InvalidForType, "mRNA feature is invalid on an mRNA (cDNA) Bioseq.");
				}
				break;
			case SEQFEAT_IMP:
				ifp = (ImpFeatPtr) sfp->data.value.ptrvalue;
				if (ifp != NULL && ifp->key != NULL && (! HasNoText (ifp->key))) {
					if (StringCmp (ifp->key, "intron") == 0 ||
						StringCmp (ifp->key, "CAAT_signal") == 0) {
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_InvalidForType, "Invalid feature for an mRNA Bioseq.");
					}
				}
				break;
			default:
				break;
		}
	} else if (bvsp->is_prerna) {
		switch (sfp->data.choice) {
			case SEQFEAT_IMP:
				ifp = (ImpFeatPtr) sfp->data.value.ptrvalue;
				if (ifp != NULL && ifp->key != NULL && (! HasNoText (ifp->key))) {
					if (StringCmp (ifp->key, "CAAT_signal") == 0) {
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_InvalidForType, "Invalid feature for an pre-RNA Bioseq.");
					}
				}
				break;
			default:
				break;
		}
	}

	if (farloc) {
		ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_FarLocation, "Feature has 'far' location - accession not packaged in record");
	}

	if ((sfp->data.choice == SEQFEAT_PUB) ||
		(sfp->cit != NULL))
		bvsp->got_a_pub = TRUE;


	if (gcp != NULL) {
		gcp->itemID = olditemid;
		gcp->thistype = olditemtype;
	}

	return TRUE;
}

static Boolean LIBCALLBACK ValidateSeqFeatIndexed (SeqFeatPtr sfp, SeqMgrFeatContextPtr context)
{
	ValidStructPtr vsp;
	BioseqValidStrPtr bvsp;

	bvsp = (BioseqValidStrPtr) context->userdata;
	vsp = bvsp->vsp;

	return ValidateSeqFeatCommon (sfp, bvsp, vsp, context->left, context->right, context->itemID, context->farloc);
}

static void ValidateSeqFeatContext (GatherContextPtr gcp)
{
	ValidStructPtr vsp;
	BioseqValidStrPtr bvsp;
	SeqFeatPtr sfp;

	bvsp = (BioseqValidStrPtr)(gcp->userdata);
	vsp = bvsp->vsp;
	sfp = (SeqFeatPtr)(gcp->thisitem);

	ValidateSeqFeatCommon (sfp, bvsp, vsp, gcp->extremes.left, gcp->extremes.right, 0, FALSE);
}

/*****************************************************************************
*
*   ValidateSeqDescrContext(gcp)
*      Gather callback helper function for validating context on a Bioseq
*
*****************************************************************************/
static Boolean ValidateSeqDescrCommon (ValNodePtr sdp, BioseqValidStrPtr bvsp, ValidStructPtr vsp)
{
	ValNodePtr vnp, vnp2;
	OrgRefPtr this_org=NULL, that_org=NULL;
	int tmpval;
	Char buf1[20], buf2[20];
	PubdescPtr pdp;
	MolInfoPtr mip;
	static char * badmod = "Inconsistent GIBB-mod [%d] and [%d]";

	vsp->sfp = NULL;
	vnp = sdp;
	vsp->descr = vnp;

	switch (vnp->choice)
	{
		case Seq_descr_mol_type:
			tmpval = (int)(vnp->data.intvalue);
			switch (tmpval)
			{
				case 8:  /* peptide */
					if (! bvsp->is_aa)
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_InvalidForType, "Nuclic acid with GIBB-mol = peptide");
					break;
				case 0:     /* unknown */
				case 255:   /* other */
					ValidErr(vsp,SEV_ERROR, ERR_SEQ_DESCR_InvalidForType , "GIBB-mol unknown or other used");
					break;
				default:    /* the rest are nucleic acid */
					if (bvsp->is_aa)
					{
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_InvalidForType, "GIBB-mol [%d] used on protein",
							tmpval);
					}
					else
					{
						if (bvsp->last_na_mol)
						{
							if (bvsp->last_na_mol != (int)vnp->data.intvalue)
							{
								ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Inconsistent GIBB-mol [%d] and [%d]",
									bvsp->last_na_mol, tmpval);
							}
						}
						else
							bvsp->last_na_mol = tmpval;
					}
					break;
			}
			break;
		case Seq_descr_modif:
			for (vnp2 = (ValNodePtr)(vnp->data.ptrvalue); vnp2 != NULL; vnp2 = vnp2->next)
			{
				tmpval = (int)(vnp2->data.intvalue);
				switch (tmpval)
				{
					case 0:   /* dna */
					case 1:	  /* rna */
						if (bvsp->is_aa)	   /* only temporarily on 0 */
						{
							ValidErr(vsp,SEV_ERROR,ERR_SEQ_DESCR_InvalidForType , "Nucleic acid GIBB-mod [%d] on protein",
								tmpval);
						}
						else if (bvsp->last_na_mod)
						{
							if (tmpval != bvsp->last_na_mod)
							{
								ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, badmod,
									bvsp->last_na_mod, tmpval);
							}
						}
						else
							bvsp->last_na_mod = tmpval;
						break;
					case 4:   /* mitochondria */
					case 5:   /* chloroplast */
					case 6:   /* kinetoplast */
					case 7:   /* cyanelle */
					case 18:  /* macronuclear */
						if (bvsp->last_organelle)
						{
							if (tmpval != bvsp->last_na_mod)
							{
								ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, badmod,
									bvsp->last_organelle, tmpval);
							}
						}
						else
							bvsp->last_organelle = tmpval;
						break;
					case 10:  /* partial */
					case 11:  /* complete */
						if (bvsp->last_partialness)
						{
							if (tmpval != bvsp->last_partialness)
							{
								ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, badmod,
									bvsp->last_partialness, tmpval);
							}
						}
						else
							bvsp->last_partialness = tmpval;
						if ((bvsp->last_left_right) && (tmpval == 11))
						{
							ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, badmod,
								bvsp->last_left_right, tmpval);
						}
						break;
					case 16:   /* no left */
					case 17:   /* no right */
						if (bvsp->last_partialness == 11)  /* complete */
						{
							ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, badmod,
								bvsp->last_partialness, tmpval);
						}
						bvsp->last_left_right = tmpval;
						break;
					case 255:  /* other */
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Unknown, "GIBB-mod = other used");
						break;
					default:
						break;
	
				}
			}
			break;
		case Seq_descr_method:
			if (! bvsp->is_aa)
			{
				ValidErr(vsp, SEV_ERROR,ERR_SEQ_DESCR_InvalidForType, "Nucleic acid with protein sequence method");
			}
			break;
		case Seq_descr_genbank:
			if (bvsp->last_gb != NULL)
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Multiple GenBank blocks");
			else
				bvsp->last_gb = vnp;
			break;
		case Seq_descr_embl:
			if (bvsp->last_embl != NULL)
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Multiple EMBL blocks");
			else
				bvsp->last_embl = vnp;
			break;
		case Seq_descr_pir:
			if (bvsp->last_pir != NULL)
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Multiple PIR blocks");
			else
				bvsp->last_pir = vnp;
			break;
		case Seq_descr_sp:
			if (bvsp->last_sp != NULL)
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Multiple SWISS-PROT blocks");
			else
				bvsp->last_sp = vnp;
			break;
		case Seq_descr_pdb:
			if (bvsp->last_pdb != NULL)
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Multiple PDB blocks");
			else
				bvsp->last_pdb = vnp;
			break;
		case Seq_descr_prf:
			if (bvsp->last_prf != NULL)
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Multiple PRF blocks");
			else
				bvsp->last_prf = vnp;
			break;
		case Seq_descr_create_date:
			if (bvsp->last_create != NULL)
			{
				tmpval = (int)DateMatch((DatePtr)vnp->data.ptrvalue,
					(DatePtr)(bvsp->last_create->data.ptrvalue), FALSE);
				if (tmpval)
				{
					DatePrint((DatePtr)(vnp->data.ptrvalue), buf1);
					DatePrint((DatePtr)(bvsp->last_create->data.ptrvalue), buf2);
					ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Inconsistent create_dates [%s] and [%s]",
						buf1, buf2);
				}
			}
			else
				bvsp->last_create = vnp;
			if (bvsp->last_update != NULL)
			{
				tmpval = (int)DateMatch((DatePtr)vnp->data.ptrvalue,
					(DatePtr)(bvsp->last_update->data.ptrvalue), FALSE);
				if (tmpval == 1)
				{
					DatePrint((DatePtr)(vnp->data.ptrvalue), buf1);
					DatePrint((DatePtr)(bvsp->last_update->data.ptrvalue), buf2);
					ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Inconsistent create_date [%s] and update_date [%s]",
						buf1, buf2);
				}
			}
			break;
		case Seq_descr_update_date:
			if (bvsp->last_create != NULL)
			{
				tmpval = (int)DateMatch((DatePtr)bvsp->last_create->data.ptrvalue,
					(DatePtr)(vnp->data.ptrvalue), FALSE);
				if (tmpval == 1)
				{
					DatePrint((DatePtr)(bvsp->last_create->data.ptrvalue), buf1);
					DatePrint((DatePtr)(vnp->data.ptrvalue), buf2);
					ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Inconsistent create_date [%s] and update_date [%s]",
						buf1, buf2);
				}
			}
			if (bvsp->last_update == NULL)
				bvsp->last_update = vnp;
			break;
		case Seq_descr_source:
			this_org = ((BioSourcePtr)(vnp->data.ptrvalue))->org;
		case Seq_descr_org:
			if (this_org == NULL)
				this_org = (OrgRefPtr)(vnp->data.ptrvalue);
			if (bvsp->last_org != NULL)
			{
				if ((this_org->taxname != NULL) && (bvsp->last_org->taxname != NULL))
				{
					if (StringCmp(this_org->taxname, bvsp->last_org->taxname))
					{
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Inconsistent taxnames [%s] and [%s]",
							this_org->taxname, bvsp->last_org->taxname);
					}
				}
			}
			else
				bvsp->last_org = this_org;
			for (vnp2 = vnp->next; vnp2 != NULL; vnp2 = vnp2->next) {
				if (vnp2->choice == Seq_descr_source || vnp2->choice == Seq_descr_org) {
					that_org = NULL;
					if (vnp2->choice == Seq_descr_source) {
						that_org = ((BioSourcePtr)(vnp2->data.ptrvalue))->org;
					}
					if (that_org == NULL) {
						that_org = (OrgRefPtr)(vnp2->data.ptrvalue);
					}
					if (that_org != NULL) {
						if ((this_org->taxname != NULL) && (that_org->taxname != NULL) &&
							StringCmp (this_org->taxname, that_org->taxname) == 0) {
							ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_MultipleBioSources,
								"Undesired multiple source descriptors");
						}
					}
				}
			}
			break;
		case Seq_descr_pub:
			bvsp->got_a_pub = TRUE;
			pdp = (PubdescPtr) vnp->data.ptrvalue;
			ValidatePubdesc (vsp, pdp);
			break;
		case Seq_descr_molinfo:
			mip = (MolInfoPtr) vnp->data.ptrvalue;
			if (mip != NULL) {
				switch (mip->biomol) {
					case MOLECULE_TYPE_PEPTIDE:  /* peptide */
						if (! bvsp->is_aa) {
							ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_InvalidForType, "Nuclic acid with Molinfo-biomol = peptide");
						}
						break;
					case 0:     /* unknown */
					case 255:   /* other */
						ValidErr(vsp,SEV_ERROR, ERR_SEQ_DESCR_InvalidForType , "Molinfo-biomol unknown or other used");
						break;
					default:   /* the rest are nucleic acid */
						if (bvsp->is_aa) {
							ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_InvalidForType, "Molinfo-biomol [%d] used on protein", (int) mip->biomol);
						} else {
							if (bvsp->last_biomol) {
								if (bvsp->last_biomol != (int) mip->biomol) {
									ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Inconsistent Molinfo-biomol [%d] and [%d]",
										bvsp->last_biomol, (int) mip->biomol);
								}
							} else {
								bvsp->last_biomol = (int) mip->biomol;
							}
						}
						break;
				}
				if (! bvsp->is_aa) {
					switch (mip->tech) {
						case MI_TECH_concept_trans:
						case MI_TECH_seq_pept:
						case MI_TECH_both:
						case MI_TECH_seq_pept_overlap:
						case MI_TECH_seq_pept_homol:
						case MI_TECH_concept_trans_a:
							ValidErr(vsp, SEV_ERROR,ERR_SEQ_DESCR_InvalidForType, "Nucleic acid with protein sequence method");
							break;
						default:
							break;
					}
				}
				if (bvsp->last_tech) {
					if (bvsp->last_tech != (int) mip->tech) {
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Inconsistent Molinfo-tech [%d] and [%d]",
							bvsp->last_tech, (int) mip->tech);
								}
				} else {
					bvsp->last_tech = (int) mip->tech;
				}
				if (bvsp->last_completeness) {
					if (bvsp->last_completeness != (int) mip->completeness) {
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_Inconsistent, "Inconsistent Molinfo-completeness [%d] and [%d]",
							bvsp->last_completeness, (int) mip->completeness);
								}
				} else {
					bvsp->last_completeness = (int) mip->completeness;
				}
			}
			break;
		default:
			break;
	}


	return TRUE;
}

static Boolean LIBCALLBACK ValidateSeqDescrIndexed (ValNodePtr sdp, SeqMgrDescContextPtr context)

{
	ValidStructPtr vsp;
	BioseqValidStrPtr bvsp;

	bvsp = (BioseqValidStrPtr) context->userdata;
	vsp = bvsp->vsp;

	return ValidateSeqDescrCommon (sdp, bvsp, vsp);
}

static void ValidateSeqDescrContext (GatherContextPtr gcp)

{
	ValidStructPtr vsp;
	BioseqValidStrPtr bvsp;
	ValNodePtr sdp;

	bvsp = (BioseqValidStrPtr)(gcp->userdata);
	vsp = bvsp->vsp;
	sdp = (ValNodePtr)(gcp->thisitem);

	ValidateSeqDescrCommon (sdp, bvsp, vsp);
}

/*****************************************************************************
*
*   ValidateBioseqContextGather(gcp)
*      Gather callback for validating context on a Bioseq
*
*****************************************************************************/
static void ValidateCitSub (ValidStructPtr vsp, CitSubPtr csp)

{
	AuthListPtr alp;
	ValNodePtr name;
	Boolean hasName = FALSE;

	if (vsp == NULL || csp == NULL) return;
	alp = csp->authors;
	if (alp != NULL) {
		if (alp->choice == 1) {
			for (name = alp->names; name != NULL; name = name->next) {
				if (! HasNoName (name)) {
					hasName = TRUE;
				}
			}
		} else if (alp->choice == 2 || alp->choice == 3) {
			for (name = alp->names; name != NULL; name = name->next) {
				if (! HasNoText ((CharPtr) name->data.ptrvalue)) {
					hasName = TRUE;
				}
			}
		}
	}
	if (! hasName) {
		ValidErr (vsp, SEV_ERROR, ERR_GENERIC_MissingPubInfo,
			"Submission citation has no author names");
	}
}

static Boolean ValidateBioseqContextIndexed (BioseqPtr bsp, BioseqValidStrPtr bvsp)
{
	ValidStructPtr vsp;
	CitSubPtr csp;
	ObjMgrDataPtr omdp;
	SeqSubmitPtr ssp;
	SubmitBlockPtr sbp;
	GatherContextPtr gcp;

	gcp = bvsp->gcp;
	vsp = bvsp->vsp;
	vsp->descr = NULL;
	vsp->sfp = NULL;
	vsp->gcp = gcp;   /* needed for ValidErr */

	SeqMgrExploreFeatures (bsp, (Pointer) bvsp, ValidateSeqFeatIndexed, NULL, NULL, NULL);

	SeqMgrExploreDescriptors (bsp, (Pointer) bvsp, ValidateSeqDescrIndexed, NULL);

	omdp = ObjMgrGetData (gcp->entityID);
	if (omdp != NULL && omdp->datatype == OBJ_SEQSUB) {
		ssp = (SeqSubmitPtr) omdp->dataptr;
		if (ssp != NULL) {
			sbp = ssp->sub;
			if (sbp != NULL) {
				bvsp->got_a_pub = TRUE;
				csp = sbp->cit;
				/* csp = (CitSubPtr) gcp->thisitem; */
				ValidateCitSub (vsp, csp);
			}
		}
	}

	return TRUE;
}

static Boolean ValidateBioseqContextGather (GatherContextPtr gcp)
{
	ValidStructPtr vsp;
	BioseqValidStrPtr bvsp;
	CitSubPtr csp;

	bvsp = (BioseqValidStrPtr)(gcp->userdata);
	vsp = bvsp->vsp;
	vsp->descr = NULL;
	vsp->sfp = NULL;
	vsp->gcp = gcp;   /* needed for ValidErr */

	switch (gcp->thistype)
	{
		case OBJ_SEQFEAT:
			ValidateSeqFeatContext (gcp);
			break;
		case OBJ_SEQDESC:
			ValidateSeqDescrContext (gcp);
			break;
		case OBJ_SEQSUB_CIT:
			bvsp->got_a_pub = TRUE;
			csp = (CitSubPtr) gcp->thisitem;
			ValidateCitSub (vsp, csp);
			break;
		default:
			break;
	}
	return TRUE;
}

/*****************************************************************************
*
*   ValidateBioseqContext(gcp)
*      Validate one Bioseq for descriptors, features, and context
*      This is done as a second Gather, focussed on the Bioseq in
*        question.
*
*****************************************************************************/
static void ValidateBioseqContext (GatherContextPtr gcp)
{
	ValidStructPtr vsp;
	BioseqPtr bsp;
	GatherScope gs;
	BioseqValidStr bvs;
	SeqFeatPtr sfp;
	ValNode fake_whole;
	SeqIdPtr sip;
	ValNodePtr vnp = NULL;
	MolInfoPtr mip = NULL;
	SeqMgrDescContext context;
	BioseqContextPtr bcp;

	vsp = (ValidStructPtr)(gcp->userdata);
	bsp = (BioseqPtr)(gcp->thisitem);
	vsp->bsp = bsp;
	vsp->descr = NULL;
	vsp->sfp = NULL;
	vsp->bssp = (BioseqSetPtr)(gcp->parentitem);

	MemSet(&gs, 0, sizeof(GatherScope));
	fake_whole.choice = SEQLOC_WHOLE;
	sip = SeqIdFindBest(bsp->id, 0);

	fake_whole.data.ptrvalue = sip;

	fake_whole.next = NULL;
	gs.target = &fake_whole;
	gs.get_feats_location = TRUE;
	gs.nointervals = TRUE;
	MemSet((Pointer)(gs.ignore), (int)TRUE, (size_t)(sizeof(Boolean) * OBJ_MAX));
	gs.ignore[OBJ_SEQDESC] = FALSE;
	gs.ignore[OBJ_SEQFEAT] = FALSE;
	gs.ignore[OBJ_SEQANNOT] = FALSE;
	gs.ignore[OBJ_SUBMIT_BLOCK] = FALSE;
	gs.ignore[OBJ_SEQSUB_CIT] = FALSE;

	gs.scope = vsp->sep;

	MemSet(&bvs, 0, sizeof(BioseqValidStr));
	bvs.vsp = vsp;

	/* now looking for molinfo on every bioseq (okay on segset) */
	if (bsp != NULL) {
		vnp = NULL;
		if (vsp->useSeqMgrIndexes) {
		  vnp = SeqMgrGetNextDescriptor (bsp, NULL, Seq_descr_molinfo, &context);
		} else {
		  bcp = BioseqContextNew(bsp);
		  vnp = BioseqContextGetSeqDescr (bcp, Seq_descr_molinfo, NULL, NULL);
		  BioseqContextFree (bcp);
		}
		if (vnp != NULL) {
			mip = (MolInfoPtr) vnp->data.ptrvalue;
		}
	}

	bvs.is_mrna = FALSE;
	bvs.is_prerna = FALSE;
	if (bsp != NULL && ISA_na (bsp->mol)) {
		if (mip != NULL) {
			if (mip->biomol == MOLECULE_TYPE_MRNA) {
				bvs.is_mrna = TRUE;
			} else if (mip->biomol == MOLECULE_TYPE_PRE_MRNA) {
			  bvs.is_prerna = TRUE;
			}
		} else if (bsp->mol == Seq_mol_rna) {
			bvs.is_mrna = TRUE;  /* if no molinfo, assume rna is mrna */
		}
	}

	if (ISA_aa(bsp->mol))
	{
		bvs.is_aa = TRUE;
		    /* check proteins in nuc-prot set have a CdRegion */
		if (vsp->bssp != NULL)
		{
			if (vsp->bssp->_class == 1)   /* in a nuc-prot set */
			{
				if (vsp->useSeqMgrIndexes) {
					sfp = SeqMgrGetCDSgivenProduct (bsp, NULL);
				} else {
					sfp = SeqEntryGetSeqFeat(vsp->sep, 3, NULL, NULL, 1, bsp);
				}
				if (sfp == NULL)   /* no CdRegion points to this bsp */
					ValidErr(vsp, SEV_ERROR, ERR_SEQ_PKG_NoCdRegionPtr,	"No CdRegion in nuc-prot set points to this protein");
			}
		}
	}

	if (vsp->useSeqMgrIndexes) {
		bvs.gcp = gcp;
		ValidateBioseqContextIndexed (bsp, &bvs);
	} else {
		GatherSeqEntry (vsp->sep, &bvs, ValidateBioseqContextGather, &gs);
	}

	vsp->gcp = gcp;    /* reset the gcp pointer changed in previous gather */
	vsp->descr = NULL;
	vsp->sfp = NULL;

	if (! bvs.got_a_pub)
		ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_NoPubFound, "No publications refer to this Bioseq.");

	if (! bvs.last_org)
		ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_NoOrgFound, "No organism name has been applied to this Bioseq.");


	if ((bvs.is_aa) && (! bvs.num_full_length_prot_ref))
		ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_NoProtRefFound, "No full length Prot-ref feature applied to this Bioseq");

	/* for now only flag missing molinfo in Sequin */
	if (mip == NULL && GetAppProperty ("SpliceValidateAsError") != NULL) {
		ValidErr(vsp, SEV_ERROR, ERR_SEQ_DESCR_NoMolInfoFound, "No Mol-info applies to this Bioseq");
	}

	return;

}

/*****************************************************************************
*
*   ValidateSeqFeat(gcp)
*
*****************************************************************************/
static Boolean EmptyOrNullString (CharPtr str)

{
  Char  ch;

  if (str == NULL) return TRUE;
  ch = *str;
  while (ch != '\0') {
    if (ch > ' ' && ch <= '~') return FALSE;
    str++;
    ch = *str;
  }
  return TRUE;
}

static void ValidateImpFeat (ValidStructPtr vsp, GatherContextPtr gcp, SeqFeatPtr sfp, ImpFeatPtr ifp)

{
  Boolean    found;
  GBQualPtr  gbqual;
  Int2       i;
  Int2       index;
  CharPtr    key;
  Int2       qual;
  Int2       val;

  if (vsp == NULL || gcp == NULL || sfp == NULL || ifp == NULL) return;
  if (StringCmp (ifp->key, "-") == 0) {
    key = StringSave ("misc_feature");
  } else {
    key = StringSaveNoNull (ifp->key);
  }
  index = GBFeatKeyNameValid (&key, FALSE);
  if (index == -1) {
    if (key != NULL) {
      ValidErr (vsp, SEV_WARNING, ERR_SEQ_FEAT_UnknownImpFeatKey, "Unknown feature key %s", key);
    } else {
      ValidErr (vsp, SEV_WARNING, ERR_SEQ_FEAT_UnknownImpFeatKey, "NULL feature key");
    }
  }
  if (StringICmp (key, "mat_peptide") == 0 ||
      StringICmp (key, "sig_peptide") == 0 ||
      StringICmp (key, "transit_peptide") == 0) {
    ValidErr (vsp, SEV_WARNING, ERR_SEQ_FEAT_InvalidForType,
              "Peptide processing feature should be converted to the appropriate protein feature subtype");
  }
  for (gbqual = sfp->qual; gbqual != NULL; gbqual = gbqual->next) {
    if (StringCmp (gbqual->qual, "gsdb_id") == 0) {
      continue;
    }
    val = GBQualNameValid (gbqual->qual);
    if (val == -1) {
      if (gbqual->qual != NULL) {
        ValidErr (vsp, SEV_WARNING, ERR_SEQ_FEAT_UnknownImpFeatQual, "Unknown qualifier %s", gbqual->qual);
      } else {
        ValidErr (vsp, SEV_WARNING, ERR_SEQ_FEAT_UnknownImpFeatQual, "NULL qualifier");
      }
    } else if (index != -1) {
      found = FALSE;
      for (i = 0; i < ParFlat_GBFeat [index].opt_num; i++) {
        qual = ParFlat_GBFeat [index].opt_qual [i];
        if (qual == val) {
          found = TRUE;
          break;
        }
      }
      if (! found) {
        for (i = 0; i < ParFlat_GBFeat [index].mand_num; i++) {
          qual = ParFlat_GBFeat [index].mand_qual [i];
          if (qual == val) {
            found = TRUE;
            break;
          }
        }
        if (! found) {
          ValidErr (vsp, SEV_WARNING, ERR_SEQ_FEAT_WrongQualOnImpFeat, "Wrong qualifier %s for feature %s", gbqual->qual, key);
        }
      }
    }
  }
  if (index != -1 && ParFlat_GBFeat [index].mand_num > 0) {
    for (i = 0; i < ParFlat_GBFeat [index].mand_num; i++) {
      found = FALSE;
      qual = ParFlat_GBFeat [index].mand_qual [i];
      for (gbqual = sfp->qual; gbqual != NULL; gbqual = gbqual->next) {
        val = GBQualNameValid (gbqual->qual);
        if (qual == val) {
          found = TRUE;
          break;
        }
      }
      if (! found) {
        if (qual == GBQUAL_citation && sfp->cit != NULL) {
          found = TRUE;
        }
      }
      if (! found) {
        ValidErr (vsp, SEV_WARNING, ERR_SEQ_FEAT_MissingQualOnImpFeat, "Missing qualifier %s for feature %s", ParFlat_GBQual_names [qual].name, key);
      }
    }
  }
  MemFree (key);
}

/* PartialAtSpliceSite uses code taken from SpliceCheckEx */
static Boolean PartialAtSpliceSite (SeqLocPtr head, Uint2 slpTag)

{
  BioseqPtr   bsp;
  Int2        residue1, residue2;
  Boolean     rsult = FALSE;
  SeqIdPtr    sip;
  SeqLocPtr   slp = NULL, first = NULL, last = NULL;
  SeqPortPtr  spp = NULL;
  Uint1       strand;
  Int4        strt, stp, donor, acceptor, len;

  if (slpTag != SLP_NOSTART && slpTag != SLP_NOSTOP) return FALSE;
  while ((slp = SeqLocFindPart (head, slp, EQUIV_IS_ONE)) != NULL) {
    if (first == NULL) {
      first = slp;
    }
    last = slp;
  }
  if (first == NULL) return FALSE;

  strand = SeqLocStrand (first);
  if (SeqLocStrand (last) != strand) return FALSE;

  if (slpTag == SLP_NOSTART) {
    slp = first;
  } else {
    slp = last;
  }
  sip = SeqLocId (slp);
  if (sip == NULL) return FALSE;
  acceptor = SeqLocStart (slp);
  donor = SeqLocStop (slp);
  bsp = BioseqLockById (sip);
  if (bsp == NULL) return FALSE;
  len = bsp->length;
  spp = SeqPortNew (bsp, 0, -1, strand, Seq_code_ncbi4na);
  BioseqUnlock (bsp);
  if (spp == NULL) return FALSE;

  if (strand != Seq_strand_minus) {
    strt = acceptor;
    stp = donor;
  } else {
    strt = donor;
    donor = acceptor;
    acceptor = strt;
    stp = len - donor - 1;
    strt = len - acceptor - 1;
  }

  if (slpTag == SLP_NOSTOP && stp < len - 2) {
    SeqPortSeek (spp, (stp + 1), SEEK_SET);
    residue1 = SeqPortGetResidue (spp);
    residue2 = SeqPortGetResidue (spp);
    if (IS_residue (residue1) && IS_residue (residue2)) {
      if ((residue1 & 4) && (residue2 & 8)) {
        rsult = TRUE;
      } else if ((residue1 & 4) && (residue2 & 2)) {
        rsult = TRUE;
      }
    }
  } else if (slpTag == SLP_NOSTART && strt > 1) {
    SeqPortSeek (spp, (strt - 2), SEEK_SET);
    residue1 = SeqPortGetResidue (spp);
    residue2 = SeqPortGetResidue (spp);
    if (IS_residue (residue1) && IS_residue (residue2)) {
      if ((residue1 & 1) && (residue2 & 4)) {
        rsult = TRUE;
      }
    }
  }

  spp = SeqPortFree (spp);
  return rsult;
}

NLM_EXTERN void ValidateSeqFeat( GatherContextPtr gcp)
{
	Int2 type, i, j;
	static char * errclass = "BadLoc";
	static char * parterr[2] = {"PartialProduct", "PartialLocation"};
	static char * parterrs[4] = {
		"Start does not include first/last residue of sequence",
		"Stop does not include first/last residue of sequence",
		"Internal partial intervals do not include first/last residue of sequence",
		"Improper use of partial (greater than or less than)" };
	Uint2 partials[2], errtype;
	Char buf[80];
	CharPtr tmp;
	ValidStructPtr vsp;
	SeqFeatPtr sfp;
	CdRegionPtr crp;
	CodeBreakPtr cbp;
	RnaRefPtr rrp;
	tRNAPtr trp;
	GBQualPtr gbq;
	Boolean pseudo, excpt;
	ImpFeatPtr ifp;
	GeneRefPtr grp;
	ProtRefPtr prp;
	ValNodePtr vnp;
	BioseqPtr bsp;
	BioseqContextPtr bcp;
	BioSourcePtr biop;
	OrgNamePtr onp;
	OrgRefPtr orp;
	Int2 biopgencode;
	Int2 cdsgencode;
	GeneticCodePtr gc;
	PubdescPtr pdp;
	DbtagPtr db = NULL;
	Int4 id = -1;
	SeqMgrDescContext context;

	vsp = (ValidStructPtr)(gcp->userdata);
	sfp = (SeqFeatPtr)(gcp->thisitem);
	vsp->descr = NULL;
	vsp->sfp = sfp;
	type = (Int2)(sfp->data.choice);

	ValidateSeqLoc(vsp, sfp->location, "Location");

	ValidateSeqLoc(vsp, sfp->product, "Product");

	partials[0] = SeqLocPartialCheck(sfp->product);
	partials[1] = SeqLocPartialCheck(sfp->location);
	if ((partials[0] != SLP_COMPLETE) || (partials[1] != SLP_COMPLETE) || (sfp->partial))  /* partialness */
	{
		                           /* a feature on a partial sequence should be partial -- if often isn't */
		if ((! sfp->partial) && (partials[1] != SLP_COMPLETE) &&
			(sfp->location->choice == SEQLOC_WHOLE))
		{
			ValidErr(vsp, SEV_INFO, ERR_SEQ_FEAT_PartialProblem, "On partial Bioseq, SeqFeat.partial should be TRUE");
		}
								  /* a partial feature, with complete location, but partial product */
		else if ((sfp->partial) && (sfp->product != NULL) &&
			(partials[1] == SLP_COMPLETE) && (sfp->product->choice == SEQLOC_WHOLE)
			&& (partials[0] != SLP_COMPLETE))
		{
			ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_PartialProblem,
				"When SeqFeat.product is a partial Bioseq, SeqFeat.location should also be partial");
		}
		                         /* gene on segmented set is now 'order', should also be partial */
		else if (type == SEQFEAT_GENE && sfp->product == NULL && partials [1] == SLP_INTERNAL) {
			if (! sfp->partial) {
				ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_PartialProblem,
					"Gene of 'order' with otherwise complete location should have partial flag set");
			}
		}
		                         /* inconsistent combination of partial/complete product,location,partial flag */
		else if (((partials[0] == SLP_COMPLETE) && (sfp->product != NULL)) ||
			(partials[1] == SLP_COMPLETE) ||
			(! sfp->partial))
		{
			tmp = StringMove(buf, "Inconsistent: ");
			if (sfp->product != NULL)
			{
				tmp = StringMove(tmp, "Product= ");
				if (partials[0])
					tmp = StringMove(tmp, "partial, ");
				else
					tmp = StringMove(tmp, "complete, ");
			}
			tmp = StringMove(tmp, "Location= ");
			if (partials[1])
				tmp = StringMove(tmp, "partial, ");
			else
				tmp = StringMove(tmp, "complete, ");
			tmp = StringMove(tmp, "Feature.partial= ");
			if (sfp->partial)
				tmp = StringMove(tmp, "TRUE");
			else
				tmp = StringMove(tmp, "FALSE");
			ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_PartialProblem, buf);
		}

		                      /* may have other error bits set as well */
		for (i = 0; i < 2; i++)
		{
			errtype = SLP_NOSTART;
			for (j = 0; j < 4; j++)
			{
				if (partials[i] & errtype)
				{
					if (i == 1 && j < 2 && PartialAtSpliceSite (sfp->location, errtype)) {
						ValidErr(vsp, SEV_INFO, ERR_SEQ_FEAT_PartialProblem,
						         "%s: %s (but is at consensus splice site)", parterr[i], parterrs[j]);
					} else {
					  ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_PartialProblem, "%s: %s", parterr[i], parterrs[j]);
					}
				}
				errtype <<= 1;
			}
		}

	}

	for (vnp = sfp->dbxref; vnp != NULL; vnp = vnp->next) {
		id = -1;
		db = vnp->data.ptrvalue;
		if (db && db->db) {
			for (i =0; i < DBNUM; i++) {
				if (StringCmp(db->db, dbtag[i]) == 0) {
					id = i;
					break;
				}
			}
			if (id == -1 || (type != SEQFEAT_CDREGION && id < 4)) {
				ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_IllegalDbXref,
					     "Illegal db_xref type %s", db->db);
			}
		}
	}

	switch (type)
	{
		case 1:        /* Gene-ref */
			grp = (GeneRefPtr) (sfp->data.value.ptrvalue);
			if (grp != NULL) {
			  if (EmptyOrNullString (grp->locus) &&
			      EmptyOrNullString (grp->allele) &&
			      EmptyOrNullString (grp->desc) &&
			      EmptyOrNullString (grp->maploc) &&
			      grp->db == NULL && grp->syn == NULL) {
			  	ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_GeneRefHasNoData,
				        "There is a gene feature where all fields are empty");
			  }
			for (vnp = grp->db; vnp != NULL; vnp = vnp->next) {
				id = -1;
				db = vnp->data.ptrvalue;
				if (db && db->db) {
					for (i =0; i < DBNUM; i++) {
						if (StringCmp(db->db, dbtag[i]) == 0) {
							id = i;
							break;
						}
					}
					if (id == -1 || (type != SEQFEAT_CDREGION && id < 4)) {
						ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_IllegalDbXref,
							     "Illegal db_xref type %s", db->db);
					}
				}
			}
			}
			break;
		case 2:        /* Org-ref */
			break;
		case 3:        /* Cdregion */
			pseudo = sfp->pseudo; /* now also uses new feature pseudo flag */
			excpt = FALSE;
			gbq = sfp->qual;
			while (gbq != NULL) {
				if (StringICmp (gbq->qual, "pseudo") == 0) {
					pseudo = TRUE;
				}
				if (StringICmp (gbq->qual, "exception") == 0) {
					excpt = TRUE;
				}
				gbq = gbq->next;
			}
			if (! pseudo) {
				CdTransCheck(vsp, sfp);
				SpliceCheck(vsp, sfp);
			}
			crp = (CdRegionPtr)(sfp->data.value.ptrvalue);
			if (crp != NULL) {
			for (cbp = crp->code_break; cbp != NULL; cbp = cbp->next)
			{
				i = SeqLocCompare(cbp->loc, sfp->location);
				if ((i != SLC_A_IN_B) && (i != SLC_A_EQ_B))
					ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_Range, "Code-break location not in coding region");
			}
			if (excpt && (! sfp->excpt)) {
				ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_ExceptInconsistent,
				        "Exception flag should be set in coding region");
			}
			if (crp->orf && sfp->product != NULL) {
				ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_OrfCdsHasProduct,
				        "An ORF coding region should not have a product");
			}
			if (pseudo && sfp->product != NULL) {
				ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_PsuedoCdsHasProduct,
				        "A pseudo coding region should not have a product");
			}
			biopgencode = 0;
			cdsgencode = 0;
			bsp = GetBioseqGivenSeqLoc (sfp->location, gcp->entityID);
			if (bsp != NULL) {
				vnp = NULL;
				if (vsp->useSeqMgrIndexes) {
					vnp = SeqMgrGetNextDescriptor (bsp, NULL, Seq_descr_source, &context);
				} else {
					bcp = BioseqContextNew (bsp);
					vnp = BioseqContextGetSeqDescr (bcp, Seq_descr_source, NULL, NULL);
				}
					if (vnp != NULL && vnp->data.ptrvalue != NULL) {
						biop = (BioSourcePtr) vnp->data.ptrvalue;
						orp = biop->org;
						if (orp != NULL && orp->orgname != NULL) {
							onp = orp->orgname;
							if (biop->genome == 4 || biop->genome == 5) {
								biopgencode = onp->mgcode;
							} else {
								biopgencode = onp->gcode;
							}
							gc = crp->genetic_code;
							if (gc != NULL) {
								for (vnp = gc->data.ptrvalue; vnp != NULL; vnp = vnp->next) {
									if (vnp->choice == 2) {
										cdsgencode = (Int2) vnp->data.intvalue;
									}
								}
							}
							if (biopgencode != cdsgencode) {
								ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_GenCodeMismatch,
								        "Genetic code conflict between CDS (code %d) and BioSource (code %d)",
								        (int) cdsgencode, (int) biopgencode);
							}
						}
					}
					if (! vsp->useSeqMgrIndexes) {
						BioseqContextFree (bcp);
					}
			}
			}
			break;
		case 4:        /* Prot-ref */
			prp = (ProtRefPtr) (sfp->data.value.ptrvalue);
			if (prp != NULL) {
			  if (prp->processed != 3 && prp->processed != 4) {
			    vnp = prp->name;
			    if ((vnp == NULL || EmptyOrNullString ((CharPtr) vnp->data.ptrvalue)) &&
			        EmptyOrNullString (prp->desc) &&
					prp->ec == NULL && prp->activity == NULL && prp->db == NULL) {
					ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_ProtRefHasNoData,
					"There is a protein feature where all fields are empty");
			    }
			  }
			for (vnp = prp->db; vnp != NULL; vnp = vnp->next) {
				id = -1;
				db = vnp->data.ptrvalue;
				if (db && db->db) {
					for (i =0; i < DBNUM; i++) {
						if (StringCmp(db->db, dbtag[i]) == 0) {
							id = i;
							break;
						}
					}
					if (id == -1 || (type != SEQFEAT_CDREGION && id < 4)) {
						ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_IllegalDbXref,
							     "Illegal db_xref type %s", db->db);
					}
				}
			}
			}
			break;
		case 5:        /* RNA-ref */
			rrp = (RnaRefPtr)(sfp->data.value.ptrvalue);
			if (rrp->type == 2) { /* mRNA */
				SpliceCheck(vsp, sfp);
			}
			if (rrp->ext.choice == 2)   /* tRNA */
			{
				trp = (tRNAPtr)(rrp->ext.value.ptrvalue);
				if (trp->anticodon != NULL)
				{
					i = SeqLocCompare(trp->anticodon, sfp->location);
					if ((i != SLC_A_IN_B) && (i != SLC_A_EQ_B))
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_Range, "Anticodon location not in tRNA");
				}
			}
			if (rrp->type == 0) {
				ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_RNAtype0, "RNA type 0 (unknown) not supported");
			}
			break;
		case 6:        /* Pub */
			pdp = (PubdescPtr) sfp->data.value.ptrvalue;
			ValidatePubdesc (vsp, pdp);
			break;
		case 7:        /* Seq */
			break;
		case 8:        /* Imp-feat */
			ifp = (ImpFeatPtr) sfp->data.value.ptrvalue;
			if (GetAppProperty ("ValidateExons") != NULL) {
				
				if (ifp != NULL && StringICmp (ifp->key, "exon") == 0) {
					SpliceCheckEx (vsp, sfp, TRUE);
				}
			}
			if (ifp != NULL) {
				ValidateImpFeat (vsp, gcp, sfp, ifp);
			}
			break;
		case 9:        /* Region */
			break;
		case 10:        /* Comment */
			break;
		case 11:        /* Bond */
			break;
		case 12:        /* Site */
			break;
		case 13:        /* Rsite-ref */
			break;
		case 14:        /* User-object */
			break;
		case 15:        /* TxInit */
			break;
		case 16:        /* Numbering */
			break;
		case 17:        /* Secondary Structure */
			break;
		case 18:        /* NonStdRes*/
			break;
		case 19:        /* Heterogen*/
			break;
		case 20:        /* BioSource*/
			break;
		default:
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_InvalidType, "Invalid SeqFeat type [%d]",
				(int)(type));
			break;
	}
	return;
}

/*****************************************************************************
*
*   CdTransCheck(sfp)
*   	Treatment of terminal 'X'
*          If either the protein or the translation end in 'X' (usually
*          due to partial last codon) it is ignored to minimize conflicts
*          between approaches to add the X or not in this case.
*
*****************************************************************************/
static CharPtr MapToNTCoords (SeqFeatPtr sfp, SeqIdPtr protID, Int4 pos)

{
  SeqLocPtr  nslp;
  SeqLocPtr  pslp;
  CharPtr    rsult;
  SeqPntPtr  spntp;

  rsult = NULL;
  if (sfp != NULL && protID != NULL && pos >= 0) {
    spntp = SeqPntNew ();
    pslp = ValNodeNew (NULL);
    pslp->choice = SEQLOC_PNT;
    pslp->data.ptrvalue = (Pointer) spntp;
    spntp->point = pos;
    spntp->id = SeqIdDup (protID);
    nslp = aaLoc_to_dnaLoc (sfp, pslp);
    if (nslp != NULL) {
      rsult = SeqLocPrint (nslp);
    }
    SeqLocFree (pslp);
    SeqLocFree (nslp);
  }
  return rsult;
}

NLM_EXTERN void CdTransCheck(ValidStructPtr vsp, SeqFeatPtr sfp)
{
	ByteStorePtr newprot = NULL;
	BioseqPtr prot1seq=NULL, prot2seq=NULL;
	SeqLocPtr slp=NULL, curr = NULL;
	Int4 prot1len = 0, prot2len, i, len;
	CdRegionPtr crp;
	SeqIdPtr protid=NULL;
	Int2 residue1, residue2, stop_count = 0, mismatch = 0, ragged = 0;
	Boolean got_stop = FALSE, test_it = TRUE;
	SeqPortPtr spp=NULL;
	Uint2 part_loc=0, part_prod=0;
	Boolean no_end = FALSE, no_beg = FALSE, show_stop = FALSE,
		got_dash = FALSE, done;
	GBQualPtr gb;
	ValNodePtr vnp, code;
	int gccode = 0;
	Boolean transl_except = FALSE, prot_ok = TRUE;
	CharPtr nuclocstr;
	CodeBreakPtr cbp;
	Int4 pos1, pos2, pos;
	SeqLocPtr tmp;

	if (sfp == NULL) return;

	if (sfp->excpt)		 /* biological exception */
		return;

	for (gb = sfp->qual; gb != NULL; gb = gb->next)	  /* pseuogene */
	{
		if (! StringICmp("pseudo", gb->qual))
			return;
	}

	crp = (CdRegionPtr)(sfp->data.value.ptrvalue);
	if (crp->code_break == NULL)  /* check for unparsed transl_except */
	{
		for (gb = sfp->qual; gb != NULL; gb = gb->next)
		{
			if (! StringCmp(gb->qual, "transl_except"))
			{
				transl_except = TRUE;
				break;
			}
		}
	}

	if (crp->genetic_code != NULL)
	{
		for (vnp = crp->genetic_code->data.ptrvalue; ((vnp != NULL) && (! gccode)); vnp = vnp->next)
		{
			switch (vnp->choice)
			{
				case 0:
				    break;
				case 1:     /* name */
					code = GeneticCodeFind(0, (CharPtr)(vnp->data.ptrvalue));
					if (code != NULL)
					{
						for (vnp = code->data.ptrvalue; ((vnp != NULL) && (! gccode)); vnp = vnp->next)
						{
							if (vnp->choice == 2) /* id */
								gccode = (int)(vnp->data.intvalue);
						}
					}
					break;
				case 2:    /* id */
				    gccode = (int)(vnp->data.intvalue);
					break;
				default:
				    gccode = 255;
					break;
			}
		}
	}
					
	newprot = ProteinFromCdRegion(sfp, TRUE);   /* include stop codons */
	if (newprot == NULL)
	{
		ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_CdTransFail, "Unable to translate");
		prot_ok = FALSE;
		goto erret;
	}

	part_loc = SeqLocPartialCheck(sfp->location);
	part_prod = SeqLocPartialCheck(sfp->product);
	if ((part_loc & SLP_STOP) || (part_prod & SLP_STOP))
		no_end = TRUE;
	else    /* complete stop, so check for ragged end */
	{
		len = SeqLocLen(sfp->location);
		if (crp->frame > 1)
			len -= (Int4)(crp->frame - 1);
		ragged = (Int2)(len % (Int4)(3));
		if (ragged) {
		len = SeqLocLen(sfp->location);
		cbp = crp->code_break;
		while (cbp != NULL)
		{
			pos1 = INT4_MAX;
			pos2 = -10;
			tmp = NULL;
			while ((tmp = SeqLocFindNext(cbp->loc, tmp)) != NULL)
			{
				pos = GetOffsetInLoc(tmp, sfp->location, 
SEQLOC_START);
				if (pos < pos1)
					pos1 = pos;
				pos = GetOffsetInLoc(tmp, sfp->location, 
SEQLOC_STOP);
				if (pos > pos2)
					pos2 = pos;
			}
			pos = pos2 - pos1; /* codon length */
			if (pos >= 0 && pos <= 1 && pos2 == len - 1)   /*  a codon */
			/* allowing a partial codon at the end */
			{
				ragged = 0;
			}

			cbp = cbp->next;
		}
		}
	}
	if (crp->frame > 1) {
		if (! (part_loc & SLP_START)) {
			ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_PartialProblem, "Suspicious CDS location - frame > 1 but not 5' partial");
		} else if ((part_loc & SLP_NOSTART) && (! PartialAtSpliceSite (sfp->location, SLP_NOSTART))) {
			ValidErr(vsp, SEV_INFO, ERR_SEQ_FEAT_PartialProblem, "Suspicious CDS location - frame > 1 and not at consensus splice site");
		}
	}

	if ((part_loc & SLP_START) || (part_prod & SLP_START))
		no_beg = TRUE;

	prot2len = BSLen(newprot);
	len = prot2len;
	BSSeek(newprot, 0, SEEK_SET);
	for (i =0 ; i < len; i++)
	{
		residue1 = BSGetByte(newprot);
		if ((i == 0) && (residue1 == '-'))
			got_dash = TRUE;
		if (residue1 == '*')
		{
			if (i == (len - 1))
				got_stop = TRUE;
			else
				stop_count++;
		}
	}

	if (stop_count)
	{
		if (got_dash)
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_StartCodon,
				"Illegal start codon and %ld internal stops. Probably wrong genetic code [%d]",
				(long)stop_count, gccode);
		else
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_InternalStop, "%ld internal stops. Genetic code [%d]",
				(long)stop_count, gccode);
		prot_ok = FALSE;
		if (stop_count > 5)
			goto erret;
	} else if (got_dash) {
		ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_StartCodon,
			"Illegal start codon used. Wrong genetic code [%d] or protein should be partial", gccode);
	}

	show_stop = TRUE;

	protid = SeqLocId(sfp->product);
	if (protid != NULL)
	{
		prot1seq = BioseqFind(protid);
		if (prot1seq != NULL)
			prot1len = prot1seq->length;
	}

	if (prot1seq == NULL)
	{
		if (prot2len > 6)
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_NoProtein, "No protein Bioseq given");
		goto erret;
	}

	len = prot2len;

	if ((got_stop)&&(len == (prot1len + 1)))  /* ok, got stop */
	{
		len--;
	}

	spp = SeqPortNew(prot1seq, 0, -1, 0, Seq_code_ncbieaa);
	if (spp == NULL) goto erret;

	    /* ignore terminal 'X' from partial last codon if present */

	done = FALSE;
	while ((! done) && (prot1len))
	{
		SeqPortSeek(spp, (prot1len - 1), SEEK_SET);
		residue1 = SeqPortGetResidue(spp);
		if (residue1 == 'X')   /* remove terminal X */
			prot1len--;
		else
			done = TRUE;
	}
	done = FALSE;
	while ((! done) && (len))
	{
		BSSeek(newprot, (len-1), SEEK_SET);
		residue2 = BSGetByte(newprot);
		if (residue2 == 'X')
			len--;
		else
			done = TRUE;
	}

	if (len == prot1len)   /* could be identical */
	{
		SeqPortSeek(spp, 0, SEEK_SET);
	   	BSSeek(newprot, 0, SEEK_SET);
		for (i = 0; i < len; i++)
		{
			residue1 = BSGetByte(newprot);
			residue2 = SeqPortGetResidue(spp);
			if (residue1 != residue2)
			{
				prot_ok = FALSE;
				if (residue2 == INVALID_RESIDUE)
					residue2 = '?';
				if (mismatch == 10)
				{
					ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_MisMatchAA, "More than 10 mismatches. Genetic code [%d]", gccode);
					break;
				}
				else if (i == 0)
				{
					if ((sfp->partial) && (! no_beg) && (! no_end))  /* ok, it's partial */
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_PartialProblem, "Start of location should probably be partial");
					else if (residue1 == '-')
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_StartCodon, "Illegal start codon used. Wrong genetic code [%d] or protein should be partial", gccode);
					else
					{
						nuclocstr = MapToNTCoords (sfp, protid, i);
						if (nuclocstr != NULL) {
							ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_MisMatchAA,
							"Residue %ld in protein [%c] != translation [%c] at %s",
								(long)(i+1), (char)residue2, (char)residue1, nuclocstr);
						} else {
							ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_MisMatchAA,
							"Residue %ld in protein [%c] != translation [%c]",
								(long)(i+1), (char)residue2, (char)residue1);
						}
						MemFree (nuclocstr);
					}
				}
				else
				{
					nuclocstr = MapToNTCoords (sfp, protid, i);
					if (nuclocstr != NULL) {
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_MisMatchAA,
							"Residue %ld in protein [%c] != translation [%c] at %s",
							(long)(i+1), (char)residue2, (char)residue1, nuclocstr);
					} else {
						ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_MisMatchAA,
							"Residue %ld in protein [%c] != translation [%c]",
							(long)(i+1), (char)residue2, (char)residue1);
					}
					MemFree (nuclocstr);
				}
				mismatch++;
			}
		}
		spp = SeqPortFree(spp);
	}
	else
	{
		ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_TransLen, "Given protein length [%ld] does not match translation length [%ld]",
			prot1len, len);
	}

	if ((sfp->partial) && (! mismatch))
	{
		if ((! no_beg) && (! no_end))   /* just didn't label */
		{
			if (! got_stop)
			{
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_PartialProblem, "End of location should probably be partial");
			}
			else
			{
				ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_PartialProblem, "This SeqFeat should not be partial");
			}
			show_stop = FALSE;
		}
	}
		

erret:
	if (show_stop)
	{
		if ((! got_stop) && (! no_end))
		{
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_NoStop, "Missing stop codon");
		}
		else if ((got_stop) && (no_end))
		{
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_PartialProblem, "Got stop codon, but 3'end is labeled partial");
		}
		else if ((got_stop) && (! no_end) && (ragged))
		{
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_TransLen, "Coding region extends %d base(s) past stop codon", (int)ragged);
		}
	}

	if (! prot_ok)
	{
		if (transl_except)
			ValidErr(vsp,SEV_WARNING, ERR_SEQ_FEAT_TranslExcept, "Unparsed transl_except qual. Skipped");
	}

	if (prot2seq != NULL)
		BioseqFree(prot2seq);
	else
		BSFree(newprot);
	SeqPortFree(spp);
	return;
}
/*****************************************************************************
*
*   SpliceCheck(sfp)
*      checks for GT/AG rule at splice junctions
*
*****************************************************************************/
#define NOVALUE 0
#define HADGT 1
#define NOGT 2

static void SpliceCheckEx(ValidStructPtr vsp, SeqFeatPtr sfp, Boolean checkAll)
{
	SeqLocPtr slp, nxt, head;
	Uint1 strand = Seq_strand_unknown;
	SeqPortPtr spp=NULL;
	SeqIdPtr last_sip=NULL, sip;
	Int2 total, ctr;
	BioseqPtr bsp = NULL;
	Int4 strt, stp, len = 0, donor, acceptor;
	Int2 residue1, residue2;
	Char tbuf[40];
	Boolean reportAsError, first, last, firstPartial, lastPartial;
	int severity;
	Uint2 partialflag;

	if (sfp == NULL) return;

	if (sfp->excpt)		 /* biological exception */
		return;

	head = sfp->location;
	if (head == NULL) return;

	reportAsError = FALSE;
	if (GetAppProperty ("SpliceValidateAsError") != NULL) {
		reportAsError = TRUE;
	}

	slp = NULL;
	total = 0;
	while ((slp = SeqLocFindPart(head, slp, EQUIV_IS_ONE)) != NULL)
	{
		total++;
		if (slp->choice == SEQLOC_EQUIV)
			return;  /* bail on this one */
		if (total == 1)
			strand = SeqLocStrand(slp);
		else
		{
			if (strand != SeqLocStrand(slp))	 /* bail on mixed strand */
				return;
		}
	}

	if ((! checkAll) && total < 2) return;
	if (total < 1) return;

	slp = NULL;
	ctr = 0;

	first = TRUE;
	last = FALSE;
	firstPartial = FALSE;
	lastPartial = FALSE;

	slp = SeqLocFindPart(head, slp, EQUIV_IS_ONE);
	while (slp != NULL)
	{
		nxt = SeqLocFindPart(head, slp, EQUIV_IS_ONE);
		last = (Boolean) (nxt == NULL);
		partialflag = SeqLocPartialCheck (slp);
		firstPartial = (Boolean) (first && (partialflag & SLP_START));
		lastPartial = (Boolean) (last && (partialflag & SLP_STOP));
		ctr++;
		sip = SeqLocId(slp);
		if (sip == NULL) break;
		if ((ctr == 1) || (! SeqIdMatch(sip, last_sip)))
		{
			spp = SeqPortFree(spp);
			bsp = BioseqLockById(sip);
			if (bsp == NULL) break;
			len = bsp->length;
			spp = SeqPortNew(bsp, 0, -1, strand, Seq_code_ncbi4na);
			BioseqUnlock(bsp);
			if (spp == NULL) break;
			last_sip = sip;
		}
		acceptor = SeqLocStart(slp);
		donor = SeqLocStop(slp);

		if (strand != Seq_strand_minus)
		{
			strt = acceptor;
			stp = donor;
		}
		else
		{
			strt = donor;
			donor = acceptor;
			acceptor = strt;
			stp = len - donor - 1;	/* orient to reverse complement seqport */
			strt = len - acceptor - 1;
		}

		if (((checkAll && (! lastPartial)) || ctr < total) && (stp < (len - 2)))   /* check donor on all but last exon and on sequence */
		{
			SeqPortSeek(spp, (stp+1), SEEK_SET);
			residue1 = SeqPortGetResidue(spp);
			residue2 = SeqPortGetResidue(spp);
			if (IS_residue(residue1) && IS_residue(residue2))
			{
				if ((! (residue1 & 4)) ||        /* not G or */
					(! (residue2 & 8)))          /* not T */
				{
				if ((residue1 & 4) && (residue2 & 2)) { /* GC minor splice site */
					tbuf[39] = '\0';
					BioseqLabel(bsp, tbuf, 39, OM_LABEL_CONTENT);
					ValidErr(vsp, SEV_WARNING, ERR_SEQ_FEAT_NotSpliceConsensus,
						"Rare splice donor consensus (GC) found instead of (GT) after exon ending at position %ld of %s",
				    	  (long)(donor+1), tbuf);
				} else {
					if (checkAll) {
					  severity = SEV_WARNING;
					} else if (reportAsError) {
					  severity = SEV_ERROR;
					} else {
					  severity = SEV_WARNING;
					}
					tbuf[39] = '\0';
					BioseqLabel(bsp, tbuf, 39, OM_LABEL_CONTENT);
					ValidErr(vsp, severity, ERR_SEQ_FEAT_NotSpliceConsensus,
						"Splice donor consensus (GT) not found after exon ending at position %ld of %s",
				     	 (long)(donor+1), tbuf);
				}
				}
			}
		}

		if (((checkAll && (! firstPartial))  || ctr != 1) && (strt > 1))
		{
			SeqPortSeek(spp, (strt - 2), SEEK_SET);
			residue1 = SeqPortGetResidue(spp);
			residue2 = SeqPortGetResidue(spp);
			if (IS_residue(residue1) && IS_residue(residue2))
			{
				if ((! (residue1 & 1)) ||        /* not A or */
					(! (residue2 & 4)))          /* not G */
				{
					if (checkAll) {
					  severity = SEV_WARNING;
					} else if (reportAsError) {
					  severity = SEV_ERROR;
					} else {
					  severity = SEV_WARNING;
					}
					tbuf[39] = '\0';
					BioseqLabel(bsp, tbuf, 39, OM_LABEL_CONTENT);
					ValidErr(vsp, severity, ERR_SEQ_FEAT_NotSpliceConsensus,
					"Splice acceptor consensus (AG) not found before exon starting at position %ld of %s",
				      (long)(acceptor+1), tbuf);
				}
			}
		}
		first = FALSE;
		slp = nxt;
	}

	SeqPortFree(spp);
    return;
}

NLM_EXTERN void SpliceCheck(ValidStructPtr vsp, SeqFeatPtr sfp)

{
	SpliceCheckEx (vsp, sfp, FALSE);
}

/*****************************************************************************
*
*   ValidateSeqLoc(vsp, slp, prefix)
*
*****************************************************************************/
NLM_EXTERN void ValidateSeqLoc(ValidStructPtr vsp, SeqLocPtr slp, CharPtr prefix)
{
	SeqLocPtr tmp, prev;
	Boolean retval = TRUE, tmpval, mixed_strand = FALSE, ordered=TRUE;
	CharPtr ctmp;
	Uint1 strand2, strand1;
	SeqIntPtr sip1, sip2;
	SeqPntPtr spp;
	PackSeqPntPtr pspp;
	SeqIdPtr id1 = NULL, id2;

	if (slp == NULL) return;

	tmp = NULL;
	prev = NULL;
	sip1 = NULL;
	strand1 = Seq_strand_other;
	while ((tmp = SeqLocFindNext(slp, tmp)) != NULL)
	{
		tmpval = TRUE;
		switch (tmp->choice)
		{
			case SEQLOC_INT:
				sip2 = (SeqIntPtr)(tmp->data.ptrvalue);
				strand2 = sip2->strand;
				id2 = sip2->id;
				tmpval = SeqIntCheck (sip2);
				if ((tmpval) && (sip1 != NULL) && (ordered))
				{
					if (SeqIdForSameBioseq(sip1->id, sip2->id))
					{
						if (strand2 == Seq_strand_minus)
						{
							if (sip1->to < sip2->to)
								ordered = FALSE;
						}
						else if (sip1->to > sip2->to)
							ordered = FALSE;
					}
				}
				break;
			case SEQLOC_PNT:
				spp = (SeqPntPtr)(tmp->data.ptrvalue);
				strand2 = spp->strand;
				id2 = spp->id;
				tmpval = SeqPntCheck (spp);
				break;
			case SEQLOC_PACKED_PNT:
				pspp = (PackSeqPntPtr)(tmp->data.ptrvalue);
				strand2 = pspp->strand;
				id2 = pspp->id;
				tmpval = PackSeqPntCheck (pspp);
				break;
			default:
			    strand2 = Seq_strand_other;
				id2 = NULL;
				break;
		}
		if (! tmpval)
		{
			retval = FALSE;
			ctmp = SeqLocPrint(tmp);
			ValidErr(vsp, SEV_FATAL, ERR_SEQ_FEAT_Range, "%s: SeqLoc [%s] out of range", prefix, ctmp);
			MemFree(ctmp);

		}

		if ((strand1 != Seq_strand_other) && (strand2 != Seq_strand_other))
		{
			if (SeqIdForSameBioseq(id1, id2))
			{
				if (strand1 != strand2)
					mixed_strand = TRUE;
			}
		}

		strand1 = strand2;
		id1 = id2;
	}

	if ((mixed_strand) || (! ordered))
	{
		ctmp = SeqLocPrint(slp);
		if (mixed_strand)
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_MixedStrand, "%s: Mixed strands in SeqLoc [%s]", prefix, ctmp);
		if (! ordered)
			ValidErr(vsp, SEV_ERROR, ERR_SEQ_FEAT_SeqLocOrder, "%s: Intervals out of order in SeqLoc [%s]", prefix, ctmp);
		MemFree(ctmp);
	}

	return;
}

/*****************************************************************************
*
*   PatchBadSequence(bsp)
*
*****************************************************************************/
NLM_EXTERN Boolean PatchBadSequence(BioseqPtr bsp)
{
	ByteStorePtr newseq;
	SeqPortPtr spp;
	Boolean is_na;
	Uint1 seqcode;
	Int2 repchar, residue;
	Int4 i, len;

	if (bsp == NULL) return FALSE;
	if (! ((bsp->repr == Seq_repr_raw) || (bsp->repr == Seq_repr_const)))
		return FALSE;

	is_na = ISA_na(bsp->mol);
	if (is_na)
	{
		seqcode = Seq_code_iupacna;
		repchar = (Int2)'N';   /* N */
	}
	else
	{
		seqcode = Seq_code_iupacaa;
		repchar = (Int2)'X';
	}

	spp = SeqPortNew(bsp, 0, -1, 0, seqcode);
	if (spp == NULL) return FALSE;

	len = bsp->length;
	newseq = BSNew(len);
	if (newseq == NULL)
	{
		SeqPortFree(spp);
		return FALSE;
	}

	for (i = 0; i < len; i++)
	{
		residue = SeqPortGetResidue(spp);
		if (residue == INVALID_RESIDUE)
		{
			residue = repchar;
		}
		BSPutByte(newseq, residue);
	}

	SeqPortFree(spp);
	BSFree(bsp->seq_data);
	bsp->seq_data = newseq;
	bsp->seq_data_type = seqcode;

	BioseqRawPack(bsp);

	return TRUE;
}

static void FindABioseq(SeqEntryPtr sep, Pointer data, Int4 index, Int2 indent)
{
	BioseqPtr PNTR bp;
	BioseqPtr bsp;

	bp = (BioseqPtr PNTR)data;
	if (*bp != NULL)   /* already got one */
		return;

	if (IS_Bioseq(sep))
	{
		bsp = (BioseqPtr)(sep->data.ptrvalue);
		*bp = bsp;
	}
	return;
}

NLM_EXTERN CharPtr FindIDForEntry (SeqEntryPtr sep, CharPtr buf)
{
	BioseqPtr bsp = NULL;
	
	if ((sep == NULL) || (buf == NULL))
		return NULL;

	*buf = '\0';
	SeqEntryExplore (sep, (Pointer)(&bsp), FindABioseq);

	if (bsp == NULL) return NULL;

	SeqIdPrint(bsp->id, buf, PRINTID_FASTA_LONG);
	return buf;
}

static CharPtr TrimSpacesOnEitherSide (CharPtr str)

{
  Uchar    ch;
  CharPtr  dst;
  CharPtr  ptr;

  if (str != NULL && str [0] != '\0') {
    dst = str;
    ptr = str;
    ch = *ptr;
    while (ch != '\0' && ch <= ' ') {
      ptr++;
      ch = *ptr;
    }
    while (ch != '\0') {
      *dst = ch;
      dst++;
      ptr++;
      ch = *ptr;
    }
    *dst = '\0';
    dst = NULL;
    ptr = str;
    ch = *ptr;
    while (ch != '\0') {
      if (ch != ' ') {
        dst = NULL;
      } else if (dst == NULL) {
        dst = ptr;
      }
      ptr++;
      ch = *ptr;
    }
    if (dst != NULL) {
      *dst = '\0';
    }
  }
  return str;
}

static void CopyLetters (CharPtr dest, CharPtr source, size_t maxsize)

{
  Char     ch;
  CharPtr  tmp;

  if (dest == NULL || maxsize < 1) return;
  *dest = '\0';
  if (source == NULL) return;
  maxsize--;
  tmp = dest;
  ch = *source;
  while (maxsize > 1 && ch != '\0') {
    if (ch != '.') {
      *dest = ch;
      dest++;
      maxsize--;
    }
    source++;
    ch = *source;
  }
  *dest = '\0';
  TrimSpacesOnEitherSide (tmp);
}

static void LookForEtAl (ValidStructPtr vsp, ValNodePtr tmp)

{
  AuthorPtr    ap;
  AuthListPtr  authors = NULL;
  CitArtPtr    cap;
  CitBookPtr   cbp;
  CitGenPtr    cgp;
  CitSubPtr    csp;
  Char         first [64];
  Char         initials [16];
  Char         last [64];
  ValNodePtr   names;
  NameStdPtr   nsp;
  PersonIdPtr  pid;

  if (vsp == NULL || tmp == NULL) return;
  switch (tmp->choice) {
		case PUB_Article:
			cap = (CitArtPtr)(tmp->data.ptrvalue);
			authors = cap->authors;
			break;
		case PUB_Man:
		case PUB_Book:
		case PUB_Proc:
			cbp = (CitBookPtr)(tmp->data.ptrvalue);
			authors = cbp->authors;
			break;
		case PUB_Gen:
			cgp = (CitGenPtr)(tmp->data.ptrvalue);
			authors = cgp->authors;
			break;
		case PUB_Sub:
			csp = (CitSubPtr)(tmp->data.ptrvalue);
			authors = csp->authors;
			break;
		default:
			break;
  }
  if (authors == NULL || authors->choice != 1) return;
  for (names = authors->names; names != NULL; names = names->next) {
    ap = names->data.ptrvalue;
    if (ap != NULL) {
      pid = ap->name;
      if (pid != NULL && pid->choice == 2) {
        nsp = pid->data;
        if (nsp != NULL && nsp->names [0] != NULL) {
          CopyLetters (last, nsp->names [0], sizeof (last));
          CopyLetters (first, nsp->names [1], sizeof (first));
          CopyLetters (initials, nsp->names [4], sizeof (initials));
          if ((StringICmp (last, "et al") == 0) ||
              (StringCmp (initials, "al") == 0 &&
               StringCmp (last, "et") == 0 &&
               first [0] == '\0')) {
            if (names->next == NULL) {
              ValidErr (vsp, SEV_WARNING, ERR_GENERIC_AuthorListHasEtAl,
                        "Author list ends in et al.");
            } else {
              ValidErr (vsp, SEV_WARNING, ERR_GENERIC_AuthorListHasEtAl,
                        "Author list contains et al.");
            }
          }
        }
      }
    }
  }
}

static void SpellCheckPub(ValidStructPtr vsp, ValNodePtr tmp)
{
	CitArtPtr cap;
	CitBookPtr cbp;
	CitGenPtr cgp;
	ValNodePtr titles = NULL;

	if ((vsp == NULL) || (tmp == NULL))
		return;

	switch (tmp->choice)
	{
		case PUB_Article:
			cap = (CitArtPtr)(tmp->data.ptrvalue);
			titles = cap->title;
			break;
		case PUB_Man:
		case PUB_Book:
		case PUB_Proc:
			cbp = (CitBookPtr)(tmp->data.ptrvalue);
			titles = cbp->title;
			break;
		case PUB_Gen:
			cgp = (CitGenPtr)(tmp->data.ptrvalue);
			if (cgp->cit != NULL)
				SpellCheckString(vsp, cgp->cit);
			if (cgp->title != NULL)
				SpellCheckString(vsp, cgp->title);
			break;
		default:
			break;
	}

	if (titles != NULL)
	{
		for (; titles != NULL; titles = titles->next)
		{
			if (titles->choice == Cit_title_name)
				SpellCheckString(vsp, (CharPtr)(titles->data.ptrvalue));
		}
	}

	return;
}

static void SpellCheckSeqDescr(GatherContextPtr gcp)

{
	PubdescPtr pdp;
	ValNodePtr tmp, vnp;
	ValidStructPtr vsp;

	vsp = (ValidStructPtr)(gcp->userdata);
	if (vsp == NULL)
		return;

	vnp = (ValNodePtr)(gcp->thisitem);
	if (vnp == NULL)
		return;

	vsp->descr = vnp;
	vsp->sfp = NULL;

	if (vnp->choice == Seq_descr_pub) {
		pdp = (PubdescPtr)(vnp->data.ptrvalue);
		for (tmp = pdp->pub; tmp != NULL; tmp = tmp->next) {
			LookForEtAl (vsp, tmp);
		}
	}

	if (vsp->spellfunc == NULL) return;

	switch (vnp->choice)
	{
		case Seq_descr_title:
		case Seq_descr_region:
		case Seq_descr_comment:
			SpellCheckString(vsp, (CharPtr)(vnp->data.ptrvalue));
			break;
		case Seq_descr_pub:
			pdp = (PubdescPtr)(vnp->data.ptrvalue);
			for (tmp = pdp->pub; tmp != NULL; tmp = tmp->next)
			{
				SpellCheckPub(vsp, tmp);
			}
			break;
		default:
			break;
	}
	return;
}

NLM_EXTERN void SpellCheckSeqFeat(GatherContextPtr gcp)
{
	PubdescPtr pdp;
	SeqFeatPtr sfp;
	ProtRefPtr prp;
	ValidStructPtr vsp;
	ValNodePtr vnp;

	vsp = (ValidStructPtr)(gcp->userdata);
	if (vsp == NULL)
		return;

	sfp = (SeqFeatPtr)(gcp->thisitem);
	if (sfp == NULL)
		return;

	vsp->descr = NULL;
	vsp->sfp = sfp;

	if (sfp->data.choice == SEQFEAT_PUB) {
		pdp = (PubdescPtr)(sfp->data.value.ptrvalue);
		for (vnp = pdp->pub; vnp != NULL; vnp = vnp->next) {
			LookForEtAl (vsp, vnp);
		}
	}

	if (vsp->spellfunc == NULL) return;

	SpellCheckString(vsp, sfp->comment);

	switch (sfp->data.choice)
	{
		case 1:        /* Gene-ref */
			break;
		case 2:        /* Org-ref */
			break;
		case 3:        /* Cdregion */
			break;
		case 4:        /* Prot-ref */
			prp = (ProtRefPtr)(sfp->data.value.ptrvalue);
			for (vnp = prp->name; vnp != NULL; vnp = vnp->next)
				SpellCheckString(vsp, (CharPtr)(vnp->data.ptrvalue));
			SpellCheckString(vsp, prp->desc);
			break;
		case 5:        /* RNA-ref */
			break;
		case 6:        /* Pub */
				pdp = (PubdescPtr)(sfp->data.value.ptrvalue);
				for (vnp = pdp->pub; vnp != NULL; vnp = vnp->next)
				{
					SpellCheckPub(vsp, vnp);
				}
			break;
		case 7:        /* Seq */
			break;
		case 8:        /* Imp-feat */
			break;
		case 9:        /* Region */
			SpellCheckString(vsp, (CharPtr)(sfp->data.value.ptrvalue));
			break;
		case 10:        /* Comment */
			break;
		case 11:        /* Bond */
			break;
		case 12:        /* Site */
			break;
		case 13:        /* Rsite-ref */
			break;
		case 14:        /* User-object */
			break;
		case 15:        /* TxInit */
			break;
		case 16:        /* Numbering */
			break;
		case 17:        /* Secondary Structure */
			break;
		case 18:        /* NonStdRes*/
			break;
		case 19:        /* Heterogen*/
			break;
		case 20:        /* BioSource*/
			break;
		default:
			break;
	}

	return;
}

NLM_EXTERN void SpellCheckString (ValidStructPtr vsp, CharPtr str)
{
	if ((vsp == NULL) || (str == NULL))
		return;

	if (vsp->spellfunc == NULL) return;

	(* (vsp->spellfunc))((char *)str, (vsp->spellcallback));

	return;
}

NLM_EXTERN void SpellCallBack (char * str)
{
	ErrSev sev;

	sev = SEV_ERROR;
	if (globalvsp != NULL && globalvsp->justwarnonspell) {
		sev = SEV_WARNING;
	}
	ValidErr(globalvsp, sev, ERR_GENERIC_Spell, "[ %s ]", (CharPtr)str);
	return;
}