File: PlugIOManager.c

package info (click to toggle)
pymol 1.7.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 42,668 kB
  • ctags: 25,775
  • sloc: ansic: 494,779; python: 75,446; cpp: 20,088; makefile: 351; sh: 172; csh: 21
file content (486 lines) | stat: -rw-r--r-- 15,694 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486

/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/
#include"os_python.h"
#include "os_std.h"
#include "MemoryDebug.h"
#include "PlugIOManager.h"
#include "Selector.h"
#include "CoordSet.h"
#include "Feedback.h"
#include "Scene.h"
#include "Executive.h"

#ifndef _PYMOL_VMD_PLUGINS
int PlugIOManagerInit(PyMOLGlobals * G)
{
  return 1;
}

int PlugIOManagerFree(PyMOLGlobals * G)
{
  return 1;
}

int PlugIOManagerRegister(PyMOLGlobals * G, void *ptr);
int PlugIOManagerRegister(PyMOLGlobals * G, void *ptr)
{
  return 1;
}

int PlugIOManagerLoadTraj(PyMOLGlobals * G, ObjectMolecule * obj,
                          char *fname, int frame,
                          int interval, int average, int start,
                          int stop, int max, char *sele, int image,
                          float *shift, int quiet, char *plugin_type)
{

  PRINTFB(G, FB_ObjectMolecule, FB_Errors)
    " ObjectMolecule-Error: sorry, VMD Molfile Plugins not compiled into this build.\n"
    ENDFB(G);
  return 0;
}

ObjectMap *PlugIOManagerLoadVol(PyMOLGlobals * G, ObjectMap * obj,
                                char *fname, int state, int quiet, char *plugin_type)
{
  PRINTFB(G, FB_ObjectMolecule, FB_Errors)
    " ObjectMap-Error: sorry, VMD Molfile Plugins not compiled into this build.\n"
    ENDFB(G);
  return NULL;
}

#else

#include "molfile_plugin.h"

#ifdef __cplusplus
extern "C" {
#endif

struct _CPlugIOManager {
  int NPlugin;
  molfile_plugin_t **PluginVLA;
};

int PlugIOManagerInitAll(PyMOLGlobals * G);     /* defined externally */

int PlugIOManagerInit(PyMOLGlobals * G)
{
  register CPlugIOManager *I = NULL;
  if((I = (G->PlugIOManager = Calloc(CPlugIOManager, 1)))) {
    I->NPlugin = 0;
    I->PluginVLA = VLAlloc(molfile_plugin_t *, 10);
    return PlugIOManagerInitAll(G);
  } else
    return 0;
}

int PlugIOManagerFreeAll(void); /* defined externally */

int PlugIOManagerFree(PyMOLGlobals * G)
{
  CPlugIOManager *I = G->PlugIOManager;
  PlugIOManagerFreeAll();
  VLAFreeP(I->PluginVLA);
  FreeP(G->PlugIOManager);
  return 1;
}

int PlugIOManagerRegister(PyMOLGlobals * G, vmdplugin_t * header);

int PlugIOManagerRegister(PyMOLGlobals * G, vmdplugin_t * header)
{
  if(G && G->PlugIOManager) {
    if(!strcmp(header->type, MOLFILE_PLUGIN_TYPE)) {
      CPlugIOManager *I = G->PlugIOManager;
      VLACheck(I->PluginVLA, molfile_plugin_t *, I->NPlugin);
      I->PluginVLA[I->NPlugin] = (molfile_plugin_t *) header;
      I->NPlugin++;
      /*           printf("register %p %s\n",header,header->name); */
    }
    return VMDPLUGIN_SUCCESS;
  } else
    return VMDPLUGIN_ERROR;
}

int PlugIOManagerLoadTraj(PyMOLGlobals * G, ObjectMolecule * obj,
                          char *fname, int frame,
                          int interval, int average, int start,
                          int stop, int max, char *sele, int image,
                          float *shift, int quiet, char *plugin_type)
{

  if(G && G->PlugIOManager && obj) {
    CPlugIOManager *I = G->PlugIOManager;
    molfile_plugin_t *plugin = NULL;
    /*    int sele0 = SelectorIndexByName(G,sele); */

    {
      /* does this reader exist? */
      int a;
      for(a = 0; a < I->NPlugin; a++) {
        if(!strcmp(plugin_type, I->PluginVLA[a]->name)) {
          plugin = I->PluginVLA[a];
          break;
        }
      }
    }
    if(!plugin) {
      PRINTFB(G, FB_ObjectMolecule, FB_Errors)
        " ObjectMolecule: unable to locate plugin '%s'\n", plugin_type ENDFB(G);
    } else {
      int natoms;
      molfile_timestep_t timestep;
      void *file_handle;
      int zoom_flag = false;
      CoordSet *cs_tmpl = obj->CSet[0];

      timestep.coords = NULL;
      timestep.velocities = NULL;

      file_handle = plugin->open_file_read(fname, plugin_type, &natoms);

      if(!file_handle) {
        PRINTFB(G, FB_ObjectMolecule, FB_Errors)
          " ObjectMolecule: plugin '%s' cannot open '%s'.\n", plugin_type, fname ENDFB(G);
      } else if (natoms!=obj->NAtom) {
	PRINTFB(G, FB_ObjectMolecule, FB_Errors)
          " ObjectMolecule: plugin '%s' cannot open file because the number of atoms in the object (%d) did not equal the number of atoms in the '%s' (%d) file.\n", plugin_type, obj->NAtom, plugin_type, natoms ENDFB(G);
      } else if(cs_tmpl) {
	/* by this point, we have opened the DCD file, and we have a valid topology file (obj->CSet[0] exists) */
        CoordSet *cs = CoordSetCopy(cs_tmpl);
        /*        printf("%p\n",file_handle); */
        timestep.coords = (float *) cs->Coord;
        {
          int cnt = 0;
          int icnt = interval;
          int n_avg = 0;
          int ncnt = 0;

	  /* read_next_timestep fills in &timestep for each iteration; we need
	   * to copy that out to a new CoordSet, each time. */
          while(!plugin->read_next_timestep(file_handle, natoms, &timestep)) {
            cnt++;
	    /* start at the 'start'-th frame; skip 'start' frames,
	     * and skip every interval/icnt frames */
            if(cnt >= start) {
              icnt--;
              if(icnt > 0) {
                PRINTFB(G, FB_ObjectMolecule, FB_Details)
                  " ObjectMolecule: skipping set %d...\n", cnt ENDFB(G);
              } else {
                icnt = interval;
                n_avg++;
              }
              if(icnt == interval) {
                if(n_avg < average) {
                  PRINTFB(G, FB_ObjectMolecule, FB_Details)
                    " ObjectMolecule: averaging set %d...\n", cnt ENDFB(G);
                } else {
                  /* compute average */
                  if(n_avg > 1) {
                    float *fp;
                    int i;
                    fp = cs->Coord;
                    for(i = 0; i < cs->NIndex; i++) {
                      *(fp++) /= n_avg;
                      *(fp++) /= n_avg;
                      *(fp++) /= n_avg;
                    }
                  }
                  /* add new coord set */
                  if(cs->fInvalidateRep) cs->fInvalidateRep(cs, cRepAll, cRepInvRep);
                  if(frame < 0) frame = obj->NCSet;
                  if(!obj->NCSet) zoom_flag = true;

		  /* make sure we have room for 'frame' CoordSet*'s in obj->CSet */
		  /* TODO: TEST this function */
                  VLACheck(obj->CSet, CoordSet*, frame); /* was CoordSet* */
		  /* bump the object's state count */
                  if(obj->NCSet <= frame) obj->NCSet = frame + 1;
		  /* if there's data in this state's coordset, emtpy it */
                  if(obj->CSet[frame]) obj->CSet[frame]->fFree(obj->CSet[frame]);
		  /* set this state's coordset to cs */
                  obj->CSet[frame] = cs;
                  ncnt++;
                  if(average < 2) {
                    PRINTFB(G, FB_ObjectMolecule, FB_Details)
                      " ObjectMolecule: read set %d into state %d...\n", cnt, frame + 1
                      ENDFB(G);
                  } else {
                    PRINTFB(G, FB_ObjectMolecule, FB_Details)
                      " ObjectMolecule: averaging set %d...\n", cnt ENDFB(G);
                    PRINTFB(G, FB_ObjectMolecule, FB_Details)
                      " ObjectMolecule: average loaded into state %d...\n", frame + 1
                      ENDFB(G);
                  }

                  if(stop > 0 && cnt >= stop || max > 0 && ncnt >= max) {
                    cs = NULL;
                    break;
                  }

                  frame++;
		  /* make a new cs */
                  cs = CoordSetCopy(cs);        /* otherwise, we need a place to put the next set */
                  timestep.coords = (float *) cs->Coord;
                  n_avg = 0;
                }
              }
            } else {
              PRINTFB(G, FB_ObjectMolecule, FB_Details)
                " ObjectMolecule: skipping set %d...\n", cnt ENDFB(G);
            }
          } /* end while */
        }
        plugin->close_file_read(file_handle);
        if(cs) cs->fFree(cs);
        SceneChanged(G);
        SceneCountFrames(G);
        if(zoom_flag)
          if(SettingGetGlobal_i(G, cSetting_auto_zoom)) {
            ExecutiveWindowZoom(G, obj->Obj.Name, 0.0, -1, 0, 0, quiet);        /* auto zoom (all states) */
          }
      }
    }
  }
  return 0;
}

ObjectMap *PlugIOManagerLoadVol(PyMOLGlobals * G, ObjectMap * obj,
                                char *fname, int state, int quiet, char *plugin_type)
{
  int ok = true;
  printf("[%s %s]\n", fname, plugin_type);

  if(G && G->PlugIOManager) {
    CPlugIOManager *I = G->PlugIOManager;
    molfile_plugin_t *plugin = NULL;
    int natoms;
    void *file_handle = NULL;

    {
      /* does this reader exist? */
      int a;
      for(a = 0; a < I->NPlugin; a++) {
        if(!strcmp(plugin_type, I->PluginVLA[a]->name)) {
          plugin = I->PluginVLA[a];
          break;
        }
      }
      if(!plugin) {
        PRINTFB(G, FB_ObjectMolecule, FB_Errors)
          " ObjectMolecule: unable to locate plugin '%s'\n", plugin_type ENDFB(G);
        ok = false;
      }
    }

    if(ok) {
      file_handle = plugin->open_file_read(fname, plugin_type, &natoms);
      if(!file_handle) {
        PRINTFB(G, FB_ObjectMolecule, FB_Errors)
          " ObjectMolecule: plugin '%s' cannot open '%s'.\n", plugin_type, fname ENDFB(G);
        ok = false;
      }
    }

    if(ok) {
      molfile_volumetric_t *metadata;
      int setsinfile = 0;
      plugin->read_volumetric_metadata(file_handle, &setsinfile, &metadata);

      /* for now, just read in all sets of data in the file */

      {
        int i;
        for(i = 0; i < setsinfile; i++) {

          const molfile_volumetric_t *v = metadata + i;

          float *datablock = NULL, *colorblock = NULL;
          size_t size = v->xsize * v->ysize * v->zsize;

          datablock = Alloc(float, size);
          if(v->has_color) {
            colorblock = Alloc(float, size);
          }

          if(plugin->read_volumetric_data(file_handle, i, datablock, colorblock)) {
            PRINTFB(G, FB_ObjectMolecule, FB_Errors)
              " ObjectMap: plugin '%s' cannot open '%s'.\n", plugin_type, fname ENDFB(G);
            ok = false;
          } else {
            /* check map type */

            if((fabs(v->xaxis[1]) > R_SMALL8) ||
               (fabs(v->xaxis[2]) > R_SMALL8) ||
               (fabs(v->yaxis[0]) > R_SMALL4) ||
               (fabs(v->yaxis[2]) > R_SMALL4) ||
               (fabs(v->zaxis[0]) > R_SMALL4) || (fabs(v->zaxis[1]) > R_SMALL4)) {

              /*              dump3f(v->xaxis,"x");
                 dump3f(v->yaxis,"y");
                 dump3f(v->zaxis,"z");
               */

              PRINTFB(G, FB_ObjectMolecule, FB_Errors)
                " ObjectMap-Error: PyMOL only handles XYZ-axes-aligned CUBE files.\n"
                ENDFB(G);
              ok = false;
            }
          }

          if(ok) {

            int isNew = false;
            if(!obj) {
              obj = (ObjectMap *) ObjectMapNew(G);
              if(!obj)
                ok = false;
              else
                isNew = true;
            }
          }

          if(ok) {
            ObjectMapState *ms = NULL;

            if(state < 0)
              state = obj->NState;
            if(obj->NState <= state) {
              VLACheck(obj->State, ObjectMapState, state);
              obj->NState = state + 1;
            }
            ms = &obj->State[state];
            ObjectMapStateInit(obj->Obj.G, ms);

            ms->FDim[0] = v->xsize;
            ms->FDim[1] = v->ysize;
            ms->FDim[2] = v->zsize;
            ms->FDim[3] = 3;

            {
              int a = 0;
              for(a = 0; a < 3; a++) {
                ms->Min[a] = 0;
                ms->Max[a] = ms->FDim[a] - 1;
              }
            }

            ms->Grid = Alloc(float, 3);
            ms->Dim = Alloc(int, 3);
            ms->Origin = Alloc(float, 3);
            ms->Range = Alloc(float, 3);

            ms->Grid[0] = v->xaxis[0] / (ms->FDim[0] - 1);      /* we only support this special case: orthogonal & cartesian-aligned */
            ms->Grid[1] = v->yaxis[1] / (ms->FDim[1] - 1);
            ms->Grid[2] = v->zaxis[2] / (ms->FDim[2] - 1);

            {
              int a;
              for(a = 0; a < 3; a++) {
                ms->Dim[a] = ms->FDim[a];
                ms->Origin[a] = v->origin[a];
                ms->Range[a] = ms->Grid[a] * (ms->Dim[a] - 1);
              }
            }

            ms->Field = IsosurfFieldAlloc(G, ms->FDim);
            ms->MapSource = cMapSourceVMDPlugin;
            ms->Field->save_points = false;     /* save points in RAM only, not session file */
            ms->Active = true;

            ObjectMapStateRegeneratePoints(ms);

            dump3f(ms->Grid, "grid");
            dump3f(ms->Origin, "origin");

            {
              int a, b, c;
              float *data_ptr = datablock;
              float max_level = -FLT_MAX;
              float min_level = FLT_MAX;

              /* VMD plugins appear to use fast-x med-y slow-z ordering: "&datablock[z*xysize + y*xsize + x]" */

              for(c = 0; c < ms->FDim[2]; c++) {
                for(b = 0; b < ms->FDim[1]; b++) {
                  for(a = 0; a < ms->FDim[0]; a++) {
                    register float level = *(data_ptr++);
                    if(max_level < level)
                      max_level = level;
                    if(min_level > level)
                      min_level = level;

                    F3(ms->Field->data, a, b, c) = level;
                  }
                }
              }

              PRINTFB(G, FB_ObjectMap, FB_Details)
                " ObjectMap: read %d values between %1.6f and %1.6f.\n",
                ms->FDim[0] * ms->FDim[1] * ms->FDim[2], min_level, max_level ENDFB(G);
            }

            if(ok) {
              int a, b, c, d;
              float v[3];
              d = 0;
              for(c = 0; c < ms->FDim[2]; c += ms->FDim[2] - 1) {
                v[2] = ms->Origin[2] + ms->Grid[2] * (c + ms->Min[2]);

                for(b = 0; b < ms->FDim[1]; b += ms->FDim[1] - 1) {
                  v[1] = ms->Origin[1] + ms->Grid[1] * (b + ms->Min[1]);

                  for(a = 0; a < ms->FDim[0]; a += ms->FDim[0] - 1) {
                    v[0] = ms->Origin[0] + ms->Grid[0] * (a + ms->Min[0]);
                    copy3f(v, ms->Corner + 3 * d);
                    d++;
                  }
                }
              }
            }

            if(ok) {
              int a;
              for(a = 0; a < 3; a++) {
                ms->ExtentMin[a] = ms->Origin[a] + ms->Grid[a] * ms->Min[a];
                ms->ExtentMax[a] = ms->Origin[a] + ms->Grid[a] * ms->Max[a];
              }
            }
          }
          FreeP(datablock);
          FreeP(colorblock);
        }
      }
    }
    if(file_handle)
      plugin->close_file_read(file_handle);
  }
  if(ok) {
    ObjectMapUpdateExtents(obj);
    SceneChanged(G);
    SceneCountFrames(G);
  }
  return obj;
}

#ifdef __cplusplus
}
#endif

#endif