Package Bio :: Package PopGen :: Package SimCoal :: Module Cache
[hide private]
[frames] | no frames]

Source Code for Module Bio.PopGen.SimCoal.Cache

 1  # Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved. 
 2   
 3  """ 
 4  (DEPRECATED) 
 5  This module allows to cache Simcoal2 results, and return on the fly 
 6  in case the calculation was done. 
 7   
 8  """ 
 9   
10  import os 
11  import tarfile 
12  from .Controller import SimCoalController 
13   
14  __docformat__ = "restructuredtext en" 
15   
16 -class SimCoalCache(object):
17 - def __init__(self, data_dir, simcoal_dir):
18 """Initializes the cache. 19 20 - data_dir - Where the cache can be found 21 - simcoal_dir - where the binaries are 22 23 IMPORTANT: The cache only makes sense if the file name univocally 24 identifies the model. 25 For now use use the model name as key, 26 and it will probably stay like that. 27 """ 28 self.dataDir = data_dir 29 self.cacheDir = os.sep.join([data_dir, 'SimCoal', 'cache']) 30 self.simcoalDir = simcoal_dir
31
32 - def run_simcoal(self, par_file, num_sims, ploydi='1', parDir=None):
33 if parDir is None: 34 parDir = os.sep.join([self.dataDir, 'SimCoal', 'runs']) 35 par_file_root = par_file[:-4] 36 tar_name = os.sep.join([self.cacheDir, ploydi, par_file_root + 37 '.tar.bz2']) 38 if os.access(tar_name, os.R_OK): 39 tf = tarfile.open(tar_name) 40 tar_num_sims = len(tf.getmembers()) - 3 41 else: 42 tar_num_sims = 0 43 if tar_num_sims >= num_sims: 44 tf.extractall(parDir) 45 tf.close() 46 return 47 else: 48 try: 49 tf.close() 50 except NameError: 51 pass # not opened in the first place, OK. 52 scc = SimCoalController(self.simcoalDir) 53 scc.run_simcoal(par_file, num_sims, ploydi, parDir) 54 tf = tarfile.open(tar_name, 'w:bz2') 55 tf.add(os.sep.join([parDir, par_file_root]), par_file_root) 56 tf.close()
57
58 - def listSimulations(self, ploidy='1'):
59 ''' 60 Lists available simulations. 61 ''' 62 files = os.listdir(self.cacheDir + os.sep + ploidy) 63 sims = [] 64 for file in files: 65 if file.endswith('.tar.bz2'): 66 sims.append(file[:-8]) 67 return sims
68
69 - def getSimulation(self, sim_name, ploidy='1', parDir=None):
70 ''' 71 Makes available a cached simulation. 72 73 @param sim_name simulation name. 74 75 This mainly means untaring a file. 76 ''' 77 if parDir is None: 78 parDir = os.sep.join([self.dataDir, 'SimCoal', 'runs']) 79 tar_name = os.sep.join([self.cacheDir, ploidy, sim_name + 80 '.tar.bz2']) 81 tf = tarfile.open(tar_name) 82 tf.extractall(parDir) 83 tf.close()
84 85 86 # if __name__ == '__main__': 87 # cache = Cache('/home/work/werk/consolidator/sc_cache', 88 # '/home/work/software/simcoal') 89 # cache.run_simcoal('.', 'island_snp-50_0.0025_10_0.083_100_60.par', 102) 90