Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

PDB Class Reference

#include <PDB.h>

List of all members.

Public Member Functions

 PDB (const char *pdbfilename)
 PDB (molfile_plugin_t *pIOHdl, void *pIOFileHdl, int numAtoms, const float *occupancy, const float *bfactor)
 PDB (const char *, Ambertoppar *)
 PDB (const char *filename, const GromacsTopFile *topology)
 ~PDB (void)
void write (const char *outfilename, const char *commentline=NULL)
int num_atoms (void)
PDBAtomatom (int place)
PDBAtomListatoms (void)
void find_extremes (const Lattice &, BigReal frac=1.0)
void get_extremes (ScaledPosition &xmin, ScaledPosition &xmax) const
void set_all_positions (Vector *)
void get_all_positions (Vector *)
void get_position_for_atom (Vector *, int)


Constructor & Destructor Documentation

PDB::PDB const char *  pdbfilename  ) 
 

Definition at line 140 of file PDB.C.

References BigReal, PDBAtom::coordinates(), PAL::data, Fclose(), Fopen(), PDBAtom::myoccupancy, NAMD_die(), NAMD_err(), new_PDBData(), PAL::next, PDBAtom::occupancy(), PDBAtomList, PDBAtomPtr, and PDBAtom::temperaturefactor().

00140                                  {
00141   FILE *infile;
00142   char buf[160];
00143 
00144   atomCount = 0;
00145   atomListHead = atomListTail = NULL;
00146   infile = Fopen(pdbfilename, "r");
00147   if (! infile) {
00148      char s[500];
00149      sprintf(s, "Cannot open file '%s' for input in PDB::PDB.", pdbfilename);
00150      NAMD_err(s);
00151   }
00152   
00153     // loop through each line in the file and get a record
00154   while ( fgets(buf, 150, infile) ) {
00155    PDBData *newelement;
00156    char *s;
00157    for (s=buf; *s && *s!='\n'; s++)  // chop off the '\n'
00158     ;
00159    *s = 0;
00160    if ( s == (buf + 149) ) {
00161      char s[500];
00162      sprintf( s, "Line too long in pdbfile %s:\n%s\n", pdbfilename, buf);
00163      NAMD_die(s);
00164    }
00165    *(s+1) = 0;  // just to be on the safe side
00166 
00167      // I now have a string; make a PDBData element out of it
00168    newelement = new_PDBData(buf);
00169    if (!newelement) {
00170       NAMD_die("Could not allocate PDBData.\n");
00171    }
00172      // I only know how to deal with ATOM and HETATM types; and
00173      //  I want to throw away the unknown data types; so
00174    if (newelement -> type() != PDBData::ATOM && 
00175            newelement -> type() != PDBData::HETATM) {
00176        delete newelement;
00177    } else {
00178        add_atom_element( (PDBAtom *) newelement);
00179    }
00180   }  // input while loop
00181  Fclose(infile);
00182  
00183  // now I have a linked list, and I know the size.  However,
00184  // that's got pretty slow access time for ramdom fetches, so
00185  // I'll turn it into an array
00186  {
00187 
00188 #ifdef MEM_OPT_VERSION
00189 //pruning the PDBData structure into PDBCoreData by only leaving X/Y/Z, occupancy and temperaturefactor
00190      atomArray = new PDBCoreData[atomCount];
00191      if(atomArray == NULL)
00192          NAMD_die("memory allocation failed in PDB::PDB");
00193 
00194      PDBAtomList *tmp = atomListHead;
00195      for(int i=0; tmp!=NULL; tmp=tmp->next, i++){
00196          const BigReal *coor = tmp->data->coordinates();
00197          atomArray[i].coor[0] = coor[0];
00198          atomArray[i].coor[1] = coor[1];
00199          atomArray[i].coor[2] = coor[2];
00200          atomArray[i].myoccupancy = tmp->data->occupancy();
00201          atomArray[i].tempfactor = tmp->data->temperaturefactor();
00202      }
00203 
00204      //free the PDBAtomList
00205      tmp = atomListHead;
00206      while(tmp!=NULL){
00207          PDBAtomList *nextTmp = tmp->next;
00208          delete tmp->data;
00209          delete tmp;
00210          tmp = nextTmp;
00211      }
00212      atomListHead = atomListTail = NULL;
00213 #else
00214   atomArray = new PDBAtomPtr[atomCount];
00215   if ( atomArray == NULL )
00216   {
00217     NAMD_die("memory allocation failed in PDB::PDB");
00218   }
00219   PDBAtomList *tmp = atomListHead;
00220   int i=0;                              // just need to copy the pointers
00221   for (i=0, tmp = atomListHead; tmp != NULL; tmp = tmp -> next, i++) {
00222     atomArray[i] = tmp -> data;
00223   }
00224   // now delete the linked list (w/o deleting the data)
00225   PDBAtomList *tmp2;
00226   for (tmp2 = tmp = atomListHead; tmp != NULL; tmp = tmp2) {
00227     tmp2 = tmp->next;
00228     delete tmp;
00229   }
00230   atomListHead = atomListTail = NULL;
00231 #endif 
00232  }  // everything converted
00233  
00234 }

PDB::PDB molfile_plugin_t *  pIOHdl,
void *  pIOFileHdl,
int  numAtoms,
const float *  occupancy,
const float *  bfactor
 

Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by The Board of Trustees of the University of Illinois. All rights reserved.

Definition at line 82 of file PDB.C.

References BigReal, PDBAtom::coordinates(), NAMD_die(), PDBAtom::occupancy(), PDBAtomPtr, and PDBAtom::temperaturefactor().

00082                                                                                                               {
00083 #ifdef MEM_OPT_VERSION
00084   NAMD_die("Sorry, plugin IO is not supported in the memory optimized version.");
00085 #else
00086   molfile_timestep_t ts;
00087   float *atomcoords;
00088   memset(&ts, 0, sizeof(molfile_timestep_t));
00089 
00090   /* set defaults for unit cell information */
00091   ts.A = ts.B = ts.C = 0.0f;
00092   ts.alpha = ts.beta = ts.gamma = 90.0f; 
00093 
00094   atomCount = numAtoms;
00095 
00096   atomcoords = (float *) malloc(3*numAtoms*sizeof(float));
00097   memset(atomcoords, 0, 3*numAtoms*sizeof(float));
00098   ts.coords = atomcoords;
00099   
00100   if (pIOHdl->read_next_timestep(pIOFileHdl, numAtoms, &ts)) {
00101     free(atomcoords);    
00102     pIOHdl->close_file_read(pIOFileHdl);
00103     NAMD_die("ERROR: failed reading atom coordinates");
00104   }
00105 
00106   //load coordinates to PDB object
00107   //Note: the PDBAtom structure is very redundant in this case
00108   PDBAtom *tmpAtoms = new PDBAtom[numAtoms];
00109   atomArray = new PDBAtomPtr[numAtoms];
00110   BigReal tmpCoords[3];
00111   for(int i=0; i<numAtoms; i++) {
00112       PDBAtom *thisAtom = tmpAtoms+i;
00113       atomArray[i] = thisAtom;
00114 
00115       tmpCoords[0] = atomcoords[0];
00116       tmpCoords[1] = atomcoords[1];
00117       tmpCoords[2] = atomcoords[2];
00118       atomcoords += 3; //set to the next atom
00119       thisAtom->coordinates(tmpCoords);
00120   }
00121   free(ts.coords);
00122 
00123   if(occupancy) {
00124       for(int i=0; i<numAtoms; i++) {
00125           PDBAtom *thisAtom = tmpAtoms+i;
00126           thisAtom->occupancy((BigReal)occupancy[i]);          
00127       }
00128   }
00129   if(bfactor) {
00130       for(int i=0; i<numAtoms; i++) {
00131           PDBAtom *thisAtom = tmpAtoms+i;       
00132           thisAtom->temperaturefactor((BigReal)bfactor[i]);       
00133       }
00134   }
00135 #endif
00136 }

PDB::PDB const char *  ,
Ambertoppar
 

Definition at line 544 of file PDB.C.

References Ambertoppar, parm::AtomNames, parm::AtomRes, BigReal, PDBAtom::coordinates(), Fopen(), j, PDBAtom::myoccupancy, NAMD_die(), NAMD_err(), PDBAtom::name(), parm::Natom, PDBAtomPtr, readtoeoln(), PDBAtom::residuename(), PDBAtom::residueseq(), parm::ResNames, and PDBAtom::serialnumber().

00545 { int i,j,k;
00546   BigReal coor[3];
00547   char buf[13],resname[5],atomname[5];
00548   FILE *infile;
00549   PDBAtom *pdb;
00550 
00551   if ((infile=Fopen(filename, "r")) == NULL)
00552     NAMD_err("Can't open AMBER coordinate file!");
00553 
00554   readtoeoln(infile);  // Skip the first line (title)
00555 
00556   fscanf(infile,"%d",&atomCount);  // Read in num of atoms
00557   if (atomCount != amber_data->Natom)
00558     NAMD_die("Num of atoms in coordinate file is different from that in parm file!");
00559   readtoeoln(infile);
00560 
00561 #ifdef MEM_OPT_VERSION
00562   atomArray = new PDBCoreData[atomCount];
00563 #else
00564   atomArray = new PDBAtomPtr[atomCount];
00565 #endif
00566 
00567   if ( atomArray == NULL )
00568   {
00569     NAMD_die("memory allocation failed in PDB::PDB");
00570   }
00571   
00572   // Read in the coordinates, which are in the format of 6F12.7
00573   // Other fields are copied from "amber_data"
00574   for (i=0; i<atomCount; ++i)
00575   { // Read x,y,z coordinates
00576     for (j=0; j<3; ++j)
00577     { for (k=0; k<12; ++k)
00578       { buf[k]=getc(infile);
00579         if (buf[k]=='\n' || buf[k]=='\0' || buf[k]==EOF)
00580           NAMD_die("Error reading AMBER coordinate file!");
00581       }
00582       buf[12] = '\0';
00583       coor[j] = atof(buf);
00584     }
00585     if (i%2 == 1)
00586       readtoeoln(infile);
00587 #ifdef MEM_OPT_VERSION
00588     atomArray[i].coor[0] = coor[0];
00589     atomArray[i].coor[1] = coor[1];
00590     atomArray[i].coor[2] = coor[2];
00591     atomArray[i].myoccupancy = PDBAtom::default_occupancy;
00592     atomArray[i].tempfactor = PDBAtom::default_temperaturefactor;
00593 #else
00594     // Copy name, resname and resid from "amber_data"
00595     for (j=0; j<4; ++j)
00596     { resname[j] = amber_data->ResNames[amber_data->AtomRes[i]*4+j];
00597       atomname[j] = amber_data->AtomNames[i*4+j];
00598     }
00599     resname[4] = atomname[4] = '\0';
00600     // Create a new PDB record, and fill in its entries
00601     pdb = new PDBAtomRecord("");
00602     pdb->name(atomname);
00603     pdb->residuename(resname);
00604     pdb->serialnumber(i+1);
00605     pdb->residueseq(amber_data->AtomRes[i]+1);
00606     pdb->coordinates(coor);
00607     atomArray[i] = pdb;  // Include the new record into the array
00608 #endif
00609   }
00610 }

PDB::PDB const char *  filename,
const GromacsTopFile topology
 

Definition at line 618 of file PDB.C.

References BigReal, PDBAtom::coordinates(), Fopen(), GromacsTopFile::getAtom(), GromacsTopFile::getNumAtoms(), LINESIZE, PDBAtom::myoccupancy, NAMD_die(), NAMD_err(), PDBAtom::name(), PDBAtomPtr, Real, PDBAtom::residuename(), PDBAtom::residueseq(), and PDBAtom::serialnumber().

00618                                                              {
00619   int i;
00620   char buf[LINESIZE];
00621   FILE *infile;
00622   
00623   /* open up the coordinate file */
00624   infile=Fopen(filename, "r");
00625   if (infile == NULL)
00626     NAMD_err("Can't open GROMACS coordinate file!");
00627 
00628   fgets(buf,LINESIZE-1,infile); // get the title
00629   // if(strcmp(buf,topology->getSystemName()) != 0)
00630   //   NAMD_die("System names in topology and coordinate files differ.");
00631 
00632   fgets(buf,LINESIZE-1,infile); // get the number of atoms
00633   sscanf(buf,"%d",&atomCount);
00634   if (atomCount != topology->getNumAtoms())
00635     NAMD_die("Num of atoms in coordinate file is different from that in topology file!");
00636 
00637   /* read in the atoms */
00638 #ifdef MEM_OPT_VERSION
00639   atomArray = new PDBCoreData[atomCount];
00640 #else
00641   atomArray = new PDBAtomPtr[atomCount];
00642 #endif
00643   if ( atomArray == NULL )
00644     NAMD_die("memory allocation failed in PDB::PDB");
00645 
00646 #ifdef MEM_OPT_VERSION
00647   for(i=0; i<atomCount; i++){
00648       fgets(buf,LINESIZE-1,infile); // get a line
00649       char *buf2 = buf+20; // skip three fields to get to the coordinates
00650       BigReal coor[3];
00651       if(3 != sscanf(buf2,"%lf%lf%lf", &coor[0],&coor[1],&coor[2]))
00652           NAMD_die("Couldn't get three coordinates from file.");
00653 
00654       coor[0] *= 10; // convert to angstroms from nanometers
00655       coor[1] *= 10;
00656       coor[2] *= 10;
00657 
00658       atomArray[i].coor[0] = coor[0];
00659       atomArray[i].coor[1] = coor[1];
00660       atomArray[i].coor[2] = coor[2];
00661       atomArray[i].myoccupancy = PDBAtom::default_occupancy;
00662       atomArray[i].tempfactor = PDBAtom::default_temperaturefactor;
00663   }
00664 #else
00665   for (i=0;i<atomCount;i++) {
00666     char *buf2, resname[11], atomname[11], atmtype[11];
00667     int resnum, typenum;
00668     Real charge,mass;
00669     BigReal coor[3];
00670     PDBAtom *pdb = new PDBAtomRecord("");  
00671     
00672     fgets(buf,LINESIZE-1,infile); // get a line
00673     buf2 = buf+20; // skip three fields to get to the coordinates
00674     if(3 != sscanf(buf2,"%lf%lf%lf",
00675                    &coor[0],&coor[1],&coor[2]))
00676       NAMD_die("Couldn't get three coordinates from file.");
00677     topology->getAtom(i,&resnum,resname,
00678                       atomname,atmtype,&typenum,&charge,&mass);
00679     coor[0] *= 10; // convert to angstroms from nanometers
00680     coor[1] *= 10;
00681     coor[2] *= 10;
00682     
00683     pdb->name(atomname);
00684     pdb->residuename(resname);
00685     pdb->serialnumber(i+1);
00686     pdb->residueseq(resnum+1);
00687     pdb->coordinates(coor);
00688     
00689     atomArray[i] = pdb;  // Include the new record into the array
00690   }
00691 #endif
00692 }

PDB::~PDB void   ) 
 

Definition at line 238 of file PDB.C.

00239 {
00240 #ifndef MEM_OPT_VERSION
00241         int i;
00242         for (i=atomCount-1; i>=0; i--)
00243            delete atomArray[i];
00244 #endif
00245         delete [] atomArray;
00246         atomArray = NULL;
00247         atomCount = 0;
00248 }


Member Function Documentation

PDBAtom * PDB::atom int  place  ) 
 

Definition at line 384 of file PDB.C.

Referenced by Molecule::build_gridforce_params(), find_extremes(), colvarproxy_namd::load_atoms(), and colvarproxy_namd::load_coords().

00385 {
00386   if (place <0 || place >= atomCount)
00387     return NULL;
00388   return atomArray[place];
00389 }

PDBAtomList* PDB::atoms void   )  [inline]
 

Definition at line 87 of file PDB.h.

References PDBAtomList.

00087 { return atomListHead; }  

void PDB::find_extremes const Lattice ,
BigReal  frac = 1.0
 

Definition at line 428 of file PDB.C.

References Lattice::a_r(), atom(), Lattice::b_r(), BigReal, Lattice::c_r(), Lattice::origin(), PDBAtomPtr, ResizeArray< Elem >::resize(), Vector::x, PDBAtom::xcoor(), Vector::y, PDBAtom::ycoor(), Vector::z, and PDBAtom::zcoor().

Referenced by WorkDistrib::patchMapInit().

00428                                                             {
00429   if (atomCount == 0) {
00430     smin = smax = 0;
00431   }
00432   else if (frac < 1.0) {
00433     // for now use the previous "sort the array" approach
00434     // for solving "frac"th largest and smallest selection problems
00435     SortableResizeArray<BigReal> coor;
00436     coor.resize(atomCount);  // allocate array space once
00437     find_extremes_helper(coor, smin.x, smax.x, lattice.a_r(), frac);
00438     find_extremes_helper(coor, smin.y, smax.y, lattice.b_r(), frac);
00439     find_extremes_helper(coor, smin.z, smax.z, lattice.c_r(), frac);
00440   }
00441   else {
00442     // finding absolute min and max does not require sorting
00443 #ifdef MEM_OPT_VERSION
00444     PDBCoreData *atomptr = atomArray;
00445     Vector p(atomptr->xcoor(),atomptr->ycoor(),atomptr->zcoor());
00446 #else
00447     PDBAtomPtr *atomptr = atomArray;
00448     PDBAtom *atom = *atomptr;
00449     Vector p(atom->xcoor(),atom->ycoor(),atom->zcoor());
00450 #endif
00451     Vector s(lattice.a_r()*p, lattice.b_r()*p, lattice.c_r()*p);
00452     smin = smax = s;
00453     atomptr++;
00454     for(int i=1; i<atomCount; i++, atomptr++){
00455 #ifdef MEM_OPT_VERSION
00456       p = Vector(atomptr->xcoor(),atomptr->ycoor(),atomptr->zcoor());
00457 #else
00458       atom = *atomptr;
00459       p = Vector (atom->xcoor(),atom->ycoor(),atom->zcoor());
00460 #endif
00461       s = Vector(lattice.a_r()*p, lattice.b_r()*p, lattice.c_r()*p);
00462       if      (smin.x > s.x) smin.x = s.x;
00463       else if (smax.x < s.x) smax.x = s.x;
00464       if      (smin.y > s.y) smin.y = s.y;
00465       else if (smax.y < s.y) smax.y = s.y;
00466       if      (smin.z > s.z) smin.z = s.z;
00467       else if (smax.z < s.z) smax.z = s.z;
00468     }
00469   }
00470   // shift using the origin
00471   BigReal origin_shift;
00472   origin_shift = lattice.a_r() * lattice.origin();
00473   smin.x -= origin_shift;
00474   smax.x -= origin_shift;
00475   origin_shift = lattice.b_r() * lattice.origin();
00476   smin.y -= origin_shift;
00477   smax.y -= origin_shift;
00478   origin_shift = lattice.c_r() * lattice.origin();
00479   smin.z -= origin_shift;
00480   smax.z -= origin_shift;
00481 }

void PDB::get_all_positions Vector  ) 
 

Definition at line 356 of file PDB.C.

References PDBAtomPtr, Vector::x, Vector::y, and Vector::z.

Referenced by WorkDistrib::createAtomLists().

00357 {
00358 #ifdef MEM_OPT_VERSION
00359     for(int i=0; i<atomCount; i++){
00360         pos[i].x = atomArray[i].coor[0];
00361         pos[i].y = atomArray[i].coor[1];
00362         pos[i].z = atomArray[i].coor[2];        
00363     }
00364 #else
00365         int i;
00366         PDBAtomPtr *atomptr;
00367 
00368         for (i=0, atomptr=atomArray; i<atomCount; atomptr++, i++)
00369         {
00370                 pos[i].x = (*atomptr)->xcoor();
00371                 pos[i].y = (*atomptr)->ycoor();
00372                 pos[i].z = (*atomptr)->zcoor();
00373         }
00374 #endif
00375 }

void PDB::get_extremes ScaledPosition xmin,
ScaledPosition xmax
const [inline]
 

Definition at line 99 of file PDB.h.

References ScaledPosition.

Referenced by WorkDistrib::patchMapInit().

00099                                                                         {
00100       xmin = smin;  xmax = smax;
00101     }

void PDB::get_position_for_atom Vector ,
int 
 

Definition at line 342 of file PDB.C.

References PDBAtomPtr, Vector::x, Vector::y, and Vector::z.

00342                                                    {
00343 #ifdef MEM_OPT_VERSION
00344     pos->x = atomArray[aid].coor[0];
00345     pos->y = atomArray[aid].coor[1];
00346     pos->z = atomArray[aid].coor[2];
00347 #else
00348     PDBAtomPtr *atomptr = &atomArray[aid];
00349     pos->x = (*atomptr)->xcoor();
00350     pos->y = (*atomptr)->ycoor();
00351     pos->z = (*atomptr)->zcoor();
00352 #endif
00353 }

int PDB::num_atoms void   ) 
 

Definition at line 313 of file PDB.C.

Referenced by Molecule::build_gridforce_params(), NamdState::configListInit(), WorkDistrib::createAtomLists(), colvarproxy_namd::load_atoms(), colvarproxy_namd::load_coords(), WorkDistrib::patchMapInit(), and read_binary_coors().

00314 {
00315   return atomCount;
00316 }

void PDB::set_all_positions Vector  ) 
 

Definition at line 321 of file PDB.C.

References PDBAtomPtr, Vector::x, Vector::y, and Vector::z.

Referenced by read_binary_coors().

00322 {
00323 #ifdef MEM_OPT_VERSION
00324     for(int i=0; i<atomCount; i++){
00325         atomArray[i].coor[0] = pos[i].x;
00326         atomArray[i].coor[1] = pos[i].y;
00327         atomArray[i].coor[2] = pos[i].z;
00328     }
00329 #else
00330         int i;
00331         PDBAtomPtr *atomptr;
00332 
00333         for (i=0, atomptr=atomArray; i<atomCount; atomptr++, i++)
00334         {
00335                 (*atomptr)->xcoor(pos[i].x);
00336                 (*atomptr)->ycoor(pos[i].y);
00337                 (*atomptr)->zcoor(pos[i].z);
00338         }
00339 #endif
00340 }

void PDB::write const char *  outfilename,
const char *  commentline = NULL
 

Definition at line 251 of file PDB.C.

References NAMD_err(), and PDBAtom::sprint().

00252 {
00253         int i;
00254         char s[200];
00255         FILE *outfile;
00256         if ((outfile = fopen(outfilename, "w")) == NULL) {
00257            sprintf(s, "Cannot open file '%s' in PDB::write.", outfilename);
00258            NAMD_err(s);
00259         }
00260 
00261         if (commentline != NULL)
00262         {
00263                 sprintf(s, "REMARK  %s\n", commentline);
00264                 if (fputs(s, outfile) == EOF)
00265                 {
00266                         NAMD_err("EOF in PDB::write writing the comment line - file system full?");
00267                 }
00268         }
00269 
00270         for (i=0; i<atomCount; i++){ // I only contain ATOM/HETATM records
00271 #ifdef MEM_OPT_VERSION    
00272       atomArray[i].sprint(s, PDBData::COLUMNS);  
00273 #else
00274           atomArray[i]->sprint(s, PDBData::COLUMNS);
00275 #endif
00276           if ( (fputs(s, outfile)    == EOF) || 
00277                (fputs("\n", outfile) == EOF)    ) {
00278             sprintf(s, "EOF in PDB::write line %d - file system full?", i);
00279             NAMD_err(s);
00280           }
00281         }
00282         if (fputs("END\n", outfile) == EOF) {
00283            NAMD_err("EOF in PDB::write while printing 'END' -- file system full?");
00284         }
00285         if (fclose(outfile) == EOF) {
00286            NAMD_err("EOF in PDB::write while closing -- file system full?");
00287         }
00288           
00289 }


The documentation for this class was generated from the following files:
Generated on Sun Feb 12 04:08:04 2012 for NAMD by  doxygen 1.3.9.1