Mercurial > repos > jjohnson > query_tabular
comparison query_tabular.xml @ 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 <tool id="query_tabular" name="Query Tabular" version="0.1.0"> | |
| 2 <description>using sqlite sql</description> | |
| 3 | |
| 4 <requirements> | |
| 5 </requirements> | |
| 6 <stdio> | |
| 7 <exit_code range="1:" /> | |
| 8 </stdio> | |
| 9 | |
| 10 <command interpreter="python"><![CDATA[ | |
| 11 query_tabular.py | |
| 12 #if $save_db | |
| 13 -s $sqlitedb | |
| 14 #else | |
| 15 -s $workdb | |
| 16 #end if | |
| 17 #for $i,$tbl in enumerate($tables): | |
| 18 #if $tbl.table_name | |
| 19 #set $tname = $tbl.table_name | |
| 20 #else | |
| 21 #set $tname = 't' + str($i + 1) | |
| 22 #end if | |
| 23 #if $tbl.col_names: | |
| 24 #set $col_names = ':' + str($tbl.col_names) | |
| 25 #else | |
| 26 #set $col_names = '' | |
| 27 #end if | |
| 28 -t ${tbl.table}=${tname}${$col_names} | |
| 29 #end for | |
| 30 #if $query: | |
| 31 -q "$query" | |
| 32 -o $output | |
| 33 #end if | |
| 34 ]]></command> | |
| 35 <inputs> | |
| 36 <param name="workdb" type="hidden" value="workdb.sqlite" label=""/> | |
| 37 <repeat name="tables" title="Add tables" min="1"> | |
| 38 <param name="table" type="data" format="tabular" label="Dataset"/> | |
| 39 <param name="table_name" type="text" value="" optional="true" label="Table name"> | |
| 40 <help>By default, tables will be named: t1,t2,...,tn</help> | |
| 41 <validator type="regex" message="Table name should start with a letter and may contain additional letters, digits, and underscores">^[A-Za-z]\w*$</validator> | |
| 42 </param> | |
| 43 <!-- | |
| 44 <param name="sel_cols" label="Include columns" type="data_column" multiple="true" data_ref="table" /> | |
| 45 --> | |
| 46 <param name="col_names" type="text" value="" optional="true" label="Column names"> | |
| 47 <help>By default, table columns will be named: c1,c2,c3,...,cn</help> | |
| 48 <validator type="regex" message="A List of separated by commas: Column names should start with a letter and may contain additional letters, digits, and underscores">^([A-Za-z]\w*)?(,([A-Za-z]\w*)?)*$</validator> | |
| 49 </param> | |
| 50 </repeat> | |
| 51 <param name="query" type="text" area="true" size="10x80" value="" optional="true" label="SQL Query"> | |
| 52 <help>By default, tables will be named: t1,t2,...,tn</help> | |
| 53 <sanitizer sanitize="False"/> | |
| 54 <validator type="regex" message="">^(?i)\s*select\s+.*\s+from\s+.*$</validator> | |
| 55 </param> | |
| 56 <param name="save_db" type="boolean" truevalue="yes" falsevalue="no" checked="false" label="Save the sqlite database"/> | |
| 57 </inputs> | |
| 58 <outputs> | |
| 59 <data format="sqlite" name="sqlitedb" label="sqlite"> | |
| 60 <filter>save_db or not (query and len(query) > 0)</filter> | |
| 61 </data> | |
| 62 <data format="tabular" name="output" label="query results"> | |
| 63 <filter>query and len(query) > 0</filter> | |
| 64 </data> | |
| 65 </outputs> | |
| 66 <tests> | |
| 67 | |
| 68 <test> | |
| 69 <repeat name="tables"> | |
| 70 <param name="table" ftype="tabular" value="customers.tsv"/> | |
| 71 <param name="table_name" value="customers"/> | |
| 72 <param name="col_names" value="CustomerID,FirstName,LastName,Email,DOB,Phone"/> | |
| 73 </repeat> | |
| 74 <repeat name="tables"> | |
| 75 <param name="table" ftype="tabular" value="sales.tsv"/> | |
| 76 <param name="table_name" value="sales"/> | |
| 77 <param name="col_names" value="CustomerID,Date,SaleAmount"/> | |
| 78 </repeat> | |
| 79 <param name="query" value="SELECT FirstName,LastName,sum(SaleAmount) as "TotalSales" FROM customers join sales on customers.CustomerID = sales.CustomerID GROUP BY customers.CustomerID ORDER BY TotalSales DESC"/> | |
| 80 <output name="output" file="sales_results.tsv"/> | |
| 81 </test> | |
| 82 | |
| 83 <test> | |
| 84 <repeat name="tables"> | |
| 85 <param name="table" ftype="tabular" value="customers.tsv"/> | |
| 86 <param name="col_names" value=",FirstName,LastName,,DOB,"/> | |
| 87 </repeat> | |
| 88 <repeat name="tables"> | |
| 89 <param name="table" ftype="tabular" value="sales.tsv"/> | |
| 90 </repeat> | |
| 91 <param name="query" value="SELECT FirstName,LastName,sum(t2.c3) as "TotalSales" FROM t1 join t2 on t1.c1 = t2.c1 GROUP BY t1.c1 ORDER BY TotalSales DESC;"/> | |
| 92 <output name="output" file="sales_results.tsv"/> | |
| 93 </test> | |
| 94 | |
| 95 <test> | |
| 96 <repeat name="tables"> | |
| 97 <param name="table" ftype="tabular" value="IEDB.tsv"/> | |
| 98 <param name="table_name" value="iedb"/> | |
| 99 <param name="col_names" value="ID,allele,seq_num,start,end,length,peptide,method,percentile_rank,ann_ic50,ann_rank,smm_ic50,smm_rank,comblib_sidney2008_score,comblib_sidney2008_rank,netmhcpan_ic50,netmhcpan_rank"/> | |
| 100 </repeat> | |
| 101 <repeat name="tables"> | |
| 102 <param name="table" ftype="tabular" value="netMHC_summary.tsv"/> | |
| 103 <param name="table_name" value="mhc_summary"/> | |
| 104 <param name="col_names" value="pos,peptide,logscore,affinity,Bind_Level,Protein,Allele"/> | |
| 105 </repeat> | |
| 106 <param name="query" value="select iedb.ID,iedb.peptide,iedb.start,iedb.end,iedb.percentile_rank,mhc_summary.logscore,mhc_summary.affinity,mhc_summary.Bind_Level from iedb left outer join mhc_summary on iedb.peptide = mhc_summary.peptide order by affinity,Bind_Level"/> | |
| 107 <output name="output" file="query_results.tsv"/> | |
| 108 </test> | |
| 109 | |
| 110 </tests> | |
| 111 <help><![CDATA[ | |
| 112 ============= | |
| 113 Query Tabular | |
| 114 ============= | |
| 115 | |
| 116 **Inputs** | |
| 117 | |
| 118 Loads tabular datasets into a SQLite_ data base. | |
| 119 | |
| 120 **Outputs** | |
| 121 | |
| 122 The results of a SQL query are output to the history as a tabular file. | |
| 123 | |
| 124 The SQLite_ data base can also be saved and output as a dataset in the history. | |
| 125 | |
| 126 | |
| 127 For help in using SQLite_ see: http://www.sqlite.org/docs.html | |
| 128 | |
| 129 **NOTE:** input for SQLite dates input field must be in the format: *YYYY-MM-DD* for example: 2015-09-30 | |
| 130 | |
| 131 See: http://www.sqlite.org/lang_datefunc.html | |
| 132 | |
| 133 **Example** | |
| 134 | |
| 135 Given 2 tabular datasets: *customers* and *sales* | |
| 136 | |
| 137 Dataset *customers* | |
| 138 | |
| 139 Table name: "customers" | |
| 140 | |
| 141 Column names: "CustomerID,FirstName,LastName,Email,DOB,Phone" | |
| 142 | |
| 143 =========== ========== ========== ===================== ========== ============ | |
| 144 #CustomerID FirstName LastName Email DOB Phone | |
| 145 =========== ========== ========== ===================== ========== ============ | |
| 146 1 John Smith John.Smith@yahoo.com 1968-02-04 626 222-2222 | |
| 147 2 Steven Goldfish goldfish@fishhere.net 1974-04-04 323 455-4545 | |
| 148 3 Paula Brown pb@herowndomain.org 1978-05-24 416 323-3232 | |
| 149 4 James Smith jim@supergig.co.uk 1980-10-20 416 323-8888 | |
| 150 =========== ========== ========== ===================== ========== ============ | |
| 151 | |
| 152 Dataset *sales* | |
| 153 | |
| 154 Table name: "sales" | |
| 155 | |
| 156 Column names: "CustomerID,Date,SaleAmount" | |
| 157 | |
| 158 ============= ============ ============ | |
| 159 #CustomerID Date SaleAmount | |
| 160 ============= ============ ============ | |
| 161 2 2004-05-06 100.22 | |
| 162 1 2004-05-07 99.95 | |
| 163 3 2004-05-07 122.95 | |
| 164 3 2004-05-13 100.00 | |
| 165 4 2004-05-22 555.55 | |
| 166 ============= ============ ============ | |
| 167 | |
| 168 The query | |
| 169 | |
| 170 :: | |
| 171 | |
| 172 SELECT FirstName,LastName,sum(SaleAmount) as "TotalSales" | |
| 173 FROM customers join sales on customers.CustomerID = sales.CustomerID | |
| 174 GROUP BY customers.CustomerID ORDER BY TotalSales DESC; | |
| 175 | |
| 176 Produces this tabular output: | |
| 177 | |
| 178 ========== ======== ========== | |
| 179 #FirstName LastName TotalSales | |
| 180 ========== ======== ========== | |
| 181 James Smith 555.55 | |
| 182 Paula Brown 222.95 | |
| 183 Steven Goldfish 100.22 | |
| 184 John Smith 99.95 | |
| 185 ========== ======== ========== | |
| 186 | |
| 187 | |
| 188 If the optional Table name and Column names inputs are not used, the query would be: | |
| 189 | |
| 190 :: | |
| 191 | |
| 192 SELECT t1.c2 as "FirstName", t1.c3 as "LastName", sum(t2.c3) as "TotalSales" | |
| 193 FROM t1 join t2 on t1.c1 = t2.c1 | |
| 194 GROUP BY t1.c1 ORDER BY TotalSales DESC; | |
| 195 | |
| 196 You can selectively name columns, e.g. on the customers input you could just name columns 2,3, and 5: | |
| 197 | |
| 198 Column names: ,FirstName,LastName,,BirthDate | |
| 199 | |
| 200 Results in the following data base table | |
| 201 | |
| 202 =========== ========== ========== ===================== ========== ============ | |
| 203 #c1 FirstName LastName c4 BithDate c6 | |
| 204 =========== ========== ========== ===================== ========== ============ | |
| 205 1 John Smith John.Smith@yahoo.com 1968-02-04 626 222-2222 | |
| 206 2 Steven Goldfish goldfish@fishhere.net 1974-04-04 323 455-4545 | |
| 207 3 Paula Brown pb@herowndomain.org 1978-05-24 416 323-3232 | |
| 208 4 James Smith jim@supergig.co.uk 1980-10-20 416 323-8888 | |
| 209 =========== ========== ========== ===================== ========== ============ | |
| 210 | |
| 211 .. _SQLite: http://www.sqlite.org/index.html | |
| 212 | |
| 213 ]]></help> | |
| 214 </tool> |
