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