Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

py_molecule.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2008 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: py_molecule.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.63 $       $Date: 2008/03/27 19:36:52 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *  Python molecule manipulation interface
00019  ***************************************************************************/
00020 
00021 #include "py_commands.h"
00022 #include <ctype.h>
00023 #include <stdlib.h>
00024 
00025 #include "config.h"
00026 #include "utilities.h"
00027 #include "VMDApp.h"
00028 #include "JString.h"
00029 #include "Molecule.h"
00030 #include "MoleculeList.h"
00031 
00032 
00033 static char mol_num_doc[] = 
00034   "num() -> int\n"
00035   "RAeturns the number of loaded molecules.";
00036 static PyObject *mol_num(PyObject *self, PyObject *args) {
00037   if (!PyArg_ParseTuple(args, (char *)":molecule.num"))
00038     return NULL;
00039 
00040   return PyInt_FromLong(get_vmdapp()->num_molecules());
00041 }
00042 
00043 static char mol_listall_doc[] =
00044   "listall() -> list\n"
00045   "Returns list of all valid molid's.";
00046 static PyObject *mol_listall(PyObject *self, PyObject *args) {
00047   if (!PyArg_ParseTuple(args, (char *)":molecule.listall"))
00048     return NULL;
00049 
00050   VMDApp *app = get_vmdapp();
00051   int num = app->num_molecules();
00052   PyObject *newlist = PyList_New(num);
00053   for (int i=0; i<num; i++)
00054     PyList_SET_ITEM(newlist, i, PyInt_FromLong(app->molecule_id(i)));
00055 
00056   return newlist;
00057 }
00058 
00059 static char mol_exists_doc[] = 
00060   "exists(molid) -> boolean\n"
00061   "Return True if molid is valid.";
00062 static PyObject *mol_exists(PyObject *self, PyObject *args) {
00063   int molid;
00064   if (!PyArg_ParseTuple(args, (char *)"i:molecule.exists", &molid))
00065     return NULL;
00066   VMDApp *app = get_vmdapp();
00067   return PyInt_FromLong(app->molecule_valid_id(molid));
00068 }
00069 
00070 static char mol_name_doc[] = 
00071   "name(molid) -> string\n"
00072   "Returns name of given molecule";
00073 static PyObject *mol_name(PyObject *self, PyObject *args) {
00074   int molid;
00075 
00076   if (!PyArg_ParseTuple(args, (char *)"i:molecule.name", &molid))
00077     return NULL;
00078 
00079   VMDApp *app = get_vmdapp();
00080   const char *name = app->molecule_name(molid);
00081   if (!name) {
00082     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00083     return NULL;
00084   }
00085   return PyString_FromString((char *)name);
00086 }
00087 
00088 static char mol_numatoms_doc[] = 
00089   "numatoms (molid) -> int\n"
00090   "Returns number of atoms in given molecule";
00091 // numatoms(molid)
00092 static PyObject *mol_numatoms(PyObject *self, PyObject *args) {
00093   int molid;
00094 
00095   if (!PyArg_ParseTuple(args, (char *)"i:molecule.numatoms", &molid))
00096     return NULL;
00097 
00098   VMDApp *app = get_vmdapp();
00099   if (!app->molecule_valid_id(molid)) {
00100     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00101     return NULL;
00102   }
00103   return PyInt_FromLong(app->molecule_numatoms(molid));
00104 }
00105 
00106 static char mol_new_doc[] = 
00107   "new(name) -> int\n"
00108   "Creates new molecule with given name and returns molid.";
00109 static PyObject *mol_new(PyObject *self, PyObject *args) {
00110   char *name;
00111   if (!PyArg_ParseTuple(args, (char *)"s:molecule.new", &name))
00112     return NULL;
00113   VMDApp *app = get_vmdapp();
00114   int molid = app->molecule_new(name);
00115   if (molid < 0) {
00116     PyErr_SetString(PyExc_ValueError, (char *)"Unable to create molecule.");
00117     return NULL;
00118   }
00119   return PyInt_FromLong(molid);
00120 }
00121 
00122 static char mol_load_doc[] = 
00123   "load(structure, sfname, coor, cfname) -> molid\n"
00124   "Load new molecule with structure file type 'structure', \n"
00125   "  structure file name 'sfname', coordinate file type 'coor', and\n"
00126   "  coordinate file name 'cfname'.  'coor' and 'cfname' are optional.";
00127 
00128 static PyObject *mol_load(PyObject *self, PyObject *args, PyObject *keywds) {
00129  
00130   char *structure = NULL, *coor = NULL, *sfname = NULL, *cfname = NULL;
00131  
00132   static char *kwlist[] = {
00133     (char *)"structure", (char *)"sfname", (char *)"coor", (char *)"cfname", 
00134     NULL
00135   };
00136 
00137   if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"ss|ss:molecule.load", kwlist, 
00138         &structure, &sfname, &coor, &cfname))
00139     return NULL;
00140 
00141   // must specify structure and a structure file
00142   if (!structure) {
00143     PyErr_SetString(PyExc_ValueError, (char *)"No structure type specified");
00144     return NULL;
00145   }
00146   if (!sfname) {
00147     PyErr_SetString(PyExc_ValueError, (char *)"No structure file specified");
00148     return NULL;
00149   }
00150 
00151   // if a coordinates file type was specified, a coordinate file must be given
00152   // as well, and vice versa.
00153   if (coor && !cfname) {
00154     PyErr_SetString(PyExc_ValueError, (char *)"No coordinate file specified");
00155     return NULL;
00156   }
00157   if (cfname && !coor) {
00158     PyErr_SetString(PyExc_ValueError, (char *)"No coordinate type specified");
00159     return NULL;
00160   }
00161 
00162   // Get the VMDApp instance from the VMDApp module
00163   VMDApp *app = get_vmdapp();
00164 
00165   // Special-case graphics molecules to load as "blank" molecules
00166   if (!strcmp(structure, "graphics")) {
00167     return PyInt_FromLong(app->molecule_new(sfname));
00168   }
00169   FileSpec spec;
00170   int molid = app->molecule_load(-1, sfname, structure, &spec);
00171   if (molid < 0) {
00172     PyErr_SetString(PyExc_ValueError, (char *)"Unable to load structure file");
00173     return NULL;
00174   }
00175   if (cfname) {
00176     app->molecule_load(molid, cfname, coor, &spec);
00177   } 
00178   return PyInt_FromLong(molid); 
00179 }
00180 
00181 static char mol_cancel_doc[] =
00182   "cancel(molid) -> None\n"
00183   "Cancel background loading of files for given molid.";
00184 static PyObject *mol_cancel(PyObject *self, PyObject *args) {
00185   // get one integer argument
00186   int molid;
00187   if (!PyArg_ParseTuple(args, (char *)"i:molecule.cancel", &molid))
00188     return NULL;
00189 
00190   VMDApp *app = get_vmdapp();
00191   if (!app->molecule_valid_id(molid)) {
00192     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00193     return NULL;
00194   } 
00195   app->molecule_cancel_io(molid);
00196 
00197   Py_INCREF(Py_None);
00198   return Py_None;
00199 }
00200 
00201 static char mol_delete_doc[] =
00202   "delete(molid) -> None\n"
00203   "Delete molecule with given molid.";
00204 static PyObject *mol_delete(PyObject *self, PyObject *args) {
00205   int molid;
00206   if (!PyArg_ParseTuple(args, (char *)"i:molecule.delete", &molid))
00207     return NULL;
00208 
00209   VMDApp *app = get_vmdapp();
00210   if (!app->molecule_valid_id(molid)) {
00211     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00212     return NULL;
00213   } 
00214   app->molecule_delete(molid);
00215   
00216   Py_INCREF(Py_None);
00217   return Py_None;
00218 }
00219 
00220 static char get_top_doc[] =
00221   "get_top(molid) -> molid\n"
00222   "Return molid of top molecule.";
00223 static PyObject *get_top(PyObject *self, PyObject *args) {
00224   if (!PyArg_ParseTuple(args, (char *)":molecule.get_top"))
00225     return NULL;
00226 
00227   return PyInt_FromLong(get_vmdapp()->molecule_top());
00228 }
00229 
00230 static char set_top_doc[] =
00231   "set_top(molid) -> None\n"
00232   "Make the molecule top.";
00233 static PyObject *set_top(PyObject *self, PyObject *args) {
00234   int molid;
00235   if (!PyArg_ParseTuple(args, (char *)"i:molecule.set_top", &molid))
00236     return NULL;
00237 
00238   VMDApp *app = get_vmdapp();
00239   if (!app->molecule_valid_id(molid)) {
00240     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00241     return NULL;
00242   } 
00243   app->molecule_make_top(molid);
00244   
00245   Py_INCREF(Py_None);
00246   return Py_None;
00247 }
00248 
00249 
00250 static PyObject *readorwrite(PyObject *self, PyObject *args, PyObject *keywds,
00251                             int do_read) {
00252 
00253   int molid = -1;
00254   char *filename = NULL, *type = NULL;
00255   int beg=0, end=-1, stride=1, waitfor = 1;
00256   PyObject *volsets = NULL, *selobj = NULL;
00257   int *on = NULL;
00258 
00259   static char *kwlist[] = {
00260     (char *)"molid", (char *)"filetype", (char *)"filename", (char *)"beg", 
00261     (char *)"end", (char *)"skip", (char *)"waitfor", (char *)"volsets", 
00262     (char *)"selection", NULL
00263   };
00264 
00265   if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"iss|iiiiO!O:read/write", kwlist,
00266     &molid, &type, &filename, &beg, &end, &stride, &waitfor, &PyList_Type, 
00267     &volsets, &selobj))
00268     return NULL;
00269 
00270   VMDApp *app = get_vmdapp();
00271  
00272   int numframes = 0;
00273   if (do_read) { 
00274     FileSpec spec;
00275     spec.first = beg;
00276     spec.last = end;
00277     spec.stride = stride;
00278     spec.waitfor = waitfor;
00279     if (volsets) {
00280       spec.nvolsets = PyList_Size(volsets);
00281       spec.setids = new int[spec.nvolsets];
00282       for (int i=0; i<spec.nvolsets; i++) 
00283         spec.setids[i] = PyInt_AsLong(PyList_GET_ITEM(volsets, i));
00284       if (PyErr_Occurred()) return NULL;
00285     } else {
00286       // Have a default of {0} for setids so that it isn't always necessary
00287       // to specify the set id's.  This should be ignored if the file type 
00288       // can't read volumetric datasets
00289       spec.nvolsets = 1;
00290       spec.setids = new int[1];
00291       spec.setids[0] = 0;
00292     }
00293     return PyInt_FromLong(app->molecule_load(molid, filename, type, &spec));
00294   }
00295   if (!app->molecule_valid_id(molid)) {
00296     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00297     return NULL;
00298   }
00299   if (selobj && selobj != Py_None) {
00300     AtomSel *sel = atomsel_AsAtomSel( selobj );
00301     if (!sel) return NULL;
00302     if (sel->molid() != molid) {
00303       PyErr_SetString( PyExc_ValueError, 
00304           "atomsel must reference same molecule as coordinates" );
00305       return NULL;
00306     }
00307     on = sel->on;
00308   }
00309   FileSpec spec;
00310   spec.first = beg;
00311   spec.last = end;
00312   spec.stride = stride;
00313   spec.waitfor = waitfor;
00314   spec.selection = on;
00315   numframes = app->molecule_savetrajectory(molid, filename, type, &spec);
00316   if (numframes < 0) {
00317     PyErr_SetString(PyExc_ValueError, (char *)"Unable to save file");
00318     return NULL;
00319   }
00320   return PyInt_FromLong(numframes);
00321 }
00322 
00323 static char mol_read_doc[] =
00324   "read(molid, filetype, filename, beg=0, end=-1, skip=1, waitfor=1)\n"
00325   "Read data from file 'filename' of type 'filetype' into molecule with\n"
00326   "id 'molid'.  molid=-1 creates new molecule.  Return number of frames\n"
00327   "read (remaining frames will be loaded in the background.\n"
00328   "  First frame = beg; default 0\n"
00329   "  Last frame = end; default all frames\n"
00330   "  Frame stride = skip; default load all frames\n"
00331   "  Frames to load before returning = waitfor; default 1\n";
00332 static PyObject *mol_read(PyObject *self, PyObject *args, PyObject *keywds) {
00333   return readorwrite(self, args, keywds, 1);
00334 }
00335 
00336 static char mol_write_doc[] =
00337   "write(molid, filetype, filename, beg=0, end=-1, skip=1, waitfor=-1,\n"
00338   "      selection=None)\n"
00339   "Write data into file 'filename' of type 'filetype' from molecule with\n"
00340   "id 'molid'.  Return number of frames written (remaining frames will be\n"
00341   "written in the background.\n"
00342   "  First frame = beg; default 0\n"
00343   "  Last frame = end; default all frames\n"
00344   "  Frame stride = skip; default load all frames\n"
00345   "  Frames to load before returning = waitfor; default 1\n"
00346   "  Specify selection of atoms to write with 'selection' keyword; pass\n"
00347   "    tuple of atom indexes as argument.";
00348 
00349 static PyObject *mol_write(PyObject *self, PyObject *args, PyObject *keywds) {
00350   return readorwrite(self, args, keywds, 0);
00351 }
00352 
00353 static char numframes_doc[] =
00354   "numframes(molid) -> number of frames in molecule with id 'molid'.";
00355 static PyObject *numframes(PyObject *self, PyObject *args) {
00356   int molid;
00357 
00358   if (!PyArg_ParseTuple(args, (char *)"i:molecule.numframes", &molid))
00359     return NULL;
00360 
00361   VMDApp *app = get_vmdapp();
00362   if (!app->molecule_valid_id(molid)) {
00363     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00364     return NULL;
00365   }
00366   return PyInt_FromLong(app->molecule_numframes(molid));
00367 }
00368 
00369 static char get_frame_doc[] =
00370   "get_frame(molid) -> current frame of molecule with id 'molid'.";
00371 static PyObject *get_frame(PyObject *self, PyObject *args) {
00372   int molid;
00373 
00374   if (!PyArg_ParseTuple(args, (char *)"i:molecule.get_frame", &molid))
00375     return NULL;
00376 
00377   VMDApp *app = get_vmdapp();
00378   if (!app->molecule_valid_id(molid)) {
00379     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00380     return NULL;
00381   }
00382   return PyInt_FromLong(app->molecule_frame(molid));
00383 }
00384 
00385 static char set_frame_doc[] =
00386   "set_frame(molid, frame)\n"
00387   "Change frame of molecule with id 'molid' to 'frame'.";
00388 // set_frame(molid, frame)
00389 static PyObject *set_frame(PyObject *self, PyObject *args) {
00390   int molid, frame;
00391 
00392   if (!PyArg_ParseTuple(args, (char *)"ii:molecule.set_frame", &molid, &frame))
00393     return NULL;
00394 
00395   VMDApp *app = get_vmdapp();
00396   Molecule *mol = app->moleculeList->mol_from_id(molid);
00397   if (!mol) {
00398     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00399     return NULL;
00400   }
00401   mol->override_current_frame(frame);
00402   mol->change_ts();
00403   Py_INCREF(Py_None);
00404   return Py_None;
00405 }
00406 
00407 static char delframe_doc[] =
00408   "delframe(molid, beg=0, end=-1, skip=0)\n"
00409   "Delete frames in the range [beg, end] inclusive, keeping every\n"
00410   "  'skip' frame between beg and end.";
00411 static PyObject *delframe(PyObject *self, PyObject *args, PyObject *keywds) {
00412 
00413   int molid = 0, beg=0, end=-1, skip=0;
00414 
00415   static char *kwlist[] = {
00416     (char *)"molid", (char *)"beg", (char *)"end", (char *)"skip", NULL
00417   };
00418 
00419   if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"i|iii:molecule.delframe", kwlist,
00420     &molid, &beg, &end, &skip))
00421     return NULL;
00422 
00423   VMDApp *app = get_vmdapp();
00424   if (!app->molecule_valid_id(molid)) {
00425     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molid");
00426     return NULL;
00427   }
00428 
00429   if (!app->molecule_deleteframes(molid, beg, end, skip)) {
00430     PyErr_SetString(PyExc_ValueError, (char *)"Unable to delete frames");
00431     return NULL;
00432   }
00433  
00434   Py_INCREF(Py_None);
00435   return Py_None;
00436 }
00437 
00438 static char dupframe_doc[] =
00439   "dupframe(molid, frame) -> Duplicate given frame.\n"
00440   "If frame is -1, duplicate the current frame; this also creates\n"
00441   "a new frame with coordinates all zero if there are no frames in\n"
00442   "the molecule.";
00443 static PyObject *dupframe(PyObject *self, PyObject *args) {
00444 
00445   int molid, frame;
00446   if (!PyArg_ParseTuple(args, (char *)"ii:molecule.dupframe", &molid, &frame))
00447     return NULL;
00448 
00449   VMDApp *app = get_vmdapp();
00450   if (!app->molecule_valid_id(molid)) {
00451     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molid");
00452     return NULL;
00453   }
00454 
00455   if (!app->molecule_dupframe(molid, frame)) {
00456     PyErr_SetString(PyExc_ValueError, (char *)"Unable to duplicate frame");
00457     return NULL;
00458   }
00459   Py_INCREF(Py_None);
00460   return Py_None;
00461 }
00462 
00463 static char mol_ssrecalc_doc[] =
00464   "ssrecalc(molid) -> recalculate secondary structure for molecule.\n"
00465   "raises RuntimeError if structure could not be calculated, usually\n"
00466   "due to the Stride program not being available.";
00467 static PyObject *mol_ssrecalc(PyObject *self, PyObject *args) {
00468   int molid;
00469   if (!PyArg_ParseTuple(args, (char *)"i:molecule.ssrecalc", &molid))
00470     return NULL;
00471 
00472   VMDApp *app = get_vmdapp();
00473   if (!app->molecule_valid_id(molid)) {
00474     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00475     return NULL;
00476   } 
00477   int rc;
00478   Py_BEGIN_ALLOW_THREADS
00479   rc = app->molecule_ssrecalc(molid);
00480   Py_END_ALLOW_THREADS
00481   if (!rc) {
00482     PyErr_SetString(PyExc_RuntimeError, 
00483         "Secondary structure could not be calculated");
00484     return NULL;
00485   }
00486   Py_INCREF(Py_None);
00487   return Py_None;
00488 }
00489 
00490 static char mol_rename_doc[] =
00491   "rename(molid, newname) -> rename molecule to 'newname'.";
00492 static PyObject *mol_rename(PyObject *self, PyObject *args) {
00493   int molid;
00494   char *newname;
00495   if (!PyArg_ParseTuple(args, (char *)"is:molecule.rename", &molid, &newname))
00496     return NULL;
00497 
00498   VMDApp *app = get_vmdapp();
00499   if (!app->molecule_valid_id(molid)) {
00500     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00501     return NULL;
00502   } 
00503   if (!app->molecule_rename(molid, newname)) {
00504     PyErr_SetString(PyExc_ValueError, (char *)"Unable to rename molecule.");
00505     return NULL;
00506   }
00507   Py_INCREF(Py_None);
00508   return Py_None;
00509 }
00510    
00511 static char add_volumetric_doc[] = 
00512   "add_volumetric(molid, name, origin, xaxis, yaxis, zaxis,\n"
00513   "               xsize, ysize, zsize, data)\n"
00514   "  Add a new volumetric data set to a molecule.  origin, xaxis, yaxis,\n"
00515   "  and zaxis are 3-vectors represented as lists.  xsize, ysize, zsize\n"
00516   "  give the size of the volumetric grid data.  data must be a list of\n"
00517   "  floats of size xsize * ysize * zsize.  origin represents the point\n"
00518   "  at the lower left rear corner of the grid.";
00519 static PyObject *mol_add_volumetric(PyObject *self, PyObject *args, PyObject *keywds) {
00520 
00521   int molid = -1;
00522   int xsize = -1, ysize = -1, zsize = -1;
00523   int size;
00524   char *name;
00525   PyObject *data = NULL, *origin = NULL, *xaxis = NULL, *yaxis = NULL, 
00526            *zaxis = NULL;
00527   float forigin[3], fxaxis[3], fyaxis[3], fzaxis[3];
00528 
00529   static char *kwlist[] = {
00530     (char *)"molid", (char *)"name", (char *)"origin",  (char *)"xaxis",
00531     (char *)"yaxis",  (char *)"zaxis",  (char *)"xsize", (char *)"ysize", 
00532     (char *)"zsize", (char *)"data", NULL
00533   };
00534 
00535   if (!PyArg_ParseTupleAndKeywords(args, keywds, 
00536        (char *)"isOOOOiiiO:molecule.add_volumetric", kwlist,
00537        &molid, &name, &origin, &xaxis, &yaxis, &zaxis, &xsize, 
00538        &ysize, &zsize, &data))
00539     return NULL;
00540 
00541   VMDApp *app = get_vmdapp();
00542   if (!app->molecule_valid_id(molid)) {
00543     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00544     return NULL;
00545   }
00546   if (xsize < 1 || ysize < 1 || zsize < 1) {
00547     PyErr_SetString(PyExc_ValueError, (char *)"Invalid size parameters");
00548     return NULL;
00549   }
00550   size = xsize * ysize * zsize;
00551   if (!PyList_Check(data)) {
00552     PyErr_SetString(PyExc_ValueError, (char *)"data must be a list");
00553     return NULL;
00554   }
00555   if (PyList_Size(data) != size) {
00556     PyErr_SetString(PyExc_ValueError, (char *)"size of list does not match specified sizes");
00557     return NULL;
00558   }
00559   // Check that any optional arguments are valid and set defaults.
00560   if (origin) {
00561     if (!py_array_from_obj(origin, forigin)) return NULL;
00562   } else {
00563     forigin[0] = forigin[1] = forigin[2] = 0;
00564   }
00565   if (xaxis) {
00566     if (!py_array_from_obj(xaxis, fxaxis)) return NULL;
00567   } else {
00568     fxaxis[0] = 1.0; fxaxis[1] = fxaxis[2] = 0;
00569   }
00570   if (yaxis) {
00571     if (!py_array_from_obj(yaxis, fyaxis)) return NULL;
00572   } else {
00573     fyaxis[1] = 1.0; fyaxis[0] = fyaxis[2] = 0;
00574   }
00575   if (zaxis) {
00576     if (!py_array_from_obj(zaxis, fzaxis)) return NULL;
00577   } else {
00578     fzaxis[2] = 1.0; fzaxis[0] = fzaxis[1] = 0;
00579   }
00580 
00581   // allocate float array here; pass it to molecule
00582   float *fdata = new float[size];
00583   for (int i=0; i<size; i++) {
00584     PyObject *elem = PyList_GET_ITEM(data, i);
00585     float tmp = PyFloat_AsDouble(elem);
00586     if (PyErr_Occurred()) {
00587       delete [] fdata;
00588       return NULL;
00589     }
00590     fdata[i] = tmp;
00591   }
00592   if (!app->molecule_add_volumetric(molid, name, forigin, fxaxis, fyaxis,
00593        fzaxis, xsize, ysize, zsize, fdata)) {
00594     PyErr_SetString(PyExc_ValueError, (char *)"Unable to add volumetric data");
00595     delete [] fdata;
00596     return NULL;
00597   }
00598   Py_INCREF(Py_None);
00599   return Py_None;
00600 }
00601    
00602 static char filenames_doc[] = "get_filenames(molid): return list of files loaded in the molecule";
00603 static char filetypes_doc[] = "get_filetypes(molid): returns list of corresponding file types.";
00604 static char databases_doc[] = "get_databases(molid): returns list of databases of origin ";
00605 static char accessions_doc[] = "get_accessions(molid): returns list of database accession codes";
00606 static char remarks_doc[] = "get_remarks(molid): returns list of per-file remarks/comments";
00607 static PyObject *get_filenames(PyObject *self, PyObject *args) {
00608   int molid;
00609   if (!PyArg_ParseTuple(args, (char *)"i:molecule.get_filenames", &molid))
00610     return NULL;
00611 
00612   VMDApp *app = get_vmdapp();
00613   Molecule *mol = app->moleculeList->mol_from_id(molid);
00614   if (!mol) {
00615     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00616     return NULL;
00617   }
00618   int num = mol->num_files();
00619   PyObject *result = PyList_New(num);
00620   for (int i=0; i<mol->num_files(); i++) {
00621     PyList_SET_ITEM(result, i, PyString_FromString(mol->get_file(i)));
00622   }
00623   return result;
00624 }
00625 static PyObject *get_filetypes(PyObject *self, PyObject *args) {
00626   int molid;
00627   if (!PyArg_ParseTuple(args, (char *)"i:molecule.get_filetypes", &molid))
00628     return NULL;
00629 
00630   VMDApp *app = get_vmdapp();
00631   Molecule *mol = app->moleculeList->mol_from_id(molid);
00632   if (!mol) {
00633     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00634     return NULL;
00635   }
00636   int num = mol->num_files();
00637   PyObject *result = PyList_New(num);
00638   for (int i=0; i<mol->num_files(); i++) {
00639     PyList_SET_ITEM(result, i, PyString_FromString(mol->get_type(i)));
00640   }
00641   return result;
00642 }
00643 static PyObject *get_databases(PyObject *self, PyObject *args) {
00644   int molid;
00645   if (!PyArg_ParseTuple(args, (char *)"i:molecule.get_databases", &molid))
00646     return NULL;
00647 
00648   VMDApp *app = get_vmdapp();
00649   Molecule *mol = app->moleculeList->mol_from_id(molid);
00650   if (!mol) {
00651     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00652     return NULL;
00653   }
00654   int num = mol->num_files();
00655   PyObject *result = PyList_New(num);
00656   for (int i=0; i<mol->num_files(); i++) {
00657     PyList_SET_ITEM(result, i, PyString_FromString(mol->get_database(i)));
00658   }
00659   return result;
00660 }
00661 static PyObject *get_accessions(PyObject *self, PyObject *args) {
00662   int molid;
00663   if (!PyArg_ParseTuple(args, (char *)"i:molecule.get_accessions", &molid))
00664     return NULL;
00665 
00666   VMDApp *app = get_vmdapp();
00667   Molecule *mol = app->moleculeList->mol_from_id(molid);
00668   if (!mol) {
00669     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00670     return NULL;
00671   }
00672   int num = mol->num_files();
00673   PyObject *result = PyList_New(num);
00674   for (int i=0; i<mol->num_files(); i++) {
00675     PyList_SET_ITEM(result, i, PyString_FromString(mol->get_accession(i)));
00676   }
00677   return result;
00678 }
00679 static PyObject *get_remarks(PyObject *self, PyObject *args) {
00680   int molid;
00681   if (!PyArg_ParseTuple(args, (char *)"i:molecule.get_remarks", &molid))
00682     return NULL;
00683 
00684   VMDApp *app = get_vmdapp();
00685   Molecule *mol = app->moleculeList->mol_from_id(molid);
00686   if (!mol) {
00687     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00688     return NULL;
00689   }
00690   int num = mol->num_files();
00691   PyObject *result = PyList_New(num);
00692   for (int i=0; i<mol->num_files(); i++) {
00693     PyList_SET_ITEM(result, i, PyString_FromString(mol->get_remarks(i)));
00694   }
00695   return result;
00696 }
00697 
00698 
00699 static char get_periodic_doc[] = 
00700   "get_periodic(molid=-1, frame=-1)\n"
00701   "return dict with keys 'a', 'b', 'c', 'alpha', 'beta', and 'gamma'\n"
00702   "representing the periodic cell layout for a particular frame.\n"
00703   "a, b, and c are the lengths of the unit cell along the first, second\n"
00704   "and third unit cell vectors, respectively.  alpha, beta, and gamma\n"
00705   "give the angle between sides B and C, A and C, and A and B,\n"
00706   "respectively.";
00707 static PyObject *get_periodic(PyObject *self, PyObject *args, PyObject *kwds) {
00708   int molid=-1;
00709   int frame=-1;
00710   static char *kwlist[] = { (char *)"molid", (char *)"frame", NULL };
00711   if (!PyArg_ParseTupleAndKeywords(args, kwds, (char *)"|ii:molecule.get_periodic", kwlist, &molid, &frame))
00712     return NULL;
00713 
00714   Timestep *ts = parse_timestep(get_vmdapp(), molid, frame);
00715   if (!ts) return NULL;
00716   PyObject *dict = PyDict_New();
00717   PyDict_SetItemString(dict, (char *)"a", PyFloat_FromDouble(ts->a_length));
00718   PyDict_SetItemString(dict, (char *)"b", PyFloat_FromDouble(ts->b_length));
00719   PyDict_SetItemString(dict, (char *)"c", PyFloat_FromDouble(ts->c_length));
00720   PyDict_SetItemString(dict, (char *)"alpha", PyFloat_FromDouble(ts->alpha));
00721   PyDict_SetItemString(dict, (char *)"beta", PyFloat_FromDouble(ts->beta));
00722   PyDict_SetItemString(dict, (char *)"gamma", PyFloat_FromDouble(ts->gamma));
00723   return dict;
00724 }
00725 
00726 static char set_periodic_doc[] =
00727   "set_periodic(molid=-1, frame=-1, a, b, c, alpha, beta, gamma)\n"
00728   "Set the periodic cell layout for a particular frame.\n"
00729   "All keywords except molid are optional.  See get_periodic for\n"
00730   "descriptions of the keywords.";
00731 static PyObject *set_periodic(PyObject *self, PyObject *args, PyObject *kwds) {
00732   int molid=-1;
00733   int frame=-1;
00734   float a=-1, b=-1, c=-1, alpha=-1, beta=-1, gamma=-1;
00735   static char *kwlist[] = { (char *)"molid", (char *)"frame", (char *)"a",
00736     (char *)"b", (char *)"c", (char *)"alpha", (char *)"beta", (char *)"gamma",
00737     NULL
00738   };
00739   if (!PyArg_ParseTupleAndKeywords(args, kwds, 
00740         (char *)"|iiffffff:molecule.set_periodic", kwlist, 
00741         &molid, &frame, &a, &b, &c, &alpha, &beta, &gamma))
00742     return NULL;
00743   Timestep *ts = parse_timestep(get_vmdapp(), molid, frame);
00744   if (!ts) return NULL;
00745   if (a >= 0) ts->a_length = a;
00746   if (b >= 0) ts->b_length = b;
00747   if (c >= 0) ts->c_length = c;
00748   if (alpha > 0) ts->alpha = alpha;
00749   if (beta > 0) ts->beta = beta;
00750   if (gamma > 0) ts->gamma = gamma;
00751 
00752   Py_INCREF(Py_None);
00753   return Py_None;
00754 }
00755 
00756 static char get_visible_doc[] =
00757   "get_visible(molid=-1) -> boolean\n"
00758   "  Get visibility state for selected molecule, default top molecule\n";
00759 static char set_visible_doc[] =
00760   "set_visible(molid=-1, state)\n"
00761   "  Turn selected molecule on or off, default top molecule\n";
00762 
00763 static PyObject *get_visible(PyObject *self, PyObject *args, PyObject *kwds) {
00764   int molid = -1;
00765   static char *kwlist[] = { "molid", NULL };
00766   if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwlist, &molid))
00767     return NULL;
00768   VMDApp *app = get_vmdapp();
00769   if (!app) return NULL;
00770   if (molid<0) molid = app->molecule_top();
00771   if (!app->molecule_valid_id(molid)) {
00772     PyErr_SetString(PyExc_ValueError, "Invalid molid");
00773     return NULL;
00774   }
00775   return PyBool_FromLong( app->molecule_is_displayed( molid ) );
00776 }
00777 static PyObject *set_visible(PyObject *self, PyObject *args, PyObject *kwds) {
00778   int molid = -1;
00779   PyObject *obj=NULL;
00780 
00781   static char *kwlist[] = { "molid", "state", NULL };
00782   if (!PyArg_ParseTupleAndKeywords(args, kwds, "iO", kwlist, &molid, &obj))
00783     return NULL;
00784   VMDApp *app = get_vmdapp();
00785   if (!app) return NULL;
00786   if (molid<0) molid = app->molecule_top();
00787   if (!app->molecule_valid_id(molid)) {
00788     PyErr_SetString(PyExc_ValueError, "Invalid molid");
00789     return NULL;
00790   }
00791   app->molecule_display( molid, PyObject_IsTrue(obj));
00792   Py_INCREF(Py_None);
00793   return Py_None;
00794 }
00795 
00796 static PyObject *get_physical_time(PyObject *self, PyObject *args, PyObject *kwds) {
00797   int molid = -1;
00798   int frame = -1;
00799   static char *kwlist[] = { "molid", "frame", NULL };
00800   if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii", kwlist, &molid, &frame))
00801     return NULL;
00802   Timestep *ts = parse_timestep(get_vmdapp(), molid, frame);
00803   if (!ts) return NULL;
00804   return PyFloat_FromDouble(ts->physical_time);
00805 }
00806 
00807 static PyObject *set_physical_time(PyObject *self, PyObject *args, PyObject *kwds) {
00808   int molid = -1;
00809   int frame = -1;
00810   double value;
00811   static char *kwlist[] = { "value", "molid", "frame", NULL };
00812   if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|ii", kwlist, &value, &molid, &frame))
00813     return NULL;
00814   Timestep *ts = parse_timestep(get_vmdapp(), molid, frame);
00815   if (!ts) return NULL;
00816   ts->physical_time = value;
00817 
00818   Py_INCREF(Py_None);
00819   return Py_None;
00820 }
00821 
00822 static PyMethodDef MolMethods[] = {
00823   {(char *)"num", (vmdPyMethod)mol_num, METH_VARARGS, mol_num_doc },
00824   {(char *)"listall", (vmdPyMethod)mol_listall, METH_VARARGS,mol_listall_doc},
00825   {(char *)"new", (vmdPyMethod)mol_new, METH_VARARGS, mol_new_doc},
00826   {(char *)"load", (PyCFunction)mol_load, METH_VARARGS | METH_KEYWORDS, mol_load_doc},
00827   {(char *)"cancel", (vmdPyMethod)mol_cancel, METH_VARARGS, mol_cancel_doc},
00828   {(char *)"delete", (vmdPyMethod)mol_delete, METH_VARARGS, mol_delete_doc},
00829   {(char *)"read", (PyCFunction)mol_read, METH_VARARGS | METH_KEYWORDS, mol_read_doc},
00830   {(char *)"write", (PyCFunction)mol_write, METH_VARARGS | METH_KEYWORDS, mol_write_doc},
00831   {(char *)"delframe", (PyCFunction)delframe, METH_VARARGS | METH_KEYWORDS, delframe_doc},
00832   {(char *)"dupframe", (vmdPyMethod)dupframe, METH_VARARGS, dupframe_doc},
00833   {(char *)"numframes", (vmdPyMethod)numframes, METH_VARARGS, numframes_doc},
00834   {(char *)"get_frame", (vmdPyMethod)get_frame, METH_VARARGS, get_frame_doc},
00835   {(char *)"set_frame", (vmdPyMethod)set_frame, METH_VARARGS, set_frame_doc},
00836   {(char *)"numatoms", (vmdPyMethod)mol_numatoms, METH_VARARGS, mol_numatoms_doc},
00837   {(char *)"exists", (vmdPyMethod)mol_exists, METH_VARARGS, mol_exists_doc},
00838   {(char *)"name", (vmdPyMethod)mol_name, METH_VARARGS, mol_name_doc},
00839   {(char *)"ssrecalc", (vmdPyMethod)mol_ssrecalc, METH_VARARGS, mol_ssrecalc_doc},
00840   {(char *)"rename", (vmdPyMethod)mol_rename, METH_VARARGS, mol_rename_doc},
00841   {(char *)"get_top", (vmdPyMethod)get_top, METH_VARARGS, get_top_doc},
00842   {(char *)"set_top", (vmdPyMethod)set_top, METH_VARARGS, set_top_doc},
00843   {(char *)"add_volumetric", (PyCFunction)mol_add_volumetric, 
00844                               METH_VARARGS | METH_KEYWORDS, add_volumetric_doc},
00845   {(char *)"get_filenames", (vmdPyMethod)get_filenames, METH_VARARGS, filenames_doc},
00846   {(char *)"get_filetypes", (vmdPyMethod)get_filetypes, METH_VARARGS, filetypes_doc},
00847   {(char *)"get_databases", (vmdPyMethod)get_databases, METH_VARARGS, databases_doc},
00848   {(char *)"get_accessions", (vmdPyMethod)get_accessions, METH_VARARGS, accessions_doc},
00849   {(char *)"get_remarks", (vmdPyMethod)get_remarks, METH_VARARGS, remarks_doc},
00850   {(char *)"get_periodic", (PyCFunction)get_periodic, METH_VARARGS | METH_KEYWORDS, get_periodic_doc},
00851   {(char *)"set_periodic", (PyCFunction)set_periodic, METH_VARARGS | METH_KEYWORDS, set_periodic_doc},
00852   {(char *)"get_visible", (PyCFunction)get_visible, METH_VARARGS | METH_KEYWORDS, get_visible_doc},
00853   {(char *)"set_visible", (PyCFunction)set_visible, METH_VARARGS | METH_KEYWORDS, set_visible_doc},
00854   {(char *)"get_physical_time", (PyCFunction)get_physical_time, METH_VARARGS | METH_KEYWORDS },
00855   {(char *)"set_physical_time", (PyCFunction)set_physical_time, METH_VARARGS | METH_KEYWORDS },
00856   {NULL, NULL}
00857 };
00858 
00859 void initmolecule() {
00860   (void) Py_InitModule((char *)"molecule", MolMethods);
00861 }
00862 

Generated on Sat Sep 6 01:27:08 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002