Mercurial > repos > jjohnson > query_tabular
comparison query_tabular.py @ 0:763c4f968dc6
Uploaded
author | jjohnson |
---|---|
date | Thu, 01 Oct 2015 09:50:16 -0400 |
parents | |
children | 24f0911f75ee |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:763c4f968dc6 |
---|---|
1 #!/usr/bin/env python | |
2 """ | |
3 """ | |
4 import sys | |
5 import os.path | |
6 import sqlite3 as sqlite | |
7 import optparse | |
8 from optparse import OptionParser | |
9 | |
10 """ | |
11 TODO: | |
12 - could add some transformations on tabular columns, e.g. a regex to format date/time strings | |
13 - allow multiple queries and outputs | |
14 - add a --json input for table definitions (or yaml) | |
15 JSON config: | |
16 { tables : [ | |
17 { file_path : '/home/galaxy/dataset_101.dat', | |
18 table_name : 't1', | |
19 column_names : ['c1','c2','c3'] | |
20 }, | |
21 { file_path : '/home/galaxy/dataset_102.dat', | |
22 table_name : 't2', | |
23 column_names : ['c1','c2','c3'] | |
24 } | |
25 ] | |
26 } | |
27 """ | |
28 | |
29 def getValueType(val): | |
30 if val or 0. == val: | |
31 try: | |
32 int(val) | |
33 return 'INTEGER' | |
34 except: | |
35 try: | |
36 float(val) | |
37 return 'REAL' | |
38 except: | |
39 return 'TEXT' | |
40 return None | |
41 | |
42 | |
43 def get_column_def(file_path,table_name,skip=0,comment_char='#',column_names=None,max_lines=100): | |
44 col_pref = ['TEXT','REAL','INTEGER',None] | |
45 col_types = [] | |
46 data_lines = 0 | |
47 try: | |
48 with open(file_path,"r") as fh: | |
49 for linenum,line in enumerate(fh): | |
50 if linenum < skip: | |
51 continue | |
52 if line.startswith(comment_char): | |
53 continue | |
54 data_lines += 1 | |
55 try: | |
56 fields = line.split('\t') | |
57 while len(col_types) < len(fields): | |
58 col_types.append(None) | |
59 for i,val in enumerate(fields): | |
60 colType = getValueType(val) | |
61 if col_pref.index(colType) < col_pref.index(col_types[i]): | |
62 col_types[i] = colType | |
63 except Exception, e: | |
64 print >> sys.stderr, 'Failed at line: %d err: %s' % (linenum,e) | |
65 except Exception, e: | |
66 print >> sys.stderr, 'Failed: %s' % (e) | |
67 for i,col_type in enumerate(col_types): | |
68 if not col_type: | |
69 col_types[i] = 'TEXT' | |
70 col_names = ['c%d' % i for i in range(1,len(col_types) + 1)] | |
71 if column_names: | |
72 for i,cname in enumerate([cn.strip() for cn in column_names.split(',')]): | |
73 if cname and i < len(col_names): | |
74 col_names[i] = cname | |
75 col_def = [] | |
76 for i,col_name in enumerate(col_names): | |
77 col_def.append('%s %s' % (col_names[i],col_types[i])) | |
78 return col_names,col_types,col_def | |
79 | |
80 def create_table(conn,file_path,table_name,skip=0,comment_char='#',column_names=None): | |
81 col_names,col_types,col_def = get_column_def(file_path,table_name,skip=skip,comment_char=comment_char,column_names=column_names) | |
82 col_func = [float if t == 'REAL' else int if t == 'INTEGER' else str for t in col_types] | |
83 table_def = 'CREATE TABLE %s (\n %s\n);' % (table_name,',\n '.join(col_def)) | |
84 # print >> sys.stdout, table_def | |
85 insert_stmt = 'INSERT INTO %s(%s) VALUES(%s)' % (table_name,','.join(col_names),','.join([ "?" for x in col_names])) | |
86 # print >> sys.stdout, insert_stmt | |
87 data_lines = 0 | |
88 try: | |
89 c = conn.cursor() | |
90 c.execute(table_def) | |
91 with open(file_path,"r") as fh: | |
92 for linenum,line in enumerate(fh): | |
93 if linenum < skip or line.startswith(comment_char): | |
94 continue | |
95 data_lines += 1 | |
96 try: | |
97 fields = line.split('\t') | |
98 vals = [col_func[i](x) if x else None for i,x in enumerate(fields)] | |
99 c.execute(insert_stmt,vals) | |
100 except Exception, e: | |
101 print >> sys.stderr, 'Failed at line: %d err: %s' % (linenum,e) | |
102 conn.commit() | |
103 c.close() | |
104 except Exception, e: | |
105 print >> sys.stderr, 'Failed: %s' % (e) | |
106 | |
107 def __main__(): | |
108 #Parse Command Line | |
109 parser = optparse.OptionParser() | |
110 parser.add_option( '-s', '--sqlitedb', dest='sqlitedb', default=None, help='The SQLite Database' ) | |
111 parser.add_option( '-t', '--table', dest='tables', action="append", default=[], help='Tabular file: file_path[=table_name[:column_name,...]' ) | |
112 parser.add_option( '-q', '--query', dest='query', default=None, help='SQL query' ) | |
113 parser.add_option( '-n', '--no_header', dest='no_header', action='store_true', default=False, help='Include a column headers line' ) | |
114 parser.add_option( '-o', '--output', dest='output', default=None, help='Output file for query results' ) | |
115 (options, args) = parser.parse_args() | |
116 | |
117 # oprn sqlite connection | |
118 conn = sqlite.connect(options.sqlitedb) | |
119 # determine output destination | |
120 if options.output != None: | |
121 try: | |
122 outputPath = os.path.abspath(options.output) | |
123 outputFile = open(outputPath, 'w') | |
124 except Exception, e: | |
125 print >> sys.stderr, "failed: %s" % e | |
126 exit(3) | |
127 else: | |
128 outputFile = sys.stdout | |
129 | |
130 # determine output destination | |
131 for ti,table in enumerate(options.tables): | |
132 table_name = 't%d' % (ti + 1) | |
133 column_names = None | |
134 fields = table.split('=') | |
135 path = fields[0] | |
136 if len(fields) > 1: | |
137 names = fields[1].split(':') | |
138 table_name = names[0] if names[0] else table_name | |
139 if len(names) > 1: | |
140 column_names = names[1] | |
141 # print >> sys.stdout, '%s %s' % (table_name, path) | |
142 create_table(conn,path,table_name,column_names=column_names) | |
143 conn.close() | |
144 | |
145 if (options.query is None): | |
146 try: | |
147 conn = sqlite.connect(options.sqlitedb) | |
148 c = conn.cursor() | |
149 tables_query = "SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name" | |
150 rslt = c.execute(tables_query).fetchall() | |
151 for table,sql in rslt: | |
152 print >> sys.stderr, "Table %s:" % table | |
153 try: | |
154 col_query = 'SELECT * FROM %s LIMIT 0' % table | |
155 cur = conn.cursor().execute(col_query) | |
156 cols = [col[0] for col in cur.description] | |
157 print >> sys.stderr, " Columns: %s" % cols | |
158 except Exception, exc: | |
159 print >> sys.stderr, "Error: %s" % exc | |
160 except Exception, exc: | |
161 print >> sys.stderr, "Error: %s" % exc | |
162 exit(0) | |
163 #if not sqlite.is_read_only_query(options.query): | |
164 # print >> sys.stderr, "Error: Must be a read only query" | |
165 # exit(2) | |
166 try: | |
167 conn = sqlite.connect(options.sqlitedb) | |
168 cur = conn.cursor() | |
169 results = cur.execute(options.query) | |
170 if not options.no_header: | |
171 outputFile.write("#%s\n" % '\t'.join([str(col[0]) for col in cur.description])) | |
172 # yield [col[0] for col in cur.description] | |
173 for i,row in enumerate(results): | |
174 # yield [val for val in row] | |
175 outputFile.write("%s\n" % '\t'.join([str(val) for val in row])) | |
176 except Exception, exc: | |
177 print >> sys.stderr, "Error: %s" % exc | |
178 | |
179 if __name__ == "__main__": __main__() | |
180 | |
181 |