#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('load_ext', 'autoreload') get_ipython().run_line_magic('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 # In[2]: from pytexshade import ipyshade from Bio.Seq import Seq from Bio.SeqRecord import SeqRecord from Bio.Align import MultipleSeqAlignment # In[3]: #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')]) # In[47]: #Shade s=ipyshade.shadedmsa4plot(msa[:,0:10],density=150,ruler=False) # In[48]: s # In[49]: df=pd.DataFrame({'x':np.arange(10),'yy':np.abs(np.sin(np.arange(10,20)/10.))}) # In[55]: 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 # In[41]: g.save('tmp/test.png') # In[69]: from plotnine.utils import resolution from plotnine.doctools import document # In[75]: from plotnine.geoms.geom_tile import geom_tile # In[ ]: # In[14]: import matplotlib.image as mpimg import io import matplotlib.pyplot as plt # In[15]: d=mpimg.imread(io.BytesIO(s.img)) plt.imshow(d) # In[16]: d.shape # In[17]: from matplotlib.colors import to_hex # In[18]: 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']) # In[36]: pict # In[30]: data.y.max()-data.y.min() # In[55]: dy=(data.y.max()-data.y.min()) f=64./1416. a=0.2 # In[56]: dh=dy*f/(a-f) # In[93]: (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)) # In[ ]: # In[ ]: