comparison filter_tabular.py @ 0:3708ff0198b7 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
author iuc
date Tue, 18 Jul 2017 09:07:07 -0400
parents
children cf34c344508d
comparison
equal deleted inserted replaced
-1:000000000000 0:3708ff0198b7
1 #!/usr/bin/env python
2
3 from __future__ import print_function
4
5 import json
6 import optparse
7 import os.path
8 import sys
9
10 from filters import filter_file
11
12
13 def __main__():
14 # Parse Command Line
15 parser = optparse.OptionParser()
16 parser.add_option('-i', '--input', dest='input', default=None,
17 help='Input file for filtering')
18 parser.add_option('-j', '--jsonfile', dest='jsonfile', default=None,
19 help='JSON array of filter specifications')
20 parser.add_option('-o', '--output', dest='output', default=None,
21 help='Output file for query results')
22 parser.add_option('-v', '--verbose', dest='verbose', default=False,
23 action='store_true',
24 help='verbose')
25 (options, args) = parser.parse_args()
26
27 if options.input is not None:
28 try:
29 inputPath = os.path.abspath(options.input)
30 inputFile = open(inputPath, 'r')
31 except Exception as e:
32 exit('Error: %s' % (e))
33 else:
34 inputFile = sys.stdin
35
36 if options.output is not None:
37 try:
38 outputPath = os.path.abspath(options.output)
39 outputFile = open(outputPath, 'w')
40 except Exception as e:
41 exit('Error: %s' % (e))
42 else:
43 outputFile = sys.stdout
44
45 filters = None
46 if options.jsonfile:
47 try:
48 with open(options.jsonfile) as fh:
49 filters = json.load(fh)
50 except Exception as e:
51 exit('Error: %s' % (e))
52
53 if options.verbose and filters:
54 for f in filters:
55 print('%s %s' % (f['filter'],
56 ', '.join(
57 ['%s: %s' % (k, f[k])
58 for k in set(f.keys()) - set(['filter'])])),
59 file=sys.stdout)
60
61 try:
62 filter_file(inputFile, outputFile, filters=filters)
63 except Exception as e:
64 exit('Error: %s' % (e))
65
66
67 if __name__ == "__main__":
68 __main__()