Skip to content

Commit 36f6d51

Browse files
committed
move concat_subplots outside of dipclust.py to plotly_dendrogram.py
1 parent a878727 commit 36f6d51

3 files changed

Lines changed: 61 additions & 59 deletions

File tree

malariagen_data/anoph/dipclust.py

Lines changed: 2 additions & 52 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,
@@ -499,56 +499,6 @@ def _dipclust_snp_trace(
499499

500500
return snp_trace, n_snps
501501

502-
def _dipclust_concat_subplots(
503-
self,
504-
figures,
505-
width,
506-
height,
507-
row_heights,
508-
region: base_params.regions,
509-
n_snps: int,
510-
sample_sets: Optional[base_params.sample_sets],
511-
sample_query: Optional[base_params.sample_query],
512-
):
513-
from plotly.subplots import make_subplots # type: ignore
514-
515-
title_lines = []
516-
if sample_sets is not None:
517-
title_lines.append(f"sample sets: {sample_sets}")
518-
if sample_query is not None:
519-
title_lines.append(f"sample query: {sample_query}")
520-
title_lines.append(f"genomic region: {region} ({n_snps} SNPs)")
521-
title = "<br>".join(title_lines)
522-
523-
# make subplots
524-
fig = make_subplots(
525-
rows=len(figures),
526-
cols=1,
527-
shared_xaxes=True,
528-
vertical_spacing=0.02,
529-
row_heights=row_heights,
530-
)
531-
532-
for i, figure in enumerate(figures):
533-
if isinstance(figure, go.Figure):
534-
# This is a figure, access the traces within it.
535-
for trace in range(len(figure["data"])):
536-
fig.append_trace(figure["data"][trace], row=i + 1, col=1)
537-
else:
538-
# Assume this is a trace, add directly.
539-
fig.append_trace(figure, row=i + 1, col=1)
540-
541-
fig.update_xaxes(visible=False)
542-
fig.update_layout(
543-
title=title,
544-
width=width,
545-
height=height,
546-
hovermode="closest",
547-
plot_bgcolor="white",
548-
)
549-
550-
return fig
551-
552502
def _insert_dipclust_snp_trace(
553503
self,
554504
*,
@@ -768,7 +718,7 @@ def plot_diplotype_clustering_advanced(
768718
# Calculate total height based on subplot heights, plus a fixed
769719
# additional component to allow for title, axes etc.
770720
height = sum(subplot_heights) + 50
771-
fig = self._dipclust_concat_subplots(
721+
fig = concat_clustering_subplots(
772722
figures=figures,
773723
width=width,
774724
height=height,

malariagen_data/anoph/hapclust.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from numpydoc_decorator import doc # type: ignore
77

88
from ..util import CacheMiss, check_types, pdist_abs_hamming, pandas_apply
9-
from ..plotly_dendrogram import plot_dendrogram
9+
from ..plotly_dendrogram import plot_dendrogram, concat_clustering_subplots
1010
from . import (
1111
base_params,
1212
plotly_params,
@@ -19,12 +19,9 @@
1919
from .snp_data import AnophelesSnpData
2020
from .snp_frq import AA_CHANGE_QUERY, _make_snp_label_effect
2121
from .hap_data import AnophelesHapData
22-
from .dipclust import AnophelesDipClustAnalysis
2322

2423

25-
class AnophelesHapClustAnalysis(
26-
AnophelesHapData, AnophelesSnpData, AnophelesDipClustAnalysis
27-
):
24+
class AnophelesHapClustAnalysis(AnophelesHapData, AnophelesSnpData):
2825
def __init__(
2926
self,
3027
**kwargs,
@@ -343,6 +340,10 @@ def _haplotype_pairwise_distances(
343340
parameters=dict(
344341
snp_transcript="Plot amino acid variants for these transcripts.",
345342
snp_filter_min_maf="Filter amino acid variants with alternate allele frequency below this threshold.",
343+
snp_query="Query to filter SNPs for amino acid heatmap. Default is to include all non-synonymous SNPs.",
344+
cluster_threshold="Height at which to cut the dendrogram to form clusters. If not provided, no clusters assignment is not performed.",
345+
min_cluster_size="Minimum number of haplotypes required in a cluster to be included when cutting the dendrogram. Default is 5.",
346+
cluster_criterion="The cluster_criterion to use in forming flat clusters. One of 'inconsistent', 'distance', 'maxclust', 'maxclust_monochronic', 'monocrit'. See scipy.cluster.hierarchy.fcluster for details.",
346347
),
347348
)
348349
def plot_haplotype_clustering_advanced(
@@ -360,7 +361,7 @@ def plot_haplotype_clustering_advanced(
360361
cohort_size: Optional[base_params.cohort_size] = None,
361362
distance_metric: hapclust_params.distance_metric = hapclust_params.distance_metric_default,
362363
cluster_threshold: Optional[float] = None,
363-
min_cluster_size: Optional[int] = None,
364+
min_cluster_size: Optional[int] = 5,
364365
cluster_criterion="distance",
365366
color: plotly_params.color = None,
366367
symbol: plotly_params.symbol = None,
@@ -487,7 +488,7 @@ def plot_haplotype_clustering_advanced(
487488
# Calculate total height based on subplot heights, plus a fixed
488489
# additional component to allow for title, axes etc.
489490
height = sum(subplot_heights) + 50
490-
fig = self._dipclust_concat_subplots(
491+
fig = concat_clustering_subplots(
491492
figures=figures,
492493
width=width,
493494
height=height,

malariagen_data/plotly_dendrogram.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,54 @@ def plot_dendrogram(
130130
)
131131

132132
return fig, leaf_data
133+
134+
135+
def concat_clustering_subplots(
136+
figures,
137+
width,
138+
height,
139+
row_heights,
140+
region,
141+
n_snps,
142+
sample_sets,
143+
sample_query,
144+
):
145+
from plotly.subplots import make_subplots # type: ignore
146+
import plotly.graph_objects as go # type: ignore
147+
148+
title_lines = []
149+
if sample_sets is not None:
150+
title_lines.append(f"sample sets: {sample_sets}")
151+
if sample_query is not None:
152+
title_lines.append(f"sample query: {sample_query}")
153+
title_lines.append(f"genomic region: {region} ({n_snps} SNPs)")
154+
title = "<br>".join(title_lines)
155+
156+
# make subplots
157+
fig = make_subplots(
158+
rows=len(figures),
159+
cols=1,
160+
shared_xaxes=True,
161+
vertical_spacing=0.02,
162+
row_heights=row_heights,
163+
)
164+
165+
for i, figure in enumerate(figures):
166+
if isinstance(figure, go.Figure):
167+
# This is a figure, access the traces within it.
168+
for trace in range(len(figure["data"])):
169+
fig.append_trace(figure["data"][trace], row=i + 1, col=1)
170+
else:
171+
# Assume this is a trace, add directly.
172+
fig.append_trace(figure, row=i + 1, col=1)
173+
174+
fig.update_xaxes(visible=False)
175+
fig.update_layout(
176+
title=title,
177+
width=width,
178+
height=height,
179+
hovermode="closest",
180+
plot_bgcolor="white",
181+
)
182+
183+
return fig

0 commit comments

Comments
 (0)