Back to index

python-biopython  1.60
test_Dialign_tool.py
Go to the documentation of this file.
00001 # Copyright 2009 by Cymon J. Cox.  All rights reserved.
00002 # Revisions 2009 copyright by Peter Cock.  All rights reserved.
00003 # This code is part of the Biopython distribution and governed by its
00004 # license.  Please see the LICENSE file that should have been included
00005 # as part of this package.
00006 """Unittests for Bio.Align.Applications interface for DIALIGN2-2."""
00007 
00008 import sys
00009 import os
00010 import unittest
00011 import subprocess
00012 from Bio import MissingExternalDependencyError
00013 from Bio.Align.Applications import DialignCommandline
00014 
00015 #Try to avoid problems when the OS is in another language
00016 os.environ['LANG'] = 'C'
00017 
00018 dialign_exe = None
00019 if sys.platform=="win32":
00020     raise MissingExternalDependencyError("DIALIGN2-2 not available on Windows")
00021 else:
00022     import commands
00023     output = commands.getoutput("dialign2-2")
00024     if "not found" not in output and "dialign2-2" in output.lower():
00025         dialign_exe = "dialign2-2"
00026         if "DIALIGN2_DIR" not in os.environ:
00027             raise MissingExternalDependencyError(\
00028                 "Environment variable DIALIGN2_DIR for DIALIGN2-2 missing.")
00029         if not os.path.isdir(os.environ["DIALIGN2_DIR"]):
00030             raise MissingExternalDependencyError(\
00031                 "Environment variable DIALIGN2_DIR for DIALIGN2-2 is not a valid directory.")
00032         if not os.path.isfile(os.path.join(os.environ["DIALIGN2_DIR"], "BLOSUM")):
00033             raise MissingExternalDependencyError(\
00034                 "Environment variable DIALIGN2_DIR directory missing BLOSUM file.")
00035         #TODO - check for tp400_dna, tp400_prot and tp400_trans too?
00036         
00037 if not dialign_exe:
00038     raise MissingExternalDependencyError(\
00039         "Install DIALIGN2-2 if you want to use the Bio.Align.Applications wrapper.")
00040 
00041 class DialignApplication(unittest.TestCase):
00042 
00043     def setUp(self):
00044         self.infile1 = "Fasta/f002" 
00045         #Standard output file
00046         self.outfile1 = "Fasta/f002.ali"
00047         #MSF output
00048         self.outfile2 = "Fasta/f002.ms"
00049 
00050     def tearDown(self):
00051         if os.path.isfile(self.outfile1):
00052             os.remove(self.outfile1)
00053         if os.path.isfile(self.outfile2):
00054             os.remove(self.outfile2)
00055 
00056     def test_Dialign_simple(self):
00057         """Simple round-trip through app with infile.
00058         """
00059         #Test using keyword arguments:
00060         cmdline = DialignCommandline(dialign_exe, input=self.infile1)
00061         self.assertEqual(str(cmdline), dialign_exe + " Fasta/f002")
00062         stdout, stderr = cmdline()
00063         self.assertEqual(stderr, "")
00064         self.assertEqual(stdout, "")
00065         self.assertTrue(os.path.exists(self.outfile1))
00066 
00067     def test_Dialign_simple_with_options(self):
00068         """Simple round-trip through app with infile and options
00069         """
00070         cmdline = DialignCommandline(dialign_exe)
00071         cmdline.set_parameter("input", self.infile1)
00072         cmdline.set_parameter("-max_link", True)
00073         cmdline.set_parameter("stars", 4)
00074         self.assertEqual(str(cmdline), dialign_exe + \
00075                          " -max_link -stars 4 Fasta/f002")
00076         stdout, stderr = cmdline()
00077         self.assertEqual(stderr, "")
00078         self.assertEqual(stdout, "")
00079         self.assertTrue(os.path.exists(self.outfile1))
00080 
00081     def test_Dialign_simple_with_MSF_output(self):
00082         """Simple round-trip through app with infile, output MSF
00083         """
00084         cmdline = DialignCommandline(dialign_exe)
00085         #Test with properties
00086         cmdline.input = self.infile1
00087         cmdline.msf = True
00088         self.assertEqual(str(cmdline), dialign_exe + " -msf Fasta/f002")
00089         stdout, stderr = cmdline()
00090         self.assertEqual(stderr, "")
00091         self.assertEqual(stdout, "")
00092         self.assertTrue(os.path.exists(self.outfile1))
00093         self.assertTrue(os.path.exists(self.outfile2))
00094 
00095     def test_Dialign_complex_command_line(self):
00096         """Round-trip through app with complex command line."""
00097         cmdline = DialignCommandline(dialign_exe)
00098         cmdline.set_parameter("input", self.infile1)
00099         cmdline.set_parameter("-nt", True)
00100         cmdline.set_parameter("-thr", 4)
00101         cmdline.set_parameter("stars", 9)
00102         cmdline.set_parameter("-ow", True)
00103         cmdline.set_parameter("mask", True)
00104         cmdline.set_parameter("-cs", True)
00105         self.assertEqual(str(cmdline), dialign_exe + \
00106                          " -cs -mask -nt -ow -stars 9 -thr 4 Fasta/f002")
00107         stdout, stderr = cmdline()
00108         self.assertEqual(stderr, "")
00109         self.assertTrue(os.path.exists(self.outfile1))
00110         self.assertTrue(stdout.startswith(" e_len = 633"))
00111 
00112 
00113 if __name__ == "__main__":
00114     runner = unittest.TextTestRunner(verbosity = 2)
00115     unittest.main(testRunner=runner)