def test_blast_output(outfile): # Try to auto-detect the format if 1: print("No parser specified. I'll try to choose one for you based") print("on the format of the output file.") print("") parser_class = choose_parser(outfile) print("It looks like you have given output that should be parsed") print("with %s.%s. If I'm wrong, you can select the correct parser" %\ (parser_class.__module__, parser_class.__name__)) print("on the command line of this script (NOT IMPLEMENTED YET).") else: raise NotImplementedError parser_class = NCBIWWW.BlastParser print("Using %s to parse the file." % parser_class.__name__) print("") scanner_class = parser_class()._scanner.__class__ consumer_class = parser_class()._consumer.__class__ #parser_class()._scanner.feed( # open(outfile), ParserSupport.TaggingConsumer()) print( "I'm going to run the data through the parser to see what happens...") parser = parser_class() try: rec = parser.parse_file(outfile) except (KeyboardInterrupt, SystemExit): raise except Exception as x: exception_info = str(x) print("Dang, the parsing failed.") else: print("Parsing succeeded, no problems detected.") print("However, you should check to make sure the following scanner") print("trace looks reasonable.") print("") parser_class()._scanner.feed(open(outfile), ParserSupport.TaggingConsumer()) return 0 print("") print("Alright. Let me try and figure out where in the parser the") print("problem occurred...") etype, value, tb = sys.exc_info() ftb = traceback.extract_tb(tb) ftb.reverse() class_found = None for err_file, err_line, err_function, err_text in ftb: if hasattr(consumer_class, err_function): class_found = consumer_class break elif hasattr(scanner_class, err_function): class_found = scanner_class break if class_found is None: print("Sorry, I could not pinpoint the error to the parser.") print("There's nothing more I can tell you.") print("Here's the traceback:") traceback.print_exception(etype, value, tb) return 1 else: print("I found the problem in %s.%s.%s, line %d:" % \ (class_found.__module__, class_found.__name__, err_function, err_line)) print(" %s" % err_text) print("This output caused an %s to be raised with the" % etype) print("information %r." % exception_info) print("") print("Let me find the line in the file that triggers the problem...") parser = parser_class() scanner, consumer = parser._scanner, parser._consumer consumer = DebuggingConsumer(consumer) try: scanner.feed(open(outfile), consumer) except etype as x: pass else: print("Odd, the exception disappeared! What happened?") return 3 print("It's caused by line %d:" % consumer.linenum) lines = open(outfile).readlines() start, end = consumer.linenum - CONTEXT, consumer.linenum + CONTEXT + 1 if start < 0: start = 0 if end > len(lines): end = len(lines) ndigits = len(str(end)) for linenum in range(start, end): line = chomp(lines[linenum]) if linenum == consumer.linenum: prefix = '*' else: prefix = ' ' s = "%s%*d %s" % (prefix, ndigits, linenum, line) s = s[:80] print(s) print("") if class_found == scanner_class: print("Problems in %s are most likely caused by changed formats." % \ class_found.__name__) print("You can start to fix this by going to line %d in module %s." % \ (err_line, class_found.__module__)) print("Perhaps the scanner needs to be made more lenient by accepting") print("the changed format?") print("") if VERBOSITY <= 0: print("For more help, you can run this script in verbose mode") print("to see detailed information about how the scanner") print("identifies each line.") else: print("OK, let's see what the scanner's doing!") print("") print("*" * 20 + " BEGIN SCANNER TRACE " + "*" * 20) try: parser_class()._scanner.feed(open(outfile), ParserSupport.TaggingConsumer()) except etype as x: pass print("*" * 20 + " END SCANNER TRACE " + "*" * 20) print("") elif class_found == consumer_class: print("Problems in %s can be caused by two things:" % \ class_found.__name__) print(" - The format of the line parsed by '%s' changed." % \ err_function) print(" - The scanner misidentified the line.") print("Check to make sure '%s' should parse the line:" % \ err_function) s = " %s" % chomp(lines[consumer.linenum]) s = s[:80] print(s) print("If so, debug %s.%s. Otherwise, debug %s." % \ (class_found.__name__, err_function, scanner_class.__name__))
### _Scanner print "Running tests on _Scanner" scanner = NCBIStandalone._Scanner() for test in all_tests: print "*" * 50, "TESTING %s" % test datafile = os.path.join("Blast", test) scanner.feed(open(datafile), ParserSupport.AbstractConsumer()) for test in detailed_tests: print "*" * 50, "TESTING %s" % test datafile = os.path.join("Blast", test) scanner.feed(open(datafile), ParserSupport.TaggingConsumer()) ### BlastParser print "Running tests on BlastParser" parser = NCBIStandalone.BlastParser() pb_parser = NCBIStandalone.PSIBlastParser() for test in all_tests: print "*" * 50, "TESTING %s" % test datafile = os.path.join("Blast", test) try: # First, try parsing it with the normal parser. rec = parser.parse(open(datafile)) except ValueError, x: # If it complains that the input is psiblast data, then
print "I'm going to run the data through the parser to see what happens..." parser = parser_class() try: rec = parser.parse_file(outfile) except KeyboardInterrupt, SystemExit: raise except Exception, x: exception_info = str(x) print "Dang, the parsing failed." else: print "Parsing succeeded, no problems detected." print "However, you should check to make sure the following scanner" print "trace looks reasonable." print parser_class()._scanner.feed(open(outfile), ParserSupport.TaggingConsumer()) return 0 print print "Alright. Let me try and figure out where in the parser the" print "problem occurred..." etype, value, tb = sys.exc_info() ftb = traceback.extract_tb(tb) ftb.reverse() class_found = None for err_file, err_line, err_function, err_text in ftb: if hasattr(consumer_class, err_function): class_found = consumer_class break elif hasattr(scanner_class, err_function): class_found = scanner_class
from Bio.File import UndoHandle meme_tests = [ "meme.dna.oops.txt", "meme.protein.oops.txt", "meme.protein.tcm.txt" ] mast_tests = [ "mast.dna.oops.txt", "mast.protein.oops.txt", "mast.protein.tcm.txt" ] print "Testing MEME Scanner" datafile = os.path.join("MEME", meme_tests[0]) uhandle = UndoHandle(open(datafile)) scanner = Parser._MEMEScanner() consumer = ParserSupport.TaggingConsumer() scanner.feed(uhandle, consumer) print "Running tests on MEME parser" meme_parser = Parser.MEMEParser() for test in meme_tests: print "*" * 50, "TESTING %s" % test datafile = os.path.join("MEME", test) rec = meme_parser.parse(open(datafile)) print "Testing MEME Scanner" datafile = os.path.join("MEME", mast_tests[0]) uhandle = UndoHandle(open(datafile))
def pb(b): if b: return 1 return 0 # TaggingConsumer print("Running tests on TaggingConsumer") class TestHandle(object): def write(self, s): print(s) h = TestHandle() tc = ParserSupport.TaggingConsumer(handle=h, colwidth=5) tc.start_section() # '***** start_section\n' tc.test1('myline') # 'test1: myline\n' tc.end_section() # '***** end_section\n' # is_blank_line print("Running tests on is_blank_line") is_blank_line = lambda *args, **keywds: \ pb(ParserSupport.is_blank_line(*args, **keywds)) print(is_blank_line('\n')) # 1 print(is_blank_line('\r\n')) # 1 print(is_blank_line('\r')) # 1
print "I'm going to run the data through the parser to see what happens..." parser = parser_class() try: rec = parser.parse_file(outfile) except KeyboardInterrupt, SystemExit: raise except Exception, x: exception_info = str(x) print "Dang, the parsing failed." else: print "Parsing succeeded, no problems detected." print "However, you should check to make sure the following scanner" print "trace looks reasonable." print parser_class()._scanner.feed( open(outfile), ParserSupport.TaggingConsumer()) return 0 print print "Alright. Let me try and figure out where in the parser the" print "problem occurred..." etype, value, tb = sys.exc_info() ftb = traceback.extract_tb(tb) ftb.reverse() class_found = None for err_file, err_line, err_function, err_text in ftb: if hasattr(consumer_class, err_function): class_found = consumer_class break elif hasattr(scanner_class, err_function): class_found = scanner_class