source: orange-bioinformatics/Orange/bioinformatics/obiKEGG2/api.py @ 1625:cefeb35cbfc9

Revision 1625:cefeb35cbfc9, 21.1 KB checked in by mitar, 2 years ago (diff)

Moving files around.

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