-
Notifications
You must be signed in to change notification settings - Fork 78
/
HiCPlotter.py
2171 lines (1812 loc) · 103 KB
/
HiCPlotter.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
'''---------------------------------------------------------------------------------------
HiCPlotter: plotting Hi-C data with additional datasets
------------------------------------------------------------------------------------------'''
import os,sys
import platform
if platform.platform().split('-')[0]=='Linux' or platform.platform().split('-')[0]=='Windows':
import matplotlib
matplotlib.use('Agg')
from math import sqrt, isnan, floor, ceil, pi
from numpy import log2, array, max
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.ticker import MultipleLocator
from matplotlib.patches import Polygon, Rectangle, Circle
from scipy.signal import argrelextrema
from scipy import ndimage
import scipy.sparse as sps
import matplotlib.pyplot as plt
import numpy as np
import argparse
import bisect
import warnings
import logging
version = "0.8.1"
def read_HiCdata(filename,header=0,footer=0,clean_nans=True,smooth_noise=0.5,ins_window=5,rel_window=8,plotInsulation=True,plotTadDomains=False,randomBins=False):
'''
load Hi-C interaction matrix from text file
parameters:
filename: file name. format "chr\tstart\tend\tdata1\tdata2\t..."
clean_nans: replace nan with 0's in rows/columns from all matrix
smooth_noise: variable values under will be replace with 0's to clean noise in the matrix
ins_window: window size for scanning diagonal of the matrix for insulation scores
rel_window: relative extrama extension size - will be extend to both directions
returns:
matrix: data matrix over the selected set of chromosome.
nums: insulation scores array.
tricks: putative insulator sites
'''
try:
matrix = np.genfromtxt(filename,skip_header=header,filling_values='0',skip_footer=footer)
except IOError:
print >>sys.stderr, 'cannot open', filename
raise SystemExit
if not randomBins and len(matrix[:,1]) != len(matrix[1,:]):
print len(matrix[:,1]),len(matrix[1,:])
print >>sys.stderr, 'unbalanced matrix('+filename+')! input should be a square matrix'
raise SystemExit
if plotInsulation or plotTadDomains and not randomBins: nums,tricks=insulation(matrix,ins_window,rel_window)
else: nums=[];tricks=[];
if clean_nans: matrix[np.isnan(matrix)]=0
matrix[matrix<smooth_noise]=0
return matrix,nums,tricks
def read_sparseHiCdata(filename,chromosome,bedFile,startBin,endBin,wholeGenome=False,smooth_noise=0.5,ins_window=5,rel_window=8,plotInsulation=True,plotTadDomains=False,randomBins=False,anchor=0):
'''
load Hi-C interaction matrix from triple-column sparse file
parameters:
filename: file name. format "bin1\tbin2\tdata\n..."
chromosome: plotting which chromosome
bedFile: a bed file for locations of bins
startBin: starting bin - 0 zero-based
endBin: end point for the plot
wholeGenome: for plotting more than one chromosome interactions. chromosome parameter will be used for until which chromosome interactions will be plotted.
smooth_noise: variable values under will be replace with 0's to clean noise in the matrix
ins_window: window size for scanning diagonal of the matrix for insulation scores
rel_window: relative extrama extension size - will be extend to both directions
returns:
matrix: data matrix over the selected set of chromosome.
nums: insulation scores array.
tricks: putative insulator sites
'''
chromosomes = {}
fourClike = []
try:
bed = open(bedFile,'r')
except IOError:
print >>sys.stderr, 'cannot open', bedFile
raise SystemExit
for line in bed.readlines():
tags = line.strip().split("\t")
if tags[0]=='chrM':continue
if tags[0] not in chromosomes.keys():
chromosomes[tags[0]]=[]
chromosomes[tags[0]].append(int(tags[3]))
else: chromosomes[tags[0]].append(int(tags[3]))
if not wholeGenome:
clast = chromosomes[chromosome][-1]-1
start = chromosomes[chromosome][0]+startBin
end = chromosomes[chromosome][0]+endBin
end=clast if end == chromosomes[chromosome][0] else end
if end > clast: end=clast
if start > clast: start=chromosomes[chromosome][0]
else:
start = 1
end = chromosomes[chromosome][-1]
clast = end
length = end-start+1
mtx = sps.dok_matrix((length, length), dtype=np.int)
try:
matrixFile = open(filename,'r')
except IOError:
print >>sys.stderr, 'cannot open', filename
raise SystemExit
for line in matrixFile.xreadlines():
tags = line.strip().split("\t")
if int(tags[0]) <= end and int(tags[0])>=start :
if int(tags[1]) <= end and int(tags[1])>=start :
mtx[int(tags[0])-start, int(tags[1])-start] = int(round(float(tags[2])))
mtx[int(tags[1])-start, int(tags[0])-start] = int(round(float(tags[2])))
if int(tags[0]) > end: break
matrix = mtx.todense()
if anchor > 0:
for element in range(0,len(matrix)-1): fourClike.append(matrix[anchor-startBin,element])
if plotInsulation or plotTadDomains and not wholeGenome: nums,tricks=insulation(matrix,ins_window,rel_window,True,startBin)
else: nums=[];tricks=[];
#matrix[matrix<smooth_noise]=0
return matrix,nums,tricks,clast-chromosomes[chromosome][0]+1,fourClike
def read_Cooldata(filename,chromosome,resolution,startBin,endBin,wholeGenome=False,smooth_noise=0.5,ins_window=5,rel_window=8,plotInsulation=True,plotTadDomains=False,randomBins=False):
import cooler
c = cooler.Cooler(filename)
if resolution != c.binsize:
print 'Be aware: resolution is not matching with the cool file!'
raise SystemExit
start = startBin * resolution
if endBin == 0:
print 'Be aware: you did not enter a valid genomic range!'
end = c.chromsizes[chromosome]
else:
end = endBin * resolution
query = chromosome + ':' + str(start) + '-' + str(end)
matrix = c.matrix(balance=True).fetch(query)
if plotInsulation or plotTadDomains and not wholeGenome: nums,tricks=insulation(matrix,ins_window,rel_window,False,startBin,True)
else: nums=[];tricks=[];
return matrix,nums,tricks
def read_bedGraph(filename,resolution,chromosome): # add stopping after certain chromosome passed
'''
reads bedGraph files for various file type plottings
parameters:
filename: file name. format could be either "chr\tstart\tend" or "chr\tstart\tend\tvalue..."
resolution: bin size for the matrix
returns:
x_scores = location along the given chromosome - start sites
x_scores2 = location along the given chromosome - end sites
y_scores = signal scores for the assay
colors = allow for colors option
'''
try:
fone=open(filename,'r')
except IOError:
print >>sys.stderr, 'cannot open', filename
raise SystemExit
x_scores=[]
x_scores2=[]
y_scores=[]
colors=[]
texts=[]
for line in fone.xreadlines():
tags = line.strip().split("\t")
if tags[0]==chromosome:
x_scores.append(float(tags[1])/resolution)
x_scores2.append(float(tags[2])/resolution)
if len(tags) > 3:
y_scores.append(float(tags[3]))
if len(tags) > 4:
hex = '#%02x%02x%02x' % (int(tags[4].split(',')[0]), int(tags[4].split(',')[1]), int(tags[4].split(',')[2]))
colors.append(hex)
if len(tags) > 5:
texts.append(tags[5])
if len(y_scores) !=0 and len(y_scores)!=len(x_scores):
print >>sys.stderr, 'BedGraph('+filename+') has some missing values'
raise SystemExit
if len(x_scores)==0 or len(x_scores2)==0:
print >>sys.stderr, 'BedGraph('+filename+') has some missing values'
raise SystemExit
# color and text controls
return x_scores,x_scores2,y_scores,colors,texts
def read_peakFile(filename,resolution,chromosome): # add stopping after certain chromosome passed
'''
reads peak files for annotating the matrix
parameters:
filename: file name. format could be either "chr\tstart\tend" or "chr\tstart\tend\tvalue..."
resolution: bin size for the matrix
returns:
origin_x = location along x axis on the given chromosome
origin_y = location along y axis on the given chromosome
radius = radius of circle
colors = allow for colors option
'''
try:
fone=open(filename,'r')
except IOError:
print >>sys.stderr, 'cannot open', filename
raise SystemExit
origin_x=[]
origin_y=[]
radius=[]
colors=[]
for line in fone.xreadlines():
tags = line.strip().split("\t")
if tags[0]==chromosome and tags[3]==chromosome:
x1 = float(tags[1])/resolution
x2 = float(tags[2])/resolution
origin_x.append(x1+(x2-x1)/2)
radius.append((x2-x1)/2)
y1 = float(tags[4])/resolution
y2 = float(tags[5])/resolution
origin_y.append(y1+(y2-y1)/2)
if len(tags) > 5:
hex = '#%02x%02x%02x' % (int(tags[6].split(',')[0]), int(tags[6].split(',')[1]), int(tags[6].split(',')[2]))
colors.append(hex)
if len(origin_y) !=0 and len(origin_x)!=len(origin_y):
print >>sys.stderr, 'Peak file ('+filename+') has some missing values'
raise SystemExit
if len(origin_x)==0 or len(origin_y)==0:
print >>sys.stderr, 'Peak file ('+filename+') has some missing values'
raise SystemExit
# color control
return origin_x,origin_y,radius,colors
def read_epilogos(filename,resolution,chromosome,start,end): # add stopping after certain chromosome passed
'''
reads epilogos file format (http://compbio.mit.edu/epilogos/)
# you can download epilogos files for human genome from http://egg2.wustl.edu/roadmap/data/byFileType/chromhmmSegmentations/ChmmModels/epilogos/
parameters:
filename: file name. format could be either "chr\tstart\tend" or "chr\tstart\tend\tvalue..."
resolution: bin size for the matrix
start: starting bin - 0 zero-based
end: end point for the plot
returns:
x_scores = location along the given chromosome - start sites
y_dict = a dictionary containing enrichments of each states for a given location
'''
try:
fone=open(filename,'r')
except IOError:
print >>sys.stderr, 'cannot open', filename
raise SystemExit
x_scores=[]
y_dict={}
start = resolution * start
end = resolution * end
for line in fone.xreadlines():
tags = line.strip().split("\t")
if tags[0]==chromosome and int(tags[1])>start and int(tags[2]) < end:
x_scores.append(float(tags[1])/resolution)
cols = np.array(tags[3].split(':')[2].split(','))
for x in range(1,len(cols)):
if x % 2 == 1 and cols[x].replace('[','').replace(']','').replace(' ','') not in y_dict.keys():
y_dict[cols[x].replace('[','').replace(']','').replace(' ','')]=[]
y_dict[cols[x].replace('[','').replace(']','').replace(' ','')].append(float(cols[x-1].replace('[','').replace(']','').replace(' ','')))
elif x % 2 == 1 :
y_dict[cols[x].replace('[','').replace(']','').replace(' ','')].append(float(cols[x-1].replace('[','').replace(']','').replace(' ','')))
if len(y_dict.keys()) ==0:
print >>sys.stderr, 'Epilogos File('+filename+') has some missing values'
raise SystemExit
return x_scores,y_dict
def read_genes(filename,resolution,chromosome,start,end):
'''
reads a sorted gene location file
parameters:
filename: file name. format could be either "chr\tstart\tend" or "chr\tstart\tend\tvalue..."
resolution: bin size for the matrix
start: starting bin - 0 zero-based
end: end point for the plot
returns:
genes : a dictionary for each gene and respective plot locations
row_count: a number to indicate how many rows will be used
row_genes: a dictionary for end locations of the genes on each row
'''
try:
fone=open(filename,'r')
except IOError:
print >>sys.stderr, 'cannot open', filename
raise SystemExit
start = resolution * start
end = resolution * end
minDist = 8000
genes = {}
row_list = []
row_genes = {}
current_start=0;current_end=0;prev_end=0;
for line in fone.xreadlines():
tags = line.strip().split("\t")
if tags[0]==chromosome:
if int(tags[1]) >= start and int(tags[2]) <= end and tags[1]+'-'+tags[2] not in genes.keys():
if len(row_list)==0:
current_start = int(tags[1])
current_end = int(tags[2])
prev_start = int(tags[1])
genes[tags[1]+'-'+tags[2]]=[]
genes[tags[1]+'-'+tags[2]].append(1)
genes[tags[1]+'-'+tags[2]].append(tags[3])
row_list.append(current_end)
row_genes[1]=[]
row_genes[1].append(current_end)
else:
if prev_start > int(tags[1]):
print prev_end, int(tags[1])
print >>sys.stderr, 'Gene File ('+filename+') is not sorted.'
raise SystemExit
else:
current_end = int(tags[2])
current_start = int(tags[1])
execute=0
genes[tags[1]+'-'+tags[2]]=[]
for item in range(0,len(row_list)):
if current_start > row_list[item]+minDist:
row_list[item]=current_end
execute=1
genes[tags[1]+'-'+tags[2]].append(item+1)
if item+1 not in row_genes.keys(): row_genes[item+1]=[]
row_genes[item+1].append(current_end)
break
if execute == 0:
genes[tags[1]+'-'+tags[2]].append(len(row_list)+1)
row_list.append(current_end)
if len(row_list) not in row_genes.keys(): row_genes[len(row_list)]=[]
row_genes[len(row_list)].append(current_end)
genes[tags[1]+'-'+tags[2]].append(tags[3])
if len(tags)>5:
genes[tags[1]+'-'+tags[2]].append(tags[4])
genes[tags[1]+'-'+tags[2]].append(tags[5])
genes[tags[1]+'-'+tags[2]].append(tags[6])
if len(tags)>7:
hex = '#%02x%02x%02x' % (int(tags[7].split(',')[0]), int(tags[7].split(',')[1]), int(tags[7].split(',')[2]))
genes[tags[1]+'-'+tags[2]].append(hex)
if len(tags)>8:
genes[tags[1]+'-'+tags[2]].append(float(tags[8]))
prev_start = current_start
if len(genes.keys()) ==0:
print >>sys.stderr, 'Gene File ('+filename+') has some missing values'
raise SystemExit
return genes,len(row_list)+1,row_genes
def where(start,end,arr):
"""Find where the start location and end location indexes in an array"""
astart = bisect.bisect_left(arr, start)
aend = bisect.bisect_right(arr[start:], end) + start
return astart, aend
def get_ellipse_coords(a=0.0, b=0.0, x=0.0, y=0.0, angle=0.0, k=2):
""" Draws an ellipse using (360*k + 1) discrete points
k = 1 means 361 points (degree by degree)
a = major axis distance,
b = minor axis distance,
x = offset along the x-axis
y = offset along the y-axis
angle = clockwise rotation [in degrees] of the ellipse;
* angle=0 : the ellipse is aligned with the positive x-axis
* angle=30 : rotated 30 degrees clockwise from positive x-axis
this function is obtained from : http://scipy-central.org/item/23/2/plot-an-ellipse
"""
pts = np.zeros((int(360*k+1), 2))
beta = -angle * np.pi/180.0
sin_beta = np.sin(beta)
cos_beta = np.cos(beta)
alpha = np.radians(np.r_[0.:360.:1j*(360*k+1)])
sin_alpha = np.sin(alpha)
cos_alpha = np.cos(alpha)
pts[:, 0] = x + (a * cos_alpha * cos_beta - b * sin_alpha * sin_beta)
pts[:, 1] = y + (a * cos_alpha * sin_beta + b * sin_alpha * cos_beta)
return pts
def insulation(matrix,w=5,tadRange=10,triple=False,mstart=0,cooler=False):
'''
calculate relative minima in a given matrix
parameters:
matrix: data matrix over the selected set of chromosomes.
start: retain after x-th bin.
end: continues until x-th bin.
w: window size for scanning diagonal of the matrix for insulation scores
tadRange: relative extrama extension size - will be extend to both directions
returns:
nums: insulation scores array.
tricks: putative insulator sites
'''
start=0;end=len(matrix)-1
scores = []
indexes = []
pBorders=[]
for i in xrange(start,end,1):
diag=0;counter=0
for j in xrange(i,i+w):
if j == end: break
else:
if triple or cooler:
if isnan(matrix[j,j-2*counter]): diag +=0 # pad with zeros for nan
else: diag += matrix[j,j-2*counter]
else:
if isnan(matrix[j][j-2*counter]): diag +=0 # pad with zeros for nan
else: diag += matrix[j][j-2*counter]
counter+=1
scores.append(diag)
indexes.append(i)
arr= np.array(scores)
arr[arr == 0] = 10000
borders = argrelextrema(arr, np.less,order=tadRange) #also try this function scipy.signal.find_peaks_cwt
regions = borders[0]
for item in range(0,len(regions)-1):
if item == 0: #check the first boundary
if len(np.nonzero(scores[slice(regions[item],regions[item+1])])[0]) < regions[item+1]-regions[item]-w/2-1:
current = np.nonzero(scores[slice(0,regions[item])])[0]
if current[0] not in pBorders and current[0]+1 not in regions: pBorders.append(current[0])
if regions[item] not in pBorders and regions[item]+1 not in regions: pBorders.append(regions[item])
current = np.nonzero(scores[slice(regions[item],regions[item+1])])[0]
if regions[item]+current[-1] not in pBorders and regions[item]+current[-1]+1 not in regions: pBorders.append(regions[item]+current[-1])
elif regions[item]+current[-1] not in pBorders and regions[item]+current[-1]+1 in regions: pBorders.append(regions[item]+current[-2]) # to get closer bin but needs to be improved
else:
if regions[item] not in pBorders and regions[item]+1 not in regions: pBorders.append(regions[item])
else:
current = np.nonzero(scores[slice(regions[item],regions[item+1])])[0]
if len(current) >= regions[item+1]-regions[item]-w/2-1:
if regions[item] not in pBorders and regions[item]+1 not in regions: pBorders.append(regions[item])
elif len(current) < regions[item+1]-regions[item]-w/2-1:
current = np.nonzero(scores[slice(regions[item],regions[item]+tadRange)])[0]
if regions[item] not in pBorders and regions[item]+1 not in regions: pBorders.append(regions[item])
if regions[item]+current[0] not in pBorders and regions[item]+current[0]+1 not in regions: pBorders.append(regions[item]+current[0])
current = np.nonzero(scores[slice((regions[item]+tadRange),regions[item+1])])[0]
if len(current)==0:
if regions[item+1] not in pBorders and regions[item]+1 not in regions: pBorders.append(regions[item+1])
else:
if regions[item]+tadRange+current[0] not in pBorders and regions[item]+tadRange+current[0]+1 not in regions: pBorders.append(regions[item]+tadRange+current[0])
if regions[item+1] not in pBorders and regions[item+1]+1 not in regions: pBorders.append(regions[item+1])
if regions[-1] not in pBorders: pBorders.append(regions[-1])
if len(matrix) not in pBorders: pBorders.append(len(matrix))
if triple: pBorders=map(lambda x:x+mstart, pBorders)
if cooler: pBorders=map(lambda x:x+mstart, pBorders)
return scores, pBorders
def HiCplotter(files=[],names=[],resolution=100000,chromosome='',output='',histograms=[],histLabels=[],fillHist=[],histMax=[],verbose=False,fileHeader=0,fileFooter=0,matrixMax=0,histColors=[],barPlots=[],barLabels=[],plotGenes='',superImpose=False,anchor=0,anchorMax=0,\
start=0,end=0,tileLabels=[],tilePlots=[],tileColors=[],tileText=False,arcLabels=[],arcPlots=[],arcColors=[],peakFiles=[],epiLogos='',window=5,tadRange=8,tripleColumn=False,bedFile='',barColors=[],dPixels=200,compareEx='',compareSm='',upSide=True,Cooler=False,\
smoothNoise=0.5,cleanNANs=True,plotTriangular=True,plotTadDomains=False,randomBins=False,wholeGenome=False,plotPublishedTadDomains=False,plotDomainsAsBars=False,imputed=False,barMax=[],spine=True,plotDomainTicks=True,triangularHeight=False,notLog=False,\
highlights=0,lHighlights=0,highFile='',heatmapColor=3,highResolution=True,plotInsulation=True,plotCustomDomains=False,publishedTadDomainOrganism=True,customDomainsFile=[],compare=False,pair=False,domColors=[],oExtension='',geneLabels=True,dark=False):
'''
plot the interaction matrix with additional datasets
Required parameters:
files (-f) : a list of filenames to be plotted.
name (-n) : a list of labels for the experiment.
chr (-chr) : chromosome to be plotted.
output (-o) : prefix for the output file.
Optional parameters:
verbose (-v) : print version and arguments into a file.
tripleColumn (-tri) : a boolean if input file is from HiC-Pro pipeline.
dark (-d) : a boolean to use black background for the output.
notLog (-nl) : a boolean to plot raw matrix without log2 transformation.
bedFile (-bed) : a file name for bin annotations, if -tri parameter is set.
plotGenes (-g) : a sorted bed file for plotting the locations of the genes.
geneLabels (-gl) : a boolean for plotting gene labels (1:default) or not (0).
histograms (-hist) : a list of filenames to be plotted as histogram.
histLabels (-h) : a list of labels for the histograms.
fillHist (-fhist) : a list whether each histogram will be filled (1) or not (0:default).
histColors (-hc) : a list of hexadecimal number for histogram filling colors.
histMax (-hm) : a list of integer for maximum values of histograms.
superImpose (-si) : a boolean to overlap two histogram files inside the same track (default:0) enable(1).
start (-s) : retain after x-th bin (0:default).
end (-e) : continues until x-th bin (default: length of the matrix).
resolution (-r) : resolution of the bins (default: 100000).
matrixMax (-mm) : an integer value for the interaction matrix heatmap scale upper-limit.
barPlots (-b) : a list of filenames to be plotted as bar plots.
barLabels (-bl) : a list of labels for the bar plots.
barColors (-bc) : a list of hexadecimal numbers for coloring the bar plots.
barMax (-bm) : a list of integer for maximum values of bar plots.
tilePlots (-t) : a list of filenames to be plotted as tile plots.
tileLabels (-tl) : a list of labels for the tile plots.
tileColors (-tc) : a list of hexadecimal numbers for coloring the tile plots.
tileText (-tt) : a boolean whether text will be displayed above tiles (0:default) or not (1).
arcPlots (-a) : a list of filenames to be plotted as arc plots.
arcLabels (-al) : a list of labels for the arc plots.
arcColors (-ac) : a list of hexadecimal numbers for coloring the arc plots.
highlights (-high) : a boolean for enabling vertical highlights on the plot (0:default), enable(1).
lHighlights (-lhigh) : a boolean for enabling lateral highlights on the plot (0:default), enable(1).
highFile (-hf) : a file name for a bed file to highlight selected intervals.
peakFiles (-peak) : a list of filenames to be plotted on the matrix.
epiLogos (-ep) : a filename to be plotted as Epilogos format.
oExtension (-ext) : an extension name for the output file format - default jpeg.
imputed (-im) : a boolean if imputed epilogos will be plotted. (default:0 for observed)
spine (-spi) : a boolean to remove top and left borders for each tracks (default:0) enable(1).
compare (-c) : a boolean to plot log2 compare first two matrices (default:0) enable(1).
compareExt (-ce) : comma separated two integers for log2 comparison matrix color spectrum (e.g. 2,4 for -2 to 4).
compareSm (-cs) : comma separated two integers for log2 comparison matrix smoothing (e.g. for 0,2 values between 0-2 will be white in image).
pair (-p) : a boolean to plot log2 pair-wise matrix comparisons (default:0) enable(1).
window (-w) : an integer of distance to calculate insulation score.
tadRange (-tr) : an integer of window to calculate local minima for TAD calls.
fileHeader (-fh) : an integer for how many lines should be ignored in the matrix file (0:default).
fileFooter (-ff) : an integer for how many lines should be skipped at the end of the matrix file (0:default).
smoothNoise (-sn) : a floating-point number to clean noise in the data.
heatmapColor (-hmc) : an integer for choosing heatmap color codes: Greys(0), Reds(1), YellowToBlue(2), YellowToRed(3-default), Hot(4), BlueToRed(5).
cleanNANs (-cn) : a boolean for replacing NaNs in the matrix with zeros (1:default) or not (0).
plotTriangular (-ptr) : a boolean for plotting rotated half matrix (1:default) or not (0).
domColors (-dc) : a list of hexadecimal numbers for coloring the domain plots.
plotTadDomains (-ptd) : a boolean for plotting TADs identified by HiCPlotter (1) or not (0:default).
plotPublishedTadDomins (-pptd) : a boolean for plotting TADs from Dixon et, al. 2012 (1:default) or not (0).
plotDomainsAsBars (-ptdb) : a boolean for plotting TADs as bars (1) instead of triangles (0:default)
highResolution (-hR) : a boolean whether plotting high resolution (1:default) or not (0).
dPixels (-dpi) : an integer to determine dots per inch in matrix, higher values for higher resolution (default:200).
plotInsulation (-pi) : a boolean for plotting insulation scores (0:default) or plot (1).
randomBins (-rb) : a boolean for plotting random resolution data (1:default) or not (0).
wholeGenome (-wg) : a boolean for plotting whole genome interactions (1:default) or not (0).
plotCustomDomains (-pcd) : a list of file names to be plotted beneath the matrix.
publishedTadDomainOrganism (-ptdo) : a boolean for plotting human (1:default) or mouse (0) TADs from Dixon et, al. 2012.
customDomainsFile (-pcdf) : a list of filenames to be plotted as TADs for each experiments.
'''
orientation = 'lower'
if upSide : orientation = 'upper'
numOfcols = len(files)
numOfrows = 4
if anchor > 0: numOfrows+=1
if plotTriangular: numOfrows+=2
if len(plotGenes)>0: numOfrows+=2
if plotTadDomains: numOfrows+=1
if plotInsulation: numOfrows+=1
if epiLogos: numOfrows+=1
if len(histograms)>0 and not superImpose: numOfrows+=len(histograms[0].split(','))
if superImpose : numOfrows+=1
if len(barPlots)>0: numOfrows+=len(barPlots[0].split(','))
if len(tilePlots)>0: numOfrows+=len(tilePlots[0].split(','))
if len(arcPlots)>0: numOfrows+=len(arcPlots[0].split(','))
if plotCustomDomains or plotPublishedTadDomains and not plotTadDomains: numOfrows+=1
if compare and not pair: numOfcols+=1; files.append('pseudo'); matrix1=[];matrix2=[]
if compare and pair: numOfrows+=(len(files)-1)*4
if pair: marray=[]
if len(files) == 1:
fig=plt.figure(figsize=(numOfcols*5+1, numOfrows+numOfrows/2+0.5), facecolor='w', edgecolor='w')
fig.set_size_inches(numOfcols*5+1, numOfrows+numOfrows/2+0.5)
else:
fig=plt.figure(figsize=(numOfcols*5+2.5, numOfrows+numOfrows/2+0.5), facecolor='w', edgecolor='w')
fig.set_size_inches(numOfcols*5+2.5, numOfrows+numOfrows/2+0.5)
if superImpose: fig.subplots_adjust(hspace=0.48,wspace=1.25)
else: fig.subplots_adjust(hspace=0.48,wspace=1.0)
if dark : plt.style.use('dark_background')
ymaxlims = []
yminlims = []
cmatrix = 0
ins_score = 0
mlength = 0
cmaps = ['Greys','Reds','YlOrBr','YlOrRd','hot']
h_start = []
h_end = []
if highlights or lHighlights:
h_start,h_end,_,_,_ = read_bedGraph(highFile,resolution,chromosome)
rlength = len(files)-1 if compare and not pair else len(files)
for exp in range(0,rlength):
rowcounter=0
if not tripleColumn and not Cooler:
matrix,nums,tricks=read_HiCdata(files[exp],fileHeader,fileFooter,cleanNANs,smoothNoise,window,tadRange,plotInsulation,plotTadDomains,randomBins)
end=len(matrix) if end == 0 else end
if end > len(matrix): end=len(matrix)
if start > len(matrix): start = 0
size=end-start
if exp == 0 : mlength = len(matrix)
elif len(matrix) != mlength and not randomBins:
print len(matrix), mlength
print >>sys.stderr, 'unbalanced matrix size of '+files[exp]+' compared to '+files[0]+' ! matrix sizes should be equal'
raise SystemExit
fourClike = []
for element in range(0,len(matrix)):
fourClike.append(matrix[anchor,element])
matrix=matrix[start:end,start:end]
elif Cooler:
matrix,nums,tricks=read_Cooldata(files[exp],chromosome,resolution,start,end,wholeGenome,smoothNoise,window,tadRange,plotInsulation,plotTadDomains,randomBins)
else:
if bedFile == '':
print >>sys.stderr, 'an annotation bed file is required for triple-column sparse input.'
raise SystemExit
matrix,nums,tricks,clength,fourClike=read_sparseHiCdata(files[exp],chromosome,bedFile,start,end,wholeGenome,smoothNoise,window,tadRange,plotInsulation,plotTadDomains,randomBins,anchor)
if end > clength: end=clength
if start > clength: start = 0
end=clength if end == 0 else end
size=end-start
length = len(matrix)
name=names[exp]
schr=chromosome.replace("chr","")
''' MAIN matrix plotting '''
ax1 = plt.subplot2grid((numOfrows, 4*len(files)), (0, exp*4), rowspan=4,colspan=4)
ax1.set_title(('%s') % (name))
if exp==0:
if not randomBins and not wholeGenome: ax1.set_ylabel('log2(interaction matrix) - %s Mb (resolution: %sKb)' % (chromosome , resolution/1000))
elif randomBins: ax1.set_ylabel('log2(interaction matrix) - %s (Genomic Bins)' % (chromosome))
elif wholeGenome: ax1.set_ylabel('')
with np.errstate(divide='ignore'): cmatrix = log2(pow(2, ceil(log2(max(matrix))/log2(2))))
if matrixMax !=0: cmatrix = matrixMax
if compare and not pair: matrix1=matrix
if exp==1 and compare and not pair: matrix2=matrix
if compare and pair: marray.append(matrix)
ax1.set_ylim(int(start or 1) - 0.5,int(start or 1) + length - 0.5)
ax1.set_xlim(int(start or 1) - 0.5,int(start or 1) + length - 0.5)
if not wholeGenome:
if heatmapColor < 5:
if not notLog:
with np.errstate(divide='ignore'): img=ax1.imshow(log2(matrix),cmap=plt.get_cmap(cmaps[heatmapColor]),origin=orientation,interpolation="nearest",extent=(int(start or 1) - 0.5,\
int(start or 1) + length - 0.5,int(start or 1) - 0.5,int(start or 1) + length - 0.5),aspect='auto')
else:
with np.errstate(divide='ignore'): img=ax1.imshow(matrix,cmap=plt.get_cmap(cmaps[heatmapColor]),origin=orientation,interpolation="nearest",extent=(int(start or 1) - 0.5,\
int(start or 1) + length - 0.5,int(start or 1) - 0.5,int(start or 1) + length - 0.5),aspect='auto')
elif heatmapColor == 3:
cmap = plt.get_cmap(cmaps[heatmapColor])
cmap.set_over('black')
if not notLog:
with np.errstate(divide='ignore'): img=ax1.imshow(log2(matrix),cmap=cmap,origin=orientation,interpolation="nearest",extent=(int(start or 1) - 0.5,\
int(start or 1) + length - 0.5,int(start or 1) - 0.5,int(start or 1) + length - 0.5),aspect='auto')
else:
with np.errstate(divide='ignore'): img=ax1.imshow(matrix,cmap=cmap,origin=orientation,interpolation="nearest",extent=(int(start or 1) - 0.5,\
int(start or 1) + length - 0.5,int(start or 1) - 0.5,int(start or 1) + length - 0.5),aspect='auto')
else:
if not notLog:
with np.errstate(divide='ignore'): img=ax1.imshow(log2(matrix),origin=orientation,interpolation="nearest",extent=(int(start or 1) - 0.5,\
int(start or 1) + length - 0.5,int(start or 1) - 0.5,int(start or 1) + length - 0.5),aspect='auto')
else:
with np.errstate(divide='ignore'): img=ax1.imshow(matrix,origin=orientation,interpolation="nearest",extent=(int(start or 1) - 0.5,\
int(start or 1) + length - 0.5,int(start or 1) - 0.5,int(start or 1) + length - 0.5),aspect='auto')
else:
if heatmapColor < 5:
with np.errstate(divide='ignore'): img=ax1.imshow(log2(matrix),cmap=plt.get_cmap(cmaps[heatmapColor]),interpolation="nearest",extent=(int(start or 1) - 0.5,\
int(start or 1) + length - 0.5,int(start or 1) - 0.5,int(start or 1) + length - 0.5),aspect='auto')
elif heatmapColor == 3:
cmap = plt.get_cmap(cmaps[heatmapColor])
cmap.set_over('black')
with np.errstate(divide='ignore'): img=ax1.imshow(log2(matrix),cmap=cmap,origin=orientation,interpolation="nearest",extent=(int(start or 1) - 0.5,\
int(start or 1) + length - 0.5,int(start or 1) - 0.5,int(start or 1) + length - 0.5),aspect='auto')
else:
with np.errstate(divide='ignore'): img=ax1.imshow(log2(matrix),interpolation="nearest",extent=(int(start or 1) - 0.5,\
int(start or 1) + length - 0.5,int(start or 1) - 0.5,int(start or 1) + length - 0.5),aspect='auto')
plt.setp(ax1.get_xticklabels(), visible=False)
if len(peakFiles) > 0:
origin_x,origin_y,radius,colors = read_peakFile(peakFiles[exp],resolution,chromosome)
for citem in range(0,len(origin_x)):
if len(colors)==0: circle = Circle((origin_x[citem], origin_y[citem]), radius[citem], facecolor='none', edgecolor='black', linewidth=1, alpha=0.85)
else: circle = Circle((origin_x[citem], origin_y[citem]), radius[citem], facecolor='none', edgecolor=colors[citem], linewidth=3, alpha=0.85)
ax1.add_patch(circle)
divider = make_axes_locatable(ax1)
img.set_clim([0,cmatrix])
if wholeGenome : plt.setp(ax1.get_yticklabels(), visible=False)
ax1.get_yaxis().set_label_coords(-0.125,0.5)
if plotTadDomains and plotDomainTicks:
ax1.set_xticks(tricks, minor=True)
ax1.xaxis.grid(True,which='minor',linewidth=2)
if h_start > 0:
if highlights:
for item in range(0,len(h_start)):
if dark: ax1.axvspan(h_start[item], h_end[item], facecolor='#78D400', alpha=0.30, linestyle='dashed')
else: ax1.axvspan(h_start[item], h_end[item], facecolor='g', alpha=0.10, linestyle='dashed')
if lHighlights:
for item in range(0,len(h_start)):
if dark: ax1.axhspan(h_start[item], h_end[item], facecolor='#78D400', alpha=0.30, linestyle='dashed')
else: ax1.axhspan(h_start[item], h_end[item], facecolor='g', alpha=0.10, linestyle='dashed')
rowcounter+=4
ax1.get_xaxis().set_label_coords(0.5,-0.125)
if numOfrows <= rowcounter and not randomBins and not wholeGenome:
cax = divider.append_axes("bottom", size="2.5%", pad=0.9)
cbar = plt.colorbar(img, cax=cax, ticks=MultipleLocator(2.0), format="%.1f",orientation='horizontal',extendfrac='auto',spacing='uniform')
plt.setp(ax1.get_xticklabels(), visible=True)
ax1.set_xlabel('Chromosome %s Mb (resolution: %sKb)' % (schr , resolution/1000))
elif numOfrows <= rowcounter and randomBins: ax1.set_xlabel('Chromosome %s (Genomic Bins)' % (schr))
elif numOfrows <= rowcounter and wholeGenome:
ax1.set_xlabel('')
cax = divider.append_axes("bottom", size="2.5%", pad=0.9)
cbar = plt.colorbar(img, cax=cax, ticks=MultipleLocator(2.0), format="%.1f",orientation='horizontal',extendfrac='auto',spacing='uniform')
plt.setp(ax1.get_xticklabels(), visible=False)
else:
cax = divider.append_axes("bottom", size="2.5%", pad=0.1)
cbar = plt.colorbar(img, cax=cax, ticks=MultipleLocator(2.0), format="%.1f",orientation='horizontal',extendfrac='auto',spacing='uniform')
plt.setp(ax1.get_xticklabels(), visible=False)
''' Whole Genome matrix plotting '''
if wholeGenome and numOfrows > rowcounter:
print >>sys.stderr, 'Whole genome can be plotted only as matrix - this feature will be improved in future releases'
raise SystemExit
''' Triangular (Rotated Matrix) plotting '''
if plotTriangular:
ax2 = plt.subplot2grid((numOfrows, 4*len(files)), (rowcounter, exp*4), rowspan=2,colspan=4,sharex=ax1)
dst=ndimage.rotate(matrix,45,order=0,reshape=True,prefilter=False,cval=0)
matrix=[];
if not triangularHeight: height=length/5
else:
if triangularHeight <= length: height = triangularHeight
else: height = length/2
ax2.set_ylim(start+length/2,start+length/2+height)
ax2.set_xlim(int(start or 1) - 0.5,int(start or 1) + length - 0.5)
ax2.set(adjustable='box-forced')
if heatmapColor < 5:
with np.errstate(divide='ignore'): img=ax2.imshow(log2(dst),origin=orientation,cmap=plt.get_cmap(cmaps[heatmapColor]),interpolation="nearest",extent=(int(start or 1) - 0.5,\
int(start or 1) + length - 0.5,int(start or 1) - 0.5,int(start or 1) + length - 0.5),aspect='auto')
else:
with np.errstate(divide='ignore'): img=ax2.imshow(log2(dst),origin=orientation,interpolation="nearest",extent=(int(start or 1) - 0.5,\
int(start or 1) + length - 0.5,int(start or 1) - 0.5,int(start or 1) + length - 0.5),aspect='auto')
dst=[];
img.set_clim([0,cmatrix-1])
plt.setp(ax2.get_yticklabels(), visible=False)
if exp==0: ax2.set_ylabel('Triangular')
ax2.get_yaxis().set_label_coords(-0.125,0.5)
if plotTadDomains and plotDomainTicks:
ax2.set_xticks(tricks, minor=True)
ax2.xaxis.grid(True,which='minor',linewidth=2)
rowcounter+=2
if numOfrows <= rowcounter and not randomBins: ax2.set_xlabel('Chromosome %s Mb (resolution: %sKb)' % (schr , resolution/1000))
elif numOfrows <= rowcounter and randomBins: ax2.set_xlabel('Chromosome %s (Genomic Bins)' % (schr))
if h_start > 0:
for item in range(0,len(h_start)):
if dark: ax2.axvspan(h_start[item], h_end[item], facecolor='#78D400', alpha=0.30, linestyle='dashed')
else: ax2.axvspan(h_start[item], h_end[item], facecolor='g', alpha=0.10, linestyle='dashed')
ax2.spines['right'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax2.xaxis.set_ticks_position('bottom')
plt.gca().yaxis.set_major_locator(plt.NullLocator())
''' Random Bins matrix/triangular plotting '''
if randomBins and numOfrows > rowcounter:
print >>sys.stderr, 'Random bins data can be plotted only as matrix and triangular - this feature will be improved in future releases'
raise SystemExit
if anchor > 0: # normalization can be useful
ax3 = plt.subplot2grid((numOfrows, 4*len(files)), (rowcounter, exp*4), rowspan=1,colspan=4,sharex=ax1)
ax3.get_yaxis().set_label_coords(-0.125,0.5)
#print fourClike,len(fourClike),start,end
if not tripleColumn:
x_comps = np.arange(len(fourClike))
if dark: ax3.plot(x_comps,fourClike,color='white')
else: ax3.plot(x_comps,fourClike,color='black')
else:
if dark: ax3.plot(np.arange(start,end),fourClike,'white')
else: ax3.plot(np.arange(start,end),fourClike,'black')
ax3.locator_params(axis='y',tight=False, nbins=3)
ax3.set_xlim(int(start or 1) - 0.5,int(start or 1) + length - 0.5)
if anchorMax > 0: ax3.set_ylim(0,anchorMax)
else: ax3.set_ylim(0,max(fourClike)/20.0)
if exp==0: ax3.set_ylabel('4C-like')
if plotTadDomains and plotDomainTicks:
ax3.set_xticks(tricks, minor=True)
ax3.xaxis.grid(True,which='minor')
if h_start > 0:
for item in range(0,len(h_start)):
if dark: ax3.axvspan(h_start[item], h_end[item], facecolor='#78D400', alpha=0.30, linestyle='dashed')
else: ax3.axvspan(h_start[item], h_end[item], facecolor='g', alpha=0.10, linestyle='dashed')
if spine > 0:
ax3.spines['right'].set_visible(False)
ax3.spines['top'].set_visible(False)
ax3.xaxis.set_ticks_position('bottom')
ax3.yaxis.set_ticks_position('left')
rowcounter+=1
if numOfrows <= rowcounter and not randomBins: ax3.set_xlabel('Chromosome %s Mb (resolution: %sKb)' % (schr , resolution/1000))
elif numOfrows <= rowcounter and randomBins: ax3.set_xlabel('Chromosome %s (Genomic Bins)' % (schr))
''' Gene plotting'''
if len(plotGenes) > 0:
ax3 = plt.subplot2grid((numOfrows, 4*len(files)), (rowcounter, exp*4), rowspan=2,colspan=4,sharex=ax1)
if exp==0: ax3.set_ylabel('Genes')
ax3.get_yaxis().set_label_coords(-0.125,0.5)
genes,trackCount,nearest = read_genes(plotGenes[0],resolution,chromosome,start,end)
plength = (end-start)*float(resolution)/1000000
if dark : gcolor='white';icolor='white'
else: gcolor = '#3C3C8C';icolor='#0C0C78'
for item in genes.keys():
if len(genes[item])>2: #plot with introns
gstart = float(item.split('-')[0])/resolution
gend = float(item.split('-')[1])/resolution
gtrack = genes[item][0]
gestart = genes[item][3].split(',')
geend = genes[item][4].split(',')
ax3.plot([gstart,gend],[trackCount-gtrack+0.125,trackCount-gtrack+0.125],color=icolor, linewidth=0.5, zorder = -1)
arrow = 5
if genes[item][2]=='-': arrow=4
if plength <= 30:
for exon in range(0,len(geend)-1):
if len(genes[item])>5:
grect = Rectangle((float(gestart[exon])/resolution,trackCount-gtrack), (float(geend[exon])/resolution-float(gestart[exon])/resolution), 0.25, color=genes[item][5])
else:
grect = Rectangle((float(gestart[exon])/resolution,trackCount-gtrack), (float(geend[exon])/resolution-float(gestart[exon])/resolution), 0.25, color=gcolor)
ax3.add_patch(grect)
if exon < len(geend)-2:
if (float(gestart[exon+1])/resolution-float(geend[exon])/resolution) > 0.5:
if genes[item][2]=='-': ax3.plot(float(gestart[exon])/resolution+(float(gestart[exon+1])/resolution-float(geend[exon])/resolution)/2-0.125,trackCount-gtrack+0.125, marker=arrow, color=icolor, markersize=1.25)
else: ax3.plot(float(gestart[exon])/resolution+(float(gestart[exon+1])/resolution-float(geend[exon])/resolution)/2+0.125,trackCount-gtrack+0.125, marker=arrow, color=icolor, markersize=1.25)
else:
if len(genes[item])>5: rect = Rectangle((gstart,trackCount-gtrack), (gend-gstart), 0.25, color=genes[item][5])
else: rect = Rectangle((gstart,trackCount-gtrack), (gend-gstart), 0.25, color=gcolor)
ax3.add_patch(rect)
else: # simple plotting
gstart = float(item.split('-')[0])/resolution
gend = float(item.split('-')[1])/resolution
gtrack = genes[item][0]
if len(genes[item])>5: rect = Rectangle((gstart,trackCount-gtrack), (gend-gstart), 0.25, color=genes[item][5])
else: rect = Rectangle((gstart,trackCount-gtrack), (gend-gstart), 0.25, color=gcolor)
ax3.add_patch(rect)
if plength <= 30 and geneLabels: # also consider the gene density
#optimize the font size
gindex = nearest[gtrack].index(int(item.split('-')[1]))
upgene = nearest[gtrack][gindex-1]
if gindex < len(nearest[gtrack])-1: downgene = nearest[gtrack][gindex+1]
else: downgene = upgene
if plength <= 2 or plength < 1: plength=1
elif plength <= 4: plength = 2
else : plength/1.5
gdist = min(abs(nearest[gtrack][gindex]-upgene),abs(nearest[gtrack][gindex]-downgene))
if len(genes[item]) > 6: ax3.text(gstart, trackCount-gtrack+0.5, genes[item][1], fontsize=genes[item][6], color=genes[item][5])
elif len(nearest[gtrack])==1: ax3.text(gstart, trackCount-gtrack+0.5, genes[item][1], fontsize=4.5/plength)
elif float(gdist)/resolution >= 2 and len(genes[item][1])<=6: ax3.text(gstart, trackCount-gtrack+0.5, genes[item][1], fontsize=4.5/plength)
elif float(gdist)/resolution >= 2 and len(genes[item][1])>6: ax3.text(gstart, trackCount-gtrack+0.5, genes[item][1], fontsize=3/plength)
elif float(gdist)/resolution < 2 and float(gdist)/resolution > 1 and len(genes[item][1])<=6: ax3.text(gstart, trackCount-gtrack+0.5, genes[item][1], fontsize=2.5/plength)
elif float(gdist)/resolution < 2 and float(gdist)/resolution >1 and len(genes[item][1])>6: ax3.text(gstart, trackCount-gtrack+0.5, genes[item][1], fontsize=2/plength)
elif float(gdist)/resolution <= 1 and float(gdist)/resolution >= 0.25: ax3.text(gstart, trackCount-gtrack+0.5, genes[item][1], fontsize=1.8)
else: ax3.text(gstart, trackCount-gtrack+0.5, genes[item][1], fontsize=1)
#if lblstndrd == 1:
#if len(genes[item][1])>5: ax3.text(gstart, trackCount-gtrack+0.5, genes[item][1], fontsize=0.5)
#else : ax3.text(gstart, trackCount-gtrack+0.5, genes[item][1], fontsize=3)
ax3.set_xlim(int(start or 1) - 0.5,int(start or 1) + length - 0.5)
ax3.set_ylim(0,trackCount+1)
plt.setp(ax3.get_yticklabels(), visible=False)
if plotTadDomains and plotDomainTicks:
ax3.set_xticks(tricks, minor=True)
ax3.xaxis.grid(True,which='minor')
if h_start > 0:
for item in range(0,len(h_start)):
if dark: ax3.axvspan(h_start[item], h_end[item], facecolor='#78D400', alpha=0.30, linestyle='dashed')
else: ax3.axvspan(h_start[item], h_end[item], facecolor='g', alpha=0.10, linestyle='dashed')
ax3.spines['right'].set_visible(False)
ax3.spines['left'].set_visible(False)
ax3.spines['top'].set_visible(False)
ax3.tick_params(left="off")
ax3.tick_params(right="off")
ax3.xaxis.set_ticks_position('bottom')