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  #TODO - Restore use of with statement for closing handles automatically 
 11  #after dropping Python 2.4 
 12   
 13   
14 -class Yn00Error(EnvironmentError):
15 """yn00 has failed. Run with verbose = True to view yn00's error 16 message"""
17 18
19 -class Yn00(Paml):
20 """This class implements an interface to yn00, part of the PAML package.""" 21
22 - def __init__(self, alignment = None, working_dir = None, 23 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", 100 parse = True):
101 Paml.run(self, ctl_file, verbose, command) 102 if parse: 103 results = read(self.out_file) 104 else: 105 results = None 106 return results
107 108
109 -def read(results_file):
110 """Parse a yn00 results file.""" 111 results = {} 112 if not os.path.exists(results_file): 113 raise IOError("Results file does not exist.") 114 with open(results_file) as handle: 115 lines = handle.readlines() 116 for line_num in range(len(lines)): 117 line = lines[line_num] 118 if "(A) Nei-Gojobori (1986) method" in line: 119 ng86_start = line_num + 1 120 elif "(B) Yang & Nielsen (2000) method" in line: 121 (results, sequences) = _parse_yn00.parse_ng86(lines[ng86_start:line_num], 122 results) 123 yn00_start = line_num + 1 124 elif "(C) LWL85, LPB93 & LWLm methods" in line: 125 results = _parse_yn00.parse_yn00(lines[yn00_start:line_num], results, 126 sequences) 127 results = _parse_yn00.parse_others(lines[line_num+1:], results, 128 sequences) 129 if len(results) == 0: 130 raise ValueError("Invalid results file.") 131 return results
132