forked from BioAnalyticResource/BAR_API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgene_information.py
229 lines (179 loc) · 8.42 KB
/
gene_information.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from flask_restx import Namespace, Resource, fields
from flask import request
from markupsafe import escape
from api.models.annotations_lookup import AgiAlias
from api.models.eplant2 import Isoforms as EPlant2Isoforms
from api.models.eplant2 import Publications as EPlant2Publications
from api.models.eplant_poplar import Isoforms as EPlantPoplarIsoforms
from api.models.eplant_tomato import Isoforms as EPlantTomatoIsoforms
from api.models.eplant_soybean import Isoforms as EPlantSoybeanIsoforms
from api.utils.bar_utils import BARUtils
from marshmallow import Schema, ValidationError, fields as marshmallow_fields
from api import cache, db
gene_information = Namespace("Gene Information", description="Information about Genes", path="/gene_information")
# I think this is only needed for Swagger UI POST
gene_isoforms_request_fields = gene_information.model(
"GeneIsoforms",
{
"species": fields.String(required=True, example="arabidopsis"),
"genes": fields.List(
required=True,
example=["AT1G01010", "AT1G01020"],
cls_or_instance=fields.String,
),
},
)
# Validation is done in a different way to keep things simple
class GeneIsoformsSchema(Schema):
species = marshmallow_fields.String(required=True)
genes = marshmallow_fields.List(cls_or_instance=marshmallow_fields.String)
@gene_information.route("/gene_alias")
class GeneAliasList(Resource):
def get(self):
"""This end point returns the list of species available"""
species = ["arabidopsis"] # This are the only species available so far
return BARUtils.success_exit(species)
@gene_information.route("/gene_alias/<string:species>/<string:gene_id>")
class GeneAlias(Resource):
@gene_information.param("species", _in="path", default="arabidopsis")
@gene_information.param("gene_id", _in="path", default="At3g24650")
@cache.cached()
def get(self, species="", gene_id=""):
"""This end point provides gene alias given a gene ID."""
aliases = []
# Escape input
species = escape(species)
gene_id = escape(gene_id)
if species == "arabidopsis":
if BARUtils.is_arabidopsis_gene_valid(gene_id):
rows = db.session.execute(db.select(AgiAlias).where(AgiAlias.agi == gene_id)).scalars().all()
[aliases.append(row.alias) for row in rows]
else:
return BARUtils.error_exit("Invalid gene id"), 400
else:
return BARUtils.error_exit("No data for the given species")
# Return results if there are data
if len(aliases) > 0:
return BARUtils.success_exit(aliases)
else:
return BARUtils.error_exit("There are no data found for the given gene")
@gene_information.route("/gene_publications/<string:gene_id>")
class GenePublications(Resource):
# @gene_information.param("species", _in="path", default="arabidopsis")
@gene_information.param("gene_id", _in="path", default="AT1G01010")
def get(self, gene_id=""):
"""This end point provides publications given a gene ID."""
publications = []
# Escape input
gene_id = escape(gene_id)
# truncate
for i in range(len(gene_id)):
if gene_id[i] == ".":
gene_id = gene_id[0:i]
break
if BARUtils.is_arabidopsis_gene_valid(gene_id):
rows = db.session.execute(db.select(EPlant2Publications).where(EPlant2Publications.gene == gene_id)).scalars().all()
for row in rows:
publications.append({"gene_id": row.gene, "author": row.author, "year": row.year, "journal": row.journal, "title": row.title, "pubmed": row.pubmed})
else:
return BARUtils.error_exit("Invalid gene id"), 400
# Return results if there are data
if len(publications) > 0:
return BARUtils.success_exit(publications)
else:
return BARUtils.error_exit("There are no data found for the given gene")
@gene_information.route("/gene_isoforms/<string:species>/<string:gene_id>")
class GeneIsoforms(Resource):
@gene_information.param("species", _in="path", default="arabidopsis")
@gene_information.param("gene_id", _in="path", default="AT1G01020")
def get(self, species="", gene_id=""):
"""This end point provides gene isoforms given a gene ID.
Only genes/isoforms with pdb structures are returned"""
gene_isoforms = []
# Escape input
species = escape(species)
gene_id = escape(gene_id)
# Set the database and check if genes are valid
if species == "arabidopsis":
database = EPlant2Isoforms
if not BARUtils.is_arabidopsis_gene_valid(gene_id):
return BARUtils.error_exit("Invalid gene id"), 400
elif species == "poplar":
database = EPlantPoplarIsoforms
if not BARUtils.is_poplar_gene_valid(gene_id):
return BARUtils.error_exit("Invalid gene id"), 400
# Format the gene first
gene_id = BARUtils.format_poplar(gene_id)
elif species == "tomato":
database = EPlantTomatoIsoforms
if not BARUtils.is_tomato_gene_valid(gene_id, False):
return BARUtils.error_exit("Invalid gene id"), 400
elif species == "soybean":
database = EPlantSoybeanIsoforms
if not BARUtils.is_soybean_gene_valid(gene_id):
return BARUtils.error_exit("Invalid gene id"), 400
else:
return BARUtils.error_exit("No data for the given species")
# Now get the data
rows = db.session.execute(db.select(database).where(database.gene == gene_id)).scalars().all()
[gene_isoforms.append(row.isoform) for row in rows]
# Found isoforms
if len(gene_isoforms) > 0:
return BARUtils.success_exit(gene_isoforms)
else:
return BARUtils.error_exit("There are no data found for the given gene")
@gene_information.route("/gene_isoforms/")
class PostGeneIsoforms(Resource):
@gene_information.expect(gene_isoforms_request_fields)
def post(self):
"""This end point returns gene isoforms data for a multiple genes for a species.
Only genes/isoforms with pdb structures are returned"""
json_data = request.get_json()
data = {}
# Validate json
try:
json_data = GeneIsoformsSchema().load(json_data)
except ValidationError as err:
return BARUtils.error_exit(err.messages), 400
genes = json_data["genes"]
species = json_data["species"]
# Set species and check gene ID format
if species == "arabidopsis":
database = EPlant2Isoforms
# Check if gene is valid
for gene in genes:
if not BARUtils.is_arabidopsis_gene_valid(gene):
return BARUtils.error_exit("Invalid gene id"), 400
elif species == "poplar":
database = EPlantPoplarIsoforms
for gene in genes:
# Check if gene is valid
if not BARUtils.is_poplar_gene_valid(gene):
return BARUtils.error_exit("Invalid gene id"), 400
elif species == "tomato":
database = EPlantTomatoIsoforms
for gene in genes:
# Check if gene is valid
if not BARUtils.is_tomato_gene_valid(gene, False):
return BARUtils.error_exit("Invalid gene id"), 400
elif species == "soybean":
database = EPlantSoybeanIsoforms
for gene in genes:
# Check if gene is valid
if not BARUtils.is_soybean_gene_valid(gene):
return BARUtils.error_exit("Invalid gene id"), 400
else:
return BARUtils.error_exit("Invalid species"), 400
# Query must be run individually for each species
rows = db.session.execute(db.select(database).where(database.gene.in_(genes))).scalars().all()
# If there are any isoforms found, return data
if len(rows) > 0:
for row in rows:
if row.gene in data:
data[row.gene].append(row.isoform)
else:
data[row.gene] = []
data[row.gene].append(row.isoform)
return BARUtils.success_exit(data)
else:
return BARUtils.error_exit("No data for the given species/genes"), 400