Skip to content

Commit bea711e

Browse files
committed
opentargets working
1 parent b286073 commit bea711e

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

docs/src/en/updates.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## ✨ What's new
44
**Version ≥ 0.30.4** (XXX XX, 2026):
5+
- [`gget opentargets`](opentargets.md): Rewrote this module to reflect the new Open Targets API structure
6+
- some output column/key names may differ to reflect the new API structure
7+
- Removed the `--filter_mode` argument
58
- [`gget blast`](blast.md): Fixed compatibility with newer pandas versions (≥ 2.0) where `pd.read_html()` no longer accepts raw HTML strings directly, causing a `FileNotFoundError` / `OSError: Filename too long` error when parsing BLAST results
69

710
**Version ≥ 0.30.3** (Feb 22, 2026):

gget/gget_opentargets.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
import textwrap
33
import pandas as pd
44
import requests
5-
import json
65

76
from .constants import OPENTARGETS_GRAPHQL_API
8-
from .utils import set_up_logger, wrap_cols_func, graphql_query, json_list_to_df
7+
from .utils import set_up_logger
98

109
logger = set_up_logger() # export GGET_LOGLEVEL=DEBUG
1110

@@ -241,19 +240,14 @@ def _unhash(x):
241240
return [_unhash(v) for v in x]
242241
return x
243242

244-
def _is_hashable_series(s):
245-
try:
246-
s.dropna().map(hash)
247-
return True
248-
except TypeError:
249-
return False
250-
251243
def opentargets(
252244
ensembl_id,
253245
resource="diseases",
254246
limit=None,
255247
verbose=True,
256248
wrap_text=False,
249+
filters=None,
250+
257251
json=False,
258252
):
259253
"""
@@ -274,6 +268,7 @@ def opentargets(
274268
Note: Not compatible with the 'tractability' and 'depmap' resources.
275269
- verbose Print progress messages (default: True).
276270
- wrap_text If True, displays data frame with wrapped text for easy reading. Default: False.
271+
- filters Filters to apply to the data. Supported filters by equality for any column in the returned data frame. Default: None (no filters applied).
277272
- json If True, returns results in JSON format instead of as a Data Frame. Default: False.
278273
279274
@@ -342,27 +337,32 @@ def opentargets(
342337
for row in rows
343338
for subdict in row[row_key]
344339
]
345-
340+
341+
if len(rows) == 0:
342+
if verbose:
343+
logger.info(f"No {resource} data found for {ensembl_id}.")
344+
return pd.DataFrame() if not json else []
346345

347346
# ---------------------------
348347
# If JSON → return normalized JSON
349348
# ---------------------------
350349
df = pd.json_normalize(rows, sep=".")
351-
df = df.map(_make_hashable).drop_duplicates().map(_unhash)
352-
353-
#? alternative approach to dropping duplicates without needing to make everything hashable first
354-
# hashable_cols = [col for col in df.columns if _is_hashable_series(df[col])]
355-
# df = df.drop_duplicates(subset=hashable_cols)
356-
#? alternative approach to dropping duplicates without needing to make everything hashable first
357-
358350
df = df.dropna(axis=1, how="all") # drop any all-NaN columns
359351
df = df.dropna(axis=0, how="all") # drop any all-NaN rows
352+
df = df.map(_make_hashable).drop_duplicates()
360353

361354
if limit is not None:
362355
df = df.head(limit)
363356

357+
df = df.map(_unhash)
364358
df = df.map(_collapse_singletons)
365359

360+
if filters is not None:
361+
for filter_key, filter_value in filters.items():
362+
if filter_key not in df.columns:
363+
raise ValueError(f"Filter key '{filter_key}' not found in data columns. Available columns: {', '.join(df.columns)}")
364+
df = df[df[filter_key] == filter_value]
365+
366366
if wrap_text:
367367
for col in df.columns:
368368
if df[col].dtype == object:

tests/from_json.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,16 @@ def normalize(x):
203203

204204
expected_result = td[test]["expected_result"]
205205
result_to_test = do_call(func, td[test]["args"])
206+
if isinstance(result_to_test, pd.DataFrame):
207+
result_to_test = json.loads(
208+
result_to_test.dropna(axis=1, how="all").to_json(
209+
orient="records", force_ascii=False
210+
)
211+
)
206212

207213
result_to_test = normalize(result_to_test)
208214
expected_result = normalize(expected_result)
209215

210-
# If DataFrame → list of lists
211-
if isinstance(result_to_test, pd.DataFrame):
212-
result_to_test = result_to_test.dropna(axis=1).values.tolist()
213-
214216
# ✅ Infer keys from expected_result
215217
if not expected_result:
216218
raise ValueError(f"Test {test} has empty expected_result")

0 commit comments

Comments
 (0)