source: orange-bioinformatics/obiKEGG2/api.py @ 1532:14a377419a09

Revision 1532:14a377419a09, 20.5 KB checked in by ales_erjavec, 2 years ago (diff)

Added KEGG interface using web services

Line 
1"""
2KEGG api interface.
3
4"""
5from __future__ import absolute_import
6
7from contextlib import closing
8
9from .service import web_service
10from .types import *
11
12class KeggApi(object):
13    """ KEGG API """
14   
15    def __init__(self):
16        self.service = web_service()
17       
18    ##################
19    # Meta information
20    ##################
21   
22    def list_databases(self):
23        """ Returns a list of available databases.
24       
25        >>> api.list_databases()
26        [Definition(entry_id='nt',...
27         
28        """
29        return map(Definition.from_items, self.service.list_databases())
30   
31    def list_organisms(self):
32        """ Return a list of all available organisms
33       
34        >>> api.list_organisms()
35        [Definition(entry_id='hsa',...
36       
37        """
38        return map(Definition.from_items, self.service.list_organisms())
39   
40    def list_pathways(self, organism):
41        """ Return a list of all available pathways for `organism`
42       
43        >>> api.list_pathways("hsa")
44        [Definition(entry_id=',...
45       
46        """
47        return map(Definition.from_items, self.service.list_pathways(organism))
48       
49    #######
50    # DBGET
51    #######
52     
53    def binfo(self, db):
54        """ Return info for database `db`
55       
56        >>> print api.dbinfo("gb")
57        genbank          GenBank nucleic acid sequence database
58        gb               Release 186.0, Oct 11
59                         National Center for Biotechnology Information
60                         144,458,648 entries, 132,067,413,372 bases
61                         Last update: 11/10/24
62                         <dbget> <fasta> <blast>
63                         
64        """
65        return BInfo.from_text(str(self.service.binfo(db)))
66   
67    def bfind(self, db, keywords):
68        """ Search database 'db' for keywords
69        """
70        result = self.service.bfind(" ".join([db, keywords]))
71        if result is not None:
72            return str(result)
73        else:
74            return result
75   
76    def bget(self, ids):
77        """
78        """
79        if not isinstance(ids, basestring):
80            # Sequence of ids
81            ids = " ".join(ids)
82        result = self.service.bget(ids)
83        if result is not None:
84            return str(result)
85        else:
86            return result
87   
88    def btit(self, ids):
89        """
90        """
91        if not isinstance(ids, basestring):
92            ids = " ".join(ids)
93           
94        result = self.service.btit(ids)
95        if result is not None:
96            return str(result)
97        else:
98            return result
99   
100    def bconv(self, ids):
101        if not isinstance(ids, basestring):
102            ids = " ".join(ids)
103           
104        result = self.service.bconv(ids)
105        if result is not None:
106            return str(result)
107        else:
108            return result
109   
110    ########
111    # LinkDB
112    ########
113   
114    def get_linkdb_by_entry(self, entry_id, db, offset, limit):
115        links = self.service.get_linkdb_by_entry(entry_id, db, offset, limit)
116        return [LinkDBRelation(**d) for d in \
117                map(dict, links)]
118       
119    def get_linkdb_between_databases(self, from_db, to_db, offset, limit):
120        links = self.service.get_linkdb_between_databases(from_db, to_db, offset, limit)
121        return [LinkDBRelation(**d) for d in \
122                map(dict, links)]
123       
124    def get_genes_by_enzyme(self, enzyme_id, org):
125        return self.service.get_genes_by_enzyme(enzyme_id, org)
126   
127    def get_enzymes_by_gene(self, genes_id):
128        return self.service.get_enzymes_by_gene(genes_id)
129   
130    def get_enzymes_by_compound(self, compound_id):
131        return self.service.get_enzymes_by_compound(compound_id)
132   
133    def get_enzymes_by_glycan(self, glycan_id):
134        return self.service.get_enzymes_by_glycan(glycan_id)
135   
136    def get_enzymes_by_reaction(self, reaction_id):
137        return self.service.get_enzymes_by_reaction(reaction_id)
138   
139    def get_compounds_by_enzyme(self, enzyme_id):
140        return self.service.get_compounds_by_enzyme(enzyme_id)
141   
142    def get_compounds_by_reaction(self, reaction_id):
143        return self.service.get_compounds_by_reaction(reaction_id)
144   
145    def get_glycans_by_enzyme(self, enzyme_id):
146        return self.service.get_glycans_by_enzyme(enzyme_id)
147   
148    def get_glycans_by_reaction(self, reaction_id):
149        return self.service.get_glycans_by_reaction(reaction_id)
150   
151    def get_reactions_by_enzyme(self, enzyme_id):
152        return self.service.get_reactions_by_enzyme(enzyme_id)
153   
154    def get_reactions_by_compound(self, compound_id):
155        return self.service.get_reactions_by_compound(compound_id)
156   
157    def get_reactions_by_glycan(self, glycan_id):
158        return self.service.get_reactions_by_glycan(glycan_id)
159   
160    ######
161    # SSDB
162    ######
163   
164    def get_best_best_neighbors_by_gene(self, genes_id, offset, limit):
165        ssr = self.service.get_best_best_neighbors_by_gene(genes_id, offset, limit)
166        return [SSDBRelation(**d) for d in \
167                map(dict, ssr)]
168   
169    def get_best_neighbors_by_gene(self, genes_id, offset, limit):
170        ssr = self.service.get_best_neighbors_by_gene(genes_id, offset, limit)
171        return [SSDBRelation(**d) for d in \
172                map(dict, ssr)]
173   
174    def get_reverse_best_neighbors_by_gene(self, genes_id, offset, limit):
175        ssr = self.service.get_reverse_best_neighbors_by_gene(genes_id, offset, limit)
176        return [SSDBRelation(**d) for d in \
177                map(dict, ssr)]
178   
179    def get_paralogs_by_gene(self, genes_id, offset, limit):
180        ssr =  self.service.get_paralogs_by_gene(genes_id, offset, limit)
181        return [SSDBRelation(**d) for d in \
182                map(dict, ssr)]
183   
184    #######
185    # Motif
186    #######
187   
188    def get_motifs_by_gene(self, genes_id, db):
189        motif = self.service.get_motifs_by_gene(genes_id, db)
190        return [MotifResult(**d) for d in \
191                map(dict, motif)]
192   
193    def get_genes_by_motifs(self, motif_id_list, offset, limit):
194        genes = self.service.get_genes_by_motifs(motif_id_list, offset, limit)
195        return [Definition(**d) for d in \
196                map(dict, genes)]
197   
198    ####
199    # KO
200    ####
201   
202    def get_ko_by_gene(self, genes_id):
203        return self.service.get_ko_by_gene(genes_id)
204   
205    def get_ko_by_ko_class(self, ko_class_id):
206        return self.service.get_ko_by_ko_class(ko_class_id)
207   
208    def get_genes_by_ko_class(self, ko_class_id, org, offset, limit):
209        return self.service.get_genes_by_ko_class(ko_class_id, org, offset, limit)
210   
211    def get_genes_by_ko(self, ko_id, org):
212        return self.service.get_genes_by_ko(ko_id, org)
213   
214    #########
215    # Pathway
216    #########
217   
218    def mark_pathway_by_objects(self, pathway_id, object_id_list):
219        return self.service.mark_pathway_by_objects(pathway_id, object_id_list)
220   
221    def color_pathway_by_objects(self, pathway_id, object_id_list, fg_color_list, bg_color_list):
222        return self.service.color_pathway_by_objects(pathway_id, object_id_list, fg_color_list, bg_color_list)
223   
224    def color_pathway_by_elements(self, pathway_id, element_id_list, fg_color_list, bg_color_list):
225        return self.service.color_pathway_by_elements(pathway_id, element_id_list, fg_color_list, bg_color_list)
226   
227    def get_html_of_marked_pathway_by_objects(self, pathway_id, object_id_list):
228        return self.service.get_html_of_marked_pathway_by_objects(pathway_id, object_id_list)
229   
230    def get_html_of_colored_pathway_by_objects(self, pathway_id, object_id_list, fg_color_list, bg_color_list):
231        return self.service.get_html_of_colored_pathway_by_objects(pathway_id, object_id_list, fg_color_list, bg_color_list)
232   
233    def get_html_of_colored_pathway_by_elements(self, pathway_id, element_id_list, fg_color_list, bg_color_list):
234        return self.service.get_html_of_colored_pathway_by_elements(pathway_id, element_id_list, fg_color_list, bg_color_list)
235   
236    def get_references_by_pathway(self, pathway_id):
237        return self.service.get_references_by_pathway(pathway_id)
238   
239    def get_element_relations_by_pathway(self, pathway_id):
240        return self.service.get_element_relations_by_pathway(pathway_id)
241   
242   
243   
244    def get_genes_by_organism(self, organism, offset=None, limit=None):
245        if offset is None and limit is None:
246            offset = 0
247            limit = self.get_number_of_genes_by_organism(organism)
248           
249        return self.service.get_genes_by_organism(organism, offset, limit)
250   
251    def get_number_of_genes_by_organism(self, organism):
252        return self.service.get_number_of_genes_by_organism(organism)
253   
254    ####################
255    # Objects by pathway
256    ####################
257   
258    def get_elements_by_pathway(self, pathway_id):
259        return self.service.get_elements_by_pathway(pathway_id)
260   
261    def get_genes_by_pathway(self, pathway_id):
262        return self.service.get_genes_by_pathway(pathway_id)
263   
264    def get_enzymes_by_pathway(self, pathway_id):
265        return self.service.get_enzymes_by_pathway(pathway_id)
266   
267    def get_compounds_by_pathway(self, pathway_id):
268        return self.service.get_compounds_by_pathway(pathway_id)
269   
270    def get_drugs_by_pathway(self, pathway_id):
271        return self.service.get_drugs_by_pathway(pathway_id)
272   
273    def get_glycans_by_pathway(self, pathway_id):
274        return self.service.get_glycans_by_pathway(pathway_id)
275   
276    def get_reactions_by_pathway(self, pathway_id):
277        return self.get_reactions_by_pathway(pathway_id)
278   
279    def get_kos_by_pathway(self, pathway_id):
280        return self.service.get_kos_by_pathway(pathway_id)
281   
282    #####################
283    # Pathways by objects
284    #####################
285   
286    def get_pathways_by_genes(self, gene_list):
287        return map(str, self.service.get_pathways_by_genes(gene_list))
288   
289    def get_pathways_by_enzymes(self, enzyme_list):
290        return map(str, self.service.get_pathways_by_enzymes(enzyme_list))
291   
292    def get_pathways_by_compounds(self, compound_list):
293        return map(str, self.service.get_pathways_by_compounds(compound_list))
294   
295    def get_pathways_by_drugs(self, drug_list):
296        return map(str, self.service.get_pathways_by_drugs(drug_list))
297   
298    def get_pathways_by_glycans(self, glycan_list):
299        return map(str, self.service.get_pathways_by_glycans(glycan_list))
300   
301    def get_pathways_by_reactions(self, reaction_list):
302        return map(str, self.service.get_pathways_by_reactions(reaction_list))
303   
304    def get_pathways_by_kos(self, ko_list):
305        return map(str, self.service.get_pathways_by_kos(ko_list))
306   
307    ##########################
308    # Relations among pathways
309    ##########################
310   
311    def get_linked_pathways(self, pathway_id):
312        if not pathway_id.startswith("path:"):
313            pathway_id = "path:" + pathway_id
314        return map(str, self.service.get_linked_pathways(pathway_id))
315   
316   
317"""
318KEGG api with caching
319"""
320
321import os
322
323from . import caching
324from .caching import cached_method, cache_entry, touch_dir
325
326try:
327    from functools import lru_cache
328except ImportError:
329    # TODO: move a copy of lru_cache in .caching if distributing this as a
330    # standalone package
331    from Orange.misc import lru_cache
332
333   
334class CachedKeggApi(KeggApi):
335    def __init__(self, store=None):
336        KeggApi.__init__(self)
337        if store is None:
338            self.store = {}
339   
340    # Needed API for cached decorator.
341    def cache_store(self):
342        from . import conf
343        path = conf.params["cache.path"]
344        touch_dir(path)
345        return caching.Sqlite3Store(os.path.join(path,
346                                                 "kegg_api_cache.sqlite3"))
347   
348    def last_modified(self, args, kwargs=None):
349        return getattr(self, "default_release", "")
350   
351    def set_default_release(self, release):
352        self.default_release = release
353       
354   
355    ##################
356    # Meta information
357    ##################
358   
359    @lru_cache() # not persistently cached
360    def list_databases(self):
361        return KeggApi.list_databases(self)
362   
363    @cached_method
364    def list_organisms(self):
365        return KeggApi.list_organisms(self)
366   
367    @cached_method
368    def list_pathways(self, organism):
369        return KeggApi.list_pathways(self, organism)
370   
371    #######
372    # DBGET
373    #######
374   
375    @lru_cache() # not persistently cached
376    def binfo(self, db):
377        return KeggApi.binfo(self, db)
378   
379    @cached_method
380    def bfind(self, db, keywords):
381        return KeggApi.bfind(self, db, keywords)
382   
383    @cached_method
384    def bget(self, ids):
385        rval = KeggApi.bget(self, ids)
386        return rval
387   
388    @cached_method
389    def bget(self, ids):
390        if not isinstance(ids, basestring):
391            return self._batch_bget(ids)
392        else:
393            return KeggApi.bget(self, ids)
394       
395    def _batch_bget(self, ids):
396        if len(ids) > 100:
397            raise ValueError("Can batch at most 100 ids at a time.")
398       
399        bget = self.bget
400        uncached = []
401        with closing(bget.cache_store()) as store:
402            # Which ids are already cached
403            # TODO: Invalidate entries by release string.
404            for id in ids:
405                key = bget.key_from_args((id,))
406                if key not in store:
407                    uncached.append(id)
408               
409        if uncached:
410            rval = KeggApi.bget(self, uncached)
411            if rval is not None:
412                entrys = rval.split("///\n")
413            else:
414                entrys = []
415               
416            if entrys and not entrys[-1].strip():
417                # Delete the last newline if present
418                del entrys[-1]
419           
420#            if len(entrys) != len(uncached):
421#                raise ValueError("Batch contains invalid ids")
422       
423            with closing(bget.cache_store()) as store:
424                for id, entry in zip(uncached, entrys):
425                    key = bget.key_from_args((id,))
426                    if entry is not None:
427                        entry = entry + "///\n"
428                    store[key] = cache_entry(entry, mtime=datetime.now())
429       
430        # Finally join all the results, but drop all None objects
431        entries = filter(lambda e: e is not None, map(bget, ids))
432       
433        rval = "".join(entries)
434        return rval
435   
436    @cached_method
437    def btit(self, ids):
438        return KeggApi.btit(self, ids)
439   
440    @cached_method
441    def bconv(self, ids):
442        return KeggApi.bconv(self, ids)
443   
444    ########
445    # LinkDB
446    ########
447   
448    @cached_method
449    def get_linkdb_by_entry(self, entry_id, db, offset, limit):
450       return KeggApi.get_linkdb_by_entry(self, entry_id, db, offset, limit)
451       
452    @cached_method
453    def get_linkdb_between_databases(self, from_db, to_db, offset, limit):
454        return KeggApi.get_linkdb_between_databases(self, from_db, to_db, offset, limit)
455           
456    @cached_method
457    def get_genes_by_enzyme(self, enzyme_id, org):
458        return KeggApi.get_genes_by_enzyme(self, enzyme_id, org)
459   
460    @cached_method
461    def get_enzymes_by_gene(self, genes_id):
462        return KeggApi.get_enzymes_by_gene(self, genes_id)
463   
464    @cached_method
465    def get_enzymes_by_compound(self, compound_id):
466        return KeggApi.get_enzymes_by_compound(self, compound_id)
467   
468    @cached_method
469    def get_enzymes_by_glycan(self, glycan_id):
470        return KeggApi.get_enzymes_by_glycan(self, glycan_id)
471   
472    @cached_method
473    def get_enzymes_by_reaction(self, reaction_id):
474        return KeggApi.get_enzymes_by_reaction(self, reaction_id)
475   
476    @cached_method
477    def get_compounds_by_enzyme(self, enzyme_id):
478        return KeggApi.get_compounds_by_enzyme(self, enzyme_id)
479   
480    @cached_method
481    def get_compounds_by_reaction(self, reaction_id):
482        return KeggApi.get_compounds_by_reaction(self, reaction_id)
483   
484    @cached_method
485    def get_glycans_by_enzyme(self, enzyme_id):
486        return KeggApi.get_glycans_by_enzyme(self, enzyme_id)
487   
488    @cached_method
489    def get_glycans_by_reaction(self, reaction_id):
490        return KeggApi.get_glycans_by_reaction(self, reaction_id)
491   
492    @cached_method
493    def get_reactions_by_enzyme(self, enzyme_id):
494        return KeggApi.get_reactions_by_enzyme(self, enzyme_id)
495   
496    @cached_method
497    def get_reactions_by_compound(self, compound_id):
498        return KeggApi.get_reactions_by_compound(self, compound_id)
499   
500    @cached_method
501    def get_reactions_by_glycan(self, glycan_id):
502        return KeggApi.get_reactions_by_glycan(self, glycan_id)
503   
504    ######
505    # SSDB
506    ######
507   
508    @cached_method
509    def get_best_best_neighbors_by_gene(self, genes_id, offset, limit):
510        return KeggApi.get_best_best_neighbors_by_gene(self, genes_id, offset, limit)
511   
512    @cached_method
513    def get_best_neighbors_by_gene(self, genes_id, offset, limit):
514        return KeggApi.get_best_neighbors_by_gene(self, genes_id, offset, limit)
515   
516    @cached_method
517    def get_reverse_best_neighbors_by_gene(self, genes_id, offset, limit):
518        return KeggApi.get_reverse_best_neighbors_by_gene(self, genes_id, offset, limit)
519   
520    @cached_method
521    def get_paralogs_by_gene(self, genes_id, offset, limit):
522        return KeggApi.get_paralogs_by_gene(self, genes_id, offset, limit)
523   
524    #######
525    # Motif
526    #######
527   
528    @cached_method
529    def get_motifs_by_gene(self, genes_id, db):
530        return KeggApi.get_motifs_by_gene(self, genes_id, db)
531   
532    @cached_method
533    def get_genes_by_motifs(self, motif_id_list, offset, limit):
534        return KeggApi.get_genes_by_motifs(self, motif_id_list, offset, limit)
535
536    ####
537    # KO
538    ####
539   
540    @cached_method
541    def get_ko_by_gene(self, genes_id):
542        return KeggApi.get_ko_by_gene(self, genes_id)
543   
544    @cached_method
545    def get_ko_by_ko_class(self, ko_class_id):
546        return KeggApi.service.get_ko_by_ko_class(self, ko_class_id)
547   
548    @cached_method
549    def get_genes_by_ko_class(self, ko_class_id, org, offset, limit):
550        return KeggApi.get_genes_by_ko_class(self, ko_class_id, org, offset, limit)
551   
552    @cached_method
553    def get_genes_by_ko(self, ko_id, org):
554        return KeggApi.get_genes_by_ko(self, ko_id, org)
555   
556    #########
557    # Pathway
558    #########
559   
560    # TODO
561   
562   
563   
564    @cached_method
565    def get_genes_by_organism(self, organism, offset=None, limit=None):
566        return KeggApi.get_genes_by_organism(self, organism, offset=offset, limit=limit)
567   
568    @cached_method
569    def get_number_of_genes_by_organism(self, organism):
570        return KeggApi.get_number_of_genes_by_organism(self, organism)
571     
572    @cached_method
573    def get_pathways_by_genes(self, gene_list):
574        return KeggApi.get_pathways_by_genes(self, gene_list)
575   
576    @cached_method
577    def get_pathways_by_enzymes(self, enzyme_list):
578        return KeggApi.get_pathways_by_enzymes(self, enzyme_list)
579   
580    @cached_method
581    def get_pathways_by_compounds(self, compound_list):
582        return KeggApi.get_pathways_by_compounds(self, compound_list)
583   
584    @cached_method
585    def get_pathways_by_drugs(self, drug_list):
586        return KeggApi.get_pathways_by_drugs(self, drug_list)
587   
588    @cached_method
589    def get_pathways_by_glycans(self, glycan_list):
590        return KeggApi.get_pathways_by_glycans(self, glycan_list)
591   
592    @cached_method
593    def get_pathways_by_reactions(self, reaction_list):
594        return KeggApi.get_pathways_by_reactions(self, reaction_list)
595   
596    @cached_method
597    def get_pathways_by_kos(self, ko_list):
598        return KeggApi.get_pathways_by_kos(self, ko_list)
599   
600    @cached_method
601    def get_elements_by_pathway(self, pathway_id):
602        return KeggApi.get_elements_by_pathway(self, pathway_id)
603   
604    @cached_method
605    def get_genes_by_pathway(self, pathway_id):
606        return KeggApi.get_genes_by_pathway(self, pathway_id)
607   
608    @cached_method
609    def get_enzymes_by_pathway(self, pathway_id):
610        return KeggApi.get_enzymes_by_pathway(self, pathway_id)
611   
612    @cached_method
613    def get_compounds_by_pathway(self, pathway_id):
614        return KeggApi.get_compounds_by_pathway(self, pathway_id)
615   
616    @cached_method
617    def get_drugs_by_pathway(self, pathway_id):
618        return KeggApi.get_drugs_by_pathway(self, pathway_id)
619   
620    @cached_method
621    def get_glycans_by_pathway(self, pathway_id):
622        return KeggApi.get_glycans_by_pathway(self, pathway_id)
623   
624    @cached_method
625    def get_reactions_by_pathway(self, pathway_id):
626        return KeggApi.get_reactions_by_pathway(self, pathway_id)
627   
628    @cached_method
629    def get_kos_by_pathway(self, pathway_id):
630        return KeggApi.get_kos_by_pathway(self, pathway_id)
631   
Note: See TracBrowser for help on using the repository browser.