-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotutils.py
1362 lines (1281 loc) · 50 KB
/
plotutils.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
Collection of plotting functions and utilities for Hi-C analysis
Part of ChromaWalker package
"""
import numpy as np
import os
import sys
#import matplotlib as mpl
#mpl.use('Agg')
import matplotlib.pyplot as plt
import random
import copy
import matplotlib.gridspec as gridspec
from time import time, sleep
from numpy.linalg import solve
import cPickle as pickle
import matplotlib.backends.backend_pdf
from matplotlib import cm
from matplotlib import collections as mc
#import mpldatacursor
import hicutils as hcu
import networkx as nx
import numpy.ma as ma
import matplotlib.patches as patches
#####################################################
# Utilities
## Padding to full arrays (including omitted data rows)
def _build_fullvector(data, mappingdata, fillval):
"""
Build full array from mapped subvector,
filling empty elements with fillval.
"""
mapping, nbins = mappingdata
datapad = np.zeros(nbins)
datapad.fill(fillval)
datapad[mapping] = data
return datapad
def _build_fullarray(data, mappingdata, fillval):
"""
Build full array from mapped subarray,
filling empty rows and columns with fillval.
"""
mapping, nbins = mappingdata
datapad = np.zeros((nbins, nbins))
datapad.fill(fillval)
for i, vec in enumerate(data):
datapad[mapping[i], mapping] = vec
return datapad
def _build_fullarray_inter(data, mappingdata1, mappingdata2, fillval):
"""
Build full array from mapped subarray,
filling empty rows and columns with fillval.
Inter-chromosomal mode: x- and y-axes have different mappingdata.
"""
mapping1, nbins1 = mappingdata1
mapping2, nbins2 = mappingdata2
datapad = np.zeros((nbins1, nbins2))
datapad.fill(fillval)
for i, vec in enumerate(data):
datapad[mapping1[i], mapping2] = vec
return datapad
def _extendarray(datapack, membership, membermode='hard'):
"""
Form an extended-array view of a reduced data matrix.
Input:
datapack: (M, M) reduced array of data values
membership: (N,) or (M, N) array of membership values
(N,) for 'hard' membership,
(M, N) for 'soft' membership
membermode: 'hard' for assigning each extended pixel to one
reduced-data pixel
Note: For extended pixels which should have no data,
set membership value to negative number.
'soft' to assign weights for each reduced-data pixel,
for each extended pixel
Returns:
extarray: Extended array
"""
# Create soft-type membership function
npack = len(datapack)
if membermode == 'hard':
membfunc = np.array([membership == i for i in range(npack)])
elif membermode == 'soft':
membfunc = membership.copy()
else:
print 'Invalid membership mode %s!' % membership
sys.exit(1)
nbins, _ = membfunc.shape
# Create extended array
extarray = np.dot(np.dot(membfunc.T, datapack), membfunc)
return extarray
#####################################################
# Plotting functions
def _plot_cytobands(cytobanddata, resolution, x, plotbname=False):
cytoband, blimlist, bnamelist, _ = cytobanddata
if plotbname:
x2 = x.twiny()
chrlen = len(cytoband) * resolution / 1e6
bands = np.array([cytoband] * 10) * 1.0
cmap = plt.get_cmap('binary')
cmap.set_under((1.0, 0.0, 0.0))
x.imshow(bands, interpolation='nearest', cmap=cmap,
vmin=0.0, vmax=4.0, aspect='auto', extent=[0, chrlen, 0, 1])
x.set_xlim(0, chrlen)
x.set_ylim(0, 1)
x.set_yticks(())
if plotbname:
x2.set_xlim(0, len(cytoband))
x2.set_ylim(0, 1)
bandctrs = [(i + j) / (2.0) for i, j in blimlist]
x2.set_xticks(bandctrs)
x2.set_xticklabels(bnamelist, rotation=45)
return
def _plot_cytobands_nchr(cytobanddatas, resolution, x, plotbname=False):
#nchrs = len(cytobanddatas)
cytoband = []
blimlist = []
bnamelist = []
shifts = []
for ct, bl, bn, _ in cytobanddatas:
shift = len(cytoband) * resolution / 1.0e6
shifts.append(shift)
cytoband.extend(list(ct))
blimlist.extend(list(np.array(bl) + shift))
bnamelist.extend(list(bn))
#cytoband, blimlist, bnamelist, _ = cytobanddata
if plotbname:
x2 = x.twiny()
chrlen = len(cytoband) * resolution / 1e6
bands = np.array([cytoband] * 10) * 1.0
cmap = plt.get_cmap('binary')
cmap.set_under((1.0, 0.0, 0.0))
print bands.shape
x.imshow(bands, interpolation='nearest', cmap=cmap,
vmin=0.0, vmax=4.0, aspect='auto', extent=[0, chrlen, 0, 1])
x.set_xlim(0, chrlen)
x.set_ylim(0, 1)
x.set_yticks(())
for shift in shifts:
x.axvline(x=shift, c='b')
if plotbname:
x2.set_xlim(0, len(cytoband))
x2.set_ylim(0, 1)
bandctrs = [(i + j) / (2.0) for i, j in blimlist]
x2.set_xticks(bandctrs)
x2.set_xticklabels(bnamelist, rotation=45)
return
def _plot_cytobands_vert(cytobanddata, resolution, x, plotbname=False):
cytoband, blimlist, bnamelist, _ = cytobanddata
if plotbname:
x2 = x.twiny()
chrlen = len(cytoband) * resolution / 1e6
bands = np.array([cytoband] * 10) * 1.0
cmap = plt.get_cmap('binary')
cmap.set_under((1.0, 0.0, 0.0))
x.imshow(bands.T, interpolation='nearest', cmap=cmap,
vmin=0.0, vmax=4.0, aspect='auto', extent=[0, 1, chrlen, 0])
x.set_ylim(chrlen, 0)
x.set_xlim(0, 1)
x.set_xticks(())
if plotbname:
x2.set_xlim(0, len(cytoband))
x2.set_ylim(0, 1)
bandctrs = [(i + j) / (2.0) for i, j in blimlist]
x2.set_yticks(bandctrs)
x2.set_yticklabels(bnamelist, rotation=45)
return
def _plot_cytobands_nchrvert(cytobanddatas, resolution, x, plotbname=False):
#nchrs = len(cytobanddatas)
cytoband = []
blimlist = []
bnamelist = []
shifts = []
for ct, bl, bn, _ in cytobanddatas:
shift = len(cytoband) * resolution / 1.0e6
shifts.append(shift)
cytoband.extend(list(ct))
blimlist.extend(list(np.array(bl) + shift))
bnamelist.extend(list(bn))
#cytoband, blimlist, bnamelist, _ = cytobanddata
if plotbname:
x2 = x.twiny()
chrlen = len(cytoband) * resolution / 1e6
bands = np.array([cytoband] * 10) * 1.0
cmap = plt.get_cmap('binary')
cmap.set_under((1.0, 0.0, 0.0))
print bands.shape
x.imshow(bands.T, interpolation='nearest', cmap=cmap,
vmin=0.0, vmax=4.0, aspect='auto', extent=[0, 1, chrlen, 0])
x.set_ylim(chrlen, 0)
x.set_xlim(0, 1)
x.set_xticks(())
for shift in shifts:
x.axhline(y=shift, c='b')
if plotbname:
x2.set_xlim(0, len(cytoband))
x2.set_ylim(0, 1)
bandctrs = [(i + j) / (2.0) for i, j in blimlist]
x2.set_yticks(bandctrs)
x2.set_yticklabels(bnamelist, rotation=45)
return
def _plot_heatmap(data, x, res, cmap=None,
n=1.0, scale='pow', extent=None, vlims=None,
badcolor=(1.0, 1.0, 1.0, 0.0), returnimg=False):
"""
Pretty plotting of interaction heatmaps.
"""
if extent is None:
extent = [0, len(data) * res / 1e6, len(data) * res / 1e6, 0]
if cmap is None:
cmap = plt.get_cmap('jet')
if vlims is None:
vlims = [None, None]
cmap.set_bad(badcolor)
if scale == 'pow':
d = np.abs(data) ** n * np.sign(data)
img = x.imshow(d, interpolation='nearest', cmap=cmap,
extent=extent, vmin=vlims[0], vmax=vlims[1], alpha=1.0)
elif scale == 'log':
img = x.imshow(np.log(data), interpolation='nearest',
cmap=cmap, extent=extent, vmin=vlims[0], vmax=vlims[1],
alpha=1.0)
else:
print 'Invalid 2D plotting scale!'
return img if returnimg else None
def _plot_heatmap_uneven(data, edges, x, res, cmap=None,
n=1.0, scale='pow', extent=None, vlims=None,
badcolor=(1.0, 1.0, 1.0, 0.0), returnimg=False):
"""
Pretty plotting of interaction heatmaps.
"""
if extent is None:
extent = [0, len(data) * res / 1e6, len(data) * res / 1e6, 0]
if cmap is None:
cmap = plt.get_cmap('jet')
if vlims is None:
vals = data.flatten()
vlims = [np.min(vals[np.isfinite(vals)]),
np.max(vals[np.isfinite(vals)])]
cmap.set_bad(badcolor)
if scale == 'pow':
d = np.abs(data) ** n * np.sign(data)
dm = ma.masked_where(np.isnan(d), d)
img = x.pcolormesh(edges, edges, dm, cmap=cmap,
vmin=vlims[0] ** n, vmax=vlims[1] ** n, alpha=1.0,
edgecolors='face')
elif scale == 'log':
d = np.log(data)
dm = ma.masked_where(np.isnan(d), d)
img = x.pcolormesh(edges, edges, dm,
cmap=cmap, vmin=np.log(vlims[0]), vmax=np.log(vlims[1]),
alpha=1.0, edgecolors='face')
else:
print 'Invalid 2D plotting scale!'
return img if returnimg else None
def _fillplot_heatmap_2bands(x, fmat, mappingdata, blimlist, res, cmap=None,
n=1.0, scale='pow', extent=None, vlims=None,
badcolor=(1.0, 1.0, 1.0, 0.0)):
"""
Plot two non-adjacent bands.
Input:
fmat: interaction matrix, with empty rows removed
mappingdata: Maps fmat pixels to full matrix pixel positions
blimlist: Band boundaries in bp
res: Resolution of full matrix
"""
# Pad array to fill band ranges
## Allocate padded array
(stpos1, enpos1), (stpos2, enpos2) = blimlist[:2]
nbins1 = (enpos1 - stpos1) / res
nbins2 = (enpos2 - stpos2) / res
nbins12 = nbins1 + nbins2
fmatpad = np.zeros((nbins12, nbins12))
## Map fmat indices to fmatpad indices
padpos = list(np.arange(nbins1) * res + stpos1) + \
list(np.arange(nbins2) * res + stpos2)
maptopad = []
mapmask = []
for mapval in mappingdata[0]:
thispos = mapval * res
if thispos in padpos:
maptopad.append(padpos.index(thispos))
mapmask.append(True)
else:
mapmask.append(False)
mapmask = np.array(mapmask)
maptopad = np.array(maptopad)
## Fill
for i, m in enumerate(maptopad):
fmatpad[m, maptopad] = fmat[i, mapmask]
print np.max(fmatpad), np.min(fmatpad[fmatpad > 0.0])
print np.product(fmatpad.shape), np.sum(np.nonzero(fmatpad))
# Plot padded array
if extent is None:
extent = [0, len(fmatpad) * res / 1e6, len(fmatpad) * res / 1e6, 0]
if cmap is None:
cmap = plt.get_cmap('jet')
if vlims is None:
vlims = [None, None]
cmap.set_bad(badcolor)
if scale == 'pow':
d = np.abs(fmatpad) ** n * np.sign(fmatpad)
x.imshow(d, interpolation='nearest', cmap=cmap,
extent=extent, vmin=vlims[0], vmax=vlims[1], alpha=1.0)
elif scale == 'log':
x.imshow(np.log(fmatpad), interpolation='nearest',
cmap=cmap, extent=extent, vmin=vlims[0], vmax=vlims[1],
alpha=1.0)
# Draw boundary line
x.axvline(x=nbins1 * res / 1.0e6, c='k')
x.axhline(y=nbins1 * res / 1.0e6, c='k')
# Set ticks at 1Mbp interval
maxticktrial = int(np.max(padpos) / 1.0e6)
tickpos = []
ticklabel = []
for i in range(maxticktrial + 1):
trialpos = int(i * 1.0e6)
if trialpos in padpos:
tickpos.append(padpos.index(trialpos) * res / 1e6)
ticklabel.append(i)
x.set_xticks(tickpos)
x.set_xticklabels(ticklabel)
x.set_yticks(tickpos)
x.set_yticklabels(ticklabel)
return
def _plot_tsetlab(x, tset, lab, mappingdata, res, scale='pow',
n=1.0, cmap=None, s=None):
"""
2D scatter plot of inter-target interactions.
"""
# Unpack data
mapping, nbins = mappingdata
if s is None:
s = 12
# Rescale values
lab2 = lab.copy()
lab2[lab == 0.0] = np.nan
labrescale = (lab2 - np.min(lab2[lab2 > 0.0])) / (np.max(lab2[lab2 > 0.0]) -
np.min(lab2[lab2 > 0.0]))
if scale == 'log':
labrescale = np.log(labrescale)
else:
labrescale = labrescale ** n
if cmap is None:
cmap = plt.get_cmap('jet')
clrarray = cmap(labrescale)
# Plot
tsetsort = np.sort(tset)
nt = len(lab)
for i in range(nt):
xval = mapping[tsetsort[i]] * res / 1.0e6
for j in range(nt):
yval = mapping[tsetsort[j]] * res / 1.0e6
if i == j:
x.scatter(xval, yval, marker='+', c='k', s=20)
else:
x.scatter(xval, yval,
marker='o', c=clrarray[i, j], s=s, lw=0,
alpha=1.0)
x.set_axis_bgcolor((0.8, 0.8, 0.8))
x.set_xlim(0, nbins * res / 1.0e6)
x.set_ylim(nbins * res / 1.0e6, 0)
def _scatterplot_RefLab_2bands(x, pars, blimlist, resvals=(100000, 200000)):
"""
Plot scatter plot of inter-target effective interactions.
Zooms into region between two bands.
Input:
resvals: (res_cytoband, res_tset)
Default value: 100kb, 200kb
"""
res_cytoband, res_tset = resvals
(stpos1, enpos1), (stpos2, enpos2) = blimlist[:2]
# Get reference tset
pars_t = copy.deepcopy(pars)
pars_t['res'] = res_tset
tset = hcu._get_tsetReference(pars_t, refdate='20160610')
tsetsort = np.sort(tset)
# Get targets in chosen region(s)
(stpos1, enpos1), (stpos2m, enpos2) = blimlist[:2]
## Get mappingdata
mappingdata = hcu._get_mappingdata(hcu._get_runbinarydir(pars_t))
mapping, nbins = mappingdata
tgtsel = []
tgtpos = []
for i, t in enumerate(tsetsort):
tpos = mapping[t] * res_tset
if (tpos >= stpos1 and tpos < enpos1) or \
(tpos >= stpos2 and tpos < enpos2):
tgtsel.append(i)
tgtpos.append(tpos / 1.0e6)
# Get lab
lab = hcu._get_TargetEffLaplacian(hcu._get_runbinarydir(pars_t), tset)
cmap = plt.get_cmap('jet')
labrescale = (lab - np.min(lab[lab > 0.0])) / (np.max(lab) -
np.min(lab[lab > 0.0]))
clrarray = cmap(labrescale)
x1 = []
x2 = []
p1 = []
p2 = []
print tgtsel
print tgtpos
for i1, t1 in enumerate(tgtsel):
for i2, t2 in enumerate(tgtsel):
if i2 == i1:
continue
x1.append(t1)
x2.append(t2)
if tgtpos[i1] > enpos1 / 1.0e6:
p1.append(tgtpos[i1] + (enpos1 - stpos1 - stpos2 +
res_tset / 2) / 1.0e6)
else:
p1.append(tgtpos[i1] - (stpos1 - res_tset / 2) / 1.0e6)
if tgtpos[i2] > enpos1 / 1.0e6:
p2.append(tgtpos[i2] + (enpos1 - stpos1 - stpos2 +
res_tset / 2) / 1.0e6)
else:
p2.append(tgtpos[i2] - (stpos1 - res_tset / 2) / 1.0e6)
# Scatter plot, broken axes
x.scatter(p1, p2, marker='o', c=clrarray[x1, x2], lw=0.5)
x.set_axis_bgcolor((0.8, 0.8, 0.8))
x.axvline(x=(enpos1 - stpos1) / 1.0e6, c='k')
x.axhline(y=(enpos1 - stpos1) / 1.0e6, c='k')
# Set ticks at 1Mbp interval
maxticktrial = int(np.max(blimlist) / 1.0e6)
tickpos = []
ticklabel = []
for i in range(maxticktrial + 1):
trialpos = int(i * 1.0e6)
if (trialpos >= stpos1 and trialpos < enpos1):
tickpos.append((trialpos - stpos1) / 1.0e6)
ticklabel.append(i)
elif (trialpos >= stpos2 and trialpos < enpos2):
tickpos.append((trialpos - stpos2 + enpos1 - stpos1) / 1.0e6)
ticklabel.append(i)
x.set_xticks(tickpos)
x.set_xticklabels(ticklabel)
x.set_yticks(tickpos)
x.set_yticklabels(ticklabel)
# Draw boundary line
lims = 0, (enpos2 - stpos2 +
enpos1) / 1.0e6 - ticklabel[0] - stpos1 / 1.0e6 \
+ ticklabel[0]
print lims
x.set_xlim(lims)
x.set_ylim(lims[::-1])
x.set_aspect(1)
def _plot_qAi(mat, tset, ax, mapping, res, shift, cmap=cm.Dark2):
tsettemp = np.sort(tset)
colors = map(cmap, mapping[np.array(tsettemp)] / np.max(mapping * 1.0))
for i, tgt in enumerate(tsettemp):
clr = colors[i]
xvals = np.arange(np.max(mapping) + 1) * res / 1.0e6
yvals = np.ones_like(xvals, dtype='float64') * np.nan
yvals[mapping] = mat[i] + shift
_ = ax.plot(xvals, yvals, c=clr)
_ = ax.set_xlim(0, np.max(xvals))
def _plot_qAi_max(mat, tset, ax, mapping, res, shift, cmap=cm.Dark2):
tsettemp = np.sort(tset)
colors = map(cmap, mapping[np.array(tsettemp)] / np.max(mapping * 1.0))
for i, tgt in enumerate(tsettemp):
clr = colors[i]
xvals = np.arange(np.max(mapping) + 1) * res / 1.0e6
yvals = np.ones_like(xvals, dtype='float64') * np.nan
yvals[mapping] = mat[i] + shift
mvals = np.ones_like(xvals, dtype='float64') * np.nan
mvals[mapping] = np.max(mat, axis=0) + shift
maxmask = (yvals == mvals)
yvals[np.array(1 - maxmask, dtype='bool')] = np.nan
#_ = ax.plot(xvals[maxmask], yvals[maxmask], c=clr)
_ = ax.plot(xvals, yvals, c=clr)
_ = ax.set_xlim(0, np.max(xvals))
xvals = np.arange(np.max(mapping) + 1) * res / 1.0e6
yvals = np.ones_like(xvals, dtype='float64') * np.nan
yvals[mapping] = np.max(mat, axis=0) + shift
#_ = ax.plot(xvals, yvals, 'b')
_ = ax.axhline(y=shift, color='k')
_ = ax.set_xlim(0, np.max(xvals))
def _plot_qAi_tgtstrip(tset, ax, mapping, res, shift, cmap=cm.Dark2,
lw=1, ls='-', c=None):
targets = []
for i in tset:
targets.append(i)
targets.sort()
if c is None:
colors = map(cmap, mapping[np.array(targets)] / np.max(mapping * 1.0))
else:
colors = ['k'] * len(targets)
for i, tgt in enumerate(targets):
clr = colors[i]
xvals = [mapping[tgt] * res / 1.0e6] * 2
yvals = shift, shift + 1.0
_ = ax.plot(xvals, yvals, c=clr, lw=lw, ls=ls)
xvals = np.arange(np.max(mapping) + 1) * res / 1.0e6
_ = ax.axhline(y=shift, color='k')
_ = ax.set_xlim(0, np.max(xvals))
def _plot_qAi_fill(qAi, tset, ax, mapping, res, shift, cmap=cm.Dark2):
targets = [v for v in qAi]
targets.sort()
ntarget = len(targets)
mat = np.array([qAi[k] for k in targets])
ntarget = len(tset)
# Cumulative qAi
qp = [np.sum(mat[:i + 1], axis=0) for i in range(ntarget)]
qp = [np.zeros_like(qp[0])] + qp
qp = np.array(qp)
centroids = [(qAi[k] * mapping) / np.sum(qAi[k]) for t in targets]
#colors = map(cmap, mapping[np.array(targets)] / np.max(mapping * 1.0))
colors = map(cmap, mapping[np.array(centroids)] / np.max(mapping * 1.0))
xvals = np.arange(np.max(mapping) + 1) * res / 1.0e6
yvals1 = np.ones_like(xvals, dtype='float64') * np.nan
yvals1[mapping] = qp[0] + shift
for i in range(ntarget):
yvals1 = np.ones_like(xvals, dtype='float64') * np.nan
yvals2 = np.ones_like(xvals, dtype='float64') * np.nan
yvals1[mapping] = qp[i] + shift
yvals2[mapping] = qp[i + 1] + shift
ax.fill_between(xvals, yvals1, yvals2, facecolor=colors[i],
where=(yvals2 > yvals1), lw=0)
ax.set_ylim(0, 1)
ax.set_xlim(np.min(xvals), np.max(xvals))
def _plot_qAi_fill_hardp(qAi, tset, ax, mapping, res, shift, cmap=cm.Dark2):
targets = [v for v in qAi]
targets.sort()
ntarget = len(targets)
mat = np.array([qAi[k] for k in targets])
mat2 = mat.copy()
for i in range(len(mat2.T)):
mat2[:, i] = 0.0
mat2[np.argmax(mat[:, i]), i] = 1.0
ntarget = len(tset)
# Cumulative qAi
qp = [np.sum(mat2[:i + 1], axis=0) for i in range(ntarget)]
qp = [np.zeros_like(qp[0])] + qp
qp = np.array(qp)
centroids = [np.sum(qAi[t] * mapping) / np.sum(qAi[t]) for t in targets]
#print np.min(np.interp(centroids, np.arange(len(mapping)), mapping,
#left=mapping[0], right=mapping[-1]) / np.max(mapping * 1.0)), \
#np.max(np.interp(centroids, np.arange(len(mapping)), mapping,
#left=mapping[0], right=mapping[-1]) / np.max(mapping * 1.0)), \
#centroids
#_ = raw_input('...:')
#colors = map(cmap, mapping[np.array(targets)] / np.max(mapping * 1.0))
#colors = map(cmap, np.interp(centroids, np.arange(len(mapping)), mapping,
#left=mapping[0], right=mapping[-1]) / np.max(mapping * 1.0))
if cmap is None:
colors = [(0.5, 0.5, 0.5) for c in centroids]
else:
colors = map(cmap, centroids / np.max(mapping * 1.0))
xvals = np.arange(np.max(mapping) + 1) * res / 1.0e6
yvals1 = np.ones_like(xvals, dtype='float64') * np.nan
yvals1[mapping] = qp[0] + shift
for i in range(ntarget):
yvals1 = np.ones_like(xvals, dtype='float64') * np.nan
yvals2 = np.ones_like(xvals, dtype='float64') * np.nan
yvals1[mapping] = qp[i] + shift
yvals2[mapping] = qp[i + 1] + shift
ax.fill_between(xvals, yvals1, yvals2, facecolor=colors[i],
where=(yvals2 > yvals1), lw=0.0)
ax.set_ylim(0, 1)
ax.set_xlim(np.min(xvals), np.max(xvals))
def _plot_qAi_fill_hardp2(qAi, tset, ax, mapping, res, shift, cmap=cm.Dark2):
targets = [v for v in qAi]
targets.sort()
ntarget = len(targets)
mat = np.array([qAi[k] for k in targets])
mat2 = mat.copy()
mat3 = np.zeros(len(mat[0]), dtype=int) - 1
for i in range(len(mat2.T)):
mat2[:, i] = 0.0
mat2[np.argmax(mat[:, i]), i] = 1.0
mat3[i] = np.argmax(mat[:, i])
ntarget = len(tset)
# Cumulative qAi
qp = [np.sum(mat2[:i + 1], axis=0) for i in range(ntarget)]
qp = [np.zeros_like(qp[0])] + qp
qp = np.array(qp)
centroids = [np.sum(qAi[t] * mapping) / np.sum(qAi[t]) for t in targets]
if cmap is None:
colors = [(0.5, 0.5, 0.5) for c in centroids]
else:
colors = map(cmap, centroids / np.max(mapping * 1.0))
xvals = np.arange(np.max(mapping) + 1) * res / 1.0e6
yvals1 = np.ones_like(xvals, dtype='float64') * np.nan
yvals1[mapping] = qp[0] + shift
blockedges = []
for i in range(ntarget):
yvals1 = np.ones_like(xvals, dtype='float64') * np.nan
yvals2 = np.ones_like(xvals, dtype='float64') * np.nan
yvals1[mapping] = qp[i] + shift
yvals2[mapping] = qp[i + 1] + shift
ax.fill_between(xvals, yvals1, yvals2, facecolor=colors[i],
where=(yvals2 > yvals1), lw=0)
thisblock = mat2[i]
thisblockedgeL = np.nonzero((thisblock[:-1] - thisblock[1:]) < 0)[0]
for edgeL in thisblockedgeL:
if mat3[edgeL] >= 0 and mat3[edgeL + 1] >= 0:
xvs = [mapping[edgeL + 1] * res / 1.0e6] * 2
yvs = [shift, shift + 1]
ax.plot(xvs, yvs, 'k')
blockedges.append(list(thisblockedgeL))
ax.set_ylim(0, 1)
ax.set_xlim(np.min(xvals), np.max(xvals))
print "block edges:"
print blockedges
def _plot_qAi_fill_hardp2_ntgrad(qAi, tset, ax, mapping, res, shift,
cmap=cm.Blues, ntmax=50):
targets = [v for v in qAi]
targets.sort()
ntarget = len(targets)
mat = np.array([qAi[k] for k in targets])
mat2 = mat.copy()
mat3 = np.zeros(len(mat[0]), dtype=int) - 1
for i in range(len(mat2.T)):
mat2[:, i] = 0.0
mat2[np.argmax(mat[:, i]), i] = 1.0
mat3[i] = np.argmax(mat[:, i])
ntarget = len(tset)
# Cumulative qAi
qp = [np.sum(mat2[:i + 1], axis=0) for i in range(ntarget)]
qp = [np.zeros_like(qp[0])] + qp
qp = np.array(qp)
centroids = [np.sum(qAi[t] * mapping) / np.sum(qAi[t]) for t in targets]
if cmap is None:
colors = [(0.5, 0.5, 0.5) for c in centroids]
else:
p = 0.3
clr = (ntarget ** p - ntmax ** p) / (2.0 ** p - ntmax ** p)
colors = [cmap(clr)] * len(centroids)
xvals = np.arange(np.max(mapping) + 1) * res / 1.0e6
yvals1 = np.ones_like(xvals, dtype='float64') * np.nan
yvals1[mapping] = qp[0] + shift
blockedges = []
for i in range(ntarget):
yvals1 = np.ones_like(xvals, dtype='float64') * np.nan
yvals2 = np.ones_like(xvals, dtype='float64') * np.nan
yvals1[mapping] = qp[i] + shift
yvals2[mapping] = qp[i + 1] + shift
ax.fill_between(xvals, yvals1, yvals2, facecolor=colors[i],
where=(yvals2 > yvals1), lw=0)
thisblock = mat2[i]
thisblockedgeL = np.nonzero((thisblock[:-1] - thisblock[1:]) < 0)[0]
for edgeL in thisblockedgeL:
if mat3[edgeL] >= 0 and mat3[edgeL + 1] >= 0:
xvs = [mapping[edgeL + 1] * res / 1.0e6] * 2
yvs = [shift, shift + 1]
ax.plot(xvs, yvs, 'k')
blockedges.append(list(mapping[thisblockedgeL]))
print blockedges
ax.set_ylim(0, 1)
ax.set_xlim(np.min(xvals), np.max(xvals))
def _plot_PartitionsHierarchy(ax, limslist, pieceidlist, tclist, ntlist,
levels, res):
"""
Plot hierarchy of partitions in rectangular box format.
Selected ntargets in ntlist
For ntarget in levels, bars are twice as big.
"""
ytickpos = []
for i, (lims2, pieceids2, tc, ntarget) in \
enumerate(zip(limslist, pieceidlist, tclist, ntlist)):
shift = len(ntlist) - i - 1 + np.sum(ntarget < np.array(levels))
height = 2.0 if ntarget in levels else 1.0
ytickpos.append(shift + 0.5 * height)
mypatches = []
for (st, en), pieceid in zip(lims2, pieceids2):
if pieceid < 0:
mypatches.append(patches.Rectangle(
(st * res / 1.0e6, shift),
(en - st) * res / 1.0e6, height, fill=False,
linewidth=0.5))
else:
mypatches.append(patches.Rectangle(
(st * res / 1.0e6, shift),
(en - st) * res / 1.0e6, height,
facecolor=tc, linewidth=0.2))
for p in mypatches:
ax.add_patch(p)
yp = [shift, shift + height]
for (st, en) in lims2:
xp = [st * res / 1.0e6, st * res / 1.0e6]
ax.plot(xp, yp, 'k', lw=1)
xp = [en * res / 1.0e6, en * res / 1.0e6]
ax.plot(xp, yp, 'k', lw=1)
ax.set_yticks(ytickpos)
ax.set_yticklabels(ntlist)
ax.set_xlim(0, np.max(map(max, lims2)) * res / 1.0e6)
ax.set_ylim(0, len(ntlist) + np.sum(ntarget >= np.array(levels)))
def _plot_hcluster_fill_hardp(hc, ax, mapping, res, shift, cmap=cm.Dark2):
mat3 = hc.copy()
ntarget = np.max(mat3) + 1
mat2 = np.array([map(int, mat3 == i) for i in range(ntarget)])
# Cumulative qAi
qp = [np.sum(mat2[:i + 1], axis=0) for i in range(ntarget)]
qp = [np.zeros_like(qp[0])] + qp
qp = np.array(qp)
centroids = [np.sum((mat3 == i) * mapping) / np.sum((mat3 == i))
for i in range(ntarget)]
colors = map(cmap, centroids / np.max(mapping * 1.0))
xvals = np.arange(np.max(mapping) + 1) * res / 1.0e6
yvals1 = np.ones_like(xvals, dtype='float64') * np.nan
yvals1[mapping] = qp[0] + shift
for i in range(ntarget):
yvals1 = np.ones_like(xvals, dtype='float64') * np.nan
yvals2 = np.ones_like(xvals, dtype='float64') * np.nan
yvals1[mapping] = qp[i] + shift
yvals2[mapping] = qp[i + 1] + shift
ax.fill_between(xvals, yvals1, yvals2, facecolor=colors[i],
where=(yvals2 > yvals1), lw=0)
thisblock = mat2[i]
thisblockedgeL = np.nonzero((thisblock[:-1] - thisblock[1:]) < 0)[0]
for edgeL in thisblockedgeL:
if mat3[edgeL] >= 0 and mat3[edgeL + 1] >= 0:
xvs = [mapping[edgeL + 1] * res / 1.0e6] * 2
yvs = [shift, shift + 1]
ax.plot(xvs, yvs, 'k')
ax.set_ylim(0, 1)
ax.set_xlim(np.min(xvals), np.max(xvals))
def _plot_lab_points(lab, tset, ax, pars, mappingdata, cytobanddata,
scale='pow', n=1.0, cmap=None, s=None):
"""
Plot effective inter-target Laplacian.
Mode: Represent by points.
"""
# Unpack pars
res = pars['res']
mapping, nbins = mappingdata
if s is None:
s = 20
# Grid lines
factor = res / 1.0e6
minpos = 0.0
maxpos = nbins * factor
bandnum = np.zeros(nbins, dtype=int)
for i, blims in enumerate(cytobanddata[1]):
ia, ib = map(int, blims)
bandnum[ia:ib + 1] = i
centmask = (np.array(cytobanddata[3])[bandnum] == 'acen')
centpos = np.nonzero(centmask)[0] * factor
mincent = np.min(centpos)
maxcent = np.max(centpos) + factor
xvalss = [[minpos, mincent], [maxcent, maxpos]]
bandbounds = bandnum[1:] - bandnum[:-1]
for bpos in np.nonzero(bandbounds)[0]:
if cytobanddata[0][bpos] == -1 or \
cytobanddata[0][bpos + 1] == -1:
continue
bposp = bpos + 1
yvals = [bposp * factor] * 2
for i in range(2):
ax.plot(xvalss[i], yvals, '0.3', lw=0.5)
ax.plot(yvals, xvalss[i], '0.3', lw=0.5)
maskval = 0.5
bgmask = np.zeros((nbins, nbins)) + 0.3
bgmask[centmask] = maskval
bgmask[:, centmask] = maskval
_plot_heatmap(bgmask, ax, res, cmap='binary', vlims=[0.0, 1.0])
# kab proper
cmap = plt.get_cmap('jet')
labrescale = (lab - np.min(lab[lab > 0.0])) / (np.max(lab) -
np.min(lab[lab > 0.0]))
if scale == 'pow':
labrescale = labrescale ** n
elif scale == 'log':
labrescale = np.log(labrescale)
else:
print 'Invalid scale!'
clrarray = cmap(labrescale)
tsetsort = np.sort(tset)
for i, t1 in enumerate(tsetsort):
xval = (mapping[t1] + 0.5) * res / 1.0e6
for j, t2 in enumerate(tsetsort):
yval = (mapping[t2] + 0.5) * res / 1.0e6
if i == j:
ax.scatter(xval, yval, marker='+', c='k', s=s)
else:
ax.scatter(xval, yval, marker='o', c=clrarray[i, j],
s=s * 0.6, lw=0, alpha=1.0)
ax.set_axis_bgcolor((0.8, 0.8, 0.8))
ax.set_xlim(0, nbins * res / 1.0e6)
ax.set_ylim(nbins * res / 1.0e6, 0)
return
def _plot_lab_block(lab, qAi, tset, ax, pars, mappingdata,
scale='pow', n=1.0, cmap=None, s=None,
badcolor=(1.0, 1.0, 1.0, 0.0)):
"""
Plot effective inter-target Laplacian.
Mode: Represent by blocks with area corresponding to most
strongly-associated target.
"""
res = pars['res']
ntarget, npx = qAi.shape
mapping, nbins = mappingdata
if s is None:
s = 20
mvals = np.argmax(qAi, axis=0)
# Version 1
lij = np.zeros((npx, npx))
for i in range(npx):
for j in range(npx):
if mvals[i] == mvals[j]:
lij[i, j] = np.nan
else:
lij[i, j] = lab[mvals[i], mvals[j]]
#rowsum = np.sum(kij, axis=0)
#kijp = kij / np.outer(rowsum, rowsum)
lijpad = _build_fullarray(lij, mappingdata, np.nan)
if scale == 'pow':
lijpad = lijpad ** n
elif scale == 'log':
lijpad = np.log(lijpad)
else:
print 'Invalid scale!'
_plot_heatmap(lijpad, ax, res, cmap=cmap, badcolor=badcolor)
# Mark out targets
for tgt in tset:
xval = (mapping[tgt]) * res / 1.0e6
ax.scatter(xval, xval, marker='+', c='r', s=s)
ax.set_xlim(0, nbins * res / 1.0e6)
ax.set_ylim(nbins * res / 1.0e6, 0)
def _plot_lab_block_2(lab, membership, ax, pars, mappingdata,
scale='pow', n=1.0, cmap=None, s=None,
badcolor=(1.0, 1.0, 1.0, 0.0), returnimg=False):
"""
Plot effective inter-target Laplacian.
Mode: Represent by blocks with area corresponding to most
strongly-associated target.
Note: membership function has been padded with empty data rows.
"""
res = pars['res']
npart, npx = membership.shape
mapping, nbins = mappingdata
if s is None:
s = 20
mvals = [(np.argmax(v) if np.sum(v) > 0.0 else -1) for v in membership.T]
#mvals = np.argmax(membership, axis=0)
# Version 1
lij = np.zeros((npx, npx)) + np.nan
for i in range(npx):
if mvals[i] == -1:
continue
for j in range(npx):
if mvals[j] == -1:
continue
if mvals[i] == mvals[j]:
lij[i, j] = np.nan
else:
lij[i, j] = lab[mvals[i], mvals[j]]
if scale == 'pow':
lij = lij ** n
elif scale == 'log':
lij = np.log(lij)
else:
print 'Invalid scale!'
img = _plot_heatmap(lij, ax, res, cmap=cmap, badcolor=badcolor,
returnimg=returnimg)
ax.set_xlim(0, nbins * res / 1.0e6)
ax.set_ylim(nbins * res / 1.0e6, 0)
if returnimg:
return lij, img
else:
return lij
def _plot_lab_block_3(lab, membership, ax, pars, mappingdata,
scale='pow', n=1.0, cmap=None, s=None,
badcolor=(1.0, 1.0, 1.0, 0.0)):
"""
Plot effective inter-target Laplacian.
Mode: Represent by blocks with area corresponding to most
strongly-associated target.
Note: membership function has been padded with empty data rows.
"""
res = pars['res']
npart, npx = membership.shape
mapping, nbins = mappingdata
if s is None:
s = 20
mvals = np.array([(np.argmax(v) if np.sum(v) > 0.0 else -1)
for v in membership.T])
bounds = np.abs(np.array([-2] + list(mvals)) -
np.array(list(mvals) + [-2])) > 0
bounds = np.nonzero(bounds)[0]
sizes = bounds[1:] - bounds[:-1]
npieces = len(sizes)
lims = [bounds[i:i + 2] for i in range(npieces)]
pieceids = [int(mvals[l[0]]) for l in lims]
edges = [0.0] + list(np.cumsum(sizes) * res / 1.0e6)
labexpand = np.zeros((npieces, npieces))
for i in range(npieces):
for j in range(npieces):
labexpand[i, j] = np.nan if ((pieceids[i] < 0 or pieceids[j] < 0)
or (pieceids[i] == pieceids[j])) \
else lab[pieceids[i], pieceids[j]]
#ax.pcolor(edges, edges, labexpand)
_plot_heatmap_uneven(labexpand, edges, ax, res, cmap=cmap,
badcolor=badcolor)
print len(edges), labexpand.shape
#mvals = np.argmax(membership, axis=0)
# Version 1
#lij = np.zeros((npx, npx)) + np.nan
#for i in range(npx):
#if mvals[i] == -1:
#continue
#for j in range(npx):
#if mvals[j] == -1:
#continue
#if mvals[i] == mvals[j]:
#lij[i, j] = np.nan
#else:
#lij[i, j] = lab[mvals[i], mvals[j]]
#if scale == 'pow':
#lij = lij ** n
#elif scale == 'log':
#lij = np.log(lij)
#else:
#print 'Invalid scale!'
#_plot_heatmap(lij, ax, res, cmap=cmap, badcolor=badcolor)
ax.set_xlim(0, nbins * res / 1.0e6)
ax.set_ylim(nbins * res / 1.0e6, 0)
return
def _plot_lab_block_2chr(lab, targetmembership, tsets, ax, pars, mappingdatas,
scale='pow', n=1.0, cmap=None, s=None,
badcolor=(1.0, 1.0, 1.0, 0.0)):
"""
Plot effective inter-target Laplacian.
Mode: Represent by blocks with area corresponding to most
strongly-associated target.
"""
res = pars['res']
nchrs = len(tsets)
nfullbins = [md[1] for md in mappingdatas]
ntarget, npx = targetmembership.shape
if s is None:
s = 20
mvals = np.array([np.argmax(v) if np.max(v) > 0.0 else np.nan
for v in targetmembership.T])
# Version 1
lij = np.ones((npx, npx)) * np.nan
for i in range(npx):
for j in range(npx):