async def test_ops(connection: aiosqlite.Connection): """Test KP operations.""" await add_data_from_string( connection, data=""" MONDO:0005148(( category biolink:Disease )) MONDO:0005148<-- predicate biolink:treats --CHEBI:6801 CHEBI:6801(( category biolink:ChemicalSubstance )) """, ) kp = KnowledgeProvider(connection) ops = await kp.get_operations() assert len(ops) == 1 await add_data_from_string( connection, data=""" CHEBI:6801(( category biolink:ChemicalSubstance )) CHEBI:6801-- predicate biolink:treats -->MONDO:0005148 MONDO:0005148(( category biolink:Disease )) """, ) kp = KnowledgeProvider(connection) ops = await kp.get_operations() assert len(ops) == 1
async def test_database_file(): """Test database file.""" f = tempfile.NamedTemporaryFile() filename = f.name async with aiosqlite.connect(filename) as connection: # add data to sqlite await add_data_from_string( connection, data=""" MONDO:0005148(( category biolink:Disease )) MONDO:0005148<-- predicate biolink:treats --CHEBI:6801 CHEBI:6801(( category biolink:ChemicalSubstance )) """, ) async with KnowledgeProvider(filename) as kp: qgraph = { "nodes": { "n0": { "categories": ["biolink:ChemicalSubstance"], }, "n1": { "categories": ["biolink:Disease"], "ids": ["MONDO:0005148"], }, }, "edges": { "e01": { "subject": "n0", "object": "n1", "predicates": ["biolink:treats"], }, }, } kgraph, results = await kp.get_results(qgraph) assert results
async def test_reverse(connection: aiosqlite.Connection): """Test simple KP.""" await add_data_from_string( connection, data=""" MONDO:0005148(( category biolink:Disease )) MONDO:0005148<-- predicate biolink:treats --CHEBI:6801 CHEBI:6801(( category biolink:ChemicalSubstance )) """, ) kp = KnowledgeProvider(connection) message = { "query_graph": { "nodes": { "n0": { "categories": ["biolink:Disease"], "ids": ["MONDO:0005148"], }, "n1": { "categories": ["biolink:ChemicalSubstance"], }, }, "edges": { "e01": { "subject": "n1", "object": "n0", "predicates": ["biolink:treats"], }, }, } } kgraph, results = await kp.get_results(message["query_graph"]) assert results
async def test_list_properties(connection: aiosqlite.Connection): """Test that we correctly handle query graph where categories, ids, and predicates are lists.""" await add_data_from_string( connection, data=""" CHEBI:136043(( category biolink:ChemicalSubstance )) CHEBI:136043-- predicate biolink:treats -->MONDO:0005148 MONDO:0005148(( category biolink:Disease )) """, ) kp = KnowledgeProvider(connection) message = { "query_graph": { "nodes": { "n0": { "categories": ["biolink:ChemicalSubstance"], "ids": ["CHEBI:136043"], }, "n1": { "categories": ["biolink:Disease"], }, }, "edges": { "e01": { "subject": "n0", "object": "n1", "predicates": ["biolink:treats"], }, }, } } kgraph, results = await kp.get_results(message["query_graph"]) assert results
async def test_symmetric(connection: aiosqlite.Connection): """Test symmetric predicate.""" await add_data_from_string( connection, data=""" MONDO:0005148(( category biolink:Disease )) MONDO:0005148<-- predicate biolink:treats --CHEBI:6801 CHEBI:6801(( category biolink:ChemicalSubstance )) """, ) kp = KnowledgeProvider(connection) message = { "query_graph": { "nodes": { "n0": { "categories": ["biolink:Disease"], "ids": ["MONDO:0005148"], }, "n1": { "categories": ["biolink:ChemicalSubstance"], }, }, "edges": { "e01": { "subject": "n0", "object": "n1", "predicates": ["biolink:related_to"], }, }, } } kgraph, results = await kp.get_results(message["query_graph"]) assert len(results) == 1 assert results[0]["node_bindings"]["n0"][0]["id"] == "MONDO:0005148" assert results[0]["node_bindings"]["n1"][0]["id"] == "CHEBI:6801"
async def test_subclass(connection: aiosqlite.Connection): """Test unrecognized key.""" await add_data_from_string( connection, data=""" CHEBI:6801(( category biolink:ChemicalSubstance )) MONDO:0005148(( category biolink:Disease )) HP:0004324(( category biolink:PhenotypicFeature )) CHEBI:6801-- predicate biolink:treats -->MONDO:0005148 MONDO:0005148-- predicate biolink:has_phenotype -->HP:0004324 """, ) kp = KnowledgeProvider(connection) message = { "query_graph": { "nodes": { "n0": { "categories": ["biolink:NamedThing"], "ids": ["CHEBI:6801"], }, "n1": { "categories": ["biolink:Disease"], }, }, "edges": { "e01": { "subject": "n0", "object": "n1", "predicates": ["biolink:treats"], }, }, } } kgraph, results = await kp.get_results(message["query_graph"]) assert results
async def test_self_edge(connection: aiosqlite.Connection): """Test self-edge.""" await add_data_from_string( connection, data=""" MONDO:0005148(( category biolink:Disease )) MONDO:0005148<-- predicate biolink:related_to --MONDO:0005148 """, ) kp = KnowledgeProvider(connection) message = { "query_graph": { "nodes": { "n0": { "ids": ["MONDO:0005148"], }, }, "edges": { "e01": { "subject": "n0", "object": "n0", }, }, } } kgraph, results = await kp.get_results(message["query_graph"]) assert len(results) == 1
async def test_branch(connection: aiosqlite.Connection): """Test a simple branch.""" await add_data_from_string( connection, data=""" MONDO:0005148(( category biolink:Disease )) NCBIGene:123(( category biolink:Gene )) CHEBI:6801(( category biolink:ChemicalSubstance )) CELL:123(( category biolink:Cell )) NCBIGene:123<-- predicate biolink:affected_by --MONDO:0005148 NCBIGene:123<-- predicate biolink:affects --CHEBI:6801 CELL:123<-- predicate biolink:affected_by --NCBIGene:123 """, ) kp = KnowledgeProvider(connection) message = { "query_graph": { "nodes": { "disease": { "categories": ["biolink:Disease"], "ids": ["MONDO:0005148"], }, "gene": { "categories": ["biolink:Gene"], }, "drug": { "categories": ["biolink:ChemicalSubstance"], }, "cell": { "categories": ["biolink:Cell"], }, }, "edges": { "e10": { "subject": "drug", "object": "gene", "predicates": ["biolink:affects"], }, "e21": { "subject": "disease", "object": "gene", "predicates": ["biolink:affected_by"], }, "e02": { "subject": "gene", "object": "cell", "predicates": ["biolink:affected_by"], }, }, } } kgraph, results = await kp.get_results(message["query_graph"]) assert len(results) == 1
async def test_prefixes(connection: aiosqlite.Connection): """Test CURIE prefixes.""" await add_data_from_string( connection, data=""" MONDO:0005148(( category biolink:Disease )) CHEBI:6801(( category biolink:ChemicalSubstance )) CHEBI:xxx(( category biolink:ChemicalSubstance )) """, ) kp = KnowledgeProvider(connection) prefixes = await kp.get_curie_prefixes() assert prefixes == { "biolink:Disease": ["MONDO"], "biolink:ChemicalSubstance": ["CHEBI"], }
async def test_extra_ids(connection: aiosqlite.Connection): """Test extra qnode ids.""" await add_data_from_string( connection, data=""" CHEBI:6801(( category biolink:ChemicalSubstance )) MONDO:0005148(( category biolink:Disease )) CHEBI:6801-- predicate biolink:treats -->MONDO:0005148 """, ) kp = KnowledgeProvider(connection, name="test") message = { "query_graph": { "nodes": { "n0": { "ids": ["CHEBI:6801", "CHEBI:6802", "CHEBI:6803"], }, "n1": { "categories": ["biolink:Disease"], }, }, "edges": { "e01": { "subject": "n0", "object": "n1", "predicates": ["biolink:treats"], }, }, } } kgraph, results = await kp.get_results(message["query_graph"]) assert len(results) == 1 for kedge in kgraph["edges"].values(): attrs = kedge["attributes"] assert len(attrs) == 1 assert attrs[0]["attribute_type_id"] == "biolink:knowledge_source" assert attrs[0]["value"] == "infores:test"