File: test_PAML_yn00.py

package info (click to toggle)
python-biopython 1.68%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 46,860 kB
  • ctags: 13,237
  • sloc: python: 160,306; xml: 93,216; ansic: 9,118; sql: 1,208; makefile: 155; sh: 63
file content (148 lines) | stat: -rw-r--r-- 5,608 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
# 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
from Bio.Phylo.PAML import yn00
from Bio.Phylo.PAML._paml import PamlError


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")
    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=list())
        self.yn00.alignment = list()
        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 = list()
        self.assertRaises((AttributeError, TypeError, OSError),
            self.yn00.run)

    def testOutputFileValid(self):
        self.yn00.alignment = self.align_file
        self.yn00.out_file = list()
        self.assertRaises((AttributeError, ValueError, 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=list())

    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, list())
        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, list())

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

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

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

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