-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_column_plots.py
More file actions
3145 lines (2940 loc) · 139 KB
/
Copy path_column_plots.py
File metadata and controls
3145 lines (2940 loc) · 139 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#adata_science_tools/_plotting/_column_plots.py
'''
updated 2026-01-30 ish to add barh_4X_dotplot_column() function
updated 2026-02-25 to add use_single_dotplot_colormap: true config option and apply to all dotplots in the script
'''
import matplotlib.pyplot as plt
# module at projects/gitbenlewis/adata_science_tools/_plotting/_column_plots.py
####### START ############. _column plots (horizontal bar / l2fc dotplots ) ###################.###################.###################.###################.
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import anndata # or use the quoted type hint instead
from matplotlib.patches import Patch
import anndata
import numpy as np
from matplotlib.ticker import StrMethodFormatter
def barh_column(
adata: anndata.AnnData | None = None,
use_adata_raw: bool = False,
layer: str | None =None,
x_df: pd.DataFrame | None = None,
var_df: pd.DataFrame | None = None,
obs_df: pd.DataFrame | None = None,
feature_list=None,
feature_label_vars_col: str | None = None,# if None then index is used
include_stripplot: bool = True,
feature_label_char_limit: int | None= 25,
feature_label_x: float = -0.02,
figsize: tuple[int, int] = (10, 30),
fig_title: str | None = None,
fig_title_y: float | None = .99,
fig_title_fontsize: int | None = 30,
feature_label_fontsize: int | None= 24,
tick_label_fontsize: int | None= 20,
legend_fontsize: int | None= 24,
tight_layout_rect_arg=[0, .05, 1, .99],
comparison_col: str | None = 'Treatment',
barh_remove_yticklabels: bool = True,
comparison_order: list[str] | None = None,
barh_subplot_xlabel: str | None = 'Expression (TPM)',
barh_sharex: bool = False,
barh_set_xaxis_lims: tuple[int, int]| None = None,
barh_legend: bool = True,
barh_legend_bbox_to_anchor: tuple[float, float] | None = (0.5, -.05),
savefig: bool = False,
file_name: str = 'test_plot.png'):
"""
adata_science_tools.barh_column()
#----------
Render a column of horizontal bar plots summarizing feature values grouped by a categorical comparison column.
------#
Parameters
#----------
adata : anndata.AnnData | None, optional
AnnData object consulted when `x_df` is not supplied; provides expression and metadata tables.
use_adata_raw : bool, optional
If `True`, use the raw counts stored in `adata.raw` for expression values instead of `adata.X`.
layer : str | None, optional
Name of an `adata.layers` matrix to use for expression values instead of `adata.X`.
x_df : pandas.DataFrame | None, optional
Observation-by-feature expression matrix supplied directly; takes precedence over `adata` sources.
var_df : pandas.DataFrame | None, optional
DataFrame of feature metadata; defaults to `adata.var` when `None`.
obs_df : pandas.DataFrame | None, optional
DataFrame of observation metadata; defaults to `adata.obs` when `None`.
feature_list : list[str] | None, optional
Ordered feature identifiers to display; entries must exist in `var_df.index`.
feature_label_vars_col : str | None, optional
Column in `var_df` containing display labels for features; defaults to the feature index.
include_stripplot : bool, optional
If `True`, include a strip plot overlay on top of the bar plots.
feature_label_char_limit : int | None, optional
Maximum number of characters retained for feature labels; set `None` to disable truncation.
feature_label_x : float, optional
Axes-relative x coordinate used to position feature labels beside each subplot.
figsize : tuple[int, int], optional
Figure size in inches passed to `plt.subplots`.
fig_title : str | None, optional
Figure-level title drawn above the bar plot column when provided.
fig_title_y : float | None, optional
Normalized y-position for the figure title.
fig_title_fontsize : int | None, optional
Font size applied to the figure title.
feature_label_fontsize : int | None, optional
Font size for feature labels on the y-axis.
tick_label_fontsize : int | None, optional
Font size used for axis tick labels.
legend_fontsize : int | None, optional
Font size for legend titles and entries.
tight_layout_rect_arg : list[float] | None, optional
Rectangle passed to `plt.tight_layout` to reserve padding around the figure.
comparison_col : str | None, optional
Observation column used to group samples prior to computing bar aggregates.
barh_remove_yticklabels : bool, optional
If `True`, remove tick labels on the y-axis (feature labels remain as axis labels).
comparison_order : list[str] | None, optional
Explicit ordering of categories in `comparison_col`; detected from data when `None`.
barh_subplot_xlabel : str | None, optional
Label applied to the shared x-axis for all bar plots.
barh_sharex : bool, optional
If `True`, share the x-axis across subplots so only the final subplot shows tick labels.
barh_set_xaxis_lims : tuple[int, int] | None, optional
Explicit x-axis limits applied to every subplot; computed from data when `None`.
barh_legend : bool, optional
If `True`, draw a legend mapping comparison levels to colors beneath the figure.
barh_legend_bbox_to_anchor : tuple[float, float] | None, optional
Legend anchor point expressed in figure-relative coordinates.
savefig : bool, optional
When `True`, save the rendered figure to `file_name`.
file_name : str, optional
Output path used when `savefig` is enabled.
-------#
Returns
#----------
tuple[matplotlib.figure.Figure, list[matplotlib.axes.Axes]]
Figure and list of Axes objects (single-element list when one feature is plotted).
-------#
Example usage
#----------
adtl.barh_column(
adata,
feature_list=feature_list,
comparison_col='Treatment',
feature_label_char_limit=25,
feature_label_x=-0.02,
figsize=(15, 25),
fig_title='Features by Treatment',
fig_title_y=1.0,
fig_title_fontsize=30,
feature_label_fontsize=24,
tick_label_fontsize=20,
legend_fontsize=20,
tight_layout_rect_arg=[0, 0.04, 1, 1],
savefig=False,
file_name='barh_column.png',
barh_subplot_xlabel='Feature Values',
barh_sharex=False,
barh_legend=True,
barh_legend_bbox_to_anchor=(0.5, -0.02),
)
-------#
"""
############ prep input tables / parse adata ############
if feature_list is None:
raise ValueError("feature_list must be provided.")
if adata is not None:
print(f"AnnData object provideed with shape {adata.shape} and {len(adata.var_names)} features.")
# if adata is provided, use it to get the data
if use_adata_raw:
if adata.raw is None:
raise ValueError("adata.raw is None, cannot use raw data.")
else:
print(f"Using adata.raw with shape {adata.raw.shape}")
adata = adata.raw.to_adata()
if layer is not None and layer not in adata.layers:
raise ValueError(f"Layer '{layer}' not found in adata.layers.")
if comparison_col not in adata.obs.columns:
raise ValueError(f"Column '{comparison_col}' not found in adata.obs.")
if x_df is not None:
print(f"Using provided x_df with shape {x_df.shape}")
_x_df = x_df.copy()
elif layer is None:
print("No layer provided, using adata.X with shape {adata.X.shape}")
_x_df = adata.X.copy() # use the raw data if no layer
elif adata is not None and layer in adata.layers:
print(f"No x_df provided, using adata.layers['{layer}'] with shape {adata.layers[layer].shape}")
_x_df = adata.layers[layer].copy()
if var_df is not None:
print(f"Using provided var_df with shape {var_df.shape}")
_var_df = var_df.copy()
else:
print(f"No var_df provided, using adata.var with shape {adata.var.shape}")
_var_df = adata.var.copy()
if obs_df is not None:
print(f"Using provided obs_df with shape {obs_df.shape}")
_obs_df = obs_df.copy()
else:
print(f"No obs_df provided, using adata.obs with shape {adata.obs.shape}")
_obs_df = adata.obs.copy()
# #) make df_obs_x, which is a tidy df with obs + expression columns
if hasattr(_x_df, "toarray"): # Convert sparse matrix to dense if necessary
_x_df = _x_df.toarray()
df_obs_x = pd.DataFrame(_x_df, columns=_var_df.index, index=_obs_df.index)
df_obs_x = pd.concat([_obs_df, df_obs_x], axis=1)
# Build feature labels for subplot y-labels
if (feature_label_vars_col is not None) and (feature_label_vars_col in _var_df.columns):
_bar_feature_label_series = _var_df[feature_label_vars_col]
_bar_feature_label_series = _bar_feature_label_series.where(
_bar_feature_label_series.notna(), _var_df.index.to_series()
).astype(str)
else:
if feature_label_vars_col is not None and feature_label_vars_col not in _var_df.columns:
print(f"Warning: feature_label_vars_col '{feature_label_vars_col}' not found in var_df; using index for labels.")
_bar_feature_label_series = _var_df.index.to_series().astype(str)
#if (feature_label_char_limit is not None) and (feature_label_char_limit > 0):
if (feature_label_char_limit is not None):
_bar_feature_label_series = _bar_feature_label_series.str.slice(0, int(feature_label_char_limit))
_bar_feature_label_map = _bar_feature_label_series.to_dict()
# Determine category order
if comparison_order is None:
# keep observed order
categories = list(pd.Series(df_obs_x[comparison_col]).astype('category').cat.categories) \
or list(df_obs_x[comparison_col].unique())
else:
categories = list(comparison_order)
# Build a fixed palette used for every subplot
palette = sns.color_palette('tab10', n_colors=len(categories))
color_map = dict(zip(categories, palette))
gene_list_len = len(feature_list)
fig, axes = plt.subplots(
gene_list_len, 1,
sharex=barh_sharex,
figsize=figsize,
)
if gene_list_len == 1:
axes = [axes] # make iterable
if fig_title is not None:
fig.suptitle(fig_title, fontsize=fig_title_fontsize, y=fig_title_y )
else:
fig.suptitle(f"{barh_subplot_xlabel} grouped by {comparison_col}\n", fontsize=fig_title_fontsize, y=fig_title_y)
for plot_num, gene in enumerate(feature_list):
ax = axes[plot_num]
# Horizontal bars (aggregated by category)
sns.barplot(
x=gene, y=comparison_col,
data=df_obs_x,
order=categories,
ax=ax,
hue=comparison_col, # Passing `palette` without assigning `hue` is deprecated and will be removed in v0.14.0. Assign the `y` variable to `hue` and set `legend=False` for the same effect
legend=False,
palette=[color_map[c] for c in categories]
)
if barh_remove_yticklabels:
ax.set_yticklabels([])
if include_stripplot:
# Overlay points (each sample), same order as bars
sns.stripplot(
x=gene, y=comparison_col,
data=df_obs_x,
order=categories,
ax=ax,
color='black',
legend=False
)
# set x-axis limits
if barh_set_xaxis_lims is not None:
ax.set_xlim(barh_set_xaxis_lims)
# set x-axis tic fontsize
ax.tick_params(axis='x', labelsize=tick_label_fontsize)
ax.xaxis.set_major_formatter(StrMethodFormatter('{x:g}'))
# remove xlabel for all but the last subplot
ax.set_xlabel('')
# set ylabel for each subplot using mapped feature label
_bar_feat_label = _bar_feature_label_map.get(gene, str(gene))
ax.set_ylabel(_bar_feat_label, rotation=0, fontsize=feature_label_fontsize, ha='right', va='center')
ax.yaxis.set_label_coords(feature_label_x, 0.5)
ax.tick_params(axis='y', labelsize=tick_label_fontsize)
# outside of the loop, set the xlabel for the last subplot
ax.set_xlabel(barh_subplot_xlabel, fontsize=legend_fontsize)
# Figure-level legend at bottom with the same bar colors
if barh_legend:
handles = [Patch(facecolor=color_map[c], edgecolor='none', label=str(c)) for c in categories]
fig.legend(
handles=handles,
labels=[str(c) for c in categories],
loc='lower center',
ncol=min(len(categories), 6),
title=comparison_col,
bbox_to_anchor=barh_legend_bbox_to_anchor,
fontsize=legend_fontsize,
title_fontsize=legend_fontsize,
)
# Leave space for the bottom legend
rect_used = (np.array(tight_layout_rect_arg) + np.array([0, 0.01, 0, 0])).tolist()
plt.tight_layout(rect=rect_used)
else:
plt.tight_layout(rect=tight_layout_rect_arg)
if savefig:
plt.savefig(file_name, dpi=300, bbox_inches="tight" )
print(f"Saved plot to {file_name}")
plt.show()
return fig, axes
####### START ############. l2fc_pvalue plots ###################.###################.###################.###################.
def l2fc_dotplot_single(
adata: anndata.AnnData | None = None,
var_df: pd.DataFrame | None = None,
feature_list: list[str] | None = None,
feature_label_vars_col: str | None = None,
feature_label_char_limit: int | None = 25,
figsize: tuple[int, int] = (8, 10),
fig_title: str | None = None,
fig_title_y: float = 1.02,
feature_label_fontsize: int | None = 14,
tick_label_fontsize: int | None = 12,
legend_fontsize: int | None = 14,
dotplot_pval_vars_col_label: str = 'pvalue',
dotplot_l2fc_vars_col_label: str = 'log2FoldChange',
dotplot_subplot_xlabel: str = 'log2fc ((target)/(ref))',
pval_label: str = 'p-value',
pvalue_cutoff_ring: float = 0.1,
sizes: tuple[int, int] = (20, 2000),
dotplot_set_xaxis_lims: tuple[int, int] | None = None,
dotplot_legend: bool = True,
dotplot_legend_bins: int | None = 4,
dotplot_legend_bbox_to_anchor: tuple[float, float] = (0.5, -0.05),
dotplot_annotate: bool = False,
dotplot_annotate_fontsize: int | None = None,
):
"""Single-axis l2fc dotplot with one row per feature."""
if not feature_list:
raise ValueError("feature_list must be provided and non-empty.")
_var_df = var_df.copy() if var_df is not None else (
adata.var.copy() if adata is not None else None)
if _var_df is None:
raise ValueError("Provide either `adata` or `var_df`.")
for col in (dotplot_pval_vars_col_label, dotplot_l2fc_vars_col_label):
if col not in _var_df.columns:
raise ValueError(f"Column '{col}' not found in var_df.")
missing = [f for f in feature_list if f not in _var_df.index]
if missing:
raise KeyError(f"Features not found in var_df index: {missing[:5]}" + (" ..." if len(missing) > 5 else ""))
log10pval_label = f"-log10({pval_label})"
_pvals = pd.to_numeric(_var_df[dotplot_pval_vars_col_label], errors="coerce").clip(1e-300, 1.0)
_var_df[log10pval_label] = -np.log10(_pvals)
size_metric_col = "dotplot_size_metric"
_var_df[size_metric_col] = np.where(_pvals > 0.5, 0.0, _var_df[log10pval_label])
plot_df = _var_df.loc[feature_list].copy()
if feature_label_vars_col and feature_label_vars_col in _var_df.columns:
_labels_series = _var_df[feature_label_vars_col]
lbls = _labels_series.where(_labels_series.notna(), _var_df.index.to_series()).astype(str)
else:
if feature_label_vars_col and feature_label_vars_col not in _var_df.columns:
print(f"Warning: feature_label_vars_col '{feature_label_vars_col}' not found; using index for labels.")
lbls = _var_df.index.to_series().astype(str)
if feature_label_char_limit is not None:
lbls = lbls.str.slice(0, int(feature_label_char_limit))
label_map = lbls.to_dict()
label_order = [label_map.get(f, str(f)) for f in feature_list]
plot_df["dotplot_feature_name"] = pd.Categorical(label_order, categories=label_order, ordered=True)
# explicit numeric y positions so feature_list[0] appears at the top
plot_df["dotplot_y"] = list(range(len(plot_df)))[::-1]
ring_col = "ring_cutoff"
log10_thresh = float(-np.log10(pvalue_cutoff_ring))
plot_df[ring_col] = np.round(log10_thresh, 2)
size_min = 0.0
size_max = float(pd.to_numeric(plot_df[size_metric_col], errors="coerce").replace([np.inf, -np.inf], np.nan).max())
size_max = float(max(size_max, log10_thresh, 1e-6))
cmap = plt.get_cmap("viridis_r")
norm = plt.Normalize(vmin=log10_thresh, vmax=size_max, clip=True)
l2fc_x_limit = float(plot_df[dotplot_l2fc_vars_col_label].abs().max())
fig, ax = plt.subplots(figsize=figsize)
if fig_title:
fig.suptitle(fig_title, fontsize=legend_fontsize + 2, y=fig_title_y)
sns.scatterplot(
data=plot_df,
x=dotplot_l2fc_vars_col_label,
y="dotplot_y",
size=ring_col,
size_norm=(size_min, size_max),
sizes=sizes,
facecolors="none",
edgecolors="red",
linewidths=1,
legend=False,
ax=ax,
)
sig_mask = plot_df[log10pval_label] >= log10_thresh
sns.scatterplot(
data=plot_df.loc[~sig_mask],
x=dotplot_l2fc_vars_col_label,
y="dotplot_y",
size=size_metric_col,
size_norm=(size_min, size_max),
sizes=sizes,
color="grey",
edgecolors="black",
linewidths=0.5,
legend=False,
ax=ax,
)
sns.scatterplot(
data=plot_df.loc[sig_mask],
x=dotplot_l2fc_vars_col_label,
y="dotplot_y",
size=size_metric_col,
size_norm=(size_min, size_max),
sizes=sizes,
hue=log10pval_label,
hue_norm=norm,
palette=cmap,
edgecolors="black",
linewidths=0.5,
legend=False,
ax=ax,
)
if dotplot_set_xaxis_lims is not None:
ax.set_xlim(dotplot_set_xaxis_lims)
else:
ax.set_xlim((-l2fc_x_limit * 1.05, l2fc_x_limit * 1.05))
ax.axvline(x=0, color="red", linestyle="--")
ax.set_xlabel(dotplot_subplot_xlabel, fontsize=legend_fontsize)
ax.set_ylabel("")
ax.tick_params(axis="x", labelsize=tick_label_fontsize)
ax.tick_params(axis="y", labelsize=feature_label_fontsize)
ax.xaxis.set_major_formatter(StrMethodFormatter("{x:g}"))
ax.set_yticks(plot_df["dotplot_y"])
ax.set_yticklabels(label_order)
if dotplot_annotate:
ann_fs = dotplot_annotate_fontsize or max(8, int(tick_label_fontsize))
for _, row in plot_df.iterrows():
if np.isfinite(row[dotplot_l2fc_vars_col_label]) and np.isfinite(row[dotplot_pval_vars_col_label]):
ax.text(
row[dotplot_l2fc_vars_col_label],
row["dotplot_feature_name"],
f"l2fc: {row[dotplot_l2fc_vars_col_label]:.2g} | p:{row[dotplot_pval_vars_col_label]:.2g}",
ha="left",
va="center",
fontsize=ann_fs,
)
if dotplot_legend:
from matplotlib.lines import Line2D
v_ring = float(-np.log10(pvalue_cutoff_ring))
n_bins = max(1, int(dotplot_legend_bins or 3))
edges = np.linspace(log10_thresh, size_max, n_bins + 1)[1:]
uniq_vals = sorted({round(float(u), 1) for u in edges if u > v_ring + 1e-6})
def _area(v): return float(np.interp(v, [size_min, size_max], sizes))
def _ms(v): return max(4.0, np.sqrt(_area(v)))
grey_handle = Line2D([0], [0], marker="o", linestyle="", markerfacecolor="grey",
markeredgecolor="black", markersize=_ms(max(size_min, min(v_ring - 0.01, size_max))),
label=f"< {v_ring:.1f}")
ring_handle = Line2D([0], [0], marker="o", linestyle="", markerfacecolor="none",
markeredgecolor="red", markeredgewidth=1.5, markersize=_ms(v_ring),
label=f"{v_ring:.1f} ring")
color_handles = [
Line2D([0], [0], marker="o", linestyle="", markerfacecolor=cmap(norm(u)),
markeredgecolor="black", markersize=_ms(u), label=f"{u:.1f}")
for u in uniq_vals
]
handles = [grey_handle] + color_handles + [ring_handle]
ax.legend(
handles=handles,
loc="lower center",
ncol=len(handles),
bbox_to_anchor=dotplot_legend_bbox_to_anchor,
title=log10pval_label,
fontsize=legend_fontsize,
title_fontsize=legend_fontsize,
frameon=True,
)
fig.tight_layout(rect=[0, 0.02 if dotplot_legend else 0, 1, 1])
return fig, ax
def l2fc_dotplot_column(
# shared parameters
adata: anndata.AnnData | None = None,
var_df: pd.DataFrame | None = None,
feature_list: list[str] | None = None, # index of adata.var / var_df
feature_label_vars_col: str | None = None, # if None then index is used
feature_label_char_limit: int | None = 25,
feature_label_x: float = -0.02,
figsize: tuple[int, int] | None = (8, 12),
fig_title: str | None = None,
fig_title_y: float = 1.03,
subfig_title_fontsize: int | None = 24,
feature_label_fontsize: int | None = 24,
tick_label_fontsize: int | None = 20,
legend_fontsize: int | None = 24,
tight_layout_rect_arg: list[float] | None = [0, 0, 1, 1],
savefig: bool = False,
file_name: str = 'l2fc_dotplot.png',
# dotplot specific parameters (mirrors barh_l2fc_dotplot_column)
dotplot_figure_plot_title: str | None = 'log2fc',
dotplot_pval_vars_col_label: str | None = 'pvalue',
dotplot_l2fc_vars_col_label: str | None = 'log2FoldChange',
dotplot_subplot_xlabel: str | None = 'log2fc ((target)/(ref))',
pval_label: str = 'p-value',
pvalue_cutoff_ring: float = 0.1,
sizes: tuple[int, int] | None = (20, 2000),
dotplot_sharex: bool = False,
dotplot_set_xaxis_lims: tuple[int, int] | None = None,
dotplot_legend: bool = True,
dotplot_legend_bins: int | None = 4,
dotplot_legend_bbox_to_anchor: tuple[int, int] | None = (0.5, -.005),
# Optional annotation on the dotplot with l2fc and p-value
dotplot_annotate: bool = False,
dotplot_annotate_xy: tuple[float, float] | None = (0.8, 1.2),
dotplot_annotate_fontsize: int | None = None,
):
"""
adata_science_tools.l2fc_dotplot_column()
#----------
Render a column of dot plots with log2FC on the x-axis, dot size/color encoding -log10(p), and a red ring marking the p-value cutoff.
------#
Parameters
#----------
adata : anndata.AnnData, optional
AnnData object consulted when `var_df` is omitted; `adata.var` supplies feature statistics.
var_df : pandas.DataFrame, optional
DataFrame indexed by features that provides log2FC and p-value columns required for plotting.
feature_list : list[str], optional
Ordered feature identifiers to render; every entry must exist in `var_df.index`.
feature_label_vars_col : str, optional
Column in `var_df` containing display labels for features; defaults to the feature index.
feature_label_char_limit : int, optional
Maximum length for feature labels; set `None` to disable truncation.
feature_label_x : float, optional
Axes-relative x coordinate used to place feature labels beside each subplot.
figsize : tuple[int, int], optional
Figure size in inches passed to `plt.figure`.
fig_title : str, optional
Text for an overall figure title when provided.
fig_title_y : float, optional
Normalized y-coordinate used for the figure title.
subfig_title_fontsize : int, optional
Font size applied to figure-level titles.
feature_label_fontsize : int, optional
Font size for feature labels on the y-axis.
tick_label_fontsize : int, optional
Font size used for x-axis tick labels.
legend_fontsize : int, optional
Font size applied to legend titles and entries.
tight_layout_rect_arg : list[float], optional
Rectangle passed to `plt.tight_layout` to reserve padding around the figure.
savefig : bool, optional
If `True`, save the rendered figure to the path given by `file_name`.
file_name : str, optional
Output path used when `savefig` is enabled.
dotplot_figure_plot_title : str, optional
Title displayed above the column of dot plots; overrides `fig_title` when set.
dotplot_pval_vars_col_label : str, optional
Column in `var_df` containing raw p-values used to compute -log10(p).
dotplot_l2fc_vars_col_label : str, optional
Column in `var_df` containing log2 fold-change values plotted along the x-axis.
dotplot_subplot_xlabel : str, optional
Label applied to the shared x-axis of the dot plots.
pval_label : str, optional
Friendly label propagated to the derived `-log10(p)` column and legend title.
pvalue_cutoff_ring : float, optional
P-value threshold encoded by the red outline and used as the minimum for the colormap.
sizes : tuple[int, int], optional
Minimum and maximum marker areas (points^2) passed to Seaborn scatterplots.
dotplot_sharex : bool, optional
If `True`, share the x-axis between subplots so only the final subplot shows tick labels.
dotplot_set_xaxis_lims : tuple[int, int], optional
Explicit x-axis limits; inferred from the data when `None`.
dotplot_legend : bool, optional
If `True`, draw the -log10(p) legend beneath the plots.
dotplot_legend_bins : int, optional
Number of colored legend bins for values above the p-value threshold; ignored when `None`.
dotplot_legend_bbox_to_anchor : tuple[float, float], optional
Legend anchor point in figure-relative coordinates.
dotplot_annotate : bool, optional
If `True`, annotate each subplot with the log2FC and p-value text.
dotplot_annotate_xy : tuple[float, float], optional
Axes-relative coordinates used for the optional annotation text.
dotplot_annotate_fontsize : int, optional
Font size for annotation text; defaults to `tick_label_fontsize` when `None`.
-------#
Returns
#----------
tuple[matplotlib.figure.Figure, matplotlib.axes.Axes | list[matplotlib.axes.Axes]]
Figure and axes (single Axes when exactly one feature is plotted).
-------#
Example usage
#----------
adtl.l2fc_dotplot_column(
adata=adata,
feature_list=feature_list,
feature_label_vars_col=None,
feature_label_char_limit=25,
feature_label_x=-0.02,
figsize=(15, 25),
fig_title='Features by Treatment',
fig_title_y=1.0,
subfig_title_fontsize=30,
feature_label_fontsize=24,
tick_label_fontsize=20,
legend_fontsize=20,
tight_layout_rect_arg=[0, 0.04, 1, 1],
savefig=False,
file_name='l2fc_dotplot_column.png',
dotplot_figure_plot_title='log2fc',
dotplot_pval_vars_col_label='ttest_ind_pvals_Target_Ref',#
dotplot_l2fc_vars_col_label='l2fc_Target_Ref',#
dotplot_subplot_xlabel='log2fc ((Target)/(Ref))',
pval_label='p-value',
pvalue_cutoff_ring=0.1,
sizes=(20, 2000),
dotplot_sharex=True,
dotplot_set_xaxis_lims=None,
dotplot_legend=True,
dotplot_legend_bins=4,
dotplot_legend_bbox_to_anchor=(0.5, -0.02),
dotplot_annotate=False,
dotplot_annotate_xy=(0.8, 1.2),
dotplot_annotate_fontsize=None,
)
-------#
"""
# Validate inputs and assemble var_df
if feature_list is None or len(feature_list) == 0:
raise ValueError("feature_list must be provided and non-empty.")
if var_df is not None:
_var_df = var_df.copy()
else:
if adata is None:
raise ValueError("Provide either `adata` or `var_df`.")
_var_df = adata.var.copy()
# Ensure required columns exist
for col in (dotplot_pval_vars_col_label, dotplot_l2fc_vars_col_label):
if col not in _var_df.columns:
raise ValueError(f"Column '{col}' not found in var_df.")
# Confirm features exist
missing = [f for f in feature_list if f not in _var_df.index]
if missing:
raise KeyError(f"Features not found in var_df index: {missing[:5]}" + (" ..." if len(missing) > 5 else ""))
# Prepare -log10(p) and size metric
log10pval_label = f'-log10({pval_label})'
_pvals = pd.to_numeric(_var_df[dotplot_pval_vars_col_label], errors='coerce')
_pvals = _pvals.clip(lower=1e-300, upper=1.0)
_var_df[log10pval_label] = -np.log10(_pvals)
size_metric_col = 'dotplot_size_metric'
_var_df[size_metric_col] = np.where(_pvals > 0.5, 0.0, _var_df[log10pval_label])
size_min = 0.0
_size_vals = pd.to_numeric(_var_df.loc[feature_list, size_metric_col], errors='coerce').replace([np.inf, -np.inf], np.nan)
size_max = float(_size_vals.max()) if np.isfinite(_size_vals.max()) else 0.0
# x-limits from |log2FC| for plotted features
l2fc_x_limit = _var_df.loc[feature_list, dotplot_l2fc_vars_col_label].abs().max()
# Ring cutoff in -log10 space and normalization for colors >= threshold
ring_col = 'ring_cutoff'
log10_thresh = float(-np.log10(pvalue_cutoff_ring))
_var_df[ring_col] = np.round(log10_thresh, 2)
size_max = float(max(size_max, log10_thresh, 1e-6))
_cmap = plt.get_cmap('viridis_r')
_color_norm = plt.Normalize(vmin=log10_thresh, vmax=max(size_max, log10_thresh), clip=True)
# Feature labels
if (feature_label_vars_col is not None) and (feature_label_vars_col in _var_df.columns):
_feature_label_series = _var_df[feature_label_vars_col]
_feature_label_series = _feature_label_series.where(_feature_label_series.notna(), _var_df.index.to_series()).astype(str)
else:
if feature_label_vars_col is not None and feature_label_vars_col not in _var_df.columns:
print(f"Warning: feature_label_vars_col '{feature_label_vars_col}' not found in var_df; using index for labels.")
_feature_label_series = _var_df.index.to_series().astype(str)
#if (feature_label_char_limit is not None) and (feature_label_char_limit > 0):
if (feature_label_char_limit is not None):
_feature_label_series = _feature_label_series.str.slice(0, int(feature_label_char_limit))
_var_df['dotplot_feature_name'] = _feature_label_series
_feature_label_map = _feature_label_series.astype(str).to_dict()
# Figure and axes
n = len(feature_list)
fig, axes = plt.subplots(n, 1, sharex=dotplot_sharex, figsize=figsize)
if fig_title is not None:
ft_size = subfig_title_fontsize or (legend_fontsize + 2)
fig.suptitle(fig_title, fontsize=ft_size, y=fig_title_y)
elif dotplot_figure_plot_title is not None:
ft_size = subfig_title_fontsize or (legend_fontsize + 2)
fig.suptitle(dotplot_figure_plot_title, fontsize=ft_size, y=fig_title_y)
else:
ft_size = subfig_title_fontsize or (legend_fontsize + 2)
fig.suptitle(f"{dotplot_subplot_xlabel}", fontsize=ft_size, y=fig_title_y)
# Ensure axes is iterable
if n == 1:
axes_list = [axes]
else:
axes_list = list(axes)
# Plot each feature
for plot_num, gene in enumerate(feature_list):
ax = axes_list[plot_num]
# A) Red ring at the cutoff
sns.scatterplot(
data=_var_df.loc[[gene]],
x=dotplot_l2fc_vars_col_label,
y='dotplot_feature_name',
size=ring_col,
size_norm=(size_min, size_max),
sizes=sizes,
facecolors="none",
edgecolors="red",
linewidths=1,
zorder=4,
legend=False,
ax=ax,
)
# B) Main dot colored by -log10(p) (grey if below threshold)
_val = float(_var_df.loc[gene, log10pval_label]) if gene in _var_df.index else np.nan
if np.isfinite(_val) and (_val >= log10_thresh):
_dot_color = _cmap(_color_norm(_val))
else:
_dot_color = 'grey'
sns.scatterplot(
data=_var_df.loc[[gene]],
x=dotplot_l2fc_vars_col_label,
y='dotplot_feature_name',
size=size_metric_col,
size_norm=(size_min, size_max),
sizes=sizes,
color=_dot_color,
edgecolors="black",
linewidths=.5,
zorder=3,
legend=False,
ax=ax,
)
# Optional annotation
if dotplot_annotate and (gene in _var_df.index):
try:
_l2fc_val = _var_df.loc[gene, dotplot_l2fc_vars_col_label]
_pval_val = _var_df.loc[gene, dotplot_pval_vars_col_label]
if np.isfinite(_l2fc_val) and np.isfinite(_pval_val):
_ann_text = f"l2fc: {_l2fc_val:.2g} | p:{_pval_val:.2g}"
_ann_fs = dotplot_annotate_fontsize or max(8, int(tick_label_fontsize))
_xy = dotplot_annotate_xy or (0.8, 1.2)
ax.text(_xy[0], _xy[1], _ann_text, transform=ax.transAxes,
ha='right', va='center', fontsize=_ann_fs, color='black')
except Exception as e:
print(f"Dotplot annotation failed for feature '{gene}': {e}")
# x limits and ticks
if dotplot_set_xaxis_lims is not None:
ax.set_xlim(dotplot_set_xaxis_lims)
else:
l2fc_xaxis_pad = 1.05
ax.set_xlim((-l2fc_x_limit * l2fc_xaxis_pad), (l2fc_x_limit * l2fc_xaxis_pad))
ax.tick_params(axis='x', labelsize=tick_label_fontsize)
ax.xaxis.set_major_formatter(StrMethodFormatter('{x:g}'))
ax.axvline(x=0, color="red", linestyle="--")
# X label only on the last subplot if sharing x
if dotplot_sharex and plot_num < (n - 1):
ax.set_xlabel('')
else:
ax.set_xlabel(dotplot_subplot_xlabel, fontsize=legend_fontsize)
# Remove y ticks and place feature label as y-axis label
ax.set_ylabel('')
ax.set_yticklabels([])
_feat_label = _feature_label_map.get(gene, str(gene))
ax.set_ylabel(_feat_label, rotation=0, fontsize=feature_label_fontsize, ha='right', va='center')
ax.yaxis.set_label_coords(feature_label_x, 0.5)
# Figure-level legend for -log10(p): ring + grey + colored bins
if dotplot_legend:
from matplotlib.lines import Line2D
cmap_min = float(-np.log10(pvalue_cutoff_ring))
cmap = plt.get_cmap('viridis_r')
vmin_leg = cmap_min
vmax_leg = max(size_max, cmap_min)
norm = plt.Normalize(vmin=vmin_leg, vmax=vmax_leg, clip=True)
v_ring = float(-np.log10(pvalue_cutoff_ring))
n_bins = max(1, int(dotplot_legend_bins or 3))
edges = np.linspace(vmin_leg, vmax_leg, n_bins + 1)
uppers = edges[1:]
uniq_vals, seen = [], set()
for u in uppers:
key = round(float(u), 1)
if key <= round(v_ring, 1) + 1e-6:
continue
if key in seen:
continue
seen.add(key)
uniq_vals.append(float(u))
def _area_for(v: float) -> float:
return float(np.interp(v, [size_min, size_max], sizes))
def _ms_for(v: float) -> float:
return max(4.0, np.sqrt(_area_for(v)))
handles = []
ms_ring = _ms_for(v_ring)
ring_handle = Line2D([0], [0], marker='o', linestyle='',
markerfacecolor='none', markeredgecolor='red', markeredgewidth=1.5,
markersize=ms_ring, label=f"{v_ring:.1f} ring")
v_grey = max(size_min, min(v_ring - 0.01, vmax_leg))
grey_handle = Line2D([0], [0], marker='o', linestyle='', markerfacecolor='grey',
markeredgecolor='black', markersize=_ms_for(v_grey), label=f"< {v_ring:.1f}")
for u in uniq_vals:
handles.append(
Line2D([0], [0], marker='o', linestyle='', markerfacecolor=cmap(norm(u)),
markeredgecolor='black', markersize=_ms_for(u), label=f"{round(u, 1):.1f}")
)
legend_handles = [grey_handle] + handles + [ring_handle]
#if len(legend_handles) >= 4:
# ncol = 4
#else:
# ncol = len(legend_handles) or 1
ncol=(len(legend_handles)-1) or 1
nrow = int(np.ceil(len(legend_handles) / ncol))
grid = [[None for _ in range(ncol)] for _ in range(nrow)]
for idx, handle in enumerate(legend_handles):
r = idx // ncol
c = idx % ncol
grid[r][c] = handle
legend_handles = []
for c in range(ncol):
for r in range(nrow):
h = grid[r][c]
if h is not None:
legend_handles.append(h)
legend_labels = [h.get_label() for h in legend_handles]
fig.legend(
handles=legend_handles,
labels=legend_labels,
loc='lower center',
ncol=ncol,
bbox_to_anchor=dotplot_legend_bbox_to_anchor,
title=f"{log10pval_label}",
fontsize=legend_fontsize,
title_fontsize=legend_fontsize,
frameon=True,
markerfirst=True,
handletextpad=0.6,
columnspacing=0.8,
borderaxespad=0.2,
)
# Layout with extra bottom margin if legend added
rect_used = (np.array(tight_layout_rect_arg) + np.array([0, 0.0, 0, 0])).tolist() if dotplot_legend else tight_layout_rect_arg
plt.tight_layout(rect=rect_used)
if savefig:
plt.savefig(file_name, dpi=300, bbox_inches="tight")
print(f"Saved plot to {file_name}")
plt.show()
return fig, (axes_list[0] if n == 1 else axes_list)
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import anndata # or use the quoted type hint instead
from matplotlib.patches import Patch
import numpy as np
from matplotlib.ticker import FormatStrFormatter
from matplotlib.ticker import StrMethodFormatter
import anndata
def barh_l2fc_dotplot_column(
# shared parameters
adata: anndata.AnnData | None = None,
layer: str | None = None,
x_df: pd.DataFrame | None = None,
var_df: pd.DataFrame | None = None,
obs_df: pd.DataFrame | None = None,
feature_list: list[str] | None = None, # index of adata
feature_label_vars_col: str | None = None, # if None than adata index used to label
feature_label_char_limit: int | None = 40,
feature_label_x: float = -0.02,
figsize: tuple[int, int]| None = (10, 15),
fig_title: str | None = None,
fig_title_y: float = 1.03,
subfig_title_y: float = 99,
fig_title_fontsize: int | None = 30,
subfig_title_fontsize: int | None = 24,
feature_label_fontsize: int | None= 24,
tick_label_fontsize: int | None= 20,
legend_fontsize: int | None= 24,
row_hspace: float | None = None,
col_wspace: float | None = 0.07,
bar2dotplot_width_ratios: list[float] | None = [1.5, 1.],
tight_layout_rect_arg: list[float] | None = [0, 0, 1, 1],
use_tight_layout: bool = True,
savefig: bool = False,
file_name: str = 'test_plot.png',
# barh specific parameters
comparison_col: str | None = 'Treatment',
comparison_order: list[str] | None = None,
hue_palette_color_list: list[str] | None = None,
barh_remove_yticklabels: bool = True,
barh_figure_plot_title: str | None = f'Expression (TPM)',
barh_subplot_xlabel: str | None = 'Expression (TPM)',
barh_sharex: bool = False,
barh_set_xaxis_lims: tuple[int, int]| None = None,
barh_legend: bool = True,
barh_legend_bbox_to_anchor: tuple[int, int] | None = (0.5, -.05),
# dotplot specific parameters
dotplot_figure_plot_title: str | None = 'log2fc',
dotplot_pval_vars_col_label: str | None = 'pvalue',
dotplot_l2fc_vars_col_label: str | None ='log2FoldChange',
dotplot_subplot_xlabel: str | None = 'log2fc ((target)/(ref))',
pval_label: str = 'p-value',
l2fc_label: str = 'log2FoldChange',
pvalue_cutoff_ring: float = 0.1,
sizes: tuple[int, int] | None = (20, 2000),
dotplot_sharex: bool = False,
dotplot_set_xaxis_lims: tuple[int, int]| None = None,
dotplot_legend: bool = True,
dotplot_legend_bins: int | None = 4,
dotplot_legend_bbox_to_anchor: tuple[int, int] | None = (0.5, -.05),
# Optional annotation on the dotplot with l2fc and p-value
dotplot_annotate: bool = False,
dotplot_annotate_xy: tuple[float, float] | None = (0.8, 1.2),
dotplot_annotate_labels: tuple[str, str] | None = ('l2fc: ', 'p:'),
dotplot_annotate_fontsize: int | None = None,
#
):
"""
adata_science_tools.barh_l2fc_dotplot_column()
#----------
Compose paired horizontal bar plots and log2FC dot plots for each feature, sharing labels and legend styling across the column.
------#
Parameters
#----------
adata : anndata.AnnData | None, optional
AnnData object consulted when explicit data frames are not supplied.
layer : str | None, optional
Name of an `adata.layers` matrix to use for expression values instead of `adata.X`.
x_df : pandas.DataFrame | None, optional
Observation-by-feature expression matrix supplied directly; takes precedence over `adata` sources.
var_df : pandas.DataFrame | None, optional
DataFrame of feature metadata; defaults to `adata.var` when `None`.
obs_df : pandas.DataFrame | None, optional
DataFrame of observation metadata; defaults to `adata.obs` when `None`.
feature_list : list[str] | None, optional
Ordered feature identifiers to display; entries must exist in `var_df.index`.
feature_label_vars_col : str | None, optional
Column in `var_df` containing display labels for features; defaults to the feature index.
feature_label_char_limit : int | None, optional
Maximum number of characters retained for feature labels; set `None` to disable truncation.
feature_label_x : float, optional
Axes-relative x coordinate used to position feature labels beside each subplot.
figsize : tuple[int, int] | None, optional
Overall figure size in inches passed to `plt.figure`.
fig_title : str | None, optional
Title rendered above both bar and dot plot columns when provided.
fig_title_y : float, optional
Normalized y-position for the figure title.
subfig_title_y : float, optional
Normalized y-position for subfigure titles.
fig_title_fontsize : int | None, optional
Font size applied to the figure title.
subfig_title_fontsize : int | None, optional
Font size used for the bar and dot subfigure titles.
feature_label_fontsize : int | None, optional
Font size for feature labels on the y-axis.
tick_label_fontsize : int | None, optional
Font size used for axis tick labels.
legend_fontsize : int | None, optional
Font size applied to legend titles and entries.
bar2dotplot_width_ratios : list[float] | None, optional
Relative width ratios for the bar plot column versus the dot plot column.
tight_layout_rect_arg : list[float] | None, optional