diff --git a/README.md b/README.md index a52f4a0..2ae9e8a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # INSTALL ## Docker -Run `docker run --publish 8080:5000 --detach --name component-use-plug synbiohub/plugin-visual-component-use` Check it is up using localhost:8080/sankey/status +Run `docker run --publish 8095:5000 --detach --name component-use-plug synbiohub/plugin-visual-component-use` Check it is up using localhost:8095/sankey/status ## Python Using python run `pip install -r requirements.txt` to install the requirements. diff --git a/app.py b/app.py index e0e9c19..e994f2f 100644 --- a/app.py +++ b/app.py @@ -12,6 +12,7 @@ from toggle_bars import toggle_bars import tempfile, os, shutil +import traceback app = Flask(__name__) @@ -27,7 +28,7 @@ def Sankey_Evaluate(): ########## REPLACE THIS SECTION WITH OWN RUN CODE ################# #uses rdf types - accepted_types = {'Component'} + accepted_types = {'Component', 'ComponentDefinition'} acceptable = rdf_type in accepted_types @@ -51,6 +52,7 @@ def Sankey_Run(): size = data['size'] rdf_type = data['type'] shallow_sbol = data['shallow_sbol'] + token = data['token'] url = complete_sbol.replace('/sbol','') @@ -61,15 +63,13 @@ def Sankey_Run(): #top_level_url = 'https://dev.synbiohub.org/public/igem/BBa_B0012/1' #retrieve information about the poi - self_df, display_id, title, role, count = input_data(top_level_url, instance_url) + self_df, display_id, title, role, count = input_data(top_level_url, instance_url, token) - #print("Find role name") #Find the role name in the ontology of the part of interest role_link = find_role_name(role, plural = False) #create data for the sankey diagram and format it correctly - df_sankey = sankey(url, top_level_url, title, instance_url) - + df_sankey = sankey(url, top_level_url, title, instance_url, token) sankey_title = "Parts Co-Located with "+ title + " (a "+role_link+")" #create a temporary directory @@ -90,7 +90,8 @@ def Sankey_Run(): return result except Exception as e: - print(e) + print(e, flush=True) + print(traceback.format_exc(), flush=True) abort(400) #flask run --host=0.0.0.0 @@ -106,7 +107,7 @@ def Bar_Evaluate(): ########## REPLACE THIS SECTION WITH OWN RUN CODE ################# #uses rdf types - accepted_types = {'Component'} + accepted_types = {'Component', 'ComponentDefinition'} acceptable = rdf_type in accepted_types @@ -130,17 +131,18 @@ def Bar_Run(): size = data['size'] rdf_type = data['type'] shallow_sbol = data['shallow_sbol'] + token = data['token'] url = complete_sbol.replace('/sbol','') try: #create input data - self_df, display_id, title, role, count = input_data(top_level_url, instance_url) + self_df, display_id, title, role, count = input_data(top_level_url, instance_url, token) #create and format data for the most_used barchart bar_df = most_used_bar(top_level_url, instance_url, display_id, title, role, - count) + count, token) #graph title for most used barchart graph_title = f'Top Ten Parts by Number of Uses Compared to {title}' @@ -161,7 +163,7 @@ def Bar_Run(): role_link = find_role_name(role, plural = False) bar_df = most_used_by_type_bar(top_level_url,instance_url, display_id, title, - role, count) + role, count, token) #graph title for most used barchart graph_title = f'Top Ten {role_link} by Number of Uses Compared to {title}' @@ -180,5 +182,6 @@ def Bar_Run(): return toggle_display except Exception as e: - print(e) + print(e, flush=True) + print(traceback.format_exc(), flush=True) abort(400) diff --git a/input_data.py b/input_data.py index 92b7320..b6d6303 100644 --- a/input_data.py +++ b/input_data.py @@ -1,8 +1,8 @@ import json import requests -from pandas.io.json import json_normalize +from pandas import json_normalize -def input_data(uri, instance): +def input_data(uri, instance, token = None): """ Finds information about an SBOL part based on its uri @@ -10,7 +10,7 @@ def input_data(uri, instance): ------- import json import requests - from pandas.io.json import json_normalize + from pandas import json_normalize Input_Query.txt Parameters @@ -55,7 +55,10 @@ def input_data(uri, instance): status = 200 - req = requests.get(instance) + headers = {} + if token is not None: + headers['X-authorization'] = token + req = requests.get(instance, headers=headers) if req.status_code != 200: #if synbiohub is offline return an error status = 424 else: @@ -66,7 +69,7 @@ def input_data(uri, instance): sparqlquery = sparqlquery.replace('https://synbiohub.org/public/igem/BBa_B0012/1',uri) #accept repsonses - r = requests.post(instance+"sparql", data = {"query":sparqlquery}, headers = {"Accept":"application/json"}) + r = requests.post(instance+"sparql", data = {"query":sparqlquery}, headers = {"Accept":"application/json", "X-authorization": token} if token is not None else {"Accept":"application/json"}) #format responses d = json.loads(r.text) diff --git a/most_used_bar.py b/most_used_bar.py index dcfea8d..c30945f 100644 --- a/most_used_bar.py +++ b/most_used_bar.py @@ -1,10 +1,10 @@ import pandas as pd import requests import json -from pandas.io.json import json_normalize +from pandas import json_normalize from uri_to_url import uri_to_url -def most_used_bar(uri, instance, display_id, title, role, count): +def most_used_bar(uri, instance, display_id, title, role, count, token): """ Uses a sparql query to obtain information about the most used parts and format the data in such a way that a graph can be made comparing the poi (part of interest) to the most used parts @@ -76,9 +76,10 @@ def most_used_bar(uri, instance, display_id, title, role, count): fl = open("Most_Used_Query.txt", "r") sparqlquery = fl.read() + #send the query r = requests.post(instance+"sparql", data = {"query":sparqlquery}, - headers = {"Accept":"application/json"}) + headers = {"Accept":"application/json", "X-authorization": token} if token is not None else {"Accept":"application/json"}) #format query results d = json.loads(r.text) diff --git a/most_used_by_type_bar.py b/most_used_by_type_bar.py index 0dbccf4..022edc7 100644 --- a/most_used_by_type_bar.py +++ b/most_used_by_type_bar.py @@ -1,10 +1,10 @@ import pandas as pd import requests import json -from pandas.io.json import json_normalize +from pandas import json_normalize from uri_to_url import uri_to_url -def most_used_by_type_bar(uri, instance, display_id, title, role, count): +def most_used_by_type_bar(uri, instance, display_id, title, role, count, token): """ Uses a sparql query to obtain information about the most used parts (of the same type as the poi e.g. all terminators) and format the data in such a way that a graph can be made comparing the poi (part of interest) to the most used parts @@ -81,7 +81,7 @@ def most_used_by_type_bar(uri, instance, display_id, title, role, count): sparql_query = sparql_query.replace("0000167", role) #perform the query - r = requests.post(instance+"sparql", data = {"query":sparql_query}, headers = {"Accept":"application/json"}) + r = requests.post(instance+"sparql", data = {"query":sparql_query}, headers = {"Accept":"application/json", "X-authorization": token} if token is not None else {"Accept":"application/json"}) #format the data d = json.loads(r.text) diff --git a/requirements.txt b/requirements.txt index 1d927dc..043ace3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,9 @@ bs4==0.0.1 -Flask==2.0.1 +Flask==2.2.5 jsonschema==3.0.1 -pandas==0.24.2 +pandas==2.2.3 plotly==3.9.0 requests==2.21.0 - - +Werkzeug==2.2.3 +waitress +lxml \ No newline at end of file diff --git a/sankey.py b/sankey.py index 3571f6b..715ae74 100644 --- a/sankey.py +++ b/sankey.py @@ -1,10 +1,10 @@ import requests import json import pandas as pd -from pandas.io.json import json_normalize +from pandas import json_normalize from uri_to_url import uri_to_url -def sankey(url, uri, title, instance): +def sankey(url, uri, title, instance, token): """ This function creates the table needed to make the sankey diagram to create the sankey diagram two linked tables are needed @@ -16,7 +16,7 @@ def sankey(url, uri, title, instance): import requests import json import pandas as pd - from pandas.io.json import json_normalize + from pandas import json_normalize Preceding_Percent_Query.txt @@ -58,7 +58,7 @@ def sankey(url, uri, title, instance): #substitute in the name of the particular part sparqlquery = sparqlquery.replace('https://synbiohub.org/public/igem/BBa_E0040/1',uri) - r = requests.post(instance+"sparql", data = {"query":sparqlquery}, headers = {"Accept":"application/json"}) + r = requests.post(instance+"sparql", data = {"query":sparqlquery}, headers = {"Accept":"application/json", "X-authorization": token} if token is not None else {"Accept":"application/json"}) #reformat query results d = json.loads(r.text)