【2.8.2】sequence logo图

图形效果:

代码示例

import matplotlib as mpl
from matplotlib.text import TextPath
from matplotlib.patches import PathPatch
from matplotlib.font_manager import FontProperties

fp = FontProperties(family="Arial", weight="bold")  # 注意这里用的是arial字体,是一种非衬线字体,对于字母的对齐很有帮助,我的电脑上没有这种字体,还需要下载,具体方法参见字体那篇博文
globscale = 1.35
LETTERS = { "T" : TextPath((-0.305, 0), "T", size=1, prop=fp),
             "G" : TextPath((-0.384, 0), "G", size=1, prop=fp),
             "A" : TextPath((-0.35, 0), "A", size=1, prop=fp),
             "C" : TextPath((-0.366, 0), "C", size=1, prop=fp) }

COLOR_SCHEME = {'G': 'orange', 
                'A': 'red', 
                'C': 'blue', 
                'T': 'darkgreen'}

def letterAt(letter, x, y, yscale=1, ax=None):
    text = LETTERS[letter]

    t = mpl.transforms.Affine2D().scale(1*globscale, yscale*globscale) + \
        mpl.transforms.Affine2D().translate(x,y) + ax.transData
    p = PathPatch(text, lw=0, fc=COLOR_SCHEME[letter],  transform=t)
    if ax != None:
        ax.add_artist(p)
    return p

ALL_SCORES2 = [[('A', 0.01653482213365913),
          ('G', 0.026710097292833978),
          ('C', 0.035613463057111966),
          ('T', 0.057235922770358522)],
         [('C', 0.020055669245080433),
          ('G', 0.023816107228533015),
          ('A', 0.031336983195438178),
          ('T', 0.058913528407423782)],
         [('T', 0.018666958185377256),
          ('G', 0.084001311834197651),
          ('A', 0.093334790926886277),
          ('C', 0.30333807051238043)],
         [('C', 0.0),
          ('G', 0.0),
          ('A', 0.32027512306044359),
          ('T', 0.82203948252180525)],
         [('C', 0.012698627658037786),
          ('A', 0.053334236163758708),
          ('T', 0.096509570201087178),
          ('G', 0.10920819785912497)],
         [('C', 0.0),
          ('G', 0.089472611853783468),
          ('A', 0.1930724782107959),
          ('T', 0.22132698721725386)],
         [('C', 0.020962390607965918),
          ('A', 0.026202988259957396),
          ('G', 0.066380903591892068),
          ('T', 0.07336836712788071)]
      ]

# import matplotlib.pyplot as plt
# import gact

fig, ax = plt.subplots(figsize=(10,3))

all_scores = ALL_SCORES2
x = 1
maxi = 0
for scores in all_scores:
    y = 0
    for base, score in scores:
        letterAt(base, x,y, score, ax)
        y += score
    x += 1
    maxi = max(maxi, y)

plt.xticks(range(1,x))
plt.xlim((0, x)) 
plt.ylim((0, maxi)) 
plt.tight_layout()      
plt.show()

参考资料

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn