File: enscoordsystem.c

package info (click to toggle)
emboss 6.6.0%2Bdfsg-15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 571,580 kB
  • sloc: ansic: 460,579; java: 29,383; perl: 13,573; sh: 12,753; makefile: 3,295; csh: 706; asm: 351; xml: 239; pascal: 237; modula3: 8
file content (3502 lines) | stat: -rw-r--r-- 99,353 bytes parent folder | download | duplicates (6)
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
/* @source enscoordsystem *****************************************************
**
** Ensembl Coordinate System functions
**
** @author Copyright (C) 1999 Ensembl Developers
** @author Copyright (C) 2006 Michael K. Schuster
** @version $Revision: 1.52 $
** @modified 2009 by Alan Bleasby for incorporation into EMBOSS core
** @modified $Date: 2013/02/17 13:02:40 $ by $Author: mks $
** @@
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library 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
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA  02110-1301,  USA.
**
******************************************************************************/

/* ========================================================================= */
/* ============================= include files ============================= */
/* ========================================================================= */

#include "enscoordsystem.h"
#include "ensmetainformation.h"
#include "enstable.h"




/* ========================================================================= */
/* =============================== constants =============================== */
/* ========================================================================= */




/* ========================================================================= */
/* =========================== global variables ============================ */
/* ========================================================================= */




/* ========================================================================= */
/* ============================= private data ============================== */
/* ========================================================================= */




/* ========================================================================= */
/* =========================== private constants =========================== */
/* ========================================================================= */




/* ========================================================================= */
/* =========================== private variables =========================== */
/* ========================================================================= */




/* ========================================================================= */
/* =========================== private functions =========================== */
/* ========================================================================= */

static int listCoordsystemCompareIdentifierAscending(
    const void *item1,
    const void *item2);

static int listCoordsystemCompareIdentifierDescending(
    const void *item1,
    const void *item2);

static int listCoordsystemCompareRankAscending(
    const void *item1,
    const void *item2);

static int listCoordsystemCompareRankDescending(
    const void *item1,
    const void *item2);

static AjBool coordsystemadaptorFetchAllbyStatement(
    EnsPCoordsystemadaptor csa,
    const AjPStr statement,
    AjPList css);

static AjBool coordsystemadaptorCacheInit(EnsPCoordsystemadaptor csa);

static AjBool coordsystemadaptorMappingpathInit(EnsPCoordsystemadaptor csa);

static AjBool coordsystemadaptorSeqregionMapInit(EnsPCoordsystemadaptor csa);

static void coordsystemadaptorMappingpathValdel(void **Pvalue);

static void coordsystemadaptorFetchAll(const void *key,
                                       void **Pvalue,
                                       void *cl);




/* ========================================================================= */
/* ======================= All functions by section ======================== */
/* ========================================================================= */




/* @filesection enscoordsystem ************************************************
**
** @nam1rule ens Function belongs to the Ensembl library.
**
******************************************************************************/




/* @datasection [EnsPCoordsystem] Ensembl Coordinate System *******************
**
** @nam2rule Coordsystem Functions for manipulating
** Ensembl Coordinate System objects
**
** @cc Bio::EnsEMBL::CoordSystem
** @cc CVS Revision: 1.14
** @cc CVS Tag: branch-ensembl-68
**
******************************************************************************/




/* @section constructors ******************************************************
**
** All constructors return a new Ensembl Coordinate System by pointer.
** It is the responsibility of the user to first destroy any previous
** Coordinate System. The target pointer does not need to be initialised to
** NULL, but it is good programming practice to do so anyway.
**
** @fdata [EnsPCoordsystem]
**
** @nam3rule New Constructor
** @nam4rule Cpy Constructor with existing object
** @nam4rule Ini Constructor with initial values
** @nam4rule Ref Constructor by incrementing the use counter
**
** @argrule Cpy cs [const EnsPCoordsystem] Ensembl Coordinate System
** @argrule Ini csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
** @argrule Ini identifier [ajuint] SQL database-internal identifier
** @argrule Ini name [AjPStr] Name
** @argrule Ini version [AjPStr] Version
** @argrule Ini rank [ajuint] Rank
** @argrule Ini dflt [AjBool] Default attribute
** @argrule Ini toplevel [AjBool] Top-level attrbute
** @argrule Ini seqlevel [AjBool] Sequence-level attribute
** @argrule Ref cs [EnsPCoordsystem] Ensembl Coordinate System
**
** @valrule * [EnsPCoordsystem] Ensembl Coordinate System or NULL
**
** @fcategory new
******************************************************************************/




/* @func ensCoordsystemNewCpy *************************************************
**
** Object-based constructor function, which returns an independent object.
**
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @return [EnsPCoordsystem] Ensembl Coordinate System or NULL
**
** @release 6.4.0
** @@
******************************************************************************/

EnsPCoordsystem ensCoordsystemNewCpy(const EnsPCoordsystem cs)
{
    EnsPCoordsystem pthis = NULL;

    if (!cs)
        return NULL;

    AJNEW0(pthis);

    pthis->Use = 1U;

    pthis->Identifier = cs->Identifier;

    pthis->Adaptor = cs->Adaptor;

    if (cs->Name)
        pthis->Name = ajStrNewRef(cs->Name);

    /*
    ** Although Coordinate System versions are optional, the AJAX String
    ** should always be defined, since Ensembl Slice names are depending
    ** on it.
    */

    if (cs->Version)
        pthis->Version = ajStrNewRef(cs->Version);
    else
        pthis->Version = ajStrNew();

    pthis->Default = cs->Default;

    pthis->SequenceLevel = cs->SequenceLevel;

    pthis->Toplevel = cs->Toplevel;

    pthis->Rank = cs->Rank;

    return pthis;
}




/* @func ensCoordsystemNewIni *************************************************
**
** Constructor for an Ensembl Coordinate System with initial values.
**
** @cc Bio::EnsEMBL::Storable::new
** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
** @param [r] identifier [ajuint] SQL database-internal identifier
** @cc Bio::EnsEMBL::CoordSystem::new
** @param [u] name [AjPStr] Name
** @param [uN] version [AjPStr] Version
** @param [r] rank [ajuint] Rank
** @param [r] dflt [AjBool] Default attribute
** @param [r] toplevel [AjBool] Top-level attrbute
** @param [r] seqlevel [AjBool] Sequence-level attribute
**
** @return [EnsPCoordsystem] Ensembl Coordinate System or NULL
**
** @release 6.4.0
** @@
** Many Ensembl Coordinate System objects do not have a concept of a version
** for the entire Ensembl Coordinate System, although they may have a
** per-sequence version.
** The 'chromosome' Ensembl Coordinate System usually has a version - i.e. the
** assembly build version - but the clonal Ensembl Coordinate System does not,
** despite having individual sequence versions. In the case where an
** Ensembl  Coordinate System does not have a version an empty string is
** used instead.
**
** The highest-level Ensembl Coordinate System (e.g. chromosome) should have
** rank 1, the second-highest level Ensembl Coordinate System (e.g. clone)
** should have rank 2 and so on.
**
** Top-level should only be set for creating an artificial
** top-level Ensembl Coordinate System of name 'toplevel'.
******************************************************************************/

EnsPCoordsystem ensCoordsystemNewIni(
    EnsPCoordsystemadaptor csa,
    ajuint identifier,
    AjPStr name,
    AjPStr version,
    ajuint rank,
    AjBool dflt,
    AjBool toplevel,
    AjBool seqlevel)
{
    EnsPCoordsystem cs = NULL;

    if (ajDebugTest("ensCoordsystemNewIni"))
        ajDebug("ensCoordsystemNewIni\n"
                "  csa %p\n"
                "  identifier %u\n"
                "  name '%S'\n"
                "  version '%S'\n"
                "  rank %u\n"
                "  dflt '%B'\n"
                "  toplevel '%B'\n"
                "  seqlevel '%B'\n",
                csa,
                identifier,
                name,
                version,
                rank,
                dflt,
                toplevel,
                seqlevel);

    if (toplevel)
    {
        if (name && ajStrGetLen(name))
        {
            if (!ajStrMatchCaseC(name, "toplevel"))
            {
                ajWarn("ensCoordsystemNewIni name parameter must be 'toplevel' "
                       "if the top-level parameter is set.\n");

                return NULL;
            }
        }

        if (rank > 0)
        {
            ajWarn("ensCoordsystemNewIni rank parameter must be 0 "
                   "if the top-level parameter is set.\n");

            return NULL;
        }

        if (seqlevel == ajTrue)
        {
            ajWarn("ensCoordsystemNewIni sequence-level parameter must not "
                   "be set if the top-level parameter is set.\n");

            return NULL;
        }

        if (dflt == ajTrue)
        {
            ajWarn("ensCoordsystemNewIni default parameter must not be set "
                   "if the top-level parameter is set.\n");

            return NULL;
        }
    }
    else
    {
        if (name && ajStrGetLen(name))
        {
            if (ajStrMatchCaseC(name, "toplevel"))
            {
                ajWarn("ensCoordsystemNewIni name parameter cannot be "
                       "'toplevel' for non-top-level "
                       "Ensembl Coordinate System objects.\n");

                return NULL;
            }
        }
        else
        {
            ajWarn("ensCoordsystemNewIni name parameter must be provided for "
                   "non-top-level Ensembl Coordinate System objects.\n");

            return NULL;
        }

        if (rank == 0)
        {
            ajWarn("ensCoordsystemNewIni rank parameter must be non-zero "
                   "for non-top-level Ensembl Coordinate System objects.\n");

            return NULL;
        }
    }

    AJNEW0(cs);

    cs->Use = 1U;

    cs->Identifier = identifier;

    cs->Adaptor = csa;

    if (toplevel)
        cs->Name = ajStrNewC("toplevel");
    else if (name)
        cs->Name = ajStrNewRef(name);

    /*
    ** Although Coordinate System versions are optional, the AJAX String
    ** should always be defined, since Ensembl Slice names are depending
    ** on it.
    */

    if (version)
        cs->Version = ajStrNewRef(version);
    else
        cs->Version = ajStrNew();

    cs->Rank = rank;

    cs->SequenceLevel = seqlevel;

    cs->Toplevel = toplevel;

    cs->Default = dflt;

    return cs;
}




/* @func ensCoordsystemNewRef *************************************************
**
** Ensembl Object referencing function, which returns a pointer to the
** Ensembl Object passed in and increases its reference count.
**
** @param [u] cs [EnsPCoordsystem] Ensembl Coordinate System
**
** @return [EnsPCoordsystem] Ensembl Coordinate System or NULL
**
** @release 6.2.0
** @@
******************************************************************************/

EnsPCoordsystem ensCoordsystemNewRef(EnsPCoordsystem cs)
{
    if (ajDebugTest("ensCoordsystemNewRef"))
    {
        ajDebug("ensCoordsystemNewRef\n"
                "  cs %p\n",
                cs);

        ensCoordsystemTrace(cs, 1);
    }

    if (!cs)
        return NULL;

    cs->Use++;

    return cs;
}




/* @section destructors *******************************************************
**
** Destruction destroys all internal data structures and frees the memory
** allocated for an Ensembl Coordinate System object.
**
** @fdata [EnsPCoordsystem]
**
** @nam3rule Del Destroy (free) an Ensembl Coordinate System
**
** @argrule * Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
**
** @valrule * [void]
**
** @fcategory delete
******************************************************************************/




/* @func ensCoordsystemDel ****************************************************
**
** Default destructor for an Ensembl Coordinate System.
**
** @param [d] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
**
** @return [void]
**
** @release 6.2.0
** @@
******************************************************************************/

void ensCoordsystemDel(EnsPCoordsystem *Pcs)
{
    EnsPCoordsystem pthis = NULL;

    if (!Pcs)
        return;

#if defined(AJ_DEBUG) && AJ_DEBUG >= 1
    if (ajDebugTest("ensCoordsystemDel"))
    {
        ajDebug("ensCoordsystemDel\n"
                "  *Pcs %p\n",
                *Pcs);

        ensCoordsystemTrace(*Pcs, 1);
    }
#endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 1 */

    if (!(pthis = *Pcs) || --pthis->Use)
    {
        *Pcs = NULL;

        return;
    }

    ajStrDel(&pthis->Name);
    ajStrDel(&pthis->Version);

    ajMemFree((void **) Pcs);

    return;
}




/* @section member retrieval **************************************************
**
** Functions for returning members of an Ensembl Coordinate System object.
**
** @fdata [EnsPCoordsystem]
**
** @nam3rule Get Return Coordinate System attribute(s)
** @nam4rule Adaptor Return the Ensembl Coordinate System Adaptor
** @nam4rule Default Return the default attribute
** @nam4rule Identifier Return the SQL database-internal identifier
** @nam4rule Name Return the name
** @nam4rule Rank Return the rank
** @nam4rule Seqlevel Return the sequence-level attribute
** @nam4rule Toplevel Return the top-level attribute
** @nam4rule Version Return the version
**
** @argrule * cs [const EnsPCoordsystem] Coordinate System
**
** @valrule Adaptor [EnsPCoordsystemadaptor]
** Ensembl Coordinate System Adaptor or NULL
** @valrule Default [AjBool] Default attribute or ajFalse
** @valrule Identifier [ajuint] SQL database-internal identifier or 0U
** @valrule Name [const AjPStr] Name or NULL
** @valrule Rank [ajuint] Rank or 0U
** @valrule Seqlevel [AjBool] Sequence-level attribute or ajFalse
** @valrule Toplevel [AjBool] Top-level attribute or ajFalse
** @valrule Version [const AjPStr] Version or NULL
**
** @fcategory use
******************************************************************************/




/* @func ensCoordsystemGetAdaptor *********************************************
**
** Get the Ensembl Coordinate System Adaptor member of an
** Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::Storable::adaptor
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @return [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor or NULL
**
** @release 6.2.0
** @@
******************************************************************************/

EnsPCoordsystemadaptor ensCoordsystemGetAdaptor(const EnsPCoordsystem cs)
{
    return (cs) ? cs->Adaptor : NULL;
}




/* @func ensCoordsystemGetDefault *********************************************
**
** Get the default member of an Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::CoordSystem::is_default
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @return [AjBool] ajTrue if the Ensembl Coordinate System version defines the
**                  default of all Ensembl Coordinate System objects with the
**                  same name.
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCoordsystemGetDefault(const EnsPCoordsystem cs)
{
    return (cs) ? cs->Default : ajFalse;
}




/* @func ensCoordsystemGetIdentifier ******************************************
**
** Get the SQL database-internal identifier member of an
** Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::Storable::dbID
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @return [ajuint] SQL database-internal identifier or 0U
**
** @release 6.2.0
** @@
******************************************************************************/

ajuint ensCoordsystemGetIdentifier(const EnsPCoordsystem cs)
{
    return (cs) ? cs->Identifier : 0U;
}




/* @func ensCoordsystemGetName ************************************************
**
** Get the name member of an Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::CoordSystem::name
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @return [const AjPStr] Name or NULL
**
** @release 6.2.0
** @@
******************************************************************************/

const AjPStr ensCoordsystemGetName(const EnsPCoordsystem cs)
{
    return (cs) ? cs->Name : NULL;
}




/* @func ensCoordsystemGetRank ************************************************
**
** Get the rank member of an Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::CoordSystem::rank
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @return [ajuint] Rank or 0U
**
** @release 6.2.0
** @@
******************************************************************************/

ajuint ensCoordsystemGetRank(const EnsPCoordsystem cs)
{
    return (cs) ? cs->Rank : 0U;
}




/* @func ensCoordsystemGetSeqlevel ********************************************
**
** Get the sequence-level member of an Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::CoordSystem::is_sequence_level
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @return [AjBool] ajTrue if the Coordinate System defines the sequence-level
** or ajFalse
**
** @release 6.4.0
** @@
******************************************************************************/

AjBool ensCoordsystemGetSeqlevel(const EnsPCoordsystem cs)
{
    return (cs) ? cs->SequenceLevel : ajFalse;
}




/* @func ensCoordsystemGetToplevel ********************************************
**
** Get the top-level member of an Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::CoordSystem::is_top_level
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @return [AjBool] ajTrue if the coordinate system defines the top-level
** or ajFalse
**
** @release 6.4.0
** @@
******************************************************************************/

AjBool ensCoordsystemGetToplevel(const EnsPCoordsystem cs)
{
    return (cs) ? cs->Toplevel : ajFalse;
}




/* @func ensCoordsystemGetVersion *********************************************
**
** Get the version member of an Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::CoordSystem::version
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @return [const AjPStr] Version or NULL
**
** @release 6.2.0
** @@
******************************************************************************/

const AjPStr ensCoordsystemGetVersion(const EnsPCoordsystem cs)
{
    return (cs) ? cs->Version : NULL;
}




/* @section member assignment *************************************************
**
** Functions for assigning members of an Ensembl Coordinate System object.
**
** @fdata [EnsPCoordsystem]
**
** @nam3rule Set Set one member of a Coordinate System
** @nam4rule Adaptor Set the Ensembl Coordinate System Adaptor
** @nam4rule Identifier Set the SQL database-internal identifier
**
** @argrule * cs [EnsPCoordsystem] Ensembl Coordinate System object
** @argrule Adaptor csa [EnsPCoordsystemadaptor]
** Ensembl Coordinate System Adaptor
** @argrule Identifier identifier [ajuint] SQL database-internal identifier
**
** @valrule * [AjBool] ajTrue upon success, ajFalse otherwise
**
** @fcategory modify
******************************************************************************/




/* @func ensCoordsystemSetAdaptor *********************************************
**
** Set the Object Adaptor member of an Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::Storable::adaptor
** @param [u] cs [EnsPCoordsystem] Ensembl Coordinate System
** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCoordsystemSetAdaptor(EnsPCoordsystem cs,
                                EnsPCoordsystemadaptor csa)
{
    if (!cs)
        return ajFalse;

    cs->Adaptor = csa;

    return ajTrue;
}




/* @func ensCoordsystemSetIdentifier ******************************************
**
** Set the SQL database-internal identifier member of an
** Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::Storable::dbID
** @param [u] cs [EnsPCoordsystem] Ensembl Coordinate System
** @param [r] identifier [ajuint] SQL database-internal identifier
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCoordsystemSetIdentifier(EnsPCoordsystem cs, ajuint identifier)
{
    if (!cs)
        return ajFalse;

    cs->Identifier = identifier;

    return ajTrue;
}




/* @section debugging *********************************************************
**
** Functions for reporting of an Ensembl Coordinate System object.
**
** @fdata [EnsPCoordsystem]
**
** @nam3rule Trace Report Ensembl Coordinate System members to debug file
**
** @argrule Trace cs [const EnsPCoordsystem] Ensembl Coordinate System
** @argrule Trace level [ajuint] Indentation level
**
** @valrule * [AjBool] ajTrue upon success, ajFalse otherwise
**
** @fcategory misc
******************************************************************************/




/* @func ensCoordsystemTrace **************************************************
**
** Trace an Ensembl Coordinate System.
**
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
** @param [r] level [ajuint] Indentation level
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCoordsystemTrace(const EnsPCoordsystem cs, ajuint level)
{
    AjPStr indent = NULL;

    if (!cs)
        return ajFalse;

    indent = ajStrNew();

    ajStrAppendCountK(&indent, ' ', level * 2);

    ajDebug("%SensCoordsystemTrace %p\n"
            "%S  Use %u\n"
            "%S  Identifier %u\n"
            "%S  Adaptor %p\n"
            "%S  Name '%S'\n"
            "%S  Version '%S'\n"
            "%S  SequenceLevel '%B'\n"
            "%S  Toplevel '%B'\n"
            "%S  Default '%B'\n"
            "%S  Rank %u\n",
            indent, cs,
            indent, cs->Use,
            indent, cs->Identifier,
            indent, cs->Adaptor,
            indent, cs->Name,
            indent, cs->Version,
            indent, cs->SequenceLevel,
            indent, cs->Toplevel,
            indent, cs->Default,
            indent, cs->Rank);

    ajStrDel(&indent);

    return ajTrue;
}




/* @section calculate *********************************************************
**
** Functions for calculating information from an
** Ensembl Coordinate System object.
**
** @fdata [EnsPCoordsystem]
**
** @nam3rule Calculate Calculate Ensembl Coordinate System information
** @nam4rule Memsize Calculate the memory size in bytes
**
** @argrule * cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @valrule Memsize [size_t] Memory size in bytes or 0
**
** @fcategory misc
******************************************************************************/




/* @func ensCoordsystemCalculateMemsize ***************************************
**
** Get the memory size in bytes of an Ensembl Coordinate System.
**
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @return [size_t] Memory size in bytes or 0
**
** @release 6.4.0
** @@
******************************************************************************/

size_t ensCoordsystemCalculateMemsize(const EnsPCoordsystem cs)
{
    size_t size = 0;

    if (!cs)
        return 0;

    size += sizeof (EnsOCoordsystem);

    if (cs->Name)
    {
        size += sizeof (AjOStr);

        size += ajStrGetRes(cs->Name);
    }

    if (cs->Version)
    {
        size += sizeof (AjOStr);

        size += ajStrGetRes(cs->Version);
    }

    return size;
}




/* @section comparison ********************************************************
**
** Functions for comparing Ensembl Coordinate System objects.
**
** @fdata [EnsPCoordsystem]
**
** @nam3rule  Match Compare two Ensembl Coordinate System objects
**
** @argrule * cs1 [const EnsPCoordsystem] Ensembl Coordinate System
** @argrule * cs2 [const EnsPCoordsystem] Ensembl Coordinate System
**
** @valrule * [AjBool] True on success
**
** @fcategory use
******************************************************************************/




/* @func ensCoordsystemMatch **************************************************
**
** Test for matching two Ensembl Coordinate System objects.
**
** @cc Bio::EnsEMBL::CoordSystem::equals
** @param [r] cs1 [const EnsPCoordsystem] First Ensembl Coordinate System
** @param [r] cs2 [const EnsPCoordsystem] Second Ensembl Coordinate System
**
** @return [AjBool] ajTrue if the Ensembl Coordinate System objects are equal
**
** @release 6.2.0
** @@
** The comparison is based on an initial pointer equality test and if that
** fails, a case-insensitive string comparison of the name and version members
** is performed.
******************************************************************************/

AjBool ensCoordsystemMatch(const EnsPCoordsystem cs1,
                           const EnsPCoordsystem cs2)
{
    if (ajDebugTest("ensCoordsystemMatch"))
    {
        ajDebug("ensCoordsystemMatch\n"
                "  cs1 %p\n"
                "  cs2 %p\n",
                cs1,
                cs2);

        ensCoordsystemTrace(cs1, 1);
        ensCoordsystemTrace(cs2, 1);
    }

    if (!cs1)
        return ajFalse;

    if (!cs2)
        return ajFalse;

    if (cs1 == cs2)
        return ajTrue;

    if (cs1->Identifier != cs2->Identifier)
        return ajFalse;

    if (!ajStrMatchCaseS(cs1->Name, cs2->Name))
        return ajFalse;

    if (!ajStrMatchCaseS(cs1->Version, cs2->Version))
        return ajFalse;

    if (cs1->Default != cs2->Default)
        return ajFalse;

    if (cs1->SequenceLevel != cs2->SequenceLevel)
        return ajFalse;

    if (cs1->Toplevel != cs2->Toplevel)
        return ajFalse;

    if (cs1->Rank != cs2->Rank)
        return ajFalse;

    return ajTrue;
}




/* @section convenience functions *********************************************
**
** Ensembl Coordinate System convenience functions
**
** @fdata [EnsPCoordsystem]
**
** @nam3rule Get Get member(s) of associated objects
** @nam4rule Species Get the Ensembl Database Adaptor species
**
** @argrule * cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @valrule Species [AjPStr] Ensembl Database Adaptor species or NULL
**
** @fcategory use
******************************************************************************/




/* @func ensCoordsystemGetSpecies *********************************************
**
** Get the species member of the Ensembl Database Adaptor the
** Ensembl Coordinate System Adaptor of an Ensembl Coordinate System is
** based on.
**
** @cc Bio::EnsEMBL::CoordSystem::species
** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
**
** @return [AjPStr] Ensembl Database Adaptor species or NULL
**
** @release 6.2.0
** @@
******************************************************************************/

AjPStr ensCoordsystemGetSpecies(const EnsPCoordsystem cs)
{
    return ensDatabaseadaptorGetSpecies(
        ensCoordsystemadaptorGetDatabaseadaptor(
            ensCoordsystemGetAdaptor(cs)));
}




/* @datasection [AjPList] AJAX List *******************************************
**
** @nam2rule List Functions for manipulating AJAX List objects
**
******************************************************************************/




/* @funcstatic listCoordsystemCompareIdentifierAscending **********************
**
** AJAX List of Ensembl Coordinate System objects comparison function to
** sort by Ensembl Coordinate System identifier in ascending order.
**
** @param [r] item1 [const void*] Ensembl Coordinate System address 1
** @param [r] item2 [const void*] Ensembl Coordinate System address 2
** @see ajListSort
**
** @return [int] The comparison function returns an integer less than,
**               equal to, or greater than zero if the first argument is
**               considered to be respectively less than, equal to, or
**               greater than the second.
**
** @release 6.4.0
** @@
******************************************************************************/

static int listCoordsystemCompareIdentifierAscending(
    const void *item1,
    const void *item2)
{
    EnsPCoordsystem cs1 = *(EnsOCoordsystem *const *) item1;
    EnsPCoordsystem cs2 = *(EnsOCoordsystem *const *) item2;

#if defined(AJ_DEBUG) && AJ_DEBUG >= 2
    if (ajDebugTest("listCoordsystemCompareIdentifierAscending"))
        ajDebug("listCoordsystemCompareIdentifierAscending\n"
                "  cs1 %p\n"
                "  cs2 %p\n",
                cs1,
                cs2);
#endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 2 */

    /* Sort empty values towards the end of the AJAX List. */

    if (cs1 && (!cs2))
        return -1;

    if ((!cs1) && (!cs2))
        return 0;

    if ((!cs1) && cs2)
        return +1;

    if (cs1->Identifier < cs2->Identifier)
        return -1;

    if (cs1->Identifier > cs2->Identifier)
        return +1;

    return 0;
}




/* @funcstatic listCoordsystemCompareIdentifierDescending *********************
**
** AJAX List of Ensembl Coordinate System objects comparison function to
** sort by Ensembl Coordinate System identifier in descending order.
**
** @param [r] item1 [const void*] Ensembl Coordinate System address 1
** @param [r] item2 [const void*] Ensembl Coordinate System address 2
** @see ajListSort
**
** @return [int] The comparison function returns an integer less than,
**               equal to, or greater than zero if the first argument is
**               considered to be respectively less than, equal to, or
**               greater than the second.
**
** @release 6.4.0
** @@
******************************************************************************/

static int listCoordsystemCompareIdentifierDescending(
    const void *item1,
    const void *item2)
{
    EnsPCoordsystem cs1 = *(EnsOCoordsystem *const *) item1;
    EnsPCoordsystem cs2 = *(EnsOCoordsystem *const *) item2;

#if defined(AJ_DEBUG) && AJ_DEBUG >= 2
    if (ajDebugTest("listCoordsystemCompareIdentifierDescending"))
        ajDebug("listCoordsystemCompareIdentifierDescending\n"
                "  cs1 %p\n"
                "  cs2 %p\n",
                cs1,
                cs2);
#endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 2 */

    /* Sort empty values towards the end of the AJAX List. */

    if (cs1 && (!cs2))
        return -1;

    if ((!cs1) && (!cs2))
        return 0;

    if ((!cs1) && cs2)
        return +1;

    if (cs1->Identifier > cs2->Identifier)
        return -1;

    if (cs1->Identifier < cs2->Identifier)
        return +1;

    return 0;
}




/* @funcstatic listCoordsystemCompareRankAscending ****************************
**
** AJAX List of Ensembl Coordinate System objects comparison function to
** sort by Ensembl Coordinate System rank in ascending order.
**
** @param [r] item1 [const void*] Ensembl Coordinate System address 1
** @param [r] item2 [const void*] Ensembl Coordinate System address 2
** @see ajListSort
**
** @return [int] The comparison function returns an integer less than,
**               equal to, or greater than zero if the first argument is
**               considered to be respectively less than, equal to, or
**               greater than the second.
**
** @release 6.4.0
** @@
******************************************************************************/

static int listCoordsystemCompareRankAscending(
    const void *item1,
    const void *item2)
{
    EnsPCoordsystem cs1 = *(EnsOCoordsystem *const *) item1;
    EnsPCoordsystem cs2 = *(EnsOCoordsystem *const *) item2;

#if defined(AJ_DEBUG) && AJ_DEBUG >= 2
    if (ajDebugTest("listCoordsystemCompareRankAscending"))
        ajDebug("listCoordsystemCompareRankAscending\n"
                "  cs1 %p\n"
                "  cs2 %p\n",
                cs1,
                cs2);
#endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 2 */

    /* Sort empty values towards the end of the AJAX List. */

    if (cs1 && (!cs2))
        return -1;

    if ((!cs1) && (!cs2))
        return 0;

    if ((!cs1) && cs2)
        return +1;

    if (cs1->Rank < cs2->Rank)
        return -1;

    if (cs1->Rank > cs2->Rank)
        return +1;

    return 0;
}




/* @funcstatic listCoordsystemCompareRankDescending ***************************
**
** AJAX List of Ensembl Coordinate System objects comparison function to
** sort by Ensembl Coordinate System rank in descending order.
**
** @param [r] item1 [const void*] Ensembl Coordinate System address 1
** @param [r] item2 [const void*] Ensembl Coordinate System address 2
** @see ajListSort
**
** @return [int] The comparison function returns an integer less than,
**               equal to, or greater than zero if the first argument is
**               considered to be respectively less than, equal to, or
**               greater than the second.
**
** @release 6.4.0
** @@
******************************************************************************/

static int listCoordsystemCompareRankDescending(
    const void *item1,
    const void *item2)
{
    EnsPCoordsystem cs1 = *(EnsOCoordsystem *const *) item1;
    EnsPCoordsystem cs2 = *(EnsOCoordsystem *const *) item2;

#if defined(AJ_DEBUG) && AJ_DEBUG >= 2
    if (ajDebugTest("listCoordsystemCompareRankDescending"))
        ajDebug("listCoordsystemCompareRankDescending\n"
                "  cs1 %p\n"
                "  cs2 %p\n",
                cs1,
                cs2);
#endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 2 */

    /* Sort empty values towards the end of the AJAX List. */

    if (cs1 && (!cs2))
        return -1;

    if ((!cs1) && (!cs2))
        return 0;

    if ((!cs1) && cs2)
        return +1;

    if (cs1->Rank > cs2->Rank)
        return -1;

    if (cs1->Rank < cs2->Rank)
        return +1;

    return 0;
}




/* @section list **************************************************************
**
** Functions for manipulating AJAX List objects.
**
** @fdata [AjPList]
**
** @nam3rule Coordsystem Functions for manipulating AJAX List objects of
** Ensembl Coordinate System objects
** @nam4rule Sort Sort functions
** @nam5rule Identifier Sort by Ensembl Coordinate System identifier member
** @nam5rule Name Sort by Ensembl Coordinate System name member
** @nam5rule Rank Sort by Ensembl Coordinate System rank member
** @nam6rule Ascending  Sort in ascending order
** @nam6rule Descending Sort in descending order
** @nam4rule Trace Trace Ensembl Coordinate System objects
**
** @argrule Sort css [AjPList]
** AJAX List of Ensembl Coordinate System objects
** @argrule Trace css [const AjPList]
** AJAX List of Ensembl Coordinate System objects
** @argrule Trace level [ajuint] Indentation level
**
** @valrule * [AjBool] ajTrue upon success, ajFalse otherwise
**
** @fcategory misc
******************************************************************************/




/* @func ensListCoordsystemSortIdentifierAscending ****************************
**
** Sort an AJAX List of Ensembl Coordinate System objects by their
** Ensembl Coordinate System identifier in ascending order.
**
** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.4.0
** @@
******************************************************************************/

AjBool ensListCoordsystemSortIdentifierAscending(AjPList css)
{
    if (!css)
        return ajFalse;

    ajListSort(css, &listCoordsystemCompareIdentifierAscending);

    return ajTrue;
}




/* @func ensListCoordsystemSortIdentifierDescending ***************************
**
** Sort an AJAX List of Ensembl Coordinate System objects by their
** Ensembl Coordinate System identifier in descending order.
**
** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.4.0
** @@
******************************************************************************/

AjBool ensListCoordsystemSortIdentifierDescending(AjPList css)
{
    if (!css)
        return ajFalse;

    ajListSort(css, &listCoordsystemCompareIdentifierDescending);

    return ajTrue;
}




/* @func ensListCoordsystemSortRankAscending **********************************
**
** Sort an AJAX List of Ensembl Coordinate System objects by their
** Ensembl Coordinate System rank in ascending order.
**
** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.4.0
** @@
******************************************************************************/

AjBool ensListCoordsystemSortRankAscending(AjPList css)
{
    if (!css)
        return ajFalse;

    ajListSort(css, &listCoordsystemCompareRankAscending);

    return ajTrue;
}




/* @func ensListCoordsystemSortRankDescending *********************************
**
** Sort an AJAX List of Ensembl Coordinate System objects by their
** Ensembl Coordinate System rank in descending order.
**
** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.4.0
** @@
******************************************************************************/

AjBool ensListCoordsystemSortRankDescending(AjPList css)
{
    if (!css)
        return ajFalse;

    ajListSort(css, &listCoordsystemCompareRankDescending);

    return ajTrue;
}




/* @func ensListCoordsystemTrace **********************************************
**
** Trace an Ensembl Coordinate System mapping path.
**
** @param [r] css [const AjPList] AJAX List of
** Ensembl Coordinate System objects
** @param [r] level [ajuint] Indentation level
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.4.0
** @@
******************************************************************************/

AjBool ensListCoordsystemTrace(const AjPList css, ajuint level)
{
    AjIList iter = NULL;

    AjPStr indent = NULL;

    EnsPCoordsystem cs = NULL;

    if (!css)
        return ajFalse;

    indent = ajStrNew();

    ajStrAppendCountK(&indent, ' ', level * 2);

    ajDebug("%SensListCoordsystemTrace %p\n"
            "%S  length %Lu\n",
            indent, css,
            indent, ajListGetLength(css));

    iter = ajListIterNewread(css);

    while (!ajListIterDone(iter))
    {
        cs = (EnsPCoordsystem) ajListIterGet(iter);

        if (cs)
            ensCoordsystemTrace(cs, level + 1);
        else
            ajDebug("%S  <nul>\n", indent);
    }

    ajListIterDel(&iter);

    ajStrDel(&indent);

    return ajTrue;
}




/* @datasection [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor ****
**
** @nam2rule Coordsystemadaptor Functions for manipulating
** Ensembl Coordinate System Adaptor objects
**
** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor
** @cc CVS Revision: 1.30
** @cc CVS Tag: branch-ensembl-68
**
******************************************************************************/





/* @funcstatic coordsystemadaptorFetchAllbyStatement **************************
**
** Run a SQL statement against an Ensembl Coordinate System Adaptor and
** consolidate the results into an AJAX List of Ensembl Coordinate System
** objects.
**
** The caller is responsible for deleting the
** Ensembl Coordinate System objects before deleting the AJAX List.
**
** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
** @param [r] statement [const AjPStr] SQL statement
** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.4.0
** @@
******************************************************************************/

static AjBool coordsystemadaptorFetchAllbyStatement(
    EnsPCoordsystemadaptor csa,
    const AjPStr statement,
    AjPList css)
{
    ajuint identifier = 0U;
    ajuint rank       = 0U;

    AjBool dflt     = AJFALSE;
    AjBool seqlevel = AJFALSE;
    AjBool toplevel = AJFALSE;

    AjPSqlstatement sqls = NULL;
    AjISqlrow sqli       = NULL;
    AjPSqlrow sqlr       = NULL;

    AjPStr name      = NULL;
    AjPStr version   = NULL;
    AjPStr attribute = NULL;
    AjPStr value     = NULL;

    AjPStrTok attrtoken = NULL;

    EnsPCoordsystem cs = NULL;

    EnsPDatabaseadaptor dba = NULL;

    if (!csa)
        return ajFalse;

    if (!statement)
        return ajFalse;

    if (!css)
        return ajFalse;

    dba = ensCoordsystemadaptorGetDatabaseadaptor(csa);

    sqls = ensDatabaseadaptorSqlstatementNew(dba, statement);

    sqli = ajSqlrowiterNew(sqls);

    while (!ajSqlrowiterDone(sqli))
    {
        identifier = 0;
        name       = ajStrNew();
        version    = ajStrNew();
        rank       = 0;
        attribute  = ajStrNew();

        value = ajStrNew();

        dflt     = ajFalse;
        toplevel = ajFalse;
        seqlevel = ajFalse;

        sqlr = ajSqlrowiterGet(sqli);

        ajSqlcolumnToUint(sqlr, &identifier);
        ajSqlcolumnToStr(sqlr, &name);
        ajSqlcolumnToStr(sqlr, &version);
        ajSqlcolumnToUint(sqlr, &rank);
        ajSqlcolumnToStr(sqlr, &attribute);

        attrtoken = ajStrTokenNewC(attribute, ",");

        while (ajStrTokenNextParse(attrtoken, &value))
        {
            if (ajStrMatchCaseC(value, "default_version"))
                dflt = ajTrue;

            if (ajStrMatchCaseC(value, "sequence_level"))
                seqlevel = ajTrue;
        }

        ajStrTokenDel(&attrtoken);

        cs = ensCoordsystemNewIni(csa,
                                  identifier,
                                  name,
                                  version,
                                  rank,
                                  dflt,
                                  toplevel,
                                  seqlevel);

        ajListPushAppend(css, (void *) cs);

        ajStrDel(&name);
        ajStrDel(&version);
        ajStrDel(&attribute);
        ajStrDel(&value);
    }

    ajSqlrowiterDel(&sqli);

    ensDatabaseadaptorSqlstatementDel(dba, &sqls);

    return ajTrue;
}




/* @funcstatic coordsystemadaptorCacheInit ************************************
**
** Initialise the internal Coordinate System cache of an
** Ensembl Coordinate System Adaptor.
**
** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.3.0
** @@
******************************************************************************/

static AjBool coordsystemadaptorCacheInit(EnsPCoordsystemadaptor csa)
{
    ajuint *Pidentifier = NULL;
    ajuint *Prank       = NULL;

    AjPList css = NULL;

    AjPStr statement = NULL;

    AjPTable versions = NULL;

    EnsPCoordsystem cs     = NULL;
    EnsPCoordsystem cstemp = NULL;

    if (!csa)
        return ajFalse;

    if (!csa->CacheByIdentifier)
    {
        csa->CacheByIdentifier = ajTableuintNew(0U);

        ajTableSetDestroyvalue(csa->CacheByIdentifier,
                               (void (*)(void **)) &ensCoordsystemDel);
    }

    if (!csa->CacheByName)
    {
        csa->CacheByName = ajTablestrNew(0U);

        ajTableSetDestroyvalue(csa->CacheByName,
                               (void (*)(void **)) &ajTableDel);
    }

    if (!csa->CacheByRank)
    {
        csa->CacheByRank = ajTableuintNew(0U);

        ajTableSetDestroyvalue(csa->CacheByRank,
                               (void (*)(void **)) &ensCoordsystemDel);
    }

    if (!csa->CacheByDefault)
    {
        csa->CacheByDefault = ajTableuintNew(0U);

        ajTableSetDestroyvalue(csa->CacheByDefault,
                               (void (*)(void **)) &ensCoordsystemDel);
    }

    statement = ajFmtStr(
        "SELECT "
        "coord_system.coord_system_id, "
        "coord_system.name, "
        "coord_system.version, "
        "coord_system.rank, "
        "coord_system.attrib "
        "FROM "
        "coord_system "
        "WHERE "
        "coord_system.species_id = %u",
        ensDatabaseadaptorGetIdentifier(
            ensCoordsystemadaptorGetDatabaseadaptor(csa)));

    css = ajListNew();

    coordsystemadaptorFetchAllbyStatement(csa, statement, css);

    while (ajListPop(css, (void **) &cs))
    {
        /* Sequence-level cache */

        if (cs->SequenceLevel)
            csa->Seqlevel = ensCoordsystemNewRef(cs);

        /* Identifier cache */

        AJNEW0(Pidentifier);

        *Pidentifier = cs->Identifier;

        cstemp = (EnsPCoordsystem)
            ajTablePut(csa->CacheByIdentifier,
                       (void *) Pidentifier,
                       (void *) ensCoordsystemNewRef(cs));

        if (cstemp)
        {
            ajWarn("coordsystemadaptorCacheInit got more than one "
                   "Ensembl Coordinate System with (PRIMARY KEY) identifier "
                   "%u.\n",
                   cstemp->Identifier);

            ensCoordsystemDel(&cstemp);
        }

        /* Name and Version cache */

        /*
        ** For each Coordinate System of a particular name one or more
        ** versions are supported. Thus, Ensembl Coordinate System objects
        ** are cached in two levels of AJAX Table objects.
        **
        ** First-level (Name) AJAX Table: An AJAX Table storing
        ** (Ensembl Coordinate System name) AJAX String objects as key data
        ** and second-level AJAX Table objects as value data.
        **
        ** Second-level (Version) AJAX Table: An AJAX Table storing
        ** (Ensembl Coordinate System version) AJAX String objects as key data
        ** and Ensembl Coordinate System objects as value data.
        */

        versions = (AjPTable) ajTableFetchmodS(csa->CacheByName, cs->Name);

        if (!versions)
        {
            /*
            ** Create a new AJAX Table of AJAX String (version) key and
            ** Ensembl Coordinate System value data.
            */

            versions = ajTablestrNew(0U);

            ajTableSetDestroyvalue(versions,
                                   (void (*)(void **)) &ensCoordsystemDel);

            ajTablePut(csa->CacheByName,
                       (void *) ajStrNewS(cs->Name),
                       (void *) versions);
        }

        cstemp = (EnsPCoordsystem)
            ajTablePut(versions,
                       (void *) ajStrNewS(cs->Version),
                       (void *) ensCoordsystemNewRef(cs));

        if (cstemp)
        {
            ajWarn("coordsystemadaptorCacheInit got more than one "
                   "Ensembl Coordinate System with (UNIQUE) name '%S' and "
                   "version '%S' with identifiers %u and %u.\n",
                   cstemp->Name,
                   cstemp->Version,
                   cstemp->Identifier,
                   cs->Identifier);

            ensCoordsystemDel(&cstemp);
        }

        /* Cache by Rank */

        AJNEW0(Prank);

        *Prank = cs->Rank;

        cstemp = (EnsPCoordsystem)
            ajTablePut(csa->CacheByRank,
                       (void *) Prank,
                       (void *) ensCoordsystemNewRef(cs));

        if (cstemp)
        {
            ajWarn("coordsystemadaptorCacheInit got more than one "
                   "Ensembl Coordinate System with (UNIQUE) rank %u and "
                   "identifiers %u and %u.\n",
                   cstemp->Rank,
                   cstemp->Identifier,
                   cs->Identifier);

            ensCoordsystemDel(&cstemp);
        }

        /* Defaults cache */

        /*
        ** Ensembl supports one or more default Coordinate System objects.
        ** Ensembl Coordinate System objects are stored in an AJAX Table
        ** with (Ensembl Coordinate System identifier) AJAX unsigned integer
        ** key data and Ensembl Coordinate System value data.
        */

        if (cs->Default)
        {
            AJNEW0(Pidentifier);

            *Pidentifier = cs->Identifier;

            cstemp = (EnsPCoordsystem)
                ajTablePut(csa->CacheByDefault,
                           (void *) Pidentifier,
                           (void *) ensCoordsystemNewRef(cs));

            if (cstemp)
            {
                ajWarn("coordsystemadaptorCacheInit got more than one "
                       "Ensembl Coordinate System with (PRIMARY KEY) "
                       "identifier %u.\n",
                       cstemp->Identifier);

                ensCoordsystemDel(&cstemp);
            }
        }

        /*
        ** All caches keep internal references to the
        ** Ensembl Coordinate System objects.
        */

        ensCoordsystemDel(&cs);
    }

    ajListFree(&css);

    ajStrDel(&statement);

    return ajTrue;
}




/* @funcstatic coordsystemadaptorMappingpathInit ******************************
**
** Initialise the internal Coordinate System mapping path cache of an
** Ensembl Coordinate System Adaptor.
**
** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.3.0
** @@
******************************************************************************/

static AjBool coordsystemadaptorMappingpathInit(EnsPCoordsystemadaptor csa)
{
    AjBool debug = AJFALSE;

    AjIList iter    = NULL;
    AjPList cskeys  = NULL;
    AjPList css     = NULL;
    AjPList mis     = NULL;
    AjPList mappath = NULL;

    AjPStr csname    = NULL;
    AjPStr csversion = NULL;
    AjPStr cskey     = NULL;
    AjPStr cs1key    = NULL;
    AjPStr cs2key    = NULL;
    AjPStr mapkey    = NULL;
    AjPStr metakey   = NULL;
    AjPStr metaval   = NULL;

    AjPStrTok cstoken   = NULL;
    AjPStrTok pathtoken = NULL;

    EnsPCoordsystem cs  = NULL;
    EnsPCoordsystem cs1 = NULL;
    EnsPCoordsystem cs2 = NULL;

    EnsPMetainformation mi = NULL;

    debug = ajDebugTest("coordsystemadaptorMappingpathInit");

    if (debug)
        ajDebug("coordsystemadaptorMappingpathInit\n"
                "  csa %p\n",
                csa);

    if (!csa)
        return ajFalse;

    if (!csa->MappingPaths)
    {
        csa->MappingPaths = ajTablestrNew(0U);

        ajTableSetDestroyvalue(
            csa->MappingPaths,
            (void (*)(void **)) &coordsystemadaptorMappingpathValdel);
    }

    cskeys = ajListNew();

    /* Read 'assembly.mapping' keys from the Ensembl Core 'meta' table. */

    metakey = ajStrNewC("assembly.mapping");

    mis = ajListNew();

    ensMetainformationadaptorFetchAllbyKey(
        ensRegistryGetMetainformationadaptor(
            ensCoordsystemadaptorGetDatabaseadaptor(csa)),
        metakey,
        mis);

    while (ajListPop(mis, (void **) &mi))
    {
        metaval = ensMetainformationGetValue(mi);

        if (debug)
            ajDebug("coordsystemadaptorMappingpathInit processing "
                    "Ensembl Meta Information value '%S'.\n",
                    metaval);

        /*
        ** Split 'assembly.mapping' Ensembl Meta Information values on
        ** '#' or '|' characters into Ensembl Coordinate System name:version
        ** keys.
        */

        pathtoken = ajStrTokenNewC(metaval, "#|");

        cskey = ajStrNew();

        while (ajStrTokenNextParse(pathtoken, &cskey))
            ajListPushAppend(cskeys, (void *) ajStrNewS(cskey));

        ajStrDel(&cskey);

        ajStrTokenDel(&pathtoken);

        if (ajListGetLength(cskeys) < 2)
            ajWarn("coordsystemadaptorMappingpathInit got the incorrectly "
                   "formatted 'assembly.mapping' value '%S' from the "
                   "Ensembl Core 'meta' table.",
                   metaval);
        else
        {
            /*
            ** Split Ensembl Coordinate System keys into names and versions and
            ** fetch the corresponding Ensembl Coordinate System objects from
            ** the database.
            */

            css = ajListNew();

            iter = ajListIterNew(cskeys);

            while (!ajListIterDone(iter))
            {
                cskey = (AjPStr) ajListIterGet(iter);

                cstoken = ajStrTokenNewC(cskey, ":");

                csname    = ajStrNew();
                csversion = ajStrNew();

                ajStrTokenNextParse(cstoken, &csname);
                ajStrTokenNextParse(cstoken, &csversion);

                ensCoordsystemadaptorFetchByName(csa,
                                                 csname,
                                                 csversion,
                                                 &cs);

                if (cs)
                    ajListPushAppend(css, (void *) cs);
                else
                    ajWarn("coordsystemadaptorMappingpathInit could not load "
                           "an Ensembl Coordinate System for name '%S' and "
                           "version '%S', as specified in the "
                           "Ensembl Core 'meta' table by '%S'.",
                           csname, csversion, metaval);

                ajStrDel(&csname);
                ajStrDel(&csversion);

                ajStrTokenDel(&cstoken);
            }

            ajListIterDel(&iter);

            /*
            ** Test the 'assembly.mapping' Meta-Information value for
            ** '#' characters. A '#' delimiter indicates a special case,
            ** where multiple parts of a 'component' region map to the
            ** same part of an 'assembled' region. As this looks like the
            ** 'long' mapping, we just make the mapping path a bit longer.
            */

            if ((ajStrFindC(metaval, "#") >= 0) && (ajListGetLength(css) == 2))
            {
                /*
                ** Insert an empty middle node into the mapping path
                ** i.e. the AJAX List of Ensembl Coordinate System objects.
                */

                iter = ajListIterNew(css);

                (void) ajListIterGet(iter);

                ajListIterInsert(iter, NULL);

                ajListIterDel(&iter);

                if (debug)
                    ajDebug("coordsystemadaptorMappingpathInit "
                            "elongated mapping path '%S'.\n",
                            metaval);
            }

            /*
            ** Take the first and last Ensembl Coordinate System objects from
            ** the AJAX List and generate name:version
            ** Ensembl Coordinate System keys, before
            ** a name1:version1|name2:version2 map key.
            */

            ajListPeekFirst(css, (void **) &cs1);
            ajListPeekLast(css, (void **) &cs2);

            if (debug)
            {
                ajDebug("coordsystemadaptorMappingpathInit cs1 %p\n", cs1);

                ensCoordsystemTrace(cs1, 1);

                ajDebug("coordsystemadaptorMappingpathInit cs2 %p\n", cs2);

                ensCoordsystemTrace(cs2, 1);
            }

            if (cs1 && cs2)
            {
                cs1key = ajFmtStr("%S:%S", cs1->Name, cs1->Version);
                cs2key = ajFmtStr("%S:%S", cs2->Name, cs2->Version);
                mapkey = ajFmtStr("%S|%S", cs1key, cs2key);

                if (debug)
                    ajDebug("coordsystemadaptorMappingpathInit mapkey '%S'\n",
                            mapkey);

                /* Does a mapping path already exist? */

                mappath = (AjPList) ajTableFetchmodS(csa->MappingPaths,
                                                     mapkey);

                if (mappath)
                {
                    /* A similar map path exists already. */

                    ajDebug("coordsystemadaptorMappingpathInit got multiple "
                            "mapping paths between Ensembl Coordinate System "
                            "objects '%S' and '%S' and chooses the shorter "
                            "mapping path arbitrarily.\n",
                            cs1key, cs2key);

                    if (ajListGetLength(css) < ajListGetLength(mappath))
                    {
                        /*
                        ** The current map path is shorter than the stored map
                        ** path. Replace the stored List with the current List
                        ** and delete the (longer) stored List. The Table key
                        ** String remains in place.
                        */

                        mappath = (AjPList) ajTablePut(
                            csa->MappingPaths,
                            (void *) ajStrNewS(mapkey),
                            (void *) css);

                        ajDebug("coordsystemadaptorMappingpathInit "
                                "deleted the longer, stored mapping path!\n");

                        while (ajListPop(mappath, (void **) &cs))
                            ensCoordsystemDel(&cs);

                        ajListFree(&mappath);
                    }
                    else
                    {
                        /*
                        ** The current mapping path is longer than the stored
                        ** mapping path. Delete this (longer) mapping path.
                        */

                        ajDebug("coordsystemadaptorMappingpathInit "
                                "deleted the longer, current mapping path!\n");

                        while (ajListPop(css, (void **) &cs))
                            ensCoordsystemDel(&cs);

                        ajListFree(&css);
                    }
                }
                else
                {
                    /* No similar mappath exists so store the new mappath. */

                    ajTablePut(csa->MappingPaths,
                               (void *) ajStrNewS(mapkey),
                               (void *) css);

                    if (debug)
                        ajDebug("coordsystemadaptorMappingpathInit "
                                "added a new mapping path '%S'.\n",
                                mapkey);
                }

                ajStrDel(&cs1key);
                ajStrDel(&cs2key);
                ajStrDel(&mapkey);
            }
            else
                ajWarn("coordsystemadaptorMappingpathInit requires that both, "
                       "first and last Ensembl Coordinate System objects of a "
                       "mapping path are defined. "
                       "See Ensembl Core 'meta.meta_value' '%S'.",
                       metaval);
        }

        /*
        ** Clear the AJAX List of Coordinate System keys, but keep the
        ** AJAX List between Ensembl Meta Information entries.
        */

        while (ajListPop(cskeys, (void **) &cskey))
            ajStrDel(&cskey);

        ensMetainformationDel(&mi);
    }

    ajListFree(&mis);
    ajListFree(&cskeys);

    ajStrDel(&metakey);

    return ajTrue;
}




/* @funcstatic coordsystemadaptorSeqregionMapInit *****************************
**
** Initialise the Ensembl Coordinate System Adaptor-internal
** Ensembl Sequence Region mapping cache. This allows mapping of internal to
** external Sequence Region identifiers and vice versa.
**
** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.3.0
** @@
******************************************************************************/

static AjBool coordsystemadaptorSeqregionMapInit(EnsPCoordsystemadaptor csa)
{
    ajuint internal = 0U;
    ajuint external = 0U;

    ajuint *Pinternal = NULL;
    ajuint *Pexternal = NULL;
    ajuint *Pprevious = NULL;

    AjBool debug = AJFALSE;

    AjPSqlstatement sqls = NULL;
    AjISqlrow sqli       = NULL;
    AjPSqlrow sqlr       = NULL;

    AjPStr build = NULL;
    AjPStr statement = NULL;

    EnsPDatabaseadaptor dba = NULL;

    debug = ajDebugTest("coordsystemadaptorSeqregionMapInit");

    if (debug)
        ajDebug("coordsystemadaptorSeqregionMapInit\n"
                "  csa %p\n",
                csa);

    if (!csa)
        return ajFalse;

    if (csa->ExternalToInternal)
        return ajFalse;
    else
    {
        csa->ExternalToInternal = ajTableuintNew(0U);

        ajTableSetDestroyvalue(csa->ExternalToInternal, &ajMemFree);
    }

    if (csa->InternalToExternal)
        return ajFalse;
    else
    {
        csa->InternalToExternal = ajTableuintNew(0U);

        ajTableSetDestroyvalue(csa->InternalToExternal, &ajMemFree);
    }

    dba = ensCoordsystemadaptorGetDatabaseadaptor(csa);

    build = ajStrNew();

    ensDatabaseadaptorFetchSchemabuild(dba, &build);

    if (debug)
        ajDebug("coordsystemadaptorSeqregionMapInit got build '%S'.\n", build);

    /* Get the relations for the current database. */

    statement = ajFmtStr(
        "SELECT "
        "seq_region_mapping.internal_seq_region_id, "
        "seq_region_mapping.external_seq_region_id "
        "FROM "
        "mapping_set, "
        "seq_region_mapping, "
        "seq_region, "
        "coord_system "
        "WHERE "
        "mapping_set.schema_build = '%S' "
        "AND "
        "mapping_set.mapping_set_id = seq_region_mapping.mapping_set_id "
        "AND "
        "seq_region_mapping.internal_seq_region_id = seq_region.seq_region_id "
        "AND "
        "seq_region.coord_system_id = coord_system.coord_system_id "
        "AND "
        "coord_system.species_id = %u",
        build,
        ensDatabaseadaptorGetIdentifier(dba));

    ajStrDel(&build);

    sqls = ensDatabaseadaptorSqlstatementNew(dba, statement);

    sqli = ajSqlrowiterNew(sqls);

    while (!ajSqlrowiterDone(sqli))
    {
        internal = 0U;
        external = 0U;

        sqlr = ajSqlrowiterGet(sqli);

        ajSqlcolumnToUint(sqlr, &internal);
        ajSqlcolumnToUint(sqlr, &external);

        /* Internal to external identifier mapping. */

        Pprevious = (ajuint *) ajTableFetchmodV(csa->InternalToExternal,
                                                (const void *) &internal);

        if (Pprevious)
            ajDebug("coordsystemadaptorSeqregionMapInit got duplicate "
                    "internal Sequence Region identifier:\n"
                    "%u -> %u\n"
                    "%u -> %u\n",
                    internal, *Pprevious,
                    internal, external);
        else
        {
            AJNEW0(Pinternal);
            AJNEW0(Pexternal);

            *Pinternal = internal;
            *Pexternal = external;

            ajTablePut(csa->InternalToExternal,
                       (void *) Pinternal,
                       (void *) Pexternal);
        }

        /* External to internal identifier mapping. */

        Pprevious = (ajuint *) ajTableFetchmodV(csa->ExternalToInternal,
                                                (const void *) &external);

        if (Pprevious)
            ajDebug("coordsystemadaptorSeqregionMapInit got duplicate "
                    "external Sequence Region identifier:\n"
                    "%u -> %u\n"
                    "%u -> %u\n",
                    external, *Pprevious,
                    external, internal);
        else
        {
            AJNEW0(Pinternal);
            AJNEW0(Pexternal);

            *Pinternal = internal;
            *Pexternal = external;

            ajTablePut(csa->ExternalToInternal,
                       (void *) Pexternal,
                       (void *) Pinternal);
        }
    }

    ajSqlrowiterDel(&sqli);

    ensDatabaseadaptorSqlstatementDel(dba, &sqls);

    ajStrDel(&statement);

    return ajTrue;
}




/* @section constructors ******************************************************
**
** All constructors return a new Ensembl Coordinate System Adaptor by pointer.
** It is the responsibility of the user to first destroy any previous
** Coordinate System Adaptor. The target pointer does not need to be
** initialised to NULL, but it is good programming practice to do so anyway.
**
** @fdata [EnsPCoordsystemadaptor]
**
** @nam3rule New Constructor
**
** @argrule New dba [EnsPDatabaseadaptor] Ensembl Database Adaptor
**
** @valrule * [EnsPCoordsystemadaptor]
** Ensembl Coordinate System Adaptor or NULL
**
** @fcategory new
******************************************************************************/




/* @func ensCoordsystemadaptorNew *********************************************
**
** Default constructor for an Ensembl Coordinate System Adaptor.
**
** Ensembl Object Adaptors are singleton objects in the sense that a single
** instance of an Ensembl Object Adaptor connected to a particular database is
** sufficient to instantiate any number of Ensembl Objects from the database.
** Each Ensembl Object will have a weak reference to the Object Adaptor that
** instantiated it. Therefore, Ensembl Object Adaptors should not be
** instantiated directly, but rather obtained from the Ensembl Registry,
** which will in turn call this function if neccessary.
**
** @see ensRegistryGetDatabaseadaptor
** @see ensRegistryGetCoordsystemadaptor
**
** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::new
** @param [u] dba [EnsPDatabaseadaptor] Ensembl Database Adaptor
**
** @return [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor or NULL
**
** @release 6.2.0
** @@
******************************************************************************/

EnsPCoordsystemadaptor ensCoordsystemadaptorNew(
    EnsPDatabaseadaptor dba)
{
    EnsPCoordsystemadaptor csa = NULL;

    if (!dba)
        return NULL;

    AJNEW0(csa);

    csa->Adaptor = dba;

    coordsystemadaptorCacheInit(csa);

    coordsystemadaptorMappingpathInit(csa);

    coordsystemadaptorSeqregionMapInit(csa);

    /*
    ** Create a Pseudo-Coordinate System 'toplevel' and cache it so that only
    ** one of these is created for each database.
    */

    csa->Toplevel = ensCoordsystemNewIni(
        csa,
        0,
        (AjPStr) NULL,
        (AjPStr) NULL,
        0,
        ajFalse,
        ajTrue,
        ajFalse);

    return csa;
}




/* @funcstatic coordsystemadaptorMappingpathValdel ****************************
**
** An ajTableSetDestroyvalue "valdel" function to clear AJAX Table value data.
** This function removes and deletes Ensembl Coordinate System objects
** from an AJAX List object, before deleting the AJAX List object.
**
** @param [d] Pvalue [void**] AJAX List address
** @see ajTableSetDestroyvalue
**
** @return [void]
**
** @release 6.3.0
** @@
******************************************************************************/

static void coordsystemadaptorMappingpathValdel(void **Pvalue)
{
    EnsPCoordsystem cs = NULL;

    if (!Pvalue)
        return;

    if (!*Pvalue)
        return;

    while (ajListPop(*((AjPList *) Pvalue), (void **) &cs))
        ensCoordsystemDel(&cs);

    ajListFree((AjPList *) Pvalue);

    return;
}




/* @section destructors *******************************************************
**
** Destruction destroys all internal data structures and frees the memory
** allocated for an Ensembl Coordinate System Adaptor object.
**
** @fdata [EnsPCoordsystemadaptor]
**
** @nam3rule Del Destroy (free) an Ensembl Coordinate System Adaptor
**
** @argrule * Pcsa [EnsPCoordsystemadaptor*]
** Ensembl Coordinate System Adaptor address
**
** @valrule * [void]
**
** @fcategory delete
******************************************************************************/




/* @func ensCoordsystemadaptorDel *********************************************
**
** Default destructor for an Ensembl Coordinate System Adaptor.
**
** This function also clears the internal Ensembl Coordinate System and
** mapping path caches.
**
** Ensembl Object Adaptors are singleton objects that are registered in the
** Ensembl Registry and weakly referenced by Ensembl Objects that have been
** instantiated by it. Therefore, Ensembl Object Adaptors should never be
** destroyed directly. Upon exit, the Ensembl Registry will call this function
** if required.
**
** @param [d] Pcsa [EnsPCoordsystemadaptor*]
** Ensembl Coordinate System Adaptor address
**
** @return [void]
**
** @release 6.2.0
** @@
******************************************************************************/

void ensCoordsystemadaptorDel(EnsPCoordsystemadaptor *Pcsa)
{
    EnsPCoordsystemadaptor pthis = NULL;

    if (!Pcsa)
        return;

#if defined(AJ_DEBUG) && AJ_DEBUG >= 1
    if (ajDebugTest("ensCoordsystemadaptorDel"))
        ajDebug("ensCoordsystemadaptorDel\n"
                "  *Pcsa %p\n",
                *Pcsa);
#endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 1 */

    if (!(pthis = *Pcsa))
        return;

    ajTableDel(&pthis->CacheByIdentifier);
    ajTableDel(&pthis->CacheByName);
    ajTableDel(&pthis->CacheByRank);
    ajTableDel(&pthis->CacheByDefault);
    ajTableDel(&pthis->MappingPaths);
    ajTableDel(&pthis->ExternalToInternal);
    ajTableDel(&pthis->InternalToExternal);

    ensCoordsystemDel(&pthis->Seqlevel);
    ensCoordsystemDel(&pthis->Toplevel);

    ajMemFree((void **) Pcsa);

    return;
}




/* @section member retrieval **************************************************
**
** Functions for returning members of an Ensembl Coordinate System Adaptor
** object.
**
** @fdata [EnsPCoordsystemadaptor]
**
** @nam3rule Get Return Coordinate System Adaptor attribute(s)
** @nam4rule Databaseadaptor Return the Ensembl Database Adaptor
**
** @argrule * csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
**
** @valrule Databaseadaptor [EnsPDatabaseadaptor]
** Ensembl Database Adaptor or NULL
**
** @fcategory use
******************************************************************************/




/* @func ensCoordsystemadaptorGetDatabaseadaptor ******************************
**
** Get the Ensembl Database Adaptor member of an
** Ensembl Coordinate System Adaptor.
**
** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
**
** @return [EnsPDatabaseadaptor] Ensembl Database Adaptor or NULL
**
** @release 6.2.0
** @@
******************************************************************************/

EnsPDatabaseadaptor ensCoordsystemadaptorGetDatabaseadaptor(
    EnsPCoordsystemadaptor csa)
{
    return (csa) ? csa->Adaptor : NULL;
}




/* @section object fetching ***************************************************
**
** Functions for fetching Ensembl Coordinate System objects from an
** Ensembl SQL database.
**
** @fdata [EnsPCoordsystemadaptor]
**
** @nam3rule Fetch Fetch Ensembl Coordinate System object(s)
** @nam4rule All   Fetch all Ensembl Coordinate System objects
** @nam4rule Allby Fetch all Ensembl Coordinate System objects
**                 matching a criterion
** @nam5rule Name  Fetch all by a name
** @nam4rule By    Fetch one Ensembl Coordinate System object
**                 matching a criterion
** @nam5rule Identifier Fetch by an SQL database-internal identifier
** @nam5rule Name  Fetch by a name
** @nam5rule Rank  Fetch by a rank
** @nam4rule Seqlevel Fetch the sequence-level Ensembl Coordinate System
** @nam4rule Toplevel Fetch the top-level Ensembl Coordinate System
**
** @argrule * csa [const EnsPCoordsystemadaptor]
** Ensembl Coordinate System Adaptor
** @argrule All css [AjPList] AJAX List of Ensembl Coordinate System objects
** @argrule AllbyName name [const AjPStr] Name
** @argrule Allby css [AjPList] AJAX List of Ensembl Coordinate System objects
** @argrule ByIdentifier identifier [ajuint] SQL database-internal identifier
** @argrule ByIdentifier Pcs [EnsPCoordsystem*] Ensembl Coordinate System
** address
** @argrule ByName name [const AjPStr] Name
** @argrule ByName version [const AjPStr] Version
** @argrule ByName Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
** @argrule ByRank rank [ajuint] Rank
** @argrule ByRank Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
** @argrule Seqlevel Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
** @argrule Toplevel Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
**
** @valrule * [AjBool] ajTrue upon success, ajFalse otherwise
**
** @fcategory use
******************************************************************************/




/* @funcstatic coordsystemadaptorFetchAll *************************************
**
** An ajTableMap "apply" function to return all Ensembl Coordinate System
** objects from an Ensembl Coordinate System Adaptor-internal cache.
**
** The caller is responsible for deleting the Ensembl Coordinate System
** objects before deleting the AJAX List.
**
** @param [u] key [const void*] AJAX unsigned integer key data address
** @param [u] Pvalue [void**] Ensembl Coordinate System value data address
** @param [u] cl [void*] AJAX List of Ensembl Coordinate System objects,
**                       passed in via ajTableMap
** @see ajTableMap
**
** @return [void]
**
** @release 6.3.0
** @@
******************************************************************************/

static void coordsystemadaptorFetchAll(const void *key,
                                       void **Pvalue,
                                       void *cl)
{
    if (!key)
        return;

    if (!Pvalue)
        return;

    if (!*Pvalue)
        return;

    if (!cl)
        return;

    ajListPushAppend((AjPList) cl, (void *)
                     ensCoordsystemNewRef(*((EnsPCoordsystem *) Pvalue)));

    return;
}




/* @func ensCoordsystemadaptorFetchAll ****************************************
**
** Fetch all Ensembl Coordinate System objects in the order of ascending rank.
**
** The caller is responsible for deleting the Ensembl Coordinate System
** objects before deleting the AJAX List.
**
** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_all
** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
**                                               System Adaptor
** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCoordsystemadaptorFetchAll(const EnsPCoordsystemadaptor csa,
                                     AjPList css)
{
    if (!csa)
        return ajFalse;

    if (!css)
        return ajFalse;

    ajTableMap(csa->CacheByIdentifier,
               &coordsystemadaptorFetchAll,
               (void *) css);

    ensListCoordsystemSortRankAscending(css);

    return ajTrue;
}




/* @func ensCoordsystemadaptorFetchAllbyName **********************************
**
** Fetch Ensembl Coordinate System objects of all versions for a name.
**
** The caller is responsible for deleting the Ensembl Coordinate System
** objects before deleting the AJAX List.
**
** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_all_by_name
** @param [r] csa [const EnsPCoordsystemadaptor]
** Ensembl Coordinate System Adaptor
** @param [r] name [const AjPStr] Name
** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.4.0
** @@
******************************************************************************/

AjBool ensCoordsystemadaptorFetchAllbyName(
    const EnsPCoordsystemadaptor csa,
    const AjPStr name,
    AjPList css)
{
    AjPTable versions = NULL;

    EnsPCoordsystem cs = NULL;

    if (!csa)
        return ajFalse;

    if (!name)
        return ajFalse;

    if (!css)
        return ajFalse;

    if (ajStrMatchCaseC(name, "seqlevel"))
    {
        ensCoordsystemadaptorFetchSeqlevel(csa, &cs);

        ajListPushAppend(css, (void *) cs);

        return ajTrue;
    }

    if (ajStrMatchCaseC(name, "toplevel"))
    {
        ensCoordsystemadaptorFetchToplevel(csa, &cs);

        ajListPushAppend(css, (void *) cs);

        return ajTrue;
    }

    versions = (AjPTable) ajTableFetchmodS(csa->CacheByName, name);

    if (versions)
        ajTableMap(versions, &coordsystemadaptorFetchAll, (void *) css);

    ensListCoordsystemSortRankAscending(css);

    return ajTrue;
}




/* @func ensCoordsystemadaptorFetchByIdentifier *******************************
**
** Fetch an Ensembl Coordinate System by its SQL database-internal identifier.
** The caller is responsible for deleting the Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_by_dbID
** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
**                                               System Adaptor
** @param [r] identifier [ajuint] SQL database-internal identifier
** @param [wP] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCoordsystemadaptorFetchByIdentifier(
    const EnsPCoordsystemadaptor csa,
    ajuint identifier,
    EnsPCoordsystem *Pcs)
{
    if (!csa)
        return ajFalse;

    if (!identifier)
        return ajFalse;

    if (!Pcs)
        return ajFalse;

    *Pcs = (EnsPCoordsystem) ajTableFetchmodV(csa->CacheByIdentifier,
                                              (const void *) &identifier);

    ensCoordsystemNewRef(*Pcs);

    return ajTrue;
}




/* @func ensCoordsystemadaptorFetchByName *************************************
**
** Fetch an Ensembl Coordinate System by name and version.
** The caller is responsible for deleting the Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_by_name
** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
**                                               System Adaptor
** @param [r] name [const AjPStr] Name
** @param [rN] version [const AjPStr] Version
** @param [wP] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCoordsystemadaptorFetchByName(
    const EnsPCoordsystemadaptor csa,
    const AjPStr name,
    const AjPStr version,
    EnsPCoordsystem *Pcs)
{
    void **valarray = NULL;

    register ajuint i = 0U;

    AjPTable versions = NULL;

    EnsPCoordsystem cs = NULL;

    if (ajDebugTest("ensCoordsystemadaptorFetchByName"))
        ajDebug("ensCoordsystemadaptorFetchByName\n"
                "  csa %p\n"
                "  name '%S'\n"
                "  version '%S'\n"
                "  Pcs %p\n",
                csa,
                name,
                version,
                Pcs);

    if (!csa)
        return ajFalse;

    if (!name)
        return ajFalse;

    if (!Pcs)
        return ajFalse;

    *Pcs = NULL;

    if (ajStrMatchCaseC(name, "seqlevel"))
        return ensCoordsystemadaptorFetchSeqlevel(csa, Pcs);

    if (ajStrMatchCaseC(name, "toplevel"))
        return ensCoordsystemadaptorFetchToplevel(csa, Pcs);

    versions = (AjPTable) ajTableFetchmodS(csa->CacheByName, name);

    if (versions)
    {
        if (version && ajStrGetLen(version))
            *Pcs = (EnsPCoordsystem) ajTableFetchmodS(versions, version);
        else
        {
            /*
            ** If no version has been specified search for the default
            ** Ensembl Coordinate System of this name.
            */

            ajTableToarrayValues(versions, &valarray);

            for (i = 0U; valarray[i]; i++)
            {
                cs = (EnsPCoordsystem) valarray[i];

                if (cs->Default)
                {
                    *Pcs = cs;

                    break;
                }
            }

            AJFREE(valarray);
        }

        ensCoordsystemNewRef(*Pcs);

        return ajTrue;
    }

    return ajTrue;
}




/* @func ensCoordsystemadaptorFetchByRank *************************************
**
** Fetch an Ensembl Coordinate System by its rank.
** The caller is responsible for deleting the Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_by_rank
** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
**                                               System Adaptor
** @param [r] rank [ajuint] Rank
** @param [wP] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCoordsystemadaptorFetchByRank(
    const EnsPCoordsystemadaptor csa,
    ajuint rank,
    EnsPCoordsystem *Pcs)
{
    if (!csa)
        return ajFalse;

    if (!rank)
        return ensCoordsystemadaptorFetchToplevel(csa, Pcs);

    if (!Pcs)
        return ajFalse;

    *Pcs = (EnsPCoordsystem) ajTableFetchmodV(csa->CacheByRank,
                                              (const void *) &rank);

    ensCoordsystemNewRef(*Pcs);

    return ajTrue;
}




/* @func ensCoordsystemadaptorFetchSeqlevel ***********************************
**
** Fetch the sequence-level Ensembl Coordinate System.
** The caller is responsible for deleting the Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_sequence_level
** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
**                                               System Adaptor
** @param [wP] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.4.0
** @@
******************************************************************************/

AjBool ensCoordsystemadaptorFetchSeqlevel(
    const EnsPCoordsystemadaptor csa,
    EnsPCoordsystem *Pcs)
{
    if (!csa)
        return ajFalse;

    if (!Pcs)
        return ajFalse;

    *Pcs = ensCoordsystemNewRef(csa->Seqlevel);

    return ajTrue;
}




/* @func ensCoordsystemadaptorFetchToplevel ***********************************
**
** Fetch the top-level Ensembl Coordinate System.
** The caller is responsible for deleting the Ensembl Coordinate System.
**
** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_top_level
** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
**                                               System Adaptor
** @param [wP] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.4.0
** @@
******************************************************************************/

AjBool ensCoordsystemadaptorFetchToplevel(
    const EnsPCoordsystemadaptor csa,
    EnsPCoordsystem *Pcs)
{
    if (!csa)
        return ajFalse;

    if (!Pcs)
        return ajFalse;

    *Pcs = ensCoordsystemNewRef(csa->Toplevel);

    return ajTrue;
}




/* @section member retrieval **************************************************
**
** Functions for returning members of an Ensembl Coordinate System Adaptor
** object.
**
** @fdata [EnsPCoordsystemadaptor]
**
** @nam3rule Get Return Coordinate System Adaptor attribute(s)
** @nam4rule Mappingpath Return the Ensembl Coordinate System mapping path
** @nam4rule Seqregionidentifier Return an Ensembl Sequence Region identifier
** @nam5rule External Return the external identifier
** @nam5rule Internal Return the internal identifier
**
** @argrule * csa [const EnsPCoordsystemadaptor] Coordinate System Adaptor
** @argrule Mappingpath cs1 [EnsPCoordsystem] First Ensembl Coordinate System
** @argrule Mappingpath cs2 [EnsPCoordsystem] Second Ensembl Coordinate System
** @argrule External srid [ajuint] Internal Ensembl Sequence Region identifier
** @argrule Internal srid [ajuint] External Ensembl Sequence Region identifier
**
** @valrule Mappingpath [const AjPList] AJAX List of
** Ensembl Coordinate System objects or NULL
** @valrule External [ajuint] External Ensembl Sequence Region identifier or 0U
** @valrule Internal [ajuint] Internal Ensembl Sequence Region identifier or 0U
**
** @fcategory use
******************************************************************************/




/* @func ensCoordsystemadaptorGetMappingpath **********************************
**
** Fetch a mapping path between two Ensembl Coordinate System objects.
**
** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::get_mapping_path
** @param [r] csa [const EnsPCoordsystemadaptor]
** Ensembl Coordinate System Adaptor
** @param [u] cs1 [EnsPCoordsystem] First Ensembl Coordinate System
** @param [u] cs2 [EnsPCoordsystem] Second Ensembl Coordinate System
**
** @return [const AjPList]
** AJAX List of Ensembl Coordinate System objects or NULL
**
** @release 6.4.0
** @@
** Given two Ensembl Coordinate System objects this will return a mapping path
** between them if one has been defined. Allowed mapping paths are explicitly
** defined in the 'meta' table. The following is an example:
**
** mysql> select * from meta where meta_key = 'assembly.mapping';
**
**   +---------+------------------+--------------------------------------+
**   | meta_id | meta_key         | meta_value                           |
**   +---------+------------------+--------------------------------------+
**   |      20 | assembly.mapping | chromosome:NCBI34|contig             |
**   |      21 | assembly.mapping | clone|contig                         |
**   |      22 | assembly.mapping | supercontig|contig                   |
**   |      23 | assembly.mapping | chromosome:NCBI34|contig|clone       |
**   |      24 | assembly.mapping | chromosome:NCBI34|contig|supercontig |
**   |      25 | assembly.mapping | supercontig|contig|clone             |
**   +---------+------------------+--------------------------------------+
**
** For a one-step mapping path to be valid there needs to be a relationship
** between the two Ensembl Coordinate System objects defined in the assembly
** table. Two step mapping paths work by building on the one-step mapping
** paths, which are already defined.
**
** The first Ensembl Coordinate System in a one step mapping path must be the
** assembled Ensembl Coordinate System and the second must be the component.
**
** A '#' delimiter indicates a special case where multiple parts of a
** 'component' region map to the same part of an 'assembled' region.
** In those cases, the mapping path is elongated by an empty middle node.
******************************************************************************/

const AjPList ensCoordsystemadaptorGetMappingpath(
    const EnsPCoordsystemadaptor csa,
    EnsPCoordsystem cs1,
    EnsPCoordsystem cs2)
{
    void **keyarray = NULL;
    void **valarray = NULL;

    register ajuint i = 0U;

    ajint match = 0;

    AjBool debug = AJFALSE;

    AjPList mappath = NULL;
    AjPList midpath = NULL;
    AjPList tmppath = NULL;

    AjPStr cs1key = NULL;
    AjPStr cs2key = NULL;
    AjPStr midkey = NULL;
    AjPStr mapkey = NULL;

    AjPStr cs1str = NULL;
    AjPStr cs2str = NULL;
    AjPStr midstr = NULL;

    AjPTable midcs1 = NULL;
    AjPTable midcs2 = NULL;

    EnsPCoordsystem csp0 = NULL;
    EnsPCoordsystem csp1 = NULL;
    EnsPCoordsystem midcs = NULL;

    debug = ajDebugTest("ensCoordsystemadaptorGetMappingpath");

    if (debug)
    {
        ajDebug("ensCoordsystemadaptorGetMappingpath\n"
                "  csa %p\n"
                "  cs1 %p\n"
                "  cs2 %p\n",
                csa,
                cs1,
                cs2);

        ensCoordsystemTrace(cs1, 1);
        ensCoordsystemTrace(cs2, 1);
    }

    if (!csa)
        return NULL;

    if (!cs1)
        return NULL;

    if (!cs2)
        return NULL;

    cs1key = ajFmtStr("%S:%S", cs1->Name, cs1->Version);
    cs2key = ajFmtStr("%S:%S", cs2->Name, cs2->Version);

    /* Lookup path for cs1key|cs2key. */

    mapkey = ajFmtStr("%S|%S", cs1key, cs2key);

    mappath = (AjPList) ajTableFetchmodV(csa->MappingPaths,
                                         (const void *) mapkey);

    ajStrDel(&mapkey);

    if (mappath && ajListGetLength(mappath))
    {
        ajStrDel(&cs1key);
        ajStrDel(&cs2key);

        return mappath;
    }

    /* Lookup path for cs2key|cs1key. */

    mapkey = ajFmtStr("%S|%S", cs2key, cs1key);

    mappath = (AjPList) ajTableFetchmodV(csa->MappingPaths,
                                         (const void *) mapkey);

    ajStrDel(&mapkey);

    if (mappath && ajListGetLength(mappath))
    {
        ajStrDel(&cs1key);
        ajStrDel(&cs2key);

        return mappath;
    }

    /*
    ** Still no success. Search for a chained mapping path involving two
    ** coordinate system pairs with a shared middle coordinate system.
    */

    if (debug)
        ajDebug("ensCoordsystemadaptorGetMappingpath got no explicit mapping "
                "path between Ensembl Coordinate System objects "
                "'%S' and '%S'.\n",
                cs1key, cs2key);

    /*
    ** Initialise temporary AJAX Table objects of AJAX String key and
    ** AJAX List of Ensembl Coordinate System objects value data.
    */

    midcs1 = ajTablestrNew(0U);
    midcs2 = ajTablestrNew(0U);

    ajTableSetDestroyvalue(
        midcs1,
        (void (*)(void **)) &coordsystemadaptorMappingpathValdel);
    ajTableSetDestroyvalue(
        midcs2,
        (void (*)(void **)) &coordsystemadaptorMappingpathValdel);

    /*
    ** Iterate over all mapping paths stored in the
    ** Coordinate System Adaptor.
    */

    ajTableToarrayKeysValues(csa->MappingPaths, &keyarray, &valarray);

    for (i = 0U; keyarray[i]; i++)
    {
        tmppath = (AjPList) valarray[i];

        /* Consider only paths of two components. */

        if (ajListGetLength(tmppath) != 2)
            continue;

        ajListPeekNumber(tmppath, 0, (void **) &csp0);
        ajListPeekNumber(tmppath, 1, (void **) &csp1);

        match = -1;

        if (ensCoordsystemMatch(csp0, cs1))
            match = 1;

        if (ensCoordsystemMatch(csp1, cs1))
            match = 0;

        if (match >= 0)
        {
            ajListPeekNumber(tmppath, (ajuint) match, (void **) &midcs);

            midkey = ajFmtStr("%S:%S", midcs->Name, midcs->Version);

            if (ajTableMatchS(midcs2, midkey))
            {
                mapkey = ajFmtStr("%S|%S", cs1key, cs2key);

                midpath = ajListNew();

                ajListPushAppend(midpath,
                                 (void *) ensCoordsystemNewRef(cs1));

                ajListPushAppend(midpath,
                                 (void *) ensCoordsystemNewRef(midcs));

                ajListPushAppend(midpath,
                                 (void *) ensCoordsystemNewRef(cs2));

                ajTablePut(csa->MappingPaths,
                           (void *) mapkey,
                           (void *) midpath);

                mappath = midpath;

                /*
                ** Inform the user that an explicit assembly.mapping entry is
                ** missing from the Ensembl Core meta table.
                */

                cs1str = (ajStrGetLen(cs1->Version)) ?
                    ajFmtStr("%S:%S", cs1->Name, cs1->Version) :
                    ajStrNewS(cs1->Name);

                midstr = (ajStrGetLen(midcs->Version)) ?
                    ajFmtStr("%S:%S", midcs->Name, midcs->Version) :
                    ajStrNewS(midcs->Name);

                cs2str = (ajStrGetLen(cs2->Version)) ?
                    ajFmtStr("%S:%S", cs2->Name, cs2->Version) :
                    ajStrNewS(cs2->Name);

                ajWarn("ensCoordsystemadaptorGetMappingpath uses an implicit"
                       "mapping path between "
                       "Ensembl Coordinate System objects '%S' and '%S'.\n"
                       "An explicit 'assembly.mapping' entry should be "
                       "added to the Ensembl Core 'meta' table.\n"
                       "Example: '%S|%S|%S'\n",
                       cs1str, cs2str,
                       cs1str, midstr, cs2str);

                ajStrDel(&cs1str);
                ajStrDel(&cs2str);
                ajStrDel(&midstr);
                ajStrDel(&midkey);

                break;
            }
            else
            {
                midpath = ajListNew();

                ajListPushAppend(midpath,
                                 (void *) ensCoordsystemNewRef(midcs));

                ajTablePut(midcs1,
                           (void *) ajStrNewS(midkey),
                           (void *) midpath);
            }

            ajStrDel(&midkey);
        }

        match = -1;

        if (ensCoordsystemMatch(csp0, cs2))
            match = 1;

        if (ensCoordsystemMatch(csp1, cs2))
            match = 0;

        if (match >= 0)
        {
            ajListPeekNumber(tmppath, (ajuint) match, (void **) &midcs);

            midkey = ajFmtStr("%S:%S", midcs->Name, midcs->Version);

            if (ajTableMatchS(midcs1, midkey))
            {
                mapkey = ajFmtStr("%S|%S", cs2key, cs1key);

                midpath = ajListNew();

                ajListPushAppend(midpath,
                                 (void *) ensCoordsystemNewRef(cs2));

                ajListPushAppend(midpath,
                                 (void *) ensCoordsystemNewRef(midcs));

                ajListPushAppend(midpath,
                                 (void *) ensCoordsystemNewRef(cs1));

                ajTablePut(csa->MappingPaths,
                           (void *) mapkey,
                           (void *) midpath);

                mappath = midpath;

                /*
                ** Inform the user that an explicit assembly.mapping entry is
                ** missing from the Ensembl Core meta table.
                */

                cs1str = (ajStrGetLen(cs1->Version)) ?
                    ajFmtStr("%S:%S", cs1->Name, cs1->Version) :
                    ajStrNewS(cs1->Name);

                midstr = (ajStrGetLen(midcs->Version)) ?
                    ajFmtStr("%S:%S", midcs->Name, midcs->Version) :
                    ajStrNewS(midcs->Name);

                cs2str = (ajStrGetLen(cs2->Version)) ?
                    ajFmtStr("%S:%S", cs2->Name, cs2->Version) :
                    ajStrNewS(cs2->Name);

                ajWarn("ensCoordsystemadaptorGetMappingpath uses an implicit "
                       "mapping path between "
                       "Ensembl Coordinate System objects '%S' and '%S'.\n"
                       "An explicit 'assembly.mapping' entry should be "
                       "added to the Ensembl Core 'meta' table.\n"
                       "Example: '%S|%S|%S'\n",
                       cs1str, cs2str,
                       cs1str, midstr, cs2str);

                ajStrDel(&cs1str);
                ajStrDel(&cs2str);
                ajStrDel(&midstr);
                ajStrDel(&midkey);

                break;
            }
            else
            {
                midpath = ajListNew();

                ajListPushAppend(midpath,
                                 (void *) ensCoordsystemNewRef(midcs));

                ajTablePut(midcs2,
                           (void *) ajStrNewS(midkey),
                           (void *) midpath);
            }

            ajStrDel(&midkey);
        }
    }

    AJFREE(keyarray);
    AJFREE(valarray);

    ajTableDel(&midcs1);
    ajTableDel(&midcs2);

    ajStrDel(&cs1key);
    ajStrDel(&cs2key);

    return mappath;
}




/* @func ensCoordsystemadaptorGetSeqregionidentifierExternal ******************
**
** Get the external Ensembl Sequence Region identifier for an
** internal Ensembl Sequence Region identifier.
** If the external Ensembl Sequence Region is not present in the mapping table,
** the identifier for the internal one will be returned.
**
** @param [r] csa [const EnsPCoordsystemadaptor]
** Ensembl Coordinate System Adaptor
** @param [r] srid [ajuint] Internal Ensembl Sequence Region identifier
**
** @return [ajuint] External Ensembl Sequence Region identifier or 0U
**
** @release 6.4.0
** @@
******************************************************************************/

ajuint ensCoordsystemadaptorGetSeqregionidentifierExternal(
    const EnsPCoordsystemadaptor csa,
    ajuint srid)
{
    ajuint *Pidentifier = NULL;

    if (!csa)
        return 0U;

    if (!srid)
        return 0U;

    Pidentifier = (ajuint *) ajTableFetchmodV(csa->InternalToExternal,
                                              (const void *) &srid);

    return (Pidentifier) ? *Pidentifier : srid;
}




/* @func ensCoordsystemadaptorGetSeqregionidentifierInternal ******************
**
** Get the internal Ensembl Sequence Region identifier for an
** external Ensembl Sequence Region identifier.
** If the internal Ensembl Sequence Region is not present in the mapping table,
** the identifier for the external one will be returned.
**
** @param [r] csa [const EnsPCoordsystemadaptor]
** Ensembl Coordinate System Adaptor
** @param [r] srid [ajuint] External Ensembl Sequence Region identifier
**
** @return [ajuint] Internal Ensembl Sequence Region identifier or 0U
**
** @release 6.4.0
** @@
******************************************************************************/

ajuint ensCoordsystemadaptorGetSeqregionidentifierInternal(
    const EnsPCoordsystemadaptor csa,
    ajuint srid)
{
    ajuint *Pidentifier = NULL;

    if (!csa)
        return 0U;

    if (!srid)
        return 0U;

    Pidentifier = (ajuint *) ajTableFetchmodV(csa->ExternalToInternal,
                                              (const void *) &srid);

    return (Pidentifier) ? *Pidentifier : srid;
}