Skip to content

Load a bundle from the public repository

Notebook source

Rendered from notebooks/repository/load-public-bundle.ipynb.

This notebook discovers complete published datasets in the public GKM Starter Kit bundle repository, retrieves a bundle and its schema, and loads the bundle into GKM Python models.

Discover available resources

This example uses refresh=False to reuse the saved index. Use refresh=True to retrieve the current public repository index.

In [1]
from ga4gh.gkm.bundles import BundleRepository

repository = BundleRepository(refresh=False)
repository.resource_names

Output:

('civic',)

Retrieve a bundle and its schema

Each resource provides bundle.json and bundle.schema.json. These calls use refresh=False to reuse saved artifacts; use refresh=True in both to retrieve current copies from the public repository.

In [2]
resource_name = repository.resource_names[0]
bundle_data = repository.get_bundle(resource_name, refresh=False)
schema_data = repository.get_bundle_json_schema(resource_name, refresh=False)

resource_name, list(bundle_data), list(schema_data)[:3]

Output:

('civic',
 ['sequenceReference',
  'location',
  'variant',
  'feature',
  'molecularProfile',
  'disease',
  'phenotype',
  'conditionSet',
  'therapy',
  'therapyGroup',
  'variantOrigin',
  'source',
  'method',
  'organization',
  'proposition',
  'evidence',
  'assertion',
  'metadata',
  'failedAssertionIds'],
 ['$defs', '$id', '$schema'])

Load the bundle

The repository returns parsed JSON objects. Serialize them to in-memory streams to use the standard load_bundle workflow.

In [3]
import io
import json

from ga4gh.gkm.bundles import load_bundle

bundle = load_bundle(
    io.StringIO(json.dumps(bundle_data)),
    schema=io.StringIO(json.dumps(schema_data)),
)

bundle.collection_names()

Output:

('sequenceReference',
 'location',
 'variant',
 'feature',
 'molecularProfile',
 'disease',
 'phenotype',
 'conditionSet',
 'therapy',
 'therapyGroup',
 'variantOrigin',
 'source',
 'method',
 'organization',
 'proposition',
 'evidence',
 'assertion')

Handle repository errors

An unknown resource is detected before its files are downloaded and raises BundleRepositoryResourceNotFoundError.

In [4]
from ga4gh.gkm.bundles import (
    BundleRepositoryResourceNotFoundError,
)

# 1. A resource name that is not listed in the index
try:
    repository.get_bundle("resource-that-does-not-exist")
except BundleRepositoryResourceNotFoundError as error:
    print(f"Unknown resource: {error}")

Output:

Unknown resource: Bundle resource is neither indexed nor completely saved locally: resource-that-does-not-exist