Skip to content

Commit

Permalink
Merge pull request #72 from geneontology/v0.2.2
Browse files Browse the repository at this point in the history
update doc, endpoint test fixes
  • Loading branch information
sierra-moxon authored Sep 12, 2023
2 parents 1c6482d + 3389738 commit 7d51f3b
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 64 deletions.
50 changes: 35 additions & 15 deletions app/routers/bioentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ontobio.config import get_config
from ontobio.golr.golr_associations import search_associations

from app.utils.golr_utils import run_solr_text_on
from app.utils.golr_utils import gu_run_solr_text_on
from app.utils.settings import ESOLR, ESOLRDoc, get_user_agent

from .slimmer import gene_to_uniprot_from_mygene
Expand Down Expand Up @@ -48,7 +48,11 @@ class RelationshipType(str, Enum):

@router.get("/api/bioentity/{id}", tags=["bioentity"], description="Get bio-entities (genes) by their identifiers.")
async def get_bioentity_by_id(
id: str = Path(..., description="The CURIE of the gene to be retrieved. (e.g. ZFIN:ZDB-GENE-990415-1)"),
id: str = Path(
...,
description="The CURIE of the gene to be retrieved. (e.g. ZFIN:ZDB-GENE-990415-44)",
example="ZFIN:ZDB-GENE-990415-44",
),
start: int = Query(0, description="The starting index for pagination. Default is 0."),
rows: int = Query(100, description="The number of results per page. Default is 100."),
):
Expand All @@ -68,8 +72,8 @@ async def get_bioentity_by_id(
:raises HTTPException: If the bioentity with the provided identifier is not found in the database.
:note:
- For example, to get a gene with the identifier 'ZFIN:ZDB-GENE-990415-1', the URL should be:
'/api/bioentity/ZFIN:ZDB-GENE-990415-1'.
- For example, to get a gene with the identifier 'ZFIN:ZDB-GENE-990415-44', the URL should be:
'/api/bioentity/ZFIN:ZDB-GENE-990415-44'.
- The 'start' and 'rows' parameters can be used for pagination of results.
'start' determines the starting index for fetching results, and 'rows' specifies
the number of results to be retrieved per page.
Expand All @@ -89,7 +93,7 @@ async def get_bioentity_by_id(

optionals = "&defType=edismax&start=" + str(start) + "&rows=" + str(rows)
# id here is passed to solr q parameter, query_filters go to the boost, fields are what's returned
bioentity = run_solr_text_on(ESOLR.GOLR, ESOLRDoc.BIOENTITY, id, query_filters, fields, optionals)
bioentity = gu_run_solr_text_on(ESOLR.GOLR, ESOLRDoc.BIOENTITY, id, query_filters, fields, optionals, False)
return bioentity


Expand All @@ -99,7 +103,11 @@ async def get_bioentity_by_id(
description="Get gene or gene product information via a GO term id, e.g. GO:0044598.",
)
async def get_annotations_by_goterm_id(
id: str = Path(..., description="The CURIE of the GO term to be used for annotation retrieval. (e.g. GO:0044598)"),
id: str = Path(
...,
description="The CURIE of the GO term to be used for annotation retrieval. (e.g. GO:0044598)",
example="GO:0044598",
),
evidence: List[str] = Query(None),
start: int = Query(0, description="The starting index for pagination. Default is 0."),
rows: int = Query(100, description="The number of results per page. Default is 100."),
Expand Down Expand Up @@ -157,7 +165,7 @@ async def get_annotations_by_goterm_id(
evidence += ")"

optionals = "&defType=edismax&start=" + str(start) + "&rows=" + str(rows) + evidence
data = run_solr_text_on(ESOLR.GOLR, ESOLRDoc.ANNOTATION, id, query_filters, fields, optionals)
data = gu_run_solr_text_on(ESOLR.GOLR, ESOLRDoc.ANNOTATION, id, query_filters, fields, optionals, False)

return data

Expand All @@ -168,10 +176,15 @@ async def get_annotations_by_goterm_id(
description="Returns genes annotated to the provided GO Term. e.g. GO:0044598",
)
async def get_genes_by_goterm_id(
id: str = Path(..., description="The CURIE of the GO term to be used for gene retrieval. (e.g. GO:0044598)"),
id: str = Path(
...,
description="The CURIE of the GO term to be used for gene retrieval. (e.g. GO:0044598)",
example="GO:0044598",
),
taxon: List[str] = Query(
default=None,
description="One or more taxon CURIE to filter associations by subject taxon",
example=["NCBITaxon:7955", "NCBITaxon:9606"],
),
relationship_type: RelationshipType = Query(
default=RelationshipType.INVOLVED_IN,
Expand Down Expand Up @@ -211,15 +224,16 @@ async def get_genes_by_goterm_id(
and 'annotation_extension_class_label' associated with the provided GO term.
"""
association_return = {}
if relationship_type == ACTS_UPSTREAM_OF_OR_WITHIN:
return search_associations(
association_return = search_associations(
subject_category="gene",
use_compact_associations=True,
object_category="function",
fq={
"regulates_closure": id,
},
subject_taxon=taxon,
invert_subject_object=True,
user_agent=USER_AGENT,
slim=slim,
taxon=taxon,
Expand All @@ -231,7 +245,7 @@ async def get_genes_by_goterm_id(
elif relationship_type == INVOLVED_IN_REGULATION_OF:
# Temporary fix until https://github.com/geneontology/amigo/pull/469
# and https://github.com/owlcollab/owltools/issues/241 are resolved
return search_associations(
association_return = search_associations(
subject_category="gene",
object_category="function",
fq={
Expand All @@ -249,7 +263,7 @@ async def get_genes_by_goterm_id(
rows=rows,
)
elif relationship_type == INVOLVED_IN:
return search_associations(
association_return = search_associations(
subject_category="gene",
object_category="function",
subject=id,
Expand All @@ -259,6 +273,7 @@ async def get_genes_by_goterm_id(
user_agent=USER_AGENT,
url=ESOLR.GOLR,
)
return {"associations": association_return.get("associations")}


@router.get(
Expand All @@ -267,7 +282,11 @@ async def get_genes_by_goterm_id(
description="Returns taxon information for genes annotated to the provided GO term, e.g. GO:0044598",
)
async def get_taxon_by_goterm_id(
id: str = Path(..., description="The CURIE of the GO term to be used for taxon retrieval. (e.g. GO:0044598)"),
id: str = Path(
...,
description="The CURIE of the GO term to be used for taxon retrieval. (e.g. GO:0044598)",
example="GO:0044598",
),
evidence: List[str] = Query(
default=None,
description="Object id, e.g. ECO:0000501 (for IEA; "
Expand Down Expand Up @@ -322,7 +341,7 @@ async def get_taxon_by_goterm_id(
taxon_restrictions += ")"

optionals = "&defType=edismax&start=" + str(start) + "&rows=" + str(rows) + evidence + taxon_restrictions
data = run_solr_text_on(ESOLR.GOLR, ESOLRDoc.ANNOTATION, id, query_filters, fields, optionals)
data = gu_run_solr_text_on(ESOLR.GOLR, ESOLRDoc.ANNOTATION, id, query_filters, fields, optionals, False)

return data

Expand All @@ -337,6 +356,7 @@ async def get_annotations_by_gene_id(
...,
description="The CURIE identifier of the gene for which GO term associations are retrieved."
"(e.g., ZFIN:ZDB-GENE-050417-357)",
example="ZFIN:ZDB-GENE-050417-357",
),
slim: List[str] = Query(
default=None,
Expand Down Expand Up @@ -415,4 +435,4 @@ async def get_annotations_by_gene_id(
for asc in pr_assocs["associations"]:
log.info(asc)
assocs["associations"].append(asc)
return assocs
return {"associations": assocs.get("associations")}
7 changes: 1 addition & 6 deletions app/routers/labeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@

@router.get("/api/ontol/labeler", tags=["ontol/labeler"])
async def expand_curie(
id: List[str] = Query(..., description="IDs to fetch labels for.", example=["GO:0003677", "MGI:3588192"])
id: List[str] = Query(..., description="IDs to fetch labels for.", example=["GO:0003677", "GO:0008150"])
):
"""Fetches a map from IDs to labels e.g. GO:0003677."""
for i in id:
if "MGI:MGI" in i:
id.remove(i)
id.append(i.replace("MGI:MGI", "MGI:"))

return batch_fetch_labels(id)
47 changes: 33 additions & 14 deletions app/routers/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from oaklib.resource import OntologyResource

import app.utils.ontology_utils as ontology_utils
from app.utils.golr_utils import run_solr_on, run_solr_text_on
from app.utils.golr_utils import gu_run_solr_text_on, run_solr_on
from app.utils.prefix_utils import get_prefixes
from app.utils.settings import ESOLR, ESOLRDoc, get_sparql_endpoint, get_user_agent
from app.utils.sparql_utils import transform, transform_array
Expand All @@ -29,22 +29,30 @@ class GraphType(str, Enum):

@router.get("/api/ontology/term/{id}", tags=["ontology"])
async def get_term_metadata_by_id(
id: str = Path(..., description="The ID of the term to extract the metadata from, e.g. GO:0003677")
id: str = Path(
..., description="The ID of the term to extract the metadata from, e.g. GO:0003677", example="GO:0003677"
),
):
"""Returns metadata of an ontology term, e.g. GO:0003677."""
ont_r = OntologyResource(url=get_sparql_endpoint())
si = SparqlImplementation(ont_r)
query = ontology_utils.create_go_summary_sparql(id)
results = si._sparql_query(query)
return transform(
transformed_result = transform(
results[0],
["synonyms", "relatedSynonyms", "alternativeIds", "xrefs", "subsets"],
)
cmaps = get_prefixes("go")
converter = Converter.from_prefix_map(cmaps, strict=False)
transformed_result["goid"] = converter.compress(transformed_result["goid"])
return transformed_result


@router.get("/api/ontology/term/{id}/graph", tags=["ontology"])
async def get_term_graph_by_id(
id: str = Path(..., description="The ID of the term to extract the graph from, e.g. GO:0003677"),
id: str = Path(
..., description="The ID of the term to extract the graph from, e.g. GO:0003677", example="GO:0003677"
),
graph_type: GraphType = Query(GraphType.topology_graph),
):
"""Returns graph of an ontology term, e.g. GO:0003677."""
Expand All @@ -61,11 +69,12 @@ async def get_term_graph_by_id(
@router.get(
"/api/ontology/term/{id}/subgraph",
tags=["ontology"],
description="Extract a subgraph from an ontology term. e.g. GO:0003677 "
"using the relationships is_a and part_of.",
description="Extract a subgraph from an ontology term. e.g. GO:0003677 using the relationships is_a and part_of.",
)
async def get_subgraph_by_term_id(
id: str = Path(..., description="The ID of the term to extract the subgraph from, e.g. GO:0003677"),
id: str = Path(
..., description="The ID of the term to extract the subgraph from, e.g. GO:0003677", example="GO:0003677"
),
start: int = Query(0, description="The start index of the results to return"),
rows: int = Query(100, description="The number of results to return"),
):
Expand All @@ -83,7 +92,9 @@ async def get_subgraph_by_term_id(
fields = "id,annotation_class_label,isa_partof_closure,isa_partof_closure_label"
optionals = "&defType=edismax&start=" + str(start) + "&rows=" + str(rows)

descendent_data = run_solr_text_on(ESOLR.GOLR, ESOLRDoc.ONTOLOGY, where_statement, query_filters, fields, optionals)
descendent_data = gu_run_solr_text_on(
ESOLR.GOLR, ESOLRDoc.ONTOLOGY, where_statement, query_filters, fields, optionals, False
)

descendents = []
for child in descendent_data:
Expand All @@ -95,7 +106,9 @@ async def get_subgraph_by_term_id(

golr_field_to_search = "id"
where_statement = "*:*&fq=" + golr_field_to_search + ":" + '"' + id + '"'
ancestor_data = run_solr_text_on(ESOLR.GOLR, ESOLRDoc.ONTOLOGY, where_statement, query_filters, fields, optionals)
ancestor_data = gu_run_solr_text_on(
ESOLR.GOLR, ESOLRDoc.ONTOLOGY, where_statement, query_filters, fields, optionals, False
)
ancestors = []
for parent in ancestor_data[0]["isa_partof_closure"]:
ancestors.append({"id": parent})
Expand All @@ -110,8 +123,8 @@ async def get_subgraph_by_term_id(
description="Returns the ancestor ontology terms shared by two ontology terms. ",
)
async def get_ancestors_shared_by_two_terms(
subject: str = Path(..., description="Identifier of a GO term, e.g. GO:0006259"),
object: str = Path(..., description="Identifier of a GO term, e.g. GO:0046483"),
subject: str = Path(..., description="Identifier of a GO term, e.g. GO:0006259", example="GO:0006259"),
object: str = Path(..., description="Identifier of a GO term, e.g. GO:0046483", example="GO:0046483"),
):
"""
Returns the ancestor ontology terms shared by two ontology terms.
Expand Down Expand Up @@ -145,7 +158,9 @@ async def get_ancestors_shared_by_two_terms(
tags=["ontology"],
description="Returns GO-CAM model identifiers for a given GO term ID, e.g. GO:0008150",
)
async def get_go_term_detail_by_go_id(id: str = Path(..., description="A GO-Term CURIE (e.g. GO:0005885, GO:0097136)")):
async def get_go_term_detail_by_go_id(
id: str = Path(..., description="A GO-Term CURIE (e.g. GO:0005885, GO:0097136)", example="GO:0008150")
):
"""
Returns models for a given GO term ID.
Expand All @@ -168,7 +183,9 @@ async def get_go_term_detail_by_go_id(id: str = Path(..., description="A GO-Term
tags=["ontology"],
description="Returns parent and children relationships for a given GO ID, e.g. GO:0005885",
)
async def get_go_hierarchy_go_id(id: str = Path(..., description="A GO-Term ID, e.g. GO:0097136")):
async def get_go_hierarchy_go_id(
id: str = Path(..., description="A GO-Term ID, e.g. GO:0097136", example="GO:0008150")
):
"""
Returns parent and children relationships for a given GO ID.
Expand Down Expand Up @@ -222,7 +239,9 @@ async def get_go_hierarchy_go_id(id: str = Path(..., description="A GO-Term ID,
tags=["ontology"],
description="Returns GO-CAM model identifiers for a given GO term ID, e.g. GO:0008150",
)
async def get_gocam_models_by_go_id(id: str = Path(..., description="A GO-Term ID(e.g. GO:0005885, GO:0097136 ...)")):
async def get_gocam_models_by_go_id(
id: str = Path(..., description="A GO-Term ID(e.g. GO:0097136 ...)", example="GO:0097136")
):
"""
Returns GO-CAM model identifiers for a given GO term ID.
Expand Down
15 changes: 9 additions & 6 deletions app/routers/ribbon.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from oaklib.resource import OntologyResource

import app.utils.ontology_utils as ontology_utils
from app.utils.golr_utils import run_solr_text_on
from app.utils.golr_utils import gu_run_solr_text_on
from app.utils.settings import ESOLR, ESOLRDoc, get_sparql_endpoint, get_user_agent
from app.utils.sparql_utils import transform_array

Expand All @@ -24,20 +24,23 @@

@router.get("/api/ontology/term/{id}/subsets", tags=["ontology"])
async def get_subsets_by_term(
id: str = Path(..., description="The ID of the term to extract the subsets from, e.g. GO:0003677")
id: str = Path(
..., description="The ID of the term to extract the subsets from, e.g. GO:0003677", example="GO:0003677"
)
):
"""Returns subsets (slims) associated to an ontology term."""
ont_r = OntologyResource(url=get_sparql_endpoint())
si = SparqlImplementation(ont_r)
query = ontology_utils.get_go_subsets_sparql_query(id)
results = si._sparql_query(query)
results = transform_array(results, [])
results = (results, "subset", "OBO:go#", "")
return results


@router.get("/api/ontology/subset/{id}", tags=["ontology"])
async def get_subset_by_id(id: str = Path(..., description="Name of the subset to map GO terms (e.g. goslim_agr)")):
async def get_subset_by_id(
id: str = Path(..., description="Name of the subset to map GO terms (e.g. goslim_agr)", example="goslim_agr")
):
"""Returns a subset (slim) by its id which is usually a name."""
result = ontology_utils.get_ontology_subsets_by_id(id=id)
return result
Expand Down Expand Up @@ -166,7 +169,7 @@ async def get_ribbon_results(
fq += "&fq=!evidence_type:IBA"
if exclude_PB:
fq += '&fq=!annotation_class:"GO:0005515"'
data = run_solr_text_on(ESOLR.GOLR, ESOLRDoc.ANNOTATION, q, qf, fields, fq)
data = gu_run_solr_text_on(ESOLR.GOLR, ESOLRDoc.ANNOTATION, q, qf, fields, fq, False)
# compute number of terms and annotations
for annot in data:
aspect = ontology_utils.aspect_map[annot["aspect"]]
Expand Down Expand Up @@ -275,7 +278,7 @@ async def get_ribbon_results(
qf = ""
fq = '&fq=bioentity:("' + '" or "'.join(mod_ids) + '")&rows=100000'
fields = "bioentity,bioentity_label,taxon,taxon_label"
data = run_solr_text_on(ESOLR.GOLR, ESOLRDoc.BIOENTITY, q, qf, fields, fq)
data = gu_run_solr_text_on(ESOLR.GOLR, ESOLRDoc.BIOENTITY, q, qf, fields, fq, False)

for entity in subjects:
for entity_detail in data:
Expand Down
4 changes: 2 additions & 2 deletions app/routers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from fastapi import APIRouter, Path, Query

from app.utils.golr_utils import run_solr_text_on
from app.utils.golr_utils import gu_run_solr_text_on
from app.utils.settings import ESOLR, ESOLRDoc, get_user_agent

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -70,7 +70,7 @@ async def autocomplete_term(
category = ESOLRDoc.ANNOTATION

optionals = "&defType=edismax&start=" + str(start) + "&rows=" + str(rows)
data = run_solr_text_on(ESOLR.GOLR, category, term + "*", query_fields, fields, optionals)
data = gu_run_solr_text_on(ESOLR.GOLR, category, term + "*", query_fields, fields, optionals, True)
docs = []

for item in data:
Expand Down
4 changes: 2 additions & 2 deletions app/routers/slimmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ async def slimmer_function(
subject: List[str] = Query(
...,
description="example: ZFIN:ZDB-GENE-980526-388, MGI:3588192",
example="ZFIN:ZDB-GENE-980526-388, MGI:3588192",
example=["ZFIN:ZDB-GENE-980526-388", "MGI:3588192"],
),
slim: List[str] = Query(
...,
description="a set of GO term ids to use as the slim, example: GO:0008150, GO:0003674, GO:0005575",
example="GO:0008150, GO:0003674, GO:0005575",
example=["GO:0008150", "GO:0003674", "GO:0005575"],
),
exclude_automatic_assertions: bool = False,
rows: int = Query(default=-1, description="Number of rows to return, -1 for all"),
Expand Down
Loading

0 comments on commit 7d51f3b

Please sign in to comment.