pythonbiopython

How do I translate an atom using Biopython? The transform method is throwing error


I am trying to transform the coordinates of atom but when I am using random vector; I am getting this error -

rotation = rotmat(pi, Vector(np.random.rand(3,1)))
  File "/home/sanu/.local/lib/python3.8/site-packages/Bio/PDB/vectors.py", line 204, in rotmat
    rot = numpy.dot(refmat(q, -p), refmat(p, -p))
  File "/home/sanu/.local/lib/python3.8/site-packages/Bio/PDB/vectors.py", line 167, in refmat
    q = q.normalized()
AttributeError: 'float' object has no attribute 'normalized' 



for model in simulation_pdb:
    for chain in model:
        if chain.get_id() == "Y":
            for residue in chain:
                for atom in residue:
                    #translation = np.random.rand(3,1)
                    #rotation = rotmat(pi, Vector(np.random.rand(3,1)))
                    translation = array((np.random.rand(3,1)), 'f')
                    rotation = rotmat(np.pi, array(np.random.rand(3,1)))
                    atom.transform(rotation, translation)
                    print(atom, atom.get_vector())

In case of the above code (as it is, no random vector), I am getting this error -

    rotation = rotmat(np.pi, array(np.random.rand(3,1)))
  File "/home/sanu/.local/lib/python3.8/site-packages/Bio/PDB/vectors.py", line 204, in rotmat
    rot = numpy.dot(refmat(q, -p), refmat(p, -p))
  File "/home/sanu/.local/lib/python3.8/site-packages/Bio/PDB/vectors.py", line 166, in refmat
    p = p.normalized()
AttributeError: 'numpy.ndarray' object has no attribute 'normalized'

What am I doing wrong here? Seems like the transform function is expecting some other datatype; which I don't know.

Edit1 - from Biopython's documentation - biopython/Bio/PDB/Atom.py Line 422

 def transform(self, rot, tran):
        """Apply rotation and translation to the atomic coordinates.
        :param rot: A right multiplying rotation matrix
        :type rot: 3x3 Numeric array
        :param tran: the translation vector
        :type tran: size 3 Numeric array
        Examples
        --------
        This is an incomplete but illustrative example::
            from numpy import pi, array
            from Bio.PDB.vectors import Vector, rotmat
            rotation = rotmat(pi, Vector(1, 0, 0))
            translation = array((0, 0, 1), 'f')
            atom.transform(rotation, translation)
        """
        self.coord = np.dot(self.coord, rot) + tran

Solution

  • OK kind of late but, my attempt; given input as (pymol generated peptide no real folding at all) avgwpt_A.pdb :

    ATOM      1  N   ALA A   1      67.846  51.727  15.161  1.00  0.00           N  
    ATOM      2  CA  ALA A   1      68.522  53.021  15.161  1.00  0.00           C  
    ATOM      3  C   ALA A   1      70.022  52.847  15.161  1.00  0.00           C  
    ATOM      4  O   ALA A   1      70.553  51.730  15.150  1.00  0.00           O  
    ATOM      5  CB  ALA A   1      68.014  53.813  16.378  1.00  0.00           C  
    ATOM      6  N   VAL A   2      70.773  53.896  15.173  1.00  0.00           N  
    ATOM      7  CA  VAL A   2      72.223  53.728  15.173  1.00  0.00           C  
    ATOM      8  C   VAL A   2      72.922  55.066  15.188  1.00  0.00           C  
    ATOM      9  O   VAL A   2      72.296  56.134  15.198  1.00  0.00           O  
    ATOM     10  CB  VAL A   2      72.665  52.877  13.916  1.00  0.00           C  
    ATOM     11  CG1 VAL A   2      74.193  52.762  13.664  1.00  0.00           C  
    ATOM     12  CG2 VAL A   2      72.143  51.420  13.932  1.00  0.00           C  
    ATOM     13  N   GLY A   3      74.212  55.098  15.190  1.00  0.00           N  
    ATOM     14  CA  GLY A   3      74.888  56.392  15.204  1.00  0.00           C  
    ATOM     15  C   GLY A   3      76.388  56.218  15.203  1.00  0.00           C  
    ATOM     16  O   GLY A   3      76.917  55.104  15.192  1.00  0.00           O  
    ATOM     17  N   LEU A   4      77.138  57.267  15.215  1.00  0.00           N  
    ATOM     18  CA  LEU A   4      78.589  57.099  15.215  1.00  0.00           C  
    ATOM     19  C   LEU A   4      79.288  58.437  15.230  1.00  0.00           C  
    ATOM     20  O   LEU A   4      78.660  59.507  15.241  1.00  0.00           O  
    ATOM     21  CB  LEU A   4      79.040  56.251  13.992  1.00  0.00           C  
    ATOM     22  CG  LEU A   4      78.445  54.825  13.832  1.00  0.00           C  
    ATOM     23  CD1 LEU A   4      79.094  54.113  12.637  1.00  0.00           C  
    ATOM     24  CD2 LEU A   4      78.610  53.968  15.098  1.00  0.00           C  
    ATOM     25  N   TRP A   5      80.577  58.469  15.232  1.00  0.00           N  
    ATOM     26  CA  TRP A   5      81.254  59.763  15.246  1.00  0.00           C  
    ATOM     27  C   TRP A   5      82.754  59.589  15.245  1.00  0.00           C  
    ATOM     28  O   TRP A   5      83.281  58.478  15.233  1.00  0.00           O  
    ATOM     29  CB  TRP A   5      80.817  60.551  16.512  1.00  0.00           C  
    ATOM     30  CG  TRP A   5      79.341  60.958  16.536  1.00  0.00           C  
    ATOM     31  CD1 TRP A   5      78.304  60.258  17.187  1.00  0.00           C  
    ATOM     32  CD2 TRP A   5      78.731  62.003  15.872  1.00  0.00           C  
    ATOM     33  CE2 TRP A   5      77.340  61.929  16.130  1.00  0.00           C  
    ATOM     34  CE3 TRP A   5      79.257  63.017  15.031  1.00  0.00           C  
    ATOM     35  NE1 TRP A   5      77.049  60.848  16.950  1.00  0.00           N  
    ATOM     36  CZ2 TRP A   5      76.464  62.876  15.556  1.00  0.00           C  
    ATOM     37  CZ3 TRP A   5      78.372  63.946  14.482  1.00  0.00           C  
    ATOM     38  CH2 TRP A   5      76.996  63.879  14.742  1.00  0.00           C  
    ATOM     39  N   PRO A   6      83.505  60.638  15.257  1.00  0.00           N  
    ATOM     40  CA  PRO A   6      84.959  60.560  15.257  1.00  0.00           C  
    ATOM     41  C   PRO A   6      85.498  60.036  16.599  1.00  0.00           C  
    ATOM     42  O   PRO A   6      84.724  59.735  17.507  1.00  0.00           O  
    ATOM     43  CB  PRO A   6      85.354  62.018  14.978  1.00  0.00           C  
    ATOM     44  CG  PRO A   6      84.201  62.829  15.588  1.00  0.00           C  
    ATOM     45  CD  PRO A   6      82.967  61.980  15.271  1.00  0.00           C  
    ATOM     46  N   THR A   7      86.770  59.913  16.774  1.00  0.00           N  
    ATOM     47  CA  THR A   7      87.263  59.414  18.054  1.00  0.00           C  
    ATOM     48  C   THR A   7      88.771  59.342  18.062  1.00  0.00           C  
    ATOM     49  O   THR A   7      89.436  59.668  17.100  1.00  0.00           O  
    ATOM     50  CB  THR A   7      86.639  58.014  18.376  1.00  0.00           C  
    ATOM     51  CG2 THR A   7      87.080  57.358  19.702  1.00  0.00           C  
    ATOM     52  OG1 THR A   7      85.224  58.110  18.479  1.00  0.00           O  
    TER   
    END
    
    

    pic :

    enter image description here

    with this code :

    import Bio
    print('########## Biopython  VERSION ##########################################')
    print('\n\n BIOPYTHON_VERSION : ', Bio.__version__)
    print('###################################################################')
    
    from Bio.PDB import (
                        PDBParser,
                        Atom,
                        PDBIO
                        )
    
    import numpy as np
    
    import random
    
    from Bio.PDB.vectors import Vector, rotmat
    
    
    
    structure_code = 'avgwpt_A'
    
    structure_name = structure_code
    
    parser_pdb= PDBParser(QUIET=False)    ### shows warnings
    
    structure_whole = parser_pdb.get_structure(structure_name, structure_name+".pdb")  
    
    
    
    
    translation = np.random.rand(3)
    
    print('--------------------')
    print('translation : ', translation)
    print('--------------------')
    
    
    
    p = Vector(random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1))
    
    q = Vector(random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1))
    
                  
    rotation = rotmat(p,q) 
    
    print('rotation : ', rotation)
    print('--------------------')
    
    
    structure_whole_copy = structure_whole.copy()
    
    for model in structure_whole_copy :
        for chain in model:
            if chain.get_id() == "A":
                
                chain.id = 'B'
                
                for residue in chain:
                    
                    for atom in residue:
    
                        atom.transform(rotation, translation)
                        
    
    for model in structure_whole_copy :
        for chain in model:
            
            chain.detach_parent()
            structure_whole[0].add(chain)
            
    
    
    io=PDBIO() 
    
    io.set_structure(structure_whole) 
    
    io.save(structure_name+"_transformed.pdb" , preserve_atom_numbering = False) 
    
    
    
    
    

    output , one example , given the random translation/rotation avgwpt_A_transformed.pdb:

    ATOM      1  N   ALA A   1      67.846  51.727  15.161  1.00  0.00           N  
    ATOM      2  CA  ALA A   1      68.522  53.021  15.161  1.00  0.00           C  
    ATOM      3  C   ALA A   1      70.022  52.847  15.161  1.00  0.00           C  
    ATOM      4  O   ALA A   1      70.553  51.730  15.150  1.00  0.00           O  
    ATOM      5  CB  ALA A   1      68.014  53.813  16.378  1.00  0.00           C  
    ATOM      6  N   VAL A   2      70.773  53.896  15.173  1.00  0.00           N  
    ATOM      7  CA  VAL A   2      72.223  53.728  15.173  1.00  0.00           C  
    ATOM      8  C   VAL A   2      72.922  55.066  15.188  1.00  0.00           C  
    ATOM      9  O   VAL A   2      72.296  56.134  15.198  1.00  0.00           O  
    ATOM     10  CB  VAL A   2      72.665  52.877  13.916  1.00  0.00           C  
    ATOM     11  CG1 VAL A   2      74.193  52.762  13.664  1.00  0.00           C  
    ATOM     12  CG2 VAL A   2      72.143  51.420  13.932  1.00  0.00           C  
    ATOM     13  N   GLY A   3      74.212  55.098  15.190  1.00  0.00           N  
    ATOM     14  CA  GLY A   3      74.888  56.392  15.204  1.00  0.00           C  
    ATOM     15  C   GLY A   3      76.388  56.218  15.203  1.00  0.00           C  
    ATOM     16  O   GLY A   3      76.917  55.104  15.192  1.00  0.00           O  
    ATOM     17  N   LEU A   4      77.138  57.267  15.215  1.00  0.00           N  
    ATOM     18  CA  LEU A   4      78.589  57.099  15.215  1.00  0.00           C  
    ATOM     19  C   LEU A   4      79.288  58.437  15.230  1.00  0.00           C  
    ATOM     20  O   LEU A   4      78.660  59.507  15.241  1.00  0.00           O  
    ATOM     21  CB  LEU A   4      79.040  56.251  13.992  1.00  0.00           C  
    ATOM     22  CG  LEU A   4      78.445  54.825  13.832  1.00  0.00           C  
    ATOM     23  CD1 LEU A   4      79.094  54.113  12.637  1.00  0.00           C  
    ATOM     24  CD2 LEU A   4      78.610  53.968  15.098  1.00  0.00           C  
    ATOM     25  N   TRP A   5      80.577  58.469  15.232  1.00  0.00           N  
    ATOM     26  CA  TRP A   5      81.254  59.763  15.246  1.00  0.00           C  
    ATOM     27  C   TRP A   5      82.754  59.589  15.245  1.00  0.00           C  
    ATOM     28  O   TRP A   5      83.281  58.478  15.233  1.00  0.00           O  
    ATOM     29  CB  TRP A   5      80.817  60.551  16.512  1.00  0.00           C  
    ATOM     30  CG  TRP A   5      79.341  60.958  16.536  1.00  0.00           C  
    ATOM     31  CD1 TRP A   5      78.304  60.258  17.187  1.00  0.00           C  
    ATOM     32  CD2 TRP A   5      78.731  62.003  15.872  1.00  0.00           C  
    ATOM     33  CE2 TRP A   5      77.340  61.929  16.130  1.00  0.00           C  
    ATOM     34  CE3 TRP A   5      79.257  63.017  15.031  1.00  0.00           C  
    ATOM     35  NE1 TRP A   5      77.049  60.848  16.950  1.00  0.00           N  
    ATOM     36  CZ2 TRP A   5      76.464  62.876  15.556  1.00  0.00           C  
    ATOM     37  CZ3 TRP A   5      78.372  63.946  14.482  1.00  0.00           C  
    ATOM     38  CH2 TRP A   5      76.996  63.879  14.742  1.00  0.00           C  
    ATOM     39  N   PRO A   6      83.505  60.638  15.257  1.00  0.00           N  
    ATOM     40  CA  PRO A   6      84.959  60.560  15.257  1.00  0.00           C  
    ATOM     41  C   PRO A   6      85.498  60.036  16.599  1.00  0.00           C  
    ATOM     42  O   PRO A   6      84.724  59.735  17.507  1.00  0.00           O  
    ATOM     43  CB  PRO A   6      85.354  62.018  14.978  1.00  0.00           C  
    ATOM     44  CG  PRO A   6      84.201  62.829  15.588  1.00  0.00           C  
    ATOM     45  CD  PRO A   6      82.967  61.980  15.271  1.00  0.00           C  
    ATOM     46  N   THR A   7      86.770  59.913  16.774  1.00  0.00           N  
    ATOM     47  CA  THR A   7      87.263  59.414  18.054  1.00  0.00           C  
    ATOM     48  C   THR A   7      88.771  59.342  18.062  1.00  0.00           C  
    ATOM     49  O   THR A   7      89.436  59.668  17.100  1.00  0.00           O  
    ATOM     50  CB  THR A   7      86.639  58.014  18.376  1.00  0.00           C  
    ATOM     51  CG2 THR A   7      87.080  57.358  19.702  1.00  0.00           C  
    ATOM     52  OG1 THR A   7      85.224  58.110  18.479  1.00  0.00           O  
    TER      53      THR A   7                                                       
    ATOM     53  N   ALA B   1       4.769  85.111 -17.356  1.00  0.00           N  
    ATOM     54  CA  ALA B   1       4.366  86.307 -18.089  1.00  0.00           C  
    ATOM     55  C   ALA B   1       5.440  87.367 -18.026  1.00  0.00           C  
    ATOM     56  O   ALA B   1       6.507  87.185 -17.426  1.00  0.00           O  
    ATOM     57  CB  ALA B   1       3.025  86.783 -17.506  1.00  0.00           C  
    ATOM     58  N   VAL B   2       5.239  88.497 -18.615  1.00  0.00           N  
    ATOM     59  CA  VAL B   2       6.277  89.521 -18.553  1.00  0.00           C  
    ATOM     60  C   VAL B   2       5.855  90.764 -19.299  1.00  0.00           C  
    ATOM     61  O   VAL B   2       4.760  90.847 -19.870  1.00  0.00           O  
    ATOM     62  CB  VAL B   2       7.631  88.949 -19.135  1.00  0.00           C  
    ATOM     63  CG1 VAL B   2       8.790  89.968 -19.315  1.00  0.00           C  
    ATOM     64  CG2 VAL B   2       8.231  87.791 -18.302  1.00  0.00           C  
    ATOM     65  N   GLY B   3       6.661  91.771 -19.344  1.00  0.00           N  
    ATOM     66  CA  GLY B   3       6.253  92.973 -20.065  1.00  0.00           C  
    ATOM     67  C   GLY B   3       7.327  94.032 -20.003  1.00  0.00           C  
    ATOM     68  O   GLY B   3       8.390  93.850 -19.405  1.00  0.00           O  
    ATOM     69  N   LEU B   4       7.125  95.161 -20.592  1.00  0.00           N  
    ATOM     70  CA  LEU B   4       8.164  96.186 -20.530  1.00  0.00           C  
    ATOM     71  C   LEU B   4       7.742  97.429 -21.276  1.00  0.00           C  
    ATOM     72  O   LEU B   4       6.644  97.512 -21.847  1.00  0.00           O  
    ATOM     73  CB  LEU B   4       9.508  95.635 -21.086  1.00  0.00           C  
    ATOM     74  CG  LEU B   4      10.114  94.373 -20.414  1.00  0.00           C  
    ATOM     75  CD1 LEU B   4      11.485  94.055 -21.027  1.00  0.00           C  
    ATOM     76  CD2 LEU B   4      10.248  94.519 -18.890  1.00  0.00           C  
    ATOM     77  N   TRP B   5       8.547  98.436 -21.321  1.00  0.00           N  
    ATOM     78  CA  TRP B   5       8.139  99.638 -22.043  1.00  0.00           C  
    ATOM     79  C   TRP B   5       9.214 100.697 -21.980  1.00  0.00           C  
    ATOM     80  O   TRP B   5      10.274 100.515 -21.384  1.00  0.00           O  
    ATOM     81  CB  TRP B   5       6.825 100.184 -21.418  1.00  0.00           C  
    ATOM     82  CG  TRP B   5       5.606  99.274 -21.592  1.00  0.00           C  
    ATOM     83  CD1 TRP B   5       5.124  98.353 -20.639  1.00  0.00           C  
    ATOM     84  CD2 TRP B   5       4.815  99.108 -22.710  1.00  0.00           C  
    ATOM     85  CE2 TRP B   5       3.864  98.098 -22.424  1.00  0.00           C  
    ATOM     86  CE3 TRP B   5       4.846  99.731 -23.984  1.00  0.00           C  
    ATOM     87  NE1 TRP B   5       4.036  97.612 -21.136  1.00  0.00           N  
    ATOM     88  CZ2 TRP B   5       2.929  97.709 -23.408  1.00  0.00           C  
    ATOM     89  CZ3 TRP B   5       3.906  99.336 -24.937  1.00  0.00           C  
    ATOM     90  CH2 TRP B   5       2.960  98.341 -24.653  1.00  0.00           C  
    ATOM     91  N   PRO B   6       9.013 101.827 -22.569  1.00  0.00           N  
    ATOM     92  CA  PRO B   6       9.995 102.902 -22.558  1.00  0.00           C  
    ATOM     93  C   PRO B   6      10.123 103.538 -21.163  1.00  0.00           C  
    ATOM     94  O   PRO B   6       9.445 103.122 -20.224  1.00  0.00           O  
    ATOM     95  CB  PRO B   6       9.422 103.865 -23.608  1.00  0.00           C  
    ATOM     96  CG  PRO B   6       7.906 103.632 -23.526  1.00  0.00           C  
    ATOM     97  CD  PRO B   6       7.795 102.123 -23.291  1.00  0.00           C  
    ATOM     98  N   THR B   7      10.946 104.514 -20.977  1.00  0.00           N  
    ATOM     99  CA  THR B   7      11.053 105.104 -19.647  1.00  0.00           C  
    ATOM    100  C   THR B   7      12.063 106.226 -19.633  1.00  0.00           C  
    ATOM    101  O   THR B   7      12.678 106.551 -20.629  1.00  0.00           O  
    ATOM    102  CB  THR B   7      11.424 104.011 -18.588  1.00  0.00           C  
    ATOM    103  CG2 THR B   7      11.581 104.497 -17.131  1.00  0.00           C  
    ATOM    104  OG1 THR B   7      10.412 103.014 -18.524  1.00  0.00           O  
    TER     105      THR B   7                                                       
    END   
    
    

    pic :

    enter image description here

    as per biopython//Bio/PDB/vectors.py line 180

    code :

    def rotmat(p, q):
        """Return a (left multiplying) matrix that rotates p onto q.
    
        :param p: moving vector
        :type p: L{Vector}
    
        :param q: fixed vector
        :type q: L{Vector}
    
        :return: rotation matrix that rotates p onto q
        :rtype: 3x3 NumPy array
    
        Examples
        --------
        >>> from Bio.PDB.vectors import rotmat
        >>> p, q = Vector(1, 2, 3), Vector(2, 3, 5)
        >>> r = rotmat(p, q)
        >>> print(q)
        <Vector 2.00, 3.00, 5.00>
        >>> print(p)
        <Vector 1.00, 2.00, 3.00>
        >>> p.left_multiply(r)
        <Vector 1.21, 1.82, 3.03>
    
        """
        rot = np.dot(refmat(q, -p), refmat(p, -p))
        return rot
    

    rotmat accepts args p and q as Vector (Bio.PDB.Vector) where Vector args does raise ValueError("Vector: x is not a list/tuple/array of 3 numbers") if not a list/tuple/array of 3 numbers ;

    see biopython/Bio/PDB/vectors.py Line 253