Back to index

python-biopython  1.60
Superimposer.py
Go to the documentation of this file.
00001 # Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk)
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 """Superimpose two structures."""
00007 
00008 import numpy
00009 
00010 from Bio.SVDSuperimposer import SVDSuperimposer
00011 from Bio.PDB.PDBExceptions import PDBException
00012 
00013 
00014 class Superimposer(object):
00015     """
00016     Rotate/translate one set of atoms on top of another,
00017     thereby minimizing the RMSD.
00018     """
00019     def __init__(self):
00020         self.rotran=None
00021         self.rms=None
00022 
00023     def set_atoms(self, fixed, moving):
00024         """
00025         Put (translate/rotate) the atoms in fixed on the atoms in 
00026         moving, in such a way that the RMSD is minimized.
00027 
00028         @param fixed: list of (fixed) atoms
00029         @param moving: list of (moving) atoms 
00030         @type fixed,moving: [L{Atom}, L{Atom},...]
00031         """
00032         if not (len(fixed)==len(moving)):
00033             raise PDBException("Fixed and moving atom lists differ in size")
00034         l=len(fixed)
00035         fixed_coord=numpy.zeros((l, 3))
00036         moving_coord=numpy.zeros((l, 3))
00037         for i in range(0, len(fixed)):
00038             fixed_coord[i]=fixed[i].get_coord()
00039             moving_coord[i]=moving[i].get_coord()
00040         sup=SVDSuperimposer()
00041         sup.set(fixed_coord, moving_coord)
00042         sup.run()
00043         self.rms=sup.get_rms()
00044         self.rotran=sup.get_rotran()
00045 
00046     def apply(self, atom_list):
00047         """
00048         Rotate/translate a list of atoms.
00049         """
00050         if self.rotran is None:
00051             raise PDBException("No transformation has been calculated yet")
00052         rot, tran=self.rotran
00053         rot=rot.astype('f')
00054         tran=tran.astype('f')
00055         for atom in atom_list:
00056             atom.transform(rot, tran)
00057 
00058 
00059 if __name__=="__main__":
00060     import sys
00061 
00062     from Bio.PDB import PDBParser, Selection
00063 
00064     p=PDBParser()
00065     s1=p.get_structure("FIXED", sys.argv[1])
00066     fixed=Selection.unfold_entities(s1, "A")
00067 
00068     s2=p.get_structure("MOVING", sys.argv[1])
00069     moving=Selection.unfold_entities(s2, "A")
00070 
00071     rot=numpy.identity(3).astype('f')
00072     tran=numpy.array((1.0, 2.0, 3.0), 'f')
00073 
00074     for atom in moving:
00075         atom.transform(rot, tran)
00076     
00077     sup=Superimposer()
00078 
00079     sup.set_atoms(fixed, moving)
00080 
00081     print sup.rotran
00082     print sup.rms
00083 
00084     sup.apply(moving)