def userRequestBodyIsSupported(self):
        """
        Checks if the provided TRAPI query contains all criteria for executing this case. This is defined as a classmethod because inheriting classes may wish
        to override supported queries via .get_valid_curies().
        :param trapi_query: TRAPI Message JSON object to check for support.
        :return: True if supported, False otherwise.
        """

        edges = self.userRequestBody['message']['query_graph']['edges']
        if len(edges) != 1: return False

        edgeId = list(edges.keys())[0]
        if isNullOrWhiteSpace(edgeId): return False
        edge = edges[edgeId]

        if type(edge) != dict: return False
        if sorted(list(edge.keys())) != ['object', 'predicate', 'subject']:
            return False
        if isNullOrWhiteSpace(edge['subject']): return False
        if isNullOrWhiteSpace(edge['object']): return False
        if edge['predicate'] != 'biolink:condition_associated_with_gene':
            return False

        nodes = self.userRequestBody['message']['query_graph']['nodes']
        if len(nodes) != 2: return False

        geneNodeFound = False
        diseaseNodeFound = False
        geneId = None
        diseaseId = None

        for nodeId, node in nodes.items():
            if isNullOrWhiteSpace(nodeId): return False
            if sorted(list(node.keys())) == [
                    'category'
            ] and node['category'] == 'biolink:Gene':
                geneNodeFound = True
                geneId = nodeId
            elif sorted(list(node.keys())) == [
                    'category', 'id'
            ] and node['category'] == 'biolink:Disease':
                if node['id'] not in self.validCuries: return False
                diseaseNodeFound = True
                diseaseId = nodeId
            else:
                return False
        if not geneNodeFound or not diseaseNodeFound: return False
        if geneId != edge['object'] or diseaseId != edge['subject']:
            return False

        return True
Пример #2
0
    def userRequestBodyIsSupported(self):
        """
        Checks if the provided TRAPI query contains all criteria for executing this case.
        :return: True if supported, False otherwise.
        """

        edges = self.userRequestBody['message']['query_graph']['edges']
        if len(edges) != 1: return False

        edgeId = list(edges.keys())[0]
        if isNullOrWhiteSpace(edgeId): return False
        edge = edges[edgeId]

        if type(edge) != dict: return False
        if sorted(list(edge.keys())) != ['object', 'predicate', 'subject']:
            return False
        if isNullOrWhiteSpace(edge['subject']): return False
        if isNullOrWhiteSpace(edge['object']): return False
        if edge['predicate'] != 'biolink:treats': return False

        nodes = self.userRequestBody['message']['query_graph']['nodes']
        if len(nodes) != 2: return False

        geneNodeFound = False
        diseaseNodeFound = False
        geneId = None
        diseaseId = None

        for nodeId, node in nodes.items():
            if isNullOrWhiteSpace(nodeId): return False
            if sorted(list(node.keys())) == [
                    'category'
            ] and node['category'] == 'biolink:Drug':
                geneNodeFound = True
                geneId = nodeId
            elif sorted(list(node.keys())) == [
                    'category', 'id'
            ] and node['category'] == 'biolink:Disease':
                # if node['id'] not in self.validCuries: return False
                diseaseNodeFound = True
                diseaseId = nodeId
            else:
                return False
        if not geneNodeFound or not diseaseNodeFound: return False
        if geneId != edge['subject'] or diseaseId != edge['object']:
            return False

        return True
    def userRequestBodyIsSupported(self):
        """
        Checks if the provided TRAPI query contains all criteria for executing this case.
        :return: True if supported, False otherwise.
        """

        edges = self.userRequestBody['message']['query_graph']['edges']
        if len(edges) != 1: return False

        self.diseaseToChemicalEdgeId = list(edges.keys())[0]
        if isNullOrWhiteSpace(self.diseaseToChemicalEdgeId): return False
        self.diseaseToChemicalEdge = edges[self.diseaseToChemicalEdgeId]

        if type(self.diseaseToChemicalEdge) != dict: return False
        if sorted(list(self.diseaseToChemicalEdge.keys())) != ['object', 'subject']: return False
        if "predicate" in self.diseaseToChemicalEdge: return False
        if isNullOrWhiteSpace(self.diseaseToChemicalEdge['subject']): return False
        if isNullOrWhiteSpace(self.diseaseToChemicalEdge['object']): return False

        nodes = self.userRequestBody['message']['query_graph']['nodes']
        if len(nodes) != 2: return False

        self.diseaseId = None
        self.diseaseNode = None
        self.chemicalId = None
        self.chemicalNode = None

        for nodeId, node in nodes.items():
            if isNullOrWhiteSpace(nodeId): return False
            if sorted(list(node.keys())) == ['category'] and node['category'] == 'biolink:ChemicalSubstance':
                self.chemicalId = nodeId
                self.chemicalNode = node
            elif sorted(list(node.keys())) == ['category', 'id'] and node['category'] == 'biolink:Disease':
                self.diseaseId = nodeId
                self.diseaseNode = node
            else:
                return False
        if self.diseaseNode is None or self.chemicalNode is None: return False
        if self.diseaseId != self.diseaseToChemicalEdge['subject'] or self.chemicalId != self.diseaseToChemicalEdge['object']: return False

        return True
Пример #4
0
    def userRequestBodyIsSupported(body: dict):
        """
        A function to evaluate whether the valid response is supported by our api
        :param body: A dictionary representing a JSON body
        :return: Boolean: True meaning our api supports these parameters, False meaning our api does not support these parameters
        """
        edges = body['message']['query_graph']['edges']
        if len(edges) != 1: return False
        edge = edges[0]
        if type(edge) != dict: return False
        if sorted(list(
                edge.keys())) != ['id', 'source_id', 'target_id', 'type']:
            return False
        if isNullOrWhiteSpace(edge['id']): return False
        if isNullOrWhiteSpace(edge['source_id']): return False
        if isNullOrWhiteSpace(edge['target_id']): return False
        if edge['type'] != 'associated': return False

        nodes = body['message']['query_graph']['nodes']
        if len(nodes) != 2: return False

        geneNodeFound = False
        diseaseNodeFound = False
        geneId = None
        diseaseId = None
        validCuries = [
            "EFO:0000249", "EFO:0000253", "EFO:0000270", "EFO:0000275",
            "EFO:0000289", "EFO:0000341", "EFO:0000378", "EFO:0000384",
            "EFO:0000401", "EFO:0000551", "EFO:0000612", "EFO:0000692",
            "EFO:0000729", "EFO:0001359", "EFO:0001360", "EFO:0001365",
            "EFO:0002506", "EFO:0003144", "EFO:0003761", "EFO:0003767",
            "EFO:0003884", "EFO:0003931", "EFO:0004190", "EFO:0004305",
            "EFO:0004312", "EFO:0004314", "EFO:0004326", "EFO:0004338",
            "EFO:0004339", "EFO:0004340", "EFO:0004342", "EFO:0004343",
            "EFO:0004458", "EFO:0004462", "EFO:0004465", "EFO:0004466",
            "EFO:0004469", "EFO:0004471", "EFO:0004501", "EFO:0004502",
            "EFO:0004509", "EFO:0004518", "EFO:0004530", "EFO:0004532",
            "EFO:0004533", "EFO:0004534", "EFO:0004535", "EFO:0004541",
            "EFO:0004555", "EFO:0004570", "EFO:0004574", "EFO:0004611",
            "EFO:0004612", "EFO:0004614", "EFO:0004615", "EFO:0004616",
            "EFO:0004623", "EFO:0004631", "EFO:0004683", "EFO:0004698",
            "EFO:0004703", "EFO:0004704", "EFO:0004705", "EFO:0004713",
            "EFO:0004735", "EFO:0004736", "EFO:0004741", "EFO:0004761",
            "EFO:0004808", "EFO:0004838", "EFO:0004844", "EFO:0004861",
            "EFO:0004997", "EFO:0005000", "EFO:0005001", "EFO:0005055",
            "EFO:0005058", "EFO:0005093", "EFO:0005094", "EFO:0005208",
            "EFO:0005246", "EFO:0005271", "EFO:0005665", "EFO:0005680",
            "EFO:0005763", "EFO:0006335", "EFO:0006336", "EFO:0006340",
            "EFO:0006807", "EFO:0006831", "EFO:0006832", "EFO:0007630",
            "EFO:0007778", "EFO:0007788", "EFO:0007789", "EFO:0007800",
            "EFO:0007817", "EFO:0007828", "EFO:0007878", "EFO:0007929",
            "EFO:0008000", "EFO:0008036", "EFO:0008037", "EFO:0008039",
            "EFO:0008328", "EFO:0008373", "EFO:0008455", "EFO:0008456",
            "EFO:0008595", "EFO:0008596", "EFO:0009270", "EFO:0009282",
            "EFO:0009283", "EFO:0009284", "EFO:0009718", "EFO:0009765",
            "EFO:0009767", "EFO:0009768", "EFO:0009769", "EFO:0009770",
            "EFO:0009792", "EFO:0009793", "EFO:0009881", "EFO:0009961",
            "EFO:0010074", "EFO:0010075", "EFO:0010076", "EFO:0010111",
            "EFO:0010112", "EFO:0010114", "EFO:0010115", "EFO:0010116",
            "EFO:0010117", "EFO:0010118", "EFO:0010119", "EFO:0010120",
            "EFO:0010177", "EFO:0010178", "EFO:0010465", "EFO:0010555",
            "EFO:1000783", "EFO:1000786", "EFO:1001506", 'MONDO:0004981',
            'MONDO:0005010', 'MONDO:0005300', 'MONDO:0007455'
        ]
        for node in nodes:
            if sorted(list(node.keys())) == ['id', 'type'
                                             ] and node['type'] == 'gene':
                if isNullOrWhiteSpace(node['id']): return False
                geneNodeFound = True
                geneId = node['id']
            elif sorted(list(node.keys())) == ['curie', 'id', 'type'
                                               ] and node['type'] == 'disease':
                if isNullOrWhiteSpace(node['id']): return False
                if node['curie'] not in validCuries: return False
                diseaseNodeFound = True
                diseaseId = node['id']
            else:
                return False
        if not geneNodeFound or not diseaseNodeFound: return False
        if geneId != edge['target_id'] or diseaseId != edge['source_id']:
            return False

        return True
    def userRequestBodyStructureIsSupported(self):
        """
        A function to evaluate whether the valid response is supported by our api
        :param body: A dictionary representing a JSON body
        :return: Boolean: True meaning our api supports these parameters, False meaning our api does not support these parameters
        """

        # todo, return why the structure isn't supported

        # validate edges in query_graph
        edges = self.userRequestBody['message']['query_graph']['edges']
        if type(edges) != dict: return False
        if len(edges) != 1: return False

        # validate the one edge
        edge = edges[list(edges.keys())[0]]
        if type(edge) != dict: return False

        # define expected keys in edge
        requiredEdgeKeys = ['object', 'predicates', 'subject']
        optionalEdgeKeys = ['constraints']
        allowedEdgeKeys = requiredEdgeKeys + optionalEdgeKeys

        # make sure all required edge keys are present
        for requiredEdgeKey in requiredEdgeKeys:
            if requiredEdgeKey not in list(edge.keys()):
                return False

        # make sure only certain edge keys are present
        for edgeKey in edge:
            if edgeKey not in allowedEdgeKeys:
                return False

        # make sure object and subject keys in edge are valid
        for key in ['object', 'subject']:
            if type(edge[key]) != str: return False
            if isNullOrWhiteSpace(edge[key]): return False

        # make sure object and subject keys in edge aren't the same
        if edge['object'] == edge['subject']: return False

        # make sure predicates in edge are valid
        if type(edge['predicates']) != list: return False
        for predicate in edge['predicates']:
            if type(predicate) != str: return False
            if isNullOrWhiteSpace(predicate): return False

        # validate nodes in query_graph
        nodes = self.userRequestBody['message']['query_graph']['nodes']
        if type(nodes) != dict: return False
        if len(nodes) != 2: return False

        # validate the two nodes
        if edge['object'] not in nodes: return False
        if edge['subject'] not in nodes: return False
        objectNode = nodes[edge['object']]
        subjectNode = nodes[edge['subject']]

        # define expected keys in nodes
        requiredNodeKeys = ['categories']
        optionalNodeKeys = ['constraints', 'ids']
        allowedNodeKeys = requiredNodeKeys + optionalNodeKeys

        for node in [objectNode, subjectNode]:
            if type(node) != dict: return False

            # make sure all required node keys are present
            for requiredNodeKey in requiredNodeKeys:
                if requiredNodeKey not in list(node.keys()):
                    return False

            # make sure only certain node keys are present
            for nodeKey in node:
                if nodeKey not in allowedNodeKeys:
                    return False

            # validate categories
            if type(node['categories']) != list: return False
            for category in node['categories']:
                if type(category) != str: return False
                if isNullOrWhiteSpace(category): return False

            # validate ids if present
            if 'ids' in node:
                if type(node['ids']) != list: return False
                for curieId in node['ids']:
                    if type(curieId) != str: return False
                    if isNullOrWhiteSpace(curieId): return False

        # validate common properties to edge and nodes
        for element in [edge, objectNode, subjectNode]:

            # validate constraints if present
            if 'constraints' in element:
                if type(element['constraints']) != list: return False
                for constraint in element['constraints']:
                    if type(constraint) != dict: return False

        # if everything else passes, its supported
        return True