Skip to content

Commit

Permalink
Add Table
Browse files Browse the repository at this point in the history
  • Loading branch information
syntonym committed Jan 16, 2021
1 parent 6f474c9 commit c69196a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
2 changes: 1 addition & 1 deletion menqu/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.0.0"
__version__ = "3.1.0"
6 changes: 4 additions & 2 deletions menqu/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from menqu.themes import CONDITIONS_THEME

from menqu.analysis import prepare, _main, parse_well, get_sample_data, _update
from menqu.widgets import BarGraphs, HeatmapGraphs, ColorPickers
from menqu.widgets import BarGraphs, HeatmapGraphs, ColorPickers, Table
import click
import sys
import os.path
Expand Down Expand Up @@ -188,6 +188,8 @@ def __init__(self):

self.bargraphs = BarGraphs(self.bargraphs_container, gene_data, condition_data, samples, genes, conditions, color_pickers=self.colorpickers.color_pickers)

self.table = Table(self.table_container, gene_data, condition_data, samples, genes, conditions, color_pickers=self.colorpickers.color_pickers)

self._socket_in_use = False

self._doc = None
Expand Down Expand Up @@ -402,7 +404,7 @@ def load_fake_data_2(self):
def start_py_web_view(port):
window = webview.create_window('menqu', 'http://localhost:5006/')

webview.start(func=start_zmq_window_server, args=(window, port))
webview.start(func=start_zmq_window_server, args=(window, port), debug=True)

def start_zmq_window_server(window, port):
context = zmq.Context()
Expand Down
72 changes: 71 additions & 1 deletion menqu/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from bokeh.palettes import Viridis256
from bokeh.core.properties import value
from bokeh.transform import linear_cmap
from bokeh.models import Column, FactorRange, ColumnDataSource, BooleanFilter, CDSView, Row, ColorPicker
from bokeh.models import Column, FactorRange, ColumnDataSource, BooleanFilter, CDSView, Row, ColorPicker, DataTable, TableColumn
from bokeh.models.widgets.tables import HTMLTemplateFormatter
from bokeh.models.callbacks import CustomJS

from menqu.helpers import apply_theme, general_mapper
from menqu.themes import CONDITIONS_THEME
Expand Down Expand Up @@ -200,3 +202,71 @@ def _redraw_conditions(self):
current_row = Row()
current_row.children.append(cp)
self._root_widget.children.append(current_row)


class Table:

def __init__(self, root, gene_data, condition_data, samples, genes, conditions, color_pickers={}):
self._gene_data = gene_data
self._condition_data = condition_data
self._genes = genes
self._conditions = conditions
self._samples = samples

self._color_pickers = color_pickers

self._maxvalue = max(max(gene_data["R1"]), max(gene_data["R2"]), max(gene_data["R3"]))
self._width = 25
self._height = 25
self._linear_color_mapper = None

self._condition_height = 25

self._root = Column()
self._draw()

root.children.append(self._root)

def redraw(self):
self._root.children = []
self._draw()

def _draw(self):
d = self._gene_data

for condition in self._conditions:
d[condition] = [self._condition_data[condition][self._condition_data["Sample"].index(sample)] for sample in d["Sample"]]

template_update_1 = '<% if (value === "True") {print(\'<div style="height: 20px; width: 20px; background-color:'
template_update_2 = ';"></div>\')} %>'
def make_template(color):
return template_update_1 + str(color) + template_update_2

template_update_1_esc = template_update_1.replace("'", "\\'")
template_update_2_esc = template_update_2.replace("'", "\\'")

formatters = [HTMLTemplateFormatter(template=make_template(color=self._color_pickers[cond].color)) for cond in self._conditions]



condition_columns = [TableColumn(field=cond, title=cond, formatter=form, width=10) for cond, form in zip(self._conditions, formatters)]
columns = [
TableColumn(field="Sample", title="Sample", width=200),
TableColumn(field="Gene", title="Gene", width=10),
TableColumn(field="R1", title="R1"),
TableColumn(field="R2", title="R2"),
TableColumn(field="R3", title="R3"),
*condition_columns
]

dt = DataTable(source=ColumnDataSource(d), columns=columns, width_policy="fit")

code = f"form.template = '{template_update_1_esc}' + cp.color + '{template_update_2_esc}'; dt.change.emit();"

for cp, formatter, col in zip(self._color_pickers.values(), formatters, condition_columns):
cp.js_on_change("color", CustomJS(args={"cp": cp, "col":col, "form": formatter, "dt": dt}, code=code))

self._root.children.append(dt)

dt.source.patch({})

0 comments on commit c69196a

Please sign in to comment.