Skip to content

Commit

Permalink
kegg highlight additional compounds
Browse files Browse the repository at this point in the history
  • Loading branch information
hamin committed Oct 24, 2024
1 parent 7aeea36 commit c93e7fa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
20 changes: 17 additions & 3 deletions routes/apps/_kegg.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from Bio.KEGG.KGML import KGML_parser
from Bio.Graphics.KGML_vis import KGMLCanvas
from Bio.Graphics.ColorSpiral import ColorSpiral
#from IPython.display import Image, HTML
import tempfile


Expand Down Expand Up @@ -62,10 +61,23 @@ def organism_options(cache, pathway_id):

return [{'label': org, 'value': org} for org in org_value.split(',')]

def additional_compound_options(cache, pathway_id, organism_id):
compound_pathway_data=read_compound_pathway(cache)
try:
pathname = pathway_id.replace("map", organism_id)
pathway=KGML_parser.read(kegg_get(pathname, "kgml"))
compound_list=[]
for compound in pathway.compounds :
c=compound.name.split(":")[-1]
compound_list.append(c)

# return [{'label': id, 'value': id} for id in compound_list] if compound_list else []
return [{'label': f"{id}: {compound_pathway_data.loc[compound_pathway_data['compound_id'] == id, 'compound_name'].values[0]}"
if not compound_pathway_data.loc[compound_pathway_data['compound_id'] == id, 'compound_name'].empty else id, 'value': id} for id in compound_list] if compound_list else []
except:
return []


def network_pdf(selected_compound, pathway_id, organism_id):
def network_pdf(selected_compound, pathway_id, organism_id, additional_compound):
# Clean up previous kegg files, clean all if total pdfs more than 50, else clean 30 mins or older files
kegg_files = glob.glob("/tmp/kegg-*.pdf")
if len(kegg_files) > 20:
Expand All @@ -80,6 +92,8 @@ def network_pdf(selected_compound, pathway_id, organism_id):

for compound in pathway.compounds :
c=compound.name.split(":")[-1]
if c in additional_compound:
compound.graphics[0].bgcolor="#FFFF00"
if c in selected_compound:
compound.graphics[0].bgcolor="#FF0000"

Expand Down
22 changes: 18 additions & 4 deletions routes/apps/kegg.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import plotly.express as px
# from plotly.io import write_image
import plotly.graph_objects as go
from ._kegg import compound_options, pathway_options, organism_options, network_pdf
from ._kegg import compound_options, pathway_options, organism_options, additional_compound_options, network_pdf
from dash import dash_table

import io
Expand Down Expand Up @@ -124,7 +124,8 @@ def make_loading(children,i):
html.H5("Filters", style={"margin-top":10}),
html.Label('Compound'), make_loading( dcc.Dropdown( id='opt-compound', multi=True, optionHeight=120), 1),
html.Label('Pathway',style={"margin-top":10}), make_loading( dcc.Dropdown( id='opt-pathway', optionHeight=90), 2 ),
html.Label('Organism',style={"margin-top":10}), make_loading( dcc.Dropdown( id='opt-organism', placeholder="No Pathway Selected"), 3 ),
html.Label('Organism',style={"margin-top":10}), make_loading( dcc.Dropdown( id='opt-organism'), 3 ),
html.Label('Highlight Additional Compound',style={"margin-top":10}), make_loading( dcc.Dropdown( id='opt-additional', multi=True, optionHeight=120), 4 ),
html.Label('Download file prefix',style={"margin-top":10}),
dcc.Input(id='download_name', value="kegg", type='text',style={"width":"100%", "height":"34px"})
],
Expand Down Expand Up @@ -203,6 +204,18 @@ def update_organisms(selected_pathway):
return [], "No Organism Found for Selected Pathway"
return org_options, "Select Organism"

# Callback to update opt-additional based on selected pathway and organism
@dashapp.callback(
Output('opt-additional', 'options'),
Output('opt-additional', 'placeholder'),
Input('opt-pathway', 'value'),
Input('opt-organism', 'value')
)
def update_additional(selected_pathway, selected_organism):
if selected_pathway is None or selected_organism is None:
return [], "No Available Compound"
return additional_compound_options(cache, selected_pathway, selected_organism), "Select Additional Compound"


# Callback on submit
@dashapp.callback(
Expand All @@ -212,16 +225,17 @@ def update_organisms(selected_pathway):
State("opt-compound", "value"),
State("opt-pathway", "value"),
State("opt-organism", "value"),
State("opt-additional", "value"),
State('download_name','value'),
)
def update_output(session_id, n_clicks, compound, pathway, organism, download_name):
def update_output(session_id, n_clicks, compound, pathway, organism, additional_compound, download_name):
if not n_clicks:
return html.Div([])

if not compound or pathway is None or organism is None:
return html.Div([dcc.Markdown("*** Please select at least a compound, pathway and organism!", style={"margin-top":"15px","margin-left":"15px"})])

pdf_path=network_pdf(compound,pathway,organism)
pdf_path=network_pdf(compound, pathway, organism, additional_compound)
if pdf_path is None:
return html.Div([dcc.Markdown("*** Failed to generate network pdf!", style={"margin-top":"15px","margin-left":"15px"})])

Expand Down

0 comments on commit c93e7fa

Please sign in to comment.