gecrooks / weblogo

WebLogo 3: Sequence Logos redrawn
weblogo.threeplusone.com
Other
146 stars 39 forks source link

Allow arbitrary y-axis scaling #108

Closed agartland closed 4 years ago

agartland commented 4 years ago

I've been experimenting with different statistics for computing symbol heights for sequence logos. It would be great if I could make logo plots of matrices that contain arbitrary heights for each symbol. Is there currently a way to do this? Would it be difficult to expose this part of the API and make it generalized? I could try to help or do it myself, but I'm not sure where to start or how to implement. Thanks! Andrew

gecrooks commented 4 years ago

You'd have to use the python API. You can build your own LogoData object (defined in logo.py), and the 'entropy' of each stack to whatever height you want. The basic usage can be seen in the command line client, _cli.py, lines 78-85. You build a LogoData object to hold the data, LogoFormat object for the display options, then feed both to a formatter function to generate the actual image.

agartland commented 4 years ago

Thanks for the tip. It seemed to be a bit more complicated than that, so posting some code here for others. In the example motif is a pandas.DataFrame with logo positions across the columns, a row index containing the alphabet symbols, and each value containing an arbitrary float value that I wanted to be the height of each symbol.


alphabet = weblogo.seq.Alphabet(''.join(motif.index))
hydrophobicity = ColorScheme([SymbolColor("RKDENQ", "blue", "hydrophilic"),
                              SymbolColor("SGHTAP", "green", "neutral"),
                              SymbolColor("YVMCLFIW", "black", "hydrophobic"),
                              SymbolColor("-*ZX", "gray", "default")],
                             alphabet=alphabet)

logodata = weblogo.LogoData(length=motif.shape[1],
                            alphabet=alphabet,
                            entropy=motif.sum(axis=0),
                            weight=np.ones(motif.shape[1]),
                            counts=motif.values.T)

logooptions = weblogo.LogoOptions(  resolution=200,
                                    show_fineprint=False,
                                    yaxis_scale=1.1*np.max(motif.values),
                                    color_scheme=hydrophobicity,
                                    unit_name='nats',
                                    yaxis_label='bits')
logoformat = weblogo.LogoFormat(logodata, logooptions)
logobytes = weblogo.logo_formatter.png_formatter(logodata, logoformat)
IPython.display.Image(data=logobytes)
gecrooks commented 4 years ago

Thanks.