Package Bio :: Package PDB :: Module AbstractPropertyMap
[hide private]
[frames] | no frames]

Source Code for Module Bio.PDB.AbstractPropertyMap

  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   
  6  """Class that maps (chain_id, residue_id) to a residue property.""" 
  7   
  8  from __future__ import print_function 
  9   
 10   
11 -class AbstractPropertyMap(object):
12 - def __init__(self, property_dict, property_keys, property_list):
13 self.property_dict = property_dict 14 self.property_keys = property_keys 15 self.property_list = property_list
16
17 - def _translate_id(self, entity_id):
18 return entity_id
19
20 - def __contains__(self, id):
21 """True if the mapping has a property for this residue. 22 23 Example: 24 >>> if (chain_id, res_id) in apmap: 25 ... res, prop = apmap[(chain_id, res_id)] 26 27 @param chain_id: chain id 28 @type chain_id: char 29 30 @param res_id: residue id 31 @type res_id: char 32 """ 33 translated_id = self._translate_id(id) 34 return (translated_id in self.property_dict)
35
36 - def __getitem__(self, key):
37 """Return property for a residue. 38 39 @param chain_id: chain id 40 @type chain_id: char 41 42 @param res_id: residue id 43 @type res_id: int or (char, int, char) 44 45 @return: some residue property 46 @rtype: anything (can be a tuple) 47 """ 48 translated_id = self._translate_id(key) 49 return self.property_dict[translated_id]
50
51 - def __len__(self):
52 """Return number of residues for which the property is available. 53 54 @return: number of residues 55 @rtype: int 56 """ 57 return len(self.property_dict)
58
59 - def has_key(self, id):
60 """True if the mapping has a property for this residue. 61 62 (Obsolete; use "id in mapping" instead.) 63 64 Example: 65 66 >>> if apmap.has_key((chain_id, res_id)): 67 ... res, prop = apmap[(chain_id, res_id)] 68 69 Is equivalent to: 70 71 >>> if (chain_id, res_id) in apmap: 72 ... res, prop = apmap[(chain_id, res_id)] 73 74 @param chain_id: chain id 75 @type chain_id: char 76 77 @param res_id: residue id 78 @type res_id: char 79 """ 80 import warnings 81 from Bio import BiopythonDeprecationWarning 82 warnings.warn("This function is deprecated; use 'id in mapping' instead", BiopythonDeprecationWarning) 83 return (id in self)
84
85 - def keys(self):
86 """Return the list of residues. 87 88 @return: list of residues for which the property was calculated 89 @rtype: [(chain_id, res_id), (chain_id, res_id),...] 90 """ 91 return self.property_keys
92
93 - def __iter__(self):
94 """Iterate over the (entity, property) list. 95 96 Handy alternative to the dictionary-like access. 97 98 Example: 99 100 >>> for (res, property) in iter(map): 101 ... print(res, property) 102 ... 103 104 @return: iterator 105 """ 106 for i in range(0, len(self.property_list)): 107 yield self.property_list[i]
108 109
110 -class AbstractResiduePropertyMap(AbstractPropertyMap):
111 - def __init__(self, property_dict, property_keys, property_list):
112 AbstractPropertyMap.__init__(self, property_dict, property_keys, 113 property_list)
114
115 - def _translate_id(self, ent_id):
116 chain_id, res_id = ent_id 117 if isinstance(res_id, int): 118 ent_id = (chain_id, (' ', res_id, ' ')) 119 return ent_id
120 121
122 -class AbstractAtomPropertyMap(AbstractPropertyMap):
123 - def __init__(self, property_dict, property_keys, property_list):
124 AbstractPropertyMap.__init__(self, property_dict, property_keys, 125 property_list)
126
127 - def _translate_id(self, ent_id):
128 if len(ent_id) == 4: 129 chain_id, res_id, atom_name, icode = ent_id 130 else: 131 chain_id, res_id, atom_name = ent_id 132 icode = None 133 if isinstance(res_id, int): 134 ent_id = (chain_id, (' ', res_id, ' '), atom_name, icode) 135 return ent_id
136