File: test_PAML_yn00.py

package info (click to toggle)
python-biopython 1.78%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 65,756 kB
  • sloc: python: 221,141; xml: 178,777; ansic: 13,369; sql: 1,208; makefile: 131; sh: 70
file content (179 lines) | stat: -rw-r--r-- 7,208 bytes parent folder | download | duplicates (2)
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
# Copyright (C) 2011, 2019 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.

"""Tests for PAML yn00 module."""

import unittest
import os
import os.path
from Bio.Phylo.PAML import yn00
from Bio.Phylo.PAML._paml import PamlError
import glob


class ModTest(unittest.TestCase):

    align_dir = os.path.join("PAML", "Alignments")
    tree_dir = os.path.join("PAML", "Trees")
    ctl_dir = os.path.join("PAML", "Control_files")
    results_dir = os.path.join("PAML", "Results")
    working_dir = os.path.join("PAML", "yn00_test")

    align_file = os.path.join(align_dir, "alignment.phylip")
    dotname_align_file = os.path.join(align_dir, "alignment_dottednames.phylip")
    out_file = os.path.join(results_dir, "test.out")
    results_file = os.path.join(results_dir, "bad_results.out")
    bad_ctl_file1 = os.path.join(ctl_dir, "bad1.ctl")
    bad_ctl_file2 = os.path.join(ctl_dir, "bad2.ctl")
    ctl_file = os.path.join(ctl_dir, "yn00", "yn00.ctl")

    def tearDown(self):
        """Just in case yn00 creates some junk files, do a clean-up."""
        del_files = [self.out_file, "2YN.dN", "2YN.dS", "2YN.t", "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):
                filepath = os.path.join(self.working_dir, filename)
                os.remove(filepath)
            os.rmdir(self.working_dir)

    def setUp(self):
        self.yn00 = yn00.Yn00()

    def testAlignmentFileIsValid(self):
        self.assertRaises((AttributeError, TypeError, OSError), yn00.Yn00, alignment=[])
        self.yn00.alignment = []
        self.yn00.out_file = self.out_file
        self.assertRaises((AttributeError, TypeError, OSError), self.yn00.run)

    def testAlignmentExists(self):
        self.assertRaises(
            (EnvironmentError, IOError), yn00.Yn00, alignment="nonexistent"
        )
        self.yn00.alignment = "nonexistent"
        self.yn00.out_file = self.out_file
        self.assertRaises(IOError, self.yn00.run)

    def testWorkingDirValid(self):
        self.yn00.alignment = self.align_file
        self.yn00.out_file = self.out_file
        self.yn00.working_dir = []
        self.assertRaises((AttributeError, TypeError, OSError), self.yn00.run)

    def testOptionExists(self):
        self.assertRaises((AttributeError, KeyError), self.yn00.set_options, xxxx=1)
        self.assertRaises((AttributeError, KeyError), self.yn00.get_option, "xxxx")

    def testAlignmentSpecified(self):
        self.yn00.out_file = self.out_file
        self.assertRaises((AttributeError, ValueError), self.yn00.run)

    def testOutputFileSpecified(self):
        self.yn00.alignment = self.align_file
        self.assertRaises((AttributeError, ValueError), self.yn00.run)

    # def testPamlErrorsCaught(self):
    #     self.yn00.alignment = self.align_file
    #     self.yn00.out_file = self.out_file
    #     self.assertRaises((EnvironmentError, PamlError),
    #                        self.yn00.run)

    def testCtlFileValidOnRun(self):
        self.yn00.alignment = self.align_file
        self.yn00.out_file = self.out_file
        self.assertRaises(
            (AttributeError, TypeError, OSError), self.yn00.run, ctl_file=[]
        )

    def testCtlFileExistsOnRun(self):
        self.yn00.alignment = self.align_file
        self.yn00.out_file = self.out_file
        self.assertRaises(IOError, self.yn00.run, ctl_file="nonexistent")

    def testCtlFileValidOnRead(self):
        self.assertRaises(
            (AttributeError, TypeError, OSError), self.yn00.read_ctl_file, []
        )
        self.assertRaises(
            (AttributeError, KeyError), self.yn00.read_ctl_file, self.bad_ctl_file1
        )
        self.assertRaises(AttributeError, self.yn00.read_ctl_file, self.bad_ctl_file2)
        target_options = {
            "verbose": 1,
            "icode": 0,
            "weighting": 0,
            "commonf3x4": 0,
            "ndata": 1,
        }
        self.yn00.read_ctl_file(self.ctl_file)
        self.assertEqual(self.yn00._options, target_options)

    def testCtlFileExistsOnRead(self):
        self.assertRaises(IOError, self.yn00.read_ctl_file, ctl_file="nonexistent")

    def testResultsValid(self):
        self.assertRaises((AttributeError, TypeError, OSError), yn00.read, [])

    def testResultsExist(self):
        self.assertRaises((EnvironmentError, IOError), yn00.read, "nonexistent")

    def testResultsParsable(self):
        self.assertRaises(ValueError, yn00.read, self.results_file)

    def testParseAllVersions(self):
        pattern = os.path.join(self.results_dir, "yn00", "yn00-*")
        for results_file in glob.glob(pattern):
            results = yn00.read(results_file)
            self.assertEqual(len(results), 5)
            self.assertEqual(len(results["Homo_sapie"]), 4)
            self.assertEqual(len(results["Homo_sapie"]["Pan_troglo"]), 5)

    def testParseLongNames(self):
        pattern = os.path.join(self.results_dir, "yn00", "yn00_long-*")
        for results_file in glob.glob(pattern):
            results = yn00.read(results_file)
            # Expect seven taxa...
            self.assertEqual(len(results), 7)
            # ...each of which is compared to the other six.
            self.assertEqual({len(v) for v in results.values()}, {6})
            # ...each of which has five measures.
            self.assertEqual(
                {len(v) for taxa in results.values() for v in taxa.values()}, {5}
            )

    def testParseDottedNames(self):
        pattern = os.path.join(self.results_dir, "yn00", "yn00_dotted-*")
        for results_file in glob.glob(pattern):
            results = yn00.read(results_file)
            # Expect seven taxa...
            self.assertEqual(len(results), 5)
            # ...each of which is compared to the other six.
            self.assertEqual({len(v) for v in results.values()}, {4})
            # ...each of which has five measures.
            self.assertEqual(
                {len(v) for taxa in results.values() for v in taxa.values()}, {5}
            )
            self.assertEqual(len(results["Homo.sapie"]), 4)
            self.assertEqual(len(results["Homo.sapie"]["Pan.troglo"]), 5)

    def testParseDottedNumNames(self):
        pattern = os.path.join(self.results_dir, "yn00", "yn00_dottednum-*")
        for results_file in glob.glob(pattern):
            results = yn00.read(results_file)
            # Expect seven taxa...
            self.assertEqual(len(results), 7)
            # ...each of which is compared to the other six.
            self.assertEqual({len(v) for v in results.values()}, {6})
            # ...each of which has five measures.
            self.assertEqual(
                {len(v) for taxa in results.values() for v in taxa.values()}, {5}
            )


if __name__ == "__main__":
    runner = unittest.TextTestRunner(verbosity=2)
    unittest.main(testRunner=runner)