|
| 1 | +Galaxy API Documentation |
| 2 | +************************ |
| 3 | + |
| 4 | +Background |
| 5 | +========== |
| 6 | +In addition to being accessible through a web interface, Galaxy can also be |
| 7 | +accessed programmatically, through shell scripts and other programs. The web |
| 8 | +interface is appropriate for things like exploratory analysis, visualization, |
| 9 | +construction of workflows, and rerunning workflows on new datasets. |
| 10 | + |
| 11 | +The web interface is less suitable for things like |
| 12 | + - Connecting a Galaxy instance directly to your sequencer and running |
| 13 | + workflows whenever data is ready. |
| 14 | + - Running a workflow against multiple datasets (which can be done with the |
| 15 | + web interface, but is tedious). |
| 16 | + - When the analysis involves complex control, such as looping and |
| 17 | + branching. |
| 18 | + |
| 19 | +The Galaxy API addresses these and other situations by exposing Galaxy |
| 20 | +internals through an additional interface, known as an Application Programming |
| 21 | +Interface, or API. |
| 22 | + |
| 23 | +Quickstart |
| 24 | +========== |
| 25 | + |
| 26 | +Log in as your user, navigate to the API Keys page in the User menu, and |
| 27 | +generate a new API key. Make a note of the API key, and then pull up a |
| 28 | +terminal. Now we'll use the display.py script in your galaxy/scripts/api |
| 29 | +directory for a short example:: |
| 30 | + |
| 31 | + % ./display.py my_key http://localhost:4096/api/histories |
| 32 | + Collection Members |
| 33 | + ------------------ |
| 34 | + #1: /api/histories/8c49be448cfe29bc |
| 35 | + name: Unnamed history |
| 36 | + id: 8c49be448cfe29bc |
| 37 | + #2: /api/histories/33b43b4e7093c91f |
| 38 | + name: output test |
| 39 | + id: 33b43b4e7093c91f |
| 40 | + |
| 41 | +The result is a Collection of the histories of the user specified by the API |
| 42 | +key (you). To look at the details of a particular history, say #1 above, do |
| 43 | +the following:: |
| 44 | + |
| 45 | + % ./display.py my_key http://localhost:4096/api/histories/8c49be448cfe29bc |
| 46 | + Member Information |
| 47 | + ------------------ |
| 48 | + state_details: {'ok': 1, 'failed_metadata': 0, 'upload': 0, 'discarded': 0, 'running': 0, 'setting_metadata': 0, 'error': 0, 'new': 0, 'queued': 0, 'empty': 0} |
| 49 | + state: ok |
| 50 | + contents_url: /api/histories/8c49be448cfe29bc/contents |
| 51 | + id: 8c49be448cfe29bc |
| 52 | + name: Unnamed history |
| 53 | + |
| 54 | +This gives detailed information about the specific member in question, in this |
| 55 | +case the History. To view history contents, do the following:: |
| 56 | + |
| 57 | + |
| 58 | + % ./display.py my_key http://localhost:4096/api/histories/8c49be448cfe29bc/contents |
| 59 | + Collection Members |
| 60 | + ------------------ |
| 61 | + #1: /api/histories/8c49be448cfe29bc/contents/6f91353f3eb0fa4a |
| 62 | + name: Pasted Entry |
| 63 | + type: file |
| 64 | + id: 6f91353f3eb0fa4a |
| 65 | + |
| 66 | +What we have here is another Collection of items containing all of the datasets |
| 67 | +in this particular history. Finally, to view details of a particular dataset |
| 68 | +in this collection, execute the following:: |
| 69 | + |
| 70 | + % ./display.py my_key http://localhost:4096/api/histories/8c49be448cfe29bc/contents/6f91353f3eb0fa4a |
| 71 | + Member Information |
| 72 | + ------------------ |
| 73 | + misc_blurb: 1 line |
| 74 | + name: Pasted Entry |
| 75 | + data_type: txt |
| 76 | + deleted: False |
| 77 | + file_name: /Users/yoplait/work/galaxy-stock/database/files/000/dataset_82.dat |
| 78 | + state: ok |
| 79 | + download_url: /datasets/6f91353f3eb0fa4a/display?to_ext=txt |
| 80 | + visible: True |
| 81 | + genome_build: ? |
| 82 | + model_class: HistoryDatasetAssociation |
| 83 | + file_size: 17 |
| 84 | + metadata_data_lines: 1 |
| 85 | + id: 6f91353f3eb0fa4a |
| 86 | + misc_info: uploaded txt file |
| 87 | + metadata_dbkey: ? |
| 88 | + |
| 89 | +And now you've successfully used the API to request and select a history, |
| 90 | +browse the contents of that history, and then look at detailed information |
| 91 | +about a particular dataset. |
| 92 | + |
| 93 | +For a more comprehensive Data Library example, set the following option in your |
| 94 | +galaxy.ini as well, and restart galaxy again:: |
| 95 | + |
| 96 | + admin_users = you@example.org |
| 97 | + library_import_dir = /path/to/some/directory |
| 98 | + |
| 99 | +In the directory you specified for 'library_import_dir', create some |
| 100 | +subdirectories, and put (or symlink) files to import into Galaxy into those |
| 101 | +subdirectories. |
| 102 | + |
| 103 | +In Galaxy, create an account that matches the address you put in 'admin_users', |
| 104 | +then browse to that user's preferences and generate a new API Key. Copy the |
| 105 | +key to your clipboard and then use these scripts:: |
| 106 | + |
| 107 | + % ./display.py my_key http://localhost:4096/api/libraries |
| 108 | + Collection Members |
| 109 | + ------------------ |
| 110 | + |
| 111 | + 0 elements in collection |
| 112 | + |
| 113 | + % ./library_create_library.py my_key http://localhost:4096/api/libraries api_test 'API Test Library' |
| 114 | + Response |
| 115 | + -------- |
| 116 | + /api/libraries/f3f73e481f432006 |
| 117 | + name: api_test |
| 118 | + id: f3f73e481f432006 |
| 119 | + |
| 120 | + % ./display.py my_key http://localhost:4096/api/libraries |
| 121 | + Collection Members |
| 122 | + ------------------ |
| 123 | + /api/libraries/f3f73e481f432006 |
| 124 | + name: api_test |
| 125 | + id: f3f73e481f432006 |
| 126 | + |
| 127 | + % ./display.py my_key http://localhost:4096/api/libraries/f3f73e481f432006 |
| 128 | + Member Information |
| 129 | + ------------------ |
| 130 | + synopsis: None |
| 131 | + contents_url: /api/libraries/f3f73e481f432006/contents |
| 132 | + description: API Test Library |
| 133 | + name: api_test |
| 134 | + |
| 135 | + % ./display.py my_key http://localhost:4096/api/libraries/f3f73e481f432006/contents |
| 136 | + Collection Members |
| 137 | + ------------------ |
| 138 | + /api/libraries/f3f73e481f432006/contents/28202595c0d2591f61ddda595d2c3670 |
| 139 | + name: / |
| 140 | + type: folder |
| 141 | + id: 28202595c0d2591f61ddda595d2c3670 |
| 142 | + |
| 143 | + % ./library_create_folder.py my_key http://localhost:4096/api/libraries/f3f73e481f432006/contents 28202595c0d2591f61ddda595d2c3670 api_test_folder1 'API Test Folder 1' |
| 144 | + Response |
| 145 | + -------- |
| 146 | + /api/libraries/f3f73e481f432006/contents/28202595c0d2591fa4f9089d2303fd89 |
| 147 | + name: api_test_folder1 |
| 148 | + id: 28202595c0d2591fa4f9089d2303fd89 |
| 149 | + |
| 150 | + % ./library_upload_from_import_dir.py my_key http://localhost:4096/api/libraries/f3f73e481f432006/contents 28202595c0d2591fa4f9089d2303fd89 bed bed hg19 |
| 151 | + Response |
| 152 | + -------- |
| 153 | + /api/libraries/f3f73e481f432006/contents/e9ef7fdb2db87d7b |
| 154 | + name: 2.bed |
| 155 | + id: e9ef7fdb2db87d7b |
| 156 | + /api/libraries/f3f73e481f432006/contents/3b7f6a31f80a5018 |
| 157 | + name: 3.bed |
| 158 | + id: 3b7f6a31f80a5018 |
| 159 | + |
| 160 | + % ./display.py my_key http://localhost:4096/api/libraries/f3f73e481f432006/contents |
| 161 | + Collection Members |
| 162 | + ------------------ |
| 163 | + /api/libraries/f3f73e481f432006/contents/28202595c0d2591f61ddda595d2c3670 |
| 164 | + name: / |
| 165 | + type: folder |
| 166 | + id: 28202595c0d2591f61ddda595d2c3670 |
| 167 | + /api/libraries/f3f73e481f432006/contents/28202595c0d2591fa4f9089d2303fd89 |
| 168 | + name: /api_test_folder1 |
| 169 | + type: folder |
| 170 | + id: 28202595c0d2591fa4f9089d2303fd89 |
| 171 | + /api/libraries/f3f73e481f432006/contents/e9ef7fdb2db87d7b |
| 172 | + name: /api_test_folder1/2.bed |
| 173 | + type: file |
| 174 | + id: e9ef7fdb2db87d7b |
| 175 | + /api/libraries/f3f73e481f432006/contents/3b7f6a31f80a5018 |
| 176 | + name: /api_test_folder1/3.bed |
| 177 | + type: file |
| 178 | + id: 3b7f6a31f80a5018 |
| 179 | + |
| 180 | + % ./display.py my_key http://localhost:4096/api/libraries/f3f73e481f432006/contents/e9ef7fdb2db87d7b |
| 181 | + Member Information |
| 182 | + ------------------ |
| 183 | + misc_blurb: 68 regions |
| 184 | + metadata_endCol: 3 |
| 185 | + data_type: bed |
| 186 | + metadata_columns: 6 |
| 187 | + metadata_nameCol: 4 |
| 188 | + uploaded_by: nate@... |
| 189 | + metadata_strandCol: 6 |
| 190 | + name: 2.bed |
| 191 | + genome_build: hg19 |
| 192 | + metadata_comment_lines: None |
| 193 | + metadata_startCol: 2 |
| 194 | + metadata_chromCol: 1 |
| 195 | + file_size: 4272 |
| 196 | + metadata_data_lines: 68 |
| 197 | + message: |
| 198 | + metadata_dbkey: hg19 |
| 199 | + misc_info: uploaded bed file |
| 200 | + date_uploaded: 2010-06-22T17:01:51.266119 |
| 201 | + metadata_column_types: str, int, int, str, int, str |
| 202 | + |
| 203 | +Other parameters are valid when uploading, they are the same parameters as are |
| 204 | +used in the web form, like 'link_data_only' and etc. |
| 205 | + |
| 206 | +The request and response format should be considered alpha and are subject to change. |
| 207 | + |
| 208 | +API Design Guidelines |
| 209 | +===================== |
| 210 | + |
| 211 | +The following section outlines guidelines related to extending and/or modifing |
| 212 | +the Galaxy API. The Galaxy API has grown in an ad-hoc fashion over time by |
| 213 | +many contributors and so clients SHOULD NOT expect the API will conform to |
| 214 | +these guidelines - but developers contributing to the Galaxy API SHOULD follow |
| 215 | +these guidelines. |
| 216 | + |
| 217 | + - API functionality should include docstring documentation for consumption |
| 218 | + by readthedocs.org. |
| 219 | + - Developers should familiarize themselves with the HTTP status code definitions |
| 220 | + http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. The API responses |
| 221 | + should properly set the status code according to the result - in particular |
| 222 | + 2XX responses should be used for successful requests, 4XX for various |
| 223 | + kinds of client errors, and 5XX for the errors on the server side. |
| 224 | + - If there is an error processing some part of request (one item in a list |
| 225 | + for instance), the status code should be set to reflect the error and the |
| 226 | + partial result may or may not be returned depending on the controller - |
| 227 | + this behavior should be documented. |
| 228 | + - API methods should throw a finite number of exceptions |
| 229 | + (defined in :doc:`galaxy.exceptions`) and these should subclass |
| 230 | + `MessageException` and not paste/wsgi HTTP exceptions. When possible, |
| 231 | + the framework itself should be responsible catching these exceptions, |
| 232 | + setting the status code, and building an error response. |
| 233 | + - Error responses should not consist of plain text strings - they should be |
| 234 | + dictionaries describing the error and containing the following:: |
| 235 | + |
| 236 | + { |
| 237 | + "status_code": 400, |
| 238 | + "err_code": 400007, |
| 239 | + "err_msg": "Request contained invalid parameter, action could not be completed.", |
| 240 | + "type": "error", |
| 241 | + "extra_error_info": "Extra information." |
| 242 | + } |
| 243 | + |
| 244 | + Various error conditions (once a format has been chosen and framework to |
| 245 | + enforce it in place) should be spelled out in this document. |
| 246 | + - Backward compatibility is important and should be maintained when possible. |
| 247 | + If changing behavior in a non-backward compatibile way please ensure one |
| 248 | + of the following holds - there is a strong reason to believe no consumers |
| 249 | + depend on a behavior, the behavior is effectively broken, or the API |
| 250 | + method being modified has not been part of a tagged dist release. |
| 251 | + |
| 252 | +The following bullet points represent good practices more than guidelines, please |
| 253 | +consider them when modifying the API. |
| 254 | + |
| 255 | + - Functionality should not be copied and pasted between controllers - |
| 256 | + consider refactoring functionality into associated classes or short of |
| 257 | + that into Mixins (http://en.wikipedia.org/wiki/Composition_over_inheritance) |
| 258 | + or into Managers (:doc:`galaxy.managers`). |
| 259 | + - API additions are more permanent changes to Galaxy than many other potential |
| 260 | + changes and so a second opinion on API changes should be sought. (Consider a |
| 261 | + pull request!) |
| 262 | + - New API functionality should include functional tests. These functional |
| 263 | + tests should be implemented in Python and placed in |
| 264 | + `test/functional/api`. (Once such a framework is in place - it is not |
| 265 | + right now). |
| 266 | + - Changes to reflect modifications to the API should be pushed upstream to |
| 267 | + the BioBlend project if possible. |
| 268 | + |
| 269 | +Longer term goals/notes. |
| 270 | + |
| 271 | + - It would be advantageous to have a clearer separation of anonymous and |
| 272 | + admin handling functionality. |
| 273 | + - If at some point in the future, functionality needs to be added that |
| 274 | + breaks backward compatibility in a significant way to a component used by |
| 275 | + the community - a "dev" variant of the API will be established and |
| 276 | + the community should be alerted and given a timeframe for when the old |
| 277 | + behavior will be replaced with the new behavior. |
| 278 | + - Consistent standards for range-based requests, batch requests, filtered |
| 279 | + requests, etc... should be established and documented here. |
| 280 | + |
| 281 | + |
| 282 | +.. include:: lib/galaxy.webapps.galaxy.api.rst |
0 commit comments