Skip to content

Commit

Permalink
Add GPI option to gpad2gocams for geneontology/gocamgen#83
Browse files Browse the repository at this point in the history
  • Loading branch information
dustine32 committed Jun 24, 2020
1 parent a5d6700 commit 86103ce
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
11 changes: 6 additions & 5 deletions bin/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import logging
import sys
import traceback
import subprocess

import yamldown

Expand Down Expand Up @@ -547,10 +548,11 @@ def produce(ctx, group, metadata_dir, gpad, ttl, target, ontology, exclude, base
@cli.command()
@click.pass_context
@click.option("--gpad_path", "-g", type=click.Path(), required=True)
@click.option("--gpi_path", "-i", type=click.Path(), required=True)
@click.option("--target", "-t", type=click.Path(), required=True)
@click.option("--ontology", "-o", type=click.Path(exists=True), required=False)
# Eventually will need access to GPI as well for getting taxon ID - should exist by now - get path from metadata
def gpad2gocams(ctx, gpad_path, target, ontology):
# Eventually will need access to GPI as well for getting taxon ID - should exist by now
def gpad2gocams(ctx, gpad_path, gpi_path, target, ontology):
if gpad_path.endswith(".gz"):
unzipped = os.path.splitext(gpad_path)[0]
unzip(gpad_path, unzipped)
Expand All @@ -562,14 +564,13 @@ def gpad2gocams(ctx, gpad_path, target, ontology):
absolute_target = os.path.abspath(target)
gpad_basename = os.path.basename(gpad_path)
gpad_basename_root, gpad_ext = os.path.splitext(gpad_basename)
if gpad_ext in ["gpad", "gpa"]:
if gpad_ext in [".gpad", ".gpa"]:
output_basename = gpad_basename_root + ".nq"
else:
output_basename = gpad_basename + ".nq"
output_path = os.path.join(absolute_target, output_basename)

ontology_graph = OntologyFactory().create(ontology, ignore_cache=True)
builder = GoCamBuilder(ontology_graph)
builder = GoCamBuilder(ontology, gpi_file=gpi_path)

for gene, associations in assocs_by_gene.items():
builder.make_model_and_add_to_store(gene, annotations=associations)
Expand Down
48 changes: 42 additions & 6 deletions ontobio/rdfgen/gocamgen/gocam_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ontobio.rdfgen.gocamgen.utils import ShexException
from ontobio.io.gpadparser import GpadParser
from ontobio.io.assocparser import AssocParserConfig
from ontobio.io.entityparser import GpiParser
from ontobio.ontol_factory import OntologyFactory
from ontobio.util.go_utils import GoAspector
import argparse
Expand All @@ -16,6 +17,7 @@
import time
import click
from os import path
from typing import List
# from abc import ABC, abstractmethod
from rdflib.graph import ConjunctiveGraph
from rdflib.store import Store
Expand All @@ -40,24 +42,26 @@


class GoCamBuilder:
def __init__(self, ontology_graph=None):
# self.ro_ontology = OntologyFactory().create("http://purl.obolibrary.org/obo/ro.owl")
# self.gorel_ontology = OntologyFactory().create("http://release.geneontology.org/2019-03-18/ontology/extensions/gorel.obo")
# Can't get logical_definitions w/ ont.create("go"), need to load ontology via PURL
# self.ontology = OntologyFactory().create("http://purl.obolibrary.org/obo/go.owl")
if ontology_graph is None:
def __init__(self, ontology=None, gpi_file=None):
if ontology is None:
ontology_graph = OntologyFactory().create("http://purl.obolibrary.org/obo/go/extensions/go-lego.owl")
else:
ontology_graph = OntologyFactory().create(ontology, ignore_cache=True)
self.ontology = ontology_graph
self.ro_ontology = self.extract_relations_ontology(self.ontology)
# self.ontology = OntologyFactory().create("http://purl.obolibrary.org/obo/go.owl")
# self.ro_ontology = OntologyFactory().create("http://purl.obolibrary.org/obo/ro.owl")
self.aspector = GoAspector(self.ontology)
self.store = plugin.get('IOMemory', Store)()
self.errors = GeneErrorSet() # Errors by gene ID
self.gpi_entities = self.parse_gpi(gpi_file)

def translate_to_model(self, gene, assocs):
model = AssocGoCamModel(gene, assocs, store=self.store)
model.ontology = self.ontology
model.ro_ontology = self.ro_ontology
model.go_aspector = self.aspector
model.gpi_entities = self.gpi_entities
model.translate()

return model
Expand Down Expand Up @@ -120,6 +124,38 @@ def extract_relations_ontology(ontology_graph):
ro_terms = ro_terms + ontology_graph.descendants(t, reflexive=True)
return ontology_graph.subontology(nodes=ro_terms)

@staticmethod
def parse_gpi(gpi_file):
# {
# "id":"MGI:MGI:87853",
# "label":"a",
# "full_name":"nonagouti",
# "synonyms":[
# "agouti",
# "As",
# "agouti signal protein",
# "ASP"
# ],
# "type":"gene",
# "parents":[
#
# ],
# "xrefs":[
# "UniProtKB:Q03288"
# ],
# "taxon":{
# "id":"NCBITaxon:10090"
# }
# }
if gpi_file is None:
return None
parser = GpiParser()
gpi_entities = {}
entities = parser.parse(gpi_file)
for entity in entities:
gpi_entities[entity['id']] = entity
return gpi_entities


class AssocExtractor:
def __init__(self, gpad_file, parser_config: AssocParserConfig = None):
Expand Down
2 changes: 2 additions & 0 deletions ontobio/rdfgen/gocamgen/gocamgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,12 @@ def __init__(self, modeltitle, assocs, connection_relations=None, store=None):
self.go_aspector = None
self.default_contributor = "http://orcid.org/0000-0002-6659-0416"
self.graph.bind("GOREL", GOREL) # Because GOREL isn't in context.jsonld's
self.gpi_entities = None

def translate(self):

self.associations.go_ontology = self.ontology
self.associations.gpi_entities = self.gpi_entities
self.associations.collapse_annotations()

for a in self.associations:
Expand Down

0 comments on commit 86103ce

Please sign in to comment.