Skip to content

Commit

Permalink
feat(python) : added a bunch more python
Browse files Browse the repository at this point in the history
Left out the Pretty parameter examples, but
could add those in if desired
  • Loading branch information
majensen committed Jan 26, 2016
1 parent bde6db9 commit e8af85e
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 14 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ gem 'rouge', '~> 1.9.0'
gem 'redcarpet', '~> 3.3.2'
gem "pdfkit", "~> 0.8.2"
gem "wkhtmltopdf-binary", "~> 0.9.9.3"
gem 'middleman-livereload', '~> 3.4.6'
gem 'rake', '~> 10.4.2'
gem 'therubyracer', '~> 0.12.1', platforms: :ruby
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ GEM
sass (>= 3.3.0, < 3.5)
compass-import-once (1.0.5)
sass (>= 3.2, < 3.5)
em-websocket (0.5.1)
eventmachine (>= 0.12.9)
http_parser.rb (~> 0.6.0)
erubis (2.7.0)
eventmachine (1.0.9.1)
execjs (2.5.2)
ffi (1.9.8)
haml (4.0.6)
Expand All @@ -38,6 +42,7 @@ GEM
hitimes (1.2.2)
hooks (0.4.0)
uber (~> 0.0.4)
http_parser.rb (0.6.0)
i18n (0.7.0)
json (1.8.3)
kramdown (1.7.0)
Expand Down Expand Up @@ -74,6 +79,10 @@ GEM
tilt (~> 1.4.1, < 2.0)
middleman-gh-pages (0.0.3)
rake (> 0.9.3)
middleman-livereload (3.4.6)
em-websocket (~> 0.5.1)
middleman-core (>= 3.3)
rack-livereload (~> 0.3.15)
middleman-sprockets (3.4.2)
middleman-core (>= 3.3)
sprockets (~> 2.12.1)
Expand All @@ -92,6 +101,8 @@ GEM
activesupport (>= 3.1)
pdfkit (0.8.2)
rack (1.6.4)
rack-livereload (0.3.16)
rack
rack-test (0.6.3)
rack (>= 1.0)
rake (10.4.2)
Expand Down Expand Up @@ -135,6 +146,7 @@ DEPENDENCIES
middleman (~> 3.3.10)
middleman-autoprefixer (~> 2.4.4)
middleman-gh-pages (~> 0.0.3)
middleman-livereload (~> 3.4.6)
middleman-syntax (~> 2.0.0)
pdfkit (~> 0.8.2)
rake (~> 10.4.2)
Expand Down
2 changes: 2 additions & 0 deletions config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# Activate the syntax highlighter
activate :syntax

# activate :livereload

activate :autoprefixer do |config|
config.browsers = ['last 2 version', 'Firefox ESR']
config.cascade = false
Expand Down
118 changes: 104 additions & 14 deletions source/includes/_query.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@ format | false | Returns response in XML or TSV format, JSON is default
$ curl 'https://gdc-api.nci.nih.gov/projects?facets=program.name&from=1&size=0&sort=program.name:asc&pretty=true'
```
```python
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:38)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> gdc_url = 'https://gdc-api.nci.nih.gov/projects?pretty=true'
>>> gdc_params = 'facets=program.name&from=1&size=0&sort=program.name:asc'
>>> response = requests.get(gdc_url,params = gdc_params)
>>> print(response.url)
https://gdc-api.nci.nih.gov/projects?pretty=true&facets=program.name&from=1&size=0&sort=program.name:asc
>>> import json
>>> print json.dumps(response.json())
import requests
import json

projects_endpt = 'https://gdc-api.nci.nih.gov/projects'
params = {'facets':'program.name',
'from':1, 'size':0,
'sort':'program.name:asc'}
response = requests.get(projects_endpt, params = params)
print json.dumps(response.json(), indent=2)
```
> The above command returns JSON structured like this:
Expand Down Expand Up @@ -79,6 +76,16 @@ A list of all valid _field_ names that can be used as facets is available in [Ap
```shell
$ curl 'https://gdc-api.nci.nih.gov/files?fields=file_name&pretty=true'
```
```python
import requests
import json

files_endpt = 'https://gdc-api.nci.nih.gov/files'
params = {'fields':'file_name'}
response = requests.get(files_endpt, params = params)
print json.dumps(response.json(), indent=2)

```
> The above command returns JSON structured like this:
Expand Down Expand Up @@ -144,6 +151,18 @@ Users can get a list of available values for a specific field in the filter by m
```shell
$ curl 'https://gdc-api.nci.nih.gov/cases?facets=clinical.gender&from=1&size=0&sort=clinical.gender:asc&pretty=true'
```
```python
import requests
import json

cases_endpt = 'https://gdc-api.nci.nih.gov/cases'
params = {'facets':'clinical.gender',
'from':1, 'size':0,
'sort':'clinical.gender:asc'}
response = requests.get(cases_endpt, params = params)
print json.dumps(response.json(), indent=2)

```
> The above command returns JSON structured like this:
Expand Down Expand Up @@ -196,11 +215,34 @@ It is possible to obtain multiple values from multiple fields in one single quer
}
}
```
```python
filt = {"op":"=",
"content":{
"field": "cases.clinical.gender",
"value": ["male"]
}
}
```
>The above JSON is URL encoded and passed to the filters parameter for the API call below.
```shell
curl 'https://gdc-api.nci.nih.gov/cases?filters=%7b%22op%22%3a+%22%3d%22%2c%0d%0a++++++%22content%22%3a+%7b%0d%0a++++++++++%22field%22%3a+%22cases.clinical.gender%22%2c%0d%0a++++++++++%22value%22%3a+%5b%22male%22%5d%0d%0a++++++%7d%0d%0a%7d'
```
```python
import requests
import json
cases_endpt = 'https://gdc-api.nci.nih.gov/cases'
filt = {"op":"=",
"content":{
"field": "cases.clinical.gender",
"value": ["male"]
}
}
params = {'filters':json.dumps(filt), 'sort':'clinical.gender:asc'}
# requests encodes automatically
response = requests.get(cases_endpt, params = params)
print json.dumps(response.json(), indent=2)
```

More in depth examples of various filter types supported in GDC are available in the [Appendix A](/developers/gdc-application-programming-interface-api-users-guide/appendix-available-fields "Appendix A - Available Fields")

Expand All @@ -226,6 +268,17 @@ When using multiple fields, operator content requires nested data containing add
```shell
curl 'https://gdc-api.nci.nih.gov/files?fields=file_name&from=101&size=5&pretty=true'
```
```python
import requests
import json

files_endpt = 'https://gdc-api.nci.nih.gov/files'
params = {'fields':'file_name',
'from':101, 'size':5}
response = requests.get(files_endpt, params = params)
print json.dumps(response.json(), indent=2)

```
> The above command returns JSON structured like this:
Expand Down Expand Up @@ -270,7 +323,17 @@ The GDC API uses pagination, the **from** query parameter specifies the first re
```shell
$ curl 'https://gdc-api.nci.nih.gov/files?fields=file_name&from=0&size=2&pretty=true'
```
```python
import requests
import json

files_endpt = 'https://gdc-api.nci.nih.gov/files'
params = {'fields':'file_name',
'from':0, 'size':2}
response = requests.get(files_endpt, params = params)
print json.dumps(response.json(), indent=2)

```
> The above command returns JSON structured like this:
```json
Expand Down Expand Up @@ -305,7 +368,17 @@ The **size** query parameter specifies the number of results to return. When siz
```shell
$ curl 'https://gdc-api.nci.nih.gov/cases?fields=submitter_id&sort=submitter_id:asc&pretty=true'
```
```python
import requests
import json

cases_endpt = 'https://gdc-api.nci.nih.gov/cases'
params = {'fields':'submitter_id',
'sort':'submitter_id:asc'}
response = requests.get(cases_endpt, params = params)
print json.dumps(response.json(), indent=2)

```
> The above command returns JSON structured like this:
```json
Expand Down Expand Up @@ -408,7 +481,7 @@ curl 'https://gdc-api.nci.nih.gov/cases?fields=submitter_id&sort=submitter_id:a
```
### Format

> A Query with format=TSV returns a results in a Tab Seperated Values (TSV) format
> A Query with format=TSV returns a results in a Tab Separated Values (TSV) format
```shell
curl 'https://gdc-api.nci.nih.gov/cases?fields=submitter_id&size=5&format=TSV&pretty=true'
Expand All @@ -419,7 +492,15 @@ TCGA-MB-A5Y8
TCGA-BQ-5876
TCGA-Z6-A9VB
```
```python
import requests

cases_endpt = 'https://gdc-api.nci.nih.gov/cases'
params = {'fields':'submitter_id',
'format':'TSV'}
response = requests.get(cases_endpt, params = params)
print response.content
```
> Same query as above with format=XML returns results in an XML format
```shell
Expand Down Expand Up @@ -457,7 +538,16 @@ TCGA-Z6-A9VB
<warnings/>
</response>
```

```python
import requests

cases_endpt = 'https://gdc-api.nci.nih.gov/cases'
params = {'fields':'submitter_id',
'format':'XML',
'pretty':'true'}
response = requests.get(cases_endpt, params = params)
print response.content
```
Returns the response data in a format other than JSON.

The following options are available:
Expand Down

0 comments on commit e8af85e

Please sign in to comment.