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  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 if True: # Dummy statement to preserve indentation for diff 47 ctl_handle = open(self.ctl_file, 'w') 48 ctl_handle.write("seqfile = %s\n" % self._rel_alignment) 49 ctl_handle.write("outfile = %s\n" % self._rel_out_file) 50 for option in self._options.items(): 51 if option[1] is None: 52 # If an option has a value of None, there's no need 53 # to write it in the control file; it's normally just 54 # commented out. 55 continue 56 ctl_handle.write("%s = %s\n" % (option[0], option[1])) 57 ctl_handle.close()
58
59 - def read_ctl_file(self, ctl_file):
60 """Parse a control file and load the options into the yn00 instance. 61 """ 62 temp_options = {} 63 if not os.path.isfile(ctl_file): 64 raise IOError("File not found: %r" % ctl_file) 65 else: 66 ctl_handle = open(ctl_file) 67 for line in ctl_handle: 68 line = line.strip() 69 uncommented = line.split("*",1)[0] 70 if uncommented != "": 71 if "=" not in uncommented: 72 ctl_handle.close() 73 raise AttributeError( 74 "Malformed line in control file:\n%r" % line) 75 (option, value) = uncommented.split("=") 76 option = option.strip() 77 value = value.strip() 78 if option == "seqfile": 79 self.alignment = value 80 elif option == "outfile": 81 self.out_file = value 82 elif option not in self._options: 83 ctl_handle.close() 84 raise KeyError("Invalid option: %s" % option) 85 else: 86 if "." in value or "e-" in value: 87 try: 88 converted_value = float(value) 89 except: 90 converted_value = value 91 else: 92 try: 93 converted_value = int(value) 94 except: 95 converted_value = value 96 temp_options[option] = converted_value 97 ctl_handle.close() 98 for option in self._options.keys(): 99 if option in temp_options.keys(): 100 self._options[option] = temp_options[option] 101 else: 102 self._options[option] = None
103
104 - def run(self, ctl_file = None, verbose = False, command = "yn00", 105 parse = True):
106 Paml.run(self, ctl_file, verbose, command) 107 if parse: 108 results = read(self.out_file) 109 else: 110 results = None 111 return results
112 113
114 -def read(results_file):
115 """Parse a yn00 results file.""" 116 results = {} 117 if not os.path.exists(results_file): 118 raise IOError("Results file does not exist.") 119 handle = open(results_file) 120 lines = handle.readlines() 121 handle.close() 122 for line_num in range(len(lines)): 123 line = lines[line_num] 124 if "(A) Nei-Gojobori (1986) method" in line: 125 ng86_start = line_num + 1 126 elif "(B) Yang & Nielsen (2000) method" in line: 127 (results, sequences) = _parse_yn00.parse_ng86(lines[ng86_start:line_num], 128 results) 129 yn00_start = line_num + 1 130 elif "(C) LWL85, LPB93 & LWLm methods" in line: 131 results = _parse_yn00.parse_yn00(lines[yn00_start:line_num], results, 132 sequences) 133 results = _parse_yn00.parse_others(lines[line_num+1:], results, 134 sequences) 135 if len(results) == 0: 136 raise ValueError("Invalid results file.") 137 return results
138