-
Notifications
You must be signed in to change notification settings - Fork 37
/
coverage.py
executable file
·532 lines (416 loc) · 14.3 KB
/
coverage.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
from copy import copy
from coolbox.plots.coverage import *
from coolbox.fetchdata import *
from coolbox.utilities import op_err_msg, get_coverage_stack, get_feature_stack
from coolbox.core.track import BigWig
from coolbox.core.track import Arcs
__all__ = [
"CoverageStack",
"Vlines", "VlinesFromFile",
"HighLights", "HighLightsFromFile",
"HiCPeaks", "TADCoverage",
"BigWigCoverage", "ArcsCoverage"
]
class Coverage(object):
"""
Coverage base class.
`Coverage` is the plots at the upper layer of Track.
>>> from coolbox.core.track import Track
>>> c1 = Coverage({})
>>> c1.properties
{'name': 'Coverage.1'}
>>> c2 = Coverage({})
>>> t1 = Track({})
>>> t2 = c1 + c2 + t1
>>> len(t2.coverages)
2
>>> assert type(c1 + c2) is CoverageStack
>>> t3 = Track({})
>>> frame = t2 + t3
>>> frame = frame + Coverage({})
>>> len(list(frame.tracks.values())[-1].coverages)
1
"""
def __new__(cls, *args, **kwargs):
if hasattr(cls, "_counts"):
cls._counts += 1
else:
cls._counts = 1
return super().__new__(cls)
def __init__(self, properties_dict):
self.properties = properties_dict
self.__bool2str()
name = self.properties.get("name")
if name is not None:
assert isinstance(name, str), "Coverage name must be a `str`."
else:
name = self.__class__.__name__ + ".{}".format(self.__class__._counts)
self.properties['name'] = name
super().__init__()
stack = get_feature_stack()
for feature in stack:
self.properties[feature.key] = feature.value
def __bool2str(self):
"""
Conver bool value to 'yes' or 'no', for compatible with pyGenomeTracks
"""
for key, value in self.properties.items():
if isinstance(value, bool):
if value:
self.properties[key] = 'yes'
else:
self.properties[key] = 'no'
@property
def name(self):
return self.properties['name']
@name.setter
def name(self, value):
self.properties['name'] = value
def __add__(self, other):
from .track import Track
from .frame import Frame
from .feature import Feature
if isinstance(other, Track):
result = copy(other)
result.append_coverage(self)
return result
elif isinstance(other, Frame):
result = copy(other)
if len(result.tracks) > 1:
first = list(result.tracks.values())[0]
first.append_coverage(self, pos='bottom')
return result
elif isinstance(other, Feature):
result = copy(self)
result.properties[other.key] = other.value
return result
elif isinstance(other, Coverage):
stack = CoverageStack([self, other])
return stack
elif isinstance(other, CoverageStack):
result = copy(other)
result.to_bottom(self)
return result
else:
raise TypeError(op_err_msg(self, other))
def __mul__(self, other):
from .frame import Frame
if isinstance(other, Frame):
result = copy(other)
result.add_cov_to_tracks(self)
return result
else:
raise TypeError(op_err_msg(self, other, op='*'))
def __enter__(self):
stack = get_coverage_stack()
stack.append(self)
return self
def __exit__(self, type, value, traceback):
stack = get_coverage_stack()
stack.pop()
def check_track_type(self, allow):
valid = any([isinstance(self.track, type_) for type_ in allow])
if not valid:
msg = "{} coverage's track must be a instance of {}".format(self.track.__class__.__name__,
[type_.__name__ for type_ in allow])
raise ValueError(msg)
class CoverageStack(object):
"""
Denote a stack of Coverage.
ps: this "Stack" is actually a "Deque",
name it "Stack" is just for imaging it vertically.
Parameters
----------
coverages : list of coolbox.core.coverage.Coverage
coverages list.
"""
def __init__(self, coverages):
self.coverages = coverages
def to_top(self, cov):
self.coverages.append(cov)
def to_bottom(self, cov):
self.coverages.insert(0, cov)
def __add__(self, other):
from .track import Track
from .frame import Frame
from .feature import Feature
if isinstance(other, Coverage):
result = copy(self)
result.to_top(other)
return result
elif isinstance(other, Track):
result = copy(other)
result.pile_coverages(self.coverages, pos='bottom')
return result
elif isinstance(other, Frame):
result = copy(other)
if len(result.tracks) != 0:
first = list(result.tracks.values())[0]
first.pile_coverages(self.coverages, pos='bottom')
return result
elif isinstance(other, Feature):
result = copy(self)
if len(result.coverages) != 0:
last = result.coverages[-1]
last.properties[other.key] = other.value
return result
else:
raise TypeError(op_err_msg(self, other))
def track_to_coverage(track_class):
def init(self, *args, **kwargs):
if track_class is BigWig:
kwargs.update({
"show_data_range": False,
})
self.track_instance = track_class(*args, **kwargs)
self.properties = self.track_instance.properties
def plot(self, ax, chrom_region, start_region, end_region):
if hasattr(self, 'track'):
if (track_class is Arcs) or (track_class is BigWig):
# update height when plot
self.track_instance.properties['height'] = self.track.properties['height']
self.track_instance.plot(ax, chrom_region, start_region, end_region)
cov_class = type(
track_class.__name__+"Coverage",
(Coverage,),
{
"__init__": init,
"__doc__": track_class.__doc__,
"plot": plot
}
)
return cov_class
BigWigCoverage = track_to_coverage(BigWig)
ArcsCoverage = track_to_coverage(Arcs)
class VlinesFromFile(Coverage, PlotVlines):
"""
Vertical lines from the file.
Parameters
----------
file_ : str
Path to file.
color : str, optional
Line color, default '#1e1e1e'.
alpha : float, optional
Line alpha value, default 0.8.
line_style : str, optional
Line style, default 'dashed'.
line_width : float, optional
Line width, default 0.5.
name : str, optional
The name of thr Coverage.
"""
def __init__(self, file_, **kwargs):
properties_dict = {
"file": file_,
"color": "#1e1e1e",
"alpha": 0.8,
"line_style": "dashed",
"line_width": 1,
}
properties_dict.update(kwargs)
super().__init__(properties_dict)
class Vlines(Coverage, PlotVlines):
"""
Vertical lines.
Parameters
----------
vlines : list of {int, str}
A list of vline positions. position can be expressed as a tuple like:
[('chr1', 10000), ('chr2', 50000)]
or a genome range string like:
['chr1:10000-10000', 'chr2:50000-50000']
color : str, optional
Line color, default '#1e1e1e'.
alpha : float, optional
Line alpha value, default 0.8.
line_style : str, optional
Line style, default 'dashed'.
line_width : float, optional
Line width, default 0.5.
name : str, optional
The name of thr Coverage.
"""
def __init__(self, vlines, **kwargs):
properties_dict = {
"vlines_list": vlines,
"color": "#1e1e1e",
"alpha": 0.8,
"line_style": "dashed",
"line_width": 1,
}
properties_dict.update(kwargs)
super().__init__(properties_dict)
class HighLightsFromFile(Coverage, PlotHighLightRegions):
"""
High light regions coverage, read the regions from the file.
Parameters
----------
file_ : str
Path to the file.
color : str, optional
High light region color,
use 'bed_rgb' for specify color from the file, default 'bed_rgb'.
alpha : float, optional
High light region alpha value, default 0.5.
border_line : bool, optional
Plot border line or not, default True.
border_line_style : str, optional
Border line style, default 'dashed'.
border_line_width : float, optional
Border line width, default 1.0.
border_line_color : str, optional
Border line color, default '#000000'
border_line_alpha : float, optional
Border line alpha value, default 0.8
name : str, optional
The name of thr Coverage.
"""
def __init__(self, file_, **kwargs):
properties_dict = {
"file": file_,
"color": "bed_rgb",
"alpha": 0.5,
"border_line": True,
"border_line_style": "dashed",
"border_line_width": 1.0,
"border_line_color": "#000000",
"border_line_alpha": 0.8,
}
properties_dict.update(kwargs)
super().__init__(properties_dict)
class HighLights(Coverage, PlotHighLightRegions):
"""
High light region.
Parameters
----------
highlight_regions : list of {str, tuple}
A list of regions for highlights, region can be expressed as a tuple or string.
region tuple like:
[('chr1', 100000, 120000), ('chr2', 130000, 150000)]
region string format: `chr:start-end` like:
['chr1:100000-120000', 'chr2:130000-150000'].
color : str, optional
High light region color, default HighLights.DEFAULT_COLOR.
alpha : float, optional
High light region alpha value, default 0.5
border_line : bool, optional
Plot border line or not, default True.
border_line_style : str, optional
Border line style, default 'dashed'
border_line_width : float, optional
Border line width, default 1.0
border_line_color : str, optional
Border line color, default '#000000'
border_line_alpha : float, optional
Border line alpha value, default 0.8
name : str, optional
The name of thr Coverage.
"""
DEFAULT_COLOR = "#ff9c9c"
def __init__(self, highlight_regions, **kwargs):
properties_dict = {
"highlight_regions": highlight_regions,
"color": HighLights.DEFAULT_COLOR,
"alpha": 0.5,
"border_line": True,
"border_line_style": "dashed",
"border_line_width": 1.0,
"border_line_color": "#000000",
"border_line_alpha": 0.8,
}
properties_dict.update(kwargs)
super().__init__(properties_dict)
class HiCPeaks(Coverage, PlotHiCPeaks):
"""
Hi-C Peaks(Loops) Coverge is a special kind of Coverage.
Used to show the peaks on the Hi-C interaction map.
Parameters
----------
file_ : str
Path to the loop file, loop file is a tab splited text file, fields:
chr1, x1, x2, chr2, y1, y2, [color], ... (other optional fields)
color : str, optional
Peak color, use 'bed_rgb' for specify color in file,
default 'bed_rgb'.
alpha : float, optional
Peak alpha value, default 0.6.
line_width : float, optional
Peak border line width, default 1.0
line_style : str, optional
Border line style, default 'solid'
fill : bool, optional
Fill center or not, default False.
fill_color : str, optional
Fill color, use 'bed_rgb' for specify color in file,
default 'bed_rgb'.
side : {'upper', 'lower', 'both'}
Plot peak in which side of the matrix.
NOTE: This parameters is useful only if the Cool track in matrix format.
"""
def __init__(self, file_, **kwargs):
properties_dict = {
"file": file_,
"color": "bed_rgb",
"alpha": 0.6,
"line_width": 1.5,
"line_style": "solid",
"fill": False,
"fill_color": "bed_rgb",
"side": "both",
}
properties_dict.update(kwargs)
super().__init__(properties_dict)
class TADCoverage(Coverage, PlotTADCoverage, FetchBed):
"""
TAD Coverage is used for plot TAD on upper layer of a track.
Parameters
----------
file_ : str
Path to the loop file, loop file is a tab splited text file, fields:
chr1, x1, x2, chr2, y1, y2, [color], ... (other optional fields)
show_score : bool
Show bed score or not.
default False.
score_font_size : {'auto', int}
Score text font size.
default 'auto'
score_font_color : str
Score text color.
default '#000000'
score_height_ratio : float
(text tag height) / (TAD height). used for adjust the position of Score text.
default 0.5
color : str, optional
Peak color, use 'bed_rgb' for specify color in file,
default 'bed_rgb'.
alpha : float, optional
Peak alpha value, default 0.3.
line_color : str, optional
Border line color, default '#000000'.
line_width : float, optional
Border line width, default 1.0.
line_style : str, optional
Border line style, default 'solid'.
fill : bool, optional
Fill center or not, default True.
"""
def __init__(self, file_, **kwargs):
properties_dict = {
"file": file_,
"show_score": False,
"score_font_size": 'auto',
"score_font_color": "#000000",
"score_height_ratio": 0.4,
"color": "bed_rgb",
"alpha": 0.3,
"border_color": "#000000",
"border_width": 1.0,
"border_style": "solid",
"fill": True,
}
properties_dict.update(kwargs)
super().__init__(properties_dict)
if __name__ == "__main__":
import doctest
doctest.testmod()