Package Biskit :: Module ModelList
[hide private]
[frames] | no frames]

Source Code for Module Biskit.ModelList

  1  ## 
  2  ## Biskit, a toolkit for the manipulation of macromolecular structures 
  3  ## Copyright (C) 2004-2005 Raik Gruenberg & Johan Leckner 
  4  ## 
  5  ## This program is free software; you can redistribute it and/or 
  6  ## modify it under the terms of the GNU General Public License as 
  7  ## published by the Free Software Foundation; either version 2 of the 
  8  ## License, or any later version. 
  9  ## 
 10  ## This program is distributed in the hope that it will be useful, 
 11  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 13  ## General Public License for more details. 
 14  ## 
 15  ## You find a copy of the GNU General Public License in the file 
 16  ## license.txt along with this program; if not, write to the Free 
 17  ## Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 18  ## 
 19  ## 
 20   
 21  ## last $Author: leckner $ 
 22  ## last $Date: 2006/09/06 13:59:10 $ 
 23  ## $Revision: 2.5 $ 
 24   
 25  """ 
 26  organise, sort, and filter list of PDBModels 
 27  """ 
 28  from DictList import DictList 
 29  from PDBModel import PDBModel 
 30  import Biskit.tools as T 
 31   
32 -class ModelList( DictList ):
33 """ 34 A list of diverse PDBModels. Special support is given to access and use 35 entries in the info dictionaries for filtering and sorting. 36 Some care is taken to avoid the adding of non-PDBModel objects (but 37 somehow it is, certainly, still possible). 38 39 See DictList for a complete description of all methods! 40 41 Comparison to Trajectory: 42 43 In contrast to Trajectory, ModelList does not require structures 44 with identical atom content. This makes it of course less efficient and 45 less powerful, in many respects. On the other hand, models are kept as 46 seperate PDBModel instances with individual residue/atom profiles and 47 info dictionaries. 48 """ 49
50 - def __init__( self, lst=[], item_type=PDBModel ):
51 """ 52 @param lst: list 53 @type lst: [ dict ] 54 @param item_type: type, class of allowed items [ dict ] 55 @type item_type: [ dict ] 56 57 @raise DictListError: if list contains non-item_type item. 58 """ 59 DictList.__init__( self, lst, item_type )
60 61
62 - def version( self ):
63 """ 64 Version of class. 65 66 @return: class version number 67 @rtype: str 68 """ 69 return DictList.version( self ) + '; ModelList $Revision: 2.5 $'
70 71
72 - def getItemValue( self, item, key, default=None ):
73 """ 74 Get a value from a given item (dictionary). Overrides DictList method. 75 76 @param item: possible entry of this list 77 @type item: dict 78 @param key: dictionary key 79 @type key: any 80 @param default: return value if key is not found (default: None) 81 @type default: any 82 83 @return: any 84 @rtype: any 85 """ 86 return item.info.get( key, default )
87 88
89 - def file2model( self, file_or_model ):
90 """ 91 Load PDBModel from file if necessary. 92 93 @param file_or_model: file name or existing model 94 @type file_or_model: str OR PDBModel 95 96 @return: existing model or model loaded from file 97 @rtype: PDBModel 98 """ 99 if isinstance( file_or_model, PDBModel ): 100 return file_or_model 101 102 return PDBModel( file_or_model )
103 104
105 - def _processNewItem( self, v, i ):
106 """ 107 Called before an item is added to the list. Override but call. 108 109 @param i: index 110 @type i: int 111 @param v: value 112 @type v: dict 113 114 @return: value 115 @rtype: dict 116 """ 117 v = self.file2model( v ) 118 119 return DictList._processNewItem( self, v, i )
120 121 122 ############# 123 ## TESTING 124 ############# 125
126 -class Test:
127 """ 128 Test class 129 """ 130 131
132 - def run( self, local=0 ):
133 """ 134 run function test 135 136 @param local: transfer local variables to global and perform 137 other tasks only when run locally 138 @type local: 1|0 139 140 @return: list with sequences of models 141 @rtype: [str] 142 """ 143 import random 144 145 f_lst = [ T.testRoot() + '/rec/1A2P.pdb', 146 T.testRoot() + '/lig/1A19.pdb', 147 T.testRoot() + '/com/1BGS.pdb' ] 148 149 ## Loading PDBs... 150 self.l = ModelList( f_lst ) 151 152 seq = [] 153 for m in self.l: 154 m.info['score'] = random.random() 155 seq += [ m.compress( m.maskProtein() ).sequence() ] 156 157 p = self.l.plot( 'score' ) 158 159 if local: 160 print self.l.valuesOf('score') 161 p.show() 162 globals().update( locals() ) 163 164 return seq
165 166 167
168 - def expected_result( self ):
169 """ 170 Precalculated result to check for consistent performance. 171 172 @return: list with sequences of models 173 @rtype: [str] 174 """ 175 return ['VINTFDGVADYLQTYHKLPDNYITKSEAQALGWVASKGNLADVAPGKSIGGDIFSNREGKLPGKSGRTWREADINYTSGFRNSDRILYSSDWLIYKTTDHYQTFTKIR', 'KKAVINGEQIRSISDLHQTLKKELALPEYYGENLDALWDCLTGWVEYPLVLEWRQFEQSKQLTENGAESVLQVFREAKAEGADITIILS', 'AQVINTFDGVADYLQTYHKLPDNYITKSEAQALGWVASKGNLADVAPGKSIGGDIFSNREGKLPGKSGRTWREADINYTSGFRNSDRILYSSDWLIYKTTDHYQTFTKIRKKAVINGEQIRSISDLHQTLKKELALPEYYGENLDALWDALTGWVEYPLVLEWRQFEQSKQLTENGAESVLQVFREAKAEGADITIILS']
176 177 178 179 if __name__ == '__main__': 180 181 test = Test() 182 183 assert test.run( local=1 ) == test.expected_result() 184