1
2
3
4
5
6 import os.path
7 from _paml import Paml, PamlError
8 import _parse_yn00
9
10
11
12
14 """yn00 has failed. Run with verbose = True to view yn00's error
15 message"""
16
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
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
43 self._set_rel_paths()
44 if True:
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
51
52
53 continue
54 ctl_handle.write("%s = %s\n" % (option[0], option[1]))
55 ctl_handle.close()
56
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):
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