|
| 1 | +from flask import request |
| 2 | +from flask_restx import Namespace, Resource, fields |
| 3 | +from markupsafe import escape |
| 4 | +from api import db |
| 5 | +from api.utils.bar_utils import BARUtils |
| 6 | +from api.models.gaia import Genes, Aliases, PubIds, Figures |
| 7 | +from sqlalchemy import func, or_ |
| 8 | +from marshmallow import Schema, ValidationError, fields as marshmallow_fields |
| 9 | +import json |
| 10 | + |
| 11 | +gaia = Namespace("Gaia", description="Gaia", path="/gaia") |
| 12 | + |
| 13 | +parser = gaia.parser() |
| 14 | +parser.add_argument( |
| 15 | + "terms", |
| 16 | + type=list, |
| 17 | + action="append", |
| 18 | + required=True, |
| 19 | + help="Publication IDs", |
| 20 | + default=["32492426", "32550561"], |
| 21 | +) |
| 22 | + |
| 23 | +publication_request_fields = gaia.model( |
| 24 | + "Publications", |
| 25 | + { |
| 26 | + "pubmeds": fields.List( |
| 27 | + required=True, |
| 28 | + example=["32492426", "32550561"], |
| 29 | + cls_or_instance=fields.String, |
| 30 | + ), |
| 31 | + }, |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +# Validation is done in a different way to keep things simple |
| 36 | +class PublicationSchema(Schema): |
| 37 | + pubmeds = marshmallow_fields.List(cls_or_instance=marshmallow_fields.String) |
| 38 | + |
| 39 | + |
| 40 | +@gaia.route("/aliases/<string:identifier>") |
| 41 | +class GaiaAliases(Resource): |
| 42 | + @gaia.param("identifier", _in="path", default="ABI3") |
| 43 | + def get(self, identifier=""): |
| 44 | + |
| 45 | + # Escape input |
| 46 | + identifier = escape(identifier) |
| 47 | + |
| 48 | + # Is it valid |
| 49 | + if BARUtils.is_gaia_alias(identifier): |
| 50 | + query_ids = [] |
| 51 | + data = [] |
| 52 | + |
| 53 | + # Check if alias exists |
| 54 | + # Note: This check can be done in on query, but optimizer is not using indexes for some reason |
| 55 | + query = db.select(Aliases.genes_id, Aliases.alias).filter(Aliases.alias == identifier) |
| 56 | + rows = db.session.execute(query).fetchall() |
| 57 | + |
| 58 | + if rows and len(rows) > 0: |
| 59 | + # Alias exists. Get the genes_ids |
| 60 | + for row in rows: |
| 61 | + query_ids.append(row.genes_id) |
| 62 | + |
| 63 | + else: |
| 64 | + # Alias doesn't exist. Get the ids if it's locus or ncbi id |
| 65 | + query = db.select(Genes.id).filter(or_(Genes.locus == identifier, Genes.geneid == identifier)) |
| 66 | + rows = db.session.execute(query).fetchall() |
| 67 | + |
| 68 | + if rows and len(rows) > 0: |
| 69 | + for row in rows: |
| 70 | + query_ids.append(row.id) |
| 71 | + else: |
| 72 | + return BARUtils.error_exit("Nothing found"), 404 |
| 73 | + |
| 74 | + # Left join is important in case aliases do not exist for the given locus / geneid |
| 75 | + query = ( |
| 76 | + db.select(Genes.species, Genes.locus, Genes.geneid, func.json_arrayagg(Aliases.alias).label("aliases")) |
| 77 | + .select_from(Genes) |
| 78 | + .outerjoin(Aliases, Aliases.genes_id == Genes.id) |
| 79 | + .filter(Genes.id.in_(query_ids)) |
| 80 | + .group_by(Genes.species, Genes.locus, Genes.geneid) |
| 81 | + ) |
| 82 | + |
| 83 | + rows = db.session.execute(query).fetchall() |
| 84 | + |
| 85 | + if rows and len(rows) > 0: |
| 86 | + for row in rows: |
| 87 | + |
| 88 | + # JSONify aliases |
| 89 | + if row.aliases: |
| 90 | + aliases = json.loads(row.aliases) |
| 91 | + else: |
| 92 | + aliases = [] |
| 93 | + |
| 94 | + record = { |
| 95 | + "species": row.species, |
| 96 | + "locus": row.locus, |
| 97 | + "geneid": row.geneid, |
| 98 | + "aliases": aliases, |
| 99 | + } |
| 100 | + |
| 101 | + # Add the record to data |
| 102 | + data.append(record) |
| 103 | + |
| 104 | + # Return final data |
| 105 | + return BARUtils.success_exit(data) |
| 106 | + |
| 107 | + else: |
| 108 | + return BARUtils.error_exit("Invalid identifier"), 400 |
| 109 | + |
| 110 | + |
| 111 | +@gaia.route("/publication_figures") |
| 112 | +class GaiaPublicationFigures(Resource): |
| 113 | + @gaia.expect(publication_request_fields) |
| 114 | + def post(self): |
| 115 | + json_data = request.get_json() |
| 116 | + |
| 117 | + # Validate json |
| 118 | + try: |
| 119 | + json_data = PublicationSchema().load(json_data) |
| 120 | + except ValidationError as err: |
| 121 | + return BARUtils.error_exit(err.messages), 400 |
| 122 | + |
| 123 | + pubmeds = json_data["pubmeds"] |
| 124 | + |
| 125 | + # Check if pubmed ids are valid |
| 126 | + for pubmed in pubmeds: |
| 127 | + if not BARUtils.is_integer(pubmed): |
| 128 | + return BARUtils.error_exit("Invalid Pubmed ID"), 400 |
| 129 | + |
| 130 | + # It is valid. Continue |
| 131 | + data = [] |
| 132 | + |
| 133 | + # Left join is important in case aliases do not exist for the given locus / geneid |
| 134 | + query = ( |
| 135 | + db.select(Figures.img_name, Figures.caption, Figures.img_url, PubIds.pubmed, PubIds.pmc) |
| 136 | + .select_from(Figures) |
| 137 | + .join(PubIds, PubIds.publication_figures_id == Figures.publication_figures_id) |
| 138 | + .filter(PubIds.pubmed.in_(pubmeds)) |
| 139 | + .order_by(PubIds.pubmed.desc()) |
| 140 | + ) |
| 141 | + |
| 142 | + rows = db.session.execute(query).fetchall() |
| 143 | + |
| 144 | + record = {} |
| 145 | + |
| 146 | + if rows and len(rows) > 0: |
| 147 | + for row in rows: |
| 148 | + |
| 149 | + # Check if record has an id. If it doesn't, this is first row. |
| 150 | + if "id" in record: |
| 151 | + # Check if this is a new pubmed id |
| 152 | + if record["id"]["pubmed"] != row.pubmed: |
| 153 | + # new record. Add old now to data and create a new record |
| 154 | + data.append(record) |
| 155 | + record = {} |
| 156 | + |
| 157 | + # Check if figures exists, if not add it. |
| 158 | + if record.get("figures") is None: |
| 159 | + # Create a new figures record |
| 160 | + record["figures"] = [] |
| 161 | + |
| 162 | + # Now append figure to the record |
| 163 | + figure = {"img_name": row.img_name, "caption": row.caption, "img_url": row.img_url} |
| 164 | + record["figures"].append(figure) |
| 165 | + |
| 166 | + # Now add the id. If it exists don't add |
| 167 | + if record.get("id") is None: |
| 168 | + record["id"] = {} |
| 169 | + record["id"]["pubmed"] = row.pubmed |
| 170 | + record["id"]["pmc"] = row.pmc |
| 171 | + |
| 172 | + # The last record |
| 173 | + data.append(record) |
| 174 | + |
| 175 | + # Return final data |
| 176 | + return BARUtils.success_exit(data) |
0 commit comments