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