Skip to content

Date ignored in Genbank writer with not-english locale #4972

Closed

Description

@bryan-brancotte

Hi,

I am writting genbank file, and sometime date are silently ignored and replace by 1980. My first suggestion is to add a warning somehow indicating that the date annotation is invalide.

When investigating the code producing the date if found that datetime instance are only considered if the abbreviated month is equivalent to the english abbreviation. With not english locale it thus sometime ignore the date and silently replace it by 01-JAN-1980.

If you confirm me that it is not the expected behavior to not work with non-english locale , I can work on a PR

Best regards,

Step to reproduce:

"""
docker run -it python:3.12 bash
apt-get update && apt-get install -y locales
echo "fr_FR.UTF-8 UTF-8" >> /etc/locale.gen
echo "de_DE.UTF-8 UTF-8" >> /etc/locale.gen
echo "pt_BR.UTF-8 UTF-8" >> /etc/locale.gen
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
echo "en_GB.UTF-8 UTF-8" >> /etc/locale.gen
locale-gen
locale -a # check that all three lang are not there
pip install git+https://github.com/biopython/biopython.git
python # and run following script
"""

import locale
from datetime import datetime
from tempfile import NamedTemporaryFile

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

date = datetime.strptime('2025-10-01', '%Y-%m-%d')
record = SeqRecord(
    Seq('ATGC'),
    id="foo",
    annotations={
        "molecule_type": "dna",
        "topology": "circular",
        "date": date,
    },
)
this_year = str(date.year)
for l in [
    'en_GB.utf8',
    'de_DE.utf8',
    'fr_FR.utf8',
    'pt_BR.utf8',
    'en_US.utf8',
]:
    locale.setlocale(locale.LC_ALL, l)
    with NamedTemporaryFile(suffix='.gb') as file:
        with open(file.name, 'w') as gb_file:
            Bio.SeqIO.write(record, gb_file, "genbank")
        with open(file.name, 'r') as written_file:
            first_line = written_file.readlines()[0]
            print(f"In {l}: {first_line}")
            if this_year not in first_line:
                print(f"ERROR with {l} as date is {date.strftime('%d-%b-%Y').upper()}")
'en_GB.utf8'
1
In en_GB.utf8: LOCUS       foo                        4 bp    dna     circular UNK 01-OCT-2025

'de_DE.utf8'
1
In de_DE.utf8: LOCUS       foo                        4 bp    dna     circular UNK 01-JAN-1980

ERROR with de_DE.utf8 as date is 01-OKT-2025
'fr_FR.utf8'
1
In fr_FR.utf8: LOCUS       foo                        4 bp    dna     circular UNK 01-JAN-1980

ERROR with fr_FR.utf8 as date is 01-OCT.-2025
'pt_BR.utf8'
1
In pt_BR.utf8: LOCUS       foo                        4 bp    dna     circular UNK 01-JAN-1980

ERROR with pt_BR.utf8 as date is 01-OUT-2025
'en_US.utf8'
1
In en_US.utf8: LOCUS       foo                        4 bp    dna     circular UNK 01-OCT-2025

Activity

  1. peterjc commented on Mar 19, 2025

    @peterjc
    Member

    The code usually expects record.annotations["date"] to be a string, but in your example it is a datetime object, and it seems this is line of our code is unintentionally locale aware:

    date = date.strftime("%d-%b-%Y").upper()

    See https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

    A pull request would be welcome! I guess this would most simply be fixed with an integer to three letter English month abbreviation dictionary, but you might have a better idea?

  2. bryan-brancotte commented on Mar 20, 2025

    @bryan-brancotte
    ContributorAuthor

    Yes integer to three letter English is enough. I was thinking of adding a test to highlight that we have to be independent of local, but I haven't yet looked at the test environment and the local available. Going for a PR in the following days.

  3. uxritu commented on Mar 20, 2025

    @uxritu

    Hi, I'm new to open-source contributions. Can I work on this issue?

  4. peterjc commented on Mar 20, 2025

    @peterjc
    Member

    @uxritu That would not be helpful. Bryan has already documented this issue and offered to work on it.

  5. peterjc commented on Mar 20, 2025

    @peterjc
    Member

    Note @uxritu has written the same comment on multiple issues today - which I am initially willing to assuming is merely over enthusiasm.

  6. bryan-brancotte commented on Mar 21, 2025

    @bryan-brancotte
    ContributorAuthor

    Hi, I'm on it indeed, but thanks !

    I have a question, do I propose to add a warning and fix the issue in one or two distinct PR ?

    Edit: I went for one, but ok to split it into two PR

  7. peterjc commented on Mar 21, 2025

    @peterjc
    Member

    One PR should be enough 👍

  8. added a commit that references this issue on Mar 21, 2025
    79759c8
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