-
Notifications
You must be signed in to change notification settings - Fork 1
/
BED_MACS.py
609 lines (508 loc) · 19.5 KB
/
BED_MACS.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
# Copyright (c) 2009 NHLBI, NIH
# Authors: Dustin Schones, Chongzhi Zang, Weiqun Peng and Keji Zhao
#
# This software is distributable under the terms of the GNU General
# Public License (GPL) v2, the text of which can be found at
# http://www.gnu.org/copyleft/gpl.html. Installing, importing or
# otherwise using this module constitutes acceptance of the terms of
# this License.
#
# Disclaimer
#
# This software is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# Comments and/or additions are welcome (send e-mail to:
"""
This module contains all the classes to deal with BED files
"""
import re, os, shutil, time, sys
from math import *
from string import *
plus = re.compile('\+');
minus = re.compile('\-');
bedError = "Error in BED class";
"""
BED types:
BED2: start, strand
BED3: chrom, start and end
BED6: BED3 + name + score + strand ('+' or '-')
BED_GRAPH: bed graph format to mimic wiggle format:
chrom, start, end, value
"""
hg_chroms = ['chr1','chr2','chr3','chr4','chr5','chr6','chr7','chr8','chr9',
'chr10','chr11','chr12','chr13','chr14','chr15','chr16','chr17',
'chr18','chr19','chr20','chr21','chr22','chrX','chrY', 'chrM'];
dm_chroms = ['chr2h', 'chr2L', 'chr2R', 'chr3h', 'chr3L', 'chr3R', 'chr4',
'chr4h', 'chrM', 'chrU', 'chrX', 'chrXh', 'chrYh'];
dm3_chroms = ['chr2L', 'chr2LHet', 'chr2R', 'chr2RHet', 'chr3L', 'chr3LHet',
'chr3R', 'chr3RHet', 'chr4', 'chrX', 'chrXHet', 'chrYHet', 'chrU',
'chrUextra', 'chrM'];
sacCer_chroms = ['chr1','chr2','chr3','chr4','chr5','chr6','chr7','chr8','chr9',
'chr10','chr11','chr12','chr13','chr14','chr15','chr16','chrM']
mm_chroms = ['chr1','chr2','chr3','chr4','chr5','chr6','chr7','chr8','chr9',
'chr10','chr11','chr12','chr13','chr14','chr15','chr16','chr17',
'chr18','chr19','chrX','chrY', 'chrM']
chroms = {"hg18":hg_chroms, "dm2":dm_chroms, "dm3":dm3_chroms,
"sacCer1":sacCer_chroms, "mm8":mm_chroms, "mm9":mm_chroms}
#----------------------------
#----------------------------
class BED2:
"""
Class for bed lines with 2 values: start and strand, this will be
useful for things like TSS information or tags where the only
important information is the start and strand.
"""
def __init__(self, start, strand):
self.start = start;
self.strand = strand;
def __set__(self, start, strand):
self.start = start;
self.strand = strand;
def getCoord(self):
outstring = str(self.start) + " " + self.strand;
try:
return outstring;
except:
sys.stderr.write("No coord information for %s\n" % self)
return ''
#----------------------------
#----------------------------
class BED3:
"""
Class for bed lines with 3 values: chrom, start and end
"""
def __init__(self, chrom, start, end):
self.chrom = chrom;
self.start = start;
self.end = end;
def __set__(self, chrom, start, end):
self.chrom = chrom;
self.start = start;
self.end = end;
def getCoord(self):
outstring = self.chrom + " " + str(self.start) + " " + str(self.end);
try:
return outstring;
except:
sys.stderr.write("No coord information for %s\n" % self)
return ''
#----------------------------
#----------------------------
class BED6:
"""
Class for bed lines with 6 values: chrom, start, end, name, score, strand
"""
def __init__(self, chrom, start, end, name, score, strand):
self.chrom = chrom;
self.start = start;
self.end = end;
self.name = name;
self.score = score;
self.strand = strand;
def __set__(self, chrom, start, end, name, score, strand):
self.chrom = chrom;
self.start = start;
self.end = end;
self.name = name;
self.score = score;
self.strand = strand;
def getCoord(self):
outstring = self.chrom + "\t" + str(self.start) + "\t" + \
str(self.end) + "\t" + self.name + "\t" + \
str(self.score) + "\t" + self.strand;
try:
return outstring;
except:
sys.stderr.write("No coord information for %s\n" % self)
return ''
#----------------------------
#----------------------------
class BED_MACS:
"""
Class for bed lines with 6 values: chrom, start, end, length, summit, score
"""
def __init__(self, chrom, start, end, length, summit, score):
self.chrom = chrom;
self.start = start;
self.end = end;
self.length = length;
self.summit = summit;
self.score = score;
def __set__(self, chrom, start, end, length, summit, score):
self.chrom = chrom;
self.start = start;
self.end = end;
self.length = length;
self.summit = summit;
self.score = score;
def getCoord(self):
outstring = self.chrom + "\t" + str(self.start) + "\t" + \
str(self.end) + "\t" + self.length + "\t" + \
str(self.summit) + "\t" + self.score;
try:
return outstring;
except:
sys.stderr.write("No coord information for %s\n" % self)
return ''
#----------------------------
#----------------------------
class BED_GRAPH:
"""
Class to deal with bed graph lines: chrom, start, end, value
This emulates the wiggle format
"""
def __init__(self, chrom, start, end, value=0):
self.chrom = chrom;
self.start = start;
self.end = end;
self.value = value;
def __set__(self, chrom, start, end, value):
self.chrom = chrom;
self.start = start;
self.end = end;
self.value = value;
def getCoord(self):
outstring = self.chrom + " " + str(self.start) + " " + str(self.end);
try:
return outstring;
except:
sys.stderr.write("No BED coord information for %s\n" % self)
return ''
def getAll(self):
outstring = self.chrom + " " + str(self.start) + " " + str(self.end) + " " + str(self.value);
try:
return outstring;
except:
sys.stderr.write("No BED all information for %s\n" % self)
return ''
#----------------------------
#----------------------------
class Starts:
"""
Extra class to just read in starts by chromosome
Keeping separate from BED class for now because of memory issues,
would like to combine this with the BED class at some point
because otherwise a lot of the functions will be duplicated, need
to think more about how to do this best
"""
def __init__(self, species="hg18", file=None):
"""
Reads in a bed file and builds a dictionary
with chromosomes as keys and lists of tag starts as values
"""
self.bed_vals = {}
for c in chroms[species]:
self.bed_vals[c] = [];
infile = open(file);
for line in infile:
""" check to make sure not a header line """
if not re.match("track", line):
line = line.strip();
sline = line.split();
if plus.match(sline[5]):
self.bed_vals[sline[0]].append(atoi(sline[1]));
elif minus.match(sline[5]):
self.bed_vals[sline[0]].append(atoi(sline[2]));
def keys(self):
"""
Return a list of the keys - duplicating the function of a dictionary
"""
return self.bed_vals.keys()
def getChroms(self):
"""
Return a list of all the chromosomes in order (ordered keys)
-- this will be useful when I think of a way to tell the class
which species I'm working in - I can have all the chrom lists
for each species here and just return the right one
"""
try:
return chroms[species];
except:
sys.stderr.write("Can't return chromosome list\n" % self)
return ''
def getNumVals(self):
"""
Return the number of bed vals in BED instance
"""
num = 0;
for c in self.bed_vals.keys():
num += len(self.bed_vals[c]);
return num;
def __setitem__(self, name, bedlist):
"""
Sets a new bed value
"""
self.bed_vals[name] = bedlist
def __getitem__(self, name):
"""
Returns a bed_val indexed by its name or None if no such bed_val exists
"""
if self.bed_vals.has_key(name):
return self.bed_vals[name]
else: raise bedError
def __del__(self):
"""
Delete, delete;
"""
self.bed_vals.clear()
def __contains__(self, item):
"""
Returns mapping iterator
"""
return self.bed_vals.has_key(item)
def __iter__(self):
"""
Returns mapping iterator
"""
return self.bed_vals.iterkeys()
def __len__(self):
"""
Returns number of bed_vals
"""
return len(self.bed_vals)
def __delitem__(self, name):
"""
removes a chrom if its name exists in the dictionary
-- I guess this could possibly be useful at some point
"""
if self.bed_vals.has_key(name):
del self.bed_vals[name]
else: raise bedError
class BED:
"""
Class to deal with bed files and do common operations
"""
def __init__(self, species="hg18", file=None, bed_type="BED3", val_threshold=0):
""" Overload __init__ so that if a threshold is given, only
grab bed vals that are above threshold -- This won't do
anything different for bed3 entries because there is no value
information in these.
Reads in a bed file and builds a dictionary with chromosomes
as keys and lists of bed elements as values
"""
self.bed_vals = {}
"""
initialize a dictionary with chromosomes
"""
for c in chroms[species]:
self.bed_vals[c] = [];
## IS THERE A BETTER WAY TO DO THIS MATCHING?!?
if(file):
if re.match(bed_type, "BED3"):
infile = open(file);
for line in infile:
""" check to make sure not a header line """
if not re.match("track", line):
line = line.strip();
sline = line.split();
if len(sline) == 3:
bed = BED3(sline[0], atoi(sline[1]), atoi(sline[2]));
self.bed_vals[sline[0]].append(bed);
if len(sline) == 4:
if atof(sline[3]) >= val_threshold:
bed = BED3(sline[0], atoi(sline[1]),
atoi(sline[2]));
self.bed_vals[sline[0]].append(bed);
if len(sline) == 6:
if atof(sline[4]) >= val_threshold:
bed = BED3(sline[0], atoi(sline[1]),
atoi(sline[2]));
self.bed_vals[sline[0]].append(bed);
if len(sline) > 6:
bed = BED3(sline[0], atoi(sline[1]), atoi(sline[2]));
self.bed_vals[sline[0]].append(bed);
elif re.match(bed_type, "BED_GRAPH"):
""" If want BED_GRAPH type """
infile = open(file);
for line in infile:
""" check to make sure not a header line """
if not re.match("track", line):
line = line.strip();
sline = line.split();
if len(sline) == 3:
sys.stderr.write("Can't make bed_graph with only \
3 elements")
raise bedError
if len(sline) == 4:
if atof(sline[3]) >= val_threshold:
bed = BED_GRAPH(sline[0], atoi(sline[1]), atoi(sline[2]),
atof(sline[3]));
self.bed_vals[sline[0]].append(bed);
if len(sline) == 6:
if atof(sline[4]) >= val_threshold:
bed = BED_GRAPH(sline[0], atoi(sline[1]), atoi(sline[2]),
atof(sline[4]));
self.bed_vals[sline[0]].append(bed);
elif re.match(bed_type, "BED2"):
""" If want BED2 type """
infile = open(file);
for line in infile:
""" check to make sure not a header line """
if not re.match("track", line):
line = line.strip();
sline = line.split();
if len(sline) == 3:
sys.stderr.write("Need BED6 to make BE2")
raise bedError
if len(sline) == 4:
sys.stderr.write("Need BED6 to make BE2")
raise bedError
if len(sline) == 6:
if atof(sline[4]) >= val_threshold:
if plus.match(sline[5]):
bed = BED2(atoi(sline[1]), sline[5]);
elif minus.match(sline[5]):
bed = BED2(atoi(sline[2]), sline[5]);
self.bed_vals[sline[0]].append(bed);
elif re.match(bed_type, "BED6"):
""" If want BED6 element """
infile = open(file);
for line in infile:
""" check to make sure not a header line """
if not re.match("track", line):
line = line.strip();
sline = line.split();
if len(sline) == 3:
sys.stderr.write("Need BED6 to make BE6")
raise bedError
if len(sline) == 4:
sys.stderr.write("Need BED6 to make BE6")
raise bedError
if len(sline) == 6:
if atof(sline[4]) >= val_threshold:
bed = BED6(sline[0], atoi(sline[1]), atoi(sline[2]),
sline[3], atof(sline[4]), sline[5]);
self.bed_vals[sline[0]].append(bed);
elif re.match(bed_type, "BED_MACS"):
""" If want BED6 element """
infile = open(file);
for line in infile:
""" check to make sure not a header line """
if not re.match("track", line):
line = line.strip();
sline = line.split();
if len(sline) == 3:
sys.stderr.write("Need BED_MACS to make BED_MACS")
raise bedError
if len(sline) == 4:
sys.stderr.write("Need BED_MACS to make BED_MACS")
raise bedError
if len(sline) == 6:
bed = BED_MACS(sline[0], atoi(sline[1]), atoi(sline[2]), atoi(sline[3]), atoi(sline[4]), atoi(sline[5]));
self.bed_vals[sline[0]].append(bed);
def keys(self):
"""
Return a list of the keys - duplicating the function of a dictionary
"""
return self.bed_vals.keys()
"""
-- BED2, BED3, BED_GRAPH all ok -- BED3 and BED_GRAPH DO NOT HAVE
STRAND INFO SO DON'T NEEED TO WORRY ABOUT THEM HERE
-- BED2 STRANDS WERE TAKE INTO CONSIDERATION WHEN READ IN
-- BED6 - NEED TO USE getStarts_consider_strands
"""
def getStarts_consider_strands(self, chrom):
"""
Return a list of starts on a given chromosome
"""
starts = [];
for t in self.bed_vals[chrom]:
if plus.match(t.strand):
starts.append(t.start);
elif minus.match(t.strand):
starts.append(t.end);
try:
return starts;
except:
sys.stderr.write("Having trouble returning starts %s\n" % self)
return ''
def getStarts(self, chrom):
"""
Return a list of starts on a given chromosome
"""
starts = [];
for t in self.bed_vals[chrom]:
starts.append(t.start);
try:
return starts;
except:
sys.stderr.write("Having trouble returning starts %s\n" % self)
return ''
def getEnds(self, chrom):
"""
Return a list of starts on a given chromosome
"""
ends = [];
for t in self.bed_vals[chrom]:
ends.append(t.end);
try:
return ends;
except:
sys.stderr.write("Having trouble returning ends %s\n" % self)
return ''
def getChroms(self):
"""
Return a list of all the chromosomes in order (ordered keys)
"""
try:
return chroms[species];
except:
sys.stderr.write("Can't return chromosome list\n" % self)
return ''
def getNumVals(self):
"""
Return the number of bed vals in BED instance
"""
num = 0;
for c in self.bed_vals.keys():
num += len(self.bed_vals[c]);
return num;
def addChrom(self, chrom, bed_list):
if self.bed_vals.has_key(chrom) and len(self.bed_vals[chrom]) > 0:
sys.stderr.write("chromsome %s already populated\n" % chrom)
raise bedError
else: self.bed_vals[chrom] = bed_list;
def __del__(self):
"""
Delete, delete;
"""
self.bed_vals.clear()
def __contains__(self, item):
"""
Returns mapping iterator
"""
return self.bed_vals.has_key(item)
def __iter__(self):
"""
Returns mapping iterator
"""
return self.bed_vals.iterkeys()
def __len__(self):
"""
Returns number of bed_vals
"""
return len(self.bed_vals)
def __delitem__(self, name):
"""
removes a chrom if its name exists in the dictionary
-- I guess this could possible be useful at some point
"""
if self.bed_vals.has_key(name):
del self.bed_vals[name]
else: raise bedError
def __setitem__(self, name, bedlist):
"""
Sets a new bed value
"""
self.bed_vals[name] = bedlist
def __getitem__(self, name):
"""
Returns a bed_val indexed by its name or None if no such bed_val exists
"""
if self.bed_vals.has_key(name):
return self.bed_vals[name]
else: raise bedError