Skip to content

LinkML

LinkML is the schema language MIxS, Biolink and NMDC are maintained in. Its generators produce JSON Schema, SHACL, OWL, Pydantic models, SQL DDL and documentation from one schema. Issue #471 makes it the canonical profile format in four steps, each gated on the one before:

  1. A converter from a metaseed profile to a LinkML schema, for every shipped profile.
  2. A round-trip gate: profile to LinkML and back must equal the original for every shipped profile.
  3. A loader that reads LinkML and builds the same internal model as today.
  4. Converting the shipped profiles, removing the YAML profile format, and importing LinkML schemas such as MIxS as profile sources.

Steps 1 and 2 are implemented: metaseed.specs.linkml converts in both directions. Step 3 is the loader: a profile stored as LinkML loads into the same ProfileSpec as its YAML form (see Reading a LinkML profile). Step 4 converted the shipped profiles: each is profile.linkml.yaml, and every one loads into exactly the ProfileSpec its former profile.yaml did, which was compared before the YAML files were removed; their comments moved with them. A profile a person creates, a plugin's, and the hub's still use YAML, and the spec builder, the MCP tools and MetaseedClient.from_yaml read and write it. LinkML import of an outside schema such as MIxS is the remaining part.

Editing a shipped profile

Edit its profile.linkml.yaml directly. Native LinkML slots (range, required, pattern, examples, ...) are read as the mapping below says; metaseed_ annotations hold the rest as plain YAML, a list or mapping under value:; the validation rules are the schema's metaseed_validation_rules annotation, one entry per rule as in a YAML profile. The class expressions under a class's all_of are written from the rules and not read: change the rule, not the expression. metaseed profile linkml regenerates a file from its loaded profile, which drops the comments, so prefer editing in place.

Converting a profile

from metaseed.specs.linkml import from_linkml, to_linkml
from metaseed.specs.loader import SpecLoader

spec = SpecLoader().load_profile("1.2", "miappe")
schema = to_linkml(spec)          # a LinkML schema as a dict, ready for yaml.safe_dump
assert from_linkml(schema) == spec

to_linkml_yaml(spec) gives the schema as YAML text, and profile_from_document(data) reads a parsed document in either format into a ProfileSpec: a LinkML schema (it has classes and no entities) through from_linkml, anything else as metaseed's own format. The spec builder's LinkML download and its import, in the web interface and in metaseed-hub, go through these two.

From the command line, metaseed profile linkml --profile miappe --version 1.2 > miappe.yaml writes the schema, which LinkML's own tools read, for example gen-shacl miappe.yaml for SHACL shapes or gen-json-schema miappe.yaml for JSON Schema.

Reading a LinkML profile

A profile version directory holds its profile as profile.yaml (metaseed's format) or as profile.linkml.yaml (a LinkML schema as to_linkml writes it), never both: a directory with both is refused with an error naming the two files, since either could be the stale one. The loader reads the LinkML file through from_linkml and applies the same checks as to a YAML file, so every module that uses the loaded profile sees the same ProfileSpec whichever format it came from; a test loads every shipped profile both ways and compares them. Saving a profile from the spec builder or the API keeps the format it is stored in.

Mapping

Profile LinkML
name, version, display_name, description name, version, title, description
the profile itself id urn:metaseed:profile:<name>:<version>, with a default_prefix of the same name
ontologies with a uri prefixes
root_entity tree_root: true on that class
entity class, with its fields as class-local attributes in field order
entity ontology_term class_uri
field type range: string, integer, float, boolean, date, datetime, uri; ontology_term becomes uriorcurie
list multivalued: true, range the items type or class
entity, list of an entity range the class, inlined: true (with inlined_as_list for a list)
required, description, label required, description, title
ontology_term slot_uri
is_identifier identifier: true
example examples
constraints.pattern, minimum, maximum pattern, minimum_value, maximum_value
constraints.min_items, max_items minimum_cardinality, maximum_cardinality
options, constraints.enum an enum <Class>_<field> with the values as permissible_values, as the range

Everything without a LinkML equivalent is kept as an annotation, which LinkML allows on any element and its generators ignore: the tier, codename, unit, ISA tag, SEEK settings, within, reference_scope, unique_within, owns, an entity's example, the profile's spec_version and validation rules, and the ontology definitions' OLS ids. An annotation's tag is metaseed_<key> and its value the key's value as plain YAML: a string, number, boolean or null as it is, a list or mapping under value: (metaseed_constraints: {value: {min_length: 2}}), which is how LinkML reads an annotation whose value is not a scalar. The schema carries metaseed_annotations: plain to say so; a schema without it, as metaseed 0.76.0 wrote, has every value encoded as JSON text and still reads. Where a native slot does not hold a value exactly (an integer example, which LinkML stores as text; the legacy item types int and bool), the value is also kept as an annotation, and reading back prefers it. That is what makes the round trip lossless rather than close.

Validation rules are kept as one annotation on the schema, which is what metaseed reads back and evaluates. In addition, a condition rule built only from field presence (doi OR pubmed_id OR title, (latitude AND longitude) OR (NOT latitude AND NOT longitude); 24 of the 30 in the shipped profile versions) is written as a LinkML class expression on each class it applies to, so LinkML's tools enforce it too: a field is a slot_conditions entry with required: true, NOT field one with value_presence: ABSENT, AND is all_of, OR is any_of, NOT (…) is none_of, and each rule is one entry of the class's all_of. LinkML's JSON Schema generator turns these into required, not required, allOf and anyOf. A rule naming a field its class does not have gets no expression on that class. The 6 comparisons (end_date >= start_date) have no LinkML equivalent and stay metaseed's only. These expressions are derived from the rules and not read back: editing one in a LinkML file changes what LinkML's tools check, not what metaseed checks.

One difference remains: metaseed counts a field holding an empty string or an empty list as absent, while JSON Schema counts any key that is present. A record checked with LinkML's tools should leave empty fields out for the two to agree.

Gates

  • Lossless: for every shipped profile, from_linkml(to_linkml(spec)) == spec; a key the converter drops fails the test.
  • Valid LinkML: every converted profile loads in LinkML's SchemaView, and the JSON Schema and SHACL generators run on it.
  • Same outcome: for every native presence rule, every combination of its fields being present or absent is checked against both metaseed's rule engine and the JSON Schema LinkML generates; they must agree on each.