summaryrefslogtreecommitdiff
path: root/network/taxon1/taxon2/tc2proc.c
blob: 91632731fd1cdcd83b57597ccc29d12ee1b1b6b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
/*----------------*/
/* $Id: tc2proc.c,v 1.33 2003/07/29 20:06:05 soussov Exp $           */
/*----------------*/

#include <stdlib.h>
#include <ncbi.h>
#include <taxinc.h>
#include <txclient.h>
#define REALTAXsyb

#define MAX_ORG_LIST	10

#define BUFF_SIZE 16
#define TAX_READ 0
#define TAX_WRITE 1

#define ORGMOD_gb_acronym 32
#define ORGMOD_gb_anamorph 33
#define ORGMOD_gb_synonym 34
#define ORGMOD_anamorph 29
#define ORGMOD_synonym 28


static TreePtr tax_tree= NULL;

static Int2 VRL_div= 0;
static Int2 PHG_div= 0;

static Int2 SpeciesRank= 26;
static Int2 SubspeciesRank= 27;
static Int2 GenusRank= 22;
static Int2 FamilyRank= 0;
static Int2 OrderRank= 0;
static Int2 ClassRank= 0;
static Int2 SYNONYM= 0;
static Int2 COMMON_NAME= 0;
static Int2 GB_COMMON= 0;
static Int2 GB_ACRONYM= 0;
static Int2 GB_SYNONYM= 0;
static Int2 GB_ANAMORPH= 0;
static Int2 ANAMORPH= 0;

static int my_timer= 0;

typedef struct t_nameList {
    struct t_nameList* next;
    char* name;
} NameList, *NameListPtr;

static struct t_or_buff {
    Int4 tax_id;
    OrgRefPtr p_org_ref;
    int timer;
    int is_uncultured;
    int is_species;
    int has_modif;
    NameListPtr blast_name;
} or_buff[BUFF_SIZE];

static CharPtr DB_PATH= "TAX_OS";

static Boolean we_want_synonyms= 0;

static OrgRefPtr getFromBuff(Int4 id, int* is_sp, int* is_uncult, NameListPtr* bnl);
static void loadInBuff(Int4 id);
static void bldOrgRefOut(OrgRefPtr dst, OrgRefPtr src, Int4 tax_id);
static int nof_tokens(char* s);

Boolean tax1_setSynonyms(Boolean on_off)
{
    Boolean ret;
    
    ret= we_want_synonyms;
    we_want_synonyms= on_off;
    return ret;
}

  
static Boolean tc2_toNode(TreeCursorPtr cursor, Int4 tax_id)
{
    if(!tax_ptree_toTaxId(cursor, tax_id, FALSE)) {
        /* this node is not in our tree */
        return (tax_ptree_addNode(tax_tree, tax_id)) ? 
            tax_ptree_toTaxId(cursor, tax_id, FALSE) : FALSE;
    }
    return TRUE;
}

static void lockBuff(int mode)
{
    mode= mode;
}

static void unlockBuff(void)
{
}

static void initBuff(void)
{
    int i;

    my_timer= 0;
  
    for(i= 0; i < BUFF_SIZE; i++) {
        or_buff[i].tax_id= 0;
        or_buff[i].p_org_ref= NULL;
        or_buff[i].blast_name= NULL;
    }
}

static Int4 getLiveId(Int4 id)
{
    TreeCursorPtr cursor= tree_openCursor(tax_tree, NULL, NULL);
    Uint2 s;
    TXC_TreeNodePtr tnp;
    
    if((cursor == NULL) || (!tc2_toNode(cursor, id))) return 0;

    tnp= tree_getNodeData(cursor, &s);
    tree_closeCursor(cursor);
    return (tnp != NULL)? tnp->tax_id : 0;
}


/**************************************************************************
 *
 *	InitTaxDB
 *
 **************************************************************************/

int InitTaxDB(void)
{
    CharPtr tmp;

    if((tmp=getenv("TAXDBPATH")) != NULL) DB_PATH= tmp;

    if(!txc_connect2Server(DB_PATH, "soussov", "vladimir", "tax2cl")) {
        
        return 0;
    }
    
    if((!txc_loadNameClasses()) || (!txc_loadRanks()) || 
       (!txc_loadDivisions()) || (!txc_loadGCs())) {
        
        return 0;
    }
    

    SpeciesRank=    tax_getRankId("species");
    SubspeciesRank= tax_getRankId("subspecies");
    GenusRank=      tax_getRankId("genus");
    FamilyRank=     tax_getRankId("family");
    OrderRank=      tax_getRankId("order");
    ClassRank=      tax_getRankId("class");


    VRL_div= tax_getDivisionId("VRL", NULL);
    PHG_div= tax_getDivisionId("PHG", NULL);

    SYNONYM=     tax_getClass_cde("synonym");
    COMMON_NAME= tax_getClass_cde("common name");
    GB_COMMON= tax_getClass_cde("genbank common name");
    GB_ACRONYM= tax_getClass_cde("genbank acronym");
    GB_SYNONYM= tax_getClass_cde("genbank synonym");
    GB_ANAMORPH= tax_getClass_cde("genbank anamorph");
    ANAMORPH= tax_getClass_cde("anamorph");

    initBuff();
    tax_tree= tax_ptree_new();
  
    return (tax_tree == NULL)? 0 : 1;
}

/**************************************************************************
 *
 *	CloseTaxDB
 *
 **************************************************************************/

static void free_blast_name(NameListPtr   blast_name)
{
    if(blast_name != NULL) {
        NameListPtr t;

        do {
            t= blast_name->next;
            MemFree(blast_name);
            blast_name= t;
        }
        while(blast_name);
    }   
}

int CloseTaxDB(void)
{
    int i;

    if(tax_tree != NULL) {
        tree_delete(tax_tree);
        
        txc_close();
        
        for(i= 0; i < BUFF_SIZE; i++) {
            if(or_buff[i].p_org_ref != NULL) {
                OrgRefFree(or_buff[i].p_org_ref);
                if(or_buff[i].blast_name != NULL) {
                    free_blast_name(or_buff[i].blast_name);
                }
            }
        }
    }
    tax_tree= NULL;
    return 1;
}

Int4 tax1_getParent(Int4 id_tax)
{
    TreeCursorPtr cursor;
    Int4 ret_id= -1;
    
    if(tax_tree == NULL) return -1;
    
    if(id_tax == 1) return 0;
    
    cursor= tree_openCursor(tax_tree, NULL, NULL);
    if(cursor == NULL) return -1;
    
    if(tc2_toNode(cursor, id_tax)) {
        TXC_TreeNodePtr tnp;
        Uint2 s;
        
        tree_parent(cursor);
        tnp= tree_getNodeData(cursor, &s);
        if(tnp != NULL) ret_id= tnp->tax_id;
    }
    
    tree_closeCursor(cursor);
    return ret_id;
}

Int4 tax1_getGenus(Int4 id_tax)
{
    TreeCursorPtr cursor;
    Int4 ret_id= -1;
    
    if(tax_tree == NULL) return -1;
    
    if(id_tax == 1) return 0;
    
    cursor= tree_openCursor(tax_tree, NULL, NULL);
    if(cursor == NULL) return -1;
    
    if(tc2_toNode(cursor, id_tax)) {
        TXC_TreeNodePtr tnp;
        Uint2 s;
        Int2 rank;
        
        do {
            tree_parent(cursor);
            tnp= tree_getNodeData(cursor, &s);
            if(tnp == NULL) {
                ret_id= -1;
                break;
            }
            ret_id= tnp->tax_id;
            rank= tnp->flags & 0xFF;
            --rank;
            if(rank == GenusRank) break;
            if((rank > 0) && (rank < GenusRank)) ret_id= -1;
        }
        while(ret_id > 1);
    }
    
    tree_closeCursor(cursor);
    return (ret_id > 1)? ret_id : -1;
}

int tax1_getChildren(Int4 id_tax, Int4** ids_out)
{
    int n= 0;
    Int4* ids;
    TreeCursorPtr cursor= tree_openCursor(tax_tree, NULL, NULL);

    *ids_out= NULL;

    if(cursor == NULL) return 0;

    if(tc2_toNode(cursor, id_tax)) {
        if(tax_ptree_addChildren(cursor) && tree_child(cursor)) {
            TXC_TreeNodePtr tnp;
            Uint2 s;
            
            do {
                n++;
            }
            while(tree_sibling(cursor));
            
            *ids_out= ids= MemNew(n*sizeof(Int4));
            
            tree_parent(cursor);
            tree_child(cursor);
            n= 0;
            do {
                tnp= tree_getNodeData(cursor, &s);
                if(tnp != NULL) ids[n++]= tnp->tax_id;
            }
            while(tree_sibling(cursor));
        }
    }
    
    tree_closeCursor(cursor);
    return n;
}


/* find last common ancestor for two nodes */
Int4 tax1_join(Int4 taxid1, Int4 taxid2)
{
    TreeNodeId nid;
    Int4 aid= 0;
    TreeCursorPtr cursor1= tree_openCursor(tax_tree, NULL, NULL);
    TreeCursorPtr cursor2= tree_openCursor(tax_tree, NULL, NULL);
    
    if((cursor1 == NULL) || (cursor2 == NULL) || 
       (!tc2_toNode(cursor1, taxid1)) || (!tc2_toNode(cursor2, taxid2))) {
        if(cursor1 != NULL) tree_closeCursor(cursor1);
        if(cursor2 != NULL) tree_closeCursor(cursor2);
        return -1;
    }
    
    nid= tree_getAncestor(cursor1, cursor2);
    
    if(tree_toNode(cursor1, nid)) {
        TXC_TreeNodePtr tnp;
        Uint2 s;
        
        tnp= tree_getNodeData(cursor1, &s);
        if(tnp != NULL) aid= tnp->tax_id;
    }
    
    tree_closeCursor(cursor1);
    tree_closeCursor(cursor2);
    return aid;
}


/***************************************************
 * Get tax_id by organism name
 * returns:
 *       tax_id if one node found
 *       0      no organism found
 *       -tax_id if more than one node found
 */
Int4 tax1_getTaxIdByName(CharPtr orgname)
{
    return tax_getIdByName(orgname, NULL, 0);
}

/***************************************************
 * Get all tax_id by organism name
 * returns:
 *       Number of tax ids found
 */
Int4 tax1_getAllTaxIdByName(CharPtr orgname, Int4 **Ids_out)
{
    int i;
    TaxNamePtr nameList;
    Int4 *Ids;
    int n= tax_findByName(orgname, TAX_NAME_SEARCH, &nameList);

    if(n < 1) return 0;

    *Ids_out= Ids= MemNew(n*sizeof(Int4));
    if(Ids != NULL) {
        for(i= 0; i < n; i++) {
            Ids[i]= nameList[i].tax_id;
            if(nameList[i].name_txt != NULL) MemFree(nameList[i].name_txt);
            if(nameList[i].unique_name != NULL) MemFree(nameList[i].unique_name);
        }
    }
    else {
        for(i= 0; i < n; i++) {
            if(nameList[i].name_txt != NULL) MemFree(nameList[i].name_txt);
            if(nameList[i].unique_name != NULL) MemFree(nameList[i].unique_name);
        }
        n= 0;
    }
    MemFree(nameList);
    return n;
}

static Boolean goodOrgMode(Uint1 t)
{
    return (t != 254) && (t != 20);
}
	
static Int4 getIdByOrgRef(CharPtr sname, OrgNamePtr orNm)
{
    if(orNm != NULL) {
        OrgModPtr o_mod= orNm->mod;
        Boolean om_flag= FALSE;
        Int4 tax_id, id;
        CharPtr altname= NULL;
        int nof_mods= 0;
        _subspecPtr ss;
        _subspec src;
        
        /* first try to search using search name */
        for(;o_mod != NULL; o_mod= o_mod->next) {
            if(o_mod->subtype == 254) {
                /* search name */
                altname= o_mod->subname;
            }
            else if(o_mod->subtype != 20) nof_mods++;
        }
        
        if(nof_mods == 0) {
            /* we have no modifiers */
            if(altname != NULL) {
                if((tax_id= tax_getIdByName(altname, NULL, 0)) > 0) return tax_id;
                return tax_getIdByName(sname, altname, 254);
            }
            return tax_getIdByName(sname, NULL, 0);
        }
        
        if(nof_mods == 1) {
            /* we have one valuable modifier */
            for(o_mod= orNm->mod; o_mod != NULL; o_mod= o_mod->next) {
                if(goodOrgMode(o_mod->subtype)) {
                    if(altname != NULL) {
                        if((tax_id= tax_getIdByName(altname, o_mod->subname, o_mod->subtype)) > 0) 
                            return tax_id; /* find by old name and modifier */
                        if((tax_id= tax_getIdByName(sname, o_mod->subname, o_mod->subtype)) > 0) 
                            return tax_id; /* find by new name and modifier */
                        return tax_getIdByName(sname, altname, 254); /* find by new name and old name */
                    }
                    return tax_getIdByName(sname, o_mod->subname, o_mod->subtype);
                }
            }
            return 0;
        }
        
        /* we have more than one modifier */
        /* first try to find organism using just names */
        if(altname != NULL) {
            if((tax_id= tax_getIdByName(altname, NULL, 0)) == 0) {
                tax_id= tax_getIdByName(sname, altname, 254);
            }
        }
        else {
            tax_id= tax_getIdByName(sname, NULL, 0);
        }
        
        if(tax_id == 0) return 0; /* we have no such names */
        if(tax_id > 0) {
            /* we have found just one node, check it against modifiers */
            id= 0;
            for(o_mod= orNm->mod; o_mod != NULL; o_mod= o_mod->next) {
                if(o_mod->subtype != 20) {
                    src.stype= o_mod->subtype;
                    src.sname= o_mod->subname;
                    src.rname= NULL;
                    if((ss= tax_SSget(tax_id, &src)) != NULL) {
                        if(ss->rname != NULL) MemFree(ss->rname);
                        if((ss->r_id != 0) && (ss->r_id != tax_id)) {
                            if(id == 0) id= ss->r_id;
                            else if(id != ss->r_id) {
                                id= -id; /* conflict in mapping */
                                break;
                            }
                        }
                    }
                }
            }
            if(id == 0) return tax_id;
            if(id < 0) return -tax_id; /* we have a mapping conflict */
            
            /* we have a mapping without conflict, we try to make the best assumption */
            return id;
        }
        
        if(tax_id < 0) {
            /* more than one tax_id was found */
            Int4Ptr ids;
            Int4 n;
            
            if(altname != NULL) {
                n= tax1_getAllTaxIdByName(altname, &ids);
                if(n < 1) n= tax1_getAllTaxIdByName(sname, &ids);
            }
            else n= tax1_getAllTaxIdByName(sname, &ids);
            
            id= 0;
            while(n-- > 0) {
                for(o_mod= orNm->mod; o_mod != NULL; o_mod= o_mod->next) {
                    if(goodOrgMode(o_mod->subtype)) {
                        src.stype= o_mod->subtype;
                        src.sname= o_mod->subname;
                        src.rname= NULL;
                        if((ss= tax_SSget(ids[n], &src)) != NULL) {
                            if(ss->rname != NULL) MemFree(ss->rname);
                            if(ss->r_id != 0) {
                                if(id == 0) id= ss->r_id;
                                else if(id != ss->r_id) id= -id;
                            }
                        }
                    }
                }
            }
            if(ids != NULL) MemFree(ids);
            if(id > 0) return id;
        }
        return tax_id;
    }
    else {
        /* we have no modifiers */
        return tax_getIdByName(sname, NULL, 0);
    }
    
}

Int4 tax1_getTaxIdByOrgRef(OrgRefPtr orgRef)
{
#ifdef TAXSERVICE
    return txc_getTaxIdByOrgRef(orgRef);
#else
    Int4 tax_id, id;
    
    if(orgRef == NULL) return 0;
    
    tax_id= 0;
    
    if((orgRef->taxname != NULL) &&
       ((tax_id= getIdByOrgRef(orgRef->taxname, orgRef->orgname)) > 0)) return tax_id;
    
    if((orgRef->common != NULL) &&
       ((tax_id= getIdByOrgRef(orgRef->common, orgRef->orgname)) > 0)) return tax_id;
    
    if(orgRef->syn != NULL) {
        ValNodePtr synonym;
        
        id= 0;
        
        for(synonym= orgRef->syn; (synonym != NULL) && (id < 1); synonym= synonym->next) {
            id= getIdByOrgRef(synonym->data.ptrvalue, orgRef->orgname);
        }
    }
    
    return (id > 0)? id : tax_id;
#endif
}

/*******************************************************************
 * Get tax_id by organism name (it could be "unique" variant of name)
 * returns:
 *       tax_id if one node found
 *       0      no organism found
 *       -tax_id if more than one node found
 */
Int4 tax1_findTaxIdByName(CharPtr orgname)
{
    Int4 id= tax_getIdByName(orgname, NULL, 0);

    if(id < 1) {
        Int4 idu= tax_uniqueName(orgname, 0);
        
        if(idu > 0) id= idu;
    }
    return id;
}


/*************************************************************************
 * Get all tax_id by organism name (it could be "unique" variant of name)
 * returns:
 *       Number of tax ids found
 */
Int4 tax1_findAllTaxIdByName(CharPtr orgname, Int4 **Ids_out)
{
    Int4 id= tax1_findTaxIdByName(orgname);
    
    if(id > 0) {
        *Ids_out= MemNew(sizeof(Int4));
        if(*Ids_out != NULL) {
            **Ids_out= id;
            return 1;
        }
        else return 0;
    }
    
    if(id < 0) {
        return tax1_getAllTaxIdByName(orgname, Ids_out);
    }
    
    return 0;
}

Int2 tax1_getAllNames(Int4 tax_id, CharPtr **out_names, Boolean unique)
{
    TaxNamePtr nameList;
    Int2 n= tax_getOrgNames(tax_id, &nameList);
    Int2 i;
    CharPtr* names;

    if(n < 1) return 0;

    *out_names= names= MemNew(n*sizeof(CharPtr));
    if(names != NULL) {
        for(i= 0; i < n; i++) {
            if(unique && (nameList[i].unique_name != NULL) && (nameList[i].unique_name[0] != '\0')) {
                names[i]= nameList[i].unique_name;
                nameList[i].unique_name= NULL;
            }
            else {
                names[i]= nameList[i].name_txt;
                nameList[i].name_txt= NULL;
            }
        }
    }
    
    for(i= 0; i < n; i++) {
        if(nameList[i].name_txt != NULL) MemFree(nameList[i].name_txt);
        if(nameList[i].unique_name != NULL) MemFree(nameList[i].unique_name);
    }
    
    MemFree(nameList);
    return n;
}


CharPtr tax1_getGCName(Int2 gc_id)
{
    return tax_getGCName(gc_id);
}

static OrgRefPtr s_tax1_getOrgRef(Int4 tax_id, int* is_species, int* is_uncultured, NameListPtr* blast_name)
{
    OrgRefPtr orp;

    tax_id= getLiveId(tax_id);
    if(tax_id == 0) return NULL;
    
    if((orp= getFromBuff(tax_id, is_species, is_uncultured, blast_name)) != NULL) {
        /* OrgRef is already in buffer */
        return orp;
    }
    
    lockBuff(TAX_WRITE);
    loadInBuff(tax_id);
    unlockBuff();
    
    return getFromBuff(tax_id, is_species, is_uncultured, blast_name);
}

OrgRefPtr tax1m_getOrgRef(Int4 tax_id, int* is_species, int* is_uncultured, CharPtr* blast_name)
{
    NameListPtr blast_name_list= NULL;
    OrgRefPtr orp= s_tax1_getOrgRef(tax_id, is_species, is_uncultured, &blast_name_list);
    if((blast_name_list != NULL) && (blast_name != NULL)) {
        *blast_name= StringSave(blast_name_list->name);
    }
    return orp;
}

OrgRefPtr tax1_getOrgRef(Int4 tax_id, int* is_species, CharPtr div, CharPtr embl_cde)
{
    OrgRefPtr orp= s_tax1_getOrgRef(tax_id, is_species, NULL, NULL);
    if(embl_cde != NULL) *embl_cde= '\0';
    if((div != NULL) && (orp != NULL) && (orp->orgname != NULL) && (orp->orgname->div != NULL)) {
        StringCpy(div, orp->orgname->div);
    }
    
    return orp;
}

static ValNodePtr make_blast_name(NameListPtr bl)
{
    ValNodePtr list= NULL;
    ValNodePtr header= NULL;
    
    while(bl != NULL) {
        list= ValNodeNew(list);
        list->data.ptrvalue= StringSave(bl->name);
        if(header == NULL) header= list;
        bl= bl->next;
    }
    return header;
}

Taxon2DataPtr tax1m_getbyid(Int4 tax_id)
{
    Taxon2DataPtr res;
    OrgRefPtr db_orgRef;
    int is_species;
    int is_uncultured;
    NameListPtr bl;

    if(tax_id <= 0) return NULL;
    db_orgRef= s_tax1_getOrgRef(tax_id, &is_species, &is_uncultured, &bl);
    if(db_orgRef == NULL) return NULL; /* nothing found */

    res= Taxon2DataNew();
    /* make new orgref */
    res->org= OrgRefNew();
    res->org->db= NULL;
    res->org->orgname= NULL;
    res->is_species_level= is_species;
    res->is_uncultured= is_uncultured;
    res->blast_name= make_blast_name(bl);

    /* fill-up orgref based on db_orgRef */
    bldOrgRefOut(res->org, db_orgRef, getLiveId(tax_id));
    return res;
}

/* the old version of the same function */
Taxon1DataPtr tax1_getbyid(Int4 tax_id)
{
    Taxon1DataPtr res;
    OrgRefPtr db_orgRef;
    int is_species;

    if(tax_id <= 0) return NULL;
    db_orgRef= s_tax1_getOrgRef(tax_id, &is_species, NULL, NULL);
    if(db_orgRef == NULL) return NULL; /* nothing found */

    res= Taxon1DataNew();
    /* make new orgref */
    res->org= OrgRefNew();
    res->org->db= NULL;
    res->org->orgname= NULL;
    res->is_species_level= is_species;
    res->embl_code= NULL;
    res->div= (db_orgRef->orgname != NULL)? StringSave(db_orgRef->orgname->div) : NULL;
    
    /* fill-up orgref based on db_orgRef */
    bldOrgRefOut(res->org, db_orgRef, getLiveId(tax_id));
    return res;
}

/*************************************************************************/
/* return pointer to first non-blank character in str1 after prefix str2 */
/* if str2 is not prefix for str1 then return str1                       */
/*************************************************************************/
static CharPtr strTail(CharPtr str1, CharPtr str2)
{
    CharPtr c;

    if(StringStr(str1, str2) != str1) return str1;

    c= str1 + StringLen(str2);

    while((*c != '\0') && IS_WHITESP(*c)) c++;

    return c;
}

static void rmWord(CharPtr str, CharPtr wrd, int wlen)
{
    if((str == wrd) || isspace(*(wrd-1))) {
        while((*(wrd+wlen) != '\0') && isspace(*(wrd+wlen))) wlen++;
    }
                       
    for(;;) {
        *wrd= *(wrd + wlen);
        if(*wrd == '\0') break;
        ++wrd;
    }
}

static Int2 getSubtypeFromName(CharPtr name)
{
    CharPtr c;
    if(strchr(name, '.') == NULL) return 0;

    /* ignore subsp. cf. and subsp. aff. */
    if (StringStr (name, "subsp. cf.") != NULL) return 0;
    if (StringStr (name, "subsp. aff.") != NULL) return 0;

    /* check for subsp */
    c= StringStr(name, "subsp.");
    if(c == name) {
        rmWord(name, c, 6);
        return (nof_tokens(c) == 1)? 22 : 0;
    }
    c= StringStr(name, "ssp.");
    if(c == name) {
        rmWord(name, c, 4);
        return (nof_tokens(c) == 1)? 22 : 0;
    }
    c= StringStr(name, "f. sp.");
    if(c == name) {
        rmWord(name, c, 6);
        return 26;
    }
    c= StringStr(name, "f.sp.");
    if(c == name) {
        rmWord(name, c, 5);
        return 26;
    }
    c= StringStr(name, "str.");
    if(c == name) {
        rmWord(name, c, 4);
        return 2;
    }
    c= StringStr(name, "substr.");
    if(c == name) {
        rmWord(name, c, 7);
        return 3;
    }
    c= StringStr(name, "var.");
    if(c == name) {
        rmWord(name, c, 4);
        return 6;
    }
    c= StringStr(name, "sv.");
    if(c == name) {
        rmWord(name, c, 3);
        return 9;
    }
    c= StringStr(name, "cv.");
    if(c == name) {
        rmWord(name, c, 3);
        return 10;
    }
    c= StringStr(name, "pv.");
    if(c == name) {
        rmWord(name, c, 3);
        return 11;
    }
    c= StringStr(name, "bv.");
    if(c == name) {
        rmWord(name, c, 3);
        return 13;
    }
    c= StringStr(name, "f.");
    if(c == name) {
        rmWord(name, c, 2);
        return 25;
    }
    c= StringStr(name, "fo.");
    if(c == name) {
        rmWord(name, c, 3);
        return 25;
    }
    c= StringStr(name, "grp.");
    if(c == name) {
        rmWord(name, c, 4);
        return 15;
    }
    return 0;
}

static OrgModPtr bldOrgMod(TreeCursorPtr cursor)
{
    TXC_TreeNodePtr parent= NULL;
    Uint2 s;
    TXC_TreeNodePtr me= tree_getNodeData(cursor, &s);
    TXC_TreeNodePtr tnp;
    TreeNodeId nid= tree_getId(cursor);
    Int2 rank, prank;
    OrgModPtr orgMdf= OrgModNew();

    while(tree_parent(cursor)) {
        if((tnp= tree_getNodeData(cursor, &s)) == NULL) continue;
        prank= tnp->flags & 0xFF;
        --prank;
        if((prank == SubspeciesRank) || 
           (prank == SpeciesRank) ||
           (prank == GenusRank)) {
            parent= tnp;
            break;
        }
    }
    tree_toNode(cursor, nid);
    
    if(parent != NULL) {
        orgMdf->subname= StringSave(strTail(me->node_label, parent->node_label));
    }
    else {
        orgMdf->subname= StringSave(me->node_label);
    }
    
    rank= me->flags & 0xFF;

    orgMdf->subtype= getSubtypeFromName(orgMdf->subname);

    if(orgMdf->subtype == 22 && rank != SubspeciesRank + 1) 
        orgMdf->subtype= 0;

    if(orgMdf->subtype <= 0) {
        if(--rank == SubspeciesRank) {
            if(nof_tokens(me->node_label) == 3) orgMdf->subtype= 22; /* subspecies */
        }
        else if(rank == tax_getRankId("varietas")) {
            orgMdf->subtype= 6; /* variety */
        }
        else if(rank == tax_getRankId("forma")) {
            orgMdf->subtype= 25; /* forma */
        }
        else if((parent != NULL) && (prank == SubspeciesRank)) {
            orgMdf->subtype= 2; /* strain */
        }
        else {
            orgMdf->subtype= 0; /* other */
        }
    }
    orgMdf->attrib= NULL;

    if(orgMdf->subtype == 0) {
        OrgModFree(orgMdf);
        orgMdf= 0;
    }
    
    return orgMdf;
}


/***************************************************************/  
/* if name is binomial name build the correspondent structures */
/* otherwise return 0                                          */
/***************************************************************/
static int binomialName(TreeCursorPtr cursor, OrgNamePtr onp)
{
    TXC_TreeNodePtr spec= NULL;
    TXC_TreeNodePtr subspec= NULL;
    TXC_TreeNodePtr genus= NULL;
    TXC_TreeNodePtr tnp;
    Uint2 s;
    TreeNodeId nid= tree_getId(cursor);
    Int2 rank;
    BinomialOrgNamePtr bName;

    do {
        tnp= tree_getNodeData(cursor, &s);
        if(tnp == NULL) continue;
        rank= tnp->flags & 0xFF;
        if(--rank == SubspeciesRank) subspec= tnp;
        else if(rank == SpeciesRank) spec= tnp;
        else if(rank == GenusRank) {
            genus= tnp;
            break;
        }
    }
    while(tree_parent(cursor));
    
    tree_toNode(cursor, nid);
    
    if(genus == NULL) {
        /* try to find subgenus */
        do {
            tnp= tree_getNodeData(cursor, &s);
            if(tnp == NULL) continue;
            rank= tnp->flags & 0xFF;
            if(--rank == (GenusRank + 1)) {
                genus= tnp;
                break;
            }
        }
        while(tree_parent(cursor));
        tree_toNode(cursor, nid);
    }
    
    if(genus == NULL) return 0; /* no genus - no binomial */
    
    onp->choice= 1; /*binomial*/
    
    onp->data= bName= BinomialOrgNameNew();
    
    bName->genus= StringSave(genus->node_label);
    
    
    if(spec != NULL) {
        /* we have a species in lineage */
        bName->species= StringSave(strTail(spec->node_label, genus->node_label));
        
        if(subspec != NULL) {
            /* we also have a subspecies in lineage */
            bName->subspecies= StringSave(strTail(subspec->node_label, spec->node_label));
        }
        else {
            bName->subspecies= NULL;
        }
        tnp= tree_getNodeData(cursor, &s);
        
        onp->mod= (tnp == spec)? NULL : bldOrgMod(cursor);    
        return 1;
    }
    
    /* no species in lineage */
    
    if(subspec != NULL) {
        /* we have no species but we have subspecies */
        bName->species= NULL;
        bName->subspecies= StringSave(strTail(subspec->node_label, genus->node_label));
        onp->mod= bldOrgMod(cursor);
        return 1;
    }
    
    /* we have no species, no subspecies but we are under species level (varietas or forma) */
    
    bName->species= NULL;
    bName->subspecies= NULL;
    onp->mod= bldOrgMod(cursor);
    return 1;
}

      
static void partialName(TreeCursorPtr cursor, OrgNamePtr onp)
{
    TaxElementPtr taxElem;
    Uint2 s;
    TXC_TreeNodePtr tnp= tree_getNodeData(cursor, &s);
    Int2 rank_id= tnp->flags & 0xFF;
    
    onp->choice= 5; /* partial */
    onp->data= taxElem= TaxElementNew();
    
    if(--rank_id == FamilyRank) {
        taxElem->fixed_level= 1; /* family */
        taxElem->level= NULL;
    }
    else if(rank_id == OrderRank) {
        taxElem->fixed_level= 2;
        taxElem->level= NULL;
    }
    else if(rank_id == ClassRank) {
        taxElem->fixed_level= 3;
        taxElem->level= NULL;
    }
    else {
        taxElem->fixed_level= 0;
        taxElem->level= StringSave(tax_getRank(rank_id));
    }
    
    taxElem->name= StringSave(tnp->node_label);
    taxElem->next= NULL;
}


/*****************************************************************
 * build synonyms valnodes
 * this routine include in valnodes synonyms and common synonyms
 */
static ValNodePtr bldSynValNodes(TaxNamePtr syn, Int2 n)
{
    ValNodePtr list= NULL;
    ValNodePtr header= NULL;
    Int2 i;
    
    for(i= 1; i < n; i++) {
        if((syn[i].class_cde == SYNONYM) || (syn[i].class_cde == COMMON_NAME)) {
            list= ValNodeNew(list);
            list->choice= (syn[i].class_cde == SYNONYM)? 1 : 0;
            list->data.ptrvalue= syn[i].name_txt;
            syn[i].name_txt= NULL;
            if(header == NULL) header= list;
        }
    }
    return header;
}
	   

/**************************************************************************
 *
 *	FsTaxGetLineage
 *
 **************************************************************************/

static CharPtr bldLineage(TreeCursorPtr cursor, int* is_uncultured, NameListPtr* blast_name)
{
    TXC_TreeNodePtr tnp;
    Uint2 s;
    TreeNodeId nid= tree_getId(cursor);
    CharPtr lineage= NULL;
    CharPtr tmp, t;
    Int2 rank= 0;
    
    
    while(tree_parent(cursor)) {
        if((tnp= tree_getNodeData(cursor, &s)) != NULL) {
            if(tnp->tax_id < 2) break;
            if(tnp->flags & TXC_UNCULTURED) *is_uncultured= 1;
            if((tnp->flags & TXC_STHIDE) == 0) { /* we do have a blast name here */
                NameListPtr node= MemNew(sizeof(NameList));
                node->name= tnp->node_label + (StringLen(tnp->node_label) + 1);
                node->next= NULL;
                *blast_name= node;
                blast_name= &(node->next);
            }
            rank= tnp->flags & 0xFF;
            if(rank > SpeciesRank) {
                if(lineage != NULL) {
                    lineage = MemFree(lineage);
                }
                continue;
            }
            
            if((tnp->flags & TXC_GBHIDE) == 0) {
                s= StringLen(tnp->node_label);
                if(lineage != NULL) {
                    s+= StringLen(lineage) + 2;
                }
                
                tmp= MemNew(s+2);
                if(tmp == NULL) continue;
                t= StringMove(tmp, tnp->node_label);
                if(lineage != NULL) {
                    t= StringMove(t, "; ");
                    t= StringMove(t, lineage);
                    MemFree(lineage);
                }
                lineage= tmp;
            }
        }
    }
    
    tree_toNode(cursor, nid);

    return lineage;
}

static ValNodePtr bldDBId(Int4 id)
{
    ValNodePtr dbnode;
    DbtagPtr dbtag;
    ObjectIdPtr object_id;

    /* populate tax_id */
    dbnode= ValNodeNew(NULL);
    dbnode->data.ptrvalue= dbtag= DbtagNew();
    dbtag->db = StringSave("taxon");
    dbtag->tag= object_id= ObjectIdNew();
    object_id->str= NULL;
    object_id->id = id;
    return dbnode;
}

static OrgNamePtr bldOrgName(TreeCursorPtr cursor, int* is_species_out, 
                             int* is_uncultured, NameListPtr* blast_name)
{
    OrgNamePtr onp;
    Uint2 s;
    TXC_TreeNodePtr tnp= tree_getNodeData(cursor, &s);
    CharPtr div_abbr;
    Int2 rank_id;
    int is_species;
    Int2 div_id;
    TreeNodeId nid= tree_getId(cursor);
    Int2 rank;

    /*Int4 p_id, s_id;*/

    if(tnp == NULL) return NULL;

    *is_uncultured= ((tnp->flags & TXC_UNCULTURED) != 0)? 1 : 0;
    if((tnp->flags & TXC_STHIDE) == 0) { /* we do have a blast name here */
        NameListPtr node= MemNew(sizeof(NameList));
        node->name= tnp->node_label + (StringLen(tnp->node_label) + 1);
        node->next= NULL;
        *blast_name= node;
        blast_name= &(node->next);
    }

    onp= OrgNameNew();

    rank_id= tnp->flags & 0xFF;
    rank_id--;
      
    div_id= (tnp->flags >> 8) & 0x3F;
    onp->gcode= (tnp->flags >> (8+6)) & 0x3F;
    onp->mgcode= (tnp->flags >> (8+6+6)) & 0x3F;
    onp->lineage= bldLineage(cursor, is_uncultured, blast_name);
	   
    is_species= (rank_id >= SpeciesRank)? 1 : 0;
    /* correct level by lineage if node has no rank */
    if(rank_id < 0) {

        while(tree_parent(cursor)) {
            tnp= tree_getNodeData(cursor, &s);
            if(tnp != NULL) {
                rank= tnp->flags & 0xFF;
                if(rank != 0) {
                    is_species= (rank >= SpeciesRank) ? 1 : 0;
                    break;
                }
            }
        }
        tree_toNode(cursor, nid);
        tnp= tree_getNodeData(cursor, &s);
    }

    if(tax_getDivision(div_id, &div_abbr, NULL)) {
        onp->div= StringSave(div_abbr);
        /* StringCpy(div, div_abbr);*/
    }
    *is_species_out= is_species;

    if(is_species) {
        /* we are on species level or below */
	     
        /* check for viruses */
        if((div_id == VRL_div) || (div_id == PHG_div)) {
            /* this is a virus */
            onp->choice= 2; /* virus */
            if(rank_id == SpeciesRank) {
                /* we are on species level */
                onp->data= StringSave(tnp->node_label);
                onp->mod= NULL;
            }
            else {
                /* we are below species */
                /* first try to find species or min rank which below species but above us */
                TreeNodeId s_id;
		
                s_id.idi= 0;
                while(tree_parent(cursor)) {
                    tnp= tree_getNodeData(cursor, &s);
                    if(tnp != NULL) {
                        rank= tnp->flags & 0xFF;
                        if(--rank >= SpeciesRank) {
                            s_id= tree_getId(cursor);
                            if(rank == SpeciesRank) break;
                        }
                        else if(rank >= 0) break;
                    }
                }
                if(s_id.idi != 0) {
                    /* we have species or something above us */
                    tree_toNode(cursor, s_id);
                }
                else {
                    /* no species above */
                    tree_toNode(cursor, nid);
                }
		
                tnp= tree_getNodeData(cursor, &s);
                onp->data= StringSave(tnp->node_label);
                if(s_id.idi != 0) {
                    tree_toNode(cursor, nid);
                }

                onp->mod= bldOrgMod(cursor);
            }
        }		
        else if(!binomialName(cursor, onp)) {
            /* name is not binomial: set partial */
            partialName(cursor, onp);
        }
    }
    else {
        /* above species */
        partialName(cursor, onp);
    }

    return onp;
}


static Boolean bldOrgRef(Int4 id, OrgRefPtr orp, int* is_species, int* is_uncult, NameListPtr* bnl)
{
    TreeCursorPtr cursor= tree_openCursor(tax_tree, NULL, NULL);
    TaxNamePtr nameList;
    Int2 n, i; 
    
    if((cursor == NULL) || (!tc2_toNode(cursor, id))) return FALSE;

    *is_species= 0;
    *is_uncult= 0;
    *bnl= NULL;

    n= tax_getOrgNames(id, &nameList);

    if(n < 1) {
        tree_closeCursor(cursor);
        return FALSE;
    }
    
    orp->taxname= nameList[0].name_txt;
    nameList[0].name_txt= NULL;
    

    /* fill-up preferred common name */
    orp->common= NULL;
    for(i= 1; i < n; i++) {
        if(nameList[i].class_cde == GB_COMMON) {
            orp->common= nameList[i].name_txt;
            nameList[i].name_txt= NULL;
            break;
        }
    }

    /* fill-up synonyms */
    orp->syn= bldSynValNodes(nameList, n);

    orp->mod= NULL;
    orp->db= bldDBId(id);
    orp->orgname= bldOrgName(cursor, is_species, is_uncult, bnl);
    /*
      ORGMOD_gb_acronym 32
      ORGMOD_gb_anamorph 33
      ORGMOD_gb_synonym 34
    */
    /* add some of the nametypes as OrgMods */
    if(orp->orgname) { /* OrgName is not empty */
        for(i= 1; i < n; i++) {
            if(nameList[i].class_cde == GB_ACRONYM) {
                OrgModPtr acr= OrgModNew();
                acr->subtype= ORGMOD_gb_acronym;
                acr->subname= nameList[i].name_txt;
                nameList[i].name_txt= NULL;
                acr->next= orp->orgname->mod;
                orp->orgname->mod= acr;
            }
            else if(nameList[i].class_cde == GB_ANAMORPH) {
                OrgModPtr anm= OrgModNew();
                anm->subtype= ORGMOD_gb_anamorph;
                anm->subname= nameList[i].name_txt;
                nameList[i].name_txt= NULL;
                anm->next= orp->orgname->mod;
                orp->orgname->mod= anm;
            }
            else if(nameList[i].class_cde == GB_SYNONYM) {
                OrgModPtr snm= OrgModNew();
                snm->subtype= ORGMOD_gb_synonym;
                snm->subname= nameList[i].name_txt;
                nameList[i].name_txt= NULL;
                snm->next= orp->orgname->mod;
                orp->orgname->mod= snm;
            }
        }
    }
    
    for(i= 0; i < n; i++) {
        if(nameList[i].name_txt != NULL) MemFree(nameList[i].name_txt);
        if(nameList[i].unique_name != NULL) MemFree(nameList[i].unique_name);
    }
    
    MemFree(nameList);

    tree_closeCursor(cursor);
    return TRUE;	
}
  

static void loadInBuff(Int4 id)
{
    int i, k= -1;
    Int4 t= my_timer + 1;
    /*Int4 bt;*/
  
    for(i= 0; i < BUFF_SIZE; i++) {
        if(or_buff[i].tax_id == 0) {
            k= i;
            break;
        }
        if(or_buff[i].timer < t) {
            t= or_buff[i].timer;
            k= i;
        }
    }

    if(k >= 0) {
        if(or_buff[k].p_org_ref != NULL) {
            OrgRefFree(or_buff[k].p_org_ref);
            free_blast_name(or_buff[k].blast_name);
        }
	    
        or_buff[k].tax_id= id;
        or_buff[k].p_org_ref= OrgRefNew();
        or_buff[k].timer= ++my_timer;
        or_buff[k].blast_name= NULL;
        if(!bldOrgRef(id, or_buff[k].p_org_ref, &or_buff[k].is_species, 
                      &or_buff[k].is_uncultured, &(or_buff[k].blast_name))) {
            OrgRefFree(or_buff[k].p_org_ref);
            or_buff[k].tax_id= 0;
        }
    }
}

static OrgRefPtr getFromBuff(Int4 id, int* is_sp, int* is_uncult, NameListPtr* bnl)
{
    int i;
    OrgRefPtr orp= NULL;

    lockBuff(TAX_READ);

    for(i= 0; i < BUFF_SIZE; i++) {
        if(or_buff[i].tax_id == id) {
            or_buff[i].timer= ++my_timer;
            orp= or_buff[i].p_org_ref;
            if(is_sp != NULL) *is_sp= or_buff[i].is_species;
            if(is_uncult != NULL) *is_uncult= or_buff[i].is_uncultured;
            if(bnl != NULL) *bnl= or_buff[i].blast_name;
            break;
        }
    }
    unlockBuff();
    return orp;
}

static OrgModPtr fixModifier(Int4 tax_id, OrgModPtr omp, CharPtr taxname)
{
    _subspec src_ss;
    _subspecPtr ss= NULL;

    memset(&src_ss, 0, sizeof(_subspec));

    if((omp->subtype < 2) ||
       (omp->subtype == ORGMOD_gb_acronym) ||
       (omp->subtype == ORGMOD_gb_anamorph) ||
       (omp->subtype == ORGMOD_gb_synonym)) {
        omp->next= NULL;
        OrgModFree(omp);
        return NULL;
    }

    if((omp->subtype == ORGMOD_anamorph ||
        omp->subtype == ORGMOD_synonym) && 
       StringICmp(omp->subname, taxname) == 0) {
        omp->next= NULL;
        OrgModFree(omp);
        return NULL;
    }
        

    if((omp->subname != NULL) && (omp->subtype != 0)) {
        src_ss.stype= omp->subtype;
        src_ss.sname= omp->subname;
        src_ss.rname= NULL;

        ss= tax_SSget(tax_id, &src_ss);
    }

    if((ss != NULL) && (ss->r_id == tax_id) && (ss->rtype == 0) && 
       ss->rname && (ss->rname[0] == '-')) {
        /* remove it */
        if(ss->rname != NULL) MemFree(ss->rname);
        omp->next= NULL;
        OrgModFree(omp);
        return NULL;
    }

    if((ss != NULL) && (ss->r_id == tax_id) && (ss->rtype != 0)) {		
        MemFree(omp->subname);
        omp->subname= src_ss.rname; src_ss.rname= NULL;
        omp->subtype= src_ss.rtype;
        return omp;
    }

    if(src_ss.rname != NULL) MemFree(src_ss.rname);

    return omp;
}

static void CleanOrgMod(Int4 tax_id, OrgNamePtr onp, CharPtr taxname)
{
    OrgModPtr omp, omp_p, omp_n;

    for(omp_p= NULL, omp= onp->mod; omp != NULL; omp= omp_n) {
        omp_n= omp->next;
        if((omp= fixModifier(tax_id, omp, taxname)) == NULL) {
            /* exclude this modifier */
            if(omp_p == NULL) {
                onp->mod= omp_n;
            }
            else omp_p->next= omp_n;
        }
        else omp_p= omp;
    }
}
	    
static void cleanOrgName(Int4 tax_id, OrgNamePtr onp, CharPtr taxname)
{
    if(onp->lineage != NULL) onp->lineage= MemFree(onp->lineage);
    if(onp->div != NULL) onp->div= MemFree(onp->div);
#if 1
    /* #if 0 means that we will trust to initial modifier */
    if(onp->mod != NULL) CleanOrgMod(tax_id, onp, taxname);
#endif
    if(onp->next != NULL) OrgNameSetFree(onp->next);
    if(onp->data != NULL) {
        switch(onp->choice) {
        case 1 : /* binomial name */
            BinomialOrgNameFree(onp->data);
            onp->data= NULL;
            break;
        case 2 : /* virus name */
            onp->data= MemFree(onp->data);
            break;
        case 5 : /* partial name */
            TaxElementSetFree(onp->data);
            onp->data= NULL;
            break;
        }
    }
}

  
static BinomialOrgNamePtr copyBinomial(BinomialOrgNamePtr src)
{
    BinomialOrgNamePtr dst;

    if(src == NULL) return NULL;

    dst= BinomialOrgNameNew();
    dst->genus= (src->genus != NULL)? StringSave(src->genus) : NULL;
    dst->species= (src->species != NULL)? StringSave(src->species) : NULL;
    dst->subspecies= (src->subspecies != NULL)? StringSave(src->subspecies) : NULL;

    return dst;
}

static TaxElementPtr copyPartial(TaxElementPtr src)
{
    TaxElementPtr dst;

    if(src == NULL) return NULL;

    dst= TaxElementNew();
    dst->fixed_level= src->fixed_level;
    dst->level= (src->level != NULL)? StringSave(src->level) : NULL;
    dst->name= (src->name != NULL)? StringSave(src->name) : NULL;
    dst->next= (src->next != NULL)? copyPartial(src->next) : NULL;
    return dst;
}

static OrgModPtr copyOrgMod(OrgModPtr src)
{
#if 1
    OrgModPtr dst;

    if(src == NULL) return NULL;
    if(src->subtype == 255)
        return (src->next != NULL)? copyOrgMod(src->next) : NULL;

    dst= OrgModNew();
    dst->subtype= src->subtype;
    dst->subname= (src->subname != NULL)? StringSave(src->subname) : NULL;
    dst->attrib= (src->attrib != NULL)? StringSave(src->attrib) : NULL;
    dst->next= (src->next != NULL)? copyOrgMod(src->next) : NULL;

    return dst;
#else
    return NULL;
#endif
}

static int subtypeConflict(Int2 t1, Int2 t2)
{
    if(t1 == t2) return 1;
    switch(t2) {
    case 2:
    case 6:
    /*case 22:*/
/*        if((t1 >= 2 && t1 <= 17) || (t1 == 22)) return 1;*/
        if((t1 >= 2 && t1 <= 17)) return 1;
        break;
    case 255:
        return 1;
    default:
        break;
    }
    return 0;
}

static void mergeOrgMod(OrgModPtr omp, OrgModPtr src, int need_copy)
{
    if(src == NULL) return;
    else {
        OrgModPtr dst, lst;
        for(dst= omp; dst != NULL; dst= dst->next) {
            lst= dst;
            if(subtypeConflict((Int2)dst->subtype, (Int2)src->subtype)) {
                lst= src->next;
                if(!need_copy) {
                    src->next= NULL;
                    OrgModFree(src);
                }
                mergeOrgMod(omp, lst, need_copy);
                return;
            }
        }

        if(need_copy) {
            lst->next= dst= OrgModNew();
            dst->subtype= src->subtype;
            dst->subname= (src->subname != NULL)? StringSave(src->subname) : NULL;
            dst->attrib= (src->attrib != NULL)? StringSave(src->attrib) : NULL;
            dst->next= NULL;
            lst= src->next;
        }
        else {
            lst->next= src;
            lst= src->next;
            src->next= NULL;
        }
        
        mergeOrgMod(omp, lst, need_copy);
    }

}
  
static ValNodePtr removeDbtag(ValNodePtr vnp)
{
    ValNodePtr vnn, vnf, vnl= NULL;
    DbtagPtr dbtag;

    for(vnf= vnp; vnp != NULL; vnp= vnn) {
        dbtag= vnp->data.ptrvalue;
        vnn= vnp->next;
        if(dbtag == NULL) return NULL;
        if(StringCmp(dbtag->db, "taxon") == 0) {
            /* taxon tag, remove it */
            if(vnl == NULL) {
                vnf= vnn;
            }
            else {
                vnl->next= vnn;
            }
            DbtagFree(dbtag);
            MemFree(vnp);
        }
        else {
            vnl= vnp;
        }
    }
    return vnf;
}
	

static void bldOrgRefOut(OrgRefPtr dst, OrgRefPtr src, Int4 tax_id)
{
    ValNodePtr vnp, vnl;
    DbtagPtr dbtag;
    ObjectIdPtr object_id;
    OrgNamePtr onp;

    dst->taxname= StringSave(src->taxname);
    dst->common= (src->common != NULL)? StringSave(src->common) : NULL;

    /* populate tax_id */
    vnp= ValNodeNew(NULL);
    if (dst->db != NULL) {
        dst->db= removeDbtag(dst->db);
    }
    vnp->next= dst->db;
    dst->db= vnp;
    vnp->data.ptrvalue= dbtag= DbtagNew();
    dbtag->db = StringSave("taxon");
    dbtag->tag= object_id= ObjectIdNew();
    object_id->str= NULL;
    object_id->id = getLiveId(tax_id);
    
    /* copy the synonym list */
    dst->syn= NULL; vnl= NULL;
    if(we_want_synonyms) {
        for(vnp= src->syn; vnp != NULL; vnp= vnp->next) {
            vnl= ValNodeNew(vnl);
            vnl->choice= vnp->choice;
            vnl->data.ptrvalue= StringSave(vnp->data.ptrvalue);
            if(dst->syn == NULL) dst->syn= vnl;
        }
    }
  
    /* copy orgname */
    if(dst->orgname == NULL) dst->orgname= onp= OrgNameNew();
    else onp= dst->orgname;

    onp->choice= src->orgname->choice;

    switch(src->orgname->choice) {
    case 1 : /*binomial*/
        onp->data= copyBinomial(src->orgname->data);
        break;
    case 2 : /* virus */
        onp->data= (src->orgname->data != NULL)? StringSave(src->orgname->data) : NULL;
        break;
    case 5 : /* partial */
        onp->data= copyPartial(src->orgname->data);
        break;
    default : /* can't handle */
        onp->data= NULL;
    }
  
    if(onp->mod == NULL) onp->mod= copyOrgMod(src->orgname->mod);
    else {
        mergeOrgMod(onp->mod, src->orgname->mod, 1);
    }
    onp->lineage= (src->orgname->lineage != NULL)? StringSave(src->orgname->lineage) : NULL;
    onp->gcode= src->orgname->gcode;
    onp->mgcode= src->orgname->mgcode;
    onp->div= StringSave(src->orgname->div);
}

static void populateReplaced(OrgRefPtr orp, OrgModPtr hitName)
{
    OrgModPtr omp, srv_mod;

    if(hitName == NULL) return;

    /* find the real hitName */
    if (hitName->subtype == 254) {
        srv_mod= hitName->next;
        hitName->next= NULL;
    }
    else {
        srv_mod= hitName;
        for(omp= hitName, hitName= hitName->next; hitName; hitName= hitName->next) {
            if(hitName->subtype == 254) {
                omp->next= hitName->next;
                hitName->next= NULL;
                break;
            }
            else {
                omp= hitName;
            }
        }
    }
                

    if(orp->orgname->mod) {
        mergeOrgMod(orp->orgname->mod, srv_mod, 0);
    }
    else {
        orp->orgname->mod= srv_mod;
    }


    if(hitName == NULL) return;

    for(omp= orp->orgname->mod; omp != NULL; omp= omp->next) {
        if(omp->subtype == 254) { /* search name is populated already */
            if((omp->attrib == NULL) && (hitName->attrib != NULL) &&
               (StringICmp(omp->subname, hitName->subname) == 0)) { 
                omp->attrib= hitName->attrib;
                hitName->attrib= NULL;
            }
            OrgModFree(hitName);
            return;
        }
    }

    if((orp->taxname != NULL) && (StringICmp(orp->taxname, hitName->subname) == 0)) {
        /* we probably don't need to populate search name */
        if(hitName->attrib == NULL) {
            OrgModFree(hitName);
            return;
        }
        else {
            Uint1 st= atoi(hitName->attrib+1);
            char* sn= strchr(hitName->attrib, '=');
            if(sn == NULL) {
                OrgModFree(hitName);
                return;
            }
            ++sn;
            for(omp= orp->orgname->mod; omp != NULL; omp= omp->next) {
                if((omp->subtype == st) && (StringICmp(omp->subname, sn) == 0)) {
                    OrgModFree(hitName);
                    return;
                }
            }
        }
    }

    /* adding this modifier */
    hitName->next= orp->orgname->mod;
    orp->orgname->mod= hitName;
}

Taxon2DataPtr tax1m_lookup(OrgRefPtr inp_orgRef, int merge)
{
    Taxon2DataPtr res;
    Int4 tax_id;
    OrgRefPtr db_orgRef;
    int is_species;
    int is_uncultured;
    NameListPtr bl;
    OrgModPtr hitName;

    tax_id= txc_findByOrg(inp_orgRef, &hitName);
    if(tax_id <= 0) return NULL;
    db_orgRef= s_tax1_getOrgRef(tax_id, &is_species, &is_uncultured, &bl);
    if(db_orgRef == NULL) return NULL;

    res= Taxon2DataNew();
    res->is_species_level= is_species;
    res->is_uncultured= is_uncultured;
    res->blast_name= make_blast_name(bl);

    if(merge) {
        /* we have to merge old orgref with the new one */
        res->org= inp_orgRef;
        /* clean-up old information */
        if(inp_orgRef->taxname != NULL) MemFree(inp_orgRef->taxname);
        if(inp_orgRef->common != NULL) MemFree(inp_orgRef->common);
        if(inp_orgRef->syn != NULL) ValNodeFreeData(inp_orgRef->syn);
        if(inp_orgRef->orgname != NULL) cleanOrgName(tax_id, inp_orgRef->orgname,
                                                     db_orgRef->taxname);
    }
    else {
        /* make new orgref */
        res->org= OrgRefNew();
        res->org->db= NULL;
        res->org->orgname= NULL;
    }
    /* fill-up orgref based on db_orgRef */
    bldOrgRefOut(res->org, db_orgRef, tax_id);
    populateReplaced(res->org, hitName);
    return res;
}

Taxon1DataPtr tax1_lookup(OrgRefPtr inp_orgRef, int merge)
{
    Taxon1DataPtr res;
    Int4 tax_id;
    OrgRefPtr db_orgRef;
    int is_species;
    OrgModPtr hitName;

    tax_id= txc_findByOrg(inp_orgRef, &hitName);

    //tax_id= tax1_getTaxIdByOrgRef(inp_orgRef);
    if(tax_id <= 0) return NULL;
    db_orgRef= s_tax1_getOrgRef(tax_id, &is_species, NULL, NULL);
    if(db_orgRef == NULL) return NULL;

    res= Taxon1DataNew();
    res->is_species_level= is_species;
    res->embl_code= NULL;
    res->div= (db_orgRef->orgname != NULL)? StringSave(db_orgRef->orgname->div) : NULL;

    if(merge) {
        /* we have to merge old orgref with the new one */
        res->org= inp_orgRef;
        /* clean-up old information */
        if(inp_orgRef->taxname != NULL) MemFree(inp_orgRef->taxname);
        if(inp_orgRef->common != NULL) MemFree(inp_orgRef->common);
        if(inp_orgRef->syn != NULL) ValNodeFreeData(inp_orgRef->syn);
        if(inp_orgRef->orgname != NULL) cleanOrgName(tax_id, inp_orgRef->orgname, 
                                                     db_orgRef->taxname);
    }
    else {
        /* make new orgref */
        res->org= OrgRefNew();
        res->org->db= NULL;
        res->org->orgname= NULL;
    }
    /* fill-up orgref based on db_orgRef */
    bldOrgRefOut(res->org, db_orgRef, tax_id);
    populateReplaced(res->org, hitName);
    return res;
}
  
  
Boolean tax1_init(void)
{
    return InitTaxDB();
}

void tax1_fini(void)
{
    CloseTaxDB();
}

TreePtr tax1e_getTaxTreePtr(void)
{
    return tax_tree;
}

Boolean tax1e_invokeNode(Int4 tax_id)
{
    return tax_ptree_addNode(tax_tree, tax_id);
}

Boolean tax1e_invokeChildren(Int4 tax_id)
{
    Boolean res= FALSE;

    TreeCursorPtr cursor= tree_openCursor(tax_tree, NULL, NULL);
    if(tax_id < 0) {
        res= TRUE;
        tax_id= -tax_id;
    }

    if((cursor == NULL) || (!tc2_toNode(cursor, tax_id))) return FALSE;

    res= res? tax_ptree_addSubtree(cursor) : tax_ptree_addChildren(cursor);

    tree_closeCursor(cursor);
    return res;
}

Boolean tax1e_toNode(TreeCursorPtr cursor, Int4 tax_id)
{
    return tc2_toNode(cursor, tax_id);
}

Int4 tax1e_getTaxId(TreeCursorPtr cursor)
{
    Uint2 s;
    TXC_TreeNodePtr tnp;
    
    tnp= tree_getNodeData(cursor, &s);

    return (tnp != NULL)? tnp->tax_id : 0;
}

CharPtr tax1e_getTaxName(TreeCursorPtr cursor)
{
    Uint2 s;
    TXC_TreeNodePtr tnp;
    
    tnp= tree_getNodeData(cursor, &s);

    return (tnp != NULL)? StringSave(tnp->node_label) : NULL;
}
    
Int4 tax1_getTaxId4Str(CharPtr str, CharPtr* substring, Int4Ptr *Ids_out)
{
    CharPtr b, e;
    Int4 tax_id;
    /*Int4Ptr Ids;*/
    int k;
    char c;
    
    *substring= NULL;
    tax_id= tax1_getTaxIdByName(str);

    if(tax_id > 1) {
        *Ids_out= MemNew(sizeof(Int4));
        **Ids_out= tax_id;
        *substring= StringSave(str);
        return 1;
    }
    else if(tax_id < 0) {
        *substring= StringSave(str);
        return tax1_getAllTaxIdByName(str, Ids_out);
    }

    /* whole string matches nothing */
    /* try the whole string inside first set of parenthesis */
    for(b= str; *b != '\0'; b++) {
        if(*b == '(') {
            k= 0;
            for(e= b+1; *e != '\0'; e++) {
                if(*e == '(') {
                    k++;
                    continue;
                }
                if(*e == ')') {
                    if(k > 0) {
                        k--; 
                        continue;
                    }

                    *e= '\0';
                    tax_id= tax1_getTaxIdByName(b+1);

                    if(tax_id > 1) {
                        *substring= StringSave(b+1);
                        *e= ')';
                        *Ids_out= MemNew(sizeof(Int4));
                        **Ids_out= tax_id;
                        return 1;
                    }
                    else if(tax_id < 0) {
                        *substring= StringSave(b+1);
                        *e= ')';
                        return tax1_getAllTaxIdByName(*substring, Ids_out);
                    }

                    /* whole string won't help lets try truncate it at first comma*/
                    *e= ')';
                    for(e= b+1; *e != '\0'; e++) {
                        if(*e == ',') {
                            *e= '\0';
                            tax_id= tax1_getTaxIdByName(b+1);

                            if(tax_id > 1) {
                                *substring= StringSave(b+1);
                                *e= ',';
                                *Ids_out= MemNew(sizeof(Int4));
                                **Ids_out= tax_id;
                                return 1;
                            }
                            else if(tax_id < 0) {
                                *substring= StringSave(b+1);
                                *e= ',';
                                return tax1_getAllTaxIdByName(*substring, Ids_out);
                            }
                            *e= ',';
                            break;
                        }
                    }
                    break;
                }
            }
            break;
        }
    }
			
    /* we still have got nothing */
    /* try the substring before first '(' */
    
    if(*b == '(') {
        /* we are staying on the first '(' */
        *b= '\0';
	
        tax_id= tax1_getTaxIdByName(str);

        if(tax_id > 1) {
            *Ids_out= MemNew(sizeof(Int4));
            **Ids_out= tax_id;
            *substring= StringSave(str);
            *b= '(';
            return 1;
        }
        else if(tax_id < 0) {
            *substring= StringSave(str);
            *b= '(';
            return tax1_getAllTaxIdByName(*substring, Ids_out);
        }
        *b= '(';
    }

    b= StringStr(str, "Organism");
    if(b == NULL) b= StringStr(str, "organism");
    if(b == NULL) b= StringStr(str, "ORGANISM");

    if(b != NULL) {
        e= StringChr(b, ':');
        if(e != NULL) {
            b= e+1;
            tax_id= tax1_getTaxIdByName(b);

            if(tax_id > 1) {
                *Ids_out= MemNew(sizeof(Int4));
                **Ids_out= tax_id;
                *substring= StringSave(b);
                return 1;
            }
            else if(tax_id < 0) {
                *substring= StringSave(b);
                return tax1_getAllTaxIdByName(*substring, Ids_out);
            }
	
            /* if multiple lines  or ; , ( */
            for(++e; *e != '\0'; e++) {
                if((*e == '\n') || (*e == ';') || (*e == ',') || (*e == '(')) {
                    c= *e;
                    *e= '\0';
                    tax_id= tax1_getTaxIdByName(b);

                    if(tax_id > 1) {
                        *substring= StringSave(b);
                        *e= c;
                        *Ids_out= MemNew(sizeof(Int4));
                        **Ids_out= tax_id;
                        return 1;
                    }
                    else if(tax_id < 0) {
                        *substring= StringSave(b);
                        *e= c;
                        return tax1_getAllTaxIdByName(*substring, Ids_out);
                    }
                    *e= c;
                    break;
                }
            }
        }
    }
    return 0;
}    

static int nameCmp(CharPtr s1, CharPtr s2)
{
    if((s1 == NULL) && (s2 == NULL)) return 0;
    if((s1 == NULL) || (s2 == NULL)) return 1;

    return strcmp(s1, s2);
}

static Int4 storedTaxId(OrgRefPtr orp)
{
    ValNodePtr vnp;
    DbtagPtr dbtag;
    ObjectIdPtr object_id;

    for(vnp= orp->db; vnp != NULL; vnp= vnp->next) {
        dbtag= vnp->data.ptrvalue;
        if((dbtag != NULL) && (StringCmp(dbtag->db, "taxon") == 0)) {
            ObjectIdPtr object_id= dbtag->tag;
            return object_id->id;
        }
    }

    return 0;
}

static Int4 OrgModCmp(OrgModPtr omp1, OrgModPtr omp2)
{
    OrgModPtr omp;
    int found;

    if(omp2 == NULL) return 0;
    if(omp1 == NULL) return 100;
    
    for(;omp2 != NULL; omp2= omp2->next) {
        found= 0;
        for(omp= omp1; omp != NULL; omp= omp->next) {
            if((omp2->subtype == omp->subtype) &&
               (nameCmp(omp2->subname, omp->subname) == 0)) {
                found= 1;
                break;
            }
        }
        if(!found) return 100;
    }
    return 0;
}

static Int4 OrgRefCmp(OrgRefPtr orp1, OrgRefPtr orp2)
{
    OrgNamePtr onp1= orp1->orgname;
    OrgNamePtr onp2= orp2->orgname;

    if(onp1 == NULL) return 4;
    if(onp2 == NULL) return -2;

    if(onp1->gcode != onp2->gcode) return 2;
    if(onp1->mgcode != onp2->mgcode) return 3;

    if(nameCmp(orp1->taxname, orp2->taxname) != 0) return 10;

    if(nameCmp(onp1->lineage, onp2->lineage) != 0) return 20;

    if(nameCmp(orp1->common, orp2->common) != 0) return 30;

    if(onp1->choice != onp2->choice) return 40;

    if(nameCmp(onp1->div, onp2->div) != 0) return 50;

    return OrgModCmp(onp1->mod, onp2->mod);
    
}
 
Int4 tax1e_needUpdate(OrgRefPtr inp_orgRef)
{
    Taxon1DataPtr res;
    Int4 tax_id;
    OrgRefPtr db_orgRef;
    int is_species;
    Boolean need_search_name= TRUE;
    CharPtr hit_name;

    tax_id= tax1_getTaxIdByOrgRef(inp_orgRef);
    if(tax_id <= 0) return -1;

    if(tax_id != storedTaxId(inp_orgRef)) return 1;
    
    db_orgRef= s_tax1_getOrgRef(tax_id, NULL, NULL, NULL /*res->embl_code*/);
    if(db_orgRef == NULL) {
        return -2;
    }

    return OrgRefCmp(inp_orgRef, db_orgRef);
}

Boolean tax1_isAlive(void)
{
    if(!tax1_inited()) return FALSE;
    return (tax1_getTaxIdByName("dog") > 1)? TRUE : FALSE;
}

/***************************************************
 * Get tax_id for given gi
 * returns:
 *       tax_id if found
 *       0      if no data or error
 */
Int4 tax1_getTaxId4GI(Int4 gi)
{
    return tax_getTaxId4GI(gi);
}

/***************************************************
 * Get pointer to "blast" name
 * Returns: the pointer on first blast name at or above this node in the lineage
 * NOTE:
 * This function does not make a copy of "blast" name, so, caller can not use
 * MemFree function for returned pointer.
 */
CharPtr tax1m_getBlastName(Int4 tax_id)
{
    CharPtr res= NULL;
    TreeCursorPtr cursor= tree_openCursor(tax_tree, NULL, NULL);

    if(tc2_toNode(cursor, tax_id)) {
        TXC_TreeNodePtr tnp;
        Uint2 s;
        do{
            tnp= tree_getNodeData(cursor, &s);
            if((tnp != NULL) && ((tnp->flags & TXC_STHIDE) == 0)) { /* we do have a blast name here */
                res= tnp->node_label + (StringLen(tnp->node_label) + 1);
                break;
            }
        }
        while(tree_parent(cursor));
    }

    tree_closeCursor(cursor);
    return res;
}

Boolean tax1_inited()
{
    return (tax_tree != NULL)? TRUE : FALSE;
}

static char* next_token(char* s)
{
    if(s == NULL) return NULL;

    while(*s && (isspace(*s) || iscntrl(*s))) {
        ++s;
    }
    
    if(*s == '\0') return NULL;
    else {
        char last;
        char first= *s;
        int j= 0;

        switch(first) {
        case '"': last= '"'; break;
        case '(': last= ')'; break;
        case '{': last= '}'; break;
        case '[': last= ']'; break;
        default:  last= '\0';
        }

        for(++s; *s != '\0'; s++) {
            if(!isalnum(*s)) {
                if(last) {
                    if(first == *s && first != '"') ++j;
                    if(last == *s && (!j--)) {
                        ++s;
                        break;
                    }
                }
                else {
                    if(*s == '.' || isspace(*s) || iscntrl(*s)) {
                        ++s;
                        break;
                    }
                }
            }
        }
            
    }

    return s;
}


static int nof_tokens(char* s)
{
    int n;
    char* nt= s;

    for(n= 0; (nt= next_token(nt)) != NULL; n++);

    return n;
}