-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasta.py
49 lines (46 loc) · 1.26 KB
/
fasta.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
import sys
class fastaReader:
def __init__(self, fn):
self.done=False
if fn=='-':
self.ifile=sys.stdin
else:
self.ifile=open(fn)
self.hdr=self.ifile.readline().rstrip()
def __iter__(self):
return self
def __next__(self):
if self.done:
raise StopIteration
body=''
while True:
l=self.ifile.readline().rstrip()
if not l:
self.done=True
if not l or l[0]=='>':
hdr=self.hdr
self.hdr=l
return (hdr, body)
else:
body+=l
class fastaWriter:
def __init__(self, fn, linelen=60):
self.ofile=open(fn, 'w')
self.linelen=linelen
def close(self):
self.ofile.close()
def writeFA(self, hdr, body):
pos=0
stop=len(body)
self.ofile.write(hdr)
self.ofile.write('\n')
while pos<stop:
self.ofile.write(body[pos:pos+self.linelen])
self.ofile.write('\n')
pos+=self.linelen
if __name__=='__main__':
rdr=fastaReader(sys.argv[1])
wrter=fastaWriter(sys.argv[2])
for hdr, body in rdr:
wrter.writeFA(hdr, body)
wrter.close()