File: mmdb_utils.cpp

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

#include <string.h>
#include <math.h>
#include <stdlib.h>

#include "mmdb_utils.h"
#include "hybrid_36.h"

namespace mmdb  {

  // ====================== Date functions  =======================

  static cpstr Month[12] = {
    "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
    "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
  };

  static cpstr nMonth[12] = {
    "01", "02", "03", "04", "05", "06",
    "07", "08", "09", "10", "11", "12"
  };

  void  Date9to11 ( cpstr Date9, pstr Date11 )  {
  // converts  DD-MMM-YY to DD-MMM-YYYY
  int i;
    i = 0;
    while ((i<12) && (strncmp(Month[i],&(Date9[3]),3)))  i++;
    if (i<12)  {   // DD-MMM-YY -> DD-MMM-YYYY
      strncpy ( Date11,Date9,7 );
      if (Date9[7]!='0')  strncpy ( &(Date11[7]),"19",2 );
                    else  strncpy ( &(Date11[7]),"20",2 );
      strncpy ( &(Date11[9]),&(Date9[7]),2 );
      Date11[2]  = '-';
      Date11[6]  = '-';
      Date11[11] = char(0);
    } else  {     // DD-MM-YY -> DD-MMM-YYYY
      strncpy ( Date11,Date9,3 );
      i = 0;
      while ((i<12) && (strncmp(nMonth[i],&(Date9[3]),2)))  i++;
      if (i<12)  {
        strncpy ( &(Date11[3]),Month[i],3 );
          if (Date9[6]!='0')  strncpy ( &(Date11[7]),"19",2 );
                    else  strncpy ( &(Date11[7]),"20",2 );
        strncpy ( &(Date11[9]),&(Date9[6]),2 );
        Date11[2]  = '-';
        Date11[6]  = '-';
        Date11[11] = char(0);
      } else
        strcpy ( Date11,"           " );
    }
  }

  void  Date11to9 ( cpstr Date11, pstr Date9 )  {
  // converts DD-MMM-YYYY to DD-MMM-YY
  int i;
    i = 0;
    while ((i<12) && (strncmp(Month[i],&(Date11[3]),3)))  i++;
    if (i<12)  {   // DD-MMM-YYYY -> DD-MMM-YY
      strncpy ( Date9,Date11,7 );
      strncpy ( &(Date9[7]),&(Date11[9]),2 );
      Date9[2] = '-';
      Date9[6] = '-';
    } else  {      // DD-MM-YYYY -> DD-MMM-YY
      strncpy ( Date9,Date11,3 );
      i = 0;
      while ((i<12) && (strncmp(nMonth[i],&(Date11[3]),2)))  i++;
      if (i<12)  {
        strncpy ( &(Date9[3]),Month[i],3 );
        strncpy ( &(Date9[7]),&(Date11[8]),2 );
        Date9[2] = '-';
        Date9[6] = '-';
      } else
        strcpy ( Date9,"         " );
    }
  }

  void  Date9toCIF ( cpstr Date9, pstr DateCIF )  {
  //  DD-MMM-YY -> YYYY-MM-DD             )
  int  i;
    i = 0;
    while ((i<12) && (strncmp(Month[i],&(Date9[3]),3)))  i++;
    if (i<12)  {   //  DD-MMM-YY  -> YYYY-MM-DD
      if (Date9[7]!='0')  strcpy ( DateCIF,"19" );
                    else  strcpy ( DateCIF,"20" );
      strncpy ( &(DateCIF[2]),&(Date9[7]),2 );
      strncpy ( &(DateCIF[5]),nMonth[i],2 );
    } else  {      //  DD-MM-YY  ->  YYYY-MM-DD
      if (Date9[6]!='0')  strcpy ( DateCIF,"19" );
                    else  strcpy ( DateCIF,"20" );
      strncpy ( &(DateCIF[2]),&(Date9[6]),2 );
      strncpy ( &(DateCIF[5]),&(Date9[3]),2 );
    }
    DateCIF[4] = '-';
    DateCIF[7] = '-';
    strncpy ( &(DateCIF[8]),Date9,2 );
    DateCIF[10] = char(0);
  }

  void  Date11toCIF ( cpstr Date11, pstr DateCIF )  {
  //  DD-MMM-YYYY -> YYYY-MM-DD
  int  i;
    i = 0;
    while ((i<12) && (strncmp(Month[i],&(Date11[3]),3)))  i++;
    if (i<12) {
      strncpy ( DateCIF,&(Date11[7]),4 );
      strncpy ( &(DateCIF[5]),nMonth[i],2 );
    } else  {
      strncpy ( DateCIF,&(Date11[6]),4 );
      strncpy ( &(DateCIF[5]),&(Date11[3]),2 );
    }
    DateCIF[4] = '-';
    DateCIF[7] = '-';
    strncpy ( &(DateCIF[8]),Date11,2 );
    DateCIF[10] = char(0);
  }

  void  DateCIFto9 ( cpstr DateCIF, pstr Date9 )  {
  //  YYYY-MM-DD -> DD-MMM-YY
  int  i;
    strncpy ( Date9,&(DateCIF[8]),2 );
    Date9[2] = '-';
    i = 0;
    while ((i<12) && (strncmp(nMonth[i],&(DateCIF[5]),2)))  i++;
    if (i<12) strncpy ( &(Date9[3]),Month[i],3 );
    else  {
      strncpy ( &(Date9[3]),&(DateCIF[5]),2 );
      Date9[5] = 'X';
    }
    Date9[6] = '-';
    strncpy ( &(Date9[7]),&(DateCIF[2]),2 );
  //  DateCIF[9] = char(0);
  }

  void  DateCIFto11 ( cpstr DateCIF, pstr Date11 )  {
  //  YYYY-MM-DD  -> DD-MMM-YYYY
  int  i;
    strncpy ( Date11,&(DateCIF[8]),2 );
    Date11[2] = '-';
    i = 0;
    while ((i<12) && (strncmp(nMonth[i],&(DateCIF[5]),2)))  i++;
    if (i<12) strncpy ( &(Date11[3]),Month[i],3 );
    else  {
      strncpy ( &(Date11[3]),&(DateCIF[5]),2 );
      Date11[5] = 'X';
    }
    Date11[6] = '-';
    strncpy ( &(Date11[7]),DateCIF,4 );
  //  DateCIF[11] = char(0);
  }


  //  =============== Format functions  ===================

  bool GetInteger ( int & N, cpstr S, int M )  {
  //   Returns true if S contains an integer number in its
  // first M characters. This number is returned in N.
  //   The return is false if no integer number may be
  // recognized. In this case, N is assigned MinInt4 value.
  pstr endptr;
  char L[50];
    strncpy ( L,S,M );
    L[M] = char(0);
    N    = mround(strtod(L,&endptr));
    if ((N==0) && (endptr==L))  {
      N = MinInt4;  // no number
      return false;
    } else
      return true;
  }

  bool GetReal ( realtype & R, cpstr S, int M )  {
  //   Returns true if S contains a real number in its
  // first M characters. This number is returned in R.
  //   The return is false if no real number may be
  // recognized. In this case, R is assigned -MaxReal value.
  pstr endptr;
  char L[50];
    strncpy ( L,S,M );
    L[M] = char(0);
    R    = strtod(L,&endptr);
    if ((R==0.0) && (endptr==L))  {
      R = -MaxReal;  // no number
      return false;
    } else
      return true;
  }

  bool  GetIntIns ( int & N, pstr ins, cpstr S, int M )  {
  //   Returns true if S contains an integer number in its
  // first M characters. This number is returned in N. In addition
  // to that, GetIntIns() retrieves the insertion code which may
  // follow the integer and returns it in "ins" (1 character +
  // terminating 0).
  //   The return is false if no integer number may be
  // recognized. In this case, N is assigned MinInt4 value,
  // "ins" just returns (M+1)th symbol of S (+terminating 0).
  pstr endptr;
  char L[50];

    if (S[M]!=' ')  {
      ins[0] = S[M];
      ins[1] = char(0);
    } else
      ins[0] = char(0);

    strncpy ( L,S,M );
    L[M] = char(0);
    if ((M==4) && ((S[0]>='A') || ((S[0]=='-') && (S[1]>='A'))))
      hy36decode ( M,L,M,&N);
    else  {
      endptr = NULL;
      N      = mround(strtod(L,&endptr));
      if ((N==0) && (endptr==L))  {
        N = MinInt4;  // no number
        return false;
      }
    }

    return true;

  }

  void  PutInteger ( pstr S, int N, int M )  {
  //  Integer N is converted into ASCII string of length M
  // and pasted onto first M characters of string S. No
  // terminating zero is added.
  //  If N is set to MinInt4, then first M characters of
  // string S are set to the space character.
  char L[50];
  int  i;
    if (N==MinInt4)
      for (i=0;i<M;i++)
        S[i] = ' ';
    else  {
      sprintf ( L,"%*i",M,N );
      strncpy ( S,L,M );
    }
  }

  void  PutRealF ( pstr S, realtype R, int M, int L )  {
  //  Real R is converted into ASCII string of length M
  // and pasted onto first M characters of string S. No
  // terminating zero is added. The conversion is done
  // according to fixed format FM.L
  //  If R is set to -MaxReal, then first M characters of
  // string S are set to the space character.
  char N[50];
  int  i;
    if (R==-MaxReal)
      for (i=0;i<M;i++)
        S[i] = ' ';
    else  {
      sprintf ( N,"%*.*f",M,L,R );
      strncpy ( S,N,M );
    }
  }

  ERROR_CODE CIFGetIntegerD ( int & I, mmcif::PLoop Loop, cpstr Tag,
                             int defValue )  {
  int        Signal;
  ERROR_CODE RC;
    Signal = 0;
    RC = CIFGetInteger ( I,Loop,Tag,Signal );
    if (RC)
      I = defValue;
    return RC;
  }

  ERROR_CODE CIFGetInteger ( int & I, mmcif::PLoop Loop, cpstr Tag,
                             int & Signal )  {
  pstr F;
  int  RC;
    RC = Loop->GetInteger ( I,Tag,Signal,true );
    if (RC==mmcif::CIFRC_WrongFormat)  {
      F = Loop->GetString ( Tag,Signal,RC );
      if (F) sprintf ( CIFErrorLocation,"loop %s.%s row %i data %s",
                       Loop->GetCategoryName(),Tag,Signal,F );
        else sprintf ( CIFErrorLocation,"loop %s.%s row %i data [NULL]",
                       Loop->GetCategoryName(),Tag,Signal );
      Signal = -Error_UnrecognizedInteger-1;
      return Error_UnrecognizedInteger;
    }
    if (RC==mmcif::CIFRC_WrongIndex)  {
      Signal = -1;
      return Error_NoData;
    }
    if (RC)  {
      F = Loop->GetString ( Tag,Signal,RC );
      if (F) sprintf ( CIFErrorLocation,"loop %s.%s row %i data %s",
                       Loop->GetCategoryName(),Tag,Signal,F );
        else sprintf ( CIFErrorLocation,"loop %s.%s row %i data [NULL]",
                       Loop->GetCategoryName(),Tag,Signal );
      Signal = -Error_NoData-1;
      return Error_NoData;
    }
    return Error_NoError;
  }


  ERROR_CODE CIFGetInteger1 ( int & I, mmcif::PLoop Loop, cpstr Tag,
                              int nrow )  {
  pstr F;
  int  RC;
    RC = Loop->GetInteger ( I,Tag,nrow,true );
    if (RC==mmcif::CIFRC_WrongFormat)  {
      F = Loop->GetString ( Tag,nrow,RC );
      if (F) sprintf ( CIFErrorLocation,"loop %s.%s row %i data %s",
                       Loop->GetCategoryName(),Tag,nrow,F );
        else sprintf ( CIFErrorLocation,"loop %s.%s row %i data [NULL]",
                       Loop->GetCategoryName(),Tag,nrow );
      return Error_UnrecognizedInteger;
    }
    if (RC==mmcif::CIFRC_WrongIndex)
      return Error_NoData;
    if (RC)  {
      F = Loop->GetString ( Tag,nrow,RC );
      if (F) sprintf ( CIFErrorLocation,"loop %s.%s row %i data %s",
                       Loop->GetCategoryName(),Tag,nrow,F );
        else sprintf ( CIFErrorLocation,"loop %s.%s row %i data [NULL]",
                       Loop->GetCategoryName(),Tag,nrow );
      return Error_NoData;
    }
    return Error_NoError;
  }


  ERROR_CODE CIFGetReal ( realtype & R, mmcif::PLoop Loop, cpstr Tag,
                          int & Signal )  {
  pstr F;
  int  RC;
    RC = Loop->GetReal ( R,Tag,Signal,true );
    if (RC==mmcif::CIFRC_WrongFormat)  {
      F = Loop->GetString ( Tag,Signal,RC );
      if (F) sprintf ( CIFErrorLocation,"loop %s.%s row %i data %s",
                       Loop->GetCategoryName(),Tag,Signal,F );
        else sprintf ( CIFErrorLocation,"loop %s.%s row %i data [NULL]",
                       Loop->GetCategoryName(),Tag,Signal );
      Signal = -Error_UnrecognizedReal-1;
      return Error_UnrecognizedReal;
    }
    if (RC==mmcif::CIFRC_WrongIndex)  {
      Signal = -1;
      return Error_NoData;
    }
    if (RC)  {
      F = Loop->GetString ( Tag,Signal,RC );
      if (F) sprintf ( CIFErrorLocation,"loop %s.%s row %i data %s",
                       Loop->GetCategoryName(),Tag,Signal,F );
        else sprintf ( CIFErrorLocation,"loop %s.%s row %i data [NULL]",
                       Loop->GetCategoryName(),Tag,Signal );
      Signal = -Error_NoData-1;
      return Error_NoData;
    }
    return Error_NoError;
  }


  ERROR_CODE CIFGetReal1 ( realtype & R, mmcif::PLoop Loop, cpstr Tag,
                           int nrow )  {
  pstr F;
  int  RC;
    RC = Loop->GetReal ( R,Tag,nrow,true );
    if (RC==mmcif::CIFRC_WrongFormat)  {
      F = Loop->GetString ( Tag,nrow,RC );
      if (F) sprintf ( CIFErrorLocation,"loop %s.%s row %i data %s",
                       Loop->GetCategoryName(),Tag,nrow,F );
        else sprintf ( CIFErrorLocation,"loop %s.%s row %i data [NULL]",
                       Loop->GetCategoryName(),Tag,nrow );
      return Error_UnrecognizedReal;
    }
    if (RC==mmcif::CIFRC_WrongIndex)
      return Error_NoData;
    if (RC)  {
      F = Loop->GetString ( Tag,nrow,RC );
      if (F) sprintf ( CIFErrorLocation,"loop %s.%s row %i data %s",
                       Loop->GetCategoryName(),Tag,nrow,F );
        else sprintf ( CIFErrorLocation,"loop %s.%s row %i data [NULL]",
                       Loop->GetCategoryName(),Tag,nrow );
      return Error_NoData;
    }
    return Error_NoError;
  }


  ERROR_CODE CIFGetString ( pstr S, mmcif::PLoop Loop, cpstr Tag,
                            int row, int SLen, cpstr DefS )  {
  pstr F;
  int RC;
    F = Loop->GetString ( Tag,row,RC );
    if ((!RC) && F)  {
      strncpy ( S,F,SLen-1 );
      Loop->DeleteField ( Tag,row );
      return Error_NoError;
    } else  {
      strcpy ( S,DefS );
      return Error_EmptyCIFLoop;
    }
  }


  ERROR_CODE CIFGetInteger ( int & I, mmcif::PStruct Struct, cpstr Tag,
                             bool Remove )  {
  pstr F;
  int  RC;
    RC = Struct->GetInteger ( I,Tag,Remove );
    if (RC==mmcif::CIFRC_WrongFormat)  {
      F = Struct->GetString ( Tag,RC );
      if (F) sprintf ( CIFErrorLocation,"structure %s.%s data %s",
                       Struct->GetCategoryName(),Tag,F );
        else sprintf ( CIFErrorLocation,"structure %s.%s data [NULL]",
                       Struct->GetCategoryName(),Tag );
      return Error_UnrecognizedInteger;
    }
    if (RC)  {
      F = Struct->GetString ( Tag,RC );
      if (F) sprintf ( CIFErrorLocation,"structure %s.%s data %s",
                       Struct->GetCategoryName(),Tag,F );
        else sprintf ( CIFErrorLocation,"structure %s.%s data [NULL]",
                       Struct->GetCategoryName(),Tag );
      return Error_NoData;
    }
    return Error_NoError;
  }

  ERROR_CODE CIFGetReal ( realtype & R, mmcif::PStruct Struct, cpstr Tag,
                          bool Remove )  {
  pstr F;
  int RC;
    RC = Struct->GetReal ( R,Tag,Remove );
    if (RC==mmcif::CIFRC_WrongFormat)  {
      F = Struct->GetString ( Tag,RC );
      if (F) sprintf ( CIFErrorLocation,"structure %s.%s data %s",
                       Struct->GetCategoryName(),Tag,F );
        else sprintf ( CIFErrorLocation,"structure %s.%s data [NULL]",
                       Struct->GetCategoryName(),Tag );
      return Error_UnrecognizedReal;
    }
    if (RC)  {
      F = Struct->GetString ( Tag,RC );
      if (F) sprintf ( CIFErrorLocation,"structure %s.%s data %s",
                       Struct->GetCategoryName(),Tag,F );
        else sprintf ( CIFErrorLocation,"structure %s.%s data [NULL]",
                       Struct->GetCategoryName(),Tag );
      return Error_NoData;
    }
    return Error_NoError;
  }

  ERROR_CODE CIFGetString ( pstr S, mmcif::PStruct Struct, cpstr Tag,
                            int SLen, cpstr DefS, bool Remove )  {
  pstr F;
  int  RC;
    F = Struct->GetString ( Tag,RC );
    if ((!RC) && F)  {
      strcpy_n0 ( S,F,SLen-1 );
      if (Remove)  Struct->DeleteField ( Tag );
      return Error_NoError;
    } else  {
      strcpy ( S,DefS );
      return Error_EmptyCIFStruct;
    }
  }


  void  PutIntIns ( pstr S, int N, int M, cpstr ins )  {
  //  Integer N is converted into ASCII string of length M
  // and pasted onto first M characters of string S. No
  // terminating zero is added. The insert code ins is put
  // immediately after the integer.
  //  If N is set to MinInt4, then first M+1 characters of
  // string S are set to space, and no insert code are
  // appended.
  char L[50];
  int  i;

    if (N==MinInt4)  {
      for (i=0;i<=M;i++)
        S[i] = ' ';
    } else  {
      if ((M!=4) || ((N>=-999) && (N<=9999)))
           sprintf    ( L,"%*i",M,N );
      else hy36encode ( M,N,L );
      strcpy_n1 ( S,L,M );
      if (ins[0]) S[M] = ins[0];
    }

  }


  void  Mat4Inverse ( const mat44 & A, mat44 & AI )  {
  //       ***  FORMER RBRINV(A,AI)  ***
  //   Function to invert 4*4 matrices (AI=A^{-1})
  mat44    c;
  mat33    x;
  realtype s,s1;
  int      ii,jj,i,i1,j,j1;

  // ---- Get cofactors of 'a' in array 'c'

    s1 = 1.0;
    for (ii=0;ii<4;ii++)  {
      s = s1;
      for (jj=0;jj<4;jj++)  {
        i = -1;
        for (i1=0;i1<4;i1++)
          if (i1!=ii)  {
            i++;
            j = -1;
            for (j1=0;j1<4;j1++)
              if (j1!=jj)  {
                j++;
                x[i][j] = A[i1][j1];
              }
          }
        c[ii][jj] = s*(x[0][0]*(x[1][1]*x[2][2]-x[1][2]*x[2][1]) +
                       x[0][1]*(x[1][2]*x[2][0]-x[1][0]*x[2][2]) +
                       x[0][2]*(x[1][0]*x[2][1]-x[1][1]*x[2][0]));
        s = -s;
      }
      s1 = -s1;
    }

  // ---- Calculate determinant

    s = 0.0;
    for (i=0;i<4;i++)
      s += A[i][0]*c[i][0];

  // ---- Get inverse matrix

    if (s!=0.0)
      for (i=0;i<4;i++)
        for (j=0;j<4;j++)
          AI[i][j] = c[j][i]/s;

  }

  realtype Mat3Inverse ( const mat33 & A, mat33 & AI )  {
  mat33    c,x;
  realtype s;
  int      ii,jj,i,i1,j,j1;

    // Get cofactors of 'a' in array 'c'

    s = 1.0;
    for (ii=0;ii<3;ii++)
      for (jj=0;jj<3;jj++)  {
        i = -1;
        for (i1=0;i1<3;i1++)
          if (i1!=ii)  {
            i++;
            j = -1;
            for (j1=0;j1<3;j1++)
              if (j1!=jj)  {
                j++;
                x[i][j] = A[i1][j1];
              }
          }
        c[ii][jj] = s*(x[0][0]*x[1][1]-x[0][1]*x[1][0]);
        s = -s;
      }

    // Calculate determinant

    s = 0.0;
    for (i=0;i<3;i++)
      s += A[i][0]*c[i][0];

    // Get inverse matrix

    if (s!=0.0)
      for (i=0;i<3;i++)
        for (j=0;j<3;j++)
          AI[i][j] = c[j][i]/s;

    return s;

  }

  void  Mat4Mult ( mat44 & A, const mat44 & B, const mat44 & C )  {
  //  Calculates A=B*C
  int i,j,k;
    for (i=0;i<4;i++)
      for (j=0;j<4;j++)  {
        A[i][j] = 0.0;
        for (k=0;k<4;k++)
          A[i][j] += B[i][k]*C[k][j];
      }
  }

  void  Mat4Div1 ( mat44 & A, const mat44 & B, const mat44 & C )  {
  //  Calculates A=B^{-1}*C
  mat44 B1;
  int   i,j,k;
    B1[0][0] = 1.0; // in order to supress warnings from some
                    // stupid compilers
    Mat4Inverse ( B,B1 );
    for (i=0;i<4;i++)
      for (j=0;j<4;j++)  {
        A[i][j] = 0.0;
        for (k=0;k<4;k++)
          A[i][j] += B1[i][k]*C[k][j];
      }
  }

  void  Mat4Div2 ( mat44 & A, const mat44 & B, const mat44 & C )  {
  //  Calculates A=B*C^{-1}
  mat44 C1;
  int   i,j,k;
    C1[0][0] = 1.0; // in order to supress warnings from some
                    // stupid compilers
    Mat4Inverse ( C,C1 );
    for (i=0;i<4;i++)
      for (j=0;j<4;j++)  {
        A[i][j] = 0.0;
        for (k=0;k<4;k++)
          A[i][j] += B[i][k]*C1[k][j];
      }
  }

  void  Mat4Init ( mat44 & A )  {
  int i,j;
    for (i=0;i<4;i++)  {
      for (j=0;j<4;j++)
        A[i][j] = 0.0;
      A[i][i] = 1.0;
    }
  }

  realtype Mat4RotDet ( const mat44 & T )  {
  //  returns determinant of the rotation part
    return T[0][0]*T[1][1]*T[2][2] +
           T[0][1]*T[1][2]*T[2][0] +
           T[1][0]*T[2][1]*T[0][2] -
           T[0][2]*T[1][1]*T[2][0] -
           T[0][0]*T[1][2]*T[2][1] -
           T[2][2]*T[0][1]*T[1][0];
  }

  bool isMat4Unit ( const mat44 & A, realtype eps, bool rotOnly )  {
  // returns true if A is a unit 4x4 matrix
  int     i,j,k;
  bool B;

    if (rotOnly)  k = 3;
            else  k = 4;

    B = true;
    for (i=0;(i<k) && B;i++)
      for (j=0;(j<k) && B;j++)
        if (i==j)  B = (fabs(1.0-A[i][j])<eps);
             else  B = (fabs(A[i][j])<eps);

    return B;

  }

  void  Mat3Init ( mat33 & A )  {
  int i,j;
    for (i=0;i<3;i++)  {
      for (j=0;j<3;j++)
        A[i][j] = 0.0;
      A[i][i] = 1.0;
    }
  }

  void  Mat4Copy ( const mat44 & A, mat44 & ACopy )  {
  int i,j;
    for (i=0;i<4;i++)
      for (j=0;j<4;j++)
        ACopy[i][j] = A[i][j];
  }

  void  Mat3Copy ( const mat33 & A, mat33 & ACopy )  {
  int i,j;
    for (i=0;i<3;i++)
      for (j=0;j<3;j++)
        ACopy[i][j] = A[i][j];
  }

  bool isMat4Eq ( const mat44 & A, const mat44 & B, realtype eps,
                  bool rotOnly )  {
  // returns true if A is equal to B within precision eps
  int  i,j,k;
  bool Eq;

    if (rotOnly)  k = 3;
            else  k = 4;

    Eq = true;
    for (i=0;(i<k) && Eq;i++)
      for (j=0;(j<k) && Eq;j++)
        Eq = (fabs(A[i][j]-B[i][j])<eps);

    return Eq;

  }


  void TransformXYZ ( const mat44 & T, realtype & X, realtype & Y,
                                                     realtype & Z )  {
  realtype x1,y1,z1;
    x1 = T[0][0]*X + T[0][1]*Y + T[0][2]*Z + T[0][3];
    y1 = T[1][0]*X + T[1][1]*Y + T[1][2]*Z + T[1][3];
    z1 = T[2][0]*X + T[2][1]*Y + T[2][2]*Z + T[2][3];
    X = x1;
    Y = y1;
    Z = z1;
  }

  realtype TransformX ( const mat44 & T, realtype X, realtype Y,
                                                     realtype Z )  {
    return  T[0][0]*X + T[0][1]*Y + T[0][2]*Z + T[0][3];
  }

  realtype TransformY ( const mat44 & T, realtype X, realtype Y,
                                                     realtype Z )  {
    return  T[1][0]*X + T[1][1]*Y + T[1][2]*Z + T[1][3];
  }

  realtype TransformZ ( const mat44 & T, realtype X, realtype Y,
                                                     realtype Z )  {
    return  T[2][0]*X + T[2][1]*Y + T[2][2]*Z + T[2][3];
  }




  char CIFErrorLocation[200] = "no error";

  static cpstr msWrongSection =
    "Wrong section. The sections in PDB file may be put in wrong order.";
  static cpstr msWrongChainID =
    "Wrong chain ID. The input may have changed to another chain.";
  static cpstr msWrongEntryID =
    "Entry ID does not match the header.";

  static cpstr msSEQRES_serNum   =
    "Serial numbers of SEQRES records do not increment by 1.";
  static cpstr msSEQRES_numRes   =
    "Different SEQRES records show different numbers of residues.";
  static cpstr msSEQRES_extraRes =
    "SEQRES records contain more residues than specified.";

  static cpstr msNCSM_Unrecognized =
    "Unrecognized numerical input in MTRIXn.";
  static cpstr msNCSM_AlreadySet   =
    "Duplicate MTRIXn record.";
  static cpstr msNCSM_WrongSerial  =
    "Serial number in MTRIXn record is wrong.";
  static cpstr msNCSM_UnmatchIG    =
    "Different MTRIXn record show different iGiven flag.";

  static cpstr msATOM_Unrecognized =
    "Numerical information in ATOM record is not recognized.";
  static cpstr msATOM_AlreadySet   =
    "Atom is already in the system.";
  static cpstr msATOM_NoResidue    =
    "No residue is found for atom.";
  static cpstr msATOM_Unmatch      =
    "Unmatch in different records for the same atom.";

  static cpstr msCantOpenFile        = "File can not be opened.";
  static cpstr msUnrecognizedInteger =
    "Wrong ASCII format of an integer.";
  static cpstr msWrongModelNo        = "Wrong model number.";
  static cpstr msDuplicatedModel     = "Duplicate model number.";
  static cpstr msNoModel             = "No model defined.";
  static cpstr msForeignFile         =
    "Attempt to read unknown-type file.";
  static cpstr msWrongEdition        =
    "Attempt to read a higher-version file.";

  static cpstr msNoData              = "Expected data field not found.";
  static cpstr msUnrecognizedReal    = "Wrong ASCII format of a real.";
  static cpstr msNotACIFFile         =
    "Not a CIF file ('data_' missing).";
  static cpstr msUnrecognCIFItems    =
    "Unrecognized item(s) in CIF file.";
  static cpstr msMissingCIFField     = "Expected CIF item(s) missing.";
  static cpstr msEmptyCIFLoop        = "Empty CIF loop encountered.";
  static cpstr msUnexpEndOfCIF       = "Unexpected end of CIF file.";
  static cpstr msMissgCIFLoopField   = "Inconsistent CIF loop.";
  static cpstr msNotACIFStructure    =
    "Wrong use of CIF structure (as a loop?).";
  static cpstr msNotACIFLoop         =
    "Wrong use of CIF loop (as a structure?).";

  static cpstr msNoSheetID           = "No Sheet ID on PDB ASCII card.";
  static cpstr msWrongSheetID        = "Wrong Sheet ID.";
  static cpstr msWrongStrandNo       =
    "Wrong Strand number on PDB SHEET card.";

  static cpstr msWrongNumberOfStrands =
    "Wrong number of strands in CIF file.";
  static cpstr msWrongSheetOrder     = "Incomplete _struct_sheet_order.";
  static cpstr msHBondInconsistency  =
    "Inconsistency in _struct_sheet_hbond.";

  static cpstr msEmptyResidueName    =
          "No residue name on PDB ATOM or TER card.";
  static cpstr msDuplicateSeqNum     =
          "Duplicate sequence number and insertion code.";

  static cpstr msEmptyFile           = "Non-existent or empty file.";

  static cpstr msNoLogicalName       = "Logical file name not found.";


  cpstr  GetErrorDescription ( ERROR_CODE ErrorCode )  {

    switch (ErrorCode)  {

      case Error_NoError              :  return "No errors.";

      case Error_WrongSection         :  return msWrongSection;
      case Error_WrongChainID         :  return msWrongChainID;
      case Error_WrongEntryID         :  return msWrongEntryID;

      case Error_SEQRES_serNum        :  return msSEQRES_serNum;
      case Error_SEQRES_numRes        :  return msSEQRES_numRes;
      case Error_SEQRES_extraRes      :  return msSEQRES_extraRes;

      case Error_NCSM_Unrecognized    :  return msNCSM_Unrecognized;
      case Error_NCSM_AlreadySet      :  return msNCSM_AlreadySet;
      case Error_NCSM_WrongSerial     :  return msNCSM_WrongSerial;
      case Error_NCSM_UnmatchIG       :  return msNCSM_UnmatchIG;

      case Error_ATOM_Unrecognized    :  return msATOM_Unrecognized;
      case Error_ATOM_AlreadySet      :  return msATOM_AlreadySet;
      case Error_ATOM_NoResidue       :  return msATOM_NoResidue;
      case Error_ATOM_Unmatch         :  return msATOM_Unmatch;

      case Error_CantOpenFile         :  return msCantOpenFile;
      case Error_UnrecognizedInteger  :  return msUnrecognizedInteger;
      case Error_WrongModelNo         :  return msWrongModelNo;
      case Error_DuplicatedModel      :  return msDuplicatedModel;
      case Error_NoModel              :  return msNoModel;
      case Error_ForeignFile          :  return msForeignFile;
      case Error_WrongEdition         :  return msWrongEdition;

      case Error_NoData               :  return msNoData;
      case Error_UnrecognizedReal     :  return msUnrecognizedReal;
      case Error_NotACIFFile          :  return msNotACIFFile;
      case Error_UnrecognCIFItems     :  return msUnrecognCIFItems;
      case Error_MissingCIFField      :  return msMissingCIFField;
      case Error_EmptyCIFLoop         :  return msEmptyCIFLoop;
      case Error_UnexpEndOfCIF        :  return msUnexpEndOfCIF;
      case Error_MissgCIFLoopField    :  return msMissgCIFLoopField;
      case Error_NotACIFStructure     :  return msNotACIFStructure;
      case Error_NotACIFLoop          :  return msNotACIFLoop;

      case Error_NoSheetID            :  return msNoSheetID;
      case Error_WrongSheetID         :  return msWrongSheetID;
      case Error_WrongStrandNo        :  return msWrongStrandNo;

      case Error_WrongNumberOfStrands :  return msWrongNumberOfStrands;
      case Error_WrongSheetOrder      :  return msWrongSheetOrder;
      case Error_HBondInconsistency   :  return msHBondInconsistency;

      case Error_EmptyResidueName     :  return msEmptyResidueName;
      case Error_DuplicateSeqNum      :  return msDuplicateSeqNum;

      case Error_EmptyFile            :  return msEmptyFile;

      case Error_NoLogicalName        :  return msNoLogicalName;

      default                         :  return "Unknown error.";

    }
  }


  //  ==============  ContainerClass  ====================

  ContainerClass::ContainerClass() : io::Stream()  {
    ContinuationNo = 0;
  }

  ContainerClass::ContainerClass ( io::RPStream Object )
                 : io::Stream(Object)  {
    ContinuationNo = 0;
  }

  bool  ContainerClass::Append ( PContainerClass CC )  {
    return  (CC->ContinuationNo>1);
  }


  //  ===================  ContString  =====================

  ContString::ContString() : ContainerClass()  {
    InitString();
  }

  ContString::ContString ( cpstr S ) : ContainerClass()  {
    InitString();
    ConvertPDBASCII ( S );
  }

  ContString::ContString ( io::RPStream Object )
             : ContainerClass(Object)  {
    InitString();
  }

  ContString::~ContString() {
    if (Line)        delete[] Line;
    if (CIFCategory) delete[] CIFCategory;
    if (CIFTag)      delete[] CIFTag;
  }

  void  ContString::InitString()  {
    Line        = NULL;
    CIFCategory = NULL;
    CIFTag      = NULL;
  }

  ERROR_CODE ContString::ConvertPDBASCII ( cpstr S )  {
    CreateCopy ( Line,S );
    return Error_NoError;
  }

  void  ContString::PDBASCIIDump ( pstr S, int )  {
    if (Line)  strcpy ( S,Line );
         else  strcpy ( S,""   );
  }

  bool ContString::PDBASCIIDump1 ( io::RFile f )  {
    if (Line)  f.WriteLine ( Line );
         else  f.LF();
    return true;
  }

/*
  void ContString::GetCIF1 ( mmcif::PData CIF, ERROR_CODE & Signal,
                             int & pos )  {
  pstr F;
  int  i,RC;
  char c;
    if ((!CIFCategory) || (!CIFTag))  {
      Signal = Error_EmptyCIF;
      return;
    }
    F = CIF->GetString ( CIFCategory,CIFTag,RC );
    if (RC || (!F))  {
      Signal = Error_EmptyCIF;
      return;
    }
    if (Signal>=(int)strlen(F))  {
      CIF->DeleteField ( CIFCategory,CIFTag );
      Signal = Error_EmptyCIF;
      return;
    }
//    i = Signal;
//    while (F[i] && (F[i]!='\n') && (F[i]!='\r'))  i++;
//    if ((Signal==0) && (i==0))  {
//      i++;
//      if (((F[Signal]=='\n') && (F[i]=='\r')) ||
//          ((F[Signal]=='\r') && (F[i]=='\n')))  i++;
//      Signal = i;
//      while (F[i] && (F[i]!='\n') && (F[i]!='\r'))  i++;
//    }
//    c = F[i];
//    F[i] = char(0);
//    CreateCopy ( Line,&(F[Signal]) );
//    if (c)  {
//      F[i]   = c;
//      Signal = i+1;
//      if (((c=='\n') && (F[Signal]=='\r')) ||
//          ((c=='\r') && (F[Signal]=='\n')))  Signal++;
//    } else
//      CIF->DeleteField ( CIFCategory,CIFTag );
    i = pos;
    while (F[i] && (F[i]!='\n') && (F[i]!='\r'))  i++;
    if ((pos==0) && (i==0))  {
      i++;
      if (((F[pos]=='\n') && (F[i]=='\r')) ||
          ((F[pos]=='\r') && (F[i]=='\n')))  i++;
      pos = i;
      while (F[i] && (F[i]!='\n') && (F[i]!='\r'))  i++;
    }
    c = F[i];
    F[i] = char(0);
    CreateCopy ( Line,&(F[pos]) );
    if (c)  {
      F[i] = c;
      pos  = i+1;
      if (((c=='\n') && (F[pos]=='\r')) ||
          ((c=='\r') && (F[pos]=='\n')))  pos++;
    } else
      CIF->DeleteField ( CIFCategory,CIFTag );
  }
*/

  void  ContString::MakeCIF ( mmcif::PData CIF, int N )  {
  pstr S;
    if ((!CIFCategory) || (!CIFTag))  return;
    S = new char[strlen(Line)+5];
    strcpy ( S,"\n" );
    strcat ( S,Line );
    CIF->PutString ( S,CIFCategory,CIFTag,(N!=0) );
    delete[] S;
  }

  bool  ContString::Append ( PContainerClass CC )  {
    if (ContainerClass::Append(CC))  {
      if (!Line)  {
        Line = PContString(CC)->Line;
        PContString(CC)->Line = NULL;
      } else
        CreateConcat ( Line,pstr("\n"),PContString(CC)->Line );
      return true;
    }
    return false;
  }

  void  ContString::Copy ( PContainerClass CString )  {
    CreateCopy ( Line,PContString(CString)->Line );
  }

  void  ContString::write ( io::RFile f )  {
  byte Version=1;
    f.WriteByte   ( &Version    );
    f.CreateWrite ( Line        );
    f.CreateWrite ( CIFCategory );
    f.CreateWrite ( CIFTag      );
  }

  void  ContString::read ( io::RFile f )  {
  byte Version;
    f.ReadByte   ( &Version    );
    f.CreateRead ( Line        );
    f.CreateRead ( CIFCategory );
    f.CreateRead ( CIFTag      );
  }

  MakeStreamFunctions(ContString)



  //  ==============  ClassContainer  ====================

  MakeStreamFunctions(ContainerClass)

  ClassContainer::ClassContainer() : io::Stream()  {
    Init();
  }

  ClassContainer::ClassContainer ( io::RPStream Object )
                  : io::Stream(Object)  {
    Init();
  }

  void ClassContainer::Init()  {
    length    = 0;
    Container = NULL;
  }

  ClassContainer::~ClassContainer()  {
    FreeContainer();
  }

  void  ClassContainer::FreeContainer()  {
  int i;
    if (Container)  {
      for (i=0;i<length;i++)
        if (Container[i])
          delete Container[i];
      delete[] Container;
    }
    Container = NULL;
    length    = 0;
  }

  void  ClassContainer::AddData ( PContainerClass Data )  {
  int               i;
  PPContainerClass C1;
    if (!Data)  return;
    if (length>0)  {
      i = length-1;
      while (i>=0)  {
        if (!Container[i])  i--;
        else if (Container[i]->GetClassID()!=Data->GetClassID())  i--;
        else break;
      }
      if (i>=0)  {
        if (Container[i]->Append(Data))  {
          delete Data;
          return;
        }
      }
    }
    C1 = new PContainerClass[length+1];
    for (i=0;i<length;i++)
      C1[i] = Container[i];
    C1[length] = Data;
    if (Container)  delete[] Container;
    Container = C1;
    length++;
  }

  void  ClassContainer::PDBASCIIDump ( io::RFile f )  {
  char S[500];
  int  i,j;
    for (i=0;i<length;i++)
      if (Container[i])  {
        if (!Container[i]->PDBASCIIDump1(f))  {
          Container[i]->PDBASCIIDump ( S,i );
          j = strlen(S);
          while (j<80)  S[j++] = ' ';
          S[80] = char(0);
          f.WriteLine ( S );
        }
      }
  }

  ERROR_CODE ClassContainer::GetCIF ( mmcif::PData CIF, int ClassID )  {
  PContainerClass ContainerClass;
  int             n;
  ERROR_CODE      rc;
    n = -1;
    do  {
      ContainerClass = MakeContainerClass ( ClassID );
      rc = ContainerClass->GetCIF ( CIF,n );
      if (rc==Error_NoError)
        AddData ( ContainerClass );
    } while (rc==Error_NoError);
    delete ContainerClass;
    if (rc==Error_EmptyCIF)
      rc = Error_NoError;
    return rc;
  }

  void  ClassContainer::MakeCIF ( mmcif::PData CIF )  {
  int i;
    for (i=0;i<length;i++)
      if (Container[i])
        Container[i]->MakeCIF ( CIF,i );
  }

  void  ClassContainer::write ( io::RFile f )  {
  int  i,ClassID;
  byte Version=1;
    f.WriteByte ( &Version );
    f.WriteInt  ( &length  );
    for (i=0;i<length;i++)
      if (Container[i])  {
        ClassID = Container[i]->GetClassID();
        f.WriteInt ( &ClassID );
        Container[i]->write ( f );
      } else  {
        ClassID = -1;
        f.WriteInt ( &ClassID );
      }
  }

  PContainerClass ClassContainer::MakeContainerClass ( int ClassID )  {
    if (ClassID==ClassID_String)  return new ContString();
    return new ContainerClass();
  }

  PContainerClass ClassContainer::GetContainerClass (int ContClassNo) {
    if ((ContClassNo<0) || (ContClassNo>=length))  return NULL;
    return Container[ContClassNo];
  }

  void  ClassContainer::Copy ( PClassContainer CContainer )  {
  int i;
    FreeContainer();
    if (CContainer)  {
      length = CContainer->length;
      if (length>0)  {
        Container = new PContainerClass[length];
        for (i=0;i<length;i++)
          if (CContainer->Container[i])  {
            Container[i] = MakeContainerClass (
                              CContainer->Container[i]->GetClassID() );
            Container[i]->Copy ( CContainer->Container[i] );
          } else
            Container[i] = NULL;
      }
    }
  }

  void  ClassContainer::read ( io::RFile f )  {
  int  i,ClassID;
  byte Version;
    FreeContainer();
    f.ReadByte ( &Version );
    f.ReadInt  ( &length  );
    if (length>0)  {
      Container = new PContainerClass[length];
      for (i=0;i<length;i++)  {
        f.ReadInt ( &ClassID );
        if (ClassID>=0)  {
          Container[i] = MakeContainerClass ( ClassID );
          Container[i]->read ( f );
        } else
          Container[i] = NULL;
      }
    }
  }

  MakeStreamFunctions(ClassContainer)



  //  ======================  ID parsers  ==========================


  AtomPath::AtomPath() : io::Stream()  {
    InitAtomPath();
  }

  AtomPath::AtomPath ( cpstr ID ) : io::Stream()  {
    InitAtomPath();
    SetPath ( ID );
  }

  AtomPath::AtomPath  ( io::RPStream Object ) : io::Stream(Object) {
    InitAtomPath();
  }

  AtomPath::~AtomPath() {}

  void AtomPath::InitAtomPath()  {
    modelNo     = 0;
    chainID [0] = char(0);
    seqNum      = MinInt4;
    insCode [0] = char(0);
    resName [0] = char(0);
    atomName[0] = char(0);
    element [0] = char(0);
    altLoc  [0] = char(0);
    isSet       = 0;
  }

  int AtomPath::SetPath ( cpstr ID )  {
  //  1. If ID starts with '/':
  //   /mdl/chn/seq(res).i/atm[elm]:a
  //
  //  2. If ID starts with a letter:
  //        chn/seq(res).i/atm[elm]:a
  //
  //  3. If ID starts with a number:
  //            seq(res).i/atm[elm]:a
  //
  //  4. If ID contains colon ':' then
  //     it may be just
  //                       atm[elm]:a
  //
  //  All spaces are ignored. isSet
  // sets bit for each element present.
  // Any element may be a wildcard '*'.
  // Wildcard for model will set modelNo=0,
  // for sequence number will set
  // seqNum=MinInt4.
  //
  // Returns:
  //   0   <-> Ok
  //   -1  <-> wrong numerical format for model
  //   -2  <-> wrong numerical format for sequence number
  //
  char N[100];
  pstr p,p1;
  int  i,k;

    isSet = 0;  // clear all bits.

    p = pstr(ID);
    while (*p==' ')  p++;

    if (!(*p))  return 0;

    if (*p=='/')  {
      //  model number
      p++;
      i = 0;
      while ((*p) && (*p!='/'))  {
        if (*p!=' ')  N[i++] = *p;
        p++;
      }
      N[i] = char(0);
      if ((!N[0]) || (N[0]=='*'))  modelNo = 0;
      else {
        modelNo = mround(strtod(N,&p1));
        if ((modelNo==0) && (p1==N))  return -1;
      }
      isSet |= APATH_ModelNo;
      if (*p!='/')  return 0;
      p++;
      while (*p==' ')  p++;
    }

    if ((*p<'0') || (*p>'9'))  {
      //  chain ID
      i = 0;
      k = sizeof(ChainID)-1;
      while ((*p) && (*p!='/'))  {
        if ((*p!=' ') && (i<k))  chainID[i++] = *p;
        p++;
      }
      chainID[i] = char(0);
      if (!chainID[0])  {
        chainID[0] = '*';
        chainID[1] = char(0);
      }
      isSet |= APATH_ChainID;
      if (*p!='/')  return 0;
      p++;
      while (*p==' ')  p++;
    }

    if (((*p>='0') && (*p<='9')) || (*p=='-') ||
         (*p=='(') || (*p=='.'))  {
      //  sequence number, residue name and insertion code
      i = 0;
      while ((*p) && (*p!='/'))  {
        if (*p!=' ')  N[i++] = *p;
        p++;
      }
      N[i] = char(0);
      i    = ParseResID ( N,seqNum,insCode,resName );
      if (i==2)  return -2;
      isSet |= APATH_SeqNum | APATH_InsCode | APATH_ResName;
      if (*p!='/')  return 0;
      p++;
      while (*p==' ')  p++;
    }

    if (FirstOccurence(p,':') || FirstOccurence(p,'['))  {
      // atom name, chemical element and alternative location
      i = 0;
      while (*p)  {
        if (*p!=' ')  N[i++] = *p;
        p++;
      }
      N[i] = char(0);
      ParseAtomID ( N,atomName,element,altLoc );
      isSet |= APATH_AtomName | APATH_Element | APATH_AltLoc;
    }

    return 0;

  }

  void AtomPath::write ( io::RFile f )  {
  byte Version=1;
    f.WriteByte ( &Version );
    io::Stream::write ( f );
    f.WriteInt  ( &modelNo );
    f.WriteInt  ( &seqNum  );
    f.WriteInt  ( &isSet   );
    f.WriteTerLine ( chainID ,false );
    f.WriteTerLine ( insCode ,false );
    f.WriteTerLine ( resName ,false );
    f.WriteTerLine ( atomName,false );
    f.WriteTerLine ( element ,false );
    f.WriteTerLine ( altLoc  ,false );
  }

  void AtomPath::read ( io::RFile f )  {
  byte Version;
    f.ReadByte ( &Version );
    io::Stream::read ( f );
    f.ReadInt  ( &modelNo );
    f.ReadInt  ( &seqNum  );
    f.ReadInt  ( &isSet   );
    f.ReadTerLine ( chainID ,false );
    f.ReadTerLine ( insCode ,false );
    f.ReadTerLine ( resName ,false );
    f.ReadTerLine ( atomName,false );
    f.ReadTerLine ( element ,false );
    f.ReadTerLine ( altLoc  ,false );
  }


  MakeStreamFunctions(AtomPath)



  //  --------------------------------------------------------

  QuickSort::QuickSort() : io::Stream()  {
    selSortLimit = 15;
    data         = NULL;
    dlen         = 0;
  }

  QuickSort::QuickSort ( io::RPStream Object )
            : io::Stream(Object)  {
    selSortLimit = 15;
    data         = NULL;
    dlen         = 0;
  }

  int QuickSort::Compare ( int i, int j )  {
  // sort by increasing data[i]
    if (((ivector)data)[i]<((ivector)data)[j])  return -1;
    if (((ivector)data)[i]>((ivector)data)[j])  return  1;
    return 0;
  }

  void QuickSort::Swap ( int i, int j )  {
  int b;
    b = ((ivector)data)[i];
    ((ivector)data)[i] = ((ivector)data)[j];
    ((ivector)data)[j] = b;
  }

  void QuickSort::SelectionSort ( int left, int right )  {
  int i,j,imin;
    for (i=left;i<right;i++) {
      imin = i;
      for (j=i+1;j<=right;j++)
        if (Compare(j,imin)<0)  imin = j;
      Swap ( i,imin );
    }
  }

  int QuickSort::Partition ( int left, int right )  {
  int lv = left;
  int lm = left-1;
  int rm = right+1;
    do  {
      do
        rm--;
      while ((rm>0) && (Compare(rm,lv)>0));
      do
        lm++;
      while ((lm<dlen) && (Compare(lm,lv)<0));
      if (lm<rm)  {
        if (lv==lm)  lv = rm;
        else if (lv==rm)  lv = lm;
        Swap ( lm,rm );
      }
    } while (lm<rm);
    return rm;
  }

  void QuickSort::Quicksort ( int left, int right )  {
  int split_pt;
    if (left<(right-selSortLimit)) {
      split_pt = Partition ( left,right );
      Quicksort ( left,split_pt    );
      Quicksort ( split_pt+1,right );
    } else
      SelectionSort ( left,right );
  }

  void QuickSort::Sort ( void * sortdata, int data_len )  {
    data = sortdata;
    dlen = data_len-1;
    if (data)  Quicksort ( 0,data_len-1 );
  }

  //  --------------------------------------------------------

  void  takeWord ( pstr & p, pstr wrd, cpstr ter, int l )  {
  pstr p1;
  int  i;
    p1 = strpbrk ( p,ter );
    if (!p1)
      p1 = p + strlen(p);
    i = 0;
    while ((p!=p1) && (i<l))  {
      wrd[i++] = *p;
      p++;
    }
    if (i>=l)  i = l-1;
    wrd[i] = char(0);
    p      = p1;
  }


  void ParseAtomID ( cpstr ID, AtomName aname, Element elname,
                               AltLoc   aloc )  {
  pstr p;

    p = pstr(ID);
    while (*p==' ')  p++;

    strcpy ( aname ,"*" );
    strcpy ( elname,"*" );
    if (*p)  aloc[0] = char(0);
       else  strcpy ( aloc,"*" );

    takeWord ( p,aname,pstr("[: "),sizeof(AtomName) );

    if (*p=='[')  {
      p++;
      takeWord ( p,elname,pstr("]: "),sizeof(Element) );
      if (*p==']')  p++;
    }

    if (*p==':')  {
      p++;
      takeWord ( p,aloc,pstr(" "),sizeof(AltLoc) );
    }

  }

  int ParseResID ( cpstr ID, int & sn, InsCode inscode,
                                       ResName resname )  {
  int  RC;
  pstr p,p1;
  char N[100];

    RC = 0;

    p = pstr(ID);
    while (*p==' ')  p++;

    sn = ANY_RES;
    strcpy ( inscode,"*" );
    strcpy ( resname,"*" );

    N[0] = char(0);
    takeWord ( p,N,pstr("(./ "),sizeof(N) );
    if ((!N[0]) || (N[0]=='*'))  {
      sn = ANY_RES;
      RC = 1;
    }
    if (!RC)  {
      sn = mround(strtod(N,&p1));
      if (p1==N)  RC = 2;
            else  inscode[0] = char(0);
    }

    if (*p=='(')  {
      p++;
      takeWord ( p,resname,pstr(")./ "),sizeof(ResName) );
      if (*p==')')  p++;
    }

    if (*p=='.')  {
      p++;
      takeWord ( p,inscode,pstr("/ "),sizeof(InsCode) );
    }

    return RC;

  }

  int ParseAtomPath ( cpstr      ID,
                      int &      mdl,
                      ChainID    chn,
                      int &      sn,
                      InsCode    ic,
                      ResName    res,
                      AtomName   atm,
                      Element    elm,
                      AltLoc     aloc,
                      PAtomPath DefPath )  {
  //   /mdl/chn/seq(res).i/atm[elm]:a, may be partial
  char    N[100];
  pstr    p,p1;
  int     i,RC;
  bool wasRes;

    wasRes = false;

    RC = 0;

    p = pstr(ID);
    while (*p==' ')  p++;

    mdl = 0;
    if (*p=='/')  {
      p++;
      N[0] = char(0);
      takeWord ( p,N,pstr("/"),sizeof(N) );
      if ((!N[0]) || (N[0]=='*'))  mdl = 0;
      else {
        mdl = mround(strtod(N,&p1));
        if ((mdl==0) && (p1==N))  return -1;
      }
    } else if (DefPath)  {
      if (DefPath->isSet & APATH_ModelNo)
        mdl = DefPath->modelNo;
    }

    strcpy ( chn,"*" );
    if ((*p<'0') || (*p>'9') || (*p=='/'))  {
      if (*p=='/')  p++;
      p1 = p;
      chn[0] = char(0);
      takeWord ( p,chn,pstr("/"),sizeof(ChainID) );
      if (strpbrk(chn,"(.[:-"))  { // this was not a chain ID!
        if (DefPath)  {
          if (DefPath->isSet & APATH_ChainID)
            strcpy ( chn,DefPath->chainID );
        } else
          strcpy ( chn,"*" );
        p = p1;
      }
    } else if (DefPath)  {
      if (DefPath->isSet & APATH_ChainID)
        strcpy ( chn,DefPath->chainID );
    }

    if (*p=='/')  p++;
    sn = ANY_RES;
    strcpy ( ic ,"*" );
    strcpy ( res,"*" );
    if (((*p>='0') && (*p<='9')) || (*p=='-') ||
         (*p=='(') || (*p=='.'))  {
      wasRes = true;
      N[0] = char(0);
      takeWord ( p,N,pstr("/"),sizeof(N) );
      i = ParseResID ( N,sn,ic,res );
      if (i==2)  return -2;
    } else if (DefPath)  {
      wasRes = (*p=='/');
      if (DefPath->isSet & APATH_SeqNum)
        sn = DefPath->seqNum;
      if (DefPath->isSet & APATH_InsCode)
        strcpy ( ic,DefPath->insCode );
      if (DefPath->isSet & APATH_ResName)
        strcpy ( res,DefPath->resName );
    }

    if (*p=='/')  p++;
    strcpy ( atm ,"*" );
    strcpy ( elm ,"*" );
    strcpy ( aloc,"*" );
    if (wasRes || FirstOccurence(p,':') || FirstOccurence(p,'['))  {
      ParseAtomID ( p,atm,elm,aloc );
    } else if (DefPath)  {
      if (DefPath->isSet & APATH_AtomName)
        strcpy ( atm,DefPath->atomName );
      if (DefPath->isSet & APATH_Element)
        strcpy ( elm,DefPath->element );
      if (DefPath->isSet & APATH_ResName)
        strcpy ( aloc,DefPath->altLoc );
    }

    if (mdl<=0)       RC |= APATH_WC_ModelNo;
    if (chn[0]=='*')  RC |= APATH_WC_ChainID;
    if (sn==ANY_RES)  RC |= APATH_WC_SeqNum;
    if (ic[0]=='*')   RC |= APATH_WC_InsCode;
    if (res[0]=='*')  RC |= APATH_WC_ResName;
    if (atm[0]=='*')  RC |= APATH_WC_AtomName;
    if (elm[0]=='*')  RC |= APATH_WC_Element;
    if (aloc[0]=='*') RC |= APATH_WC_AltLoc;

    if (RC & (APATH_WC_ModelNo  | APATH_WC_ChainID |
              APATH_WC_SeqNum   | APATH_WC_InsCode |
              APATH_WC_AtomName | APATH_WC_AltLoc))
      RC |= APATH_Incomplete;

    return RC;

  }


  int  ParseSelectionPath (
               cpstr   CID,
               int &   iModel,
               pstr    Chains,
               int &   sNum1,
               InsCode ic1,
               int &   sNum2,
               InsCode ic2,
               pstr    RNames,
               pstr    ANames,
               pstr    Elements,
               pstr    altLocs
                          )  {
  int     l,j;
  pstr    p,p1;
  pstr    N;
  int     seqNum [2];
  InsCode insCode[2];
  pstr    ID;
  bool wasModel,wasChain,wasRes,haveNeg;

    l  = IMax(10,strlen(CID))+1;
    ID = new char[l];
    N  = new char[l];

    p  = pstr(CID);
    p1 = ID;
    while (*p)  {
      if (*p!=' ')  {
        *p1 = *p;
        p1++;
      }
      p++;
    }
    *p1 = char(0);

    p = ID;

    iModel = 0;
    strcpy ( Chains,"*" );
    seqNum[0] = ANY_RES;
    seqNum[1] = ANY_RES;
    strcpy ( insCode[0],"*" );
    strcpy ( insCode[1],"*" );
    strcpy ( RNames    ,"*" );
    strcpy ( ANames    ,"*" );
    strcpy ( Elements  ,"*" );
    strcpy ( altLocs   ,"*" );

    wasModel = false;
    wasChain = false;
    wasRes   = false;

    if (*p=='/')  {
      //  CID starts with the slash -- take model number first
      p++;
      N[0] = char(0);
      takeWord ( p,N,pstr("/"),l );
      if ((!N[0]) || (N[0]=='*'))  iModel = 0;
      else {
        iModel = mround(strtod(N,&p1));
        if ((iModel==0) && (p1==N))  return -1;
      }
      if (*p=='/')  p++;
      wasModel = true;
    }

    if ((*p) && (wasModel || (*p<'0') || (*p>'9')))  {
      p1 = p;
      Chains[0] = char(0);
      takeWord ( p,Chains,pstr("/"),l );
      if (strpbrk(Chains,"(.[:-"))  {  // this was not a chain ID!
        strcpy ( Chains,"*" );
        p = p1;
      } else
        wasChain = true;
      if (*p=='/')  p++;
    }

    if ((*p) && (wasChain  || ((*p>='0') && (*p<='9')) || (*p=='-') ||
                 (*p=='(') || (*p=='.')  || (*p=='*')))  {
      j = 0;
      do  {
        // take the sequence number
        haveNeg  = false;
        if (*p=='-') {
          haveNeg = true;
          p++;
        }
        N[0] = char(0);
        takeWord ( p,N,pstr("(.-/"),l );
        if ((!N[0]) || (N[0]=='*'))
          seqNum[j] = ANY_RES;
        else  {
          seqNum[j] = mround(strtod(N,&p1));
          if (p1==N)   return -2;
          if (haveNeg) seqNum[j] = - seqNum[j];
        }
        // take the residue list
        if (*p=='(')  {
          p++;
          takeWord ( p,RNames,pstr(").-/"),l );
          if (*p==')')  p++;
        }
        // take the insertion code
        if (seqNum[j]!=ANY_RES)
          insCode[j][0] = char(0);
        if (*p=='.')  {
          p++;
          takeWord ( p,insCode[j],pstr("-/"),sizeof(InsCode) );
        }
        if (*p=='-')  {
          p++;
          j++;
        } else  {
          if (j==0)  {
            seqNum[1] = seqNum[0];
            strcpy ( insCode[1],insCode[0] );
          }
          j = 10;
        }
      } while (j<2);
      wasRes = true;
    } else
      wasRes = (*p=='/');

    if (*p=='/')  p++;
    if ((*p) && (wasRes || FirstOccurence(p,':') || FirstOccurence(p,'[')))  {
      if (*p)  altLocs[0] = char(0);
      takeWord ( p,ANames,pstr("[:"),l );
      if (!ANames[0])  strcpy ( ANames,"*" );
      if (*p=='[')  {
        p++;
        takeWord ( p,Elements,pstr("]:"),l );
        if (*p==']')  p++;
      }
      if (*p==':')  {
        p++;
        takeWord ( p,altLocs,pstr(" "),l );
      }
    }

    /*
    printf ( "  iModel   = %i\n"
             "  Chains   = '%s'\n"
             "  seqNum1  = %i\n"
             "  insCode1 = '%s'\n"
             "  seqNum2  = %i\n"
             "  insCode2 = '%s'\n"
             "  RNames   = '%s'\n"
             "  ANames   = '%s'\n"
             "  Elements = '%s'\n"
             "  altLocs  = '%s'\n",
             iModel,Chains,seqNum[0],insCode[0],
             seqNum[1],insCode[1],RNames,ANames,
             Elements,altLocs );
    */

    sNum1 = seqNum[0];
    sNum2 = seqNum[1];
    strcpy ( ic1,insCode[0] );
    strcpy ( ic2,insCode[1] );

    delete[] ID;
    delete[] N;

    return 0;

  }


  void MakeSelectionPath (
               pstr       CID,
               int        iModel,
               cpstr      Chains,
               int        sNum1,
               const InsCode ic1,
               int        sNum2,
               const InsCode ic2,
               cpstr      RNames,
               cpstr      ANames,
               cpstr      Elements,
               cpstr      altLocs
                          )  {
  char S[100];
  int  k;

    if (iModel>0)  {
      sprintf ( CID,"/%i",iModel );
      k = 1;
    } else  {
      CID[0] = char(0);
      k = 0;
    }

    if (Chains[0]!='*')  {
      if (k>0)  strcat ( CID,"/"    );
      strcat ( CID,Chains );
      k = 2;
    }

    if ((sNum1!=-MaxInt4) || (ic1[0]!='*'))  {
      if (k>0)  {
        if (k<2)  strcat ( CID,"/*" );
        strcat  ( CID,"/" );
      }
      if (sNum1>-MaxInt4)  sprintf ( S,"%i",sNum1 );
                     else  strcpy  ( S,"*" );
      if (ic1[0]!='*')  {
        strcat ( S,"." );
        strcat ( S,ic1 );
      }
      strcat ( CID,S );

      if ((sNum2!=-MaxInt4) || (ic2[0]!='*'))  {
        strcat ( CID,"-" );
        if (sNum1>-MaxInt4)  sprintf ( S,"%i",sNum2 );
                       else  strcpy  ( S,"*" );
        if (ic2[0]!='*')  {
          strcat ( S,"." );
          strcat ( S,ic2 );
        }
        strcat ( CID,S );
      }

      k = 3;

    }

    if (RNames[0]!='*')  {
      if      (k<1)  strcat ( CID,"("    );
      else if (k<2)  strcat ( CID,"*/*(" );
      else if (k<3)  strcat ( CID,"/*("  );
      strcat ( CID,RNames );
      strcat ( CID,")"    );
      k = 4;
    }

    if (ANames[0]!='*')  {
      if      (k<1)  strcat ( CID,"/*/*/*/" );  // full path
      else if (k<2)  strcat ( CID,"/*/*/"   );  // /mdl + /*/*/
      else if (k<3)  strcat ( CID,"/*/"     );  // /mdl/chn + /*/
      else if (k<4)  strcat ( CID,"/"       );  // /mdl/chn/res + /
      strcat ( CID,ANames );
      strcat ( CID,")"    );
      k = 5;
    }

    if (Elements[0]!='*')  {
      if      (k<1)  strcat ( CID,"["       );
      else if (k<2)  strcat ( CID,"/*/*/*[" );
      else if (k<3)  strcat ( CID,"/*/*["   );
      else if (k<4)  strcat ( CID,"/*["     );
      else if (k<5)  strcat ( CID,"["       );
      strcat ( CID,Elements );
      strcat ( CID,"]"    );
      k = 6;
    }

    if (altLocs[0]!='*')  {
      if      (k<1)  strcat ( CID,":"       );
      else if (k<2)  strcat ( CID,"/*/*/*:" );
      else if (k<3)  strcat ( CID,"/*/*:"   );
      else if (k<4)  strcat ( CID,"/*:"     );
      else if (k<6)  strcat ( CID,":"       );
      strcat ( CID,altLocs );
    }

  }

} // namespace mmdb