Skip to content

Edge case B-factor writing error #5046

Closed

Description

@OWissett

When writing a PDB file with B-factor values above 999, you will end up getting an invalid length B-factor value in the PDB. This is not a common occurence, however, with the rise of people using B-factor fields for other values - typically from computational tools, it is a real problem.

The issue arrises from the format string in Bio.PDB.PDBIO:

_ATOM_FORMAT_STRING = (
    "%s%5i %-4s%c%3s %c%4i%c   %8.3f%8.3f%8.3f%s%6.2f      %4s%2s%2s\n"
)

Particularly, the %6.2f format code for the B-factor. This ensures that the minimum string length is 6 characters, not the maximum. For cases where the B-factor is above 999, you will have additional characters.

I have verified this in python3.10.12, and according to PEP3101:

[[fill]align][sign][#][0][minimumwidth][.precision][type]

Therefore, I propose that when a B-factor value is over 999, we set the precision to 1, and when over 9999 we emit a warning. For normal cases this will have no effect as it is very rare to have B-factor values in such a range.

Activity

  1. JoaoRodrigues commented on Aug 28, 2025

    @JoaoRodrigues
    Member

    Hi @OWissett,

    The 6.2f format actually comes from the PDB format itself (see here). Instead of changing the PDBWriter to accommodate a 'non-standard' format, I'd recommend you write the structures as mmCIF if you have such b-factors, since there you don't have any limitation on sizes. Would that work for your use case?

  2. OWissett commented on Aug 28, 2025

    @OWissett
    ContributorAuthor

    That spec gives it as a fixed width, whereas in python 6.2f only sets a min. The current implementation will create invalid PDBs. I don't see a reason to not implement this change.

    I am doing this on behalf of someone else, who has insisted that they must use PDBs - I have already suggested they us mmCIFs - for exactly this reason.

    As an alternative, I think it would be at least worth making biopython emit a warning if this happens, even if we don't clip the precision. I think the user should be made aware that they have violated the PDB standard, rather than it doing it quietly and you only know later when you use an ancient piece of software that only works with PDBs.

  3. JoaoRodrigues commented on Aug 29, 2025

    @JoaoRodrigues
    Member

    Oh I see what you mean, you mean when we have a number like 999999, you get two added decimal places and that breaks the format. Got it, I didn't understand it from your first message. Let me comment on your review then!

  4. OWissett commented on Aug 29, 2025

    @OWissett
    ContributorAuthor

    Awesome thanks! Sorry if I wasn't clear enough in the original issue.

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