-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgene_information.py
More file actions
206 lines (163 loc) · 7.25 KB
/
gene_information.py
File metadata and controls
206 lines (163 loc) · 7.25 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
from flask_restx import Namespace, Resource, fields
from flask import request
from markupsafe import escape
from api.utils.bar_utils import BARUtils
from marshmallow import Schema, ValidationError, fields as marshmallow_fields
from api import cache, db
from sqlalchemy import text
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):
with db.engines["annotations_lookup"].connect() as conn:
rows = conn.execute(
text("select alias from agi_alias where agi=:agi"),
{"agi": gene_id},
)
[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_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 = db.engines["eplant2"]
if not BARUtils.is_arabidopsis_gene_valid(gene_id):
return BARUtils.error_exit("Invalid gene id"), 400
elif species == "poplar":
database = db.engines["eplant_poplar"]
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 = db.engines["eplant_tomato"]
if not BARUtils.is_tomato_gene_valid(gene_id, False):
return BARUtils.error_exit("Invalid gene id"), 400
elif species == "soybean":
database = db.engines["eplant_soybean"]
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
with database.connect() as conn:
rows = conn.execute(
text("select isoform from isoforms where gene=:gene"), {"gene": gene_id}
)
[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 = db.engines["eplant2"]
# 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 = db.engines["eplant_poplar"]
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 = db.engines["eplant_tomato"]
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 = db.engines["eplant_soybean"]
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
with database.connect() as conn:
results = conn.execute(
text("select gene, isoform from isoforms where gene in :genes"),
{"genes": genes},
)
rows = results.fetchall()
# 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