From 880f6f95459e623c5167eda5b78179187bc9eacf Mon Sep 17 00:00:00 2001 From: Tobias Sargeant Date: Tue, 28 Jul 2020 16:58:34 +0100 Subject: [PATCH 1/3] Fix Faidx.write_fai() to produce output readable by Faidx.read_fai() * _index_as_string needs to be called. * :n formats using the locale, bit int() doesn't parse using the locale, which means that numbers may be output with commas, and cannot be read back. Fix by formating with :d for this case. --- pyfaidx/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyfaidx/__init__.py b/pyfaidx/__init__.py index d9f40f7..876a288 100644 --- a/pyfaidx/__init__.py +++ b/pyfaidx/__init__.py @@ -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.rlen:d}\t{v.offset:d}\t{v.lenc:d}\t{v.lenb:d}\n'.format(k=k, v=v) def read_fai(self): try: @@ -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): From 19955ee0ea314c3e991336e460082d7f46a7372c Mon Sep 17 00:00:00 2001 From: Tobias Sargeant Date: Tue, 28 Jul 2020 18:27:03 +0100 Subject: [PATCH 2/3] Add a test for Faidx.write_fai() --- tests/test_feature_indexing.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_feature_indexing.py b/tests/test_feature_indexing.py index e55dd9e..a0000aa 100644 --- a/tests/test_feature_indexing.py +++ b/tests/test_feature_indexing.py @@ -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. From 956f315af48a22fe010a106eb53b5ea8b4db6604 Mon Sep 17 00:00:00 2001 From: Matt Shirley Date: Wed, 29 Jul 2020 10:22:21 -0400 Subject: [PATCH 3/3] Update IndexRecord.__str__ method to disregard locale --- pyfaidx/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyfaidx/__init__.py b/pyfaidx/__init__.py index 876a288..0024904 100644 --- a/pyfaidx/__init__.py +++ b/pyfaidx/__init__.py @@ -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): @@ -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 '{k}\t{v.rlen:d}\t{v.offset:d}\t{v.lenc:d}\t{v.lenb:d}\n'.format(k=k, v=v) + yield '{k}\t{v}\n'.format(k=k, v=str(v)) def read_fai(self): try: