Skip to content

SeqRecord.reverse_complement() edits parent sequence in place #5015

Closed

Description

@manulera

This likely has to do with recent changes in the biopython class instantiation (1.85 I believe).

cc @peterjc

from Bio.SeqRecord import SeqRecord
from Bio.Seq import Seq

a = SeqRecord(Seq("AA"))
print(a._per_letter_annotations)
a.reverse_complement()
print(a._per_letter_annotations)

Prints

None
{}

Activity

  1. manulera commented on Jun 18, 2025

    @manulera
    ContributorAuthor

    I am happy to contribute the fix, but I don't really know where to start looking.

  2. peterjc commented on Jun 21, 2025

    @peterjc
    Member

    The sequence is NOT edited (as per the issue title):

    >>> s = Seq("AAAA")
    >>> sr = SeqRecord(s)
    >>> sr.reverse_complement()
    SeqRecord(seq=Seq('TTTT'), id='<unknown id>', name='<unknown name>', description='<unknown description>', dbxrefs=[])
    >>> sr
    SeqRecord(seq=Seq('AAAA'), id='<unknown id>', name='<unknown name>', description='<unknown description>', dbxrefs=[])
    >>> s
    Seq('AAAA')

    Nor does the public property .letter_annotations change:

    >>> sr = SeqRecord(Seq("A"))
    >>> sr.letter_annotations
    {}
    >>> sr.reverse_complement()
    SeqRecord(seq=Seq('T'), id='<unknown id>', name='<unknown name>', description='<unknown description>', dbxrefs=[])
    >>> sr.letter_annotations
    {}

    Yes, it looks like the private property ._per_letter_annotations gets initialised from None to {} but that is an internal implementation detail.

  3. mdehoon commented on Jun 21, 2025

    @mdehoon
    Contributor

    Yes, it looks like the private property ._per_letter_annotations gets initialised from None to {} but that is an internal implementation detail.

    Still it is strange that this happens.

  4. peterjc commented on Jun 21, 2025

    @peterjc
    Member

    It happens here in the reverse-complement method:

            if isinstance(letter_annotations, dict):
                answer.letter_annotations = letter_annotations
            elif letter_annotations:
                # Copy the old per letter annotations, reversing them
                for key, value in self.letter_annotations.items():
                    answer.letter_annotations[key] = value[::-1]

    This calls the .letters_annotations property which will initialize the dict as the internal value was still None. So it is just a delayed initialization.

  5. mdehoon commented on Jun 21, 2025

    @mdehoon
    Contributor

    This calls the .letters_annotations property which will initialize the dict as the internal value was still None. So it is just a delayed initialization.

    But in general we don't want to initialize _per_letter_annotations if there is no need for it. Then I think reverse_complement should look at _per_letter_annotations instead of letter_annotations, and do nothing if _per_letter_annotations is None.

  6. peterjc commented on Jun 21, 2025

    @peterjc
    Member

    That would be better I suppose.

  7. manulera commented on Jun 23, 2025

    @manulera
    ContributorAuthor

    For context, in pydna we have an equality function for seqrecords which we use in tests. I know you specifically discourage this but it is quite handy to compare objects to be equal in tests. That's how it impacts us.

  8. added 2 commits that reference this issue on Jul 18, 2025
    04bfcb3
    9edecd4
  9. added a commit that references this issue on Jul 24, 2025
    b027825
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions