-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #310 from AllenNeuralDynamics/release-v0.17.0
Release v0.17.0
- Loading branch information
Showing
17 changed files
with
545 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""REST service to retrieve metadata from databases.""" | ||
|
||
__version__ = "0.16.0" | ||
__version__ = "0.17.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Package to handle mgi requests""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"""Module to handle retrieving information from MGI""" | ||
|
||
import logging | ||
from typing import Any, List, Optional, Union | ||
|
||
import requests | ||
from pydantic import BaseModel, Extra, Field, HttpUrl | ||
from pydantic_settings import BaseSettings | ||
|
||
from aind_metadata_service.response_handler import ModelResponse | ||
|
||
|
||
class MgiSettings(BaseSettings): | ||
"""Settings required for endpoint""" | ||
|
||
url: HttpUrl = Field( | ||
default="https://www.informatics.jax.org//quicksearch/alleleBucket" | ||
) | ||
|
||
class Config: | ||
"""Set env prefix and forbid extra fields.""" | ||
|
||
env_prefix = "MGI_" | ||
extra = Extra.forbid | ||
|
||
|
||
class MgiSummaryRow(BaseModel): | ||
"""Model of Summary Row dictionary returned""" | ||
|
||
detailUri: Optional[str] = Field(default=None) | ||
featureType: Optional[str] = Field(default=None) | ||
strand: Optional[str] = Field(default=None) | ||
chromosome: Optional[str] = Field(default=None) | ||
stars: Optional[str] = Field(default=None) | ||
bestMatchText: Optional[str] = Field(default=None) | ||
bestMatchType: Optional[str] = Field(default=None) | ||
name: Optional[str] = Field(default=None) | ||
location: Optional[str] = Field(default=None) | ||
symbol: Optional[str] = Field(default=None) | ||
|
||
|
||
class MgiResponse(BaseModel): | ||
"""Model for response from MGI""" | ||
|
||
summaryRows: List[MgiSummaryRow] | ||
totalCount: Optional[int] = Field(default=None) | ||
meta: Optional[Any] = Field(default=None) | ||
|
||
|
||
class MgiClient: | ||
"""Client to connect to Mgi""" | ||
|
||
def __init__(self, settings: MgiSettings): | ||
"""Class constructor""" | ||
|
||
self.settings = settings | ||
|
||
def get_allele_info( | ||
self, allele_name: str | ||
) -> Union[MgiResponse, ModelResponse]: | ||
""" | ||
Get allele info from mgi endpoint | ||
Parameters | ||
---------- | ||
allele_name : str | ||
Returns | ||
------- | ||
MgiResponse | ||
""" | ||
try: | ||
params = { | ||
"queryType": "exactPhrase", | ||
"query": allele_name, | ||
"submit": "Quick+Search", | ||
"startIndex": "0", | ||
"results": "1", | ||
} | ||
response = requests.get( | ||
url=self.settings.url.unicode_string(), params=params | ||
) | ||
response.raise_for_status() | ||
response_model = MgiResponse(**response.json()) | ||
return response_model | ||
except Exception as e: | ||
logging.exception(e) | ||
return ModelResponse.internal_server_error_response() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Module to handle mapping data from mgi to aind-data-schema models""" | ||
|
||
import logging | ||
import re | ||
from typing import Union | ||
|
||
from aind_data_schema_models.pid_names import PIDName | ||
from aind_data_schema_models.registries import Registry | ||
|
||
from aind_metadata_service.client import StatusCodes | ||
from aind_metadata_service.mgi.client import MgiResponse | ||
from aind_metadata_service.response_handler import ModelResponse | ||
|
||
|
||
class MgiMapper: | ||
"""Class that handles mapping mgi info""" | ||
|
||
DETAIL_URI_PATTERN = re.compile(r"/allele/MGI:(\d+)") | ||
|
||
def __init__(self, mgi_info: Union[MgiResponse, ModelResponse]): | ||
"""Class constructor""" | ||
|
||
self.mgi_info = mgi_info | ||
|
||
def get_model_response(self) -> ModelResponse: | ||
"""Get a model response from mgi ingo""" | ||
|
||
try: | ||
if isinstance(self.mgi_info, ModelResponse): | ||
return self.mgi_info | ||
|
||
if len(self.mgi_info.summaryRows) == 0: | ||
return ModelResponse.no_data_found_error_response() | ||
first_summary_row = self.mgi_info.summaryRows[0] | ||
|
||
# 4 stars represent an exact match | ||
if ( | ||
first_summary_row.stars != "****" | ||
or first_summary_row.bestMatchType != "Synonym" | ||
): | ||
return ModelResponse.no_data_found_error_response() | ||
|
||
if ( | ||
re.match(self.DETAIL_URI_PATTERN, first_summary_row.detailUri) | ||
is not None | ||
): | ||
registry_identifier = re.match( | ||
self.DETAIL_URI_PATTERN, first_summary_row.detailUri | ||
).group(1) | ||
else: | ||
registry_identifier = None | ||
|
||
pid_name = PIDName( | ||
name=first_summary_row.symbol, | ||
abbreviation=None, | ||
registry=Registry.MGI, | ||
registry_identifier=registry_identifier, | ||
) | ||
|
||
model_response = ModelResponse( | ||
aind_models=[pid_name], status_code=StatusCodes.DB_RESPONDED | ||
) | ||
return model_response | ||
except Exception as e: | ||
logging.exception(e) | ||
return ModelResponse.internal_server_error_response() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.