|
1 | | -from flask_restx import Namespace, Resource |
| 1 | +from flask import request |
| 2 | +from flask_restx import Namespace, Resource, fields |
2 | 3 | from markupsafe import escape |
3 | 4 | from api import db |
4 | 5 | from api.utils.bar_utils import BARUtils |
5 | | -from api.models.gaia import Genes, Aliases |
| 6 | +from api.models.gaia import Genes, Aliases, PubIds, Figures |
6 | 7 | from sqlalchemy import func, or_ |
| 8 | +from marshmallow import Schema, ValidationError, fields as marshmallow_fields |
7 | 9 | import json |
8 | 10 |
|
9 | 11 | gaia = Namespace("Gaia", description="Gaia", path="/gaia") |
10 | 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 | + |
11 | 39 |
|
12 | 40 | @gaia.route("/aliases/<string:identifier>") |
13 | 41 | class GaiaAliases(Resource): |
@@ -78,3 +106,71 @@ def get(self, identifier=""): |
78 | 106 |
|
79 | 107 | else: |
80 | 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