Skip to content

Commit

Permalink
Refactor JSON writer functions
Browse files Browse the repository at this point in the history
- Refactored 3 JSON writer functions to a state that they were probably intended to be in: 1 json function with branching logic depending on 'serialization' param.
  • Loading branch information
joeflack4 committed May 11, 2024
1 parent 01388a2 commit 6015796
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
49 changes: 19 additions & 30 deletions src/sssom/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,20 @@ def write_rdf(
print(t.decode(), file=file)


# todo: not sure the need for serialization param here; seems superfluous for some of these funcs
def write_fhir_json(msdf: MappingSetDataFrame, output: TextIO, serialisation="fhir") -> None:
"""Write a mapping set dataframe to the file as FHIR ConceptMap JSON."""
data = to_fhir_json(msdf)
json.dump(data, output, indent=2)


def write_json(msdf: MappingSetDataFrame, output: TextIO, serialisation="json") -> None:
"""Write a mapping set dataframe to the file as JSON."""
if serialisation == "json":
data = to_json(msdf)
json.dump(data, output, indent=2)
else:
raise ValueError(f"Unknown json format: {serialisation}, currently only json supported")
func_map: Dict[str, Callable] = {
"fhir_json": to_fhir_json,
"json": to_json,
"ontoportal_json": to_ontoportal_json,
}
if serialisation not in func_map:
raise ValueError(
f"Unknown JSON format: {serialisation}. Supported flavors: {', '.join(func_map.keys())}"
)
func: Callable = func_map[serialisation]
data = func(msdf)
json.dump(data, output, indent=2)


def write_owl(
Expand All @@ -133,18 +133,6 @@ def write_owl(
print(t.decode(), file=file)


def write_ontoportal_json(
msdf: MappingSetDataFrame, output: TextIO, serialisation: str = "ontoportal_json"
) -> None:
"""Write a mapping set dataframe to the file as the ontoportal mapping JSON model."""
if serialisation != "ontoportal_json":
raise ValueError(
f"Unknown json format: {serialisation}, currently only ontoportal_json supported"
)
data = to_ontoportal_json(msdf)
json.dump(data, output, indent=2)


# Converters
# Converters convert a mappingsetdataframe to an object of the supportes types (json, pandas dataframe)

Expand Down Expand Up @@ -264,9 +252,6 @@ def to_rdf_graph(msdf: MappingSetDataFrame) -> Graph:
return graph


# TODO: add to CLI & to these functions: r4 vs r5 param
# TODO: What if the msdf doesn't have everything we need? (i) metadata, e.g. yml, (ii) what if we need to override?
# - todo: later: allow any nested aribtrary override: (get in kwargs, else metadata.get(key, None))
def to_fhir_json(msdf: MappingSetDataFrame) -> Dict:
"""Convert a mapping set dataframe to a JSON object.
Expand All @@ -276,6 +261,10 @@ def to_fhir_json(msdf: MappingSetDataFrame) -> Dict:
Resources:
- ConcpetMap::SSSOM mapping spreadsheet: https://docs.google.com/spreadsheets/d/1J19foBAYO8PCHwOfksaIGjNu-q5ILUKFh2HpOCgYle0/edit#gid=1389897118
TODO: add to CLI & to these functions: r4 vs r5 param
TODO: What if the msdf doesn't have everything we need? (i) metadata, e.g. yml, (ii) what if we need to override?
- todo: later: allow any nested aribtrary override: (get in kwargs, else metadata.get(key, None))
Minor todos
todo: `mapping_justification` consider `ValueString` -> `ValueCoding` https://github.com/timsbiomed/issues/issues/152
todo: when/how to conform to R5 instead of R4?: https://build.fhir.org/conceptmap.html
Expand Down Expand Up @@ -496,9 +485,9 @@ def to_ontoportal_json(msdf: MappingSetDataFrame) -> List[Dict]:
WRITER_FUNCTIONS: Dict[str, Tuple[Callable, Optional[str]]] = {
"tsv": (write_table, None),
"owl": (write_owl, SSSOM_DEFAULT_RDF_SERIALISATION),
"ontoportal_json": (write_ontoportal_json, None),
"fhir_json": (write_fhir_json, None),
"json": (write_json, None),
"ontoportal_json": (write_json, "ontoportal_json"),
"fhir_json": (write_json, "fhir_json"),
"json": (write_json, "json"),
"rdf": (write_rdf, SSSOM_DEFAULT_RDF_SERIALISATION),
}
for rdf_format in RDF_FORMATS:
Expand Down
6 changes: 2 additions & 4 deletions tests/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
from sssom.writers import (
_update_sssom_context_with_prefixmap,
to_json,
write_fhir_json,
write_json,
write_ontoportal_json,
write_owl,
write_rdf,
write_table,
Expand Down Expand Up @@ -132,7 +130,7 @@ def test_write_sssom_fhir(self):
"""Test writing as FHIR ConceptMap JSON."""
path = os.path.join(test_out_dir, "test_write_sssom_fhir.json")
with open(path, "w") as file:
write_fhir_json(self.msdf, file)
write_json(self.msdf, file, "fhir_json")
# todo: @Joe: after implementing reader/importer, change this to `msdf = parse_sssom_fhir_json()`
with open(path, "r") as file:
d = json.load(file)
Expand All @@ -154,7 +152,7 @@ def test_write_sssom_ontoportal_json(self):
"""Test writing as ontoportal JSON."""
path = os.path.join(test_out_dir, "test_write_sssom_ontoportal_json.json")
with open(path, "w") as file:
write_ontoportal_json(self.msdf, file)
write_json(self.msdf, file, "ontoportal_json")

with open(path, "r") as file:
d = json.load(file)
Expand Down

0 comments on commit 6015796

Please sign in to comment.