Ejemplo n.º 1
0
def test_standard_api_resource(client, requests_mock):
    assert ordered(StandardAPIResource.handle_exception(
        [
            Exception('foo'),
            TranQLException('bar',details='baz')
        ],
        warning=True
    )) ==  ordered({
        "errors" : [
            {
                "message" : "foo",
                "details" : ""
            },
            {
                "message" : "bar",
                "details" : "baz"
            }
        ],
        "status" : "Warning"
    })

    assert StandardAPIResource.response({
        'status' : 'Warning',
        'errors' : [],
        'knowledge_graph' : {
            'nodes' : [],
            'edges' : []
        }
    }) == (
        {
        'status' : 'Warning',
        'errors' : [],
        'knowledge_graph' : {
            'nodes' : [],
            'edges' : []
        }
        },
        200
    )

    assert StandardAPIResource.response({
        'status' : 'Error',
        'errors' : []
    }) == (
        {
        'status' : 'Error',
        'errors' : []
        },
        500
    )
Ejemplo n.º 2
0
def test_decorate_kg(client):
    knowledge_graph = {
        "edges": [{
            "id": "e0",
            "source_id": "n0",
            "target_id": "n1",
            "type": "targets"
        }],
        "nodes": [{
            "id": "n0",
            "type": "chemical_substance"
        }, {
            "id": "n1",
            "type": "gene"
        }]
    }
    expected = {
        "edges": [{
            "id": "e0",
            "source_id": "n0",
            "target_id": "n1",
            "type": "targets",
            "reasoner": ["robokop"],
            "source_database": ["unknown"]
        }],
        "nodes": [{
            "id": "n0",
            "type": "chemical_substance",
            "reasoner": ["robokop"]
        }, {
            "id": "n1",
            "type": "gene",
            "reasoner": ["robokop"]
        }]
    }
    args = {'reasoners': ['robokop']}
    response = client.post(f'/tranql/decorate_kg',
                           query_string=args,
                           data=json.dumps(knowledge_graph),
                           content_type='application/json')
    assert ordered(response.json) == ordered(expected)
Ejemplo n.º 3
0
def test_rtx_convert_curies(client):
    converted_to_rtx = RtxQuery.convert_curies_to_rtx({
        "question_graph": {
            "nodes": [{
                "curie": "CHEMBL:CHEMBL3",
                "id": "chemical_substance",
                "type": "chemical_substance"
            }],
            "edges": []
        }
    })
    assert ordered(converted_to_rtx) == ordered({
        "question_graph": {
            "nodes": [{
                "curie": "CHEMBL.COMPOUND:CHEMBL3",
                "id": "chemical_substance",
                "type": "chemical_substance"
            }],
            "edges": []
        }
    })
    converted_to_standard = RtxQuery.convert_curies_to_standard({
        "knowledge_map": [{
            "node_bindings": {
                "chemical_substance": ["CHEMBL.COMPOUND:CHEMBL3"]
            },
            "edge_bindings": {
                "foo": [""]
            }
        }],
        "knowledge_graph": {
            "nodes": [{
                "id": "CHEMBL.COMPOUND:CHEMBL3"
            }, {
                "id": "FOOBAR"
            }],
            "edges": [{
                "source_id": "CHEMBL.COMPOUND:CHEMBL3",
                "target_id": "FOOBAR"
            }]
        },
        "question_graph": {
            "nodes": [{
                "curie": "CHEMBL.COMPOUND:CHEMBL3",
                "id": "chemical_substance",
                "type": "chemical_substance"
            }],
            "edges": []
        }
    })
    assert ordered(converted_to_standard) == ordered({
        "knowledge_map": [{
            "node_bindings": {
                "chemical_substance": "CHEMBL:CHEMBL3"
            },
            "edge_bindings": {
                "foo": [""]
            }
        }],
        "knowledge_graph": {
            "nodes": [{
                "id": "CHEMBL:CHEMBL3"
            }, {
                "id": "FOOBAR"
            }],
            "edges": [{
                "source_id": "CHEMBL:CHEMBL3",
                "target_id": "FOOBAR"
            }]
        },
        "question_graph": {
            "nodes": [{
                "curie": "CHEMBL:CHEMBL3",
                "id": "chemical_substance",
                "type": "chemical_substance"
            }],
            "edges": []
        }
    })
Ejemplo n.º 4
0
def test_ast_merge_results (requests_mock):
    set_mock(requests_mock, "workflow-5")
    """ Validate that
            -- Results from the query plan are being merged together correctly
    """
    print("test_ast_merge_answers ()")
    tranql = TranQL ()
    tranql.resolve_names = False
    ast = tranql.parse ("""
        SELECT cohort_diagnosis:disease->diagnoses:disease
          FROM '/clinical/cohort/disease_to_chemical_exposure'
         WHERE cohort_diagnosis = 'MONDO:0004979' --asthma
           AND Sex = '0'
           AND cohort = 'all_patients'
           AND max_p_value = '0.5'
           SET '$.knowledge_graph.nodes.[*].id' AS diagnoses
    """)

    select = ast.statements[0]

    # What is the proper format for the name of a mock file? This should be made into one
    mock_responses = [
        {
            'knowledge_graph': {
                'nodes': [
                    {'id': 'CHEBI:28177', 'type': 'chemical_substance'},
                    {'id': 'HGNC:2597', 'type': 'gene'},
                    {
                        'id': 'egg',
                        'name':'test_name_merge',
                        'type': 'foo_type',
                        'test_attr': ['a','b']
                    },
                    {
                        'id': 'equivalent_identifier_merge',
                        'equivalent_identifiers': ['TEST:00000'],
                        'merged_property': [
                            'a',
                            'b'
                        ]
                    }
                ],
                'edges': [
                    {'id': 'e0', 'source_id': 'CHEBI:28177', 'target_id': 'HGNC:2597'},
                    {
                        # Test if edges that are connected to merged nodes will be successfully merged with other duplicate edges
                        'source_id' : 'CHEBI:28177',
                        'target_id' : 'egg',
                        'type': ['merge_this'],
                        'merge_this_list' : ['edge_1'],
                        'unique_attr_e_1' : 'e_1',
                        'id' : 'winning_edge_id'
                    },
                ]
            },
            'knowledge_map': [
                {
                    'node_bindings': {
                        'chemical_substance': 'CHEBI:28177',
                        'gene': 'HGNC:2597'
                    },
                    'edge_bindings': {}
                }
            ]
        },
        {
            'knowledge_graph': {
                'nodes': [
                    {'id': 'CHEBI:28177', 'type': 'chemical_substance'},
                    {
                        'id': 'also_test_array_type_and_string_type_merge',
                        'name':'test_name_merge',
                        'type': ['foo_type','bar_type'],
                        'test_attr': ['a','c']
                    },
                    {'id': 'TEST:00000', 'type': 'test', 'merged_property': ['a','c']},
                ],
                'edges': [
                    {'id': 'e0', 'source_id': 'CHEBI:28177', 'target_id': 'TEST:00000'},
                    {
                        'source_id' : 'CHEBI:28177',
                        'target_id' : 'also_test_array_type_and_string_type_merge',
                        'type': ['merge_this'],
                        'merge_this_list' : ['edge_2'],
                        'unique_attr_e_2' : 'e_2'
                    }
                ]
            },
            'knowledge_map': [
                {
                    'node_bindings': {
                        'chemical_substance': 'CHEBI:28177',
                        'test': 'TEST:00000'
                    },
                    'edge_bindings': {}
                }
            ]
        }
    ]

    expected_result = {
        "knowledge_graph": {
            "edges": [
                {
                    "id": "e0",
                    "source_id": "CHEBI:28177",
                    "target_id": "HGNC:2597",
                    "type": []
                },
                {
                    "id": "e0",
                    "source_id": "CHEBI:28177",
                    "target_id": "equivalent_identifier_merge",
                    "type": []
                },
                {
                    "id" : "winning_edge_id",
                    "source_id" : "CHEBI:28177",
                    "target_id" : "egg",
                    "type" : ["merge_this"],
                    "merge_this_list" : ["edge_1", "edge_2"],
                    "unique_attr_e_1" : "e_1",
                    "unique_attr_e_2" : "e_2"
                }
            ],
            "nodes": [
                {
                    "equivalent_identifiers": [
                        "CHEBI:28177"
                    ],
                    "id": "CHEBI:28177",
                    "type": ["chemical_substance"]
                },
                {
                    "equivalent_identifiers": [
                        "HGNC:2597"
                    ],
                    "id": "HGNC:2597",
                    "type": ["gene"]
                },
                {
                    "equivalent_identifiers": [
                        "also_test_array_type_and_string_type_merge",
                        "egg"
                    ],
                    "type": [
                        "foo_type",
                        "bar_type"
                    ],
                    "id": "egg",
                    "name": "test_name_merge",
                    "test_attr": [
                        "a",
                        "b",
                        "c"
                    ]
                },
                {
                    "equivalent_identifiers": [
                        "TEST:00000",
                        "equivalent_identifier_merge"
                    ],
                    "merged_property": ["a", "b", "c"],
                    "id": "equivalent_identifier_merge",
                    "type": ["test"]
                }
            ]
        },
        "knowledge_map": [
            {
                "edge_bindings": {},
                "node_bindings": {
                    "chemical_substance": "CHEBI:28177",
                    "gene": "HGNC:2597"
                }
            },
            {
                "edge_bindings": {},
                "node_bindings": {
                    "chemical_substance": "CHEBI:28177",
                    "test": "equivalent_identifier_merge"
                }
            }
        ],
        'question_graph': {
            'edges': [
                {
                    'id': 'foo',
                    'type': 'test'
                }
            ],
            'nodes': [
                {
                    'id': 'bar',
                    'type': 'bartest'
                }
            ]
        }
    }
    merged_results = select.merge_results (
        mock_responses,
        tranql,
        {
            'edges': [
                {
                    'id': 'foo',
                    'type': 'test'
                }
            ],
            'nodes': [
                {
                    'id': 'bar',
                    'type': 'bartest'
                }
            ]
        },
        root_order=None
    )
    assert ordered(merged_results) == ordered(expected_result)
Ejemplo n.º 5
0
def test_ast_merge_knowledge_maps (requests_mock):
    set_mock(requests_mock, "workflow-5")
    tranql = TranQL ()
    tranql.asynchronous = False
    tranql.resolve_names = False
    ast = tranql.parse ("""
        select chemical_substance->disease->gene
          from "/schema"
         where chemical_substance="CHEMBL:CHEMBL3"
    """)

    # select = ast.statements[0]
    # statements = select.plan (select.planner.plan (select.query))
    # print(statements[0].query.order)

    # (select.execute_plan(tranql))

    responses = [
        {
            'knowledge_map' : [
                {
                    'node_bindings' : {
                    'chemical_substance' : 'CHEBI:100',
                        'disease' : 'MONDO:50'
                    },
                    'edge_bindings' : {
                        'e0' : 'ROOT_EDGE'
                    }
                }
            ],
            'question_order' : ['chemical_substance','disease']
        },
        {
            'knowledge_map' : [
                {
                    'node_bindings' : {
                        'disease' : 'MONDO:50',
                        'gene' : 'HGNC:1',
                        'metabolite' : 'KEGG:C00017'
                    },
                    'edge_bindings' : {
                        'e1' : 'TEST_EDGE'
                    }
                }
            ],
            'question_order' : ['disease','gene','metabolite']
        },
        {
            'knowledge_map' : [
                {
                    'node_bindings' : {
                        'disease' : 'MONDO:50',
                        'gene' : 'HGNC:1',
                        'metabolite' : 'KEGG:FOOBAR'
                    },
                    'edge_bindings' : {

                    }
                }
            ],
            'question_order' : ['disease','gene','metabolite']
        },
        {
            'knowledge_map' : [
                {
                    'node_bindings' : {
                        'metabolite' : 'KEGG:FOOBAR',
                        'protein' : 'UniProtKB:TESTING'
                    },
                    'edge_bindings' : {

                    }
                }
            ],
            'question_order' : ['metabolite','protein']
        },
        {
            'knowledge_map' : [
                {
                    'node_bindings' : {
                        'metabolite' : 'KEGG:C00017',
                        'protein' : 'UniProtKB:Q9NZJ5'
                    },
                    'edge_bindings' : {

                    }
                }
            ],
            'question_order' : ['metabolite','protein']
        }
    ]

    merged = SelectStatement.connect_knowledge_maps(responses,[
        'chemical_substance',
        'disease',
        'gene',
        'metabolite',
        'protein'
    ])

    assert_lists_equal(ordered(merged), ordered([
        {
            "node_bindings" : {
                "chemical_substance" : "CHEBI:100",
                "disease" : "MONDO:50",
                "gene" : "HGNC:1",
                "metabolite" : "KEGG:FOOBAR",
                "protein" : "UniProtKB:TESTING"
            },
            "edge_bindings" : {
                "e0" : "ROOT_EDGE"
            }
        },
        {
            "node_bindings" : {
                "chemical_substance" : "CHEBI:100",
                "disease" : "MONDO:50",
                "gene" : "HGNC:1",
                "metabolite" : "KEGG:C00017",
                "protein" : "UniProtKB:Q9NZJ5"
            },
            "edge_bindings" : {
                "e0" : "ROOT_EDGE",
                "e1" : "TEST_EDGE",
            }
        }
    ]))
Ejemplo n.º 6
0
def test_merge_messages(client):
    messages = [{
        "knowledge_graph": {
            "edges": [{
                "source_id": "TEST:CS1",
                "target_id": "TEST:G1",
                "type": "targets"
            }],
            "nodes": [{
                "id": "TEST:CS1",
                "type": "chemical_substance"
            }, {
                "id": "TEST:G1",
                "type": "gene"
            }]
        },
        "knowledge_map": []
    }, {
        "knowledge_graph": {
            "edges": [{
                "source_id": "TEST:CS2",
                "target_id": "TEST:G2",
                "type": "interacts_with"
            }],
            "nodes": [{
                "equivalent_identifiers": ["TEST:CS1"],
                "id": "TEST:merged",
                "type": ["chemical_substance", "Drug"]
            }, {
                "id": "TEST:CS2",
                "type": "chemical_substance"
            }, {
                "id": "TEST:G2",
                "type": "gene"
            }]
        },
        "knowledge_map": []
    }]
    expected = {
        "knowledge_graph": {
            "nodes": [{
                "id": "TEST:CS1",
                "type": ["chemical_substance", "Drug"],
                "equivalent_identifiers": ["TEST:merged", "TEST:CS1"]
            }, {
                "id": "TEST:G1",
                "type": ["gene"],
                "equivalent_identifiers": ["TEST:G1"]
            }, {
                "id": "TEST:CS2",
                "type": ["chemical_substance"],
                "equivalent_identifiers": ["TEST:CS2"]
            }, {
                "id": "TEST:G2",
                "type": ["gene"],
                "equivalent_identifiers": ["TEST:G2"]
            }],
            "edges": [{
                "source_id": "TEST:CS1",
                "target_id": "TEST:G1",
                "type": ["targets"]
            }, {
                "source_id": "TEST:CS2",
                "target_id": "TEST:G2",
                "type": ["interacts_with"]
            }]
        },
        "knowledge_map": [],
        "question_graph": {
            "nodes": [],
            "edges": []
        }
    }
    args = {
        'name_based_merging': True,
        'resolve_names': False,
        'question_graph': json.dumps({
            "nodes": [],
            "edges": []
        })
    }
    response = client.post(f'/tranql/merge_messages',
                           query_string=args,
                           data=json.dumps(messages),
                           content_type='application/json')

    assert ordered(response.json) == ordered(expected)