Back to index

python-biopython  1.60
test_AlignIO_convert.py
Go to the documentation of this file.
00001 # Copyright 2009 by Peter Cock.  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 
00006 """Unit tests for Bio.SeqIO.convert(...) function."""
00007 import unittest
00008 from StringIO import StringIO
00009 
00010 from Bio import AlignIO
00011 from Bio.Alphabet import generic_protein, generic_nucleotide, generic_dna
00012 
00013 
00014 #Top level function as this makes it easier to use for debugging:
00015 def check_convert(in_filename, in_format, out_format, alphabet=None):
00016     #Write it out using parse/write
00017     handle = StringIO()
00018     aligns = list(AlignIO.parse(open(in_filename), in_format, None, alphabet))
00019     try:
00020         count = AlignIO.write(aligns, handle, out_format)
00021     except ValueError:
00022         count = 0
00023     #Write it out using convert passing filename and handle
00024     handle2 = StringIO()
00025     try:
00026         count2 = AlignIO.convert(in_filename, in_format, handle2, out_format, alphabet)
00027     except ValueError:
00028         count2 = 0
00029     assert count == count2
00030     assert handle.getvalue() == handle2.getvalue()
00031     #Write it out using convert passing handle and handle
00032     handle2 = StringIO()
00033     try:
00034         count2 = AlignIO.convert(open(in_filename), in_format, handle2, out_format, alphabet)
00035     except ValueError:
00036         count2 = 0
00037     assert count == count2
00038     assert handle.getvalue() == handle2.getvalue()
00039     #TODO - convert passing an output filename?
00040 
00041 class ConvertTests(unittest.TestCase):
00042     """Cunning unit test where methods are added at run time."""
00043     def simple_check(self, filename, in_format, out_format, alphabet):
00044         check_convert(filename, in_format, out_format, alphabet)
00045 
00046 tests = [
00047     ('Clustalw/hedgehog.aln', "clustal", None),
00048     ('Nexus/test_Nexus_input.nex', "nexus", None),
00049     ('Stockholm/simple.sth', "stockholm", None),
00050     ('GFF/multi.fna', "fasta", generic_nucleotide),
00051     ("Quality/example.fastq", "fastq", None),
00052     ("Quality/example.fastq", "fastq-sanger", generic_dna),
00053     ('Fasta/output001.m10', "fasta-m10", None),
00054     ('IntelliGenetics/VIF_mase-pro.txt', "ig", generic_protein),
00055     ('NBRF/clustalw.pir', "pir", None),
00056     ]
00057 output_formats = ["fasta"] + sorted(AlignIO._FormatToWriter)
00058 
00059 for filename, in_format, alphabet in tests:
00060     for out_format in output_formats:
00061         def funct(fn,fmt1, fmt2, alpha):
00062             f = lambda x : x.simple_check(fn, fmt1, fmt2, alpha)
00063             f.__doc__ = "Convert %s from %s to %s" % (fn, fmt1, fmt2)
00064             return f
00065         setattr(ConvertTests, "test_%s_%s_to_%s" \
00066                 % (filename.replace("/","_").replace(".","_"), in_format, out_format),
00067                 funct(filename, in_format, out_format, alphabet))
00068     del funct
00069 
00070 if __name__ == "__main__":
00071     runner = unittest.TextTestRunner(verbosity = 2)
00072     unittest.main(testRunner=runner)