Package Biskit :: Package Dock :: Module ComplexRandomizer
[hide private]
[frames] | no frames]

Source Code for Module Biskit.Dock.ComplexRandomizer

  1  ## Automatically adapted for numpy.oldnumeric Mar 26, 2007 by alter_code1.py 
  2   
  3  ## generate random orientations of receptor and ligand 
  4  ## 
  5  ## Biskit, a toolkit for the manipulation of macromolecular structures 
  6  ## Copyright (C) 2004-2006 Raik Gruenberg & Johan Leckner 
  7  ## 
  8  ## This program is free software; you can redistribute it and/or 
  9  ## modify it under the terms of the GNU General Public License as 
 10  ## published by the Free Software Foundation; either version 2 of the 
 11  ## License, or any later version. 
 12  ## 
 13  ## This program is distributed in the hope that it will be useful, 
 14  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 16  ## General Public License for more details. 
 17  ## 
 18  ## You find a copy of the GNU General Public License in the file 
 19  ## license.txt along with this program; if not, write to the Free 
 20  ## Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 21  ## 
 22  ## 
 23  ## 
 24  ## $Revision: 2.13 $ 
 25  ## last $Author: graik $ 
 26  ## last $Date: 2007/03/26 18:40:42 $ 
 27  """ 
 28  Create Complexes with random orientation from a receptor and ligand structure. 
 29  """ 
 30       
 31  from Biskit.Dock.Complex import Complex 
 32  import Biskit.mathUtils as ma 
 33  import Biskit.molUtils as mol 
 34  import Biskit.tools as t 
 35  import numpy.oldnumeric.random_array as ra 
 36  import numpy.oldnumeric as N 
 37  from Biskit import Xplorer, PCRModel 
 38   
 39  import tempfile 
 40   
41 -class ComplexRandomizer:
42 """ 43 Create Biskit.Dock.Complex(es) with random orientation 44 """ 45
46 - def __init__( self, mrec, mlig, rec_out=None, lig_out=None, debug=0 ):
47 """ 48 @param mrec: receptor model 49 @type mrec: PCRModel 50 @param mlig: ligand model 51 @type mlig: PCRModel 52 @param rec_out: rec output(default: None) 53 @type rec_out: str 54 @param lig_out: lig output (default: None) 55 @type lig_out: str 56 @param debug: 1, keep temporary xplor files (default: 0) 57 @type debug: 1|0 58 """ 59 ## rec and lig with centered coordinates 60 self.rec = self.__center_model( mrec ) 61 self.lig = self.__center_model( mlig ) 62 63 ## this way they will be unique in ComplexList 64 if rec_out: 65 self.rec.saveAs( rec_out ) 66 if lig_out: 67 self.lig.saveAs( lig_out ) 68 69 ## get max. center-to-atom distances 70 self.d_max_rec = self.__max_distance( self.rec ) 71 self.d_max_lig = self.__max_distance( self.lig ) 72 73 self.xp_log = tempfile.mktemp('rb_min_xplor_log') 74 75 ## keep temporary xplor files 76 self.debug = debug
77 78
79 - def __center_model( self, model ):
80 """ 81 translate PDBModel so that it's center is in 0,0,0 82 83 @param model: model to center 84 @type model: PDBModel 85 86 @return: PDBModel (clone of model) 87 @rtype: PDBModel 88 """ 89 r = model.clone() 90 r.keep( N.nonzero( N.logical_not( r.maskH2O() ) ) ) 91 center = r.centerOfMass() 92 r.setXyz( r.getXyz() - center ) 93 94 return r
95 96
97 - def __max_distance( self, model ):
98 """ 99 largest center to any other atom distance 100 101 @param model: model with centered coordinates 102 @type model: PDBModel 103 104 @return: largest distance 105 @rtype: float 106 """ 107 center = model.centerOfMass() 108 dist = N.sqrt( N.sum( ( model.getXyz()-center )**2 , 1 ) ) 109 110 return max( dist )
111 112
113 - def __random_translation( self ):
114 """ 115 Random translation on a sphere around 0,0,0 with fixed radius 116 The radius is the sum of the (max) radius of receptor and ligand 117 118 @return: translation array 3 x 1 of float 119 @rtype: array 120 """ 121 radius = (self.d_max_rec + self.d_max_lig) / 2.0 122 xyz = ra.random( 3 ) - 0.5 123 124 scale = radius*1.0 / N.sqrt( N.sum( xyz**2 ) ) 125 126 return scale * xyz
127 128
129 - def __random_matrix( self ):
130 """ 131 Random rotation matrix. 132 133 @return: 4 x 4 array of float, random rotation and translation matrix 134 @rtype: array 135 """ 136 r = ma.randomRotation() 137 ## r = N.array([[1,0,0],[0,1,0],[0,0,1]],'f') 138 t = self.__random_translation() 139 140 ## create 3 x 4 matrix: 0:3, 0:3 contains rot; 3,0:3 contains trans 141 result = N.concatenate( (r, N.transpose( [ t.tolist() ] )), 1) 142 143 ## make it square 144 result = N.concatenate( (result, N.array([[0,0,0,1]], N.Float32)), 0 ) 145 146 return result
147 148
149 - def random_complex_remote( self ):
150 """ 151 Create a complex where the recrptor and ligand have random 152 orientations but are spaced within contact distance. 153 154 @return: rec & lig spaced r_rec + r_lig apart in random orientation 155 @rtype: Complex 156 """ 157 return Complex( self.rec, self.lig, 158 ligMatrix= self.__random_matrix() )
159 160
161 - def __minimize_complex( self, com ):
162 """ 163 Use Xplor to rigid body minimize the random complex. 164 165 @param com: random complex 166 @type com: Complex 167 """ 168 xp = ComplexMinimizer( com, t.tempDir(), log=self.xp_log ) 169 xp.run()
170 171
172 - def random_complex( self, inp_mirror=None ):
173 """ 174 @return: randomized and minimized complex 175 @rtype: Complex 176 """ 177 self.cm = ComplexMinimizer( self.random_complex_remote(), 178 debug=self.debug ) 179 self.cm.run( inp_mirror=inp_mirror ) 180 181 com = Complex( self.rec, self.lig ) 182 183 rt = com.extractLigandMatrix( self.cm.lig ) 184 com.setLigMatrix( rt ) 185 186 return com
187 188
189 -class ComplexMinimizer( Xplorer ):
190 """ 191 Rigid-body minimize receptor and ligand of a Complex 192 using soft vdW pot. 193 """ 194
195 - def __init__( self, com, debug=0, **params ):
196 197 self.com = com 198 199 self.rec_psf = com.rec().getPsfFile() 200 self.lig_psf = com.lig().getPsfFile() 201 202 recCode = com.rec().getPdbCode() 203 ligCode = com.lig().getPdbCode() 204 205 self.rec_in = tempfile.mktemp( recCode + ".pdb" ) 206 self.lig_in = tempfile.mktemp( ligCode + ".pdb" ) 207 208 self.lig_out = tempfile.mktemp( "lig_out.pdb" ) 209 self.rec_out = tempfile.mktemp( "rec_out.pdb" ) 210 211 self.inp_template = t.projectRoot() +\ 212 '/external/xplor/rb_minimize_complex.inp' 213 214 self.param19 = t.projectRoot() + \ 215 '/external/xplor/toppar/param19.pro' 216 217 self.result = None 218 219 Xplorer.__init__( self, self.inp_template, debug=debug, **params )
220 221
222 - def prepare( self ):
223 """ 224 Prepare for calculation. Write input files. 225 """ 226 self.com.rec().writePdb( self.rec_in ) 227 self.com.lig().writePdb( self.lig_in )
228 229
230 - def cleanup( self ):
231 """ 232 Remove temporary files. 233 """ 234 Xplorer.cleanup( self ) 235 236 if not self.debug: 237 238 t.tryRemove( self.rec_in ) 239 t.tryRemove( self.lig_in ) 240 241 t.tryRemove( self.rec_out ) 242 t.tryRemove( self.lig_out )
243 244
245 - def finish( self ):
246 """ 247 When done, write result to disc. 248 """ 249 self.rec = PCRModel( self.com.rec_model.getPsfFile(), self.rec_out ) 250 self.lig = PCRModel( self.com.lig_model.getPsfFile(), self.lig_out )
251 252 253 254 ############# 255 ## TESTING 256 ############# 257 import Biskit.test as BT 258
259 -class Test(BT.BiskitTest):
260 """Test case 261 262 The test generates 3 random complexes. In interactive mode, 263 the 3 complexes are displayed as movie in Pymol. They are 264 written out as Amber trajectory if debug=True. 265 """ 266 267 TAGS = [ BT.EXE, BT.LONG ] 268
269 - def prepare(self):
270 import tempfile 271 self.f_pfb = tempfile.mktemp('_test.pdb') 272 self.f_crd = tempfile.mktemp('_test.crd')
273
274 - def cleanUp(self):
275 t.tryRemove( self.f_pfb ) 276 t.tryRemove( self.f_crd )
277
278 - def test_ComplexRandomizer(self):
279 """Dock.ComplexRandomizer test""" 280 from Biskit import Trajectory 281 282 if self.local: 283 print "\nLoading Rec and Lig files ...", 284 285 rec_pdb = t.testRoot() + '/rec/1A2P.pdb' 286 lig_pdb = t.testRoot() + '/lig/1A19.pdb' 287 288 rec_psf = t.testRoot() + '/rec/1A2P.psf' 289 lig_psf = t.testRoot() + '/lig/1A19.psf' 290 291 rec = PCRModel( rec_psf, rec_pdb ) 292 lig = PCRModel( lig_psf, lig_pdb ) 293 294 if self.local: 295 print "Initializing Randomizer..." 296 297 self.cr = ComplexRandomizer( rec, lig, debug=self.DEBUG ) 298 299 if self.local: 300 print "Creating 3 random complexes..." 301 302 cs = [ self.cr.random_complex() for i in range(3) ] 303 304 self.traj = Trajectory( [ c.model() for c in cs ] ) 305 306 if self.local: 307 self.display( self.traj ) 308 globals().update( locals() ) 309 310 self.assertEqual( len(self.traj), 3 )
311 312
313 - def display(self, traj ):
314 """Display random complexes as trajectory in Pymol. 315 Only run in local interactive mode. 316 """ 317 from Biskit import Pymoler 318 319 print "activate debug switch to get random complexes written to disc!" 320 if self.DEBUG: 321 print "writing random complex as trajectory to file..." 322 traj.ref.writePdb( self.f_pfb ) 323 traj.writeCrd( self.f_crd ) 324 print 'Wrote reference pdb file to: %s' % self.f_pfb 325 print 'Wrote crd file to: %s' % self.f_crd 326 327 self.pm = Pymoler( full=0 ) 328 329 mname = self.pm.addMovie( [ traj[i] for i in range(len(traj)) ] ) 330 self.pm.add('hide all') 331 self.pm.add('show cartoon') 332 self.pm.add('spectrum') 333 self.pm.add('mplay') 334 335 self.pm.run()
336 337 338 if __name__ == '__main__': 339 340 BT.localTest() 341