Skip to content

Commit

Permalink
Revert modifications to the settings configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jschaff committed May 29, 2024
1 parent 13b0717 commit c9670d1
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 211 deletions.
200 changes: 198 additions & 2 deletions biothings_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,204 @@

import requests

from .base import BiothingClient, str_types, __version__, df_avail, caching_avail
from .settings import CLIENT_SETTINGS, COMMON_KWARGS, COMMON_ALIASES
from .base import BiothingClient, __version__, alwayslist, caching_avail, df_avail, str_types
from .docstring.chem import DOCSTRING as CHEM_DOCSTRING
from .docstring.gene import DOCSTRING as GENE_DOCSTRING
from .docstring.variant import DOCSTRING as VARIANT_DOCSTRING
from .mixins.gene import MyGeneClientMixin
from .mixins.variant import MyVariantClientMixin
from .utils.variant import MYVARIANT_TOP_LEVEL_JSONLD_URIS

# ***********************************************
# * Aliases.
# *
# * (key, value) pairs where the function named "key" in utils.BiothingClient
# * is aliased as "value" in the returned client class.
# ***********************************************

# Function aliases common to all clients
COMMON_ALIASES = {
"_metadata": "metadata",
"_set_caching": "set_caching",
"_stop_caching": "stop_caching",
"_clear_cache": "clear_cache",
"_get_fields": "get_fields",
"_query": "query",
"_querymany": "querymany",
}

# Set project specific aliases
MYGENE_ALIASES = copy(COMMON_ALIASES)
MYGENE_ALIASES.update({"_getannotation": "getgene", "_getannotations": "getgenes"})
MYVARIANT_ALIASES = copy(COMMON_ALIASES)
MYVARIANT_ALIASES.update({"_getannotation": "getvariant", "_getannotations": "getvariants"})
MYCHEM_ALIASES = copy(COMMON_ALIASES)
MYCHEM_ALIASES.update({"_getannotation": "getchem", "_getannotations": "getchems"})
MYCHEM_ALIASES.update({"getchem": "getdrug", "getchems": "getdrugs"})
MYTAXON_ALIASES = copy(COMMON_ALIASES)
MYTAXON_ALIASES.update({"_getannotation": "gettaxon", "_getannotations": "gettaxa"})
MYDISEASE_ALIASES = copy(COMMON_ALIASES)
MYDISEASE_ALIASES.update({"_getannotation": "getdisease", "_getannotations": "getdiseases"})
MYGENESET_ALIASES = copy(COMMON_ALIASES)
MYGENESET_ALIASES.update({"_getannotation": "getgeneset", "_getannotations": "getgenesets"})

# ***********************************************
# * Kwargs.
# *
# * These keyword arguments are attached to the returned client class
# * on class creation.
# ***********************************************

# Object creation kwargs common to all clients
COMMON_KWARGS = {
"_pkg_user_agent_header": "biothings_client.py",
"_query_endpoint": "/query/",
"_metadata_endpoint": "/metadata",
"_metadata_fields_endpoint": "/metadata/fields",
"_top_level_jsonld_uris": [],
"_docstring_obj": {},
"_delay": 1,
"_step": 1000,
"_scroll_size": 1000,
"_max_query": 1000,
}
# project specific kwargs
MYGENE_KWARGS = copy(COMMON_KWARGS)
MYGENE_KWARGS.update(
{
"_default_url": "https://mygene.info/v3",
"_annotation_endpoint": "/gene/",
"_optionally_plural_object_type": "gene(s)",
"_default_cache_file": "mygene_cache",
"_entity": "gene",
"_docstring_obj": GENE_DOCSTRING,
}
)
MYVARIANT_KWARGS = copy(COMMON_KWARGS)
MYVARIANT_KWARGS.update(
{
"_default_url": "https://myvariant.info/v1",
"_annotation_endpoint": "/variant/",
"_optionally_plural_object_type": "variant(s)",
"_default_cache_file": "myvariant_cache",
"_entity": "variant",
"_top_level_jsonld_uris": MYVARIANT_TOP_LEVEL_JSONLD_URIS,
"_docstring_obj": VARIANT_DOCSTRING,
}
)
MYCHEM_KWARGS = copy(COMMON_KWARGS)
MYCHEM_KWARGS.update(
{
"_default_url": "https://mychem.info/v1",
"_annotation_endpoint": "/chem/",
"_optionally_plural_object_type": "chem(s)",
"_entity": "chem",
"_default_cache_file": "mychem_cache",
"_docstring_obj": CHEM_DOCSTRING,
}
)
MYDISEASE_KWARGS = copy(COMMON_KWARGS)
MYDISEASE_KWARGS.update(
{
"_default_url": "https://mydisease.info/v1",
"_annotation_endpoint": "/disease/",
"_optionally_plural_object_type": "disease(s)",
"_entity": "disease",
"_default_cache_file": "mydisease_cache",
}
)
MYTAXON_KWARGS = copy(COMMON_KWARGS)
MYTAXON_KWARGS.update(
{
"_default_url": "https://t.biothings.io/v1",
"_annotation_endpoint": "/taxon/",
"_optionally_plural_object_type": "taxon/taxa",
"_entity": "taxon",
"_default_cache_file": "mytaxon_cache",
}
)
MYGENESET_KWARGS = copy(COMMON_KWARGS)
MYGENESET_KWARGS.update(
{
"_default_url": "https://mygeneset.info/v1",
"_annotation_endpoint": "/geneset/",
"_optionally_plural_object_type": "geneset(s)",
"_entity": "geneset",
"_default_cache_file": "mygeneset_cache",
}
)

# ***********************************************
# * Client settings
# *
# * This object contains the client-specific settings necessary to
# * instantiate a new biothings client. The currently supported
# * clients are the keys of this object.
# *
# * class - the client Class name
# * class_kwargs - keyword arguments passed to Class on creation
# * function_aliases - client specific function aliases in Class
# * ancestors - a list of classes that Class inherits from
# ***********************************************

CLIENT_SETTINGS = {
"gene": {
"class_name": "MyGeneInfo",
"class_kwargs": MYGENE_KWARGS,
"attr_aliases": MYGENE_ALIASES,
"base_class": BiothingClient,
"mixins": [MyGeneClientMixin],
},
"variant": {
"class_name": "MyVariantInfo",
"class_kwargs": MYVARIANT_KWARGS,
"attr_aliases": MYVARIANT_ALIASES,
"base_class": BiothingClient,
"mixins": [MyVariantClientMixin],
},
"taxon": {
"class_name": "MyTaxonInfo",
"class_kwargs": MYTAXON_KWARGS,
"attr_aliases": MYTAXON_ALIASES,
"base_class": BiothingClient,
"mixins": [],
},
"drug": {
"class_name": "MyChemInfo",
"class_kwargs": MYCHEM_KWARGS,
"attr_aliases": MYCHEM_ALIASES,
"base_class": BiothingClient,
"mixins": [],
},
"chem": {
"class_name": "MyChemInfo",
"class_kwargs": MYCHEM_KWARGS,
"attr_aliases": MYCHEM_ALIASES,
"base_class": BiothingClient,
"mixins": [],
},
"compound": {
"class_name": "MyChemInfo",
"class_kwargs": MYCHEM_KWARGS,
"attr_aliases": MYCHEM_ALIASES,
"base_class": BiothingClient,
"mixins": [],
},
"disease": {
"class_name": "MyDiseaseInfo",
"class_kwargs": MYDISEASE_KWARGS,
"attr_aliases": MYDISEASE_ALIASES,
"base_class": BiothingClient,
"mixins": [],
},
"geneset": {
"class_name": "MyGenesetInfo",
"class_kwargs": MYGENESET_KWARGS,
"attr_aliases": MYGENESET_ALIASES,
"base_class": BiothingClient,
"mixins": [],
},
}


def copy_func(f, name=None):
Expand Down
9 changes: 6 additions & 3 deletions biothings_client/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Python Client for generic Biothings API services
"""

from __future__ import print_function

import logging
Expand Down Expand Up @@ -38,15 +39,14 @@
__version__ = "0.3.1"

logger = logging.getLogger("biothings.client")
logger.setLevel(logging.INFO)

if is_py27:
# we need to setup default log handler in Py 2.7
# Py 3.x does it by default
handler = logging.StreamHandler()
# formatter = logging.Formatter("%(levelname)s:%(name)s:%(message)s")
# handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

# Future work:
# Consider use "verbose" settings to control default logging output level
Expand Down Expand Up @@ -121,7 +121,6 @@ def __init__(self, url=None):
if self.url[-1] == "/":
self.url = self.url[:-1]
self.max_query = self._max_query

# delay and step attributes are for batch queries.
self.delay = self._delay # delay is ignored when requests made from cache.
self.step = self._step
Expand Down Expand Up @@ -392,6 +391,10 @@ def _getannotations(self, ids, fields=None, **kwargs):
.. Hint:: If you need to pass a very large list of input ids, you can pass a generator
instead of a full list, which is more memory efficient.
"""
if isinstance(ids, str_types):
ids = ids.split(",") if ids else []
if not (isinstance(ids, (list, tuple, Iterable))):
raise ValueError('input "ids" must be a list, tuple or iterable.')
if fields:
kwargs["fields"] = fields
kwargs = self._handle_common_kwargs(kwargs)
Expand Down
Loading

0 comments on commit c9670d1

Please sign in to comment.