diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a8816c --- /dev/null +++ b/.gitignore @@ -0,0 +1,162 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm-project.org/#use-with-ide +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/pdm.lock b/pdm.lock new file mode 100644 index 0000000..dcacd3a --- /dev/null +++ b/pdm.lock @@ -0,0 +1,96 @@ +# This file is @generated by PDM. +# It is not intended for manual editing. + +[metadata] +groups = ["default", "dev"] +strategy = ["cross_platform", "inherit_metadata"] +lock_version = "4.4.1" +content_hash = "sha256:3246066da381b60f795abc7dd843f7057f2f1048414acd0c12916e4a0761544c" + +[[package]] +name = "colorama" +version = "0.4.6" +requires_python = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +summary = "Cross-platform colored terminal text." +groups = ["dev"] +marker = "sys_platform == \"win32\"" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + +[[package]] +name = "exceptiongroup" +version = "1.2.0" +requires_python = ">=3.7" +summary = "Backport of PEP 654 (exception groups)" +groups = ["dev"] +marker = "python_version < \"3.11\"" +files = [ + {file = "exceptiongroup-1.2.0-py3-none-any.whl", hash = "sha256:4bfd3996ac73b41e9b9628b04e079f193850720ea5945fc96a08633c66912f14"}, + {file = "exceptiongroup-1.2.0.tar.gz", hash = "sha256:91f5c769735f051a4290d52edd0858999b57e5876e9f85937691bd4c9fa3ed68"}, +] + +[[package]] +name = "iniconfig" +version = "2.0.0" +requires_python = ">=3.7" +summary = "brain-dead simple config-ini parsing" +groups = ["dev"] +files = [ + {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, + {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, +] + +[[package]] +name = "packaging" +version = "24.0" +requires_python = ">=3.7" +summary = "Core utilities for Python packages" +groups = ["dev"] +files = [ + {file = "packaging-24.0-py3-none-any.whl", hash = "sha256:2ddfb553fdf02fb784c234c7ba6ccc288296ceabec964ad2eae3777778130bc5"}, + {file = "packaging-24.0.tar.gz", hash = "sha256:eb82c5e3e56209074766e6885bb04b8c38a0c015d0a30036ebe7ece34c9989e9"}, +] + +[[package]] +name = "pluggy" +version = "1.4.0" +requires_python = ">=3.8" +summary = "plugin and hook calling mechanisms for python" +groups = ["dev"] +files = [ + {file = "pluggy-1.4.0-py3-none-any.whl", hash = "sha256:7db9f7b503d67d1c5b95f59773ebb58a8c1c288129a88665838012cfb07b8981"}, + {file = "pluggy-1.4.0.tar.gz", hash = "sha256:8c85c2876142a764e5b7548e7d9a0e0ddb46f5185161049a79b7e974454223be"}, +] + +[[package]] +name = "pytest" +version = "8.1.1" +requires_python = ">=3.8" +summary = "pytest: simple powerful testing with Python" +groups = ["dev"] +dependencies = [ + "colorama; sys_platform == \"win32\"", + "exceptiongroup>=1.0.0rc8; python_version < \"3.11\"", + "iniconfig", + "packaging", + "pluggy<2.0,>=1.4", + "tomli>=1; python_version < \"3.11\"", +] +files = [ + {file = "pytest-8.1.1-py3-none-any.whl", hash = "sha256:2a8386cfc11fa9d2c50ee7b2a57e7d898ef90470a7a34c4b949ff59662bb78b7"}, + {file = "pytest-8.1.1.tar.gz", hash = "sha256:ac978141a75948948817d360297b7aae0fcb9d6ff6bc9ec6d514b85d5a65c044"}, +] + +[[package]] +name = "tomli" +version = "2.0.1" +requires_python = ">=3.7" +summary = "A lil' TOML parser" +groups = ["dev"] +marker = "python_version < \"3.11\"" +files = [ + {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, + {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, +] diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..fe1a660 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,25 @@ +[project] +name = "stringdb" +version = "0.1.0" +description = "StringDB" +authors = [ + {name = "Hugo Cachitas",email = "hcachitas@gmail.com"}, +] +dependencies = [ +] +requires-python = ">=3.8" +readme = "README.md" +license = {text = "MIT"} + +[build-system] +requires = ["setuptools>=61", "wheel"] +build-backend = "setuptools.build_meta" + + +[tool.pdm] +distribution = true + +[tool.pdm.dev-dependencies] +dev = [ + "pytest>=8.1.1", +] diff --git a/src/stringdb/__init__.py b/src/stringdb/__init__.py new file mode 100644 index 0000000..165f117 --- /dev/null +++ b/src/stringdb/__init__.py @@ -0,0 +1,3 @@ +from .api import API + +__all__ = ["API"] diff --git a/src/stringdb/api.py b/src/stringdb/api.py new file mode 100644 index 0000000..e9c0779 --- /dev/null +++ b/src/stringdb/api.py @@ -0,0 +1,68 @@ +import logging +from typing import List, Optional + +logger = logging.getLogger(__name__) + + +class API: + VERSION_ADDRESSES = { + "12.0": "https://string-db.org/", + "11.5": "https://version-11-5.string-db.org/", + "11.0b": "https://version-11-0b.string-db.org/", + "11.0": "https://version-11-0.string-db.org/", + "10.5": "https://version-10-5.string-db.org/", + "10.0": "https://version10.string-db.org/", + } + + def __init__(self, version=None, client=None): + self.client = client + + if version: + self.stable_address = self.VERSION_ADDRESSES[version] + self.version = version + else: + self.version = next(iter(self.VERSION_ADDRESSES)) + self.stable_address = self.VERSION_ADDRESSES[self.version] + + @property + def api_url(self): + return self.stable_address + "api" + + def _get(self, endpoint: str, params: dict = {}): + url = "/".join([self.api_url, "json", endpoint]) + logger.info("GET", url, params) + params.update(caller_identity=__name__) + if self.client: + response = self.client.get(url, params=params) + logger.info(response.status_code) + response.raise_for_status() + return response.json() + return (url, params) + + def get_string_ids( + self, + identifiers: List[str], + species: int, + limit: int = 1, + echo_query: bool = True, + ): + params = { + "identifiers": "\r".join(identifiers), # your protein list + "species": species, # species NCBI identifier + "limit": limit, # only one (best) identifier per input protein + "echo_query": echo_query, # see your input identifiers in the output + } + return self._get("get_string_ids", params=params) + + def get_interaction_partners( + self, + identifiers: List[str], + species: int, + # limit: Optional[int] = None, + ): + params = dict( + identifiers="\r".join(identifiers), + species=species, + # limit=limit, + ) + return self._get("interaction_partners", params=params) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..ecae87e --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,46 @@ +import stringdb + + +def test_default_version(): + api = stringdb.API() + + latest_version = next(iter(stringdb.API.VERSION_ADDRESSES)) + + assert api.version == latest_version + + +def test_older_versions(): + for version in ["10.0", "10.5", "11.0", "11.0b", "11.5"]: + api = stringdb.API(version) + assert api.version == version + + +def test_default_url(): + api = stringdb.API() + assert api.api_url == "https://string-db.org/api" + + +def test_clientless_api(): + api = stringdb.API() + assert api.client is None + + result = api._get("endpoint", params=dict(param1="value", param2=9000)) + + assert len(result) == 2 + + url, params = result + + assert url == api.api_url + "/json/endpoint" + assert params["param1"] == "value" + assert params["param2"] == 9000 + + +def test_get_string_ids(): + api = stringdb.API() + + url, params = api.get_string_ids(["edin"], species=7227) + + assert url == api.api_url + "/json/get_string_ids" + assert params["identifiers"] == "edin" + assert params["species"] == 7227 + assert params["caller_identity"] == "stringdb.api"