Skip to content

Commit

Permalink
Implementation of population frequencies with selection1d stream and …
Browse files Browse the repository at this point in the history
…fake frequency data
  • Loading branch information
savitakartik committed Mar 28, 2024
1 parent 72bd84e commit a9a5bb0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
6 changes: 6 additions & 0 deletions tsqc/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ def mutations_df(self):
"num_descendants": counts.num_descendants,
"num_inheritors": counts.num_inheritors,
"num_parents": counts.num_parents,
"Pop_A": np.random.randint(0, 20, ts.num_mutations),
"Pop_B": np.random.randint(0, 20, ts.num_mutations),
"Pop_C": np.random.randint(0, 20, ts.num_mutations),
}
)

Expand All @@ -391,6 +394,9 @@ def mutations_df(self):
"num_descendants": "int",
"num_inheritors": "int",
"num_parents": "int",
"Pop_A": "int",
"Pop_B": "int",
"Pop_C": "int",
}
)

Expand Down
56 changes: 46 additions & 10 deletions tsqc/pages/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

def make_muts_panel(log_y, tsm):
plot_width = 1000
muts_df = tsm.mutations_df
y_dim = "time"
if log_y:
tsm.mutations_df["log_time"] = np.log10(1 + tsm.mutations_df["time"])
muts_df["log_time"] = np.log10(1 + tsm.mutations_df["time"])
y_dim = "log_time"

points = tsm.mutations_df.hvplot.scatter(
points = muts_df.hvplot.scatter(
x="position",
y=y_dim,
hover_cols=["id", "num_parents", "num_descendants", "num_inheritors"],
Expand All @@ -31,9 +32,8 @@ def make_muts_panel(log_y, tsm):
)

range_stream = hv.streams.RangeXY(source=points)
streams = [range_stream]

filtered = points.apply(filter_points, streams=streams)
filtered = points.apply(filter_points, streams=[range_stream])

tooltips = [
("ID", "@id"),
Expand All @@ -46,25 +46,25 @@ def make_muts_panel(log_y, tsm):
color="num_inheritors",
alpha="num_inheritors",
colorbar=True,
cmap="BuGn",
cmap="kgy",
colorbar_position="left",
clabel="inheritors",
tools=[hover],
tools=[hover, "tap"],
)

hover = filtered.apply(hover_points)
shaded = hd.datashade(filtered, width=400, height=400, streams=streams)
shaded = hd.datashade(filtered, width=400, height=400, streams=[range_stream])

main = (shaded * hover).opts(
hv.opts.Points(tools=["hover"], alpha=0.1, hover_alpha=0.2, size=10),
)

time_hist = hv.DynamicMap(
make_hist_on_axis(dimension=y_dim, points=points), streams=streams
make_hist_on_axis(dimension=y_dim, points=points), streams=[range_stream]
)
site_hist = hv.DynamicMap(
make_hist_on_axis(dimension="position", points=points),
streams=streams,
streams=[range_stream],
)

breakpoints = tsm.ts.breakpoints(as_array=True)
Expand All @@ -76,7 +76,7 @@ def make_muts_panel(log_y, tsm):
"y1": tsm.mutations_df[y_dim].max(),
}
)
trees_hist = hv.DynamicMap(selected_hist(bp_df), streams=streams)
trees_hist = hv.DynamicMap(selected_hist(bp_df), streams=[range_stream])
trees_hist.opts(
width=config.PLOT_WIDTH,
height=100,
Expand All @@ -102,6 +102,41 @@ def make_muts_panel(log_y, tsm):
annot_track = make_annotation_plot(tsm, genes_df)
layout += annot_track

selection_stream = hv.streams.Selection1D(source=points)
pops = ["Pop_A", "Pop_B", "Pop_C"]

def update_pop_freq_plot(index):
if not index:
return hv.Bars([], kdims="pop", vdims="freq").opts(
width=config.PLOT_WIDTH, height=400, title="Tap on a mutation"
)
mut_data = muts_df.loc[index[0]]
freqs = mut_data[pops].values
bars = hv.Bars(zip(pops, freqs), "pop", "freq").opts(
width=config.PLOT_WIDTH,
height=400,
framewise=True,
title=f"Mutation {index[0]}",
ylim=(0, 20),
)
return bars

def update_mut_info_table(index):
if not index:
return hv.Table([], kdims=["info"], vdims=["value"]).opts(
width=config.PLOT_WIDTH, title="Tap on a mutation"
)
mut_data = muts_df.loc[index[0]].drop(pops)
return hv.Table(mut_data.items(), kdims=["info"], vdims=["value"]).opts(
width=config.PLOT_WIDTH, title=f"Mutation {index[0]}"
)

pop_data_dynamic = hv.DynamicMap(update_pop_freq_plot, streams=[selection_stream])
mut_info_table_dynamic = hv.DynamicMap(
update_mut_info_table, streams=[selection_stream]
)
layout += (pop_data_dynamic + mut_info_table_dynamic).cols(2)

return pn.Column(layout.opts(shared_axes=True).cols(1))


Expand Down Expand Up @@ -148,4 +183,5 @@ def page(tsm):
pn.pane.Markdown("### Plot Options"),
log_y_checkbox,
)

return pn.Column(plot_options, muts_panel)

0 comments on commit a9a5bb0

Please sign in to comment.