Ejemplo n.º 1
0
    def add_prefixmap(self, map_name: str, include_defaults: bool= True) -> None:
        """
        Add a prefixcommons map.  Only prefixes that have not been previously defined are added.

        :param map_name: prefixcommons map name
        :param include_defaults: if True, take defaults from the map.
        :return:
        """
        for k, v in curie_util.read_biocontext(map_name).items():
            if not k:
                if include_defaults and not self._default:
                    self._default = v
            elif k not in self:
                if NCName.is_valid(k):
                    self[k] = v
                else:
                    print(f"Warning: biocontext map {map_name} has illegal prefix: {k}", file=sys.stderr)
Ejemplo n.º 2
0
    def uri_for(self, uri_or_curie: Any) -> URIRef:
        """
        Map a curie or URI into a full URIRef.

        :param uri_or_curie: "NCNAME ':' suffix" or plain URI
        :return: Corresponding URI
        """
        uri_or_curie = str(uri_or_curie)
        if '://' in uri_or_curie:
            return URIRef(uri_or_curie)
        if ':' in uri_or_curie:
            prefix, local = str(uri_or_curie).split(':', 1)
            if not prefix:
                prefix = self._default_key
            elif not NCName.is_valid(prefix):
                raise ValueError(f"Not a valid CURIE: {uri_or_curie}")
        else:
            prefix, local = self._base_key, uri_or_curie

        if prefix not in self:
            raise ValueError(f"Unknown CURIE prefix: {prefix}")
        return URIRef(self.join(self[prefix], local))