-
Notifications
You must be signed in to change notification settings - Fork 0
/
maf_splitfa.py
50 lines (43 loc) · 1.28 KB
/
maf_splitfa.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
import sys
import fileinput
from re import split
def mfa_iter(mfa_file):
"""
iterate over MAF derived multi-block fasta file
"""
header = ''
seqs = dict()
with fileinput.input(mfa_file) as f:
for line in f:
if line.startswith('\n'):
if header != '':
yield(header, seqs)
header = ''
seqs = dict()
elif line.startswith('>'):
tmp = split(' |\.', line.strip()[1:])
src = tmp[0]
if header == '':
header = tmp[1]
seq = next(f).rstrip().upper()
seqs[src] = seq.translate({ord('#'): ord('-')})
if header != '':
yield(header, seqs)
def split_mfa(mfa_file, out_path):
"""
perform split
"""
if not out_path.endswith('/'):
out_path = out_path + '/'
for header, block in mfa_iter(mfa_file):
new_file = out_path + header + '.fa'
fh = open(new_file, 'w')
for sp in block:
seq_wgap = block[sp]
if len(seq_wgap.replace('-', '')) > 0:
print(">" + sp, file=fh)
print(seq_wgap, file=fh)
fh.close()
return
if __name__ == '__main__':
split_mfa(*sys.argv[1:])