cuiksamples2atoms.c
Go to the documentation of this file.
1 #include "defines.h"
2 #include "filename.h"
3 #include "world.h"
4 
5 #include <stdlib.h>
6 
111 int main(int argc, char **arg)
112 {
113  if (argc>1)
114  {
115  Tparameters parameters;
116  Tworld world;
117 
118  Tfilename fparam;
119  Tfilename fsamples;
120  Tfilename fatoms;
121 
122  boolean nv;
123  double *sample;
124 
125  char *fn;
126  FILE *fs;
127 
128  unsigned int i,r;
129  boolean end;
130  int token;
131 
132  FILE *fout;
133 
134  /* Init parameters */
135  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
136  fprintf(stderr,"Reading parameters : %s\n",GetFileFullName(&fparam));
137 
138  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
139 
140  /* Read the problem file */
141  InitWorldFromFile(&parameters,FALSE,TRUE,arg[1],&world);
142 
143  if (!IsMechanismInWorldAllSpheres(&world))
144  Warning("The world is not sphere-only");
145 
146  /* Open the file from where to read the samples */
147  r=(unsigned int)(GetParameter(CT_REPRESENTATION,&parameters));
148  if (r==REP_JOINTS)
149  CreateFileName(NULL,arg[1],NULL,JOINTS_EXT,&fsamples);
150  else
151  CreateFileName(NULL,arg[1],NULL,LINKS_EXT,&fsamples);
152 
153  fprintf(stderr,"Reading one sample from : %s\n",GetFileFullName(&fsamples));
154 
155  fs=fopen(GetFileFullName(&fsamples),"r");
156 
157  if (!fs)
158  Error("Could not open the file to read the samples");
159 
160  if (argc>2)
161  fn=arg[2];
162  else
163  fn=arg[1];
164 
165  /* Open the file where to store the bio-information */
166  CreateFileName(NULL,fn,NULL,ATOM_EXT,&fatoms);
167  fprintf(stderr,"Writing atoms to : %s\n",GetFileFullName(&fatoms));
168 
169  /* Allocate space for the sample/dof */
170  nv=GetWorldNumSystemVariables(&world);
171  NEW(sample,nv,double);
172 
173  /* Read one sample from the file of samples */
174  fout=fopen(GetFileFullName(&fatoms),"w");
175  if (!fout)
176  Error("Could not open the file to store the atom positions");
177 
178  end=FALSE;
179  do {
180  for(i=0;((!end)&&(i<nv));i++)
181  {
182  token=fscanf(fs,"%lf",&(sample[i]));
183  if ((token==EOF)||(token==0))
184  end=TRUE;
185  }
186  /* If the sample could be read correctly, save the atom positions */
187  if (!end)
188  WorldPrintAtoms(&parameters,fout,sample,&world);
189  } while (!end);
190 
191  fclose(fout);
192 
193  /* Close the input file */
194  fclose(fs);
195 
196  /* Release memeory */
197  free(sample);
198  DeleteWorld(&world);
199  DeleteParameters(&parameters);
200 
201  DeleteFileName(&fparam);
202  DeleteFileName(&fsamples);
203  DeleteFileName(&fatoms);
204  }
205  else
206  {
207  fprintf(stderr,"Use:\n");
208  fprintf(stderr," cuiksamples2atoms <problem_name> <out_name>\n");
209  fprintf(stderr," <problem_name> the prefix to define the world,\n");
210  fprintf(stderr," .params and .samples files.\n");
211  fprintf(stderr," <out_name> [optional] The output atom file.\n");
212  fprintf(stderr,"CAUTION: This only works for carefully defined worlds\n");
213  fprintf(stderr," where the atoms are in the same order as in\n");
214  fprintf(stderr," the PDB. This is NOT the case for automatically\n");
215  fprintf(stderr," generated worlds (as those generated for protein loops)\n");
216  }
217 
218  return(EXIT_SUCCESS);
219 }