|
| 1 | +from flask_restx import Namespace, Resource |
| 2 | +from markupsafe import escape |
| 3 | +from api import db |
| 4 | +from api.utils.bar_utils import BARUtils |
| 5 | +from api.models.gaia import Aliases |
| 6 | +from sqlalchemy import func |
| 7 | +import json |
| 8 | + |
| 9 | +gaia = Namespace("Gaia", description="Gaia", path="/gaia") |
| 10 | + |
| 11 | + |
| 12 | +@gaia.route("/aliases/<string:identifier>") |
| 13 | +class GaiaAliases(Resource): |
| 14 | + @gaia.param("identifier", _in="path", default="ABI3") |
| 15 | + def get(self, identifier=""): |
| 16 | + |
| 17 | + # Escape input |
| 18 | + identifier = escape(identifier) |
| 19 | + |
| 20 | + # Is it valid |
| 21 | + if BARUtils.is_alphanumeric(identifier): |
| 22 | + # Convert to json |
| 23 | + identifier_json = json.dumps([identifier]) |
| 24 | + |
| 25 | + # Get data |
| 26 | + # Note: SQLAlchmemy or_ did not work here. Query had AND for some reason. |
| 27 | + query = db.select(Aliases).filter( |
| 28 | + (func.json_contains(func.lower(Aliases.data), func.lower(identifier_json), "$.aliases")) |
| 29 | + | (func.json_extract(func.lower(Aliases.data), "$.geneid") == func.lower(identifier)) |
| 30 | + | (func.json_extract(func.lower(Aliases.data), "$.locus") == func.lower(identifier)), |
| 31 | + ) |
| 32 | + row = db.session.execute(query).scalars().first() |
| 33 | + |
| 34 | + if row: |
| 35 | + return BARUtils.success_exit(row.data) |
| 36 | + else: |
| 37 | + return BARUtils.error_exit("Nothing found"), 404 |
| 38 | + |
| 39 | + else: |
| 40 | + return BARUtils.error_exit("Invalid identifier"), 400 |
0 commit comments