00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <stdio.h>
00015 #include "general.h"
00016 #include "linalg3.h"
00017 #include "filecls.h"
00018
00019 #define NPM 1000000
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Converter{
00029 public:
00030 int _NP;
00031 char matname[100];
00032 int reflash; double ex,ey,ez,sx0,sy0,sz0;
00033 double ratio;
00034 class Vector3 _SR[NPM];
00035
00036 int _LATOMTYPE[NPM];
00037 class Matrix33 _H, _VIRIAL;
00038
00039 Converter():reflash(1),ex(1),ey(1),ez(1),sx0(0),sy0(0),sz0(0),ratio(1){};
00040 int readcn(char *name)
00041 {
00042 int i;
00043 char *buffer; char *pp,fname[100];
00044 class LFile *f;
00045
00046 strcpy(fname,name);
00047 f=LFile::Open(fname,LFile::O_Read);
00048 if(f==NULL)
00049 {
00050 sprintf(fname,"%s.bz2",name);
00051 f=LFile::Open(fname,LFile::O_Read);
00052 if(f==NULL)
00053 {
00054 sprintf(fname,"%s.gz",name);
00055 f=LFile::Open(fname,LFile::O_Read);
00056 if(f==NULL) return -1;
00057 }
00058 }
00059
00060 LFile::LoadToString(fname,buffer,0);
00061
00062 pp=buffer;
00063 sscanf(pp, "%d", &_NP);
00064
00065 pp=strchr(pp, '\n');
00066 pp++;
00067 for(i=0;i<_NP;i++)
00068 {
00069 char *q;
00070 q=pp;
00071 pp=strchr(pp,'\n');
00072 if(pp) *(char *)pp++=0;
00073 sscanf(q, "%lf %lf %lf",
00074 &(_SR[i].x),&(_SR[i].y),&(_SR[i].z));
00075 }
00076
00077 sscanf(pp, "%lf %lf %lf %lf %lf %lf %lf %lf %lf",
00078 &(_H[0][0]),&(_H[0][1]),&(_H[0][2]),
00079 &(_H[1][0]),&(_H[1][1]),&(_H[1][2]),
00080 &(_H[2][0]),&(_H[2][1]),&(_H[2][2]));
00081
00082 free(buffer);
00083 LFile::Close(f);
00084 return 0;
00085 }
00086 void reflashS()
00087 { for(int i=0;i<_NP;i++) _SR[i].subint(); }
00088 int writepdb(char *fname)
00089 {
00090 double a, b, c, alpha, beta, gamma;
00091 double b1,b2,c1,c2,c3;
00092 Vector3 va, vb, vc; Matrix33 h0;
00093 FILE *f;
00094
00095 f=fopen(fname,"w");
00096
00097 va.set(_H[0][0],_H[1][0],_H[2][0]); va*=ratio;
00098 vb.set(_H[0][1],_H[1][1],_H[2][1]); vb*=ratio;
00099 vc.set(_H[0][2],_H[1][2],_H[2][2]); vc*=ratio;
00100 a=va.norm();
00101 b=vb.norm();
00102 c=vc.norm();
00103 alpha=acos(vb*vc/b/c);
00104 beta=acos(vc*va/c/a);
00105 gamma=acos(va*vb/a/b);
00106 fprintf(f,"HEADER cn2pdb NP=%d\n",_NP);
00107 fprintf(f,"HEADER 500 MUST_PBC\n");
00108 fprintf(f,"CRYST1%9.3lf%9.3lf%9.3lf"
00109 "%7.2lf%7.2lf%7.2lf P 1 1\n",
00110 a,b,c,alpha*180/M_PI,beta*180/M_PI,gamma*180/M_PI);
00111
00112 b1=b*cos(gamma);
00113 b2=b*sin(gamma);
00114 c1=c*cos(beta);
00115 c2=c*(cos(alpha)-cos(beta)*cos(gamma))/sin(gamma);
00116 c3=sqrt(c*c-c1*c1-c2*c2);
00117 h0.set(a,b1,c1,
00118 0,b2,c2,
00119 0,0,c3);
00120
00121
00122 for(int i=0;i<_NP;i++)
00123 {
00124 Vector3 r,s;
00125 s=_SR[i];
00126
00127 s.subint();
00128 s.x+=0.5; s.y+=0.5; s.z+=0.5;
00129
00130
00131
00132
00133
00134
00135
00136 r=h0*s;
00137
00138 fprintf(f,"ATOM %5d %2s"
00139 " 1 %8.3f%8.3f%8.3f\n",
00140 i+1,
00141 ((_LATOMTYPE[i]==0)?matname:
00142 ((_LATOMTYPE[i]==16)?"O":"Si")),
00143 r.x,r.y,r.z);
00144 }
00145 fclose(f);
00146 LFile::BZipFile(fname,true);
00147 return 0;
00148 }
00149 } con;
00150
00151 int cn2pdb(int argv, char *argc[])
00152 {
00153 char fname[100], *ext;
00154 int i,n;
00155 if(argv==1)
00156 {
00157 fprintf(stderr,"Usage: %s [-<NAME>] [-f] [-e[x,y,z]<n>]"
00158 "[-s(x,y,z)<n>] [-r<n>] filename.cn\n",argc[0]);
00159 return -1;
00160 }
00161 sprintf(con.matname,"%s","Mo");
00162 n=argv;
00163 for(i=1;i<n;i++)
00164 {
00165 if(argc[i][0]=='-')
00166 {
00167 if((argc[i][1]=='f')&&(argc[i][2]==0))
00168 {
00169 con.reflash=0; continue;
00170 }
00171 if((argc[i][1]=='e')&&(argc[i][2]!=0))
00172 {
00173 if((argc[i][2]!='x')&&(argc[i][2]!='y')&&(argc[i][2]!='z'))
00174 {
00175 sscanf(argc[i]+2,"%lf",&con.ex);
00176 con.ey=con.ez=con.ex;continue;
00177 }
00178 else
00179 {
00180 switch(argc[i][2]) {
00181 case('x'): sscanf(argc[i]+3,"%lf",&con.ex);continue;
00182 case('y'): sscanf(argc[i]+3,"%lf",&con.ey);continue;
00183 case('z'): sscanf(argc[i]+3,"%lf",&con.ez);continue;
00184 }
00185 }
00186 }
00187 if((argc[i][1]=='s')&&(argc[i][2]!=0))
00188 {
00189 if((argc[i][2]!='x')&&(argc[i][2]!='y')&&(argc[i][2]!='z'))
00190 {
00191 continue;
00192 }
00193 else
00194 {
00195 switch(argc[i][2]) {
00196 case('x'): sscanf(argc[i]+3,"%lf",&con.sx0);continue;
00197 case('y'): sscanf(argc[i]+3,"%lf",&con.sy0);continue;
00198 case('z'): sscanf(argc[i]+3,"%lf",&con.sz0);continue;
00199 }
00200 }
00201 }
00202 if((argc[i][1]=='r')&&(argc[i][2]!=0))
00203 {
00204 sscanf(argc[i]+2,"%lf",&con.ratio);
00205 continue;
00206 }
00207 sscanf(argc[i]+1,"%s",con.matname);
00208
00209 continue;
00210 }
00211
00212
00213 sprintf(fname,"%s",argc[i]);
00214
00215 ext=strstr(fname, ".cn");
00216 if(ext==NULL)
00217 {
00218 fprintf(stderr,HIR"invalid file"NOR"(not .cn) %s\n",fname);
00219 continue;
00220 }
00221
00222 con.readcn(fname);
00223
00224 if(con.reflash) con.reflashS();
00225
00226 fprintf(stderr,"%s -> ",fname);
00227 ext[1]='p';
00228 ext[2]='d';
00229 ext[3]='b';
00230 ext[4]=0;
00231 fprintf(stderr,"%s\n",fname);
00232 con.writepdb(fname);
00233 }
00234 return 0;
00235 }
00236
00237 int main(int argv, char *argc[])
00238 {
00239
00240
00241
00242
00243 #ifdef _CN2PDB
00244 cn2pdb(argv,argc);
00245 #elif defined(_MCN2CN)
00246 maurice2cn(argv,argc);
00247 #elif defined(_VCN2CN)
00248 vasily2cn(argv,argc);
00249 #elif defined(_LCN2PDB)
00250 lcn2pdb(argv,argc);
00251 #elif defined(_MCN2PDB)
00252 mcn2pdb(argv,argc);
00253 #endif
00254 return 0;
00255 }
00256