comparison blast2html.py @ 105:b3b5ee557170

update test
author Jan Kanis <jan.code@jankanis.nl>
date Mon, 07 Jul 2014 11:42:43 +0200
parents a22c909c9b57
children ee2b105d772a
comparison
equal deleted inserted replaced
104:a22c909c9b57 105:b3b5ee557170
407 407
408 def main(): 408 def main():
409 default_template = path.join(path.dirname(__file__), 'blast2html.html.jinja') 409 default_template = path.join(path.dirname(__file__), 'blast2html.html.jinja')
410 410
411 parser = argparse.ArgumentParser(description="Convert a BLAST XML result into a nicely readable html page", 411 parser = argparse.ArgumentParser(description="Convert a BLAST XML result into a nicely readable html page",
412 usage="{0} [-i] INPUT [-o OUTPUT]".format(sys.argv[0])) 412 usage="{0} [-i] INPUT [-o OUTPUT] [--genelink-template URL_TEMPLATE]".format(sys.argv[0]))
413 input_group = parser.add_mutually_exclusive_group(required=True) 413 input_group = parser.add_mutually_exclusive_group(required=True)
414 input_group.add_argument('positional_arg', metavar='INPUT', nargs='?', type=argparse.FileType(mode='r'), 414 input_group.add_argument('positional_arg', metavar='INPUT', nargs='?', type=argparse.FileType(mode='r'),
415 help='The input Blast XML file, same as -i/--input') 415 help='The input Blast XML file, same as -i/--input')
416 input_group.add_argument('-i', '--input', type=argparse.FileType(mode='r'), 416 input_group.add_argument('-i', '--input', type=argparse.FileType(mode='r'),
417 help='The input Blast XML file') 417 help='The input Blast XML file')
424 # care too much. 424 # care too much.
425 parser.add_argument('--template', type=argparse.FileType(mode='r'), default=default_template, 425 parser.add_argument('--template', type=argparse.FileType(mode='r'), default=default_template,
426 help='The template file to use. Defaults to blast_html.html.jinja') 426 help='The template file to use. Defaults to blast_html.html.jinja')
427 427
428 dblink_group = parser.add_mutually_exclusive_group() 428 dblink_group = parser.add_mutually_exclusive_group()
429 dblink_group.add_argument('--genelink-template', default='http://www.ncbi.nlm.nih.gov/nucleotide/{accession}?report=genbank&log$=nuclalign', 429 dblink_group.add_argument('--genelink-template', metavar='URL_TEMPLATE',
430 default='http://www.ncbi.nlm.nih.gov/nucleotide/{accession}?report=genbank&log$=nuclalign',
430 help="""A link template to link hits to a gene bank webpage. The template string is a 431 help="""A link template to link hits to a gene bank webpage. The template string is a
431 Python format string. It can contain the following replacement elements: {id[N]}, {fullid}, 432 Python format string. It can contain the following replacement elements: {id[N]}, {fullid},
432 {defline[N]}, {fulldefline}, {accession}, where N is a number. id[N] and defline[N] will be 433 {defline[N]}, {fulldefline}, {accession}, where N is a number. id[N] and defline[N] will be
433 replaced by the Nth element of the id or defline, where '|' is the field separator. 434 replaced by the Nth element of the id or defline, where '|' is the field separator.
434 435
449 if args.input == None: 450 if args.input == None:
450 parser.error('no input specified') 451 parser.error('no input specified')
451 452
452 if six.PY2: 453 if six.PY2:
453 # The argparse.FileType wrapper doesn't support an encoding 454 # The argparse.FileType wrapper doesn't support an encoding
454 # argument or such, so for python 2 we need to wrap or reopen 455 # argument, so for python 2 we need to wrap or reopen the
455 # the output. The input files are already read as utf-8 by the 456 # output. The input files are already read as utf-8 by the
456 # respective libraries. 457 # respective libraries.
457 458
458 # One option is using codecs, but the codecs' writelines() 459 # One option is using codecs, but the codecs' writelines()
459 # method doesn't support streaming but collects all output and 460 # method doesn't support streaming but collects all output and
460 # writes at once. On the other hand the io module is slower 461 # writes at once (see Python issues #5445 and #21910). On the
461 # (though not significantly). 462 # other hand the io module is slower (though not
463 # significantly).
462 464
463 # args.output = codecs.getwriter('utf-8')(args.output) 465 # args.output = codecs.getwriter('utf-8')(args.output)
464 args.output = io.open(args.output.name, 'w') 466 args.output = io.open(args.output.name, 'w')
465 467
466 templatedir, templatename = path.split(args.template.name) 468 templatedir, templatename = path.split(args.template.name)