-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastaPar.py
38 lines (27 loc) · 1020 Bytes
/
fastaPar.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
def fastaParser():
with open("seqs.fa") as fh:
header = ""
#header = None
sequence = ""
for line in fh:
line = line.strip()
if line.startswith(">"):
#if header:
if len(header) > 0:
#only greater than 0 if youve already read a header line in...
yield (header,sequence)
sequence = ""
header = line.lstrip('>')
#line is a header
header = line.lstrip(">")
else:
#line is a sequence line
sequence += line.strip()
#need this bc if not the last seq wont be printed bc the trigger for yield above is theh next >... there is not next for the last seq
yield(header,sequence)
def main():
for seqName,seq in fastaParser():
print(seqName)
print(seq)
if __name__ == '__main__':
main()