Back to index

python-biopython  1.60
test_TCoffee_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 TCOFFEE."""
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 TCoffeeCommandline
00014 
00015 #Try to avoid problems when the OS is in another language
00016 os.environ['LANG'] = 'C'
00017 
00018 t_coffee_exe = None
00019 if sys.platform=="win32":
00020     raise MissingExternalDependencyError(\
00021         "Testing TCOFFEE on Windows not supported yet")
00022 else:
00023     import commands
00024     output = commands.getoutput("t_coffee -version")
00025     if "not found" not in output \
00026     and ("t_coffee" in output.lower() or "t-coffee" in output.lower()):
00027         t_coffee_exe = "t_coffee"
00028 
00029 if not t_coffee_exe:
00030     raise MissingExternalDependencyError(\
00031         "Install TCOFFEE if you want to use the Bio.Align.Applications wrapper.")
00032 
00033 class TCoffeeApplication(unittest.TestCase):
00034 
00035     def setUp(self):
00036         self.infile1 = "Fasta/fa01"
00037         self.outfile1 = "fa01.aln"
00038         self.outfile2 = "fa01.html" #Written by default when no output set
00039         self.outfile3 = "Fasta/tc_out.pir"
00040         self.outfile4 = "Fasta/tc_out.phy"
00041 
00042     def tearDown(self):
00043         if os.path.isfile(self.outfile1):
00044             os.remove(self.outfile1)
00045         if os.path.isfile(self.outfile2):
00046             os.remove(self.outfile2)
00047         if os.path.isfile(self.outfile3):
00048             os.remove(self.outfile3)
00049         if os.path.isfile(self.outfile4):
00050             os.remove(self.outfile4)
00051 
00052     def test_TCoffee_1(self):
00053         """Round-trip through app and read clustal alignment from file
00054         """
00055         cmdline = TCoffeeCommandline(t_coffee_exe, infile=self.infile1)
00056         self.assertEqual(str(cmdline), t_coffee_exe + " -infile Fasta/fa01")
00057         stdout, stderr = cmdline()
00058         self.assertTrue(stderr.strip().startswith("PROGRAM: T-COFFEE"))
00059         align = AlignIO.read(open(self.outfile1), "clustal")
00060         records = list(SeqIO.parse(open(self.infile1),"fasta"))
00061         self.assertEqual(len(records),len(align))
00062         for old, new in zip(records, align):
00063             self.assertEqual(old.id, new.id)
00064             self.assertEqual(str(new.seq).replace("-",""), str(old.seq).replace("-",""))
00065 
00066     def test_TCoffee_2(self):
00067         """Round-trip through app and read pir alignment from file
00068         """
00069         cmdline = TCoffeeCommandline(t_coffee_exe, quiet=True)
00070         cmdline.infile = self.infile1
00071         cmdline.outfile = self.outfile3
00072         cmdline.output = "pir_aln"
00073         self.assertEqual(str(cmdline), t_coffee_exe + " -output pir_aln "
00074                     "-infile Fasta/fa01 -outfile Fasta/tc_out.pir -quiet")
00075         stdout, stderr = cmdline()
00076         #Can get warnings in stderr output
00077         self.assertTrue("error" not in stderr.lower(), stderr)
00078         align = AlignIO.read(open(self.outfile3), "pir")
00079         records = list(SeqIO.parse(open(self.infile1),"fasta"))
00080         self.assertEqual(len(records),len(align))
00081         for old, new in zip(records, align):
00082             self.assertEqual(old.id, new.id)
00083             self.assertEqual(str(new.seq).replace("-",""), str(old.seq).replace("-",""))
00084 
00085     def test_TCoffee_3(self):
00086         """Round-trip through app and read clustalw alignment from file
00087         """
00088         cmdline = TCoffeeCommandline(t_coffee_exe, gapopen=-2)
00089         cmdline.infile = self.infile1
00090         cmdline.outfile = self.outfile4
00091         cmdline.set_parameter("output", "clustalw_aln")
00092         cmdline.outorder = "input"
00093         cmdline.set_parameter("gapext", -5)
00094         cmdline.type = "protein"
00095         self.assertEqual(str(cmdline), t_coffee_exe + " -output clustalw_aln "
00096                          "-infile Fasta/fa01 -outfile Fasta/tc_out.phy "
00097                          "-type protein -outorder input -gapopen -2 -gapext -5")
00098         stdout, stderr = cmdline()
00099         self.assertTrue(stderr.strip().startswith("PROGRAM: T-COFFEE"))
00100         align = AlignIO.read(open(self.outfile4), "clustal")
00101         records = list(SeqIO.parse(open(self.infile1),"fasta"))
00102         self.assertEqual(len(records),len(align))
00103         for old, new in zip(records, align):
00104             self.assertEqual(old.id, new.id)
00105             self.assertEqual(str(new.seq).replace("-",""), str(old.seq).replace("-",""))
00106 
00107 if __name__ == "__main__":
00108     runner = unittest.TextTestRunner(verbosity = 2)
00109     unittest.main(testRunner=runner)