Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'Residue' object has no attribute 'sort' #4075

Closed
raoufkeskes opened this issue Sep 8, 2022 · 8 comments
Closed

AttributeError: 'Residue' object has no attribute 'sort' #4075

raoufkeskes opened this issue Sep 8, 2022 · 8 comments
Assignees

Comments

@raoufkeskes
Copy link

raoufkeskes commented Sep 8, 2022

Code

I am reporting a problem with Biopython version, Python version, and operating
system as follows:

from Bio.PDB import PDBParser
p = PDBParser()
structure = p.get_structure("/home/raouf-ks/Downloads/7nd4_HL.pdb",
                            "/home/raouf-ks/Downloads/7nd4_HL.pdb")
for model in structure:
    for chain in model:
        for residue in chain:
            residue.sort()

Output

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    residue.sort()
AttributeError: 'Residue' object has no attribute 'sort'

the methode sort according to the documentation should sort child atoms, where N, CA, C, O always come first, thereafter alphabetically by name, with any alternative location specifier for disordered atoms (altloc) as a tie-breaker.
but it is not working !

@peterjc
Copy link
Member

peterjc commented Sep 8, 2022

You are looking at the (old) documentation for Biopython 1.75. This method was deprecated (not sure when) and removed in Biopython 1.79, quoting DEPRECATED.rst

The sort and get_atom methods of the Residue class were removed in Release 1.79.

That method therefore isn't listed in the current documentation:

https://biopython.org/docs/latest/api/Bio.PDB.Residue.html
https://biopython.org/docs/1.79/api/Bio.PDB.Residue.html#Bio.PDB.Residue.Residue.sort

@peterjc peterjc closed this as completed Sep 8, 2022
@raoufkeskes
Copy link
Author

raoufkeskes commented Sep 8, 2022

Ok I got you !

so do you have an alternative way to sort atoms after removing it in the new version ?

@peterjc
Copy link
Member

peterjc commented Sep 8, 2022

The deprecation message was: The custom sort() method will be removed in the future in favour of rich comparison methods. Use the built-in sorted() function instead.

See also #1360.

@raoufkeskes
Copy link
Author

I used the built-in sorted() methods but it does not solve the problem

I hope that they will bring back the sort method, it was perfect !

@JoaoRodrigues
Copy link
Member

JoaoRodrigues commented Sep 8, 2022 via email

@raoufkeskes
Copy link
Author

raoufkeskes commented Sep 8, 2022

@JoaoRodrigues

It works BUT my goal is to sort atoms in a PDB, ..., my problem is therefore in this case after sorting with built-in sorted method, I want to save them in the residue, something that was doable with only residue.sort()

PDB

ATOM      1  C   ALA A  27     110.972 180.723 157.553  1.00 81.55           C  
ATOM      2  O   ALA A  27     111.891 180.818 158.365  1.00 81.55           O  
ATOM      3  CB  ALA A  27     109.037 182.248 157.980  1.00 81.55           C
ATOM      4  N   ALA A  27     109.355 180.240 159.340  1.00 81.55           N
ATOM      5  CA  ALA A  27     109.519 180.807 158.006  1.00 81.55           C      
ATOM      6  N   TYR A  28     111.174 180.539 156.250  1.00 83.54           N  
ATOM      7  CA  TYR A  28     112.503 180.472 155.666  1.00 83.54           C  
ATOM      8  C   TYR A  28     112.548 181.334 154.414  1.00 83.54           C  
ATOM      9  O   TYR A  28     111.532 181.534 153.744  1.00 83.54           O  
ATOM     10  CB  TYR A  28     112.891 179.030 155.316  1.00 83.54           C  
ATOM     11  CG  TYR A  28     113.043 178.122 156.516  1.00 83.54           C  
ATOM     12  CD1 TYR A  28     111.936 177.531 157.109  1.00 83.54           C  
ATOM     13  CD2 TYR A  28     114.294 177.851 157.052  1.00 83.54           C  
ATOM     14  CE1 TYR A  28     112.069 176.701 158.205  1.00 83.54           C  
ATOM     15  CE2 TYR A  28     114.437 177.020 158.147  1.00 83.54           C  
ATOM     16  CZ  TYR A  28     113.322 176.449 158.720  1.00 83.54           C  
ATOM     17  OH  TYR A  28     113.461 175.620 159.809  1.00 83.54           O 

python code

in_pdb_file = "/home/raouf-ks/Downloads/7nd4_custom.pdb"
out_pdb_file="/home/raouf-ks/Downloads/7nd4_custom_sorted.pdb"

p = PDBParser()
structure = p.get_structure(in_pdb_file, in_pdb_file)
for model in structure:
    for chain in model:
        for residue in chain:
            print("before : ", [a for a in residue.get_atoms()])
            print("after : ", [a for a in sorted(residue.get_atoms())])
            # HOW TO SAVE THE SORTED ATOMS
            break
        break
    break

# save 
io = PDBIO()
io.set_structure(structure)
io.save(out_pdb_file)

output

before :  [<Atom C>, <Atom O>, <Atom CB>, <Atom N>, <Atom CA>]
after :  [<Atom N>, <Atom CA>, <Atom C>, <Atom O>, <Atom CB>]

@JoaoRodrigues
Copy link
Member

JoaoRodrigues commented Sep 8, 2022 via email

@raoufkeskes
Copy link
Author

@JoaoRodrigues
since I checked previous versions code, I fixed it with a tricky line of code but I hope it will be back

residue.child_list.sort()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants