Package Biskit :: Package Mod :: Module ModelMaster
[hide private]
[frames] | no frames]

Source Code for Module Biskit.Mod.ModelMaster

  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  ## Contributions: Olivier PERIN, Raik Gruenberg 
 20  ## last $Author: leckner $ 
 21  ## last $Date: 2006/09/13 10:28:14 $ 
 22  ## $Revision: 2.8 $ 
 23  """ 
 24  Parallelize Modeller runs 
 25  """ 
 26   
 27  from Biskit.PVM.TrackingJobMaster import TrackingJobMaster 
 28   
 29  import Biskit.hosts as hosts 
 30  from Biskit.tools import projectRoot 
 31  import Biskit.tools as T 
 32  import os, glob 
 33   
34 -class ModelMaster(TrackingJobMaster):
35 36 slave_script = projectRoot() + '/Biskit/Mod/ModelSlave.py' 37
38 - def __init__(self, hosts, folders, fastaTarget=None, f_pir=None, 39 template_folder=None, fout=None, starting_model=1, 40 ending_model=10, ferror=None, verbose=1, **kw):
41 """ 42 @param hosts: list of host-names 43 @type hosts: [str] 44 @param folders: list of project directories (path) 45 @type folders: [str] 46 @param fastaTarget: path to find 'target.fasta' 47 @type fastaTarget: str 48 @param f_pir: alignment filename 49 @type f_pir: str 50 @param template_folder: directories for input atom files 51 @type template_folder: str 52 @param fout: default modeller input file 'modeller.top' 53 @type fout: str 54 @param starting_model: index of the first model (default: 1) 55 @type starting_model: int 56 @param ending_model: index of the last model (default: 10) 57 @type ending_model: int 58 @param ferror: filename to output errors from the Slave 59 @type ferror: str 60 @param verbose: verbosity level (default: 1) 61 @type verbose: 1|0 62 @param kw: additional TrackingJobMaster arguments:: 63 chunk_size - int, number of items that are processed per job 64 niceness - {str_host-name: int_niceness} 65 slave_script - str, absolute path to slave-script 66 show_output - 1|0, display one xterm per slave [0] 67 add_hosts - 1|0, add hosts to PVM before starting [1] 68 @type kw: param=value 69 """ 70 self.verbose = verbose 71 self.folders = folders 72 self.fastaTarget = fastaTarget 73 self.f_pir = f_pir 74 75 self.template_folder = template_folder 76 self.fout = fout 77 self.starting_model = starting_model 78 self.ending_model = ending_model 79 self.ferror = ferror or 'ModelSlaveErrors.out' 80 81 data = self.setupJobs() 82 83 84 TrackingJobMaster.__init__(self, data=data, chunk_size=1, hosts=hosts, 85 slave_script = self.slave_script, 86 redistribute=0, verbose=verbose, **kw)
87 88
89 - def __dir_or_none( self, folder, filename ):
90 if filename is None: 91 return None 92 return os.path.join( folder, filename )
93 94
95 - def setupJobs(self):
96 """ 97 Prepare the job dictionnary for 'ModelSlave' 98 99 @return: input informations for modeller for each project 100 @rtype: {{str}} 101 """ 102 r = {} 103 104 for f in self.folders: 105 modeller_input = {} 106 modeller_input['outFolder'] = T.absfile(f) 107 modeller_input['fastaTarget'] = \ 108 self.__dir_or_none( f, self.fastaTarget ) 109 modeller_input['f_pir'] = self.__dir_or_none( f, self.f_pir ) 110 modeller_input['template_folder'] = \ 111 self.__dir_or_none( f, self.template_folder ) 112 modeller_input['starting_model'] = self.starting_model 113 modeller_input['ending_model'] = self.ending_model 114 115 r[T.absfile(f)] = modeller_input 116 117 return r
118 119
120 - def getInitParameters(self, slave_tid):
121 """ 122 Hand over parameters to slave once. 123 124 @param slave_tid: slave task id 125 @type slave_tid: int 126 127 @return: dictionary with init parameters 128 @rtype: {param:value} 129 """ 130 return {'progress_str':'slave calculating..', 'ferror':self.ferror}
131 132
133 - def cleanup( self ):
134 if self.verbose: print "Cleaning up..."
135 136
137 - def done( self ):
138 if self.verbose: print "Done modeling."
139 140 141 142 143 ############# 144 ## TESTING 145 ############# 146
147 -class Test:
148 """ 149 Test class 150 """ 151
152 - def run( self, local=0, run=0, model_testRoot=0 ):
153 """ 154 run function test 155 156 @param local: transfer local variables to global and perform 157 other tasks only when run locally 158 @type local: 1|0 159 @param run: run the full test (call external application) or not 160 @type run: 1|0 161 @param model_testRoot: align the full validation project in testRoot 162 @type model_testRoot: 1|0 163 164 @return: 1 165 @rtype: int 166 """ 167 import tempfile 168 import shutil 169 170 if model_testRoot: 171 172 projRoot = T.testRoot()+'/Mod/project' 173 projects = glob.glob( projRoot + '/validation/*' ) 174 175 master = ModelMaster(folders = projects, 176 hosts=hosts.cpus_all[ : 10 ]) 177 178 r = master.calculateResult() 179 180 if local: 181 globals().update( locals() ) 182 183 return 1 184 185 186 ## collect the input files needed 187 outfolder = tempfile.mkdtemp( '_test_Modeller' ) 188 os.mkdir( outfolder +'/templates' ) 189 os.mkdir( outfolder +'/t_coffee' ) 190 191 shutil.copytree( T.testRoot() + '/Mod/project/templates/modeller', 192 outfolder + '/templates/modeller' ) 193 194 shutil.copy( T.testRoot() + '/Mod/project/t_coffee/final.pir_aln', 195 outfolder + '/t_coffee' ) 196 197 shutil.copy( T.testRoot() + '/Mod/project/target.fasta', 198 outfolder ) 199 200 master = ModelMaster(folders = [outfolder], 201 hosts=hosts.cpus_all[ : 10 ], 202 show_output = local, 203 verbose = local ) 204 205 if run: 206 r = master.calculateResult() 207 if local: print 'The models result can be found in %s/modeller'%outfolder 208 209 if local: 210 globals().update( locals() ) 211 212 ## cleanup 213 T.tryRemove( outfolder, tree=1 ) 214 215 return 1
216 217
218 - def expected_result( self ):
219 """ 220 Precalculated result to check for consistent performance. 221 222 @return: 1 223 @rtype: int 224 """ 225 return 1
226 227 228 if __name__ == '__main__': 229 230 test = Test() 231 232 assert test.run( run=1, local=1 ) == test.expected_result() 233