comparison tools/seq_select_by_id/seq_select_by_id.py @ 8:8e1a90917fa7 draft

v0.0.13 Python 3 compatible exception handling
author peterjc
date Wed, 17 May 2017 09:23:03 -0400
parents a5602454b0ad
children 3b0a14722175
comparison
equal deleted inserted replaced
7:a5602454b0ad 8:8e1a90917fa7
24 from __future__ import print_function 24 from __future__ import print_function
25 25
26 import sys 26 import sys
27 27
28 if "-v" in sys.argv or "--version" in sys.argv: 28 if "-v" in sys.argv or "--version" in sys.argv:
29 print("v0.0.12") 29 print("v0.0.13")
30 sys.exit(0) 30 sys.exit(0)
31 31
32 # Parse Command Line 32 # Parse Command Line
33 try: 33 try:
34 tabular_file, col_arg, in_file, seq_format, out_file = sys.argv[1:] 34 tabular_file, col_arg, in_file, seq_format, out_file = sys.argv[1:]
116 # but doing the header and index at the low level is too fidly. 116 # but doing the header and index at the low level is too fidly.
117 name = None # We want the variable to leak from the iterator's scope... 117 name = None # We want the variable to leak from the iterator's scope...
118 iterator = (records[name] for name in parse_ids(tabular_file, column)) 118 iterator = (records[name] for name in parse_ids(tabular_file, column))
119 try: 119 try:
120 count = writer.write_file(iterator) 120 count = writer.write_file(iterator)
121 except KeyError, err: 121 except KeyError:
122 out_handle.close() 122 out_handle.close()
123 if name not in records: 123 if name not in records:
124 sys.exit("Identifier %r not found in sequence file" % name) 124 sys.exit("Identifier %r not found in sequence file" % name)
125 else: 125 else:
126 raise err 126 raise
127 out_handle.close() 127 out_handle.close()
128 else: 128 else:
129 # Avoid overhead of parsing into SeqRecord objects, 129 # Avoid overhead of parsing into SeqRecord objects,
130 # just re-use the original formatting from the input file. 130 # just re-use the original formatting from the input file.
131 out_handle = open(out_file, "w") 131 out_handle = open(out_file, "w")