Skip to content

Commit

Permalink
trying to fix type error on 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
sierra-moxon committed May 15, 2024
1 parent 83f35a8 commit 91c8670
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 40 deletions.
1 change: 1 addition & 0 deletions bin/ontobio-parse-assocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import sys
import json
import logging
from typing import Dict, List

def main():
"""
Expand Down
4 changes: 2 additions & 2 deletions bin/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import logging
import sys
import traceback

from typing import Dict, List
import yamldown

from functools import wraps
Expand Down Expand Up @@ -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()

Expand Down
54 changes: 28 additions & 26 deletions ontobio/io/gafgpibridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from ontobio.model.association import GoAssociation, gp_type_label_to_curie


class Entity(dict):

def __init__(self, d):
Expand All @@ -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)
2 changes: 1 addition & 1 deletion tests/test_gafparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
15 changes: 4 additions & 11 deletions tests/test_gpiwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
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
base_path = Path(__file__).parent / "resources"

# 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():
Expand All @@ -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():
Expand All @@ -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': ""
}
Expand Down

0 comments on commit 91c8670

Please sign in to comment.