AAlogoPlot.single_logo
- AAlogoPlot.single_logo(df_logo=None, df_logo_info=None, aal_kws=None, df_parts=None, labels=None, label_test=1, tmd_len=None, info_bar_color='gray', info_bar_ylim=None, target_p1_site=None, figsize=(8, 4), height_ratio=(1, 6), fontsize_labels=None, name_data=None, name_data_pos='top', name_data_color='black', name_data_fontsize=None, logo_font_name='Verdana', logo_color_scheme='weblogo_protein', logo_stack_order='big_on_top', logo_width=0.96, logo_vpad=0.05, logo_vsep=0.0, start=1, tmd_color='mediumspringgreen', jmd_color='blue', fontsize_tmd_jmd=None, weight_tmd_jmd='normal', highlight_tmd_area=True, highlight_alpha=0.15, xtick_size=None, xtick_width=2.0, xtick_length=11.0)[source]
Plot a single sequence logo with optional bit-score bar and target middle domain (TMD) / juxta middle domain (JMD) annotations.
Renders a pre-computed logo matrix from
AAlogoas a letter-stack sequence logo using logomaker [Tareen20], and draws colored TMD/JMD part annotations beneath the x-axis. An optional bit-score bar panel can be shown above the logo whendf_logo_infois provided. SeeAAlogoPlot.multi_logo()for stacking multiple logos for group comparison.Added in version 1.1.0.
- Parameters:
df_logo (pd.DataFrame, shape (n_positions, n_amino_acids), optional) – Logo matrix as returned by
AAlogo.get_df_logo(). Rows are residue positions, columns are amino acids. Required unlessaal_kwsis given, in which case it is computed internally and must beNone.df_logo_info (pd.Series, shape (n_positions,), optional) – Per-position information content as returned by
AAlogo.get_df_logo_info(). If provided, a bit-score bar is rendered above the main logo. Must beNonewhenaal_kwsis given (it is then computed internally and the bar is shown).aal_kws (dict, optional) –
AAlogo.get_df_logo()/AAlogo.get_df_logo_info()keyword arguments. If given,df_logoanddf_logo_infoare computed internally and both must beNone. Mutually exclusive withdf_logoanddf_logo_info(see Notes).df_parts (pd.DataFrame, shape (n_samples, n_parts), optional) – Sequence parts DataFrame with at least one of the standard part columns (
jmd_n,tmd,jmd_c), as passed toAAlogo.get_df_logo(). If given,df_logoanddf_logo_infoare computed internally (so the bit-score bar is shown). Mutually exclusive withdf_logo/df_logo_infoand withaal_kws(see Notes).labels (array-like, shape (n_samples,), optional) – Class labels for the samples in
df_parts. If provided, only samples withlabel_testare included in the logo computation. Only used together withdf_parts.label_test (int, default=1) – Class label of the test group to select from
labels. Only used together withdf_parts.tmd_len (int, optional) – Fixed length (>=1) to align all target middle domain (TMD) sequences before computing the logo. If
None, the maximum TMD length indf_partsis used. Only used together withdf_parts.info_bar_color (str, default='gray') – Color of the bit-score bars in the optional top panel.
info_bar_ylim (tuple of float, optional) – Y-axis limits
(min, max)for the bit-score bar. IfNone, set automatically.target_p1_site (int, optional) – If set, replaces the standard JMD/TMD x-axis with P-site notation (P1, P2, …, P1’, P2’, …) anchored at this position index.
figsize (tuple of (int or float), default=(8, 4)) – Figure size
(width, height)in inches.height_ratio (tuple of (int or float), default=(1, 6)) – Height ratio
(info_bar, logo)whendf_logo_infois provided.fontsize_labels (int or float, optional) – Font size for axis labels. If
None, uses the package default.name_data (str, optional) – Dataset name to annotate on the plot.
name_data_pos ({'top', 'right', 'bottom', 'left'}, default='top') – Position of the
name_dataannotation.name_data_color (str, default='black') – Color of the
name_dataannotation.name_data_fontsize (int or float, optional) – Font size of the
name_dataannotation.logo_font_name (str, default='Verdana') – Font name for amino acid letter rendering.
logo_color_scheme (str, default='weblogo_protein') – Color scheme for amino acid letters (passed to
logomaker).logo_stack_order ({'big_on_top', 'small_on_top', 'fixed'}, default='big_on_top') – Stacking order of letters within each position column.
logo_width (float, default=0.96) – Relative width of each letter column (0 to 1).
logo_vpad (float, default=0.05) – Vertical padding between stacked letters.
logo_vsep (float, default=0.0) – Vertical separation between stacked letters.
start (int, default=1) – Residue number assigned to the first position of JMD-N.
tmd_color (str, default='mediumspringgreen') – Color for TMD x-tick marks, part bar, and highlight.
jmd_color (str, default='blue') – Color for JMD x-tick marks and part bar.
fontsize_tmd_jmd (int or float, optional) – Font size for the ‘JMD-N’, ‘TMD’, ‘JMD-C’ part labels. If
None, auto-sized.weight_tmd_jmd ({'normal', 'bold'}, default='normal') – Font weight for the ‘JMD-N’, ‘TMD’, ‘JMD-C’ part labels.
highlight_tmd_area (bool, default=True) – If
True, shades the TMD region with a semi-transparent rectangle.highlight_alpha (float, default=0.15) – Opacity of the TMD highlight (0 = fully transparent, 1 = opaque).
xtick_size (int or float, optional) – Font size for x-tick labels. If
None, uses the package default.xtick_width (int or float, default=2.0) – Line width of x-tick marks.
xtick_length (int or float, default=11.0) – Length of x-tick marks in points.
- Returns:
fig (Figure) – Figure object.
ax (Axes or tuple of (Axes, Axes)) – When
df_logo_infoisNone: a singleAxesfor the logo panel. Whendf_logo_infois provided: a tuple(ax_logo, ax_info)whereax_infois the bit-score bar above the logo.
Notes
There are three ways to supply the logo data, and exactly one must be used:
precomputed
df_logo(optionally withdf_logo_info);the raw inputs
df_parts(withlabels,label_test,tmd_len);an
aal_kwsdict bundling theAAlogogetter arguments.
Mixing sources (e.g.
df_partstogether withdf_logooraal_kws) raisesValueError.df_partsis the most direct shortcut: when given (anddf_logoisNone),AAlogoPlotinstantiatesAAlogowith this plot’slogo_typeand computes bothdf_logo(viaAAlogo.get_df_logo()) anddf_logo_info(viaAAlogo.get_df_logo_info()) fromdf_parts/labels/label_test/tmd_len, then renders the logo with the bit-score bar.aal_kwsis the equivalent shortcut as a single dict, useful when the getter arguments are assembled programmatically. It holds the arguments shared by both methods, e.g.df_parts,labels,label_test,tmd_len,start_n,characters_to_ignore, andpseudocount. Unknown keys raiseValueError. Example:aal_kws=dict(df_parts=df_parts, labels=labels, label_test=1, tmd_len=20).
See also
AAlogoPlot.multi_logo(): for stacked multi-group comparison.AAlogo: to computedf_logoanddf_logo_info.
Examples
AAlogoPlot.single_logorenders a single sequence logo with optional TMD/JMD part annotations. Whendf_logo_infois provided, a bit-score bar is added above the logo in a second panel.import warnings warnings.filterwarnings('ignore') import aaanalysis as aa import matplotlib.pyplot as plt aa.plot_settings() sf = aa.SequenceFeature() df_seq = aa.load_dataset(name='DOM_GSEC', n=100) labels = df_seq['label'].to_list() df_parts = sf.get_df_parts(df_seq=df_seq, list_parts=['jmd_n', 'tmd', 'jmd_c']) aal = aa.AAlogo(logo_type='probability') df_logo = aal.get_df_logo(df_parts=df_parts, labels=labels, label_test=1) df_logo_info = aal.get_df_logo_info(df_parts=df_parts, labels=labels, label_test=1) aal_plot = aa.AAlogoPlot(logo_type='probability', jmd_n_len=10, jmd_c_len=10) print(f"df_logo shape: {df_logo.shape}, positions = {len(df_logo)}")
df_logo shape: (43, 20), positions = 43
The logo matrix returned by
AAlogo.get_df_logo. Rows are residue positions, columns are amino acids.tmd_lenis derived aslen(df_logo) - jmd_n_len - jmd_c_lenusing the lengths set at initialization.fig, ax = aal_plot.single_logo(df_logo=df_logo) plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m
When provided, a second panel is added above the logo showing per-position information content as a bar chart (y-axis label: ‘Bits’). The return value changes from a single
Axesto a tuple(ax_logo, ax_info).# Without df_logo_info: returns single Axes fig, ax = aal_plot.single_logo(df_logo=df_logo) print(f"Without df_logo_info: axes type = {type(ax).__name__}") plt.close() # With df_logo_info: returns tuple (ax_logo, ax_info) fig, axes = aal_plot.single_logo(df_logo=df_logo, df_logo_info=df_logo_info) ax_logo, ax_info = axes print(f"With df_logo_info: axes type = {type(axes).__name__}, " f"ax_logo ylabel='{ax_logo.get_ylabel()}', ax_info ylabel='{ax_info.get_ylabel()}'") plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m Without df_logo_info: axes type = Axes [94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m With df_logo_info: axes type = tuple, ax_logo ylabel='Probability [%]', ax_info ylabel='Bits'
aal_kwsis a convenience shortcut that skips the manualAAlogostep. Instead of building anAAlogoand callingget_df_logo/get_df_logo_infoyourself, pass their shared arguments (df_parts,labels,label_test,tmd_len,start_n,characters_to_ignore,pseudocount) as a single dict.AAlogoPlotthen instantiatesAAlogointernally (using this plot’slogo_type), computes bothdf_logoanddf_logo_info, and renders the logo together with the bit-score bar. It is mutually exclusive withdf_logo/df_logo_info.# One-shot: skip the manual AAlogo step -- aal_kws computes df_logo + df_logo_info internally aal_plot = aa.AAlogoPlot(logo_type='probability', jmd_n_len=10, jmd_c_len=10) fig, axes = aal_plot.single_logo( aal_kws=dict(df_parts=df_parts, labels=labels, label_test=1), name_data='DOM_GSEC positive', ) print(f"Returned axes type = {type(axes).__name__} (logo panel + info bar)") plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m Returned axes type = tuple (logo panel + info bar)
The most direct shortcut: pass the raw inputs instead of precomputed frames. When
df_partsis given (anddf_logoisNone),AAlogoPlotinstantiatesAAlogointernally (using this plot’slogo_type) and computes bothdf_logoanddf_logo_infofromdf_parts/labels/label_test/tmd_len, then renders the logo together with the bit-score bar. This is equivalent to theaal_kwsdict form and is mutually exclusive withdf_logo/df_logo_infoand withaal_kws. The result is identical to the manual two-getter path.# Direct inputs: skip BOTH the manual AAlogo step and the aal_kws dict aal_plot = aa.AAlogoPlot(logo_type='probability', jmd_n_len=10, jmd_c_len=10) fig, axes = aal_plot.single_logo( df_parts=df_parts, labels=labels, label_test=1, name_data='DOM_GSEC positive', ) print(f"Returned axes type = {type(axes).__name__} (logo panel + info bar)") # Internally this recomputes the very same frames as the manual two-getter path df_logo_direct = aa.AAlogo(logo_type='probability').get_df_logo( df_parts=df_parts, labels=labels, label_test=1) print(f"df_logo recomputed identically: {df_logo_direct.equals(df_logo)}") plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m Returned axes type = tuple (logo panel + info bar) df_logo recomputed identically: True
info_bar_colorsets the bar color in the top panel.info_bar_ylimfixes the y-axis of the top panel to(min, max)— useful for comparing multiple plots on the same scale.fig, axes = aal_plot.single_logo( df_logo=df_logo, df_logo_info=df_logo_info, info_bar_color='steelblue', info_bar_ylim=(0, 2.0), # fix y-axis of the top panel ) ax_logo, ax_info = axes print(f"ax_info ylim: {ax_info.get_ylim()}") plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m ax_info ylim: (np.float64(0.0), np.float64(2.0))
Controls the relative height of
(info_bar_panel, logo_panel)whendf_logo_infois provided. Default is(1, 6)— logo is 6× taller than the bar.for ratio in [(1, 6), (1, 1)]: fig, axes = aal_plot.single_logo( df_logo=df_logo, df_logo_info=df_logo_info, height_ratio=ratio, ) ax_logo, ax_info = axes h_logo = ax_logo.get_position().height h_info = ax_info.get_position().height print(f"height_ratio={ratio}: logo/bar height ratio = {h_logo/h_info:.1f}") plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m height_ratio=(1, 6): logo/bar height ratio = 6.0
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m height_ratio=(1, 1): logo/bar height ratio = 1.0
name_dataannotates the plot with a text label.Routing rule: when
df_logo_infois provided andname_data_pos='top', the label is placed onax_info(the top panel). For all other positions, it is placed onax_logo.# With df_logo_info: 'top' goes to ax_info, other positions go to ax_logo for pos in ['top', 'right', 'left', 'bottom']: fig, axes = aal_plot.single_logo( df_logo=df_logo, df_logo_info=df_logo_info, name_data='DOM_GSEC positive', name_data_pos=pos, ) ax_logo, ax_info = axes expected_ax = 'ax_info' if pos == 'top' else 'ax_logo' print(f"name_data_pos='{pos}': label placed on {expected_ax}") plt.close() # Show the 'top' case fig, axes = aal_plot.single_logo( df_logo=df_logo, df_logo_info=df_logo_info, name_data='DOM_GSEC positive', name_data_pos='top', ) plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m name_data_pos='top': label placed on ax_info [94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m name_data_pos='right': label placed on ax_logo [94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m name_data_pos='left': label placed on ax_logo [94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m name_data_pos='bottom': label placed on ax_logo [94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m
logo_typeat init controls the y-axis label only. For'probability', the backend also scales values ×100 and fixes yticks to[0, 25, 50, 75, 100]. The inputdf_logois not mutated — scaling is applied to an internal copy.max_before = df_logo.values.max() for logo_type in ['probability', 'counts', 'weight', 'information']: df_logo_t = aa.AAlogo(logo_type=logo_type).get_df_logo( df_parts=df_parts, labels=labels, label_test=1) aal_plot_t = aa.AAlogoPlot(logo_type=logo_type, jmd_n_len=10, jmd_c_len=10) fig, ax = aal_plot_t.single_logo(df_logo=df_logo_t) print(f"logo_type='{logo_type}': ylabel='{ax.get_ylabel()}'") plt.close() # Confirm df_logo not mutated by any aal_plot_t call assert df_logo.values.max() == max_before, "df_logo was mutated!" print(f"df_logo.max() unchanged after all aal_plot_t calls: {max_before:.4f}")
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m logo_type='probability': ylabel='Probability [%]' [94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m logo_type='counts': ylabel='Counts' [94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m logo_type='weight': ylabel='Weight' [94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m logo_type='information': ylabel='Bits' df_logo.max() unchanged after all aal_plot_t calls: 0.4444
These derive
tmd_len = len(df_logo) - jmd_n_len - jmd_c_len, which sets the extent of the TMD region in the part bar, x-tick coloring, and highlight. Heredf_logohas 40 positions (10 + 20 + 10).# Same df_logo, different jmd lengths -> different TMD region highlighted for jmd_n, jmd_c in [(10, 10), (5, 5), (0, 0)]: aal_plot_t = aa.AAlogoPlot(logo_type='probability', jmd_n_len=jmd_n, jmd_c_len=jmd_c) tmd_len = len(df_logo) - jmd_n - jmd_c fig, ax = aal_plot_t.single_logo(df_logo=df_logo) fig.suptitle(f"jmd_n_len={jmd_n}, jmd_c_len={jmd_c} → tmd_len={tmd_len}", y=1.01) plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m
[94mPlotting single logo (TMD length=33, JMD-N=5, JMD-C=5)[0m
[94mPlotting single logo (TMD length=43, JMD-N=0, JMD-C=0)[0m
Set the color of the part bar, x-tick marks, and TMD highlight rectangle.
fig, ax = aal_plot.single_logo( df_logo=df_logo, tmd_color='darkorange', jmd_color='steelblue', ) plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m
highlight_tmd_area=True(default) draws a shaded rectangle over the TMD region.highlight_alphasets its opacity.for alpha in [0.0, 0.15, 0.5]: fig, ax = aal_plot.single_logo( df_logo=df_logo, highlight_tmd_area=alpha > 0, highlight_alpha=alpha if alpha > 0 else 0.15, ) fig.suptitle(f"highlight_tmd_area={alpha > 0}, highlight_alpha={alpha}", y=1.01) plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m
Shifts the x-tick label numbers — the first position of JMD-N gets this number. Does not move the highlight or part bar.
for start in [1, 10]: fig, ax = aal_plot.single_logo(df_logo=df_logo, start=start) xtick_labels = [t.get_text() for t in ax.get_xticklabels() if t.get_text()] print(f"start={start}: first x-tick label = '{xtick_labels[0]}'") plt.close()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m start=1: first x-tick label = '1' [94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m start=10: first x-tick label = '10'
Control the font weight and size of the ‘JMD-N’, ‘TMD’, ‘JMD-C’ text labels drawn below the x-axis.
fig, ax = aal_plot.single_logo( df_logo=df_logo, weight_tmd_jmd='bold', fontsize_tmd_jmd=14, ) plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m
When set, replaces the entire TMD/JMD x-axis with P-site notation (P2, P1, P1’, P2’, …). The value is the 0-based position index of the P1 site. All of
start,fontsize_tmd_jmd,weight_tmd_jmd,xtick_width,xtick_lengthare ignored whentarget_p1_siteis set.# P1 at position index 10 (JMD-N/TMD boundary in a 10+20+10 layout) fig, ax = aal_plot.single_logo(df_logo=df_logo, target_p1_site=10, figsize=(10,4), xtick_size=7) xtick_labels = [t.get_text() for t in ax.get_xticklabels()] print(f"x-tick labels: {xtick_labels[:6]} ...") plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m x-tick labels: ['P10', 'P9', 'P8', 'P7', 'P6', 'P5'] ...
Sets the
(width, height)of the whole figure in inches. Whendf_logo_infois provided, both panels share this total height, split byheight_ratio.for figsize in [(8, 3), (14, 5)]: fig, ax = aal_plot.single_logo(df_logo=df_logo, figsize=figsize) actual = tuple(round(x, 1) for x in fig.get_size_inches()) print(f"figsize={figsize}: actual fig size = {actual}") plt.close()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m figsize=(8, 3): actual fig size = (np.float64(8.0), np.float64(3.0)) [94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m figsize=(14, 5): actual fig size = (np.float64(14.0), np.float64(5.0))
logo_color_schemeis passed directly tologomaker.Logo.logo_stack_ordercontrols letter stacking:'big_on_top'(default),'small_on_top', or'fixed'.for scheme, order in [('weblogo_protein', 'big_on_top'), ('chemistry', 'small_on_top')]: fig, ax = aal_plot.single_logo( df_logo=df_logo, logo_color_scheme=scheme, logo_stack_order=order, ) fig.suptitle(f"color_scheme='{scheme}', stack_order='{order}'", y=1.01) plt.show()
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m
[94mPlotting single logo (TMD length=23, JMD-N=10, JMD-C=10)[0m
Further parameters.
AAlogoPlot.single_logoalso accepts:fontsize_labels— Font size for axis labels;name_data_color— Color of thename_dataannotation;name_data_fontsize— Font size of thename_dataannotation;logo_font_name— Font name for amino acid letter rendering;logo_width— Relative width of each letter column (0 to 1);logo_vpad— Vertical padding between stacked letters;logo_vsep— Vertical separation between stacked letters.