Skip to content

Commit

Permalink
add derived transformations spec example and file based transformatio…
Browse files Browse the repository at this point in the history
…n spec
  • Loading branch information
sierra-moxon committed Nov 13, 2024
1 parent aed82cf commit 28b53d0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 15 deletions.
18 changes: 16 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "linkml-map"
version = "0.0.0"
version = "0.3.8.post1.dev0+aed82cf"
description = "a framework for specifying and executing mappings between data models"
authors = ["cmungall <[email protected]>"]
readme = "README.md"
Expand All @@ -12,6 +12,7 @@ asteval = "^0.9.29"
deepdiff = "^6.7.1"
pydantic = ">=2.0.0"
ucumvert = "^0.1.1"
linkml-model = "^1.8.0"

[tool.poetry.dev-dependencies]
pytest = "^7.3.1"
Expand All @@ -24,7 +25,7 @@ mkdocs-windmill = "*"
tox = "*"

[tool.poetry-dynamic-versioning]
enable = true
enable = false
vcs = "git"
style = "pep440"

Expand Down
77 changes: 66 additions & 11 deletions tests/test_transformer/test_biolink_subsetting.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,62 @@
from pathlib import Path
from typing import List
from pytest import fixture

from linkml_runtime.dumpers import yaml_dumper
import pytest
from pprint import pprint
from linkml_map.datamodel.transformer_model import TransformationSpecification
from linkml_map.datamodel.transformer_model import TransformationSpecification, ClassDerivation, SlotDerivation
from linkml_map.inference.schema_mapper import SchemaMapper
from linkml_map.session import Session
from linkml_runtime.utils.schemaview import SchemaView
from src.linkml_map.utils.loaders import load_specification
from linkml_map.transformer.transformer import Transformer
from linkml_map.utils.multi_file_transformer import Transformation
from linkml_model.meta import ClassDefinition
from linkml.utils.schema_builder import SchemaBuilder
from linkml_runtime.utils.formatutils import camelcase

REPO_ROOT = Path(__file__).resolve().parent.parent



def test_biolink_subsetting():
# Test that the subsetting of the Biolink Model is correct
# This test is a placeholder and should be replaced with a real test
repo_root = Path(__file__).resolve().parent.parent
@fixture
def biolink_schema():
schema_url = "https://raw.githubusercontent.com/biolink/biolink-model/master/biolink-model.yaml"
sv = SchemaView(schema_url)
return sv

def test_biolink_subsetting_manual(biolink_schema):

transform_file = REPO_ROOT / "input/examples/biolink/transform/biolink-example-profile.transform.yaml"
# Initialize Session and SchemaBuilder
session = Session()

# Set the source schema in the session
session.set_source_schema(biolink_schema)

tr_spec = load_specification(transform_file)
mapper = SchemaMapper()
mapper.source_schemaview = biolink_schema

target_schema_obj = mapper.derive_schema(specification=tr_spec,
target_schema_id="biolink-profile",
target_schema_name="BiolinkProfile")

transform_file = repo_root / "input/examples/biolink/transform/biolink-example-profile.transform.yaml"

yaml_dumper.dump(target_schema_obj, str("biolink-profile.yaml"))

transformed_sv = SchemaView("biolink-profile.yaml")

for class_name in transformed_sv.all_classes():
print(class_name)


def test_biolink_subset_auto(biolink_schema):
# Initialize Session and SchemaBuilder
session = Session()

# Set the source schema in the session
session.set_source_schema(sv)
session.set_source_schema(biolink_schema)

SUBSET_CLASSES = ["gene",
"disease",
Expand All @@ -36,13 +67,37 @@ def test_biolink_subsetting():
"phenotypic feature",
]

class_derivations = get_biolink_class_derivations(biolink_schema, SUBSET_CLASSES)

ts = TransformationSpecification(class_derivations=class_derivations)

tr_spec = load_specification(transform_file)
mapper = SchemaMapper()
mapper.source_schemaview = sv
mapper.source_schemaview = biolink_schema

target_schema_obj = mapper.derive_schema(specification=tr_spec,
target_schema_obj = mapper.derive_schema(specification=ts,
target_schema_id="biolink-profile",
target_schema_name="BiolinkProfile")

yaml_dumper.dump(target_schema_obj, str("biolink-profile.yaml"))

transformed_sv = SchemaView("biolink-profile.yaml")

for class_name in transformed_sv.all_classes():
print(class_name)



# Function to get Biolink class definitions
def get_biolink_class_derivations(sv, subset_classes) -> dict:
# Example implementation to fetch class definitions
# This should be replaced with the actual implementation
class_derivations ={}
for class_name in subset_classes:
class_derivation = ClassDerivation(populated_from=class_name,
name=camelcase(class_name))
for slot in sv.get_class(class_name).slots:
slot_derivation = SlotDerivation(populated_from=slot, name=camelcase(slot))
class_derivation.slot_derivations[slot] = slot_derivation
class_derivations[camelcase(class_name)] = class_derivation
return class_derivations

0 comments on commit 28b53d0

Please sign in to comment.