isRNAcirc / src / header / FIT_RMSD.h
#ifndef _FIT_RMSD_ARRAYS_H
#define _FIT_RMSD_ARRAYS_H
#define RMSD_phais (double) 0.00001
#define RMSD_max (int) 5000
double RMSD_rmsdx(int lenx, double x1[][3], double x2[][3], double x1cen[], double x2cen[], double M[][3]) {
double yplus[3],rms=0;
for(int k=0;k<lenx;k++) {
for(int i=0;i<3;i++) {
yplus[i]=0;
for(int j=0;j<3;j++) yplus[i]+=M[i][j]*(x2[k][j]-x2cen[j]);
rms+=((x1[k][i]-x1cen[i])-yplus[i])*((x1[k][i]-x1cen[i])-yplus[i]);
}
}
return sqrt(rms/(double) lenx);
}
double FIT_RMSD_MM(int lenx, double x1[][3], double x2[][3], double x1cen[], double x2cen[], double M[][3]) {
double rms = 0.0, phai[3] = {0.0}, seta[3] = {0.0}, delta[3] = {0.0}, setap = 0.0;
double deltap = 0.0, mx[3] = {0.0}, my[3] = {0.0}, mz[3] = {0.0};
double C[3][3] = {0.0}, C2[3][3] = {0.0};
for(int i=0;i<3;i++) { phai[i]=1.0; seta[i]=1.0; delta[i]=1.0; }
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
C[i][j]=0;
for(int k=0;k<lenx;k++) C[i][j]+=(x1[k][j]-x1cen[j])*(x2[k][i]-x2cen[i]);
}
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
C2[i][j]=0;
for(int k=0;k<3;k++) C2[i][j]+=M[i][k]*C[k][j];
}
rms=(double) RMSD_rmsdx (lenx, x1, x2, x1cen, x2cen, M);
int p = 0, q = 0, r = 0;
while(fabs(phai[0])>RMSD_phais || fabs(phai[1])>RMSD_phais || fabs(phai[2])>RMSD_phais) {
if(p==0) { q=1; r=2; }
if(p==1) { q=2; r=0; }
if(p==2) { q=0; r=1; }
setap=C2[r][q]-C2[q][r];
deltap=C2[q][q]+C2[r][r];
for(int k=0;k<3;k++) {
mx[k]=M[p][k];
my[k]=(deltap*M[q][k]+setap*M[r][k])/sqrt(deltap*deltap+setap*setap);
mz[k]=(-setap*M[q][k]+deltap*M[r][k])/sqrt(deltap*deltap+setap*setap);
}
for(int k=0;k<3;k++) { M[p][k]=mx[k]; M[q][k]=my[k]; M[r][k]=mz[k]; }
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
C2[i][j]=0;
for(int k=0;k<3;k++) C2[i][j]+=M[i][k]*C[k][j];
}
rms=(double) RMSD_rmsdx(lenx, x1, x2, x1cen, x2cen, M);
seta[0]=C2[2][1]-C2[1][2];
delta[0]=C2[1][1]+C2[2][2];
seta[1]=C2[0][2]-C2[2][0];
delta[1]=C2[2][2]+C2[0][0];
seta[2]=C2[1][0]-C2[0][1];
delta[2]=C2[0][0]+C2[1][1];
for(int k=0;k<3;k++) phai[k]=atan(seta[k]/delta[k]);
if(p==2) p=0;
else p++;
}
return rms;
}
void rotate_xx1_to_xx3 (double x1cen[], double x2cen[], double M[][3], int lenxx, double xx1[][3], double xx3[][3]) {
double yyplus[1000][3];
for(int k=0;k<lenxx;k++){
for(int i=0;i<3;i++){
yyplus[k][i]=0;
for(int j=0;j<3;j++)
yyplus[k][i]+=M[i][j]*(xx1[k][j]-x1cen[j]);
}
xx3[k][0]=yyplus[k][0]+x2cen[0]; xx3[k][1]=yyplus[k][1]+x2cen[1]; xx3[k][2]=yyplus[k][2]+x2cen[2];
}
}
void FIT_RMSD_arrays_init(int lenx, double x1[][3], double x2[][3], double x1cen[], double x2cen[], double M[][3]) {
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(i==j) M[i][j]=1.0;
else M[i][j]=0.0;
for(int i=0;i<3;i++){ x1cen[i]=0.0; x2cen[i]=0.0; }
for(int i=0;i<lenx; i++) {
x1cen[0]+=x1[i][0]; x1cen[1]+=x1[i][1]; x1cen[2]+=x1[i][2];
x2cen[0]+=x2[i][0]; x2cen[1]+=x2[i][1]; x2cen[2]+=x2[i][2];
}
for(int i=0;i<3;i++) { x1cen[i]=x1cen[i]/(double)lenx; x2cen[i]=x2cen[i]/(double)lenx; }
}
void FIT_RMSD_vectors_init(vector<double> xyz, vector<double> XYZ, int lenx, \
double x1[][3], double x2[][3], \
double x1cen[], double x2cen[], double M[][3]) {
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(i==j) M[i][j]=1.0;
else M[i][j]=0.0;
for(int i=0;i<3;i++){ x1cen[i]=0.0; x2cen[i]=0.0; }
for(int i=0;i<lenx; i++) {
x1[i][0]=xyz[3*i]; x1[i][1]=xyz[3*i+1]; x1[i][2]=xyz[3*i+2];
x2[i][0]=XYZ[3*i]; x2[i][1]=XYZ[3*i+1]; x2[i][2]=XYZ[3*i+2];
x1cen[0]+=xyz[3*i]; x1cen[1]+=xyz[3*i+1]; x1cen[2]+=xyz[3*i+2];
x2cen[0]+=XYZ[3*i]; x2cen[1]+=XYZ[3*i+1]; x2cen[2]+=XYZ[3*i+2];
}
for(int i=0;i<3;i++) { x1cen[i]=x1cen[i]/(double)lenx; x2cen[i]=x2cen[i]/(double)lenx; }
}
double RMSD_vectors_rmsd(vector<double> xyz, vector<double> XYZ) {
//if(xyz.size() != XYZ.size()) {cout << "RMSD_vectors size() error!" << endl; return 100.0; }
//if(xyz.size() %3 !=0 || XYZ.size() %3 != 0) {cout << "RMSD_vectors size() error!" << endl; return 100.0; }
double x1[RMSD_max][3]= {0.0}, x2[RMSD_max][3]= {0.0};
double x1cen[3] = {0.0}, x2cen[3] = {0.0}, M[3][3] = {0.0};
int lenx=xyz.size()/3; //cout << "lenx = " << lenx << endl;
FIT_RMSD_vectors_init( xyz, XYZ, lenx, x1, x2, x1cen, x2cen, M);
double rms = 0.0;
rms=FIT_RMSD_MM(lenx, x1, x2, x1cen, x2cen, M); //cout << "rms = " << rms << endl;
return rms;
}
double RMSD_arrays_rmsd( int lenx, double x1[][3], double x2[][3]) {
double x1cen[3] = {0.0}, x2cen[3] = {0.0}, M[3][3] = {0.0};
FIT_RMSD_arrays_init( lenx, x1, x2, x1cen, x2cen, M);
double rms = 0.0;
rms=FIT_RMSD_MM(lenx, x1, x2, x1cen, x2cen, M); //cout << "rms = " << rms << endl;
return rms;
}
double RMSD_vectors_xx1_to_xx3 (vector<double> xyz, vector<double> XYZ, double x1cen[3], double x2cen[3], double M[][3] ) {
double x1[RMSD_max][3]= {0.0}, x2[RMSD_max][3]= {0.0};
int lenx=xyz.size()/3; //cout << "lenx = " << lenx << endl;
FIT_RMSD_vectors_init( xyz, XYZ, lenx, x1, x2, x1cen, x2cen, M);
double rms = 0.0;
rms= FIT_RMSD_MM(lenx, x1, x2, x1cen, x2cen, M); //cout << "rms = " << rms << endl;
return rms;
}
double RMSD_arrays_xx1_to_xx3 (int lenx, double x1[][3], double x2[][3], int lenxx, double xx1[][3], double xx3[][3] ) {
double x1cen[3] = {0.0}, x2cen[3] = {0.0}, M[3][3] = {0.0};
(void) FIT_RMSD_arrays_init( lenx, x1, x2, x1cen, x2cen, M);
double rms = 0.0;
rms= FIT_RMSD_MM(lenx, x1, x2, x1cen, x2cen, M); //cout << "rms = " << rms << endl;
// xx1 to xx3
rotate_xx1_to_xx3(x1cen, x2cen, M, lenxx, xx1,xx3);
return rms;
}
double RMSD_arrays_cens_M (int lenx, double x1[][3], double x2[][3], double x1cen[3], double x2cen[3], double M[][3] ) {
FIT_RMSD_arrays_init( lenx, x1, x2, x1cen, x2cen, M);
double rms = 0.0;
rms= FIT_RMSD_MM(lenx, x1, x2, x1cen, x2cen, M); //cout << "rms = " << rms << endl;
return rms;
}
double RMSD_vectors_cens_M (vector<double> xyz, vector<double> XYZ, double x1cen[3], double x2cen[3], double M[][3] ) {
double x1[RMSD_max][3]= {0.0}, x2[RMSD_max][3]= {0.0};
int lenx=xyz.size()/3; //cout << "lenx = " << lenx << endl;
FIT_RMSD_vectors_init( xyz, XYZ, lenx, x1, x2, x1cen, x2cen, M);
double rms = 0.0;
rms= FIT_RMSD_MM(lenx, x1, x2, x1cen, x2cen, M); //cout << "rms = " << rms << endl;
return rms;
}
/*
double RMSD_vectors_W_cens_M (vector<double> xyz, vector<double> XYZ, vector<int> weight, double x1cen[3], double x2cen[3], double M[][3] ) {
if(xyz.size()!=weight.size()*3) {cout << "xyz.size != weight.size()*3" << endl; return 1000.0;}
vector<double> xyz_w, XYZ_w;
for(int i=0;i<xyz.size();i++){
for(int j=0;j<weight[i];j++) {
xyz_w.push_back(xyz[3*i]);
xyz_w.push_back(xyz[3*i+1]);
xyz_w.push_back(xyz[3*i+2]);
XYZ_w.push_back(XYZ[3*i]);
XYZ_w.push_back(XYZ[3*i+1]);
XYZ_w.push_back(XYZ[3*i+2]);
}
}
double rms=RMSD_vectors_cens_M(xyz_w, XYZ_w, x1cen, x2cen, M);
return rms;
}
*/
#endif