File: _paml.py

package info (click to toggle)
python-biopython 1.68%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 46,860 kB
  • ctags: 13,237
  • sloc: python: 160,306; xml: 93,216; ansic: 9,118; sql: 1,208; makefile: 155; sh: 63
file content (164 lines) | stat: -rw-r--r-- 6,463 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright (C) 2011 by Brandon Invergo (b.invergo@gmail.com)
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.

from __future__ import print_function

import os
import subprocess


try:
    from os.path import relpath as _relpath
except ImportError:
    # New in Python 2.6
    def _relpath(path, start=None):
        """Return a relative version of a path.

        Implementation by James Gardner in his BareNecessities
        package, under MIT licence.

        With a fix for Windows where posixpath.sep (and functions like
        join) use the Unix slash not the Windows slash.
        """
        import posixpath
        if start is None:
            start = posixpath.curdir
        else:
            start = start.replace(os.path.sep, posixpath.sep)
        if not path:
            raise ValueError("no path specified")
        else:
            path = path.replace(os.path.sep, posixpath.sep)
        start_list = posixpath.abspath(start).split(posixpath.sep)
        path_list = posixpath.abspath(path).split(posixpath.sep)
        # Work out how much of the filepath is shared by start and path.
        i = len(posixpath.commonprefix([start_list, path_list]))
        rel_list = [posixpath.pardir] * (len(start_list) - i) + path_list[i:]
        if not rel_list:
            return posixpath.curdir.replace(posixpath.sep, os.path.sep)
        return posixpath.join(*rel_list).replace(posixpath.sep, os.path.sep)


class PamlError(EnvironmentError):
    """paml has failed. Run with verbose = True to view the error
    message"""


class Paml(object):
    """Base class for wrapping PAML commands."""

    def __init__(self, alignment=None, working_dir=None,
                 out_file=None):
        if working_dir is None:
            self.working_dir = os.getcwd()
        else:
            self.working_dir = working_dir
        if alignment is not None:
            if not os.path.exists(alignment):
                raise IOError("The specified alignment file does not exist.")
        self.alignment = alignment
        self.out_file = out_file
        self._options = {}  # will be set in subclasses

    def write_ctl_file(self):
        pass

    def read_ctl_file(self):
        pass

    def print_options(self):
        """Print out all of the options and their current settings."""
        for option in self._options.items():
            print("%s = %s" % (option[0], option[1]))

    def set_options(self, **kwargs):
        """Set the value of an option.

        This function abstracts the options dict to prevent the user from
        adding options that do not exist or misspelling options.
        """
        for option, value in kwargs.items():
            if option not in self._options:
                raise KeyError("Invalid option: " + option)
            else:
                self._options[option] = value

    def get_option(self, option):
        """Return the value of an option."""
        if option not in self._options:
            raise KeyError("Invalid option: " + option)
        else:
            return self._options.get(option)

    def get_all_options(self):
        """Return the values of all the options."""
        return list(self._options.items())

    def _set_rel_paths(self):
        """Convert all file/directory locations to paths relative to the current working directory.

        paml requires that all paths specified in the control file be
        relative to the directory from which it is called rather than
        absolute paths.
        """
        if self.working_dir is not None:
            self._rel_working_dir = _relpath(self.working_dir)
        if self.alignment is not None:
            self._rel_alignment = _relpath(self.alignment,
                self.working_dir)
        if self.out_file is not None:
            self._rel_out_file = _relpath(self.out_file, self.working_dir)

    def run(self, ctl_file, verbose, command):
        """Run a paml program using the current configuration and then parse the results.

        Return a process signal so the user can determine if
        the execution was successful (return code 0 is successful, -N
        indicates a failure). The arguments may be passed as either
        absolute or relative paths, despite the fact that paml
        requires relative paths.
        """
        if self.alignment is None:
            raise ValueError("Alignment file not specified.")
        if not os.path.exists(self.alignment):
            raise IOError("The specified alignment file does not exist.")
        if self.out_file is None:
            raise ValueError("Output file not specified.")
        if self.working_dir is None:
            raise ValueError("Working directory not specified.")
        # Get the current working directory
        cwd = os.getcwd()
        # Move to the desired working directory
        if not os.path.exists(self.working_dir):
            os.mkdir(self.working_dir)
        os.chdir(self.working_dir)
        # If no external control file was specified...
        if ctl_file is None:
            # Dynamically build a control file
            self.write_ctl_file()
            if verbose:
                result_code = subprocess.call([command, self.ctl_file])
            else:
                # To suppress output, redirect it to a pipe to nowhere
                result_code = subprocess.call([command, self.ctl_file],
                                              stdout=subprocess.PIPE)
        else:
            if not os.path.exists(ctl_file):
                raise IOError("The specified control file does not exist.")
            if verbose:
                result_code = subprocess.call([command, ctl_file])
            else:
                result_code = subprocess.call([command, ctl_file],
                                              stdout=subprocess.PIPE)
        os.chdir(cwd)
        if result_code > 0:
            # If the program fails for any reason
            raise PamlError(
                "%s has failed (return code %i). Run with verbose = True to view error message"
                % (command, result_code))
        if result_code < 0:
            # If the paml process is killed by a signal somehow
            raise EnvironmentError("The %s process was killed (return code %i)."
                                   % (command, result_code))