comparison sqlite_to_tabular.py @ 0:cf32fc695e72 draft

planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/sqlite_to_tabular commit 64a950cafd655311c99a54f96a27b49f0bdf2731-dirty
author jjohnson
date Thu, 21 Jan 2016 09:35:35 -0500
parents
children 123b9ca5e26a
comparison
equal deleted inserted replaced
-1:000000000000 0:cf32fc695e72
1 #!/usr/bin/env python
2
3 import sys
4 import re
5 import os.path
6 import optparse
7 import sqlite3 as sqlite
8
9
10 def regex_match(expr, item):
11 return re.match(expr, item) is not None
12
13
14 def regex_search(expr, item):
15 return re.search(expr, item) is not None
16
17
18 def regex_sub(expr, replace, item):
19 return re.sub(expr, replace, item)
20
21
22 def __main__():
23 # Parse Command Line
24 parser = optparse.OptionParser()
25 parser.add_option('-s', '--sqlitedb', dest='sqlitedb', default=None, help='The SQLite Database')
26 parser.add_option('-q', '--query', dest='query', default=None, help='SQL query')
27 parser.add_option('-Q', '--query_file', dest='query_file', default=None, help='SQL query file')
28 parser.add_option('-n', '--no_header', dest='no_header', action='store_true', default=False, help='Include a column headers line')
29 parser.add_option('-o', '--output', dest='output', default=None, help='Output file for query results')
30 (options, args) = parser.parse_args()
31
32 # determine output destination
33 if options.output is not None:
34 try:
35 outputPath = os.path.abspath(options.output)
36 outputFile = open(outputPath, 'w')
37 except Exception, e:
38 print >> sys.stderr, "failed: %s" % e
39 exit(3)
40 else:
41 outputFile = sys.stdout
42
43 query = None
44 if (options.query_file is not None):
45 with open(options.query_file, 'r') as fh:
46 query = ''
47 for line in fh:
48 query += line
49 elif (options.query is not None):
50 query = options.query
51
52 if (query is None):
53 try:
54 conn = sqlite.connect(options.sqlitedb)
55 c = conn.cursor()
56 tables_query = "SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name"
57 rslt = c.execute(tables_query).fetchall()
58 for table, sql in rslt:
59 print >> sys.stderr, "Table %s:" % table
60 try:
61 col_query = 'SELECT * FROM %s LIMIT 0' % table
62 cur = conn.cursor().execute(col_query)
63 cols = [col[0] for col in cur.description]
64 print >> sys.stderr, " Columns: %s" % cols
65 except Exception, exc:
66 print >> sys.stderr, "Error: %s" % exc
67 except Exception, exc:
68 print >> sys.stderr, "Error: %s" % exc
69 exit(0)
70 try:
71 conn = sqlite.connect(options.sqlitedb)
72 conn.create_function("re_match", 2, regex_match)
73 conn.create_function("re_search", 2, regex_search)
74 conn.create_function("re_sub", 3, regex_sub)
75 cur = conn.cursor()
76 results = cur.execute(query)
77 if not options.no_header:
78 outputFile.write("#%s\n" % '\t'.join([str(col[0]) for col in cur.description]))
79 for i, row in enumerate(results):
80 outputFile.write("%s\n" % '\t'.join([str(val) for val in row]))
81 except Exception, exc:
82 print >> sys.stderr, "Error: %s" % exc
83 exit(1)
84
85 if __name__ == "__main__":
86 __main__()