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

Source Code for Module Biskit.AmberCrdEntropist

  1  ## 
  2  ## Biskit, a toolkit for the manipulation of macromolecular structures 
  3  ## Copyright (C) 2004-2006 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  ## last $Author: graik $ 
 21  ## last $Date: 2006/12/30 19:27:24 $ 
 22  ## $Revision: 2.5 $ 
 23   
 24  """ 
 25  Run ptraj entropy analysis on existing amber trajectory (.crd) and 
 26  topology (.parm7) file. 
 27  """ 
 28       
 29  import os.path as osp 
 30  import re 
 31  import tempfile 
 32   
 33  import Biskit.tools as T 
 34  ## import Biskit.settings as settings 
 35  from Biskit.Errors import BiskitError 
 36  from Biskit.Executor import Executor 
 37   
38 -class EntropistError( BiskitError ):
39 pass
40
41 -class AmberCrdEntropist( Executor ):
42 """ 43 Run ptraj entropy analysis on existing amber trajectory (.crd) and 44 topology (.parm7) file. 45 """ 46 47 ## template for ptraj input script 48 ptraj_script = """ 49 trajin %(f_crd)s %(start)i %(stop)i %(step)i 50 matrix mwcovar name mwc 51 analyze matrix mwc vecs 0 thermo 52 """ 53
54 - def __init__( self, f_parm, f_crd, f_template=None, 55 s=0, e=None, step=1, **kw ):
56 """ 57 @param f_parm: path to amber topology file 58 @type f_parm: str 59 @param f_crd: path to amber trajectory file 60 @type f_crd: str 61 @param f_template: alternative ptraj input template (default: None) 62 @type f_template: str 63 @param s: start frame (default: 0, first) 64 @type s: int 65 @param e: end frame (default: None, last) 66 @type e: int 67 @param step: frame offset (default: 1, no offset ) 68 @type step: int 69 70 @param kw: additional key=value parameters for Executor: 71 @type kw: key=value pairs 72 :: 73 debug - 0|1, keep all temporary files (default: 0) 74 verbose - 0|1, print progress messages to log (log != STDOUT) 75 node - str, host for calculation (None->local) NOT TESTED 76 (default: None) 77 nice - int, nice level (default: 0) 78 log - Biskit.LogFile, program log (None->STOUT) (default: None) 79 """ 80 template = f_template or self.ptraj_script 81 82 Executor.__init__( self, 'ptraj', template=template, push_inp=0, **kw ) 83 84 self.f_parm = T.absfile( f_parm ) 85 self.f_crd = T.absfile( f_crd ) 86 87 self.start = s 88 self.stop = e or 10000000 89 self.step = step 90 91 ## default result 92 self.result = { 'T':None, 'mass':None, 'vibes':None, 93 'S_total':None, 'S_trans':None, 'S_rot':None, 94 'S_vibes':None, 95 'contributions':None, 'nframes':None, 96 'version':self.version(), 'node':self.node }
97 98
99 - def version( self ):
100 """ 101 Version of class. 102 103 @return: version 104 @rtype: str 105 """ 106 return 'AmberCrdEntropist $Revision: 2.5 $'
107 108
109 - def command( self ):
110 """ 111 Build the command string. 112 113 @return: command 114 @rtype: str 115 """ 116 return "%s %s %s" % (self.exe.bin, self.f_parm, self.f_in)
117 118 119
120 - def __tryMatch( self, regex, str, integer=0 ):
121 m = regex.search( str ) 122 if m: 123 return round( float( m.groups()[0] ), 3 ) 124 return None
125 126
127 - def finish( self ):
128 """ 129 Overrides Executor method 130 """ 131 self.result = self.parsePtrajResult( self.f_out )
132 133
134 - def isFailed( self ):
135 """ 136 Detect whether external program failed, override! 137 """ 138 if not osp.exists( self.f_out ): 139 return 1 140 141 return 0
142 143
144 - def parsePtrajResult( self, f_out ):
145 """ 146 Extract results from ptraj. 147 148 @param f_out: result of ptraj run 149 @type f_out: str 150 151 @return: extracted prtaj result 152 @rtype: dict 153 154 @raise EntropistError: if unexpected end of ptraj output file 155 """ 156 ## regular expressions for parsing of ptraj output 157 re_thermo= re.compile('- Thermochemistry -') 158 re_T = re.compile('^\s*temperature\s*(\d+\.\d+)\s*kelvin') 159 re_mass = re.compile('^\s*molecular mass\D*(\d+\.\d+)\s*amu') 160 re_vibes = re.compile('^\s*Warning--\s*(\d+)\s*vibrations have low') 161 re_table = re.compile('^-{80}$') 162 re_value = re.compile('(-*\d+\.\d+)$') 163 re_nsets = re.compile('Successfully read in (\d+) sets') 164 165 r = self.result 166 167 try: 168 lines = open( f_out, 'r' ).readlines() 169 lines.reverse() 170 l = lines.pop() 171 172 while not re_thermo.search(l): 173 l = lines.pop() 174 175 while not re_table.search(l): 176 r['T'] = r['T'] or self.__tryMatch( re_T, l ) 177 r['mass'] = r['mass'] or self.__tryMatch( re_mass, l ) 178 r['vibes']= r['vibes'] or self.__tryMatch( re_vibes,l ) 179 l = lines.pop() 180 181 l = lines.pop() 182 v = [] 183 while l != '\n': 184 v += [ self.__tryMatch( re_value, l ) ] 185 l = lines.pop() 186 187 r['S_total'], r['S_trans'], r['S_rot'], r['S_vibes'] = tuple(v[:4]) 188 r['contributions'] = v[4:] 189 190 while len( lines ) > 0: 191 r['nframes'] = r['nframes'] or self.__tryMatch( re_nsets, l ) 192 l = lines.pop() 193 194 except IndexError: 195 raise EntropistError, 'unexpected end of ptraj output file.' 196 197 return r
198 199 if __name__ == '__main__': 200 201 print "Setting up" 202 203 f = T.testRoot() + '/Amber/AmberCrdEntropist/' 204 205 e = AmberCrdEntropist( f + 'lig_traj.parm', 206 f + 'lig_traj.crd', debug=0, verbose=1) 207 208 print "Running" 209 210 e.run() 211