-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmFISH_timeseries_clustering.py
173 lines (139 loc) · 4.62 KB
/
smFISH_timeseries_clustering.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Analysis!
Cluster the time traces and then plot a heatmap for the dynamics traces
"""
"""
Import python packages
"""
import HTSeq
import collections
import itertools
import os
import subprocess
import collections
import datetime
import yaml
import fnmatch
import shlex
import numpy
import scipy
import scipy.io as sio
import pyensembl
import h5py
import pandas as pd
import numpy as np
import matplotlib
import cPickle as pickle
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import scipy.cluster.hierarchy as sch
import rpy2
import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr
rpy2.robjects.numpy2ri.activate()
def cleanAxis(ax):
ax.set_frame_on(False)
for label in ax.axes.get_xticklabels():
label.set_visible(False)
for label in ax.axes.get_yticklabels():
label.set_visible(False)
for tick in ax.axes.get_xticklines():
tick.set_visible(False)
for tick in ax.axes.get_yticklines():
tick.set_visible(False)
for spine in ax.spines.values():
spine.set_visible(False)
"""
Figure out the length of the longest time trace
"""
direc = "/scratch/PI/mcovert/dvanva/sequencing/smFISH"
file_name_save = os.path.join(direc, "good_cells_300min.pkl")
all_cells = pickle.load(open(file_name_save))
times = [300]
R = rpy2.robjects.r
DTW = importr('dtw')
DTWCLUST = importr('dtwclust')
for t in times:
longest_time = 0
number_of_cells = 0
for cell in all_cells:
number_of_cells += 1
longest_time = np.amax([longest_time, cell.norm_med.shape[0]])
dynamics_matrix = np.zeros((number_of_cells,longest_time))
"""
Fill up the heat map matrix
"""
cell_counter = 0
for cell in all_cells:
dynam = cell.norm_med
dynamics_matrix[cell_counter,0:dynam.shape[0]] = dynam
cell_counter += 1
"""
Perform hierarchical clustering
"""
distance_matrix = np.zeros((number_of_cells, number_of_cells))
for i in xrange(number_of_cells):
print i
for j in xrange(number_of_cells):
alignment = R.SBD(dynamics_matrix[i,:], dynamics_matrix[j,:], znorm = True)
distance_matrix[i,j] = alignment.rx('dist')[0][0]
np.savez('/scratch/PI/mcovert/dvanva/sequencing/smFISH/'+str(t)+"_dynamics_distance_matrix_kshape.npz", distance_matrix = distance_matrix)
dynamics_load = np.load('/scratch/PI/mcovert/dvanva/sequencing/smFISH/'+str(t)+"_dynamics_distance_matrix_kshape.npz")
distance_matrix = dynamics_load['distance_matrix']
Y = sch.linkage(distance_matrix, method = 'ward')
ind_dynamics = sch.fcluster(Y,0.5*np.amax(Y[:,2]),'distance') - 1
"""
Plot dendrogram
"""
fig = plt.figure()
ax_dendro = fig.add_axes([0.09, 0.1, 0.2, 0.8], frame_on = False)
Z = sch.dendrogram(Y, orientation = 'right', color_threshold = 0.5*np.amax(Y[:,2]))
ax_dendro.set_xticks([])
ax_dendro.set_yticks([])
"""
Plot heatmap
"""
ax_heatmap = fig.add_axes([0.3, 0.1, 0.6, 0.8])
index = Z['leaves']
dynamics_ordered = dynamics_matrix[index,:]
im = ax_heatmap.matshow(dynamics_ordered, aspect = 'auto', origin = 'lower', cmap = plt.get_cmap('Reds'), interpolation = 'none')
fig.colorbar(im, ticks = [0, 1], orientation = 'vertical')
ax_heatmap.set_title(str(t) +' minute NFkB activity heatmap - ' + str(number_of_cells) + ' cells', y = 1.05)
ax_heatmap.set_xlabel('Time')
ax_heatmap.set_yticks([])
ax_heatmap.set_xticks([])
plt.savefig("plots/smFISH_dynamics_clustering_" + str(t) + "min.pdf")
"""
Assign clusters
"""
for j in xrange(number_of_cells):
all_cells[j].clusterID = ind_dynamics[j]
file_name_save = os.path.join(direc, "good_cells_" + str(t) + "min_cluster_w_smFISH_traces.pkl")
pickle.dump(all_cells, open(file_name_save, 'wb'), protocol = pickle.HIGHEST_PROTOCOL)
"""
Compute k-shape average for each cluster
"""
max_clust_id = np.amax(ind_dynamics)
min_clust_id = np.amin(ind_dynamics)
cluster_id_list = np.arange(min_clust_id, max_clust_id + 1)
cluster_dynamics_DBA = np.zeros((max_clust_id-min_clust_id+1,longest_time), dtype = 'float32')
for j in xrange(len(cluster_id_list)):
cluster_id = cluster_id_list[j]
num_of_cells = len(np.where(ind_dynamics == cluster_id)[0])
dyn_matrix = np.zeros((num_of_cells,longest_time))
dyn_list = []
cell_counter = 0
for cell in all_cells:
if cell.clusterID == cluster_id:
dyn_matrix[cell_counter,0:dynam.shape[0]] = cell.norm_med
dyn_list += [cell.norm_med]
cell_counter += 1
# temp = dba(dyn_list)
temp = R.shape_extraction(dyn_matrix, znorm = True)
temp = np.array(temp)
temp -= temp[0]
temp /= np.amax(temp)
cluster_dynamics_DBA[j,:] = temp
# print cluster_dynamics_DBA
colors = ['g', 'r', 'c','purple','yellow']
np.savez("/scratch/PI/mcovert/dvanva/sequencing/smFISH/300_cluster_avg_kshape_smFISH.npz", cluster_dynamics_avg = cluster_dynamics_DBA)