예제 #1
0
def expand(curie: str, prefix_maps: Optional[List[dict]] = None, fallback: bool = True) -> str:
    """
    Expand a given CURIE to an URI, based on mappings from `prefix_map`.

    This method will return the CURIE as the IRI if there is no mapping found.

    Parameters
    ----------
    curie: str
        A CURIE
    prefix_maps: Optional[List[dict]]
        A list of prefix maps to use for mapping
    fallback: bool
        Determines whether to fallback to default prefix mappings, as determined
        by `prefixcommons.curie_util`, when CURIE prefix is not found in `prefix_maps`.

    Returns
    -------
    str
        A URI corresponding to the CURIE

    """
    default_curie_maps = [get_jsonld_context('monarch_context'), get_jsonld_context('obo_context')]
    if prefix_maps:
        uri = expand_uri(curie, prefix_maps)
        if uri == curie and fallback:
            uri = expand_uri(curie, default_curie_maps)
    else:
        uri = expand_uri(curie, default_curie_maps)

    return uri
예제 #2
0
파일: validator.py 프로젝트: biolink/kgx
    def __init__(
            self,
            verbose: bool = False,
            progress_monitor: Optional[Callable[[GraphEntityType, List], None]] = None,
            schema: Optional[str] = None,
            error_log: str = None
    ):
        ErrorDetecting.__init__(self, error_log)

        # formal arguments
        self.verbose: bool = verbose
        self.progress_monitor: Optional[
            Callable[[GraphEntityType, List], None]
        ] = progress_monitor

        # TODO: fix... this attribute is not used anywhere at the moment?
        self.schema: Optional[str] = schema

        # internal attributes
        # associated currently active _currently_active_toolkit with this Validator instance
        self.validating_toolkit = self.get_toolkit()
        self.prefix_manager = PrefixManager()
        self.jsonld = get_jsonld_context()
        self.prefixes = self.get_all_prefixes(self.jsonld)
        self.required_node_properties = self.get_required_node_properties()
        self.required_edge_properties = self.get_required_edge_properties()
예제 #3
0
파일: validator.py 프로젝트: biolink/kgx
    def get_all_prefixes(jsonld: Optional[Dict] = None) -> set:
        """
        Get all prefixes from Biolink Model JSON-LD context.

        It also sets ``self.prefixes`` for subsequent access.

        Parameters
        ---------
        jsonld: Optional[Dict]
            The JSON-LD context

        Returns
        -------
        Optional[Dict]
            A set of prefixes

        """
        if not jsonld:
            jsonld = get_jsonld_context()
        prefixes: Set = set(
            k
            for k, v in jsonld.items()
            if isinstance(v, str)
            or (isinstance(v, dict) and v.setdefault("@prefix", False))
        )  # @type: ignored
        if "biolink" not in prefixes:
            prefixes.add("biolink")
        return prefixes
예제 #4
0
def contract(uri: str,
             prefix_maps: Optional[List[Dict]] = None,
             fallback: bool = True) -> str:
    """
    Contract a given URI to a CURIE, based on mappings from `prefix_maps`.
    If no prefix map is provided then will use defaults from prefixcommons-py.

    This method will return the URI as the CURIE if there is no mapping found.

    Parameters
    ----------
    uri: str
        A URI
    prefix_maps: Optional[List[Dict]]
        A list of prefix maps to use for mapping
    fallback: bool
        Determines whether to fallback to default prefix mappings, as determined
        by `prefixcommons.curie_util`, when URI prefix is not found in `prefix_maps`.

    Returns
    -------
    str
        A CURIE corresponding to the URI

    """
    curie = uri
    default_curie_maps = [
        get_jsonld_context("monarch_context"),
        get_jsonld_context("obo_context"),
    ]
    if prefix_maps:
        curie_list = contract_uri(uri, prefix_maps)
        if len(curie_list) == 0:
            if fallback:
                curie_list = contract_uri(uri, default_curie_maps)
                if curie_list:
                    curie = curie_list[0]
        else:
            curie = curie_list[0]
    else:
        curie_list = contract_uri(uri, default_curie_maps)
        if len(curie_list) > 0:
            curie = curie_list[0]

    return curie
예제 #5
0
 def __init__(self, verbose: bool = False):
     self.toolkit = get_toolkit()
     self.prefix_manager = PrefixManager()
     self.jsonld = get_jsonld_context()
     self.prefixes = Validator.get_all_prefixes(self.jsonld)
     self.required_node_properties = Validator.get_required_node_properties(
     )
     self.required_edge_properties = Validator.get_required_edge_properties(
     )
     self.verbose = verbose
예제 #6
0
    def __init__(self, url: str = None):
        """
        Initialize an instance of PrefixManager.

        Parameters
        ----------
        url: str
            The URL from which to read a JSON-LD context for prefix mappings

        """
        if url:
            context = cu.read_remote_jsonld_context(url)
        else:
            context = get_jsonld_context()
        self.set_prefix_map(context)
예제 #7
0
    def get_all_prefixes(jsonld: dict = None) -> set:
        """
        Get all prefixes from Biolink Model JSON-LD context.

        It also sets ``self.prefixes`` for subsequent access.

        Returns
        -------
        set
            A set of prefixes

        """
        if not jsonld:
            jsonld = get_jsonld_context()
        prefixes = set(k for k, v in jsonld.items() if isinstance(v, str))
        if 'biolink' not in prefixes:
            prefixes.add('biolink')
        return prefixes
예제 #8
0
    def __init__(self,
                 verbose: bool = False,
                 progress_monitor: Optional[Callable[[GraphEntityType, List],
                                                     None]] = None):
        # formal arguments
        self.verbose: bool = verbose
        self.progress_monitor: Optional[Callable[[GraphEntityType, List],
                                                 None]] = progress_monitor

        # internal attributes
        self.toolkit = get_toolkit()
        self.prefix_manager = PrefixManager()
        self.jsonld = get_jsonld_context()
        self.prefixes = Validator.get_all_prefixes(self.jsonld)
        self.required_node_properties = Validator.get_required_node_properties(
        )
        self.required_edge_properties = Validator.get_required_edge_properties(
        )
        self.errors: List[ValidationError] = list()
예제 #9
0
    def get_all_prefixes(jsonld: Optional[Dict] = None) -> set:
        """
        Get all prefixes from Biolink Model JSON-LD context.

        It also sets ``self.prefixes`` for subsequent access.

        Parameters
        ---------
        jsonld: Optional[Dict]
            The JSON-LD context

        Returns
        -------
        Optional[Dict]
            A set of prefixes

        """
        if not jsonld:
            jsonld = get_jsonld_context()
        prefixes = set(jsonld.keys())
        if 'biolink' not in prefixes:
            prefixes.add('biolink')
        return prefixes