Back to index

python-biopython  1.60
Cache.py
Go to the documentation of this file.
00001 # Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved.
00002 
00003 """
00004 This module allows to cache Simcoal2 results, and return on the fly
00005 in case the calculation was done.
00006 
00007 """
00008 
00009 from logging import debug
00010 from sys import exit
00011 import os
00012 import tarfile
00013 import tempfile
00014 from Controller import SimCoalController
00015 
00016 class SimCoalCache(object):
00017     def __init__(self, data_dir, simcoal_dir):
00018         """Initializes the cache.
00019         
00020         data_dir - Where the cache can be found
00021         simcoal_dir - where the binaries are
00022 
00023         IMPORTANT: The cache only makes sense if the file name univocally
00024         identifies the model.
00025           For now use use the model name as key,
00026           and it will probably stay like that.
00027         """
00028         self.dataDir = data_dir
00029         self.cacheDir = os.sep.join([data_dir, 'SimCoal', 'cache'])
00030         self.simcoalDir = simcoal_dir
00031 
00032 
00033     def run_simcoal(self, par_file, num_sims, ploydi = '1', parDir = None):
00034         if parDir == None:
00035             parDir = os.sep.join([self.dataDir, 'SimCoal', 'runs'])
00036         par_file_root = par_file[:-4]
00037         tar_name = os.sep.join([self.cacheDir, ploydi, par_file_root + 
00038             '.tar.bz2'])
00039         if os.access(tar_name, os.R_OK):
00040             tf = tarfile.open(tar_name)
00041             tar_num_sims = len(tf.getmembers()) - 3
00042         else:
00043             tar_num_sims = 0
00044         if tar_num_sims >= num_sims:
00045             tf.extractall(parDir)
00046             tf.close()
00047             return
00048         else:
00049             try:
00050                 tf.close()
00051             except NameError:
00052                 pass #not opened in the first place, OK.
00053         scc = SimCoalController(self.simcoalDir)
00054         scc.run_simcoal(par_file, num_sims, ploydi, parDir)
00055         tf = tarfile.open(tar_name, 'w:bz2')
00056         tf.add(os.sep.join([parDir, par_file_root]), par_file_root)
00057         tf.close()
00058 
00059     def listSimulations(self, ploidy = '1'):
00060         '''
00061            Lists available simulations.
00062         '''
00063         files = os.listdir(self.cacheDir + os.sep + ploidy)
00064         sims = []
00065         for file in files:
00066             if file.endswith('.tar.bz2'):
00067                 sims.append(file[:-8])
00068         return sims
00069 
00070     def getSimulation(self, sim_name, ploidy = '1', parDir = None):
00071         '''
00072            Makes available a cached simulation.
00073 
00074            @param sim_name simulation name.
00075 
00076            This mainly means untaring a file.
00077         '''
00078         if parDir == None:
00079             parDir = os.sep.join([self.dataDir, 'SimCoal', 'runs'])
00080         tar_name = os.sep.join([self.cacheDir, ploidy, sim_name + 
00081             '.tar.bz2'])
00082         tf = tarfile.open(tar_name)
00083         tf.extractall(parDir)
00084         tf.close()
00085 
00086 
00087 #if __name__ == '__main__':
00088 #  cache = Cache('/home/work/werk/consolidator/sc_cache',
00089 #      '/home/work/software/simcoal')
00090 #  cache.run_simcoal('.', 'island_snp-50_0.0025_10_0.083_100_60.par', 102)