Skip to content

Data Querying + Gen3 SDK

Overview ⚙️

Gen3 supports API access to Files and Metadata, allowing users to download and query their data via the Gen3 SDK and GraphQL queries.

Quick Start ⚡️

Adapted from the Gen3 SDK Quick Start page

1. Install

The Gen3 SDK is available for installation via PyPi:

pip install gen3

2. Authenticate

Configure a Profile with Credentials

Download an API Key from the Profile page and save it to ~/.gen3/credentials.json

Gen3 Profile page

Gen3 Credentials

3. Query

Query (example.graphql)

query ExampleQuery {
  files: file(first: 1000) {
    file_name
    project_id
    id
  }
  patients: patient(first: 1000) {
    name
    project_id
    id
  }
  observations: observation(first: 1000) {
    code
    project_id
    id
  }
}

Script (example.py)

from gen3.auth import Gen3Auth
from gen3.query import Gen3Query
import json

auth = Gen3Auth()

query = ''

# Read in Example Query
with open('example.graphql') as f:
    query = f.read()

response = Gen3Query.graphql_query(Gen3Query(auth), query_string=query)

formatted = json.dumps(response, indent=2)

print(formatted)

# >>> Example Output

Output

$ python example.py
{
  "data": {
    "files": [
      {
        "file_name": "example.bam",
        "project_id": "cbds-example",
      },...
    ],
    "patients": [
      {
        "name": "Example Name",
        "project_id": "cbds-example",
      },...
    ],
    "observations": [
      {
        "code": "Example Code",
        "project_id": "cbds-example",
      },...
    ]
  }
}

Additional Resources 📚