File: ParsInsert.cpp

package info (click to toggle)
parsinsert 1.04-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 456 kB
  • ctags: 589
  • sloc: cpp: 6,519; makefile: 95
file content (3099 lines) | stat: -rw-r--r-- 99,974 bytes parent folder | download | duplicates (3)
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
/////////////////////////////////////////////////////////////////////////////////////////////////// 
//  File      : ParsInsert.cpp
//  Purpose   : Parsimonious Insertion of unclassified seqeunces into Phylogenetic Tree
//
//  Developer : David Knox (david.knox@colorado.edu) Jan 2011
//  Copyright : Copyright (C) 2007-2011 David Knox
//
//  Web site  : http://parsinsert.sourceforge.net/
//
/////////////////////////////////////////////////////////////////////////////////////////////////// 
//	  This file is part of ParsInsert.
//
//    ParsInsert is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    ParsInsert is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with ParsInsert.  If not, see <http://www.gnu.org/licenses/>.
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Sample command for parsimonious insertion:
//		-x rdp.taxonomy -t core_set.tree -s core_set_aligned.fasta unclassified_seq.fasta  
//
//
///////////////////////////////////////////////////////////////////////////////////////////////////

#include "Knox_Stddef.h"
#include "ParsInsert.h"
#include "PNode.h"
#include "SeqList.h"
#include "Taxonomy.h"
#include "Attrs.h"

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

///////////////////////////////////////////////////////////////////////////////////////////////////

#define APPNAME						"ParsInsert"
#define VERSION						APPNAME " Version 1.04 " __DATE__ " " __TIME__
#define COPYRIGHT \
"ParsInsert: Parsimonious Insertion of unclassified sequences into phylogenetic tree\n"\
"            Copyright (C) 2007-2011 David Knox\n"\
"            This program comes with ABSOLUTELY NO WARRANTY\n"\
"            This is free software, and you are welcome to redistribute under certain conditions - see license."

#define USAGE	"Application:%s\n\n%s [options]  <insert_sequences>      \n"    \
				"      Parsimonious Insertion of Sequences into Given Tree                  \n\n"  \
				"     -m <mask file>           - read mask from this file                   \n"    \
                "     -s <tree sequences>      - read core tree sequences from this file    \n"    \
                "                                (default: PI_Tree.fasta)                   \n"    \
                "     -t <tree file>           - read core tree from this file              \n"    \
                "                                (default: PI_Tree.tree)                    \n"    \
                "     -x <tree taxonomy>       - read core tree taxomony from this file     \n"    \
                "     -o <output file>         - output taxonomy for each insert sequence to this file\n"    \
                "                                (default: PI_Results.log)                  \n"    \
				"     -l[-|<log file>]         - create log file (default is ParsInsert.log)\n"    \
				"     -n#                      - number of best matches to display          \n"    \
				"     -c#                      - percent threshold cutoff                   \n"    \
				"     -p                       - print node comments in newick file         \n"    \
				"     -D#                      - print branch lengths using # decimal places\n"    \
				"\n"  
//              "     -p <parsimony file>      - read parsimony values from this file       \n"    

///////////////////////////////////////////////////////////////////////////////////////////////////

FILE				*newseqs      = NULL;			// sequence file of unclassified insertions

BOOL				createLog     = TRUE;
int					fast		  = 0;

int                 nCalcCost     = 0;				// counters for cost calc stats
int                 nCalcCostFull = 0;				// counters for cost calc stats
BOOL                useCalcCost   = 4;				// calculation method to be used

char                *statsName      = "PI_Results.log";
char                *logFilename    = "ParsInsert.log";
char                *treeFilename   = "PI_Tree.tree";
char                *seqFilename    = "PI_Tree.fasta";
char                *fullTreeName   = NULL;

char                *files[64]      = {NULL};
int                 nfiles          = 0;

BOOL                precision       = FALSE;
int					scoreThresh 	= 80; 			// default to 80% or better to add item	

///////////////////////////////////////////////////////////////////////////////////////////////////

FILE				*stats          = NULL;
time_t              start           = 0;

long long			allocatedMemory = 0;

CSequenceFile		*treeSeqfile   = NULL;
CSequenceFile		*insertSeqfile = NULL;
CPTree              *fullTree      = NULL;

// Variables used during the parsimony calculation
//
volatile int		nSeqRemaining   = 0;    // used to show progress during parsimony calculation
CParsimonyList      parsimonyList;          // parsimonious sequence for common ancestors
CParsimonyList      unionList;              // union of all possible nucleotides at each position
CSequenceFile		*taxonomy       = NULL;
CPTree              *tree           = NULL;
char                *taxName        = NULL;
char                *maskName       = NULL;
char                *parsName       = NULL;

map<string,CPNode*>	coreNames;

CInsertPosArray		inserts;

char				mask[MAX_SEQ_SIZE];
bool				useMask         = FALSE;

int					testing         = 1;
BOOL                fitch           = TRUE;
BOOL                jukes_cantor    = TRUE;
BOOL				withComments	= FALSE;

///////////////////////////////////////////////////////////////////////////////////////////////////

enum				{RANK_DOMAIN, RANK_PHYLUM, RANK_CLASS, RANK_ORDER, RANK_FAMILY, RANK_GENUS, RANK_SPECIES, RANK_N};
int					rankCounts[RANK_N][4];
LPCSTR				rankNames[] = {	"Domain", 
									"Phylum", 
									"Class", 
                                    "Order",
									"Family", 
									"Genus", 
									"Species", 
									NULL};

class CRankCounts
    {
    public:
        int					rankCounts[RANK_N][4];
    };
    
typedef map<string,CRankCounts*>	CMapRankCounts;
CMapRankCounts      				phylumRankCounts;

///////////////////////////////////////////////////////////////////////////////////////////////////

typedef	vector<CInsertPos*>				CTreeInsertList;
typedef map<CPNode*,CTreeInsertList*>	CTreeInserts;

///////////////////////////////////////////////////////////////////////////////////////////////////

CSequenceFile*	ReadSequenceFile(LPCSTR filename);
CSequenceFile*	ReadTaxonomyFile(LPCSTR filename);
LPCSTR			GetTaxonomy(LPCSTR name, CPNode *node=NULL);

CPTree*			ReadNewickTree(LPCSTR filename);
int				LoadParsimonyIndex();			// loads a parsimony index
int				LoadSequenceIndex();			// loads a sequence index
int				BuildSequenceIndex();			// build seq index from sequence file

BOOL			RemoveUnwantedNodes(CPNode *node, CSequenceFile *seqfile);
BOOL			SetInternalTaxonomy(CPTree *tree);
BOOL            SetMyTaxonomy(CPNode *node);
BOOL            CheckLeafTaxonomy(CPTree *tree);

int             CheckTreeSequences(CPTree *tree, CSequenceFile *treeSeqfile);
int             ParsimonyInsertion();

int             CreateParsimonySets(LPCSTR parsName, CPTree *tree);
int				CalcAncestorParsimony(CPTree *tree);	// Build parsimony values (write to file)
int				CalcNodeParsimony(CPNode *node);
int             ForceNodeParsimony(CPNode *node);
int             WriteParsimonySet(CPNode *node, CSequenceFile *parsimony);

void			FindInsertLocations(CSequenceFile *insertSeqfile);
void			FindInsertLocations(CSequenceFile *treeSeqfile, CPTree *tree, CSequenceFile *insertSeqfile, LPCSTR seqname);
void			FindInsertLocations(CSequenceFile *treeSeqfile, CPTree *tree, CSequenceFile *insertSeqfile, CInsertPosArray &inserts);
void			FindInsertLocations_Serial(CSequenceFile *treeSeqfile, CPTree *tree, CSequenceFile *insertSeqfile, CInsertPosArray &inserts);

int				CalcCost(CParsimonySet &next_pars, CParsimonySet &node_pars, int &diffs, int &partials, int &indel, int thresh=INT_MAX);
int				CalcCost_NEW4(CParsimonySet &next_pars, CParsimonySet &node_pars, int &diffs, int &partials, int &indel, int thresh=INT_MAX);
int				CalcCost_ORIG(CParsimonySet &next_pars, CParsimonySet &node_pars, int &diffs, int &partials, int &indel, int thresh=INT_MAX);
void            BuildScoreMatrix();

void			DisplayInsertLocations(CInsertPosArray &inserts, BOOL all);
void            DisplayBestInsertLocations(CInsertPos *item, BOOL all);
void			OutputInsertLocations(CInsertPosArray &inserts, BOOL all);
void 			DisplayRankCounts(LPCSTR header, int counts[][4]);
void			ReleaseInserts(CInsertPosArray &inserts);
CParsimonySet* 	GetParsimonySet(CPNode *node, BOOL create=FALSE);
int 			GetTaxonomyMatch(string &taxA, string &taxB, string &marker, int counts[][4]);
void            GetRankFromTax(int rank, string &tax, string &rankStr, BOOL only=TRUE);

CTreeInserts*	BuildTreeInsertLists(CInsertPosArray &inserts);
BOOL			BuildInsertTree(CTreeInserts* insertLists, CPTree *tree);
CPNode* 		AddChild(CPTree *tree, CPNode *node, CInsertPos *in);
double 			CalcDist(CPNode *parent, CInsertPos* in, int &diffs, int &parts, int &indel);

BOOL			BuildInsertTree(CInsertPosArray &inserts, CPTree *tree);
BOOL			AddTaxonomyToTree(CPTree *tree, CSequenceFile *taxonomy);

BOOL			ReadMask(LPCSTR filename);

void			CompareTaxonomy(LPCSTR tax1, LPCSTR tax2);

void            TestInsertPositions();
BOOL            FullTree_Initialization();
double          ComparePosition(LPCSTR name, CPNode *parent, CPTree *fullTree, CPTree *coreTree);

///////////////////////////////////////////////////////////////////////////////////////////////////

inline LPCSTR GetNodeName(CPNode *node)
    {
    string		name = node->attrs.GetString(ATTR_NAME,"");
    
    if (name.empty()) 
        name = node->title;
    
    return name.c_str();
    }


///////////////////////////////////////////////////////////////////////////////////////////////////
// The one and only application object

int main(int argc, char* argv[])
	{
	int nRetCode = 0;

	memset(rankCounts, 0, sizeof(rankCounts));

	DisplayL("%s\n", COPYRIGHT);

	int i = 1;
	while (i < argc)
		{
        if (argv[i][0] == '-')
            {
            // Process argument
            switch (argv[i][1])
                {
                case 'c': // cutoff threshold percent
                    {
                    int v;
                    if (argv[i][2] != 0)
                        v = atoi(&argv[i][2]);
                    else if (i < argc)
                        v = atoi(argv[++i]);
                    scoreThresh = v;
                    }
                    break;
                        
                case 'd': // distance calc method
                    {
                    int v;
                    if (argv[i][2] != 0)
                        v = atoi(&argv[i][2]);
                    else if (i < argc)
                        v = atoi(argv[++i]);
                    useCalcCost = v;
                    }
                    break;
                        
                case 'D': // distance calc method
                    {
                    int v;
                    if (argv[i][2] != 0)
                        v = atoi(&argv[i][2]);
                    else if (i < argc)
                        v = atoi(argv[++i]);
                    if (v >= 0)
                    	{
                    	DisplayL("Setting Tree Floating Point Precision to %d\n", v);
                    	CPTree::PRECISION = v;
                    	}
                    }
                    break;
                        
                case 'f':
                    {
                    int v = atoi(&argv[i][2]);
                    fast = v;
                    }
                    break;
                    
                case 'F':
                    {
                    fitch = (argv[i][2] != '-');
                    }
                    break;

                case 'j':
                    {
                    jukes_cantor = (argv[i][2] != '-');
                    }
                    break;
                        
                case 'l':
                    createLog = (argv[i][2] != '-');
                    if (argv[i][2] != 0)
                        logFilename = &argv[i][2];
                    else if ((i < argc) && (argv[i][2] != '-'))
                        logFilename = argv[++i];
                    break;

                case 'n': // number of matches to display
                    {
                    int v = atoi(&argv[i][2]);
                    if (v > 0)
                        CBestLocation::default_len = v;
                    }
                    break;
                    
                case 'm': // mask input file 
                    {
                    if (argv[i][2] != 0)
                        maskName = &argv[i][2];
                    else if (i < argc)
                        maskName = argv[++i];
                    }
                    break;
                    
                case 'o': // output file 
                    {
                    if (argv[i][2] != 0)
                       statsName = &argv[i][2];
                    else if (i < argc)
                       statsName = argv[++i];
                    }
                    break;
                        
                        
                case 'p':
                    {
                    withComments = (argv[i][2] != '-');
                    }
                    break;

                case 'P':
                    {
                    precision = (argv[i][2] != '-');
                    }
                    break;

                case 's': // tree sequence file 
                    {
                    if (argv[i][2] != 0)
                       seqFilename = &argv[i][2];
                    else if (i < argc)
                       seqFilename = argv[++i];
                    }
                    break;
                        
                case 't': // tree file 
                    {
                    if (argv[i][2] != 0)
                       treeFilename = &argv[i][2];
                    else if (i < argc)
                       treeFilename = argv[++i];
                    }
                    break;
                                                
                case 'x': // taxonomy input file 
                    {
                    if (argv[i][2] != 0)
                        taxName = &argv[i][2];
                    else if (i < argc)
                        taxName = argv[++i];
                    }
                    break;
                       
                default:
                    DisplayL("invalid command option '%c'\n", argv[i][1]);
                    nRetCode = RC_ERROR_OPTIONS;
                    break;
                }
            }
        else
            {
            files[nfiles++] = argv[i];
            }
        i++;
        }

	if (createLog)
		OpenAppLog(logFilename, "a+");

	// Write out our version and the command line
	string			cmd;
	for (int k=0 ; k < argc ; ++k)
		{
		if (!cmd.empty())
			cmd += " ";
		cmd += argv[k];
		}
	DisplayL("%s\nCommand Line:[%s]\n", VERSION, cmd.c_str());
    
    if (nfiles < 1)
        {
        DisplayL("ERROR - missing files to process\n");
        DisplayT(USAGE, VERSION, APPNAME);
        nRetCode = RC_ERROR_CMDLINE;
        }

	start = time(NULL);

	if (treeFilename != NULL)
		{
		DisplayL(" tree file: %s\n", treeFilename);
		}
		
	if (seqFilename != NULL)
		{
		DisplayL(" tree sequence file: %s\n", seqFilename);
		}
		
	if (maskName != NULL)
		{
		DisplayL(" mask file: %s\n", maskName);
		
		if (!ReadMask(maskName))
			{
			DisplayT("Cannot open mask file [%s]", maskName);
			nRetCode = RC_ERROR_MASKFILE;
			}

		useMask = TRUE;
		}

	if (taxName != NULL)
		{
		DisplayL(" taxomony index file: %s\n", taxName);
		taxonomy = ReadTaxonomyFile(taxName);
		if (taxonomy->f == NULL)
			{
			DisplayT("Cannot open taxomony index FASTA file [%s]", taxName);
			nRetCode = RC_ERROR_TAXFILE;
			}
        else
            DisplayL(" taxomony index file: %d taxa\n", taxonomy->GetCount());
		}

    BuildScoreMatrix();
        
    if (nRetCode == 0)
        {
        nRetCode = ParsimonyInsertion();
        }
        
	// release the data
        
    if (treeSeqfile != NULL)
        delete treeSeqfile;
    
    if (insertSeqfile != NULL)
        delete insertSeqfile;
    
    if (tree != NULL)
        delete tree;
    
    if (stats != NULL)
        fclose(stats);
        
	if (taxonomy != NULL)
		delete taxonomy;

///	ReleaseInserts(inserts);

	DisplayL("Process Completed: %d sec\n", time(NULL)-start);
	CloseAppLog();

	return nRetCode;
	}


///////////////////////////////////////////////////////////////////////////////////////////////////

int ParsimonyInsertion()
    {
    int             ret = 0;
    time_t			stage;

    // Algorithm
    /*
     1.	If sequence index available
     load sequence index
     else
     create sequence index from sequence FASTA file
     
     2.	Read Tree file
     
     3.	If parsimony index available
     load parsimony index
     else if parsimony FASTA file availble
     create parsimony index from parsimony FASTA file
     else
     Create parsimony FASTA file and index file
     
     4.	If new sequence index available
     Load new sequence index
     else 
     Create new sequence index from new sequence FASTA file
     
     5.	for each group of new sequences that fit into memory
     for each node in tree
     for each seq in group
     calc score to convert node into seq
     Add score,node pair to best for seq
     Keep top K scores
     for each seq in group
     write top scoring positions and Taxonomy
     */

    stats  = fopen(statsName, "w");

    // Read Tree File
    DisplayL("Reading Newick Tree: [%s]\n", treeFilename);
    stage = time(NULL);
    tree = ReadNewickTree(treeFilename);
    if (tree == NULL)
        {
        DisplayL("Error accessing Newick Tree file [%s]\n", treeFilename);
        return RC_ERROR_TREEFILE;
        }
    else
        DisplayL("Newick Tree Read Completed: %d taxa, %d sec\n", (tree->nodeList.size()+1)/2, time(NULL)-stage);

	// Build list of core leaf names
	CPNodeList		&coreNodes = tree->nodeList;
	for (CPNodeListIter iter=coreNodes.begin() ; iter != coreNodes.end() ; ++iter)
		{
		CPNode		*node = *iter;
		string 		name = GetNodeName(node);
		if (!node->IsBranchNode())
			{
			if (coreNames.find(name) != coreNames.end())
				DisplayL("Found duplicate name: [%s]/n", name.c_str());
			coreNames[name] = node;
			}
		}
		
    // Read Sequence File
    DisplayL("Tree Sequence Reading: [%s]\n", seqFilename);
    stage = time(NULL);
    treeSeqfile = ReadSequenceFile(seqFilename);
    ret = CheckTreeSequences(tree, treeSeqfile);
    if (ret != 0)
        return ret;
    DisplayL("Tree Sequence Read Completed: %d sequences, %d sec\n", treeSeqfile->seqlist.size(), time(NULL)-stage);
    
    // read sequences to be inserted
    DisplayL("Reading sequences for insertion: [%s]\n", files[0]);
    stage = time(NULL);
    insertSeqfile = new CSequenceFile(files[0], 0);
    insertSeqfile->ReadSequenceFile(1);
    if (insertSeqfile->GetCount() == 0)
        {
        DisplayL("No insertion sequences found\n");
        return RC_ERROR_NO_INSERT_SEQ;
        }
    DisplayL("Insert Sequecne Read Completed: %d sequences, %d sec\n", insertSeqfile->GetCount(), time(NULL)-stage);
        
    ///			Only calc the ones actually needed
    DisplayL("Checking Taxa Taxonomy\n");
    CheckLeafTaxonomy(tree);
    DisplayL("Setting Internal Taxonomy\n");
    stage = time(NULL);
    SetInternalTaxonomy(tree);
    DisplayL("Internal Taxonomy Completed: %d sec\n", time(NULL)-stage);
    
        // Set parsimony either from file or building from sequences
    DisplayL("Setting internal parsimony sequences ...\n");
    stage = time(NULL);
    CreateParsimonySets(parsName, tree);
    DisplayL("Parsimony Calculation Completed: %d sec\n", time(NULL)-stage);

    if (fullTreeName != NULL)
        FullTree_Initialization();

    DisplayL("[%d sec elapsed] Searching for insertion points:\n", time(NULL)-start);
    //DisplayL("[%d fast]\n", fast);
    if (useCalcCost == 4)
        {
        CPNodeList		&nodes = tree->nodeList;
        for (CPNodeListIter iter=nodes.begin() ; iter != nodes.end() ; ++iter)
            {
            CPNode          *node = *iter;
            CParsimonySet	*node_pars  = GetParsimonySet(node);
            
            if (node_pars->segCounts == NULL)
                node_pars->BuildSegCounts((useMask ? mask : NULL));
            }
        }
    stage = time(NULL);
    switch (fast)
        {
        case 0:
            {
            FindInsertLocations_Serial(treeSeqfile, tree, insertSeqfile, inserts);
            }
            break;
        default:
            {
            FindInsertLocations(treeSeqfile, tree, insertSeqfile, inserts);
            }
            break;
        }                   
    DisplayL("[%d sec elapsed] Insert Locations Completed: %d sec\n", time(NULL)-start, time(NULL)-stage);
    time_t  insertTime = time(NULL)-stage;

    ///    DisplayInsertLocations(inserts, TRUE);
    memset(rankCounts, 0, sizeof(rankCounts));
    OutputInsertLocations(inserts, FALSE);

    // Place inserts into tree
    AddTaxonomyToTree(tree, taxonomy);
    //tree->WriteNewickTree("AfterAddTax.tree", tree->root, TRUE);

    CTreeInserts		*insertmap = BuildTreeInsertLists(inserts);
    BuildInsertTree(insertmap, tree);

    string			treename = insertSeqfile->fname;
    int				n = treename.find_last_of('.');
    if (n > 0)
        treename = treename.substr(0, n) + ".tree";
    DisplayL("Writing Newick tree file: [%s]\n", treename.c_str());
    tree->WriteNewickTree(treename.c_str(), tree->root, withComments);

    DisplayRankCounts("Rank Matches:", rankCounts);
    
    if (testing > 2)
	    DisplayL("CalcCost calls[%d] = %d / %d (%d)\n", useCalcCost, nCalcCostFull, nCalcCost,nCalcCost-nCalcCostFull);
	if (insertTime > 0)
	    DisplayL("Insert Time = %d, %d per hour\n", insertTime, 3600 * inserts.size() / insertTime );

	if (testing > 2)
		{
		// display the rank counts for each phylum
		CMapRankCounts::iterator   iter;
		for (iter=phylumRankCounts.begin() ; iter != phylumRankCounts.end() ; ++iter)
			{
			string      phy  = iter->first;
			DisplayRankCounts(phy.c_str(), (iter->second)->rankCounts);
			}
		}
		
    return ret;
    }

///////////////////////////////////////////////////////////////////////////////////////////////////

int CheckTreeSequences(CPTree *tree, CSequenceFile *treeSeqfile)
    {
    // Check to see that we have a sequence for each leaf of the tree
    int         ret = 0;
    
    if (treeSeqfile->seqlist.size() == 0)
        {
        DisplayL("Error accessing Tree Sequencefile [%s]\n", treeSeqfile->fname.c_str());
        ret = RC_ERROR_TREESEQ;
        }
        
    CPNodeList		&nodes = tree->nodeList;
    for (CPNodeListIter iter=nodes.begin() ; iter != nodes.end() ; ++iter)
        {
        CPNode		*node = *iter;
                
        if (node == NULL) continue;
        if (node->IsBranchNode()) continue;
            
        // look up the sequence name in the sequence file
        string		name = GetNodeName(node);
        
        // get sequence from the sequence file
        CSequenceItem *seq = treeSeqfile->GetSeq(name.c_str());
        if ((seq == NULL) || (seq->len == 0))
            {
            DisplayL("ERROR - tree member sequence is not available [%s]\n", name.c_str());
            ret = RC_ERROR_TREESEQ_MISSING;
            }
        }
           
    return ret;
    }
    
///////////////////////////////////////////////////////////////////////////////////////////////////

int CreateParsimonySets(LPCSTR parsName, CPTree *tree)
    {
    // Create parsimony sets from FASTA file
    //
    if (parsName != NULL)
        {
        CSequenceFile		*parsimony    = NULL;
        
        DisplayL(" parsimony index file: %s\n", parsName);
        parsimony = ReadSequenceFile(parsName);
        if (parsimony->f == NULL)
            {
            DisplayT("Cannot open parsimony index FASTA file [%s]", parsName);
            return -10;
            }
        DisplayL(" parsimony file: %d internal nodes\n", parsimony->GetCount());
        
        // loop creating the parsimony sets for each node (leaf and internal)
        parsimony->ResetSeqIterator();
        CSequenceItem	*seq = parsimony->GetNextSeq();
        while (seq != NULL)
            {                            
            seq->ReadSeqData(parsimony->f);
            
            CParsimonySet		*node_pars = NULL;
            
            node_pars = (CParsimonySet*)parsimonyList[seq->name];
            if (node_pars == NULL)
                {
                // does not exist, create new entry
                //	DisplayL("Create new CParsimonySet for %s\n", name.c_str());
                
                node_pars = new CParsimonySet();
                node_pars->Convert(seq->data);
                char    str[100*1024];
                node_pars->BuildSets(str,sizeof(str));
                node_pars->Trace(seq->name.c_str(), str);
                parsimonyList[seq->name] = node_pars;
                }
            else
                node_pars->Convert(seq->data);
            
            seq->ReleaseSeqData();
            seq = parsimony->GetNextSeq();
            }
        
        if (parsimony != NULL)
            delete parsimony;
        }
    else
        {
        // create parsimony for tree from leaf sequences
        DisplayL("Calculating internal parsimony sequences ...\n");
        CalcAncestorParsimony(tree);
        }
        
    return 0;
    }
            
///////////////////////////////////////////////////////////////////////////////////////////////////

CSequenceFile*	ReadTaxonomyFile(LPCSTR filename)
	{
	CSequenceFile	*seqfile = new CSequenceFile();

	string			fname = filename;
	if (fname.substr(fname.length()-4) == ".idx")
		{
		// read this index file
		seqfile->ReadSequenceIndexFile(filename);

		fname = fname.substr(0, fname.length()-4);
		seqfile->Open(fname.c_str(), "r");
		}
	else
		{
		// read the file and create index
		seqfile->Open(filename);
		seqfile->ReadTaxonomyFile();	
		}

	return seqfile;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

CSequenceFile*	ReadSequenceFile(LPCSTR filename)
	{
	CSequenceFile	*seqfile = new CSequenceFile();

	string			fname = filename;
	if (fname.substr(fname.length()-4) == ".idx")
		{
		// read this index file
		seqfile->ReadSequenceIndexFile(filename);

		fname = fname.substr(0, fname.length()-4);
		seqfile->Open(fname.c_str(), "r");
		}
	else
		{
		// read the file and create index
		seqfile->Open(filename);
		seqfile->ReadSequenceFile(1);	
		}

	return seqfile;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

BOOL ReadMask(LPCSTR filename)
	{
	if (filename == NULL) return FALSE;

	FILE			*f = fopen(filename, "r");
	if (f == NULL) return FALSE;

	memset(mask, 0, sizeof(mask));
	int				m = 0;

	char			buffer[MAX_SEQ_SIZE];
	while (ReadNextLine(buffer, sizeof(buffer), f))
		{
		for (int i=0 ; (i < strlen(buffer)) && (m < sizeof(mask)) ; ++i)
			{
            switch (buffer[i])
                {
                case '0':
                case '1':
                    mask[m] = buffer[i] - '0';
                    break;
                
                case '.':
                case '-':
                    mask[m] = 0;
                    break;
            
                default:
                    mask[m] = 1;
                    break;
                }
            ++m;
			}
		}

	return TRUE;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

CPTree*	ReadNewickTree(LPCSTR filename)
	{
	TRACE("opening source file: [%s]\n", filename);
	
	FILE				*f = fopen(filename, "r");
	if (f == NULL)	
		{
		Display("Could not open source file: [%s]\n", filename);
		return NULL;
		}

	static		char		newick[50*1024*1024];	// should get the file size for the buffer
	int			len=0;

	CPTree *newickTree = new CPTree();

	DisplayT("Opening file: %s\t...", filename);

	if (f != NULL)
		{
		char buffer[32*1024];
		newickTree->filename = filename;
		//newickTree->ReadTreeAttrs(); // get defaults

		memset(newick, 0, sizeof(newick));
		int			maxlinelen = 0;
		int			linecount  = 0;
		while (ReadNextLine(buffer, sizeof(buffer), f) > 0)
			{
			if (buffer[0] == '#') continue;
			int blen = strlen(buffer);
			// strip the leading whitespace
			int			skip;
			for (skip=0 ; (skip < blen) && isspace(buffer[skip]) ; ++skip)
				; // skip whitespace
			LPCSTR buf = buffer + skip;
			memcpy(&newick[len], buf, blen-skip);
			len += blen-skip;
			//ASSERT(len < 50*1024*1024);
			maxlinelen = max(maxlinelen, blen);
			}

		if (++linecount%100 == 0)
			Display("Opening file: %s\tLines:%d", filename, linecount);

		fclose(f);
		}

	DisplayT("Parsing Newick file: %s\t...", filename);
	newickTree->Parse(newick);

	return newickTree;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

BOOL CheckLeafTaxonomy(CPTree *tree)
    {
	// calc the taxonomy of the tree
	if (taxonomy == NULL) return FALSE;
	
	CPNodeList		missing;
    int             n = 0;
	CPNodeList		&nodes = tree->nodeList;
	for (CPNodeListIter iter=nodes.begin() ; iter != nodes.end() ; ++iter)
		{
        CPNode		*node = *iter;
        string      tax;
        
        if (node == NULL) continue;
        if (node->IsBranchNode()) continue;
        
        // check for taxonomy
        CSequenceItem *seq = taxonomy->GetSequence(node->title.c_str());
        if (seq != NULL)
            {
            tax = seq->hdr;
            Trim(tax, "\n\r\t ");
            // find first \t rest of line is taxonomy
            int pos = tax.find_first_of('\t');
            if (pos >= 0)
                tax = tax.substr(pos+1,tax.length());
            seq->ReleaseSeqData();
            }
                  
        if (tax.empty())
            {
            //DisplayL("\tNO TAXONOMY for [%s]\n", node->title.c_str());
            missing.push_back(node);
            ++n;
            }
        }
        
    if (n != 0)
        {
        DisplayL("%d of %d nodes are missing taxonomy\n\t", n, nodes.size());
  
		int			count = 0;
	    for (CPNodeListIter iter=missing.begin() ; iter != missing.end() ; ++iter)
			{
   			CPNode		*node = *iter;
   	   		DisplayL(" %-12.12s%s", node->title.c_str(), (++count%8 == 0 ? "\n\t" :""));
   		    }
        DisplayL("\n");
		}
		
    return (n == 0);
    }
            
///////////////////////////////////////////////////////////////////////////////////////////////////

BOOL SetInternalTaxonomy(CPTree *tree)
	{
	// calc the taxonomy of the tree
	if (taxonomy == NULL) return FALSE;
	
    CTaxEntry		taxEntries("Tree Taxonomy");	// keeps track of diversity seen in taxonomy of tree sequences
    
	nSeqRemaining = tree->nodeList.size()/2;

	CPNodeList		&nodes = tree->nodeList;
	for (CPNodeListIter iter=nodes.begin() ; iter != nodes.end() ; ++iter)
		{
		CPNode		*node = *iter;

		if (node == NULL) continue;
		if (!node->IsBranchNode()) 
            {
            CSequenceItem *seq = taxonomy->GetSequence(node->title.c_str());
                if (seq != NULL)
                    {
                    string tax = seq->hdr;
                    Trim(tax, "\n\r\t ");
                    // find first \t rest of line is taxonomy
                    int pos = tax.find_first_of('\t');
                    if (pos >= 0)
                        tax = tax.substr(pos+1,tax.length());
                    seq->ReleaseSeqData();
                    taxEntries.Add(tax.c_str());	// update diversity
                    }
            continue;
            }

		CPNodeList		descent;
		node->GetDecendants(descent, TRUE);

		map<string,int>				commonTax;
		map<string,int>::iterator	cIter;
		string						common;
		string						tax;
		int							nDescent = 0;

//        DisplayL("Taxonomy for [%s]\n", node->title.c_str());
		for (CPNodeListIter diter=descent.begin() ; diter != descent.end() ; ++diter)
			{
			CPNode		*des = *diter;
			if (des == NULL) continue;

            tax.clear();
			CSequenceItem *seq = taxonomy->GetSequence(des->title.c_str());
			if (seq != NULL)
				{
				tax = seq->hdr;
				Trim(tax, "\n\r\t ");
				// find first \t rest of line is taxonomy
				int pos = tax.find_first_of('\t');
				if (pos >= 0)
					tax = tax.substr(pos+1,tax.length());
				seq->ReleaseSeqData();
                }

			// add each part of the taxonomy to the common list
			int				p;
			string			entry;
			
			entry = tax;
			p = commonTax[entry];
			commonTax[entry] = p+1;
			
			for (int i=0 ; i < tax.length() ; ++i)
				{
				string			entry = tax;
				if ((tax[i] == ';') || (tax[i] == '/'))
					{
					entry = tax.substr(0, i);
					p = commonTax[entry];
					commonTax[entry] = p+1;
					}
				}
				
			if (!tax.empty())
				++nDescent;
			}

		// find the longest, most trusted value
		int			commonThresh = 50 * nDescent / 100;  // 60% of set descendents are required to select value
		int			commonBest = 0;  // best value seen
		common = "";
		for (cIter=commonTax.begin() ; cIter != commonTax.end() ; ++cIter)
			{
			string		key = (*cIter).first;
			int			v = (*cIter).second;
			if ((v >= commonThresh) && (v > commonBest*90/100) && (common.length() < key.length()))
				{
				common = key;
				commonBest = v;
				}
			}

		// DEBUGGING code for assignment of taxonomy for internal branch
//        DisplayL("\tAssigning: [%s]\n\n", common.c_str());
//        if (common.empty())
//            {
//            DisplayL("Taxonomy for [%s]\n", node->title.c_str());
//            for (CPNodeListIter diter=descent.begin() ; diter != descent.end() ; ++diter)
 //               {
//                CPNode		*des = *diter;
//                if (des == NULL) continue;
//                
//                CSequenceItem *seq = taxonomy->GetSequence(des->title.c_str());
//                if (seq != NULL)
//                    {
//                    tax = seq->hdr;
//                    Trim(tax, "\n\r\t ");
//                    // find first \t rest of line is taxonomy
//                    int pos = tax.find_first_of('\t');
//                    if (pos >= 0)
//                        tax = tax.substr(pos+1,tax.length());
//                    seq->ReleaseSeqData();
//                    }
//                DisplayL("\t[%s]\t[%s]\n", des->title.c_str(), tax.c_str());
//                }
//            }

        node->attrs.Add(ATTR_TAXLABEL, common);
	//	DisplayL("Setting Internal Taxonomy: %s [%s]\n", node->title.c_str(), common.c_str());
		if (--nSeqRemaining%1000 == 0)
			DisplayT("\rSetting Internal Taxonomy %d nodes remaining     ", nSeqRemaining);
		
		LPSTR		data = (LPSTR)malloc(common.length()+1);
		strcpy(data, common.c_str());
		CSequenceItem		*seq = new CSequenceItem(node->title.c_str(), -1, common.length(), data);
		taxonomy->seqlist[seq->name] = seq;
		}

	DisplayT("\n");
    taxEntries.Display(" ", 3);
	return TRUE;
	}


///////////////////////////////////////////////////////////////////////////////////////////////////

void FindInsertLocations_Serial(CSequenceFile *treeSeqfile, CPTree *tree, CSequenceFile *insertSeqfile, CInsertPosArray &inserts)
	{
	// for each node in the tree
	//		for each seq for insertion
	//			calc cost

	time_t				start = time(NULL);

	// build insertion list
	insertSeqfile->ResetSeqIterator();
	CSequenceItem		*seq = insertSeqfile->GetNextSeq();
	while (seq != NULL)
		{
		CSequenceItem	*iseq = insertSeqfile->GetSequence(seq->name.c_str());

		// Check to see if this leaf is in the core tree
		if (coreNames.find(seq->name.c_str()) != coreNames.end())
			{
			DisplayL("**** Sequence is already in core tree: %s\n", seq->name.c_str());
			seq = insertSeqfile->GetNextSeq();		// get next sequence
			continue;
			}

		if (iseq == NULL)
			{
			DisplayL("**** Cannot find sequence for %s\n", seq->name.c_str());
			seq = insertSeqfile->GetNextSeq();		// get next sequence
			continue;
			}

		// check that we have the taxonomy
		string				tax;
		if (taxonomy != NULL)
			tax = GetTaxonomy(seq->name.c_str());
			
		CInsertPos		*item = new CInsertPos();
		item->seq = iseq;
		item->pars.Convert(iseq->GetSeqData());

		for (int i=0 ; i < item->pars.len ; ++i)
			{
			if (item->pars.data[i] != 0)
				{
				if (useMask && (mask[i] != 0)) 
					++item->nSites;
				else if (!useMask)
					++item->nSites;

				item->pars.end = i;
				if (item->pars.start == -1)
					item->pars.start = i;
				}
			}

        item->pars.BuildSegCounts((useMask ? mask : NULL));

		if (item->nSites < 1000)
			TRACE("SHORT");

		DisplayL("Insert [%20s](len=%d) [%5d sites] (%d - %d) [%s]\n", 
						item->seq->name.c_str(), seq->len, 
						item->nSites, 
						item->pars.start, item->pars.end,
						tax.c_str());
		inserts.push_back(item);

		seq = insertSeqfile->GetNextSeq();		// get next sequence
		}

	// For each node in the tree
	//		for each seq in insertion list
	//			score the seq to the node
	//			keep the best scores
	
	int				diffs;
	int				parts;
	int				indel;

	CPNodeList		&nodes = tree->nodeList;
	int				count  = 0;
	DisplayL("Searching for position to insert %d sequences in tree of %d nodes. {Setup: %d sec}\n", inserts.size(), tree->nodeList.size(), time(NULL)-start+1);
	for (CPNodeListIter iter=nodes.begin() ; iter != nodes.end() ; ++iter)
		{
		CPNode		*node = *iter;

		if (++count%1000 == 0)
			DisplayL("[%10d/%d] {%d sec} [%s]\n", count, tree->nodeList.size(), time(NULL)-start+1, node->title.c_str());

		CParsimonySet		*node_pars = GetParsimonySet(node);
		if (node_pars == NULL)
			continue;

		// check match to each of the insert seq
		for (int i=0 ; i < inserts.size() ; ++i)
			{
			CInsertPos		*insert = inserts[i];
		
//			if (insert->nSites < 100) continue;

			int				thresh = insert->best.WorstScore();
            
            int nodecost = CalcCost(insert->pars, *node_pars, diffs, parts, indel, thresh);

			//TRACE("[%10s] %d\n", node->title, nodecost);
			if (nodecost != INT_MAX) 
				{
				// get taxonomy
				string		t = GetNodeName(node);

				string			tax = GetTaxonomy(t.c_str(), node);

				insert->best.Add(nodecost, node, tax.c_str());				
				}
			}
		}

	time_t			finish = time(NULL);
	DisplayL("Insertion of %d sequences in [%d sec]\n", inserts.size(), finish-start+1);
	}

///////////////////////////////////////////////////////////////////////////////////////////////////
//
// fast insertion that only processes nodes that have a chance of getting a good score
//
void FindInsertLocations(CSequenceFile *treeSeqfile, CPNode *node, CSequenceItem *seq, CInsertPos *item)
	{
	int 				nodecost = INT_MAX;
	int					diffs    = INT_MAX;
	int					parts;
	int					indel;	
	BOOL				recurse = FALSE;

    --nSeqRemaining;
    CParsimonySet		*node_pars  = GetParsimonySet(node);
	
	if (node_pars == NULL)
		{
		DisplayL("**** Error: could not generate Parsimony Set\n");
		return;
		}
    
    nodecost = CalcCost(item->pars, *node_pars, diffs, parts, indel, INT_MAX);
	//int				per = (item->nSites-(diffs+indel))* 100 / item->nSites;

	//TRACE("[%10s] %d\n", node->title, nodecost);
	if (nodecost < item->best.WorstScore()) // found atleast a partial match
		{
		// add to best match list
		string			name = GetNodeName(node);
		string			tax = GetTaxonomy(name.c_str(), node);

		item->best.Add(nodecost, node, tax.c_str());				
		}
		
	LPCSTR		items = "9";
	if (node->IsBranchNode())
		{
		char		c = node->title[node->title.length()-1];
		if (strchr(items, c) == NULL)
			recurse = TRUE;
		else
			{
		//	DisplayL("Checking '%c'\n", c);
			int				thresh = item->best.WorstScore();
		//	if (thresh < INT_MAX)
		//		thresh = thresh * 120 / 100;
			thresh = min(thresh, item->nSites*fast/100);
	
			// get the max value for matching any other node in subtree
		    CParsimonySet		*union_pars = unionList[node->title];
			CalcCost(item->pars, *union_pars, diffs, parts, indel, thresh);
			
			int				best   = diffs + indel; 
		//	DisplayL("[min(%8d,%8d) > %8d (%8d,%8d)[%s]\n", item->best.WorstScore(), item->nSites*fast/100, diffs,parts,indel, node->title.c_str());
			recurse = (best <= thresh);
		//	if (!recurse)
		//		DisplayL("skipping descendents of [%s]\n", GetNodeName(node));
			}
		}
		
	// Only propogate along this subtree if we can possibly obtain better scores
	if ((fast == 0) || recurse)
		{
		// Now try all the children of this node
		CPNodeList		&children = node->edges;
		for (CPNodeListIter iter=children.begin() ; iter != children.end() ; ++iter)
			{
			CPNode *child = *iter;
			FindInsertLocations(treeSeqfile, child, seq, item);
			}
		}
	}

///////////////////////////////////////////////////////////////////////////////////////////////////
//
// fast insertion that only processes nodes that have a chance of getting a good score
//
void FindInsertLocations(CSequenceFile *treeSeqfile, CPTree *tree, CSequenceFile *insertSeqfile, CInsertPosArray &inserts)
	{
	//	for each seq for insertion
	// 		insert in the tree - starting at root

	CPNode			*root = tree->root;
	int				count  = 0;

	time_t				start = time(NULL);

	// build insertion list
	insertSeqfile->ResetSeqIterator();
	CSequenceItem		*seq = insertSeqfile->GetNextSeq();
	while (seq != NULL)
		{
		CSequenceItem	*iseq = insertSeqfile->GetSequence(seq->name.c_str());

		// Check to see if this leaf is in the core tree
		if (coreNames.find(seq->name.c_str()) != coreNames.end())
			{
			DisplayL("**** Sequence is already in core tree: %s\n", seq->name.c_str());
			seq = insertSeqfile->GetNextSeq();		// get next sequence
			continue;
			}

		if (iseq == NULL)
			{
			DisplayL("**** Cannot find sequence for %s\n", seq->name.c_str());
			seq = insertSeqfile->GetNextSeq();		// get next sequence
			continue;
			}

		// check that we have the taxonomy
		string				tax;
		if (taxonomy != NULL)
			tax = GetTaxonomy(seq->name.c_str());
			
		if (tax.empty() && testing)
			{
			DisplayL("**** Cannot find sequence taxonomy for %s\n", seq->name.c_str());
			seq = insertSeqfile->GetNextSeq();		// get next sequence
			//continue;
			}

		CInsertPos		*item = new CInsertPos();
		item->seq = iseq;
		item->pars.Convert(iseq->GetSeqData());

		for (int i=0 ; i < item->pars.len ; ++i)
			{
			if (item->pars.data[i] != 0)
				{
				if (useMask && (mask[i] != 0)) 
					++item->nSites;
				else if (!useMask)
					++item->nSites;

				item->pars.end = i;
				if (item->pars.start == -1)
					item->pars.start = i;
				}
			}

		if (++count%100 == 0)
			DisplayL("[%10d/%d] {%d sec} [%s]\n", count, insertSeqfile->seqCount, time(NULL)-start+1, seq->name.c_str());
		if (item->nSites < 800)
			TRACE("SHORT");

        nSeqRemaining = tree->nodeList.size();
        time_t      start_item = time(NULL);
		DisplayL("Insert [%20s](len=%d) [%5d sites] (%d - %d) [%s]", 
						item->seq->name.c_str(), item->seq->len, 
						item->nSites, 
						item->pars.start, item->pars.end,
						tax.c_str());
		inserts.push_back(item);

	///	DisplayL("Searching for position to insert %d sequences in tree of %d nodes. {Setup: %d sec}\n", inserts.size(), tree->nodeList.size(), time(NULL)-start+1);
        FindInsertLocations(treeSeqfile, root, seq, item);
        if (nSeqRemaining > 0)
	        DisplayL("(%d sec) [%5d skipped]\n", time(NULL)-start_item, nSeqRemaining);
		else
	        DisplayL("(%d sec)\n", time(NULL)-start_item);

    /// DisplayBestInsertLocations(item, TRUE);
			
		seq = insertSeqfile->GetNextSeq();		// get next sequence
		}

	time_t			finish = time(NULL);
	DisplayL("Insertion of %d sequences in [%d sec]\n", inserts.size(), finish-start+1);
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

WORD    *scoreMatrix = NULL;
void BuildScoreMatrix()
    {
    if (scoreMatrix != NULL)
        free(scoreMatrix);
    
    scoreMatrix    = (WORD*)calloc(2, 4096*4096);
    
    for (int i=0 ; i < 4*1024 ; ++i)
        for (int j=0 ; j < 4*1024 ; ++j)
            {
            int         key  = ((i << 12) | j);

            int         diffs = 0;
            int         indel = 0;
            int         parts = 0;
            
            // count number of diffs, indel, partial
            for (int k=0 ; k < 3 ; ++k)
                {
                BYTE v1 = (i >> (k*4)) & 0x0F; // shifts by 0, 4, 8
                BYTE v2 = (j >> (k*4)) & 0x0F;
                if (v1 != v2)
                    {
                    if ((v1 & v2) != 0)
                        parts += 1;
                    else if ((v1 != 0) && (v2 != 0))
                        diffs += 1;
                    else 
                        indel += 1;
                    }
                }
            if ((diffs > 3) || (indel > 3) || (parts > 3) || ((diffs+indel+parts) > 3))
                DisplayL("BAD BAD COMPUTER");
            scoreMatrix[key] = (diffs << 0) | (indel << 3) | (parts << 6);
            }
    }
    
///////////////////////////////////////////////////////////////////////////////////////////////////

int CalcCost_NEW1(CParsimonySet &next_pars, CParsimonySet &node_pars, int &diffs, int &partials, int &indel, int thresh)
    {
	// calc delta to insert next_pars
	int                 len  = min(next_pars.len, node_pars.len);
    register LPCSTR		m    = mask;
    register BYTE		*p1  = next_pars.data;
    register BYTE		*p2  = node_pars.data;
    register DWORD      key;
    
   	++nCalcCost;
    diffs    = 0;
	partials = 0;
	indel    = 0;
	for (int i=0 ; (i < len) ; i+=3)
		{
        key = 0;
        
        for (int j=0 ; j < 3 ; ++j)
            {
            key = key << 4;
            if ((!useMask || *m++) && ((i+j >= next_pars.start) && (i+j <= next_pars.end)))
                {
                key |= ((DWORD)(*p1) << 12) | (*p2&0x0F);
                }
            
            ++p1;
            ++p2;
            }
            
        // look up score for all three values
        register WORD   value = scoreMatrix[key];
        
        if (value  == 0) continue;
        
        diffs += value & 0x07;                
        indel += (value >> 3) & 0x07;
        partials += (value >> 6) & 0x07;

        thresh -= (value & 0x07) + ((value >> 3) & 0x07) + ((value >> 6) & 0x07);
        
        if (thresh < 0)
            return INT_MAX;
        }
    
    ++nCalcCostFull;
	return diffs + indel + partials/4;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

int CalcCost_NEW4(CParsimonySet &next_pars, CParsimonySet &node_pars, int &diffs, int &partials, int &indel, int thresh)
    {
	// calc delta to insert next_pars
	int			len = min(next_pars.len, node_pars.len);
	LPCSTR		m   = mask;
	
    ++nCalcCost;

    if ((next_pars.segCounts != NULL) && (node_pars.segCounts != NULL))
        {
        // compare the counts, looking for known error
        int count = next_pars.CompareSegments(&node_pars);
        
        // if error is over threshold, skip        
        if (count > thresh)
            return INT_MAX;
        }
                    
    ++nCalcCostFull;
    
	diffs    = 0;
	partials = 0;
	indel    = 0;
    BYTE		*p1  = next_pars.data;
    BYTE		*p2  = node_pars.data;
    
	for (int i=0 ; i < len ; ++i)
		{
        if (!useMask || *(m++)) 
            {
            char		s1 = *p1;
            char		s2 = *p2;
            
            if (s1 != s2)
                {
                // there is a difference, is is partial, diff, or indel
                if ((s1 & s2) != 0) 
                    ++partials;
                else if ((s1 != 0) && (s2 != 0))
                    ++diffs;
                else if ((i >= next_pars.start) && (i <= next_pars.end))  // only count indel if within next_pars area
                    ++indel;

                if ((diffs+indel+partials/4) > thresh)
                    return INT_MAX;
                }
            }
        
        ++p1;
        ++p2;
		}
    
	return diffs + indel + partials/4;
    }

///////////////////////////////////////////////////////////////////////////////////////////////////

int CalcCost_ORIG(CParsimonySet &next_pars, CParsimonySet &node_pars, int &diffs, int &partials, int &indel, int thresh)
	{
	// calc delta to insert next_pars
	int			len = min(next_pars.len, node_pars.len);
	register LPCSTR		m   = mask;
	
    ++nCalcCost;
    
	diffs    = 0;
	partials = 0;
	indel    = 0;
    
    register BYTE        *p1  = next_pars.data;
    register BYTE        *p2  = node_pars.data;
    
	for (int i=0 ; i < len ; ++i)
		{
		if ((!useMask || *m++) && ((i >= next_pars.start) && (i <= next_pars.end)))   // only count(indel) if within next_pars area
			{
			register char		s1 = *p1;
			register char		s2 = *p2;
			
			if (s1 != s2)
				{
				// there is a difference, is is partial, diff, or indel
				if ((s1 & s2) != 0) 
					++partials;
				else if ((s1 != 0) && (s2 != 0))
					++diffs;
				else 
					++indel;
				
                if ((diffs+indel+partials/4) > thresh)
                    return INT_MAX;
                }
			}
			
		++p1;
		++p2;
		}

    ++nCalcCostFull;
	return diffs + indel + partials/4;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

int CalcCost(CParsimonySet &next_pars, CParsimonySet &node_pars, int &diffs, int &partials, int &indel, int thresh)
    {
    switch (useCalcCost)
        {                        
        case 1:  return CalcCost_NEW1(next_pars, node_pars, diffs, partials, indel, thresh);
        case 4:  return CalcCost_NEW4(next_pars, node_pars, diffs, partials, indel, thresh);
        default: return CalcCost_ORIG(next_pars, node_pars, diffs, partials, indel, thresh);
        }
    }
    
///////////////////////////////////////////////////////////////////////////////////////////////////

CParsimonySet* GetParsimonySet(CPNode *node, BOOL create)
	{
	CParsimonySet		*node_pars = NULL;

	string				name = GetNodeName(node);
	node_pars = (CParsimonySet*)parsimonyList[name];
	
	if ((node_pars == NULL) && create)
		{
		// does not exist, create new entry
    //	DisplayL("Create new CParsimonySet for %s\n", name.c_str());
		
		node_pars = new CParsimonySet();
        
        // get sequence from the sequence file
        CSequenceItem *seq = treeSeqfile->GetSeq(name.c_str());
        if ((seq != NULL) && (seq->len > 0))
            {
            node_pars->Convert(seq->data);
            seq->ReleaseSeqData();
            }

        parsimonyList[name] = node_pars;
                    
	//	if (parsimonyList.size() % 100 == 0)
	//		DisplayL("Created %d parsimony sets\n", parsimonyList.size());
		}
        
	return node_pars;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

int	CalcAncestorParsimony(CPTree *tree)
	{
	DisplayT("Calculating Parsimony ...");

	if (treeSeqfile == NULL)
		{
		DisplayT("*** Cannot calc parsimony, no sequence file loaded");
		return 0;
		}

	
	time_t				start = time(NULL);
	int					cost = 0;

	nSeqRemaining = tree->nodeList.size();

	cost = CalcNodeParsimony(tree->root);

    if (fitch)
        {
        DisplayL("Applying parsimonious selections back down the tree.\n");
        int     changes = ForceNodeParsimony(tree->root);
        DisplayL("%d nodes were modified\n", changes);
        }
/***
	// Save the calculated parsimony for quicker startup
	//
	DisplayT("Writing Parsimony ...");

    CSequenceFile   parsimony;
    if (!parsimony.Open("Parsimony.FASTA", "w+"))
        {
            DisplayT("*** Cannot open parsimony file");
            return 0;
        }

    // for each branch in the tree
	//		write sequence to file
    nSeqRemaining = (tree->nodeList.size() + 1) / 2;
    WriteParsimonySet(tree->root, &parsimony);
    
	parsimony.Close();
***/

	time_t				delta = time(NULL) - start + 1;
	double				npersec = tree->nodeList.size() * 1. / delta;
	DisplayT("\nProcessed %d nodes in %d seconds, %.0f nodes per sec\n", tree->nodeList.size(), delta, npersec);
	
	return cost;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

int ForceNodeParsimony(CPNode *node)
    {
	// force specific parsimony from parent onto the children 
    
	if (node == NULL) return 0;
    if (!node->IsBranchNode()) return 0;
    
    CParsimonySet	*node_pars = GetParsimonySet(node, FALSE);
    if (node_pars == NULL) return 0;
    
    //DisplayL("Update node [%s]\n", node->title.c_str());
    
    int     count = 0;
    for (CPNodeListIter iter=node->edges.begin() ; iter != node->edges.end() ; ++iter)
        {
        CPNode *child = (*iter);
        if (child == NULL)
            {
            DisplayL("***** Child not found [%s]\n", node->title.c_str());
            continue;
            }
                    
        CParsimonySet		*childParsSet = GetParsimonySet(child, FALSE);
        if (childParsSet == NULL) continue;
        
        if (childParsSet->Force(node_pars) > 0)
            ++count;
        
        count += ForceNodeParsimony(child);
        }
    
    return count;
    }
    
///////////////////////////////////////////////////////////////////////////////////////////////////

int CalcNodeParsimony(CPNode *node)
	{
	// calc parsimony of the tree
	int					cost = 0;
	string				alignment;

	if (node == NULL) return INT_MAX;

    CParsimonySet	*node_pars = GetParsimonySet(node, TRUE);

	if (node->IsBranchNode())
		{
		CParsimonySet	unionSeq;
		CParsimonySet	interSeq;

		int		count = 0;
		for (CPNodeListIter iter=node->edges.begin() ; iter != node->edges.end() ; ++iter)
			{
			CPNode *child = (*iter);
			if (child == NULL)
				{
				DisplayL("***** Child not found [%s]\n", node->title.c_str());
				continue;
				}

			CParsimonySet		*childParsSet = GetParsimonySet(child, FALSE);

			if ((childParsSet == NULL) || (childParsSet->len == 0))
                {
				cost += CalcNodeParsimony(child);
                childParsSet = GetParsimonySet(child);
                }
                
			unionSeq.Union(childParsSet);
			interSeq.Intersect(childParsSet);

			if (childParsSet->len > 0)
				count++;
			}
        //	unionSeq.Display("UNION");
        //	interSeq.Display("INTER");
        cost += node_pars->Set(&unionSeq, &interSeq);

        // save the union information for scoring subtree
            
        string				name = node->title;
        CParsimonySet		*union_pars = new CParsimonySet();
        union_pars->Union(&unionSeq);
                    
        unionList[name] = union_pars;   
        }
    else 
        {                
        // taxa node
        if (node_pars->len == 0)
            {
            // read the sequence from tree sequence file
            // create the parsimony set from sequence
            string          name = GetNodeName(node);
            CSequenceItem   *seq = treeSeqfile->GetSequence(name.c_str());
            if (seq != NULL) 
                {
                node_pars->Convert(seq->GetSeqData());
                seq->ReleaseSeqData();
                }
            }
        }

	//string		t = node->attrs.GetString(ATTR_NAME);
	//if (t.empty()) t = node->title;
	//TRACE("Setting [%s], Cost = %d\n", t.c_str(), cost);
	node->attrs.Add("PARSIMONY_COST", cost);

	if (--nSeqRemaining%1000 == 0)
		DisplayT("\rCalcParsimony %d nodes remaining     ", nSeqRemaining);

	return cost;
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////

int WriteParsimonySet(CPNode *node, CSequenceFile *parsimony)
    {
    // Write the node's parsimony value to sequence file
    
    if (!node->IsBranchNode())
        return 0;
    
    // calc the parsimony output string
    CParsimonySet	*node_pars = GetParsimonySet(node, TRUE);
    char			*pars = NULL;
    node_pars->BuildString(pars, -1);
    string				name = GetNodeName(node);

    // add new sequence to the parsSequences
    CSequenceItem *seq =  new CSequenceItem(name.c_str(), -1, strlen(pars), pars);
    parsimony->seqlist[name] = seq;
    parsimony->WriteSequence(seq);
    ///	DisplayL("Parsimony for [%s] from %d decendants: %d,[%100.100s]\n", node->title.c_str(), count, strlen(pars), pars);
    free(pars);
    
    if (--nSeqRemaining%1000 == 0)
        DisplayT("\r %d nodes remaining to be written     ", nSeqRemaining);
        
    return 1;
    }

///////////////////////////////////////////////////////////////////////////////////////////////////

LPCSTR GetTaxonomy(LPCSTR name, CPNode *node)
	{
	static string		tax;

	tax.clear();
	if (taxonomy != NULL)
		{
		CSequenceItem *seq = taxonomy->GetSequenceHeader(name);
		if ((seq != NULL) && (seq->hdr != NULL))
			{
			tax = seq->hdr;
			TrimRight(tax, "\n\r\t ");

			int		k = tax.find_first_of("\t ");
			if (k > 0)
				tax = tax.substr(k+1, tax.length());
			Trim(tax, "\n\r\t ");
			}
		}

	return tax.c_str();
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

void DisplayInsertLocations(CInsertPosArray &inserts, BOOL all)
{
	// show best for each insert
	int				diffs;
	int				parts;
	int				indel;
    
    //	int				nerror       = 0;
    //	int				exact        = 0;
    //	int				partial_seq  = 0;
    //	int				partial_tree = 0;
    //	int				partials[10] = {10*0};	
    
	for (int i=0 ; i < inserts.size() ; ++i)
		{
            string				name = inserts[i]->seq->name;
            
            string				tax = GetTaxonomy(name.c_str());
            
            DisplayL("======================================================================\n"); 
            if (all)
                DisplayL("\nInsertion of [%s][%5d sites] [%d matches] Taxonomy[%s]:\n", 
                			inserts[i]->seq->name.c_str(), inserts[i]->nSites, 
                			inserts[i]->best.list.size(), tax.c_str());
            
            string					taxFirst;
            string					taxBest;
            CBestList::iterator		iterB = inserts[i]->best.list.begin();
            int						count = 0;
            
            if (inserts[i]->best.list.size() == 0)
                DisplayL("\tNO MATCHES found\n");
            
            for (iterB=inserts[i]->best.list.begin() ; iterB != inserts[i]->best.list.end() ; ++iterB)
                {
                ++count;
                if ((iterB->score > inserts[i]->nSites/5) && (count > 3)) 
                    break;
                
                //	TRACE("Getting taxonomy for [%s]", iterB->node->title);
                ///??			SetMyTaxonomy(iterB->node);
                if (iterB->tax.empty())
                    {
                    iterB->node->GetComment(ATTR_TAXLABEL, taxBest, "");
                    
                    if (taxBest.empty() && (taxonomy != NULL))
                        {
                        string		t = GetNodeName(iterB->node);                                    
                        taxBest = GetTaxonomy(t.c_str(), iterB->node);
                        }
                    
                    iterB->tax = taxBest;
                    }
                //	TRACE("[%s]\n", iterB->tax);
                
                taxBest = iterB->tax;
                if (taxFirst.empty())
                    taxFirst = taxBest;
                
                CParsimonySet		*node_pars = GetParsimonySet(iterB->node);
                char				marker;
                if (node_pars != NULL)
                    {
                    int cost = CalcCost(inserts[i]->pars, *node_pars, diffs, parts, indel);
                    ///		ASSERT(cost==iterB->score);
                    
                    int		per = -1;
                    if (inserts[i]->nSites > 0)
                        per = (inserts[i]->nSites-(diffs+indel))* 100 / inserts[i]->nSites;
                    
                    marker = ' ';
                    if (cost <= inserts[i]->nSites*3/100)
                        marker = '*';
                    else if (cost <= inserts[i]->nSites*6/100)
                        marker = '+';
                    
                    if (all)
                        {
                        DisplayL("%c(%3d%%) %6d (%4d diffs, %4d partials, %4d indels) [%12s][%s]\n", 
                                 marker, per,
                                 iterB->score, diffs, parts, indel,
                                 iterB->node->title.c_str(), taxBest.c_str());
                        
                        if (stats)
                            {
                            string		match;
                            GetTaxonomyMatch(tax, taxBest, match, rankCounts);
                            
                            string		name = "";
                            if (iterB == inserts[i]->best.list.begin())
                                name = inserts[i]->seq->name;
                            
                            fprintf(stats, "%15s\t%s\t%3d%%\t%4d\t%4d\t%4d\t%s\t%s\t%s\n", 
                                    name.c_str(), iterB->node->title.c_str(), per, 
                                    diffs, indel, parts, match.c_str(), taxBest.c_str(), tax.c_str());
                            }
                        }
                    }
                }
            
            
            iterB = inserts[i]->best.list.begin();
            if (iterB != inserts[i]->best.list.end())
                {
                    taxBest = iterB->tax;
                    
                    string		marker = "";
                    string		t = (taxBest.empty() ? taxFirst : taxBest);
                    
                    GetTaxonomyMatch(tax, t, marker, rankCounts);
                    
                    CParsimonySet		*node_pars = GetParsimonySet(iterB->node);
                    
                    int cost = CalcCost(inserts[i]->pars, *node_pars, diffs, parts, indel);
                    DisplayL("\nInsertion [%5d sites] [%20.20s] %6d (%4d diffs, %4d partials, %4d indels)\n", 
                             inserts[i]->nSites, inserts[i]->seq->name.c_str(),
                             cost, diffs, parts, indel,
                             0);
                    
                    string				diff;
                    int					i;
                    for (i=0 ; (i < tax.length()) && (i < taxBest.length()) ; ++i)
                        diff += (tax[i] == taxBest[i] ? ' ' : 'X');
                    for ( ; i < tax.length() ; ++i)
                        diff += 'X';
                    for ( ; i < taxBest.length() ; ++i)
                        diff += 'X';
                    
                    DisplayL("%12.12s\t  Seq[%s]\n", marker.c_str(), tax.c_str());
                    DisplayL("%12.12s\t     [%s]\n", "", diff.c_str());
                    DisplayL("%12.12s\t Best[%s]\n", "", taxBest.c_str());
                    if (taxBest != taxFirst)
                        DisplayL("\tFirst[%s]\n", taxFirst.c_str());
                }
		}
}

///////////////////////////////////////////////////////////////////////////////////////////////////

void DisplayBestInsertLocations(CInsertPos *item, BOOL all)
    {
	// show best for each insert
	int				diffs;
	int				parts;
	int				indel;
    
    string				name = item->seq->name;
    string				tax = GetTaxonomy(name.c_str());
    
    DisplayL("======================================================================\n"); 
    if (all)
        DisplayL("\nInsertion of [%s][%5d sites] [%d matches] Taxonomy[%s]:\n", 
        			item->seq->name.c_str(), item->nSites, item->best.list.size(), tax.c_str());
            
    string					taxFirst;
    string					taxBest;
    CBestList::iterator		iterB = item->best.list.begin();
    int						count = 0;
            
    if (item->best.list.size() == 0)
        DisplayL("\tNO MATCHES found\n");
    
    for (iterB=item->best.list.begin() ; iterB != item->best.list.end() ; ++iterB)
        {
        ++count;
        if ((iterB->score > item->nSites/5) && (count > 3)) 
            break;
        
        //	TRACE("Getting taxonomy for [%s]", iterB->node->title);
        ///??			SetMyTaxonomy(iterB->node);
        if (iterB->tax.empty())
            {
            iterB->node->GetComment(ATTR_TAXLABEL, taxBest, "");
            
            if (taxBest.empty() && (taxonomy != NULL))
                {
                string		t = GetNodeName(iterB->node);
                
                taxBest = GetTaxonomy(t.c_str(), iterB->node);
                }
            
            iterB->tax = taxBest;
            }
        //TRACE("[%s]\n", iterB->tax);
        
        taxBest = iterB->tax;
        if (taxFirst.empty())
            taxFirst = taxBest;
        
        CParsimonySet		*node_pars = GetParsimonySet(iterB->node);
        char				marker;
        if (node_pars != NULL)
            {
            int cost = CalcCost(item->pars, *node_pars, diffs, parts, indel);
            ///		ASSERT(cost==iterB->score);
            
            int		per = -1;
            if (item->nSites > 0)
                per = (item->nSites-(diffs+indel))* 100 / item->nSites;
            
            marker = ' ';
            if (cost <= item->nSites*3/100)
                marker = '*';
            else if (cost <= item->nSites*6/100)
                marker = '+';
            
            if (all)
                {
                DisplayL("%c(%3d%%) %6d (%4d diffs, %4d partials, %4d indels) [%12s][%s]\n", 
                         marker, per,
                         iterB->score, diffs, parts, indel,
                         iterB->node->title.c_str(), taxBest.c_str());
                
                if (stats)
                    {
                    string		match;
                    GetTaxonomyMatch(tax, taxBest, match, rankCounts);
                    
                    string		name = "";
                    if (iterB == item->best.list.begin())
                        name = item->seq->name;
                    
                    fprintf(stats, "%15s\t%s\t%3d%%\t%4d\t%4d\t%4d\t%s\t%s\t%s\n", 
                            name.c_str(), iterB->node->title.c_str(), per, 
                            diffs, indel, parts, match.c_str(), taxBest.c_str(), tax.c_str());
                    }
                }
            }
        }
    
    iterB = item->best.list.begin();
    if (iterB != item->best.list.end())
        {
        taxBest = iterB->tax;
        
        string		marker = "";
        string		t = (taxBest.empty() ? taxFirst : taxBest);
        
        GetTaxonomyMatch(tax, t, marker, rankCounts);
        
        CParsimonySet		*node_pars = GetParsimonySet(iterB->node);
        
        int cost = CalcCost(item->pars, *node_pars, diffs, parts, indel);
        DisplayL("\nInsertion [%5d sites] [%20.20s] %6d (%4d diffs, %4d partials, %4d indels)\n", 
                 item->nSites, item->seq->name.c_str(),
                 cost, diffs, parts, indel,
                 0);
        
        string				diff;
        int					i;
        for (i=0 ; (i < tax.length()) && (i < taxBest.length()) ; ++i)
            diff += (tax[i] == taxBest[i] ? ' ' : 'X');
        for ( ; i < tax.length() ; ++i)
            diff += 'X';
        for ( ; i < taxBest.length() ; ++i)
            diff += 'X';
        
        DisplayL("%12.12s\t  Seq[%s]\n", marker.c_str(), tax.c_str());
        DisplayL("%12.12s\t     [%s]\n", "", diff.c_str());
        DisplayL("%12.12s\t Best[%s]\n", "", taxBest.c_str());
        if (taxBest != taxFirst)
            DisplayL("\tFirst[%s]\n", taxFirst.c_str());
        }
    }

///////////////////////////////////////////////////////////////////////////////////////////////////

void DisplayRankCounts(LPCSTR header, int counts[][4])
	{
	if (!testing) return;
	if (counts[0][0] == 0)
        return;
        
    DisplayL("\n%s\n                     _________Precision________    __________Recall__________\n", header);
	for (int r=0 ; r < RANK_N ; ++r)
		{
		if (counts[r][0] == 0) continue;
		double	per  = 100. * counts[r][1] / counts[r][0];
        double  per2 = 100. * counts[r][3] / counts[r][2];
        
        DisplayL("\t%10.10s: %8d %8d (%6.2f%%)     %8d %8d (%6.2f%%)\n", 
        			rankNames[r], counts[r][0], counts[r][1], per, counts[r][3], counts[r][2], per2);
		}
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

// STATS FORMAT: name, insert position title, percentage match, diffs, indel, partials, conf level, best taxonomy [, correct taxonomy]
#define TSTATS_HEADER "#Insertion Name\tSite   \tMatch:%\tdiff\tindel\tpartial\tconf\ttaxonomy | correct taxonomy\n"
#define TSTATS_FORMAT "%15s\t%s\t%3d%%\t%4d\t%4d\t%4d\t%1d\t%s\t|\t%s\n"
#define STATS_HEADER "#Insertion Name\tMatch:%\ttaxonomy\n"
#define STATS_FORMAT "%15s\t%3d%%\t%s\n"
#define STATS_FORMAT2 "%15s\t    \t%s\n"

int			LOWER_SCORE = 0;
int			UPPER_SCORE = 100;
int			LOWER_PER = 90;
int			UPPER_PER = 101;

void OutputInsertLocations(CInsertPosArray &inserts, BOOL all)
    {
	// show best for each insert
	int				diffs;
	int				parts;
	int				indel;
    
	CTaxEntry		taxEntries("root");	// keeps track of diversity seen in taxonomy of inserted sequences
    
    if (stats)
		fprintf(stats, "%s\n", STATS_HEADER);
        
	for (int i=0 ; i < inserts.size() ; ++i)
		{
        string                  name = inserts[i]->seq->name;        
        string					taxCorrect = GetTaxonomy(name.c_str());
        string					taxFirst;
        string					taxBest;
        CBestList::iterator		iterB = inserts[i]->best.list.begin();
        int						count = 0;
        CTaxEntry				taxVotes("Votes:");	// keep track of all high scoring taxonomies for single sequence
        
        DisplayL("======================================================================\n"); 
        DisplayL("\nInsertion: [%20.20s] [%5d sites]\n", name.c_str(), inserts[i]->nSites); 
            
        if (inserts[i]->best.list.size() == 0)
            {
            DisplayL("\tNO MATCHES found\n");
            if (stats)
                {
                char   buf[1024];
                sprintf(buf, "NO MATCHES found (%d sites in sequence)", inserts[i]->nSites); 
                fprintf(stats, STATS_FORMAT, name.c_str(), 0, buf);
                if (!taxCorrect.empty())
                    fprintf(stats, STATS_FORMAT2, "",taxCorrect.c_str());
                fprintf(stats, "\n");
                }

            continue;
            }

        int						bestScore = inserts[i]->best.list.begin()->score;
        int						threshScore = bestScore + (bestScore >= 100 ? 50 : (bestScore > 50 ? 20 : 10));
        
        for (iterB=inserts[i]->best.list.begin() ; iterB != inserts[i]->best.list.end() ; ++iterB)
            {
            ++count;
            if ((iterB->score > inserts[i]->nSites/5) && (count > 3)) // score too low for processing, process at least 3
                break;
            
            string		t = GetNodeName(iterB->node);
            //	find the taxonomy for this insertion point
            if (iterB->tax.empty())
                {
                iterB->node->GetComment(ATTR_TAXLABEL, taxBest, "");
                
                if (taxBest.empty() && (taxonomy != NULL))
                    {
                    SetMyTaxonomy(iterB->node); // set taxonomy at insertion point
                    
                    taxBest = GetTaxonomy(t.c_str(), iterB->node);
                    }
                
                iterB->tax = taxBest;
                }
                
            // find distance from position in full tree
            double         dist = -1;
            if (fullTree)
            	{
                dist = ComparePosition(t.c_str(), iterB->node, fullTree, tree);
	            DisplayL("\t\t%5d [%5f] [%s][%s]\n", iterB->score, dist, iterB->node->title.c_str(), iterB->tax.c_str());
                }
            
            if (iterB->score < threshScore)
                {
                int         votes = ((threshScore-iterB->score)*100/threshScore); // most votes for best scores
                taxVotes.Add(iterB->tax.c_str(), votes);
                }
                
            taxBest = iterB->tax;
            if (taxFirst.empty())
                taxFirst = taxBest;
            
            CParsimonySet		*node_pars = GetParsimonySet(iterB->node);
            int					conf;
            if (node_pars != NULL)
                {
                int cost = CalcCost(inserts[i]->pars, *node_pars, diffs, parts, indel);
                //ASSERT(cost==iterB->score);
                
                int		per = -2;
                if (inserts[i]->nSites > 0)
                    per = (inserts[i]->nSites-(diffs+indel))* 100 / inserts[i]->nSites;

                conf = 0;
                if (cost <= inserts[i]->nSites*3/100)
                    conf = 9;
                else if (cost <= inserts[i]->nSites*6/100)
                    conf = 5;
                
	            DisplayL("\t\t%5d [%3d%%] [%s][%s]\n", iterB->score, per, iterB->node->title.c_str(), iterB->tax.c_str());

                if (all)
                    {
                    DisplayL("%1d (%3d%%) %6d (%4d diffs, %4d partials, %4d indels) [%12s]%c[%s]\n", 
                             conf, per,
                             iterB->score, diffs, parts, indel,
                             iterB->node->title.c_str(),(iterB->node->IsBranchNode()?'^':' '), taxBest.c_str());
                    
                    if (stats)
                        {
                        string		name = "";
                        if (iterB == inserts[i]->best.list.begin())
                            name = inserts[i]->seq->name;
 
                        fprintf(stats, TSTATS_FORMAT, name.c_str(), iterB->node->title.c_str(), per, 
                        			diffs, indel, parts, conf, taxBest.c_str(), taxCorrect.c_str());
                        }
                    }
                }
            }
        
        iterB = inserts[i]->best.list.begin();
        if (iterB != inserts[i]->best.list.end())
            {
            taxBest = iterB->tax;
            
            string		marker = "";
            string		t = (taxBest.empty() ? taxFirst : taxBest);
                     
            if (t.empty())
                {
                // try to find taxonomy from ancestory
                CPNode      *node = iterB->node->parent;
                while ((node != NULL) && t.empty())
                    {
					t = node->attrs.GetString(ATTR_TAXLABEL);
		    	//	DisplayL("\t using ancestor (%s) taxonomy: [%s] \n", GetNodeName(node), t.c_str());
                    node = node->parent;
                    }
                taxBest = t;
                }
                
            CParsimonySet		*node_pars = GetParsimonySet(iterB->node);
            int cost = CalcCost(inserts[i]->pars, *node_pars, diffs, parts, indel);
            DisplayL("\t %6d (%4d diffs, %4d partials, %4d indels) [%s] \n", 
                     cost, diffs, parts, indel, taxBest.c_str());
            
            int			per = -1;
            if (inserts[i]->nSites > 0)
                per = (inserts[i]->nSites-cost)* 100 / inserts[i]->nSites;
            
            if (per >= scoreThresh)
                {
                if (testing > 2)
	                taxVotes.Display("");
                CStringList taxVoteList;
                taxVotes.FindBest(60, taxVoteList);
                string      leader = "Assigned Taxonomy:";
                if (taxVoteList.size() > 0)
                	{
					for (int knox=0; knox < taxVoteList.size() ; ++knox)
						{
						DisplayL("%18s[%s]\n", leader.c_str(), taxVoteList[knox].c_str());
						leader = "";
						}
					if ((taxVoteList.size() > 0) && !taxVoteList[0].empty())
						taxBest = taxVoteList[0];
					}
				else						
					DisplayL("%18s[%s]\n", leader.c_str(), taxBest.c_str());
                	
                taxEntries.Add(taxBest.c_str());	// update diversity

                if (!taxCorrect.empty())
                    {
                    GetTaxonomyMatch(taxCorrect, (taxBest.empty()? taxFirst : taxBest), marker, rankCounts);

                    string      phylum;
                    GetRankFromTax(RANK_PHYLUM, taxCorrect, phylum, FALSE);
                    if (phylumRankCounts.find(phylum.c_str()) == phylumRankCounts.end())
                        {
                        // create ranks
                        phylumRankCounts[phylum.c_str()] = new CRankCounts();
                        }
                    CRankCounts *counts = phylumRankCounts[phylum.c_str()]; 
                    GetTaxonomyMatch(taxCorrect, (taxBest.empty()? taxFirst : taxBest), marker, counts->rankCounts);
                    }
                }    
            else
            	{
                taxBest.clear();
                DisplayL("no assignment (%d%% < %d%%)", per, scoreThresh);
                }
                
            int				conf = 0;
            if (cost <= inserts[i]->nSites*3/100)
                conf = 9;
            else if (cost <= inserts[i]->nSites*6/100)
                conf = 5;
            
            if ( !taxCorrect.empty() && testing )
                DisplayL(" Correct Taxonomy:[%s]\n", taxCorrect.c_str());
            
            if (stats)
                {
                size_t n;
                while ((n=taxBest.find(' ')) != string::npos)
                    taxBest.erase(n,1);
                while ((n=taxCorrect.find(' ')) != string::npos)
                    taxCorrect.erase(n,1);
                    
                if (taxBest.empty())
                    taxBest = "No assignment could be made!";
                fprintf(stats, STATS_FORMAT, name.c_str(),  per, taxBest.c_str());
                if (!taxCorrect.empty())
                    {
                    fprintf(stats, STATS_FORMAT2, "",taxCorrect.c_str());
                    
                    // find first position of difference
                    string diffs;
                    int i = 0;
                    while ((i < taxBest.length()) && (i < taxCorrect.length()) && (taxBest[i] == taxCorrect[i]))
                        {
                        ++i;
                        diffs += " ";
                        }
                    while ((i < taxBest.length()) && (i < taxCorrect.length()))
                        {
                        ++i;
                        if (i < taxBest.length())
                            diffs += "x";
                        else
                            diffs += "o";
                        }
                    
                    //if (diffs.find('x') < diffs.length())
                        fprintf(stats, STATS_FORMAT2, "",diffs.c_str());
                    }
                    
                fprintf(stats, "\n");
                fflush(stats);
                }
            }
		}
    
	DisplayL("Taxonomies List:\n");
	taxEntries.Display(" ");
    }

///////////////////////////////////////////////////////////////////////////////////////////////////

int ParseTaxonomy(string t, CStringList &taxList)
	{
	// find next separator, add to 
	string		tax = t;
	ToLower(tax);
	while (!tax.empty())
		{
		// find separator
		int			sep = 0;
		while ((sep < tax.length()) && (tax[sep] != ';') && (tax[sep] != '/'))
			++sep;

		string		entry = tax.substr(0, sep);
		LPCSTR		unclassified = "unclass";

		string		lower = entry.substr(0, strlen(unclassified));
		for (int i=0 ; i < sizeof(unclassified) ; ++i)
			lower[i] = tolower(lower[i]);
			
		if ((entry.size() > 0) && (strncmp(lower.c_str(), unclassified, strlen(unclassified)) != 0)) // does NOT begin with "Unclassified"
			taxList.push_back(entry);

		if (sep < tax.length())
			tax = tax.substr(sep+1);
		else
			tax.clear();
		}

	return taxList.size();
	}
    
///////////////////////////////////////////////////////////////////////////////////////////////////

void GetRankFromTax(int rank, string &tax, string &rankStr, BOOL only)
    {
    CStringList		taxList;        
    ParseTaxonomy(tax, taxList);
    char            sep = tax.find(';') >= 0 ? ';' : '/';
    
    rankStr.clear();
    
    for (int i=0 ; (i <= rank) && (i < taxList.size()) ; ++i)
        {
        if (!only)
            {
            if (!rankStr.empty())
                rankStr += sep;
            }

        if (!only || (i == rank))
            rankStr += taxList[i];
        }
    }


///////////////////////////////////////////////////////////////////////////////////////////////////

int GetTaxonomyMatch(string &taxA, string &taxB, string &marker, int counts[][4])
	{
    // taxA is the correct solution
    // taxB is our assigned solution
    //
	int		levels = 0;
	BOOL	matching = TRUE;	// TRUE if still matching
	marker = "";

	// parse the entries from each taxonomy
	CStringList		taxListA;
	CStringList		taxListB;

	ParseTaxonomy(taxA, taxListA);
	ParseTaxonomy(taxB, taxListB);

	for (int k=0 ; (k < taxListA.size()) && (k < taxListB.size()) && (k < RANK_N) ; ++k)
		{
		counts[k][0] += 1;  // saw another rank at this level

		if (matching && (k < taxListB.size()))
			{
			LPCSTR		tA = taxListA[k].c_str();
			LPCSTR		tB = taxListB[k].c_str();

			if (strcmp(tA,tB) == 0)
				{
				// matched at this level
				counts[k][1] += 1; // saw another match at this level
				++levels;
				}
			else if (precision) // count all mistakes?
				matching = FALSE;
			}
		}
    
    matching = TRUE;
    for (int k=0 ; (k < taxListA.size()) && (k < RANK_N) ; ++k)
        {
        counts[k][2] += 1;  // saw another rank at this level
        
        if (matching && (k < taxListB.size()))
            {
            LPCSTR		tA = taxListA[k].c_str();
            LPCSTR		tB = taxListB[k].c_str();
            
            if (strcmp(tA,tB) == 0)
                {
                // matched at this level
                counts[k][3] += 1; // saw another match at this level
                }
            else 
                matching = FALSE;
            }
        }
        

	// Create marker for display
	if ((levels < taxListA.size()) && (levels < taxListB.size()))
		{
		// check to see how close we are
		if (levels >= 4)
			{
			marker = ">>>";
			}
		else
			{
			marker = "XXX";
			}
		}
	else if (levels < taxListA.size())
		{
		marker = ">  ";
		}
	else if (levels < taxListB.size())
		{
		marker = "<  ";
		}
	for (int r=levels ; r > 0 ; --r)
		marker += rankNames[r-1][0];

	return levels;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

BOOL AddTaxonomyToTree(CPTree *tree, CSequenceFile *taxonomy)
	{
	if (tree == NULL) return FALSE;
	if (taxonomy == NULL) return FALSE;

	CPNodeList		&nodes = tree->nodeList;
	for (CPNodeListIter iter=nodes.begin() ; iter != nodes.end() ; ++iter)
		{
		string				tax;
		CPNode				*node = *iter;

		if (node == NULL) continue;

		CSequenceItem		*seq = taxonomy->GetSequence(node->title.c_str());

		if (seq != NULL) 
            {
            if (seq->data)
                tax = seq->data;
            else if (seq->hdr)
                tax = seq->hdr;
            }

		int		pos = tax.find_first_of('\t');
		if (pos < 20)
			tax = tax.substr(pos+1);
		TrimRight(tax, "\n\r\t ");

		if (!node->IsBranchNode())
			tax = "CORE: " + tax;

		node->attrs.Add(ATTR_COMMENT, tax);
		}

	return TRUE;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

// Builds a map of tree node names that have list of new sequences to be inserted as children
CTreeInserts* BuildTreeInsertLists(CInsertPosArray &inserts)
	{
	if (inserts.size() == 0) return NULL;
	CTreeInserts*	treeInserts = new CTreeInserts();

	for (int i=0 ; i < inserts.size() ; ++i)
		{
		CInsertPos		*in = inserts[i];
		if (in->best.list.size() == 0) continue;
		if (in->seq == NULL) continue;

		CPNode					*node   = in->best.list[0].node;
		if (!node->IsBranchNode())
			node = node->parent;

		CTreeInsertList			*iList = (*treeInserts)[node];

		if (iList == NULL)
			{
			iList = new CTreeInsertList();
			(*treeInserts)[node] = iList; 
			}

		iList->push_back(in);
		}

	return treeInserts;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

BOOL BuildInsertTree(CTreeInserts* insertLists, CPTree *tree)
	{
	if (tree == NULL) return FALSE;
    if (insertLists == NULL) return FALSE;
	if (insertLists->size() == 0) return FALSE;

	// for each item in the map
	for (CTreeInserts::iterator iter=insertLists->begin() ; iter != insertLists->end() ; ++iter)
		{
		CPNode 			*node    = (*iter).first;
		CTreeInsertList *inserts = (*iter).second;

		CPNode			*child = NULL;
		
		if (inserts->size() == 0) continue;
		
		CInsertPos		*in = (*inserts)[0];
		int				score = in->best.list[0].score * 100 / in->nSites;
		if (score > 100-scoreThresh) 
			{
			DisplayL("[%s] at %d%% does not meet threshold (%d%%)\n", in->seq->name.c_str(), score, 100-scoreThresh);
			continue;
			}
		
		if (inserts->size() == 1)
			{
			// insert as child of the node
			CInsertPos		*in = (*inserts)[0];

			child = AddChild(tree, node, in);
			node->AddEdge(child);
			}
		else
			{
			// Creaate new branch node
			// for each item in the list
			//		add as child of the new branch

			double			shortest = 1000000.;
			CPNode			*branch = new CPNode(tree);
			node->AddEdge(branch);

			for (int i=0 ; i < inserts->size() ; ++i)
				{
				CInsertPos		*in = (*inserts)[i];
				child = AddChild(tree, node, in);
				double		dist = child->attrs.GetDouble(ATTR_DISTANCE);
				if (dist < shortest)
					shortest = dist;
				branch->AddEdge(child);
				}

			if (shortest > 0)
				{
			//	double		dist = branch->attrs.GetDouble(ATTR_DISTANCE);
			//	DisplayL("Shortest distance %f\n", shortest);
				shortest /= 2;
				branch->attrs.Add(ATTR_DISTANCE, shortest);
				// subtract shortest from each of the children
				for (CPNodeListIter iter=branch->edges.begin() ; iter != branch->edges.end() ; ++iter)
					{
					CPNode		*child = (*iter);
					double		dist = child->attrs.GetDouble(ATTR_DISTANCE);
					dist -= shortest;
					child->attrs.Add(ATTR_DISTANCE, dist);
					}
				}
			}

		}

	return TRUE;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

CPNode* AddChild(CPTree *tree, CPNode *node, CInsertPos *in)
	{
	int				diffs;
	int				parts;
	int				indel;

	CPNode			*inserted = new CPNode(tree);

	inserted->attrs.Add(ATTR_NAME, in->seq->name);

	double dist = CalcDist(node, in, diffs, parts, indel);
    
    if (jukes_cantor)
        {
        //        distance = -3/4 * ln(1 - 4/3*p)
        //        where p is the proportion of sites with different nucleotides.       
        double       p = (diffs + indel) * 1. / in->nSites;
 
        dist = -3./4. * log(1. - (4./3.*p));
        }
        
	inserted->attrs.Add(ATTR_DISTANCE, dist);

	// Take parent taxonomy as node's taxonomy
	string 		tax = in->best.list[0].tax;
	
	while (tax.empty() && (node != NULL))
		{
		tax = node->attrs.GetString(ATTR_TAXLABEL);
	//	DisplayL("\t\ttrying ancestor [%s]\n", GetNodeName(node));
		node = node->parent;
		}

	int			per = -1;
	if (in->nSites > 0)
		per = (in->nSites-(diffs+indel))* 100 / in->nSites;
		
	char			comment[2*1024];
	sprintf(comment,"Sites:%d, %d%% diffs[%d,%d,%d], Score:%d, [%s]", in->nSites, per, diffs, parts, indel, in->best.list[0].score, tax.c_str());
	inserted->attrs.Add(ATTR_COMMENT, comment);

	return inserted;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

double CalcDist(CPNode *parent, CInsertPos* in, int &diffs, int &parts, int &indel)
	{
	// calc the distance for tree branch edge
	double				dist = 0.1;
	CParsimonySet		*node_pars = GetParsimonySet(parent);
	if (node_pars != NULL)
		{
		CParsimonySet		pars(&in->pars);
		CalcCost(pars, *node_pars, diffs, parts, indel); // just need the details

		if (in->nSites > 0)
			dist = (diffs+indel)*1. / in->nSites;
		}

	return dist;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

BOOL SetMyTaxonomy(CPNode *node)
    {
	// calc the taxonomy of this node
    
	if (taxonomy == NULL) return FALSE;
	if (node == NULL) return FALSE;
	if (!node->IsBranchNode()) return FALSE;
    
	string				common;
	string				tax;
    
	LPCSTR				def = "not set";
	common = node->attrs.GetString(ATTR_TAXLABEL, def);
	if (common != def) return TRUE;
	common.clear();
    
	for (CPNodeListIter iter=node->edges.begin() ; iter != node->edges.end() ; ++iter)
		{
        CPNode		*child = *iter;
        
        if (node->IsBranchNode())
            {
            SetMyTaxonomy(child);
            }
        
        CSequenceItem *seq = taxonomy->GetSequence(child->title.c_str());
        if (seq != NULL)
            {
            tax = seq->hdr;
            int		pos = tax.find_first_of('\t');
            if (pos < 20)
                tax = tax.substr(pos+1,tax.length());
            TrimRight(tax, "\n\r\t ");
            if (tax.empty())
                {
                string		name = GetNodeName(child);
                DisplayL("No Taxnomy for [%s][%s]\n", name.c_str(), child->title.c_str());
                }
            }
        else if ((seq->hdr == NULL) || (seq->hdr))
            {
            string		name = GetNodeName(child);
            DisplayL("No Taxnomy for [%s][%s]\n", name.c_str(), child->title.c_str());
            }
                    
        if (common.empty())
            common = tax;
        else if (!tax.empty())
            {
            // calc how much is the same between two list
            int			i;
            for (i=0 ; (i < common.length()) && (i < tax.length()) ; ++i)
                {
                    if (common[i] != tax[i])
                        break;
                }
            if (i > 0)
                common = common.substr(0,i);
            else
                common.clear();
            }
		}
    
    if (tax.empty())
        {
        string		name = GetNodeName(node);
        DisplayL("No Taxnomy for [%s][%s]\n", name.c_str(), node->title.c_str());
        }
    
    node->attrs.Add(ATTR_TAXLABEL, common);
	CSequenceItem		*seq = new CSequenceItem(node->title.c_str(), -1, 0); //, common.GetLength(), common);
	seq->AllocateSeqHeader(common.length()+1);
	strcpy(seq->hdr, common.c_str());
    
	CSequenceListIter iter = taxonomy->seqlist.find(seq->name);
	if (iter != taxonomy->seqlist.end())
		{
        delete (*iter).second;
		}
	taxonomy->seqlist[seq->name] = seq;
    
	return TRUE;
    }

///////////////////////////////////////////////////////////////////////////////////////////////////
#define CORE_NAME1 "CORE_NAME1"
#define CORE_NAME2 "CORE_NAME2"

// routine to mark the branch nodes with multiple subtrees with core node
int CalcCoreBranches(CPNode *node, CSequenceFile *core_seq)
    {
    int             count = 0;
    
    if (node == NULL) return count;
    
    if (node->IsBranchNode())
        {
        // for each child
        //      check to see if it has a core sequence
        string          core1;
        string          core2;
        CPNodeList		&children = node->edges;
        for (CPNodeListIter iter=children.begin() ; iter != children.end() ; ++iter)
            {
            CPNode *child = *iter;
            if (CalcCoreBranches(child, core_seq) > 0)
                {
                ++count;
                string      name = child->attrs.GetString(CORE_NAME1);
                if (core1.empty())
                    core1 = name;
                else
                    core2 = name;
                }
            }
            
        node->SetAttr(CORE_NAME1, core1.c_str());
        node->SetAttr(CORE_NAME2, core2.c_str());
        }
    else
        {
        // check to see if the node is in the core sequences
        string      name = GetNodeName(node); 
        if (core_seq->GetSeq(name.c_str()) != NULL)
            {
            ++count; // found in core sequence list
            node->SetAttr(CORE_NAME1, name);
            }
        }

    return count;
    }

// routine to find closest ancestor marked with core multiple core branches

// routine to find branch node containing two given core nodes
CPNode *FindBranchContaining(CPTree *tree, LPCSTR one, LPCSTR two)
    {
    if ((one == NULL) || (two == NULL)) return NULL;
    
    // find the node for each name given
    CPNode *node_one = tree->Find(one);
    if (node_one == NULL) return NULL;
    
    CPNode *node_two = tree->Find(two);
    if (node_two == NULL) return NULL;
    
    // Create list of ancestors for each node
    
    CPNodeList ancestors1;
    CPNodeList ancestors2;
    
    node_one->GetAncestors(ancestors1, FALSE);   // ordered from root to direct parent
    node_two->GetAncestors(ancestors2, FALSE);   // ordered from root to direct parent
    
    // Find the last (lowest in tree) common ancestor
    int i = 0;
    while ((i < ancestors1.size()) && (i < ancestors2.size()) && (ancestors1[i] == ancestors2[i]))
        ++i;
    // ith position is first difference

    if (i == 0) return NULL;
    
    return ancestors1[i-1];
    }

///////////////////////////////////////////////////////////////////////////////////////////////////

// routine to calc the distance between two given branch nodes
double DistanceBetween(CPNode *one, CPNode *two)
    {
    double         dist = 0;
    
    if ((one == NULL) || (two == NULL)) return INT_MAX;
    
    // Create list of ancestors for each node

    CPNodeList ancestors1;
    CPNodeList ancestors2;
    
    one->GetAncestors(ancestors1, FALSE);   // ordered from root to direct parent
    two->GetAncestors(ancestors2, FALSE);   // ordered from root to direct parent

    // Find the last (lowest in tree) common ancestor
    int i = 0;
    while ((i < ancestors1.size()) && (i < ancestors2.size()) && (ancestors1[i] == ancestors2[i]))
            ++i;
    // ith position is first difference
    
    // Distance is the number of branches from each node to common ancestor
//    dist += (ancestors1.size() - i); // distance to common ancestor from node one
//    dist += (ancestors2.size() - i); // distance to common ancestor from node two
    while ((i < ancestors1.size()) || (i < ancestors2.size()))
        {
        if (i < ancestors1.size())
            dist += ancestors1[i]->attrs.GetDouble(ATTR_DISTANCE);
        if (i < ancestors2.size())
            dist += ancestors2[i]->attrs.GetDouble(ATTR_DISTANCE);
            
        ++i;
        }
    
    return dist;
    }

///////////////////////////////////////////////////////////////////////////////////////////////////

// Compare the position of insertion to the original position in a full tree
//      name - name of inserted node
//      parent - node in coreTree where "name" node has been inserted
//      origTree - original full tree containing correct position for "name" node
//      coreTree - core tree in which parent is a member
//  Returns the distance from current position (parent) to correct position
//
double ComparePosition(LPCSTR name, CPNode *parent, CPTree *origTree, CPTree *coreTree)
    {
    double     dist = -1;
    
    // Find the position in the original tree
    CPNode      *orig = origTree->Find(name);
    
    // Find the first parent with at least 2 branchs with core sequences
    string      core2;
    while (core2.empty() && (orig != NULL))
        {
        orig = orig->parent;
        if (orig != NULL)
            core2 = orig->attrs.GetString(CORE_NAME2);
        }
        
    if (orig == NULL)
        {
        //DisplayL("Could not find original position for [%s]\n", name);
        return -3;    // did not find original position
        }
        
    // Find the two core names associated with core ancestor
    string      core1 = orig->attrs.GetString(CORE_NAME1);
    
    // Find same core ancestor in core tree
    CPNode      *core = FindBranchContaining(coreTree, core1.c_str(), core2.c_str());
    if (core == NULL)
        {
        DisplayL("Could not find core position for [%s][%s]\n", core1.c_str(), core2.c_str());
        return -2;    // did not find core position
        }
        
    // if parent is not a branch node, get parent's parent
    if (!parent->IsBranchNode())
        parent = parent->parent;
        
    // Calc distance between core ancestor and insertion point
    dist = DistanceBetween(parent, core);
    //DisplayL("Distance error [%20.20s] = %d\n", name, dist);
    
    return dist;
    }

///////////////////////////////////////////////////////////////////////////////////////////////////

// Compare the position of insertions to the original position in a full tree
void ComparePositions(CInsertPosArray &inserts, CPTree *origTree, CPTree *coreTree)
    {
        
    for (int i=0 ; i < inserts.size() ; ++i)
        {
        string                  name = inserts[i]->seq->name;        

        DisplayL("======================================================================\n"); 
        DisplayL("\nDistance to Insertion: [%20.20s] [%5d sites]\n", name.c_str(), inserts[i]->best.list.size()); 
        
        if (inserts[i]->best.list.size() == 0)
            {
            DisplayL("\tNO MATCHES found for [%s]\n", name.c_str());
            continue;
            }
                  
        CBestList::iterator		iterB = inserts[i]->best.list.begin();

        for (iterB=inserts[i]->best.list.begin() ; iterB != inserts[i]->best.list.end() ; ++iterB)
            {
            CPNode      *parent = iterB->node;
            string      pname = GetNodeName(parent);
            
            int         dist = ComparePosition(name.c_str(), parent, origTree, coreTree);

            DisplayL("\tDistance error from [%20.20s] = %d\n", pname.c_str(), dist);
            }
        }
                
    }
    
///////////////////////////////////////////////////////////////////////////////////////////////////

BOOL FullTree_Initialization()
    {
    if (tree == NULL) return FALSE;
    if (treeSeqfile == NULL) return FALSE;
    if (fullTreeName == NULL) return FALSE;
    
    // Read in original tree
    DisplayL("Reading Newick Tree: [%s]\n", fullTreeName);
    fullTree = ReadNewickTree(fullTreeName);
    if (fullTree == NULL)
        {
        DisplayL("Error accessing Newick Tree file [%s]\n", fullTreeName);
        return FALSE;
        }
    
    DisplayL("Newick Tree Read Completed: %d taxa\n", (fullTree->nodeList.size()+1)/2);
        
    // Set the core names into ancestors
    CalcCoreBranches(fullTree->root, treeSeqfile);
    
    return TRUE;
    }

void TestInsertPositions()
    {
    if (inserts.size() == 0) return;
    if (fullTree == NULL) 
        {
        if (!FullTree_Initialization())
            return;
        }
        
    ComparePositions(inserts, fullTree , tree);
    }

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////