-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.py
executable file
·647 lines (512 loc) · 22.9 KB
/
info.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
"""
Read from Csv file and generate the cluster data from it.
"""
import os
import numpy as np
import pandas as pd
from dcpyps import dataset, dcio
from cost_function import compute_stretch_number, compute_separation_dict, compute_separation_dp, test_dp
from preparation import filter_first_last, impose_resolution
from PlotAnalysis import PlotSingle
class Patch:
'''
Patch class contains the patch information and the cluster detail.
'''
def __init__(self, path):
self._filepath, self._patch_name = os.path.split(path)
def read_scn(self, tres=0.0, tcrit=None, event_num = 0, duration = float('inf')):
'''
read from scn file and divide the record based on the tcrit.
'''
self._cluster_dict = {}
self._cluster_number = 0
screcord = dataset.SCRecord([os.path.join(self._filepath, self._patch_name),],
tres=tres/1e6, tcrit=tcrit/1e3)
patchname = []
for patch in screcord.filenames:
head, tail = os.path.split(patch)
root, ext = os.path.splitext(tail)
patchname.append(root)
patchname = ','.join(patchname)
self._patch_name = patchname
open_period = np.array(screcord.opint)*1000
shut_period = np.array(screcord.shint)*1000
open_amp = np.array(screcord.opamp)*-1
shut_amp = np.array(screcord.shamp)
open_flag = np.array(screcord.oppro)
shut_flag = np.array(screcord.shpro)
shut_tcrit = np.where(shut_period < tcrit)[0]
split_index = np.split(shut_tcrit,
np.where(np.diff(shut_tcrit) != 1)[0]+1)
for cluster in split_index:
if (len(cluster) > event_num) or ((sum(open_period[cluster]) + sum(shut_period[cluster]))> duration):
self._cluster_number += 1
new_cluster = Cluster(self._patch_name, self._cluster_number, patch = self)
new_cluster.add_info(self._cluster_number,
sum(open_period[:cluster[0]]+shut_period[:cluster[0]]),
sum(open_period[:cluster[-1]+1]+shut_period[:cluster[-1]+1]),
open_period[cluster], shut_period[cluster],
open_amp[cluster], shut_amp[cluster],
open_flag[cluster], shut_flag[cluster],
impose_resolution = False)
self._cluster_dict[self._cluster_number] = new_cluster
def get_path(self):
'''
return the complete path.
'''
return os.path.join(self._filepath, self._patch_name)
def scan(self, filterone = False):
'''
Scan the csv file to determine clusters.
'''
self._cluster_dict = {}
self._cluster_number = 0
state, start, end, amp, dwell = np.genfromtxt(
self.get_path(),
delimiter=',', usecols=(2,4,5,6,8),unpack=True)
# Remove all the empty rows
nonempty = np.isfinite(state)
state = state[nonempty]
start = start[nonempty]
end = end[nonempty]
amp = amp[nonempty]
dwell = dwell[nonempty]
# Detect number of clusters
# If the end time is different from the next start time
# A new cluster is defined
sep_list = [0, ]
for i in range(len(state) - 1):
if end[i] != start[i+1]:
sep_list.append(i+1)
sep_list.append(len(state))
# Create a new cluster data if it is longer than 100ms
for i in range(len(sep_list) - 1):
if (end[sep_list[i+1]-1] - start[sep_list[i]]) > 100:
# Get the indice for open and shut
indice = np.array(range(sep_list[i], sep_list[i+1]))
open_period_idx = np.intersect1d(indice, np.nonzero(state))
shut_period_idx = np.intersect1d(indice, np.where(state == 0)[0])
# Make sure that there is the smae number of open and shut period
cluster_length = min(len(open_period_idx), len(shut_period_idx))
open_period_idx = open_period_idx[:cluster_length]
shut_period_idx = shut_period_idx[:cluster_length]
# Filter out the clusters which is irrigiular
open_period = dwell[open_period_idx]
shut_period = dwell[shut_period_idx]
if filterone:
while filter_first_last(open_period, shut_period):
open_period, shut_period = filter_first_last(open_period, shut_period)
if (len(open_period) != cluster_length):
cluster_length = len(open_period)
first_idx = np.where(dwell[open_period_idx] == open_period[0])[0][0]
last_idx = np.where(dwell[open_period_idx] == open_period[-1])[0][-1]+1
open_period_idx = open_period_idx[first_idx: last_idx]
shut_period_idx = shut_period_idx[first_idx: last_idx]
# Create new cluster
self._cluster_number += 1
new_cluster = Cluster(self._patch_name, i+1, patch = self)
new_cluster.add_info(self._cluster_number,
start[open_period_idx[0]], end[shut_period_idx[-1]],
dwell[open_period_idx], dwell[shut_period_idx],
amp[open_period_idx], amp[shut_period_idx],
np.zeros(len(open_period_idx)),
np.zeros(len(shut_period_idx))
,impose_resolution = False)
# )
self._cluster_dict[self._cluster_number] = new_cluster
def get_cluster(self, cluster_index, output = False):
'''
Return the requested cluster.
'''
if not hasattr(self, '_cluster_dict'):
self.scan()
if output:
print(self._cluster_dict[cluster_index])
return self._cluster_dict[cluster_index]
def get_cluster_list(self, output = False):
'''
Return all clusters in this patch.
'''
if not hasattr(self, '_cluster_dict'):
self.scan()
if output:
print(self._cluster_list)
return list(self._cluster_dict.values())
__getitem__ = get_cluster
def filter_cluster(self, func, *params):
'''
Filter out the clusters which returns False by func.
'''
self._cluster_number = 0
cluster_dict = {}
for index in self._cluster_list:
cluster = self._cluster_dict[index]
if func(cluster, *params):
self._cluster_number += 1
cluster.cluster_no = self._cluster_number
cluster_dict[self._cluster_number] = cluster
self._cluster_dict = cluster_dict
def write_scn(self, modified = 'modified_', filepath = None,
patch_name = None):
'''
Write into scn file.
'''
if filepath is None:
filepath = self._filepath
if patch_name is None:
patch_name = modified + self._patch_name+'.SCN'
intervals = []
amplitudes = []
flags = []
end = 0
for cluster_num in self._cluster_list:
cluster = self._cluster_dict[cluster_num]
#add a shut time interval which represents the intercluster shut time
shut = cluster.start - end
end = cluster.end
intervals.append(shut)
amplitudes.append(0)
# Flag this shut time as bad
flags.append(8)
# Add data
intervals.append(cluster.period)
amplitudes.append(cluster.amp)
flags.append(cluster.flag)
intervals = np.hstack(intervals)
amplitudes = np.hstack(amplitudes)*-1
amplitudes = amplitudes.astype('int')
flags = np.hstack(flags)
flags = flags.astype('int')
filename = os.path.join(filepath, patch_name)
dcio.scn_write(intervals, amplitudes, flags, filename=filename)
def _get_mean_open(self):
mean_open = []
for cluster in self._cluster_dict.values():
mean_open.append(cluster.mean_open)
return mean_open
mean_open = property(_get_mean_open)
def _get_mean_shut(self):
mean_shut = []
for cluster in self._cluster_dict.values():
mean_shut.append(cluster.mean_shut)
return mean_shut
mean_shut = property(_get_mean_shut)
def _get_open_period(self):
open_period = []
for cluster in self._cluster_dict.values():
open_period.append(cluster.open_period)
open_period = np.hstack(open_period)
return open_period
open_period = property(_get_open_period)
def _get_shut_period(self):
shut_period = []
for cluster in self._cluster_dict.values():
shut_period.append(cluster.shut_period)
shut_period = np.hstack(shut_period)
return shut_period
shut_period = property(_get_shut_period)
def _get_amp_distribution(self):
amp_distribution = []
for cluster in self._cluster_dict.values():
amp_distribution.append(cluster.mean_amp)
return amp_distribution
amp_distribution = property(_get_amp_distribution)
def _get_transition_distribution(self):
transition_distribution = []
for cluster in self._cluster_dict.values():
transition_distribution.append(cluster.event_num)
return transition_distribution
transition_distribution = property(_get_transition_distribution)
def _get_popen_distribution(self):
popen_distribution = []
for cluster in self._cluster_dict.values():
popen_distribution.append(cluster.popen)
return popen_distribution
popen_distribution = property(_get_popen_distribution)
def _get_cluster_number(self): return self._cluster_number
cluster_number = property(_get_cluster_number)
def _get_transition_number(self): return len(self.open_period) + len(self.shut_period)
transition_number = property(_get_transition_number)
def _get_patchname(self): return self._patch_name
patchname = property(_get_patchname)
def __iter__(self):
cluster_list = self._cluster_list.copy()
while cluster_list:
yield self._cluster_dict[cluster_list.pop(0)]
def __repr__(self):
return 'Patch({})'.format(self._patch_name)
def __str__(self):
if not hasattr(self, 'cluster_number'):
self.scan()
str_filepath = 'Filepath: {} \n'.format(self.get_path())
str_clusternumber = 'Number of clusters: {} \n'.format(int(self._cluster_number))
return str_filepath+str_clusternumber
def _get_cluster_list(self): return list(self._cluster_dict.keys())
_cluster_list = property(_get_cluster_list)
class Cluster:
'''
Detail information about a cluster.
'''
def __init__(self, patchname = None, cluster_no = None, patch = None):
self.patchname = patchname
self.cluster_no = cluster_no
self.patch = patch
self.resolution = None
def add_info(self, cluster_no, start, end,
open_period, shut_period,
open_amp, shut_amp,
open_flag, shut_flag,
impose_resolution = True):
'''
Add information about the cluster.
'''
self.resolution = 0.111
self.cluster_no = cluster_no
self.start = start
self.end = end
self.open_period = open_period
self.shut_period = shut_period
self.open_amp = open_amp
self.shut_amp = shut_amp
self._open_flag = open_flag
self._shut_flag = shut_flag
self.event_num = len(self.open_period) + len(self.shut_period)
# Impose resolution
if impose_resolution:
self.impose_resolution()
self._expolatory_analysis()
def load_SCRecord(self, screcord):
'''
Load data from SCRecord.
'''
patchname = []
for patch in screcord.filenames:
head, tail = os.path.split(patch)
root, ext = os.path.splitext(tail)
patchname.append(root)
patchname = ','.join(patchname)
self.patchname = patchname
self.start = 0
self.end = sum(screcord.periods)
self.open_period = np.array(screcord.opint)*1000
self.shut_period = np.array(screcord.shint)*1000
self.open_amp = np.array(screcord.opamp)*-1/1000
self.shut_amp = np.array(screcord.shamp)
self._open_flag = np.array(screcord.oppro)
self._shut_flag = np.array(screcord.shpro)
self._expolatory_analysis()
def _expolatory_analysis(self):
'''
Calculate the popen, mean amplitude, duration and event_sum.
'''
# Calculate the mean Popen
self.popen = np.sum(self.open_period)/(np.sum(self.open_period) +
np.sum(self.shut_period))
# Calculate mean amplitude
# Only takes into account of the periods longer than 0.3ms
# If all the periods are less than 0.3ms take the median instead
# self.mean_amp = np.mean(self.open_amp-self.shut_amp)
if self.resolution and any(self.open_period > self.resolution*2):
valid = self.open_amp[self.open_period > self.resolution*2]
self.mean_amp = np.mean(valid)
self.min_amp = min(valid)
self.max_amp = max(valid)
else:
self.mean_amp = np.mean(self.open_period)
self.min_amp = -1
self.max_amp = -1
# Calculate the duration
self.duration = sum(self.open_period) + sum(self.shut_period)
# Number of events in th cluster
self.event_num = len(self.open_period) + len(self.shut_period)
def _get_dataframe(self, output_list = ['patchname', 'cluster_no', 'period', 'amp', 'flag']):
'''
Generate dataframe for plotting (currently).
'''
tempdict = {name: getattr(self, name) for name in output_list}
df = pd.DataFrame(tempdict)
df = df[output_list]
# Check if the even periods are shut and the odd periods are open.
assert np.mean(df['amp'][::2]) > 10 * np.mean(df['amp'][1::2])
df['state'] = np.ones(self.event_num, dtype=bool)
df['state'][1::2] = False
return df
dataframe = property(_get_dataframe)
def _get_period(self):
period = np.empty(self.event_num)
period[::2] = self.open_period
period[1::2] = self.shut_period
return period
def _set_period(self, period):
self.open_period = period[::2]
self.shut_period = period[1::2]
period = property(_get_period, _set_period)
def _get_amp(self):
amp = np.empty(self.event_num)
amp[::2] = self.open_amp
amp[1::2] = self.shut_amp
return amp
def _set_amp(self, amp):
self.open_amp = amp[::2]
self.shut_amp = amp[1::2]
amp = property(_get_amp, _set_amp)
def _get_flag(self):
flag = np.empty(self.event_num)
flag[::2] = self._open_flag
flag[1::2] = self._shut_flag
return flag
def _set_flag(self, flag):
self._open_flag = flag[::2]
self._shut_flag = flag[1::2]
flag = property(_get_flag, _set_flag)
def _get_mean_open(self):
return np.mean(self.open_period)
mean_open = property(_get_mean_open)
def _get_mean_shut(self):
return np.mean(self.shut_period)
mean_shut = property(_get_mean_shut)
def impose_resolution(self, resolution = 0.3):
'''
Impose resolution. By default: 0.3ms
'''
self.resolution = resolution
if impose_resolution(self.start,
self.end,
self.period,
self.amp,
self.flag,
resolution):
(self.start,
self.end,
self.period,
self.amp,
self.flag) = impose_resolution(self.start,
self.end,
self.period,
self.amp,
self.flag,
resolution)
def get_cluster_detail(self):
'''
Get the detail info of the start, end, open_period, shut_period,
open_amp and shut_amp as a dictionary.
'''
cluster_dict = {}
cluster_dict['start'] = self.start
cluster_dict['end'] = self.end
cluster_dict['open_period'] = self.open_period
cluster_dict['shut_period'] = self.shut_period
cluster_dict['open_amp'] = self.open_amp
cluster_dict['shut_amp'] = self.shut_amp
return cluster_dict
def compute_mode(self, mode_number = 10, threshold = 3):
'''
Compute the ways of separating the mode.
'''
separation_dict, cost_dict, mean_cost_dict = compute_separation_dict(
np.log(self.open_period), np.log(self.shut_period), mode_number)
#test_dp(np.log(self.open_period), np.log(self.shut_period), 10)
mode_number = compute_stretch_number(cost_dict, mean_cost_dict, threshold)
self.mode_number = mode_number
self._separation_dict = separation_dict
self._cost_dict = cost_dict
self._mean_cost_dict = mean_cost_dict
def compute_mode_detail(self, output = False):
'''
Compute the Popen, mean open time, mean shut time.
'''
mode_separation = self._separation_dict[self.mode_number]
mode_start = []
mode_stop = []
popen_list = []
mean_open = []
mean_shut = []
for i in range(len(mode_separation)-1):
open_period = self.open_period[mode_separation[i]:mode_separation[i+1]]
shut_period = self.shut_period[mode_separation[i]:mode_separation[i+1]]
popen_list.append(sum(open_period)/(sum(open_period) + sum(shut_period)))
mean_open.append(np.exp(np.mean(np.log(open_period))))
mean_shut.append(np.exp(np.mean(np.log(shut_period))))
mode_start.append(sum(self.open_period[:mode_separation[i]])
+ sum(self.shut_period[:mode_separation[i]]))
mode_stop.append(sum(self.open_period[:mode_separation[i+1]])
+ sum(self.shut_period[:mode_separation[i+1]]))
self.mode_start = self.start + np.array(mode_start)
self.mode_stop = self.start + np.array(mode_stop)
self.popen_list = popen_list
self.mode_mean_open = mean_open
self.mode_mean_shut = mean_shut
if output:
for i in range(len(mode_stop)):
print('Mode {}: Start: {:.2f}, End: {:.2f}, Popen {:.2f}, Mean open: {:.2f}, Mean shut: {:.2f}'.format(
i+1, self.mode_start[i], self.mode_stop[i], popen_list[i], mean_open[i],
mean_shut[i]))
def _get_separation(self):
return self._separation_dict[self.mode_number]
separation = property(_get_separation)
def _get_normalised_cost(self):
return np.array([self._cost_dict[i] for i in self._cost_dict])/self.event_num
normalised_cost = property(_get_normalised_cost)
def _get_normalised_mean_cost(self):
return np.array([self._mean_cost_dict[i] for i in self._mean_cost_dict])/self.event_num
normalised_mean_cost = property(_get_normalised_mean_cost)
def _get_difference(self):
difference = self.normalised_mean_cost - self.normalised_cost
return difference[1:]
difference = property(_get_difference)
def get_mode_detail(self):
'''
Get the detail info of mode_start, mode_stop, popen_list, mean_open,
mean_shut as a dictionary.
'''
if hasattr(self, 'mode_start'):
mode_dict = {}
mode_dict['mode_start'] = self.mode_start
mode_dict['mode_stop'] = self.mode_stop
mode_dict['popen_list'] = self.popen_list
mode_dict['duration'] = self.mode_stop - self.mode_start
mode_dict['mean_open'] = self.mode_mean_open
mode_dict['mean_shut'] = self.mode_mean_shut
mode_dict['separation'] = self._separation_dict[self.mode_number]
mode_dict['event_num'] = np.diff(mode_dict['separation'])
mode_dict['cost_dict'] = self._cost_dict
mode_dict['mean_cost_dict'] = self._mean_cost_dict
return mode_dict
else:
return None
def identity(self):
return '({}:{})'.format(self.patchname,self.cluster_no)
def __eq__(self, other):
if (self.patchname == other.patchname) and (self.cluster_no == other.cluster_no):
return True
else:
return False
def __repr__(self):
return 'Cluster({}/{})'.format(self.patchname,self.cluster_no)
def __str__(self):
str_filepath = 'Patch Name: {} \n'.format(self.patchname)
str_cluster_no = 'Cluster number: {} \n'.format(int(self.cluster_no))
str_start_end = 'From {:.2f} s to {:.2f} s. \n'.format(self.start/1000, self.end/1000)
str_duration = 'Cluster duration: {:.2f} ms \n'.format(self.duration)
str_amp = 'Mean amplitude: {:.2f} pA. \n'.format(self.mean_amp)
str_popen = 'Popen is {:.2f}'.format(self.popen)
return str_filepath+str_cluster_no+str_start_end+str_duration+str_amp+str_popen
def show_origianl(self):
plot = PlotSingle()
plot.load_cluster(self)
plot.plot_original(savefig = False, display = True)
class BatchCluster(Cluster):
'''
Cluster class with detailed information for batch analysis.
Initialize with kwargs for receptor, mutation, composition, agonist, concentration
Default: 'GlyR', 'wt', 'alpha1', 'glycine', None
'''
def __init__(self, cluster, **kwargs):
self.__dict__ = cluster.__dict__.copy()
self.receptor = kwargs.get('receptor', 'GlyR')
self.mutation = kwargs.get('mutation', 'wt')
self.composition = kwargs.get('composition', 'alpha1')
self.agonist = kwargs.get('agonist', 'glycine')
self.concentration = kwargs.get('concentration', None)
def __repr__(self):
return 'BatchCluster({}/{})'.format(self.patchname,self.cluster_no)