Skip to content

Commit

Permalink
Merge pull request #196 from mdshw5/develop
Browse files Browse the repository at this point in the history
Merge changes from #184 to release
  • Loading branch information
mdshw5 committed Jul 22, 2022
2 parents bf48bb4 + bee57e7 commit 00e62b8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ __pycache__
.coverage
.coverage.*
tests/data/chr22*
.eggs/
tests/data/genes.fasta.gz
*~
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://filesystem-spec.readthedocs.io>`_ filesystems:
Expand Down
5 changes: 3 additions & 2 deletions pyfaidx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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()])

Expand Down
29 changes: 29 additions & 0 deletions tests/test_Path.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 00e62b8

Please sign in to comment.