From 074e10e633854c415b5cb123469f3a3fcc2fdefe Mon Sep 17 00:00:00 2001 From: David Palao Date: Fri, 28 Jan 2022 13:13:26 +0100 Subject: [PATCH 01/10] chore: ignoring .eggs dir and test data file fetched by download_gene_fasta.py --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index cc3cdad..5550efa 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ __pycache__ .coverage .coverage.* tests/data/chr22* +.eggs/ +tests/data/genes.fasta.gz From 07ea5974b8a3914556412e0e09ecb9364dbc759b Mon Sep 17 00:00:00 2001 From: David Palao Date: Fri, 28 Jan 2022 14:42:08 +0100 Subject: [PATCH 02/10] test: permit pathlib.Path instances as filenames --- tests/test_Path.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/test_Path.py diff --git a/tests/test_Path.py b/tests/test_Path.py new file mode 100644 index 0000000..95e6d70 --- /dev/null +++ b/tests/test_Path.py @@ -0,0 +1,42 @@ +import os +from unittest import TestCase +from pathlib import Path + +from pyfaidx import Faidx, Fasta + + +path = os.path.dirname(__file__) +os.chdir(path) + + +class TestAcceptPath(TestCase): + def setUp(self): + pass + + def tearDown(self): + try: + os.remove('data/genes.fasta.fai') + except EnvironmentError: + pass # some tests may delete this file + + def test_Faidx(self): + """ 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(self): + """ 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 + + + + + + + + From 71126a922a49a98bf50d75062f305a173692641d Mon Sep 17 00:00:00 2001 From: David Palao Date: Fri, 28 Jan 2022 14:43:35 +0100 Subject: [PATCH 03/10] improvement: pathlib.Path can be passed to Fasta and Faidx --- pyfaidx/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyfaidx/__init__.py b/pyfaidx/__init__.py index 1d192b0..15dbd5d 100644 --- a/pyfaidx/__init__.py +++ b/pyfaidx/__init__.py @@ -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( @@ -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( From f2c8563bfeba2c5fc5909da36df0f065e9098361 Mon Sep 17 00:00:00 2001 From: David Palao Date: Fri, 28 Jan 2022 14:52:42 +0100 Subject: [PATCH 04/10] chore: ignoring emacs backup files in git --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5550efa..132b7fc 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ __pycache__ tests/data/chr22* .eggs/ tests/data/genes.fasta.gz +*~ From 542e71b313ad3430f8df4d2662d6aae82e3fcef6 Mon Sep 17 00:00:00 2001 From: Matt Shirley Date: Fri, 28 Jan 2022 09:21:11 -0500 Subject: [PATCH 05/10] Migrate tests to pytest framework --- tests/test_Path.py | 57 ++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/tests/test_Path.py b/tests/test_Path.py index 95e6d70..4dbc669 100644 --- a/tests/test_Path.py +++ b/tests/test_Path.py @@ -1,42 +1,29 @@ import os -from unittest import TestCase +import pytest from pathlib import Path - from pyfaidx import Faidx, Fasta - path = os.path.dirname(__file__) os.chdir(path) - -class TestAcceptPath(TestCase): - def setUp(self): - pass - - def tearDown(self): - try: - os.remove('data/genes.fasta.fai') - except EnvironmentError: - pass # some tests may delete this file - - def test_Faidx(self): - """ 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(self): - """ 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 - - - - - - - - +@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 From d4bf53525b72b90f91806fac90f67fcdaeb7ecd2 Mon Sep 17 00:00:00 2001 From: Matt Shirley Date: Fri, 28 Jan 2022 09:28:43 -0500 Subject: [PATCH 06/10] Trigger GH actions run From bdb6d7a1e0ca498c106f0ec93e58b2ef1d9b6a66 Mon Sep 17 00:00:00 2001 From: Matt Shirley Date: Fri, 28 Jan 2022 09:47:17 -0500 Subject: [PATCH 07/10] Add documentation for #184 --- README.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.rst b/README.rst index 5505948..3dba57b 100755 --- a/README.rst +++ b/README.rst @@ -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: From 6a73aab5bce6efbf93d8996e66cf1abd5c39e89c Mon Sep 17 00:00:00 2001 From: Matt Shirley Date: Fri, 22 Jul 2022 10:57:21 -0400 Subject: [PATCH 08/10] Update README.rst --- README.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 87a7850..22ac988 100755 --- a/README.rst +++ b/README.rst @@ -339,7 +339,8 @@ The FastaVariant class provides a way to integrate single nucleotide variant cal 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')) From e9a7e70163b5b75bbf848b61fb2bb6f2f363dbe5 Mon Sep 17 00:00:00 2001 From: Matt Shirley Date: Fri, 22 Jul 2022 11:06:04 -0400 Subject: [PATCH 09/10] Do not call str() on paths --- pyfaidx/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyfaidx/__init__.py b/pyfaidx/__init__.py index 90998ee..498740d 100644 --- a/pyfaidx/__init__.py +++ b/pyfaidx/__init__.py @@ -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 """ - filename = str(filename) self.filename = filename self.mutable = mutable self.faidx = Faidx( From bee57e78012f2b334674871e764447bc2f756790 Mon Sep 17 00:00:00 2001 From: Matt Shirley Date: Fri, 22 Jul 2022 11:17:59 -0400 Subject: [PATCH 10/10] Inherit Fasta.filename from Faidx.filename --- pyfaidx/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyfaidx/__init__.py b/pyfaidx/__init__.py index 498740d..3380ffb 100644 --- a/pyfaidx/__init__.py +++ b/pyfaidx/__init__.py @@ -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()])