Importers¶
Importers take either a representation of a schema in a different language or example data and bootstraps a schema.
Importers are the opposite of Generators in the core LinkML framework
Warning
Generally importers take a less expressive language than LinkML and attempts to create the corresponding
LinkML schema. This may be less optimal than a hand-crafted schema. For example, when converting to a
representation that lacks inheritance, no is_a slots
will be created.
Importing from JSON-Schema¶
The import-json-schema command can be used:
schemauto import-json-schema tests/resources/model_card.schema.json
Importing from Kwalify¶
The import-kwalify command can be used:
schemauto import-kwalify tests/resources/test.kwalify.yaml
Importing from OWL¶
You can import from a schema-style OWL ontology. This must be in functional syntax
Use robot to convert ahead of time:
robot convert -i schemaorg.ttl -o schemaorg.ofn
schemauto import-owl schemaorg.ofn
Importing from SHACL¶
You can import from a SHACL shapes graph. SHACL expresses a data model the way
LinkML does – classes with typed, cardinality-constrained slots – so shapes map
onto classes and their sh:property shapes onto attributes.
schemauto import-shacl tests/resources/shacl_simple.ttl \
--default-prefix usr --model-uri http://example.org/ -o user.yaml
Two conventions for relating shapes to classes are both supported, and the mode is detected automatically.
Explicit, where a shape names the class it constrains. This is the style used by DCAT-AP and by most hand-written shape files:
ex:UserShape a sh:NodeShape ; sh:targetClass ex:User ;
sh:property [ sh:path schema:name ; sh:datatype xsd:string ] .
Implicit, where the shape is itself the class. This is the style used by large
published ontologies such as ASHRAE 223P and Brick, which declare a term
rdfs:Class and sh:NodeShape at once and carry the hierarchy on
rdfs:subClassOf:
s223:Equipment a rdfs:Class, sh:NodeShape ;
rdfs:subClassOf s223:Connectable ;
sh:property [ sh:path s223:hasProperty ; sh:class s223:Property ] .
Options¶
--default-prefix and --model-uri together say which namespace is the
schema’s own. Terms from any other namespace are prefixed, so a vocabulary that
redeclares an imported name does not silently lose one of the two.
--identifier adds an identifier slot to root classes. SHACL models no notion of
identity, so this is opt-in rather than assumed.
--enum-root imports a subclass tree as enumerations. Some ontologies model
enumerations by punning class and instance – a member is rdfs:subClassOf its
kind and typed as itself, never an rdf:type instance of the kind – so
membership has to be read from the subclass closure:
schemauto import-shacl 223p.ttl --default-prefix s223 \
--model-uri 'http://data.ashrae.org/standard223#' \
--enum-root s223:EnumerationKind --identifier id -o s223.yaml
Limitations¶
sh:sparql constraints, sh:severity and sh:message are not imported.
These are validation concerns with no LinkML equivalent, and a shape carrying only
sh:sparql contributes neither a range nor a cardinality. Sequence and
alternative property paths are skipped for the same reason; sh:inversePath
becomes an inverseOf_ slot.
Importing from SQL¶
You can import a schema from a SQL database
The default is to assume a SQLite database:
schemauto import-sql path/to/my.db
You can also connect to any database server provided you have the necessary client software installed, using a SQL Alchemy connection path.
For example, for the RNA Central public database
schemauto import-sql postgresql+psycopg2://reader:NWDMCE5xdipIjRrp@hh-pgsql-public.ebi.ac.uk:5432/pfmegrnargs
Importing from caDSR¶
caDSR is an ISO-11179 compliant metadata registry. The ISO-11179 conceptual model can be mapped to LinkML. The canonical mapping maps a CDE onto a LinkML slot.
NCI implements a JSON serialization of ISO-11197. You can import this JSON and convert to LinkML:
schemauto import-cadsr "cdes/*.json"
Importing from DBML¶
DBML is a simple DSL for defining database schemas. It is a subset of SQL DDL.
Packages for importing¶
- class schema_automator.importers.JsonSchemaImportEngine(use_attributes: bool = False, is_openapi: bool = False)[source]¶
An ImportEngine that imports a JSON-Schema representation to a LinkML Schema
- convert(input: str, name=None, format='json', **kwargs) SchemaDefinition[source]¶
Converts a JSON-Schema json file into a LinkML schema
- Parameters:
input
name
format
kwargs
- Returns:
- translate_numeric_constraints(obj: Dict, slot: SlotDefinition) None[source]¶
Translates jsonschema numeric range keywords onto a slot
LinkML
minimum_value/maximum_valueare always inclusive, so an exclusive jsonschema bound is converted to the equivalent inclusive one. That conversion is exact for integers, where “greater than 0” is simply “at least 1”. Floats have no equivalent, so the bound is widened to the inclusive one and a warning is logged.Both spellings of exclusivity are accepted: the draft-4 form, where
exclusiveMinimumis a boolean makingminimumexclusive, and the draft-6 and later form, whereexclusiveMinimumis itself a number. The latter turns up on integers more often than hand-written schemas would suggest, since generators such as pydantic mapgt/ltonto it regardless of the type.- Parameters:
obj – jsonschema object for a numeric property
slot – slot to be modified in place
- class schema_automator.importers.OwlImportEngine(mappings: dict = None)[source]¶
An ImportEngine that takes schema-style OWL and converts it to a LinkML schema
- class schema_automator.importers.FrictionlessImportEngine[source]¶
An ImportEngine that imports Frictionless data packages with schema information
See:
- class schema_automator.importers.DOSDPImportEngine(mappings: dict = None)[source]¶
An ImportEngine that imports Ontology Design Patterns specified as DOSDP Yaml into a LinkML schema
See DOSDPs
Every template maps to a LinkML class, the default name for a template Foo as FooTemplate
- convert(files: str, range_as_enums=True, **kwargs) SchemaDefinition[source]¶
Converts one or more YAML files into a Schema
- Parameters:
files
range_as_enums – if True, then class ranges are mapped to Enums
kwargs
- Returns:
- create_class(pattern: Pattern, range_as_enums: bool = True, denormalize_labels: bool = True) ClassDefinition[source]¶
Create a LinkML class from a template/pattern
The class will have name FooTemplate, and will mixin TemplateClass, and will have one slot per var (slots will also be inherited from TemplateClass)
by default, the range of each var will be a class RangeClass, unless treated as enums
- Parameters:
pattern – DOSDP template
range_as_enums – if true, ranges will be enums
denoramlized – if true, create an optional label for each var
- Returns:
- class schema_automator.importers.CADSRImportEngine[source]¶
An ImportEngine that imports NCI CADSR CDEs
Ingests the output of caDSR API.
Note that we include a LinkML schema for this in schema_automator.metamodels.cadsr
Each CDE (DataElement) becomes a unique slot
the CDE is added as a lot of a context-specific class
the context-specific class is a subclass of the CDE’s DataElementConcept
Note that this creates a lot of 1-1 classes, as in many cases there is no attempt to group concepts. However, this is not always the case.
E.g. the concept with publicId 2012668 (Access Route) is used in 5 contexts (AHRQ, CCR, …)
Each context-specific concept has its own set of CDEs
See also https://github.com/monarch-initiative/cde-harmonization