cuikenergy.c
Go to the documentation of this file.
1 #include "filename.h"
2 #include "box_list.h"
3 #include "error.h"
4 #include "defines.h"
5 #include "parameters.h"
6 #include "bioworld.h"
7 
8 
9 #include <stdio.h>
10 #include <math.h>
11 #include <strings.h>
12 
81 int main(int argc, char **arg)
82 {
83  Tparameters parameters;
84  Tworld world;
85  TBioWorld bioWorld;
86  boolean bio;
87  Tfilename fparam,fsols,fcost;
88  Tbox *box;
89  FILE *fout;
90  unsigned int n,k,nBoxes;
91  double *point;
92  Tlist boxList;
93  Titerator i;
94  double *cost,minCost,maxCost;
95  double *conf;
96  unsigned int nMin,nMax;
97  double e;
98  boolean log_cost,scale_cost;
99 
100  if (argc>2)
101  {
102  /* default flags */
103  log_cost=FALSE;
104  scale_cost=TRUE;
105 
106  /* parse parameters */
107  for(k=1;k<argc-2;k++)
108  {
109  if (strcasecmp(arg[k],"scale")==0)
110  scale_cost=TRUE;
111  if (strcasecmp(arg[k],"log")==0)
112  log_cost=TRUE;
113  if (strcasecmp(arg[k],"noscale")==0)
114  scale_cost=FALSE;
115  if (strcasecmp(arg[k],"nolog")==0)
116  log_cost=FALSE;
117  }
118 
119  /*Init parameters*/
120  CreateFileName(NULL,arg[argc-2],NULL,PARAM_EXT,&fparam);
121  fprintf(stderr,"Reading parameters : %s\n",GetFileFullName(&fparam));
122  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
123 
124  if (InitBioWorld(&parameters,arg[argc-2],NO_UINT,&conf,&bioWorld))
125  bio=TRUE;
126  else
127  {
128  InitWorldFromFile(&parameters,TRUE,arg[argc-2],&world);
129  bio=FALSE;
130  }
131 
132  CreateFileName(NULL,arg[argc-1],NULL,SOL_EXT,&fsols);
133  fprintf(stderr,"Reading solution file : %s\n",GetFileFullName(&fsols));
134  ReadListOfBoxes(GetFileFullName(&fsols),&boxList);
135  nBoxes=ListSize(&boxList);
136  if (nBoxes==0)
137  Error("Empty solution file in cuikenergy");
138 
139  /* Compute the energy for each conformation */
140  InitIterator(&i,&boxList);
141  First(&i);
142  k=0;
143  NEW(cost,nBoxes,double);
144  while(!EndOfList(&i))
145  {
146  box=(Tbox *)GetCurrent(&i);
147  if (k==0)
148  {
149  n=GetBoxNIntervals(box);
150  NEW(point,n,double);
151  }
152  GetBoxCenter(NULL,point,box);
153  if (bio)
154  e=BioWorldEnergy(&parameters,FALSE,point,(void *)&bioWorld);
155  else
156  e=WorldPotentialEnergy(&parameters,FALSE,point,(void *)&world);
157 
158  if (log_cost)
159  cost[k]=log(e);
160  else
161  cost[k]=e;
162 
163  fprintf(stderr,"%u -> %g (%g)\n",k,cost[k],e);
164 
165  if (k==0)
166  {
167  minCost=cost[k]; nMin=k;
168  maxCost=cost[k]; nMax=k;
169  }
170  else
171  {
172  if (cost[k]>maxCost)
173  { maxCost=cost[k]; nMax=k; }
174  else
175  {
176  if (cost[k]<minCost)
177  { minCost=cost[k]; nMin=k; }
178  }
179  }
180  k++;
181  Advance(&i);
182  }
183  fprintf(stderr,"Extreme energies : %g (%u) -- %g (%u)\n",minCost,nMin,maxCost,nMax);
184  fprintf(stderr,"Parameters: ");
185  if (log_cost)
186  fprintf(stderr,"log,");
187  else
188  fprintf(stderr,"nolog,");
189  if (scale_cost)
190  fprintf(stderr,"scale\n");
191  else
192  fprintf(stderr,"noscale\n");
193 
194 
195  /* Normalize and write */
196  CreateFileName(NULL,arg[argc-1],NULL,COST_EXT,&fcost);
197  fprintf(stderr,"Writing cost file : %s\n",GetFileFullName(&fcost));
198 
199  fout=fopen(GetFileFullName(&fcost),"w");
200  if (!fout)
201  Error("Output file for costs can not be opened");
202 
203  for(k=0;k<nBoxes;k++)
204  {
205  if (scale_cost)
206  {
207  e=((cost[k]-minCost)/(maxCost-minCost))*(1-1e-3)+1e-3;
208  fprintf(fout,"%g\n",e);
209  }
210  else
211  fprintf(fout,"%g\n",cost[k]);
212  }
213  fclose(fout);
214 
215  DeleteFileName(&fsols);
216  DeleteFileName(&fcost);
217  DeleteFileName(&fparam);
218 
219  free(point);
220  free(cost);
221  DeleteListOfBoxes(&boxList);
222  DeleteParameters(&parameters);
223  if (bio)
224  {
225  DeleteBioWorld(&bioWorld);
226  free(conf);
227  }
228  else
229  DeleteWorld(&world);
230  }
231  else
232  {
233  fprintf(stderr," Wrong number of parameters.\n");
234  fprintf(stderr," Use:\n");
235  fprintf(stderr," cuikenergy [log|nolog, scale|noscale] <problem_name>.pdb <sol_name>\n");
236  fprintf(stderr," where:\n");
237  fprintf(stderr," <log|nolog> [optional] Applies (or not) the log to the energy (default: nolog).\n");
238  fprintf(stderr," <scale|noscale> [optional] Scales (or not) the energy to the range [0,1] (default: scale).\n");
239  fprintf(stderr," <problem_name> the input molecular information file (pdb).\n");
240  fprintf(stderr," <sol_name> Configurations to evaluate.\n");
241  fprintf(stderr," The extension (e.g., '.pdb') is required\n");
242  fprintf(stderr," All the extensions managed by OpenBabel can be used\n");
243  fprintf(stderr," The ouput file is <sol_name>.cost\n");
244  }
245 
246  return(EXIT_SUCCESS);
247 }
void First(Titerator *i)
Moves an iterator to the first position of its associated list.
Definition: list.c:356
#define FALSE
FALSE.
Definition: boolean.h:30
void DeleteBioWorld(TBioWorld *bw)
Destructor.
Definition: bioworld.c:1924
double BioWorldEnergy(Tparameters *p, boolean simp, double *conformation, void *bw)
Computes the energy of a given configuration.
Definition: bioworld.c:1902
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
Data structure to hold the information about the name of a file.
Definition: filename.h:271
Definition of the Tfilename type and the associated functions.
#define COST_EXT
File extension for arrays of costs.
Definition: filename.h:156
#define TRUE
TRUE.
Definition: boolean.h:21
void Error(const char *s)
General error function.
Definition: error.c:80
#define PARAM_EXT
File extension for parameter files.
Definition: filename.h:132
All the necessary information to generate equations for mechanisms.
Definition: world.h:229
boolean ReadListOfBoxes(char *filename, Tlist *l)
Reads a list of boxes from a file.
Definition: box_list.c:286
Collection of methods to work on Tlist of boxes.
void DeleteWorld(Tworld *w)
Destructor.
Definition: world.c:3952
unsigned int GetBoxNIntervals(Tbox *b)
Box dimensionality.
Definition: box.c:1016
Error and warning functions.
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
int main(int argc, char **arg)
Main body of the cuikenergy application.
Definition: cuikenergy.c:81
void DeleteListOfBoxes(Tlist *l)
Destructor.
Definition: box_list.c:353
boolean EndOfList(Titerator *i)
Checks if an iterator is pointing at the end of the list.
Definition: list.c:445
Definitions of constants and macros used in several parts of the cuik library.
A generic list.
Definition: list.h:46
void InitIterator(Titerator *i, Tlist *list)
Constructor.
Definition: list.c:284
boolean InitBioWorld(Tparameters *p, char *filename, unsigned int maxAtomsLink, double **conformation, TBioWorld *bw)
Initializes a world form a biomolecule.
Definition: bioworld.c:1394
A table of parameters.
void CreateFileName(char *path, char *name, char *suffix, char *ext, Tfilename *fn)
Constructor.
Definition: filename.c:22
A bridge between world structures and biological information.
void InitParametersFromFile(char *file, Tparameters *p)
Constructor from a file.
Definition: parameters.c:51
char * GetFileFullName(Tfilename *fn)
Gets the file full name (paht+name+extension).
Definition: filename.c:151
#define SOL_EXT
File extension for solution files.
Definition: filename.h:138
boolean InitWorldFromFile(Tparameters *p, boolean error, char *fn, Tworld *w)
Constructor.
A box.
Definition: box.h:83
void * GetCurrent(Titerator *i)
Gets the element pointed by the iterator.
Definition: list.c:299
#define NO_UINT
Used to denote an identifier that has not been initialized.
Definition: defines.h:435
void DeleteParameters(Tparameters *p)
Destructor.
Definition: parameters.c:294
Structure with the molecular and the mechanical information.
Definition: bioworld.h:28
List iterator.
Definition: list.h:61
void GetBoxCenter(boolean *used, double *c, Tbox *b)
Returns the box center along the selected dimensions.
Definition: box.c:721
unsigned int ListSize(Tlist *list)
Gets the number of elements in the list.
Definition: list.c:180
Definition of the Tparameters type and the associated functions.
double WorldPotentialEnergy(Tparameters *p, boolean simp, double *sol, void *w)
Evaluates the potential energy of a configuration.
Definition: world.c:2680
boolean Advance(Titerator *i)
Moves an iterator to the next position of its associated list.
Definition: list.c:373