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