annotate bio2srr.py @ 1:b07378e15ad9

planemo upload commit bce663a3ac9d4b5f86a3becf93c7639221ee01ea
author jpayne
date Thu, 20 Dec 2018 11:04:53 -0500
parents 02ac32a00e25
children 556cac4fb538
rev   line source
jpayne@0 1 #! /usr/bin/env python3
jpayne@0 2
jpayne@0 3 "Grab SRR numbers from BioProjects via the EMBL-ENA REST API's."
jpayne@0 4
jpayne@0 5 import requests
jpayne@0 6 import sys
jpayne@0 7
jpayne@0 8 sra_exp_query = "http://www.ebi.ac.uk/ebisearch/ws/rest/sra-experiment?query={bioproject}"
jpayne@0 9
jpayne@0 10 sample = """{
jpayne@0 11 "hitCount": 2,
jpayne@0 12 "entries": [
jpayne@0 13 {
jpayne@0 14 "id": "SRX377510",
jpayne@0 15 "source": "sra-experiment"
jpayne@0 16 },
jpayne@0 17 {
jpayne@0 18 "id": "SRX583279",
jpayne@0 19 "source": "sra-experiment"
jpayne@0 20 }
jpayne@0 21 ],
jpayne@0 22 "facets": []
jpayne@0 23 }"""
jpayne@0 24
jpayne@0 25 sra_run_query = "http://www.ebi.ac.uk/ebisearch/ws/rest/sra-run?query={experiment}"
jpayne@0 26
jpayne@0 27 sample = """{
jpayne@0 28 "hitCount": 1,
jpayne@0 29 "entries": [
jpayne@0 30 {
jpayne@0 31 "id": "SRR1029665",
jpayne@0 32 "source": "sra-run"
jpayne@0 33 }
jpayne@0 34 ],
jpayne@0 35 "facets": []
jpayne@0 36 }"""
jpayne@0 37
jpayne@0 38 if __name__ == "__main__":
jpayne@0 39 try:
jpayne@0 40 bioproject = sys.argv[1]
jpayne@0 41 b_result = requests.get(sra_exp_query.format(bioproject=bioproject), headers=dict(Accept="application/json"))
jpayne@0 42 b_result.raise_for_status()
jpayne@0 43 if b_result.json()['entries']:
jpayne@0 44 for experiment in [d['id'] for d in b_result.json()['entries']]:
jpayne@0 45 r_result = requests.get(sra_run_query.format(experiment=experiment), headers=dict(Accept="application/json"))
jpayne@0 46 r_result.raise_for_status()
jpayne@0 47 for run in [d['id'] for d in r_result.json()['entries']]:
jpayne@0 48 print(run)
jpayne@0 49 else:
jpayne@0 50 print(f"No results found for '{bioproject}'.", file=sys.stderr)
jpayne@0 51 quit(1)
jpayne@0 52 except IndexError:
jpayne@0 53 raise ValueError("Please provide an NCBI BioProject, NCBI BioSample, EMBL Project, or EMBL Study accession.")
jpayne@0 54 except KeyError as e:
jpayne@0 55 raise ValueError() from e
jpayne@0 56
jpayne@0 57
jpayne@0 58