Skip to content

Commit b286073

Browse files
committed
checkpoint, working on unit tests
1 parent 8b77550 commit b286073

File tree

2 files changed

+97
-189
lines changed

2 files changed

+97
-189
lines changed

tests/fixtures/test_opentargets.json

Lines changed: 73 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,7 @@
6464
"drug.id": "CHEMBL1743035",
6565
"drug.name": "LEBRIKIZUMAB",
6666
"drug.drugType": "Antibody",
67-
"drug.mechanismsOfAction.rows": [
68-
[
69-
[
70-
"mechanismOfAction",
71-
"Interleukin-13 inhibitor"
72-
]
73-
]
74-
],
67+
"drug.mechanismsOfAction.rows": "Interleukin-13 inhibitor",
7568
"drug.description": "Antibody drug with a maximum clinical stage of Approval (across all indications), with an approval for atopic eczema and eczematoid dermatitis and 5 investigational indications.",
7669
"drug.synonyms": [
7770
"Lebrikizumab",
@@ -84,171 +77,63 @@
8477
"RO-5490255",
8578
"Tnx-650"
8679
],
87-
"drug.tradeNames": [
88-
"Ebglyss"
89-
],
80+
"drug.tradeNames": "Ebglyss",
9081
"drug.maximumClinicalStage": "APPROVAL",
9182
"drug.indications.rows": [
92-
[
93-
[
94-
"disease",
95-
[
96-
[
97-
"id",
98-
"EFO_0000274"
99-
],
100-
[
101-
"name",
102-
"atopic eczema"
103-
]
104-
]
105-
]
106-
],
107-
[
108-
[
109-
"disease",
110-
[
111-
[
112-
"id",
113-
"EFO_0000768"
114-
],
115-
[
116-
"name",
117-
"idiopathic pulmonary fibrosis"
118-
]
119-
]
120-
]
121-
],
122-
[
123-
[
124-
"disease",
125-
[
126-
[
127-
"id",
128-
"MONDO_0004979"
129-
],
130-
[
131-
"name",
132-
"asthma"
133-
]
134-
]
135-
]
136-
],
137-
[
138-
[
139-
"disease",
140-
[
141-
[
142-
"id",
143-
"HP_0000964"
144-
],
145-
[
146-
"name",
147-
"Eczematoid dermatitis"
148-
]
149-
]
150-
]
151-
],
152-
[
153-
[
154-
"disease",
155-
[
156-
[
157-
"id",
158-
"EFO_0000341"
159-
],
160-
[
161-
"name",
162-
"chronic obstructive pulmonary disease"
163-
]
164-
]
165-
]
166-
],
167-
[
168-
[
169-
"disease",
170-
[
171-
[
172-
"id",
173-
"EFO_0000183"
174-
],
175-
[
176-
"name",
177-
"Hodgkins lymphoma"
178-
]
179-
]
180-
]
181-
],
182-
[
183-
[
184-
"disease",
185-
[
186-
[
187-
"id",
188-
"MONDO_0004784"
189-
],
190-
[
191-
"name",
192-
"allergic asthma"
193-
]
194-
]
195-
]
196-
]
83+
{
84+
"id": "EFO_0000274",
85+
"name": "atopic eczema"
86+
},
87+
{
88+
"id": "EFO_0000768",
89+
"name": "idiopathic pulmonary fibrosis"
90+
},
91+
{
92+
"id": "MONDO_0004979",
93+
"name": "asthma"
94+
},
95+
{
96+
"id": "HP_0000964",
97+
"name": "Eczematoid dermatitis"
98+
},
99+
{
100+
"id": "EFO_0000341",
101+
"name": "chronic obstructive pulmonary disease"
102+
},
103+
{
104+
"id": "EFO_0000183",
105+
"name": "Hodgkins lymphoma"
106+
},
107+
{
108+
"id": "MONDO_0004784",
109+
"name": "allergic asthma"
110+
}
197111
]
198112
},
199113
{
200114
"drug.id": "CHEMBL1742985",
201115
"drug.name": "ANRUKINZUMAB",
202116
"drug.drugType": "Antibody",
203-
"drug.mechanismsOfAction.rows": [
204-
[
205-
[
206-
"mechanismOfAction",
207-
"Interleukin-13 inhibitor"
208-
]
209-
]
210-
],
117+
"drug.mechanismsOfAction.rows": "Interleukin-13 inhibitor",
211118
"drug.description": "Antibody drug with a maximum clinical stage of Phase 2 (across all indications), with 2 investigational indications.",
212119
"drug.synonyms": [
213120
"Anrukinzumab",
214121
"IMA-638"
215122
],
216-
"drug.tradeNames": [],
123+
"drug.tradeNames": null,
217124
"drug.maximumClinicalStage": "PHASE_2",
218125
"drug.indications.rows": [
219-
[
220-
[
221-
"disease",
222-
[
223-
[
224-
"id",
225-
"MONDO_0004979"
226-
],
227-
[
228-
"name",
229-
"asthma"
230-
]
231-
]
232-
]
233-
],
234-
[
235-
[
236-
"disease",
237-
[
238-
[
239-
"id",
240-
"EFO_0000729"
241-
],
242-
[
243-
"name",
244-
"ulcerative colitis"
245-
]
246-
]
247-
]
248-
]
126+
{
127+
"id": "MONDO_0004979",
128+
"name": "asthma"
129+
},
130+
{
131+
"id": "EFO_0000729",
132+
"name": "ulcerative colitis"
133+
}
249134
]
250135
}
251-
]
136+
]
252137
},
253138
"test_opentargets_expression": {
254139
"function_call_to_reproduce": "output = opentargets(ensembl_id='ENSG00000169194', resource='expression', limit=2, json=True, verbose=False); print(json.dumps(output, indent=2))",
@@ -298,25 +183,37 @@
298183
"limit": 2
299184
},
300185
"expected_result": [
301-
{
302-
"tissueId": "UBERON_0002367",
303-
"tissueName": "prostate gland",
304-
"cellLineName": "DU 145",
305-
"expression": 0.0343439169,
306-
"diseaseFromSource": "Prostate Adenocarcinoma",
307-
"depmapId": "ACH-000979",
308-
"geneEffect": -0.1433678865
309-
},
310-
{
311-
"tissueId": "UBERON_0002367",
312-
"tissueName": "prostate gland",
313-
"cellLineName": "WPE1-NA22",
314-
"expression": 0.0291899424,
315-
"diseaseFromSource": "Non-Cancerous",
316-
"depmapId": "ACH-001422",
317-
"geneEffect": 0.069347702
318-
}
319-
]
186+
{
187+
"score": 0.999,
188+
"count": 3,
189+
"sourceDatabase": "string",
190+
"intA": "ENSP00000304915",
191+
"intABiologicalRole": "unspecified role",
192+
"intB": "ENSP00000361004",
193+
"intBBiologicalRole": "unspecified role",
194+
"targetA.id": "ENSG00000169194",
195+
"targetA.approvedSymbol": "IL13",
196+
"speciesA.taxonId": 134,
197+
"targetB.id": "ENSG00000123496",
198+
"targetB.approvedSymbol": "IL13RA2",
199+
"speciesB.taxonId": 134
200+
},
201+
{
202+
"score": 0.999,
203+
"count": 3,
204+
"sourceDatabase": "string",
205+
"intA": "ENSP00000304915",
206+
"intABiologicalRole": "unspecified role",
207+
"intB": "ENSP00000360730",
208+
"intBBiologicalRole": "unspecified role",
209+
"targetA.id": "ENSG00000169194",
210+
"targetA.approvedSymbol": "IL13",
211+
"speciesA.taxonId": 134,
212+
"targetB.id": "ENSG00000131724",
213+
"targetB.approvedSymbol": "IL13RA1",
214+
"speciesB.taxonId": 134
215+
}
216+
]
320217
},
321218
"test_opentargets_pharmacogenetics": {
322219
"function_call_to_reproduce": "output = opentargets(ensembl_id='ENSG00000169194', resource='pharmacogenetics', limit=2, json=True, verbose=False); print(json.dumps(output, indent=2))",
@@ -360,7 +257,7 @@
360257
"variantFunctionalConsequence.id": "SO:0001627",
361258
"variantFunctionalConsequence.label": "intron_variant"
362259
}
363-
]
260+
]
364261
},
365262
"test_opentargets_tractability": {
366263
"function_call_to_reproduce": "output = opentargets(ensembl_id='ENSG00000169194', resource='tractability', limit=2, json=True, verbose=False); print(json.dumps(output, indent=2))",

tests/from_json.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,35 @@ def normalize(x):
167167
if isinstance(x, tuple):
168168
x = [normalize(v) for v in x]
169169

170-
elif isinstance(x, list):
170+
if isinstance(x, list):
171171
x = [normalize(v) for v in x]
172172

173-
# 🔥 Detect list-of-pairs dict safely
173+
# Detect list-of-pairs -> dict recursively.
174174
if all(isinstance(i, list) and len(i) == 2 for i in x):
175-
# try both interpretations
176175
try:
177-
return {i[0]: i[1] for i in x} # key, value
176+
x = {i[0]: i[1] for i in x}
178177
except Exception:
179-
pass
180-
181-
try:
182-
return {i[1]: i[0] for i in x} # value, key
183-
except Exception:
184-
pass
185-
186-
elif isinstance(x, dict):
187-
return {k: normalize(v) for k, v in x.items()}
178+
try:
179+
x = {i[1]: i[0] for i in x}
180+
except Exception:
181+
pass
182+
183+
# Collapse singleton wrappers such as:
184+
# [{"drug": {...}}] -> {"drug": {...}}
185+
# ["26387812"] -> "26387812"
186+
if isinstance(x, list) and len(x) == 1:
187+
return normalize(x[0])
188+
189+
if isinstance(x, dict):
190+
x = {k: normalize(v) for k, v in x.items()}
191+
192+
# Collapse single-key wrapper dicts such as:
193+
# {"drug": {...}} -> {...}
194+
# {"drug": None} -> None
195+
if len(x) == 1:
196+
return normalize(next(iter(x.values())))
197+
198+
return x
188199

189200
return x
190201

0 commit comments

Comments
 (0)