Repository: cogent3/cogent3
Pull request #2117 (2024-11-19 21:08:14)

Misc
## Summary by Sourcery

Enhance file handling by adding support for .phy as a phylip format and improve fasta record formatting speed. Update tests to include phylip file suffix handling and document changes in the changelog.

Enhancements:
- Added support for recognizing .phy as a phylip format in sequence/alignment loading functions.
- Improved the formatting speed of fasta records.

Documentation:
- Updated changelog to reflect enhancements in file handling and fasta record formatting.

Tests:
- Introduced a new test for phylip file suffixes to ensure correct parser selection.


Patch:
diff --git a/changelog.d/20241025_082035_Gavin.Huttley.md b/changelog.d/20241025_082035_Gavin.Huttley.md
new file mode 100644
index 000000000..37f7dce7b
--- /dev/null
+++ b/changelog.d/20241025_082035_Gavin.Huttley.md
@@ -0,0 +1,42 @@
+<!--
+A new scriv changelog fragment.
+
+Uncomment the section that is right (remove the HTML comment wrapper).
+-->
+
+<!--
+### Contributors
+
+- A bullet item for the Contributors category.
+
+-->
+
+### ENH
+
+- `cogent3.open_()` now automatically handles .xz and .lzma compressed files.
+- sequence / alignment loading functions recognise .phy as phylip format.
+
+<!--
+### BUG
+
+- A bullet item for the BUG category.
+
+-->
+<!--
+### DOC
+
+- A bullet item for the DOC category.
+
+-->
+<!--
+### Deprecations
+
+- A bullet item for the Deprecations category.
+
+-->
+<!--
+### Discontinued
+
+- A bullet item for the Discontinued category.
+
+-->
diff --git a/changelog.d/20241026_143333_Gavin.Huttley.md b/changelog.d/20241026_143333_Gavin.Huttley.md
new file mode 100644
index 000000000..ef0f390a5
--- /dev/null
+++ b/changelog.d/20241026_143333_Gavin.Huttley.md
@@ -0,0 +1,42 @@
+<!--
+A new scriv changelog fragment.
+
+Uncomment the section that is right (remove the HTML comment wrapper).
+-->
+
+<!--
+### Contributors
+
+- A bullet item for the Contributors category.
+
+-->
+
+### ENH
+
+- Formatting of fasta records is now much quicker.
+
+
+<!--
+### BUG
+
+- A bullet item for the BUG category.
+
+-->
+<!--
+### DOC
+
+- A bullet item for the DOC category.
+
+-->
+<!--
+### Deprecations
+
+- A bullet item for the Deprecations category.
+
+-->
+<!--
+### Discontinued
+
+- A bullet item for the Discontinued category.
+
+-->
diff --git a/src/cogent3/parse/sequence.py b/src/cogent3/parse/sequence.py
index b77541a0e..efe1ac0e2 100644
--- a/src/cogent3/parse/sequence.py
+++ b/src/cogent3/parse/sequence.py
@@ -111,6 +111,7 @@ class LineBasedParser:
 
 PARSERS = {
     "phylip": LineBasedParser(phylip.MinimalPhylipParser),
+    "phy": LineBasedParser(phylip.MinimalPhylipParser),
     "paml": LineBasedParser(paml.PamlParser),
     "fasta": fasta.iter_fasta_records,
     "mfa": fasta.iter_fasta_records,


Test patch:
diff --git a/tests/test_parse/test_sequence.py b/tests/test_parse/test_sequence.py
index a9cc56acd..7c9f7274e 100644
--- a/tests/test_parse/test_sequence.py
+++ b/tests/test_parse/test_sequence.py
@@ -1,4 +1,3 @@
-# the delegator module
 import pathlib
 
 import pytest
@@ -74,3 +73,19 @@ def test_is_genbank(fmt):
 @pytest.mark.parametrize("fmt", ("blah", "fa", "xml", "nex", None))
 def test_is_not_genbank(fmt):
     assert not sequence.is_genbank(fmt)
+
+
+@pytest.fixture(params=("phylip", "phy"))
+def phylip_file(DATA_DIR, tmp_path, request):
+    with open(DATA_DIR / "interleaved.phylip", mode="rt") as f:
+        data = f.read()
+    outpath = tmp_path / f"interleaved.{request.param}"
+    outpath.write_text(data)
+    return outpath
+
+
+def test_select_parser_phylip_suffixes(phylip_file):
+    from cogent3 import load_aligned_seqs
+
+    got = load_aligned_seqs(phylip_file)
+    assert set(got.names) == {"human", "chimp", "mouse"}