source: orange-bioinformatics/orangecontrib/bio/obiGeneAtlas.py @ 1992:ef31099d4f65

Revision 1992:ef31099d4f65, 24.8 KB checked in by Ales Erjavec <ales.erjavec@…>, 8 weeks ago (diff)

Added a warning about the deprecated API when using obiGeneAtlas.

Line 
1"""
2========================================
3Gene Expression Atlas (``obiGeneAtlas``)
4========================================
5
6.. warning::
7
8    Deprecated!!!
9    Gene Expression Atlas REST api has been deprecated and will be removed
10    in the future.
11
12
13Interface to Gene Expression Atlas.
14
15`Gene Expression Atlas <http://www.ebi.ac.uk/gxa/>`_ is a curated subset of
16gene expression experiments in Array Express Archive.
17
18
19.. autofunction:: gene_expression_atlas
20
21.. autofunction:: default_gene_matcher
22
23.. autofunction:: to_taxid
24
25"""
26
27from __future__ import absolute_import
28
29import os
30import shelve
31import warnings
32from collections import defaultdict, namedtuple
33from contextlib import closing, contextmanager
34
35from Orange.utils import serverfiles
36
37from . import obiGene
38
39GeneResults = namedtuple("GeneResults", "id name synonyms expressions")
40ExpressionResults = namedtuple("ExpressionResults", "ef efv up down experiments")
41ExperimentExpression = namedtuple("ExperimentExpression", "accession expression pvalue")
42
43##
44GeneAtlasResult = GeneResults
45AtlasExpressions = ExpressionResults
46AtlasExperiment = ExperimentExpression
47##
48
49CACHE_VERSION = 1
50
51
52def _cache(name="AtlasGeneResult.shelve"):
53    """ Return a open cache instance (a shelve object).
54    """
55    if not os.path.exists(serverfiles.localpath("GeneAtlas")):
56        try:
57            os.makedirs(serverfiles.localpath("GeneAtlas"))
58        except OSError:
59            pass
60    cache = shelve.open(serverfiles.localpath("GeneAtlas", name))
61    if cache.get(name + "__CACHE_VERSION__", None) == CACHE_VERSION:
62        return cache
63    else:
64        cache.close()
65        cache = shelve.open(serverfiles.localpath("GeneAtlas", name), "n")
66        cache[name + "__CACHE_VERSION__"] = CACHE_VERSION
67        return cache
68
69
70SLEEP_TIME_MULTIPLIER = 3.0
71
72def gene_expression_atlas(genes, progress_callback=None):
73    """ Return GeneResults instances for genes (genes must be valid ensembl ids).
74    """
75    import time
76    genes = list(genes)
77    result_dict = {}
78    genes_not_cached = []
79    # See which genes are already cached
80    with closing(_cache()) as cache:
81        for gene in genes:
82            if str(gene) in cache:
83                result_dict[gene] = cache[str(gene)]
84            else:
85                genes_not_cached.append(gene)
86   
87    batch_size = 10
88    start = 0
89    res = []
90    while start < len(genes_not_cached):
91        batch = genes_not_cached[start: start + batch_size]
92        start += batch_size
93        start_time = time.time()
94        batch_res = batch_gene_atlas_expression(batch)
95        # Cache the new results.
96        # TODO: handle genes without any results.
97        genes_with_no_results = set(batch) - set(r.id for r in batch_res) 
98        with closing(_cache()) as cache:
99            for atlas_res in batch_res:
100                cache[str(atlas_res.id)] = atlas_res
101                result_dict[atlas_res.id] = atlas_res
102            for g in genes_with_no_results:
103                cache[str(g)] = None
104        res.extend(batch_res)
105        # Sleep
106        if start % (batch_size * 10) == 0:
107            # every 10 batches wait one minute before continuing.
108            time.sleep(60)
109        else:
110            time.sleep(min(20.0, SLEEP_TIME_MULTIPLIER*(time.time() - start_time)))
111           
112        if progress_callback:
113            progress_callback(100.0 * start / len(genes_not_cached))
114   
115    return [result_dict.get(g, None) for g in genes]
116
117   
118def batch_gene_atlas_expression(genes):
119    cond = GenePropertyCondition("Ensgene", "Is", genes)
120    res = run_query(cond, format="json")
121    results = res["results"]
122    results_genes = []
123    for one_result in results:
124        gene = one_result["gene"]
125        id = gene["id"]
126        name = gene["name"]
127        synonyms = gene.get("synonyms", [])
128        expressions = one_result["expressions"]
129        result_expressions = []
130        for expression in expressions:
131            ef = expression["ef"]
132            efv = expression["efv"]
133            up = expression["upExperiments"]
134            down = expression["downExperiments"]
135            experiments = expression["experiments"]
136            result_experiment = []
137            for exp in experiments:
138                if "accession" in exp:
139                    exp_accession = exp["accession"]
140                elif "experimentAccession" in exp:
141                    exp_accession = exp["experimentAccession"]
142                else:
143                    raise KeyError()
144                if "expression" in exp:
145                    updown = exp["expression"]
146                elif "updn" in exp:
147                    updown = exp["updn"]
148                else:
149                    raise KeyError
150                pval = exp["pvalue"]
151                result_experiment.append(ExperimentExpression(exp_accession, updown, pval))
152            result_expressions.append(ExpressionResults(ef, efv, up, down, result_experiment))
153        results_genes.append(GeneResults(id, name, synonyms, result_expressions))
154    return results_genes
155
156
157def default_gene_matcher(organism):
158    """ Return a default gene matcher for organism
159    (targeting Ensembl gene ids).
160   
161    """
162    taxid = to_taxid(organism)
163    matcher = obiGene.matcher(
164      [obiGene.GMEnsembl(taxid),
165       [obiGene.GMEnsembl(taxid),
166        obiGene.GMNCBI(taxid)]]
167    )
168    matcher.set_targets(obiGene.EnsembleGeneInfo(taxid).keys())
169    return matcher
170
171from Orange.utils import lru_cache
172
173
174@lru_cache(maxsize=3)
175def _cached_default_gene_matcher(organism):
176    return default_gene_matcher(organism)
177   
178
179def get_atlas_summary(genes, organism, gene_matcher=None,
180                      progress_callback=None):
181    """ Return 3 dictionaries containing a summary of atlas information
182    about three experimental factors:
183   
184        - Organism Part (OP)
185        - Disease State (DS)
186        - Cell type (CT)
187   
188    Each dictionary contains query genes as keys. Values are dictionaries
189    mapping factor values to a 2-tuple containig the count of up regulated
190    and down regulated experiments.
191   
192    Example ::
193   
194        >>> get_atlas_summary(["ENSG00000159216"], "Homo sapiens")
195        ({u'RUNX1': ...
196       
197    """
198    if gene_matcher is None:
199        gene_matcher = _cached_default_gene_matcher(organism)
200       
201    matched, unmatched = [], []
202    for gene, match in zip(genes, map(gene_matcher.umatch, genes)):
203        if match:
204            matched.append(match)
205        else:
206            unmatched.append(gene)
207    if unmatched:
208        warnings.warn("Unmatched genes " + "," .join(["%r" % g for g in unmatched]))
209   
210    results = gene_expression_atlas(matched, progress_callback=progress_callback)
211   
212    def collect_ef_summary(result, ef, summary):
213        for exp in result.expressions:
214            if exp.ef == ef:
215                if any([exp.up, exp.down]):
216                    summary[result.name][exp.efv] = (exp.up, exp.down)
217       
218           
219    op, ds, ct = defaultdict(dict), defaultdict(dict), defaultdict(dict)
220    for res in results:
221        if res:
222            collect_ef_summary(res, "organism_part", op)
223            collect_ef_summary(res, "disease_state", ds)
224            collect_ef_summary(res, "cell_type", ct)
225       
226    return dict(op), dict(ds), dict(ct)
227
228def drop_none(iter):
229    """ Drop all ``None`` from the iterator.
230    """
231    for e in iter:
232        if e is not None:
233            yield e
234           
235def construct_atlas_gene_sets(genes, organism, factors=["organism_part",
236                                    "disease_state", "cell_type"],
237                              max_pvalue=1e-5):
238    """ Construct gene sets for atlas experimental factor values in
239    ``factors``.
240    """
241    results = gene_expression_atlas(genes)
242    sets = defaultdict(list)
243   
244    for res in drop_none(results):
245        for exp in res.expressions:
246            if exp.ef not in factors:
247                continue
248            diff_exp = [e for e in exp.experiments \
249                        if e.pvalue <= max_pvalue]
250            if diff_exp:
251                sets[exp.ef, exp.efv].append(res.id)
252
253    organism = "+".join(organism.lower().split())
254    from .obiGeneSets import GeneSets, GeneSet
255   
256    def display_string(name):
257        return name.capitalize().replace("_", " ")
258   
259    gene_sets = []
260    for (ef, efv), genes in sets.items():
261        ef_display = display_string(ef)
262        gs = GeneSet(genes, "Diff. expressed in %s=%s." % (ef_display, efv), id=ef + ":" + efv,
263                     description="Diff. expressed in %s=%s" % (ef_display, efv),
264                     link="http://www.ebi.ac.uk/gxa/qrs?specie_0={organism}&gprop_0=&gnot_0=&gval_0=&fact_1=&fexp_1=UPDOWN&fmex_1=&fval_1=%22{efv}%22+&view=hm".format( \
265                            organism=organism, efv="+".join(efv.lower().split())),
266                     hierarchy=("Gene expression atlas", ef_display))
267        gene_sets.append(gs)
268    return GeneSets(gene_sets)
269
270
271# Mapping for common taxids from obiTaxonomy
272TAXID_TO_ORG = {"": "Anopheles gambiae",
273                "3702": "Arabidopsis thaliana",
274                "9913": "Bos taurus",
275                "6239": "Caenorhabditis elegans",
276                "7955": "Danio rerio",
277                "7227": "Drosophila melanogaster",
278                "": "Epstein barr virus",
279                "": "Gallus gallus",
280                "9606": "Homo sapiens",
281                "": "Human cytomegalovirus",
282                "": "Kaposi sarcoma-associated herpesvirus",
283                "10090": "Mus musculus",
284                "10116": "Rattus norvegicus",
285                "4932": "Saccharomyces cerevisiae",
286                "4896": "Schizosaccharomyces pombe",
287                "8355": "Xenopus laevis"
288     }
289
290def to_taxid(name):
291    dd = dict((v, k) for k, v in TAXID_TO_ORG.items())
292    if name in dd:
293        return dd[name]
294    else:
295        from . import obiTaxonomy as tax
296        ids = tax.to_taxid(name, mapTo=TAXID_TO_ORG.keys())
297        if ids:
298            return ids.pop()
299        else:
300            raise ValueError("Unknown organism.")
301
302
303__doc__ += """\
304Low level REST query interface
305------------------------------
306
307Use `query_atlas_simple` for simple querys.
308
309Example (query human genes for experiments in which they are up regulated) ::
310
311    >>> run_simple_query(genes=["SORL1", "PSIP1", "CDKN1C"], regulation="up", organism="Homo sapiens")
312    {u'...
313   
314Or use the `AtlasCondition` subclasses in this module to construct a more
315advanced query and use the `run_query` function.
316
317Example (query human genes annotated to the GO term 'transporter activity'
318that are up regulated in the liver in at least three experiments) ::
319
320    >>> go_cond = GenePropertyCondition("Goterm", "Is", "transporter activity")
321    >>> liver_cond = ExperimentalFactorCondition("Organism_part", "up", 3, "liver")
322    >>> org_cond = OrganismCondition("Homo sapiens")
323    >>> cond_list = ConditionList([go_cond, liver_cond, org_cond])
324    >>> run_query(cond_list)
325    {u'...
326
327"""
328
329import urllib2
330from  StringIO import StringIO
331import json
332from xml.etree.ElementTree import ElementTree
333
334parse_json = json.load
335
336
337def parse_xml(stream):
338    """ Parse an xml stream into an instance of xml.etree.ElementTree.ElementTree.
339    """
340    return ElementTree(file=stream)
341
342
343class GeneExpressionAtlasConenction(object):
344    """
345    A connection to Gene Expression Atlas database.
346
347    :param address:
348        Address of the GXA server (default: http://www-test.ebi.ac.uk/gxa/api/deprecated).
349    :param timeout:
350        Socket timeout (default 30).
351    :param cache:
352        A dict like object to use as a cache.
353
354    """
355    DEFAULT_ADDRESS = "http://www-test.ebi.ac.uk/gxa/api/deprecated"
356    DEFAULT_CACHE = serverfiles.localpath(
357        "GeneAtlas", "GeneAtlasConnectionCache.shelve")
358
359    def __init__(self, address=None, timeout=30, cache=None):
360
361        self.address = address if address is not None else self.DEFAULT_ADDRESS
362        self.timeout = timeout
363        self.cache = cache if cache is not None else self.DEFAULT_CACHE
364
365    def query(self, condition, format="json", start=None, rows=None, indent=False):
366        warnings.warn(
367            "The Gene Expression Atlas REST api has been deprecated and " +
368            "will be removed in the future.",
369            UserWarning)
370
371        url = self.address + "?" + condition.rest()
372        if start is not None and rows is not None:
373            url += "&start={0}&rows={1}".format(start, rows)
374        url += "&format={0}".format(format)
375        if indent:
376            url += "&indent"
377        #print url
378
379        if self.cache is not None:
380            return self._query_cached(url, format)
381        else:
382            return urllib2.urlopen(url)
383
384    def _query_cached(self, url, format):
385        if self.cache is not None:
386            with self.open_cache("r") as cache:
387                if url in cache:
388                    return StringIO(cache[url])
389
390            response = urllib2.urlopen(url)
391            contents = response.read()
392            # Test if the contents is a valid json or xml string (some
393            # times the stream just stops in the middle, so this makes
394            # sure we don't cache an invalid response
395            # TODO: what about errors (e.g. 'cannot handle the
396            # query in a timely fashion'
397            if format == "json":
398                parse_json(StringIO(contents))
399            else:
400                parse_xml(StringIO(contents))
401
402            with self.open_cache("w") as cache:
403                cache[url] = contents
404
405            return StringIO(contents)
406        else:
407            return urllib2.urlopen(url)
408
409    def open_cache(self, flag="r"):
410        """
411        Return a context manager for a dict like object.
412        """
413        if isinstance(self.cache, basestring):
414            try:
415                return closing(_open_shelve(self.cache, flag))
416            except Exception:
417                return fake_closing({})
418        else:
419            return fake_closing(self.cache)
420
421
422def _open_shelve(filename, flag="r"):
423    dirname = os.path.dirname(filename)
424    if not os.path.isdir(dirname):
425        os.makedirs(dirname)
426    exists = os.path.exists(filename)
427    if flag in ["r", "w"] and not exists:
428        # needs to be created first
429        # XXX: Race condition
430        s = shelve.open(filename, "c")
431        s.close()
432
433    return shelve.open(filename, flag)
434
435
436@contextmanager
437def fake_closing(obj):
438    yield obj
439   
440   
441# Names of all Gene Property filter names
442GENE_FILTERS = \
443    ["Name", # Gene name
444     "Goterm", #Gene Ontology Term
445     "Interproterm", #InterPro Term
446     "Disease", #Gene-Disease Assocation
447     "Keyword", #Gene Keyword
448     "Protein", #Protein
449
450     "Dbxref", #Other Database Cross-Refs
451     "Embl", #EMBL-Bank ID
452     "Ensfamily", #Ensembl Family
453     "Ensgene", #Ensembl Gene ID
454
455     "Ensprotein", #Ensembl Protein ID
456     "Enstranscript", #Ensembl Transcript ID
457     "Goid", #Gene Ontology ID
458     "Image", #IMAGE ID
459     "Interproid", #InterPro ID
460     "Locuslink", #Entrez Gene ID
461
462     "Omimid", #OMIM ID
463     "Orf", #ORF
464     "Refseq", #RefSeq ID
465     "Unigene", #UniGene ID
466     "Uniprot", #UniProt Accession
467
468     "Hmdb", #HMDB ID
469     "Chebi", #ChEBI ID
470     "Cas", #CAS
471     "Uniprotmetenz", #Uniprotmetenz
472     "Gene", #Gene Name or Identifier
473     "Synonym", #Gene Synonym
474     ]
475   
476# Valid Gene Property filter qualifiers
477GENE_FILTER_QUALIFIERS =\
478    ["Is",
479     "IsNot"
480     ]
481
482# Organisms in the Atlas
483ATLAS_ORGANISMS = \
484    ["Anopheles gambiae",
485     "Arabidopsis thaliana",
486     "Bacillus subtilis",
487     "Bos taurus",
488     "Caenorhabditis elegans",
489     "Canis familiaris",
490     "Ciona intestinalis",
491     "Ciona savignyi",
492     "Danio rerio",
493     "Dasypus novemcinctus",
494     "Drosophila melanogaster",
495     "Epstein barr virus",
496     "Equus caballus",
497     "Gallus gallus",
498     "Gorilla gorilla",
499     "Homo sapiens",
500     "Human cytomegalovirus",
501     "Human immunodeficiency virus",
502     "Kaposi sarcoma-associated herpesvirus",
503     "Macaca mulatta",
504     "Monodelphis domestica",
505     "Mus musculus",
506     "Ornithorhynchus anatinus",
507     "Oryza sativa",
508     "Pan troglodytes",
509     "Pongo abelii",
510     "Populus trichocarpa",
511     "Rattus norvegicus",
512     "Saccharomyces cerevisiae",
513     "Schizosaccharomyces pombe",
514     "Sus scrofa",
515     "Unknown",
516     "Vitis vinifera",
517     "Xenopus laevis",
518     "Xenopus tropicalis"
519     ]
520   
521def ef_ontology():
522    """ Return the `EF <http://www.ebi.ac.uk/efo/>`_ (Experimental Factor) ontology
523    """
524    from . import obiOntology
525    from Orange.utils import serverfiles
526    # Should this be in the OBOFoundry (Ontology) domain
527    try:
528        file = open(serverfiles.localpath_download("ArrayExpress", "efo.obo"), "rb")
529    except urllib2.HTTPError:
530        file = urllib2.urlopen("http://efo.svn.sourceforge.net/svnroot/efo/trunk/src/efoinobo/efo.obo")
531    return obiOntology.OBOOntology(file)
532
533
534class Condition(object):
535    """ Base class for Gene Expression Atlas query condition
536    """
537    def validate(self):
538        """ Validate condition in a subclass.
539        """
540        raise NotImplementedError
541   
542    def rest(self):
543        """ Return a REST query part in a subclass.
544        """
545        raise NotImplementedError
546   
547   
548class ConditionList(list, Condition):
549    """ A list of AtlasCondition instances.
550    """ 
551    def validate(self):
552        for item in self:
553            item.validate()
554       
555    def rest(self):
556        return "&".join(cond.rest() for cond in self)
557
558
559class GenePropertyCondition(Condition):
560    """ An atlas gene filter condition.
561   
562    :param property: Property of the gene. If None or "" all properties
563        will be searched.
564    :param qualifier: Qualifier can be 'Is' or 'IsNot'
565    :param value: The value to search for.
566   
567    Example ::
568   
569        >>> # Condition on a gene name
570        >>> condition = GenePropertyCondition("Name", "Is", "AS3MT")
571        >>> # Condition on genes from a GO Term
572        >>> condition = GenePropertyCondition("Goterm", "Is", "p53 binding")
573        >>> # Condition on disease association
574        >>> condition = GenePropertyCondition("Disease", "Is", "cancer")
575       
576    """
577    def __init__(self, property, qualifier, value):
578        self.property = property or ""
579        self.qualifier = qualifier
580        if isinstance(value, basestring):
581            self.value = value.replace(" ", "+")
582        elif isinstance(value, list):
583            self.value = "+".join(value)
584        else:
585            raise ValueError(value)
586       
587        self.validate()
588       
589    def validate(self):
590        assert(self.property in GENE_FILTERS + [""])
591        assert(self.qualifier in GENE_FILTER_QUALIFIERS + [""])
592       
593    def rest(self):
594        return "gene{property}{qualifier}={value}".format(**self.__dict__)
595       
596       
597class ExperimentalFactorCondition(Condition):
598    """ An atlas experimental factor filter condition.
599   
600    :param factor: EFO experiamntal factor
601    :param regulation: "up", "down", "updown", "any" or "none"
602    :param n: Minimum number of of experimants with this condition
603    :param value: Experimantal factor value
604   
605    Example ::
606   
607        >>> # Any genes up regulated in at least 3 experiments involving cancer.
608        >>> condition = ExperimentalFactorCondition("", "up", 3, "cancer")
609        >>> # Only genes which are up/down regulated in the heart in at least one experiment.
610        >>> condition = ExperimentalFactorCondition("Organism_part", "updown", 1, "heart")
611       
612    """
613    def __init__(self, factor, regulation, n, value):
614        self.factor = factor
615        self.regulation = regulation
616        self.n = n
617        self.value = value
618        self.validate()
619       
620    def validate(self):
621        # TODO: validate the factor and value
622#        assert(self.factor in ef_ontology())
623        assert(self.regulation in ["up", "down", "updown", "none", "any"])
624       
625    def rest(self):
626        return "{regulation}{n}In{factor}={value}".format(**self.__dict__)
627       
628       
629class OrganismCondition(Condition):
630    """ Condition on organism.
631    """
632    def __init__(self, organism):
633        self.organism = organism
634        self.validate()
635       
636    def validate(self):
637        assert(self.organism in ATLAS_ORGANISMS)
638       
639    def rest(self):
640        return "species={0}".format(self.organism.replace(" ", "+").lower())
641       
642       
643class ExperimentCondition(Condition):
644    """ Condition on experiement
645   
646    :param property: Property of the experiment. If None or "" all properties
647        will be searched.
648    :param qualifier: Qualifier can be 'Has' or 'HasNot'
649    :param value: The value to search for.
650   
651    Example ::
652   
653        >>> # Condition on a experiemnt acession
654        >>> condition = ExperimentCondition("", "", "E-GEOD-24283")
655        >>> # Condition on experiments involving lung
656        >>> condition = ExperimentCondition("Organism_part", "Has", "lung")
657       
658    """
659    EXPERIMENT_FILTER_QUALIFIERS = [
660                "Has",
661                "HasNot"]
662   
663    def __init__(self, property, qualifier, value):
664        self.property = property
665        self.qualifier = qualifier
666        if isinstance(value, basestring):
667            self.value = value.replace(" ", "+")
668        elif isinstance(value, list):
669            self.value = "+".join(value)
670        else:
671            raise ValueError(value)
672       
673        self.validate()
674       
675    def validate(self):
676        # TODO: check to EFO factors
677#        assert(self.property in EXPERIMENT_FILTERS + [""])
678        assert(self.qualifier in self.EXPERIMENT_FILTER_QUALIFIERS + [""])
679       
680    def rest(self):
681        return "experiment{property}{qualifier}={value}".format(**self.__dict__)
682       
683       
684class GeneExpressionAtlasError(Exception):
685    """ An error response from the Atlas server.
686    """
687    pass
688   
689   
690def __check_atlas_error_json(response):
691    if "error" in response:
692        raise GeneExpressionAtlasError(response["error"])
693    return response
694 
695     
696def __check_atlas_error_xml(response):
697    error = response.find("error")
698    if error is not None:
699        raise GeneExpressionAtlasError(error.text)
700    return response
701   
702       
703def run_simple_query(genes=None, regulation=None, organism=None,
704                     condition=None, format="json", start=None,
705                     rows=None):
706    """ A simple Gene Atlas query.
707   
708    :param genes: A list of gene names to search for.
709    :param regulation: Search for experiments in which `genes` are "up",
710        "down", "updown", "any" or "none" regulated. If "any" all experiments
711        are searched.
712    :param organism: Search experiments for organism. If None all experiments
713        are searched.
714    :param condition: An EFO factor value (e.g. "brain")
715   
716    Example ::
717       
718        >>> run_simple_query(genes=['Pou5f1', 'Dppa3'], organism="Mus musculus")
719        {u'...
720       
721        >>> run_simple_query(genes=['Pou5f1', 'Dppa3'], regulation="up", organism="Mus musculus")
722        {u'...
723       
724        >>> run_simple_query(genes=['Pou5f1', 'Dppa3'], regulation="up", condition="liver", organism="Mus musculus")
725        {u'...
726       
727    """
728    conditions = ConditionList()
729    if genes:
730        conditions.append(GenePropertyCondition("Gene", "Is", genes))
731    if regulation or condition:
732        regulation = "any" if regulation is None else regulation
733        condition = "" if condition is None else condition
734        conditions.append(ExperimentalFactorCondition("", regulation, 1, condition))
735    if organism:
736        conditions.append(OrganismCondition(organism))
737       
738    connection = GeneExpressionAtlasConenction()
739    results = connection.query(conditions, format=format, start=start,
740                               rows=rows)
741    if format == "json":
742        return parse_json(results)
743    else:
744        return parse_xml(results)
745
746"""\
747.. todo:: can this be implemented query_atlas(organism="...", Locuslink="...", Chebi="...", up3InCompound="..." downInEFO="...")
748      Need a full list of accepted factors
749"""
750
751def run_query(condition, format="json", start=None, rows=None, indent=False, connection=None):
752    """ Query Atlas based on a `condition` (instance of :class:`Condition`)
753   
754    Example ::
755       
756        >>> condition1 = GenePropertyCondition("Goterm", "Is", "p53 binding")
757        >>> condition2 = ExperimentalFactorCondition("Organism_part", "up", 3, "heart")
758        >>> condition = ConditionList([condition1, condition2])
759        >>> run_query(condition)
760        {u'...
761       
762    """
763    if connection is None:
764        connection = GeneExpressionAtlasConenction()
765    results = connection.query(condition, format=format, start=start,
766                               rows=rows, indent=indent)
767    if format == "json":
768        response = parse_json(results)
769        return __check_atlas_error_json(response)
770    else:
771        response = parse_xml(results)
772        return __check_atlas_error_xml(response)
773   
774def test():
775    from pprint import pprint   
776    pprint(get_atlas_summary(['Pou5f1', 'Dppa3'], 'Mus musculus'))
777       
778    pprint(get_atlas_summary(['PDLIM5', 'FGFR2' ], 'Homo sapiens'))
779    import doctest 
780    doctest.testmod(optionflags=doctest.ELLIPSIS)
781   
782if __name__ == "__main__":
783    test()
Note: See TracBrowser for help on using the repository browser.