-
Notifications
You must be signed in to change notification settings - Fork 0
/
mask_bed.py
131 lines (117 loc) · 5.11 KB
/
mask_bed.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
__author__ = 'armartin'
from optparse import OptionParser
from collections import defaultdict, OrderedDict, Callable
USAGE = """
mask_bed.py --bed
--mask
--out
"""
parser = OptionParser(USAGE)
parser.add_option('--bed')
parser.add_option('--mask')
parser.add_option('--out')
(options, args) = parser.parse_args()
mask = open(options.mask)
bed = open(options.bed)
out = open(options.out, 'w')
chrs = map(str, range(1,23))
chrs.append('X')
class DefaultOrderedDict(OrderedDict):
def __init__(self, default_factory=None, *a, **kw):
if (default_factory is not None and
not isinstance(default_factory, Callable)):
raise TypeError('first argument must be callable')
OrderedDict.__init__(self, *a, **kw)
self.default_factory = default_factory
def __getitem__(self, key):
try:
return OrderedDict.__getitem__(self, key)
except KeyError:
return self.__missing__(key)
def __missing__(self, key):
if self.default_factory is None:
raise KeyError(key)
self[key] = value = self.default_factory()
return value
def __reduce__(self):
if self.default_factory is None:
args = tuple()
else:
args = self.default_factory,
return type(self), args, None, None, self.items()
def copy(self):
return self.__copy__()
def __copy__(self):
return type(self)(self.default_factory, self)
def __deepcopy__(self, memo):
import copy
return type(self)(self.default_factory,
copy.deepcopy(self.items()))
def __repr__(self):
return 'OrderedDefaultDict(%s, %s)' % (self.default_factory,
OrderedDict.__repr__(self))
#masked files need to be ordered so bed can interated through properly
mask_dict = DefaultOrderedDict(OrderedDict)
for line in mask:
line = line.strip().split()
mask_dict[line[0]][int(line[1])] = line
last_chr = 0
out = open(options.out, 'w')
while True:
line = bed.readline().strip().split()
if not line: break
try:
current_chr = int(line[0])
except ValueError:
current_chr = 23
if line[0] in mask_dict:
for mask_start in mask_dict[line[0]]:
#mask is the same tract
if int(line[1]) == mask_start and int(line[2]) == int(mask_dict[line[0]][mask_start][2]):
out.write('\t'.join(mask_dict[line[0]][mask_start]) + '\n')
#mask is within a single tract
elif int(line[1]) <= mask_start and int(line[2]) >= int(mask_dict[line[0]][mask_start][2]):
if int(line[1]) != mask_start:
tract1 = list(line)
tract1[2] = str(mask_start-1)
tract1[5] = mask_dict[line[0]][mask_start][4]
out.write('\t'.join(tract1) + '\n')
out.write('\t'.join(mask_dict[line[0]][mask_start]) + '\n')
if int(line[2]) != mask_dict[line[0]][mask_start][5]:
tract2 = list(line)
tract2[1] = str(int(mask_dict[line[0]][mask_start][2])+1)
tract2[4] = mask_dict[line[0]][mask_start][5]
tract2[5] = line[5]
out.write('\t'.join(tract2) + '\n')
#mask is within two tracts
elif int(line[1]) <= mask_start and int(line[2]) <= int(mask_dict[line[0]][mask_start][2]) and int(line[2]) >= mask_start:
if int(line[1]) != mask_start:
tract1 = list(line)
tract1[2] = str(mask_start-1)
tract1[5] = mask_dict[line[0]][mask_start][4]
out.write('\t'.join(tract1) + '\n')
#otherwise no tract 1 because mask subsumes it
out.write('\t'.join(mask_dict[line[0]][mask_start]) + '\n')
line = bed.readline().strip().split() #get the next tract
if int(line[2]) != mask_dict[line[0]][mask_start][5]:
tract2 = list(line)
tract2[1] = str(int(mask_dict[line[0]][mask_start][2])+1)
tract2[4] = mask_dict[line[0]][mask_start][5]
tract2[5] = line[5]
out.write('\t'.join(tract2) + '\n')
#otherwise no tract 2 because mask subsumes it
elif last_chr != current_chr and int(line[1]) > mask_start and int(line[1]) < mask_dict[line[0]][mask_start][2]:
out.write('\t'.join(mask_dict[line[0]][mask_start]) + '\n')
if len(mask_dict[line[0]]) > 1:
continue
else:
out.write('\t'.join(line) + '\n')
#elif last_chr != current_chr and int(line[1]) > mask_start and int(line[1]) > mask_dict[line[0]][mask_start][2]:
# print 'beginning2 ' + str(mask_start) + ' ' + mask_dict[line[0]][mask_start][2]
else: #not within mask
out.write('\t'.join(line) + '\n')
#tract ends in middle of mask
last_chr = current_chr
else:
out.write('\t'.join(line) + '\n')
out.close()