Back to index

python-biopython  1.60
test_AlignIO_FastaIO.py
Go to the documentation of this file.
00001 # Copyright 2011 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 """Simple 'print-and-compare' unit test for fasta-m10 parser.
00006 
00007 Created to check for any regressions from my new implementation of the
00008 parser.
00009 """
00010 import os
00011 from Bio import AlignIO
00012 
00013 # test_files is a list of tuples containing:
00014 # - string:  file format
00015 # - integer: number of sequences per alignment
00016 # - integer: number of alignments
00017 # - string:  relative filename
00018 #
00019 # Most of the input files are also used by test_SeqIO.py,
00020 # and by other additional tests as noted below.
00021 test_files = [ \
00022     ("fasta-m10", 2, 4, 'Fasta/output001.m10'),
00023     ("fasta-m10", 2, 6, 'Fasta/output002.m10'),
00024     ("fasta-m10", 2, 3, 'Fasta/output003.m10'),
00025     ("fasta-m10", 2, 1, 'Fasta/output004.m10'),
00026     ("fasta-m10", 2, 1, 'Fasta/output005.m10'),
00027     ("fasta-m10", 2, 1, 'Fasta/output006.m10'),
00028     ("fasta-m10", 2, 9, 'Fasta/output007.m10'),
00029     ("fasta-m10", 2, 12,'Fasta/output008.m10'),
00030     ]
00031 
00032 #Main tests...
00033 for (t_format, t_per, t_count, t_filename) in test_files:
00034     assert t_format == "fasta-m10" and t_per == 2
00035 
00036     print "Testing reading %s format file %s with %i alignments" \
00037           % (t_format, t_filename, t_count)
00038     assert os.path.isfile(t_filename), t_filename
00039 
00040     #Try as an iterator using handle
00041     alignments  = list(AlignIO.parse(handle=open(t_filename,"r"), format=t_format))
00042     assert len(alignments)  == t_count, \
00043          "Found %i alignments but expected %i" % (len(alignments), t_count)
00044     for alignment in alignments:
00045         assert len(alignment) == t_per, \
00046             "Expected %i records per alignment, got %i" \
00047             % (t_per, len(alignment))
00048 
00049 
00050     #Print the alignment
00051     for i,alignment in enumerate(alignments):
00052         print "="*78
00053         print "Alignment %i, with %i sequences of length %i" \
00054               % (i,
00055                  len(alignment),
00056                  alignment.get_alignment_length())
00057         for k in sorted(alignment._annotations):
00058             print " - %s: %r" % (k, alignment._annotations[k])
00059         assert alignment[0].name == "query"
00060         assert alignment[1].name == "match"
00061         #Show each sequence row horizontally
00062         for record in alignment:
00063             print "-"*78
00064             print record.id
00065             print record.description
00066             print repr(record.seq)
00067             assert not record.features
00068             assert not record.letter_annotations
00069             for k in sorted(record.annotations):
00070                 print " - %s: %r" % (k, record.annotations[k])
00071     print "="*78
00072 print "Finished tested reading files"