Back to index

python-biopython  1.60
test_SVDSuperimposer.py
Go to the documentation of this file.
00001 # This code is part of the Biopython distribution and governed by its
00002 # license.  Please see the LICENSE file that should have been included
00003 # as part of this package.
00004 
00005 #TODO - Don't use "from XXX import *"
00006 try:
00007     from numpy import *
00008     from numpy import dot #missing in PyPy's micronumpy
00009 except ImportError:
00010     from Bio import MissingPythonDependencyError
00011     raise MissingPythonDependencyError(
00012         "Install NumPy if you want to use Bio.SVDSuperimposer.")
00013 
00014 from Bio.SVDSuperimposer import *
00015 
00016 # start with two coordinate sets (Nx3 arrays - Float0)
00017 
00018 x=array([[51.65, -1.90, 50.07],
00019          [50.40, -1.23, 50.65],
00020          [50.68, -0.04, 51.54],
00021          [50.22, -0.02, 52.85]], 'f')
00022 
00023 y=array([[51.30, -2.99, 46.54],
00024          [51.09, -1.88, 47.58],
00025          [52.36, -1.20, 48.03],
00026          [52.71, -1.18, 49.38]], 'f')
00027 
00028 sup=SVDSuperimposer()
00029 
00030 # set the coords
00031 # y will be rotated and translated on x
00032 sup.set(x, y)
00033 
00034 # do the lsq fit
00035 sup.run()
00036 
00037 # get the rmsd
00038 rms=sup.get_rms()
00039 
00040 # get rotation (right multiplying!) and the translation
00041 rot, tran=sup.get_rotran()
00042 
00043 # rotate y on x manually
00044 y_on_x1=dot(y, rot)+tran
00045 
00046 # same thing
00047 y_on_x2=sup.get_transformed()
00048 
00049 def simple_matrix_print(matrix):
00050     """Simple string to display a floating point matrix
00051 
00052     This should give the same output on multiple systems.  This is
00053     needed because a simple "print matrix" uses scientific notation
00054     which varies between platforms.
00055 
00056     Only 4 decimal places are used to avoid false test failures due
00057     to slight differences in the calculation (e.g. due to different
00058     versions of the underlying libraries or the compilation options
00059     they used).
00060     """
00061 
00062     #This uses a fancy double nested list expression.
00063     #If and when Biopython requires Python 2.4 or later,
00064     #it would be slightly nicer to use generator expressions.
00065     return "[" \
00066     + "\n ".join(["[" \
00067                  + " ".join(["% 1.4f" % val for val in row]) \
00068                  + "]" for row in matrix]) \
00069     + "]"
00070 
00071 # output results
00072 print simple_matrix_print(y_on_x1)
00073 print
00074 print simple_matrix_print(y_on_x2)
00075 print
00076 print "%.2f" % rms