You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
CPython
Darwin-17.3.0-x86_64-i386-64bit
1.70
Expected behaviour
Residue.id(value) or res.id = value
changes the id of the residue. This is used for renumbering a PDB.
This used to work fine for many years and now results in an error, with no obvious override mechanism.
Actual behaviour
ValueError: Cannot change id from `(' ', 8, ' ')` to `(' ', 9, ' ')`. The id `(' ', 9, ' ')` is already used for a sibling of this entity.
In addition, using detach_child() on the residue for the 'already in use sibling' does not work and results in this error:
File "/Library/Python/2.7/site-packages/Bio/PDB/Entity.py", line 120, in detach_child
child = self.child_dict[id]
KeyError: (' ', 9, ' ')
Steps to reproduce
Change the id of a residue to another residue number, that is a 'child'
Hey @JoaoRodrigues - Thanks for the push. I'm still cloning, but here is a quick tested snippet you should be able to run on 1A80, which is in Test/PDB. (res.id can also be res.value() to set the tupple, and both error the same way.)
Hi.
I authored the change which causes your issue.
For your example, 1A8O, I guess an error is the expected result, since you are trying to change the id of ASP 152 to 153, but residue 153 is already present as ILE and ids should be unique.
Previous versions of biopython would allow you to change the id of the residue, but leave the chain in an inconsistent state, which is why I changed the code to raise an error.
pdb = parser.get_structure("1A8O", "Tests/PDB/1A8O.pdb")
res = pdb[0]['A'][(' ', 152, ' ')]
res.id=(' ', 1, ' ') # Works fine for me
res.id=(' ', 153, ' ') # Causes an Error
This has broken many, many scripts out in the wild for PDB manipulation. The chain at the end of the manipulation is consistent. Raising an error before a whole work up of manipulation makes the manipulations harder to do, but if detach_child works, that would be great. Perhaps it fails for consistent detaches? I'll try the detach_child function on master. Thanks for the info.
I believe that many scripts out in the wild used the old behaviour and made sure themselves that the chain was consistent at the end of all manipulations. I had I script like that as well.
However, I felt it would be better to let biopython ensure that the chain is consistent at all times. This way, I guess, many scripts in the future will be less error prone. I had many bugs where I changed a residueA.id to newid, but chain[newid] gave a different residue, because I forgot to update the child_dict of the chain.
Maybe we should add a function to biopython that renumbers the whole chain. Could you give more detail, what kind of renumbering you like to achieve?
I am trying to use a BioPython derived script to renumber antibodies in a pdb and I still get the same error. To date is there any way to renumber antibodies with an existing scheme (imgt, chothia, etc)?
I disagree with the warning because the original problem still remains. Changing the id for the residue to clash with another one is not a good solution because it breaks things under the hood.
This is the code for the id setter:
@id.setter
def id(self, value):
"""Change the id of this entity.
This will update the child_dict of this entity's parent
and invalidate all cached full ids involving this entity.
@raises: ValueError
"""
if value == self._id:
return
if self.parent:
if value in self.parent.child_dict:
raise ValueError(
f"Cannot change id from `{self._id}` to `{value}`."
f" The id `{value}` is already used for a sibling of this entity."
)
del self.parent.child_dict[self._id]
self.parent.child_dict[value] = self
self._id = value
self._reset_full_id()
As you see, changing Residue.id will cause a change in Residue.parent.child_dict, which is a dictionary, which requires unique keys. If you allow changing ids without this check, you end up with a mismatch between that child_dict and the actual residue.id. If you're a responsible user, you'll update them later, although that's a ton of extra work and you'll have to know what to do. For more casual users, it's a trap because you have inconsistencies..
My solution here would be to create a new empty chain and add the residues from the old chain, renumbering them as you go. Why would this not work for you?
Specifically:
from Bio.PDB.Chain import Chain
from Bio.PDB import PDBParser
from Bio.PDB import PDBIO
parser = PDBParser()
pdb = parser.get_structure("1A8O", "1A8O.pdb")
# Renumber chain A
chain = pdb[0]["A"]
chain.parent.detach_child(chain.id)
new_chain = Chain("A")
for res in chain:
res = res.copy()
h, num, ins = res.id
res.id = (h, num + 1, ins)
new_chain.add(res)
pdb[0].add(new_chain)
io = PDBIO()
io.set_structure(pdb)
io.save("1A8O_renum.pdb")
It's now been 6 years and I can't tell you the number of times myself, my colleagues, and my collaborators have hit this issue - numbering is something that needs to be easily able to change. Numbering should not have anything to do with chemical connectivity. In protein design, numbering changes happen often and need to be taken care of.
So, with yet another colleague coming to me who spent hours trying to debug something that shouldn't have had to use his time in the first place - can we PLEASE revert this change and bring back the old behavior. There is much general interest in doing that in this thread, and beyond.
Thank you.
Hi @jadolfbr, happy to discuss it, but I did my best to provide an explanation last time you commented, provided a workaround, and there was no follow-up. I understand the frustration, but there are good arguments against reverting the behavior so I'd like to have an actual discussion about this before considering so.
I just spent three hours tracking this down while trying to renumber a pdb. The solution provide by @JoaoRodrigues is convoluted and not intuitive in the slightest bit. At most, this should be a warning not an exception.
My proposed workaround is to 1) create a new chain, 2) add the renumbered residues there, 3) swap this chain with the old chain in the structure. Quite far from convoluted.
I am willing to support the downgrading to a warning, but I just want to be sure that what you are trying to do isn't solvable otherwise. Again - the exception was added for good reason. Could you share a snippet or a script of what you are trying to do exactly?
I've shared snippets. @sacombs can share something probably as well.
Here is one literally from a few days ago from ChatGPT when I was playing around and of course ended in the error:
def renumber_pdb(input_pdb_file, output_pdb_file):
parser = PDB.PDBParser()
io = PDB.PDBIO()
structure = parser.get_structure("structure", input_pdb_file)
# Initialize the residue number
res_num = 1
for model in structure:
for chain in model:
# Change all chains to 'A'
chain.id = 'A'
for residue in chain:
# Ensure only standard residues are renumbered
if PDB.is_aa(residue, standard=True):
# Renumber the residue
residue.id = (' ', res_num, ' ')
res_num += 1
# Save the modified structure
io.set_structure(structure)
io.save(output_pdb_file)
I would like to downgrade this to a warning. If you are already doing renumbering, I think a warning is sufficient. Kinda like if I open the hood to my car to mess with it, going in I know that there are things there I can mess up - but the hood isn't locked down.
Right now it is. The workaround is basically building a whole new object up instead of just modifying what we have. Anyone who is doing this knows what they are doing and after a full numbering manipulation the state is ok.
Here's the problem with what you want to change. The code, as is, will replace the key in Entity.child_dict to point to the new value. Say you have a structure with 5 residues, numbered 1 to 5. Now say you renumber 1 to 2, so you have 2, 2, 3, 4, 5. The child_list of the Structure will still have 5 elements, but the child_dict, which is used everywhere in the code base as a proxy for an iterator for elements of an Entity, will have 4 elements. Unless you do the renumbering operation in one pass and affect all residues and there are absolutely no errors, you're OK. If anything goes wrong, there are parts of the code that will start to break. Disordered entities for instance.
This also opens the door for issues when you have typos in your renumbering scheme. For example, this very silly example is now forbidden (for good reason) but not if we downgrade:
for res in struct.get_residues():
het, num, alt = res.id
res.id = (het, 1, alt)
Do you see why we (or me) are so reluctant to downgrade this? Is doing the new chain that much of a big deal in your case? The ChatGPT snippet doesn't tell me what you are trying to actually do...
Yes, renumbering always happens in one pass. Otherwise, the structure wouldn't be renumbered properly at all and the state would definitely be bad. Thanks for changing this to a warning, it will certainly help for some things in the future and fix a bunch of code in the wild.
Activity
peterjc commentedon Mar 9, 2018
Can you make a self contained example, and/or confirm an older version of Biopython where this worked?
CC: @etal @JoaoRodrigues @lennax
jadolfbr commentedon Mar 9, 2018
Version 1.68 works fine. I'll get a self-contained snippet for you today @peterjc
peterjc commentedon Mar 9, 2018
Using one of the
Tests/PDB/files would probably be easiest for us - it might even end up as a new unit test?JoaoRodrigues commentedon Mar 26, 2018
Hi @jadolfbr , any luck with the snippet?
I would wager this is because of the "propagation" of parent ids, but hard to look at without a specific case.
jadolfbr commentedon Mar 26, 2018
Hey @JoaoRodrigues - Thanks for the push. I'm still cloning, but here is a quick tested snippet you should be able to run on 1A80, which is in Test/PDB. (
res.idcan also beres.value()to set the tupple, and both error the same way.)peterjc commentedon Apr 1, 2018
Now that #1567 has been merged, does this problem still occur in the latest master branch?
jadolfbr commentedon May 17, 2018
@peterjc - Sorry this took me so long. I just cloned master and installed via pip. Same issue persists.
peterjc commentedon May 17, 2018
Thanks - sadly bad news. Back to @JoaoRodrigues to investigate...
JoaoRodrigues commentedon May 19, 2018
Bernhard10 commentedon Jul 26, 2018
Hi.
I authored the change which causes your issue.
For your example, 1A8O, I guess an error is the expected result, since you are trying to change the id of ASP 152 to 153, but residue 153 is already present as ILE and ids should be unique.
Previous versions of biopython would allow you to change the id of the residue, but leave the chain in an inconsistent state, which is why I changed the code to raise an error.
On my machine, the following works:
And in contrast to your report,
detach_child worksfor me:Could you find a minimal example, where detach_child does not work?
jadolfbr commentedon Jul 26, 2018
This has broken many, many scripts out in the wild for PDB manipulation. The chain at the end of the manipulation is consistent. Raising an error before a whole work up of manipulation makes the manipulations harder to do, but if detach_child works, that would be great. Perhaps it fails for consistent detaches? I'll try the detach_child function on master. Thanks for the info.
Bernhard10 commentedon Jul 27, 2018
I believe that many scripts out in the wild used the old behaviour and made sure themselves that the chain was consistent at the end of all manipulations. I had I script like that as well.
However, I felt it would be better to let biopython ensure that the chain is consistent at all times. This way, I guess, many scripts in the future will be less error prone. I had many bugs where I changed a residueA.id to
newid, butchain[newid]gave a different residue, because I forgot to update thechild_dictof the chain.Maybe we should add a function to biopython that renumbers the whole chain. Could you give more detail, what kind of renumbering you like to achieve?
jadolfbr commentedon May 8, 2019
@Bernhard10 - I'd like to modify the residue numbers and chain IDs for a PDB. IE - this particular work is for antibody renumbering, which requires a specific numbering scheme for subsequent work (Chothia, Aho, etc.). https://www.bioc.uzh.ch/plueckthun/antibody/Numbering/NumFrame.html
PyIgClassify uses Biopython under the hood to do the renumbering - but we are now stuck on biopython 1.89. http://dunbrack2.fccc.edu/PyIgClassify/
If there is a work-around, I would love to know. Right now, I'm looking at rolling my own, but I'd rather not if I can avoid it.
peterjc commentedon May 8, 2019
Would downgrading the new exception to a warning be a sensible compromise?
3 remaining items
Bernhard10 commentedon May 22, 2019
@jadolfbr As a workaround, I guess you can also assign to
residue._idinstead ofresidue.idjadolfbr commentedon Nov 1, 2019
A renumber chain function would work well. I will try the workaround.
wjs20 commentedon Jan 8, 2022
I had another workaround for this. A bit hacky but it works;
You derive your old id to new id mapping where the new numbers have a gap (here at position 95)
Then you reverse the list
And then you assign
If you do it this way there are never any clashes
frenko commentedon May 19, 2022
I am trying to use a BioPython derived script to renumber antibodies in a pdb and I still get the same error. To date is there any way to renumber antibodies with an existing scheme (imgt, chothia, etc)?
jadolfbr commentedon Jan 27, 2023
It is now 2023. Any way we can change this to a warning in the meantime for a complete solution?
JoaoRodrigues commentedon Jan 27, 2023
I disagree with the warning because the original problem still remains. Changing the id for the residue to clash with another one is not a good solution because it breaks things under the hood.
This is the code for the
idsetter:As you see, changing
Residue.idwill cause a change inResidue.parent.child_dict, which is a dictionary, which requires unique keys. If you allow changing ids without this check, you end up with a mismatch between that child_dict and the actual residue.id. If you're a responsible user, you'll update them later, although that's a ton of extra work and you'll have to know what to do. For more casual users, it's a trap because you have inconsistencies..My solution here would be to create a new empty chain and add the residues from the old chain, renumbering them as you go. Why would this not work for you?
Specifically:
jadolfbr commentedon Feb 8, 2024
Hey guys,
It's now been 6 years and I can't tell you the number of times myself, my colleagues, and my collaborators have hit this issue - numbering is something that needs to be easily able to change. Numbering should not have anything to do with chemical connectivity. In protein design, numbering changes happen often and need to be taken care of.
So, with yet another colleague coming to me who spent hours trying to debug something that shouldn't have had to use his time in the first place - can we PLEASE revert this change and bring back the old behavior. There is much general interest in doing that in this thread, and beyond.
Thank you.
-Jared
JoaoRodrigues commentedon Feb 8, 2024
Hi @jadolfbr, happy to discuss it, but I did my best to provide an explanation last time you commented, provided a workaround, and there was no follow-up. I understand the frustration, but there are good arguments against reverting the behavior so I'd like to have an actual discussion about this before considering so.
sacombs commentedon Feb 8, 2024
I just spent three hours tracking this down while trying to renumber a pdb. The solution provide by @JoaoRodrigues is convoluted and not intuitive in the slightest bit. At most, this should be a warning not an exception.
JoaoRodrigues commentedon Feb 8, 2024
My proposed workaround is to 1) create a new chain, 2) add the renumbered residues there, 3) swap this chain with the old chain in the structure. Quite far from convoluted.
I am willing to support the downgrading to a warning, but I just want to be sure that what you are trying to do isn't solvable otherwise. Again - the exception was added for good reason. Could you share a snippet or a script of what you are trying to do exactly?
jadolfbr commentedon Feb 8, 2024
I've shared snippets. @sacombs can share something probably as well.
Here is one literally from a few days ago from ChatGPT when I was playing around and of course ended in the error:
I would like to downgrade this to a warning. If you are already doing renumbering, I think a warning is sufficient. Kinda like if I open the hood to my car to mess with it, going in I know that there are things there I can mess up - but the hood isn't locked down.
Right now it is. The workaround is basically building a whole new object up instead of just modifying what we have. Anyone who is doing this knows what they are doing and after a full numbering manipulation the state is ok.
JoaoRodrigues commentedon Feb 8, 2024
Here's the problem with what you want to change. The code, as is, will replace the key in
Entity.child_dictto point to the new value. Say you have a structure with 5 residues, numbered 1 to 5. Now say you renumber 1 to 2, so you have2, 2, 3, 4, 5. Thechild_listof the Structure will still have 5 elements, but thechild_dict, which is used everywhere in the code base as a proxy for an iterator for elements of an Entity, will have 4 elements. Unless you do the renumbering operation in one pass and affect all residues and there are absolutely no errors, you're OK. If anything goes wrong, there are parts of the code that will start to break. Disordered entities for instance.This also opens the door for issues when you have typos in your renumbering scheme. For example, this very silly example is now forbidden (for good reason) but not if we downgrade:
Do you see why we (or me) are so reluctant to downgrade this? Is doing the new chain that much of a big deal in your case? The ChatGPT snippet doesn't tell me what you are trying to actually do...
JoaoRodrigues commentedon Feb 8, 2024
And just so you don't think I'm being annoying just for the sake of being annoying: #4623.
jadolfbr commentedon Mar 27, 2024
Yes, renumbering always happens in one pass. Otherwise, the structure wouldn't be renumbered properly at all and the state would definitely be bad. Thanks for changing this to a warning, it will certainly help for some things in the future and fix a bunch of code in the wild.