Skip to main content

Python client for BugSeq (https://bugseq.com)

Project description

BugSeq Python Client Library

Authentication and client code for interacting with the BugSeq API.

Installation

pip install bugseq-client

Example Usage

import tempfile
from pathlib import Path

import requests
from bugseq_client.auth import (
    DEFAULT_OAUTH_CONFIG,
    Session,
    get_default_credential_storage_provider,
)
from bugseq_client.openapi_client import (
    ApiClient,
    AppModelsJobRunOptionsSampleType,
    Configuration,
    JobRunSubmitRequest,
    JobsApi,
    Kit,
    MoleculeType,
    Platform,
    RunOptionsRequest,
)
from bugseq_client.utils.upload import multipart_upload


# you should replace this with your lab ID
LAB_ID = "lab_123"


def submit_test_job(api_client: ApiClient, file_ids: list[str]):
    api = JobsApi(api_client)

    job_run_submit_request = JobRunSubmitRequest(
        user_provided_name="My awesome analysis",
        file_ids=file_ids,
        run_options=RunOptionsRequest(
            platform=Platform.NANOPORE,
            kit=Kit.MINION_R10_4_1,
            sample_type=AppModelsJobRunOptionsSampleType.RESPIRATORY_UPPER,
            molecule_type=MoleculeType.DNA,
        ),
        lab_id=LAB_ID,
        testmode=True,
    )
    api.submit_analysis_v1_jobs_post(job_run_submit_request)


def download_url_to_file(download_url: str, dst: Path):
    with requests.get(download_url, stream=True) as r:
        r.raise_for_status()
        with open(dst, "wb") as f:
            for chunk in r.iter_content(chunk_size=8192):
                if chunk:  # filter out keep-alive chunks
                    f.write(chunk)


def fetch_and_download_job_results(api_client: ApiClient):
    api = JobsApi(api_client)

    print("fetching jobs")
    jobs_resp = api.list_analyses_v1_jobs_get()
    for job in jobs_resp.job_runs:
        print(f"id={job.id} name={job.user_provided_name} status={job.job_status}")

    print()

    assert len(jobs_resp.job_runs) > 0, "No jobs found, no results to pull"

    job_id = jobs_resp.job_runs[0].id

    results_dir = Path(f"downloaded-results-{job_id}")

    print(f"fetching outputs for job {job_id} to {results_dir}")

    results_resp = api.get_analysis_results_v1_jobs_job_id_results_get(job_id)
    for output in results_resp.outputs:
        print(f"  output filename={output.filename} size={output.size}")

        if not output.filename.startswith("sample_reports/"):
            print("  does not match summary_reports filter, skipping")
            continue

        download_resp = (
            api.download_analysis_result_v1_jobs_job_id_results_download_get(
                job_id, output.filename
            )
        )

        download_dst = results_dir / output.filename
        download_dst.parent.mkdir(exist_ok=True, parents=True)
        download_url_to_file(download_resp.url, download_dst)
        print(f"  downloaded filename={output.filename} dst={str(download_dst)}")


class App:
    def __init__(self):
        storage = get_default_credential_storage_provider()
        self.session = Session(DEFAULT_OAUTH_CONFIG, storage)

    def run(self):
        token = self.session.get_token()

        configuration = Configuration(
            host="https://api.bugseq.com",
            access_token=token,
        )

        with ApiClient(configuration) as api_client:
            with tempfile.NamedTemporaryFile() as tmp:
                tmp_path = Path(tmp.name)
                tmp_path.write_text("test")
                file_id = multipart_upload(api_client, tmp_path)

            submit_test_job(api_client, file_ids=[file_id])
            fetch_and_download_job_results(api_client)


def main():
    app = App()
    app.run()


if __name__ == "__main__":
    main()

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

bugseq_client-0.1.7.tar.gz (65.8 kB view details)

Uploaded Source

Built Distribution

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

bugseq_client-0.1.7-py3-none-any.whl (162.8 kB view details)

Uploaded Python 3

File details

Details for the file bugseq_client-0.1.7.tar.gz.

File metadata

  • Download URL: bugseq_client-0.1.7.tar.gz
  • Upload date:
  • Size: 65.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for bugseq_client-0.1.7.tar.gz
Algorithm Hash digest
SHA256 9eff845db6272484021c0b451295600412b4fa9fa22e4b3c16c4d5b3f3db5a1b
MD5 649db58bcb3416cd4b9d201d1cebe729
BLAKE2b-256 4c3de5e4cc0cddc73f8137f95af0526b0165ebbccafc282cae7b4ea1a600cba6

See more details on using hashes here.

File details

Details for the file bugseq_client-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: bugseq_client-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 162.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for bugseq_client-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 f448eb68551e0e4270aafb011a0b1eb41c9b3ed6ed35b8d0aa12d1415526b437
MD5 73c8842d8f2ac11118e68ff940aa5b13
BLAKE2b-256 3b9f77cf98d1a67f18f4fb78a60e5ae7a512b0829b045f2478522338773a51a2

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