Skip to content

Commit

Permalink
Add enrichment endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cachitas committed Apr 7, 2024
1 parent 34b9945 commit 97fc0e7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/stringx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ def homology(identifiers: list[str], species: int) -> typing.Any:
)


def enrichment(
identifiers: list[str],
species: int,
*,
background_identifiers: list[str] | None = None,
) -> typing.Any:
with Client(identity=identity) as client:
return (
client.enrichment(
identifiers=identifiers,
species=species,
background_identifiers=background_identifiers,
)
.raise_for_status()
.json()
)


def version():
with Client(identity=identity) as client:
return client.version()
21 changes: 21 additions & 0 deletions src/stringx/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ def homology(

return self.post(url, params=params)

def enrichment(
self,
identifiers: list[str],
species: int,
*,
background_identifiers: list[str] | None = None,
format: str | None = None,
) -> httpx.Response:
url = f"api/{format or self.format}/enrichment"

params = httpx.QueryParams(
identifiers="\r".join(identifiers),
species=species,
)
if background_identifiers:
params = params.add(
"background_string_identifiers", "\r".join(background_identifiers)
)

return self.post(url, params=params)

def version(self) -> str:
request = self.build_request("GET", "api/json/version")
request.url = request.url.copy_remove_param("caller_identity")
Expand Down
26 changes: 26 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,32 @@ def test_homology(httpx_mock, test_client, format):
test_client.homology(identifiers, species, format=format)


@pytest.mark.parametrize("format", ["tsv", "tsv-no-header", "json", "xml"])
def test_enrichment(httpx_mock, test_client, format):
identifiers = ["id1"]
background_identifiers = ["id2"]
species = 1234

httpx_mock.add_response(
url=httpx.URL(
f"https://string-db.org/api/{format}/enrichment",
params={
"identifiers": identifiers,
"background_string_identifiers": background_identifiers,
"species": species,
"caller_identity": test_client.identity,
},
),
)

test_client.enrichment(
identifiers,
species,
background_identifiers=background_identifiers,
format=format,
)


def test_version(httpx_mock, test_client):
httpx_mock.add_response(
url=httpx.URL("https://string-db.org/api/json/version"),
Expand Down
6 changes: 6 additions & 0 deletions tests/test_stringx.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ def test_interaction_partners(httpx_mock):
def test_homology(httpx_mock):
httpx_mock.add_response(json=True)
stringx.homology(["id1"], 1234)


def test_enrichment(httpx_mock):
httpx_mock.add_response(json=True)
stringx.enrichment(["id1"], 1234)
stringx.enrichment(["id1"], 1234, background_identifiers=["id2"])

0 comments on commit 97fc0e7

Please sign in to comment.