File: test_TCoffee_tool.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 (176 lines) | stat: -rw-r--r-- 6,990 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
# Copyright 2009 by Cymon J. Cox.  All rights reserved.
# 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.
"""Unittests for Bio.Align.Applications interface for TCOFFEE."""

import sys
import os
import unittest
from Bio import AlignIO, SeqIO, MissingExternalDependencyError
from Bio.Align.Applications import TCoffeeCommandline

# Try to avoid problems when the OS is in another language
os.environ["LANG"] = "C"

t_coffee_exe = None
if sys.platform == "win32":
    raise MissingExternalDependencyError("Testing TCOFFEE on Windows not supported yet")
else:
    from subprocess import getoutput

    output = getoutput("t_coffee -version")
    if "not found" not in output and "not recognized" not in output:
        if "t_coffee" in output.lower() or "t-coffee" in output.lower():
            t_coffee_exe = "t_coffee"

if not t_coffee_exe:
    raise MissingExternalDependencyError(
        "Install TCOFFEE if you want to use the Bio.Align.Applications wrapper."
    )


class TCoffeeApplication(unittest.TestCase):
    def setUp(self):
        self.infile1 = "Fasta/fa01"
        # TODO: Use a temp dir for the output files:
        self.outfile1 = "fa01.aln"
        self.outfile2 = "fa01.html"  # Written by default when no output set
        self.outfile3 = "Fasta/tc_out.pir"
        self.outfile4 = "Fasta/tc_out.aln"
        self.outfile5 = "Fasta/tc_out.phy"
        self.outfile6 = "Fasta/tc_out.msf"

    def tearDown(self):
        if os.path.isfile(self.outfile1):
            os.remove(self.outfile1)
        if os.path.isfile(self.outfile2):
            os.remove(self.outfile2)
        if os.path.isfile(self.outfile3):
            os.remove(self.outfile3)
        if os.path.isfile(self.outfile4):
            os.remove(self.outfile4)
        if os.path.isfile(self.outfile5):
            os.remove(self.outfile5)

    def test_TCoffee_fasta(self):
        """Round-trip through app and read clustal alignment from file."""
        cmdline = TCoffeeCommandline(t_coffee_exe, infile=self.infile1)
        self.assertEqual(str(cmdline), t_coffee_exe + " -infile Fasta/fa01")
        stdout, stderr = cmdline()
        self.assertTrue(stderr.strip().startswith("PROGRAM: T-COFFEE"))
        align = AlignIO.read(self.outfile1, "clustal")
        records = list(SeqIO.parse(self.infile1, "fasta"))
        self.assertEqual(len(records), len(align))
        for old, new in zip(records, align):
            self.assertEqual(old.id, new.id)
            self.assertEqual(
                str(new.seq).replace("-", ""), str(old.seq).replace("-", "")
            )

    def test_TCoffee_pir(self):
        """Round-trip through app and read pir alignment from file."""
        cmdline = TCoffeeCommandline(t_coffee_exe, quiet=True)
        cmdline.infile = self.infile1
        cmdline.outfile = self.outfile3
        cmdline.output = "pir_aln"
        self.assertEqual(
            str(cmdline),
            t_coffee_exe
            + " -output pir_aln -infile Fasta/fa01 -outfile Fasta/tc_out.pir -quiet",
        )
        stdout, stderr = cmdline()
        # Can get warnings in stderr output
        self.assertNotIn("error", stderr.lower(), stderr)
        align = AlignIO.read(self.outfile3, "pir")
        records = list(SeqIO.parse(self.infile1, "fasta"))
        self.assertEqual(len(records), len(align))
        for old, new in zip(records, align):
            self.assertEqual(old.id, new.id)
            self.assertEqual(
                str(new.seq).replace("-", ""), str(old.seq).replace("-", "")
            )

    def test_TCoffee_clustalw(self):
        """Round-trip through app and read clustalw alignment from file."""
        cmdline = TCoffeeCommandline(t_coffee_exe, gapopen=-2)
        cmdline.infile = self.infile1
        cmdline.outfile = self.outfile4
        cmdline.set_parameter("output", "clustalw_aln")
        cmdline.outorder = "input"
        cmdline.set_parameter("gapext", -5)
        cmdline.type = "protein"
        self.assertEqual(
            str(cmdline),
            t_coffee_exe
            + " -output clustalw_aln -infile Fasta/fa01 -outfile Fasta/tc_out.aln "
            "-type protein -outorder input -gapopen -2 -gapext -5",
        )
        stdout, stderr = cmdline()
        self.assertTrue(stderr.strip().startswith("PROGRAM: T-COFFEE"))
        align = AlignIO.read(self.outfile4, "clustal")
        records = list(SeqIO.parse(self.infile1, "fasta"))
        self.assertEqual(len(records), len(align))
        for old, new in zip(records, align):
            self.assertEqual(old.id, new.id)
            self.assertEqual(
                str(new.seq).replace("-", ""), str(old.seq).replace("-", "")
            )

    def test_TCoffee_phylip(self):
        """Round-trip through app and read PHYLIP alignment from file."""
        cmdline = TCoffeeCommandline(
            t_coffee_exe,
            infile=self.infile1,
            outfile=self.outfile5,
            quiet=True,
            output="phylip_aln",
        )
        self.assertEqual(
            str(cmdline),
            t_coffee_exe + " -output phylip_aln "
            "-infile Fasta/fa01 -outfile Fasta/tc_out.phy -quiet",
        )
        stdout, stderr = cmdline()
        # Can get warnings in stderr output
        self.assertNotIn("error", stderr.lower(), stderr)
        align = AlignIO.read(self.outfile5, "phylip")
        records = list(SeqIO.parse(self.infile1, "fasta"))
        self.assertEqual(len(records), len(align))
        for old, new in zip(records, align):
            # TCoffee does strict 10 character truncation as per original PHYLIP
            self.assertEqual(old.id[:10], new.id[:10])
            self.assertEqual(
                str(new.seq).replace("-", ""), str(old.seq).replace("-", "")
            )

    def test_TCoffee_msf(self):
        """Round-trip through app and read GCG MSF alignment from file."""
        cmdline = TCoffeeCommandline(
            t_coffee_exe,
            infile=self.infile1,
            outfile=self.outfile6,
            quiet=True,
            output="msf_aln",
        )
        self.assertEqual(
            str(cmdline),
            t_coffee_exe
            + " -output msf_aln -infile Fasta/fa01 -outfile Fasta/tc_out.msf -quiet",
        )
        stdout, stderr = cmdline()
        # Can get warnings in stderr output
        self.assertNotIn("error", stderr.lower(), stderr)
        align = AlignIO.read(self.outfile6, "msf")
        records = list(SeqIO.parse(self.infile1, "fasta"))
        self.assertEqual(len(records), len(align))
        for old, new in zip(records, align):
            self.assertEqual(old.id, new.id)
            self.assertEqual(
                str(new.seq).replace("-", ""), str(old.seq).replace("-", "")
            )


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