Skip to main content

Unified Python client for Alliance of Genome Resources (AGR) curation APIs

Project description

AGR Curation API Client

Tests Lint and Type Check Database Integration Tests Claude Code Review

A unified Python client for Alliance of Genome Resources (AGR) curation APIs.

Features

  • Unified Interface: Single client for all AGR curation API endpoints
  • Multiple Data Sources: Supports REST API, GraphQL, and direct database access
  • Type Safety: Full type hints and Pydantic models for request/response validation
  • Fuzzy Search: Partial matching and synonym search for genes, alleles, and other entities
  • Retry Logic: Automatic retry with exponential backoff for transient failures
  • Authentication: Support for API key and Okta token authentication
  • Async Support: Built on httpx for both sync and async operations
  • Comprehensive Error Handling: Detailed exceptions for different error scenarios

Installation

pip install agr-curation-api-client

For development:

git clone https://github.com/alliance-genome/agr_curation_api_client.git
cd agr_curation_api_client
make install-dev

Authentication

The client supports automatic Okta token generation using the same environment variables as other AGR services:

export OKTA_DOMAIN="your-okta-domain"
export OKTA_API_AUDIENCE="your-api-audience"
export OKTA_CLIENT_ID="your-client-id"
export OKTA_CLIENT_SECRET="your-client-secret"

With these environment variables set, the client will automatically obtain an authentication token when initialized.

Quick Start

Basic Usage

from agr_curation_api import AGRCurationAPIClient, APIConfig

# Option 1: Automatic authentication (requires OKTA env vars)
client = AGRCurationAPIClient()

# Option 2: Manual token configuration
config = APIConfig(
    base_url="https://curation.alliancegenome.org/api",
    okta_token="your-okta-token"  # Optional - will auto-retrieve if not provided
)
client = AGRCurationAPIClient(config)

# Use the client
with client:
    # Get genes from WormBase
    genes = client.get_genes(data_provider="WB", limit=10)
    
    for gene in genes:
        symbol = gene.gene_symbol.get("displayText", "") if gene.gene_symbol else ""
        print(f"{gene.curie}: {symbol}")

Data Source Selection

The client supports multiple data access methods with automatic fallback. By default, it tries database access first (fastest), then GraphQL, then REST API.

# Automatic data source selection (default behavior)
client = AGRCurationAPIClient()
genes = client.get_genes(taxon="NCBITaxon:6239", limit=100)  # Uses database if available

# Force specific data source
client = AGRCurationAPIClient(data_source="db")      # Database only
client = AGRCurationAPIClient(data_source="graphql") # GraphQL only
client = AGRCurationAPIClient(data_source="api")     # REST API only

# Direct database access for advanced queries
gene = client.db.get_gene("WB:WBGene00001234")
allele = client.db.get_allele("WB:WBVar00001234")

Working with Genes

from agr_curation_api import AGRCurationAPIClient, Gene

# Use default configuration
client = AGRCurationAPIClient()

# Get genes from a specific data provider
wb_genes = client.get_genes(data_provider="WB", limit=100)
print(f"Found {len(wb_genes)} WormBase genes")

# Get a specific gene by ID (works with database, GraphQL, or API)
gene = client.get_gene("WB:WBGene00001234")
if gene:
    print(f"Gene: {gene.gene_symbol}")
    print(f"Full name: {gene.gene_full_name}")
    print(f"Species: {gene.taxon}")

# Get all genes (paginated)
all_genes = client.get_genes(limit=5000, page=0)

Working with Species

# Get all species
species_list = client.get_species()

for species in species_list:
    print(f"{species.abbreviation}: {species.display_name}")

# Find a specific species
wb_species = [s for s in species_list if s.abbreviation == "WB"]
if wb_species:
    print(f"WormBase: {wb_species[0].full_name}")

Working with Ontology Terms

# Get GO term root nodes
go_roots = client.get_ontology_root_nodes("goterm")
print(f"Found {len(go_roots)} GO root terms")

# Get children of a specific GO term
children = client.get_ontology_node_children("GO:0008150", "goterm")  # biological_process
for child in children:
    print(f"{child.curie}: {child.name}")

# Get disease ontology terms
disease_roots = client.get_ontology_root_nodes("doterm")

# Get anatomical terms
anatomy_roots = client.get_ontology_root_nodes("anatomicalterm")

Working with Expression Annotations

# Get expression annotations for WormBase
wb_expressions = client.get_expression_annotations(
    data_provider="WB",
    limit=100
)

for expr in wb_expressions:
    if expr.expression_annotation_subject:
        gene_id = expr.expression_annotation_subject.get("primaryExternalId")
        gene_symbol = expr.expression_annotation_subject.get("geneSymbol", {}).get("displayText")
        print(f"Gene: {gene_id} ({gene_symbol})")
        
    if expr.expression_pattern:
        anatomy = expr.expression_pattern.get("whereExpressed", {}).get("anatomicalStructure", {}).get("curie")
        print(f"  Expressed in: {anatomy}")

Working with Alleles

# Get alleles from a specific data provider
wb_alleles = client.get_alleles(data_provider="WB", limit=50)

for allele in wb_alleles:
    symbol = allele.allele_symbol.get("displayText", "") if allele.allele_symbol else ""
    print(f"{allele.curie}: {symbol}")

# Get a specific allele by ID (works with database, GraphQL, or API)
allele = client.get_allele("WB:WBVar00001234")
if allele:
    print(f"Allele: {allele.allele_symbol}")
    print(f"Full name: {allele.allele_full_name}")
    print(f"Extinction status: {allele.is_extinct}")

Searching Entities

The client provides two search methods with different strengths:

Fuzzy Search (search_entities_fuzzy): Best for user-friendly, autocomplete-style searching

  • Database-only (direct SQL queries)
  • Partial text matching: "rut" finds "rutabaga", "RUT", "rut-1"
  • Automatically searches symbols, full names, and synonyms
  • Returns results with relevance scoring (exact > starts-with > contains)
  • Use when: searching by partial names, building autocomplete, finding entities by common/historical names
  • Supported types: gene, allele, agm, strain, genotype, fish

Generic Search (search_entities): Best for programmatic queries with known field structures

  • REST API-based with structured filters
  • Exact field matching with precise field paths (e.g., "geneSymbol.displayText")
  • Supports complex boolean filter logic
  • Returns complete entity objects
  • Use when: you know exact field names, need complex filters, require full entity data
  • Supports all entity types available in the API

Fuzzy Search

# Search for genes with partial matching
# Example: Find genes containing "rut" in Drosophila
results = client.db.search_entities_fuzzy(
    entity_type='gene',
    search_pattern='rut',
    taxon_curie='NCBITaxon:7227',
    include_synonyms=True,
    limit=10
)

for result in results:
    print(f"{result['entity_curie']}: {result['entity']}")
    print(f"  Match type: {result['match_type']}")  # exact, starts_with, or contains
    print(f"  Relevance: {result['relevance']}")    # 1 (best) to 3 (least)

# Search for alleles without synonyms
allele_results = client.db.search_entities_fuzzy(
    entity_type='allele',
    search_pattern='daf',
    taxon_curie='NCBITaxon:6239',
    include_synonyms=False,
    limit=20
)

# Supported entity types: 'gene', 'allele', 'agm', 'strain', 'genotype', 'fish'
# Results are ordered by relevance (exact matches first, then starts-with, then contains)

Generic Search

# Generic entity search
search_filters = {
    "dataProvider.abbreviation": "WB",
    "geneSymbol.displayText": "daf-16"
}

results = client.search_entities(
    entity_type="gene",
    search_filters=search_filters,
    limit=10
)

print(f"Total results: {results.total_results}")
print(f"Returned: {results.returned_records}")

for gene_data in results.results:
    print(f"Found gene: {gene_data}")

Error Handling

from agr_curation_api import (
    AGRAPIError,
    AGRAuthenticationError,
    AGRConnectionError,
    AGRTimeoutError,
    AGRValidationError
)

try:
    reference = client.get_reference("invalid-id")
except AGRAuthenticationError:
    print("Authentication failed - check your credentials")
except AGRValidationError as e:
    print(f"Invalid data: {e}")
except AGRTimeoutError:
    print("Request timed out - try again later")
except AGRConnectionError:
    print("Connection failed - check network")
except AGRAPIError as e:
    print(f"API error: {e}")
    if e.status_code:
        print(f"Status code: {e.status_code}")

Configuration Options

The APIConfig class supports the following options:

  • base_url: Base URL for the A-Team Curation API (default: "https://curation.alliancegenome.org/api")
  • okta_token: Okta bearer token for authentication (auto-retrieved if not provided)
  • timeout: Request timeout in seconds (default: 30)
  • max_retries: Maximum retry attempts (default: 3)
  • retry_delay: Initial delay between retries in seconds (default: 1)
  • verify_ssl: Whether to verify SSL certificates (default: True)
  • headers: Additional headers to include in requests

Environment Variables

The client uses the following environment variables for configuration:

  • ATEAM_API: Override the default A-Team API URL (default: uses production curation API)
  • OKTA_DOMAIN: Your Okta domain (required for automatic authentication)
  • OKTA_API_AUDIENCE: Your API audience (required for automatic authentication)
  • OKTA_CLIENT_ID: Your Okta client ID (required for automatic authentication)
  • OKTA_CLIENT_SECRET: Your Okta client secret (required for automatic authentication)

Development

Running Tests

make test

Code Quality

# Run linting
make lint

# Run type checking
make type-check

# Format code
make format

# Run all checks
make check

Building Documentation

cd docs
make html

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'feat: add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agr_curation_api_client-0.7.4.tar.gz (45.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agr_curation_api_client-0.7.4-py3-none-any.whl (38.3 kB view details)

Uploaded Python 3

File details

Details for the file agr_curation_api_client-0.7.4.tar.gz.

File metadata

  • Download URL: agr_curation_api_client-0.7.4.tar.gz
  • Upload date:
  • Size: 45.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for agr_curation_api_client-0.7.4.tar.gz
Algorithm Hash digest
SHA256 1c62ba9485754644b33e768dfe3cf9dbd521198c6803b09484d4061c39df4f69
MD5 780986ebe74bb8338df11c4b32bb4b29
BLAKE2b-256 bade4d418de740dc1438ef07aa344c1b55b079c3df4e49dca7c298a6c4889e67

See more details on using hashes here.

File details

Details for the file agr_curation_api_client-0.7.4-py3-none-any.whl.

File metadata

File hashes

Hashes for agr_curation_api_client-0.7.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0e5b900b0948f50e2d0a9a7cf6ca5d1db84772a74d597261d655b3c0941008b5
MD5 db0fbf3922f873a4dff14a6bc3557312
BLAKE2b-256 e50d676ea045259382524f09fff65d45096c7aa53666d6b7b586c37417a4d236

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page