-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastaReader.py
74 lines (61 loc) · 2.18 KB
/
fastaReader.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
'''
Author: David L. Bernick
Edited by: Jairo Navarro
The MIT License (MIT)
Copyright (c) 2015 David L. Bernick, Jairo Navarro
'''
import sys
class FastAreader :
'''
Class to provide reading of a file containing one or more FASTA
formatted sequences:
object instantiation:
FastAreader(<file name>):
object attributes:
fname: the initial file name
methods:
readFasta() : returns header and sequence as strings.
Author: David Bernick
Date: April 19, 2013
'''
def __init__ (self, fname=''):
'''contructor: saves attribute fname '''
self.fname = fname
def doOpen (self):
if self.fname is '':
return sys.stdin
else:
return open(self.fname)
def readFasta (self):
'''
using filename given in init, returns each included FastA record
as 2 strings - header and sequence. If filename was not provided,
stdin is used. Whitespace is removed, and sequence is upcased.
The initial '>' is removed from the header.
'''
header = ''
sequence = ''
with self.doOpen() as fileH:
# initialize return containers
header = ''
sequence = ''
# skip to first fasta header
line = fileH.readline()
while not line.startswith('>') :
line = fileH.readline()
header = line[1:].rstrip()
# header is saved, get the rest of the sequence
# up until the next header is found
# then yield the results and wait for the next call.
# next call will resume at the yield point
# which is where we have the next header
for line in fileH:
if line.startswith ('>'):
yield header,sequence
header = line[1:].rstrip()
sequence = ''
else :
sequence += ''.join(line.rstrip().split()).upper()
# final header and sequence will be seen with an end of file
# with clause will terminate, so we do the final yield of the data
yield header,sequence