Back to index

python-biopython  1.60
test_Probcons_tool.py
Go to the documentation of this file.
00001 # Copyright 2009 by Cymon J. Cox.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 """Unittests for Bio.Align.Applications interface for PROBCONS."""
00006 
00007 import sys
00008 import os
00009 import unittest
00010 import subprocess
00011 from cStringIO import StringIO
00012 from Bio import AlignIO, SeqIO, MissingExternalDependencyError
00013 from Bio.Align.Applications import ProbconsCommandline
00014 
00015 #Try to avoid problems when the OS is in another language
00016 os.environ['LANG'] = 'C'
00017 
00018 probcons_exe = None
00019 if sys.platform=="win32":
00020     raise MissingExternalDependencyError("PROBCONS not available on Windows")
00021 else:
00022     import commands
00023     output = commands.getoutput("probcons")
00024     if "not found" not in output and "probcons" in output.lower():
00025         probcons_exe = "probcons"
00026 
00027 if not probcons_exe:
00028     raise MissingExternalDependencyError(\
00029         "Install PROBCONS if you want to use the Bio.Align.Applications wrapper.")
00030 
00031 class ProbconsApplication(unittest.TestCase):
00032 
00033     def setUp(self):
00034         self.infile1 = "Fasta/fa01"
00035         self.annotation_outfile = "Fasta/probcons_annot.out"
00036 
00037     def tearDown(self):
00038         if os.path.isfile(self.annotation_outfile):
00039             os.remove(self.annotation_outfile)
00040 
00041     def test_Probcons_alignment_fasta(self):
00042         """Round-trip through app and read fasta alignment from stdout
00043         """
00044         cmdline = ProbconsCommandline(probcons_exe, input=self.infile1)
00045         self.assertEqual(str(cmdline), probcons_exe + " Fasta/fa01")
00046         self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
00047         stdout, stderr = cmdline()
00048         self.assertTrue(stderr.startswith("\nPROBCONS"))
00049         align = AlignIO.read(StringIO(stdout), "fasta")
00050         records = list(SeqIO.parse(self.infile1,"fasta"))
00051         self.assertEqual(len(records),len(align))
00052         for old, new in zip(records, align):
00053             self.assertEqual(old.id, new.id)
00054             self.assertEqual(str(new.seq).replace("-",""), str(old.seq).replace("-",""))
00055  
00056     def test_Probcons_alignment_clustalw(self):
00057         """Round-trip through app and read clustalw alignment from stdout
00058         """
00059         cmdline = ProbconsCommandline(probcons_exe)
00060         cmdline.set_parameter("input", "Fasta/fa01")
00061         cmdline.clustalw = True
00062         self.assertEqual(str(cmdline), probcons_exe + " -clustalw Fasta/fa01")
00063         self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
00064         stdout, stderr = cmdline()
00065         self.assertTrue(stderr.strip().startswith("PROBCONS"))
00066         align = AlignIO.read(StringIO(stdout), "clustal")
00067         records = list(SeqIO.parse(self.infile1,"fasta"))
00068         self.assertEqual(len(records),len(align))
00069         for old, new in zip(records, align):
00070             self.assertEqual(old.id, new.id)
00071             self.assertEqual(str(new.seq).replace("-",""), str(old.seq).replace("-",""))
00072 
00073     def test_Probcons_complex_commandline(self):
00074         """Round-trip through app with complex command line and output file
00075         """
00076         cmdline = ProbconsCommandline(probcons_exe, pre=1)
00077         cmdline.set_parameter("input", "Fasta/fa01")
00078         cmdline.consistency = 4
00079         cmdline.set_parameter("--iterative-refinement", 222)
00080         cmdline.set_parameter("a", True)
00081         cmdline.annot = self.annotation_outfile
00082         self.assertEqual(str(cmdline), probcons_exe +
00083                 " -c 4 -ir 222 -pre 1 -annot Fasta/probcons_annot.out "
00084                 "-a Fasta/fa01")
00085         stdout, stderr = cmdline()
00086         self.assertTrue(stderr.startswith("\nPROBCONS"))
00087         self.assertTrue(stdout.startswith(">AK1H_ECOLI/1-378"))
00088 
00089 
00090 if __name__ == "__main__":
00091     runner = unittest.TextTestRunner(verbosity = 2)
00092     unittest.main(testRunner=runner)