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

Source Code for Module Biskit.Dock.HexParser

  1  ## Automatically adapted for numpy.oldnumeric Mar 26, 2007 by alter_code1.py 
  2   
  3  ## 
  4  ## Biskit, a toolkit for the manipulation of macromolecular structures 
  5  ## Copyright (C) 2004-2006 Raik Gruenberg & Johan Leckner 
  6  ## 
  7  ## This program is free software; you can redistribute it and/or 
  8  ## modify it under the terms of the GNU General Public License as 
  9  ## published by the Free Software Foundation; either version 2 of the 
 10  ## License, or any later version. 
 11  ## 
 12  ## This program is distributed in the hope that it will be useful, 
 13  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 15  ## General Public License for more details. 
 16  ## 
 17  ## You find a copy of the GNU General Public License in the file 
 18  ## license.txt along with this program; if not, write to the Free 
 19  ## Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 20  ## 
 21  ##  
 22  ## $Revision: 2.10 $ 
 23  ## last $Author: graik $ 
 24  ## $Date: 2007/03/26 18:40:42 $ 
 25   
 26  """ 
 27  Parse output file from hex docking run. 
 28  """ 
 29   
 30  import re 
 31  import numpy.oldnumeric as Numeric  ## array 
 32  from Biskit.Dock import Complex, ComplexList 
 33   
 34  from Biskit import PCRModel 
 35  import Biskit.tools as t 
 36   
37 -class HexParser:
38 """ 39 Parse hex result file and extract Complex objects into a dictionary 40 indexed by the hex solution number. 41 The hex file is only closed when this object is discarded. 42 The HexParser is created with a dictionary containing PCRModel objects 43 for ligand and receptor indexed by hex model number. 44 """ 45
46 - def __init__(self, hexFile, rec_dic, lig_dic, forceModel=None):
47 """ 48 @param hexFile: name of hex output file 49 @type hexFile: string 50 @param rec_dic: map between model number and PCRModel 51 { 1 : model1, 2 : model2,..} types: { int : PCRModel} 52 @type rec_dic: {int:model} 53 @param lig_dic: same as rec_dic but for ligand PCRModels 54 @type lig_dic: {int:model} 55 @param forceModel: force parser to accept model numbers 56 @type forceModel: int, int 57 """ 58 self.hex = open( hexFile ) 59 self.rec_models = rec_dic 60 self.lig_models = lig_dic 61 self.forceModel = forceModel 62 ## pattern for analyzing single line of type "Value: -1.23": 63 self.ex_line = re.compile("^(\S+):\s*([-0-9\.\s]*)") 64 ## pattern to find one line of transformation matrix 65 self.ex_matrix = re.compile("([-0-9]+\.[0-9]+e[-+0-9]+)")
66 67
68 - def __del__(self):
69 self.hex.close()
70 71
72 - def nextComplex(self):
73 """ 74 Take list of lines, extract all Hex info about one complex 75 (Solution number, hex energy,..) also extract 16 numbers of 76 the transformation matrix and put them into 4 by 4 numeric 77 array. 78 79 @return: Complex created from the output from Hex 80 @rtype: Complex 81 """ 82 ## get set of lines describing next complex: 83 lines = self._nextBlock() 84 if lines == None: 85 ## EOF 86 return None 87 ## skip incomplete records 88 if len(lines) < 13: 89 lines = self._nextBlock() 90 91 ## fill info dictionary 92 i = {} 93 matrix = None 94 for l in lines: 95 try: 96 ## labels has to be in the same order as in hex.out 97 m = self.ex_line.search( l ) 98 if m != None: 99 m = m.groups() 100 101 if m[0] == 'Orientation': 102 i['hex_clst'] = int(m[1]) 103 elif m[0] == 'Solution': 104 i['soln'] = int(m[1]) 105 elif m[0] == 'ReceptorModel': 106 if self.forceModel: 107 i['model1'] = self.forceModel[0] 108 else: 109 i['model1'] = int(m[1]) 110 elif m[0] == 'LigandModel': 111 if self.forceModel: 112 i['model2'] = self.forceModel[1] 113 else: 114 i['model2'] = int(m[1]) 115 elif m[0] == 'Bumps': 116 if int(m[1]) != -1: 117 i['bumps'] = int(m[1]) 118 elif m[0] == 'ReferenceRMS': 119 i['rms'] = float(m[1]) 120 elif m[0] == 'Vshape': 121 if float(m[1]) != 0.0: 122 i['hex_Vshape'] = float(m[1]) 123 elif m[0] == 'Vclash': 124 if float(m[1]) != 0.0: 125 i['hex_Vclash'] = float(m[1]) 126 elif m[0] == 'Etotal': 127 i['hex_etotal'] = float(m[1]) 128 elif m[0] == 'Eshape': 129 i['hex_eshape'] = float(m[1]) 130 elif m[0] == 'LigandMatrix': 131 ## get all numbers of matrix as list of strings 132 strings = self.ex_matrix.findall( l ) 133 ## convert that to list of floats 134 numbers = [] 135 for each in strings: 136 numbers += [float(each)] 137 ## convert that to list of lists of 4 floats each 138 matrix = [] 139 for j in range(0,4): 140 matrix.append( numbers[4*j:4*(j+1)] ) 141 ## create 4 by 4 Numeric array from 4 by 4 list 142 matrix = Numeric.array(matrix, Numeric.Float32) 143 except AttributeError: 144 print "HexParser.nextComplex(): ",t.lastError() 145 146 ## Create new complex taking PCR models from dictionary 147 c = Complex( self.rec_models[ i['model1'] ], 148 self.lig_models[ i['model2'] ], matrix, i ) 149 return c
150 151
152 - def _nextBlock(self):
153 """ 154 return all lines describing next complex in the Hex output file. 155 156 @return: list of information strings 157 @rtype: [str] 158 """ 159 line = self.hex.readline() 160 result = [] 161 while line: 162 if line[:4] != "# --": 163 ## found matrix line, append it to previous line 164 if self.ex_matrix.search(line) != None: 165 result[-1] = result[-1] + line 166 else: 167 result += [line] 168 else: 169 return result 170 line = self.hex.readline() 171 172 ## end of file: 173 return None
174 175
176 - def parseHex(self):
177 """ 178 Create one Complex Object for each paragraph in hex output file. 179 180 @return: ComplexList with all complexes from the Hex output 181 @rtype: ComplexList 182 """ 183 complexes = ComplexList() 184 c = self.nextComplex() 185 186 while (c <> None): 187 complexes.append(c) 188 c = self.nextComplex( ) ## look for next cluster 189 190 return complexes
191 192 193 ############# 194 ## TESTING 195 ############# 196 import Biskit.test as BT 197
198 -class Test(BT.BiskitTest):
199 """Test case""" 200
201 - def test_hexParser(self):
202 """Dock.hexParser test""" 203 204 rec_dic = t.Load( t.testRoot() + "/dock/rec/1A2P_model.dic" ) 205 lig_dic = t.Load( t.testRoot() + "/dock/lig/1A19_model.dic" ) 206 207 self.h = HexParser( t.testRoot() + "/dock/hex/1A2P-1A19_hex.out", 208 rec_dic, lig_dic) 209 210 c_lst = self.h.parseHex() 211 212 if self.local: 213 print c_lst[1].info 214 215 globals().update( locals() ) 216 217 218 self.assertEqual( c_lst[1].info.keys(), 219 ['soln', 'rms', 'hex_clst', 'hex_eshape', 220 'model2', 'model1', 'hex_etotal', 'date'] )
221 222 if __name__ == '__main__': 223 224 BT.localTest() 225