Package Bio :: Package PDB :: Module Superimposer'
[hide private]
[frames] | no frames]

Source Code for Module Bio.PDB.Superimposer'

 1  # Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk) 
 2  # This code is part of the Biopython distribution and governed by its 
 3  # license.  Please see the LICENSE file that should have been included 
 4  # as part of this package. 
 5   
 6  """Superimpose two structures.""" 
 7   
 8  from __future__ import print_function 
 9   
10  import numpy 
11   
12  from Bio.SVDSuperimposer import SVDSuperimposer 
13  from Bio.PDB.PDBExceptions import PDBException 
14   
15   
16 -class Superimposer(object):
17 """ 18 Rotate/translate one set of atoms on top of another, 19 thereby minimizing the RMSD. 20 """
21 - def __init__(self):
22 self.rotran=None 23 self.rms=None
24
25 - def set_atoms(self, fixed, moving):
26 """ 27 Put (translate/rotate) the atoms in fixed on the atoms in 28 moving, in such a way that the RMSD is minimized. 29 30 @param fixed: list of (fixed) atoms 31 @param moving: list of (moving) atoms 32 @type fixed,moving: [L{Atom}, L{Atom},...] 33 """ 34 if not (len(fixed)==len(moving)): 35 raise PDBException("Fixed and moving atom lists differ in size") 36 l=len(fixed) 37 fixed_coord=numpy.zeros((l, 3)) 38 moving_coord=numpy.zeros((l, 3)) 39 for i in range(0, len(fixed)): 40 fixed_coord[i]=fixed[i].get_coord() 41 moving_coord[i]=moving[i].get_coord() 42 sup=SVDSuperimposer() 43 sup.set(fixed_coord, moving_coord) 44 sup.run() 45 self.rms=sup.get_rms() 46 self.rotran=sup.get_rotran()
47
48 - def apply(self, atom_list):
49 """ 50 Rotate/translate a list of atoms. 51 """ 52 if self.rotran is None: 53 raise PDBException("No transformation has been calculated yet") 54 rot, tran=self.rotran 55 rot=rot.astype('f') 56 tran=tran.astype('f') 57 for atom in atom_list: 58 atom.transform(rot, tran)
59 60 61 if __name__=="__main__": 62 import sys 63 64 from Bio.PDB import PDBParser, Selection 65 66 p=PDBParser() 67 s1=p.get_structure("FIXED", sys.argv[1]) 68 fixed=Selection.unfold_entities(s1, "A") 69 70 s2=p.get_structure("MOVING", sys.argv[1]) 71 moving=Selection.unfold_entities(s2, "A") 72 73 rot=numpy.identity(3).astype('f') 74 tran=numpy.array((1.0, 2.0, 3.0), 'f') 75 76 for atom in moving: 77 atom.transform(rot, tran) 78 79 sup=Superimposer() 80 81 sup.set_atoms(fixed, moving) 82 83 print(sup.rotran) 84 print(sup.rms) 85 86 sup.apply(moving) 87