forked from mfiers/leapfrog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlf_finddiff.py
executable file
·226 lines (187 loc) · 6.51 KB
/
lf_finddiff.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
#!/usr/bin/env python
import os
import string
import argparse
import collections
class Peekorator(object):
def __init__(self, generator):
self.empty = False
self.peek = None
self.generator = generator
try:
self.peek = self.generator.next()
except StopIteration:
self.empty = True
def __iter__(self):
return self
def next(self):
"""
Return the self.peek element, or raise StopIteration
if empty
"""
if self.empty:
raise StopIteration()
to_return = self.peek
try:
self.peek = self.generator.next()
except StopIteration:
self.peek = None
self.empty = True
return to_return
class GFFRecord:
def __init__(self, line, tag):
ls = line.split()
self.tag = tag
self.seqid = ls[0]
self.source = ls[1]
self.type = ls[2]
self.start = int(ls[3])
self.end = int(ls[4])
self.score = float(ls[5])
self.strand = ls[6]
self.phase = ls[7]
attrs = ls[8].split(';')
for a in attrs:
k, v = map(string.strip, a.split('=', 1))
self.__dict__[k] = v
def __str__(self):
return self.ID
class GFFReader(object):
def __init__(self, filename, only_unique):
self.filename = filename
self.F = open(self.filename)
self.tag = filename.rsplit('/', 1)[-1].replace('.gff', '')
self.only_unique = only_unique
def __iter__(self):
return self
def getline(self):
while True:
line = self.F.readline()
if not line:
break
if line[0] != '#':
break
if not line:
raise StopIteration()
return line
def next(self):
while True:
record = GFFRecord(self.getline(), self.tag)
if self.only_unique and record.type[:11] != 'REFS.UNIQUE':
continue
break
return record
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--base')
parser.add_argument('inputgff', nargs='+')
parser.add_argument('-q', '--only_unique', action='store_true', default=False,
help='only process "REFS.UNQIUE.*" features')
parser.add_argument('-d', '--only_differential', action='store_true', default=False,
help='show only regions that are differential between the samples')
args = parser.parse_args()
base = args.base
inputfiles = args.inputgff
nicenames = [os.path.basename(x).replace('.gff', '') for x in inputfiles]
# outgff = ['%s.%s.gff' % (base, x) for x in nicenames]
parsers = [Peekorator(GFFReader(x, args.only_unique)) for x in inputfiles]
FOUT1 = open(base + '.regions', 'w')
FOUT2 = open(base + '.table', 'w')
COREGFF = open(base + '.gff', 'w')
# GFFOUT = [open(x, 'w') for x in outgff]
# write FOUT2 header
FOUT2.write("\t")
FOUT2.write("chr\tstart\tstop\tfamily\t")
FOUT2.write("\t".join(["b_" + x for x in nicenames]))
FOUT2.write("\t")
FOUT2.write("\t".join(["s_" + x for x in nicenames]))
FOUT2.write("\n")
chromosomes = []
this_chromosome = None
group_count = 0
while True:
# find peak to work on
# check if we're still on the current chromosome
next_chromosomes = [p.peek.seqid for p in parsers if p.peek]
if len(next_chromosomes) == 0:
# end of file:
break
if not this_chromosome in next_chromosomes:
assert(len(set(next_chromosomes)) == 1)
this_chromosome = next_chromosomes[0]
# find the lowest coordinate - and pick that as a reference
reference_peek = None
for i, parser in enumerate(parsers):
bmp = parser.peek
if not bmp:
continue
if bmp.seqid != this_chromosome:
# not on this chromosome - we'll pick this one up later
continue
if not reference_peek:
reference_peek = bmp
elif bmp.start < reference_peek.start:
reference_peek = bmp
# now we have a reference peek - start collecting peeks that
# overlap with the reference peek - and are of the same family
def gff_type_to_fam(s):
return s.replace('REFS.', '')\
.replace('UNIQUE.', '')\
.replace('NOTUNIQ.', '')\
.replace('(', '')\
.replace(')', '')\
.replace('#', '')
# current bump stats
bump_start = reference_peek.start
bump_end = reference_peek.end
bump_seqid = reference_peek.seqid
bump_type = gff_type_to_fam(reference_peek.type)
bump_group = []
while True:
found_overlap = False
for i, parser in enumerate(parsers):
check_bmp = parser.peek
if not check_bmp:
continue
check_type = gff_type_to_fam(check_bmp.type)
if check_bmp.seqid != bump_seqid:
# different chromosome
continue
if check_bmp.start > bump_end:
# past the current bump - ignore
continue
if check_type != bump_type:
# different type (family) - ignore
continue
# found an overlap - same chrom, same type:
new_bump = parser.next()
bump_group.append(new_bump)
bump_end = max(bump_end, new_bump.end)
found_overlap = True
if not found_overlap:
break
if not bump_group:
# assume end of file
break
tags = sorted(list(set([b.tag for b in bump_group])))
scores = collections.defaultdict(int)
for b in bump_group:
scores[b.tag] = max(scores[b.tag], b.score)
# print tags
# print scores
ld = [{True: '1', False: '0'}[nn in tags] for nn in nicenames]
ls = [str(scores[nn]) for nn in nicenames]
# print ld # print ls
if args.only_differential:
if len(set(ld)) < 2:
continue
group_count += 1
FOUT1.write("\t".join(map(str, [bump_seqid, bump_start, bump_end, bump_type, str(len(tags))] + tags)))
FOUT1.write("\n")
FOUT2.write("bg%08d\t%s\t%s\t%s\t%s\t%s\t%s\n" % (group_count, bump_seqid, bump_start, bump_end, bump_type, "\t".join(ld), "\t".join(ls)))
gff_attrs = ['ID=REFS.REGION.%s.%s.%s' % (
bump_type, bump_seqid, bump_start)]
for t in tags:
gff_attrs.append('Present_in=%s' % t)
COREGFF.write("%s\n" % "\t".join(map(str, [
bump_seqid, 'REFS', 'REFS.REGION.%s' % bump_type, bump_start, bump_end,
'.', '.', '.', ";".join(gff_attrs)])))