diff --git a/.gitignore b/.gitignore index cc3cdad..132b7fc 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,6 @@ __pycache__ .coverage .coverage.* tests/data/chr22* +.eggs/ +tests/data/genes.fasta.gz +*~ diff --git a/README.rst b/README.rst index 3a1f464..0baa0f9 100755 --- a/README.rst +++ b/README.rst @@ -335,6 +335,17 @@ The FastaVariant class provides a way to integrate single nucleotide variant cal >>> consensus = FastaVariant('tests/data/chr22.fasta', 'tests/data/chr22.vcf.gz', sample='NA06984', het=True, hom=True, call_filter='GT == "0/1"') >>> consensus['22'].variant_sites (16042793, 29187373, 29187448, 29194610, 29821332) + +You can also specify paths using ``pathlib.Path`` objects. + +.. code:: python + + #new in v0.7.1 + >>> from pyfaidx import Fasta + >>> from pathlib import Path + >>> genes = Fasta(Path('tests/data/genes.fasta')) + >>> genes + Fasta("tests/data/genes.fasta") Accessing fasta files from `filesystem_spec `_ filesystems: diff --git a/pyfaidx/__init__.py b/pyfaidx/__init__.py index 65b5ac8..3380ffb 100644 --- a/pyfaidx/__init__.py +++ b/pyfaidx/__init__.py @@ -353,7 +353,7 @@ def __init__(self, Sequence() object or as a raw string. Default: False (i.e. return a Sequence() object). """ - + if fsspec and isinstance(filename, fsspec.core.OpenFile): self.filename = filename.path assert getattr(filename, 'mode', 'rb') == 'rb' @@ -1061,7 +1061,6 @@ def __init__(self, filename: name of fasta file or fsspec.core.OpenFile instance indexname: name of index file or fsspec.core.OpenFile instance """ - self.filename = filename self.mutable = mutable self.faidx = Faidx( filename, @@ -1081,6 +1080,8 @@ def __init__(self, rebuild=rebuild, build_index=build_index) + self.filename = self.faidx.filename + _record_constructor = MutableFastaRecord if self.mutable else FastaRecord self.records = OrderedDict([(rname, _record_constructor(rname, self)) for rname in self.faidx.index.keys()]) diff --git a/tests/test_Path.py b/tests/test_Path.py new file mode 100644 index 0000000..4dbc669 --- /dev/null +++ b/tests/test_Path.py @@ -0,0 +1,29 @@ +import os +import pytest +from pathlib import Path +from pyfaidx import Faidx, Fasta + +path = os.path.dirname(__file__) +os.chdir(path) + +@pytest.fixture +def remove_index(): + yield + try: + os.remove('data/genes.fasta.fai') + except EnvironmentError: + pass # some tests may delete this file + +def test_Faidx(remove_index): + """ Ensures that Faidx can be created with a pathlib.Path as filename """ + filename = 'data/genes.fasta' + faidx = Faidx(filename) + faidx_w_path = Faidx(Path(filename)) + assert faidx.filename == faidx_w_path.filename + +def test_Fasta(remove_index): + """ Ensures that Fasta can be created with a pathlib.Path as filename """ + filename = 'data/genes.fasta' + fasta = Fasta(filename) + fasta_w_path = Fasta(Path(filename)) + assert fasta.filename == fasta_w_path.filename