Skip to content

Commit 78bf046

Browse files
authored
Merge pull request #284 from asherpasha/dev
GAIA: initial commit
2 parents f837254 + d9ea318 commit 78bf046

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def create_app():
5252

5353
# Now add routes
5454
from api.resources.gene_information import gene_information
55+
from api.resources.gaia import gaia
5556
from api.resources.rnaseq_gene_expression import rnaseq_gene_expression
5657
from api.resources.microarray_gene_expression import microarray_gene_expression
5758
from api.resources.proxy import bar_proxy
@@ -66,6 +67,7 @@ def create_app():
6667
from api.resources.llama3 import llama3
6768

6869
bar_api.add_namespace(gene_information)
70+
bar_api.add_namespace(gaia)
6971
bar_api.add_namespace(rnaseq_gene_expression)
7072
bar_api.add_namespace(microarray_gene_expression)
7173
bar_api.add_namespace(bar_proxy)

api/models/gaia.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from api import db
2+
from sqlalchemy.dialects.mysql import json
3+
4+
5+
class Aliases(db.Model):
6+
__bind_key__ = "gaia"
7+
__tablename__ = "aliases"
8+
9+
id: db.Mapped[int] = db.mapped_column(db.Integer, nullable=False, primary_key=True)
10+
data: db.Mapped[json] = db.mapped_column(db.JSON, nullable=True, primary_key=False)

api/resources/gaia.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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

api/utils/bar_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,17 @@ def is_integer(data):
252252
else:
253253
return False
254254

255+
@staticmethod
256+
def is_alphanumeric(data):
257+
"""Check if the input is alphanumeric.
258+
:param data
259+
:return: True if alphanumeric
260+
"""
261+
if re.search(r"^[a-z0-9]{1,50}$", data, re.I):
262+
return True
263+
else:
264+
return False
265+
255266
@staticmethod
256267
def format_poplar(poplar_gene):
257268
"""Format Poplar gene ID to be Potri.016G107900, i.e. capitalized P and G

0 commit comments

Comments
 (0)