-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmergeMethods
More file actions
executable file
·114 lines (92 loc) · 2.97 KB
/
Copy pathmergeMethods
File metadata and controls
executable file
·114 lines (92 loc) · 2.97 KB
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
#!/usr/bin/python
'''
Takes a results file (in which all the methods have been merged in)
And outputs a merged file, indicating in column 10 how many methods give support
'''
import sys
if len(sys.argv) < 2:
print 'USAGE: mergeMethods IN_FILE'
sys.exit(1)
infile = sys.argv[1]
# Anatomy of a hit
# KH30Av1_psym570005 SKH30av1 + 2079152 2079171 - -90 -71 GTATTTGCAAAATATTTTCA nhmmer 1.0 100.0 Fur_1
# Anatomy of a result hit
# KH30Av1_psym570005 SKH30av1 + 2079152 2079171 - -90 -71 GTATTTGCAAAATATTTTCA 4 Fur_1
class Hit(object):
def __init__(self, s):
self.gene = s[0]
self.dna = s[1]
self.strand = s[2]
self.start = int(s[3])
self.stop = int(s[4])
self.gstrand = s[5]
self.gstart = s[6]
self.gstop = s[7]
self.seq = s[8]
self.method = s[9]
self.score = s[10]
self.threshold = s[11]
self.reg = s[12]
self._fixRegName()
def __str__(self):
return '\t'.join( [self.dna, self.strand, str(self.start),
str(self.stop), self.gstrand, self.gstart, self.gstop,
self.seq] )
def __len__(self):
return self.stop-self.start
def _fixRegName(self):
'''
Ugly exception handler for SM_b regulators
Removes the regulator tag (i.e. _1)
'''
reg = self.reg.replace('SM_b', 'SM-b')
reg = reg.split('_')[0]
reg = reg.replace('SM-b', 'SM_b')
self.reg = reg
def checkMerge(h, hits):
'''
Check if the hit is included in or includes the hit set
Returns True or False
'''
if len(hits) == 0:
return True
minstart = min([x.start for x in hits])
maxstop = max([x.stop for x in hits])
if h.stop < minstart or h.start > maxstop:
return False
# Included
if h.start >= minstart and h.stop <= maxstop:
return True
# Includes
if h.start <= minstart and h.stop >= maxstop:
return True
return False
# Read the whole file and create a set of hits - divided by dnaid
hits = {}
for l in open(infile):
s = l.rstrip().split('\t')
h = Hit(s)
hits[h.dna] = hits.get(h.dna, set())
hits[h.dna].add(h)
merged = []
for dna in hits:
# Order them by hit start
shits = sorted(hits[dna], key=lambda x: x.start, reverse=True)
# Actual merge
m = set()
while len(shits) != 0:
h = shits.pop()
if checkMerge(h, m):
m.add(h)
else:
if len(m) > 0:
merged.append(m)
else:
m.add(h)
merged.append(m)
m = set()
for h in merged:
for gene in set([x.gene for x in h]):
# Get the best motif (the longest one)
best = sorted(filter(lambda x:x.gene==gene, h), key=lambda x:len(x))[-1]
print gene + '\t' + str(best) + '\t' + str(len(set([x.method for x in h]))) + '\t' + best.reg