File: residue.cpp

package info (click to toggle)
openbabel 2.0.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 15,652 kB
  • ctags: 15,998
  • sloc: cpp: 128,176; ansic: 86,170; sh: 8,920; perl: 1,847; python: 1,369; pascal: 793; makefile: 381; xml: 97
file content (1604 lines) | stat: -rw-r--r-- 46,033 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
/**********************************************************************
residue.cpp - Handle macromolecule residues.
 
Copyright (C) 2001, 2002  OpenEye Scientific Software, Inc.
Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison
 
This file is part of the Open Babel project.
For more information, see <http://openbabel.sourceforge.net/>
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
 
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 General Public License for more details.
***********************************************************************/

/**********************************************************************
Global arrays Residue, ElemDesc and function GetResidueNumber were
obtained in part or whole from RasMol2 by Roger Sayle.
***********************************************************************/

///////////////////////////////////////////////////////////////////////////////
// File Includes
///////////////////////////////////////////////////////////////////////////////

#include "babelconfig.h"

#if HAVE_IOSTREAM
#include <iostream>
#elif HAVE_IOSTREAM_H
#include <iostream.h>
#endif

#if HAVE_FSTREAM
#include <fstream>
#elif HAVE_FSTREAM_H
#include <fstream.h>
#endif

#include "mol.h"
#include "bitvec.h"

using namespace std;

namespace OpenBabel
{
/** \class OBResidue
    \brief Residue information
 
    The residue information is drawn from PDB or MOL2 files (or similar), which
    track biomolecule information,
    and are stored in the OBResidue class. OBResidues are stored inside the 
    OBAtom class and OBMol classes. 
    The residue information for an atom can be requested in 
    the following way:
\code
 OBAtom *atom;
 OBResidue *r;
 atom = mol.GetAtom(1);
 r = atom->GetResidue();
\endcode
    The residue information for a molecule can be manipulated too:
\code
  cout << "This molecule has " << mol.NumResidues() << " residues." << endl;
  OBResidue *r;
  r = mol.GetResidue(1);
\endcode
*/

///////////////////////////////////////////////////////////////////////////////
// Global Definitions
///////////////////////////////////////////////////////////////////////////////

#define MAXSETNO 40
#define MAXELEM  1024
#define MINELEM  29
#define MAXRES   100
#define MINRES   54

///////////////////////////////////////////////////////////////////////////////
// Amino Acid Definitions
///////////////////////////////////////////////////////////////////////////////

#define AA_ALA (1<<1)
#define AA_GLY (1<<2)
#define AA_LEU (1<<3)
#define AA_SER (1<<4)
#define AA_VAL (1<<5)
#define AA_THR (1<<6)
#define AA_LYS (1<<7)
#define AA_ASP (1<<8)
#define AA_ILE (1<<9)
#define AA_ASN (1<<10)
#define AA_GLU (1<<11)
#define AA_PRO (1<<12)
#define AA_ARG (1<<13)
#define AA_PHE (1<<14)
#define AA_GLN (1<<15)
#define AA_TYR (1<<16)
#define AA_HIS (1<<17)
#define AA_CYS (1<<18)
#define AA_MET (1<<19)
#define AA_TRP (1<<20)

//! Residue property definitions
namespace OBAminoAcidProperty
{
static const unsigned int ACIDIC      =  0;
static const unsigned int ACYCLIC     =  1;
static const unsigned int ALIPHATIC   =  2;
static const unsigned int AROMATIC    =  3;
static const unsigned int BASIC       =  4;
static const unsigned int BURIED      =  5;
static const unsigned int CHARGED     =  6;
static const unsigned int CYCLIC      =  7;
static const unsigned int HYDROPHOBIC =  8;
static const unsigned int LARGE       =  9;
static const unsigned int MEDIUM      = 10;
static const unsigned int NEGATIVE    = 11;
static const unsigned int NEUTRAL     = 12;
static const unsigned int POLAR       = 13;
static const unsigned int POSITIVE    = 14;
static const unsigned int SMALL       = 15;
static const unsigned int SURFACE     = 16;
}

//! Residue atom properties
namespace OBResidueAtomProperty
{
static const unsigned int ALPHA_CARBON     = 0;
static const unsigned int AMINO_BACKBONE   = 1;
static const unsigned int BACKBONE         = 2;
static const unsigned int CYSTEINE_SULPHUR = 3;
static const unsigned int LIGAND           = 4;
static const unsigned int NUCLEIC_BACKBONE = 5;
static const unsigned int SHAPELY_BACKBONE = 6;
static const unsigned int SHAPELY_SPECIAL  = 7;
static const unsigned int SIDECHAIN        = 8;
static const unsigned int SUGAR_PHOSPHATE  = 9;
}
/// Residue names
namespace OBResidueIndex
{
static const unsigned int ALA   =  0;
static const unsigned int GLY   =  1;
static const unsigned int LEU   =  2;
static const unsigned int SER   =  3;
static const unsigned int VAL   =  4;
static const unsigned int THR   =  5;
static const unsigned int LYS   =  6;
static const unsigned int ASP   =  7;
static const unsigned int ILE   =  8;
static const unsigned int ASN   =  9;
static const unsigned int GLU   = 10;
static const unsigned int PRO   = 11;
static const unsigned int ARG   = 12;
static const unsigned int PHE   = 13;
static const unsigned int GLN   = 14;
static const unsigned int TYR   = 15;
static const unsigned int HIS   = 16;
static const unsigned int CYS   = 17;
static const unsigned int MET   = 18;
static const unsigned int TRP   = 19;
static const unsigned int ASX   = 20;
static const unsigned int GLX   = 21;
static const unsigned int PCA   = 22;
static const unsigned int HYP   = 23;
static const unsigned int A     = 24;
static const unsigned int C     = 25;
static const unsigned int G     = 26;
static const unsigned int T     = 27;
static const unsigned int U     = 28;
static const unsigned int UPLUS = 29;
static const unsigned int I     = 30;
//static const unsigned int _1MA  = 31;
//static const unsigned int _5MC  = 32;
static const unsigned int OMC   = 33;
//static const unsigned int _1MG  = 34;
//static const unsigned int _2MG  = 35;
static const unsigned int M2G   = 36;
//static const unsigned int _7MG  = 37;
static const unsigned int OMG   = 38;
static const unsigned int YG    = 39;
static const unsigned int H2U   = 40;
//static const unsigned int _5MU  = 41;
static const unsigned int PSU   = 42;
static const unsigned int UNK   = 43;
static const unsigned int ACE   = 44;
static const unsigned int FOR   = 45;
static const unsigned int HOH   = 46;
static const unsigned int DOD   = 47;
static const unsigned int SO4   = 48;
static const unsigned int PO4   = 49;
static const unsigned int NAD   = 50;
static const unsigned int COA   = 51;
static const unsigned int NAP   = 52;
static const unsigned int NDP   = 53;
}
/// Residue types.
namespace OBResidueProperty
{
static const unsigned int AMINO        = 0;
static const unsigned int AMINO_NUCLEO = 1;
static const unsigned int COENZYME     = 2;
static const unsigned int ION          = 3;
static const unsigned int NUCLEO       = 4;
static const unsigned int PROTEIN      = 5;
static const unsigned int PURINE       = 6;
static const unsigned int PYRIMIDINE   = 7;
static const unsigned int SOLVENT      = 8;
static const unsigned int WATER        = 9;
}

///////////////////////////////////////////////////////////////////////////////
// Amino Acid Property Definitions
///////////////////////////////////////////////////////////////////////////////

#define IS_ACIDIC(x)      ((x) & ((AA_ASP)|(AA_GLU)))
#define IS_ACYCLIC(x)     ((x) & ((AA_ALA)|(AA_GLY)|(AA_LEU)|(AA_SER)| \
                                  (AA_VAL)|(AA_THR)|(AA_LYS)|(AA_ASP)| \
                                  (AA_ILE)|(AA_ASN)|(AA_GLU)|(AA_GLN)| \
                                  (AA_CYS)|(AA_MET)))
#define IS_ALIPHATIC(x)   ((x) & ((AA_ALA)|(AA_GLY)|(AA_ILE)|(AA_LEU)| \
                                  (AA_VAL)))
#define IS_AROMATIC(x)    ((x) & ((AA_HIS)|(AA_PHE)|(AA_TRP)|(AA_TYR)))
#define IS_BASIC(x)       ((x) & ((AA_ARG)|(AA_HIS)|(AA_LYS)))
#define IS_BURIED(x)      ((x) & ((AA_ALA)|(AA_CYS)|(AA_ILE)|(AA_LEU)| \
                                  (AA_MET)|(AA_PHE)|(AA_TRP)|(AA_VAL)))
#define IS_CHARGED(x)     ((x) & ((AA_ASP)|(AA_GLU)|(AA_ARG)|(AA_HIS)| \
                                  (AA_LYS)))
#define IS_CYCLIC(x)      ((x) & ((AA_HIS)|(AA_PHE)|(AA_PRO)|(AA_TRP)| \
                                  (AA_TYR)))
#define IS_HYDROPHOBIC(x) ((x) & ((AA_ALA)|(AA_LEU)|(AA_VAL)|(AA_ILE)| \
                                  (AA_PRO)|(AA_PHE)|(AA_MET)|(AA_TRP)))
#define IS_LARGE(x)       ((x) & ((AA_ARG)|(AA_PHE)|(AA_GLN)|(AA_TYR)| \
                                  (AA_HIS)|(AA_LEU)|(AA_LYS)|(AA_ILE)| \
                                  (AA_GLU)|(AA_MET)|(AA_TRP)))
#define IS_MEDIUM(x)      ((x) & ((AA_VAL)|(AA_THR)|(AA_ASP)|(AA_ASN)| \
                                  (AA_PRO)|(AA_CYS)))
#define IS_NEGATIVE(x)    ((x) & ((AA_ASP)|(AA_GLU)))
#define IS_NEUTRAL(x)     ((x) & ((AA_ALA)|(AA_GLY)|(AA_LEU)|(AA_SER)| \
                                  (AA_VAL)|(AA_THR)|(AA_PHE)|(AA_GLN)| \
                                  (AA_TYR)|(AA_HIS)|(AA_CYS)|(AA_MET)| \
                                  (AA_TRP)|(AA_ILE)|(AA_ASN)|(AA_PRO)))
#define IS_POLAR(x)       ((x) & ((AA_ASP)|(AA_ILE)|(AA_ASN)|(AA_GLU)| \
                                  (AA_SER)|(AA_THR)|(AA_ARG)|(AA_GLN)| \
                                  (AA_CYS)|(AA_HIS)))
#define IS_POSITIVE(x)    ((x) & ((AA_ARG)|(AA_HIS)|(AA_LYS)))
#define IS_SMALL(x)       ((x) & ((AA_ALA)|(AA_GLY)|(AA_SER)))
#define IS_SURFACE(x)     ((x) & ((AA_THR)|(AA_LYS)|(AA_ASP)|(AA_ILE)| \
                                  (AA_ASN)|(AA_GLU)|(AA_PRO)|(AA_ARG)| \
                                  (AA_GLY)|(AA_SER)|(AA_GLN)|(AA_TYR)| \
                                  (AA_HIS)))

////////////////////////////////////////////////////////////////////////////////
// Global Variables
////////////////////////////////////////////////////////////////////////////////

static char Residue[MAXRES][4] = {
                                     /*===============*/
                                     /*  Amino Acids  */
                                     /*===============*/

                                     /* Ordered by Cumulative Frequency in Brookhaven *
                                      * Protein Databank, December 1991               */

                                     "ALA", /* 8.4% */     "GLY", /* 8.3% */
                                     "LEU", /* 8.0% */     "SER", /* 7.5% */
                                     "VAL", /* 7.1% */     "THR", /* 6.4% */
                                     "LYS", /* 5.8% */     "ASP", /* 5.5% */
                                     "ILE", /* 5.2% */     "ASN", /* 4.9% */
                                     "GLU", /* 4.9% */     "PRO", /* 4.4% */
                                     "ARG", /* 3.8% */     "PHE", /* 3.7% */
                                     "GLN", /* 3.5% */     "TYR", /* 3.5% */
                                     "HIS", /* 2.3% */     "CYS", /* 2.0% */
                                     "MET", /* 1.8% */     "TRP", /* 1.4% */

                                     "ASX", "GLX", "PCA", "HYP",

                                     /*===================*/
                                     /*  DNA Nucleotides  */
                                     /*===================*/
                                     "  A", "  C", "  G", "  T",

                                     /*===================*/
                                     /*  RNA Nucleotides  */
                                     /*===================*/
                                     "  U", " +U", "  I", "1MA",
                                     "5MC", "OMC", "1MG", "2MG",
                                     "M2G", "7MG", "OMG", " YG",
                                     "H2U", "5MU", "PSU",

                                     /*=================*/
                                     /*  Miscellaneous  */
                                     /*=================*/
                                     "UNK", "ACE", "FOR", "HOH",
                                     "DOD", "SO4", "PO4", "NAD",
                                     "COA", "NAP", "NDP"
                                 };

/* Avoid SGI Compiler Warnings! */
static char ElemDesc[MAXELEM][4] = {
                                       { ' ', 'N', ' ', ' ' },  /* 0*/
                                       { ' ', 'C', 'A', ' ' },  /* 1*/
                                       { ' ', 'C', ' ', ' ' },  /* 2*/
                                       { ' ', 'O', ' ', ' ' },  /* 3*/   /* 0-3   Amino Acid Backbone    */
                                       { ' ', 'C', '\'', ' ' }, /* 4*/
                                       { ' ', 'O', 'T', ' ' },  /* 5*/
                                       { ' ', 'S', ' ', ' ' },  /* 6*/
                                       { ' ', 'P', ' ', ' ' },  /* 7*/   /* 4-7   Shapely Amino Backbone */
                                       { ' ', 'O', '1', 'P' },  /* 8*/
                                       { ' ', 'O', '2', 'P' },  /* 9*/
                                       { ' ', 'O', '5', '*' },  /*10*/
                                       { ' ', 'C', '5', '*' },  /*11*/
                                       { ' ', 'C', '4', '*' },  /*12*/
                                       { ' ', 'O', '4', '*' },  /*13*/
                                       { ' ', 'C', '3', '*' },  /*14*/
                                       { ' ', 'O', '3', '*' },  /*15*/
                                       { ' ', 'C', '2', '*' },  /*16*/
                                       { ' ', 'O', '2', '*' },  /*17*/
                                       { ' ', 'C', '1', '*' },  /*18*/   /* 7-18  Nucleic Acid Backbone  */
                                       { ' ', 'C', 'A', '2' },  /*19*/   /* 19    Shapely Special        */
                                       { ' ', 'S', 'G', ' ' },  /*20*/   /* 20    Cysteine Sulphur       */
                                       { ' ', 'N', '1', ' ' },  /*21*/
                                       { ' ', 'N', '2', ' ' },  /*22*/
                                       { ' ', 'N', '3', ' ' },  /*23*/
                                       { ' ', 'N', '4', ' ' },  /*24*/
                                       { ' ', 'N', '6', ' ' },  /*25*/
                                       { ' ', 'O', '2', ' ' },  /*26*/
                                       { ' ', 'O', '4', ' ' },  /*27*/
                                       { ' ', 'O', '6', ' ' }   /*28*/   /* 21-28 Nucleic Acid H-Bonding */
                                   };

static unsigned int ResNo  = MINRES;
static unsigned int ElemNo = MINELEM;

///////////////////////////////////////////////////////////////////////////////
// Residue Functions
///////////////////////////////////////////////////////////////////////////////

static unsigned int GetAtomIDNumber(const char *atomid)
{
    if (atomid != NULL)
    {
        int ch1 = toupper(atomid[0]);
        int ch2 = toupper(atomid[1]);
        int ch3 = toupper(atomid[2]);
        int ch4 = toupper(atomid[3]);

        if (ch1 == ' ')
        {
            switch(ch2)
            {
            case 'C':

                switch(ch3)
                {
                case 'A':

                    if (ch4 == ' ')
                        return 1;
                    else if (ch4 == '2')
                        return 19;

                    break;

                case ' ':
                    if (ch4 == ' ')
                        return 2;
                    break;

                case '\'':
                    if (ch4 == ' ')
                        return 4;
                    break;

                case '1':
                    if (ch4 == '*')
                        return 18;
                    break;

                case '2':
                    if (ch4 == '*')
                        return 16;
                    break;

                case '3':
                    if (ch4 == '*')
                        return 14;
                    break;

                case '4':
                    if (ch4 == '*')
                        return 12;
                    break;

                case '5':
                    if (ch4 == '*')
                        return 11;
                    break;

                }

                break;

            case 'N':

                if (ch4 == ' ')
                {
                    switch (ch3)
                    {
                    case ' ':
                        return 0;
                    case '1':
                        return 21;
                    case '2':
                        return 22;
                    case '3':
                        return 23;
                    case '4':
                        return 24;
                    case '6':
                        return 25;
                    }
                }

                break;

            case 'O':

                switch(ch3)
                {
                case ' ':

                    if (ch4 == ' ')
                        return 3;

                    break;

                case 'T':

                    if (ch4 == ' ')
                        return 5;

                    break;

                case '1':

                    if (ch4 == 'P')
                        return 8;

                    break;

                case '2':

                    if (ch4 == 'P')
                        return 9;
                    else if (ch4 == '*')
                        return 17;
                    else if (ch4 == ' ')
                        return 26;

                    break;

                case '3':

                    if (ch4 == '*')
                        return 15;

                    break;

                case '4':

                    if (ch4 == '*')
                        return 13;
                    else if (ch4 == ' ')
                        return 27;

                    break;

                case '5':

                    if (ch4 == '*')
                        return 10;

                    break;

                case '6':

                    if (ch4 == ' ')
                        return 28;

                    break;
                }

                break;

            case 'P':

                if ((ch3 == ' ') && (ch4 == ' '))
                    return 7;

                break;

            case 'S':

                if (ch4 == ' ')
                {
                    if (ch3 == ' ')
                        return 6;
                    else if (ch3 == 'G')
                        return 20;
                }

                break;
            }
        }

        unsigned int refno;
        for( refno = MINELEM ; refno < ElemNo ; refno++ )
            if( !strncmp(ElemDesc[refno], atomid, 4) )
                return refno;

        if ( ElemNo < MAXELEM - 1 )
        {
            ElemNo++;
            ElemDesc[refno][0] = (char) ch1;
            ElemDesc[refno][1] = (char) ch2;
            ElemDesc[refno][2] = (char) ch3;
            ElemDesc[refno][3] = (char) ch4;
            return refno;
        }
        else
        {
	  obErrorLog.ThrowError(__FUNCTION__, "Maximum number of atom ids exceeded", obWarning);
	  return 0;
        }
    }
    else
    {
      obErrorLog.ThrowError(__FUNCTION__, "NULL Atom IDs specified", obWarning);
      return 0;
    }
}

static unsigned int GetResidueNumber(const char *res)
{
    if (res != NULL)
    {
        int ch1 = toupper(res[0]);
        int ch2 = toupper(res[1]);
        int ch3 = toupper(res[2]);

        switch( ch1 )
        {
        case(' '):

                        if( ch2 == ' ' )
                {
                    switch( ch3 )
                    {
                    case('A'):  return( 24 );
                    case('C'):  return( 25 );
                    case('G'):  return( 26 );
                    case('T'):  return( 27 );
                    case('U'):  return( 28 );
                    case('I'):  return( 30 );
                    }
                }
                else if( ch2 == '+' )
                {
                    if( ch3 == 'U' )
                        return( 29 );
                }
                else if( ch2 == 'Y' )
                {
                    if( ch3 == 'G' )
                        return( 39 );
                }

            break;

        case('0'):

                        if( ch2 == 'M' )
                {
                    if( ch3 == 'C' )
                        return( 33 );
                    else if( ch3 == 'G' )
                        return( 38 );
                }

            break;

        case('1'):

                        if( ch2 == 'M' )
                {
                    if( ch3 == 'A' )
                        return( 31 );
                    else if( ch3 == 'G' )
                        return( 34 );
                }

            break;

        case('2'):

                        if( ch2 == 'M' )
                            if( ch3 == 'G' )
                                return( 35 );

            break;

        case('5'):

                        if( ch2 == 'M' )
                {
                    if( ch3 == 'C' )
                        return( 32 );
                    else if( ch3 == 'U' )
                        return( 41 );
                }

            break;

        case('7'):

                        if( ch2 == 'M' )
                            if( ch3 == 'G' )
                                return( 37 );

            break;

        case('A'):

                        if( ch2 == 'L' )
                {
                    if( ch3 == 'A' )
                        return(  0 );
                }
                else if( ch2 == 'S' )
                {
                    if( ch3 == 'P' )
                        return(  7 );
                    else if( ch3 == 'N' )
                        return(  9 );
                    else if( ch3 == 'X' )
                        return( 20 );
                }
                else if( ch2 == 'R' )
                {
                    if( ch3 == 'G' )
                        return( 12 );
                }
                else if( ch2 == 'C' )
                {
                    if( ch3 == 'E' )
                        return( 44 );
                }
                else if( ch2 == 'D' )
                {
                    if( ch3 == 'E' )
                        return( 24 );    /* "ADE" -> "  A" */
                }

            break;

        case('C'):

                        if( ch2 == 'Y' )
                {
                    if( ch3 == 'S' )
                        return( 17 );
                    else if( ch3 == 'H' )
                        return( 17 );    /* "CYH" -> "CYS" */
                    else if( ch3 == 'T' )
                        return( 25 );    /* "CYT" -> "  C" */
                }
                else if( ch2 == 'O' )
                {
                    if( ch3 == 'A' )
                        return( 51 );
                }
                else if( ch2 == 'P' )
                {
                    if( ch3 == 'R' )
                        return( 11 );    /* "CPR" -> "PRO" */
                }
                else if( ch2 == 'S' )
                {
                    if( ch3 == 'H' )
                        return( 17 );    /* "CSH" -> "CYS" */
                    else if( ch3 == 'M' )
                        return( 17 );    /* "CSM" -> "CYS" */
                }

            break;

        case('D'):

                        if( ch2 == 'O' )
                {
                    if( ch3 == 'D' )
                        return( 47 );
                }
                else if( ch2 == '2' )
                {
                    if( ch3 == 'O' )
                        return( 47 );    /* "D2O" -> "DOD" */
                }

            break;

        case('F'):

                        if( ch2 == 'O' )
                            if( ch3 == 'R' )
                                return( 45 );

            break;

        case('G'):

                        if( ch2 == 'L' )
                {
                    if( ch3 == 'Y' )
                        return(  1 );
                    else if( ch3 == 'U' )
                        return( 10 );
                    else if( ch3 == 'N' )
                        return( 14 );
                    else if( ch3 == 'X' )
                        return( 21 );
                }
                else if( ch2 == 'U' )
                {
                    if( ch3 == 'A' )
                        return( 26 );    /* "GUA" -> "  G" */
                }

            break;

        case('H'):

                        if( ch2 == 'I' )
                {
                    if( ch3 == 'S' )
                        return( 16 );
                }
                else if( ch2 == 'O' )
                {
                    if( ch3 == 'H' )
                        return( 46 );
                }
                else if( ch2 == 'Y' )
                {
                    if( ch3 == 'P' )
                        return( 23 );
                }
                else if( ch2 == '2' )
                {
                    if( ch3 == 'O' )
                        return( 46 );    /* "H20" -> "HOH" */
                    else if( ch3 == 'U' )
                        return( 40 );
                }

            break;

        case('I'):

                        if( ch2 == 'L' )
                            if( ch3 == 'E' )
                                return(  8 );

            break;

        case('L'):

                        if( ch2 == 'E' )
                {
                    if( ch3 == 'U' )
                        return(  2 );
                }
                else if( ch2 == 'Y' )
                {
                    if( ch3 == 'S' )
                        return(  6 );
                }

            break;

        case('M'):

                        if( ch2 == 'E' )
                {
                    if( ch3 == 'T' )
                        return( 18 );
                }
                else if( ch2 == '2' )
                {
                    if( ch3 == 'G' )
                        return( 36 );
                }

            break;

        case('N'):

                        if( ch2 == 'A' )
                {
                    if( ch3 == 'D' )
                        return( 50 );
                    else if( ch3 == 'P' )
                        return( 52 );
                }
                else if( ch2 == 'D' )
                {
                    if( ch3 == 'P' )
                        return( 53 );
                }

            break;

        case('P'):

                        if( ch2 == 'R' )
                {
                    if( ch3 == 'O' )
                        return( 11 );
                }
                else if( ch2 == 'H' )
                {
                    if( ch3 == 'E' )
                        return( 13 );
                }
                else if( ch2 == 'C' )
                {
                    if( ch3 == 'A' )
                        return( 22 );
                }
                else if( ch2 == 'O' )
                {
                    if( ch3 == '4' )
                        return( 49 );
                }
                else if( ch2 == 'S' )
                {
                    if( ch3 == 'U' )
                        return( 42 );
                }

            break;

        case('S'):

                        if( ch2 == 'E' )
                {
                    if( ch3 == 'R' )
                        return(  3 );
                }
                else if( ch2 == 'O' )
                {
                    if( ch3 == '4' )
                        return( 48 );
                    else if( ch3 == 'L' )
                        return( 46 );    /* "SOL" -> "HOH" */
                }
                else if( ch2 == 'U' )
                {
                    if( ch3 == 'L' )
                        return( 48 );    /* "SUL" -> "SO4" */
                }

            break;

        case('T'):

                        if( ch2 == 'H' )
                {
                    if( ch3 == 'R' )
                        return(  5 );
                    else if( ch3 == 'Y' )
                        return( 27 );    /* "THY" -> "  T" */
                }
                else if( ch2 == 'Y' )
                {
                    if( ch3 == 'R' )
                        return( 15 );
                }
                else if( ch2 == 'R' )
                {
                    if( ch3 == 'P' )
                        return( 19 );
                    else if( ch3 == 'Y' )
                        return( 19 );    /* "TRY" -> "TRP" */
                }
                else if( ch2 == 'I' )
                {
                    if( ch3 == 'P' )
                        return( 46 );    /* "TIP" -> "HOH" */
                }

            break;

        case('U'):

                        if( ch2 == 'N' )
                {
                    if( ch3 == 'K' )
                        return( 43 );
                }
                else if( ch2 == 'R' )
                {
                    if( ch3 == 'A' )
                        return( 28 );    /* "URA" -> "  U" */
                    else if( ch3 == 'I' )
                        return( 28 );    /* "URI" -> "  U" */
                }

            break;

        case('V'):

                        if( ch2 == 'A' )
                            if( ch3 == 'L' )
                                return(  4 );

            break;

        case('W'):

                        if( ch2 == 'A' )
                            if( ch3 == 'T' )
                                return( 46 );    /* "WAT" -> "HOH" */

            break;
        }

        unsigned int refno;
        for( refno = MINRES; refno < ResNo ; refno++ )
            if( !strncmp(Residue[refno],res,3) )
                return refno;

        if ( ResNo < MAXRES - 1 )
{
            ResNo++;
            Residue[refno][0] = (char) ch1;
            Residue[refno][1] = (char) ch2;
            Residue[refno][2] = (char) ch3;
            return refno;
        }
    }

    return OBResidueIndex::UNK;
}

static void SetResidueKeys(const char   *residue,
                           unsigned int &reskey,
                           unsigned int &aakey)
{
    reskey = GetResidueNumber(residue);
    switch(reskey)
    {
    case OBResidueIndex::ALA:
        aakey = AA_ALA;
        break;
    case OBResidueIndex::GLY:
        aakey = AA_GLY;
        break;
    case OBResidueIndex::LEU:
        aakey = AA_LEU;
        break;
    case OBResidueIndex::SER:
        aakey = AA_SER;
        break;
    case OBResidueIndex::VAL:
        aakey = AA_VAL;
        break;
    case OBResidueIndex::THR:
        aakey = AA_THR;
        break;
    case OBResidueIndex::LYS:
        aakey = AA_LYS;
        break;
    case OBResidueIndex::ASP:
        aakey = AA_ASP;
        break;
    case OBResidueIndex::ILE:
        aakey = AA_ILE;
        break;
    case OBResidueIndex::ASN:
        aakey = AA_ASN;
        break;
    case OBResidueIndex::GLU:
        aakey = AA_GLU;
        break;
    case OBResidueIndex::PRO:
        aakey = AA_PRO;
        break;
    case OBResidueIndex::ARG:
        aakey = AA_ARG;
        break;
    case OBResidueIndex::PHE:
        aakey = AA_PHE;
        break;
    case OBResidueIndex::GLN:
        aakey = AA_GLN;
        break;
    case OBResidueIndex::TYR:
        aakey = AA_TYR;
        break;
    case OBResidueIndex::HIS:
        aakey = AA_HIS;
        break;
    case OBResidueIndex::CYS:
        aakey = AA_CYS;
        break;
    case OBResidueIndex::MET:
        aakey = AA_MET;
        break;
    case OBResidueIndex::TRP:
        aakey = AA_TRP;
        break;
    default:
        aakey = 0;
        break;
    }
}

///////////////////////////////////////////////////////////////////////////////
// OBResidue: Constructors / Destructor
///////////////////////////////////////////////////////////////////////////////

OBResidue::OBResidue()
{
    _chain    = 'A';
    _idx      = 0;
    _aakey    = 0;
    _reskey   = OBResidueIndex::UNK;
    _resnum   = 0;
    _resname  = "";
    _vdata.clear();
}

OBResidue::OBResidue(const OBResidue &src)
// Currently does not copy vdata information
{
    _chain    = src._chain;
    _aakey    = src._aakey;
    _reskey   = src._reskey;
    _resnum   = src._resnum;
    _resname  = src._resname;
    _atomid   = src._atomid;
    _hetatm   = src._hetatm;
    _sernum   = src._sernum;
}

OBResidue::~OBResidue()
{
    vector<OBAtom*>::iterator a;
    for ( a = _atoms.begin() ; a != _atoms.end() ; a++ )
        (*a)->SetResidue(NULL);
    _atoms.clear();
    if (!_vdata.empty())
    {
        vector<OBGenericData*>::iterator m;
        for (m = _vdata.begin();m != _vdata.end();m++)
            delete *m;
        _vdata.clear();
    }
}

///////////////////////////////////////////////////////////////////////////////
// OBResidue: Operator Overloads
///////////////////////////////////////////////////////////////////////////////

OBResidue &OBResidue::operator = (const OBResidue &src)
//copy residue information
// Currently does not copy vdata informtion
{
    if (this != &src)
    {
        _chain    = src._chain;
        _aakey    = src._aakey;
        _reskey   = src._reskey;
        _resnum   = src._resnum;
        _resname  = src._resname;
        _atomid   = src._atomid;
        _hetatm   = src._hetatm;
        _sernum   = src._sernum;
    }

    return(*this);
}

///////////////////////////////////////////////////////////////////////////////
// OBResidue: Data Access / Manipulation
///////////////////////////////////////////////////////////////////////////////

void OBResidue::AddAtom(OBAtom *atom)
{
    if (atom != NULL)
    {
        atom->SetResidue(this);

        _atoms.push_back(atom);
        _atomid.push_back("");
        _hetatm.push_back(false);
        _sernum.push_back(0);
    }
}

void OBResidue::InsertAtom(OBAtom *atom)
{
    if (atom != NULL)
    {
        atom->SetResidue(this);

        _atoms.push_back(atom);
        _atomid.push_back("");
        _hetatm.push_back(false);
        _sernum.push_back(0);
    }
}

void OBResidue::RemoveAtom(OBAtom *atom)
{
    if (atom != NULL)
    {
        for ( unsigned int i = 0 ; i < _atoms.size() ; i++ )
        {
            if (_atoms[i] == atom)
            {
                atom->SetResidue(NULL);
                _atoms.erase(_atoms.begin() + i);
                _atomid.erase(_atomid.begin() + i);
                _hetatm.erase(_hetatm.begin() + i);
                _sernum.erase(_sernum.begin() + i);
            }
        }
    }
}

void OBResidue::Clear(void)
{
    for (unsigned int i = 0 ; i < _atoms.size() ; i++ )
        _atoms[i]->SetResidue(NULL);

    _chain   = 'A';
    _idx     = 0;
    _aakey   = 0;
    _reskey  = OBResidueIndex::UNK;
    _resnum  = 0;
    _resname = "";

    _atoms.clear();
    _atomid.clear();
    _hetatm.clear();
    _sernum.clear();
}

void OBResidue::SetChain(char chain)
{
    _chain = chain;
}

void OBResidue::SetChainNum(unsigned int chainnum)
{
    _chain = (char) ('A' + chainnum - 1);
}

void OBResidue::SetIdx(unsigned int idx)
{
    _idx = idx;
}

void OBResidue::SetName(const string &name)
{
    _resname = name;
    SetResidueKeys(_resname.c_str(), _reskey, _aakey);
}

void OBResidue::SetNum(unsigned int resnum)
{
    _resnum = resnum;
}

void OBResidue::SetAtomID(OBAtom *atom, const string &id)
{
    for ( unsigned int i = 0 ; i < _atoms.size() ; i++ )
        if (_atoms[i] == atom)
            _atomid[i] = id;
}

void OBResidue::SetHetAtom(OBAtom *atom, bool hetatm)
{
    for ( unsigned int i = 0 ; i < _atoms.size() ; i++ )
        if (_atoms[i] == atom)
            _hetatm[i] = hetatm;
}

void OBResidue::SetSerialNum(OBAtom *atom, unsigned int sernum)
{
    for ( unsigned int i = 0 ; i < _atoms.size() ; i++ )
        if (_atoms[i] == atom)
            _sernum[i] = sernum;
}

vector<OBAtom*> OBResidue::GetAtoms(void) const
{
    return _atoms;
}

vector<OBBond*> OBResidue::GetBonds(bool exterior) const
{
    OBAtom         *atom;
    vector<OBBond*> bonds;
    OBBitVec        idxs;
    unsigned int    sz;

    sz = (unsigned int) _atoms.size();
    for ( unsigned int i = 0 ; i < sz ; i++ )
    {
        atom = _atoms[i];
        OBBond *bond;
        vector<OBEdgeBase*>::iterator b;
        for (bond = atom->BeginBond(b) ; bond ; bond = atom->NextBond(b))
        {
            if (!idxs.BitIsOn(bond->GetIdx()))
            {
                if (!exterior)
                {
                    if (bond->GetNbrAtom(atom)->GetResidue() == this)
                        bonds.push_back(&(*bond));
                }
                else
                    bonds.push_back(&(*bond));

                idxs.SetBitOn(bond->GetIdx());
            }
        }
    }

    return bonds;
}

string OBResidue::GetName(void) const
{
    return _resname;
}

unsigned int OBResidue::GetNum(void) const
{
    return _resnum;
}

unsigned int OBResidue::GetNumAtoms(void) const
{
    return (unsigned int)_atoms.size();
}

char OBResidue::GetChain(void) const
{
    return _chain;
}

unsigned int OBResidue::GetChainNum(void) const
{
    if (isdigit(_chain))
        return (_chain - '0');
    else
        return (_chain - 'A' + 1);
}

unsigned int OBResidue::GetIdx(void) const
{
    return(_idx);
}

unsigned int OBResidue::GetResKey(void) const
{
    return(_reskey);
}

string OBResidue::GetAtomID(OBAtom *atom) const
{
    for ( unsigned int i = 0 ; i < _atoms.size() ; i++ )
        if (_atoms[i] == atom)
            return _atomid[i];
    return "";
}

unsigned int OBResidue::GetSerialNum(OBAtom *atom) const
{
    for ( unsigned int i = 0 ; i < _atoms.size() ; i++ )
        if (_atoms[i] == atom)
            return _sernum[i];
    return 0;
}

bool OBResidue::IsHetAtom(OBAtom *atom) const
{
    for ( unsigned int i = 0 ; i < _atoms.size() ; i++ )
        if (_atoms[i] == atom)
            return _hetatm[i];
    return false;
}

///////////////////////////////////////////////////////////////////////////////
// OBResidue: Iteration Utilities
///////////////////////////////////////////////////////////////////////////////

OBAtom *OBResidue::BeginAtom(vector<OBAtom*>::iterator &i)
{
    i = _atoms.begin();
    return ((i == _atoms.end()) ? NULL : *i);
}

OBAtom *OBResidue::NextAtom(vector<OBAtom*>::iterator &i)
{
    i++;
    return ((i == _atoms.end()) ? NULL : *i);
}

///////////////////////////////////////////////////////////////////////////////
// OBResidue: Information Functions
///////////////////////////////////////////////////////////////////////////////

bool OBResidue::GetAminoAcidProperty(int property) const
{
    switch(property)
    {
    case OBAminoAcidProperty::ACIDIC:
        return IS_ACIDIC(_aakey)      != 0;
    case OBAminoAcidProperty::ACYCLIC:
        return IS_ACYCLIC(_aakey)     != 0;
    case OBAminoAcidProperty::ALIPHATIC:
        return IS_ALIPHATIC(_aakey)   != 0;
    case OBAminoAcidProperty::AROMATIC:
        return IS_AROMATIC(_aakey)    != 0;
    case OBAminoAcidProperty::BASIC:
        return IS_BASIC(_aakey)       != 0;
    case OBAminoAcidProperty::BURIED:
        return IS_BURIED(_aakey)      != 0;
    case OBAminoAcidProperty::CHARGED:
        return IS_CHARGED(_aakey)     != 0;
    case OBAminoAcidProperty::CYCLIC:
        return IS_CYCLIC(_aakey)      != 0;
    case OBAminoAcidProperty::HYDROPHOBIC:
        return IS_HYDROPHOBIC(_aakey) != 0;
    case OBAminoAcidProperty::LARGE:
        return IS_LARGE(_aakey)       != 0;
    case OBAminoAcidProperty::MEDIUM:
        return IS_MEDIUM(_aakey)      != 0;
    case OBAminoAcidProperty::NEGATIVE:
        return IS_NEGATIVE(_aakey)    != 0;
    case OBAminoAcidProperty::NEUTRAL:
        return IS_NEUTRAL(_aakey)     != 0;
    case OBAminoAcidProperty::POLAR:
        return IS_POLAR(_aakey)       != 0;
    case OBAminoAcidProperty::POSITIVE:
        return IS_POSITIVE(_aakey)    != 0;
    case OBAminoAcidProperty::SMALL:
        return IS_SMALL(_aakey)       != 0;
    case OBAminoAcidProperty::SURFACE:
        return IS_SURFACE(_aakey)     != 0;
    default:
        return false;
    }
}

bool OBResidue::GetAtomProperty(OBAtom *atom, int property) const
{
    if (atom != NULL)
    {
        unsigned int atomid = GetAtomIDNumber(GetAtomID(atom).c_str());

        switch(property)
        {
        case OBResidueAtomProperty::ALPHA_CARBON:
            return (atomid == 1);

        case OBResidueAtomProperty::AMINO_BACKBONE:
            return (atomid <= 3);

        case OBResidueAtomProperty::BACKBONE:
            return (atomid <= 18);

        case OBResidueAtomProperty::CYSTEINE_SULPHUR:
            return (atomid == 20);

        case OBResidueAtomProperty::LIGAND:
            return IsHetAtom(atom) &&
                   !GetResidueProperty(OBResidueProperty::SOLVENT);

        case OBResidueAtomProperty::NUCLEIC_BACKBONE:
            return ((atomid >= 7) && (atomid <= 18));

        case OBResidueAtomProperty::SHAPELY_BACKBONE:
            return (atomid <= 7);

        case OBResidueAtomProperty::SHAPELY_SPECIAL:
            return (atomid == 19);

        case OBResidueAtomProperty::SIDECHAIN:
            return GetResidueProperty(OBResidueProperty::AMINO_NUCLEO) &&
                   (atomid > 18);

        case OBResidueAtomProperty::SUGAR_PHOSPHATE:
            return (atomid == 7);
        }
    }

    return false;
}

bool OBResidue::GetResidueProperty(int property) const
{
    switch(property)
    {
    case OBResidueProperty::AMINO:
        return (_reskey <= OBResidueIndex::HYP);

    case OBResidueProperty::AMINO_NUCLEO:
        return (_reskey <= OBResidueIndex::PSU);

    case OBResidueProperty::COENZYME:
        return (_reskey >= OBResidueIndex::NAD) &&
               (_reskey <= OBResidueIndex::NDP);

    case OBResidueProperty::ION:
        return (_reskey == OBResidueIndex::SO4) ||
               (_reskey == OBResidueIndex::PO4);

    case OBResidueProperty::NUCLEO:
        return (_reskey >= OBResidueIndex::A) &&
               (_reskey <= OBResidueIndex::PSU);

    case OBResidueProperty::PROTEIN:
        return (_reskey <= OBResidueIndex::HYP) ||
               ((_reskey >= OBResidueIndex::UNK) &&
                (_reskey <= OBResidueIndex::FOR));

    case OBResidueProperty::PURINE:
        return (_reskey == OBResidueIndex::A) ||
               (_reskey == OBResidueIndex::G);

    case OBResidueProperty::PYRIMIDINE:
        return (_reskey == OBResidueIndex::C) ||
               (_reskey == OBResidueIndex::T);

    case OBResidueProperty::SOLVENT:
        return (_reskey >= OBResidueIndex::HOH) &&
               (_reskey <= OBResidueIndex::PO4);

    case OBResidueProperty::WATER:
        return (_reskey == OBResidueIndex::HOH) ||
               (_reskey == OBResidueIndex::DOD);

    default:
        return false;
    }
}

bool OBResidue::IsResidueType(int restype) const
{
    return ((int)_reskey == restype);
}

// OBGenericData methods
bool OBResidue::HasData(string &s)
//returns true if the generic attribute/value pair exists
{
    if (_vdata.empty())
        return(false);

    vector<OBGenericData*>::iterator i;

    for (i = _vdata.begin();i != _vdata.end();i++)
        if ((*i)->GetAttribute() == s)
            return(true);

    return(false);
}

bool OBResidue::HasData(const char *s)
//returns true if the generic attribute/value pair exists
{
    if (_vdata.empty())
        return(false);

    vector<OBGenericData*>::iterator i;

    for (i = _vdata.begin();i != _vdata.end();i++)
        if ((*i)->GetAttribute() == s)
            return(true);

    return(false);
}

bool OBResidue::HasData(unsigned int dt)
//returns true if the generic attribute/value pair exists
{
    if (_vdata.empty())
        return(false);

    vector<OBGenericData*>::iterator i;

    for (i = _vdata.begin();i != _vdata.end();i++)
        if ((*i)->GetDataType() == dt)
            return(true);

    return(false);
}

OBGenericData *OBResidue::GetData(string &s)
//returns the value given an attribute
{
    vector<OBGenericData*>::iterator i;

    for (i = _vdata.begin();i != _vdata.end();i++)
        if ((*i)->GetAttribute() == s)
            return(*i);

    return(NULL);
}

OBGenericData *OBResidue::GetData(const char *s)
//returns the value given an attribute
{
    vector<OBGenericData*>::iterator i;

    for (i = _vdata.begin();i != _vdata.end();i++)
        if ((*i)->GetAttribute() == s)
            return(*i);

    return(NULL);
}

OBGenericData *OBResidue::GetData(unsigned int dt)
{
    vector<OBGenericData*>::iterator i;
    for (i = _vdata.begin();i != _vdata.end();i++)
        if ((*i)->GetDataType() == dt)
            return(*i);
    return(NULL);
}

void OBResidue::DeleteData(unsigned int dt)
{
    vector<OBGenericData*> vdata;
    vector<OBGenericData*>::iterator i;
    for (i = _vdata.begin();i != _vdata.end();i++)
        if ((*i)->GetDataType() == dt)
            delete *i;
        else
            vdata.push_back(*i);
    _vdata = vdata;
}

void OBResidue::DeleteData(vector<OBGenericData*> &vg)
{
    vector<OBGenericData*> vdata;
    vector<OBGenericData*>::iterator i,j;

    bool del;
    for (i = _vdata.begin();i != _vdata.end();i++)
    {
        del = false;
        for (j = vg.begin();j != vg.end();j++)
            if (*i == *j)
            {
                del = true;
                break;
            }
        if (del)
            delete *i;
        else
            vdata.push_back(*i);
    }
    _vdata = vdata;
}

void OBResidue::DeleteData(OBGenericData *gd)
{
    vector<OBGenericData*>::iterator i;
    for (i = _vdata.begin();i != _vdata.end();i++)
        if (*i == gd)
        {
            delete *i;
            _vdata.erase(i);
        }

}

} // end namespace OpenBabel

//! \file residue.cpp
//! \brief Handle macromolecule residues.