From 807513f2b0423c5def8ff2ab712ea1aee329578c Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Wed, 10 Oct 2012 14:16:50 -0700 Subject: [PATCH 1/3] Added a KEGG API Wrapper, docs, and tests --- Bio/KEGG/__init__.py | 197 ++++++++++++++++++++++++++++++++++++++++- Doc/Tutorial.tex | 88 ++++++++++++++++++ Tests/output/test_KEGG | 41 +++++++++ Tests/test_KEGG.py | 157 ++++++++++++++++++++++++++++++++ 4 files changed, 482 insertions(+), 1 deletion(-) diff --git a/Bio/KEGG/__init__.py b/Bio/KEGG/__init__.py index 880e3c3856f..c28e6c10e38 100644 --- a/Bio/KEGG/__init__.py +++ b/Bio/KEGG/__init__.py @@ -1,4 +1,6 @@ -# Copyright 2001 by Tarjei Mikkelsen. All rights reserved. +# Copyright 2001 by Tarjei Mikkelsen. +# Copyright 2012 by Kevin Wu. +# All rights reserved. # This code is part of the Biopython distribution and governed by its # license. Please see the LICENSE file that should have been included # as part of this package. @@ -81,3 +83,196 @@ def _write_kegg(item, info, indent=KEGG_ITEM_LENGTH): if item is not "": # ensure item is only written on first line item = "" return s + + +def _q(op, arg1, arg2=None, arg3=None): + + import urllib2 + URL = "http://rest.kegg.jp/%s" + + if arg2 and arg3: + args = "%s/%s/%s/%s" % (op, arg1, arg2, arg3) + elif arg2: + args = "%s/%s/%s" % (op, arg1, arg2) + else: + args = "%s/%s" % (op, arg1) + + req = urllib2.Request(URL % (args)) + resp = urllib2.urlopen(req) + + return resp + + +# http://www.kegg.jp/kegg/rest/keggapi.html +def info(arg1): + # http://rest.kegg.jp/info/ + # + # = pathway | brite | module | disease | drug | environ | + # ko | genome | | compound | glycan | reaction | + # rpair | rclass | enzyme | genomes | genes | ligand | kegg + # = KEGG organism code or T number + if arg1: + resp = _q("info", arg1) + else: + raise Exception("Invalid info operation.") + + return resp + + +def list_(arg1, arg2=None): + # http://rest.kegg.jp/list// + # + # = pathway | module + # = KEGG organism code + if isinstance(arg1, str) and (arg1 == "pathway" or arg1 == "module") and arg2: + resp = _q("list", arg1, arg2) + + # http://rest.kegg.jp/list/ + # + # = pathway | brite | module | disease | drug | environ | + # ko | genome | | compound | glycan | reaction | + # rpair | rclass | enzyme | organism + # = KEGG organism code or T number + # + # + # http://rest.kegg.jp/list/ + # + # = KEGG database entries involving the following + # = pathway | brite | module | disease | drug | environ | + # ko | genome | | compound | glycan | reaction | + # rpair | rclass | enzyme + # = KEGG organism code or T number + elif arg1 and not arg2: + if isinstance(arg1, list) and len(arg1) <= 100: + arg1 = ("+").join(arg1) + elif isinstance(arg1, list) and len(arg1) > 100: + raise Exception("Maximuim number of queries is 100") + resp = _q("list", arg1) + + else: + raise Exception("Invalid list operation.") + + return resp + + +def find(arg1, arg2=None, arg3=None): + # http://rest.kegg.jp/find///