view bio2srr.py @ 0:02ac32a00e25

planemo upload commit 4aa8338dc8bcd7f6c0fb675044ea9d0c045ee7f3
author jpayne
date Tue, 18 Dec 2018 15:17:42 -0500
parents
children 556cac4fb538
line wrap: on
line source
#! /usr/bin/env python3

"Grab SRR numbers from BioProjects via the EMBL-ENA REST API's."

import requests
import sys

sra_exp_query = "http://www.ebi.ac.uk/ebisearch/ws/rest/sra-experiment?query={bioproject}"

sample = """{
    "hitCount": 2,
    "entries": [
        {
            "id": "SRX377510",
            "source": "sra-experiment"
        },
        {
            "id": "SRX583279",
            "source": "sra-experiment"
        }
    ],
    "facets": []
}"""

sra_run_query = "http://www.ebi.ac.uk/ebisearch/ws/rest/sra-run?query={experiment}"

sample = """{
    "hitCount": 1,
    "entries": [
        {
            "id": "SRR1029665",
            "source": "sra-run"
        }
    ],
    "facets": []
}"""

if __name__ == "__main__":
	try:
		bioproject = sys.argv[1]
		b_result = requests.get(sra_exp_query.format(bioproject=bioproject), headers=dict(Accept="application/json"))
		b_result.raise_for_status()
		if b_result.json()['entries']:
			for experiment in [d['id'] for d in b_result.json()['entries']]:
				r_result = requests.get(sra_run_query.format(experiment=experiment), headers=dict(Accept="application/json"))
				r_result.raise_for_status()
				for run in [d['id'] for d in r_result.json()['entries']]:
					print(run)
		else:
			print(f"No results found for '{bioproject}'.", file=sys.stderr)
			quit(1)
	except IndexError:
		raise ValueError("Please provide an NCBI BioProject, NCBI BioSample, EMBL Project, or EMBL Study accession.")
	except KeyError as e:
		raise ValueError() from e