forked from BioAnalyticResource/BAR_API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgene_information.py
More file actions
566 lines (467 loc) · 20.7 KB
/
gene_information.py
File metadata and controls
566 lines (467 loc) · 20.7 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
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.eplant2 import TAIR10GFF3 as EPlant2TAIR10GFF3
from api.models.eplant2 import AgiAlias as EPlant2AgiAlias
from api.models.eplant2 import AgiAnnotation as EPlant2AgiAnnotation
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 db
from sqlalchemy import func
gene_information = Namespace("Gene Information", description="Information about Genes", path="/gene_information")
parser = gene_information.parser()
parser.add_argument(
"terms",
type=list,
action="append",
required=True,
help="Gene IDs, format example: AT1G01010",
default=["AT1G01020", "AT1G01030"],
)
# I think this is only needed for Swagger UI POST
gene_information_request_fields = gene_information.model(
"GeneInformation",
{
"species": fields.String(required=True, example="arabidopsis"),
"genes": fields.List(
required=True,
example=["AT1G01010", "AT1G01020"],
cls_or_instance=fields.String,
),
},
)
query_genes_request_fields = gene_information.model(
"GeneInformation",
{
"species": fields.String(required=True, example="arabidopsis"),
"terms": fields.List(
required=True,
example=["AT1G01010", "AT1G01020"],
cls_or_instance=fields.String,
),
},
)
# Validation is done in a different way to keep things simple
class GeneInformationSchema(Schema):
species = marshmallow_fields.String(required=True)
genes = marshmallow_fields.List(cls_or_instance=marshmallow_fields.String)
@gene_information.route("/gene_aliases")
class GeneAliases(Resource):
@gene_information.expect(gene_information_request_fields)
def post(self):
"""This end point retrieves gene aliases for a large dataset"""
json_data = request.get_json()
data = {}
# Validate json
try:
json_data = GeneInformationSchema().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 = AgiAlias
# 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
else:
return BARUtils.error_exit("Invalid species"), 400
# Query must be run individually for each species
lowered_genes = [gene.lower() for gene in genes]
rows = (
db.session.execute(db.select(database).where(func.lower(database.agi).in_(lowered_genes))).scalars().all()
)
# If there are any isoforms found, return data
data = []
data_items = {}
if len(rows) > 0:
for row in rows:
if row.agi in data_items.keys():
data_items[row.agi].append(row.agi)
else:
data_items[row.agi] = []
data_items[row.agi].append(row.alias)
for gene in data_items.keys():
data.append({"gene": gene, "aliases": data_items[gene]})
return BARUtils.success_exit(data)
else:
return BARUtils.error_exit("No data for the given species/genes"), 400
@gene_information.route("/gene_publications/<string:species>/<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, species="", gene_id=""):
"""This end point provides publications given a gene ID."""
publications = []
# Escape input
species = escape(species)
gene_id = escape(gene_id)
# Set the database and check if genes are valid
if species == "arabidopsis":
database = EPlant2Publications
# Remove Arabidopsis isoforms
gene_id = gene_id.split(".")[0]
if not BARUtils.is_arabidopsis_gene_valid(gene_id):
return BARUtils.error_exit("Invalid gene id"), 400
else:
return BARUtils.error_exit("No data for the given species")
# Get data
rows = db.session.execute(db.select(database).where(database.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,
}
)
# 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(
"/genes_by_position/<string:species>/<string:chromosome>/<string:start_param>/<string:end_param>"
)
class GeneTair10Gff3(Resource):
@gene_information.param("species", _in="path", default="arabidopsis")
@gene_information.param("chromosome", _in="path", default="1")
@gene_information.param("start_param", _in="path", default=3000)
@gene_information.param("end_param", _in="path", default=6000)
def get(self, species="", chromosome="", start_param="", end_param=""):
"""This end point provides genes given position."""
# Check if all parameters are provided
if not chromosome or not start_param or not end_param:
return BARUtils.error_exit("Missing parameters"), 400
# Check if the start param is smaller than end param
if start_param >= end_param:
return BARUtils.error_exit("Start location should be smaller than the end location"), 400
# Check if both parameters are valid figures
if not BARUtils.is_integer(start_param) or not BARUtils.is_integer(end_param):
return BARUtils.error_exit("At lease one parameter is not valid")
# Escape input
species = escape(species)
chromosome = escape(chromosome)
start_param = escape(start_param)
end_param = escape(end_param)
# Set database
if species == "arabidopsis":
gff3_database = EPlant2TAIR10GFF3
alias_database = EPlant2AgiAlias
annotation_database = EPlant2AgiAnnotation
if chromosome not in ["1", "2", "3", "4", "5", "C", "M"]:
return BARUtils.error_exit("Invalid chromosome"), 400
# Arabidopsis Gene format
gene_id = "AT" + str(chromosome) + "G"
else:
return BARUtils.error_exit("No data for the given species"), 400
# Construct the query
query1 = db.select(gff3_database.geneId, gff3_database.Start, gff3_database.End, gff3_database.Strand).where(
gff3_database.Type == "gene",
gff3_database.geneId.startswith(gene_id),
(
gff3_database.Start.between(start_param, end_param)
| gff3_database.End.between(start_param, end_param)
| ((gff3_database.Start < start_param) & (gff3_database.End > end_param))
),
)
result1 = db.session.execute(query1).all()
gene_ids = [row[0] for row in result1]
# Get aliases
lowered_gene_ids = [gene_id.lower() for gene_id in gene_ids]
query2 = db.select(alias_database.agi, alias_database.alias).where(
func.lower(alias_database.agi).in_(lowered_gene_ids)
)
result2 = db.session.execute(query2).all()
all_aliases = {}
for row in result2:
if row[0] not in all_aliases:
all_aliases[row[0]] = []
all_aliases[row[0]].append(row[1])
# Get annotation
query3 = db.select(annotation_database.agi, annotation_database.annotation).where(
func.lower(annotation_database.agi).in_(lowered_gene_ids)
)
result3 = db.session.execute(query3).all()
all_annotations = {}
for row in result3:
temp = row[1].split("__")
if len(temp) > 1:
all_annotations[row[0].upper()] = temp[1]
else:
all_annotations[row[0].upper()] = temp[0]
genes = []
for row in result1:
gene = {
"id": row[0],
"start": row[1],
"end": row[2],
"strand": row[3],
"aliases": all_aliases.get(row[0], []),
"annotation": all_annotations.get(row[0].upper(), None),
}
genes.append(gene)
return BARUtils.success_exit(genes)
@gene_information.route("/gene_query")
class GeneQueryGene(Resource):
@gene_information.expect(query_genes_request_fields)
def post(self):
"""This end point provides gene information for multiple genes given multiple terms."""
# Escape input
data = request.get_json()
species = data["species"]
terms = [term.upper() for term in data["terms"]]
# Species check
if species == "arabidopsis":
# Term check
for one_term in terms:
if not BARUtils.is_arabidopsis_gene_valid(one_term):
return BARUtils.error_exit("Input list contains invalid term"), 400
alias_database = EPlant2AgiAlias
gff3_database = EPlant2TAIR10GFF3
annotation_database = EPlant2AgiAnnotation
else:
return BARUtils.error_exit("No data for the given species"), 400
gene_ids = []
gene_fail = []
for one_term in terms:
query = (
db.select(alias_database.agi).where(func.lower(alias_database.agi).contains(one_term.lower())).limit(1)
)
result = db.session.execute(query).fetchone()
if result is not None:
gene_ids.append(result[0])
else:
gene_fail.append(one_term)
# For terms that do not have results
for fail_term in gene_fail:
query = (
db.select(gff3_database.geneId)
.where(
((gff3_database.Type == "gene") | (gff3_database.Type == "transposable_element_gene")),
func.lower(gff3_database.geneId).contains(fail_term.lower()),
)
.limit(1)
)
result = db.session.execute(query).fetchone()
if result:
gene_ids.append(result[0])
# Find information for each term
query = db.select(gff3_database.geneId, gff3_database.Start, gff3_database.End, gff3_database.Strand).where(
((gff3_database.Type == "gene") | (gff3_database.Type == "transposable_element_gene")),
gff3_database.Source == "TAIR10",
gff3_database.geneId.in_(gene_ids),
)
result = db.session.execute(query).all()
genes_info = {}
for row in result:
if row[0] not in genes_info:
gene = {
"id": row[0],
"chromosome": "Chr" + row[0][2:3],
"start": row[1],
"end": row[2],
"strand": row[3],
"aliases": [],
"annotation": None,
}
genes_info[row[0]] = gene
# Get aliases
lowered_gene_ids = [gene_id.lower() for gene_id in gene_ids]
query = db.select(alias_database.agi, alias_database.alias).where(
func.lower(alias_database.agi).in_(lowered_gene_ids)
)
result = db.session.execute(query).all()
for row in result:
if row[0] in genes_info:
genes_info[row[0]]["aliases"].append(row[1])
# Get annotations
query = db.select(annotation_database.agi, annotation_database.annotation).where(
func.lower(annotation_database.agi).in_(lowered_gene_ids)
)
result = db.session.execute(query)
for row in result:
if row[0].upper() in genes_info:
temp = row[1].split("__")
if len(temp) > 1:
genes_info[row[0].upper()]["annotation"] = temp[1]
else:
genes_info[row[0].upper()]["annotation"] = temp[0]
return BARUtils.success_exit(genes_info)
@gene_information.route("/single_gene_query/<string:species>/<string:term>")
class SingleGeneQueryGene(Resource):
@gene_information.param("species", _in="path", default="arabidopsis")
@gene_information.param("term", _in="path", default="AT1G01010")
def get(self, species="", term=""):
"""This end point provides gene information for a single gene given one term."""
# Escape input
species = escape(species)
term = escape(term).upper()
# Species check
if species == "arabidopsis":
alias_database = EPlant2AgiAlias
gff3_database = EPlant2TAIR10GFF3
annotation_database = EPlant2AgiAnnotation
# Term check
if not BARUtils.is_arabidopsis_gene_valid(term):
return BARUtils.error_exit("Input term invalid"), 400
else:
return BARUtils.error_exit("No data for the given species"), 400
query = db.select(alias_database.agi).where(func.lower(alias_database.agi) == term.lower()).limit(1)
result = db.session.execute(query).fetchone()
if not result:
query = (
db.select(gff3_database.geneId)
.where(
((gff3_database.Type == "gene") | (gff3_database.Type == "transposable_element_gene")),
func.lower(gff3_database.geneId) == term.lower(),
)
.limit(1)
)
result = db.session.execute(query).fetchone()
genes_info = {}
if result:
# Find information for the term
query = db.select(gff3_database.geneId, gff3_database.Start, gff3_database.End, gff3_database.Strand).where(
((gff3_database.Type == "gene") | (gff3_database.Type == "transposable_element_gene")),
gff3_database.Source == "TAIR10",
func.lower(gff3_database.geneId) == term.lower(),
)
result = db.session.execute(query).fetchone()
# This Arabidopsis specific.
gene = {
"id": result[0],
"chromosome": "Chr" + result[0][2:3],
"start": result[1],
"end": result[2],
"strand": result[3],
"aliases": [],
"annotation": None,
}
genes_info[result[0]] = gene
# Get aliases
query = db.select(alias_database.agi, alias_database.alias).where(
func.lower(alias_database.agi) == term.lower()
)
result = db.session.execute(query).all()
for row in result:
if row[1] not in gene["aliases"]:
gene["aliases"].append(row[1])
# Get annotations
query = db.select(annotation_database.agi, annotation_database.annotation).where(
func.lower(annotation_database.agi) == term.lower()
)
result = db.session.execute(query).all()
for row in result:
temp = row[1].split("__")
if len(temp) > 1:
gene["annotation"] = temp[1]
else:
gene["annotation"] = temp[0]
return BARUtils.success_exit(genes_info)
@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_information_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 = GeneInformationSchema().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