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