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   
9 -class AbstractPropertyMap(object):
10 - def __init__(self, property_dict, property_keys, property_list):
11 self.property_dict=property_dict 12 self.property_keys=property_keys 13 self.property_list=property_list
14
15 - def _translate_id(self, entity_id):
16 return entity_id
17
18 - def __contains__(self, id):
19 """True if the mapping has a property for this residue. 20 21 Example: 22 >>> if (chain_id, res_id) in apmap: 23 ... res, prop = apmap[(chain_id, res_id)] 24 25 @param chain_id: chain id 26 @type chain_id: char 27 28 @param res_id: residue id 29 @type res_id: char 30 """ 31 translated_id = self._translate_id(id) 32 return (translated_id in self.property_dict)
33
34 - def __getitem__(self, key):
35 """ 36 Return property for a residue. 37 38 @param chain_id: chain id 39 @type chain_id: char 40 41 @param res_id: residue id 42 @type res_id: int or (char, int, char) 43 44 @return: some residue property 45 @rtype: anything (can be a tuple) 46 """ 47 translated_id=self._translate_id(key) 48 return self.property_dict[translated_id]
49
50 - def __len__(self):
51 """ 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 warnings.warn("This function is obsolete; use 'id in mapping' instead", PendingDeprecationWarning) 82 return (id in self)
83
84 - def keys(self):
85 """ 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 """ 95 Iterate over the (entity, property) list. Handy alternative to 96 the dictionary-like access. 97 98 Example: 99 >>> for (res, property) in iter(map): 100 ... print res, property 101 102 @return: iterator 103 """ 104 for i in range(0, len(self.property_list)): 105 yield self.property_list[i]
106 107
108 -class AbstractResiduePropertyMap(AbstractPropertyMap):
109 - def __init__(self, property_dict, property_keys, property_list):
110 AbstractPropertyMap.__init__(self, property_dict, property_keys, 111 property_list)
112
113 - def _translate_id(self, ent_id):
114 chain_id, res_id=ent_id 115 if isinstance(res_id, int): 116 ent_id=(chain_id, (' ', res_id, ' ')) 117 return ent_id
118 119
120 -class AbstractAtomPropertyMap(AbstractPropertyMap):
121 - def __init__(self, property_dict, property_keys, property_list):
122 AbstractPropertyMap.__init__(self, property_dict, property_keys, 123 property_list)
124
125 - def _translate_id(self, ent_id):
126 if len(ent_id)==4: 127 chain_id, res_id, atom_name, icode=ent_id 128 else: 129 chain_id, res_id, atom_name=ent_id 130 icode=None 131 if isinstance(res_id, int): 132 ent_id=(chain_id, (' ', res_id, ' '), atom_name, icode) 133 return ent_id
134