Back to index

python-biopython  1.60
check_output.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """Check for the ability to read and write identical GenBank records.
00003 
00004 This script takes as input a single file to be tested for reading records.
00005 It will run through this file and check that the output of the parsed and
00006 printed record matches the original record.
00007 
00008 Usage:
00009 python check_output.py <name of file to parse>
00010 """
00011 # standard modules
00012 import sys
00013 import os
00014 import cStringIO
00015 import gzip
00016 
00017 # biopython
00018 from Bio import GenBank
00019 
00020 def do_comparison(good_record, test_record):
00021     """Compare two records to see if they are the same.
00022 
00023     Ths compares the two GenBank record, and will raise an AssertionError
00024     if two lines do not match, showing the non-matching lines.
00025     """
00026     good_handle = cStringIO.StringIO(good_record)
00027     test_handle = cStringIO.StringIO(test_record)
00028 
00029     while 1:
00030         good_line = good_handle.readline()
00031         test_line = test_handle.readline()
00032 
00033         if not(good_line) and not(test_line):
00034             break
00035 
00036         if not(good_line):
00037             if good_line.strip():
00038                 raise AssertionError("Extra info in Test: `%s`" % test_line)
00039         if not(test_line):
00040             if test_line.strip():
00041                 raise AssertionError("Extra info in Expected: `%s`"
00042                                      % good_line)
00043 
00044         assert test_line == good_line, \
00045                "Expected does not match Test.\nExpect:`%s`\nTest  :`%s`\n" % \
00046                (good_line, test_line)
00047     
00048 def write_format(file):
00049     record_parser = GenBank.RecordParser(debug_level = 2)
00050 
00051     print "Testing GenBank writing for %s..." % os.path.basename(file)
00052     # be able to handle gzipped files
00053     if file.find(".gz") >= 0:
00054         cur_handle = gzip.open(file, "r")
00055         compare_handle = gzip.open(file, "r")
00056     else:
00057         cur_handle = open(file, "r")
00058         compare_handle = open(file, "r")
00059 
00060     iterator = GenBank.Iterator(cur_handle, record_parser)
00061     compare_iterator = GenBank.Iterator(compare_handle)
00062         
00063     while 1:
00064         cur_record = iterator.next()
00065         compare_record = compare_iterator.next()
00066             
00067         if cur_record is None or compare_record is None:
00068             break
00069 
00070         # print "\tTesting for %s" % cur_record.version
00071 
00072         output_record = str(cur_record) + "\n"
00073         try:
00074             do_comparison(compare_record, output_record)
00075         except AssertionError, msg:
00076             print "\tTesting for %s" % cur_record.version
00077             print msg
00078 
00079     cur_handle.close()
00080     compare_handle.close()
00081 
00082 if __name__ == "__main__":
00083     if len(sys.argv) != 2:
00084         print __doc__
00085         sys.exit()
00086 
00087     write_format(sys.argv[1])