|
| 1 | +from flask_restx import Namespace, Resource |
| 2 | +from markupsafe import escape |
| 3 | +from api import db |
| 4 | +from api.models.annotations_lookup import AtAgiLookup |
| 5 | +from api.models.arabidopsis_ecotypes import SampleData as EcotypesSampleData |
| 6 | +from api.utils.bar_utils import BARUtils |
| 7 | +from api.utils.world_efp_utils import WorldeFPUtils |
| 8 | + |
| 9 | + |
| 10 | +microarray_gene_expression = Namespace( |
| 11 | + "Microarray Gene Expression", |
| 12 | + description="Microarray (probe-based) Gene Expression data from the BAR Databases", |
| 13 | + path="/microarray_gene_expression", |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@microarray_gene_expression.route("/world_efp/<string:species>/<string:gene_id>") |
| 18 | +class GetWorldeFPExpression(Resource): |
| 19 | + @microarray_gene_expression.param("species", _in="path", default="arabidopsis") |
| 20 | + @microarray_gene_expression.param("gene_id", _in="path", default="At1g01010") |
| 21 | + def get(self, species="", gene_id=""): |
| 22 | + """This end point returns World Efp gene expression data""" |
| 23 | + species = escape(species) |
| 24 | + gene_id = escape(gene_id) |
| 25 | + |
| 26 | + if species == "arabidopsis": |
| 27 | + if not BARUtils.is_arabidopsis_gene_valid(gene_id): |
| 28 | + return BARUtils.error_exit("Invalid gene id") |
| 29 | + else: |
| 30 | + return BARUtils.error_exit("Invalid species") |
| 31 | + subquery = ( |
| 32 | + db.select(AtAgiLookup.probeset) |
| 33 | + .where(AtAgiLookup.agi == gene_id) |
| 34 | + .order_by(AtAgiLookup.date.desc()) |
| 35 | + .limit(1) |
| 36 | + .subquery() |
| 37 | + ) |
| 38 | + |
| 39 | + sq_query = db.session.query(subquery) |
| 40 | + if sq_query.count() > 0: |
| 41 | + sq_result = sq_query[0][0] |
| 42 | + else: |
| 43 | + return BARUtils.error_exit("There are no data found for the given gene") |
| 44 | + |
| 45 | + rows = db.session.execute( |
| 46 | + db.select( |
| 47 | + EcotypesSampleData.data_probeset_id, EcotypesSampleData.data_signal, EcotypesSampleData.data_bot_id |
| 48 | + ).where(EcotypesSampleData.data_probeset_id == sq_result) |
| 49 | + ).all() |
| 50 | + final_json = {} |
| 51 | + |
| 52 | + if len(rows) > 0: |
| 53 | + for row in rows: |
| 54 | + if row[2][5:8] not in final_json: |
| 55 | + final_json[row[2][5:8]] = WorldeFPUtils.wrap_json(row[2][5:8], row[2], row[1], row[0]) |
| 56 | + elif row[2][5:8] in final_json: |
| 57 | + final_json[row[2][5:8]]["values"].update({row[2]: row[1]}) |
| 58 | + return BARUtils.success_exit(final_json) |
| 59 | + else: |
| 60 | + return BARUtils.error_exit("There are no data found for the given gene") |
0 commit comments