Skip to content

Commit

Permalink
Merge pull request mdshw5#184 from palao/develop
Browse files Browse the repository at this point in the history
pathlib.Path can be passed to Fasta and Faidx
  • Loading branch information
mdshw5 authored Jan 31, 2022
2 parents 5a81446 + bdb6d7a commit 18993f7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 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
*~
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,16 @@ 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
>>> from pyfaidx import Fasta
>>> from pathlib import Path
>>> genes = Fasta(Path('tests/data/genes.fasta'))
>>> genes
Fasta("tests/data/genes.fasta")
.. _faidx:
Expand Down
2 changes: 2 additions & 0 deletions pyfaidx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ def __init__(self,
Sequence() object or as a raw string.
Default: False (i.e. return a Sequence() object).
"""
filename = str(filename)
self.filename = filename

if filename.lower().endswith('.bgz') or filename.lower().endswith(
Expand Down Expand Up @@ -997,6 +998,7 @@ def __init__(self,
An object that provides a pygr compatible interface.
filename: name of fasta file
"""
filename = str(filename)
self.filename = filename
self.mutable = mutable
self.faidx = Faidx(
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 18993f7

Please sign in to comment.