Skip to content

Commit

Permalink
Merge pull request #166 from folded/fix_write_fai
Browse files Browse the repository at this point in the history
Fix write_fai
  • Loading branch information
mdshw5 authored Jul 29, 2020
2 parents d85dfd4 + 956f315 commit 79273ee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions pyfaidx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def __getitem__(self, key):
return tuple.__getitem__(self, key)

def __str__(self):
return "{rlen:n}\t{offset:n}\t{lenc:n}\t{lenb:n}\n".format(
return "{rlen:d}\t{offset:d}\t{lenc:d}\t{lenb:d}".format(
**self._asdict())

def __len__(self):
Expand Down Expand Up @@ -460,7 +460,7 @@ def __repr__(self):
def _index_as_string(self):
""" Returns the string representation of the index as iterable """
for k, v in self.index.items():
yield '\t'.join([k, str(v)])
yield '{k}\t{v}\n'.format(k=k, v=str(v))

def read_fai(self):
try:
Expand Down Expand Up @@ -607,7 +607,7 @@ def build_index(self):
def write_fai(self):
with self.lock:
with open(self.indexname, 'w') as outfile:
for line in self._index_as_string:
for line in self._index_as_string():
outfile.write(line)

def from_buffer(self, start, end):
Expand Down
14 changes: 13 additions & 1 deletion tests/test_feature_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,24 @@ def test_open(*args, **kwargs):
finally:
shutil.rmtree(tmp_dir)

def test_read_back_index(self):
"""Ensure that index files written with write_fai() can be read back"""
import locale
old_locale = locale.getlocale(locale.LC_NUMERIC)
try:
locale.setlocale(locale.LC_NUMERIC, 'en_US.utf8')
faidx = Faidx('data/genes.fasta')
faidx.write_fai()
faidx = Faidx('data/genes.fasta', build_index=False)
finally:
locale.setlocale(locale.LC_NUMERIC, old_locale)

@raises(IndexNotFoundError)
def test_issue_134_no_build_index(self):
""" Ensure that index file is not built when build_index=False. See mdshw5/pyfaidx#134.
"""
faidx = Faidx('data/genes.fasta', build_index=False)

@raises(FastaIndexingError)
def test_issue_144_no_defline(self):
""" Ensure that an exception is raised when a file contains no deflines. See mdshw5/pyfaidx#144.
Expand Down

0 comments on commit 79273ee

Please sign in to comment.