%load_ext autoreload
%autoreload 2
from plotnine import ggplot,geom_rect, geom_point, aes, stat_smooth,geom_bar, xlim, ylim, facet_wrap, theme_bw,theme_xkcd, geom_line, geom_tile
from plotnine import scale_y_continuous,scale_x_continuous, theme_bw,theme_classic, theme_dark, theme_light, theme_matplotlib, theme_minimal, theme_seaborn, theme_void
import plotnine as pn
from plotnine.data import mtcars
import pandas as pd
import numpy as np
from seqplot import p9tools
from seqplot.p9tools import geom_seq_x
from pytexshade import ipyshade
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Align import MultipleSeqAlignment
#Prepare a Multiple Sequence Alignment in biopython
human_h2a_z_core=Seq('SRSQRAGLQFPVGRIHRHLKSRTTSHGRVGATAAVYSAAILEYLTAEVLELAGNASKDLKVKRITPRHLQLAIRGDEELDSLI-KATIAGGGVIPHIHKSLIG')
xenopus_h2a_core=Seq('TRSSRAGLQFPVGRVHRLLRKGNYAE-RVGAGAPVYLAAVLEYLTAEILELAGNAARDNKKTRIIPRHLQLAVRNDEELNKLLGRVTIAQGGVLPNIQSVLLP')
msa=MultipleSeqAlignment([SeqRecord(xenopus_h2a_core,id='H2A',name='H2A'),SeqRecord(human_h2a_z_core,id='H2AZ',name='H2AZ')])
#Shade
s=ipyshade.shadedmsa4plot(msa[:,0:10],density=150,ruler=False)
s
df=pd.DataFrame({'x':np.arange(10),'yy':np.abs(np.sin(np.arange(10,20)/10.))})
g=(ggplot(data=df,mapping=aes(x='x', y='yy'))
+ geom_point(size=0.1)+geom_bar(stat='identity')+scale_x_continuous(limits=(-0.5,9.5),expand=(0,0.05),breaks=np.arange(-0.5,10.0,0.5))
+ geom_seq_x(seqimg=s.img,xlim=(0,9),ylim=(0,1),aspect_ratio=0.1)
+ theme_light()+pn.theme(aspect_ratio=0.1,dpi=300,plot_margin=0))
g
<ggplot: (-9223363278996355012)>
g.save('tmp/test.png')
/opt/miniconda3/envs/moldyn/lib/python3.7/site-packages/plotnine/ggplot.py:729: PlotnineWarning: Saving 6.4 x 4.8 in image. from_inches(height, units), units), PlotnineWarning) /opt/miniconda3/envs/moldyn/lib/python3.7/site-packages/plotnine/ggplot.py:730: PlotnineWarning: Filename: tmp/test.png warn('Filename: {}'.format(filename), PlotnineWarning)
from plotnine.utils import resolution
from plotnine.doctools import document
from plotnine.geoms.geom_tile import geom_tile
import matplotlib.image as mpimg
import io
import matplotlib.pyplot as plt
d=mpimg.imread(io.BytesIO(s.img))
plt.imshow(d)
<matplotlib.image.AxesImage at 0x7f0a6236f630>
d.shape
(64, 1416, 4)
from matplotlib.colors import to_hex
pict=pd.DataFrame([(i[1],-i[0],to_hex(d[i])) for i in np.ndindex(d.shape[0],d.shape[1])],columns=['x','y','c'])
pict
x | y | c | |
---|---|---|---|
0 | 0 | 0 | #ffffff |
1 | 1 | 0 | #ffffff |
2 | 2 | 0 | #ffffff |
3 | 3 | 0 | #ffffff |
4 | 4 | 0 | #ffffff |
... | ... | ... | ... |
90619 | 1411 | -63 | #ffffff |
90620 | 1412 | -63 | #ffffff |
90621 | 1413 | -63 | #ffffff |
90622 | 1414 | -63 | #ffffff |
90623 | 1415 | -63 | #ffffff |
90624 rows × 3 columns
data.y.max()-data.y.min()
0.9999232575641008
dy=(data.y.max()-data.y.min())
f=64./1416.
a=0.2
dh=dy*f/(a-f)
(ggplot(data=df,mapping=aes(x='x', y='y'))
+ geom_point(size=0.1)+geom_bar(stat='identity')#+scale_x_continuous(limits=(-0.5,102.5),expand=(0,0))
#+scale_y_continuous(expand=(0,0))
+ geom_tile(data=pict,mapping=aes(x='x*103./1416.-0.5', y='y/64.*dh'),fill=pict['c'])
+ theme_light()+pn.theme(aspect_ratio=a,dpi=300,plot_margin=0))
<ggplot: (8730006475240)>