@@ -142,6 +142,18 @@ def _load_raxml_help(self):
142142 iface .ml_help .props .buffer .props .text = res .stdout .decode ().lstrip ()
143143 LOG .debug ('got RAxML --help' )
144144
145+ try :
146+ # get number of available CPUs
147+ res = run (args = 'nproc ' , stdout = PIPE , stderr = PIPE , shell = True )
148+ cpus = int (res .stdout .decode ().strip ())
149+ iface .cpu_count .set_text (str (cpus ))
150+ cpu_adj = iface .cpu_use .get_adjustment ().props
151+ cpu_adj .upper = cpus
152+ cpu_adj .value = cpus
153+ LOG .debug ('found %d CPUs' % cpus )
154+ except Exception as ex :
155+ LOG .error ('reading CPUs failed' )
156+
145157 def reload_ui_state (self ):
146158 data = self .data
147159 iface = self .iface
@@ -180,7 +192,7 @@ def start_ML(self, widget, mode, run_after=None):
180192
181193 if mode in ['raxml' , 'raxml_export' ]:
182194 ml .raxml = iface .raxml_exe .get_filename ()
183- for w_name in ['evo_modify' , 'bootstraps' , 'rand' , 'pars' , 'raxml_seed' ]:
195+ for w_name in ['evo_modify' , 'bootstraps' , 'rand' , 'pars' , 'raxml_seed' , 'cpu_use' ]:
184196 ml .__setattr__ (w_name , iface .__getattribute__ (w_name ).get_text ())
185197 if w_name in ['bootstraps' , 'rand' , 'pars' ]:
186198 ml .__setattr__ (w_name , int (ml .__getattribute__ (w_name )))
@@ -227,23 +239,24 @@ def do_ML(self, mode):
227239 iface .i = 0
228240
229241 # prepare the calls
230- chck = '%s --msa %s --check --model %s' \
231- + ml .evo_modify + ' --prefix %s '
242+ chck = '%s --msa "%s" --check --model %s' \
243+ + ml .evo_modify + ' --prefix "%s" '
232244
233- inML = '%s --msa %s --model %s' + ml .evo_modify + \
234- ' --prefix %s ' + ' --seed %d' % ml .raxml_seed + \
235- ' --threads auto{16 } --workers auto{16}' \
245+ inML = '%s --msa "%s" --model %s' + ml .evo_modify + \
246+ ' --prefix "%s" ' + ' --seed %d' % ml .raxml_seed + \
247+ ' --threads auto{%s } --workers auto{%s}' + \
236248 ' --redo --tree %s ' % ',' .join (
237249 [a for a in ['rand{%d}' % ml .rand if ml .rand > 0 else None ,
238250 'pars{%d}' % ml .pars if ml .pars > 0 else None ] if a ])
239251
240- boot = '%s --bootstrap --msa %s --model %s --tree %s ' + \
241- ' --prefix %s ' + ' --bs-trees %d' % ml .bootstraps + \
252+ boot = '%s --bootstrap --msa "%s" --model %s --tree "%s" ' + \
253+ ' --prefix "%s" ' + ' --bs-trees %d' % ml .bootstraps + \
242254 ' --seed %d' % ml .raxml_seed + \
243- ' --threads auto{16 } --workers auto{16 } --redo'
255+ ' --threads auto{%s } --workers auto{%s } --redo'
244256
245- supp = '%s --support --tree %s --bs-trees %s --bs-metric fbp,tbe ' \
246- '--prefix %s --threads auto{16} --workers auto{16} --redo'
257+ supp = '%s --support --tree "%s" --bs-trees %s --bs-metric fbp,tbe ' + \
258+ '--prefix "%s" --threads ' + \
259+ 'auto{%s} --workers auto{%s} --redo'
247260
248261 # res = run(stdout=PIPE, stderr=PIPE, args=shlex.split(
249262 # arg % (ml.raxml, msa, prefix / 'bs_')))
@@ -256,45 +269,48 @@ def do_ML(self, mode):
256269
257270 if ml .raxml_shell :
258271 with open (shell , 'w' ) as sh :
259- sh .write ('#!/bin/bash\n \n trap \' \' SIGINT \n \ n ' )
272+ sh .write ('#!/bin/bash\n \n ' )
260273
261274 # loop over the stages
262275 for i , (desc , key , prev , arg , add ) in enumerate (zip (
263276 ['check MSA' , 'infer ML tree' , 'bootstrapping' , 'calc. branch support' ],
264277 [False , 'ML' , 'BS' , False ], [0 , 1 , ml .rand + ml .pars + 1 , iface .k - 2 ],
265278 [chck , inML , boot , supp ],
266279 [(ml .raxml , msa , ml .evo_model , prefix / 'chk' ),
267- (ml .raxml , msa , ml .evo_model , prefix / 'ml' ),
280+ (ml .raxml , msa , ml .evo_model ,
281+ prefix / 'ml' , ml .cpu_use , ml .cpu_use ),
268282 (ml .raxml , msa , prefix / 'ml.raxml.bestModel' ,
269- prefix / 'ml.raxml.bestTree' , prefix / 'bs' ),
283+ prefix / 'ml.raxml.bestTree' ,
284+ prefix / 'bs' , ml .cpu_use , ml .cpu_use ),
270285 (ml .raxml , prefix / 'ml.raxml.bestTree' ,
271- prefix / 'bs.raxml.bootstraps' , prefix / 'sp' )])):
286+ prefix / 'bs.raxml.bootstraps' ,
287+ prefix / 'sp' , ml .cpu_use , ml .cpu_use )])):
272288
273289 if ml .raxml_shell and mode == 'raxml' :
274290 with open (shell , 'a' ) as sh :
275291 sh .write ('# %s\n ' % desc )
276- sh .write (arg % add )
277- # if i != 2:
278- # sh.write(arg % add)
279- # else:
280- # # special case bootstrapping:
281- # bash = '''
282- # mkfifo pipe || exit 1
283- # (%s) > pipe &
284- # pid=$!
285- # echo "AB12PHYLO: Bootstrapping PID is $pid"
286- # while read -r line; do
287- # echo "$line"
288- # if [[ "${line::12}" == "Elapsed time" ]]; then
289- # echo "AB12PHYLO: Finished bootstrapping, terminating process $pid to ensure it exits."
290- # kill -s SIGTERM $pid
291- # break
292- # fi
293- # done < pipe
294- # rm pipe
295- # '''.strip() % (arg % add)
296- # sh.write(bash)
297- # sh.write('\n\necho "AB12PHYLO: %s done"\n' % desc)
292+ # sh.write(arg % add)
293+ if i != 2 :
294+ sh .write (arg % add )
295+ else :
296+ # special case bootstrapping:
297+ bash = '''
298+ mkfifo pipe || exit 1
299+ (%s) > pipe &
300+ pid=$!
301+ echo "AB12PHYLO: Bootstrapping PID is $pid"
302+ while read -r line; do
303+ echo "$line"
304+ if [[ "${line::12}" == "Elapsed time" ]]; then
305+ echo "AB12PHYLO: Finished bootstrapping, terminating process $pid to ensure it exits."
306+ kill -s SIGTERM $pid
307+ break
308+ fi
309+ done < pipe
310+ rm pipe
311+ ''' .strip () % (arg % add )
312+ sh .write (bash )
313+ sh .write ('\n \n echo "AB12PHYLO: %s done"\n ' % desc )
298314 if i != 3 :
299315 sh .write ('\n sleep 1s\n \n ' )
300316 continue
@@ -346,17 +362,41 @@ def do_ML(self, mode):
346362 LOG .debug (iface .text )
347363 sh = 'raxml_run.sh'
348364 with open (sh , 'w' ) as sf :
349- sf .write ('#!/bin/bash' )
365+ bash = '''
366+ #!/bin/bash\n
367+ echo "You can set a CPU limit here! This will proceed in a bit, interrupt with Ctrl+C"\n
368+ print_usage() {
369+ printf "Usage: Limit the number of threads/logical cores used via -f <number>"\n }\n
370+ print_usage\n
371+ sleep 10s\n
372+ cpu_limit=400\n
373+ while getopts 'f:' flag; do
374+ case "${flag}" in
375+ f) cpu_limit="${OPTARG}" ;;
376+ *) print_usage
377+ exit 1 ;;\n esac\n done\n
378+ # get the number of CPUs available
379+ cpus=$(nproc)\n
380+ # find the minimum of the CPUs allowed and available
381+ if [ $cpu_limit -lt $cpus ]; then
382+ used=$cpu_limit\n fi
383+ if [ $cpus -lt $cpu_limit ];then
384+ used=$cpus\n fi\n
385+ echo "CPUs available: $cpus, use at most $used"
386+ ''' .strip ()
387+ sf .write (bash )
350388 sf .write ('\n \n # Check MSA\n ' )
351- sf .write (chck % ('./raxml-ng' , 'msa.fasta' , Path (ml .evo_model ).name , 'chk' ))
389+ sf .write (chck % ('./raxml-ng' , 'msa.fasta' ,
390+ Path (ml .evo_model ).name , 'chk' ))
352391 sf .write ('\n \n # Find best ML tree\n ' )
353- sf .write (inML % ('./raxml-ng' , 'msa.fasta' , Path (ml .evo_model ).name , 'ml' ))
392+ sf .write (inML % ('./raxml-ng' , 'msa.fasta' ,
393+ Path (ml .evo_model ).name , 'ml' , '$used' , '$used' ))
354394 sf .write ('\n \n # Compute bootstrap iterations\n ' )
355395 sf .write (boot % ('./raxml-ng' , 'msa.fasta' , 'ml.raxml.bestModel' ,
356- 'ml.raxml.bestTree' , 'bs' ))
396+ 'ml.raxml.bestTree' , 'bs' , '$used' , '$used' ))
357397 sf .write ('\n \n # Calculate branch support\n ' )
358398 sf .write (supp % ('./raxml-ng' , 'ml.raxml.bestTree' ,
359- 'bs.raxml.bootstraps' , 'sp' ))
399+ 'bs.raxml.bootstraps' , 'sp' , '$used' , '$used' ))
360400 sf .write ('\n \n ' )
361401
362402 with ZipFile (self .wd / 'RAxML_export.zip' , 'w' , ZIP_DEFLATED ) as zf :
0 commit comments