forked from biopython/biopython
/
test_PAML_baseml.py
210 lines (182 loc) · 7.95 KB
/
test_PAML_baseml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Copyright (C) 2011 by Brandon Invergo (b.invergo@gmail.com)
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
import unittest
import os
import os.path
import sys
from Bio.Phylo.PAML import baseml
from Bio.Phylo.PAML._paml import PamlError
class ModTest(unittest.TestCase):
align_file = os.path.join("PAML", "alignment.phylip")
tree_file = os.path.join("PAML", "species.tree")
bad_tree_file = os.path.join("PAML", "bad.tree")
out_file = os.path.join("PAML", "test.out")
working_dir = os.path.join("PAML", "baseml_test")
results_file = os.path.join("PAML", "bad_results.out")
bad_ctl_file1 = os.path.join("PAML", "bad1.ctl")
bad_ctl_file2 = os.path.join("PAML", "bad2.ctl")
ctl_file = os.path.join("PAML", "baseml.ctl")
def __del__(self):
"""Just in case BASEML creates some junk files, do a clean-up."""
del_files = [self.out_file, "2base.t",
"in.basemlg", "baseml.ctl", "lnf", "rates", "rst", "rst1",
"rub"]
for filename in del_files:
if os.path.exists(filename):
os.remove(filename)
if os.path.exists(self.working_dir):
for filename in os.listdir(self.working_dir):
os.remove(filename)
os.rmdir(self.working_dir)
def setUp(self):
self.bml = baseml.Baseml()
def testAlignmentFileIsValid(self):
self.assertRaises((AttributeError, TypeError),
baseml.Baseml, alignment = 1)
self.bml.alignment = 1
self.bml.tree = self.tree_file
self.bml.out_file = self.out_file
self.assertRaises((AttributeError, TypeError),
self.bml.run)
def testAlignmentExists(self):
self.assertRaises((EnvironmentError, IOError), baseml.Baseml,
alignment = "nonexistent")
self.bml.alignment = "nonexistent"
self.bml.tree = self.tree_file
self.bml.out_file = self.out_file
self.assertRaises((EnvironmentError, IOError),
self.bml.run)
def testTreeFileValid(self):
self.assertRaises((AttributeError, TypeError),
baseml.Baseml, tree = 1)
self.bml.alignment = self.align_file
self.bml.tree = 1
self.bml.out_file = self.out_file
self.assertRaises((AttributeError, TypeError),
self.bml.run)
def testTreeExists(self):
self.assertRaises((EnvironmentError, IOError), baseml.Baseml,
tree = "nonexistent")
self.bml.alignment = self.align_file
self.bml.tree = "nonexistent"
self.bml.out_file = self.out_file
self.assertRaises((EnvironmentError, IOError),
self.bml.run)
def testWorkingDirValid(self):
self.bml.tree = self.tree_file
self.bml.alignment = self.align_file
self.bml.out_file = self.out_file
self.bml.working_dir = 1
self.assertRaises((AttributeError, TypeError),
self.bml.run)
def testOutputFileValid(self):
self.bml.tree = self.tree_file
self.bml.alignment = self.align_file
self.bml.out_file = 1
self.assertRaises((AttributeError, TypeError),
self.bml.run)
def testOptionExists(self):
self.assertRaises((AttributeError, KeyError),
self.bml.set_options, xxxx=1)
self.assertRaises((AttributeError, KeyError),
self.bml.get_option, "xxxx")
def testAlignmentSpecified(self):
self.bml.tree = self.tree_file
self.bml.out_file = self.out_file
self.assertRaises((AttributeError, ValueError),
self.bml.run)
def testTreeSpecified(self):
self.bml.alignment = self.align_file
self.bml.out_file = self.out_file
self.assertRaises((AttributeError, ValueError),
self.bml.run)
def testOutputFileSpecified(self):
self.bml.alignment = self.align_file
self.bml.tree = self.tree_file
self.assertRaises((AttributeError, ValueError),
self.bml.run)
def testPamlErrorsCaught(self):
self.bml.alignment = self.align_file
self.bml.tree = self.bad_tree_file
self.bml.out_file = self.out_file
self.assertRaises((EnvironmentError, PamlError),
self.bml.run)
def testCtlFileValidOnRun(self):
self.bml.alignment = self.align_file
self.bml.tree = self.tree_file
self.bml.out_file = self.out_file
self.assertRaises((AttributeError, TypeError),
self.bml.run, ctl_file = 1)
def testCtlFileExistsOnRun(self):
self.bml.alignment = self.align_file
self.bml.tree = self.tree_file
self.bml.out_file = self.out_file
self.assertRaises((EnvironmentError, IOError),
self.bml.run, ctl_file = "nonexistent")
def testCtlFileValidOnRead(self):
self.assertRaises((AttributeError, TypeError),
self.bml.read_ctl_file, 1)
self.assertRaises((AttributeError, KeyError),
self.bml.read_ctl_file, self.bad_ctl_file1)
self.assertRaises(AttributeError,
self.bml.read_ctl_file, self.bad_ctl_file2)
target_options = {"noisy": 0,
"verbose": 0,
"runmode": 0,
"model": 6,
"model_options": None,
"Mgene": 1,
"ndata": None,
"clock": 0,
"fix_kappa": 0,
"kappa": 5,
"fix_alpha": 0,
"alpha": 0.5,
"Malpha": 0,
"ncatG": 5,
"fix_rho": 1,
"rho": 0,
"nparK": 0,
"nhomo": 0,
"getSE": 0,
"RateAncestor": 0,
"Small_Diff": 7e-6,
"cleandata": 1,
"icode": None,
"fix_blength": None,
"method": 0}
self.bml.read_ctl_file(self.ctl_file)
self.assertEqual(sorted(self.bml._options.keys()), sorted(target_options.keys()))
for key in target_options:
self.assertEqual(self.bml._options[key], target_options[key], \
"%s: %r vs %r" \
% (key, self.bml._options[key], target_options[key]))
def testCtlFileExistsOnRead(self):
self.assertRaises(IOError,
self.bml.read_ctl_file, ctl_file = "nonexistent")
def testResultsValid(self):
self.assertRaises((AttributeError, TypeError),
baseml.read, 1)
def testResultsExist(self):
self.assertRaises(IOError, baseml.read, "nonexistent")
def testResultsParsable(self):
self.assertRaises(ValueError, baseml.read, self.results_file)
def testParseAllVersions(self):
folder = os.path.join("PAML","Results", "baseml", "versions")
for results_file in os.listdir(folder):
file_path = os.path.join(folder, results_file)
if os.path.isfile(file_path) and results_file[:6] == "baseml":
results = baseml.read(file_path)
self.assertEqual(len(results), 6)
self.assertEqual(len(results["parameters"]), 7)
def testParseSEs(self):
SE_results_file = os.path.join("PAML", "Results", "baseml",
"baseml_SE.out")
SE_results = baseml.read(SE_results_file)
SE_parameters = SE_results.get("parameters")
self.assertNotEqual(SE_parameters.get("SEs"), None)
if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity = 2)
unittest.main(testRunner=runner)