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