Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom-height letters #361

Open
gezmi opened this issue Nov 23, 2021 · 1 comment
Open

Custom-height letters #361

gezmi opened this issue Nov 23, 2021 · 1 comment

Comments

@gezmi
Copy link

gezmi commented Nov 23, 2021

Hi!

This is a great package and I would definitely use it a lot. I have a question: is there any way to provide custom-height metric to sequence logos? Like in the ggseqlogo package in R, where the letters can even have negative height for depletion: https://omarwagih.github.io/ggseqlogo/#custom-height_logos

Thank you!

@padix-key
Copy link
Member

padix-key commented Nov 23, 2021

Hi, unfortunately there is currently no way to provide custom heights to symbols in a sequence logo.
The only way is modifying the symbols attribute in the input SequenceProfile, but this does not allow for negative heights.

However, the source code of the actual plotting is quite short, if you are willing to implement the use case yourself:

import numpy as np
from ...visualize import set_font_size_in_coord
from ..alphabet import LetterAlphabet
from .colorschemes import get_color_scheme
import warnings
from ..align import Alignment
from .. import SequenceProfile
def plot_sequence_logo(axes, profile, scheme=None, **kwargs):
"""
Create a sequence logo. :footcite:`Schneider1990`
A sequence logo is visualizes the positional composition and
conservation of a profile encoded in the size of the letters.
Each position displays all symbols that are occurring at this
position stacked on each other, with their relative heights depicting
their relative frequency.
The height of such a stack depicts its conservation.
It is the maximum possible Shannon entropy of the alphabet
subtracted by the positional entropy.
Parameters
----------
axes : Axes
The axes to draw the logo one.
profile: SequenceProfile
The logo is created based on this profile.
scheme : str or list of (tuple or str)
Either a valid color scheme name
(e.g. ``"rainbow"``, ``"clustalx"``, ``blossom``, etc.)
or a list of *Matplotlib* compatible colors.
The list length must be at least as long as the
length of the alphabet used by the `profile`.
**kwargs
Additional `text parameters <https://matplotlib.org/api/text_api.html#matplotlib.text.Text>`_.
References
----------
.. footbibliography::
"""
from matplotlib.text import Text
if isinstance(profile, Alignment):
warnings.warn("Using an alignment for this method is deprecated; use a profile instead", DeprecationWarning)
profile = SequenceProfile.from_alignment(profile)
alphabet = profile.alphabet
if not isinstance(alphabet, LetterAlphabet):
raise TypeError("The sequences' alphabet must be a letter alphabet")
if scheme is None:
colors = get_color_scheme("rainbow", alphabet)
elif isinstance(scheme, str):
colors = get_color_scheme(scheme, alphabet)
else:
colors = scheme
# 'color' and 'size' property is not passed on to text
kwargs.pop("color", None)
kwargs.pop("size", None)
frequencies, entropies, max_entropy = _get_entropy(profile)
stack_heights = (max_entropy - entropies)
symbols_heights = stack_heights[:, np.newaxis] * frequencies
index_order = np.argsort(symbols_heights, axis=1)
for i in range(symbols_heights.shape[0]):
# Iterate over the alignment columns
index_order = np.argsort(symbols_heights)
start_height = 0
for j in index_order[i]:
# Stack the symbols at position on top of the preceeding one
height = symbols_heights[i,j]
if height > 0:
symbol = alphabet.decode(j)
text = axes.text(
i+0.5, start_height, symbol,
ha="left", va="bottom", color=colors[j],
# Best results are obtained with this font size
size=1,
**kwargs
)
text.set_clip_on(True)
set_font_size_in_coord(text, width=1, height=height)
start_height += height
axes.set_xlim(0.5, len(profile.symbols)+0.5)
axes.set_ylim(0, max_entropy)

symbol_heights and start_height are the variables you are looking for.

I will keep this issue open, since I think custom heights are a useful feature to have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants