From 91c8670d2b94fdfd955f822176555a5834a55ae9 Mon Sep 17 00:00:00 2001 From: Sierra Taylor Moxon Date: Tue, 14 May 2024 17:38:55 -0700 Subject: [PATCH] trying to fix type error on 3.7 --- bin/ontobio-parse-assocs.py | 1 + bin/validate.py | 4 +-- ontobio/io/gafgpibridge.py | 54 +++++++++++++++++++------------------ tests/test_gafparser.py | 2 +- tests/test_gpiwriter.py | 15 +++-------- 5 files changed, 36 insertions(+), 40 deletions(-) diff --git a/bin/ontobio-parse-assocs.py b/bin/ontobio-parse-assocs.py index f74174da..beece86b 100755 --- a/bin/ontobio-parse-assocs.py +++ b/bin/ontobio-parse-assocs.py @@ -34,6 +34,7 @@ import sys import json import logging +from typing import Dict, List def main(): """ diff --git a/bin/validate.py b/bin/validate.py index 0a2eef6f..2c037b3a 100755 --- a/bin/validate.py +++ b/bin/validate.py @@ -13,7 +13,7 @@ import logging import sys import traceback - +from typing import Dict, List import yamldown from functools import wraps @@ -448,7 +448,7 @@ def produce_gpi(dataset, target_dir, gaf_path, ontology_graph, gpad_gpi_output_v gpi_path = os.path.join(os.path.split(gaf_path)[0], "{}.gpi".format(dataset)) with open(gaf_path) as gf, open(gpi_path, "w") as gpi: click.echo("Using {} as the gaf to build gpi with".format(gaf_path)) - bridge = gafgpibridge.GafGpiBridge() + bridge = gafgpibridge gpiwriter = entitywriter.GpiWriter(file=gpi, version=gpad_gpi_output_version) gpi_cache = set() diff --git a/ontobio/io/gafgpibridge.py b/ontobio/io/gafgpibridge.py index 29479f4d..be9dc034 100644 --- a/ontobio/io/gafgpibridge.py +++ b/ontobio/io/gafgpibridge.py @@ -3,6 +3,7 @@ from ontobio.model.association import GoAssociation, gp_type_label_to_curie + class Entity(dict): def __init__(self, d): @@ -13,35 +14,36 @@ def __hash__(self): return hash(d) +def convert_association(association) -> Entity | None: + """ + 'id' is already `join`ed in both the Association and the Entity, + so we don't have to worry about what that looks like. We assume + it's correct. + """ + + if isinstance(association, GoAssociation): + # print(json.dumps(association, indent=4)) + gpi_obj = { + 'id': str(association.subject.id), + 'label': association.subject.label, # db_object_symbol, + 'full_name': association.subject.fullname, # db_object_name, + 'synonyms': association.subject.synonyms, + 'type': [gp_type_label_to_curie(association.subject.type[0])], #db_object_type, + 'parents': "", # GAF does not have this field, but it's optional in GPI + 'xrefs': "", # GAF does not have this field, but it's optional in GPI + 'taxon': { + 'id': str(association.subject.taxon) + } + } + return Entity(gpi_obj) + + return None + + class GafGpiBridge(object): def __init__(self): self.cache = [] - def convert_association(self, association) -> Entity: - """ - 'id' is already `join`ed in both the Association and the Entity, - so we don't have to worry about what that looks like. We assume - it's correct. - """ - - if isinstance(association, GoAssociation): - # print(json.dumps(association, indent=4)) - gpi_obj = { - 'id': str(association.subject.id), - 'label': association.subject.label, # db_object_symbol, - 'full_name': association.subject.fullname, # db_object_name, - 'synonyms': association.subject.synonyms, - 'type': [gp_type_label_to_curie(association.subject.type[0])], #db_object_type, - 'parents': "", # GAF does not have this field, but it's optional in GPI - 'xrefs': "", # GAF does not have this field, but it's optional in GPI - 'taxon': { - 'id': str(association.subject.taxon) - } - } - return Entity(gpi_obj) - - return None - - def entities(self) -> List[Entity]: + def entities(self): return list(self.cache) diff --git a/tests/test_gafparser.py b/tests/test_gafparser.py index 9c907408..48f821f1 100644 --- a/tests/test_gafparser.py +++ b/tests/test_gafparser.py @@ -580,7 +580,7 @@ def test_gaf_gpi_bridge(): gaf = ["MGI", "MGI:1923503", "0610006L08Rik", "enables", "GO:0003674", "MGI:MGI:2156816|GO_REF:0000015", "ND", "", "F", "RIKEN cDNA 0610006L08 gene", "", "gene", "taxon:10090", "20120430", "MGI", "", ""] association = gafparser.to_association(gaf, qualifier_parser=assocparser.Qualifier2_2()).associations[0] - bridge = gafgpibridge.GafGpiBridge() + bridge = gafgpibridge entity = bridge.convert_association(association) assert entity.get("type") == ["gene"] diff --git a/tests/test_gpiwriter.py b/tests/test_gpiwriter.py index 38cb102e..3cec74df 100644 --- a/tests/test_gpiwriter.py +++ b/tests/test_gpiwriter.py @@ -5,7 +5,6 @@ import pytest -@pytest.mark.parametrize("gpad_gpi_output_version", ["2.0", "1.2"]) @pytest.mark.parametrize("gpad_gpi_output_version", ["2.0", "1.2"]) def test_produce_gpi(gpad_gpi_output_version): # Base path relative to this script @@ -13,7 +12,6 @@ def test_produce_gpi(gpad_gpi_output_version): # Define the paths for the GAF and expected GPI file gaf_path = base_path / "mgi.gaf" - gpi_path = base_path / "mgi.gpi" # Ensure the GAF file exists to avoid FileNotFoundError if not gaf_path.exists(): @@ -33,15 +31,10 @@ def test_produce_gpi(gpad_gpi_output_version): # Verify the contents of the GPI file p = entityparser.GpiParser() with open(output_gpi_path, "r") as f: - assert p.parse(f) is not None, "The GPI file could not be parsed." + assert p.parse(f) is not None f.seek(0) # Reset file pointer to the beginning results = p.parse(f) - assert len(results) > 5, "The GPI file should have about 9 unique genes from ~ 90 associations in the GAF file." - - with open(output_gpi_path, "r") as f: - lines = f.readlines() - - assert len(lines) > 0, "The GPI file should not be empty." + assert len(results) > 5 def test_gpi_2_0_writer(): @@ -52,9 +45,9 @@ def test_gpi_2_0_writer(): 'synonyms': [], 'type': ["SO:0000000"], # db_object_type, 'taxon': {"id": "NCBITaxon:10090"}, - 'encoded_by': "", # encoded_by + 'encoded_by': "", # encoded_by 'parents': "", - 'protein_containing_complex_members': "", # protein_containing_complex_members + 'protein_containing_complex_members': "", # protein_containing_complex_members 'xrefs': "", 'properties': "" }