compara-deep-learning/open_synteny_matrices.py

124 lines
4 KiB
Python
Raw Normal View History

2019-06-10 09:37:49 -07:00
import numpy as np
import os
import json
2019-06-12 05:22:20 -07:00
import gc
2019-06-10 09:37:49 -07:00
from read_data import read_data_homology
import matplotlib.pyplot as plt
import seaborn as sns
2019-06-12 05:22:20 -07:00
from matplotlib.backends.backend_pdf import PdfPages
2019-06-10 09:37:49 -07:00
2019-06-12 05:22:20 -07:00
a_h,d_h=read_data_homology("data_homology")
d_h=list(d_h.keys())
2019-06-10 09:37:49 -07:00
ndir="processed/synteny_matrices/"
2019-06-12 05:22:20 -07:00
nf1="_synteny_matrices_global"
nf2="_synteny_matrices_local"
nf3="_indexes"
2019-06-10 09:37:49 -07:00
lsy={}
with open("processed/neighbor_genes.json","r") as file:
lsy=dict(json.load(file))
2019-06-12 05:22:20 -07:00
for i in range(len(d_h)):
print("{}.{}".format(i+1,d_h[i]))
2019-06-10 09:37:49 -07:00
2019-06-12 05:22:20 -07:00
while(1):
try:
ch=int(input("Enter your choice:"))
synteny_matrices_global=np.load(ndir+str(d_h[ch-1])+nf1+".npy")
synteny_matrices_local=np.load(ndir+str(d_h[ch-1])+nf2+".npy")
indexes=np.load(ndir+str(d_h[ch-1])+nf3+".npy")
2019-06-17 03:17:47 -07:00
print(len(indexes))
2019-06-12 05:22:20 -07:00
break
except:
print("Choice invalid or incomplete files!!!!!. Try Another Index.")
df=a_h[ch-1].loc[indexes]
a_h=[]
gc.collect()
2019-06-10 09:37:49 -07:00
inddict={}
for i in range(len(indexes)):
inddict[indexes[i]]=i
2019-06-12 05:22:20 -07:00
ng=["Levenshtein Distance","Levenshtein Distance Reverse"]
nl=["Local Alignment Score","Local Alignment Score Reverse"]
print(indexes)
2019-06-10 09:37:49 -07:00
while(1):
2019-06-12 05:22:20 -07:00
try:
i=int(input("Enter the index:"))
except:
break
2019-06-10 09:37:49 -07:00
if i in inddict:
2019-06-12 05:22:20 -07:00
font = {'family': 'sans-serif',
'color': 'darkturquoise',
'weight': 'heavy',
'size': 20,
}
pdf=PdfPages(str(i)+".pdf")
2019-06-10 09:37:49 -07:00
print("Species",df.loc[i].species)
print("Homology Species",df.loc[i].homology_species)
print("Gene Stable Id:",df.loc[i].gene_stable_id)
print("Homology Gene Stable Id:",df.loc[i].homology_gene_stable_id)
2019-06-12 05:22:20 -07:00
text="Species:"+df.loc[i].species
text+="\n"+"Gene Stable Id:"+df.loc[i].gene_stable_id
text+="\n"+"Homology Species:"+df.loc[i].homology_species
text+="\n"+"Homology Gene Stable Id:"+df.loc[i].homology_gene_stable_id
text+="\n"+"Homology Type:"+df.loc[i].homology_type
fp=plt.figure(figsize=(10,10))
fp.text(0.5,0.5,text,ha="center",fontdict=font)
pdf.savefig()
plt.close()
2019-06-10 09:37:49 -07:00
print("Global aligned matrix:")
g1=df.loc[i].gene_stable_id
g2=df.loc[i].homology_gene_stable_id
x=[]
y=[]
for n in range(len(lsy[g1]['b'])-1,-1,-1):
x.append(lsy[g1]['b'][n])
x.append(g1)
for k in lsy[g1]['f']:
x.append(k)
for n in range(len(lsy[g2]['b'])-1,-1,-1):
y.append(lsy[g2]['b'][n])
y.append(g2)
for k in lsy[g2]['f']:
y.append(k)
loc=inddict[i]
sg=synteny_matrices_global[loc]
sl=synteny_matrices_local[loc]
for m in range(sg.shape[-1]):
matrix=sg[:,:,m]
2019-06-12 05:22:20 -07:00
print(matrix)
fig,ax=plt.subplots(figsize=(8,8))
ax.set_xlabel(str(df.loc[i].homology_species),fontsize=10)
ax.set_ylabel(str(df.loc[i].species),fontsize=10)
hmap=sns.heatmap(matrix,xticklabels=y, yticklabels=x,annot=True,ax=ax,linewidths=.5,cmap="YlGnBu",annot_kws={"size": 10})
hmap.figure.subplots_adjust(left=0.33,bottom=0.33,right=0.79,top=0.79)
ax.set_title(ng[m])
#plt.text(1,0.5,text,size=10)
pdf.savefig()
2019-06-10 09:37:49 -07:00
plt.show()
print("Local Alignment Matrix")
for m in range(sg.shape[-1]):
matrix=sl[:,:,m]
print(matrix)
2019-06-12 05:22:20 -07:00
fig,ax=plt.subplots(figsize=(8,8))
ax.set_xlabel(df.loc[i].homology_species)
ax.set_ylabel(df.loc[i].species)
hmap=sns.heatmap(matrix,xticklabels=y, yticklabels=x,annot=True,ax=ax,linewidths=.5,cmap="YlGnBu",annot_kws={"size": 10})
hmap.figure.subplots_adjust(left=0.27,bottom=0.29,right=0.92)
ax.set_title(nl[m])
pdf.savefig()
2019-06-10 09:37:49 -07:00
plt.show()
2019-06-12 05:22:20 -07:00
pdf.close()
2019-06-10 09:37:49 -07:00
else:
print("Index not found")