从matplotlib中的Needleman-Wunsch成对序列比对绘制得分矩阵

Kev*_*dez 2 python bioinformatics matplotlib biopython

我正在尝试根据Python中的全局对齐算法(或Needleman-Wunsch算法)绘制矩阵.

我不知道matplotlib是否是这种情况下最好的工具.我试图使用Bokeh,但结构很难适合我想要的矩阵.

我正在使用Bio.SeqIO(BioPython的标准序列输入/输出接口)来存储两个序列.

我得到的结果与此图像类似:

在此输入图像描述

这可能在Matplotlib?我怎样才能做到这一点?

UPDATE

最后,我能够根据ImportanceOfBeingErnest给出的答案构建算法.结果如下:

在此输入图像描述

以下是此实现的要点:plot_needleman_wunsch.py

这是整个项目(正在进行中):bmc-sequence-alignment

Imp*_*est 6

没有明确的算法说明将箭头放在问题中; 因此,这个答案集中于在matplotlib中采用类似情节的方法.

在此输入图像描述

这里的想法是将数字放在绘图中的整数位置,并绘制小网格线n+0.5以获得类似于表格的外观.箭头绘制为在4列阵列中定义的位置之间的注释(前2列:箭头开始的x和y,第三和第四列:箭头末端的x,y).

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np;np.random.seed(5)
plt.rcParams["figure.figsize"] = 4,5
param = {"grid.linewidth" : 1.6,
         "grid.color"     : "lightgray",
         "axes.linewidth" : 1.6,
         "axes.edgecolor" : "lightgray"}
plt.rcParams.update(param)

#Data
headh = list("GATCCA")
headv = list("GTGCCT")

v = np.zeros((7,7), dtype=int)
v[1:,1:] = np.random.randint(-2,7, size=(6,6))

arrows = np.random.randint(0,v.shape[1], size=(14,4))
opt = np.array([(0,1),(1,0),(1,1)])
arrows[:,2:] = arrows[:,:2] + opt[np.random.randint(0,3,size=14 )]

arrowsb = np.random.randint(0,v.shape[1], size=(7,4))
optb = np.array([(0,1),(1,0),(1,1)])
arrowsb[:,2:] = arrowsb[:,:2] + optb[np.random.randint(0,3,size=7 )]

#Plot
fig, ax=plt.subplots()
ax.set_xlim(-1.5, v.shape[1]-.5 )
ax.set_ylim(-1.5, v.shape[0]-.5 )
ax.invert_yaxis()
for i in range(v.shape[0]):
    for j in range(v.shape[1]):
        ax.text(j,i,v[i,j], ha="center", va="center")
for i, l in enumerate(headh):
    ax.text(i+1,-1,l, ha="center", va="center", fontweight="semibold")
for i, l in enumerate(headv):
    ax.text(-1,i+1,l, ha="center", va="center", fontweight="semibold")

ax.xaxis.set_minor_locator(ticker.FixedLocator(np.arange(-1.5, v.shape[1]-.5,1)))
ax.yaxis.set_minor_locator(ticker.FixedLocator(np.arange(-1.5, v.shape[1]-.5,1)))
plt.tick_params(axis='both', which='both', bottom='off', top='off', 
                left="off", right="off", labelbottom='off', labelleft='off')
ax.grid(True, which='minor')


arrowprops=dict(facecolor='crimson',alpha=0.5, lw=0, 
                shrink=0.2,width=2, headwidth=7,headlength=7)
for i in range(arrows.shape[0]):
    ax.annotate("", xy=arrows[i,2:], xytext=arrows[i,:2], arrowprops=arrowprops)
arrowprops.update(facecolor='blue')
for i in range(arrowsb.shape[0]):
    ax.annotate("", xy=arrowsb[i,2:], xytext=arrowsb[i,:2], arrowprops=arrowprops)
plt.show()
Run Code Online (Sandbox Code Playgroud)