Skip to content

Commit 6165311

Browse files
authored
Merge branch 'master' into gh976-empty-window-g123-h12
2 parents caf4f2f + c5d0c75 commit 6165311

5 files changed

Lines changed: 663 additions & 78 deletions

File tree

malariagen_data/anoph/dipclust.py

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
_multiallelic_diplotype_mean_sqeuclidean,
1414
_multiallelic_diplotype_mean_cityblock,
1515
)
16-
from ..plotly_dendrogram import _plot_dendrogram
16+
from ..plotly_dendrogram import _plot_dendrogram, concat_clustering_subplots
1717
from . import (
1818
base_params,
1919
plotly_params,
@@ -22,13 +22,9 @@
2222
dipclust_params,
2323
cnv_params,
2424
)
25-
from .snp_frq import AnophelesSnpFrequencyAnalysis
25+
from .snp_frq import AnophelesSnpFrequencyAnalysis, AA_CHANGE_QUERY
2626
from .cnv_frq import AnophelesCnvFrequencyAnalysis
2727

28-
AA_CHANGE_QUERY = (
29-
"effect in ['NON_SYNONYMOUS_CODING', 'START_LOST', 'STOP_LOST', 'STOP_GAINED']"
30-
)
31-
3228

3329
class AnophelesDipClustAnalysis(
3430
AnophelesCnvFrequencyAnalysis, AnophelesSnpFrequencyAnalysis
@@ -508,56 +504,6 @@ def _dipclust_snp_trace(
508504

509505
return snp_trace, n_snps
510506

511-
def _dipclust_concat_subplots(
512-
self,
513-
figures,
514-
width,
515-
height,
516-
row_heights,
517-
region: base_params.regions,
518-
n_snps: int,
519-
sample_sets: Optional[base_params.sample_sets],
520-
sample_query: Optional[base_params.sample_query],
521-
):
522-
from plotly.subplots import make_subplots # type: ignore
523-
524-
title_lines = []
525-
if sample_sets is not None:
526-
title_lines.append(f"sample sets: {sample_sets}")
527-
if sample_query is not None:
528-
title_lines.append(f"sample query: {sample_query}")
529-
title_lines.append(f"genomic region: {region} ({n_snps} SNPs)")
530-
title = "<br>".join(title_lines)
531-
532-
# make subplots
533-
fig = make_subplots(
534-
rows=len(figures),
535-
cols=1,
536-
shared_xaxes=True,
537-
vertical_spacing=0.02,
538-
row_heights=row_heights,
539-
)
540-
541-
for i, figure in enumerate(figures):
542-
if isinstance(figure, go.Figure):
543-
# This is a figure, access the traces within it.
544-
for trace in range(len(figure["data"])):
545-
fig.add_trace(figure["data"][trace], row=i + 1, col=1)
546-
else:
547-
# Assume this is a trace, add directly.
548-
fig.add_trace(figure, row=i + 1, col=1)
549-
550-
fig.update_xaxes(visible=False)
551-
fig.update_layout(
552-
title=title,
553-
width=width,
554-
height=height,
555-
hovermode="closest",
556-
plot_bgcolor="white",
557-
)
558-
559-
return fig
560-
561507
def _insert_dipclust_snp_trace(
562508
self,
563509
*,
@@ -597,7 +543,7 @@ def _insert_dipclust_snp_trace(
597543
print(
598544
f"No SNPs were found below {snp_filter_min_maf} allele frequency. Omitting SNP genotype plot."
599545
)
600-
return figures, subplot_heights
546+
return figures, subplot_heights, n_snps_transcript
601547

602548
@doc(
603549
summary=""""
@@ -740,8 +686,13 @@ def plot_diplotype_clustering_advanced(
740686
figures.append(cnv_trace)
741687
subplot_heights.append(cnv_row_height * n_cnv_genes)
742688

689+
n_snps_transcripts = []
743690
if isinstance(snp_transcript, str):
744-
figures, subplot_heights = self._insert_dipclust_snp_trace(
691+
(
692+
figures,
693+
subplot_heights,
694+
n_snps_transcript,
695+
) = self._insert_dipclust_snp_trace(
745696
transcript=snp_transcript,
746697
figures=figures,
747698
subplot_heights=subplot_heights,
@@ -753,12 +704,18 @@ def plot_diplotype_clustering_advanced(
753704
dendro_sample_id_order=dendro_sample_id_order,
754705
snp_filter_min_maf=snp_filter_min_maf,
755706
snp_colorscale=snp_colorscale,
707+
snp_row_height=snp_row_height,
756708
chunks=chunks,
757709
inline_array=inline_array,
758710
)
711+
n_snps_transcripts.append(n_snps_transcript)
759712
elif isinstance(snp_transcript, list):
760713
for st in snp_transcript:
761-
figures, subplot_heights = self._insert_dipclust_snp_trace(
714+
(
715+
figures,
716+
subplot_heights,
717+
n_snps_transcript,
718+
) = self._insert_dipclust_snp_trace(
762719
transcript=st,
763720
figures=figures,
764721
subplot_heights=subplot_heights,
@@ -770,14 +727,16 @@ def plot_diplotype_clustering_advanced(
770727
dendro_sample_id_order=dendro_sample_id_order,
771728
snp_filter_min_maf=snp_filter_min_maf,
772729
snp_colorscale=snp_colorscale,
730+
snp_row_height=snp_row_height,
773731
chunks=chunks,
774732
inline_array=inline_array,
775733
)
734+
n_snps_transcripts.append(n_snps_transcript)
776735

777736
# Calculate total height based on subplot heights, plus a fixed
778737
# additional component to allow for title, axes etc.
779738
height = sum(subplot_heights) + 50
780-
fig = self._dipclust_concat_subplots(
739+
fig = concat_clustering_subplots(
781740
figures=figures,
782741
width=width,
783742
height=height,
@@ -798,6 +757,44 @@ def plot_diplotype_clustering_advanced(
798757
legend=dict(itemsizing=legend_sizing, tracegroupgap=0),
799758
)
800759

760+
# add lines to aa plot - looks neater
761+
if snp_transcript:
762+
n_transcripts = (
763+
len(snp_transcript) if isinstance(snp_transcript, list) else 1
764+
)
765+
for i in range(n_transcripts):
766+
tx_idx = len(figures) - n_transcripts + i + 1
767+
if n_snps_transcripts[i] > 0:
768+
fig.add_hline(
769+
y=-0.5, line_width=1, line_color="grey", row=tx_idx, col=1
770+
)
771+
for j in range(n_snps_transcripts[i]):
772+
fig.add_hline(
773+
y=j + 0.5,
774+
line_width=1,
775+
line_color="grey",
776+
row=tx_idx,
777+
col=1,
778+
)
779+
780+
fig.update_xaxes(
781+
showline=True,
782+
linecolor="grey",
783+
linewidth=1,
784+
row=tx_idx,
785+
col=1,
786+
mirror=True,
787+
)
788+
789+
fig.update_yaxes(
790+
showline=True,
791+
linecolor="grey",
792+
linewidth=1,
793+
row=tx_idx,
794+
col=1,
795+
mirror=True,
796+
)
797+
801798
if show:
802799
fig.show(renderer=renderer)
803800
return None

0 commit comments

Comments
 (0)