cuikatoms2samples.c
Go to the documentation of this file.
1 #include "defines.h"
2 #include "filename.h"
3 #include "bioworld.h"
4 
5 #include <stdlib.h>
6 
73 int main(int argc, char **arg)
74 {
75  if (argc>1)
76  {
77  Tparameters parameters;
78  TBioWorld bioWorld;
79 
80  Tfilename fparam;
81  Tfilename fsamples;
82  Tfilename fatoms;
83 
84  boolean nv;
85  double *sample;
86 
87  unsigned int na;
88  double *atoms;
89 
90  unsigned int i,j,k,r;
91  boolean end;
92  int token;
93 
94  FILE *fin;
95  FILE *fout;
96 
97  /* Init parameters */
98  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
99  fprintf(stderr,"Reading parameters : %s\n",GetFileFullName(&fparam));
100  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
101 
102  /* Read the problem file */
103  InitBioWorld(&parameters,arg[1],NO_UINT,&sample,&bioWorld);
104  free(sample);
105 
106  /* File from where to read the atoms */
107  CreateFileName(NULL,arg[1],NULL,ATOM_EXT,&fatoms);
108  fin=fopen(GetFileFullName(&fatoms),"r");
109  if (!fin)
110  Error("Can not open the input file with the atom positions");
111 
112  /* Open the file where to store the output samples */
113  r=(unsigned int)(GetParameter(CT_REPRESENTATION,&parameters));
114  if (r==REP_JOINTS)
115  CreateFileName(NULL,arg[1],"_a2s",JOINTS_EXT,&fsamples);
116  else
117  CreateFileName(NULL,arg[1],"_a2s",LINKS_EXT,&fsamples);
118  fprintf(stderr,"Writing samples to : %s\n",GetFileFullName(&fsamples));
119 
120  fout=fopen(GetFileFullName(&fsamples),"w");
121  if (!fout)
122  Error("Could not open the file to write the samples");
123 
124  /* Allocate space for the atoms and the sample */
125  na=BioWorldNAtoms(&bioWorld);
126  NEW(atoms,3*na,double);
127 
128  /* Read one sample from the file of samples */
129  end=FALSE;
130  do {
131  k=0;
132  for(i=0;((!end)&&(i<na));i++)
133  {
134  for(j=0;((!end)&&(j<3));j++,k++)
135  {
136  token=fscanf(fin,"%lf",&(atoms[k]));
137  if ((token==EOF)||(token==0))
138  end=TRUE;
139  }
140  }
141 
142  if (!end)
143  {
144  /* Deduce the conformation from the atom positions */
145  nv=BioWordConformationFromAtomPositions(&parameters,atoms,&sample,&bioWorld);
146 
147  /* Save the conformation */
148  for(i=0;i<nv;i++)
149  fprintf(fout,"%.16f ",sample[i]);
150  fprintf(fout,"\n");
151 
152  free(sample);
153  }
154 
155  } while(!end);
156 
157  /* Close the files */
158  fclose(fin);
159  fclose(fout);
160 
161  /* Release memeory */
162  free(atoms);
163 
164  DeleteBioWorld(&bioWorld);
165  DeleteParameters(&parameters);
166 
167  DeleteFileName(&fparam);
168  DeleteFileName(&fsamples);
169  DeleteFileName(&fatoms);
170  }
171  else
172  {
173  fprintf(stderr," Wrong number of parameters.\n");
174  fprintf(stderr," Use:\n");
175  fprintf(stderr," cuikatoms2samples <problem_name>.pdb\n");
176  fprintf(stderr," where <problem_name>.pdb contains the molecular information\n");
177  fprintf(stderr," The '.pab' extension is required\n");
178  fprintf(stderr," All the extensions managed by OpenBabel can be used\n");
179  }
180 
181  return(EXIT_SUCCESS);
182 }