diff --git a/src/biomappings/contribute/obo.py b/src/biomappings/contribute/obo.py index 0f53a55d..31d37a0f 100644 --- a/src/biomappings/contribute/obo.py +++ b/src/biomappings/contribute/obo.py @@ -31,16 +31,17 @@ def update_obo(*, prefix: str, path: Union[str, Path]) -> None: file.writelines(lines) -def update_obo_lines(*, lines: List[str], mappings: List[Dict[str, Any]]) -> List[str]: +def update_obo_lines(*, lines: List[str], mappings: List[Dict[str, Any]], progress: bool = True) -> List[str]: """Update the lines of an OBO file. :param mappings: Mappings to add :param lines: A list of lines of the file (still containing trailing newlines) + :param progress: Show a progress bar :returns: New lines. Does not modify the original list. """ lines = deepcopy(lines) - for mapping in tqdm(mappings, unit="mapping", unit_scale=True): + for mapping in tqdm(mappings, unit="mapping", unit_scale=True, disable=not progress): target_prefix = mapping["target prefix"] target_prefix = bioregistry.get_preferred_prefix(target_prefix) or target_prefix target_identifier = standardize_identifier(target_prefix, mapping["target identifier"]) diff --git a/tests/test_contribute.py b/tests/test_contribute.py index 7331613f..2b877e18 100644 --- a/tests/test_contribute.py +++ b/tests/test_contribute.py @@ -9,8 +9,8 @@ class TestContributeOBO(unittest.TestCase): """A test case for contributing to OBO flat files.""" - def test_addition(self): - """Test adding a non-redundant mapping.""" + def setUp(self) -> None: + """Set up the test case with a specific mapping.""" mappings = get_curated_mappings("uberon") sources = {mapping["source identifier"] for mapping in mappings} self.assertIn("UBERON:0000018", sources, msg="Mappings are not loaded properly") @@ -23,7 +23,10 @@ def test_addition(self): and mappings["target prefix"] == "idomal" ] self.assertEqual(1, len(mappings)) + self.mappings = mappings + def test_addition(self): + """Test adding a non-redundant mapping.""" original = dedent( """\ [Term] @@ -61,5 +64,53 @@ def test_addition(self): ) self.assertEqual( expected.splitlines(), - update_obo_lines(mappings=mappings, lines=original.splitlines()), + update_obo_lines(mappings=self.mappings, lines=original.splitlines(), progress=False), + ) + + def test_skip_redundant_1(self): + """Test skipping adding a mapping that doesn't have metadata.""" + original = dedent( + """\ + [Term] + id: UBERON:0000018 + name: compound eye + def: "A light sensing organ composed of ommatidia." [FB:gg, Wikipedia:Compound_eye] + subset: organ_slim + synonym: "adult compound eye" RELATED [] + xref: BTO:0001921 + xref: HAO:0000217 + xref: IDOMAL:0002421 + xref: TGMA:0000024 + intersection_of: UBERON:0000970 ! eye + relationship: only_in_taxon NCBITaxon:6656 {source="PMID:21062451"} ! Arthropoda + property_value: seeAlso "https://github.com/obophenotype/uberon/issues/457" xsd:anyURI + """ + ) + self.assertEqual( + original.splitlines(), + update_obo_lines(mappings=self.mappings, lines=original.splitlines(), progress=False), + ) + + def test_skip_redundant_2(self): + """Test skipping adding a mapping that doesn't have metadata.""" + original = dedent( + """\ + [Term] + id: UBERON:0000018 + name: compound eye + def: "A light sensing organ composed of ommatidia." [FB:gg, Wikipedia:Compound_eye] + subset: organ_slim + synonym: "adult compound eye" RELATED [] + xref: BTO:0001921 + xref: HAO:0000217 + xref: IDOMAL:0002421 {dcterms:contributor="https://orcid.org/0000-0003-4423-4370"} ! compound eye + xref: TGMA:0000024 + intersection_of: UBERON:0000970 ! eye + relationship: only_in_taxon NCBITaxon:6656 {source="PMID:21062451"} ! Arthropoda + property_value: seeAlso "https://github.com/obophenotype/uberon/issues/457" xsd:anyURI + """ + ) + self.assertEqual( + original.splitlines(), + update_obo_lines(mappings=self.mappings, lines=original.splitlines(), progress=False), )