upstream-dependencies/galaxy-sequence-utils/galaxy_utils/sequence/fasta.py
1# Dan Blankenberg
2import bz2
3import gzip
4
5
6class fastaSequence:
7 def __init__(self):
8 self.identifier = None
9 self.sequence = '' # holds raw sequence string: no whitespace
10
11 def __len__(self):
12 return len(self.sequence)
13
14 def __str__(self):
15 return f"{self.identifier}\n{self.sequence}\n"
16
17
18class fastaReader:
19 def __init__(self, fh):
20 self.file = fh
21
22 def close(self):
23 return self.file.close()
24
25 def __next__(self):
26 line = self.file.readline()
27 # remove header comment lines
28 while line and line.startswith('#'):
29 line = self.file.readline()
30 if not line:
31 raise StopIteration
32 assert line.startswith('>'), "FASTA headers must start with >"
33 rval = fastaSequence()
34 rval.identifier = line.strip()
35 offset = self.file.tell()
36 while True:
37 line = self.file.readline()
38 if not line or line.startswith('>'):
39 if line:
40 self.file.seek(offset) # this causes sequence id lines to be read twice, once to determine previous sequence end and again when getting actual sequence; can we cache this to prevent it from being re-read?
41 return rval
42 # 454 qual test data that was used has decimal scores that don't have trailing spaces
43 # so we'll need to parse and build these sequences not based upon de facto standards
44 # i.e. in a less than ideal fashion
45 line = line.rstrip()
46 if ' ' in rval.sequence or ' ' in line:
47 rval.sequence = f"{rval.sequence}{line} "
48 else:
49 rval.sequence += line
50 offset = self.file.tell()
51
52 def __iter__(self):
53 while True:
54 try:
55 yield next(self)
56 except StopIteration:
57 self.close()
58 # Catch exception and return normally
59 return
60
61
62class fastaNamedReader:
63 def __init__(self, fh):
64 self.file = fh
65 self.reader = fastaReader(self.file)
66 self.offset_dict = {}
67 self.eof = False
68
69 def close(self):
70 return self.file.close()
71
72 def get(self, sequence_id):
73 if not isinstance(sequence_id, str):
74 sequence_id = sequence_id.identifier
75 rval = None
76 if sequence_id in self.offset_dict:
77 initial_offset = self.file.tell()
78 seq_offset = self.offset_dict[sequence_id].pop(0)
79 if not self.offset_dict[sequence_id]:
80 del self.offset_dict[sequence_id]
81 self.file.seek(seq_offset)
82 rval = next(self.reader)
83 self.file.seek(initial_offset)
84 else:
85 while True:
86 offset = self.file.tell()
87 try:
88 fasta_seq = next(self.reader)
89 except StopIteration:
90 self.eof = True
91 break # eof, id not found, will return None
92 if fasta_seq.identifier == sequence_id:
93 rval = fasta_seq
94 break
95 else:
96 if fasta_seq.identifier not in self.offset_dict:
97 self.offset_dict[fasta_seq.identifier] = []
98 self.offset_dict[fasta_seq.identifier].append(offset)
99 return rval
100
101 def has_data(self):
102 # returns a string representation of remaining data, or empty string (False) if no data remaining
103 eof = self.eof
104 count = 0
105 rval = ''
106 if self.offset_dict:
107 count = sum(map(len, self.offset_dict.values()))
108 if not eof:
109 offset = self.file.tell()
110 try:
111 next(self.reader)
112 except StopIteration:
113 eof = True
114 self.file.seek(offset)
115 if count:
116 rval = f"There were {count:d} known sequences not utilized. "
117 if not eof:
118 rval += "An additional unknown number of sequences exist in the input that were not utilized."
119 return rval
120
121
122class fastaWriter:
123 def __init__(self, fh=None, format=None, path=None):
124 if fh is None:
125 assert path is not None
126 if format and format.endswith(".gz"):
127 fh = gzip.open(path, "wt")
128 elif format and format.endswith(".bz2"):
129 fh = bz2.open(path, mode="wt")
130 else:
131 fh = open(path, "w")
132 else:
133 if format and format.endswith(".gz"):
134 fh = gzip.GzipFile(fileobj=fh)
135 elif format and format.endswith(".bz2"):
136 raise Exception("bz2 formats do not support file handle inputs")
137 self.file = fh
138
139 def write(self, fasta_read):
140 # this will include color space adapter base if applicable
141 self.file.write(f">{fasta_read.identifier[1:]}\n{fasta_read.sequence}\n")
142
143 def close(self):
144 return self.file.close()