File: test_Probcons_tool.py

package info (click to toggle)
python-biopython 1.85%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 126,372 kB
  • sloc: xml: 1,047,995; python: 332,722; ansic: 16,944; sql: 1,208; makefile: 140; sh: 81
file content (105 lines) | stat: -rw-r--r-- 4,102 bytes parent folder | download
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
# 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 PROBCONS."""

import os
import sys
import unittest
import warnings
from io import StringIO

from Bio import AlignIO
from Bio import BiopythonDeprecationWarning
from Bio import MissingExternalDependencyError
from Bio import SeqIO

with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=BiopythonDeprecationWarning)
    from Bio.Align.Applications import ProbconsCommandline

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

probcons_exe = None
if sys.platform == "win32":
    raise MissingExternalDependencyError("PROBCONS not available on Windows")
else:
    from subprocess import getoutput

    output = getoutput("probcons")
    if "not found" not in output and "not recognized" not in output:
        if "probcons" in output.lower():
            probcons_exe = "probcons"

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


class ProbconsApplication(unittest.TestCase):
    def setUp(self):
        self.infile1 = "Fasta/fa01"
        self.annotation_outfile = "Fasta/probcons_annot.out"

    def tearDown(self):
        if os.path.isfile(self.annotation_outfile):
            os.remove(self.annotation_outfile)

    def test_Probcons_alignment_fasta(self):
        """Round-trip through app and read fasta alignment from stdout."""
        cmdline = ProbconsCommandline(probcons_exe, input=self.infile1)
        self.assertEqual(str(cmdline), probcons_exe + " Fasta/fa01")
        self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
        stdout, stderr = cmdline()
        self.assertTrue(stderr.startswith("\nPROBCONS"))
        align = AlignIO.read(StringIO(stdout), "fasta")
        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_Probcons_alignment_clustalw(self):
        """Round-trip through app and read clustalw alignment from stdout."""
        cmdline = ProbconsCommandline(probcons_exe)
        cmdline.set_parameter("input", "Fasta/fa01")
        cmdline.clustalw = True
        self.assertEqual(str(cmdline), probcons_exe + " -clustalw Fasta/fa01")
        self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
        stdout, stderr = cmdline()
        self.assertTrue(stderr.strip().startswith("PROBCONS"))
        align = AlignIO.read(StringIO(stdout), "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_Probcons_complex_commandline(self):
        """Round-trip through app with complex command line and output file."""
        cmdline = ProbconsCommandline(probcons_exe, pre=1)
        cmdline.set_parameter("input", "Fasta/fa01")
        cmdline.consistency = 4
        cmdline.set_parameter("--iterative-refinement", 222)
        cmdline.set_parameter("a", True)
        cmdline.annot = self.annotation_outfile
        self.assertEqual(
            str(cmdline),
            probcons_exe
            + " -c 4 -ir 222 -pre 1 -annot Fasta/probcons_annot.out -a Fasta/fa01",
        )
        stdout, stderr = cmdline()
        self.assertTrue(stderr.startswith("\nPROBCONS"))
        self.assertTrue(stdout.startswith(">AK1H_ECOLI/1-378"))


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