# 聚类图
# grm文件个体号
grm_id_file=cwd_path+"\\"+output_name+"-grm"+".grm.id"
grm_id=pd.read_csv(grm_id_file,sep="\s+",header=None)
grm_file=cwd_path+"\\"+output_name+"-grm"+".grm.bin"
%matplotlib inline
def read_bin(grm_file):
with open(grm_file,'rb') as f:
f_content=f.read()
f_len=int(len(f_content)/4)
print(f_len)
content=struct.unpack('f'*f_len,f_content)
f.close()
print(len(content))
grm=np.zeros((len(grm_id),len(grm_id)))
indices=np.tril_indices(len(grm_id))
grm[indices]=content
disMat = sch.distance.pdist(grm,'euclidean')
ZB=sch.linkage(disMat,method='ward')
LB=grm_id[1]
LB=LB.tolist()
plt.figure(figsize=(60,8),dpi=600)
P=sch.dendrogram(ZB,labels=LB,color_threshold=10,leaf_font_size=3)#color_threshold=10,纵坐标小于10的簇设置单独颜色,leaf_font_size=3,个体号字体大小
return grm,P
grm,P_grm=read_bin(grm_file)
# 下三角矩阵补全成对称矩阵并将对角线设为1
grm2=grm+grm.T
row,col = np.diag_indices_from(grm2)
grm2[row,col]=1
grm3=pd.DataFrame(grm2)
# 给G矩阵添加行列个体号
grm3.columns=grm_id[1]
grm3=pd.merge(grm_id[1],grm3,left_index=True,right_index=True)
grm3.to_excel(cwd_path+"\\"+output_name+"-亲缘关系矩阵"+".xlsx")
image.png