-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmca2bracket.py
executable file
·268 lines (205 loc) · 6.78 KB
/
mca2bracket.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
#!/usr/bin/python
import os
import re
import sys
import glob
import minilib as biolib
import mdg_dt as MDGDT
try:
sys.stderr = open('/tmp/sstr_error.log', 'w')
except:
pass
ix_pattern = re.compile('([A-Za-z]|\'[0-9]\')-?[0-9]+(\.[A-Z])?')
###
def read_data(fn):
sequence = []
structure = []
unidentified = set([])
edge_re = re.compile('W[a-z]/W[a-z]')
pair_re = re.compile('([AG]-U)|(C-G)|(G-[CU])|(U-[AG])')
fi = open(fn)
mode = 'ignore'
while True:
line = fi.readline()
if not line: break
line = line.strip()
if line.endswith('----'):
if line.startswith('Residue conformations'):
mode = 'sequence'
if len(sequence) > 0:
""" ignore multimodels """
break
elif line.startswith('Base-pairs'):
mode = 'structure'
else:
mode = 'ignore'
pass
continue
pass
if mode == 'ignore': continue
elif mode == 'sequence':
line = line.split()
base = line[2].strip()
if base in biolib.BASEDICT:
base = biolib.BASEDICT[base]
sequence.append((line[0], base))
else:
unidentified.add(base)
elif mode == 'structure':
is_ss_pair = 'antiparallel' in line and \
'cis' in line and \
edge_re.search(line) and \
pair_re.search(line)
if is_ss_pair:
line = line.split()
line = line[0]
try:
base1 = ix_pattern.search(line)
line = line[base1.end():]
base2 = ix_pattern.search(line)
structure.append([base1.group(), base2.group()])
except:
print line, 'fail'
pass
"""
if len(tmp) == len(line) - 1:
structure.append(line[0].split('-'))
elif len(tmp) == len(line) - 2:
pass
else:
p = line.find('-')
"""
else:
continue
pass
if len(unidentified) > 0:
sys.stderr.write('UNIDENTIFIED RESIDUES: %s\n' % \
str(list(unidentified)))
pass
return sequence, structure
###
def do_something():
return 0
###
def remove_pknots(structure):
knotless = []
queue = [x for x in sorted(structure)]
i = 0
while True:
if len(queue) == 0: break
u, queue2 = queue[0], queue[1:]
queue = []
for j, v in enumerate(queue2):
if u[0] < v[1] and v[0] < u[1] and v[1] > u[1]:
#sys.stderr.write('Pair %s is crossing pair %s\n' % \
# (str(v), str(u)))
pass
elif u[0] == v[0] or u[1] == v[0] or \
u[0] == v[1] or u[1] == v[1]:
pass
else:
queue.append(v)
pass
pass
knotless.append(u)
pass
return knotless
###
def check_multipairs(structure):
contacts = {}
for p in structure:
if p[0] in contacts:
contacts[p[0]].append(p[1])
else:
contacts[p[0]] = [p[1]]
pass
if p[1] in contacts:
contacts[p[1]].append(p[0])
else:
contacts[p[1]] = [p[0]]
pass
mpairs = [(x, [str(y) for y in contacts[x]])
for x in contacts if len(contacts[x])>1]
for mp in mpairs:
sys.stderr.write('%s %s\n' % (str(mp[0]), ', '.join(mp[1])))
pass
return None
###
def create_brackets(structure, sequence):
brackets = ['.' for i in xrange(len(sequence))]
index = [x[0] for x in sequence]
structure = [(index.index(b1), index.index(b2))
for b1, b2 in structure]
ss_tree = MDGDT.MDG_Stem()
ss_tree.assemble(sorted(structure), outp=open('/dev/null', 'w'))
knotless = sorted(ss_tree.preorder())
if ss_tree.has_tertiary_pairs() and ss_tree.has_tertiary_stem():
print ss_tree.te_contacts
return None
# print 'KLESS', knotless
def write_structure(structure, n):
brackets = ['.' for i in xrange(n)]
for i, j in structure:
if brackets[i] != '.' or brackets[j] != '.':
sys.stderr.write('%s %s %i %i already occupied\n' % \
(b1, b2, i, j))
if brackets[i] == '(' or brackets[i] == '<':
brackets[i] = '<'
elif brackets[i] == ')' or brackets[i] == '>':
brackets[i] ='#'
if brackets[j] == '(' or brackets[j] == '<':
brackets[j] = '@'
elif brackets[j] == ')' or brackets[j] == '>':
brackets[j] ='>'
else:
brackets[i] = '('
brackets[j] = ')'
pass
sys.stderr.write('***\n')
return ''.join(brackets)
return write_structure(structure, len(sequence)) + '\n' + \
write_structure(knotless, len(sequence))
###
def main(argv):
fo = open('darts_set_secondary_structures.fas', 'w')
valid_ids = []
dataset = open('../dataset')
while True:
line = dataset.readline()
#print line
if not line: break
line = line.strip()
if re.match('[0-9][0-9a-z]{3}(:[A-Za-z0-9]+)?', line):
chains = ['pdb' + line[:4] + '1' + x
for x in line[5:]]
if len(chains) == 1 and len(chains[0]) == 8:
chains[0] += '?'
pass
# print chains
for chain in chains:
#print chain
valid_ids.extend(glob.glob(chain + '.mca'))
pass
else:
#print 'No match'
pass
pass
# print valid_ids
for pid in valid_ids:
if not os.path.exists(pid):
sys.stderr.write(pid + ' does not exist\n')
continue
sys.stderr.write(pid + '\n')
seq, struct = read_data(pid)
brackets = create_brackets(struct, seq)
fo.write('>' + pid[:-4] + '\n')
fo.write(''.join([x[1] for x in seq]) + '\n')
fo.write(brackets + '\n')
pass
fo.close()
return 0
if __name__ == '__main__': main(sys.argv[1:])
"""
1111.222....222..344455555...666....666..55555.444..31111
((((.(((....)))..(((((((((...(((....)))..))))).)))..)))))
"""