jpayne@0: #! /usr/bin/env python3 jpayne@0: jpayne@0: "Grab SRR numbers from BioProjects via the EMBL-ENA REST API's." jpayne@0: jpayne@0: import requests jpayne@0: import sys jpayne@0: jpayne@0: sra_exp_query = "http://www.ebi.ac.uk/ebisearch/ws/rest/sra-experiment?query={bioproject}" jpayne@0: jpayne@0: sample = """{ jpayne@0: "hitCount": 2, jpayne@0: "entries": [ jpayne@0: { jpayne@0: "id": "SRX377510", jpayne@0: "source": "sra-experiment" jpayne@0: }, jpayne@0: { jpayne@0: "id": "SRX583279", jpayne@0: "source": "sra-experiment" jpayne@0: } jpayne@0: ], jpayne@0: "facets": [] jpayne@0: }""" jpayne@0: jpayne@0: sra_run_query = "http://www.ebi.ac.uk/ebisearch/ws/rest/sra-run?query={experiment}" jpayne@0: jpayne@0: sample = """{ jpayne@0: "hitCount": 1, jpayne@0: "entries": [ jpayne@0: { jpayne@0: "id": "SRR1029665", jpayne@0: "source": "sra-run" jpayne@0: } jpayne@0: ], jpayne@0: "facets": [] jpayne@0: }""" jpayne@0: jpayne@0: if __name__ == "__main__": jpayne@0: try: jpayne@0: bioproject = sys.argv[1] jpayne@0: b_result = requests.get(sra_exp_query.format(bioproject=bioproject), headers=dict(Accept="application/json")) jpayne@0: b_result.raise_for_status() jpayne@0: if b_result.json()['entries']: jpayne@0: for experiment in [d['id'] for d in b_result.json()['entries']]: jpayne@0: r_result = requests.get(sra_run_query.format(experiment=experiment), headers=dict(Accept="application/json")) jpayne@0: r_result.raise_for_status() jpayne@0: for run in [d['id'] for d in r_result.json()['entries']]: jpayne@0: print(run) jpayne@0: else: jpayne@0: print(f"No results found for '{bioproject}'.", file=sys.stderr) jpayne@0: quit(1) jpayne@0: except IndexError: jpayne@0: raise ValueError("Please provide an NCBI BioProject, NCBI BioSample, EMBL Project, or EMBL Study accession.") jpayne@0: except KeyError as e: jpayne@0: raise ValueError() from e jpayne@0: jpayne@0: jpayne@0: