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

Source Code for Module Biskit.TrajFlexSlave

  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: graik $ 
 22  ## last $Date: 2006/12/22 14:27:30 $ 
 23  ## $Revision: 2.3 $ 
 24   
 25  """ 
 26  Parallize calculation of pairwise rmsd between the frames of a trajectory. 
 27  """ 
 28   
 29  from Biskit.PVM import JobSlave 
 30  import Biskit.tools as T 
 31  import Biskit.rmsFit as rmsFit 
 32  import Numeric as N 
 33  from Biskit.LogFile import ErrLog, LogFile 
 34   
 35  import os, time 
 36   
37 -class TrajFlexSlave( JobSlave ):
38 """ 39 Calculate the pairwise rmsd between frames of a trajectory. 40 """ 41
42 - def initialize(self, params):
43 """ 44 Expects:: 45 {'ferror':str, 46 'trajMap':[int], 47 'only_off_diagonal':1|0, 48 'only_cross_member':1|0} 49 50 @param params: parameters passed over from the L{TrajFlexMaster} 51 @type params: dict 52 """ 53 54 self.__dict__.update( params ) 55 56 if not self.ferror: 57 self.errorLog = ErrLog() 58 else: 59 self.errorLog = LogFile( self.ferror, mode='a' ) 60 61 self.frame_cache = {}
62 63
64 - def __getFrames(self, f ):
65 """ 66 Load coordinate frames from file or take them from own cache 67 68 @param f: file name 69 @type f: str 70 71 @return: coordiante frames 72 @rtype: array 73 """ 74 if f in self.frame_cache: 75 return self.frame_cache[ f ] 76 77 self.frame_cache[f] = T.Load(f) 78 79 return self.frame_cache[ f ]
80 81
82 - def requested( self, i, j ):
83 """ 84 Checks if the rmsd of two frames i and j are to be calculated:: 85 requested( int_f1, int_f2 ) -> 1|0 86 87 @param i: frame number 88 @type i: int 89 @param j: frame number 90 @type j: int 91 92 @return: if the rms of the two frames is supposed to be calculated 93 @rtype: 1|0 94 """ 95 if self.only_off_diagonal and i == j: 96 return 0 97 98 if self.trajMap is None or not self.only_cross_member: 99 return 1 100 101 return self.trajMap[i] != self.trajMap[j]
102 103
104 - def calcRmsd( self, window, f1, f2 ):
105 """ 106 Calulate the rmsd between two frame chunks. 107 108 @param window: start and end of two frame chunks within the 109 whole trajectory 110 @type window: ((int, int),(int,int)) 111 @param f1: frame chunk 112 @type f1: array 113 @param f2: frame chunk 114 @type f2: array 115 116 @return: the rms between the frames 117 @rtype: [float] 118 """ 119 try: 120 i_start, i_stop = window[0] 121 j_start, j_stop = window[1] 122 123 a = N.zeros( (i_stop-i_start, j_stop-j_start), 'f' ) 124 125 i = j = -1 126 127 ## block on the diagonal, only calculate one half of it 128 S = (self.only_off_diagonal and window[0] == window[1]) 129 130 for i in range( i_start, i_stop ): 131 for j in range( S * i - S * j_start + j_start, j_stop ): 132 133 if self.requested( i, j ): 134 135 rt, rmsdLst = rmsFit.match( f1[i-i_start], 136 f2[j-j_start], 1 ) 137 a[i-i_start,j-j_start] = rmsdLst[0][1] 138 139 return N.ravel(a).tolist() 140 141 except Exception, why: 142 self.reportError( 'ERROR '+str(why), (i,j) ) 143 return
144 145
146 - def go(self, jobs):
147 """ 148 Run job. 149 150 @param jobs: { ((int,int),(int,int)) : (str, str) }, maps start and end 151 position of two chunks of coordinate frames to the 152 files where the two chunks are pickled. 153 @type jobs: {((int,int),(int,int)) : (str, str)} 154 155 @return: the rms between the frames 156 @rtype: [float] 157 """ 158 159 result = {} 160 frames = None 161 162 startTime = time.time() 163 164 try: 165 166 for i, frames in jobs.items(): 167 168 T.flushPrint( str(i) ) 169 170 f1 = self.__getFrames( frames[0]) 171 f2 = self.__getFrames( frames[1]) 172 173 result[ i ] = self.calcRmsd(i, f1, f2 ) 174 175 print "\navg time for last %i complexes: %f s" %\ 176 ( len(jobs), (time.time()-startTime)/len(jobs)) 177 178 except IOError, why: 179 self.reportError("Cannot open temporary frame file "+\ 180 "(can happen if slave catches exit signal)", 181 frames ) 182 183 return result
184 185
186 - def reportError(self, msg, window ):
187 """ 188 Report errors. 189 190 @param msg: error message 191 @type msg: str 192 @param window: start and end of two frame chunks within the 193 whole trajectory 194 @type window: ((int, int),(int,int)) 195 """ 196 try: 197 s = '%s on %s, frames %s \n' % \ 198 (msg, os.uname()[1], str(window) ) 199 s += '\nErrorTrace:\n' + T.lastErrorTrace() + '\n' 200 201 try: 202 print s 203 except: 204 pass 205 206 self.errorLog.add( s ) 207 208 except Exception, why: 209 f = open('ErrorReportError_TrajFlexSlave','a') 210 f.write( str(why) ) 211 try: 212 f.write( T.lastErrorTrace() ) 213 except: 214 pass 215 f.close()
216 217 218 if __name__ == '__main__': 219 220 import os, sys 221 222 if len(sys.argv) == 2: 223 224 niceness = int(sys.argv[1]) 225 os.nice(niceness) 226 227 slave = TrajFlexSlave() 228 slave.start() 229