File: superpose_2.cpp

package info (click to toggle)
mustang 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 932 kB
  • ctags: 415
  • sloc: cpp: 7,870; makefile: 91; sh: 18
file content (261 lines) | stat: -rw-r--r-- 10,252 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*******************************************************************************
 * Copyright (c) 2005, Arun S Konagurthu, The University of Melbourne.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification are permitted provided that the following conditions are met:
 * 
 * (1) Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * (2) Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * (3) Neither the name of the University of Melbourne nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESSINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/
#include <iostream>
using std::cout ;
using std::cerr ;
using std::endl;
using std::flush ;
using std::ios ;
#include <iomanip>
using std::setprecision ;
using std::setw ;
#include <fstream>
using std::ifstream ;
using std::ofstream ;
#include <cmath>
#include "superpose_2.h"
#include "jacobi.h"
#include "macros.h"
#define sup_verbose 0
#define sup_gibberish 0

double CM_A[3] , CM_B[3] ; // Centers of Mass of vector sets A and B
//double **DELTA_Coords_PLUS , **DELTA_Coords_MINUS ;
//double Quat[4][4] ;
/* *******************************************
 * GLOBAL FUNCTION DEFINITION                *
 * ******************************************* */
void SUPERPOSE_2(float **CoordsA , float **CoordsB , int Size , float *RMSD , float ROTATION_MAT[][3] , double CM_A[] , double CM_B[] )
{
	//cout << "Performing Quaternion Superposition..." << std::flush ;
	//double CM_A[3] ,  CM_B[3] ;
	double **DELTA_Coords_PLUS ;
	double **DELTA_Coords_MINUS ;
	double Quat[4][4] ;
	float eigen_values[4] ;
	float eigen_vectors[4][4] ;
	float local_rmsd = -1 ;
	/* LOCAL FUNCTION PROTOTYPES */
	void Calculate_Center_of_Mass( float ** , float ** , int , double [] , double []) ; 
	void Calculate_Coordinate_differentials( float ** , float ** , int , double [] , double [] , double *** , double ***) ; 
	void Calculate_Quaternion_Matrix( double ** , double ** , int , double[][4] ) ;
	void Calculate_RMSD(float [4] , int , float* ) ;
	void Calculate_Best_Rotation_Matrix( float [][4] , float [][3]) ;
	void DEALLOC(double ** , double ** , int ) ;
	
	Calculate_Center_of_Mass(CoordsA , CoordsB , Size , CM_A , CM_B ); // for each of the two vector sets
	Calculate_Coordinate_differentials(CoordsA , CoordsB , Size , CM_A , CM_B , &DELTA_Coords_PLUS , &DELTA_Coords_MINUS) ; 
	Calculate_Quaternion_Matrix( DELTA_Coords_PLUS , DELTA_Coords_MINUS , Size ,  Quat ) ; // contructs a quaterion from the coordinate differences
	JACOBI_ROTATIONS( Quat , eigen_values , eigen_vectors ) ; // performs jacobi rotations on quaternion;  
								  // outputs sorted Eigen values & vectors
	Calculate_RMSD( eigen_values , Size , &local_rmsd  ) ;
	*RMSD = local_rmsd ;
	
	Calculate_Best_Rotation_Matrix( eigen_vectors , ROTATION_MAT ) ;
	DEALLOC(DELTA_Coords_PLUS , DELTA_Coords_MINUS , Size ) ;

}
void Calculate_Center_of_Mass(float **CoordsA , float **CoordsB , int Size , double CM_A[] , double CM_B[] )
{
	
	CM_A[ 0 ] = CM_A[ 1 ] = CM_A[ 2 ] = 0.0 ;
	CM_B[ 0 ] = CM_B[ 1 ] = CM_B[ 2 ] = 0.0 ;
	double temp = 0 ;
	for( int i = 0 ; i < 3 ; i++ )
	{
		temp = 0 ;
		for( int j = 0 ; j < Size ; j++ )
			temp+=CoordsA[j][i] ;
		CM_A[ i ] = temp / Size ;
	}
	for( int i = 0 ; i < 3 ; i++ )
	{
		temp = 0 ;
		for( int j = 0 ; j < Size ; j++ )
			temp+=CoordsB[j][i] ;
		CM_B[ i ] = temp / Size ;
	}
	if( sup_verbose )
	{
		cout << setprecision(3) << ios::fixed;
		cout << "\n\tCenter of Mass of vectors set A : " ;
		for( int i = 0 ; i < 3 ; i++ )
			cout << std::setw(9) << CM_A[ i ] ;
		cout << endl ;
		cout << "\tCenter of Mass of vectors set B : " ;
		for( int i = 0 ; i < 3 ; i++ )
			cout << std::setw(9) << CM_B[ i ] ;
		cout << endl ;
		cout << "\tTarget vector       : " ;
		for( int i = 0 ; i < 3 ; i++ )
			cout << std::setw(9) << CM_B[i] - CM_A[ i ] ;
		cout << endl ;
	}
}

void Calculate_Coordinate_differentials(float **CoordsA , float **CoordsB , int Size , double CM_A[] , double CM_B[] , double ***DELTA_Coords_PLUS , double ***DELTA_Coords_MINUS ) 
{
	//Allocating space for DELTA_X_PLUS and DELTA_X_MINUS.
	(*DELTA_Coords_PLUS) = new double* [Size] ;
	for( int i = 0 ; i < Size ; i ++ )
		(*DELTA_Coords_PLUS)[i] = new double [3] ;
	(*DELTA_Coords_MINUS) = new double* [Size] ;
	for( int i = 0 ; i < Size ; i ++ )
		(*DELTA_Coords_MINUS)[i] = new double [3] ;
	
	// Calculating coordinate differences, DELTA_X_PLUS and DELTA_X_MINUS. Assume A to be Movable and B fixed
	for( int i = 0 ; i < Size  ; i++ )
		for( int j = 0 ; j < 3 ; j ++ )
		{
			(*DELTA_Coords_PLUS)[i][j] =  ( CoordsA[i][j] - CM_A[j] ) + ( CoordsB[i][j] - CM_B[j] ) ;
			(*DELTA_Coords_MINUS)[i][j] = ( CoordsA[i][j] - CM_A[j] ) - ( CoordsB[i][j] - CM_B[j] ) ;
		}
}

void Calculate_Quaternion_Matrix( double **DELTA_Coords_PLUS , double **DELTA_Coords_MINUS , int Size, double Quat[][4] )
{
	//Calculate Square-Symmetric Quaternion Matrix
	//initialize
	for( int i = 0 ; i < 4 ; i++ )
		for( int j = 0 ; j < 4 ; j++ )
			Quat[i][j] = 0.0 ;
	
	// Filling upper triangle of the quaternion matrix
	for( int i = 0 ; i < Size  ; i++ )
	{
		//Diags =Sum of squared cyclic coordinate differences
		Quat[0][0] += pow( DELTA_Coords_MINUS[i][0] , 2 ) +
			      pow( DELTA_Coords_MINUS[i][1] , 2 ) +
			      pow( DELTA_Coords_MINUS[i][2] , 2 ) ;
		
		Quat[1][1] += pow( DELTA_Coords_PLUS[i][1]  , 2 ) +
			      pow( DELTA_Coords_PLUS[i][2]  , 2 ) +
			      pow( DELTA_Coords_MINUS[i][0] , 2 ) ;
		
		Quat[2][2] += pow( DELTA_Coords_PLUS[i][0]  , 2 ) +
			      pow( DELTA_Coords_PLUS[i][2]  , 2 ) +
			      pow( DELTA_Coords_MINUS[i][1] , 2 ) ;
		
		Quat[3][3] += pow( DELTA_Coords_PLUS[i][0]  , 2 ) +
			      pow( DELTA_Coords_PLUS[i][1]  , 2 ) +
			      pow( DELTA_Coords_MINUS[i][2] , 2 ) ;
		// Cross differences
		Quat[0][1] += DELTA_Coords_PLUS[i][1] * DELTA_Coords_MINUS[i][2] -
			      DELTA_Coords_MINUS[i][1] * DELTA_Coords_PLUS[i][2] ;

		Quat[0][2] += DELTA_Coords_MINUS[i][0] * DELTA_Coords_PLUS[i][2] -
			      DELTA_Coords_PLUS[i][0] * DELTA_Coords_MINUS[i][2] ;

		Quat[0][3] += DELTA_Coords_PLUS[i][0] * DELTA_Coords_MINUS[i][1] -
			      DELTA_Coords_MINUS[i][0] * DELTA_Coords_PLUS[i][1] ;

		Quat[1][2] += DELTA_Coords_MINUS[i][0] * DELTA_Coords_MINUS[i][1] -
			      DELTA_Coords_PLUS[i][0] * DELTA_Coords_PLUS[i][1] ;

		Quat[1][3] += DELTA_Coords_MINUS[i][0] * DELTA_Coords_MINUS[i][2] -
			      DELTA_Coords_PLUS[i][0] * DELTA_Coords_PLUS[i][2] ;

		Quat[2][3] += DELTA_Coords_MINUS[i][1] * DELTA_Coords_MINUS[i][2] -
			      DELTA_Coords_PLUS[i][1] * DELTA_Coords_PLUS[i][2] ;
	}
	// Fill the rest by transposing it onto itself
	Quat[1][0] = Quat[0][1] ; Quat[2][0] = Quat[0][2] ; Quat[2][1] = Quat[1][2] ;  
	Quat[3][0] = Quat[0][3] ; Quat[3][1] = Quat[1][3] ; Quat[3][2] = Quat[2][3] ;
	if( sup_verbose )
	{
		cout << "\tQuaternion_Matrix:\n" ;
		for( int i = 0 ; i < 4 ; i++ )
		{
			cout << "\t" ;
			for( int j = 0 ; j < 4 ; j++ )
				cout << setw(12) << Quat[i][j] ;
			cout << endl;
		}
		
	}
}
void Calculate_RMSD( float eigen_values[] , int Size , float *RMSD ) 
{
	*RMSD = sqrt(fabs( eigen_values[3] / Size )) ;
	
}
void Calculate_Best_Rotation_Matrix( float eigen_vectors[][4] , float ROTATION_MAT[][3]) 
{
	ROTATION_MAT[0][0] = pow( eigen_vectors[0][3] , 2 ) + pow( eigen_vectors[1][3] , 2 ) -  
			     pow( eigen_vectors[2][3] , 2 ) - pow( eigen_vectors[3][3] , 2 ) ;
	ROTATION_MAT[1][0] = 2 *( 
			          eigen_vectors[1][3] * eigen_vectors[2][3] +
			          eigen_vectors[0][3] * eigen_vectors[3][3] 
			        ) ;
	ROTATION_MAT[2][0] = 2 *( 
			          eigen_vectors[1][3] * eigen_vectors[3][3] -
			          eigen_vectors[0][3] * eigen_vectors[2][3] 
			        ) ;
	ROTATION_MAT[0][1] = 2 *( 
			          eigen_vectors[1][3] * eigen_vectors[2][3] -
			          eigen_vectors[0][3] * eigen_vectors[3][3] 
			        ) ;
	ROTATION_MAT[1][1] = pow( eigen_vectors[0][3] , 2 ) + pow( eigen_vectors[2][3] , 2 ) -  
			     pow( eigen_vectors[1][3] , 2 ) - pow( eigen_vectors[3][3] , 2 ) ;
	ROTATION_MAT[2][1] = 2 *( 
			          eigen_vectors[2][3] * eigen_vectors[3][3] +
			          eigen_vectors[0][3] * eigen_vectors[1][3] 
			        ) ;
	ROTATION_MAT[0][2] = 2 *( 
			          eigen_vectors[1][3] * eigen_vectors[3][3] +
			          eigen_vectors[0][3] * eigen_vectors[2][3] 
			        ) ;
	ROTATION_MAT[1][2] = 2 *( 
			          eigen_vectors[2][3] * eigen_vectors[3][3] -
			          eigen_vectors[0][3] * eigen_vectors[1][3] 
			        ) ;
	ROTATION_MAT[2][2] = pow( eigen_vectors[0][3] , 2 ) + pow( eigen_vectors[3][3] , 2 ) -  
			     pow( eigen_vectors[1][3] , 2 ) - pow( eigen_vectors[2][3] , 2 ) ;
	if(gibberish)
	{
		cout << "\n\t Rotation Matrix corresponding to the best superposition :\n" ;
		for( int i = 0 ; i < 3 ; i ++ )
		{
			cout << "\t" ;
			for( int j = 0 ; j < 3 ; j ++ )
				cout << setw(12) <<ROTATION_MAT[i][j] ;
			cout << endl ;
		}
	}
}

void DEALLOC( double **a , double **b , int Size )
{
	for ( int i = 0 ; i < Size ; i++ )
		delete[]  a[i]  ;
	delete[] a ;
	for ( int i = 0 ; i < Size ; i++ )
		delete[]  b[i]  ;
	delete[] b ;
}