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
|
# Copyright (C) 2011 by Brandon Invergo (b.invergo@gmail.com)
# 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.
import unittest
import os
import sys
from Bio.Phylo.PAML import codeml, baseml, yn00
from Bio import MissingExternalDependencyError
def is_exe(filepath):
"""Test if a file is an executable."""
return os.path.exists(filepath) and os.access(filepath, os.X_OK)
def which(program):
"""Find the path to an executable."""
filepath, filename = os.path.split(program)
os_path = os.environ["PATH"].split(os.pathsep)
if sys.platform == "win32":
try:
# This can vary depending on the Windows language.
prog_files = os.environ["PROGRAMFILES"]
except KeyError:
prog_files = r"C:\Program Files"
# For Windows, the user is instructed to move the programs to a folder
# and then to add the folder to the system path. Just in case they didn't
# do that, we can check for it in Program Files.
likely_dirs = ["", # Current dir
prog_files,
os.path.join(prog_files, "paml41"),
os.path.join(prog_files, "paml43"),
os.path.join(prog_files, "paml44"),
os.path.join(prog_files, "paml45")] + sys.path
os_path.extend(likely_dirs)
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
# Find the PAML binaries
if sys.platform == "win32":
binaries = ["codeml.exe", "baseml.exe", "yn00.exe"]
else:
binaries = ["codeml", "baseml", "yn00"]
for binary in binaries:
if which(binary) is None:
raise MissingExternalDependencyError(
"Install PAML if you want to use the Bio.Phylo.PAML wrapper.")
class Common(unittest.TestCase):
"""Base class for PAML unit tests."""
del_files = []
def __del__(self):
"""Just in case tool creates some junk files, do a clean-up."""
del_files = self.del_files
for filename in del_files:
if os.path.exists(filename):
os.remove(filename)
class CodemlTest(Common):
"""Tests for PAML tool codeml."""
def setUp(self):
self.cml = codeml.Codeml()
def testCodemlBinary(self):
"""Test that the codeml binary runs and generates correct output
and is the correct version.
"""
ctl_file = os.path.join("PAML", "Control_files", "codeml", "codeml.ctl")
self.cml.read_ctl_file(ctl_file)
self.cml.alignment = os.path.join("PAML", "Alignments", "alignment.phylip")
self.cml.tree = os.path.join("PAML", "Trees", "species.tree")
self.cml.out_file = os.path.join("PAML", "temp.out")
self.cml.working_dir = os.path.join("PAML", "codeml_test")
results = self.cml.run()
self.assertTrue(results["version"] > "4.0")
self.assertTrue("NSsites" in results)
self.assertEqual(len(results["NSsites"]), 1)
self.assertEqual(len(results["NSsites"][0]), 5)
class BasemlTest(Common):
"""Tests for PAML tool baseml."""
def setUp(self):
self.bml = baseml.Baseml()
def testBasemlBinary(self):
"""Test that the baseml binary runs and generates correct output
and is the correct version.
"""
ctl_file = os.path.join("PAML", "Control_files", "baseml", "baseml.ctl")
self.bml.read_ctl_file(ctl_file)
self.bml.alignment = os.path.join("PAML", "Alignments", "alignment.phylip")
self.bml.tree = os.path.join("PAML", "Trees", "species.tree")
self.bml.out_file = os.path.join("PAML", "temp.out")
self.bml.working_dir = os.path.join("PAML", "baseml_test")
results = self.bml.run()
self.assertTrue(results["version"] > "4.0")
self.assertTrue("parameters" in results)
self.assertEqual(len(results["parameters"]), 5)
class Yn00Test(Common):
"""Tests for PAML tool yn00."""
def setUp(self):
self.yn = yn00.Yn00()
def testYn00Binary(self):
"""Test that the yn00 binary runs and generates correct output.
yn00 output does not specify the version number.
"""
ctl_file = os.path.join("PAML", "Control_files", "yn00", "yn00.ctl")
self.yn.read_ctl_file(ctl_file)
self.yn.alignment = os.path.join("PAML", "Alignments", "alignment.phylip")
self.yn.out_file = os.path.join("PAML", "temp.out")
self.yn.working_dir = os.path.join("PAML", "yn00_test")
results = self.yn.run()
self.assertEqual(len(results), 5)
if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity=2)
unittest.main(testRunner=runner)
clean_up()
|