Beispiel #1
0
 def get_edge(self, plan, source_name, source_type, target_name,
              target_type, predicate, edge_direction):
     """ Determine if a transition between two types is supported by
     any of the registered sub-schemas.
     """
     edge = None
     schema = None
     for schema_name, sub_schema_package in self.schema.items():
         sub_schema = sub_schema_package['schema']
         sub_schema_url = sub_schema_package['url']
         #print (sub_schema)
         if source_type in sub_schema:
             print(f"  --{schema_name} - {source_type} => {target_type}")
             if target_type in sub_schema[source_type]:
                 top_schema = None
                 if len(plan) > 0:
                     top = plan[-1]
                     top_schema = top[0]
                 if top_schema == schema_name:
                     # this is the next edge in an ongoing segment.
                     top[2].append([
                         source_name, source_type, predicate,
                         edge_direction, target_name, target_type
                     ])
                 else:
                     plan.append([
                         schema_name, sub_schema_url,
                         [[
                             source_name, source_type, predicate,
                             edge_direction, target_name, target_type
                         ]]
                     ])
         else:
             implicit_conversion = BiolinkModelWalker()
             for conv_type in implicit_conversion.get_transitions(
                     source_type):
                 implicit_conversion_schema = "implicit_conversion"
                 implicit_conversion_url = self.schema[
                     implicit_conversion_schema]['url']
                 if conv_type in sub_schema:
                     print(
                         f"  --impconv: {schema_name} - {conv_type} => {target_type}"
                     )
                     if target_type in sub_schema[conv_type]:
                         plan.append([
                             implicit_conversion_schema,
                             implicit_conversion_url,
                             [[
                                 source_name, source_type, predicate,
                                 edge_direction, conv_type, conv_type
                             ]]
                         ])
                         plan.append([
                             schema_name, sub_schema_url,
                             [[
                                 conv_type, conv_type, predicate,
                                 edge_direction, target_name, target_type
                             ]]
                         ])
Beispiel #2
0
    def post(self):
        """
        biolink-model conversions.
        ---
        tag: validation
        description: Convert biolink model types.
        requestBody:
            description: Input message
            required: true
            content:
                application/json:
                    schema:
                        $ref: '#/definitions/Message'
        responses:
            '200':
                description: Success
                content:
                    text/plain:
                        schema:
                            type: string
                            example: "Successfully validated"
            '400':
                description: Malformed message
                content:
                    text/plain:
                        schema:
                            type: string

        """
        self.validate(request)
        question = request.json['question_graph']
        question_nodes = question['nodes']
        source_node = question_nodes[0]
        target_node = question_nodes[1]
        target_type = target_node['type']
        response = copy.deepcopy(request.json)
        if len(question_nodes) == 2:
            biolink_model_walker = BiolinkModelWalker()
            conversion = biolink_model_walker.translate(
                node=source_node, target_type=target_type)
            if conversion is not None:
                response['knowledge_graph'] = {
                    "nodes": [conversion],
                    "edges": []
                }
                response['answers'] = [{
                    'node_bindings': {
                        target_type: conversion['id']
                    }
                }]
            else:
                raise ValueError(
                    f"Unable to convert {source_node} to {target_type}")
            print(json.dumps(response, indent=2))
        if not 'answers' in response:
            response['answers'] = []
        return self.normalize_message(response)
Beispiel #3
0
 def plan_edge(self, plan, source, target, predicate):
     """ Determine if a transition between two types is supported by
     any of the registered sub-schemas.
     """
     edge = None
     schema = None
     for schema_name, sub_schema_package in self.schema.schema.items():
         """ Look for a path satisfying this edge in each schema. """
         sub_schema = sub_schema_package['schema']
         sub_schema_url = sub_schema_package['url']
         if source.type_name in sub_schema:
             logger.debug(
                 f"  --{schema_name} - {source.type_name} => {target.type_name}"
             )
             if target.type_name in sub_schema[source.type_name]:
                 """ Matching path. Write it to the plan. """
                 top_schema = None
                 if len(plan) > 0:
                     top = plan[-1]
                     top_schema = top[0]
                 if top_schema == schema_name:
                     # this is the next edge in an ongoing segment.
                     top[2].append([source, predicate, target])
                 else:
                     plan.append([
                         schema_name, sub_schema_url,
                         [[source, predicate, target]]
                     ])
         else:
             """ No explicit matching plan for this edge. Do implicit conversions make it work? """
             implicit_conversion = BiolinkModelWalker()
             for conv_type in implicit_conversion.get_transitions(
                     source.type_name):
                 implicit_conversion_schema = "implicit_conversion"
                 implicit_conversion_url = self.schema.schema[
                     implicit_conversion_schema]['url']
                 if conv_type in sub_schema:
                     logger.debug(
                         f"  --impconv: {schema_name} - {conv_type} => {target.type_name}"
                     )
                     if target.type_name in sub_schema[conv_type]:
                         plan.append([
                             implicit_conversion_schema,
                             implicit_conversion_url,
                             [[
                                 source, predicate,
                                 Concept(name=conv_type,
                                         type_name=conv_type,
                                         include_patterns=target.
                                         include_patterns,
                                         exclude_patterns=target.
                                         exclude_patterns)
                             ]]
                         ])
                         plan.append([
                             schema_name, sub_schema_url,
                             [[
                                 Concept(name=conv_type,
                                         type_name=conv_type,
                                         include_patterns=source.
                                         include_patterns,
                                         exclude_patterns=source.
                                         exclude_patterns), predicate,
                                 target
                             ]]
                         ])