File: Cache.py

package info (click to toggle)
python-biopython 1.68%2Bdfsg-3~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 46,856 kB
  • sloc: python: 160,306; xml: 93,216; ansic: 9,118; sql: 1,208; makefile: 155; sh: 63
file content (86 lines) | stat: -rw-r--r-- 2,883 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved.

"""Cache for Simcoal2 results (DEPRECATED).

This module allows you to cache Simcoal2 results, and return on the fly
in case the calculation was done.
"""

import os
import tarfile
from .Controller import SimCoalController


class SimCoalCache(object):
    def __init__(self, data_dir, simcoal_dir):
        """Initializes the cache.

            - data_dir - Where the cache can be found
            - simcoal_dir - where the binaries are

        IMPORTANT: The cache only makes sense if the file name univocally
        identifies the model.
        For now use use the model name as key,
        and it will probably stay like that.
        """
        self.dataDir = data_dir
        self.cacheDir = os.sep.join([data_dir, 'SimCoal', 'cache'])
        self.simcoalDir = simcoal_dir

    def run_simcoal(self, par_file, num_sims, ploydi='1', parDir=None):
        if parDir is None:
            parDir = os.sep.join([self.dataDir, 'SimCoal', 'runs'])
        par_file_root = par_file[:-4]
        tar_name = os.sep.join([self.cacheDir, ploydi, par_file_root +
                                '.tar.bz2'])
        if os.access(tar_name, os.R_OK):
            tf = tarfile.open(tar_name)
            tar_num_sims = len(tf.getmembers()) - 3
        else:
            tar_num_sims = 0
        if tar_num_sims >= num_sims:
            tf.extractall(parDir)
            tf.close()
            return
        else:
            try:
                tf.close()
            except NameError:
                pass  # not opened in the first place, OK.
        scc = SimCoalController(self.simcoalDir)
        scc.run_simcoal(par_file, num_sims, ploydi, parDir)
        tf = tarfile.open(tar_name, 'w:bz2')
        tf.add(os.sep.join([parDir, par_file_root]), par_file_root)
        tf.close()

    def listSimulations(self, ploidy='1'):
        """
           Lists available simulations.
        """
        files = os.listdir(self.cacheDir + os.sep + ploidy)
        sims = []
        for file in files:
            if file.endswith('.tar.bz2'):
                sims.append(file[:-8])
        return sims

    def getSimulation(self, sim_name, ploidy='1', parDir=None):
        """Makes available a cached simulation.

        @param sim_name simulation name.

        This mainly means untaring a file.
        """
        if parDir is None:
            parDir = os.sep.join([self.dataDir, 'SimCoal', 'runs'])
        tar_name = os.sep.join([self.cacheDir, ploidy, sim_name +
                                '.tar.bz2'])
        tf = tarfile.open(tar_name)
        tf.extractall(parDir)
        tf.close()


# if __name__ == '__main__':
#  cache = Cache('/home/work/werk/consolidator/sc_cache',
#      '/home/work/software/simcoal')
#  cache.run_simcoal('.', 'island_snp-50_0.0025_10_0.083_100_60.par', 102)