Пример #1
0
def test_query_by_query_graph_2():
    query = {
        "message": {
            "query_graph": {
                "edges": {
                    "qg2": {
                        "subject": "qg1",
                        "object": "qg0",
                        "predicates": ["biolink:physically_interacts_with"]
                    }
                },
                "nodes": {
                    "qg0": {
                        "name": "acetaminophen",
                        "ids": ["CHEMBL.COMPOUND:CHEMBL112"],
                        "categories": ["biolink:ChemicalEntity"]
                    },
                    "qg1": {
                        "name": None,
                        "ids": None,
                        "categories": ["biolink:Protein"]
                    }
                }
            }
        }
    }
    araxq = ARAXQuery()
    araxq.query(query)
    response = araxq.response
    print(response.show())

    assert response.status == 'OK'
    message = response.envelope.message
    assert len(message.results) >= 32
    assert response.envelope.schema_version == '1.2.0'
Пример #2
0
def _do_arax_query(query: dict) -> List[Union[ARAXResponse, Message]]:
    araxq = ARAXQuery()
    response = araxq.query(query)
    if response.status != 'OK':
        print(response.show(level=response.DEBUG))
    #return [response, araxq.message]
    return [response, response.envelope.message]
Пример #3
0
def _do_arax_query(
        query: dict,
        print_response: bool = True) -> List[Union[Response, Message]]:
    araxq = ARAXQuery()
    response = araxq.query(query)
    if response.status != 'OK' and print_response:
        print(response.show(level=response.DEBUG))
    return [response, araxq.message]
Пример #4
0
 def _answer_query_force_local(self, request_body: dict) -> dict:
     self.log.debug(
         f"{self.kp_name}: Pretending to send query to KG2 API (really it will be run locally)"
     )
     arax_query = ARAXQuery()
     kg2_araxquery_response = arax_query.query(request_body, mode='RTXKG2')
     json_response = kg2_araxquery_response.envelope.to_dict()
     return json_response
Пример #5
0
 def _run_arax_query(request_body: dict,
                     log: ARAXResponse) -> Tuple[ARAXResponse, Message]:
     araxq = ARAXQuery()
     sub_query_response = araxq.query(request_body, mode="RTXKG2")
     if sub_query_response.status != 'OK':
         log.error(
             f"Encountered an error running ARAXQuery within Expand: {sub_query_response.show(level=sub_query_response.DEBUG)}"
         )
     return sub_query_response, araxq.message
Пример #6
0
def test_query_by_canned_query_Q0():
    query = { 'message': { 'query_type_id': 'Q0', 'terms': { 'term': 'lovastatin' } } }
    araxq = ARAXQuery()
    result = araxq.query(query)
    print(result.show())
    assert result.status == 'OK'
    message = araxq.message
    assert message.n_results == 1
    assert message.type == 'translator_reasoner_message'
    assert message.schema_version == '0.9.2'                    # FIXME
Пример #7
0
def _do_arax_query(
        query: dict,
        timeout: Optional[int] = None) -> List[Union[ARAXResponse, Message]]:
    araxq = ARAXQuery()
    if timeout:
        query["query_options"] = {"kp_timeout": timeout}
    response = araxq.query(query)
    if response.status != 'OK':
        print(response.show(level=response.DEBUG))
    return [response, response.envelope.message]
Пример #8
0
 def _run_arax_query(actions_list: List[str],
                     log: ARAXResponse) -> Tuple[ARAXResponse, Message]:
     araxq = ARAXQuery()
     sub_query_response = araxq.query(
         {"operations": {
             "actions": actions_list
         }})
     if sub_query_response.status != 'OK':
         log.error(
             f"Encountered an error running ARAXQuery within Expand: {sub_query_response.show(level=sub_query_response.DEBUG)}"
         )
     return sub_query_response, araxq.message
Пример #9
0
def _run_query_and_do_standard_testing(
    actions: Optional[List[str]] = None,
    json_query: Optional[dict] = None,
    kg_should_be_incomplete=False,
    debug=False,
    should_throw_error=False,
    error_code: Optional[str] = None,
    timeout: Optional[int] = None
) -> Tuple[Dict[str, Dict[str, Node]], Dict[str, Dict[str, Edge]]]:
    # Run the query
    araxq = ARAXQuery()
    assert actions or json_query  # Must provide some sort of query to run
    query_object = {
        "operations": {
            "actions": actions
        }
    } if actions else {
        "message": {
            "query_graph": json_query
        }
    }
    if timeout:
        query_object["query_options"] = {"kp_timeout": timeout}
    response = araxq.query(query_object)
    message = araxq.message
    if response.status != 'OK':
        print(response.show(level=ARAXResponse.DEBUG))
    assert response.status == 'OK' or should_throw_error
    if should_throw_error and error_code:
        assert response.error_code == error_code

    # Convert output knowledge graph to a dictionary format for faster processing (organized by QG IDs)
    dict_kg = eu.convert_standard_kg_to_qg_organized_kg(
        message.knowledge_graph)
    nodes_by_qg_id = dict_kg.nodes_by_qg_id
    edges_by_qg_id = dict_kg.edges_by_qg_id

    # Optionally print more detail
    if debug:
        _print_nodes(nodes_by_qg_id)
        _print_edges(edges_by_qg_id)
        _print_counts_by_qgid(nodes_by_qg_id, edges_by_qg_id)
        print(response.show(level=ARAXResponse.DEBUG))

    # Run standard testing (applies to every test case)
    assert eu.qg_is_fulfilled(
        message.query_graph, dict_kg, enforce_required_only=True
    ) or kg_should_be_incomplete or should_throw_error
    _check_for_orphans(nodes_by_qg_id, edges_by_qg_id)
    _check_property_format(nodes_by_qg_id, edges_by_qg_id)
    _check_node_categories(message.knowledge_graph.nodes, message.query_graph)

    return nodes_by_qg_id, edges_by_qg_id
Пример #10
0
def test_query_by_query_graph_2():
    query = { "message": { "query_graph": { "edges": [
                { "id": "qg2", "source_id": "qg1", "target_id": "qg0", "type": "physically_interacts_with" }
            ],
            "nodes": [
                { "id": "qg0", "name": "acetaminophen", "curie": "CHEMBL.COMPOUND:CHEMBL112", "type": "chemical_substance" },
                { "id": "qg1", "name": None, "desc": "Generic protein", "curie": None, "type": "protein" }
            ] } } }
    araxq = ARAXQuery()
    result = araxq.query(query)
    print(result.show())
    assert result.status == 'OK'
    message = araxq.message
    assert message.n_results == 32
    assert message.schema_version == '0.9.3'
Пример #11
0
 def _run_arax_query(actions_list: List[str],
                     log: Response) -> DictKnowledgeGraph:
     araxq = ARAXQuery()
     sub_query_response = araxq.query({
         "previous_message_processing_plan": {
             "processing_actions": actions_list
         }
     })
     if sub_query_response.status != 'OK':
         log.error(
             f"Encountered an error running ARAXQuery within Expand: {sub_query_response.show(level=sub_query_response.DEBUG)}"
         )
         return dict()
     sub_query_message = araxq.message
     return sub_query_message.knowledge_graph
Пример #12
0
def _run_query_and_do_standard_testing(
    actions_list: List[str],
    kg_should_be_incomplete=False,
    debug=False,
    should_throw_error=False
) -> Tuple[Dict[str, Dict[str, Node]], Dict[str, Dict[str, Edge]]]:
    # Run the query
    araxq = ARAXQuery()
    response = araxq.query({
        "previous_message_processing_plan": {
            "processing_actions": actions_list
        }
    })
    message = araxq.message
    if response.status != 'OK':
        print(response.show(level=Response.DEBUG))
    assert response.status == 'OK' or should_throw_error

    # Convert output knowledge graph to a dictionary format for faster processing (organized by QG IDs)
    dict_kg = eu.convert_standard_kg_to_dict_kg(message.knowledge_graph)
    nodes_by_qg_id = dict_kg.nodes_by_qg_id
    edges_by_qg_id = dict_kg.edges_by_qg_id

    # Optionally print more detail
    if debug:
        _print_nodes(nodes_by_qg_id)
        _print_edges(edges_by_qg_id)
        _print_counts_by_qgid(nodes_by_qg_id, edges_by_qg_id)
        print(response.show(level=Response.DEBUG))

    # Run standard testing (applies to every test case)
    assert eu.qg_is_fulfilled(
        message.query_graph,
        dict_kg) or kg_should_be_incomplete or should_throw_error
    _check_for_orphans(nodes_by_qg_id, edges_by_qg_id)
    _check_property_format(nodes_by_qg_id, edges_by_qg_id)
    _check_node_types(message.knowledge_graph.nodes, message.query_graph)
    if not any(action for action in actions_list
               if "synonym_handling=add_all" in action):
        _check_counts_of_curie_qnodes(nodes_by_qg_id, message.query_graph)

    return nodes_by_qg_id, edges_by_qg_id
Пример #13
0
    def query_size_of_adjacent_nodes(self, node_curie, source_type, adjacent_type, kp="infores:rtx-kg2", rel_type=None):
        """
        Query adjacent nodes of a given source node based on adjacent node type.
        :param node_curie: (required) the curie id of query node. It accepts both single curie id or curie id list eg. "UniProtKB:P14136" or ['UniProtKB:P02675', 'UniProtKB:P01903', 'UniProtKB:P09601', 'UniProtKB:Q02878']
        :param source_type: (required) the type of source node, eg. "gene"
        :param adjacent_type: (required) the type of adjacent node, eg. "biological_process"
        :param kp: (optional) the knowledge provider to use, eg. "infores:rtx-kg2"(default)
        :param rel_type: (optional) edge type to consider, eg. "involved_in"
        :return a tuple with a dict containing the number of adjacent nodes for the query node and a list of removed nodes
        """

        res = None
        source_type = ComputeFTEST.convert_string_to_snake_case(source_type.replace('biolink:',''))
        source_type = ComputeFTEST.convert_string_biolinkformat(source_type)
        adjacent_type = ComputeFTEST.convert_string_to_snake_case(adjacent_type.replace('biolink:',''))
        adjacent_type = ComputeFTEST.convert_string_biolinkformat(adjacent_type)

        if rel_type is None:
            nodesynonymizer = NodeSynonymizer()
            normalized_nodes = nodesynonymizer.get_canonical_curies(node_curie)
            failure_nodes = list()
            mapping = {node:normalized_nodes[node]['preferred_curie'] for node in normalized_nodes if normalized_nodes[node] is not None}
            failure_nodes += list(normalized_nodes.keys() - mapping.keys())
            query_nodes = list(set(mapping.values()))
            query_nodes = [curie_id.replace("'", "''") if "'" in curie_id else curie_id for curie_id in query_nodes]
            # special_curie_ids = [curie_id for curie_id in query_nodes if "'" in curie_id]

            # Get connected to kg2c sqlite
            connection = sqlite3.connect(self.sqlite_file_path)
            cursor = connection.cursor()

            # Extract the neighbor count data
            node_keys_str = "','".join(query_nodes)  # SQL wants ('node1', 'node2') format for string lists
            sql_query = f"SELECT N.id, N.neighbor_counts " \
                        f"FROM neighbors AS N " \
                        f"WHERE N.id IN ('{node_keys_str}')"
            cursor.execute(sql_query)
            rows = cursor.fetchall()
            rows = [curie_id.replace("\'","'").replace("''", "'") if "'" in curie_id else curie_id for curie_id in rows]
            connection.close()

            # Load the counts into a dictionary
            neighbor_counts_dict = {row[0]:eval(row[1]) for row in rows}

            res_dict = {node:neighbor_counts_dict[mapping[node]].get(adjacent_type) for node in mapping if mapping[node] in neighbor_counts_dict and neighbor_counts_dict[mapping[node]].get(adjacent_type) is not None}
            failure_nodes += list(mapping.keys() - res_dict.keys())

            if len(failure_nodes) != 0:
                return (res_dict, failure_nodes)
            else:
                return (res_dict, [])

        else:
            if kp == 'ARAX/KG1':
                self.response.warning(f"Since the edge type '{rel_type}' is from KG1, we still use the DSL expand(kg=ARAX/KG1) to query neighbor count. However, the total node count is based on KG2c from 'nodesynonymizer.get_total_entity_count'. So the FET result might not be accurate.")

            # construct the instance of ARAXQuery class
            araxq = ARAXQuery()

            # check if node_curie is a str or a list
            if type(node_curie) is str:
                query_node_curie = node_curie
            elif type(node_curie) is list:
                node_id_list_str = "["
                for index in range(len(node_curie)):
                    node = node_curie[index]
                    if index + 1 == len(node_curie):
                        node_id_list_str = node_id_list_str + str(node) + "]"
                    else:
                        node_id_list_str = node_id_list_str + str(node) + ","

                query_node_curie = node_id_list_str
            else:
                self.response.error("The 'node_curie' argument of 'query_size_of_adjacent_nodes' method within FET only accepts str or list")
                return res

            # call the method of ARAXQuery class to query adjacent node
            query = {"operations": {"actions": [
                "create_message",
                f"add_qnode(ids={query_node_curie}, categories={source_type}, key=FET_n00)",
                f"add_qnode(categories={adjacent_type}, key=FET_n01)",
                f"add_qedge(subject=FET_n00, object=FET_n01, key=FET_e00, predicates={rel_type})",
                f"expand(edge_key=FET_e00,kp={kp})",
                #"resultify()",
                "return(message=true, store=false)"
            ]}}

            try:
                result = araxq.query(query)
                if result.status != 'OK':
                    self.response.error(f"Fail to query adjacent nodes from infores:rtx-kg2 for {node_curie}")
                    return res
                else:
                    res_dict = dict()
                    message = araxq.response.envelope.message
                    if type(node_curie) is str:
                        tmplist = set([edge_key for edge_key in message.knowledge_graph.edges if message.knowledge_graph.edges[edge_key].subject == node_curie or message.knowledge_graph.edges[edge_key].object == node_curie])  ## edge has no direction
                        if len(tmplist) == 0:
                            self.response.warning(f"Fail to query adjacent nodes from {kp} for {node_curie} in FET probably because expander ignores node type. For more details, please see issue897.")
                            return (res_dict,[node_curie])
                        res_dict[node_curie] = len(tmplist)
                        return (res_dict,[])
                    else:
                        check_empty = False
                        failure_nodes = list()
                        for node in node_curie:
                            tmplist = set([edge_key for edge_key in message.knowledge_graph.edges if message.knowledge_graph.edges[edge_key].subject == node or message.knowledge_graph.edges[edge_key].object == node])  ## edge has no direction
                            if len(tmplist) == 0:
                                self.response.warning(f"Fail to query adjacent nodes from {kp} for {node} in FET probably because expander ignores node type. For more details, please see issue897.")
                                failure_nodes.append(node)
                                check_empty = True
                                continue
                            res_dict[node] = len(tmplist)

                        if check_empty is True:
                            return (res_dict,failure_nodes)
                        else:
                            return (res_dict,[])
            except:
                tb = traceback.format_exc()
                error_type, error, _ = sys.exc_info()
                self.response.error(tb, error_code=error_type.__name__)
                self.response.error(f"Something went wrong with querying adjacent nodes from {kp} for {node_curie}")
                return res
Пример #14
0
def _do_arax_query(query: dict):
    araxq = ARAXQuery()
    response = araxq.query(query)
    if response.status != 'OK':
        print(response.show(level=response.DEBUG))
    return [response, response.envelope.message]
Пример #15
0
    def _query_size_of_adjacent_nodes_parallel(self, this):
        # This method is expected to be run within this class
        """
        Query the size of adjacent nodes of a given source node based on adjacent node type in parallel.
        :param this is a list containing five sub-arguments below since this function is exectued in parallel.
        :return the number of adjacent nodes for the query node
        """
        #:sub-argument node_curie: (required) the curie id of query node, eg. "UniProtKB:P14136"
        #:sub-argument source_type: (required) the type of source node, eg. "gene"
        #:sub-argument adjacent_type: (required) the type of adjacent node, eg. "biological_process"
        #:sub-argument kp: (optional) the knowledge provider to use, eg. "ARAX/KG1"(default)
        #:sub-argument rel_type: (optional) edge type to consider, eg. "involved_in"

        error_message = []
        if len(this) == 5:
            # this contains four arguments and assign them to different variables
            node_curie, source_type, adjacent_type, kp, rel_type = this
        elif len(this) == 4:
            node_curie, source_type, adjacent_type, kp = this
            rel_type = None
        elif len(this) == 3:
            node_curie, source_type, adjacent_type = this
            kp = "ARAX/KG1"
            rel_type = None
        else:
            error_message.append("The '_query_size_of_adjacent_nodes_parallel' method within FET only accepts four arguments: node_curie, adjacent_type, kp, rel_type")
            return error_message

        if kp=='ARAX/KG2' or kp == 'ARAX/KG2c':
            kp="ARAX/KG2"

        # construct the instance of ARAXQuery class
        araxq = ARAXQuery()

        # check if node_curie is a str
        if type(node_curie) is str:
            pass
        else:
            error_message.append("The 'node_curie' argument of '_query_size_of_adjacent_nodes_parallel' method within FET only accepts str")
            return error_message

        # call the method of ARAXQuery class to query adjacent node

        if rel_type:
            query = {"operations": {"actions": [
                "create_message",
                f"add_qnode(id={node_curie}, category={source_type}, key=FET_n00)",
                f"add_qnode(category={adjacent_type}, key=FET_n01)",
                f"add_qedge(subject=FET_n00, object=FET_n01, key=FET_e00, predicate={rel_type})",
                f"expand(edge_key=FET_e00,kp={kp})",
                #"resultify()",
                "return(message=true, store=false)"
            ]}}
        else:
            query = {"operations": {"actions": [
                "create_message",
                f"add_qnode(id={node_curie}, category={source_type}, key=FET_n00)",
                f"add_qnode(category={adjacent_type}, key=FET_n01)",
                f"add_qedge(subject=FET_n00, object=FET_n01, key=FET_e00)",
                f"expand(edge_key=FET_e00,kp={kp})",
                #"resultify()",
                "return(message=true, store=false)"
            ]}}

        try:
            result = araxq.query(query)
            if result.status != 'OK':
                error_message.append(f"Fail to query adjacent nodes from {kp} for {node_curie}")
                return error_message
            else:
                message = araxq.response.envelope.message
                tmplist = set([edge_key for edge_key in message.knowledge_graph.edges if message.knowledge_graph[edge_key].subject == node_curie or message.knowledge_graph[edge_key].object == node_curie]) ## edge has no direction
                if len(tmplist) == 0:
                    error_message.append(f"Fail to query adjacent nodes from {kp} for {node_curie}")
                    return error_message
                res = len(tmplist)
                return res
        except:
            tb = traceback.format_exc()
            error_type, error, _ = sys.exc_info()
            error_message.append((tb, error_type.__name__))
            error_message.append(f"Something went wrong with querying adjacent nodes from {kp} for {node_curie}")
            return error_message
Пример #16
0
    def query_size_of_adjacent_nodes(self, node_curie, source_type, adjacent_type, kp="ARAX/KG1", rel_type=None, use_cypher_command=False):
        """
        Query adjacent nodes of a given source node based on adjacent node type.
        :param node_curie: (required) the curie id of query node. It accepts both single curie id or curie id list eg. "UniProtKB:P14136" or ['UniProtKB:P02675', 'UniProtKB:P01903', 'UniProtKB:P09601', 'UniProtKB:Q02878']
        :param source_type: (required) the type of source node, eg. "gene"
        :param adjacent_type: (required) the type of adjacent node, eg. "biological_process"
        :param kp: (optional) the knowledge provider to use, eg. "ARAX/KG1"(default)
        :param rel_type: (optional) edge type to consider, eg. "involved_in"
        :param use_cypher_command: Boolean (True or False). If True, it used cypher command to the size of query adjacent nodes(default:True)
        :return a tuple with a dict containing the number of adjacent nodes for the query node and a list of removed nodes
        """

        res = None

        if kp=='ARAX/KG2' or kp == 'ARAX/KG2c':
            kp="ARAX/KG2"

        if use_cypher_command is True:

            #create the RTXConfiguration object
            rtxConfig = RTXConfiguration()
            # Connection information for the neo4j server, populated with orangeboard
            if kp=="ARAX/KG1":
                driver = GraphDatabase.driver(rtxConfig.neo4j_bolt, auth=basic_auth(rtxConfig.neo4j_username, rtxConfig.neo4j_password))
            elif kp=="ARAX/KG2":
                rtxConfig.live = "KG2"
                driver = GraphDatabase.driver(rtxConfig.neo4j_bolt, auth=basic_auth(rtxConfig.neo4j_username, rtxConfig.neo4j_password))
            else:
                self.response.error(f"The 'kp' argument of 'query_size_of_adjacent_nodes' method within FET only accepts 'ARAX/KG1' or 'ARAX/KG2' for cypher query right now")
                return res

            session = driver.session()

            # check if node_curie is a str or a list
            if type(node_curie) is str:
                if not rel_type:
                    query = f"match (n00:{adjacent_type})-[]-(n01) where n01.id='{node_curie}' with collect(distinct n00.id) as nodes_n00, n01 as node_n01 return node_n01.id as curie, size(nodes_n00) as count"
                else:
                    query = f"match (n00:{adjacent_type})-[:{rel_type}]-(n01) where n01.id='{node_curie}' with collect(distinct n00.id) as nodes_n00, n01 as node_n01 return node_n01.id as curie, size(nodes_n00) as count"
            elif type(node_curie) is list:
                if not rel_type:
                    query = f"match (n00:{adjacent_type})-[]-(n01) where n01.id in {node_curie} with collect(distinct n00.id) as nodes_n00, n01 as node_n01 return node_n01.id as curie, size(nodes_n00) as count"
                else:
                    query = f"match (n00:{adjacent_type})-[:{rel_type}]-(n01) where n01.id in {node_curie} with collect(distinct n00.id) as nodes_n00, n01 as node_n01 return node_n01.id as curie, size(nodes_n00) as count"
            else:
                self.response.error("The 'node_curie' argument of 'query_size_of_adjacent_nodes' method within FET only accepts str or list")
                return res

            try:
                cypher_res = session.run(query)
                result = pd.DataFrame(cypher_res.data())
                if result.shape[0] == 0:
                    self.response.error(f"Fail to query adjacent nodes from {kp} for {node_curie}")
                    return res
                else:
                    res_dict = dict()
                    has_error = False
                    if type(node_curie) is str:
                        res_dict[node_curie] = result['count'][0]
                        return res_dict
                    else:
                        for node in node_curie:
                            if node in list(result['curie']):
                                row_ind = list(result['curie']).index(node)
                                res_dict[node] = result.iloc[row_ind, 1]
                            else:
                                self.response.error(f"Fail to query adjacent nodes from {kp} for {node}")
                                has_error = True

                        if len(res_dict)==0:
                            return res
                        elif has_error is True:
                            return res
                        else:
                            return res_dict
            except:
                tb = traceback.format_exc()
                error_type, error, _ = sys.exc_info()
                self.response.error(tb, error_code=error_type.__name__)
                self.response.error(f"Something went wrong with querying adjacent nodes from {kp} for {node_curie}")
                return res

        else:

            # construct the instance of ARAXQuery class
            araxq = ARAXQuery()

            # check if node_curie is a str or a list
            if type(node_curie) is str:
                query_node_curie = node_curie
            elif type(node_curie) is list:
                node_id_list_str = "["
                for index in range(len(node_curie)):
                    node = node_curie[index]
                    if index + 1 == len(node_curie):
                        node_id_list_str = node_id_list_str + str(node) + "]"
                    else:
                        node_id_list_str = node_id_list_str + str(node) + ","

                query_node_curie = node_id_list_str
            else:
                self.response.error(
                    "The 'node_curie' argument of 'query_size_of_adjacent_nodes' method within FET only accepts str or list")
                return res

            # call the method of ARAXQuery class to query adjacent node
            if rel_type:
                query = {"operations": {"actions": [
                    "create_message",
                    f"add_qnode(id={query_node_curie}, category={source_type}, key=FET_n00)",
                    f"add_qnode(category={adjacent_type}, key=FET_n01)",
                    f"add_qedge(subject=FET_n00, object=FET_n01, key=FET_e00, predicate={rel_type})",
                    f"expand(edge_key=FET_e00,kp={kp})",
                    #"resultify()",
                    "return(message=true, store=false)"
                ]}}
            else:
                query = {"operations": {"actions": [
                    "create_message",
                    f"add_qnode(id={query_node_curie}, category={source_type}, key=FET_n00)",
                    f"add_qnode(category={adjacent_type}, key=FET_n01)",
                    f"add_qedge(subject=FET_n00, object=FET_n01, key=FET_e00)",
                    f"expand(edge_key=FET_e00,kp={kp})",
                    #"resultify()",
                    "return(message=true, store=false)"
                ]}}

            try:
                result = araxq.query(query)
                if result.status != 'OK':
                    self.response.error(f"Fail to query adjacent nodes from {kp} for {node_curie}")
                    return res
                else:
                    res_dict = dict()
                    message = araxq.response.envelope.message
                    if type(node_curie) is str:
                        tmplist = set([edge_key for edge_key in message.knowledge_graph.edges if message.knowledge_graph.edges[edge_key].subject == node_curie or message.knowledge_graph.edges[edge_key].object == node_curie])  ## edge has no direction
                        if len(tmplist) == 0:
                            self.response.warning(f"Fail to query adjacent nodes from {kp} for {node_curie} in FET probably because expander ignores node type. For more details, please see issue897.")
                            return (res_dict,[node_curie])
                        res_dict[node_curie] = len(tmplist)
                        return (res_dict,[])
                    else:
                        check_empty = False
                        failure_node = list()
                        for node in node_curie:
                            tmplist = set([edge_key for edge_key in message.knowledge_graph.edges if message.knowledge_graph.edges[edge_key].subject == node or message.knowledge_graph.edges[edge_key].object == node])  ## edge has no direction
                            if len(tmplist) == 0:
                                self.response.warning(f"Fail to query adjacent nodes from {kp} for {node} in FET probably because expander ignores node type. For more details, please see issue897.")
                                failure_node.append(node)
                                check_empty = True
                                continue
                            res_dict[node] = len(tmplist)

                        if check_empty is True:
                            return (res_dict,failure_node)
                        else:
                            return (res_dict,failure_node)
            except:
                tb = traceback.format_exc()
                error_type, error, _ = sys.exc_info()
                self.response.error(tb, error_code=error_type.__name__)
                self.response.error(f"Something went wrong with querying adjacent nodes from {kp} for {node_curie}")
                return res
Пример #17
0
def _do_arax_query(query: dict) -> List[Union[Response, Message]]:
    araxq = ARAXQuery()
    response = araxq.query(query)
    return [response, araxq.message]
if database == 'COHD':
    DSL_queries = cohd_DSL_queries
elif database == 'DTD':
    DSL_queries = dtd_DSL_queries

########### Below is the main code to run script ##################
araxq = ARAXQuery()
response = ARAXResponse()
synonymizer = NodeSynonymizer()

target_curie_list = []
check_wrong_queries = []
for index, query in enumerate(DSL_queries):
    # print(query)
    result = araxq.query(query)
    response = araxq.response
    if result.status != 'OK':
        # print(response.show(level=ARAXResponse.DEBUG))
        check_wrong_queries += [index + 1]
    else:
        message = response.envelope.message
        target_curie_list += [
            node_key for node_key, _ in message.knowledge_graph.nodes.items()
        ]

if database == 'DTD':
    if len(check_wrong_queries) != 0:
        print(
            f'Something wrong occurred in these DSL queries {check_wrong_queries}'
        )
Пример #19
0
def QGI_test7():
    # This is to test a three hop query with one end pinned (should result in FET ARAXi commands), and actually run the query
    input_query_graph = {
        "message": {
            "query_graph": {
                "edges": {
                    "e00": {
                        "object": "n01",
                        "subject": "n00"
                    },
                    "e01": {
                        "object": "n02",
                        "subject": "n01"
                    },
                    "e02": {
                        "object": "n03",
                        "subject": "n02"
                    }
                },
                "nodes": {
                    "n00": {
                        "categories": [
                            "biolink:ChemicalEntity"
                        ],
                        "ids": [
                            "DRUGBANK:DB00150"
                        ]
                    },
                    "n01": {
                        "categories": [
                            "biolink:Protein"
                        ]
                    },
                    "n02": {
                        "categories": [
                            "biolink:MolecularActivity"
                        ]
                    },
                    "n03": {
                        "categories": [
                            "biolink:ChemicalEntity"
                        ]
                    }
                }
            }

        }
    }

    #### Create a template Message
    response = ARAXResponse()
    messenger = ARAXMessenger()
    messenger.create_envelope(response)
    message = ARAXMessenger().from_dict(input_query_graph['message'])
    response.envelope.message.query_graph = message.query_graph

    interpreter = ARAXQueryGraphInterpreter()
    interpreter.translate_to_araxi(response)
    if response.status != 'OK':
        print(response.show(level=ARAXResponse.DEBUG))
        return response

    araxi_commands = response.data['araxi_commands']
    for cmd in araxi_commands:
        print(f"  - {cmd}")

    #### Show the final result
    # print('-------------------------')
    # print(response.show(level=ARAXResponse.DEBUG))
    # print(json.dumps(message.to_dict(),sort_keys=True,indent=2))

    #### Actually run the query
    from ARAX_query import ARAXQuery
    import ast
    araxq = ARAXQuery()
    # Run the query
    araxq.query({**input_query_graph, "operations": {"actions": araxi_commands}})
    # unpack the response
    response = araxq.response
    envelope = response.envelope
    message = envelope.message  # overrides the current message
    envelope.status = response.error_code
    envelope.description = response.message
    # return the message ID
    print(f"Returned response id: {envelope.id}")
    print('-------------------------')
    # print the whole message
    # print(json.dumps(ast.literal_eval(repr(envelope)), sort_keys=True, indent=2))
    # save message to file (since I can't get the UI working locally for some reason)
    with open('QGI_test7.json', 'w', encoding='utf-8') as f:
        json.dump(ast.literal_eval(repr(envelope)), f, ensure_ascii=False, indent=4)