Skip to content
Snippets Groups Projects
Commit 1281606e authored by AndiMajore's avatar AndiMajore
Browse files

python_nedrex code (for now)

parent 214e6c6b
No related branches found
No related tags found
No related merge requests found
Showing
with 1474 additions and 0 deletions
.. include:: ../README.rst
=====
Usage
=====
To use python-nedrex in a project::
import python_nedrex
black -l 120 python_nedrex
isort --profile black python_nedrex
flake8 --max-line-length=120 python_nedrex
pylint --max-line-length=120 python_nedrex
bandit python_nedrex
mypy --strict python_nedrex
[MASTER]
# NOTE: This has been modified to disable C0114, C0115, and C0116 (docstrings)
# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-allow-list=
# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code. (This is an alternative name to extension-pkg-allow-list
# for backward compatibility.)
extension-pkg-whitelist=
# Return non-zero exit code if any of these messages/categories are detected,
# even if score is above --fail-under value. Syntax same as enable. Messages
# specified are enabled, while categories only check already-enabled messages.
fail-on=
# Specify a score threshold to be exceeded before program exits with error.
fail-under=10.0
# Files or directories to be skipped. They should be base names, not paths.
ignore=CVS
# Add files or directories matching the regex patterns to the ignore-list. The
# regex matches against paths and can be in Posix or Windows format.
ignore-paths=
# Files or directories matching the regex patterns are skipped. The regex
# matches against base names, not paths. The default value ignores emacs file
# locks
ignore-patterns=^\.#
# Python code to execute, usually for sys.path manipulation such as
# pygtk.require().
#init-hook=
# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the
# number of processors available to use.
jobs=1
# Control the amount of potential inferred values when inferring a single
# object. This can help the performance when dealing with large functions or
# complex, nested conditions.
limit-inference-results=100
# List of plugins (as comma separated values of python module names) to load,
# usually to register additional checkers.
load-plugins=
# Pickle collected data for later comparisons.
persistent=yes
# Minimum Python version to use for version dependent checks. Will default to
# the version used to run pylint.
py-version=3.6
# Discover python modules and packages in the file system subtree.
recursive=no
# When enabled, pylint would attempt to guess common misconfiguration and emit
# user-friendly hints instead of false-positive error messages.
suggestion-mode=yes
# Allow loading of arbitrary C extensions. Extensions are imported into the
# active Python interpreter and may run arbitrary code.
unsafe-load-any-extension=no
[MESSAGES CONTROL]
# Only show warnings with the listed confidence levels. Leave empty to show
# all. Valid levels: HIGH, CONTROL_FLOW, INFERENCE, INFERENCE_FAILURE,
# UNDEFINED.
confidence=
# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifiers separated by comma (,) or put this
# option multiple times (only on the command line, not in the configuration
# file where it should appear only once). You can also use "--disable=all" to
# disable everything first and then re-enable specific checks. For example, if
# you want to run only the similarities checker, you can use "--disable=all
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use "--disable=all --enable=classes
# --disable=W".
disable=raw-checker-failed,
bad-inline-option,
locally-disabled,
file-ignored,
suppressed-message,
useless-suppression,
deprecated-pragma,
use-symbolic-message-instead,
missing-module-docstring,
missing-class-docstring,
missing-function-docstring
# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once). See also the "--disable" option for examples.
enable=c-extension-no-member
[REPORTS]
# Python expression which should return a score less than or equal to 10. You
# have access to the variables 'fatal', 'error', 'warning', 'refactor',
# 'convention', and 'info' which contain the number of messages in each
# category, as well as 'statement' which is the total number of statements
# analyzed. This score is used by the global evaluation report (RP0004).
evaluation=max(0, 0 if fatal else 10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10))
# Template used to display messages. This is a python new-style format string
# used to format the message information. See doc for all details.
#msg-template=
# Set the output format. Available formats are text, parseable, colorized, json
# and msvs (visual studio). You can also give a reporter class, e.g.
# mypackage.mymodule.MyReporterClass.
output-format=text
# Tells whether to display a full report or only the messages.
reports=no
# Activate the evaluation score.
score=yes
[REFACTORING]
# Maximum number of nested blocks for function / method body
max-nested-blocks=5
# Complete name of functions that never returns. When checking for
# inconsistent-return-statements if a never returning function is called then
# it will be considered as an explicit return statement and no message will be
# printed.
never-returning-functions=sys.exit,argparse.parse_error
[LOGGING]
# The type of string formatting that logging methods do. `old` means using %
# formatting, `new` is for `{}` formatting.
logging-format-style=old
# Logging modules to check that the string format arguments are in logging
# function parameter format.
logging-modules=logging
[SPELLING]
# Limits count of emitted suggestions for spelling mistakes.
max-spelling-suggestions=4
# Spelling dictionary name. Available dictionaries: none. To make it work,
# install the 'python-enchant' package.
spelling-dict=
# List of comma separated words that should be considered directives if they
# appear and the beginning of a comment and should not be checked.
spelling-ignore-comment-directives=fmt: on,fmt: off,noqa:,noqa,nosec,isort:skip,mypy:
# List of comma separated words that should not be checked.
spelling-ignore-words=
# A path to a file that contains the private dictionary; one word per line.
spelling-private-dict-file=
# Tells whether to store unknown words to the private dictionary (see the
# --spelling-private-dict-file option) instead of raising a message.
spelling-store-unknown-words=no
[MISCELLANEOUS]
# List of note tags to take in consideration, separated by a comma.
notes=FIXME,
XXX,
TODO
# Regular expression of note tags to take in consideration.
#notes-rgx=
[TYPECHECK]
# List of decorators that produce context managers, such as
# contextlib.contextmanager. Add to this list to register other decorators that
# produce valid context managers.
contextmanager-decorators=contextlib.contextmanager
# List of members which are set dynamically and missed by pylint inference
# system, and so shouldn't trigger E1101 when accessed. Python regular
# expressions are accepted.
generated-members=
# Tells whether missing members accessed in mixin class should be ignored. A
# class is considered mixin if its name matches the mixin-class-rgx option.
ignore-mixin-members=yes
# Tells whether to warn about missing members when the owner of the attribute
# is inferred to be None.
ignore-none=yes
# This flag controls whether pylint should warn about no-member and similar
# checks whenever an opaque object is returned when inferring. The inference
# can return multiple potential results while evaluating a Python object, but
# some branches might not be evaluated, which results in partial inference. In
# that case, it might be useful to still emit no-member and other checks for
# the rest of the inferred objects.
ignore-on-opaque-inference=yes
# List of class names for which member attributes should not be checked (useful
# for classes with dynamically set attributes). This supports the use of
# qualified names.
ignored-classes=optparse.Values,thread._local,_thread._local
# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis). It
# supports qualified module names, as well as Unix pattern matching.
ignored-modules=
# Show a hint with possible names when a member name was not found. The aspect
# of finding the hint is based on edit distance.
missing-member-hint=yes
# The minimum edit distance a name should have in order to be considered a
# similar match for a missing member name.
missing-member-hint-distance=1
# The total number of similar names that should be taken in consideration when
# showing a hint for a missing member.
missing-member-max-choices=1
# Regex pattern to define which classes are considered mixins ignore-mixin-
# members is set to 'yes'
mixin-class-rgx=.*[Mm]ixin
# List of decorators that change the signature of a decorated function.
signature-mutators=
[VARIABLES]
# List of additional names supposed to be defined in builtins. Remember that
# you should avoid defining new builtins when possible.
additional-builtins=
# Tells whether unused global variables should be treated as a violation.
allow-global-unused-variables=yes
# List of names allowed to shadow builtins
allowed-redefined-builtins=
# List of strings which can identify a callback function by name. A callback
# name must start or end with one of those strings.
callbacks=cb_,
_cb
# A regular expression matching the name of dummy variables (i.e. expected to
# not be used).
dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_
# Argument names that match this expression will be ignored. Default to name
# with leading underscore.
ignored-argument-names=_.*|^ignored_|^unused_
# Tells whether we should check for unused import in __init__ files.
init-import=no
# List of qualified module names which can have objects that can redefine
# builtins.
redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io
[FORMAT]
# Expected format of line ending, e.g. empty (any line ending), LF or CRLF.
expected-line-ending-format=
# Regexp for a line that is allowed to be longer than the limit.
ignore-long-lines=^\s*(# )?<?https?://\S+>?$
# Number of spaces of indent required inside a hanging or continued line.
indent-after-paren=4
# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1
# tab).
indent-string=' '
# Maximum number of characters on a single line.
max-line-length=100
# Maximum number of lines in a module.
max-module-lines=1000
# Allow the body of a class to be on the same line as the declaration if body
# contains single statement.
single-line-class-stmt=no
# Allow the body of an if to be on the same line as the test if there is no
# else.
single-line-if-stmt=no
[SIMILARITIES]
# Comments are removed from the similarity computation
ignore-comments=yes
# Docstrings are removed from the similarity computation
ignore-docstrings=yes
# Imports are removed from the similarity computation
ignore-imports=yes
# Signatures are removed from the similarity computation
ignore-signatures=no
# Minimum lines number of a similarity.
min-similarity-lines=4
[STRING]
# This flag controls whether inconsistent-quotes generates a warning when the
# character used as a quote delimiter is used inconsistently within a module.
check-quote-consistency=no
# This flag controls whether the implicit-str-concat should generate a warning
# on implicit string concatenation in sequences defined over several lines.
check-str-concat-over-line-jumps=no
[BASIC]
# Naming style matching correct argument names.
argument-naming-style=snake_case
# Regular expression matching correct argument names. Overrides argument-
# naming-style. If left empty, argument names will be checked with the set
# naming style.
#argument-rgx=
# Naming style matching correct attribute names.
attr-naming-style=snake_case
# Regular expression matching correct attribute names. Overrides attr-naming-
# style. If left empty, attribute names will be checked with the set naming
# style.
#attr-rgx=
# Bad variable names which should always be refused, separated by a comma.
bad-names=foo,
bar,
baz,
toto,
tutu,
tata
# Bad variable names regexes, separated by a comma. If names match any regex,
# they will always be refused
bad-names-rgxs=
# Naming style matching correct class attribute names.
class-attribute-naming-style=any
# Regular expression matching correct class attribute names. Overrides class-
# attribute-naming-style. If left empty, class attribute names will be checked
# with the set naming style.
#class-attribute-rgx=
# Naming style matching correct class constant names.
class-const-naming-style=UPPER_CASE
# Regular expression matching correct class constant names. Overrides class-
# const-naming-style. If left empty, class constant names will be checked with
# the set naming style.
#class-const-rgx=
# Naming style matching correct class names.
class-naming-style=PascalCase
# Regular expression matching correct class names. Overrides class-naming-
# style. If left empty, class names will be checked with the set naming style.
#class-rgx=
# Naming style matching correct constant names.
const-naming-style=UPPER_CASE
# Regular expression matching correct constant names. Overrides const-naming-
# style. If left empty, constant names will be checked with the set naming
# style.
#const-rgx=
# Minimum line length for functions/classes that require docstrings, shorter
# ones are exempt.
docstring-min-length=-1
# Naming style matching correct function names.
function-naming-style=snake_case
# Regular expression matching correct function names. Overrides function-
# naming-style. If left empty, function names will be checked with the set
# naming style.
#function-rgx=
# Good variable names which should always be accepted, separated by a comma.
good-names=i,
j,
k,
ex,
Run,
_
# Good variable names regexes, separated by a comma. If names match any regex,
# they will always be accepted
good-names-rgxs=
# Include a hint for the correct naming format with invalid-name.
include-naming-hint=no
# Naming style matching correct inline iteration names.
inlinevar-naming-style=any
# Regular expression matching correct inline iteration names. Overrides
# inlinevar-naming-style. If left empty, inline iteration names will be checked
# with the set naming style.
#inlinevar-rgx=
# Naming style matching correct method names.
method-naming-style=snake_case
# Regular expression matching correct method names. Overrides method-naming-
# style. If left empty, method names will be checked with the set naming style.
#method-rgx=
# Naming style matching correct module names.
module-naming-style=snake_case
# Regular expression matching correct module names. Overrides module-naming-
# style. If left empty, module names will be checked with the set naming style.
#module-rgx=
# Colon-delimited sets of names that determine each other's naming style when
# the name regexes allow several styles.
name-group=
# Regular expression which should only match function or class names that do
# not require a docstring.
no-docstring-rgx=^_
# List of decorators that produce properties, such as abc.abstractproperty. Add
# to this list to register other decorators that produce valid properties.
# These decorators are taken in consideration only for invalid-name.
property-classes=abc.abstractproperty
# Regular expression matching correct type variable names. If left empty, type
# variable names will be checked with the set naming style.
#typevar-rgx=
# Naming style matching correct variable names.
variable-naming-style=snake_case
# Regular expression matching correct variable names. Overrides variable-
# naming-style. If left empty, variable names will be checked with the set
# naming style.
#variable-rgx=
[CLASSES]
# Warn about protected attribute access inside special methods
check-protected-access-in-special-methods=no
# List of method names used to declare (i.e. assign) instance attributes.
defining-attr-methods=__init__,
__new__,
setUp,
__post_init__
# List of member names, which should be excluded from the protected access
# warning.
exclude-protected=_asdict,
_fields,
_replace,
_source,
_make
# List of valid names for the first argument in a class method.
valid-classmethod-first-arg=cls
# List of valid names for the first argument in a metaclass class method.
valid-metaclass-classmethod-first-arg=cls
[IMPORTS]
# List of modules that can be imported at any level, not just the top level
# one.
allow-any-import-level=
# Allow wildcard imports from modules that define __all__.
allow-wildcard-with-all=no
# Analyse import fallback blocks. This can be used to support both Python 2 and
# 3 compatible code, which means that the block might have code that exists
# only in one or another interpreter, leading to false positives when analysed.
analyse-fallback-blocks=no
# Deprecated modules which should not be used, separated by a comma.
deprecated-modules=
# Output a graph (.gv or any supported image format) of external dependencies
# to the given file (report RP0402 must not be disabled).
ext-import-graph=
# Output a graph (.gv or any supported image format) of all (i.e. internal and
# external) dependencies to the given file (report RP0402 must not be
# disabled).
import-graph=
# Output a graph (.gv or any supported image format) of internal dependencies
# to the given file (report RP0402 must not be disabled).
int-import-graph=
# Force import order to recognize a module as part of the standard
# compatibility libraries.
known-standard-library=
# Force import order to recognize a module as part of a third party library.
known-third-party=enchant
# Couples of modules and preferred modules, separated by a comma.
preferred-modules=
[DESIGN]
# List of regular expressions of class ancestor names to ignore when counting
# public methods (see R0903)
exclude-too-few-public-methods=
# List of qualified class names to ignore when counting class parents (see
# R0901)
ignored-parents=
# Maximum number of arguments for function / method.
max-args=5
# Maximum number of attributes for a class (see R0902).
max-attributes=7
# Maximum number of boolean expressions in an if statement (see R0916).
max-bool-expr=5
# Maximum number of branch for function / method body.
max-branches=12
# Maximum number of locals for function / method body.
max-locals=15
# Maximum number of parents for a class (see R0901).
max-parents=7
# Maximum number of public methods for a class (see R0904).
max-public-methods=20
# Maximum number of return / yield for function / method body.
max-returns=6
# Maximum number of statements in function / method body.
max-statements=50
# Minimum number of public methods for a class (see R0903).
min-public-methods=2
[EXCEPTIONS]
# Exceptions that will emit a warning when being caught. Defaults to
# "BaseException, Exception".
overgeneral-exceptions=BaseException,
Exception
"""Top-level package for python-nedrex."""
__author__ = """David James Skelton"""
__email__ = "james.skelton@newcastle.ac.uk"
__version__ = "0.1.1"
from typing import Optional
from attrs import define
@define
class _Config:
_url_base: Optional[str] = None
_url_vpd: Optional[str] = None
_api_key: Optional[str] = None
@property
def url_base(self) -> Optional[str]:
"""Returns the API URL base stored on the _Config instance"""
return self._url_base
@property
def url_vpd(self) -> Optional[str]:
"""Returns the VPD URL base stored on the _Config instance"""
return self._url_vpd
@property
def api_key(self) -> Optional[str]:
"""Returns the API key stored on the _Config instance"""
return self._api_key
def set_url_base(self, url_base: str) -> None:
"""Sets the URL base for the API in the configuration"""
self._url_base = url_base.rstrip("/")
def set_url_vpd(self, url_vpd: str) -> None:
"""Sets the URL base for the VPD in the configuration"""
self._url_vpd = url_vpd.rstrip("/")
def set_api_key(self, key: str) -> None:
"""Sets the API key in the configuration"""
self._api_key = key
config: _Config = _Config()
from pathlib import Path as _Path
from typing import IO as _IO
from typing import Any as _Any
from typing import Dict as _Dict
from typing import Optional as _Optional
from python_nedrex import config as _config
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import download_file as _download_file
from python_nedrex.common import http as _http
def bicon_request(
expression_file: _IO[str],
lg_min: int = 10,
lg_max: int = 15,
network: str = "DEFAULT",
) -> str:
files = {"expression_file": expression_file}
data = {"lg_min": lg_min, "lg_max": lg_max, "network": network}
url = f"{_config.url_base}/bicon/submit"
resp = _http.post(url, data=data, files=files, headers={"x-api-key": _config.api_key})
result: str = _check_response(resp)
return result
def check_bicon_status(uid: str) -> _Dict[str, _Any]:
url = f"{_config.url_base}/bicon/status"
resp = _http.get(url, params={"uid": uid}, headers={"x-api-key": _config.api_key})
result: _Dict[str, _Any] = _check_response(resp)
return result
def download_bicon_data(uid: str, target: _Optional[str] = None) -> None:
if target is None:
target = str(_Path(f"{uid}.zip").resolve())
url = f"{_config.url_base}/bicon/download?uid={uid}"
_download_file(url, target)
from typing import List as _List
from typing import Optional as _Optional
from python_nedrex import config as _config
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import check_status_factory as _check_status_factory
from python_nedrex.common import http as _http
def closeness_submit(
seeds: _List[str],
only_direct_drugs: bool = True,
only_approved_drugs: bool = True,
N: _Optional[int] = None, # pylint: disable=C0103
) -> str:
url = f"{_config.url_base}/closeness/submit"
body = {"seeds": seeds, "only_direct_drugs": only_direct_drugs, "only_approved_drugs": only_approved_drugs, "N": N}
resp = _http.post(url, json=body, headers={"x-api-key": _config.api_key})
result: str = _check_response(resp)
return result
check_closeness_status = _check_status_factory("/closeness/status")
def download_closeness_results(uid: str) -> str:
url = f"{_config.url_base}/closeness/download"
params = {"uid": uid}
resp = _http.get(url, params=params, headers={"x-api-key": _config.api_key})
result: str = _check_response(resp, return_type="text")
return result
import urllib.request
from typing import Any, Callable, Dict, Optional
import cachetools
import requests # type: ignore
from requests.adapters import HTTPAdapter # type: ignore
from urllib3.util.retry import Retry # type: ignore
from python_nedrex import config
from python_nedrex.exceptions import ConfigError, NeDRexError
# Start - code derived from https://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/
DEFAULT_TIMEOUT = 120
class TimeoutHTTPAdapter(HTTPAdapter): # type: ignore
def __init__(self, *args: Any, **kwargs: Any) -> None:
self.timeout = DEFAULT_TIMEOUT
if "timeout" in kwargs:
self.timeout = kwargs["timeout"]
del kwargs["timeout"]
super().__init__(*args, **kwargs)
# pylint: disable=arguments-differ
def send(self, request: requests.Request, **kwargs: Any) -> requests.Response:
timeout = kwargs.get("timeout")
if timeout is None:
kwargs["timeout"] = self.timeout
return super().send(request, **kwargs)
retry_strategy = Retry(total=3, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504])
http = requests.Session()
adapter = TimeoutHTTPAdapter(max_retries=retry_strategy)
http.mount("https://", adapter)
http.mount("http://", adapter)
# End - code derived from https://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/
def check_response(resp: requests.Response, return_type: str = "json") -> Any:
if resp.status_code == 401:
data = resp.json()
if data["detail"] == "An API key is required to access the requested data":
raise ConfigError("no API key set in the configuration")
if resp.status_code == 422:
data = resp.json()
raise NeDRexError(data["detail"])
if resp.status_code == 404:
raise NeDRexError("not found")
if return_type == "json":
data = resp.json()
elif return_type == "text":
data = resp.text
else:
raise NeDRexError(f"invalid value for return_type ({return_type!r}) in check_response")
return data
@cachetools.cached(cachetools.TTLCache(1, ttl=10))
def get_pagination_limit() -> Any:
url = f"{config.url_base}/pagination_max"
return requests.get(url, headers={"x-api-key": config.api_key}).json()
def check_pagination_limit(limit: Optional[int], upper_limit: int) -> None:
if limit and upper_limit < limit:
raise NeDRexError(f"limit={limit:,} is too great (maximum is {upper_limit:,})")
def download_file(url: str, target: str) -> None:
if config.api_key is not None:
opener = urllib.request.build_opener()
opener.addheaders = [("x-api-key", config.api_key)]
urllib.request.install_opener(opener)
try:
urllib.request.urlretrieve(url, target)
except urllib.error.HTTPError as err:
if err.code == 404:
raise NeDRexError("not found") from err
raise NeDRexError("unexpected failure") from err
def check_status_factory(url_suffix: str) -> Callable[[str], Dict[str, Any]]:
def return_func(uid: str) -> Dict[str, Any]:
url = f"{config.url_base}{url_suffix}"
params = {"uid": uid}
resp = http.get(url, params=params, headers={"x-api-key": config.api_key})
result: Dict[str, Any] = check_response(resp)
return result
return return_func
"""Module containing functions relating to the general routes in the NeDRex API
Additionally, this module also contains routes for obtaining API keys.
"""
from typing import Any as _Any
from typing import Dict as _Dict
from typing import Generator as _Generator
from typing import List as _List
from typing import Optional as _Optional
from typing import cast as _cast
from python_nedrex import config as _config
from python_nedrex.common import check_pagination_limit as _check_pagination_limit
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import get_pagination_limit as _get_pagination_limit
from python_nedrex.common import http as _http
from python_nedrex.decorators import check_url_base as _check_url_base
from python_nedrex.exceptions import NeDRexError as _NeDRexError
def _check_type(coll_name: str, coll_type: str) -> bool:
if coll_type == "edge":
if coll_name in get_edge_types():
return True
raise _NeDRexError(f"type={coll_name!r} not in NeDRex edge types")
if coll_type == "node":
if coll_name in get_node_types():
return True
raise _NeDRexError(f"type={coll_name!r} not in NeDRex node types")
raise _NeDRexError(f"_check_type received invalid coll_type={coll_type!r}")
@_check_url_base
def api_keys_active() -> bool:
"""Checks whether API keys are active for the instance of NeDRex set in the config
Returns True if the keys are active, False otherwise
"""
url = f"{_config.url_base}/api_key_setting"
response = _http.get(url)
if response.status_code != 200:
raise Exception("Unexpected non-200 status code")
return _cast(bool, response.json())
@_check_url_base
def get_api_key(*, accept_eula: bool = False) -> _Any:
"""Obtains a new API key from the NeDRex API.
This function will only return if accept_eula is explicitly set to True
"""
if accept_eula is not True:
raise _NeDRexError("an API key cannot be obtained unless accept_eula is set to True")
url = f"{_config.url_base}/admin/api_key/generate"
response = _http.post(url, json={"accept_eula": accept_eula})
return response.json()
@_check_url_base
def get_node_types() -> _Any:
"""
Returns the list of node types stored in NeDRexDB
Returns:
node_list (list[str]): List of node types in NeDRex
"""
url: str = f"{_config.url_base}/list_node_collections"
response = _http.get(url, headers={"x-api-key": _config.api_key})
node_list = _check_response(response)
return node_list
@_check_url_base
def get_edge_types() -> _Any:
"""
Returns a list of edge types stored in NeDRexDB
Returns:
edge_list (list[str]): List of edge types in NeDRex
"""
url: str = f"{_config.url_base}/list_edge_collections"
response = _http.get(url, headers={"x-api-key": _config.api_key})
edge_list = _check_response(response)
return edge_list
@_check_url_base
def get_collection_attributes(coll_type: str, include_counts: bool = False) -> _Any:
"""
Retrurns a list of the available attributes stored in NeDRex for the given type
Parameters:
type (str): The node or edge type to get available attributes for
Returns:
attr_list (list[str]): The list of available attributes for the specified node/edge type
"""
url: str = f"{_config.url_base}/{coll_type}/attributes"
response = _http.get(url, headers={"x-api-key": _config.api_key}, params={"include_counts": include_counts})
attr_list = _check_response(response)
return attr_list
@_check_url_base
def get_node_ids(coll_type: str) -> _Any:
"""
Returns a list of node identifiers in NeDRex for the given type
Parameters:
type(str): The node type to get IDs for
Returns:
node_ids (list[str]): The list of available node_ids for the specified node type
"""
_check_type(coll_type, "node")
url: str = f"{_config.url_base}/{coll_type}/attributes/primaryDomainId/json"
resp = _http.get(url, headers={"x-api-key": _config.api_key})
data = _check_response(resp)
node_ids = [i["primaryDomainId"] for i in data]
return node_ids
@_check_url_base
def get_nodes(
node_type: str,
attributes: _Optional[_List[str]] = None,
node_ids: _Optional[_List[str]] = None,
limit: _Optional[int] = None,
offset: int = 0,
) -> _Any:
"""
Returns nodes in NeDRex for the given type
Parameters:
node_type (str): The node type to collect
attributes (Optional[list[str]]): A list of attributes to return for the collected nodes. The default
(None) returns all attributes.
node_ids (Optional[list[str]]): A list of the specific node IDs to return. The default (None) returns all
nodes.
limit (Optional[int]): A limit for the number of records to be returned. The maximum value for this is set
by the API.
offset (int): The number of records to skip before returning records. Default is 0 (no records skipped).
Returns:
node_ids (list[str]): The list of available node_ids for the specified node type
"""
_check_type(node_type, "node")
upper_limit = _get_pagination_limit()
_check_pagination_limit(limit, upper_limit)
params = {"node_id": node_ids, "attribute": attributes, "offset": offset, "limit": limit}
resp = _http.get(
f"{_config.url_base}/{node_type}/attributes/json", params=params, headers={"x-api-key": _config.api_key}
)
items = _check_response(resp)
return items
@_check_url_base
def iter_nodes(
node_type: str, attributes: _Optional[_List[str]] = None, node_ids: _Optional[_List[str]] = None
) -> _Generator[_Dict[str, _Any], None, None]:
_check_type(node_type, "node")
upper_limit = _get_pagination_limit()
params: _Dict[str, _Any] = {"node_id": node_ids, "attribute": attributes, "limit": upper_limit}
offset = 0
while True:
params["offset"] = offset
resp = _http.get(
f"{_config.url_base}/{node_type}/attributes/json", params=params, headers={"x-api-key": _config.api_key}
)
data = _check_response(resp)
for doc in data:
yield doc
if len(data) < upper_limit:
break
offset += upper_limit
@_check_url_base
def get_edges(edge_type: str, limit: _Optional[int] = None, offset: _Optional[int] = None) -> _Any:
"""
Returns edges in NeDRex of the given type
Parameters:
edge_type (str): The node type to collect
limit (Optional[int]): A limit for the number of records to be returned. The maximum value for this is set
by the API.
offset (int): The number of records to skip before returning records. Default is 0 (no records skipped).
"""
_check_type(edge_type, "edge")
params = {"limit": limit, "offset": offset, "api_key": _config.api_key}
resp = _http.get(f"{_config.url_base}/{edge_type}/all", params=params, headers={"x-api-key": _config.api_key})
items = _check_response(resp)
return items
@_check_url_base
def iter_edges(edge_type: str) -> _Generator[_Dict[str, _Any], None, None]:
_check_type(edge_type, "edge")
upper_limit = _get_pagination_limit()
offset = 0
while True:
params = {"offset": offset, "limit": upper_limit}
resp = _http.get(f"{_config.url_base}/{edge_type}/all", params=params, headers={"x-api-key": _config.api_key})
data = _check_response(resp)
for doc in data:
yield doc
if len(data) < upper_limit:
break
offset += upper_limit
from typing import Any, Callable, TypeVar
from python_nedrex import config
from python_nedrex.exceptions import ConfigError
R = TypeVar("R")
def check_url_base(func: Callable[..., R]) -> Callable[..., R]:
def wrapped_fx(*args: Any, **kwargs: Any) -> Any:
if hasattr(config, "url_base") and config.url_base is not None:
return func(*args, **kwargs)
raise ConfigError("API URL is not set in the config")
return wrapped_fx
def check_url_vpd(func: Callable[..., R]) -> Callable[..., R]:
def wrapped_fx(*args: Any, **kwargs: Any) -> Any:
if hasattr(config, "url_vpd") and config.url_vpd is not None:
return func(*args, **kwargs)
raise ConfigError("VPD URL is not set in the config")
return wrapped_fx
from typing import List as _List
from python_nedrex import config as _config
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import check_status_factory as _check_status_factory
from python_nedrex.common import http as _http
def diamond_submit(
seeds: _List[str],
n: int, # pylint: disable=C0103
alpha: int = 1,
network: str = "DEFAULT",
edges: str = "all",
) -> str:
if edges not in {"limited", "all"}:
raise ValueError(f"invalid value for argument edges ({edges!r}), should be all|limited")
url = f"{_config.url_base}/diamond/submit"
body = {
"seeds": seeds,
"n": n,
"alpha": alpha,
"network": network,
"edges": edges,
}
resp = _http.post(url, json=body, headers={"x-api-key": _config.api_key})
result: str = _check_response(resp)
return result
check_diamond_status = _check_status_factory("/diamond/status")
def download_diamond_results(uid: str) -> str:
url = f"{_config.url_base}/diamond/download"
params = {"uid": uid}
resp = _http.get(url, params=params, headers={"x-api-key": _config.api_key})
result: str = _check_response(resp, return_type="text")
return result
"""Module containing python functions to access the disorder routes in the NeDRex API"""
from typing import Any as _Any
from typing import Callable as _Callable
from typing import List as _List
from typing import Union as _Union
from python_nedrex import config as _config
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import http as _http
from python_nedrex.decorators import check_url_base as _check_url_base
def _generate_route(path: str) -> _Callable[[_Union[str, _List[str]]], _Any]:
@_check_url_base
def new_func(codes: _Union[str, _List[str]]) -> _Any:
if isinstance(codes, str):
codes = [codes]
url = f"{_config.url_base}/disorder/{path}"
resp = _http.get(url, params={"q": codes}, headers={"x-api-key": _config.api_key})
return _check_response(resp)
return new_func
search_by_icd10 = _generate_route("get_by_icd10")
get_disorder_descendants = _generate_route("descendants")
get_disorder_ancestors = _generate_route("ancestors")
get_disorder_parents = _generate_route("parents")
get_disorder_children = _generate_route("children")
from typing import List as _List
from python_nedrex import config as _config
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import check_status_factory as _check_status_factory
from python_nedrex.common import http as _http
def domino_submit(seeds: _List[str], network: str = "DEFAULT") -> str:
url = f"{_config.url_base}/domino/submit"
body = {"seeds": seeds, "network": network}
resp = _http.post(url, json=body, headers={"x-api-key": _config.api_key})
result: str = _check_response(resp)
return result
check_domino_status = _check_status_factory("/domino/status")
class NeDRexError(Exception):
pass
class ConfigError(NeDRexError):
pass
from pathlib import Path as _Path
from typing import Any as _Any
from typing import Dict as _Dict
from typing import List as _List
from typing import Optional as _Optional
from python_nedrex import config as _config
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import download_file as _download_file
from python_nedrex.common import http as _http
# pylint: disable=R0913
def build_request(
nodes: _Optional[_List[str]] = None,
edges: _Optional[_List[str]] = None,
ppi_evidence: _Optional[_List[str]] = None,
include_ppi_self_loops: bool = False,
taxid: _Optional[_List[int]] = None,
drug_groups: _Optional[_List[str]] = None,
concise: bool = True,
include_omim: bool = True,
disgenet_threshold: float = 0.0,
use_omim_ids: bool = False,
split_drug_types: bool = False,
) -> str:
if nodes is None:
nodes = ["disorder", "drug", "gene", "protein"]
if edges is None:
edges = [
"disorder_is_subtype_of_disorder",
"drug_has_indication",
"drug_has_target",
"gene_associated_with_disorder",
"protein_encoded_by_gene",
"protein_interacts_with_protein",
]
if ppi_evidence is None:
ppi_evidence = ["exp"]
if taxid is None:
taxid = [9606]
if drug_groups is None:
drug_groups = ["approved"]
body = {
"nodes": nodes,
"edges": edges,
"ppi_evidence": ppi_evidence,
"ppi_self_loops": include_ppi_self_loops,
"taxid": taxid,
"drug_groups": drug_groups,
"concise": concise,
"include_omim": include_omim,
"disgenet_threshold": disgenet_threshold,
"use_omim_ids": use_omim_ids,
"split_drug_types": split_drug_types,
}
url = f"{_config.url_base}/graph/builder"
resp = _http.post(url, json=body, headers={"x-api-key": _config.api_key})
result: str = _check_response(resp)
return result
# pylint: enable=R0913
def check_build_status(uid: str) -> _Dict[str, _Any]:
url = f"{_config.url_base}/graph/details/{uid}"
resp = _http.get(url, headers={"x-api-key": _config.api_key})
result: _Dict[str, _Any] = _check_response(resp)
return result
def download_graph(uid: str, target: _Optional[str] = None) -> None:
if target is None:
target = str(_Path(f"{uid}.graphml").resolve())
url = f"{_config.url_base}/graph/download/{uid}/{uid}.graphml"
_download_file(url, target)
from typing import Any as _Any
from typing import Dict as _Dict
from typing import List as _List
from python_nedrex import config as _config
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import http as _http
def kpm_submit(seeds: _List[str], k: int, network: str = "DEFAULT") -> str:
url = f"{_config.url_base}/kpm/submit"
body = {"seeds": seeds, "k": k, "network": network}
resp = _http.post(url, json=body, headers={"x-api-key": _config.api_key})
result: str = _check_response(resp)
return result
def check_kpm_status(uid: str) -> _Dict[str, _Any]:
url = f"{_config.url_base}/kpm/status"
params = {"uid": uid}
resp = _http.get(url, params=params, headers={"x-api-key": _config.api_key})
result: _Dict[str, _Any] = _check_response(resp)
return result
from typing import List as _List
from python_nedrex import config as _config
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import check_status_factory as _check_status_factory
from python_nedrex.common import http as _http
# pylint: disable=R0913
def must_request(
seeds: _List[str],
hubpenalty: float,
multiple: bool,
trees: int,
maxit: int,
network: str = "DEFAULT",
) -> str:
body = {
"seeds": seeds,
"network": network,
"hubpenalty": hubpenalty,
"multiple": multiple,
"trees": trees,
"maxit": maxit,
}
url = f"{_config.url_base}/must/submit"
resp = _http.post(url, json=body, headers={"x-api-key": _config.api_key})
result: str = _check_response(resp)
return result
# pylint: enable=R0913
check_must_status = _check_status_factory("/must/status")
from typing import Any as _Any
from typing import Dict as _Dict
from typing import Iterable as _Iterable
from typing import List as _List
from typing import Optional as _Optional
from python_nedrex import config as _config
from python_nedrex.common import check_pagination_limit as _check_pagination_limit
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import get_pagination_limit as _get_pagination_limit
from python_nedrex.common import http as _http
from python_nedrex.exceptions import NeDRexError
def ppis(evidence: _Iterable[str], skip: int = 0, limit: _Optional[int] = None) -> _List[_Dict[str, _Any]]:
evidence_set = set(evidence)
extra_evidence = evidence_set - {"exp", "pred", "ortho"}
if extra_evidence:
raise NeDRexError(f"unexpected evidence types: {extra_evidence}")
maximum_limit = _get_pagination_limit()
_check_pagination_limit(limit, maximum_limit)
params = {"iid_evidence": list(evidence_set), "skip": skip, "limit": limit}
resp = _http.get(f"{_config.url_base}/ppi", params=params, headers={"x-api-key": _config.api_key})
result: _List[_Dict[str, _Any]] = _check_response(resp)
return result
from typing import Dict as _Dict
from typing import Iterable as _Iterable
from typing import List as _List
from typing import Union as _Union
from python_nedrex import config as _config
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import http as _http
def get_encoded_proteins(gene_list: _Iterable[_Union[int, str]]) -> _Dict[str, _List[str]]:
"""
Genes the proteins encoded by genes in a supplied gene list.
The genes can be submitted either as a list of strings or integers.
"""
genes = []
for gene in gene_list:
if isinstance(gene, int):
gene = str(gene)
if not isinstance(gene, str):
raise ValueError("items in gene_list must be int or str")
gene = gene.lower()
if not gene.startswith("entrez."):
genes.append(f"entrez.{gene}")
else:
genes.append(gene)
url = f"{_config.url_base}/relations/get_encoded_proteins"
resp = _http.get(url, params={"gene": genes}, headers={"x-api-key": _config.api_key})
result: _Dict[str, _List[str]] = _check_response(resp)
return result
def get_drugs_indicated_for_disorders(disorder_list: _Iterable[str]) -> _Dict[str, _List[str]]:
disorders = []
for disorder in disorder_list:
if not isinstance(disorder, str):
raise ValueError("items in disorder_list must be str")
if disorder.startswith("mondo."):
disorders.append(disorder)
else:
disorders.append(f"mondo.{disorder}")
url = f"{_config.url_base}/relations/get_drugs_indicated_for_disorders"
resp = _http.get(url, params={"disorder": disorders}, headers={"x-api-key": _config.api_key})
result: _Dict[str, _List[str]] = _check_response(resp)
return result
def get_drugs_targetting_proteins(protein_list: _Iterable[str]) -> _Dict[str, _List[str]]:
proteins = []
for protein in protein_list:
if not isinstance(protein, str):
raise ValueError("items in protein_list must be str")
if protein.startswith("uniprot."):
proteins.append(protein)
else:
proteins.append(f"uniprot.{protein}")
url = f"{_config.url_base}/relations/get_drugs_targetting_proteins"
resp = _http.get(url, params={"protein": proteins}, headers={"x-api-key": _config.api_key})
result: _Dict[str, _List[str]] = _check_response(resp)
return result
def get_drugs_targetting_gene_products(gene_list: _Iterable[str]) -> _Dict[str, _List[str]]:
genes = []
for gene in gene_list:
if isinstance(gene, int):
gene = str(gene)
if not isinstance(gene, str):
raise ValueError("items in gene_list must be int or str")
gene = gene.lower()
if not gene.startswith("entrez."):
genes.append(f"entrez.{gene}")
else:
genes.append(gene)
url = f"{_config.url_base}/relations/get_drugs_targetting_gene_products"
resp = _http.get(url, params={"gene": genes}, headers={"x-api-key": _config.api_key})
result: _Dict[str, _List[str]] = _check_response(resp)
return result
from typing import List as _List
from python_nedrex import config as _config
from python_nedrex.common import check_response as _check_response
from python_nedrex.common import check_status_factory as _check_status_factory
from python_nedrex.common import http as _http
# pylint: disable=R0913
def robust_submit(
seeds: _List[str],
network: str = "DEFAULT",
initial_fraction: float = 0.25,
reduction_factor: float = 0.9,
num_trees: int = 30,
threshold: float = 0.1,
) -> str:
body = {
"seeds": seeds,
"network": network,
"initial_fraction": initial_fraction,
"reduction_factor": reduction_factor,
"num_trees": num_trees,
"threshold": threshold,
}
url = f"{_config.url_base}/robust/submit"
resp = _http.post(url, json=body, headers={"x-api-key": _config.api_key})
result: str = _check_response(resp)
return result
# pylint: enable=R0913
check_robust_status = _check_status_factory("/robust/status")
def download_robust_results(uid: str) -> str:
url = f"{_config.url_base}/robust/results"
params = {"uid": uid}
resp = _http.get(url, params=params, headers={"x-api-key": _config.api_key})
result: str = _check_response(resp, return_type="text")
return result
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment