diff --git a/gget/gget_opentargets.py b/gget/gget_opentargets.py index 31517c5a..8d9a53a1 100644 --- a/gget/gget_opentargets.py +++ b/gget/gget_opentargets.py @@ -188,11 +188,14 @@ def f( ) -> bool: for filter_id, filter_values in filters.items(): split_key = id_key[filter_id].split(".") - actual_value = row + actual_value: dict[str, ...] | list[dict[str, ...]] = row for k in split_key: if actual_value is None: break - actual_value = actual_value[k] + if type(actual_value) is list: + actual_value = [v[k] for v in actual_value] + else: + actual_value = actual_value[k] if mode == "and": if type(actual_value) is list: @@ -354,8 +357,6 @@ def fun( ) if limit is None and limit_key is not None and is_rows_based_query: - # wait 1 second as a courtesy - time.sleep(1) variables["pagination"] = limit_func(total_count, is_rows_based_query) new_results = graphql_query( @@ -382,6 +383,9 @@ def fun( rows = [row for row in rows if filter_(row)] converter(rows) + if actual_limit is not None: # just in case the converter changed the length + rows = rows[:actual_limit] + df = json_list_to_df( rows, df_schema, diff --git a/tests/fixtures/test_opentargets.json b/tests/fixtures/test_opentargets.json index 4db19b56..5d80c05c 100644 --- a/tests/fixtures/test_opentargets.json +++ b/tests/fixtures/test_opentargets.json @@ -47,6 +47,16 @@ }, "expected_result": "e6030c0f3da91f8176deaf29dcc08285" }, + "test_opentargets_no_limit_accurate_coverage": { + "type": "assert_equal_json_hash", + "args": { + "ensembl_id": "ENSG00000169194", + "resource": "diseases", + "verbose": false, + "wrap_text": true + }, + "expected_result": "e6030c0f3da91f8176deaf29dcc08285" + }, "test_opentargets_no_specified_resource": { "type": "assert_equal", "args": { @@ -146,6 +156,61 @@ }, "expected_result": "dcdd8421204bb3a273c8ca0b2de8d088" }, + "test_opentargets_drugs_filter": { + "type": "assert_equal", + "args": { + "ensembl_id": "ENSG00000169194", + "resource": "drugs", + "filters": { + "disease_id": "EFO_0000274" + }, + "limit": 2 + }, + "expected_result": [ + [ + "CHEMBL1743081", + "TRALOKINUMAB", + "Antibody", + "Interleukin-13 inhibitor", + "Antibody drug with a maximum clinical trial phase of IV (across all indications) that was first approved in 2021 and is indicated for atopic eczema and eczematoid dermatitis and has 5 investigational indications.", + [ + "CAT-354", + "Tralokinumab" + ], + [ + "Adbry", + "Adtralza" + ], + "EFO_0000274", + "atopic eczema", + 4, + [], + true + ], + [ + "CHEMBL1743081", + "TRALOKINUMAB", + "Antibody", + "Interleukin-13 inhibitor", + "Antibody drug with a maximum clinical trial phase of IV (across all indications) that was first approved in 2021 and is indicated for atopic eczema and eczematoid dermatitis and has 5 investigational indications.", + [ + "CAT-354", + "Tralokinumab" + ], + [ + "Adbry", + "Adtralza" + ], + "EFO_0000274", + "atopic eczema", + 3, + [ + "NCT03587805" + ], + true + ] + ] + }, "test_opentargets_tractability": { @@ -234,7 +299,7 @@ } ] }, - "test_pharmacogenetics_no_limit": { + "test_opentargets_pharmacogenetics_no_limit": { "type": "assert_equal_json_hash_nested", "args": { "ensembl_id": "ENSG00000169194", @@ -242,9 +307,105 @@ }, "expected_result": "34e64088e72da5710cdacd522c5fbd67" }, + "test_opentargets_pharmacogenetics_filter": { + "type": "assert_equal_nested", + "args": { + "ensembl_id": "ENSG00000169194", + "resource": "pharmacogenetics", + "filters": { + "drug_id": "CHEMBL535" + } + }, + "expected_result": [ + { + "direct_target": false, + "drugs": [ + { + "id": "CHEMBL535", + "name": "sunitinib" + } + ], + "evidence_level": "3", + "genotype": "TT", + "genotype_annotation": "Patients with renal cell carcinoma and the TT genotype may have a decreased severity of drug-induced toxicity when administered sunitinib as compared to patients with the CC or CT genotypes. Other clinical and genetic factors may also influence severity of drug-induced toxicity in patients with renal cell carcinoma who are administered sunitinib.", + "genotype_id": "5_132657117_C_T,T", + "literature": [ + "26387812", + "26387812" + ], + "phenotype": "decreased severity of drug-induced toxicity", + "response_category": "toxicity", + "rs_id": "rs1800925", + "source": "pharmgkb", + "variant_consequence_id": "SO:0001631", + "variant_consequence_label": "upstream_gene_variant" + }, + { + "direct_target": false, + "drugs": [ + { + "id": "CHEMBL535", + "name": "sunitinib" + } + ], + "evidence_level": "3", + "genotype": "CC", + "genotype_annotation": "Patients with renal cell carcinoma and the CC genotype may have a decreased severity of drug-induced toxicity when administered sunitinib as compared to patients with the TT genotype. Other clinical and genetic factors may also influence severity of drug-induced toxicity in patients with renal cell carcinoma who are administered sunitinib.", + "genotype_id": "5_132657117_C_C,C", + "literature": [ + "26387812", + "26387812" + ], + "phenotype": "decreased severity of drug-induced toxicity", + "response_category": "toxicity", + "rs_id": "rs1800925", + "source": "pharmgkb", + "variant_consequence_id": "SO:0002073", + "variant_consequence_label": "no_sequence_alteration" + }, + { + "direct_target": false, + "drugs": [ + { + "id": "CHEMBL535", + "name": "sunitinib" + } + ], + "evidence_level": "3", + "genotype": "CT", + "genotype_annotation": "Patients with renal cell carcinoma and the CT genotype may have a decreased severity of drug-induced toxicity when administered sunitinib as compared to patients with the TT genotype. Other clinical and genetic factors may also influence severity of drug-induced toxicity in patients with renal cell carcinoma who are administered sunitinib.", + "genotype_id": "5_132657117_C_C,T", + "literature": [ + "26387812", + "26387812" + ], + "phenotype": "decreased severity of drug-induced toxicity", + "response_category": "toxicity", + "rs_id": "rs1800925", + "source": "pharmgkb", + "variant_consequence_id": "SO:0001631", + "variant_consequence_label": "upstream_gene_variant" + } + ] + }, + "test_opentargets_pharmacogenetics_filter_or": { + "type": "assert_equal_json_hash_nested", + "args": { + "ensembl_id": "ENSG00000130164", + "resource": "pharmacogenetics", + "filters": { + "drug_id": [ + "CHEMBL1643", + "CHEMBL24" + ] + }, + "filter_mode": "or" + }, + "expected_result": "870d20f5746e90144ff986e800767b87" + }, - "test_expression": { + "test_opentargets_expression": { "type": "assert_equal", "args": { "ensembl_id": "ENSG00000169194", @@ -302,7 +463,7 @@ ] ] }, - "test_expression_no_limit": { + "test_opentargets_expression_no_limit": { "type": "assert_equal_json_hash", "args": { "ensembl_id": "ENSG00000169194", @@ -310,9 +471,37 @@ }, "expected_result": "192b5fceebfbb1cc7386cf319c264688" }, + "test_opentargets_expression_filter": { + "type": "assert_equal", + "args": { + "ensembl_id": "ENSG00000169194", + "resource": "expression", + "filters": { + "tissue_id": "UBERON_0000473", + "organ": "reproductive organ" + } + }, + "expected_result": [ + [ + "UBERON_0000473", + "testis", + 5, + 1026, + "", + 3, + [ + "reproductive system" + ], + [ + "reproductive organ", + "reproductive structure" + ] + ] + ] + }, - "test_depmap": { + "test_opentargets_depmap": { "type": "assert_equal_json_hash", "args": { "ensembl_id": "ENSG00000169194", @@ -320,9 +509,40 @@ }, "expected_result": "83de5c6bfee6e796bd5d68af1af702fc" }, + "test_opentargets_depmap_filter": { + "type": "assert_equal", + "args": { + "ensembl_id": "ENSG00000169194", + "resource": "depmap", + "filters": { + "tissue_id": "UBERON_0002367" + }, + "limit": 2 + }, + "expected_result": [ + [ + "ACH-000977", + -0.08063846, + "UBERON_0002367", + "prostate gland", + "LNCaP clone FGC", + "SIDM00683", + "Prostate Adenocarcinoma" + ], + [ + "ACH-000979", + -0.09671201, + "UBERON_0002367", + "prostate gland", + "DU 145", + "SIDM00120", + "Prostate Adenocarcinoma" + ] + ] + }, - "test_interactions": { + "test_opentargets_interactions": { "type": "assert_equal", "args": { "ensembl_id": "ENSG00000169194", @@ -362,7 +582,7 @@ ] ] }, - "test_interactions_no_limit": { + "test_opentargets_interactions_no_limit": { "type": "assert_equal_json_hash", "args": { "ensembl_id": "ENSG00000169194", @@ -370,7 +590,7 @@ }, "expected_result": "483362ee0e9454f16cde738a6dc4887c" }, - "test_interactions_simple_filter": { + "test_opentargets_interactions_simple_filter": { "type": "assert_equal", "args": { "ensembl_id": "ENSG00000169194", @@ -411,7 +631,7 @@ ] ] }, - "test_interactions_complex_filter": { + "test_opentargets_interactions_complex_filter": { "type": "assert_equal", "args": { "ensembl_id": "ENSG00000169194", @@ -533,6 +753,30 @@ }, "expected_result": "ValueError" }, + "test_error_opentargets_diseases_filter": { + "type": "error", + "args": { + "ensembl_id": "ENSG00000169194", + "resource": "diseases", + "filters": { + "disease_id": "EFO_0000274" + } + }, + "expected_result": "ValueError" + }, + + + "test_error_opentargets_drugs_invalid_filter": { + "type": "error", + "args": { + "ensembl_id": "ENSG00000169194", + "resource": "drugs", + "filters": { + "organ": "brain" + } + }, + "expected_result": "ValueError" + }, "test_error_opentargets_tractability_limit": {