Back to index

python-biopython  1.60
test_Mafft_tool.py
Go to the documentation of this file.
00001 # This code is part of the Biopython distribution and governed by its
00002 # license.  Please see the LICENSE file that should have been included
00003 # as part of this package.
00004 """
00005 Unittests for Bio.Align.Applications interface for MAFFT
00006 
00007 This code is part of the Biopython distribution and governed by its
00008 license.  Please see the LICENSE file that should have been included
00009 as part of this package.
00010 """
00011 
00012 import sys
00013 import os
00014 import unittest
00015 import subprocess
00016 from Bio import MissingExternalDependencyError
00017 from Bio.Align.Applications import MafftCommandline
00018 
00019 #Try to avoid problems when the OS is in another language
00020 os.environ['LANG'] = 'C'
00021 
00022 mafft_exe = None
00023 if sys.platform=="win32":
00024     raise MissingExternalDependencyError("Testing with MAFFT not implemented on Windows yet")
00025 else:
00026     import commands
00027     output = commands.getoutput("mafft -help")
00028     if "not found" not in output and "MAFFT" in output:
00029         mafft_exe = "mafft"
00030 if not mafft_exe:
00031     raise MissingExternalDependencyError(\
00032         "Install MAFFT if you want to use the Bio.Align.Applications wrapper.")
00033 
00034 def check_mafft_version(mafft_exe):
00035     child = subprocess.Popen("%s --help" % mafft_exe,
00036                              stdout=subprocess.PIPE,
00037                              stderr=subprocess.PIPE,
00038                              universal_newlines=True,
00039                              shell=(sys.platform!="win32"))
00040     stdoutdata, stderrdata = child.communicate()
00041     output = stdoutdata + "\n" + stderrdata
00042     return_code = child.returncode
00043     del child
00044     if "correctly installed?" in output \
00045     or "mafft binaries have to be installed" in output:
00046         raise MissingExternalDependencyError(
00047             "MAFFT does not seem to be correctly installed.")
00048 
00049     #e.g. "MAFFT version 5.732 (2005/09/14)\n"
00050     #e.g. "  MAFFT v6.717b (2009/12/03)\n"
00051     for marker in ["MAFFT version", "MAFFT v"]:
00052         index = output.find(marker)
00053         if index == -1:
00054             continue
00055         version = output[index+len(marker):].strip().split(None,1)[0]
00056         if int(version.split(".",1)[0]) < 6:
00057             raise MissingExternalDependencyError("Test requires MAFFT v6 or "
00058                                                  "later (found %s)." % version)
00059         return True
00060     raise MissingExternalDependencyError("Couldn't determine MAFFT version.")
00061 
00062 #This also checks it actually runs!
00063 check_mafft_version(mafft_exe)
00064 
00065 class MafftApplication(unittest.TestCase):
00066 
00067     def setUp(self):
00068         self.infile1  = "Fasta/f002"
00069 
00070     def tearDown(self):
00071         if os.path.isfile("Fasta/f002.tree"):
00072             os.remove("Fasta/f002.tree")
00073 
00074     def test_Mafft_simple(self):
00075         """Simple round-trip through app with infile.
00076         Result passed to stdout.
00077         """
00078         #Use a keyword argument at init,
00079         cmdline = MafftCommandline(mafft_exe, input=self.infile1)
00080         self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
00081         stdoutdata, stderrdata = cmdline()
00082         self.assertTrue(stdoutdata.startswith(">gi|1348912|gb|G26680|G26680"))
00083         self.assertTrue("Progressive alignment ..." in stderrdata, stderrdata)
00084         self.assertTrue("$#=0" not in stderrdata)
00085 
00086     def test_Mafft_with_options(self):
00087         """Simple round-trip through app with infile and options.
00088         Result passed to stdout.
00089         """
00090         cmdline = MafftCommandline(mafft_exe)
00091         cmdline.set_parameter("input", self.infile1)
00092         cmdline.set_parameter("maxiterate", 100)
00093         cmdline.set_parameter("--localpair", True)
00094         self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
00095         stdoutdata, stderrdata = cmdline()
00096         self.assertTrue(stdoutdata.startswith(">gi|1348912|gb|G26680|G26680"))
00097         self.assertTrue("$#=0" not in stderrdata)
00098 
00099     def test_Mafft_with_Clustalw_output(self):
00100         """Simple round-trip through app with clustal output"""
00101         cmdline = MafftCommandline(mafft_exe)
00102         #Use some properties:
00103         cmdline.input = self.infile1
00104         cmdline.clustalout = True
00105         self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
00106         stdoutdata, stderrdata = cmdline()
00107         #e.g. "CLUSTAL format alignment by MAFFT ..."
00108         #or "CLUSTAL (-like) formatted alignment by MAFFT FFT-NS-2 (v6.240)"
00109         self.assertTrue(stdoutdata.startswith("CLUSTAL"), stdoutdata)
00110         self.assertTrue("$#=0" not in stderrdata)
00111 
00112     def test_Mafft_with_complex_command_line(self):
00113         """Round-trip with complex command line."""
00114         cmdline = MafftCommandline(mafft_exe)
00115         cmdline.set_parameter("input", self.infile1)
00116         cmdline.set_parameter("--localpair", True)
00117         cmdline.set_parameter("--weighti", 4.2)
00118         cmdline.set_parameter("retree", 5)
00119         cmdline.set_parameter("maxiterate", 200)
00120         cmdline.set_parameter("--nofft", True)
00121         cmdline.set_parameter("op", 2.04)
00122         cmdline.set_parameter("--ep", 0.51)
00123         cmdline.set_parameter("--lop", 0.233)
00124         cmdline.set_parameter("lep", 0.2)
00125         cmdline.set_parameter("--reorder", True)
00126         cmdline.set_parameter("--treeout", True)
00127         cmdline.set_parameter("nuc", True)
00128         self.assertEqual(str(eval(repr(cmdline))), str(cmdline))
00129         self.assertEqual(str(cmdline), mafft_exe \
00130                          + " --localpair --weighti 4.2 --retree 5 " \
00131                          + "--maxiterate 200 --nofft --op 2.04 --ep 0.51" \
00132                          + " --lop 0.233 --lep 0.2 --reorder --treeout" \
00133                          + " --nuc Fasta/f002")
00134         stdoutdata, stderrdata = cmdline()
00135         self.assertTrue(stdoutdata.startswith(">gi|1348912|gb|G26680|G26680"))
00136         self.assertTrue("$#=0" not in stderrdata)
00137 
00138 
00139 if __name__ == "__main__":
00140     runner = unittest.TextTestRunner(verbosity = 2)
00141     unittest.main(testRunner=runner)