Back to index

python-biopython  1.60
AbstractPropertyMap.py
Go to the documentation of this file.
00001 # Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk)
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 """Class that maps (chain_id, residue_id) to a residue property."""
00007 
00008 
00009 class AbstractPropertyMap(object):
00010     def __init__(self, property_dict, property_keys, property_list):
00011         self.property_dict=property_dict
00012         self.property_keys=property_keys
00013         self.property_list=property_list
00014 
00015     def _translate_id(self, entity_id):
00016         return entity_id
00017 
00018     def __contains__(self, id):
00019         """True if the mapping has a property for this residue.
00020 
00021         Example:
00022             >>> if (chain_id, res_id) in apmap:
00023             ...     res, prop = apmap[(chain_id, res_id)]
00024 
00025         @param chain_id: chain id
00026         @type chain_id: char 
00027 
00028         @param res_id: residue id
00029         @type res_id: char 
00030         """
00031         translated_id = self._translate_id(id)
00032         return (translated_id in self.property_dict)
00033 
00034     def __getitem__(self, key):
00035         """
00036         Return property for a residue.
00037 
00038         @param chain_id: chain id
00039         @type chain_id: char 
00040 
00041         @param res_id: residue id
00042         @type res_id: int or (char, int, char) 
00043 
00044         @return: some residue property 
00045         @rtype: anything (can be a tuple)
00046         """
00047         translated_id=self._translate_id(key)
00048         return self.property_dict[translated_id]
00049 
00050     def __len__(self):
00051         """
00052         Return number of residues for which the property is available.
00053 
00054         @return: number of residues
00055         @rtype: int
00056         """
00057         return len(self.property_dict)
00058 
00059     def has_key(self, id):
00060         """True if the mapping has a property for this residue.
00061 
00062         (Obsolete; use "id in mapping" instead.)
00063 
00064         Example:
00065 
00066             >>> if apmap.has_key((chain_id, res_id)):
00067             ...     res, prop = apmap[(chain_id, res_id)]
00068 
00069         Is equivalent to:
00070 
00071             >>> if (chain_id, res_id) in apmap:
00072             ...     res, prop = apmap[(chain_id, res_id)]
00073 
00074         @param chain_id: chain id
00075         @type chain_id: char 
00076 
00077         @param res_id: residue id
00078         @type res_id: char 
00079         """
00080         import warnings
00081         warnings.warn("This function is obsolete; use 'id in mapping' instead", PendingDeprecationWarning)
00082         return (id in self)
00083 
00084     def keys(self):
00085         """
00086         Return the list of residues.
00087 
00088         @return: list of residues for which the property was calculated
00089         @rtype: [(chain_id, res_id), (chain_id, res_id),...] 
00090         """
00091         return self.property_keys
00092 
00093     def __iter__(self):
00094         """
00095         Iterate over the (entity, property) list. Handy alternative to 
00096         the dictionary-like access.
00097 
00098         Example:
00099             >>> for (res, property) in iter(map):
00100             ...     print res, property
00101 
00102         @return: iterator
00103         """
00104         for i in range(0, len(self.property_list)):
00105             yield self.property_list[i]
00106 
00107 
00108 class AbstractResiduePropertyMap(AbstractPropertyMap):
00109     def __init__(self, property_dict, property_keys, property_list):
00110         AbstractPropertyMap.__init__(self, property_dict, property_keys, 
00111                 property_list)
00112 
00113     def _translate_id(self, ent_id):
00114         chain_id, res_id=ent_id
00115         if isinstance(res_id, int):
00116             ent_id=(chain_id, (' ', res_id, ' '))
00117         return ent_id
00118 
00119 
00120 class AbstractAtomPropertyMap(AbstractPropertyMap):
00121     def __init__(self, property_dict, property_keys, property_list):
00122         AbstractPropertyMap.__init__(self, property_dict, property_keys, 
00123                 property_list)
00124 
00125     def _translate_id(self, ent_id):
00126         if len(ent_id)==4:
00127             chain_id, res_id, atom_name, icode=ent_id
00128         else:
00129             chain_id, res_id, atom_name=ent_id
00130             icode=None
00131         if isinstance(res_id, int):
00132             ent_id=(chain_id, (' ', res_id, ' '), atom_name, icode)
00133         return ent_id
00134