Skip to content

Commit ab5710d

Browse files
Merge pull request #47 from AustralianBioCommons/fix-flip-multiplicity
Implement multiplicity flipping in link conversion
2 parents 8ed4af0 + d3bf13e commit ab5710d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/gen3schemadev/converter.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,25 @@ def create_core_metadata_link(child_name: str, required: bool = False) -> dict:
196196
return link_obj.to_dict()
197197

198198

199+
def flip_multiplicity(multiplicity: str) -> str:
200+
"""
201+
Flip the multiplicity of a link.
202+
203+
Args:
204+
multiplicity: The current multiplicity of the link.
205+
206+
Returns:
207+
The flipped multiplicity.
208+
"""
209+
if multiplicity == "one_to_one" or multiplicity == "many_to_many":
210+
return multiplicity
211+
elif multiplicity == "one_to_many":
212+
return "many_to_one"
213+
elif multiplicity == "many_to_one":
214+
return "one_to_many"
215+
else:
216+
raise ValueError(f"Invalid multiplicity: {multiplicity}")
217+
199218
def convert_node_links(links: list[dict], required: bool = True) -> list[dict]:
200219
"""
201220
Convert a list of link dictionaries into the Gen3 schema 'links' format.
@@ -214,7 +233,7 @@ def convert_node_links(links: list[dict], required: bool = True) -> list[dict]:
214233
backref=link_suffix(link['child']),
215234
label="part_of",
216235
target_type=link['parent'],
217-
multiplicity=link['multiplicity'],
236+
multiplicity=flip_multiplicity(link['multiplicity']),
218237
required=required
219238
)
220239
link_list.append(link_obj.to_dict())
@@ -539,7 +558,7 @@ def construct_props(node_name: str, data: DataSourceProtocol) -> dict:
539558

540559
# Add link properties
541560
for link in links:
542-
link_prop = create_link_prop(link['parent'], link['multiplicity'])
561+
link_prop = create_link_prop(link['parent'], flip_multiplicity(link['multiplicity']))
543562
props_dict.update(link_prop)
544563

545564
# if it's an Enum, add the enum values

tests/test_converter.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_convert_node_links():
7575
assert link0["name"] == "samples"
7676
assert link0["backref"] == "lipidomics_files"
7777
assert link0["target_type"] == "sample"
78-
assert link0["multiplicity"] == "many_to_one"
78+
assert link0["multiplicity"] == "one_to_many"
7979
assert link0["required"] is True
8080
assert "label" in link0 and link0["label"] is 'part_of'
8181

@@ -84,7 +84,7 @@ def test_convert_node_links():
8484
assert link1["name"] == "projects"
8585
assert link1["backref"] == "samples"
8686
assert link1["target_type"] == "project"
87-
assert link1["multiplicity"] == "one_to_many"
87+
assert link1["multiplicity"] == "many_to_one"
8888
assert link1["required"] is True
8989
assert "label" in link1 and link1["label"] is 'part_of'
9090

@@ -448,8 +448,8 @@ def test_construct_prop_lipidomics_file(fixture_input_yaml_pass):
448448
result = construct_props("lipidomics_file", fixture_input_yaml_pass)
449449
expected = {
450450
"$ref": "_definitions.yaml#/data_file_properties",
451-
"samples": {"$ref": "_definitions.yaml#/to_many"},
452-
"assays": {"$ref": "_definitions.yaml#/to_many"},
451+
"samples": {"$ref": "_definitions.yaml#/to_one"},
452+
"assays": {"$ref": "_definitions.yaml#/to_one"},
453453
"core_metadata_collections": {"$ref": "_definitions.yaml#/to_one"},
454454
'cv': {'description': 'Coefficient of variation (%)', 'type': 'number'}
455455

@@ -507,7 +507,7 @@ def test_construct_prop_sample(fixture_input_yaml_pass):
507507
"description": "Free text notes (string)"
508508
},
509509
"projects": {
510-
"$ref": "_definitions.yaml#/to_many"
510+
"$ref": "_definitions.yaml#/to_one"
511511
}
512512
}
513513
assert result == expected
@@ -562,15 +562,15 @@ def fixture_expected_output_lipid():
562562
'backref': 'lipidomics_files',
563563
'label': 'part_of',
564564
'target_type': 'sample',
565-
'multiplicity': 'one_to_many',
565+
'multiplicity': 'many_to_one',
566566
'required': True
567567
},
568568
{
569569
'name': 'assays',
570570
'backref': 'lipidomics_files',
571571
'label': 'part_of',
572572
'target_type': 'assay',
573-
'multiplicity': 'one_to_many',
573+
'multiplicity': 'many_to_one',
574574
'required': True
575575
},
576576
{
@@ -585,8 +585,8 @@ def fixture_expected_output_lipid():
585585
}],
586586
'properties': {
587587
'$ref': '_definitions.yaml#/data_file_properties',
588-
'samples': {'$ref': '_definitions.yaml#/to_many'},
589-
'assays': {'$ref': '_definitions.yaml#/to_many'},
588+
'samples': {'$ref': '_definitions.yaml#/to_one'},
589+
'assays': {'$ref': '_definitions.yaml#/to_one'},
590590
'core_metadata_collections': {'$ref': '_definitions.yaml#/to_one'},
591591
'cv': {'description': 'Coefficient of variation (%)', 'type': 'number'}
592592
},

0 commit comments

Comments
 (0)