Package Bio :: Package Phylo :: Package PAML :: Module _paml
[hide private]
[frames] | no frames]

Source Code for Module Bio.Phylo.PAML._paml

  1  # Copyright (C) 2011 by Brandon Invergo (b.invergo@gmail.com) 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license. Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  from __future__ import print_function 
  7   
  8  import os 
  9  import subprocess 
 10   
 11  try: 
 12      from os.path import relpath as _relpath 
 13  except ImportError: 
 14      # New in Python 2.6 
15 - def _relpath(path, start=None):
16 """Return a relative version of a path. 17 18 Implementation by James Gardner in his BareNecessities 19 package, under MIT licence. 20 21 With a fix for Windows where posixpath.sep (and functions like 22 join) use the Unix slash not the Windows slash. 23 """ 24 import posixpath 25 if start is None: 26 start = posixpath.curdir 27 else: 28 start = start.replace(os.path.sep, posixpath.sep) 29 if not path: 30 raise ValueError("no path specified") 31 else: 32 path = path.replace(os.path.sep, posixpath.sep) 33 start_list = posixpath.abspath(start).split(posixpath.sep) 34 path_list = posixpath.abspath(path).split(posixpath.sep) 35 # Work out how much of the filepath is shared by start and path. 36 i = len(posixpath.commonprefix([start_list, path_list])) 37 rel_list = [posixpath.pardir] * (len(start_list)-i) + path_list[i:] 38 if not rel_list: 39 return posixpath.curdir.replace(posixpath.sep, os.path.sep) 40 return posixpath.join(*rel_list).replace(posixpath.sep, os.path.sep)
41 42
43 -class PamlError(EnvironmentError):
44 """paml has failed. Run with verbose = True to view the error 45 message"""
46 47
48 -class Paml(object):
49
50 - def __init__(self, alignment=None, working_dir=None, 51 out_file=None):
52 if working_dir is None: 53 self.working_dir = os.getcwd() 54 else: 55 self.working_dir = working_dir 56 if alignment is not None: 57 if not os.path.exists(alignment): 58 raise IOError("The specified alignment file does not exist.") 59 self.alignment = alignment 60 self.out_file = out_file
61
62 - def write_ctl_file(self):
63 pass
64
65 - def read_ctl_file(self):
66 pass
67
68 - def print_options(self):
69 """Print out all of the options and their current settings.""" 70 for option in self._options.items(): 71 print("%s = %s" % (option[0], option[1]))
72
73 - def set_options(self, **kwargs):
74 """Set the value of an option. 75 76 This function abstracts the options dict to prevent the user from 77 adding options that do not exist or mispelling options. 78 """ 79 for option, value in kwargs.items(): 80 if option not in self._options: 81 raise KeyError("Invalid option: " + option) 82 else: 83 self._options[option] = value
84
85 - def get_option(self, option):
86 """Return the value of an option.""" 87 if option not in self._options: 88 raise KeyError("Invalid option: " + option) 89 else: 90 return self._options.get(option)
91
92 - def get_all_options(self):
93 """Return the values of all the options.""" 94 return list(self._options.items())
95
96 - def _set_rel_paths(self):
97 """Convert all file/directory locations to paths relative to the current working directory. 98 99 paml requires that all paths specified in the control file be 100 relative to the directory from which it is called rather than 101 absolute paths. 102 """ 103 if self.working_dir is not None: 104 self._rel_working_dir = _relpath(self.working_dir) 105 if self.alignment is not None: 106 self._rel_alignment = _relpath(self.alignment, 107 self.working_dir) 108 if self.out_file is not None: 109 self._rel_out_file = _relpath(self.out_file, self.working_dir)
110
111 - def run(self, ctl_file, verbose, command):
112 """Run a paml program using the current configuration and then parse the results. 113 114 Return a process signal so the user can determine if 115 the execution was successful (return code 0 is successful, -N 116 indicates a failure). The arguments may be passed as either 117 absolute or relative paths, despite the fact that paml 118 requires relative paths. 119 """ 120 if self.alignment is None: 121 raise ValueError("Alignment file not specified.") 122 if not os.path.exists(self.alignment): 123 raise IOError("The specified alignment file does not exist.") 124 if self.out_file is None: 125 raise ValueError("Output file not specified.") 126 if self.working_dir is None: 127 raise ValueError("Working directory not specified.") 128 # Get the current working directory 129 cwd = os.getcwd() 130 # Move to the desired working directory 131 if not os.path.exists(self.working_dir): 132 os.mkdir(self.working_dir) 133 os.chdir(self.working_dir) 134 # If no external control file was specified... 135 if ctl_file is None: 136 # Dynamically build a control file 137 self.write_ctl_file() 138 if verbose: 139 result_code = subprocess.call([command, self.ctl_file]) 140 else: 141 # To suppress output, redirect it to a pipe to nowhere 142 result_code = subprocess.call([command, self.ctl_file], 143 stdout=subprocess.PIPE) 144 else: 145 if not os.path.exists(ctl_file): 146 raise IOError("The specified control file does not exist.") 147 if verbose: 148 result_code = subprocess.call([command, ctl_file]) 149 else: 150 result_code = subprocess.call([command, ctl_file], 151 stdout=subprocess.PIPE) 152 os.chdir(cwd) 153 if result_code > 0: 154 # If the program fails for any reason 155 raise PamlError( 156 "%s has failed (return code %i). Run with verbose = True to view error message" 157 % (command, result_code)) 158 if result_code < 0: 159 # If the paml process is killed by a signal somehow 160 raise EnvironmentError("The %s process was killed (return code %i)." 161 % (command, result_code))
162