File: nxscharactersblock.cpp

package info (click to toggle)
libncl 2.1.18%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,344 kB
  • ctags: 3,526
  • sloc: xml: 43,716; cpp: 30,400; sh: 10,658; python: 231; makefile: 190
file content (5518 lines) | stat: -rw-r--r-- 207,505 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
//	Copyright (C) 1999-2003 Paul O. Lewis
//
//	This file is part of NCL (Nexus Class Library) version 2.0.
//
//	NCL 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 2 of the License, or
//	(at your option) any later version.
//
//	NCL 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 NCL; if not, write to the Free Software Foundation, Inc.,
//	59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
/**
 * This file includes contributions by Brian O'Meara. August 2005.
 * These changes include the ability to parse continuous data types.
 */
#include <iomanip>
#include <climits>

#include "ncl/nxscharactersblock.h"
#include "ncl/nxsreader.h"
#include "ncl/nxsassumptionsblock.h"
#include "ncl/nxssetreader.h"
#include <algorithm>
#include <iterator>
using namespace std;

CodonRecodingStruct getCodonRecodingStruct(NxsGeneticCodesEnum gCode);
std::vector<NxsDiscreteStateCell> getToCodonRecodingMapper(NxsGeneticCodesEnum gCode);


void NxsDiscreteDatatypeMapper::DebugWriteMapperFields(std::ostream & out) const
{
	out << nStates << "\"fundamental\" states\n";
	out << "Symbols = \"" << symbols << "\"\n";
	if (respectCase)
		out << "Symbol comparison respects case (is case-sensitive)\n";
	else
		out << "Symbol comparison does not respect case (is case-insensitive)\n";
	if (gapChar == '\0')
		out << "No Gaps\n";
	else
		out << "Gap char is " << gapChar << "\n";

	out << "State codes:\n";
	int nsc = (int)GetNumStateCodes();
	for (int scc = 0; scc < nsc; ++scc)
		{
		int sc = scc + sclOffset;
		out << sc << ' ';
		if (sc == NXS_MISSING_CODE)
			out << missing << '\n';
		else if (sc == NXS_GAP_STATE_CODE)
			out << gapChar << '\n';
		else
			{
			const std::set<NxsDiscreteStateCell> & ssfc(GetStateSetForCode(sc));
			std::set<NxsDiscreteStateCell>::const_iterator sIt = ssfc.begin();
			if (ssfc.size() == 1)
				{
				out << symbols[*sIt];
				}
			else
				{
				if (IsPolymorphic(sc))
					out << '(';
				else
					out << '{';
				for (; sIt != ssfc.end(); ++sIt)
					{
					if (*sIt == NXS_MISSING_CODE)
						out << missing;
					else if (*sIt == NXS_GAP_STATE_CODE)
						out << gapChar;
					else
						out << symbols[*sIt];
					}
				if (IsPolymorphic(sc))
					out << ')';
				else
					out << '}';
				}
			out << '\n';
			}
		}

	std::map<char, NxsString>::const_iterator eeIt = extraEquates.begin();
	if (eeIt != extraEquates.end())
		{
		out << "Extra equates:\n";
		for (; eeIt != extraEquates.end(); ++eeIt)
			out << eeIt->first  << " -> " << eeIt->second << '\n';
		}
	out.flush();
}

static unsigned char lcBaseToInd(char );

static unsigned char lcBaseToInd(char c) {
	if (c == 'a')
		return 0;
	if (c == 'c')
		return 1;
	if (c == 'g')
		return 2;
	if (c == 't')
		return 3;
	throw NxsException("Expecting a DNA base");
}

NxsCodonTriplet::NxsCodonTriplet(const char *triplet)
{
	std::string s(triplet);
	if (s.length() != 3)
		throw NxsException("Expecting a triplet of bases");
	NxsString::to_lower(s);
	this->firstPos = lcBaseToInd(s[0]);
	this->secondPos = lcBaseToInd(s[1]);
	this->thirdPos = lcBaseToInd(s[2]);
}


NxsCodonTriplet::MutDescription NxsCodonTriplet::getSingleMut(const NxsCodonTriplet & other) const {
	if (firstPos == other.firstPos) {
		if (secondPos == other.secondPos) {
			if (thirdPos == other.thirdPos)
				return MutDescription(0,0);
			return MutDescription((int)thirdPos, (int)other.thirdPos);
		}
		if (thirdPos == other.thirdPos)
			return MutDescription((int)secondPos, (int)other.secondPos);
		return MutDescription(-1, -1);
	}
	if (secondPos == other.secondPos) {
		if (thirdPos == other.thirdPos)
			return MutDescription((int)firstPos, (int)other.firstPos);
		return MutDescription(-1, -1);
	}
	return MutDescription(-1, -1);
}


/*******************************************************************************
 * deletes "fundamental" states (rather than gaps or ambiguity codes) from a
 * datatype mapper.
 * Equates (default or user-defined) are not supported in the current version of the function
 *	(so this will only work on standard or codons data).
 */

void NxsDiscreteDatatypeMapper::DeleteStateIndices(const std::set<NxsDiscreteStateCell> & deletedInds)
{
	if (deletedInds.empty())
		return;
	if (*(deletedInds.begin()) < 0 || *(deletedInds.rbegin()) >= (NxsDiscreteStateCell)this->nStates)
		throw NxsException("DeleteStateIndices can only delete fundamental states");
	if (!(NxsCharactersBlock::GetDefaultEquates(this->datatype).empty() && extraEquates.empty()))
		throw NxsException("DeleteStateIndices can not currently work on datatypes with equates");
	std::vector<NxsDiscreteStateCell> remap;
	NxsDiscreteStateCell newIndex = 0;
	std::string nsym;
	for (NxsDiscreteStateCell i = 0; i < (NxsDiscreteStateCell) this->nStates; ++i)
		{
		if (deletedInds.find(i) == deletedInds.end())
			{
			remap.push_back(newIndex++);
			nsym.append(1, symbols[i]);
			}
		else
			remap.push_back(NXS_INVALID_STATE_CODE);
		}
	const unsigned oldNStates = nStates;
	std::vector<NxsDiscreteStateSetInfo> oldStateSetsVec = this->stateSetsVec;
	symbols = nsym;

	this->RefreshMappings(0L);

	for (unsigned i = oldNStates - sclOffset; i < oldStateSetsVec.size(); ++i)
		{
		const NxsDiscreteStateSetInfo & ssi = oldStateSetsVec[i];
		std::set<NxsDiscreteStateCell> stSet;
		for (std::set<NxsDiscreteStateCell>::const_iterator s = ssi.states.begin(); s != ssi.states.end(); ++s)
			{
			NxsDiscreteStateCell u = *s;
			if (u < 0)
				stSet.insert(u);
			else
				{
				NxsDiscreteStateCell r = remap.at(u);
				if (r >= 0)
					stSet.insert(r);
				}
			}
		// We have to add every "extra" state set, so that the indexing for the higher state codes is just shifted by the number of states deleted
		AddStateSet(stSet, ssi.nexusSymbol, true, ssi.isPolymorphic);
		}
}

std::vector<NxsDiscreteStateCell> getToCodonRecodingMapper(NxsGeneticCodesEnum gCode)
{
	std::vector<NxsDiscreteStateCell> v;
	if(gCode == NXS_GCODE_STANDARD) {
		const NxsDiscreteStateCell trnxs_gcode_standard[] = {0, 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, -1, 48, -1, 49, 50, 51, 52, 53, -1, 54, 55, 56, 57, 58, 59, 60};
		std::copy(trnxs_gcode_standard, trnxs_gcode_standard + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_VERT_MITO) {
		const NxsDiscreteStateCell trnxs_gcode_vert_mito[] = {0, 1, 2, 3, 4, 5, 6, 7, -1, 8, -1, 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, -1, 46, -1, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59};
		std::copy(trnxs_gcode_vert_mito, trnxs_gcode_vert_mito + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_YEAST_MITO) {
		const NxsDiscreteStateCell trnxs_gcode_yeast_mito[] = {0, 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, -1, 48, -1, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61};
		std::copy(trnxs_gcode_yeast_mito, trnxs_gcode_yeast_mito + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_MOLD_MITO) {
		const NxsDiscreteStateCell trnxs_gcode_mold_mito[] = {0, 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, -1, 48, -1, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61};
		std::copy(trnxs_gcode_mold_mito, trnxs_gcode_mold_mito + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_INVERT_MITO) {
		const NxsDiscreteStateCell trnxs_gcode_invert_mito[] = {0, 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, -1, 48, -1, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61};
		std::copy(trnxs_gcode_invert_mito, trnxs_gcode_invert_mito + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_CILIATE) {
		const NxsDiscreteStateCell trnxs_gcode_ciliate[] = {0, 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, -1, 56, 57, 58, 59, 60, 61, 62};
		std::copy(trnxs_gcode_ciliate, trnxs_gcode_ciliate + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_ECHINO_MITO) {
		const NxsDiscreteStateCell trnxs_gcode_echino_mito[] = {0, 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, -1, 48, -1, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61};
		std::copy(trnxs_gcode_echino_mito, trnxs_gcode_echino_mito + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_EUPLOTID) {
		const NxsDiscreteStateCell trnxs_gcode_euplotid[] = {0, 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, -1, 48, -1, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61};
		std::copy(trnxs_gcode_euplotid, trnxs_gcode_euplotid + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_PLANT_PLASTID) {
		const NxsDiscreteStateCell trnxs_gcode_plant_plastid[] = {0, 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, -1, 48, -1, 49, 50, 51, 52, 53, -1, 54, 55, 56, 57, 58, 59, 60};
		std::copy(trnxs_gcode_plant_plastid, trnxs_gcode_plant_plastid + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_ALT_YEAST) {
		const NxsDiscreteStateCell trnxs_gcode_alt_yeast[] = {0, 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, -1, 48, -1, 49, 50, 51, 52, 53, -1, 54, 55, 56, 57, 58, 59, 60};
		std::copy(trnxs_gcode_alt_yeast, trnxs_gcode_alt_yeast + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_ASCIDIAN_MITO) {
		const NxsDiscreteStateCell trnxs_gcode_ascidian_mito[] = {0, 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, -1, 48, -1, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61};
		std::copy(trnxs_gcode_ascidian_mito, trnxs_gcode_ascidian_mito + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_ALT_FLATWORM_MITO) {
		const NxsDiscreteStateCell trnxs_gcode_alt_flatworm_mito[] = {0, 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, -1, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62};
		std::copy(trnxs_gcode_alt_flatworm_mito, trnxs_gcode_alt_flatworm_mito + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_BLEPHARISMA_MACRO) {
		const NxsDiscreteStateCell trnxs_gcode_blepharisma_macro[] = {0, 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, -1, 48, 49, 50, 51, 52, 53, 54, -1, 55, 56, 57, 58, 59, 60, 61};
		std::copy(trnxs_gcode_blepharisma_macro, trnxs_gcode_blepharisma_macro + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_CHLOROPHYCEAN_MITO) {
		const NxsDiscreteStateCell trnxs_gcode_chlorophycean_mito[] = {0, 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, -1, 48, 49, 50, 51, 52, 53, 54, -1, 55, 56, 57, 58, 59, 60, 61};
		std::copy(trnxs_gcode_chlorophycean_mito, trnxs_gcode_chlorophycean_mito + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_TREMATODE_MITO) {
		const NxsDiscreteStateCell trnxs_gcode_trematode_mito[] = {0, 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, -1, 48, -1, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61};
		std::copy(trnxs_gcode_trematode_mito, trnxs_gcode_trematode_mito + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_SCENEDESMUS_MITO) {
		const NxsDiscreteStateCell trnxs_gcode_scenedesmus_mito[] = {0, 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, -1, 48, 49, 50, -1, 51, 52, 53, -1, 54, 55, 56, 57, 58, 59, 60};
		std::copy(trnxs_gcode_scenedesmus_mito, trnxs_gcode_scenedesmus_mito + 64, std::back_inserter(v));
		return v;
	}
	if(gCode == NXS_GCODE_THRAUSTOCHYTRIUM_MITO) {
		const NxsDiscreteStateCell trnxs_gcode_thraustochytrium_mito[] = {0, 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, -1, 48, -1, 49, 50, 51, 52, 53, -1, 54, 55, 56, -1, 57, 58, 59};
		std::copy(trnxs_gcode_thraustochytrium_mito, trnxs_gcode_thraustochytrium_mito + 64, std::back_inserter(v));
		return v;
	}
	throw NxsException("Unrecognized genetic code.");
}


CodonRecodingStruct getCodonRecodingStruct(NxsGeneticCodesEnum gCode)
{
	CodonRecodingStruct c;
	unsigned n;

	if(gCode == NXS_GCODE_STANDARD) {
		const int ccitacnxs_gcode_standard[] = {0, 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, 49, 51, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63};
		n = 61;
		const int caaindnxs_gcode_standard[] = {8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_standard[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_standard, ccitacnxs_gcode_standard + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_standard, caaindnxs_gcode_standard + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_standard, ccodstrnxs_gcode_standard + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_VERT_MITO) {
		const int ccitacnxs_gcode_vert_mito[] = {0, 1, 2, 3, 4, 5, 6, 7, 9, 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, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63};
		n = 60;
		const int caaindnxs_gcode_vert_mito[] = {8, 11, 8, 11, 16, 16, 16, 16, 15, 15, 10, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_vert_mito[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGC", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_vert_mito, ccitacnxs_gcode_vert_mito + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_vert_mito, caaindnxs_gcode_vert_mito + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_vert_mito, ccodstrnxs_gcode_vert_mito + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_YEAST_MITO) {
		const int ccitacnxs_gcode_yeast_mito[] = {0, 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, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63};
		n = 62;
		const int caaindnxs_gcode_yeast_mito[] = {8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_yeast_mito[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_yeast_mito, ccitacnxs_gcode_yeast_mito + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_yeast_mito, caaindnxs_gcode_yeast_mito + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_yeast_mito, ccodstrnxs_gcode_yeast_mito + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_MOLD_MITO) {
		const int ccitacnxs_gcode_mold_mito[] = {0, 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, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63};
		n = 62;
		const int caaindnxs_gcode_mold_mito[] = {8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_mold_mito[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_mold_mito, ccitacnxs_gcode_mold_mito + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_mold_mito, caaindnxs_gcode_mold_mito + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_mold_mito, ccodstrnxs_gcode_mold_mito + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_INVERT_MITO) {
		const int ccitacnxs_gcode_invert_mito[] = {0, 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, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63};
		n = 62;
		const int caaindnxs_gcode_invert_mito[] = {8, 11, 8, 11, 16, 16, 16, 16, 15, 15, 15, 15, 10, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_invert_mito[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_invert_mito, ccitacnxs_gcode_invert_mito + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_invert_mito, caaindnxs_gcode_invert_mito + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_invert_mito, ccodstrnxs_gcode_invert_mito + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_CILIATE) {
		const int ccitacnxs_gcode_ciliate[] = {0, 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, 57, 58, 59, 60, 61, 62, 63};
		n = 63;
		const int caaindnxs_gcode_ciliate[] = {8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 13, 19, 13, 19, 15, 15, 15, 15, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_ciliate[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAA", "TAC", "TAG", "TAT", "TCA", "TCC", "TCG", "TCT", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_ciliate, ccitacnxs_gcode_ciliate + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_ciliate, caaindnxs_gcode_ciliate + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_ciliate, ccodstrnxs_gcode_ciliate + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_ECHINO_MITO) {
		const int ccitacnxs_gcode_echino_mito[] = {0, 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, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63};
		n = 62;
		const int caaindnxs_gcode_echino_mito[] = {11, 11, 8, 11, 16, 16, 16, 16, 15, 15, 15, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_echino_mito[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_echino_mito, ccitacnxs_gcode_echino_mito + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_echino_mito, caaindnxs_gcode_echino_mito + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_echino_mito, ccodstrnxs_gcode_echino_mito + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_EUPLOTID) {
		const int ccitacnxs_gcode_euplotid[] = {0, 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, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63};
		n = 62;
		const int caaindnxs_gcode_euplotid[] = {8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 1, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_euplotid[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_euplotid, ccitacnxs_gcode_euplotid + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_euplotid, caaindnxs_gcode_euplotid + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_euplotid, ccodstrnxs_gcode_euplotid + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_PLANT_PLASTID) {
		const int ccitacnxs_gcode_plant_plastid[] = {0, 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, 49, 51, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63};
		n = 61;
		const int caaindnxs_gcode_plant_plastid[] = {8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_plant_plastid[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_plant_plastid, ccitacnxs_gcode_plant_plastid + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_plant_plastid, caaindnxs_gcode_plant_plastid + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_plant_plastid, ccodstrnxs_gcode_plant_plastid + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_ALT_YEAST) {
		const int ccitacnxs_gcode_alt_yeast[] = {0, 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, 49, 51, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63};
		n = 61;
		const int caaindnxs_gcode_alt_yeast[] = {8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 15, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_alt_yeast[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_alt_yeast, ccitacnxs_gcode_alt_yeast + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_alt_yeast, caaindnxs_gcode_alt_yeast + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_alt_yeast, ccodstrnxs_gcode_alt_yeast + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_ASCIDIAN_MITO) {
		const int ccitacnxs_gcode_ascidian_mito[] = {0, 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, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63};
		n = 62;
		const int caaindnxs_gcode_ascidian_mito[] = {8, 11, 8, 11, 16, 16, 16, 16, 5, 15, 5, 15, 10, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_ascidian_mito[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_ascidian_mito, ccitacnxs_gcode_ascidian_mito + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_ascidian_mito, caaindnxs_gcode_ascidian_mito + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_ascidian_mito, ccodstrnxs_gcode_ascidian_mito + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_ALT_FLATWORM_MITO) {
		const int ccitacnxs_gcode_alt_flatworm_mito[] = {0, 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, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63};
		n = 63;
		const int caaindnxs_gcode_alt_flatworm_mito[] = {11, 11, 8, 11, 16, 16, 16, 16, 15, 15, 15, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_alt_flatworm_mito[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAA", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_alt_flatworm_mito, ccitacnxs_gcode_alt_flatworm_mito + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_alt_flatworm_mito, caaindnxs_gcode_alt_flatworm_mito + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_alt_flatworm_mito, ccodstrnxs_gcode_alt_flatworm_mito + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_BLEPHARISMA_MACRO) {
		const int ccitacnxs_gcode_blepharisma_macro[] = {0, 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, 49, 50, 51, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63};
		n = 62;
		const int caaindnxs_gcode_blepharisma_macro[] = {8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 13, 19, 15, 15, 15, 15, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_blepharisma_macro[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAG", "TAT", "TCA", "TCC", "TCG", "TCT", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_blepharisma_macro, ccitacnxs_gcode_blepharisma_macro + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_blepharisma_macro, caaindnxs_gcode_blepharisma_macro + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_blepharisma_macro, ccodstrnxs_gcode_blepharisma_macro + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_CHLOROPHYCEAN_MITO) {
		const int ccitacnxs_gcode_chlorophycean_mito[] = {0, 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, 49, 50, 51, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63};
		n = 62;
		const int caaindnxs_gcode_chlorophycean_mito[] = {8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 9, 19, 15, 15, 15, 15, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_chlorophycean_mito[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAG", "TAT", "TCA", "TCC", "TCG", "TCT", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_chlorophycean_mito, ccitacnxs_gcode_chlorophycean_mito + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_chlorophycean_mito, caaindnxs_gcode_chlorophycean_mito + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_chlorophycean_mito, ccodstrnxs_gcode_chlorophycean_mito + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_TREMATODE_MITO) {
		const int ccitacnxs_gcode_trematode_mito[] = {0, 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, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63};
		n = 62;
		const int caaindnxs_gcode_trematode_mito[] = {11, 11, 8, 11, 16, 16, 16, 16, 15, 15, 15, 15, 10, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_trematode_mito[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_trematode_mito, ccitacnxs_gcode_trematode_mito + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_trematode_mito, caaindnxs_gcode_trematode_mito + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_trematode_mito, ccodstrnxs_gcode_trematode_mito + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_SCENEDESMUS_MITO) {
		const int ccitacnxs_gcode_scenedesmus_mito[] = {0, 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, 49, 50, 51, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63};
		n = 61;
		const int caaindnxs_gcode_scenedesmus_mito[] = {8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 9, 19, 15, 15, 15, 1, 18, 1, 9, 4, 9, 4};
		const char * ccodstrnxs_gcode_scenedesmus_mito[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAG", "TAT", "TCC", "TCG", "TCT", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_scenedesmus_mito, ccitacnxs_gcode_scenedesmus_mito + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_scenedesmus_mito, caaindnxs_gcode_scenedesmus_mito + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_scenedesmus_mito, ccodstrnxs_gcode_scenedesmus_mito + n, std::back_inserter(c.codonStrings));
		return c;
	}
	if(gCode == NXS_GCODE_THRAUSTOCHYTRIUM_MITO) {
		const int ccitacnxs_gcode_thraustochytrium_mito[] = {0, 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, 49, 51, 52, 53, 54, 55, 57, 58, 59, 61, 62, 63};
		n = 60;
		const int caaindnxs_gcode_thraustochytrium_mito[] = {8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 15, 15, 15, 15, 1, 18, 1, 4, 9, 4};
		const char * ccodstrnxs_gcode_thraustochytrium_mito[] = {"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", "TAC", "TAT", "TCA", "TCC", "TCG", "TCT", "TGC", "TGG", "TGT", "TTC", "TTG", "TTT"};
		std::copy(ccitacnxs_gcode_thraustochytrium_mito, ccitacnxs_gcode_thraustochytrium_mito + n, std::back_inserter(c.compressedCodonIndToAllCodonsInd));
		std::copy(caaindnxs_gcode_thraustochytrium_mito, caaindnxs_gcode_thraustochytrium_mito + n, std::back_inserter(c.aaInd));
		std::copy(ccodstrnxs_gcode_thraustochytrium_mito, ccodstrnxs_gcode_thraustochytrium_mito + n, std::back_inserter(c.codonStrings));
		return c;
	}
	throw NxsException("Unrecognized genetic code.");
}

CodonRecodingStruct NxsCharactersBlock::RemoveStopCodons(NxsGeneticCodesEnum gCode)
{
	NxsDiscreteDatatypeMapper * mapper = this->GetMutableDatatypeMapperForChar(0);
	if (mapper == 0L)
		throw NxsException("Invalid characters block (no datatype mapper)");
	if (mapper->GetDatatype() != codon)
		throw NxsException("Characters block must be of the type codons when RemoveStopCodons is called");
	if (mapper->geneticCode != NXS_GCODE_NO_CODE)
		throw NxsException("Characters block must be an uncompressed codons type when RemoveStopCodons is called");

	const std::vector<NxsDiscreteStateCell> v = getToCodonRecodingMapper(gCode);
	CodonRecodingStruct c = getCodonRecodingStruct(gCode);
	const unsigned nRS = (unsigned)c.compressedCodonIndToAllCodonsInd.size();
	const unsigned offset = 64 - nRS;
	NxsDiscreteStateMatrix	dMat(this->discreteMatrix);
	unsigned rowInd = 0;
	for (NxsDiscreteStateMatrix::iterator rowIt = dMat.begin(); rowIt != dMat.end(); ++rowIt)
		{
		NxsDiscreteStateRow & row = *rowIt;
		unsigned charInd = 0;
		for (NxsDiscreteStateRow::iterator cellIt = row.begin(); cellIt != row.end(); ++cellIt)
			{
			const NxsDiscreteStateCell cell = *cellIt;
			if (cell >= 64)
				*cellIt = cell - offset;
			else if (cell >= 0)
				{
				const NxsDiscreteStateCell recoded = v[cell];
				if (recoded < 0)
					{
					NxsString m;
					m << "Stop codon found at character ";
					m << charInd + 1;
					m << " for taxon ";
					m << rowInd + 1;
					throw NxsException(m);
					}
				*cellIt = recoded;
				}
			++charInd;
			}
		++rowInd;
		}
	dMat.swap(this->discreteMatrix);
	std::set<NxsDiscreteStateCell> deletedInds;
	for (NxsDiscreteStateCell i = 0; i < 64; ++i)
		{
		if (v[(int)i] < 0)
			deletedInds.insert(i);
		}
	mapper->DeleteStateIndices(deletedInds);
	return c;
}

unsigned NxsCharactersBlock::NumAmbigInTaxon(const unsigned taxInd, const NxsUnsignedSet * charIndices, const bool countOnlyCompletelyMissing, const bool treatGapsAsMissing) const
{
	const NxsDiscreteStateRow & row = GetDiscreteMatrixRow(taxInd);
	unsigned nAmbig = 0;
	const NxsDiscreteDatatypeMapper * m;
	if (charIndices == NULL)
		{
		unsigned cInd = 0;
		for (NxsDiscreteStateRow::const_iterator cellIt = row.begin(); cellIt != row.end(); ++cellIt)
			{
			m = GetDatatypeMapperForChar(cInd++);
			NCL_ASSERT(m);
			const NxsDiscreteStateCell & c = *cellIt;
			if (c < 0 || c >= (NxsDiscreteStateCell) m->GetNumStates())
				{
				if (countOnlyCompletelyMissing)
					{
					if (c == NXS_MISSING_CODE)
						nAmbig++;
					}
				else
					{
					if (c != NXS_GAP_STATE_CODE || treatGapsAsMissing)
						nAmbig++;
					}
				}
			}
		}
	else
		{
		for (NxsUnsignedSet::const_iterator c = charIndices->begin(); c != charIndices->end(); ++c)
			{
			const unsigned cIndex = *c;
			m = GetDatatypeMapperForChar(cIndex);
			const NxsDiscreteStateCell & sc = row.at(cIndex);
			if (sc < 0 || sc >= (NxsDiscreteStateCell) m->GetNumStates())
				{
				if (countOnlyCompletelyMissing)
					{
					if (sc == NXS_MISSING_CODE)
						nAmbig++;
					}
				else
					{
					if (sc != NXS_GAP_STATE_CODE || treatGapsAsMissing)
						nAmbig++;
					}
				}
			}
		}
	return nAmbig;
}

bool NxsCharactersBlock::FirstTaxonStatesAreSubsetOfSecond(
  const unsigned firstTaxonInd,
  const unsigned secondTaxonInd,
  const NxsUnsignedSet * charIndices,
  const bool treatAmbigAsMissing,
  const bool treatGapAsMissing) const
{
	const NxsDiscreteStateRow & firstRow = GetDiscreteMatrixRow(firstTaxonInd);
	const NxsDiscreteStateRow & secondRow = GetDiscreteMatrixRow(secondTaxonInd);
	const NxsDiscreteDatatypeMapper * m;
	if (charIndices == NULL)
		{
		unsigned cInd = 0;
		NxsDiscreteStateRow::const_iterator firstIt = firstRow.begin();
		NxsDiscreteStateRow::const_iterator secondIt = secondRow.begin();
		for (; firstIt != firstRow.end(); ++firstIt, ++secondIt)
			{
			m = GetDatatypeMapperForChar(cInd++);
			const NxsDiscreteStateCell ns = (NxsDiscreteStateCell) m->GetNumStates();
			NxsDiscreteStateCell f = *firstIt;
			NxsDiscreteStateCell s = *secondIt;
			if (treatAmbigAsMissing)
				{
				if (f >= ns)
					f = NXS_MISSING_CODE;
				if (s >= ns)
					s = NXS_MISSING_CODE;
				}
			if (!m->FirstIsSubset(f, s, treatGapAsMissing))
				return false;
			}
		}
	else
		{
		for (NxsUnsignedSet::const_iterator c = charIndices->begin(); c != charIndices->end(); ++c)
			{
			const unsigned cIndex = *c;
			m = GetDatatypeMapperForChar(cIndex);
			const NxsDiscreteStateCell ns = m->GetNumStates();
			NxsDiscreteStateCell f = firstRow.at(cIndex);
			NxsDiscreteStateCell s = secondRow.at(cIndex);
			if (treatAmbigAsMissing)
				{
				if (f >= ns)
					f = NXS_MISSING_CODE;
				if (s >= ns)
					s = NXS_MISSING_CODE;
				}
			if (!m->FirstIsSubset(f, s, treatGapAsMissing))
				return false;
			}
		}
	return true;
}

std::pair<unsigned, unsigned> NxsCharactersBlock::GetPairwiseDist(
  const unsigned firstTaxonInd,
  const unsigned secondTaxonInd,
  const NxsUnsignedSet * charIndices,
  const bool treatAmbigAsMissing,
  const bool treatGapAsMissing) const
{
	const NxsDiscreteStateRow & firstRow = GetDiscreteMatrixRow(firstTaxonInd);
	const NxsDiscreteStateRow & secondRow = GetDiscreteMatrixRow(secondTaxonInd);
	const NxsDiscreteDatatypeMapper * m;
	unsigned nDiffs = 0;
	unsigned nSites = 0;
	if (charIndices == NULL)
		{
		unsigned cInd = 0;
		NxsDiscreteStateRow::const_iterator firstIt = firstRow.begin();
		NxsDiscreteStateRow::const_iterator secondIt = secondRow.begin();
		for (; firstIt != firstRow.end(); ++firstIt, ++secondIt)
			{
			m = GetDatatypeMapperForChar(cInd++);
			const NxsDiscreteStateCell ns = m->GetNumStates();
			NxsDiscreteStateCell f = *firstIt;
			NxsDiscreteStateCell s = *secondIt;
			if (treatAmbigAsMissing)
				{
				if (f >= ns)
					f = NXS_MISSING_CODE;
				if (s >= ns)
					s = NXS_MISSING_CODE;
				}
			if (f < 0 || s < 0)
				{
				if (treatGapAsMissing && (f == NXS_GAP_STATE_CODE || s == NXS_GAP_STATE_CODE))
					continue;
				if (f == NXS_MISSING_CODE || s == NXS_MISSING_CODE)
					continue;
				}
			nSites++;
			const std::set<NxsDiscreteStateCell> & ssim = m->GetStateIntersection(f, s);
			if (!ssim.empty())
				++nDiffs;
			}
		}
	else
		{
		for (NxsUnsignedSet::const_iterator c = charIndices->begin(); c != charIndices->end(); ++c)
			{
			m = GetDatatypeMapperForChar(*c);
			const NxsDiscreteStateCell ns = (NxsDiscreteStateCell) m->GetNumStates();
			NxsDiscreteStateCell f = firstRow.at(*c);
			NxsDiscreteStateCell s = secondRow.at(*c);
			if (treatAmbigAsMissing)
				{
				if (f >= ns)
					f = NXS_MISSING_CODE;
				if (s >= ns)
					s = NXS_MISSING_CODE;
				}
			if (f < 0 || s < 0)
				{
				if (treatGapAsMissing && (f == NXS_GAP_STATE_CODE || s == NXS_GAP_STATE_CODE))
					continue;
				if (f == NXS_MISSING_CODE || s == NXS_MISSING_CODE)
					continue;
				}
			nSites++;
			const std::set<NxsDiscreteStateCell> & ssi = m->GetStateIntersection(f, s);
			if (!ssi.empty())
				++nDiffs;
			}
		}
	return std::pair<unsigned, unsigned>(nDiffs, nSites);
}


void NxsDiscreteDatatypeMapper::BuildStateSubsetMatrix() const
{
	if (stateIntersectionMatrix.empty())
		BuildStateIntersectionMatrix();
	isStateSubsetMatrix.clear();
	isStateSubsetMatrixGapsMissing.clear();
	const unsigned nsPlus = (unsigned)stateSetsVec.size();
	IsStateSubsetRow r(nsPlus, false);
	isStateSubsetMatrix.assign(nsPlus, r);
	isStateSubsetMatrixGapsMissing.assign(nsPlus, r);
	for (unsigned i = 0; i < nsPlus; ++i)
		{
		for (unsigned j = 0; j < nsPlus; ++j)
			{
			if (!stateIntersectionMatrix[i][j].empty())
				{
				isStateSubsetMatrix[i][j] = true;
				isStateSubsetMatrixGapsMissing[i][j] = true;
				}
			}
		}
	isStateSubsetMatrixGapsMissing[0][1] = true;
	isStateSubsetMatrixGapsMissing[1][0] = true;
}

void NxsDiscreteDatatypeMapper::BuildStateIntersectionMatrix() const
{
	const std::set<NxsDiscreteStateCell> emptySet;

	stateIntersectionMatrix.clear();

	const unsigned nsPlus = (unsigned const)stateSetsVec.size();
	const unsigned offset = (unsigned)(sclOffset + 2);
	StateIntersectionRow emptyRow(nsPlus, emptySet);
	stateIntersectionMatrix.assign(nsPlus, emptyRow);
	for (unsigned i = offset; i < nsPlus; ++i)
		{
		for (unsigned j = i; j < nsPlus; ++j)
			{
			const unsigned offi = i + sclOffset;
			const unsigned offj = j + sclOffset;
			std::set<NxsDiscreteStateCell> intersect;
			const std::set<NxsDiscreteStateCell>	&fs =  GetStateSetForCode(offi);
			const std::set<NxsDiscreteStateCell>	&ss =  GetStateSetForCode(offj);
			set_intersection(fs.begin(), fs.end(), ss.begin(), ss.end(), inserter(intersect, intersect.begin()));
			stateIntersectionMatrix[i - NXS_GAP_STATE_CODE][j - NXS_GAP_STATE_CODE] = intersect;
			if (i != j)
				stateIntersectionMatrix[j - NXS_GAP_STATE_CODE][i - NXS_GAP_STATE_CODE] = stateIntersectionMatrix[i - NXS_GAP_STATE_CODE][j - NXS_GAP_STATE_CODE];
			}
		}

	std::set<NxsDiscreteStateCell> tmpSet;
	NCL_ASSERT(1 == NXS_MISSING_CODE - NXS_GAP_STATE_CODE);
	tmpSet.insert(NXS_GAP_STATE_CODE);
	stateIntersectionMatrix[0][0] = tmpSet;

	tmpSet.clear();
	tmpSet.insert(NXS_MISSING_CODE);
	stateIntersectionMatrix[1][1] = tmpSet;
	for (unsigned i = offset; i < nsPlus; ++i)
		{
		const unsigned offi = i + sclOffset;
		stateIntersectionMatrix[1][i - NXS_GAP_STATE_CODE] = GetStateSetForCode(offi);
		}
}


NxsGeneticCodesEnum geneticCodeNameToEnum(std::string n)
{
	NxsString::to_lower(n);
	if (n == "standard")
		return NXS_GCODE_STANDARD;
	if (n == "vertmito")
		return NXS_GCODE_VERT_MITO;
	if (n == "yeastmito")
		return NXS_GCODE_YEAST_MITO;
	if (n == "moldmito")
		return NXS_GCODE_MOLD_MITO;
	if (n == "invertmito")
		return NXS_GCODE_INVERT_MITO;
	if (n == "ciliate")
		return NXS_GCODE_CILIATE;
	if (n == "echinomito")
		return NXS_GCODE_ECHINO_MITO;
	if (n == "euplotid")
		return NXS_GCODE_EUPLOTID;
	if (n == "plantplastid")
		return NXS_GCODE_PLANT_PLASTID;
	if (n == "altyeast")
		return NXS_GCODE_ALT_YEAST;
	if (n == "ascidianmito")
		return NXS_GCODE_ASCIDIAN_MITO;
	if (n == "altflatwormmito")
		return NXS_GCODE_ALT_FLATWORM_MITO;
	if (n == "blepharismamacro")
		return NXS_GCODE_BLEPHARISMA_MACRO;
	if (n == "chlorophyceanmito")
		return NXS_GCODE_CHLOROPHYCEAN_MITO;
	if (n == "trematodemito")
		return NXS_GCODE_TREMATODE_MITO;
	if (n == "scenedesmusmito")
		return NXS_GCODE_SCENEDESMUS_MITO;
	if (n == "thraustochytriummito")
		return NXS_GCODE_THRAUSTOCHYTRIUM_MITO;
	NxsString err = "Unrecognized genetic code name: ";
	err << n;
	throw NxsException(err);
}

std::string geneticCodeEnumToName(NxsGeneticCodesEnum n)
{
	if (n == NXS_GCODE_STANDARD)
		return "Standard";
	if (n == NXS_GCODE_VERT_MITO)
		return "VertMito";
	if (n == NXS_GCODE_YEAST_MITO)
		return "YeastMito";
	if (n == NXS_GCODE_MOLD_MITO)
		return "MoldMito";
	if (n == NXS_GCODE_INVERT_MITO)
		return "InvertMito";
	if (n == NXS_GCODE_CILIATE)
		return "Ciliate";
	if (n == NXS_GCODE_ECHINO_MITO)
		return "EchinoMito";
	if (n == NXS_GCODE_EUPLOTID)
		return "Euplotid";
	if (n == NXS_GCODE_PLANT_PLASTID)
		return "PlantPlastid";
	if (n == NXS_GCODE_ALT_YEAST)
		return "AltYeast";
	if (n == NXS_GCODE_ASCIDIAN_MITO)
		return "AscidianMito";
	if (n == NXS_GCODE_ALT_FLATWORM_MITO)
		return "AltFlatwormMito";
	if (n == NXS_GCODE_BLEPHARISMA_MACRO)
		return "BlepharismaMacro";
	if (n == NXS_GCODE_CHLOROPHYCEAN_MITO)
		return "ChlorophyceanMito";
	if (n == NXS_GCODE_TREMATODE_MITO)
		return "Trematodemito";
	if (n == NXS_GCODE_SCENEDESMUS_MITO)
		return "ScenedesmusMito";
	if (n == NXS_GCODE_THRAUSTOCHYTRIUM_MITO)
		return "ThraustochytriumMito";
	NxsString err = "Unrecognized genetic code enumeration: ";
	err << n;
	throw NxsException(err);
}

std::vector<std::string> getGeneticCodeNames()
{
	std::vector<std::string> n(NXS_GCODE_CODE_ENUM_SIZE);
	n[NXS_GCODE_STANDARD] = "Standard" ;
	n[NXS_GCODE_VERT_MITO] = "VertMito" ;
	n[NXS_GCODE_YEAST_MITO] = "YeastMito" ;
	n[NXS_GCODE_MOLD_MITO] = "MoldMito" ;
	n[NXS_GCODE_INVERT_MITO] = "InvertMito" ;
	n[NXS_GCODE_CILIATE] = "Ciliate" ;
	n[NXS_GCODE_ECHINO_MITO] = "EchinoMito" ;
	n[NXS_GCODE_EUPLOTID] = "Euplotid" ;
	n[NXS_GCODE_PLANT_PLASTID] = "PlantPlastid" ;
	n[NXS_GCODE_ALT_YEAST] = "AltYeast" ;
	n[NXS_GCODE_ASCIDIAN_MITO] = "AscidianMito" ;
	n[NXS_GCODE_ALT_FLATWORM_MITO] = "AltFlatwormMito" ;
	n[NXS_GCODE_BLEPHARISMA_MACRO] = "BlepharismaMacro" ;
	n[NXS_GCODE_CHLOROPHYCEAN_MITO] = "ChlorophyceanMito" ;
	n[NXS_GCODE_TREMATODE_MITO] = "TrematodeMito" ;
	n[NXS_GCODE_SCENEDESMUS_MITO] = "ScenedesmusMito" ;
	n[NXS_GCODE_THRAUSTOCHYTRIUM_MITO] = "ThraustochytriumMito" ;
	return n;
}



/*
  code index 0 => "Standard"
  code index 1 => "Vertebrate Mitochondrial"
  code index 2 => "Yeast Mitochondrial"
  code index 3 => "Mold Mitochondrial; Protozoan Mitochondrial; Coelenterate Mitochondrial; Mycoplasma; Spiroplasma"
  code index 4 => "Invertebrate Mitochondrial"
  code index 5 => "Ciliate Nuclear; Dasycladacean Nuclear; Hexamita Nuclear"
  code index 8 => "Echinoderm Mitochondrial; Flatworm Mitochondrial"
  code index 9 => "Euplotid Nuclear"
  code index 10 => "Bacterial and Plant Plastid"
  code index 11 => "Alternative Yeast Nuclear"
  code index 12 => "Ascidian Mitochondrial"
  code index 13 => "Alternative Flatworm Mitochondrial"
  code index 14 => "Blepharisma Macronuclear"
  code index 15 => "Chlorophycean Mitochondrial"
  code index 20 => "Trematode Mitochondrial"
  code index 21 => "Scenedesmus obliquus Mitochondrial"
  code index 22 => "Thraustochytrium Mitochondrial"
*/
std::string getGeneticCodeAAOrder(NxsGeneticCodesEnum codeIndex)
{
	std::vector<std::string> code(NXS_GCODE_CODE_ENUM_SIZE);
	code[NXS_GCODE_STANDARD] =  "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSS*CWCLFLF";
	code[NXS_GCODE_VERT_MITO] = "KNKNTTTT*S*SMIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSSWCWCLFLF";
	code[NXS_GCODE_YEAST_MITO] = "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSSWCWCLFLF";
	code[NXS_GCODE_MOLD_MITO] = "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSSWCWCLFLF";
	code[NXS_GCODE_INVERT_MITO] = "KNKNTTTTSSSSMIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSSWCWCLFLF";
	code[NXS_GCODE_CILIATE] = "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVVQYQYSSSS*CWCLFLF";
	code[NXS_GCODE_ECHINO_MITO] = "NNKNTTTTSSSSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSSWCWCLFLF";
	code[NXS_GCODE_EUPLOTID] = "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSSCCWCLFLF";
	code[NXS_GCODE_PLANT_PLASTID] = "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSS*CWCLFLF";
	code[NXS_GCODE_ALT_YEAST] = "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLSLEDEDAAAAGGGGVVVV*Y*YSSSS*CWCLFLF";
	code[NXS_GCODE_ASCIDIAN_MITO] = "KNKNTTTTGSGSMIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSSWCWCLFLF";
	code[NXS_GCODE_ALT_FLATWORM_MITO] = "NNKNTTTTSSSSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVVYY*YSSSSWCWCLFLF";
	code[NXS_GCODE_BLEPHARISMA_MACRO] = "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*YQYSSSS*CWCLFLF";
	code[NXS_GCODE_CHLOROPHYCEAN_MITO] = "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*YLYSSSS*CWCLFLF";
	code[NXS_GCODE_TREMATODE_MITO] = "NNKNTTTTSSSSMIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSSWCWCLFLF";
	code[NXS_GCODE_SCENEDESMUS_MITO] = "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*YLY*SSS*CWCLFLF";
	code[NXS_GCODE_THRAUSTOCHYTRIUM_MITO] = "KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSS*CWC*FLF";
	int c = (int) codeIndex;
	return code.at(c);
}



std::vector<NxsDiscreteStateCell> getGeneticCodeIndicesAAOrder(const NxsGeneticCodesEnum codeIndex);


std::vector<NxsDiscreteStateCell> getGeneticCodeIndicesAAOrder(const NxsGeneticCodesEnum codeIndex)
{
	std::vector<NxsDiscreteStateCell> aaInd(64);
	aaInd[0] = 8;
	aaInd[1] = 11;
	aaInd[2] = 8;
	aaInd[3] = 11;
	aaInd[4] = 16;
	aaInd[5] = 16;
	aaInd[6] = 16;
	aaInd[7] = 16;
	aaInd[8] = 14;
	aaInd[9] = 15;
	aaInd[10] = 14;
	aaInd[11] = 15;
	aaInd[12] = 7;
	aaInd[13] = 7;
	aaInd[14] = 10;
	aaInd[15] = 7;
	aaInd[16] = 13;
	aaInd[17] = 6;
	aaInd[18] = 13;
	aaInd[19] = 6;
	aaInd[20] = 12;
	aaInd[21] = 12;
	aaInd[22] = 12;
	aaInd[23] = 12;
	aaInd[24] = 14;
	aaInd[25] = 14;
	aaInd[26] = 14;
	aaInd[27] = 14;
	aaInd[28] = 9;
	aaInd[29] = 9;
	aaInd[30] = 9;
	aaInd[31] = 9;
	aaInd[32] = 3;
	aaInd[33] = 2;
	aaInd[34] = 3;
	aaInd[35] = 2;
	aaInd[36] = 0;
	aaInd[37] = 0;
	aaInd[38] = 0;
	aaInd[39] = 0;
	aaInd[40] = 5;
	aaInd[41] = 5;
	aaInd[42] = 5;
	aaInd[43] = 5;
	aaInd[44] = 17;
	aaInd[45] = 17;
	aaInd[46] = 17;
	aaInd[47] = 17;
	aaInd[48] = 20;
	aaInd[49] = 19;
	aaInd[50] = 20;
	aaInd[51] = 19;
	aaInd[52] = 15;
	aaInd[53] = 15;
	aaInd[54] = 15;
	aaInd[55] = 15;
	aaInd[56] = 20;
	aaInd[57] = 1;
	aaInd[58] = 18;
	aaInd[59] = 1;
	aaInd[60] = 9;
	aaInd[61] = 4;
	aaInd[62] = 9;
	aaInd[63] = 4;
	if (codeIndex == NXS_GCODE_VERT_MITO) {
		aaInd[8] = 20;
		aaInd[10] = 20;
		aaInd[12] = 10;
		aaInd[56] = 18;
	}
	else if (codeIndex == NXS_GCODE_YEAST_MITO) {
		aaInd[56] = 18;
	}
	else if (codeIndex == NXS_GCODE_MOLD_MITO) {
		aaInd[56] = 18;
	}
	else if (codeIndex == NXS_GCODE_INVERT_MITO) {
		aaInd[8] = 15;
		aaInd[10] = 15;
		aaInd[12] = 10;
		aaInd[56] = 18;
	}
	else if (codeIndex == NXS_GCODE_CILIATE) {
		aaInd[48] = 13;
		aaInd[50] = 13;
	}
	else if (codeIndex == NXS_GCODE_ECHINO_MITO) {
		aaInd[0] = 11;
		aaInd[8] = 15;
		aaInd[10] = 15;
		aaInd[56] = 18;
	}
	else if (codeIndex == NXS_GCODE_EUPLOTID) {
		aaInd[56] = 1;
	}
	else if (codeIndex == NXS_GCODE_ALT_YEAST) {
		aaInd[30] = 15;
	}
	else if (codeIndex == NXS_GCODE_ASCIDIAN_MITO) {
		aaInd[8] = 5;
		aaInd[10] = 5;
		aaInd[12] = 10;
		aaInd[56] = 18;
	}
	else if (codeIndex == NXS_GCODE_ALT_FLATWORM_MITO) {
		aaInd[0] = 11;
		aaInd[8] = 15;
		aaInd[10] = 15;
		aaInd[48] = 19;
		aaInd[56] = 18;
	}
	else if (codeIndex == NXS_GCODE_BLEPHARISMA_MACRO) {
		aaInd[50] = 13;
	}
	else if (codeIndex == NXS_GCODE_CHLOROPHYCEAN_MITO) {
		aaInd[50] = 9;
	}
	else if (codeIndex == NXS_GCODE_TREMATODE_MITO) {
		aaInd[0] = 11;
		aaInd[8] = 15;
		aaInd[10] = 15;
		aaInd[12] = 10;
		aaInd[56] = 18;
	}
	else if (codeIndex == NXS_GCODE_SCENEDESMUS_MITO) {
		aaInd[50] = 9;
		aaInd[52] = 20;
	}
	else if (codeIndex == NXS_GCODE_THRAUSTOCHYTRIUM_MITO) {
		aaInd[60] = 20;
	}
	return aaInd;
}


void NxsCharactersBlock::CodonPosPartitionToPosList(const NxsPartition &codonPos, std::list<int> * charIndices)
{
	if (charIndices == 0L)
		return;
	const NxsUnsignedSet * firstPos = 0L;
	const NxsUnsignedSet * secondPos = 0L;
	const NxsUnsignedSet * thirdPos = 0L;
	for (NxsPartition::const_iterator pIt = codonPos.begin(); pIt != codonPos.end(); ++pIt)
		{
		if (pIt->first == "1")
			{
			NCL_ASSERT(firstPos == 0L);
			firstPos = &(pIt->second);
			}
		else if (pIt->first == "2")
			{
			NCL_ASSERT(secondPos == 0L);
			secondPos = &(pIt->second);
			}
		else if (pIt->first == "3")
			{
			NCL_ASSERT(thirdPos == 0L);
			thirdPos = &(pIt->second);
			}
		}
	if (firstPos == 0L || secondPos == 0L || thirdPos == 0L)
		throw NxsException("Expecting partition subsets named 1, 2, and 3");
	if (firstPos->size() != secondPos->size() || firstPos->size() != thirdPos->size())
		throw NxsException("Expecting the partition subsets named 1, 2, and 3 to have the same size");
	NxsUnsignedSet::const_iterator fIt = firstPos->begin();
	NxsUnsignedSet::const_iterator sIt = secondPos->begin();
	NxsUnsignedSet::const_iterator thIt = thirdPos->begin();
	const NxsUnsignedSet::const_iterator endIt = firstPos->end();
	for (; fIt != endIt; ++fIt, ++sIt, ++thIt)
		{
		charIndices->push_back(*fIt);
		charIndices->push_back(*sIt);
		charIndices->push_back(*thIt);
		}
}

/* allocates a new charaters block with amino acids for the codons in the characters block (which should have datatype = codon).
*/
NxsCharactersBlock * NxsCharactersBlock::NewProteinCharactersBlock(
  const NxsCharactersBlock * codonBlock,
  bool mapPartialAmbigToUnknown,
  bool gapToUnknown,
  NxsGeneticCodesEnum codeIndex)
{
	std::vector<NxsDiscreteStateCell> aas = getGeneticCodeIndicesAAOrder(codeIndex);
	return NxsCharactersBlock::NewProteinCharactersBlock(codonBlock, mapPartialAmbigToUnknown, gapToUnknown, aas);
}


/* allocates a new charaters block with amino acids for the codons in the characters block (which should have datatype = codon).

*/
NxsCharactersBlock * NxsCharactersBlock::NewProteinCharactersBlock(
  const NxsCharactersBlock * codonBlock,
  bool mapPartialAmbigToUnknown,
  bool gapToUnknown,
  const std::vector<NxsDiscreteStateCell> & aaIndices) /** the index of the amino acid symbols for the codon (where the order of codons is alphabetical: AAA, AAC, AAG, AAT, ACA, ...TTT **/
{
	if (!codonBlock)
		return NULL;
	if (codonBlock->GetDataType() != NxsCharactersBlock::codon)
		throw NxsException("NewProteinCharactersBlock must be called with a block of codon datatype");
	const unsigned nc = codonBlock->GetNCharTotal();

	/* create a new characters block with the same TAXA, but no ASSUMPTIONS block */
	NxsTaxaBlockAPI * taxa = codonBlock->GetTaxaBlockPtr(NULL);
	NxsCharactersBlock * aaBlock = new NxsCharactersBlock(taxa, NULL);
	aaBlock->SetNChar(nc);
	aaBlock->SetNTax(codonBlock->GetNTaxWithData());
	aaBlock->missing = codonBlock->missing;
	aaBlock->gap = (gapToUnknown ? '\0' : codonBlock->gap);
	aaBlock->gapMode = codonBlock->gapMode;
	aaBlock->datatype = NxsCharactersBlock::protein;
	aaBlock->originalDatatype = codonBlock->originalDatatype;
	aaBlock->ResetSymbols();
	aaBlock->tokens = false;


	NxsPartition dummy;
	std::vector<DataTypesEnum> dummyVec;
	aaBlock->CreateDatatypeMapperObjects(dummy, dummyVec);
	const NxsDiscreteDatatypeMapper * codonMapper = codonBlock->GetDatatypeMapperForChar(0);
	NxsDiscreteDatatypeMapper * aaMapper = aaBlock->GetMutableDatatypeMapperForChar(0);
	aaMapper->geneticCode = codonMapper->geneticCode;

	const unsigned ntax = (taxa == 0L ? codonBlock->GetNTaxWithData() : taxa->GetNTax());
	aaBlock->datatypeReadFromFormat = false;
	aaBlock->statesFormat = STATES_PRESENT;
	aaBlock->restrictionDataype = false;
	aaBlock->supportMixedDatatype = false;
	aaBlock->convertAugmentedToMixed = false;
	aaBlock->writeInterleaveLen = INT_MAX;


	NxsDiscreteStateRow matRow(nc, 0);
	aaBlock->discreteMatrix.assign(ntax, matRow);
	if (mapPartialAmbigToUnknown && (gapToUnknown || codonBlock->GetGapSymbol() != '\0'))
		{
		for (unsigned taxInd = 0; taxInd < ntax; ++taxInd)
			{
			const NxsDiscreteStateRow & sourceRow = codonBlock->discreteMatrix.at(taxInd);
			NxsDiscreteStateRow & destRow = aaBlock->discreteMatrix.at(taxInd);
			for (unsigned c = 0; c < nc ; ++c)
				{
				const NxsDiscreteStateCell codon = sourceRow[c];
				if (codon < 0 || codon > 63)
					destRow[c] = NXS_MISSING_CODE;
				else
					destRow[c] = aaIndices.at(codon);
				}
			}
		}
	else
		{
		throw NxsException("NewProteinCharactersBlock is not implemented for cases in which you are not mapping any ambiguity to the missing state code.");
		}
	return aaBlock;
}


/* allocates a new charaters block with all of the active characters in `charBlock`
	but with a 64-state codon datatype. The order of codons is:
	 0   1   2   3   4   5  ... 63
	AAA AAC AAG AAT ACA ACC ... TTT
	The caller is responsible for deleting the new NxsCharactersBlock object

	If honorCharActive is true, then inactive characters are simply skipped in the reading
	frame (treated as if they were introns) rather than being treated as missing.
*/
NxsCharactersBlock * NxsCharactersBlock::NewCodonsCharactersBlock(
  const NxsCharactersBlock * dnaBlock,
  bool mapPartialAmbigToUnknown,
  bool gapsToUnknown,
  bool honorCharActive,
  const std::list<int> * charIndices,
  NxsCharactersBlock ** spareNucs)
{
	if (!dnaBlock)
		return NULL;
	DataTypesEnum nucType = dnaBlock->GetDataType();
	if (nucType != NxsCharactersBlock::dna && nucType != NxsCharactersBlock::rna && nucType != NxsCharactersBlock::nucleotide)
		return NULL;
	std::list<int> charInds;
	const std::list<int> * sourceChars;
	std::list<int> culled;
	NxsUnsignedSet untranslated;



	unsigned nc = dnaBlock->GetNCharTotal();

	if (charIndices == NULL)
		{
		for (unsigned i = 0; i < nc; ++i)
			charInds.push_back((int)i);
		sourceChars = &charInds;
		}
	else
		sourceChars = charIndices;

	if (honorCharActive)
		{
		for (std::list<int>::const_iterator cIt = sourceChars->begin(); cIt != sourceChars->end(); ++cIt)
			{
			const int c = *cIt;
			if (c < 0 || dnaBlock->IsActiveChar((unsigned) c))
				culled.push_back(c);
			}
		if (spareNucs)
			{
			for (unsigned c = 0; c < nc; ++c)
				{
				if (dnaBlock->IsActiveChar((unsigned) c))
					untranslated.insert(c);
				}
			}
		sourceChars = &culled;
		}
	else if (spareNucs)
		{
		for (unsigned c = 0; c < nc; ++c)
			untranslated.insert(c);
		}

	const unsigned nnucs = (const unsigned)sourceChars->size();
	if (nnucs % 3)
		throw NxsException("Cannot create a codons block with a number of characters that is not a multiple of 3");
	const unsigned ncodons = nnucs/3;

	/* create a new characters block with the same TAXA, but no ASSUMPTIONS block */
	NxsTaxaBlockAPI * taxa = dnaBlock->GetTaxaBlockPtr(NULL);
	NxsCharactersBlock * codonsBlock = new NxsCharactersBlock(taxa, NULL);
	codonsBlock->SetNChar(ncodons);
	codonsBlock->SetNTax(dnaBlock->GetNTaxWithData());
	codonsBlock->missing = dnaBlock->missing;
	codonsBlock->gap = (gapsToUnknown ? '\0' : dnaBlock->gap);
	codonsBlock->gapMode = dnaBlock->gapMode;
	codonsBlock->symbols.assign(64, '\0');
	codonsBlock->tokens = false;
	const char * gsl[] = {"AAA",  "AAC",  "AAG",  "AAT",  "ACA",  "ACC",  "ACG",  "ACT",  "AGA",  "AGC",  "AGG",  "AGT",  "ATA",  "ATC",  "ATG",  "ATT",  "CAA",  "CAC",  "CAG",  "CAT",  "CCA",  "CCC",  "CCG",  "CCT",  "CGA",  "CGC",  "CGG",  "CGT",  "CTA",  "CTC",  "CTG",  "CTT",  "GAA",  "GAC",  "GAG",  "GAT",  "GCA",  "GCC",  "GCG",  "GCT",  "GGA",  "GGC",  "GGG",  "GGT",  "GTA",  "GTC",  "GTG",  "GTT",  "TAA",  "TAC",  "TAG",  "TAT",  "TCA",  "TCC",  "TCG",  "TCT",  "TGA",  "TGC",  "TGG",  "TGT",  "TTA",  "TTC",  "TTG",  "TTT"};

	codonsBlock->globalStateLabels.reserve(64);
	for (unsigned i = 0 ; i < 64; ++i)
		codonsBlock->globalStateLabels.push_back(NxsString(gsl[i]));

	/* equivalent of HandleFormat */
	codonsBlock->datatype = NxsCharactersBlock::codon;
	codonsBlock->originalDatatype = nucType;

	const NxsPartition dummy;
	const std::vector<DataTypesEnum> dummyVec;
	codonsBlock->CreateDatatypeMapperObjects(dummy, dummyVec);
	NxsDiscreteDatatypeMapper * codonMapper = codonsBlock->GetMutableDatatypeMapperForChar(0);
	codonMapper->geneticCode = NXS_GCODE_NO_CODE;

	const unsigned ntax = (taxa == 0L ? dnaBlock->GetNTaxWithData() : taxa->GetNTax());
	codonsBlock->datatypeReadFromFormat = false;
	codonsBlock->statesFormat = STATES_PRESENT;
	codonsBlock->restrictionDataype = false;
	codonsBlock->supportMixedDatatype = false;
	codonsBlock->convertAugmentedToMixed = false;
	codonsBlock->writeInterleaveLen = INT_MAX;


	const int maxUnambigNucState = 3;
	const NxsDiscreteStateCell codonMissingState = NXS_MISSING_CODE;
	NxsDiscreteStateRow matRow(ncodons, 0);
	codonsBlock->discreteMatrix.assign(ntax, matRow);
	const std::list<int>::const_iterator endNucIt = sourceChars->end();
	if (mapPartialAmbigToUnknown && (gapsToUnknown || dnaBlock->GetGapSymbol() != '\0'))
		{
		for (unsigned taxInd = 0; taxInd < ntax; ++taxInd)
			{
			std::list<int>::const_iterator nucIt = sourceChars->begin();
			const NxsDiscreteStateRow & sourceRow = dnaBlock->discreteMatrix.at(taxInd);
			NxsDiscreteStateRow & destRow = codonsBlock->discreteMatrix.at(taxInd);
			for (unsigned codonInd = 0; codonInd < ncodons ; ++codonInd)
				{
				NCL_ASSERT(nucIt != endNucIt);
				const int fInd = *nucIt++;
				NCL_ASSERT(nucIt != endNucIt);
				const int sInd = *nucIt++;
				NCL_ASSERT(nucIt != endNucIt);
				const int tInd = *nucIt++;
				if (spareNucs)
					{
					untranslated.erase(fInd);
					untranslated.erase(sInd);
					untranslated.erase(tInd);
					}
				if (fInd < 0 || sInd < 0 || tInd < 0)
					destRow[codonInd] = codonMissingState;
				else
					{
					const NxsDiscreteStateCell fb = sourceRow[fInd];
					const NxsDiscreteStateCell sb = sourceRow[sInd];
					const NxsDiscreteStateCell tb = sourceRow[tInd];
					if (fb < 0 || sb < 0 || tb < 0 || fb > maxUnambigNucState || sb > maxUnambigNucState || tb > maxUnambigNucState)
						destRow[codonInd] = codonMissingState;
					else
						destRow[codonInd] = 16*fb + 4*sb + tb;
					}
				}
			}
		}
	else
		{
		throw NxsException("NewCodonsCharactersBlock is not implemented for cases in which you are not mapping any ambiguity to the missing state code.");
		}
	if (!untranslated.empty())
		{
		const unsigned nunt = (const unsigned)untranslated.size();

		NxsCharactersBlock * untBlock = new NxsCharactersBlock(taxa, NULL);
		untBlock->SetNChar(nunt);
		untBlock->SetNTax(ntax);
		untBlock->missing = dnaBlock->missing;
		untBlock->gap = (gapsToUnknown ? '\0' : dnaBlock->gap);
		untBlock->gapMode = dnaBlock->gapMode;
		untBlock->datatype = nucType;
		untBlock->originalDatatype = dnaBlock->originalDatatype;
		untBlock->ResetSymbols();
		untBlock->tokens = false;


		untBlock->CreateDatatypeMapperObjects(dummy, dummyVec);
		untBlock->datatypeReadFromFormat = false;
		untBlock->statesFormat = STATES_PRESENT;
		untBlock->restrictionDataype = false;
		untBlock->supportMixedDatatype = false;
		untBlock->convertAugmentedToMixed = false;
		untBlock->writeInterleaveLen = INT_MAX;


		NxsDiscreteStateRow umatRow(nunt, 0);
		untBlock->discreteMatrix.assign(ntax, umatRow);
		if (mapPartialAmbigToUnknown && (gapsToUnknown || dnaBlock->GetGapSymbol() != '\0'))
			{
			for (unsigned taxInd = 0; taxInd < ntax; ++taxInd)
				{
				const NxsDiscreteStateRow & sourceRow = dnaBlock->discreteMatrix.at(taxInd);
				NxsDiscreteStateRow & destRow = untBlock->discreteMatrix.at(taxInd);
				unsigned untIndex = 0;
				for (NxsUnsignedSet::const_iterator uIt  = untranslated.begin(); uIt != untranslated.end() ; ++uIt, ++untIndex)
					{
					const unsigned ind = *uIt;
					destRow.at(untIndex) = sourceRow[ind];
					}
				}
			}
		else
			{
			throw NxsException("NewProteinCharactersBlock is not implemented for cases in which you are not mapping any ambiguity to the missing state code.");
			}
		*spareNucs = untBlock;
		}
	else if (spareNucs)
		*spareNucs = NULL;
	return codonsBlock;
}


std::vector<double>  NxsTransformationManager::GetDoubleWeights(const std::string &set_name) const
	{
	std::vector<double> r;
	const ListOfDblWeights *p = 0L;
	std::map<std::string, ListOfDblWeights>::const_iterator dIt = dblWtSets.begin();
	for (; dIt != dblWtSets.end(); ++dIt)
		{
		if (NxsString::case_insensitive_equals(dIt->first.c_str(), set_name.c_str()))
			{
			p = &(dIt->second);
			break;
			}
		}
	if (p)
		{
		ListOfDblWeights::const_iterator wIt = p->begin();
		const ListOfDblWeights::const_iterator ewIt = p->end();
		for (; wIt != ewIt; ++wIt)
			{
			double w = wIt->first;
			const std::set<unsigned> &s = wIt->second;
			std::set<unsigned>::const_reverse_iterator ip = s.rbegin();
			const std::set<unsigned>::const_reverse_iterator e = s.rend();
			for (; ip != e; ++ip)
				{
				if (*ip >= r.size())
					r.resize(1 + *ip, 1.0);
				r[*ip] = w;
				}
			}
		}
	return r;
	}

std::vector<int> NxsTransformationManager::GetIntWeights(const std::string &set_name) const
	{
	std::vector<int> r;
	const ListOfIntWeights *p = 0L;
	std::map<std::string, ListOfIntWeights>::const_iterator dIt = intWtSets.begin();
	for (; dIt != intWtSets.end(); ++dIt)
		{
		if (NxsString::case_insensitive_equals(dIt->first.c_str(), set_name.c_str()))
			{
			p = &(dIt->second);
			break;
			}
		}
	if (p)
		{
		ListOfIntWeights::const_iterator wIt = p->begin();
		const ListOfIntWeights::const_iterator ewIt = p->end();
		for (; wIt != ewIt; ++wIt)
			{
			int w = wIt->first;
			const std::set<unsigned> &s = wIt->second;
			std::set<unsigned>::const_reverse_iterator ip = s.rbegin();
			const std::set<unsigned>::const_reverse_iterator e = s.rend();
			for (; ip != e; ++ip)
				{
				if (*ip >= r.size())
					r.resize(1 + *ip, 1);
				r[*ip] = w;
				}
			}
		}
	return r;
	}

/*! creates a datatype mapper from the parsing information (this is the ctor used
		most frequently during a parse).
*/
NxsDiscreteDatatypeMapper::NxsDiscreteDatatypeMapper(
	NxsCharactersBlock::DataTypesEnum datatypeE,
	const std::string & symbolsStr,
	char missingChar,
	char gap,
	char matchingChar,
	bool respectingCase,
	const std::map<char, NxsString> & moreEquates)
	:geneticCode(NXS_GCODE_NO_CODE),
	cLookup(NULL),
	stateCodeLookupPtr(NULL),
	symbols(symbolsStr),
	nStates(0),
	matchChar(matchingChar),
	gapChar(gap),
	missing(missingChar),
	respectCase(respectingCase),
	extraEquates(moreEquates),
	datatype(datatypeE),
	restrictionDataype(false),
	userDefinedEquatesBeforeConversion(false)
	{
	if (symbols.empty())
		symbols = NxsCharactersBlock::GetDefaultSymbolsForType(datatype);
	if (datatype == NxsCharactersBlock::mixed)
		throw NxsException("Cannot create a mixed datatype mapper"); // this should be the only empty string-generating datatype
	RefreshMappings(0L);
	}

void NxsDiscreteDatatypeMapper::DebugPrint(std::ostream & out) const
	{
	out << GetNumStatesIncludingGap() << "states (";
	if (gapChar == '\0')
		out << "no gaps";
	else
		out << "including the gap \"state\"";
	const int nsc = (int) stateSetsVec.size();
	out << '\n' << nsc << " state codes.\n";
	out << "NEXUS     State Code      States\n";
	for (int sc = sclOffset; sc < sclOffset + nsc; ++sc)
		{
		std::string nex;
		for (int c = 0; c < 127; ++c)
			{
			if (cLookup[c] == sc)
				nex.append(1, (char) c);
			}
		int buf =  (int) (10 - nex.size());
		nex.append(buf, ' ');
		out << nex << "    " << sc << "     ";
		const std::set<NxsDiscreteStateCell>	&ss = GetStateSetForCode(sc);
		std::string decoded;
		for (std::set<NxsDiscreteStateCell>::const_iterator s = ss.begin(); s != ss.end(); ++s)
			decoded.append(StateCodeToNexusString(*s));
		if (decoded.length() < 2)
			out << decoded;
		else if (IsPolymorphic(sc))
			out << '(' << decoded << ')';
		else
			out << '{' << decoded << '}';
		out << '\n';
		}
	}

/*!
	Takes the parsed settings that pertain to the datatype and converts them into a set of NxsDiscreteDatatypeMapper
	objects to be used to encode the characters.
*/
void NxsCharactersBlock::CreateDatatypeMapperObjects(const NxsPartition & dtParts, const std::vector<DataTypesEnum> & dtcodes)
	{
	try {
		mixedTypeMapping.clear();
		if (datatype != mixed)
			{
			NxsDiscreteDatatypeMapper d(datatype, symbols, missing, gap, matchchar, respectingCase, userEquates);
			datatype = d.GetDatatype();
			DatatypeMapperAndIndexSet das(d, NxsUnsignedSet());
			datatypeMapperVec.clear();
			datatypeMapperVec.push_back(das);
			}
		else
			{
			datatypeMapperVec.clear();
			NCL_ASSERT(dtParts.size() == dtcodes.size());
			datatypeMapperVec.reserve(dtParts.size());
			std::vector<DataTypesEnum>::const_iterator cIt = dtcodes.begin();
			//@@@TMP add code to fill  DataTypesEnum -> NxsUnsignedSet map  here ! for DZ and DS.
			for (NxsPartition::const_iterator pIt = dtParts.begin(); pIt != dtParts.end(); ++pIt, ++cIt)
				{
				std::string mt;
				if (*cIt == standard)
					mt.assign("0123456789"); /*mrbayes is the only program to support MIXED and it uses a default (not extendable) symbols list of 0123456789 rather than 01*/
				NxsDiscreteDatatypeMapper d(*cIt, mt, missing, gap, matchchar, respectingCase, userEquates);
				const NxsUnsignedSet & indexSet = pIt->second;
				DatatypeMapperAndIndexSet das(d, pIt->second);
				NxsUnsignedSet & mappedInds =  mixedTypeMapping[*cIt];
				mappedInds.insert(indexSet.begin(), indexSet.end());
				datatypeMapperVec.push_back(das);
				}
			}
		}
	catch (const NxsException & x)
		{
		std::string y = "An error was detected while trying to create a datatype mapping structure.  This portion of code tends to generate cryptic error messages, so if the following message is not helpful, double check the syntax in the FORMAT command of your block.\n";
		y.append(x.msg);
		throw NxsException(y, x.pos, x.line, x.col);
		}
	}




/*!
	If you say FORMAT DATATYPE=DNA SYMBOLS="01" ; then the valid symbols become "ACGT01"

	AugmentedSymbolsToMixed tries to split such a matrix into a datatype=mixed(dna:charset_dna,standard:charset_std)
	by inferring the charpartition (charset_dna,charset_std).  It does this by using GetNamedStateSetOfColumn to
	detect which states were listed in a column.

	Returns true if the translation to mixed was performed.  This will only occur if GetOriginalDataType() != GetDataType()
 	because this is the symptom that there was symbol augmentation of a built in datatype.

	Note that in the GetNamedStateSetOfColumn
	then ? will not expand the states present in a symbol. Thus when parsing:
	Matrix 1:                     Matrix 2:
	s   ACGT10{ACGT01-}           s   ACGT10?
	t   ACGT100                   t   ACGT100
		The last character of the first taxon would be parsed as having the potential to have states {ACGT01-}.
		But when interperted with GetNamedStateSetOfColumn, Matrix 2 can be "explained" by four DNA columns, and three
		Standard (01) columns.  Matrix 1, on the other hand would be found to have four DNA columns, and two
		Standard (01) columns, and one standard ("ACGT01") column.
	Note: this function ignores the gap mode setting and treats gaps as newstates for the purposes of
		the conversion.

	Temporary:  Will return false if userDefinedEquatesBeforeConversion is true
*/
bool NxsCharactersBlock::AugmentedSymbolsToMixed()
	{
	DataTypesEnum odt = GetOriginalDataType();
	if (IsMixedType() || (odt == GetDataType()))
		return false;
	const std::string origSymb = GetDefaultSymbolsForType(odt);
	const std::string cutSymb = symbols.substr(0, origSymb.length());
	if (origSymb != cutSymb)
		return false;
	const std::string augmentSymbols = symbols.substr(origSymb.length());
	if (augmentSymbols.empty())
		return false;
	for (std::string::const_iterator a = augmentSymbols.begin(); a != augmentSymbols.end(); ++a)
		{
		if (!isdigit(*a))
			return false;
		}

	NxsUnsignedSet stdTypeChars;
	NxsUnsignedSet origTypeChars;
	std::set<NxsDiscreteStateCell> torigStateInds;
	std::set<NxsDiscreteStateCell> tstdStateInds;
	torigStateInds.insert(NXS_GAP_STATE_CODE);
	tstdStateInds.insert(NXS_GAP_STATE_CODE);
	for (NxsDiscreteStateCell j = 0; j < (NxsDiscreteStateCell)origSymb.length(); ++j)
		torigStateInds.insert(j);
	for (NxsDiscreteStateCell j =  (NxsDiscreteStateCell)origSymb.length(); j < (NxsDiscreteStateCell)symbols.length(); ++j)
		tstdStateInds.insert(j);
	const std::set<NxsDiscreteStateCell> origStateInds(torigStateInds);
	const unsigned nosi = (unsigned)origStateInds.size();
	const std::set<NxsDiscreteStateCell> stdStateInds(tstdStateInds);
	const unsigned nssi = (unsigned)stdStateInds.size();

	/*Check each column for patterns that can not be mapped to origSymb or augmentSymbols */
	const unsigned nChars = GetNCharTotal();
	GapModeEnum cached_gap_mode = this->gapMode;
	this->gapMode = GAP_MODE_NEWSTATE;
	try {
		for (unsigned colIndex = 0; colIndex < nChars; ++colIndex)
			{
			const std::set<NxsDiscreteStateCell> cs = GetNamedStateSetOfColumn(colIndex);
			std::set<NxsDiscreteStateCell> origUnion;
			set_union(origStateInds.begin(), origStateInds.end(), cs.begin(), cs.end(), inserter(origUnion, origUnion.begin()));
			if (origUnion.size() > nosi)
				{
				std::set<NxsDiscreteStateCell> stdUnion;
				set_union(stdStateInds.begin(), stdStateInds.end(), cs.begin(), cs.end(), inserter(stdUnion, stdUnion.begin()));
				if (stdUnion.size() > nssi)
					return false;
				stdTypeChars.insert(colIndex);
				}
			else
				origTypeChars.insert(colIndex);
			}
		}
	catch (...)
		{
		this->gapMode = cached_gap_mode;
		throw;
		}
	this->gapMode = cached_gap_mode;
	/* If we get here then the mapping to mixed type will succeed */

	/* copy the incoming matrix and mapper */
	VecDatatypeMapperAndIndexSet mdm = datatypeMapperVec;
	const NxsDiscreteDatatypeMapper & oldMapper = mdm[0].first;
	if (oldMapper.GetUserDefinedEquatesBeforeConversion())
		return false; /* dealing with equates correctly is not implemented below, so we'll bale out */

	/* add the new mappers */
	std::map<char, NxsString> noEquates;
	datatypeMapperVec.clear();
	NxsDiscreteDatatypeMapper o(odt, origSymb, missing, gap, matchchar, respectingCase, noEquates);
	datatypeMapperVec.push_back(DatatypeMapperAndIndexSet(o, origTypeChars));
	NxsDiscreteDatatypeMapper s(NxsCharactersBlock::standard, augmentSymbols, missing, gap, matchchar, respectingCase, noEquates);
	datatypeMapperVec.push_back(DatatypeMapperAndIndexSet(s, stdTypeChars));


	NxsDiscreteDatatypeMapper & newOrigTMapper = datatypeMapperVec[0].first;
	NxsDiscreteDatatypeMapper & newStdTMapper = datatypeMapperVec[1].first;

	/* now we recode discrete matrix with new state codes */
	const NxsDiscreteStateCell nOrigStates = (NxsDiscreteStateCell) origSymb.size();
	std::map<NxsDiscreteStateCell, NxsDiscreteStateCell> oldToNewStateCode;
	NxsDiscreteStateMatrix::iterator rowIt = discreteMatrix.begin();
	for (unsigned colIndex = 0; rowIt != discreteMatrix.end(); ++colIndex, ++rowIt)
		{
		NxsDiscreteStateRow & row = *rowIt;
		unsigned column = 0;
		for (NxsDiscreteStateRow::iterator cell = row.begin(); cell != row.end(); ++cell, ++column)
			{
			const NxsDiscreteStateCell initStateCode = *cell;
			if (initStateCode  >= 0 ) //gap and missing codes do not need translation
				{
				std::map<NxsDiscreteStateCell, NxsDiscreteStateCell>::const_iterator otnIt = oldToNewStateCode.find(initStateCode);
				if (otnIt == oldToNewStateCode.end())
					{
					const bool isOrigT = origTypeChars.count(column) > 0;
					const std::set<NxsDiscreteStateCell> oldSymbols = oldMapper.GetStateSetForCode(initStateCode);
					const std::string oldNexusString = oldMapper.StateCodeToNexusString(initStateCode);
					const char oldNexusChar = (oldNexusString.length() == 1 ? oldNexusString[0] : '\0');
					const bool isPoly =  oldMapper.IsPolymorphic(initStateCode);
					NxsDiscreteStateCell newStateCode ;
					if (isOrigT)
						{ //old symbol indices will still be the new symbol indices
						newStateCode = newOrigTMapper.StateCodeForStateSet(oldSymbols, isPoly, true, oldNexusChar);
						newOrigTMapper.StateCodeToNexusString(newStateCode);
						}
					else
						{
						std::set<NxsDiscreteStateCell> transSymbols;
						for (std::set<NxsDiscreteStateCell>::const_iterator sIt = oldSymbols.begin(); sIt != oldSymbols.end(); ++sIt)
							{
							if (*sIt >= nOrigStates)
								transSymbols.insert(*sIt - nOrigStates);
							else
								{
								NCL_ASSERT(*sIt < 0);
								transSymbols.insert(*sIt);
								}
							}
						newStateCode = newStdTMapper.StateCodeForStateSet(transSymbols, isPoly, true, oldNexusChar);
						newStdTMapper.StateCodeToNexusString(newStateCode);
						}
					oldToNewStateCode[initStateCode] = newStateCode;
					*cell = newStateCode;
					}
				else
					*cell = otnIt->second;
				}
			}
		}
	datatype = NxsCharactersBlock::mixed;
	mixedTypeMapping.clear();
	mixedTypeMapping[odt] = origTypeChars;
	mixedTypeMapping[NxsCharactersBlock::standard] = stdTypeChars;
	return true;
	}
/*!
	Called when FORMAT command needs to be parsed from within the DIMENSIONS block. Deals with everything after the
	token FORMAT up to and including the semicolon that terminates the FORMAT command.
*/
void NxsCharactersBlock::HandleFormat(
  NxsToken &token)	/* the token used to read from `in' */
	{
	errormsg.clear();
	ProcessedNxsCommand tokenVec;
	token.ProcessAsCommand( &tokenVec);

	const ProcessedNxsCommand::const_iterator tvEnd = tokenVec.end();
	NxsPartition dtParts;
	std::vector<DataTypesEnum> dtv;
	std::vector<bool> isR;
	if (!datatypeReadFromFormat)
		{
		bool standardDataTypeAssumed = true;
		bool ignoreCaseAssumed = true;
		datatype = standard;
		originalDatatype = standard;
		ResetSymbols();
		respectingCase = false;
		restrictionDataype = false;
		for (ProcessedNxsCommand::const_iterator wIt = tokenVec.begin(); wIt != tvEnd; ++wIt)
			{
			if (wIt->Equals("DATATYPE"))
				{
				DemandEquals(wIt, tvEnd, " after keyword DATATYPE");
				ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, " after \"DATATYPE =\" in FORMAT command");
				if (wIt->Equals("STANDARD"))
					{
					datatype = standard;
					symbols = "01";
					}
				else if (wIt->Equals("DNA"))
					datatype = dna;
				else if (wIt->Equals("RNA"))
					datatype = rna;
				else if (wIt->Equals("NUCLEOTIDE"))
					datatype = nucleotide;
				else if (wIt->Equals("PROTEIN"))
					datatype = protein;
				else if (wIt->Equals("RESTRICTION"))
					{
					datatype = standard;
					restrictionDataype = true;
					}
				else if (wIt->Equals("CONTINUOUS"))
					{
					datatype = continuous;
					statesFormat = INDIVIDUALS;
					items = std::vector<std::string>(1, std::string("AVERAGE"));
					tokens = true;
					}
				else if (supportMixedDatatype && wIt->Equals("MIXED"))
					{
					datatype = mixed;
					ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, " after \"DATATYPE=MIXED\" in FORMAT command. Expecting (");
					if (!wIt->Equals("("))
						{
						errormsg << "Expecting ( after \"DATATYPE=MIXED\" but found " << wIt->GetToken();
						throw NxsException(errormsg, *wIt);
						}
					ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, " after \"DATATYPE=MIXED(\" in FORMAT command. Expecting a datatype");
					ostringstream fakestream;
					while (!wIt->Equals(")"))
						{
						fakestream << ' ' << NxsString::GetEscaped(wIt->GetToken());
						ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, " in \"DATATYPE=MIXED\" in FORMAT command. Expecting a closing ) to terminate the list.");
						}
					fakestream << ';';
					const std::string accumulated = fakestream.str();
					istringstream fakeinput(accumulated);
					NxsToken subToken(fakeinput);
					try
						{
						std::string mt("mixed datatype definition");
						subToken.GetNextToken();
						this->ReadPartitionDef(dtParts, *this, mt, "Character", "Datatype=Mixed", subToken, false, true, false);
						}
					catch (NxsException & x)
						{
						errormsg = x.msg;
						throw NxsException(errormsg, *wIt);
						}
					catch (...)
						{
						errormsg << "Error parsing \"DATATYPE=MIXED\" subcommand in FORMAT the command.";
						throw NxsException(errormsg, *wIt);
						}
					for (NxsPartition::const_iterator pIt = dtParts.begin(); pIt != dtParts.end(); ++pIt)
						{
						NxsString name(pIt->first.c_str());
						name.ToUpper();
						if (name == "RESTRICTION")
							{
							dtv.push_back(standard);
							isR.push_back(true);
							}
						else
							{
							isR.push_back(false);
							if (name == "STANDARD")
								dtv.push_back(standard);
							else if (name == "DNA")
								dtv.push_back(dna);
							else if (name == "RNA")
								dtv.push_back(rna);
							else if (name == "NUCLEOTIDE")
								dtv.push_back(nucleotide);
							else if (name == "PROTEIN")
								dtv.push_back(protein);
							else
								{
								errormsg << pIt->first <<  " is not a valid DATATYPE within a " <<  NCL_BLOCKTYPE_ATTR_NAME << " block";
								throw NxsException(errormsg, *wIt);
								}
							}
						}
					}
				else
					{
					errormsg << wIt->GetToken() <<  " is not a valid DATATYPE within a " <<  NCL_BLOCKTYPE_ATTR_NAME << " block";
					throw NxsException(errormsg, *wIt);
					}
				datatypeReadFromFormat = true;
				originalDatatype = datatype;
				ResetSymbols();
				standardDataTypeAssumed = false;
				if (!ignoreCaseAssumed)
					break;
				}
			else if (wIt->Equals("RESPECTCASE"))
				{
				ignoreCaseAssumed = false;
				respectingCase = true;
				if (!standardDataTypeAssumed)
					break;
				}
			}
		}
	for (ProcessedNxsCommand::const_iterator wIt = tokenVec.begin(); wIt != tvEnd; ++wIt)
		{

		if (wIt->Equals("DATATYPE"))// we should have already processed this
			{
			ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "after DATATYPE in FORMAT command"); // =
			ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "after DATATYPE = in FORMAT command"); // datatype
			}
		else if (wIt->Equals("RESPECTCASE"))
			{
			if (!respectingCase)
				{
				errormsg << "Only one FORMAT command should occur per DATA or CHARACTERS block.";
				throw NxsException(errormsg, *wIt);
				}
			}
		else if (wIt->Equals("MISSING"))
			{
			DemandEquals(wIt, tvEnd, "after keyword MISSING");
			ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "after \"MISSING = \" in FORMAT command");
			const std::string t = wIt->GetToken();
			if (t.length() != 1)
				{
				errormsg << "MISSING symbol should be a single character, but " << t << " was specified";
				WarnDangerousContent(errormsg, *wIt);
				}
			else if (token.IsPunctuationToken(t) && !token.IsPlusMinusToken(t))
				{
				errormsg << "MISSING symbol specified cannot be a punctuation token (" << t << " was specified)";
				WarnDangerousContent(errormsg, *wIt);
				}
			else if (token.IsWhitespaceToken(t))
				{
				errormsg << "MISSING symbol specified cannot be a whitespace character (" << t << " was specified)";
				WarnDangerousContent(errormsg, *wIt);
				}
			missing = t[0];
			}
		else if (wIt->Equals("GAP"))
			{
			DemandEquals(wIt, tvEnd, "after keyword GAP");
			ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "after \"GAP = \" in FORMAT command");
			const std::string t = wIt->GetToken();
			if (t.length() != 1)
				{
				errormsg << "GAP symbol should be a single character, but " << t << " was specified";
				WarnDangerousContent(errormsg, *wIt);
				}
			else if (token.IsPunctuationToken(t) && !token.IsPlusMinusToken(t))
				{
				errormsg << "GAP symbol specified cannot be a punctuation token " << t << " was specified";
				WarnDangerousContent(errormsg, *wIt);
				}
			else if (token.IsWhitespaceToken(t))
				{
				errormsg << "GAP symbol specified cannot be a whitespace character " << t << " was specified";
				WarnDangerousContent(errormsg, *wIt);
				}
			gap = t[0];
			}
		else if (wIt->Equals("MATCHCHAR"))
			{
			DemandEquals(wIt, tvEnd, "after keyword MATCHCHAR");
			ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "after \"MATCHCHAR = \" in FORMAT command");
			const std::string t = wIt->GetToken();
			if (t.length() != 1)
				{
				errormsg << "MATCHCHAR symbol should be a single character, but " << t << " was specified";
				WarnDangerousContent(errormsg, *wIt);
				}
			else if (token.IsPunctuationToken(t) && !token.IsPlusMinusToken(t))
				{
				errormsg << "MATCHCHAR symbol specified cannot be a punctuation token (" << t << " was specified)";
				WarnDangerousContent(errormsg, *wIt);
				}
			else if (token.IsWhitespaceToken(t))
				{
				errormsg << "MATCHCHAR symbol specified cannot be a whitespace character (" << t << " was specified)";
				WarnDangerousContent(errormsg, *wIt);
				}
			matchchar = t[0];
			}
		else if (wIt->Equals("SYMBOLS") || wIt->Equals("SYMBOL"))
			{
			if (datatype == NxsCharactersBlock::continuous)
				throw NxsException("SYMBOLS subcommand not allowed for DATATYPE=CONTINUOUS", *wIt);
			if (restrictionDataype)
				throw NxsException("SYMBOLS subcommand not allowed for DATATYPE=RESTRICTION", *wIt);
			NxsDiscreteStateCell numDefStates;
			unsigned maxNewStates = NCL_MAX_STATES;
			switch(datatype)
				{
				case NxsCharactersBlock::dna:
				case NxsCharactersBlock::rna:
				case NxsCharactersBlock::nucleotide:
					numDefStates = 4;
					maxNewStates = NCL_MAX_STATES-4;
					break;

				case NxsCharactersBlock::protein:
					numDefStates = 21;
					maxNewStates = NCL_MAX_STATES-21;
					break;

				default:
					numDefStates = 0; // replace symbols list for standard datatype
					symbols.clear();
					maxNewStates = NCL_MAX_STATES;
				}
			DemandEquals(wIt, tvEnd, "after keyword SYMBOLS");
			ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "\" to start the symbols list");
			if (!wIt->Equals("\""))
				{
				errormsg << "Expecting \" after Symbols= but " << wIt->GetToken() << " was found";
				throw NxsException(errormsg, *wIt);
				}
			ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "closing \" of symbols list");
			NxsString s;
			while (!wIt->Equals("\""))
				{
				s += wIt->GetToken().c_str();
				ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "closing \" of symbols list");
				}

			const std::string tos = NxsString::strip_whitespace(s);
			const char * to = tos.c_str();
			unsigned tlen = (unsigned)tos.length();
			if (tlen > maxNewStates)
				{
				errormsg << "SYMBOLS defines " << tlen << " new states but only " << maxNewStates << " new states allowed for this DATATYPE";
				throw NxsException(errormsg, *wIt);
				}
			// Check to make sure user has not used any symbols already in the
			// default symbols list for this data type
			//
			std::string preprocessedS;
			for (unsigned i = 0; i < tlen; i++)
				{
				if (to[i] == '~')
					{
					if (i == 0 || i == tlen -1)
						{
						errormsg << "A ~ in a SYMBOLS list is interpreted as a range of symbols.  The ~ cannot be the first or last character in the symbols list";
						throw NxsException(errormsg, token);
						}
					const int jj = i - 1 ;
					const char prevChar = to[jj];
					const char nextChar = to[i+1];
					if ((isdigit(prevChar) && isdigit(nextChar)) || (isalpha(prevChar) && isalpha(nextChar)))
						{
						if (nextChar > prevChar)
							{
							for (char c = (char)((int)prevChar + 1) ; c < nextChar;)
								{
								preprocessedS.append(1, c);
								c = (char) ((int)c + 1);
								}
							}
						else
							{
							errormsg << "Endpoint of SYMBOLS range must be greater than the starting point.  This was not true of " << prevChar << '~' << nextChar;
							throw NxsException(errormsg, token);
							}
						}
					else
						{
						errormsg << prevChar << '~' << nextChar << " is an illegal SYMBOLS range. A range must go from a letter to a letter or from a number to number" ;
						throw NxsException(errormsg, token);
						}
					}
				else
					preprocessedS += to[i];
				}
			NxsString processedS;
			for (std::string::const_iterator pp = preprocessedS.begin(); pp != preprocessedS.end(); ++pp)
				{
				const char c = *pp;
				if (IsInSymbols(c))
					{
					errormsg << "The character " << c << " defined in SYMBOLS is predefined for this DATATYPE and should not occur in a SYMBOLS statement";
					if (nexusReader)
						{
						nexusReader->NexusWarnToken(errormsg, NxsReader::SKIPPING_CONTENT_WARNING, token);
						errormsg.clear();
						}
					}
				else if (   (respectingCase && (userEquates.find(c) != userEquates.end()))
						 || (! respectingCase && (userEquates.find(toupper(c)) != userEquates.end() || userEquates.find(tolower(*pp)) != userEquates.end())))
					{
					errormsg << "The character " << *pp << " defined in SYMBOLS subcommand, has already been introduced as an EQUATE key.  The use of a character as both a state symbol and an equate key is not allowed.";
					throw NxsException(errormsg, token);
					}
				else
					processedS += *pp;
				}
			if (!processedS.empty())
				{
				if (this->datatype == dna || this->datatype == rna || this->restrictionDataype || this->datatype == protein)
					{
					if (this->allowAugmentingOfSequenceSymbols)
						{
						errormsg << "Adding symbols to the " << GetNameOfDatatype(this->datatype) << " datatype will cause the matrix to be treated as if it were a";
						if (this->convertAugmentedToMixed)
							errormsg << " MIXED datatype matrix";
						else
							errormsg << " STANDARD datatype matrix";
						if (!this->convertAugmentedToMixed)
							nexusReader->NexusWarnToken(errormsg, NxsReader::AMBIGUOUS_CONTENT_WARNING, token);
						errormsg.clear();
						}
					else
						{
						errormsg << "Symbols cannot be added to the " << GetNameOfDatatype(this->datatype) << " datatype.";
						throw NxsException(errormsg, token);
						}
					}
				// If we've made it this far, go ahead and add the user-defined
				// symbols to the end of the list of predefined symbols
				//
				symbols += processedS.c_str();
				}
			}

		else if (wIt->Equals("EQUATE"))
			{
			if (datatype == NxsCharactersBlock::continuous)
				throw NxsException("EQUATE subcommand not allowed for DATATYPE=CONTINUOUS", *wIt);

			DemandEquals(wIt, tvEnd, "after keyword EQUATE");
			ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "\" to start the Equate definition");
			if (!wIt->Equals("\""))
				{
				errormsg << "Expecting '\"' after keyword EQUATE but found " << wIt->GetToken() << " instead";
				throw NxsException(errormsg, *wIt);
				}
			ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "\" to end the Equate definition");
			while (!wIt->Equals("\""))
				{
				std::string t = wIt->GetToken();
				if (t.length() != 1)
					{
					errormsg << "Expecting single-character EQUATE symbol but found " << wIt->GetToken() << " instead";
					throw NxsException(errormsg, *wIt);
					}
				const char ch = t[0];
				bool badEquateSymbol = false;

				// Equate symbols cannot be punctuation (except for + and -)
				//
				if (token.IsPunctuationToken(t) && !token.IsPlusMinusToken(t))
					badEquateSymbol = true;
				else if (ch == '^')
					badEquateSymbol = true;
				if (badEquateSymbol)
					{
					errormsg << "EQUATE symbol specified (" << wIt->GetToken() <<  ") is not valid. Equate symbols cannot be any of the following: ()[]{}/\\,;:=*'\"`<>^";
					WarnDangerousContent(errormsg, *wIt);
					}
				if (ch == missing || ch == matchchar || ch == gap || IsInSymbols(ch))
					{
					errormsg << "EQUATE symbol specified (" << wIt->GetToken() <<  ") is not valid; An Equate symbol cannot be a state symbol or identical to the  missing,  gap, or matchchar symbols.";
					throw NxsException(errormsg, *wIt);
					}

				DemandEquals(wIt, tvEnd, " in EQUATE definition");
				ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "State or set of states in Equate definition");
				NxsString s;
				s = wIt->GetToken().c_str();
				if (wIt->Equals("{"))
					{
					while (!wIt->Equals("}"))
						{
						ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "} to close the state set in an equate definition");
						s += wIt->GetToken().c_str();
						}
					}
				else if (wIt->Equals("("))
					{
					while (!wIt->Equals(")"))
						{
						ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, ") to close the state set in an equate definition");
						s += wIt->GetToken().c_str();
						}
					}
				const std::string nows = NxsString::strip_whitespace(s);
				userEquates[ch] = NxsString(nows.c_str());
				ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "\" to end the Equate definition");
				}
			}

		else if (wIt->Equals("LABELS"))
			labels = true;
		else if (wIt->Equals("NOLABELS"))
			labels = false;
		else if (wIt->Equals("TRANSPOSE"))
			transposing = true;
		else if (wIt->Equals("INTERLEAVE"))
			interleaving = true;
		else if (wIt->Equals("ITEMS"))
			{
			DemandEquals(wIt, tvEnd, "after keyword ITEMS");
			items.clear();
			// This should be STATES (no other item is supported at this time)
			//
			ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "after \"ITEMS =\" in FORMAT command");
			if (datatype == NxsCharactersBlock::continuous)
				{
				std::string s;
				if (wIt->Equals("("))
					{
					ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, ") to close Items list in FORMAT command");
					while (!wIt->Equals(")"))
						{
						s = wIt->GetToken();
						NxsString::to_upper(s);
						items.push_back(std::string(s.c_str()));
						ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, ") to close Items list in FORMAT command");
						}
					}
				else
					{
					s = wIt->GetToken();
					NxsString::to_upper(s);
					items.push_back(std::string(s.c_str()));
					}
				}
			else
				{
				if (!wIt->Equals("STATES"))
					throw NxsException("Sorry, only ITEMS=STATES is supported for discrete datatypes at this time", *wIt);
				items = std::vector<std::string>(1, std::string("STATES"));
				}
			}
		else if (wIt->Equals("STATESFORMAT"))
			{
			DemandEquals(wIt, tvEnd, "after keyword STATESFORMAT");
			ProcessedNxsToken::IncrementNotLast(wIt, tvEnd, "after \"STATESFORMAT =\" in FORMAT command");
			if (wIt->Equals("STATESPRESENT"))
				statesFormat = STATES_PRESENT;
			else
				{
				if (datatype == NxsCharactersBlock::continuous)
					{
					if (wIt->Equals("INDIVIDUALS"))
						statesFormat = INDIVIDUALS;
					else
						throw NxsException("Sorry, only STATESFORMAT=STATESPRESENT or STATESFORMAT=INDIVIDUALS are supported for continuous datatypes at this time", *wIt);
					}
				else
					throw NxsException("Sorry, only STATESFORMAT=STATESPRESENT supported for discrete datatypes at this time", *wIt);
				}
			}
		else if (wIt->Equals("TOKENS"))
			tokens = true;
		else if (wIt->Equals("NOTOKENS"))
			{
			if (datatype == NxsCharactersBlock::continuous)
				throw NxsException("NOTOKENS is not allowed for the CONTINUOUS datatype", *wIt);
			tokens = false;
			}
		}
	if (IsInSymbols(missing))
		{
		errormsg << "The \"missing\" character \'" << missing << "\' may not be included in the SYMBOLS list.";
		throw NxsException(errormsg, *tokenVec.begin());
		}
	if (IsInSymbols(matchchar))
		{
		errormsg << "The \"matchchar\" character \'" << matchchar << "\' may not be included in the SYMBOLS list.";
		throw NxsException(errormsg, *tokenVec.begin());
		}
	if (IsInSymbols(gap))
		{
		errormsg << "The \"gap\" character \'" << gap << "\' may not be included in the SYMBOLS list.";
		throw NxsException(errormsg, *tokenVec.begin());
		}

	if (matchchar != '\0')
		{
		if ((matchchar == gap) || (!respectingCase && toupper(matchchar) == toupper(gap)))
			{
			errormsg << "MatchChar and Gap symbol cannot be identical!  Both were set to " << gap;
			throw NxsException(errormsg, *tokenVec.begin());
			}
		if ((matchchar == missing) || (!respectingCase && toupper(matchchar) == toupper(missing)))
			{
			errormsg << "MatchChar and Missing symbol cannot be identical!  Both were set to " << missing;
			throw NxsException(errormsg, *tokenVec.begin());
			}
		}
	if ((gap != '\0') && ((gap == missing) || (!respectingCase && toupper(gap) == toupper(missing))))
		{
		errormsg << "Gap symbol and Missing symbol cannot be identical!  Both were set to " << missing;
		throw NxsException(errormsg, *tokenVec.begin());
		}

	// Perform some last checks before leaving the FORMAT command
	//
	if (!tokens && datatype == continuous)
		GenerateNxsException(token, "TOKENS must be defined for DATATYPE=CONTINUOUS");
	if (tokens && (datatype == dna || datatype == rna || datatype == nucleotide))
		GenerateNxsException(token, "TOKENS not allowed for the DATATYPEs DNA, RNA, or NUCLEOTIDE");
	CreateDatatypeMapperObjects(dtParts, dtv);
	if (IsMixedType() && tokens)
		{
		errormsg = "The combination of DATATYPE=Mixed  and TOKENS are not currently supported.";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}
	unsigned mapInd = 0;
	for (std::vector<bool>::const_iterator b = isR.begin(); b != isR.end(); ++b, ++mapInd)
		{
		if (*b)
			{
			DatatypeMapperAndIndexSet &mapper = datatypeMapperVec.at(mapInd);
			mapper.first.SetWasRestrictionDataype(true);
			}
		}
	}

/*! creates a standard datatype mapper (symbols "01" and no gaps or equates) */
NxsDiscreteDatatypeMapper::NxsDiscreteDatatypeMapper()
	:geneticCode(NXS_GCODE_NO_CODE),
	datatype(NxsCharactersBlock::standard),
	restrictionDataype(false),
	userDefinedEquatesBeforeConversion(false)
	{
	symbols.assign("01");
	matchChar = '\0';
	gapChar = '\0';
	missing = '?';
	respectCase = false;
	RefreshMappings(0L);
	}

NxsDiscreteDatatypeMapper & NxsDiscreteDatatypeMapper::operator=(const NxsDiscreteDatatypeMapper& other)
	{
	symbols = other.symbols;
	lcsymbols = other.lcsymbols;
	nStates = other.nStates;
	matchChar = other.matchChar;
	gapChar = other.gapChar;
	missing = other.missing;
	respectCase = other.respectCase;
	extraEquates = other.extraEquates;
	datatype = other.datatype;
	geneticCode = other.geneticCode;
	sclOffset = other.sclOffset;
	stateSetsVec = other.stateSetsVec;
	if (stateSetsVec.empty())
		stateCodeLookupPtr = 0L;
	else
		stateCodeLookupPtr = &stateSetsVec[-sclOffset];
	charToStateCodeLookup = other.charToStateCodeLookup;
	if (charToStateCodeLookup.empty())
		cLookup = 0L;
	else
		cLookup = &charToStateCodeLookup[127];
	restrictionDataype = other.restrictionDataype;
	userDefinedEquatesBeforeConversion = other.userDefinedEquatesBeforeConversion;
	return *this;
	}

/*! creates a datatype mapper for a datatype with the default symbols, and possibly a gap char.

	Uses ? as the missing symbol.
*/
NxsDiscreteDatatypeMapper::NxsDiscreteDatatypeMapper(
	NxsCharactersBlock::DataTypesEnum datatypeE, /*!< the datatype enum facet -- cannot be mixed*/
	bool usegaps) /*!< if true then '-' will be used as the gapChar*/
	:geneticCode(NXS_GCODE_NO_CODE),
	cLookup(NULL),
	stateCodeLookupPtr(NULL),
	matchChar('.'),
	gapChar('\0'),
	missing('?'),
	respectCase(false),
	datatype(datatypeE),
	restrictionDataype(false),
	userDefinedEquatesBeforeConversion(false)
	{
	symbols = NxsCharactersBlock::GetDefaultSymbolsForType(datatype);
	if (usegaps)
		gapChar = '-';
	if (datatype == NxsCharactersBlock::mixed)
		throw NxsException("Cannot create a mixed datatype mapper"); // this should be the only empty string-generating datatype
	RefreshMappings(0L);
	}

/*! \returns true if this and other have:
		The same symbols list,
		the same interpretation of state codes.

	Note that the "keys" used in equates may differ (that is a syntactic difference not a semantic one) even if the function returns true.

	This function is useful when checking to see if a Datatype differs significantly from the default type.  If two types are semantically
	equivalent then their state-coded matrices can be concatenated (or you can manipulate either matrix using the same operations).
*/
bool NxsDiscreteDatatypeMapper::IsSemanticallyEquivalent(const NxsDiscreteDatatypeMapper &other) const
	{
	if (datatype != other.datatype)
		return false;
	if (symbols != other.symbols)
		return false;
	bool thisHasGap = (gapChar != '\0');
	bool otherHasGap = (other.gapChar != '\0');
	if (thisHasGap != otherHasGap)
		return false;
	const NxsDiscreteStateCell nsc = (NxsDiscreteStateCell)GetHighestStateCode();
	if(nsc != (NxsDiscreteStateCell) other.GetHighestStateCode())
		return false;
	for (NxsDiscreteStateCell i = 0; i <= nsc; ++i)
		{
		if (GetStateSetForCode(i) != other.GetStateSetForCode(i))
			return false;
		}
	return true;
	}

/*!
	Must be called when the symbols list changes.
	Uses symbols, gap, missing, respectCase,  extraEquates, and datatype fields to establish new mappings.
	token can be NULL if the call is not triggered by the reading of a NEXUS token.
*/
void NxsDiscreteDatatypeMapper::RefreshMappings(NxsToken *token)
	{
	nStates = (unsigned)symbols.length();
	if (nStates ==  0)
		{
		if (datatype != NxsCharactersBlock::continuous)
			throw NxsException("Cannot create a datatype mapper with no symbols");
		return;
		}
	if (!respectCase)
		{
		NxsString::to_upper(symbols);
		lcsymbols = symbols;
		}
	else
		lcsymbols.clear();

	NxsString::to_lower(lcsymbols);

	if (missing == '\0')
		throw NxsException("Cannot create a datatype mapper with no missing data symbol");

	charToStateCodeLookup.assign(384, NXS_INVALID_STATE_CODE); /*256+128 = 384 -- this way we can deal with signed or unsigned chars by pointing cLookup to element 128*/
	cLookup = &charToStateCodeLookup[127];
	stateIntersectionMatrix.clear();
	isStateSubsetMatrix.clear();
	isStateSubsetMatrixGapsMissing.clear();

	stateSetsVec.clear();
	stateCodeLookupPtr = 0L;
	sclOffset = (gapChar == '\0' ? -1 : -2);

	std::string bogus;
	std::istringstream bogusStream(bogus);
	NxsToken bogusToken(bogusStream);
	token = (token == NULL ? &bogusToken : token);

	/* add the "fundamental" states. */
	std::set<NxsDiscreteStateCell> stSet;
	std::set<NxsDiscreteStateCell> missingSet;
	if (gapChar != '\0')
		{
		stSet.insert(NXS_GAP_STATE_CODE);
		/* this is the one of only 2 times that  we don't call AddStateSet to add a state set
			we do this to avoid illegal indexing of stateSets[1] when there
			is only one element in the vector.
		*/
		stateSetsVec.push_back(NxsDiscreteStateSetInfo(stSet, false, gapChar));
		cLookup[(int) gapChar] = NXS_GAP_STATE_CODE;

		missingSet.insert(NXS_GAP_STATE_CODE);
		}


		/*
			Add the missing state code
			this is the other time that we don't call AddStateSet (to avoid illegal indexing).
		*/
	NCL_ASSERT(missing != '\0');
	NCL_ASSERT(nStates > 0);
	for (NxsDiscreteStateCell s = 0; s < (NxsDiscreteStateCell) nStates; ++s)
		missingSet.insert(s);

	char sym = (respectCase ? missing : (char) toupper(missing));
	stateSetsVec.push_back(NxsDiscreteStateSetInfo(missingSet, false, sym));
	const NxsDiscreteStateCell stateCode = (const NxsDiscreteStateCell)stateSetsVec.size() + sclOffset - 1;
	NCL_ASSERT(NXS_MISSING_CODE == stateCode);
	if (respectCase)
		cLookup[(int) missing] = stateCode;
	else
		{
		cLookup[(int) tolower(missing)] = stateCode;
		cLookup[(int) toupper(missing)] = stateCode;
		}
	NCL_ASSERT(cLookup[(int) missing] == NXS_MISSING_CODE);
	for (NxsDiscreteStateCell s = 0; s < (NxsDiscreteStateCell) nStates; ++s)
		{
		stSet.clear();
		stSet.insert(s);
		AddStateSet(stSet, symbols[s], respectCase, false);
		}

	/* add the default equates */
	std::map<char, NxsString> defEq = NxsCharactersBlock::GetDefaultEquates(datatype);



	bool convertToStandard = false;
	if (((datatype == NxsCharactersBlock::nucleotide) || (datatype == NxsCharactersBlock::dna)) && symbols != "ACGT")
		convertToStandard = true;
	else if ((datatype == NxsCharactersBlock::rna) && symbols != "ACGU")
		convertToStandard = true;
	else if ((datatype == NxsCharactersBlock::protein) && symbols != "ACDEFGHIKLMNPQRSTVWY*")
		convertToStandard = true;
	if (convertToStandard)
		{
		if (!extraEquates.empty())
			userDefinedEquatesBeforeConversion = true;
		defEq.insert(extraEquates.begin(), extraEquates.end());
		extraEquates.clear();
		defEq.swap(extraEquates);
		/* respectcase is only "applicable" to Standard datatype
			Any symbol extension will be at the end of the symbols list,
			so here we add the lower case symbols as equates.
		*/
		if (respectCase)
			{
			std::string lcsym = NxsCharactersBlock::GetDefaultSymbolsForType(datatype);
			NxsString::to_lower(lcsym);
			std::string ucsym = lcsym;
			NxsString::to_upper(ucsym);
			for (unsigned i = 0; i < ucsym.length(); ++i)
				{
				if (ucsym[i] != lcsym[i])
					{
					NxsString u;
					u.append(1, ucsym[i]);
					extraEquates[lcsym[i]] = u;
					}
				}
			}
		datatype =  NxsCharactersBlock::standard;
		}

	/* It is nice to put the all-states code at state code = num_states So here, we'll put this equate in that slot (if such an equate exists)*/
	std::set<char> targetSet(symbols.begin(), symbols.end());
	char allStateEquateKey = '\0';
	std::map<char, NxsString>::const_iterator eqIt = defEq.begin();
	NxsString taxonName;
	for (; eqIt != defEq.end(); ++eqIt)
		{
		const char c = eqIt->first;
		const char u = toupper(c);
		bool addEq = true;
		if (c == missing || c == matchChar || c == gapChar)
			addEq = false;
		if (!respectCase && (u == toupper(missing) || u == toupper(matchChar) || u == toupper(gapChar)))
			addEq = false;
		if (addEq)
			{
			const NxsString & s = eqIt->second;
			unsigned slen = (unsigned)s.length();
			if (slen == 2 + symbols.length())
				{
				if (s[0] == '{' && s[slen -1] == '}')
					{
					std::set<char> contained;
					for (unsigned j = 1; j < slen - 1; ++j)
						contained.insert(s[j]);
					if (contained == targetSet)
						{
						allStateEquateKey = c;
						NxsDiscreteStateCell sc = StateCodeForNexusPossibleMultiStateSet(allStateEquateKey, s, *token, UINT_MAX, UINT_MAX, 0L, taxonName);
						cLookup[(int) allStateEquateKey] = sc;
						break;
						}
					}
				}

			}
		}

	eqIt = defEq.begin();
	for (; eqIt != defEq.end(); ++eqIt)
		{
		const char c = eqIt->first;
		if (c == allStateEquateKey)
			continue;
		const char u = toupper(c);
		bool addEq = true;
		if (c == missing || c == matchChar || c == gapChar)
			addEq = false;
		if (!respectCase && (u == toupper(missing) || u == toupper(matchChar) || u == toupper(gapChar)))
			addEq = false;
		if (addEq)
			{
			const NxsString & s = eqIt->second;
			NxsDiscreteStateCell sc = StateCodeForNexusPossibleMultiStateSet(c, s, *token, UINT_MAX, UINT_MAX, 0L, taxonName);
			cLookup[(int) c] = sc;
			}
		}




	/* add user-defined equates, and only retain the new ones (those that are not datatype defaults). */
	std::map<char, NxsString> neededExtraEquates;
	for (eqIt = extraEquates.begin(); eqIt != extraEquates.end(); ++eqIt)
		{
		const char c = eqIt->first;
		const char u = toupper(c);
		if (PositionInSymbols(c) == NXS_INVALID_STATE_CODE)
			{
			bool addEq = true;
			if (c == missing || c == matchChar || c == gapChar)
				addEq = false;
			if (!respectCase && (u == toupper(missing) || u == toupper(matchChar) || u == toupper(gapChar)))
				addEq = false;
			if (addEq)
				{
				const NxsDiscreteStateCell prevCode = cLookup[(int) c];
				const NxsString & s = eqIt->second;
				NxsDiscreteStateCell sc = StateCodeForNexusPossibleMultiStateSet(c, s, *token, UINT_MAX, UINT_MAX, 0L, taxonName);
				cLookup[(int) c] = sc;
				if (sc != prevCode) /* the equate was new */
					neededExtraEquates[c] = s;
				}
			}
		else
			{
			NCL_ASSERT(convertToStandard); // a equate key that is equal to a symbol can happen if the symbols list is augmented (resulting in a conversion to standard datatype)
			}
		}
	extraEquates = neededExtraEquates;
	}

/*!
	Returns the state code of a (possible new state set) `sset`.  This may trigger the reallocation of mapping info.
	nexusSymbol can be '\0' if there is not a single-character symbol that represents this state set.

	if `addToLookup` is false and the state set is not found then NXS_INVALID_STATE_CODE will be returned.

	if the stateset is added with a `nexusSymbol` then the new "symbol" will be case-sensitive
	(this is an mechanism for entering equates and equates are always case sensitive).

	New "fundamental" states can NOT be introduced using this function -- if unknown states are encountered, an exception will be generated.
*/
NxsDiscreteStateCell NxsDiscreteDatatypeMapper::StateCodeForStateSet(const std::set<NxsDiscreteStateCell> & sset, bool isPolymorphic, bool addToLookup, char nexusSymbol)
	{
	if (sset.size() == 1)
		{
		NxsDiscreteStateCell c = *sset.begin();
		ValidateStateIndex(c);
		return c;
		}
	NCL_ASSERT(stateCodeLookupPtr);
	NxsDiscreteStateSetInfo *sclStart = stateCodeLookupPtr + nStates;
	const NxsDiscreteStateCell nCodes = (NxsDiscreteStateCell)stateSetsVec.size();

	/*we can start at nStates, because < nStates will be handled in the sset.size() == 1 above */
	for (NxsDiscreteStateCell i = nStates - sclOffset; i < nCodes; ++i)
		{
		NxsDiscreteStateSetInfo & stateSetInfo = *sclStart++;
		if (sset == stateSetInfo.states && isPolymorphic == stateSetInfo.isPolymorphic)
			return i + sclOffset;
		}
	for (std::set<NxsDiscreteStateCell>::const_iterator sIt = sset.begin(); sIt != sset.end(); ++sIt)
		ValidateStateIndex(*sIt);
	if (!isPolymorphic)
		{
		const unsigned nsymbs = (const unsigned)sset.size();
		if (gapChar != '\0' && nsymbs == GetNumStatesIncludingGap())
			return NXS_MISSING_CODE;
		}
	if (!addToLookup)
		return NXS_INVALID_STATE_CODE;
	return AddStateSet(sset, nexusSymbol, true, isPolymorphic);
	}

/*!
	Adds a new state set and returns its code.
	Does NOT check if the state set is present.
	It is also MANDATORY that this function be called with the fundamental states first (and in order) before
	 being called with any multi state sets (this is done by RefreshMappings)
*/
NxsDiscreteStateCell NxsDiscreteDatatypeMapper::AddStateSet(const std::set<NxsDiscreteStateCell> & states, char nexusSymbol, bool symRespectCase, bool isPolymorphic)
	{
	stateIntersectionMatrix.clear();
	isStateSubsetMatrix.clear();
	isStateSubsetMatrixGapsMissing.clear();


	bool reallyIsPoly = (states.size() > 1 && isPolymorphic);
	char sym = (symRespectCase ? nexusSymbol : (char) toupper(nexusSymbol));
	stateSetsVec.push_back(NxsDiscreteStateSetInfo(states, reallyIsPoly, sym));
	/* if we have gaps, then the sclOffset is -1 and we want to enable
		stateCodeLookup[-1], so we set stateCodeLookup to &stateSets[1]
		hence the -sclOffset below
	*/
	stateCodeLookupPtr = &stateSetsVec[-sclOffset];

	const NxsDiscreteStateCell stateCode = (const NxsDiscreteStateCell)stateSetsVec.size() + sclOffset - 1;
	if (nexusSymbol != '\0')
		{
		if (symRespectCase)
			cLookup[(int) nexusSymbol] = stateCode;
		else
			{
			cLookup[(int) tolower(nexusSymbol)] = stateCode;
			cLookup[(int) toupper(nexusSymbol)] = stateCode;
			}
		}
	return stateCode;
	}



/*!
	Throws an NxsNCLAPIException  if `c` is not a valid index of one of the "fundamental" states for the datatype.
*/
void NxsDiscreteDatatypeMapper::ValidateStateIndex(NxsDiscreteStateCell c) const
	{
	if (c < NXS_MISSING_CODE)
		{
		if (c == NXS_GAP_STATE_CODE)
			{
			if (gapChar == '\0')
				throw NxsNCLAPIException("Illegal usage of NXS_GAP_STATE_CODE in a datatype without gaps");
			return;
			}
		if (c == NXS_INVALID_STATE_CODE)
			throw NxsNCLAPIException("Illegal usage of NXS_INVALID_STATE_CODE as a state index");
		throw NxsNCLAPIException("Illegal usage of unknown negative state index");
		}
	else if (c >= (NxsDiscreteStateCell) nStates)
		throw NxsNCLAPIException("Illegal usage of state index >= the number of states");
	}

/*!
	Throws an NxsNCLAPIException  if `c` is not a valid state code.
*/
void NxsDiscreteDatatypeMapper::ValidateStateCode(NxsDiscreteStateCell c) const
	{
	if (c < sclOffset)
		{
		if (c == NXS_GAP_STATE_CODE)
			{
			if (gapChar == '\0')
				throw NxsNCLAPIException("Illegal usage of NXS_GAP_STATE_CODE in a datatype without gaps");
			}
		if (c == NXS_INVALID_STATE_CODE)
			throw NxsNCLAPIException("Illegal usage of NXS_INVALID_STATE_CODE as a state code");
		throw NxsNCLAPIException("Illegal usage of unknown negative state index");
		}
	else if (c >= (((NxsDiscreteStateCell) stateSetsVec.size()) + sclOffset))
	    {
	    NxsString err = "Illegal usage of state code > the highest state code. c = ";
	    err << int(c) << " (NxsDiscreteStateCell) stateSetsVec.size() = " << (NxsDiscreteStateCell) stateSetsVec.size();
	    err << " sclOffset = " << sclOffset;
	    throw NxsNCLAPIException(err);
	    }
	}


void NxsDiscreteDatatypeMapper::GenerateNxsExceptionMatrixReading(char const* message, unsigned int taxInd, unsigned int charInd,
NxsToken* token, const NxsString &nameStr)
	{
	NxsString e = "Error reading character ";
	e << charInd + 1<<" for taxon " << taxInd + 1;
	if (!nameStr.empty())
		{
		NxsString nasn;
		nasn << taxInd + 1;
		if (nasn != nameStr)
			e << " (name \""<< nameStr <<"\")";
		}
	e << ":\n" << message;
	if (token)
		throw NxsException(e, *token);
	else
		throw NxsException(e);
	}

/*!
	Returns true if the state code maps to a collection of states that were flagged as polymorphic.
	generates a NxsNCLAPIException if `c` is not a valid state code
*/
bool NxsDiscreteDatatypeMapper::IsPolymorphic(NxsDiscreteStateCell c) const
	{
	NCL_ASSERT(stateCodeLookupPtr);
	ValidateStateCode(c);
	return stateCodeLookupPtr[c].isPolymorphic;
	}


/*!
	Returns NXS_INVALID_STATE_CODE or the index of `c` in the symbols list.
	case-sensitivity is controlled by this->respectCase attribute.

	NOTE: the gap "state" and missing characters are NOT in the symbols list.
*/
NxsDiscreteStateCell NxsDiscreteDatatypeMapper::PositionInSymbols(char c) const
	{
	NxsDiscreteStateCell p = (NxsDiscreteStateCell)symbols.find(c);
	if (p >= 0 && p < (NxsDiscreteStateCell) nStates)
		return p;
	if (!respectCase)
		{
		p = (NxsDiscreteStateCell)lcsymbols.find(c);
		if (p >= 0 && p < (NxsDiscreteStateCell) nStates)
			return p;
		}
	return NXS_INVALID_STATE_CODE;
	}


/*!
	Returns the NEXUS reperesenation of the state code `scode` which may be a
	multiple character string such as {DNY}
   Generates a NxsNCLAPIException if `c` is not a valid state code.
	If the string cannot be expressed (insufficient symbols are defined) then
	`demandSymbols` controls the behavior.  If `demandSymbols` is true than a
	NxsNCLAPIException is thrown. If `demandSymbols` is false then no output is
	written.
*/
void NxsDiscreteDatatypeMapper::WriteStateCodeAsNexusString(std::ostream & out, NxsDiscreteStateCell scode, bool demandSymbols) const
	{
	//out << "WriteStateCodeAsNexusString-debug scode=" << scode<< '\n';
	ValidateStateCode(scode);
	const NxsDiscreteStateSetInfo & stateSetInfo =  stateCodeLookupPtr[scode];
	NCL_ASSERT (&(stateSetsVec.at(scode-sclOffset)) == &stateSetInfo);
	char c = stateSetInfo.nexusSymbol;
	if (c != '\0')
		{
		out << c;
		return;
		}
	std::string towrite;
	std::set<NxsDiscreteStateCell>::const_iterator sIt = stateSetInfo.states.begin();
	const std::set<NxsDiscreteStateCell>::const_iterator endIt = stateSetInfo.states.end();
	for (; sIt != endIt; ++sIt)
		{
		const NxsDiscreteStateCell state = *sIt;
		const NxsDiscreteStateSetInfo & subStateSetInfo =  stateCodeLookupPtr[state];
		const char subc = subStateSetInfo.nexusSymbol;
		if (subc != '\0')
			towrite.append(1, subc);
		else if (demandSymbols)
			{
			NxsString err("No symbol found for state code ");
			err << state;
			throw NxsNCLAPIException(err);
			}
		else
			return;
		}

	out <<	(stateSetInfo.isPolymorphic ? '(' : '{');
	out << towrite;
	out <<	(stateSetInfo.isPolymorphic ? ')' : '}');
	}

unsigned NxsDiscreteDatatypeMapper::GetNumStatesInStateCode(NxsDiscreteStateCell scode) const
	{
	ValidateStateCode(scode);
	const NxsDiscreteStateSetInfo & stateSetInfo =  stateCodeLookupPtr[scode];
	return (unsigned)stateSetInfo.states.size();
	}

void NxsDiscreteDatatypeMapper::WriteStartOfFormatCommand(std::ostream & out) const
	{
	out << "    FORMAT Datatype=" << NxsCharactersBlock::GetNameOfDatatype(datatype);
	if (this->missing != '?')
		{
		out << " Missing=";
		out << this->missing;
		}
	if (this->gapChar != '\0')
		{
		out << "  Gap=";
		out << this->gapChar;
		}
	if (this->datatype != NxsCharactersBlock::continuous)
		{
		unsigned numDefStates = 4;
		if (this->datatype == NxsCharactersBlock::protein)
			numDefStates = 21;
		else if (this->datatype == NxsCharactersBlock::standard)
			numDefStates = 0;
		unsigned nSym = (unsigned)this->symbols.length();
		if (nSym > numDefStates && this->datatype != NxsCharactersBlock::codon)
			{
			out << " Symbols=\"";
			for (unsigned i = numDefStates; i < nSym; ++i)
				{
				char c = symbols[i];
				if (c == '\0')
					break;
				out << c;
				}
			out <<"\"";
			}
		}
	const std::map<char, NxsString> defEquates = NxsCharactersBlock::GetDefaultEquates(datatype);
	std::map<char, NxsString> toWrite;
	const std::map<char, NxsString>::const_iterator notFound = defEquates.end();
	std::map<char, NxsString>::const_iterator inDefEquates;
	for (std::map<char, NxsString>::const_iterator i = extraEquates.begin(); i != extraEquates.end(); ++i)
		{
		const char key =  (*i).first;
		const NxsString val =  i->second;
		inDefEquates = defEquates.find(key);
		if (inDefEquates == notFound || inDefEquates->second != val)
			toWrite[key] = val;
		}
	if (toWrite.size() > 0)
		{
		out << " Equate=\"";
		for (std::map<char, NxsString>::const_iterator j = toWrite.begin(); j != toWrite.end(); ++j)
			out << ' ' << j->first << '=' << j->second;
		out <<"\"";
		}
	}

bool NxsCharactersBlock::HandleNextContinuousState(NxsToken &token, unsigned taxNum, unsigned charNum, ContinuousCharRow & row, const NxsString & )
	{
	if (interleaving)
		token.SetLabileFlagBit(NxsToken::newlineIsToken);
	token.SetLabileFlagBit(NxsToken::hyphenNotPunctuation);
	std::vector<double> v;
	std::vector<int> scored;
	token.GetNextToken();
	NxsString t;
	if (interleaving && token.AtEOL())
		return false;
	if (token.Equals("("))
		{
		token.SetLabileFlagBit(NxsToken::hyphenNotPunctuation);
		token.GetNextToken();
		while (!token.Equals(")"))
			{
			t = token.GetToken();
			if (t.length() == 1 && (t[0] == missing || t[0] == gap))
				{
				v.push_back(DBL_MAX);
				scored.push_back(0);
				}
			else if (t.length() == 1 && t[0] == matchchar)
				{
				v.push_back(DBL_MAX);
				scored.push_back(2);
				}
			else if (!t.IsADouble())
				GenerateUnexpectedTokenNxsException(token, "a number");
			else
				{
				v.push_back(t.ConvertToDouble());
				scored.push_back(1);
				}
			token.SetLabileFlagBit(NxsToken::hyphenNotPunctuation);
			token.GetNextToken();
			}
		}
	else
		{
		t = token.GetToken();
		if (t.length() == 1 && (t[0] == missing || t[0] == gap))
			{
			v.push_back(DBL_MAX);
			scored.push_back(0);
			}
		else if (t.length() == 1 && t[0] == matchchar)
			{
			v.push_back(DBL_MAX);
			scored.push_back(2);
			}
		else if (!t.IsADouble())
			GenerateUnexpectedTokenNxsException(token, "a number");
		else
			{
			v.push_back(t.ConvertToDouble());
			scored.push_back(1);
			}
		}
	unsigned n_read = (unsigned)v.size();
	if (n_read < items.size())
		{
		errormsg.clear();
		errormsg << "For each cell of the MATRIX a value for each of the " << (unsigned)items.size() <<  " ITEMS listed in the FORMAT command is expected.\nOnly " <<  n_read << " values read.";
		GenerateNxsException(token);
		}
	// We've read in the state now, so if this character has been eliminated, we don't want to go any further with it
	//
	if (charNum == UINT_MAX)
		return true;

	if (charNum > row.size())
		GenerateNxsException(token, "Internal Error: character index out of range in continuousMatrix.");

	ContinuousCharCell & cell = row[charNum];
	cell.clear();

	std::vector<std::string >::const_iterator itemIt = items.begin();
	std::string key;
	unsigned curr_ind_in_v = 0;
	for (; itemIt != items.end(); ++itemIt, ++curr_ind_in_v)
		{
		key = *itemIt;
		if (scored[curr_ind_in_v] == 1)
			cell[key] = vector<double>(1, v[curr_ind_in_v]);
		else if (scored[curr_ind_in_v] == 0)
			cell[key] = vector<double>();
		else
			{
			if (taxNum == 0)
				GenerateNxsException(token, "MATCHCHAR cannot be used in the first taxon");
			const vector<double> & first_taxon_vector = continuousMatrix[0][charNum][key];
			if (first_taxon_vector.empty())
				GenerateNxsException(token, "First taxon does not have a value to copy, but a MATCHCHAR was found.");
			else
				cell[key] = vector<double>(1, first_taxon_vector[0]);
			}
		}
	unsigned curr_ind_mapped = 1;
	if (!key.empty() && curr_ind_in_v < n_read)
		{
		vector<double> & curr_cell_vector = cell[key];
		for (; curr_ind_in_v < n_read; ++curr_ind_in_v, ++curr_ind_mapped)
			{
			if (scored[curr_ind_in_v] == 1)
				curr_cell_vector.push_back(v[curr_ind_in_v]);
			else if (scored[curr_ind_in_v] != 0)
				curr_cell_vector.push_back(DBL_MAX);
			else
				{
				if (taxNum == 0)
					GenerateNxsException(token, "MATCHCHAR cannot be used in the first taxon");
				const vector<double> & first_taxon_vector = continuousMatrix[0][charNum][key];
				if (first_taxon_vector.size() < curr_ind_mapped+1)
					GenerateNxsException(token, "First taxon does not have a value to copy, but a MATCHCHAR was found.");
				else
					curr_cell_vector.push_back(first_taxon_vector[curr_ind_mapped]);
				}
			}
		}
	return true;
	}

NxsDiscreteStateCell NxsDiscreteDatatypeMapper::StateCodeForNexusChar(
  const char currChar,
  NxsToken *token,
  unsigned taxNum,		/* the taxon index, in range [0..`ntax') */
  unsigned charNum,		/* the character index, in range [0..`nChar') */
  const NxsDiscreteStateRow * firstTaxonRow,
  const NxsString & nameStr) const
  	{
	NxsDiscreteStateCell currState = cLookup[static_cast<int>(currChar)];
	if (currState == NXS_INVALID_STATE_CODE)
		{
		NxsString emsg;
		if (currChar == matchChar)
			{
			if (firstTaxonRow == NULL)
				GenerateNxsExceptionMatrixReading("Unexpected use of MatchChar in first taxon with data.", taxNum, charNum, token, nameStr);
			if (firstTaxonRow->size() <= charNum)
				{
				emsg << "MatchChar found for character number "  << charNum+1 << " but the first taxon does not have a character state stored for this character.";
				GenerateNxsExceptionMatrixReading(emsg, taxNum, charNum, token, nameStr);
				}
			currState = (*firstTaxonRow)[charNum];
			}
		else
			{
			emsg << "Invalid state specified \"" << currChar << "\"";
			GenerateNxsExceptionMatrixReading(emsg, taxNum, charNum, token, nameStr);
			}
		}
	return currState;
	}

bool NxsCharactersBlock::HandleNextDiscreteState(
  NxsToken &token,
  unsigned taxNum,
  unsigned charNum,
  NxsDiscreteStateRow & row,
  NxsDiscreteDatatypeMapper &mapper,
  const NxsDiscreteStateRow * firstTaxonRow,
  const NxsString & nameStr)
  	{
  	if (interleaving)
		token.SetLabileFlagBit(NxsToken::newlineIsToken);
	NCL_ASSERT(!tokens);
	token.SetLabileFlagBit(NxsToken::parentheticalToken);
	token.SetLabileFlagBit(NxsToken::curlyBracketedToken);
	token.SetLabileFlagBit(NxsToken::singleCharacterToken);

	token.GetNextToken();

	if (interleaving && token.AtEOL())
		return false;
	const NxsString &stateAsNexus = token.GetTokenReference();
	NxsDiscreteStateCell sc =  mapper.EncodeNexusStateString(stateAsNexus, token, taxNum, charNum, firstTaxonRow, nameStr);
	NCL_ASSERT(charNum < row.size());
	row[charNum] = sc;
	return true;
	}

NxsDiscreteStateCell NxsDiscreteDatatypeMapper::StateCodeForNexusPossibleMultiStateSet(
  const char nexusSymbol,
  const std::string &stateAsNexus,
  NxsToken & token,	/* the token used to read from `in' */
  const unsigned taxNum,		/* the taxon index, in range [0..`ntax') */
  const unsigned charNum,		/* the character index, in range [0..`nChar') */
  const NxsDiscreteStateRow * firstTaxonRow, const NxsString &nameStr)
	{
	NCL_ASSERT(stateAsNexus.length() > 0);
	const char firstChar = stateAsNexus[0];
	if (firstChar == '(' || firstChar == '{')
		return StateCodeForNexusMultiStateSet(nexusSymbol, stateAsNexus, &token, taxNum, charNum, firstTaxonRow, nameStr);
	if (stateAsNexus.length() > 1)
		{
		NxsString emsg;
		emsg << "Expecting  {} or () around a multiple character state set.  Found " << stateAsNexus << " for taxon " << nameStr;
		GenerateNxsExceptionMatrixReading(emsg, taxNum, charNum, &token, nameStr);
		}

	NxsDiscreteStateCell currState = StateCodeForNexusChar(firstChar, &token, taxNum, charNum, firstTaxonRow, nameStr);
	cLookup[(int) nexusSymbol] = currState;
	return currState;
	}

NxsDiscreteStateCell NxsDiscreteDatatypeMapper::StateCodeForNexusMultiStateSet(
  const char nexusSymbol,
  const std::string &stateAsNexus,
  NxsToken * token,	/* the token used to read from `in' */
  const unsigned taxNum,		/* the taxon index, in range [0..`ntax') */
  const unsigned charNum,		/* the character index, in range [0..`nChar') */
  const NxsDiscreteStateRow * firstTaxonRow,
  const NxsString &nameStr)
  	{
	const char firstChar = stateAsNexus[0];
	NxsString emsg;
	const bool poly = (firstChar == '(');
	if ((!poly) && firstChar != '{')
		{
		emsg << "Expecting a state symbol of set of symbols in () or  {} braces.  Found " << stateAsNexus;
		GenerateNxsExceptionMatrixReading(emsg, taxNum, charNum, token, nameStr);
		}
	bool tildeFound = false;
	NxsDiscreteStateCell prevState = NXS_INVALID_STATE_CODE;
	char prevChar = firstChar;
	std::string::const_iterator cIt = stateAsNexus.begin();
	std::string::const_iterator endIt = stateAsNexus.end();
	--endIt;
	NCL_ASSERT((poly && *endIt == ')') || (!poly && *endIt == '}'));
	std::set<NxsDiscreteStateCell> sset;
	for (++cIt; cIt != endIt; ++cIt)
		{
		const char currChar = *cIt;
		if ((strchr("\n\r \t", currChar) == NULL) && currChar != ',')
			{
			if (currChar == '~')
				{
				if (prevState < 0 || prevState >= (NxsDiscreteStateCell)nStates)
					{
					emsg << "A state range cannot start with " << prevChar;
					GenerateNxsExceptionMatrixReading(emsg, taxNum, charNum, token, nameStr);
					}
				tildeFound = true;
				}
			else
				{
				// Add state symbol and record if it is the first or last one in case we encounter a tilde
				NxsDiscreteStateCell currState;
				if (tildeFound)
					{
					currState = PositionInSymbols(currChar);
					if (currState == NXS_INVALID_STATE_CODE)
						{
						emsg << "A state range cannot end with " << currChar;
						GenerateNxsExceptionMatrixReading(emsg, taxNum, charNum, token, nameStr);
						}
					if (currState < prevState)
						{
						emsg << prevChar << '~' << currChar << " is not a valid state range (the end state is a lower index than the start)";
						GenerateNxsExceptionMatrixReading(emsg, taxNum, charNum, token, nameStr);
						}
					for (NxsDiscreteStateCell i = prevState; i <= currState; ++i)
						sset.insert(i);
					tildeFound = false;
					}
				else
					{
					currState = StateCodeForNexusChar(currChar, token, taxNum, charNum, firstTaxonRow, nameStr);
					sset.insert(currState);
					}
				prevState = currState;
				prevChar = currChar;
				}
			}
		}
	if (prevChar == '~')
		{
		emsg << "State range not terminated -- ending in ~" << *endIt;
		GenerateNxsExceptionMatrixReading(emsg, taxNum, charNum, token, nameStr);
		}
	if (sset.empty())
		{
		emsg << "An illegal (empty) state range was found \"" << stateAsNexus << '\"';
		GenerateNxsExceptionMatrixReading(emsg, taxNum, charNum, token, nameStr);
		}
	return StateCodeForStateSet(sset, poly, true, nexusSymbol);
	}



/*!
	Called from HandleNextState to read in the next state when TOKENS was specified. Looks up state in character
	states listed for the character to make sure it is a valid state, and returns state's value (0, 1, 2, ...). Note:
	does NOT handle adding the state's value to matrix. Save the return value (call it k) and use the following command
	to add it to matrix: matrix->AddState(i, j, k);
*/
bool NxsCharactersBlock::HandleNextTokenState(
  NxsToken &token,
  unsigned taxNum,
  unsigned charNum,
  NxsDiscreteStateRow & row,
  NxsDiscreteDatatypeMapper &mapper,
  const NxsDiscreteStateRow * firstTaxonRow,
  const NxsString & nameStr)
  {
	if (interleaving)
		token.SetLabileFlagBit(NxsToken::newlineIsToken);
	token.GetNextToken();
	if (interleaving && token.AtEOL())
		return false;
	if (token.GetTokenLength() ==  0)
		GenerateNxsException(token, "Unexpected empty token encountered");

	int polymorphism = token.Equals("(");
	int uncertainty	 = token.Equals("{");
	if (!uncertainty && !polymorphism)
		{
		row[charNum] = HandleTokenState(token, taxNum, charNum, mapper, firstTaxonRow, nameStr);
		return true;
		}

	/*TODO - supporting this requires more bookeeping to allow the mapper to deal with
	anonymous fundamental states  -- difficult because we don't know the number of symbols in TOKENS mode.*/
	errormsg = "Currently polymorphism and ambiguity are not supported for matrices in TOKENS mode: ";
	errormsg << token.GetToken() << " found while reading character " << charNum + 1 << " of taxon \"" << nameStr << '\"';
	throw NxsException(errormsg, token);

	bool tildeFound = false;
	NxsDiscreteStateCell prevState = NXS_INVALID_STATE_CODE;
	std::string prevToken = token.GetToken();
	std::set<NxsDiscreteStateCell> sset;
	for (;;)
		{
		// OPEN ISSUE: What about newlines if interleaving? I'm assuming
		// that the newline must come between characters to count.

		token.SetLabileFlagBit(NxsToken::tildeIsPunctuation);
		token.GetNextToken();

		if (token.Equals(","))
			{
			; /*Mesquite exports with , in state sets. We'll ignore the comma*/
			}
		if (polymorphism)
			{
			if (token.Equals(")"))
				{
				if (tildeFound)
					mapper.GenerateNxsExceptionMatrixReading("Range of states still being specified when ')' encountered", taxNum, charNum, &token, nameStr);
				break;
				}
			if (token.Equals("{"))
				mapper.GenerateNxsExceptionMatrixReading("Illegal range of states '{' found inside '()'", taxNum, charNum, &token, nameStr);

			}
		else if (uncertainty)
			{
			if (token.Equals("}"))
				{
				if (tildeFound)
					mapper.GenerateNxsExceptionMatrixReading("Range of states still being specified when '}' encountered", taxNum, charNum, &token, nameStr);
				break;
				}
			if (token.Equals("("))
				mapper.GenerateNxsExceptionMatrixReading("Illegal range of states '(' found inside '{}'", taxNum, charNum, &token, nameStr);
			}
		else if (token.Equals("~"))
			{
			if (prevState < 0 || prevState >= (NxsDiscreteStateCell)symbols.length())
				{
				errormsg.clear();
				errormsg << "A state range cannot start with " << prevToken;
				mapper.GenerateNxsExceptionMatrixReading(errormsg, taxNum, charNum, &token, nameStr);
				}
			tildeFound = true;
			}
		else
			{
			NxsDiscreteStateCell currState;
			if (tildeFound)
				{
				currState = HandleTokenState(token, taxNum, charNum, mapper, firstTaxonRow, nameStr);
				if (currState <= prevState)
					{
					errormsg = "Last state in specified range (";
					errormsg << token.GetToken() << ") must be greater than the first";
					mapper.GenerateNxsExceptionMatrixReading(errormsg, taxNum, charNum, &token, nameStr);
					}
				for (NxsDiscreteStateCell i = prevState; i <= currState; ++i)
					sset.insert(i);
				tildeFound = false;
				}
			else
				{
				// Add current state, then set first to that state's value
				// State's value is its position within the list of states
				// for that character
				//
				currState = HandleTokenState(token, taxNum, charNum, mapper, firstTaxonRow, nameStr);
				sset.insert(currState);
				}
			prevState = currState;
			prevToken = token.GetToken();
			}
		}

	if (prevToken == "~")
		{
		errormsg.clear();
		errormsg << "State range not terminated -- ending in ~" << token.GetToken();
		mapper.GenerateNxsExceptionMatrixReading(errormsg, taxNum, charNum, &token, nameStr);
		}
	if (sset.empty())
		{
		errormsg.clear();
		errormsg << "An illegal (empty) state range -- either  {} or ()";
		mapper.GenerateNxsExceptionMatrixReading(errormsg, taxNum, charNum, &token, nameStr);
		}
	row[charNum] = mapper.StateCodeForStateSet(sset, (const bool)(polymorphism != 0), true, '\0');
	return true;
	}

NxsDiscreteStateCell NxsCharactersBlock::HandleTokenState(
  NxsToken &token,	/* the token used to read from `in' */
  unsigned taxNum,
  unsigned charNum,
  NxsDiscreteDatatypeMapper &,
  const NxsDiscreteStateRow * ,
  const NxsString & nameStr)
  	{
	// Token may be one of the character states listed for character charNum in charStates
	const std::string t = token.GetToken(respectingCase);
	NxsStringVectorMap::const_iterator bagIter	= charStates.find(charNum);
	/*
	 if (bagIter == charStates.end())
		return mapper.EncodeNexusStateString(t, token, taxNum, charNum, firstTaxonRow);
	*/
	NxsStringVector::const_iterator ci_begin	= bagIter->second.begin();
	NxsStringVector::const_iterator ci_end		= bagIter->second.end();
	NxsStringVector::const_iterator cit;
	NxsDiscreteStateCell k = 0;
	for (; ci_begin != ci_end; ++ci_begin, ++k)
		{
		if (respectingCase)
			{
			if (*ci_begin == t)
				return k;
			}
		else
			{
			if (NxsString::case_insensitive_equals(t.c_str(), ci_begin->c_str()))
				return k;
			}
		}
	//return mapper.EncodeNexusStateString(t, token, taxNum, charNum, firstTaxonRow);
	errormsg = "Unrecognized state ";
	errormsg << t << " found while reading character " << charNum + 1 << " of taxon number " << taxNum + 1;
	if (!nameStr.empty())
		errormsg << "(name \"" << nameStr << "\")";
	throw NxsException(errormsg, token);
	}


unsigned NxsCharactersBlock::GetMaxIndex() const
	{
	unsigned nct = GetNCharTotal();
	if (nct == 0)
		return UINT_MAX;
	return nct - 1;
	}

/*!
 Returns the number of indices that correspond to the label (and the number
 of items that would be added to *inds if inds points to an empty set).
*/
unsigned NxsCharactersBlock::GetIndicesForLabel(const std::string &label, NxsUnsignedSet *inds) const
	{
	NxsString emsg;
	const unsigned numb = CharLabelToNumber(label);
	if (numb != 0)
		{
		if (inds)
			inds->insert(numb - 1);
		return 1;
		}
	if (!defCodonPosPartitionName.empty())
		{
		std::string t(label);
		NxsString::to_upper(t);
		std::string n;
		if (t == "POS1")
			n.assign("1");
		else if (t == "POS2")
			n.assign("2");
		else if (t == "POS3")
			n.assign("3");
		else if (t == "NONCODING")
			n.assign("N");
		if (!n.empty())
			{
			NxsPartitionsByName::const_iterator pit = codonPosPartitions.find(defCodonPosPartitionName);
			if (pit != codonPosPartitions.end())
				{
				const NxsPartition & p = pit->second;
				for (NxsPartition::const_iterator s = p.begin(); s != p.end(); ++s)
					{
					if (NxsString::case_insensitive_equals(n.c_str(), s->first.c_str()))
						{
						unsigned nel = (unsigned)s->second.size();
						if (inds)
							inds->insert(s->second.begin(), s->second.end());
						return nel;
						}
					}
				}
			}
		}
	if (NxsString::case_insensitive_equals(label.c_str(), "CONSTANT"))
		{
		NxsUnsignedSet c;
		FindConstantCharacters(c);
		if (inds)
			inds->insert(c.begin(), c.end());
		return (unsigned)c.size();
		}
	if (NxsString::case_insensitive_equals(label.c_str(), "GAPPED"))
		{
		NxsUnsignedSet c;
		FindGappedCharacters(c);
		if (inds)
			inds->insert(c.begin(), c.end());
		return (unsigned)c.size();
		}
	return GetIndicesFromSetOrAsNumber(label, inds, charSets, GetMaxIndex(), "character");
	}

/*!
	Returns true if this set replaces an older definition.
*/
bool NxsCharactersBlock::AddNewIndexSet(const std::string &label, const NxsUnsignedSet & inds)
	{
	NxsString ls(label.c_str());
	bool replaced = charSets.count(ls) > 0;
	charSets[ls] = inds;
	return replaced;
	}

/*!
	Returns true if this set replaces an older definition.
*/
bool NxsCharactersBlock::AddNewPartition(const std::string &label, const NxsPartition & inds)
	{
	NxsString ls(label.c_str());
	ls.ToUpper();
	bool replaced = charPartitions.count(ls) > 0;
	charPartitions[ls] = inds;
	return replaced;
	}

/*!
	Returns true if this set replaces an older definition.
*/
bool NxsCharactersBlock::AddNewCodonPosPartition(const std::string &label, const NxsPartition & inds, bool isDef)
	{
	NxsString ls(label.c_str());
	ls.ToUpper();
	bool replaced = codonPosPartitions.count(ls) > 0;
	codonPosPartitions[ls] = inds;
	if (isDef)
		defCodonPosPartitionName = ls;
	return replaced;
	}

/*!
	Returns true if this set replaces an older definition.
*/
bool NxsCharactersBlock::AddNewExSet(const std::string &label, const NxsUnsignedSet & inds)
	{
	NxsString ls(label.c_str());
	bool replaced = exSets.count(ls) > 0;
	exSets[ls] = inds;
	return replaced;
	}

/*! In v2.1 of the API, the NxsTaxaBlockAPI and NxsAssumptionsBlockAPI pointers
	are usually NULL.  These block assignments are made during the parse.
*/
NxsCharactersBlock::NxsCharactersBlock(
  NxsTaxaBlockAPI *tb,			/* the taxa block object to consult for taxon labels (can be 0L)*/
  NxsAssumptionsBlockAPI *ab)	/* the assumptions block object to consult for exclusion sets (can be 0L) */
  	:NxsTaxaBlockSurrogate(tb, NULL)
	{
	assumptionsBlock = ab;
	NCL_BLOCKTYPE_ATTR_NAME = "CHARACTERS";
	supportMixedDatatype = false;
	convertAugmentedToMixed = false;
	allowAugmentingOfSequenceSymbols = false;
	writeInterleaveLen = -1;
	Reset();
	}
/*! Excludes characters whose indices are contained in the set `exset'.
	\returns number of characters actually excluded (some may have already been excluded).
*/
unsigned NxsCharactersBlock::ApplyExset(
  NxsUnsignedSet &exset)	/* set of character indices to exclude in range [0..`nChar') */
	{
	excluded.clear();
	set_union(eliminated.begin(), eliminated.end(), exset.begin(), exset.end(), inserter(excluded, excluded.begin()));
	return (unsigned) excluded.size();
	}

/*! Includes characters whose indices are contained in the set `inset'.
	\returns number of characters that are included after the operation
*/
unsigned NxsCharactersBlock::ApplyIncludeset(
  NxsUnsignedSet &inset)	/* set of character indices to include in range [0..`nChar') */
	{
	NxsUnsignedSet inc(inset);
	inc.erase(eliminated.begin(), eliminated.end());
	excluded.erase(inc.begin(), inc.end());
	return nChar - (unsigned) excluded.size();
	}

/*! Converts a character label to a 1-offset number corresponding to the character's position based on data from
	the CharLabels NEXUS command.
	If `s' is not a valid character label, returns the value 0.
*/
unsigned NxsCharactersBlock::CharLabelToNumber(
  const std::string &inp) const	/* the character label to convert */
	{
	NxsString s(inp.c_str());
	s.ToUpper();
	std::map<std::string, unsigned>::const_iterator ltindIt = ucCharLabelToIndex.find(s);
	if (ltindIt == ucCharLabelToIndex.end())
		return 0;
	return 1 + ltindIt->second;
	}

/*!
	Transfers all data from `other' to this object, leaving `other' completely empty. Used to convert a NxsDataBlock
	object to a NxsCharactersBlock object in programs where it is desirable to just have a NxsCharactersBlock for
	storage but also allow users to enter the information in the form of the deprecated NxsDataBlock. This function
	does not make a copy of such things as the data matrix, instead just transferring the pointer to that object from
	other to this. This is whay it was named Consume rather than CopyFrom.
*/
void NxsCharactersBlock::Consume(
  NxsCharactersBlock &other)	/* NxsCharactersBlock object from which to copy */
	{
	if (assumptionsBlock)
		assumptionsBlock->SetCallback(NULL);
	assumptionsBlock = other.assumptionsBlock;
	other.assumptionsBlock = NULL;
	if (assumptionsBlock)
		assumptionsBlock->SetCallback(this);

	nChar = other.nChar;
	nTaxWithData = other.nTaxWithData;
	matchchar = other.matchchar;
	respectingCase = other.respectingCase;
	transposing = other.transposing;
	interleaving = other.interleaving;
	tokens = other.tokens;
	labels = other.labels;
	missing = other.missing;
	gap = other.gap;
	gapMode = other.gapMode;
	symbols = other.symbols;
	userEquates = other.userEquates;
	defaultEquates = other.defaultEquates;
	discreteMatrix = other.discreteMatrix;
	continuousMatrix = other.continuousMatrix;
	eliminated = other.eliminated;
	excluded = other.excluded;
	ucCharLabelToIndex = other.ucCharLabelToIndex;
	indToCharLabel = other.indToCharLabel;
	charStates = other.charStates;
	globalStateLabels = other.globalStateLabels;
	items = other.items;
	charSets = other.charSets;
	charPartitions = other.charPartitions;
	exSets = other.exSets;
	datatype = other.datatype;
	originalDatatype = other.originalDatatype;
	datatypeReadFromFormat = other.datatypeReadFromFormat;
	statesFormat = other.statesFormat;
	datatypeMapperVec = other.datatypeMapperVec;
	isEmpty = false;
	isUserSupplied = other.isUserSupplied;
	supportMixedDatatype = other.supportMixedDatatype;
	convertAugmentedToMixed = other.convertAugmentedToMixed;
	allowAugmentingOfSequenceSymbols = other.allowAugmentingOfSequenceSymbols;
	writeInterleaveLen = other.writeInterleaveLen;
	other.Reset();
	transfMgr.Reset();
	}


void NxsCharactersBlock::WriteStatesForTaxonAsNexus(
  std::ostream &out,			/* output stream on which to print matrix */
  unsigned taxNum,
  unsigned beginCharInd,
  unsigned endCharInd) const	{
  	NCL_ASSERT(endCharInd <= this->nChar);

	if (datatype == continuous)
		{
		const ContinuousCharRow & row = GetContinuousMatrixRow(taxNum);
		if (!row.empty())
			{
			NCL_ASSERT(endCharInd <= row.size());
			for (unsigned charInd = beginCharInd; charInd < endCharInd; ++charInd)
				{
				out << ' ';
				ShowStateLabels(out, taxNum, charInd, UINT_MAX);
				}
			}
		}
	else
		{
		const NxsDiscreteStateRow & row = GetDiscreteMatrixRow(taxNum);
		const unsigned rs = (const unsigned)row.size();
		NCL_ASSERT(endCharInd <= rs);
		if (rs > 0)
			{
			if (this->datatype == NxsCharactersBlock::codon)
				{
				for (unsigned charInd = beginCharInd; charInd < endCharInd; ++charInd)
					{
					NxsDiscreteStateCell sc = row[charInd];
					if (sc == NXS_GAP_STATE_CODE)
						out << gap << gap << gap;
					else if (sc >= 0 && sc < (NxsDiscreteStateCell) globalStateLabels.size())
						out << globalStateLabels[sc];
					else
						out << missing << missing << missing;
					}
				}
			else
				{
				const NxsDiscreteDatatypeMapper * dm = GetDatatypeMapperForChar(0);
				if (dm == NULL)
					throw NxsNCLAPIException("No DatatypeMapper in WriteStatesForTaxonAsNexus");
				if (IsMixedType())
					{

					for (unsigned charInd = beginCharInd; charInd < endCharInd; ++charInd)
						{
						dm = GetDatatypeMapperForChar(charInd);
						if (dm == NULL)
							{
							errormsg = "No DatatypeMapper for character ";
							errormsg << charInd + 1 << " in WriteStatesForTaxonAsNexus";
							throw NxsNCLAPIException(errormsg);
							}
						const NxsDiscreteStateCell c = row.at(charInd);
						dm->WriteStateCodeAsNexusString(out, c);
						}
					}
				else
					{
					if (tokens)
						{
						for (unsigned charInd = beginCharInd; charInd < endCharInd; ++charInd)
							{
							NxsDiscreteStateCell sc = row[charInd];
							out << ' ';
							if (sc == NXS_GAP_STATE_CODE)
								out << gap;
							else
								{
								NxsString sl = GetStateLabel(charInd, sc); /*v2.1to2.2 4 */
								if (sl == " ")
									{
									errormsg = "Writing character state ";
									errormsg << 1 + sc << " for character " << 1+charInd << ", but no appropriate chararcter label or symbol was found.";
									throw NxsNCLAPIException(errormsg);
									}
								else
									out  << NxsString::GetEscaped(sl);
								}
							}
						}
					else
						{
						std::vector<NxsDiscreteStateCell>::const_iterator endIt = row.begin() + beginCharInd;
						std::vector<NxsDiscreteStateCell>::const_iterator begIt = endIt;
						if (endCharInd == row.size())
							endIt = row.end();
						else
							endIt += endCharInd - beginCharInd;
						dm->WriteStateCodeRowAsNexus(out, begIt, endIt);
						}
					}
				}
			}
		}
	}


/*!
	Provides a dump of the contents of the `matrix' variable. Useful for testing whether data is being read as
	expected. If marginText is NULL, matrix output is placed flush left. If each line of output should be prefaced with
	a tab character, specify "\t" for `marginText'.
*/
void NxsCharactersBlock::DebugShowMatrix(
  std::ostream &out,			/* output stream on which to print matrix */
  bool ,	/* deprecated, matchchar no longer used for output */
  const char *marginText) const /* for printing first on each line */
	{
	if (!taxa)
		return;
	const unsigned width = taxa->GetMaxTaxonLabelLength();
	const unsigned ntt = GetNTaxTotal();
	for (unsigned i = 0; i < ntt; i++)
		{
		bool skip = true;
		if (datatype == continuous)
			{
			const ContinuousCharRow & row = GetContinuousMatrixRow(i);
			skip = row.empty();
			}
		else
			{
			const NxsDiscreteStateRow & row = GetDiscreteMatrixRow(i);
			skip = row.empty();
			}
		if (!skip)
			{
			if (marginText != NULL)
				out << marginText;
			const NxsString currTaxonLabel = taxa->GetTaxonLabel(i); /*v2.1to2.2 4 */
			out << currTaxonLabel;
			unsigned currTaxonLabelLen = (unsigned)currTaxonLabel.size();
			unsigned diff = width - currTaxonLabelLen;
			std::string spacer(diff+5, ' ');
			out << spacer;
			WriteStatesForTaxonAsNexus(out, i, 0, nChar);
			out << endl;
			}
		}
	}

unsigned NxsCharactersBlock::GetMaxObsNumStates(bool countMissingStates, bool onlyActiveChars) NCL_COULD_BE_CONST /*v2.1to2.2 1 */
	{
	unsigned maxN = 1;
	for (unsigned j = 0; j < nChar; j++)
		{
		if (!onlyActiveChars || IsActiveChar(j))
			maxN = std::max(maxN, GetObsNumStates(j, countMissingStates));
		}
	return maxN;
	}

/*!
	Performs a count of the number of active characters.
*/
unsigned NxsCharactersBlock::GetNumActiveChar() NCL_COULD_BE_CONST /*v2.1to2.2 1 */
	{
	unsigned num_active_char = 0;
	for (unsigned i = 0; i < nChar; i++)
		{
		if (IsActiveChar(i))
			num_active_char++;
		}
	return num_active_char;
	}



/* Returns label for character state `j' at character `i', if a label has been specified. If no label was specified,
	returns string containing a single blank (i.e., " ").
*/
NxsString NxsCharactersBlock::GetStateLabelImpl( /*v2.1to2.2 4 */
  unsigned i,	/* the locus in range [0..`nChar') */
  unsigned j) const	/* the 0-offset index of the state of interest */
	{
	NxsString s = " ";
	NxsStringVectorMap::const_iterator cib = charStates.find(i);
	if (cib != charStates.end() && j < static_cast<unsigned>(cib->second.size()))
		return cib->second[j];
	if (!globalStateLabels.empty() && (j < globalStateLabels.size()))
		return globalStateLabels[j];
	return s;
	}


/*!
	Returns true if `ch' can be found in the `symbols' array. The value of `respectingCase' is used to determine
	whether or not the search should be case sensitive. Assumes `symbols' is non-NULL.
*/
bool NxsCharactersBlock::IsInSymbols(
  char ch) NCL_COULD_BE_CONST /* the symbol character to search for */ /*v2.1to2.2 1 */
	{
	char char_in_question = (respectingCase ? ch : (char)toupper(ch));
	for (std::string::const_iterator sIt = symbols.begin(); sIt != symbols.end(); ++sIt)
		{
		const char char_in_symbols = (respectingCase ? *sIt : (char)toupper(*sIt));
		if (char_in_symbols == char_in_question)
			return true;
		}
	return false;
	}

/*!
	Called when CHARLABELS command needs to be parsed from within the DIMENSIONS block. Deals with everything after
	the token CHARLABELS up to and including the semicolon that terminates the CHARLABELS command. If an ELIMINATE
	command has been processed, labels for eliminated characters will not be stored.
*/
void NxsCharactersBlock::HandleCharlabels(
  NxsToken &token)	/* the token used to read from `in' */
	{
	ucCharLabelToIndex.clear();
	indToCharLabel.clear();
	unsigned ind = 0;
	for (;;)
		{
		token.GetNextToken();
		if (token.Equals(";"))
			break;
		else
			{
			if (ind >= nChar)
				GenerateNxsException(token, "Number of character labels exceeds NCHAR specified in DIMENSIONS command");
			NxsString t = token.GetToken();
			if (t != " ")
				{
				indToCharLabel[ind] = t;
				t.ToUpper();
				ucCharLabelToIndex[t] = ind;
				}
			ind++;
			}
		}
	}

/*!
	Called when CHARSTATELABELS command needs to be parsed from within the CHARACTERS block. Deals with everything
	after the token CHARSTATELABELS up to and including the semicolon that terminates the CHARSTATELABELS command.
	CharLabels data structures  will store labels only for characters that have not been eliminated, and likewise for
	`charStates'. Specifically, `charStates[0]' refers to the vector of character state labels for the first
	non-eliminated character.
*/
void NxsCharactersBlock::HandleCharstatelabels(
  NxsToken &token)	/* the token used to read from `in' */
	{
	unsigned currChar = 0;
	bool semicolonFoundInInnerLoop = false;
	bool tokenAlreadyRead = false;
	bool save = true;

	charStates.clear();
	ucCharLabelToIndex.clear();
	indToCharLabel.clear();

	for (;;)
		{
		save = true;

		if (semicolonFoundInInnerLoop)
			break;

		if (tokenAlreadyRead)
			tokenAlreadyRead = false;
		else
			token.GetNextToken();

		if (token.Equals(";"))
			break;

		// Token should be the character number; create a new association
		//
		int sn = -1;
        try {
            sn = token.GetToken().ConvertToInt();
            }
        catch (NxsString::NxsX_NotANumber &x)
            {
            }
		unsigned n = (unsigned)sn;
		if (sn < 1 || n > nChar || n <= currChar)
			{
			errormsg = "Invalid character number (";
			errormsg += token.GetToken();
			errormsg += ") found in CHARSTATELABELS command (either out of range or not interpretable as an integer)";
			throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
			}

		currChar = n;

		token.GetNextToken();

		// Token should be the character label or / if there is no label
		//	' ' is a placeholder for no label.
		//
		if (save)
			{
			NxsString t = token.GetToken();
			if (t != " " && !token.Equals("/"))
				{
				indToCharLabel[currChar - 1] = t;
				t.ToUpper();
				ucCharLabelToIndex[t] = currChar - 1;
				}
			}
		if (!token.Equals("/"))
			token.GetNextToken();

		// Token should be a slash character if state labels were provided for this character; otherwise,
		// token should be one of the following:
		// 1) the comma separating information for different characters, in which case we read in the
		//	  next token (which should be the next character number)
		// 2) the semicolon indicating the end of the command
		//
		if (!token.Equals("/"))
			{
			if (!token.Equals(",") && !token.Equals(";"))
				{
				errormsg = "Expecting a comma or semicolon here, but found \"";
				errormsg += token.GetToken();
				errormsg += "\" instead";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}
			if (token.Equals(","))
				token.GetNextToken();
			tokenAlreadyRead = true;
			continue;
			}

		// Now create a new association for the character states list

		for (;;)
			{
			token.GetNextToken();

			if (token.Equals(";"))
				{
				semicolonFoundInInnerLoop = true;
				break;
				}

			if (token.Equals(","))
				break;

			if (save)
				{
				if (datatype == continuous)
					GenerateNxsException(token, "State Labels cannot be specified when the datatype is continuous");

				// Token should be a character state label; add it to the list
				NxsString cslabel = token.GetToken();
				charStates[n - 1].push_back(cslabel);
				}

			} // inner for (;;) loop (grabbing state labels for character n)
		} // outer for (;;) loop
	}

/*!
	Called when DIMENSIONS command needs to be parsed from within the CHARACTERS block. Deals with everything after
	the token DIMENSIONS up to and including the semicolon that terminates the DIMENSIONs command. `newtaxaLabel',
	`ntaxLabel' and `ncharLabel' are simply "NEWTAXA", "NTAX" and "NCHAR" for this class, but may be different for
	derived classes that use `newtaxa', `ntax' and `nChar' for other things (e.g., ntax is number of populations in
	an ALLELES block)
*/
void NxsCharactersBlock::HandleDimensions(
  NxsToken &token,			/* the token used to read from `in' */
  NxsString newtaxaLabel,	/* the label used in data file for `newtaxa' */
  NxsString ntaxLabel,		/* the label used in data file for `ntax' */
  NxsString ncharLabel)		/* the label used in data file for `nChar' */
	{
	nChar = 0;
	unsigned ntaxRead = 0;
	for (;;)
		{
		token.GetNextToken();
		if (token.Equals(newtaxaLabel))
			newtaxa = true;
		else if (token.Equals(ntaxLabel))
			{
			DemandEquals(token, "after NTAX in DIMENSIONS command");
			ntaxRead = DemandPositiveInt(token, ntaxLabel.c_str());
			}
		else if (token.Equals(ncharLabel))
			{
			DemandEquals(token, "in DIMENSIONS command");
			nChar = DemandPositiveInt(token, ncharLabel.c_str());
			}
		else if (token.Equals(";"))
			break;
		}

	if (nChar == 0)
		{
		errormsg = "DIMENSIONS command must have an NCHAR subcommand .";
		throw NxsException(errormsg, token);
		}
	if (newtaxa)
		{
		if (ntaxRead == 0)
			{
			errormsg = "DIMENSIONS command must have an NTAX subcommand when the NEWTAXA option is in effect.";
			throw NxsException(errormsg, token);
			}
		AssureTaxaBlock(createImpliedBlock, token, "Dimensions");
		if (!createImpliedBlock)
			{
			taxa->Reset();
			if (nexusReader)
				nexusReader->RemoveBlockFromUsedBlockList(taxa);
			}
		taxa->SetNtax(ntaxRead);
		nTaxWithData = ntaxRead;
		}
	else
		{
		AssureTaxaBlock(false, token, "Dimensions");
		const unsigned ntaxinblock = taxa->GetNTax();
		if (ntaxinblock == 0)
			{
			errormsg = "A TAXA block must be read before character data, or the DIMENSIONS command must use the NEWTAXA.";
			throw NxsException(errormsg, token);
			}

		if (ntaxinblock < ntaxRead)
			{
			errormsg = ntaxLabel;
			errormsg += " in ";
			errormsg += NCL_BLOCKTYPE_ATTR_NAME;
			errormsg += " block must be less than or equal to NTAX in TAXA block\nNote: one circumstance that can cause this error is \nforgetting to specify ";
			errormsg += ntaxLabel;
			errormsg += " in DIMENSIONS command when \na TAXA block has not been provided";
			throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
			}
		nTaxWithData = (ntaxRead == 0 ? ntaxinblock : ntaxRead);
		}
	}

/*!
	Called when ELIMINATE command needs to be parsed from within the CHARACTERS block. Deals with everything after the
	token ELIMINATE up to and including the semicolon that terminates the ELIMINATE command. Any character numbers
	or ranges of character numbers specified are stored in the NxsUnsignedSet `eliminated', which remains empty until
	an ELIMINATE command is processed. Note that like all sets the character ranges are adjusted so that their offset
	is 0. For example, given "eliminate 4-7;" in the data file, the eliminate array would contain the values 3, 4, 5
	and 6 (not 4, 5, 6 and 7). It is assumed that the ELIMINATE command comes before character labels and/or character
	state labels have been specified; an error message is generated if the user attempts to use ELIMINATE after a
	CHARLABELS, CHARSTATELABELS, or STATELABELS command.
*/
void NxsCharactersBlock::HandleEliminate(
  NxsToken &token)	/* the token used to read from `in' */
	{
	if (!eliminated.empty() && nexusReader)
		nexusReader->NexusWarnToken("Only one ELIMINATE command should be used in a CHARACTERS or DATA block (it must appear before the MATRIX command).\n   New character eliminations will be added to the previous eliminated characters (the previously eliminated characters will continue to be excluded).", NxsReader::UNCOMMON_SYNTAX_WARNING, token);
	token.GetNextToken();
	NxsSetReader::ReadSetDefinition(token, *this, "Character", "Eliminate", &eliminated);
	NCL_ASSERT(eliminated.size() <= nChar);
	for (NxsUnsignedSet::const_iterator elIt = eliminated.begin(); elIt != eliminated.end(); ++elIt)
		excluded.insert(*elIt);
	}



/*!
	Called from HandleMatrix function to read in a standard (i.e., non-transposed) matrix. Interleaving, if
	applicable, is dealt with herein.
*/
void NxsCharactersBlock::HandleStdMatrix(
  NxsToken &token)	/* the token used to read from `in' */
	{
	NCL_ASSERT(taxa != NULL);
	unsigned indOfTaxInCommand;
	unsigned indOfTaxInMemory;
	unsigned currChar = 0;
	unsigned firstChar = 0;
	unsigned lastChar = nChar;
	unsigned nextFirst = 0;
	unsigned page = 0;
	const bool continuousData =  (datatype == NxsCharactersBlock::continuous);
	const unsigned ntlabels = taxa->GetNumTaxonLabels();
	errormsg.clear();
	bool taxaBlockNeedsLabels = (ntlabels == 0);
	if (!taxaBlockNeedsLabels && ntlabels < nTaxWithData)
		{
		errormsg << "Not enough taxlabels are known to read characters for " << nTaxWithData << " taxa in the Matrix command.";
		throw NxsException(errormsg, token);
		}
	ContinuousCharRow emptyContRow;
	NxsDiscreteStateRow emptyDiscRow;
	ContinuousCharRow *contRowPtr = NULL;
	NxsDiscreteStateRow *discRowPtr = NULL;
	ContinuousCharRow *ftContRowPtr = NULL;
	NxsDiscreteStateRow *ftDiscRowPtr = NULL;
	const bool isContinuous = (datatype == NxsCharactersBlock::continuous);
	if (isContinuous)
		emptyContRow.resize(nChar);
	else
		emptyDiscRow.assign(nChar, NXS_INVALID_STATE_CODE);
	std::vector<unsigned> toInMem(nTaxWithData, UINT_MAX);
	std::vector<unsigned> nCharsRead(nTaxWithData, 0);

	unsigned numSigInts = NxsReader::getNumSignalIntsCaught();
	const bool checkingSignals = NxsReader::getNCLCatchesSignals();
	const unsigned MAX_NUM_CHARS_BETWEEN_SIGNAL_CHECKS = 1000;
	for (; currChar < nChar; page++)
		{
		for (indOfTaxInCommand = 0; indOfTaxInCommand < nTaxWithData ; indOfTaxInCommand++)
			{
			unsigned numCharsSinceLastSignalCheck = 0;
			if (checkingSignals && NxsReader::getNumSignalIntsCaught() != numSigInts)
				{
				if (datatype == NxsCharactersBlock::continuous)
					continuousMatrix.clear();
				else
					discreteMatrix.clear();
				throw NxsSignalCanceledParseException("Reading Characters Block");
				}
			NxsString nameStr;
			if (labels)
				{
				token.GetNextToken();
				nameStr = token.GetToken();
				if (taxaBlockNeedsLabels)
					{
					if (taxa->IsAlreadyDefined(nameStr))
						{
						errormsg << "Data for this taxon (" << nameStr << ") has already been saved";
						throw NxsException(errormsg, token);
						}
					try {
						indOfTaxInMemory = taxa->AddTaxonLabel(nameStr);
						}
					catch (NxsException &x)
						{
						if (nameStr == ";")
							{
							errormsg << "Unexpected ; after only " << indOfTaxInCommand << " taxa were read (expecting characters for " << nTaxWithData << " taxa).";
							throw NxsException(errormsg, token);
							}
						x.addPositionInfo(token);
						throw x;
						}
					}
				else
					{
					unsigned numOfTaxInMemory = taxa->TaxLabelToNumber(nameStr);
					if (numOfTaxInMemory == 0)
						{
						if (token.Equals(";"))
							{
							if (currChar != nChar)
								errormsg << "Unexpected ; (after only " << currChar << " characters were read)";
							else
								errormsg << "Unexpected ; (after characters were read for only " << indOfTaxInCommand << "out of " << nTaxWithData << " taxa)";
							}
						else
							errormsg << "Could not find taxon named \"" << nameStr << "\" among stored taxon labels";
						if (currChar > 0)
							errormsg << "\n   Expecting data for taxon \"" << taxa->GetTaxonLabel(toInMem[indOfTaxInCommand]) << "\"";
						throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
						}
					indOfTaxInMemory = numOfTaxInMemory - 1;
					}
				}
			else
				{
				indOfTaxInMemory = indOfTaxInCommand;
				nameStr << (indOfTaxInMemory + 1);
				}
			if (page == 0)
				{
				if (isContinuous)
					{
					NCL_ASSERT(indOfTaxInMemory < continuousMatrix.size());
					continuousMatrix[indOfTaxInMemory] = emptyContRow;
					}
				else
					{
					NCL_ASSERT(indOfTaxInMemory < discreteMatrix.size());
					discreteMatrix[indOfTaxInMemory] = emptyDiscRow;
					}
				if (toInMem[indOfTaxInCommand] != UINT_MAX)
					{
					errormsg << "Characters for taxon \"" << nameStr << "\" (number " << indOfTaxInMemory + 1 << "and \"" << taxa->GetTaxonLabel(indOfTaxInMemory) << "\" according to the taxa block) have already been stored";
					throw NxsException(errormsg, token);
					}
				toInMem[indOfTaxInCommand] = indOfTaxInMemory;
				}
			else
				{
				if (toInMem[indOfTaxInCommand] != indOfTaxInMemory)
					{
					errormsg << "Ordering of taxa must be identical to that in first interleave page. Taxon \"" << nameStr << "\" was not expected.";
					throw NxsException(errormsg, token);
					}
				}

			if (firstChar > 0 && nCharsRead[indOfTaxInCommand] >= firstChar)
				{
				errormsg << "Data for this taxon (" << nameStr << ") have already been saved";
				throw NxsException(errormsg, token);
				}
			if (isContinuous)
				{
				contRowPtr = &continuousMatrix[indOfTaxInMemory];
				if (ftDiscRowPtr == NULL)
					ftContRowPtr = contRowPtr;
				}
			else
				{
				discRowPtr = &discreteMatrix[indOfTaxInMemory];
				if (ftDiscRowPtr == NULL)
					ftDiscRowPtr = discRowPtr;
				}

			//******************************************************
			//******** Beginning of loop through characters ********
			//******************************************************
			bool atEOL = false;
			for (currChar = firstChar; currChar < lastChar; currChar++)
				{
				if (checkingSignals)
					{
					if (numCharsSinceLastSignalCheck >= MAX_NUM_CHARS_BETWEEN_SIGNAL_CHECKS)
						{
						if (NxsReader::getNumSignalIntsCaught() != numSigInts)
							{
							if (datatype == NxsCharactersBlock::continuous)
								continuousMatrix.clear();
							else
								discreteMatrix.clear();
							throw NxsSignalCanceledParseException("Reading Characters Block");
							}
						numCharsSinceLastSignalCheck = 0;
						}
					else
						numCharsSinceLastSignalCheck++;
					}

				NxsDiscreteDatatypeMapper * currMapper =  GetMutableDatatypeMapperForChar(currChar);
				// atEOL will be false only if a newline character is encountered before character j processed
				if (continuousData)
					atEOL = HandleNextContinuousState(token, indOfTaxInMemory, currChar, *contRowPtr, nameStr);
				else
					{
					NCL_ASSERT(currMapper);
					if (tokens)
						atEOL = HandleNextTokenState(token, indOfTaxInMemory, currChar, *discRowPtr, *currMapper, ftDiscRowPtr, nameStr);
					else
						atEOL = HandleNextDiscreteState(token, indOfTaxInMemory, currChar, *discRowPtr, *currMapper, ftDiscRowPtr, nameStr);
					}
				if (interleaving && !atEOL)
					{
					if (lastChar < nChar && currChar != lastChar)
						{
						errormsg << "Each line within an interleave page must comprise the same number of characters.  Error reading taxon \"" << nameStr << '\"';
						throw NxsException(errormsg, token);
						}

					// currChar should be firstChar in next go around
					nextFirst = currChar;

					// Set lastChar to currChar so that we can check to make sure the remaining lines
					// in this interleave page end at the same place
					lastChar = currChar;
					}
				}
			if (lastChar > 0)
				nCharsRead[indOfTaxInCommand] = lastChar - 1;
			if (lastChar < nChar && indOfTaxInCommand > 0)
				{
				token.SetLabileFlagBit(NxsToken::newlineIsToken);
				token.GetNextToken();
				if (!token.AtEOL())
					{
					errormsg << "Each line within an interleave page must comprise the same number of characters\n. Expecting the end of a line, but found " << token.GetToken() << " when reading data for taxon \"" << nameStr << '\"';
					throw NxsException(errormsg, token);
					}
				}
			else
				{
				const char nextch = token.PeekAtNextChar();
				if (indOfTaxInCommand > 0 && (!atEOL) && (strchr(";[\n\r \t", nextch) == NULL) && nexusReader)
					{
					errormsg << "Expecting a whitespace character at the end of the characters for taxon \""<< nameStr << "\" but found " << nextch;
					nexusReader->NexusWarnToken(errormsg, NxsReader::UNCOMMON_SYNTAX_WARNING, token);
					errormsg.clear();
					}
				}
			}
		firstChar = nextFirst;
		lastChar = nChar;
		taxaBlockNeedsLabels = false; /* taxaBlockNeedsLabels can only be true on the first page */
		}
	}

/*!
	Called from HandleMatrix function to read in a transposed matrix. Interleaving, if applicable, is dealt with herein.
*/
void NxsCharactersBlock::HandleTransposedMatrix(
  NxsToken &token)	/* the token used to read from in */
	{
	NCL_ASSERT(taxa);
	unsigned currTaxon = 0;
	unsigned firstTaxon = 0;
	unsigned lastTaxon = nTaxWithData;
	unsigned nextFirst = 0;
	unsigned page = 0;
	const bool continuousData =  (datatype == NxsCharactersBlock::continuous);
	unsigned indOfCharInCommand, indOfCharInMemory;
	const bool isContinuous = (datatype == NxsCharactersBlock::continuous);

	if (isContinuous)
		{
		ContinuousCharRow emptyContRow(nChar);
		for (unsigned i = 0; i < nTaxWithData; ++ i)
			continuousMatrix[i] = emptyContRow;
		}
	else
		{
		NxsDiscreteStateRow emptyDiscRow(nChar, NXS_INVALID_STATE_CODE);
		for (unsigned i = 0; i < nTaxWithData; ++ i)
			discreteMatrix[i] = emptyDiscRow;
		}
	vector<unsigned> toInMem(nChar, UINT_MAX);
	vector<unsigned> nTaxRead(nChar, 0);
	bool needsCharLabels = indToCharLabel.empty();
	for (;;	page++)
		{
		for (indOfCharInCommand = 0; indOfCharInCommand < nChar; indOfCharInCommand++)
			{
			NxsString rawToken;
			if (labels)
				{
				token.GetNextToken();
				if (needsCharLabels)
					{
					rawToken = token.GetToken();
					NxsString s = rawToken;
					s.ToUpper();
					if (ucCharLabelToIndex.count(s) > 0)
						{
						errormsg << "Data for this character (" << token.GetToken() << ") has already been saved";
						throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
						}
					ucCharLabelToIndex[s] = indOfCharInCommand;
					indToCharLabel[indOfCharInCommand] = rawToken;
					indOfCharInMemory = indOfCharInCommand;
					}
				else // either not first interleaved page or character labels not previously defined
					{
					rawToken = token.GetToken();
					NxsString s = rawToken;
					s.ToUpper();
					LabelToIndexMap::const_iterator iter = ucCharLabelToIndex.find(s);
					if (iter == ucCharLabelToIndex.end())
						{
						errormsg << "Could not find character named " << token.GetToken() <<  " among stored character labels";
						throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
						}
					indOfCharInMemory = iter->second;
					}
				}
			else
				indOfCharInMemory = indOfCharInCommand;

			if (page == 0)
				{
				if (toInMem[indOfCharInCommand] != UINT_MAX)
					{
					errormsg << "States for character " << indOfCharInCommand;
					if (!rawToken.empty())
						errormsg << " (" << rawToken << ") ";
					errormsg << "have already been stored";
					throw NxsException(errormsg, token);
					}
				toInMem[indOfCharInCommand] = indOfCharInMemory;
				}
			else
				{
				if (toInMem[indOfCharInCommand] != indOfCharInMemory)
					{
					errormsg << "The order of characters must be in the same order in each page of the interleaved matrix. Character " << rawToken << " was unexpected.";
					throw NxsException(errormsg, token);
					}
				}
			if (firstTaxon > 0 && nTaxRead[indOfCharInCommand] >= firstTaxon)
				{
				errormsg << "Data for this character ";
				if (!rawToken.empty())
					errormsg << '(' << rawToken << ") ";
				errormsg << "has already been saved";
				throw NxsException(errormsg, token);
				}

			NxsDiscreteDatatypeMapper * currMapper =  GetMutableDatatypeMapperForChar(indOfCharInMemory);

			for (currTaxon = firstTaxon; currTaxon < lastTaxon; currTaxon++)
				{
				bool atEOL = false;
				NxsString nameStr;
				nameStr << 1+currTaxon;
				if (continuousData)
					{
					ContinuousCharRow *contRowPtr = &continuousMatrix[currTaxon];
					atEOL = HandleNextContinuousState(token, currTaxon, indOfCharInMemory, *contRowPtr, nameStr);
					}
				else
					{
					NxsDiscreteStateRow *discRowPtr = &discreteMatrix[currTaxon];
					if (tokens)
						atEOL = HandleNextTokenState(token,  currTaxon, indOfCharInMemory, *discRowPtr, *currMapper, NULL, nameStr);
					else
						atEOL = HandleNextDiscreteState(token, currTaxon, indOfCharInMemory, *discRowPtr, *currMapper, NULL, nameStr);
					}
				if (interleaving && !atEOL)
					{
					if (lastTaxon < nTaxWithData && currTaxon != lastTaxon)
						GenerateNxsException(token, "Each line within an interleave page must comprise the same number of taxa");

					// currTaxon should be firstChar in next go around
					nextFirst = currTaxon;

					// Set lastTaxon to currTaxon so that we can check to make sure the
					// remaining lines in this interleave page end at the same place
					lastTaxon = currTaxon;
					}
				}
			if (currTaxon > 0)
				nTaxRead[indOfCharInCommand] = currTaxon - 1;
			if (lastTaxon < nTaxWithData && indOfCharInCommand > 0)
				{
				token.SetLabileFlagBit(NxsToken::newlineIsToken);
				token.GetNextToken();
				if (!token.AtEOL())
					{
					errormsg = "Each line within an interleave page must comprise the same number of taxa\n.";
					errormsg << "Expecting the end of a line, but found " << token.GetToken();
					throw NxsException(errormsg, token);
					}
				}
			}
		firstTaxon = nextFirst;
		lastTaxon = nTaxWithData;
		if (currTaxon == nTaxWithData)
			break;
		needsCharLabels = false;
		}
	}

/*!
	Called when MATRIX command needs to be parsed from within the CHARACTERS block. Deals with everything after the
	token MATRIX up to and including the semicolon that terminates the MATRIX command.
*/
void NxsCharactersBlock::HandleMatrix(
  NxsToken &token)	/* the token used to read from `in' */
	{
	const NxsPartition dtParts;
	const std::vector<DataTypesEnum> dtv;
	if (datatypeMapperVec.empty())
		CreateDatatypeMapperObjects(dtParts, dtv);
	if (taxa == NULL)
		AssureTaxaBlock(false, token, "Matrix");

	if (tokens && GetDataType() == standard)
		{
		/* we can run into trouble here because the number of states can be larger than the
		symbols list in the NxsDiscreteDatatypeMapper object (because CharState labels can be
		used in a matrix, and symbols don't have to be introduced for each character).

		We deal with that here, by introducing \0 symbols
		*/
		const unsigned nStatesWSymbols = (const unsigned)symbols.length();
		unsigned nStatesTotal = nStatesWSymbols;
		for (NxsStringVectorMap::const_iterator cib = this->charStates.begin(); cib != this->charStates.end(); ++cib)
			{
			const NxsStringVector & stateLabelsVec = cib->second;
			const unsigned ns = (unsigned)stateLabelsVec.size();
			if (ns > nStatesTotal)
				nStatesTotal = ns;
			}
		if (nStatesTotal > nStatesWSymbols)
			{
			symbols.append(nStatesTotal-nStatesWSymbols, '\0');
			CreateDatatypeMapperObjects(dtParts, dtv);
			}
		}
	const unsigned ntax = taxa->GetNTax();
	if (ntax == 0)
		{
		errormsg = "Must precede ";
		errormsg << NCL_BLOCKTYPE_ATTR_NAME << " block with a TAXA block or specify NEWTAXA and NTAX in the DIMENSIONS command";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}

	discreteMatrix.clear();
	continuousMatrix.clear();

	if (datatype == NxsCharactersBlock::continuous)
		{
		continuousMatrix.clear();
		continuousMatrix.resize(ntax);
		}
	else
		{
		discreteMatrix.clear();
		discreteMatrix.resize(ntax);
		}
	if (IsMixedType())
		{
		if (transposing)
			throw NxsUnimplementedException("Reading of transposed, mixed datatype matrices will probably never be supported by NCL");
		/*	HandleMixedDatatypeMatrix(token); */
		}
	if (transposing)
		HandleTransposedMatrix(token);
	else
		HandleStdMatrix(token);
	DemandEndSemicolon(token, "MATRIX");
	if (assumptionsBlock)
		assumptionsBlock->SetCallback(this);
	if (convertAugmentedToMixed)
		AugmentedSymbolsToMixed();
	}

/*!
	Called when STATELABELS command needs to be parsed from within the DIMENSIONS block. Deals with everything after
	the token STATELABELS up to and including the semicolon that terminates the STATELABELS command. Note that the
	numbers of states are shifted back one before being stored so that the character numbers in the NxsStringVectorMap
	objects are 0-offset rather than being 1-offset as in the NxsReader data file.
*/
void NxsCharactersBlock::HandleStatelabels(
  NxsToken &token)	/* the token used to read from `in' */
	{
	if (datatype == continuous)
		GenerateNxsException(token, "STATELABELS cannot be specified when the datatype is continuous");
	charStates.clear();
	for (;;)
		{
		token.GetNextToken();
		if (token.Equals(";"))
			break;
        int n = -1;
        try {
            n = token.GetToken().ConvertToInt();
            }
        catch (NxsString::NxsX_NotANumber &x)
            {
            }
		if (n < 1 || n > (int)nChar)
			{
			errormsg = "Invalid character number (";
			errormsg << token.GetToken() << ") found in STATELABELS command (either out of range or not interpretable as an integer)";
			throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
			}
		NxsStringVector & v = charStates[n - 1];
		for (;;)
			{
			token.GetNextToken();
			if (token.Equals(";") || token.Equals(","))
				break;
			v.push_back(token.GetToken());
			}
		}
	}

/*!
	This function provides the ability to read everything following the block name (which is read by the NxsReader
	object) to the END or ENDBLOCK statement. Characters are read from the input stream `in'. Overrides the abstract
	virtual function in the base class.
*/
void NxsCharactersBlock::Read(
  NxsToken &token)	/* the token used to read from `in' */
	{
	isEmpty = false;
	isUserSupplied = true;

	NxsString s;
	s = "BEGIN ";
	s += NCL_BLOCKTYPE_ATTR_NAME;
	DemandEndSemicolon(token, s.c_str());
	nTaxWithData = 0;

	for (;;)
		{
		token.GetNextToken();
		NxsBlock::NxsCommandResult res = HandleBasicBlockCommands(token);
		if (res == NxsBlock::NxsCommandResult(STOP_PARSING_BLOCK))
			{
			if (discreteMatrix.empty() && continuousMatrix.empty())
				{
				errormsg.clear();
				errormsg << "\nA " << NCL_BLOCKTYPE_ATTR_NAME << " block must contain a Matrix command";
				throw NxsException(errormsg, token);
				}
			return;
			}
		if (res != NxsBlock::NxsCommandResult(HANDLED_COMMAND))
			{
			if (token.Equals("DIMENSIONS"))
				HandleDimensions(token, "NEWTAXA", "NTAX", "NCHAR");
			else if (token.Equals("FORMAT"))
				HandleFormat(token);
			else if (token.Equals("ELIMINATE"))
				HandleEliminate(token);
			else if (token.Equals("TAXLABELS"))
				HandleTaxLabels(token);
			else if (token.Equals("CHARSTATELABELS"))
				HandleCharstatelabels(token);
			else if (token.Equals("CHARLABELS"))
				HandleCharlabels(token);
			else if (token.Equals("STATELABELS"))
				HandleStatelabels(token);
			else if (token.Equals("MATRIX"))
				HandleMatrix(token);
			else
				SkipCommand(token);
			}
		}
	}

/*!
	This function outputs a brief report of the contents of this CHARACTERS block. Overrides the abstract virtual
	function in the base class.
*/
void NxsCharactersBlock::Report(
  std::ostream &out) NCL_COULD_BE_CONST  /* the output stream to which to write the report */ /*v2.1to2.2 1 */
	{
	out << '\n' << NCL_BLOCKTYPE_ATTR_NAME << " block contains ";
	if (nTaxWithData == 0)
		out << "no taxa";
	else if (nTaxWithData == 1)
		out << "one taxon";
	else
		out << nTaxWithData << " taxa";
	out << " and ";
	if (nChar == 0)
		out << "no characters";
	else if (nChar == 1)
		out << "one character";
	else
		out << nChar << " characters";
	out << endl;

	out << "  Data type is \"" << this->GetDatatypeName() << "\"" << endl;

	if (respectingCase)
		out << "  Respecting case" << endl;
	else
		out << "  Ignoring case" << endl;

	if (tokens)
		out << "  Multicharacter tokens allowed in data matrix" << endl;
	else
		out << "  Data matrix entries are expected to be single symbols" << endl;

	if (labels && transposing)
		out << "  Character labels are expected on left side of matrix" << endl;
	else if (labels && !transposing)
		out << "  Taxon labels are expected on left side of matrix" << endl;
	else
		out << "  No labels are expected on left side of matrix" << endl;

	if (!indToCharLabel.empty())
		{
		out << "  Character and character state labels:" << endl;
		for (unsigned k = 0; k < nChar; k++)
			{
			const std::map<unsigned, std::string>::const_iterator toLit = indToCharLabel.find(k);
			const unsigned kNum = 1 + k;
			if (toLit == indToCharLabel.end())
				out << "    " << kNum << "    (no label provided for this character)" << endl;
			else
				out << "    " << kNum << "    " << toLit->second << endl;

			// Output state labels if any are defined for this character
			//
			NxsStringVectorMap::const_iterator cib = charStates.find(k);
			if (cib != charStates.end())
				{
				int ns = (int)cib->second.size();
				for (int m = 0; m < ns; m++)
					out << "        " << cib->second[m] << endl;
				}
			}
		}

	if (transposing && interleaving)
		out << "  Matrix transposed and interleaved" << endl;
	else if (transposing && !interleaving)
		out << "  Matrix transposed but not interleaved" << endl;
	else if (!transposing && interleaving)
		out << "  Matrix interleaved but not transposed" << endl;
	else
		out << "  Matrix neither transposed nor interleaved" << endl;

	out << "  Missing data symbol is '" << missing << '\'' << endl;

	if (matchchar != '\0')
		out << "  Match character is '" << matchchar << '\'' << endl;
	else
		out << "  No match character specified" << endl;

	if (gap != '\0')
		out << "  Gap character specified is '" << gap << '\'' << endl;
	else
		out << "  No gap character specified" << endl;

	out << "  Valid symbols are: " << symbols << endl;

	int numEquateMacros = (int)(userEquates.size() + defaultEquates.size());
	if (numEquateMacros > 0)
		{
		out << "  Equate macros in effect:" << endl;
		std::map<char, NxsString>::const_iterator i = defaultEquates.begin();
		for (; i != defaultEquates.end(); ++i)
			{
			out << "   " << (*i).first << " = " << i->second << endl;
			}
		i = userEquates.begin();
		for (; i != userEquates.end(); ++i)
			{
			out << "   " << (*i).first << " = " << i->second << endl;
			}
		}
	else
		out << "  No equate macros have been defined" << endl;

	if (eliminated.empty())
		out << "  No characters were eliminated" << endl;
	else
		{
		out << "  The following characters were eliminated:" << endl;
		NxsUnsignedSet::const_iterator k;
		for (k = eliminated.begin(); k != eliminated.end(); k++)
			{
			out << "   " << ((*k)+1) << endl;
			}
		}


	if (excluded.empty())
		out << "  no characters excluded" << endl;
	else
		{
		out << "  The following characters have been excluded:\n";
		for (NxsUnsignedSet::const_iterator eIt = excluded.begin(); eIt != excluded.end(); ++eIt)
			out << "   " << (*eIt+1) << endl;
		}
	out << "  Data matrix:" << endl;
	DebugShowMatrix(out, false, "    ");
	}

void NxsCharactersBlock::WriteAsNexus(std::ostream &out) const
	{
	out << "BEGIN CHARACTERS;\n";
	WriteBasicBlockCommands(out);
	out << "    DIMENSIONS";
	if (this->taxa)
		{
		const unsigned wod = GetNTaxWithData();
		if (wod > 0)
			{
			const unsigned tnt = taxa->GetNTax();
			if (wod != tnt)
				out << " NTax=" << wod;
			}
		}
	const unsigned multiplier = (this->datatype == NxsCharactersBlock::codon ? 3 : 1);
	out << " NChar=" << multiplier*(this->nChar) << ";\n";
	this->WriteEliminateCommand(out);
	this->WriteFormatCommand(out);
	this->WriteCharStateLabelsCommand(out);
	this->WriteMatrixCommand(out);
	WriteSkippedCommands(out);
	out << "END;\n";
	}


void NxsCharactersBlock::WriteEliminateCommand(
  std::ostream &out) const /* output stream on which to print matrix */
	{
	if (eliminated.empty())
		return;
	out << "    ELIMINATE";
	for (NxsUnsignedSet::const_iterator u = this->eliminated.begin(); u != this->eliminated.end(); ++u)
		out << ' ' << (1 + *u);
	out << ";\n";
	}


void NxsCharactersBlock::WriteMatrixCommand(
  std::ostream &out) const /* output stream on which to print matrix */
	{
	if (taxa == NULL)
		return;
	unsigned width = taxa->GetMaxTaxonLabelLength();
	const unsigned ntaxTotal = taxa->GetNTax();
	out << "Matrix\n";
	int prec = 6;
	if (datatype == continuous)
		prec = (int)out.precision(10);
	unsigned stride = (this->writeInterleaveLen < 1 ? this->nChar : this->writeInterleaveLen);
	unsigned begChar = 0;
	while (begChar < this->nChar)
		{
		if (begChar > 0)
			out << '\n';
		unsigned endChar  = std::min(begChar + stride, this->nChar);
		for (unsigned i = 0; i < ntaxTotal; i++)
			{
			if (this->TaxonIndHasData(i))
				{
				const std::string currTaxonLabel = NxsString::GetEscaped(taxa->GetTaxonLabel(i));
				out << currTaxonLabel;
				unsigned currTaxonLabelLen = (unsigned)currTaxonLabel.size();
				unsigned diff = width - currTaxonLabelLen;
				for (unsigned k = 0; k < diff+5; k++)
					out << ' ';

				WriteStatesForMatrixRow(out, i, UINT_MAX, begChar, endChar);
				out << '\n';
				}
			}
		begChar = endChar;
		}
	out << ";\n";
	if (datatype == continuous)
		out.precision(prec);
	}

std::string NxsCharactersBlock::GetMatrixRowAsStr(const unsigned rowIndex) const /* output stream on which to print matrix */
	{
	if (!this->TaxonIndHasData(rowIndex))
		return std::string();
	std::ostringstream o;
	WriteStatesForMatrixRow(o, rowIndex, UINT_MAX, 0, this->nChar);
	return o.str();
	}

void NxsCharactersBlock::WriteStatesForMatrixRow(
  std::ostream &out,				/* the output stream on which to write */
  unsigned currTaxonIndex,	/* the taxon, in range [0..`ntax') */
  unsigned ,
  unsigned beginChar,
  unsigned endChar) const		/* the index of the first taxon (if UINT_MAX, don't use matchchar) */
	{
	WriteStatesForTaxonAsNexus(out, currTaxonIndex, beginChar, endChar);
	}


void NxsCharactersBlock::WriteCharLabelsCommand(std::ostream &out) const
	{
	if (indToCharLabel.empty())
		return;
	out << "    CHARLABELS";
	std::map<unsigned, std::string>::const_iterator resultSearchIt;
	const std::map<unsigned, std::string>::const_iterator endIt = indToCharLabel.end();
	unsigned emptyLabelsToWrite = 0;
	for (unsigned oit = 0; oit < nChar; ++oit)
		{
		resultSearchIt = indToCharLabel.find(oit);
		if (resultSearchIt == endIt)
			emptyLabelsToWrite++;
		else
			{
			for (unsigned j = 0; j < emptyLabelsToWrite; ++j)
				out << " _";
			emptyLabelsToWrite = 0;
			out << ' ' << NxsString::GetEscaped(resultSearchIt->second);
			}
		}
	out << ";\n";
	}

void NxsCharactersBlock::WriteCharStateLabelsCommand(std::ostream &out) const
	{
	if (charStates.empty())
		{
		this->WriteCharLabelsCommand(out);
		return;
		}
	const NxsString mtString;
	bool isFirst = true;
	std::map<unsigned, std::string>::const_iterator resultSearchIt;
	const std::map<unsigned, std::string>::const_iterator endIt = indToCharLabel.end();
	const NxsStringVectorMap::const_iterator endCSIt = this->charStates.end();
	for (unsigned oit = 0; oit < nChar; ++oit)
		{
		resultSearchIt = indToCharLabel.find(oit);
		NxsString escapedCLabel;
		if (resultSearchIt != endIt)
			escapedCLabel = NxsString::GetEscaped(resultSearchIt->second).c_str();
		const NxsStringVectorMap::const_iterator cib = this->charStates.find(oit);
		if (isFirst)
			{
			out << "    CharStateLabels \n      ";
			isFirst = false;
			}
		else
			out << ",\n      ";
		out << 1 + oit << ' ';
		if (cib != endCSIt)
			{
			const NxsStringVector & stateLabelsVec = cib->second;
			unsigned ns = (unsigned)stateLabelsVec.size();
			if (!escapedCLabel.empty())
				out << escapedCLabel;
			out << " / ";
			for (unsigned m = 0; m < ns; m++)
				out << " " << NxsString::GetEscaped(stateLabelsVec[m]);
			}
		else if (!escapedCLabel.empty())
			out << escapedCLabel;
		else out << '/';
		}
	out << ";\n";
	}

void NxsCharactersBlock::WriteFormatCommand(std::ostream &out) const
	{
	const NxsDiscreteDatatypeMapper * mapper =  GetDatatypeMapperForChar(0);
	if (IsMixedType())
		{
		out << "    FORMAT Datatype=MIXED(";
		bool first = true;
		for (std::vector<DatatypeMapperAndIndexSet>::const_iterator mIt = datatypeMapperVec.begin(); mIt != datatypeMapperVec.end(); ++mIt)
			{
			if (first)
				first = false;
			else
				out << ", ";
			out << GetNameOfDatatype(mIt->first.GetDatatype()) << ':';
			NxsSetReader::WriteSetAsNexusValue(mIt->second, out);
			}
		out << ')';
		if (this->missing != '?')
			out << " Missing=" << this->missing;
		if (this->gap != '\0')
			out << "  Gap=" << this->gap;
		}
	else
		mapper->WriteStartOfFormatCommand(out);

	if (this->respectingCase)
		out << " RespectCase";

	if (this->matchchar != '\0')
		out << "  MatchChar=" << this->matchchar;
	if (this->datatype == continuous)
		{
		out << " Items = (";
		for (vector<std::string>::const_iterator iIt = items.begin(); iIt != items.end(); ++iIt)
			out << *iIt << ' ';
		out << ")";
		if (this->statesFormat == STATES_PRESENT)
			out << " StatesFormat=StatesPresent";
		}
	else if (this->statesFormat == INDIVIDUALS)
			out << " StatesFormat=Individuals";

	if (this->tokens && this->datatype != NxsCharactersBlock::continuous) /*TOKENS is the only choice for continuous data*/
		out << " Tokens";
	if (this->writeInterleaveLen > 1 && (this->nChar > (unsigned)this->writeInterleaveLen ))
		{
		out << " Interleave";
		}
	out << ";\n";
	}

std::map<char, NxsString> NxsCharactersBlock::GetDefaultEquates(DataTypesEnum dt)
	{
	std::map<char, NxsString> defEquates;
	if (dt == NxsCharactersBlock::dna || dt == NxsCharactersBlock::rna || dt == NxsCharactersBlock::nucleotide)
		{
		defEquates['R'] = NxsString("{AG}");
		defEquates['M'] = NxsString("{AC}");
		defEquates['S'] = NxsString("{CG}");
		defEquates['V'] = NxsString("{ACG}");
		if (dt == NxsCharactersBlock::dna || dt == NxsCharactersBlock::nucleotide)
			{
			defEquates['Y'] = NxsString("{CT}");
			defEquates['K'] = NxsString("{GT}");
			defEquates['W'] = NxsString("{AT}");
			defEquates['H'] = NxsString("{ACT}");
			defEquates['B'] = NxsString("{CGT}");
			defEquates['D'] = NxsString("{AGT}");
			defEquates['N'] = NxsString("{ACGT}");
			defEquates['X'] = NxsString("{ACGT}");
			if (dt == NxsCharactersBlock::nucleotide)
				defEquates['U'] ='T';
			}
		else
			{
			defEquates['Y'] = NxsString("{CU}");
			defEquates['K'] = NxsString("{GU}");
			defEquates['W'] = NxsString("{AU}");
			defEquates['H'] = NxsString("{ACU}");
			defEquates['B'] = NxsString("{CGU}");
			defEquates['D'] = NxsString("{AGU}");
			defEquates['N'] = NxsString("{ACGU}");
			defEquates['X'] = NxsString("{ACGU}");
			}
		}
	else if (dt == NxsCharactersBlock::protein)
		{
		defEquates['B'] = NxsString("{DN}");
		defEquates['Z'] = NxsString("{EQ}");
		defEquates['X'] = NxsString("{ACDEFGHIKLMNPQRSTVWY*}");
		}
	/* molecular datatypes are the only datatypes with default equates and
		keys of either case are equivalent.
	*/
	NxsString upperKeys;
	for (std::map<char, NxsString>::const_iterator k = defEquates.begin(); k != defEquates.end(); ++k)
		{
		upperKeys += k->first;
		}
	for (std::string::const_iterator k = upperKeys.begin(); k != upperKeys.end(); ++k)
		{
		const char c = *k;
		const char lc = (char)tolower(c);
		defEquates[lc] = defEquates[c];
		}

	return defEquates;
	}

const char * NxsCharactersBlock::GetNameOfDatatype(DataTypesEnum datatype)
	{
	switch(datatype)
		{
		case NxsCharactersBlock::codon:
		case NxsCharactersBlock::dna:
			return "DNA";
		case NxsCharactersBlock::rna:
			return "RNA";
		case NxsCharactersBlock::nucleotide:
			return "Nucleotide";
		case NxsCharactersBlock::protein:
			return "Protein";
		case NxsCharactersBlock::continuous:
			return "Continuous"; // do not change!  phylobase uses this!!!
		default:
			return "Standard";
		}
	}

/*!
	Returns NxsCharactersBlock object to the state it was in when first created.
*/
void NxsCharactersBlock::Reset()
	{
	ResetSurrogate();
	NxsBlock::Reset();
	nTaxWithData = 0;
	nChar = 0;
	newtaxa				= false;
	interleaving		= false;
	transposing			= false;
	respectingCase		= false;
	labels				= true;
	tokens				= false;
	datatype			= NxsCharactersBlock::standard;
	originalDatatype	= NxsCharactersBlock::standard;
	datatypeReadFromFormat = false;
	missing				= '?';
	gap					= '\0';
	gapMode = GAP_MODE_MISSING;
	matchchar			= '\0';
	symbols.clear();
	ResetSymbols();

	ucCharLabelToIndex.clear();
	indToCharLabel.clear();
	charSets.clear();
	charPartitions.clear();
	codonPosPartitions.clear();
	defCodonPosPartitionName.clear();
	exSets.clear();
	charStates.clear();
	globalStateLabels.clear();
	userEquates.clear();
	defaultEquates.clear();
	eliminated.clear();
	datatypeMapperVec.clear();
	discreteMatrix.clear();
	continuousMatrix.clear();
	items = std::vector<std::string>(1, std::string("STATES"));
	statesFormat = STATES_PRESENT;
	restrictionDataype = false;
	}

std::string NxsCharactersBlock::GetDefaultSymbolsForType(NxsCharactersBlock::DataTypesEnum dt)
	{
	switch(dt)
		{
		case NxsCharactersBlock::nucleotide:
		case NxsCharactersBlock::dna:
			return std::string("ACGT");
		case NxsCharactersBlock::rna:
			return std::string("ACGU");
		case NxsCharactersBlock::protein:
			return std::string("ACDEFGHIKLMNPQRSTVWY*");
		case NxsCharactersBlock::standard:
			return std::string("01");
		default:
			return std::string();

		}
	return std::string();
	}
/*!
	Resets standard symbol set after a change in `datatype' is made. Also flushes equates list and installs standard
	equate macros for the current `datatype'.
*/
void NxsCharactersBlock::ResetSymbols()
	{
	symbols = GetDefaultSymbolsForType(datatype);
	userEquates.clear();
	defaultEquates = GetDefaultEquates(datatype);
	datatypeMapperVec.clear();
	}

/*!
	Looks up the state(s) at row `i', column `charNum' of matrix and writes it (or them) to out. If there is uncertainty or
	polymorphism, the list of states is surrounded by the appropriate set of symbols (i.e., parentheses for
	polymorphism, curly brackets for uncertainty). If TOKENS was specified, the output takes the form of the defined
	state labels; otherwise, the correct symbol is looked up in `symbols' and output.
*/
void NxsCharactersBlock::ShowStateLabels(
  std::ostream &out,				/* the output stream on which to write */
  unsigned taxInd,				/* the taxon, in range [0..`ntax') */
  unsigned charInd,				/* the character, in range [0..`nChar') */
  unsigned ) const		/* the index of the first taxon (if UINT_MAX, don't use matchchar) */
	{
	if (datatype == continuous)
		{
		const ContinuousCharCell & cell = continuousMatrix.at(taxInd).at(charInd);
		std::vector<std::string>::const_iterator itemIt = items.begin();
		bool parensNeeded = items.size() > 1;
		if (items.size() == 1)
			{
			ContinuousCharCell::const_iterator oit = cell.find(*itemIt);
			if (oit != cell.end() && oit->second.size() > 1)
				parensNeeded = true;
			}
		if (parensNeeded)
			out	 << '(';
		for (; itemIt != items.end(); ++itemIt)
			{
			ContinuousCharCell::const_iterator cit = cell.find(*itemIt);
			if (cit == cell.end())
				out << missing << ' ';
			else
				{
				if (cit->second.empty())
					out << missing << ' ';
				else
					{
					vector<double>::const_iterator vIt = cit->second.begin();
					for(; vIt != cit->second.end(); ++vIt)
						{
						if (*vIt == DBL_MAX)
							out << missing << ' ';
						else
							out << *vIt << ' ';
						}
					}
				}
			}
		if (parensNeeded)
			out	 << ") ";
		else
			out << ' ';
		return;
		}
	const NxsDiscreteDatatypeMapper * mapper = GetDatatypeMapperForChar(charInd);
	NCL_ASSERT(mapper != NULL);
	const NxsDiscreteStateCell currStateCode = discreteMatrix.at(taxInd).at(charInd);
	if (tokens)
		{
		out << ' ';
		if (currStateCode >= 0 && currStateCode < (NxsDiscreteStateCell) mapper->GetNumStates())
			{
			NxsStringVectorMap::const_iterator ci = charStates.find(charInd);
			if (ci != charStates.end() && ((NxsDiscreteStateCell) ci->second.size()) > currStateCode)
				out << ci->second[currStateCode];
			else if (currStateCode < 0)
				{
				if (currStateCode == NXS_MISSING_CODE)
					out << this->GetMissingSymbol();
				else if (currStateCode == NXS_GAP_STATE_CODE)
					out << this->GetGapSymbol();
				else
					out << '_';
				}
			else if (globalStateLabels.size() > (unsigned) currStateCode)
				out << globalStateLabels[currStateCode];
			else
				out << '_';
			return;
			}
		}
	mapper->WriteStateCodeAsNexusString(out, currStateCode);
	}

/*!
	Writes out the state (or states) stored in this NxsDiscreteDatum object to the buffer `s' using the symbols array
	to do the necessary translation of the numeric state values to state symbols. In the case of polymorphism or
	uncertainty, the list of states will be surrounded by brackets or parentheses (respectively). Assumes `s' is
	non-NULL and long enough to hold everything printed.
*/
void NxsCharactersBlock::WriteStates(
  NxsDiscreteDatum &d,	/* the datum to be queried */
  char *s,				/* the buffer to which to print */
  unsigned slen) NCL_COULD_BE_CONST /* the length of the buffer `s' */ /*v2.1to2.2 1 */
	{
	std::ostringstream outs;
	ShowStates(outs, d.taxInd, d.charInd);
	std::string sfo = outs.str();
	if (s == NULL || sfo.length() > slen)
		throw NxsNCLAPIException("Char buffer too small in NxsCharactersBlock::WriteStates");
	strcpy(s, sfo.c_str());
	}

/*!
	This function is no longer the most efficient way to access parsed data (see notes on NxsCharacterBlock and
	GetMatrix() and GetMatrixDecoder() methods.

	Returns the number of states for taxon `i', character `j'.
*/
unsigned NxsCharactersBlock::GetNumStates(
  unsigned taxInd,	/* the taxon in range [0..`ntax') */
  unsigned charInd) NCL_COULD_BE_CONST /* the character in range [0..`nChar') */ /*v2.1to2.2 1 */
	{
	const NxsDiscreteDatatypeMapper * mapper = GetDatatypeMapperForChar(charInd);
	NCL_ASSERT(mapper != NULL);
	const NxsDiscreteStateCell currStateCode = discreteMatrix.at(taxInd).at(charInd);
	return mapper->GetNumStatesInStateCode(currStateCode);
	}

/*! Excludes character with index `i`.
*/
void NxsCharactersBlock::ExcludeCharacter(
  unsigned i)	/* index of character to exclude in range [0..`nChar') */
	{
	if (i >= nChar)
		{
		errormsg  = "Character index is ExcludeCharacter out-of-range.   Must be < ";
		errormsg << nChar;
		throw NxsNCLAPIException(errormsg);
		}
	excluded.insert(i);
	}
/*! Includes (or "activates") character with index `i`.
*/
void NxsCharactersBlock::IncludeCharacter(
  unsigned i)	/* index of character to include in range [0..`nChar') */
	{
	if (i >= nChar)
		{
		errormsg  = "Character index is ExcludeCharacter out-of-range.   Must be < ";
		errormsg << nChar;
		throw NxsNCLAPIException(errormsg);
		}
	excluded.erase(i);
	}

bool NxsCharactersBlock::IsGapState(
  unsigned taxInd,	/* the taxon, in range [0..`ntax') */
  unsigned charInd) NCL_COULD_BE_CONST /* the character, in range [0..`nChar') */ /*v2.1to2.2 1 */
	{
	if (this->datatype == continuous)
		return false;
	const NxsDiscreteStateRow & row = discreteMatrix.at(taxInd);
	return (row.size() > charInd && row[charInd] == NXS_GAP_STATE_CODE);
	}

bool NxsCharactersBlock::IsMissingState(
  unsigned taxInd,	/* the taxon, in range [0..`ntax') */
  unsigned charInd) NCL_COULD_BE_CONST /* the character, in range [0..`nChar') */ /*v2.1to2.2 1 */
	{
	if (this->datatype == continuous)
		{
		return !continuousMatrix.at(taxInd).empty();
		}
	const NxsDiscreteStateRow & row = discreteMatrix.at(taxInd);
	return (row.size() <= charInd || (row[charInd] == NXS_MISSING_CODE));
	}


void NxsCharactersBlock::FindConstantCharacters(NxsUnsignedSet &c) const
	{
	vector<NxsDiscreteStateCell> iv;
	for (unsigned colIndex = 0; colIndex < nChar; ++colIndex)
		{
		const NxsDiscreteDatatypeMapper * mapper = GetDatatypeMapperForChar(colIndex);
		if (mapper == NULL)
			throw NxsNCLAPIException("No DatatypeMapper in FindConstantCharacters");

		std::set<NxsDiscreteStateCell> intersectionSet = mapper->GetStateSetForCode(NXS_MISSING_CODE);
		for (NxsDiscreteStateMatrix::const_iterator rowIt = discreteMatrix.begin(); rowIt != discreteMatrix.end(); ++rowIt)
			{
			const NxsDiscreteStateRow & row = *rowIt;
			if (row.size() > colIndex)
				{
				const NxsDiscreteStateCell sc = row[colIndex];
				std::set<NxsDiscreteStateCell> currSet = mapper->GetStateSetForCode(sc);
				iv.clear();
				set_intersection(currSet.begin(), currSet.end(), intersectionSet.begin(), intersectionSet.end(), std::back_inserter(iv));
				intersectionSet.clear();
				if (iv.empty())
					break;
				intersectionSet.insert(iv.begin(), iv.end());
				}
			}
		if (!intersectionSet.empty())
			c.insert(colIndex);
		}
	}

void NxsCharactersBlock::FindGappedCharacters(NxsUnsignedSet &c) const
	{
	vector<NxsDiscreteStateCell> iv;
	for (unsigned colIndex = 0; colIndex < nChar; ++colIndex)
		{
		for (NxsDiscreteStateMatrix::const_iterator rowIt = discreteMatrix.begin(); rowIt != discreteMatrix.end(); ++rowIt)
			{
			const NxsDiscreteStateRow & row = *rowIt;
			if (row.size() > colIndex && row[colIndex] == NXS_GAP_STATE_CODE)
				{
				c.insert(colIndex);
				break;
				}
			}
		}
	}

/* Behaves like GetMaximalStateSetOfColumn except that missing data columns do not increase
	size of the returned state set.
	If GapMode is missing, then gaps are not counted.
*/
std::set<NxsDiscreteStateCell> NxsCharactersBlock::GetNamedStateSetOfColumn(const unsigned colIndex) const
	{
	const NxsDiscreteDatatypeMapper * mapper = GetDatatypeMapperForChar(colIndex);
	if (mapper == NULL)
		throw NxsNCLAPIException("No DatatypeMapper in GetNamedStateSetOfColumn");

	std::set<NxsDiscreteStateCell> sset;
	std::set<NxsDiscreteStateCell> scodes;
	const unsigned maxnstates = mapper->GetNumStatesIncludingGap();
	for (NxsDiscreteStateMatrix::const_iterator rowIt = discreteMatrix.begin(); rowIt != discreteMatrix.end(); ++rowIt)
		{
		const NxsDiscreteStateRow & row = *rowIt;
		if (row.size() > colIndex)
			{
			const NxsDiscreteStateCell sc = row[colIndex];
			const bool isIgnoredGap = (sc == NXS_GAP_STATE_CODE) && (this->gapMode == GAP_MODE_MISSING);
			const bool toBeCounted = !(sc == NXS_MISSING_CODE || isIgnoredGap);
			if (toBeCounted && scodes.count(sc) == 0)
				{
				scodes.insert(sc);
				const std::set<NxsDiscreteStateCell>	& ts = mapper->GetStateSetForCode(sc);
				sset.insert(ts.begin(), ts.end());
				if (sset.size() == maxnstates)
					break;
				}
			}
		}
	return sset;
	}
/* Returns the union of all states that are consistent with a column */
std::set<NxsDiscreteStateCell> NxsCharactersBlock::GetMaximalStateSetOfColumn(const unsigned colIndex) const
	{
	const NxsDiscreteDatatypeMapper * mapper = GetDatatypeMapperForChar(colIndex);
	if (mapper == NULL)
		throw NxsNCLAPIException("No DatatypeMapper in GetMaximalStateSetOfColumn");

	std::set<NxsDiscreteStateCell> sset;
	std::set<NxsDiscreteStateCell> scodes;
	const unsigned maxnstates = mapper->GetNumStatesIncludingGap();
	for (NxsDiscreteStateMatrix::const_iterator rowIt = discreteMatrix.begin(); rowIt != discreteMatrix.end(); ++rowIt)
		{
		const NxsDiscreteStateRow & row = *rowIt;
		if (row.size() > colIndex)
			{
			const NxsDiscreteStateCell sc = row[colIndex];
			if (scodes.count(sc) == 0)
				{
				scodes.insert(sc);
				const std::set<NxsDiscreteStateCell>	& ts = mapper->GetStateSetForCode(sc);
				sset.insert(ts.begin(), ts.end());
				if (sset.size() == maxnstates)
					break;
				}
			}
		}
	return sset;
	}

 bool NxsCharactersBlock::IsPolymorphic(
  unsigned taxInd,	/* the taxon in range [0..`ntax') */
  unsigned charInd) NCL_COULD_BE_CONST /* the character in range [0..`nChar') */ /*v2.1to2.2 1 */
	{
	const NxsDiscreteDatatypeMapper * mapper = GetDatatypeMapperForChar(charInd);
	NCL_ASSERT(mapper);
	if (taxInd >= discreteMatrix.size())
		throw NxsNCLAPIException("Taxon index out of range of NxsCharactersBlock::IsPolymorphic");
	const NxsDiscreteStateRow & row = discreteMatrix[taxInd];
	if (row.size() <= charInd)
		throw NxsNCLAPIException("Character index out of range of NxsCharactersBlock::IsPolymorphic");
	return mapper->IsPolymorphic(row[charInd]);
	}


/*!
	Shows the states for taxon `i', character `j', on the stream `out'. Uses `symbols' array to translate the states
	from the way they are stored (as integers) to the symbol used in the original data matrix. Assumes `i' is in the
	range [0..`ntax') and `j' is in the range [0..`nChar'). Also assumes `matrix' is non-NULL.
*/
void NxsCharactersBlock::ShowStates(
  std::ostream &out, /* the stream on which to show the state(s) */
  unsigned taxInd,	/* the (0-offset) index of the taxon in question */
  unsigned charInd) NCL_COULD_BE_CONST /* the (0-offset) index of the character in question */ /*v2.1to2.2 1 */
	{
	bool ft = tokens;
	tokens = false;
	ShowStateLabels(out, taxInd, charInd, UINT_MAX);
	tokens = ft;
	}

/*---------------------------------------------------------------------------------------
 Results in aliasing of the taxa, assumptionsBlock blocks!
*/
void NxsCharactersBlock::CopyCharactersContents(const NxsCharactersBlock &other)
	{
	assumptionsBlock = other.assumptionsBlock;
	nChar = other.nChar;
	nTaxWithData = other.nTaxWithData;
	matchchar = other.matchchar;
	respectingCase = other.respectingCase;
	transposing = other.transposing;
	interleaving = other.interleaving;
	tokens = other.tokens;
	labels = other.labels;
	missing = other.missing;
	gap = other.gap;
	gapMode = other.gapMode;
	symbols = other.symbols;
	userEquates = other.userEquates;
	datatypeMapperVec = other.datatypeMapperVec;
	discreteMatrix = other.discreteMatrix;
	continuousMatrix = other.continuousMatrix;
	eliminated = other.eliminated;
	excluded = other.excluded;
	ucCharLabelToIndex = other.ucCharLabelToIndex;
	indToCharLabel = other.indToCharLabel;
	charStates = other.charStates;
	globalStateLabels = other.globalStateLabels;
	items = other.items;
	charSets = other.charSets;
	exSets = other.exSets;
	charPartitions = other.charPartitions;
	codonPosPartitions = other.codonPosPartitions;
	defCodonPosPartitionName = other.defCodonPosPartitionName;
	transfMgr = other.transfMgr;
	datatype = other.datatype;
	statesFormat = other.statesFormat;
	supportMixedDatatype = other.supportMixedDatatype;
	convertAugmentedToMixed = other.convertAugmentedToMixed;
	allowAugmentingOfSequenceSymbols = other.allowAugmentingOfSequenceSymbols;
	restrictionDataype = other.restrictionDataype;
	writeInterleaveLen = other.writeInterleaveLen;
	}


NxsCharactersBlock *NxsCharactersBlockFactory::GetBlockReaderForID(const std::string & idneeded, NxsReader *reader, NxsToken *)
	{
	if (reader == NULL || idneeded != "CHARACTERS")
		return NULL;
	NxsCharactersBlock * nb  = new NxsCharactersBlock(NULL, NULL);
	nb->SetCreateImpliedBlock(true);
	nb->SetImplementsLinkAPI(true);
	return nb;
	}

// returns a vector of vectors of  the states for each state code.
// 	The second to the last element will be empty to correspond to  NXS_GAP_STATE_CODE = -2
// 	The last element will be empty to correspond to NXS_MISSING_CODE = -1

std::vector<std::vector<int> > NxsDiscreteDatatypeMapper::GetPythonicStateVectors() const
	{

	std::vector<std::vector<int> > pv(this->GetNumStateCodes());

	const int endIndex = (((int) stateSetsVec.size()) + sclOffset);
	for (int i = 0; i < endIndex; ++i)
		{
		NxsDiscreteStateRow r = this->GetStateVectorForCode(i);
		pv[i].reserve(r.size());
		for (NxsDiscreteStateRow::const_iterator rIt = r.begin(); rIt != r.end(); ++rIt)
			pv[i].push_back((int)*rIt);
		}
	return pv;
	}