Skip to content

Commit 2e0c935

Browse files
committed
Improved legends and tick marks
1 parent b1f7eda commit 2e0c935

File tree

2 files changed

+86
-28
lines changed

2 files changed

+86
-28
lines changed

panagram/__main__.py

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class View:
3737
#Initial end coordinate (optional)
3838
end: int = field(positional=True, nargs="?", metavar="end")
3939

40+
order: list = field(default=None)
41+
4042
#Run server in production mode (important for a public-facing server)
4143
ndebug: bool = field(action="store_true")
4244

panagram/view.py

+84-28
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ def create_tree(bitmap):
466466
)
467467
tree_tmp2 = hierarchy.to_tree(matrix, False)
468468
treedata = get_newick(tree_tmp2, tree_tmp2.dist, index.genome_names)
469+
print(treedata)
469470

470471
palette = sns.color_palette("RdPu", 130).as_hex()
471472
total_kmers = max(kmer_num_tmp) #[-1] #kmer_num_tmp["Solqui2"]
@@ -595,7 +596,7 @@ def make_anns(x,y, text, kmer_num, i):
595596
b=10,
596597
l=10,
597598
r=10))
598-
return fig
599+
return fig,label_legend
599600

600601
def get_local_info(bar_sum_regional, anchor_name, chrs):
601602
fig = make_subplots(
@@ -652,7 +653,7 @@ def plot_interactive(anchor_name, chrom, start_coord, end_coord, step, pancounts
652653
rows=4, cols=1,
653654
shared_xaxes=True,
654655
vertical_spacing=0.01,
655-
row_heights=[1,2,4,4],
656+
row_heights=[1,4,8,8],
656657
#subplot_titles=("Ref. Sequence Position","", "", "Conserved K-mers","" )
657658
)
658659
#start_coord = bitmap_counts
@@ -673,7 +674,6 @@ def plot_interactive(anchor_name, chrom, start_coord, end_coord, step, pancounts
673674
t0 = t1
674675

675676
cntr = 0
676-
anno = index.query_anno(anchor_name, chrom, start_coord, end_coord)
677677

678678
t1 = time.perf_counter()
679679
print(f"\tAnno Query {t1 - t0:0.4f} seconds")
@@ -708,8 +708,12 @@ def plot_interactive(anchor_name, chrom, start_coord, end_coord, step, pancounts
708708

709709
anno_names = ["gene"]
710710

711+
anno = index.query_anno(anchor_name, chrom, start_coord, end_coord)#.set_index("type_id").sort_index()
711712
anno["break"] = np.nan
713+
grp = anno.groupby("type_id")
712714
for t, df in anno.groupby("type_id"):
715+
#for i,t in enumerate(anno_types):
716+
# df = anno.loc[t]
713717
xs = df[["start","end","break"]].to_numpy().flatten()
714718
ys = np.full(len(xs),-t*2)
715719
name = df["type"].iloc[0]
@@ -732,15 +736,29 @@ def plot_interactive(anchor_name, chrom, start_coord, end_coord, step, pancounts
732736
mode="lines+markers"
733737
), row=2, col=1)
734738

735-
ys = np.arange(-len(ann_colors),0)+1
736-
names = anno_names[::-1]
737-
if len(ys) > 10:
738-
n = len(ys)//10
739-
ys = ys[::n]
740-
names = names[::n]
739+
#ys = np.arange(-len(ann_colors),0)+1
740+
#names = anno_types[::-1]+["gene"] #anno_names[::-1]
741+
#if len(ys) > 10:
742+
# n = len(ys)//10
743+
# ys = ys[::n]
744+
# names = names[::n]
745+
#print(ys)
746+
#print(names)
747+
748+
ticks = index.genomes[anchor_name].anno_type_ids.reset_index()
749+
ticks.columns = ["name","idx"]
750+
ticks.loc[0,"name"] = "gene"
751+
ticks["y"] = -ticks["idx"]
752+
print(ticks)
753+
#ticks.index[0] = "gene"
754+
755+
if len(ticks) > 5:
756+
n = len(ticks)//5
757+
ticks = ticks.iloc[::n]
758+
print(ticks)
741759

742760
fig.update_yaxes(
743-
tickvals=ys,ticktext=names,
761+
ticktext=ticks["name"],tickvals=ticks["y"],
744762
range=[-len(ann_colors)-0.5,1.5], #title="Annotation",
745763
row=2, col=1
746764
)
@@ -777,30 +795,48 @@ def plot_interactive(anchor_name, chrom, start_coord, end_coord, step, pancounts
777795

778796
#This is the conserved kmer plotting section
779797
fig.append_trace(go.Bar(x=x, y=pancounts.loc[0], name=str(0),
780-
legendgroup="group1",
781-
legendgrouptitle_text="Conserved K-mers",
798+
#legendgroup="group1",
799+
#legendgrouptitle_text="Conserved K-mers",
800+
showlegend=False,
782801
marker=dict(color='grey'),
783802
marker_line=dict(color='grey')
784-
),
785-
row=3, col=1 )
803+
), row=3, col=1 )
786804

787805
t1 = time.perf_counter()
788806
print(f"\tConserved k-mers (grey) {t1 - t0:0.4f} seconds")
789807
t0 = t1
808+
809+
fig.add_trace(go.Scattergl(
810+
x=[1,2],y=[1,2],
811+
marker=dict(
812+
color=[1, index.ngenomes],
813+
coloraxis="coloraxis",
814+
colorscale="viridis",
815+
colorbar_title="Pan-Count"
816+
), showlegend=False,opacity=0
817+
),row=3,col=1)
790818

791819
for i in pancounts.index[1:]:
792820
fig.append_trace(go.Bar(x=x, y=pancounts.loc[i], name=str(i),
793821
legendgroup="group1",
794822
legendgrouptitle_text="Conserved K-mers",
795-
marker=dict(color=colors[i-1]),
796-
marker_line=dict(color=colors[i-1])
797-
),
798-
row=3, col=1 )
823+
marker=dict(
824+
color=colors[i-1],
825+
line_color=colors[i-1],
826+
#colorscale="viridis",
827+
#coloraxis="coloraxis",colorbar_title="Pan-Count",
828+
),
829+
#marker_line=dict(color=colors[i-1]),
830+
showlegend=False,
831+
), row=3, col=1 )
799832

800833
fig.update_layout(barmode='stack', bargap=0.0)
801834
fig.update_xaxes(showticklabels=False, row=3, col=1)
802835

803-
fig.add_trace(go.Heatmap(z=paircounts, x=paircounts.columns, y=paircounts.index, coloraxis="coloraxis"),
836+
fig.add_trace(go.Heatmap(
837+
z=paircounts, x=paircounts.columns, y=paircounts.index,
838+
coloraxis="coloraxis2"
839+
),
804840
row=4, col=1 )
805841

806842
t1 = time.perf_counter()
@@ -826,8 +862,23 @@ def plot_interactive(anchor_name, chrom, start_coord, end_coord, step, pancounts
826862
fig.update_yaxes(title_text="# of k-mers", range=[0,adjusted_bin_size] , row=3, col=1)
827863

828864
#TODO don't use template, manually set background to white
829-
cax = {"colorscale":"plasma_r","colorbar":{"title":"Pair Cons.","y":0,"len":0.35,"yanchor":"bottom"}}
830-
fig.update_layout(xaxis_range=[start_coord,end_coord], font=dict(size=16),coloraxis=cax)
865+
pan_cax = {
866+
"colorscale" : "viridis",
867+
"colorbar" : {"title":"Pangenome Conservation","y":0.72,"len":0.35,"yanchor":"top","title_side":"right"}
868+
}
869+
870+
pair_cax = {
871+
"colorscale":"plasma_r",
872+
"colorbar": {
873+
"title":"Pairwise Conservation","y":0,"len":0.35,"yanchor":"bottom","title_side":"right"}
874+
}
875+
876+
fig.update_layout(
877+
xaxis_range=[start_coord,end_coord],
878+
font=dict(size=16),
879+
coloraxis=pan_cax,
880+
coloraxis2=pair_cax,
881+
)
831882

832883
t1 = time.perf_counter()
833884
print(f"\tTruly finishing touches {t1 - t0:0.4f} seconds")
@@ -1486,10 +1537,21 @@ def update_all_figs( chr_num, chrom, anchor_name, redo_wg, start_coord, end_coor
14861537
t1 = time.perf_counter()
14871538
print(f"query bitmap {t1 - t0:0.4f} seconds")
14881539
t0 = t1
1540+
1541+
b = bitmap.sample(n=min(len(bitmap),50000))
1542+
fig3,genome_order = create_tree(b)
1543+
1544+
t1 = time.perf_counter()
1545+
print(f"tree plot in {t1 - t0:0.4f} seconds")
1546+
t0 = t1
14891547

14901548
bin_size = ((end_coord - start_coord) // params.max_chr_bins) + 1
14911549
pan, pair = index.bitmap_to_bins(bitmap, bin_size)
14921550

1551+
if params.order is not None:
1552+
genome_order = params.order
1553+
pair = pair.loc[genome_order]
1554+
14931555
t1 = time.perf_counter()
14941556
print(f"transform bitmap {t1 - t0:0.4f} seconds")
14951557
toc_tmp_1 = t1
@@ -1523,17 +1585,11 @@ def update_all_figs( chr_num, chrom, anchor_name, redo_wg, start_coord, end_coor
15231585
fig2 = get_local_info(bar_sum_regional, anchor_name, chrom)
15241586
toc_tmp_33 = time.perf_counter()
15251587
print(f"fig 2 plot in {toc_tmp_33 - toc_tmp_32:0.4f} seconds")
1526-
1527-
b = bitmap.sample(n=min(len(bitmap),50000))
1528-
fig3 = create_tree(b)
1529-
1530-
toc_tmp_4 = time.perf_counter()
1531-
print(f"tree plot in {toc_tmp_4 - toc_tmp_33:0.4f} seconds")
15321588

15331589
#if redo_wg == 1:
15341590
big_plot = no_update#plot_whole_genome(anchor_name)
15351591
toc_tmp_5 = time.perf_counter()
1536-
print(f"plots big in {toc_tmp_5 - toc_tmp_4:0.4f} seconds")
1592+
print(f"plots big in {toc_tmp_5 - toc_tmp_33:0.4f} seconds")
15371593

15381594
pg_sizes_fig = make_genome_size_plot(anchor_name)
15391595
toc_tmp_6 = time.perf_counter()

0 commit comments

Comments
 (0)