-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseqlim.py
316 lines (276 loc) · 9.13 KB
/
seqlim.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
import sys
from collections import defaultdict, OrderedDict, Counter
__version__ = "0.2.5"
class Seq:
def __init__(self, tag, seq):
self.tag = tag
self.seq = seq
class Tag(OrderedDict):
def make(self, tag):
cells = tag.strip().split('|')
cell_len = len(cells)
n = 0
while n + 1 < cell_len:
self[cells[n]] = cells[n+1]
n += 2
def __str__(self):
tags = []
for k in self:
tags.extend([str(k), self[k]])
return '|'.join(tags)
def add(self, k, v):
self[k] = v.replace('|', '_')
@classmethod
def parse(cls, tag):
ob = cls()
ob.make(tag)
return ob
class MSeq(list):
def parse_fasta(self, string):
tag, seq = '', ''
for l in string.splitlines():
if not l:
continue
if l[0] == '>':
if seq:
self.add(tag.strip(), seq.strip().replace(' ', ''))
seq = ''
tag = l[1:]
else:
seq += l
self.add(tag.strip(), seq.strip().replace(' ', ''))
def parse_phylip(self, string):
blocks = string.split('\n\n')
i = 0
for block in blocks:
if not block:
continue
j = 0
for l in block.splitlines():
if not l:
continue
if l[0] != ' ':
cs = l.split()
self.add(cs[0], ''.join(cs[1:]))
elif i > 0:
self[j].seq += l.strip().replace(' ', '')
j += 1
i += 1
def parse_msf(self, string):
_, alg = string.split('//')
blocks = alg.strip().split('\n\n')
tags = []
tag2seq = {}
for block in blocks:
if not block:
continue
for l in block.splitlines():
if not l:
continue
cs = l.split()
if not cs[0] in tags:
tags.append(cs[0])
tag2seq[cs[0]] = ''
tag2seq[cs[0]] += ''.join(cs[1:])
for tag in tags:
self.add(tag, tag2seq[tag])
def iterparse_fasta(self, ITER):
tag, seq = '', ''
for l in ITER:
l = l.strip()
if l and l[0] == '>':
if seq:
yield Seq(tag.rstrip(), seq.strip().replace(' ', ''))
seq = ''
tag = l[1:]
else:
seq += l
yield Seq(tag.rstrip(), seq.strip().replace(' ', ''))
def add(self, tag, seq):
self.append(Seq(tag, seq))
def erase_common_gaps(self):
pos2num = defaultdict(int)
for o in self:
for idx in [i for i, x in enumerate(o.seq) if x == '-']:
pos2num[idx] += 1
seqnum = len(self)
pos_to_erase = []
for k, v in pos2num.items():
if v == seqnum:
pos_to_erase.append(k)
pos_to_erase.sort(reverse=True)
for o in self:
for pos in pos_to_erase:
o.seq = o.seq[:pos] + o.seq[pos+1:]
def upper(self):
for o in self:
if not o.seq.isupper():
o.seq = o.seq.upper()
def lower(self):
for o in self:
if not o.seq.islower():
o.seq = o.seq.lower()
def rm(self, char):
for o in self:
o.seq = o.seq.replace(char, '')
def seq_type(self):
seq_len = 0
c = Counter(self[0].seq.lower())
for k, v in c.items():
if k not in ('-', 'x'):
seq_len += v
if (c['a'] + c['c'] + c['g'] + c['t'] + c['u']) / float(seq_len) > .8:
if c['u']:
return 'RNA'
else:
return 'DNA'
else:
return 'Protein'
def seq_len(self, raise_error=False):
seqLen = 0
for ob in self:
currentLen = len(ob.seq)
if seqLen:
if seqLen != currentLen:
if raise_error:
sys.stderr.write('Mismatch in sequence len.\n')
sys.exit(0)
else:
return False
else:
seqLen = currentLen
return seqLen
def write_phylip(
self, oh,
max_tag_len=10, line_len=60, block_len=10
):
seq_len = len(self[0].seq)
seq_num = len(self)
seq_offset = max_tag_len+3
oh.write(' %s %s\n' % (seq_num, seq_len))
for i, s in enumerate(range(0, seq_len, line_len)):
for j in range(seq_num):
if i:
header = ' '*seq_offset
else:
tag = self[j].tag[:max_tag_len]
header = tag+ ' '*(seq_offset-len(tag))
seq = ' '.join(
self.chunks(self[j].seq[s:s+line_len], block_len)
)
oh.write(header+seq+'\n')
oh.write('\n')
def write_nexus(
self, oh,
max_tag_len=10, line_len=60
):
seq_len = len(self[0].seq)
seq_num = len(self)
seq_offset = max_tag_len+3
datatype=self.seq_type().lower()
if not datatype:
sys.stderr.write('unknown datatype\n')
sys.exit(0)
oh.write('#NEXUS\n\nBegin data;\n')
oh.write('Dimensions ntax=%s nchar=%s;\n' % (seq_num, seq_len))
oh.write('Format datatype='+datatype+' interleave=yes gap=-;\n')
oh.write('Matrix\n')
for i, s in enumerate(range(0, seq_len, line_len)):
for j in range(seq_num):
tag = self[j].tag[:max_tag_len]
header = tag+' '*(seq_offset-len(tag))
oh.write(header+self[j].seq[s:s+line_len]+"\n")
oh.write('\n')
oh.write('\t;\nend;\n')
def write_fasta(self, oh, line_len=60):
for ob in self:
oh.write(
">%s\n%s\n" % (
ob.tag,
"\n".join(self.chunks(ob.seq, line_len))
)
)
def write(
self, oh, outfmt='fasta',
max_tag_len=10, line_len=60, block_len=10, quiet=True
):
outfmt = outfmt.lower()
if outfmt in ('phylip', 'phy'):
self.write_phylip(
oh,
max_tag_len=max_tag_len,
line_len=line_len,
block_len=block_len
)
elif outfmt == 'tsv':
for ob in self:
oh.write(ob.tag+'\t'+ob.seq+'\n')
elif outfmt == 'csv':
for ob in self:
oh.write(ob.tag+','+ob.seq+'\n')
elif outfmt in ('fasta', 'fas', 'mfa', 'fna', 'fsa', 'fa'):
self.write_fasta(
oh,
line_len=line_len,
)
elif outfmt in ('nex', 'nxs', 'nexus'):
self.write_nexus(
oh,
line_len=line_len,
)
elif outfmt == 'msf':
seqs = []
tags = []
seq_len = len(self[0].seq)
seq_offset = max_tag_len + 3
for o in self:
tags.append(o.tag[:max_tag_len])
count = 0
seq = []
while 1:
frag = o.seq[count:count+block_len]
if not frag:
break
count += block_len
seq.append(frag.replace('-', '.'))
seqs.append(seq)
block_num = int(line_len/block_len)
datatype=self.seq_type()
if datatype == 'Protein':
datatype = 'P'
else:
datatype = 'N'
oh.write('PileUp\n\n')
oh.write(' MSF: %s TYPE: %s Check: 0 ..\n\n' % (seq_len, datatype))
for j in range(len(self)):
oh.write(
' Name: %s oo Len: %s Check: 0 Weight: 10.00 ..\n' % (tags[j], len(seqs[j]))
)
oh.write('\n//\n\n')
for i, n in enumerate(range(0, seq_len, line_len)):
for j in range(len(self)):
oh.write(tags[j]+' '*(seq_offset-len(tags[j]))+' '.join(seqs[j][i*block_num:(i+1)*block_num])+'\n')
oh.write('\n\n')
if not quiet:
sys.stdout.write('saved at {}\n'.format(oh.name))
@classmethod
def parse_string(cls, string, infmt='fasta'):
o = cls()
infmt = infmt.lower()
if infmt in ('fasta', 'fas', 'mfa', 'fna', 'fsa', 'fa'):
o.parse_fasta(string)
elif infmt == 'msf':
o.parse_msf(string)
elif infmt in ('phylip', 'phy'):
o.parse_phylip(string)
return o
@classmethod
def parse(cls, fh, infmt='fasta'):
return cls.parse_string(fh.read(), infmt=infmt)
@classmethod
def iterparse(cls, fh):
o = cls()
return o.iterparse_fasta(fh)
@staticmethod
def chunks(S, n):
return [S[c:c+n] for c in range(0, len(S), n)]