/Bio/PDB/AbstractPropertyMap.py

https://github.com/timwintle/biopython · Python · 134 lines · 64 code · 17 blank · 53 comment · 5 complexity · 0b5d7d0242603e50f51ee4fca506460f MD5 · raw file

  1. # Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk)
  2. # This code is part of the Biopython distribution and governed by its
  3. # license. Please see the LICENSE file that should have been included
  4. # as part of this package.
  5. """Class that maps (chain_id, residue_id) to a residue property."""
  6. class AbstractPropertyMap(object):
  7. def __init__(self, property_dict, property_keys, property_list):
  8. self.property_dict=property_dict
  9. self.property_keys=property_keys
  10. self.property_list=property_list
  11. def _translate_id(self, entity_id):
  12. return entity_id
  13. def __contains__(self, id):
  14. """True if the mapping has a property for this residue.
  15. Example:
  16. >>> if (chain_id, res_id) in apmap:
  17. ... res, prop = apmap[(chain_id, res_id)]
  18. @param chain_id: chain id
  19. @type chain_id: char
  20. @param res_id: residue id
  21. @type res_id: char
  22. """
  23. translated_id = self._translate_id(id)
  24. return (translated_id in self.property_dict)
  25. def __getitem__(self, key):
  26. """
  27. Return property for a residue.
  28. @param chain_id: chain id
  29. @type chain_id: char
  30. @param res_id: residue id
  31. @type res_id: int or (char, int, char)
  32. @return: some residue property
  33. @rtype: anything (can be a tuple)
  34. """
  35. translated_id=self._translate_id(key)
  36. return self.property_dict[translated_id]
  37. def __len__(self):
  38. """
  39. Return number of residues for which the property is available.
  40. @return: number of residues
  41. @rtype: int
  42. """
  43. return len(self.property_dict)
  44. def has_key(self, id):
  45. """True if the mapping has a property for this residue.
  46. (Obsolete; use "id in mapping" instead.)
  47. Example:
  48. >>> if apmap.has_key((chain_id, res_id)):
  49. ... res, prop = apmap[(chain_id, res_id)]
  50. Is equivalent to:
  51. >>> if (chain_id, res_id) in apmap:
  52. ... res, prop = apmap[(chain_id, res_id)]
  53. @param chain_id: chain id
  54. @type chain_id: char
  55. @param res_id: residue id
  56. @type res_id: char
  57. """
  58. import warnings
  59. warnings.warn("This function is obsolete; use 'id in mapping' instead", PendingDeprecationWarning)
  60. return (id in self)
  61. def keys(self):
  62. """
  63. Return the list of residues.
  64. @return: list of residues for which the property was calculated
  65. @rtype: [(chain_id, res_id), (chain_id, res_id),...]
  66. """
  67. return self.property_keys
  68. def __iter__(self):
  69. """
  70. Iterate over the (entity, property) list. Handy alternative to
  71. the dictionary-like access.
  72. Example:
  73. >>> for (res, property) in iter(map):
  74. ... print res, property
  75. @return: iterator
  76. """
  77. for i in range(0, len(self.property_list)):
  78. yield self.property_list[i]
  79. class AbstractResiduePropertyMap(AbstractPropertyMap):
  80. def __init__(self, property_dict, property_keys, property_list):
  81. AbstractPropertyMap.__init__(self, property_dict, property_keys,
  82. property_list)
  83. def _translate_id(self, ent_id):
  84. chain_id, res_id=ent_id
  85. if isinstance(res_id, int):
  86. ent_id=(chain_id, (' ', res_id, ' '))
  87. return ent_id
  88. class AbstractAtomPropertyMap(AbstractPropertyMap):
  89. def __init__(self, property_dict, property_keys, property_list):
  90. AbstractPropertyMap.__init__(self, property_dict, property_keys,
  91. property_list)
  92. def _translate_id(self, ent_id):
  93. if len(ent_id)==4:
  94. chain_id, res_id, atom_name, icode=ent_id
  95. else:
  96. chain_id, res_id, atom_name=ent_id
  97. icode=None
  98. if isinstance(res_id, int):
  99. ent_id=(chain_id, (' ', res_id, ' '), atom_name, icode)
  100. return ent_id