-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simpler code, adds .xz support
- Loading branch information
Showing
4 changed files
with
62 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
def fasta_iter(fname, full_header=False): | ||
'''Iterate over a (possibly gzipped) FASTA file | ||
Parameters | ||
---------- | ||
fname : str | ||
Filename. | ||
If it ends with .gz, gzip format is assumed | ||
If .bz2 then bzip2 format is assumed | ||
if .xz, then lzma format is assumerd | ||
full_header : boolean (optional) | ||
If True, yields the full header. Otherwise (the default), only the | ||
first word | ||
Yields | ||
------ | ||
(h,seq): tuple of (str, str) | ||
''' | ||
header = None | ||
chunks = [] | ||
if fname.endswith('.gz'): | ||
import gzip | ||
op = gzip.open | ||
elif fname.endswith('.bz2'): | ||
import bz2 | ||
op = bz2.open | ||
elif fname.endswith('.xz'): | ||
import lzma | ||
op = lzma.open | ||
else: | ||
op = open | ||
with op(fname, 'rt') as f: | ||
for line in f: | ||
if line[0] == '>': | ||
if header is not None: | ||
yield header,''.join(chunks) | ||
line = line[1:].strip() | ||
if not line: | ||
header = '' | ||
elif full_header: | ||
header = line.strip() | ||
else: | ||
header = line.split()[0] | ||
chunks = [] | ||
else: | ||
chunks.append(line.strip()) | ||
if header is not None: | ||
yield header, ''.join(chunks) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters