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

Source Code for Module Bio.Phylo.PAML.yn00

  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  import os.path 
  7  from ._paml import Paml 
  8  from . import _parse_yn00 
  9   
 10   
 11  # TODO - Restore use of with statement for closing handles automatically 
 12  # after dropping Python 2.4 
 13   
 14   
15 -class Yn00Error(EnvironmentError):
16 """yn00 failed. Run with verbose=True to view yn00's error message."""
17 18
19 -class Yn00(Paml):
20 """An interface to yn00, part of the PAML package.""" 21
22 - def __init__(self, alignment=None, working_dir=None, out_file=None):
23 """Initialize the Yn00 instance. 24 25 The user may optionally pass in strings specifying the locations 26 of the input alignment, the working directory and 27 the final output file. 28 """ 29 Paml.__init__(self, alignment, working_dir, out_file) 30 self.ctl_file = "yn00.ctl" 31 self._options = {"verbose": None, 32 "icode": None, 33 "weighting": None, 34 "commonf3x4": None, 35 "ndata": None}
36
37 - def write_ctl_file(self):
38 """Dynamically build a yn00 control file from the options. 39 40 The control file is written to the location specified by the 41 ctl_file property of the yn00 class. 42 """ 43 # Make sure all paths are relative to the working directory 44 self._set_rel_paths() 45 with open(self.ctl_file, 'w') as ctl_handle: 46 ctl_handle.write("seqfile = %s\n" % self._rel_alignment) 47 ctl_handle.write("outfile = %s\n" % self._rel_out_file) 48 for option in self._options.items(): 49 if option[1] is None: 50 # If an option has a value of None, there's no need 51 # to write it in the control file; it's normally just 52 # commented out. 53 continue 54 ctl_handle.write("%s = %s\n" % (option[0], option[1]))
55
56 - def read_ctl_file(self, ctl_file):
57 """Parse a control file and load the options into the yn00 instance.""" 58 temp_options = {} 59 if not os.path.isfile(ctl_file): 60 raise IOError("File not found: %r" % ctl_file) 61 else: 62 with open(ctl_file) as ctl_handle: 63 for line in ctl_handle: 64 line = line.strip() 65 uncommented = line.split("*", 1)[0] 66 if uncommented != "": 67 if "=" not in uncommented: 68 raise AttributeError( 69 "Malformed line in control file:\n%r" % line) 70 (option, value) = uncommented.split("=") 71 option = option.strip() 72 value = value.strip() 73 if option == "seqfile": 74 self.alignment = value 75 elif option == "outfile": 76 self.out_file = value 77 elif option not in self._options: 78 raise KeyError("Invalid option: %s" % option) 79 else: 80 if "." in value or "e-" in value: 81 try: 82 converted_value = float(value) 83 except ValueError: 84 converted_value = value 85 else: 86 try: 87 converted_value = int(value) 88 except ValueError: 89 converted_value = value 90 temp_options[option] = converted_value 91 for option in self._options: 92 if option in temp_options: 93 self._options[option] = temp_options[option] 94 else: 95 self._options[option] = None
96
97 - def run(self, ctl_file=None, verbose=False, command="yn00", parse=True):
98 Paml.run(self, ctl_file, verbose, command) 99 if parse: 100 results = read(self.out_file) 101 else: 102 results = None 103 return results
104 105
106 -def read(results_file):
107 """Parse a yn00 results file.""" 108 results = {} 109 if not os.path.exists(results_file): 110 raise IOError("Results file does not exist.") 111 with open(results_file) as handle: 112 lines = handle.readlines() 113 for line_num in range(len(lines)): 114 line = lines[line_num] 115 if "(A) Nei-Gojobori (1986) method" in line: 116 ng86_start = line_num + 1 117 elif "(B) Yang & Nielsen (2000) method" in line: 118 (results, sequences) = _parse_yn00.parse_ng86(lines[ng86_start:line_num], 119 results) 120 yn00_start = line_num + 1 121 elif "(C) LWL85, LPB93 & LWLm methods" in line: 122 results = _parse_yn00.parse_yn00(lines[yn00_start:line_num], results, 123 sequences) 124 results = _parse_yn00.parse_others(lines[line_num + 1:], results, 125 sequences) 126 if len(results) == 0: 127 raise ValueError("Invalid results file.") 128 return results
129