-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathget_gene_structures.cgi
More file actions
executable file
·262 lines (238 loc) · 7.83 KB
/
get_gene_structures.cgi
File metadata and controls
executable file
·262 lines (238 loc) · 7.83 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
#!/usr/bin/python3
import base64
import cgi
import json
import urllib.request
from PIL import Image, ImageDraw
print("Access-Control-Allow-Origin: *")
print("Content-Type: application/json\n") # HTML is following
# ----- CONSTANTS -----
EXON_IMG_WIDTH = 450
EXON_IMG_HEIGHT = 8
# ----- VARIABLES -----
exon_and_CDS = {"start": [], "end": []}
mRNA = {"start": [], "end": []}
expression_score = []
variants = []
variant_count = 0
# ----- GET THE LOCUS OF INTEREST -----
gene_id = cgi.FieldStorage().getvalue("locus")
# ----- GETS MAPPING INFO FOR THE GENE ID -----
map_info = json.loads(
urllib.request.urlopen(
"https://bar.utoronto.ca/webservices/bar_araport/gene_structure_by_locus.php?locus="
+ gene_id
).read()
)
printout = ""
printout = printout + "{"
printout = printout + '"locus" : "' + gene_id + '", '
printout = (
printout + '"locus_start" : "' + str(map_info["features"][0]["start"]) + '", '
)
printout = printout + '"locus_end" : "' + str(map_info["features"][0]["end"]) + '", '
printout = printout + '"splice_variants" : ['
# Get start/end of the LOCUS
start = int(map_info["features"][0]["start"])
end = int(map_info["features"][0]["end"])
strand = int(map_info["features"][0]["strand"])
i = 0
for subfeature in map_info["features"][0]["subfeatures"]:
# Need a comma if it is not the first element
if i == 0:
printout = printout + "{"
else:
printout = printout + ", {"
# To return a count of the number of variants returned by Araport
variant_count = variant_count + 1
# Extract variant
variants.append(subfeature["subfeatures"])
# Return all exons' coordinates
printout = printout + '"exon_coordinates" : ['
# Define the colours
white = (255, 255, 255)
black = (0, 0, 0)
red = (220, 20, 60)
orange = (255, 140, 0)
blue = (0, 0, 255)
# TO DO: Fix the green and dark green shades...
green = (166, 220, 166)
darkgreen = (0, 125, 0)
# Generate gene structure image based on the information in map_info.
# Create an image for gene structure
exon_graph_image = Image.new("RGB", (EXON_IMG_WIDTH, EXON_IMG_HEIGHT), white)
exongraph = ImageDraw.Draw(exon_graph_image)
count = 0 # Need a comma if it is not the first element...
for region in variants[i]:
# We want to return regions marked as type = exons and type = CDS
if region["type"] == "exon" or region["type"] == "CDS":
exon_and_CDS["start"].append(int(region["start"]))
exon_and_CDS["end"].append(int(region["end"]))
if count == 0:
printout = (
printout
+ "{"
+ '"exon_start" : '
+ str(int(region["start"]))
+ ', "exon_end" : '
+ str(int(region["end"]))
+ "}"
)
else:
printout = (
printout
+ ", {"
+ '"exon_start" : '
+ str(int(region["start"]))
+ ', "exon_end" : '
+ str(int(region["end"]))
+ "}"
)
count = count + 1 # To add a comma only...
# We want to graph all types of features in the gene structure image
if region["type"] == "exon":
exongraph.rectangle(
(
(
int(
float(region["start"] - start)
/ (end - start)
* EXON_IMG_WIDTH
),
0,
),
(
int(
float(region["end"] - start)
/ (end - start)
* EXON_IMG_WIDTH
),
EXON_IMG_HEIGHT,
),
),
darkgreen,
)
elif region["type"] == "CDS":
exongraph.rectangle(
(
(
int(
float(region["start"] - start)
/ (end - start)
* EXON_IMG_WIDTH
),
0,
),
(
int(
float(region["end"] - start)
/ (end - start)
* EXON_IMG_WIDTH
),
EXON_IMG_HEIGHT,
),
),
darkgreen,
)
elif region["type"] == "five_prime_UTR":
exongraph.rectangle(
(
(
int(
float(region["start"] - start)
/ (end - start)
* EXON_IMG_WIDTH
),
0,
),
(
int(
float(region["end"] - start)
/ (end - start)
* EXON_IMG_WIDTH
),
EXON_IMG_HEIGHT,
),
),
green,
)
elif region["type"] == "three_prime_UTR":
exongraph.rectangle(
(
(
int(
float(region["start"] - start)
/ (end - start)
* EXON_IMG_WIDTH
),
0,
),
(
int(
float(region["end"] - start)
/ (end - start)
* EXON_IMG_WIDTH
),
EXON_IMG_HEIGHT,
),
),
green,
)
# Nucleotide padding
nucleotidePadding = 100
exongraph.rectangle(
((0, 0), ((EXON_IMG_WIDTH / nucleotidePadding), EXON_IMG_HEIGHT)), white
)
exongraph.rectangle(
(
((EXON_IMG_WIDTH - (EXON_IMG_WIDTH / nucleotidePadding)), 0),
(EXON_IMG_WIDTH, EXON_IMG_HEIGHT),
),
white,
)
# Line in the middle
exongraph.rectangle(
((0, EXON_IMG_HEIGHT / 2), (EXON_IMG_WIDTH, EXON_IMG_HEIGHT / 2)), black
)
# Insert appropriate arrows to indicate direction of the gene
if strand == -1:
exongraph.polygon(
(
(0, EXON_IMG_HEIGHT / 2),
(3, EXON_IMG_HEIGHT - (EXON_IMG_HEIGHT - 1)),
(3, EXON_IMG_HEIGHT - 1),
),
black,
)
elif strand == +1:
exongraph.polygon(
(
(EXON_IMG_WIDTH, EXON_IMG_HEIGHT / 2),
(EXON_IMG_WIDTH - 3, EXON_IMG_HEIGHT - (EXON_IMG_HEIGHT - 1)),
(EXON_IMG_WIDTH - 3, EXON_IMG_HEIGHT - 1),
),
black,
)
f = open("get_exon_base64_exongraph.png", "wb")
exon_graph_image.save(f)
f.close()
printout = (
printout
+ "], "
+ '"start" : '
+ str(start)
+ ", "
+ '"end" : '
+ str(end)
+ ", "
+ '"gene_structure" : '
)
printout = printout + '"'
with open("get_exon_base64_exongraph.png", "rb") as fl:
printout = printout + base64.b64encode(fl.read()).decode("utf-8")
printout = printout + '"'
fl.close()
i = i + 1
printout = printout + "}"
printout = printout + "]" + ', "variant_count" : "' + str(variant_count) + '"' + "}"
print(printout.replace("\n", " "))