Skip to content

Commit

Permalink
Add API from past project
Browse files Browse the repository at this point in the history
  • Loading branch information
cachitas committed Mar 18, 2024
1 parent ed0da80 commit 82db177
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 0 deletions.
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
96 changes: 96 additions & 0 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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",
]
3 changes: 3 additions & 0 deletions src/stringdb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .api import API

__all__ = ["API"]
68 changes: 68 additions & 0 deletions src/stringdb/api.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file added tests/__init__.py
Empty file.
Loading

0 comments on commit 82db177

Please sign in to comment.