Skip to content

Commit fd97091

Browse files
committed
Fixed issues
Fixed column hard coding issues and switched to using temporary files when things were written to the harddisk
1 parent a4f08e6 commit fd97091

6 files changed

Lines changed: 52 additions & 59 deletions

File tree

app.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from most_used_by_type_bar import most_used_by_type_bar
1212
from toggle_bars import toggle_bars
1313

14-
import os
14+
import tempfile, os, shutil
1515

1616
app = Flask(__name__)
1717

@@ -59,9 +59,6 @@ def Sankey_Run():
5959
#instance_ur = 'https://synbiohub.org/'
6060
#url = 'https://synbiohub.org/public/igem/BBa_B0012/1'
6161
#top_level_url = 'https://dev.synbiohub.org/public/igem/BBa_B0012/1'
62-
63-
#get current working directory
64-
cwd = os.getcwd()
6562

6663
#retrieve information about the poi
6764
self_df, display_id, title, role, count = input_data(top_level_url, instance_url)
@@ -74,18 +71,22 @@ def Sankey_Run():
7471
df_sankey = sankey(url, top_level_url, title, instance_url)
7572

7673
sankey_title = "Parts Co-Located with "+ title + " (a "+role_link+")"
77-
filename= os.path.join(cwd, f'sankey_{display_id}_.html')
74+
75+
#create a temporary directory
76+
temp_dir = tempfile.TemporaryDirectory()
77+
78+
#name file
79+
filename = os.path.join(temp_dir.name, "Sankey.html")
7880

81+
7982
#create the sankey diagram
8083
sankey_graph(filename, df_sankey, 'Node, Label',
8184
'Link', 'Color', 'Source','Target', 'Value',
8285
'Link Color', sankey_title, url_not_name=False)
86+
8387

8488
#obtain the html from the sankey diagram
8589
result = retrieve_html(filename)
86-
87-
#delete the copy of the sankey diagram on the server
88-
os.remove(filename)
8990

9091
return result
9192
except Exception as e:
@@ -133,9 +134,6 @@ def Bar_Run():
133134
url = complete_sbol.replace('/sbol','')
134135

135136
try:
136-
137-
#current working directory
138-
cwd = os.getcwd()
139137

140138
#create input data
141139
self_df, display_id, title, role, count = input_data(top_level_url, instance_url)
@@ -147,39 +145,36 @@ def Bar_Run():
147145
#graph title for most used barchart
148146
graph_title = f'Top Ten Parts by Number of Uses Compared to <a href="{url}" target="_blank">{title}</a>'
149147

150-
#where to save the file
151-
filename1= os.path.join(cwd, f'bar1_{display_id}.html')
148+
#create a temporary directory
149+
temp_dir = tempfile.TemporaryDirectory()
150+
151+
#name file
152+
filename1 = os.path.join(temp_dir.name, "Most_Used.html")
152153

153154
#create the most used barchart
154155
bar_plot('title','count','color',bar_df, graph_title, filename1, 'deff')
155156

156157
#retrieve html
157158
most_used = retrieve_html(filename1)
158159

159-
#remove file
160-
os.remove(filename1)
161-
162160
#find poi role ontology link
163161
role_link = find_role_name(role, plural = False)
164162

165163
bar_df = most_used_by_type_bar(top_level_url,instance_url, display_id, title,
166164
role, count)
167-
165+
168166
#graph title for most used barchart
169167
graph_title = f'Top Ten {role_link} by Number of Uses Compared to <a href="{url}" target="_blank">{title}</a>'
170168

171-
#where to save the file
172-
filename2= os.path.join(cwd, f'bar2_{display_id}.html')
173-
169+
#name file
170+
filename2 = os.path.join(temp_dir.name, "Most_Used_Type.html")
171+
174172
#create the most used barchart
175173
bar_plot('title','count','color',bar_df, graph_title, filename2, 'deff')
176174

177175
#retrieve html
178176
by_role = retrieve_html(filename2)
179177

180-
#remove file
181-
os.remove(filename2)
182-
183178
#create bar toggle html
184179
toggle_display = toggle_bars(most_used,by_role)
185180

input_data.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import requests
3-
from pandas.io.json import json_normalize
3+
from pandas import json_normalize
44

55
def input_data(uri, instance):
66
"""
@@ -71,11 +71,10 @@ def input_data(uri, instance):
7171
#format responses
7272
d = json.loads(r.text)
7373
a = json_normalize(d['results']['bindings'])
74-
75-
#renames columns from ['count.datatype', 'count.type', 'count.value', 'def.type', 'def.value',
76-
# 'displayId.type', 'displayId.value', 'role.type', 'role.value',
77-
# 'title.type', 'title.value']
78-
a.columns = ['cd', 'ct','count', 'dt', 'deff', 'dist', 'displayId','rt', 'roletog', 'tt', 'title']
74+
75+
#renames columns
76+
rename_dict = {'count.datatype':'cd', 'count.type':'ct', 'count.value':'count', 'def.type':'dt', 'def.value':'deff', 'displayId.type':'dist', 'displayId.value':'displayId', 'role.type':'rt', 'role.value':'roletog', 'title.type':'tt', 'title.value':'title'}
77+
a.columns = [rename_dict[col] for col in a.columns]
7978

8079
#split column roletog at SO: to leave the http://identifiers.org/so in the column http
8180
#and the roler number (e.g. 0000141) in the column role

most_used_bar.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pandas as pd
22
import requests
33
import json
4-
from pandas.io.json import json_normalize
4+
from pandas import json_normalize
55
from uri_to_url import uri_to_url
66

77
def most_used_bar(uri, instance, display_id, title, role, count):
@@ -14,7 +14,7 @@ def most_used_bar(uri, instance, display_id, title, role, count):
1414
import pandas as pd
1515
import requests
1616
import json
17-
from pandas.io.json import json_normalize
17+
from pandas import json_normalize
1818
from uri_to_url import uri_to_url
1919
Most_Used_Query.txt
2020
@@ -84,11 +84,10 @@ def most_used_bar(uri, instance, display_id, title, role, count):
8484
d = json.loads(r.text)
8585
bar_df = json_normalize(d['results']['bindings'])
8686

87-
#rename columns from ['count.datatype', 'count.type', 'count.value',
88-
# 'def.type', 'def.value', 'displayId.type', 'displayId.value',
89-
# 'role.type', 'role.value', 'title.type', 'title.value']
90-
bar_df.columns = ['cd', 'ct','count', 'dt', 'deff', 'dist', 'displayId',
91-
'rt', 'roletog', 'tt', 'title']
87+
88+
#rename columns
89+
rename_dict = {'count.datatype':'cd', 'count.type':'ct', 'count.value':'count', 'def.type':'dt', 'def.value':'deff', 'displayId.type':'dist', 'displayId.value':'displayId', 'role.type':'rt', 'role.value':'roletog', 'title.type':'tt', 'title.value':'title'}
90+
bar_df.columns = [rename_dict[col] for col in bar_df.columns]
9291

9392
#drop unneeded columns
9493
bar_df = bar_df.drop(['cd', 'ct', 'dt', 'dist', 'rt', 'tt'], axis=1)
@@ -109,9 +108,10 @@ def most_used_bar(uri, instance, display_id, title, role, count):
109108
bar_df['deff'] = uri_to_url(bar_df['deff'], instance, spoofed_instance)
110109

111110
#change the final row in the dataframe (usually row 11)
112-
#to contain the information about the poi
113-
bar_df.iloc[robustness] = [count,part_url,display_id,
114-
"http://identifiers.org/so/SO:"+str(role),title]
111+
#poi row is added like this so the ordering of the columns doesn't have to match
112+
poi_row = pd.DataFrame.from_dict({'displayId':[display_id], 'title':[title], 'count':[count],
113+
'roletog':[f"http://identifiers.org/so/SO:{str(role)}"], 'deff':[part_url]})
114+
bar_df.iloc[robustness] = poi_row.iloc[0]
115115

116116
#define what colour each role should get (other is ignored)
117117
colormap = {

most_used_by_type_bar.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pandas as pd
22
import requests
33
import json
4-
from pandas.io.json import json_normalize
4+
from pandas import json_normalize
55
from uri_to_url import uri_to_url
66

77
def most_used_by_type_bar(uri, instance, display_id, title, role, count):
@@ -15,7 +15,7 @@ def most_used_by_type_bar(uri, instance, display_id, title, role, count):
1515
import pandas as pd
1616
import requests
1717
import json
18-
from pandas.io.json import json_normalize
18+
from pandas import json_normalize
1919
Most_Used_By_Type_Query.txt
2020
2121
Parameters
@@ -87,12 +87,11 @@ def most_used_by_type_bar(uri, instance, display_id, title, role, count):
8787
d = json.loads(r.text)
8888
bar_df = json_normalize(d['results']['bindings'])
8989

90-
#rename the columns from ['count.datatype', 'count.type', 'count.value',
91-
# 'def.type', 'def.value', 'displayId.type', 'displayId.value',
92-
# 'role.type', 'role.value', 'title.type', 'title.value']
93-
bar_df.columns = ['cd', 'ct','count', 'dt', 'deff', 'dist', 'displayId',
94-
'rt', 'roletog', 'tt', 'title']
9590

91+
#rename columns
92+
rename_dict = {'count.datatype':'cd', 'count.type':'ct', 'count.value':'count', 'def.type':'dt', 'def.value':'deff', 'displayId.type':'dist', 'displayId.value':'displayId', 'role.type':'rt', 'role.value':'roletog', 'title.type':'tt', 'title.value':'title'}
93+
bar_df.columns = [rename_dict[col] for col in bar_df.columns]
94+
9695
#drop unneeded columns
9796
bar_df = bar_df.drop(['cd', 'ct', 'dt', 'dist', 'rt', 'tt'], axis=1)
9897

@@ -110,11 +109,13 @@ def most_used_by_type_bar(uri, instance, display_id, title, role, count):
110109

111110
#replace uris with urls
112111
bar_df['deff'] = uri_to_url(bar_df['deff'], instance, spoofed_instance)
113-
112+
114113
#change the final row in the dataframe (usually row 11)
115-
#to contain the information about the poi
116-
bar_df.iloc[robustness] = [count,part_url,display_id,
117-
"http://identifiers.org/so/SO:"+str(role),title]
114+
#poi row is added like this so the ordering of the columns doesn't have to match
115+
poi_row = pd.DataFrame.from_dict({'displayId':[display_id], 'title':[title], 'count':[count],
116+
'roletog':[f"http://identifiers.org/so/SO:{str(role)}"], 'deff':[part_url]})
117+
bar_df.iloc[robustness] = poi_row.iloc[0]
118+
118119

119120
#define what colour each role should get (other is ignored)
120121
colormap = {

sankey.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import requests
22
import json
33
import pandas as pd
4-
from pandas.io.json import json_normalize
4+
from pandas import json_normalize
55
from uri_to_url import uri_to_url
66

77
def sankey(url, uri, title, instance):
@@ -64,13 +64,10 @@ def sankey(url, uri, title, instance):
6464
d = json.loads(r.text)
6565
order_df = json_normalize(d['results']['bindings'])
6666

67-
#rename columns from ['average_preceeding.datatype', 'average_preceeding.type',
68-
# 'average_preceeding.value', 'count.datatype', 'count.type',
69-
# 'count.value', 'def2.type', 'def2.value', 'displayId.type',
70-
# 'displayId.value', 'role.type', 'role.value', 'title.type',
71-
# 'title.value']
72-
order_df.columns = ['ad', 'at','centfol', 'cd', 'ct', 'count', 'dt','deff', 'dt1', 'displayId','rt', 'roletog', 'tt','title']
73-
67+
#rename columns
68+
rename_dict = {'average_preceeding.datatype':'ad', 'average_preceeding.type':'at', 'average_preceeding.value':'centfol', 'count.datatype':'cd', 'count.type':'ct', 'count.value':'count', 'def2.type':'dt', 'def2.value':'deff', 'displayId.type':'dt1', 'displayId.value':'displayId', 'role.type':'rt', 'role.value':'roletog', 'title.type':'tt', 'title.value':'title'}
69+
order_df.columns = [rename_dict[col] for col in order_df.columns]
70+
7471
#drop unneeded columns
7572
order_df = order_df.drop(['ad', 'at', 'cd', 'ct', 'dt', 'dt1', 'rt', 'tt'], axis=1)
7673

@@ -85,6 +82,7 @@ def sankey(url, uri, title, instance):
8582
#'roletog' - role of the part (e.g. http://identifiers.org/so/SO:0000141)
8683
#'title' - human name of the part
8784
"""
85+
8886
#change number columns from strings to number type
8987
order_df['count'] = order_df['count'].apply(pd.to_numeric)
9088
order_df['centfol'] = order_df['centfol'].apply(pd.to_numeric)

uri_to_url.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def uri_to_url(data, instance, spoofed_instance):
4343
2 https://dev.synbiohub.org/public/igem/BBa_C0040/1
4444
"""
4545
#checks if any changes need to be made
46-
if spoofed_insance != instance:
46+
if spoofed_instance != instance:
4747

4848
#finds the data type of the input data
4949
data_type = type(data)

0 commit comments

Comments
 (0)