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