<%
	import os
	import sys
	import numpy as np
	import scipy.stats as stats
	import statsmodels.api as sm

	def get_hpo_link(hpo_code):
		link = '-'
		if hpo_code != '-':
			link = f"<a href=\"https://hpo.jax.org/app/browse/term/{hpo_code}\">{hpo_code}</a>"
		return link
	
	def format_data(data):
		patient_list = []
		for element in data: # Cluster
			clID, patient_number, patient_ids, hpo_codes, hpo_names = element
			# TODO: mostrar registro por paciente
			#STDERR.puts element.inspect
			for i, patID in enumerate(patient_ids):
				patient_record = [clID, patient_number]
				patient_record.append( patID )
				patient_record.append( ", ".join([get_hpo_link(hpo_code) for hpo_code in hpo_codes[i]] ) )
				patient_record.append( ", ".join(hpo_names[i] ))
				patient_list.append( patient_record )
		data.clear()
		data.extend(patient_list) 
		data.insert(0, ["Cluster ID","Patients in Cluster","Patient IDs", "HPO codes", "Phenotypes"])	

	hpo = plotter.hash_vars["hpo"]
	all_heatmap_factors = ['cl_id']
	all_heatmap_factors.extend(plotter.hash_vars["extra_attr_list"])

	# clust_overrep_analysis
	clust_overrep_analysis = []
	clusters = plotter.hash_vars['clusters']
	patient_data = plotter.hash_vars['patient_data']
	total_pats = len(patient_data.extra_attr)
	attr_dict = {} # get patients for each category in extra_attr
	for pat_id, ex_attrs in patient_data.extra_attr.items():
		for factor, category in ex_attrs.items():
			key = f"{factor}-{category}"
			query = attr_dict.get(f"{factor}-{category}")
			if query == None:
				attr_dict[key] = [pat_id]
			else:
				query.append(pat_id)
	for element in clusters: # Cluster
		clID, patient_number, patient_ids, hpo_codes, hpo_names = element
		if patient_number > 1:
			for category, cat_pat_ids in attr_dict.items():
				intersectedIDs = set(cat_pat_ids).intersection(set(patient_ids))
				intersection_length = len(intersectedIDs)
				if intersection_length > 0:
					n1_items = len(patient_ids)
					n2_items = len(cat_pat_ids)
					# Analogous formulation with stats.fisher_exact(data, alternative='greater')
					p_value = stats.hypergeom.sf(intersection_length-1, total_pats, n1_items, n2_items)
					clust_overrep_analysis.append([clID, category, intersection_length, patient_number, n2_items - intersection_length, p_value])

	if len(clust_overrep_analysis) > 0:
		pvals = np.array([cat[5] for cat in clust_overrep_analysis])
		adj_pvals =  sm.stats.multipletests(pvals, method='fdr_bh', is_sorted=False, returnsorted=False)[1] #2expcalc?
		for i, adj_pval in enumerate(adj_pvals): clust_overrep_analysis[i].append(adj_pval)
		clust_overrep_analysis = [ cat for cat in clust_overrep_analysis if cat[5] < 0.05 ] #soft filter using raw paval only
	clust_overrep_analysis.insert(0, ['clID', 'Category', 'Intersection', 'Cluster patients', 'Category specific patients', 'P-value', 'Adj P-value'])
	plotter.hash_vars["clust_overrep_analysis"] = clust_overrep_analysis
%>

<div style="width: 90%; background-color:#ecf0f1; margin: 0 auto;"> 
	<h1 style="text-align: center; background-color:#d6eaf8">Patient HPO profiles by cluster.</h1>
		${ plotter.table(id="clusters", header= True, border= 2, row_names= False, text= True, cell_align= ["center"] * 5, 
			 styled= 'dt', attrib= {'class': 'table'},
			func = format_data)
		}
</div>
% if plotter.hash_vars["full_sim_matrix"] != []:
	<div style="width: 90%; background-color:#ecf0f1; margin: 0 auto;"> 
		<h1 style="text-align: center; background-color:#d6eaf8">General representation</h1>
			${ plotter.heatmap(id="full_sim_matrix", header= True, row_names= True, text= True, 
								var_attr = list(range(1, len(all_heatmap_factors) + 1)), 
								config= {"varOverlays" : all_heatmap_factors},
								tree = plotter.hash_vars['full_sim_tree'], treeBy = 'variables', 
								height = 800, width =800) }
	</div>
% endif

% if plotter.hash_vars["hpo_pat_matrix"] != []:
	<div style="width: 90%; background-color:#ecf0f1; margin: 0 auto;"> 
		<h1 style="text-align: center; background-color:#d6eaf8">Patient-HPO representation</h1>
			${ plotter.heatmap(id="hpo_pat_matrix", header= True, row_names= True, text= True, height = 800, width =1000,
								var_attr = list(range(1, len(all_heatmap_factors) + 1)), 
								tree = plotter.hash_vars['full_sim_tree'], treeBy = 'variables',
								config= {"varOverlays" : all_heatmap_factors}
								)}
	</div>
	<div style="width: 90%; background-color:#ecf0f1; margin: 0 auto;"> 
		<h2 style="text-align: center; background-color:#d6eaf8">Overrepresentation analysis over categories from patient factors.</h2>
			${ plotter.table(id="clust_overrep_analysis", header= True, border= 2, row_names= False, text= True, cell_align= ["center"] * 7, 
				styled= 'dt', attrib= {'class': 'table'})
			}
	</div>	

% endif

<div style="width: 90%; background-color:#ecf0f1; margin: 0 auto;"> 
	<h1 style="text-align: center; background-color:#d6eaf8"> Cluster detailed view.</h1>
	
		% for clID, sim_matrix in plotter.hash_vars["sim_mat4cluster"].items():
			<% plotter.hash_vars["sim_matrix"] = sim_matrix %>
			${ plotter.similarity_matrix_plot(id= "sim_matrix", header= True, row_names= True, x_label = "Sim", title= f"Cluster {clID}" ) }
		% endfor
</div>