Back to index

python-biopython  1.60
Controller.py
Go to the documentation of this file.
00001 # Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 """
00007 This module allows you to control Simcoal2.
00008 
00009 """
00010 
00011 import os
00012 import sys
00013 import tempfile
00014 from shutil import copyfile
00015 from logging import debug
00016 
00017 class SimCoalController(object):
00018     def __init__(self, simcoal_dir):
00019         """Initializes the controller.
00020         
00021         simcoal_dir is the directory where simcoal is.
00022         
00023         The initializer checks for existance and executability of binaries.
00024         """
00025         self.simcoal_dir = simcoal_dir
00026         self.os_name = os.name #remove this?
00027         dir_contents = os.listdir(self.simcoal_dir)
00028         #We expect the tool to be installed as simcoal2(.exe)
00029         #without any trailing version number.
00030         self.bin_name = "simcoal2"
00031         if self.bin_name not in dir_contents:
00032             #Try case insensitive,
00033             dir_contents = [x.lower() for x in dir_contents]
00034         if self.bin_name not in dir_contents:
00035             #Try with .exe
00036             self.bin_name += '.exe'
00037         if self.bin_name not in dir_contents:
00038             raise IOError("SimCoal not available")
00039         if not os.access(os.path.join(self.simcoal_dir, self.bin_name),
00040                          os.X_OK):
00041             raise IOError("SimCoal not executable")
00042 
00043     def run_simcoal(self, par_file, num_sims, ploydi = '1', par_dir = '.'):
00044         """Executes SimCoal.
00045         """
00046         if par_dir == None:
00047             par_dir = os.sep.join([".", 'SimCoal', 'runs'])
00048         curr_dir = os.getcwd()
00049         #TODO - Make sure we change drive on Windows as well?
00050         os.chdir(par_dir)
00051         exe = os.path.join(self.simcoal_dir, self.bin_name)
00052         if " " in exe:
00053             exe = '"' + exe + '"'
00054         cmd = exe + ' ' + par_file + ' ' + str(num_sims) + ' ' + ploydi
00055         #TODO - Better way to spot if on Jython on Windows?
00056         if sys.platform=="win32" or self.bin_name.endswith(".exe"):
00057             #There is no /dev/nul on Windows
00058             cmd += ' > nul 2>nul'
00059         else:
00060             cmd += ' >/dev/null 2>&1'
00061         os.system(cmd)
00062         os.chdir(curr_dir)
00063     
00064