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

Source Code for Module Bio.PDB.Selection

 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  """Selection of atoms, residues, etc.""" 
 7   
 8  import itertools 
 9   
10  from Bio.PDB.Atom import Atom 
11  from Bio.PDB.Entity import Entity 
12  from Bio.PDB.PDBExceptions import PDBException 
13   
14   
15  entity_levels = ["A", "R", "C", "M", "S"] 
16   
17   
18 -def uniqueify(items):
19 """Return a list of the unique items in the given iterable. 20 21 Order is NOT preserved. 22 """ 23 return list(set(items))
24 25
26 -def get_unique_parents(entity_list):
27 """Translate a list of entities to a list of their (unique) parents.""" 28 unique_parents = set(entity.get_parent() for entity in entity_list) 29 return list(unique_parents)
30 31
32 -def unfold_entities(entity_list, target_level):
33 """Unfold entities list to a child level (e.g. residues in chain). 34 35 Unfold a list of entities to a list of entities of another 36 level. E.g.: 37 38 list of atoms -> list of residues 39 list of modules -> list of atoms 40 list of residues -> list of chains 41 42 o entity_list - list of entities or a single entity 43 o target_level - char (A, R, C, M, S) 44 45 Note that if entity_list is an empty list, you get an empty list back: 46 47 >>> unfold_entities([], "A") 48 [] 49 50 """ 51 if not target_level in entity_levels: 52 raise PDBException("%s: Not an entity level." % target_level) 53 if entity_list == []: 54 return [] 55 if isinstance(entity_list, Entity) or isinstance(entity_list, Atom): 56 entity_list = [entity_list] 57 58 level = entity_list[0].get_level() 59 if not all(entity.get_level() == level for entity in entity_list): 60 raise PDBException("Entity list is not homogeneous.") 61 62 target_index = entity_levels.index(target_level) 63 level_index = entity_levels.index(level) 64 65 if level_index == target_index: # already right level 66 return entity_list 67 68 if level_index > target_index: # we're going down, e.g. S->A 69 for i in range(target_index, level_index): 70 #entity_list = itertools.chain.from_iterable(entity_list) # 2.6+ 71 entity_list = itertools.chain(*entity_list) 72 else: # we're going up, e.g. A->S 73 for i in range(level_index, target_index): 74 # find unique parents 75 entity_list = set(entity.get_parent() for entity in entity_list) 76 return list(entity_list)
77 78
79 -def _test():
80 """Run the Bio.PDB.Selection module's doctests (PRIVATE).""" 81 import doctest 82 print "Running doctests ..." 83 doctest.testmod() 84 print "Done"
85 86 87 if __name__ == "__main__": 88 _test() 89