Back to index

python-biopython  1.60
Selection.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 """Selection of atoms, residues, etc."""
00007 
00008 from Bio.PDB.Entity import Entity
00009 from Bio.PDB.PDBExceptions import PDBException
00010 
00011 
00012 entity_levels=["A", "R", "C", "M", "S"]
00013 
00014 
00015 def uniqueify(items):
00016     """Return a list of the unique items in the given iterable.
00017 
00018     Order is NOT preserved.
00019     """
00020     return list(set(items))
00021 
00022 
00023 def get_unique_parents(entity_list):
00024     """Translate a list of entities to a list of their (unique) parents.""" 
00025     parents = [entity.get_parent() for entity in entity_list]
00026     return uniqueify(parents)
00027 
00028 
00029 def unfold_entities(entity_list, target_level):
00030     """Unfold entities list to a child level (e.g. residues in chain).
00031 
00032     Unfold a list of entities to a list of entities of another 
00033     level.  E.g.:
00034 
00035     list of atoms -> list of residues
00036     list of modules -> list of atoms
00037     list of residues -> list of chains
00038 
00039     o entity_list - list of entities or a single entity
00040     o target_level - char (A, R, C, M, S)
00041 
00042     Note that if entity_list is an empty list, you get an empty list back:
00043 
00044     >>> unfold_entities([], "A")
00045     []
00046 
00047     """
00048     if not target_level in entity_levels:
00049         raise PDBException("%s: Not an entity level." % target_level)
00050     if entity_list == []:
00051         return []
00052     if isinstance(entity_list, Entity):
00053         # single entity
00054         entity_list=[entity_list]
00055     # level of entity list
00056     level=entity_list[0].get_level()
00057     for entity in entity_list:
00058         if not (entity.get_level()==level):
00059             raise PDBException("Entity list is not homogeneous.")
00060     target_index=entity_levels.index(target_level)
00061     level_index=entity_levels.index(level)
00062     if level_index==target_index:
00063         # already right level
00064         return entity_list
00065     if level_index>target_index:
00066         # we're going down, e.g. S->A
00067         for i in range(target_index, level_index):
00068             new_entity_list=[]
00069             for entity in entity_list:
00070                 new_entity_list=new_entity_list+entity.get_list()
00071             entity_list=new_entity_list
00072     else:
00073         # we're going up, e.g. A->S
00074         for i in range(level_index, target_index):
00075             new_entity_list=[]  
00076             for entity in entity_list:
00077                 parent=entity.get_parent()
00078                 new_entity_list.append(parent)
00079             # find unique parents
00080             entity_list=uniqueify(new_entity_list)
00081     return entity_list
00082 
00083 
00084 def _test():
00085     """Run the Bio.PDB.Selection module's doctests (PRIVATE)."""
00086     import doctest
00087     print "Runing doctests ..."
00088     doctest.testmod()
00089     print "Done"
00090 
00091 
00092 if __name__ == "__main__":
00093     _test()
00094