-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsimplefasta.py
51 lines (40 loc) · 1.17 KB
/
simplefasta.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
from __future__ import print_function
try:
from builtins import zip
from builtins import str
except ImportError:
pass
def readfasta(infile):
labels = []
sequences = []
curlabel = None
cursequence = ""
def updatelists():
if len(cursequence) != 0:
sequences.append(cursequence)
if curlabel is not None:
labels.append(curlabel)
else:
labels.append('seq'+str(len(sequences)))
for line in infile:
if line[0] == ">":
updatelists()
cursequence = ""
curlabel = line[1:].strip()
else:
cursequence += line.strip()
updatelists()
return list(zip(labels, sequences))
def writefasta(outfile, entries):
for label, seq in entries:
print(">" + label, file=outfile)
print(seq, file=outfile)
if __name__ == "__main__":
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin)
args = parser.parse_args()
results = readfasta(args.infile)
for result in results:
print(result)