Exemple #1
0
def delete_game(search_title):  # noqa: E501
    """Delete game by title

     # noqa: E501

    :param search_title: 
    :type search_title: str

    :rtype: None
    """
    found = False
    count = 0
    while not found and count < len(all_games):
        if search_title.lower() == all_games[count].title:
            found = True
        else:
            count += 1
    if found:
        del all_games[count]
        return Message(message="game '{}' deleted".format(search_title),
                       code=200,
                       success=True)
    else:
        return Message(message="game '{}' not found".format(search_title),
                       code=404,
                       success=False)
Exemple #2
0
def getDefaultResponse(agent):
    graph = agent.get_graph()
    rg = resultGraph(graph)
    r = Message(context="translator_indigo_qa",
                datetime=str(datetime.datetime.now()),
                results=[Result(result_graph=rg)])
    return (r)
Exemple #3
0
def get_node_pairs_to_overlay(subject_qnode_key: str, object_qnode_key: str, query_graph: QueryGraph,
                              knowledge_graph: KnowledgeGraph, log: ARAXResponse) -> Set[Tuple[str, str]]:
    """
    This function determines which combinations of subject/object nodes in the KG need to be overlayed (e.g., have a
    virtual edge added between). It makes use of Resultify to determine what combinations of subject and object nodes
    may actually appear together in the same Results. (See issue #1069.) If it fails to narrow the node pairs for
    whatever reason, it defaults to returning all possible combinations of subject/object nodes.
    """
    log.debug(f"Narrowing down {subject_qnode_key}--{object_qnode_key} node pairs to overlay")
    kg_nodes_by_qg_id = get_node_ids_by_qg_id(knowledge_graph)
    kg_edges_by_qg_id = get_edge_ids_by_qg_id(knowledge_graph)
    # Grab the portion of the QG already 'expanded' (aka, present in the KG)
    sub_query_graph = QueryGraph(nodes={key:qnode for key, qnode in query_graph.nodes.items() if key in set(kg_nodes_by_qg_id)},
                                 edges={key:qedge for key, qedge in query_graph.edges.items() if key in set(kg_edges_by_qg_id)})

    # Compute results using Resultify so we can see which nodes appear in the same results
    resultifier = ARAXResultify()
    sub_response = ARAXResponse()
    sub_response.envelope = Response()
    sub_response.envelope.message = Message()
    sub_message = sub_response.envelope.message
    sub_message.query_graph = sub_query_graph
    sub_message.knowledge_graph = KnowledgeGraph(nodes=knowledge_graph.nodes.copy(),
                                                 edges=knowledge_graph.edges.copy())
    #sub_response.envelope.message = sub_message
    resultify_response = resultifier.apply(sub_response, {})

    # Figure out which node pairs appear together in one or more results
    if resultify_response.status == 'OK':
        node_pairs = set()
        for result in sub_message.results:
            subject_curies_in_this_result = {node_binding.id for key, node_binding_list in result.node_bindings.items() for node_binding in node_binding_list if
                                            key == subject_qnode_key}
            object_curies_in_this_result = {node_binding.id for key, node_binding_list in result.node_bindings.items() for node_binding in node_binding_list if
                                            key == object_qnode_key}
            pairs_in_this_result = set(itertools.product(subject_curies_in_this_result, object_curies_in_this_result))
            node_pairs = node_pairs.union(pairs_in_this_result)
        log.debug(f"Identified {len(node_pairs)} node pairs to overlay (with help of resultify)")
        if node_pairs:
            return node_pairs
    # Back up to using the old (O(n^2)) method of all combinations of subject/object nodes in the KG
    log.warning(f"Failed to narrow down node pairs to overlay; defaulting to all possible combinations")
    return set(itertools.product(kg_nodes_by_qg_id[subject_qnode_key], kg_nodes_by_qg_id[object_qnode_key]))
Exemple #4
0
    def from_dict(self, message):

        if str(message.__class__
               ) != "<class 'openapi_server.models.message.Message'>":
            message = Message().from_dict(message)

        # When tested from ARAX_query_graph_interpreter, none of this subsequent stuff is needed

        #print(message.query_graph.__class__)
        #message.query_graph = QueryGraph().from_dict(message.query_graph)
        #message.knowledge_graph = KnowledgeGraph().from_dict(message.knowledge_graph)

        #### This is an unfortunate hack that fixes qnode.curie entries
        #### Officially a curie can be a str or a list. But Swagger 2.0 only permits one type and we set it to str
        #### so when it gets converted from_dict, the list gets converted to a str because that's its type
        #### Here we force it back. This should no longer be needed when we are properly on OpenAPI 3.0
        #if message.query_graph is not None and message.query_graph.nodes is not None:
        #    for qnode_key,qnode in message.query_graph.nodes.items():
        #        print(qnode.__class__)
        #if qnode.id is not None and isinstance(qnode.id,str):
        #    if qnode.id[0:2] == "['":
        #        try:
        #            qnode.id = ast.literal_eval(qnode.id)
        #        except:
        #            pass

        #new_nodes = []
        #for qnode in message.query_graph.nodes:
        #    print(type(qnode))
        #    new_nodes.append(QNode().from_dict(qnode))
        #message.query_graph.nodes = new_nodes
        #for qedge in message.query_graph.edges:
        #    new_edges.append(QEdge().from_dict(qedge))
        #message.query_graph.edges = new_edges

        #if message.results is not None:
        #    for result in message.results:
        #        if result.result_graph is not None:
        #            #eprint(str(result.result_graph.__class__))
        #            if str(result.result_graph.__class__) != "<class 'openapi_server.models.knowledge_graph.KnowledgeGraph'>":
        #                result.result_graph = KnowledgeGraph().from_dict(result.result_graph)

        return message
Exemple #5
0
def mvp_target_query(chemical_substance):
    agent = KGAgent()
    agent.mvp_target_query(chemical_substance)
    graph = agent.get_graph()

    chembl_id = chemical_substance
    start_node = [n for n,d in graph.nodes(data=True) if 'chembl_id' in d and d['chembl_id'] == chembl_id][0]
    neighbors = graph[start_node]

    results = []
    for neighbor in neighbors:
        subgraph = graph.subgraph([start_node, neighbor])
        rg = resultGraph(subgraph)
        results.append(Result(result_graph=rg))

    r = Message(context="translator_indigo_qa",
                 datetime=str(datetime.datetime.now()),
                 results=results)
    return(r)
Exemple #6
0
def post_new_game(game):  # noqa: E501
    """Add new game

     # noqa: E501

    :param game: Game parameters
    :type game: dict | bytes

    :rtype: Message
    """
    if connexion.request.is_json:
        game = Game.from_dict(connexion.request.get_json())  # noqa: E501
    else:
        tmp_dict = {}
        for k in connexion.request.form.keys():
            value = connexion.request.form[k]
            if k == 'platforms':
                value = [item.strip() for item in value.split(',')]
            tmp_dict[k] = value
        game = Game.from_dict(tmp_dict)
        game.title = game.title.lower()
    ret_code, ret_msg = _add_game(game)
    return Message(message=ret_msg, code=ret_code, success=ret_code
                   == 201) if connexion.request.is_json else ret_code
Exemple #7
0
 def get_results(self):
     message = Message(results=self.results, query_graph=self.query_graph, knowledge_graph=self.knowledge_graph)
     results_response = Response(message = message)
     return results_response
Exemple #8
0
    def from_dict(self, message):

        if str(message.__class__
               ) == "<class 'openapi_server.models.message.Message'>":
            return message

        #### 2021-02 Temporary hack. Convert some lists into strings and some strings in lists
        print("Fixing edges", file=sys.stderr, flush=True)
        if 'query_graph' in message and message['query_graph'] is not None:
            if 'edges' in message['query_graph'] and message['query_graph'][
                    'edges'] is not None:
                for edge_key, edge in message['query_graph']['edges'].items():
                    if 'predicate' in edge and edge['predicate'] is not None:
                        if isinstance(edge['predicate'], str):
                            edge['predicate'] = [edge['predicate']]

            print("Fixing nodes", file=sys.stderr, flush=True)
            if 'nodes' in message['query_graph'] and message['query_graph'][
                    'nodes'] is not None:
                for node_key, node in message['query_graph']['nodes'].items():
                    if 'category' in node and node['category'] is not None:
                        if isinstance(node['category'], str):
                            #eprint(f"node {node_key} category {node['category']}")
                            node['category'] = [node['category']]
                    if 'id' in node and node['id'] is not None:
                        if isinstance(node['id'], str):
                            #eprint(f"node {node_key} id {node['id']}")
                            node['id'] = [node['id']]

        #eprint("Done")
        #eprint(json.dumps(message,indent=2,sort_keys=True))

        #### Deserialize
        message = Message().from_dict(message)

        #### Revert some things back temporarily

        #        if message.query_graph is not None:
        #            print("DEFixing edges", file=sys.stderr, flush=True)
        #            if message.query_graph.edges is not None:
        #                for edge_key, edge in message.query_graph.edges.items():
        #                    if edge.predicate is not None:
        #                        if isinstance(edge.predicate, list):
        #                            if len(edge.predicate) > 0:
        #                                edge.predicate = edge.predicate[0]
        #                            else:
        #                                edge.predicate = None

        #            print("DEFixing nodes", file=sys.stderr, flush=True)
        #            if message.query_graph.nodes is not None:
        #                for node_key, node in message.query_graph.nodes.items():
        #                    if node.category is not None:
        #                        if isinstance(node.category, list):
        #                            if len(node.category) > 0:
        #                                node.category = node.category[0]
        #                            else:
        #                                node.category = None
        # id is NOT fixed

        # When tested from ARAX_query_graph_interpreter, none of this subsequent stuff is needed

        #print(message.query_graph.__class__)
        #message.query_graph = QueryGraph().from_dict(message.query_graph)
        #message.knowledge_graph = KnowledgeGraph().from_dict(message.knowledge_graph)

        #### This is an unfortunate hack that fixes qnode.curie entries
        #### Officially a curie can be a str or a list. But Swagger 2.0 only permits one type and we set it to str
        #### so when it gets converted from_dict, the list gets converted to a str because that's its type
        #### Here we force it back. This should no longer be needed when we are properly on OpenAPI 3.0
        #if message.query_graph is not None and message.query_graph.nodes is not None:
        #    for qnode_key,qnode in message.query_graph.nodes.items():
        #        print(qnode.__class__)
        #if qnode.id is not None and isinstance(qnode.id,str):
        #    if qnode.id[0:2] == "['":
        #        try:
        #            qnode.id = ast.literal_eval(qnode.id)
        #        except:
        #            pass

        #new_nodes = []
        #for qnode in message.query_graph.nodes:
        #    print(type(qnode))
        #    new_nodes.append(QNode().from_dict(qnode))
        #message.query_graph.nodes = new_nodes
        #for qedge in message.query_graph.edges:
        #    new_edges.append(QEdge().from_dict(qedge))
        #message.query_graph.edges = new_edges

        #if message.results is not None:
        #    for result in message.results:
        #        if result.result_graph is not None:
        #            #eprint(str(result.result_graph.__class__))
        #            if str(result.result_graph.__class__) != "<class 'openapi_server.models.knowledge_graph.KnowledgeGraph'>":
        #                result.result_graph = KnowledgeGraph().from_dict(result.result_graph)

        return message
Exemple #9
0
    def create_envelope(self, response, describe=False):
        """
        Creates a basic empty ARAXResponse object with basic boilerplate metadata
        :return: ARAXResponse object with execution information and the new message object inside the data envelope
        :rtype: ARAXResponse
        """

        # #### Command definition for autogenerated documentation
        command_definition = {
            'dsl_command': 'create_envelope()',
            'description':
            """The `create_envelope` command creates a basic empty Response object with basic boilerplate metadata
                such as reasoner_id, schema_version, etc. filled in. This DSL command takes no arguments. This command is not explicitly
                necessary, as it is called implicitly when needed. e.g. If a DSL program begins with add_qnode(), the
                create_envelope() will be executed automatically if there is not yet a ARAXResponse. If there is already ARAXResponse in memory,
                then this command will destroy the previous one (in memory) and begin a new envelope.""",
            'parameters': {}
        }

        if describe:
            return command_definition

        #### Store the passed response object
        self.response = response

        #### Create the top-level Response object called an envelope
        response.info("Creating an empty template TRAPI Response")
        envelope = Response()
        response.envelope = envelope
        self.envelope = envelope

        # Create a Message object and place it in the envelope
        message = Message()
        response.envelope.message = message
        self.message = message

        #### Fill it with default information
        envelope.id = None
        envelope.type = "translator_reasoner_response"
        envelope.reasoner_id = "ARAX"
        envelope.tool_version = RTXConfiguration().version
        envelope.schema_version = "1.0.0"
        envelope.status = "OK"
        envelope.description = "Created empty template response"
        envelope.context = "https://raw.githubusercontent.com/biolink/biolink-model/master/context.jsonld"
        envelope.logs = response.messages
        envelope.datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        #### Create an empty master knowledge graph
        message.knowledge_graph = KnowledgeGraph()
        message.knowledge_graph.nodes = {}
        message.knowledge_graph.edges = {}

        #### Create an empty query graph
        message.query_graph = QueryGraph()
        message.query_graph.nodes = {}
        message.query_graph.edges = {}

        #### Create empty results
        message.results = []

        #### Return the response
        response.data['envelope'] = envelope
        return response
Exemple #10
0
def create_results(
    qg: QueryGraph,
    kg: QGOrganizedKnowledgeGraph,
    log: ARAXResponse,
    overlay_fet: bool = False,
    rank_results: bool = False,
    qnode_key_to_prune: Optional[str] = None,
) -> Response:
    regular_format_kg = convert_qg_organized_kg_to_standard_kg(kg)
    resultifier = ARAXResultify()
    prune_response = ARAXResponse()
    prune_response.envelope = Response()
    prune_response.envelope.message = Message()
    prune_message = prune_response.envelope.message
    prune_message.query_graph = qg
    prune_message.knowledge_graph = regular_format_kg
    if overlay_fet:
        log.debug(
            f"Using FET to assess quality of intermediate answers in Expand")
        connected_qedges = [
            qedge for qedge in qg.edges.values()
            if qedge.subject == qnode_key_to_prune
            or qedge.object == qnode_key_to_prune
        ]
        qnode_pairs_to_overlay = {
            (qedge.subject if qedge.subject != qnode_key_to_prune else
             qedge.object, qnode_key_to_prune)
            for qedge in connected_qedges
        }
        for qnode_pair in qnode_pairs_to_overlay:
            pair_string_id = f"{qnode_pair[0]}-->{qnode_pair[1]}"
            log.debug(f"Overlaying FET for {pair_string_id} (from Expand)")
            fet_qedge_key = f"FET{pair_string_id}"
            try:
                overlayer = ARAXOverlay()
                params = {
                    "action": "fisher_exact_test",
                    "subject_qnode_key": qnode_pair[0],
                    "object_qnode_key": qnode_pair[1],
                    "virtual_relation_label": fet_qedge_key
                }
                overlayer.apply(prune_response, params)
            except Exception as error:
                exception_type, exception_value, exception_traceback = sys.exc_info(
                )
                log.warning(
                    f"An uncaught error occurred when overlaying with FET during Expand's pruning: {error}: "
                    f"{repr(traceback.format_exception(exception_type, exception_value, exception_traceback))}"
                )
            if prune_response.status != "OK":
                log.warning(
                    f"FET produced an error when Expand tried to use it to prune the KG. "
                    f"Log was: {prune_response.show()}")
                log.debug(f"Will continue pruning without overlaying FET")
                # Get rid of any FET edges that might be in the KG/QG, since this step failed
                remove_edges_with_qedge_key(
                    prune_response.envelope.message.knowledge_graph,
                    fet_qedge_key)
                qg.edges.pop(fet_qedge_key, None)
                prune_response.status = "OK"  # Clear this so we can continue without overlaying
            else:
                if fet_qedge_key in qg.edges:
                    qg.edges[
                        fet_qedge_key].option_group_id = f"FET_VIRTUAL_GROUP_{pair_string_id}"
                else:
                    log.warning(
                        f"Attempted to overlay FET from Expand, but it didn't work. Pruning without it."
                    )

    # Create results and rank them as appropriate
    log.debug(f"Calling Resultify from Expand for pruning")
    resultifier.apply(prune_response, {})
    if rank_results:
        try:
            log.debug(f"Ranking Expand's intermediate pruning results")
            ranker = ARAXRanker()
            ranker.aggregate_scores_dmk(prune_response)
        except Exception as error:
            exception_type, exception_value, exception_traceback = sys.exc_info(
            )
            log.error(
                f"An uncaught error occurred when attempting to rank results during Expand's pruning: "
                f"{error}: {repr(traceback.format_exception(exception_type, exception_value, exception_traceback))}."
                f"Log was: {prune_response.show()}",
                error_code="UncaughtARAXiError")
            # Give any unranked results a score of 0
            for result in prune_response.envelope.message.results:
                if result.score is None:
                    result.score = 0
    return prune_response