Created
June 29, 2018 20:04
-
-
Save jackparmer/d90e51953f895c6b360c509df6924521 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dash | |
import dash_html_components as html | |
import dash_core_components as dcc | |
import pandas as pd | |
from dash.dependencies import Input, Output | |
from itertools import cycle | |
class GeneSetDropdown: | |
def __init__(self, gmt_file_name): | |
self.gene_sets = pd.read_csv(gmt_file_name, sep="\t", header=None) | |
self.set_names = self.gene_sets.iloc[:, 0].tolist() | |
self.indices = self.gene_sets.index.tolist() | |
def get_set(self, idx): | |
return filter(lambda x: not pd.isnull(x), self.gene_sets.loc[idx].tolist()[2:]) | |
def make_component(self, id): | |
return dcc.Dropdown( | |
id=id, | |
options=[{ | |
"label": label, | |
"value": value | |
} for label, value in zip(self.set_names, self.indices)] | |
) | |
GSD = GeneSetDropdown("h.all.v6.1.symbols.gmt") | |
GSD_component = GSD.make_component('gene-set-dropdown') | |
class GeneSearchBox: | |
def __init__(self, gmt_file_name): | |
self.genes = set(pd.read_csv( | |
gmt_file_name, | |
sep="\t", | |
header=None | |
).iloc[:, 2:].values.flatten().tolist()) | |
@staticmethod | |
def make_component(id): | |
return dcc.Textarea( | |
id=id, | |
value="", | |
placeholder="Enter a whitespace-separated list of HUGO symbols", | |
) | |
def check_valid(self, hugo_id): | |
return hugo_id in self.genes | |
def get_gene_color(self, hugo_id): | |
return 'green' if self.check_valid(hugo_id) else 'red' | |
@staticmethod | |
def parse_input(text_body): | |
return text_body.split() | |
GSB = GeneSearchBox("h.all.v6.1.symbols.gmt") | |
GSB_component = GSB.make_component('gene-searchbox') | |
app = dash.Dash('Dash') | |
app.layout = html.Div([ | |
GSB_component, | |
html.Div(id='gene-searchbox-parse'), | |
GSD_component, | |
html.Div(id='gene-set-list') | |
]) | |
@app.callback(Output('gene-set-list', 'children'), | |
[Input('gene-set-dropdown', 'value')]) | |
def populate_gene_set_list(value): | |
return [g + ' ' for g in GSD.get_set(value)] | |
@app.callback(Output('gene-searchbox-parse', 'children'), | |
[Input('gene-searchbox', 'value')]) | |
def populate_gene_search_results(value): | |
attempted_genes = GSB.parse_input(value) | |
return [html.Span(g + ' ', style={"color": GSB.get_gene_color(g)}) | |
for g in attempted_genes] | |
app.run_server(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment