Skip to content

Commit

Permalink
Merge branch 'main' into issue-142b
Browse files Browse the repository at this point in the history
  • Loading branch information
cmungall authored Jul 30, 2024
2 parents ae5030c + a55ba85 commit 9bf91e4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
36 changes: 26 additions & 10 deletions schemasheets/schemamaker.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Converts a schema sheet into a LinkML schema"""
import codecs
import contextlib
import os
import sys
import csv
import logging
import tempfile
from pathlib import Path
from urllib.request import urlopen
from copy import copy
Expand Down Expand Up @@ -98,25 +97,42 @@ class SchemaSheetRowException(Exception):
@dataclass
class SchemaMaker:
"""
Engine for making LinkML schemas from Schema Sheets
Engine for making LinkML schemas from Schema Sheets.
"""
schema: SchemaDefinition = None
"""Generated schema."""

element_map: Dict[Tuple[str, str], Element] = None

metamodel: SchemaView = None
"""Schema describing LinkML elements."""

cardinality_vocabulary: str = None

use_attributes: bool = None
"""If True, use attributes instead of slots."""

default_name: str = None
"""Default name for the schema."""

unique_slots: bool = None
"""If True, slots are unique across classes."""

gsheet_id: str = None
"""Google sheet ID."""

gsheet_cache_dir: str = None

table_config_path: str = None
"""Path to table configuration file."""

base_schema_path: str = None

def create_schema(self, csv_files: Union[str, List[str]], **kwargs) -> SchemaDefinition:
"""
Create a LinkML schema from a collection of Schema Sheets
Create a LinkML schema from one or more Schema Sheets.
:param csv_files: schema sheets
:param csv_files: schema sheets paths
:param kwargs:
:return: generated schema
"""
Expand All @@ -132,8 +148,8 @@ def create_schema(self, csv_files: Union[str, List[str]], **kwargs) -> SchemaDef
if not isinstance(csv_files, list):
csv_files = [csv_files]
for f in csv_files:
self.merge_sheet(f, **kwargs)
# reconstitute schema
# reconstitute schema
self.load_and_merge_sheet(f, **kwargs)
self.schema = SchemaDefinition(**json_dumper.to_dict(self.schema))
self.schema.imports.append('linkml:types')
self.schema.prefixes['linkml'] = Prefix('linkml', 'https://w3id.org/linkml/')
Expand All @@ -149,7 +165,7 @@ def create_schema(self, csv_files: Union[str, List[str]], **kwargs) -> SchemaDef

def _tidy_slot_usage(self):
"""
removes all slot usages marked inapplicable
removes all slot usages marked inapplicable.
:return:
"""
Expand All @@ -160,9 +176,9 @@ def _tidy_slot_usage(self):
c.slots.remove(sn)
del c.slot_usage[sn]

def merge_sheet(self, file_name: str, delimiter='\t') -> None:
def load_and_merge_sheet(self, file_name: str, delimiter='\t') -> None:
"""
Merge information from the given schema sheet into the current schema
Merge information from the given schema sheet into the current schema.
:param file_name: schema sheet
:param delimiter: default is tab
Expand Down
9 changes: 6 additions & 3 deletions schemasheets/schemasheet_datamodel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Core data model for a SchemaSheet."""
import csv
from dataclasses import dataclass
from typing import Union, Dict, List, Any
Expand All @@ -18,8 +19,7 @@
DESCRIPTOR = str
ROW = Dict[str, Any]


#c = ClassDefinition
# Vocabulary for types
T_SCHEMA = 'schema'
T_CLASS = 'class'
T_SLOT = 'slot'
Expand Down Expand Up @@ -101,6 +101,9 @@ def add_info(self, info: Union[Dict, DESCRIPTOR]) -> None:
self.maps_to = info
mm = get_metamodel()
snmap = mm.slot_name_mappings()
for k, v in snmap.items():
if k != v.name:
print(k,v.name)
# TODO: use alias
snmap['uri'] = snmap['type_uri']
if self.maps_to.startswith("metaslot."):
Expand Down Expand Up @@ -274,4 +277,4 @@ def get_configmodel() -> SchemaView:
"""
package = 'schemasheets.conf.configschema'
data = pkgutil.get_data(package, f'configschema.yaml')
return SchemaView(data.decode("utf-8"))
return SchemaView(data.decode("utf-8"))
2 changes: 1 addition & 1 deletion tests/test_schema_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

def test_roundtrip_schema():
"""
Tests linkml2sheets by roundtripping from the standard personinfo schema in YAML
Tests linkml2sheets by round-tripping from the standard personinfo schema in YAML
"""
sm = SchemaMaker()
# sheets2linkml, from SHEET
Expand Down
3 changes: 3 additions & 0 deletions tests/test_structured_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@


def test_structured_syntax():
"""
Test that structured syntax is roundtripped
"""
sm = ss.SchemaMaker()
sheet_path = str(INPUT_DIR / "structured_syntax.tsv")
out_path = str(OUTPUT_DIR / "structured_syntax-roundtrip.tsv")
Expand Down

0 comments on commit 9bf91e4

Please sign in to comment.