#%%
# importing required libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from sklearn.preprocessing import MinMaxScaler
import scipy.cluster.hierarchy as shc
from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics import accuracy_score
#%%
# reading the data
data_orig = pd.read_csv(r'/dataset_cluster_analysis.csv')
head = data_orig.head()  #checking the first 5 rows of the data.
#%%
data = data_orig.drop(['mutation'], axis=1)
data = data.fillna(data.mode().iloc[0]) #imputing any NaNs with the mode of column.
#%%
scaler = MinMaxScaler()
scaler.fit(data)
data = scaler.transform(data)
data = pd.DataFrame(data,columns=['Coarse', 'structural eye ', 'OMA', 'IHH', 'OFC≤-2SD'])
#%%
data.info()
#%%
# statistics of the data
data.columns
data.shape
data.describe()
#%%
# draws dendrogram 
plt.figure(figsize=(10, 7))  
plt.title("Dendrograms")  
dend = shc.dendrogram(shc.linkage(data, method='ward'))
#%%
# running hierarchical clustering
cluster = AgglomerativeClustering(n_clusters=2, affinity='euclidean', linkage='ward')  
clusters_predicted = cluster.fit_predict(data)
#%%
# putting the results in data frame format & combining 'mutation' column
clustersDf = pd.DataFrame(clusters_predicted)
clustersDf.columns = ['clusters_predicted']
combinedDf = pd.concat([data, clustersDf], axis = 1).reset_index()

data_final = combinedDf.join(data_orig['mutation'])
data_final = data_final.drop(['index'], axis =1)
#%%
# visualising the two clusters
plt.subplots(figsize = (15,5))
sns.countplot(x=data_final['mutation'], order=data_final['mutation'].value_counts().index,hue=data_final['clusters_predicted'])
plt.show()
#%%
# exporting results
dataexport = data_final.to_excel(r'\results.xlsx', index = False)
#%%
# creating dendrogram clustermap
mut = data_final.mutation # using the 'mutation' column of the variable 'data_final'.
lut = dict(zip(mut.unique(), "rbg")) # linking each unique item in 'lut' to an rbg colour. 2 items (0,1) are now linked to red and blue.
row_colors = mut.map(lut) # linking the rgb to each corresponding item in the mutation column. 
plot = sns.clustermap(data, metric="euclidean", standard_scale=None, method="ward", cmap="mako",row_colors=row_colors) # running the plot with mutation column included in the heatmap.
plt.show()
#%%
# computing accuracy score
accuracy_score(data_final.mutation, data_final.clusters_predicted)
# %%
# plotting confusion matrix
sns.set(font_scale=3)
confusion_matrix = sklearn.metrics.confusion_matrix(data_final.mutation, data_final.clusters_predicted)
plt.figure(figsize=(16, 14))
sns.heatmap(confusion_matrix, cmap = "Blues", annot=True, fmt="d", annot_kws={"size": 20}, yticklabels = ['ARID1B','SOX11'])
plt.title("Confusion matrix", fontsize=30)
plt.ylabel('Mutation', fontsize=25)
plt.xlabel('Predicted Cluster', fontsize=25)
# %%