From 67a37865e5f5af8897ae1afc10916f0ecf1ac18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Johansson?= Date: Mon, 10 Mar 2025 14:01:25 +0000 Subject: [PATCH] Retain letter_annotation as None in upper and lower methods (#4966) * gitignore .python-version * added name & email * fix so that _per_letter_annotations is not set by calling upper or lower methods * added test for initiation of SeqRecord checking the __dict__ and a test for the __dict__ created by upper --- .gitignore | 6 ++++++ Bio/SeqRecord.py | 12 ++++++++++-- CONTRIB.rst | 1 + Tests/test_SeqRecord.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 2f8a2c8d8..19d579278 100644 --- a/.gitignore +++ b/.gitignore @@ -134,3 +134,9 @@ ENV/ # Testing **/.coverage + +# Ignore the .python-version file used by pyenv +# This file specifies the Python version for a project, +# but should not be committed to Git, as different developers +# may use different Python versions. +.python-version diff --git a/Bio/SeqRecord.py b/Bio/SeqRecord.py index 739942d75..648d3c51e 100644 --- a/Bio/SeqRecord.py +++ b/Bio/SeqRecord.py @@ -1127,7 +1127,11 @@ class SeqRecord: dbxrefs=self.dbxrefs[:], features=self.features[:], annotations=self.annotations.copy(), - letter_annotations=self.letter_annotations.copy(), + letter_annotations=( + None + if self._per_letter_annotations is None + else self.letter_annotations.copy() + ), ) def lower(self) -> "SeqRecord": @@ -1174,7 +1178,11 @@ class SeqRecord: dbxrefs=self.dbxrefs[:], features=self.features[:], annotations=self.annotations.copy(), - letter_annotations=self.letter_annotations.copy(), + letter_annotations=( + None + if self._per_letter_annotations is None + else self.letter_annotations.copy() + ), ) def isupper(self): diff --git a/CONTRIB.rst b/CONTRIB.rst index 5fc55b186..41e35b942 100644 --- a/CONTRIB.rst +++ b/CONTRIB.rst @@ -66,6 +66,7 @@ please open an issue on GitHub or mention it on the mailing list. - Bertrand Frottier - Bertrand Néron - Bill Barnard +- Björn Johansson - Blaise Li - Bob Bussell - Bogdan T. diff --git a/Tests/test_SeqRecord.py b/Tests/test_SeqRecord.py index f6ad5fe2e..cfb45fa8e 100644 --- a/Tests/test_SeqRecord.py +++ b/Tests/test_SeqRecord.py @@ -133,6 +133,21 @@ class SeqRecordCreation(unittest.TestCase): with self.assertRaises(TypeError): SeqRecord(Seq("ACGT"), features={}) + def test_default_properties(self): + seqobj = Seq("A") + default__dict__ = { + "_seq": seqobj, + "id": "", + "name": "", + "description": "", + "dbxrefs": [], + "annotations": {}, + "_per_letter_annotations": None, + "features": [], + } + bsr = SeqRecord(seqobj) + self.assertEqual(bsr.__dict__, default__dict__) + class SeqRecordMethods(unittest.TestCase): """Test SeqRecord methods.""" @@ -226,6 +241,20 @@ Seq('ABCDEFGHIJKLMNOPQRSTUVWZYX')""" def test_upper(self): self.assertEqual("ABCDEFGHIJKLMNOPQRSTUVWZYX", self.record.lower().upper().seq) + seqobj = Seq("A") + default__dict__ = { + "_seq": seqobj, + "id": "", + "name": "", + "description": "", + "dbxrefs": [], + "annotations": {}, + "_per_letter_annotations": None, + "features": [], + } + bsr = SeqRecord(seqobj) + bsru = bsr.upper() + self.assertEqual(bsru.__dict__, default__dict__) def test_lower(self): self.assertEqual("abcdefghijklmnopqrstuvwzyx", self.record.lower().seq)