-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvisualization.py
137 lines (119 loc) · 4.58 KB
/
visualization.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import numpy as np
from matplotlib import pyplot as plt
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
import umap
def visualize(data, data_integrated, datatype=None, mode='PCA'):
assert (mode in ["PCA", "UMAP", 'TSNE']), "mode has to be either one of 'PCA', 'UMAP', or 'TSNE'."
dataset_num = len(data)
styles = ['g', 'r', 'b', 'y', 'k', 'm', 'c', 'greenyellow', 'lightcoral', 'teal']
# data_map = ['Chromatin accessibility', 'DNA methylation', 'Gene expression']
# color_map = ['E5.5','E6.5','E7.5']
embedding = []
dataset_xyz = []
for i in range(dataset_num):
dataset_xyz.append("data{:d}".format(i+1))
if mode=='PCA':
embedding.append(PCA(n_components=2).fit_transform(data[i]))
elif mode=='TSNE':
embedding.append(TSNE(n_components=2).fit_transform(data[i]))
else:
embedding.append(umap.UMAP(n_components=2, n_neighbors=20, min_dist=0.7).fit_transform(data[i]))
fig = plt.figure()
if datatype is not None:
for i in range(dataset_num):
plt.subplot(1,dataset_num,i+1)
for j in set(datatype[i]):
index = np.where(datatype[i]==j)
plt.scatter(embedding[i][index,0], embedding[i][index,1], c=styles[j], s=5.)
plt.title(dataset_xyz[i])
if mode=='PCA':
plt.xlabel('PCA-1')
plt.ylabel('PCA-2')
elif mode=='TSNE':
plt.xlabel('TSNE-1')
plt.ylabel('TSNE-2')
else:
plt.xlabel('UMAP-1')
plt.ylabel('UMAP-2')
# plt.title(data_map[i])
else:
for i in range(dataset_num):
plt.subplot(1,dataset_num,i+1)
plt.scatter(embedding[i][:,0], embedding[i][:,1],c=styles[i], s=5.)
plt.title(dataset_xyz[i])
if mode=='PCA':
plt.xlabel('PCA-1')
plt.ylabel('PCA-2')
elif mode=='TSNE':
plt.xlabel('TSNE-1')
plt.ylabel('TSNE-2')
else:
plt.xlabel('UMAP-1')
plt.ylabel('UMAP-2')
plt.title(dataset_xyz[i])
plt.tight_layout()
data_all = np.vstack((data_integrated[0], data_integrated[1]))
for i in range(2, dataset_num):
data_all = np.vstack((data_all, data_integrated[i]))
if mode=='PCA':
embedding_all = PCA(n_components=2).fit_transform(data_all)
elif mode=='TSNE':
embedding_all = TSNE(n_components=2).fit_transform(data_all)
else:
embedding_all = umap.UMAP(n_components=2, n_neighbors=20, min_dist=0.7).fit_transform(data_all)
tmp = 0
num = [0]
for i in range(dataset_num):
num.append(tmp+np.shape(data_integrated[i])[0])
tmp += np.shape(data_integrated[i])[0]
embedding = []
for i in range(dataset_num):
embedding.append(embedding_all[num[i]:num[i+1]])
color = [[1,0.2,0], [0,1,0.2], [0.2,0,1], [0.5, 1, 0.5], [0.1, 0.8, 0.2]]
# marker=['x','^','o','*','v']
fig = plt.figure()
if datatype is not None:
plt.subplot(1,2,1)
for i in range(dataset_num):
plt.scatter(embedding[i][:,0], embedding[i][:,1], c=color[i], s=5., alpha=0.8)
plt.title('Integrated Embeddings')
if mode=='PCA':
plt.xlabel('PCA-1')
plt.ylabel('PCA-2')
elif mode=='TSNE':
plt.xlabel('TSNE-1')
plt.ylabel('TSNE-2')
else:
plt.xlabel('UMAP-1')
plt.ylabel('UMAP-2')
plt.subplot(1,2,2)
for i in range(dataset_num):
for j in set(datatype[i]):
index = np.where(datatype[i]==j)
plt.scatter(embedding[i][index,0], embedding[i][index,1], c=styles[j], s=5., alpha=0.8)
plt.title('Integrated Cell Types')
if mode=='PCA':
plt.xlabel('PCA-1')
plt.ylabel('PCA-2')
elif mode=='TSNE':
plt.xlabel('TSNE-1')
plt.ylabel('TSNE-2')
else:
plt.xlabel('UMAP-1')
plt.ylabel('UMAP-2')
else:
for i in range(dataset_num):
plt.scatter(embedding[i][:,0], embedding[i][:,1], c=styles[i], s=5., alpha=0.8)
plt.title('Integrated Embeddings')
if mode=='PCA':
plt.xlabel('PCA-1')
plt.ylabel('PCA-2')
elif mode=='TSNE':
plt.xlabel('TSNE-1')
plt.ylabel('TSNE-2')
else:
plt.xlabel('UMAP-1')
plt.ylabel('UMAP-2')
plt.tight_layout()
plt.show()