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
|
#!/usr/bin/env python
# Copyright 2000 Brad Chapman. All rights reserved.
#
# 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.
"""Check for the ability to read and write identical GenBank records.
This script takes as input a single file to be tested for reading records.
It will run through this file and check that the output of the parsed and
printed record matches the original record.
Usage:
python check_output.py <name of file to parse>
"""
# standard modules
import sys
import os
import gzip
from io import StringIO
# biopython
from Bio import GenBank
def do_comparison(good_record, test_record):
"""Compare two records to see if they are the same.
This compares the two GenBank record, and will raise an AssertionError
if two lines do not match, showing the non-matching lines.
"""
good_handle = StringIO(good_record)
test_handle = StringIO(test_record)
while True:
good_line = good_handle.readline()
test_line = test_handle.readline()
if not good_line and not test_line:
break
if not good_line:
if good_line.strip():
raise AssertionError(f"Extra info in Test: `{test_line}`")
if not test_line:
if test_line.strip():
raise AssertionError(f"Extra info in Expected: `{good_line}`")
assert test_line == good_line, (
"Expected does not match Test.\nExpect:`%s`\nTest :`%s`\n"
% (good_line, test_line)
)
def write_format(file):
"""Write a GenBank record from a Genbank file and compare them."""
record_parser = GenBank.RecordParser(debug_level=2)
print("Testing GenBank writing for %s..." % os.path.basename(file))
# be able to handle gzipped files
if ".gz" in file:
cur_handle = gzip.open(file, "rb")
compare_handle = gzip.open(file, "rb")
else:
cur_handle = open(file)
compare_handle = open(file)
iterator = GenBank.Iterator(cur_handle, record_parser)
compare_iterator = GenBank.Iterator(compare_handle)
while True:
cur_record = next(iterator)
compare_record = next(compare_iterator)
if cur_record is None or compare_record is None:
break
# print("\tTesting for %s" % cur_record.version)
output_record = str(cur_record) + "\n"
try:
do_comparison(compare_record, output_record)
except AssertionError as msg:
print(f"\tTesting for {cur_record.version}")
print(msg)
cur_handle.close()
compare_handle.close()
if __name__ == "__main__":
if len(sys.argv) != 2:
print(__doc__)
sys.exit()
write_format(sys.argv[1])
|