matplotlib中由needemanwunsch成对序列比对绘制得分矩阵

2024-05-29 10:06:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图根据Python中的全局对齐算法(或Needleman–Wunsch algorithm)绘制一个矩阵。在

我不知道matplotlib是否是这种情况下的最佳工具。我试图使用Bokeh,但是它的结构很难像我想要的那样适合一个矩阵。在

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

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

enter image description here

在Matplotlib中可以吗?我怎么能做到呢?在

更新

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

enter image description here

下面是这个实现的要点:plot_needleman_wunsch.py

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


Tags: 工具算法matplotlibbokeh绘制情况序列矩阵
1条回答
网友
1楼 · 发布于 2024-05-29 10:06:23

对于在问题中放置箭头的算法没有明确的描述;因此,这个答案集中在matplotlib中处理类似绘图的方法。在

{a1}

这里的想法是将数字放在绘图中的整数位置,并在n+0.5处绘制次要网格线,以获得类似于表格的外观。箭头在4列数组中定义的位置之间绘制为注释(前2列:箭头开始的x和y,第3列和第4列:箭头结束的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()

相关问题 更多 >

    热门问题