-
Notifications
You must be signed in to change notification settings - Fork 0
/
gene_hierarchy_and_similarity.py
73 lines (56 loc) · 2.24 KB
/
gene_hierarchy_and_similarity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pandas as pd
from sklearn.metrics import pairwise_distances
from sklearn.metrics.pairwise import cosine_similarity
from scipy.spatial.distance import pdist, squareform
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pprint
# Load your genetic data from a CSV file
file_path = 'synthatic_sTR.csv' # Replace with your file path
data = pd.read_csv(file_path, header=0) # Reads from the first row as headers
# get data
data_samples = data.iloc[:, :]
pprint.pprint(data_samples)
# 1. Cosine Similarity Matrix (for comparing people based on their genetic markers)
cosine_sim_matrix = cosine_similarity(data_samples)
# 2. Euclidean Distance Matrix (for measuring distance between people's genetic profiles)
euclidean_dist_matrix = squareform(pdist(data_samples, metric='euclidean'))
# 3. Pearson Correlation Matrix (to check linear relationships between people)
pearson_corr_matrix = np.corrcoef(data_samples)
# 4. Spearman Correlation Matrix (for non-linear relationships between people)
# Transpose the data to get correlations between rows
spearman_corr_matrix = data_samples.T.corr(method='spearman')
# Visualize and save each matrix separately
# Cosine Similarity
plt.figure(figsize=(10, 8))
sns.heatmap(cosine_sim_matrix, cmap='coolwarm', cbar=True)
plt.title('Cosine Similarity Matrix')
plt.tight_layout()
plt.savefig('Cosine_Similarity_Matrix.png')
plt.show()
plt.close() # Close the plot to avoid overlap
# Euclidean Distance
plt.figure(figsize=(10, 8))
sns.heatmap(euclidean_dist_matrix, cmap='viridis', cbar=True)
plt.title('Euclidean Distance Matrix')
plt.tight_layout()
plt.savefig('Euclidean_Distance_Matrix.png')
plt.show()
plt.close() # Close the plot to avoid overlap
# Pearson Correlation
plt.figure(figsize=(10, 8))
sns.heatmap(pearson_corr_matrix, cmap='coolwarm', cbar=True)
plt.title('Pearson Correlation Matrix')
plt.tight_layout()
plt.savefig('Pearson_Correlation_Matrix.png')
plt.show()
plt.close() # Close the plot to avoid overlap
# Spearman Correlation
plt.figure(figsize=(10, 8))
sns.heatmap(spearman_corr_matrix, cmap='coolwarm', cbar=True)
plt.title('Spearman Correlation Matrix')
plt.tight_layout()
plt.savefig('Spearman_Correlation_Matrix.png')
plt.show()
plt.close() # Close the plot to avoid overlap