Mercurial > repos > jjohnson > query_tabular
annotate query_tabular.py @ 11:fd16243931d6 draft
Uploaded
author | jjohnson |
---|---|
date | Fri, 12 May 2017 10:18:42 -0400 |
parents | e84d1c3bf4fe |
children | 5b4f6cf857cf |
rev | line source |
---|---|
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
1 #!/usr/bin/env python |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
2 """ |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
3 """ |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
4 import sys |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
5 import re |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
6 import os.path |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
7 import json |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
8 import sqlite3 as sqlite |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
9 import optparse |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
10 from optparse import OptionParser |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
11 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
12 """ |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
13 TODO: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
14 - could read column names from comment lines, but issues with legal names |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
15 - could add some transformations on tabular columns, |
6 | 16 filter - skip_regex |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
17 e.g. a regex to format date/time strings |
4
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
18 format: { |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
19 c2 : re.sub('pat', 'sub', c2) |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
20 c3 : len(c3) |
6 | 21 } |
22 def format(colname,val, expr): | |
23 normalize input list columns | |
24 iterate over list values creating one row per iteration | |
25 option for input line_num column | |
26 create associated table | |
27 fk, name, value # e.g. PSM table with list of proteins containing peptide | |
28 fk, name, value[, value] # if multiple columns similarly indexed, e.g. vcf | |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
29 - column_defs dict of columns to create from tabular input |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
30 column_defs : { 'name1' : 'expr', 'name2' : 'expr'} |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
31 - allow multiple queries and outputs |
6 | 32 repeat min - max with up to max conditional outputs |
33 | |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
34 - add a --json input for table definitions (or yaml) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
35 JSON config: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
36 { tables : [ |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
37 { file_path : '/home/galaxy/dataset_101.dat', |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
38 table_name : 't1', |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
39 column_names : ['c1', 'c2', 'c3'], |
5
19ae309ec53c
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 29288f94a382686e263623cf6ddcd235ed5f2310-dirty
jjohnson
parents:
4
diff
changeset
|
40 pkey_autoincr : 'id' |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
41 comment_lines : 1 |
5
19ae309ec53c
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 29288f94a382686e263623cf6ddcd235ed5f2310-dirty
jjohnson
parents:
4
diff
changeset
|
42 unique: ['c1'], |
19ae309ec53c
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 29288f94a382686e263623cf6ddcd235ed5f2310-dirty
jjohnson
parents:
4
diff
changeset
|
43 index: ['c2','c3'] |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
44 }, |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
45 { file_path : '/home/galaxy/dataset_102.dat', |
3
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
46 table_name : 'gff', |
6 | 47 column_names : ['seqname',,'date','start','end'] |
3
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
48 comment_lines : 1 |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
49 load_named_columns : True |
6 | 50 filters : [{'filter': 'regex', 'pattern': '#peptide', 'action': 'exclude_match'}, |
51 {'filter': 'replace', 'column': 3, 'replace': 'gi[|]', 'pattern': ''}] | |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
52 }, |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
53 { file_path : '/home/galaxy/dataset_103.dat', |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
54 table_name : 'test', |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
55 column_names : ['c1', 'c2', 'c3'] |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
56 } |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
57 ] |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
58 } |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
59 """ |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
60 |
6 | 61 |
62 class LineFilter( object ): | |
63 def __init__(self,source,filter_dict): | |
64 self.source = source | |
65 self.filter_dict = filter_dict | |
8 | 66 # print >> sys.stderr, 'LineFilter %s' % filter_dict if filter_dict else 'NONE' |
11 | 67 self.func = lambda i,l: l.rstrip('\r\n') if l else None |
68 self.src_lines = [] | |
69 self.src_line_cnt = 0 | |
6 | 70 if not filter_dict: |
71 return | |
72 if filter_dict['filter'] == 'regex': | |
73 rgx = re.compile(filter_dict['pattern']) | |
74 if filter_dict['action'] == 'exclude_match': | |
11 | 75 self.func = lambda i,l: l if not rgx.match(l) else None |
6 | 76 elif filter_dict['action'] == 'include_match': |
11 | 77 self.func = lambda i,l: l if rgx.match(l) else None |
6 | 78 elif filter_dict['action'] == 'exclude_find': |
11 | 79 self.func = lambda i,l: l if not rgx.search(l) else None |
6 | 80 elif filter_dict['action'] == 'include_find': |
11 | 81 self.func = lambda i,l: l if rgx.search(l) else None |
6 | 82 elif filter_dict['filter'] == 'replace': |
83 p = filter_dict['pattern'] | |
84 r = filter_dict['replace'] | |
85 c = int(filter_dict['column']) - 1 | |
11 | 86 self.func = lambda i,l: '\t'.join([x if i != c else re.sub(p,r,x) for i,x in enumerate(l.split('\t'))]) |
87 elif filter_dict['filter'] == 'prepend_line_num': | |
88 self.func = lambda i,l: '%d\t%s' % (i,l) | |
89 elif filter_dict['filter'] == 'append_line_num': | |
90 self.func = lambda i,l: '%s\t%d' % (l.rstrip('\r\n'),i) | |
91 elif filter_dict['filter'] == 'skip': | |
92 cnt = filter_dict['count'] | |
93 self.func = lambda i,l: l if i > cnt else None | |
94 elif filter_dict['filter'] == 'normalize': | |
95 cols = [int(c) - 1 for c in filter_dict['columns']] | |
96 sep = filter_dict['separator'] | |
97 self.func = lambda i,l: self.normalize(l,cols,sep) | |
6 | 98 def __iter__(self): |
99 return self | |
11 | 100 def normalize(self,line,split_cols,sep): |
101 lines = [] | |
102 fields = line.rstrip('\r\n').split('\t') | |
103 split_fields = dict() | |
104 cnt = 0 | |
105 for c in split_cols: | |
106 if c < len(fields): | |
107 split_fields[c] = fields[c].split(sep) | |
108 cnt = max(cnt, len(split_fields[c])) | |
109 if cnt == 0: | |
110 lines.append('\t'.join(fields)) | |
111 else: | |
112 for n in range(0, cnt): | |
113 flds = [x if c not in split_cols else split_fields[c][n] if n < len(split_fields[c]) else '' for (c, x) in enumerate(fields)] | |
114 lines.append('\t'.join(flds)) | |
115 return lines | |
116 def get_lines(self): | |
6 | 117 for i,next_line in enumerate(self.source): |
11 | 118 self.src_line_cnt += 1 |
119 line = self.func(self.src_line_cnt,next_line) | |
120 # print >> sys.stderr, 'LineFilter %s: %d %s' % (str(self.filter_dict),self.src_line_cnt,line) | |
6 | 121 if line: |
11 | 122 if isinstance(line,list): |
123 self.src_lines.extend(line) | |
124 else: | |
125 self.src_lines.append(line) | |
126 return | |
127 def next(self): | |
128 if not self.src_lines: | |
129 self.get_lines() | |
130 if self.src_lines: | |
131 return self.src_lines.pop(0) | |
6 | 132 raise StopIteration |
133 | |
134 | |
135 class TabularReader: | |
136 """ | |
137 Tabular file iterator. Returns a list | |
138 """ | |
139 def __init__(self, file_path, skip=0, comment_char=None, col_idx=None, filters=None): | |
140 self.skip = skip | |
141 self.comment_char = comment_char | |
142 self.col_idx = col_idx | |
143 self.filters = filters | |
144 self.tsv_file = open(file_path) | |
145 if skip and skip > 0: | |
9 | 146 for i in range(skip): |
6 | 147 if not self.tsv_file.readline(): |
148 break | |
149 source = LineFilter(self.tsv_file,None) | |
150 if comment_char: | |
151 source = LineFilter(source,{"filter": "regex", "pattern": comment_char, "action": "exclude_match"}) | |
152 if filters: | |
153 for f in filters: | |
154 source = LineFilter(source,f) | |
155 self.source = source | |
156 def __iter__(self): | |
157 return self | |
158 def next(self): | |
159 ''' Iteration ''' | |
160 for i,line in enumerate(self.source): | |
161 fields = line.rstrip('\r\n').split('\t') | |
162 if self.col_idx: | |
163 fields = [fields[i] for i in self.col_idx] | |
164 return fields | |
165 raise StopIteration | |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
166 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
167 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
168 def getValueType(val): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
169 if val or 0. == val: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
170 try: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
171 int(val) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
172 return 'INTEGER' |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
173 except: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
174 try: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
175 float(val) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
176 return 'REAL' |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
177 except: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
178 return 'TEXT' |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
179 return None |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
180 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
181 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
182 def get_column_def(file_path, table_name, skip=0, comment_char='#', |
6 | 183 column_names=None, max_lines=100,load_named_columns=False,filters=None): |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
184 col_pref = ['TEXT', 'REAL', 'INTEGER', None] |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
185 col_types = [] |
3
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
186 col_idx = None |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
187 data_lines = 0 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
188 try: |
6 | 189 tr = TabularReader(file_path,skip=skip, comment_char=comment_char, col_idx=None, filters=filters) |
190 for linenum, fields in enumerate(tr): | |
191 if linenum > max_lines: | |
192 break | |
193 try: | |
194 while len(col_types) < len(fields): | |
195 col_types.append(None) | |
196 for i, val in enumerate(fields): | |
197 colType = getValueType(val) | |
198 if col_pref.index(colType) < col_pref.index(col_types[i]): | |
199 col_types[i] = colType | |
200 except Exception, e: | |
201 print >> sys.stderr, 'Failed at line: %d err: %s' % (linenum, e) | |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
202 except Exception, e: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
203 print >> sys.stderr, 'Failed: %s' % (e) |
3
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
204 for i,col_type in enumerate(col_types): |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
205 if not col_type: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
206 col_types[i] = 'TEXT' |
3
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
207 if column_names: |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
208 col_names = [] |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
209 if load_named_columns: |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
210 col_idx = [] |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
211 for i, cname in enumerate([cn.strip() for cn in column_names.split(',')]): |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
212 if cname != '': |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
213 col_idx.append(i) |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
214 col_names.append(cname) |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
215 col_types = [col_types[i] for i in col_idx] |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
216 else: |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
217 col_names = ['c%d' % i for i in range(1, len(col_types) + 1)] |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
218 for i, cname in enumerate([cn.strip() for cn in column_names.split(',')]): |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
219 if cname and i < len(col_names): |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
220 col_names[i] = cname |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
221 else: |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
222 col_names = ['c%d' % i for i in range(1, len(col_types) + 1)] |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
223 col_def = [] |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
224 for i, col_name in enumerate(col_names): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
225 col_def.append('%s %s' % (col_names[i], col_types[i])) |
3
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
226 return col_names, col_types, col_def, col_idx |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
227 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
228 |
6 | 229 def create_table(conn, file_path, table_name, skip=0, comment_char='#', pkey_autoincr=None, column_names=None,load_named_columns=False,filters=None,unique_indexes=[],indexes=[]): |
230 | |
231 col_names, col_types, col_def, col_idx = get_column_def(file_path, table_name, skip=skip, comment_char=comment_char, | |
232 column_names=column_names,load_named_columns=load_named_columns,filters=filters) | |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
233 col_func = [float if t == 'REAL' else int if t == 'INTEGER' else str for t in col_types] |
5
19ae309ec53c
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 29288f94a382686e263623cf6ddcd235ed5f2310-dirty
jjohnson
parents:
4
diff
changeset
|
234 table_def = 'CREATE TABLE %s (\n %s%s\n);' % ( |
6 | 235 table_name, |
5
19ae309ec53c
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 29288f94a382686e263623cf6ddcd235ed5f2310-dirty
jjohnson
parents:
4
diff
changeset
|
236 '%s INTEGER PRIMARY KEY AUTOINCREMENT,' % pkey_autoincr if pkey_autoincr else '', |
19ae309ec53c
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 29288f94a382686e263623cf6ddcd235ed5f2310-dirty
jjohnson
parents:
4
diff
changeset
|
237 ', \n '.join(col_def)) |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
238 # print >> sys.stdout, table_def |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
239 insert_stmt = 'INSERT INTO %s(%s) VALUES(%s)' % (table_name, ','.join(col_names), ','.join(["?" for x in col_names])) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
240 # print >> sys.stdout, insert_stmt |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
241 data_lines = 0 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
242 try: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
243 c = conn.cursor() |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
244 c.execute(table_def) |
4
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
245 conn.commit() |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
246 c.close() |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
247 for i,index in enumerate(unique_indexes): |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
248 index_name='idx_uniq_%s_%d' % (table_name,i) |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
249 index_columns = index.split(',') |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
250 create_index(conn, table_name, index_name, index_columns,unique=True) |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
251 for i,index in enumerate(indexes): |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
252 index_name='idx_%s_%d' % (table_name,i) |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
253 index_columns = index.split(',') |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
254 create_index(conn, table_name, index_name, index_columns) |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
255 c = conn.cursor() |
6 | 256 tr = TabularReader(file_path,skip=skip, comment_char=comment_char, col_idx=col_idx, filters=filters) |
257 for linenum, fields in enumerate(tr): | |
258 data_lines += 1 | |
259 try: | |
260 vals = [col_func[i](x) if x else None for i, x in enumerate(fields)] | |
261 c.execute(insert_stmt, vals) | |
262 except Exception, e: | |
263 print >> sys.stderr, 'Failed at line: %d err: %s' % (linenum, e) | |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
264 conn.commit() |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
265 c.close() |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
266 except Exception, e: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
267 print >> sys.stderr, 'Failed: %s' % (e) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
268 exit(1) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
269 |
6 | 270 |
4
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
271 def create_index(conn, table_name, index_name, index_columns, unique=False): |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
272 index_def = "CREATE %s INDEX %s on %s(%s)" % ('UNIQUE' if unique else '', index_name, table_name, ','.join(index_columns)) |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
273 c = conn.cursor() |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
274 c.execute(index_def) |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
275 conn.commit() |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
276 c.close() |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
277 |
6 | 278 |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
279 def regex_match(expr, item): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
280 return re.match(expr, item) is not None |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
281 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
282 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
283 def regex_search(expr, item): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
284 return re.search(expr, item) is not None |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
285 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
286 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
287 def regex_sub(expr, replace, item): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
288 return re.sub(expr, replace, item) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
289 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
290 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
291 def get_connection(sqlitedb_path, addfunctions=False): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
292 conn = sqlite.connect(sqlitedb_path) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
293 if addfunctions: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
294 conn.create_function("re_match", 2, regex_match) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
295 conn.create_function("re_search", 2, regex_search) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
296 conn.create_function("re_sub", 3, regex_sub) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
297 return conn |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
298 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
299 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
300 def __main__(): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
301 # Parse Command Line |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
302 parser = optparse.OptionParser() |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
303 parser.add_option('-s', '--sqlitedb', dest='sqlitedb', default=None, help='The SQLite Database') |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
304 parser.add_option('-t', '--table', dest='tables', action="append", default=[], help='Tabular file: file_path[=table_name[:column_name, ...]') |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
305 parser.add_option('-j', '--jsonfile', dest='jsonfile', default=None, help='Tabular file: file_path[=table_name[:column_name, ...]') |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
306 parser.add_option('-q', '--query', dest='query', default=None, help='SQL query') |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
307 parser.add_option('-Q', '--query_file', dest='query_file', default=None, help='SQL query file') |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
308 parser.add_option('-n', '--no_header', dest='no_header', action='store_true', default=False, help='Include a column headers line') |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
309 parser.add_option('-o', '--output', dest='output', default=None, help='Output file for query results') |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
310 (options, args) = parser.parse_args() |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
311 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
312 # open sqlite connection |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
313 conn = get_connection(options.sqlitedb) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
314 # determine output destination |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
315 if options.output is not None: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
316 try: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
317 outputPath = os.path.abspath(options.output) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
318 outputFile = open(outputPath, 'w') |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
319 except Exception, e: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
320 print >> sys.stderr, "failed: %s" % e |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
321 exit(3) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
322 else: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
323 outputFile = sys.stdout |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
324 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
325 # get table defs |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
326 if options.tables: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
327 for ti, table in enumerate(options.tables): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
328 table_name = 't%d' % (ti + 1) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
329 column_names = None |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
330 fields = table.split('=') |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
331 path = fields[0] |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
332 if len(fields) > 1: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
333 names = fields[1].split(':') |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
334 table_name = names[0] if names[0] else table_name |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
335 if len(names) > 1: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
336 column_names = names[1] |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
337 # print >> sys.stdout, '%s %s' % (table_name, path) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
338 create_table(conn, path, table_name, column_names=column_names) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
339 if options.jsonfile: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
340 try: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
341 fh = open(options.jsonfile) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
342 tdef = json.load(fh) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
343 if 'tables' in tdef: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
344 for ti, table in enumerate(tdef['tables']): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
345 path = table['file_path'] |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
346 table_name = table['table_name'] if 'table_name' in table else 't%d' % (ti + 1) |
3
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
347 comment_lines = table['comment_lines'] if 'comment_lines' in table else 0 |
6 | 348 comment_char = table['comment_char'] if 'comment_char' in table else None |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
349 column_names = table['column_names'] if 'column_names' in table else None |
3
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
350 if column_names: |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
351 load_named_columns = table['load_named_columns'] if 'load_named_columns' in table else False |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
352 else: |
3e3b3c883bec
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 60d1a49c09f87c1c1ec6fecbe54aa226bdc695a7-dirty
jjohnson
parents:
0
diff
changeset
|
353 load_named_columns = False |
4
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
354 unique_indexes = table['unique'] if 'unique' in table else [] |
9d73dca48178
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9893037a046e4f4c1e7ba859e05f49378c398cb1-dirty
jjohnson
parents:
3
diff
changeset
|
355 indexes = table['index'] if 'index' in table else [] |
6 | 356 filters = table['filters'] if 'filters' in table else None |
5
19ae309ec53c
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 29288f94a382686e263623cf6ddcd235ed5f2310-dirty
jjohnson
parents:
4
diff
changeset
|
357 pkey_autoincr = table['pkey_autoincr'] if 'pkey_autoincr' in table else None |
19ae309ec53c
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 29288f94a382686e263623cf6ddcd235ed5f2310-dirty
jjohnson
parents:
4
diff
changeset
|
358 create_table(conn, path, table_name, pkey_autoincr=pkey_autoincr, column_names=column_names, |
6 | 359 skip=comment_lines, comment_char=comment_char, load_named_columns=load_named_columns, |
360 filters=filters,unique_indexes=unique_indexes, indexes=indexes) | |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
361 except Exception, exc: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
362 print >> sys.stderr, "Error: %s" % exc |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
363 conn.close() |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
364 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
365 query = None |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
366 if (options.query_file is not None): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
367 with open(options.query_file, 'r') as fh: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
368 query = '' |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
369 for line in fh: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
370 query += line |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
371 elif (options.query is not None): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
372 query = options.query |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
373 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
374 if (query is None): |
6 | 375 tables_query = \ |
376 "SELECT name, sql FROM sqlite_master WHERE type='table' ORDER BY name" | |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
377 try: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
378 conn = get_connection(options.sqlitedb) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
379 c = conn.cursor() |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
380 rslt = c.execute(tables_query).fetchall() |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
381 for table, sql in rslt: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
382 print >> sys.stderr, "Table %s:" % table |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
383 try: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
384 col_query = 'SELECT * FROM %s LIMIT 0' % table |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
385 cur = conn.cursor().execute(col_query) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
386 cols = [col[0] for col in cur.description] |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
387 print >> sys.stderr, " Columns: %s" % cols |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
388 except Exception, exc: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
389 print >> sys.stderr, "Error: %s" % exc |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
390 except Exception, exc: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
391 print >> sys.stderr, "Error: %s" % exc |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
392 exit(0) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
393 # if not sqlite.is_read_only_query(query): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
394 # print >> sys.stderr, "Error: Must be a read only query" |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
395 # exit(2) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
396 try: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
397 conn = get_connection(options.sqlitedb, addfunctions=True) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
398 cur = conn.cursor() |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
399 results = cur.execute(query) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
400 if not options.no_header: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
401 outputFile.write("#%s\n" % '\t'.join([str(col[0]) for col in cur.description])) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
402 # yield [col[0] for col in cur.description] |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
403 for i, row in enumerate(results): |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
404 # yield [val for val in row] |
10 | 405 outputFile.write("%s\n" % '\t'.join([str(val) if val is not None else '' for val in row])) |
0
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
406 except Exception, exc: |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
407 print >> sys.stderr, "Error: %s" % exc |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
408 exit(1) |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
409 |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
410 if __name__ == "__main__": |
926c62f7fa09
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/query_tabular commit 9ae87502ea7c3da33ecc453872c4eb2f41ecea4a-dirty
jjohnson
parents:
diff
changeset
|
411 __main__() |