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 (BigReal *min, BigReal *max, Vector rec, BigReal frac=1.0) 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 485 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().

00486 { int i,j,k;
00487   BigReal coor[3];
00488   char buf[13],resname[5],atomname[5];
00489   FILE *infile;
00490   PDBAtom *pdb;
00491 
00492   if ((infile=Fopen(filename, "r")) == NULL)
00493     NAMD_err("Can't open AMBER coordinate file!");
00494 
00495   readtoeoln(infile);  // Skip the first line (title)
00496 
00497   fscanf(infile,"%d",&atomCount);  // Read in num of atoms
00498   if (atomCount != amber_data->Natom)
00499     NAMD_die("Num of atoms in coordinate file is different from that in parm file!");
00500   readtoeoln(infile);
00501 
00502 #ifdef MEM_OPT_VERSION
00503   atomArray = new PDBCoreData[atomCount];
00504 #else
00505   atomArray = new PDBAtomPtr[atomCount];
00506 #endif
00507 
00508   if ( atomArray == NULL )
00509   {
00510     NAMD_die("memory allocation failed in PDB::PDB");
00511   }
00512   
00513   // Read in the coordinates, which are in the format of 6F12.7
00514   // Other fields are copied from "amber_data"
00515   for (i=0; i<atomCount; ++i)
00516   { // Read x,y,z coordinates
00517     for (j=0; j<3; ++j)
00518     { for (k=0; k<12; ++k)
00519       { buf[k]=getc(infile);
00520         if (buf[k]=='\n' || buf[k]=='\0' || buf[k]==EOF)
00521           NAMD_die("Error reading AMBER coordinate file!");
00522       }
00523       buf[12] = '\0';
00524       coor[j] = atof(buf);
00525     }
00526     if (i%2 == 1)
00527       readtoeoln(infile);
00528 #ifdef MEM_OPT_VERSION
00529     atomArray[i].coor[0] = coor[0];
00530     atomArray[i].coor[1] = coor[1];
00531     atomArray[i].coor[2] = coor[2];
00532     atomArray[i].myoccupancy = PDBAtom::default_occupancy;
00533     atomArray[i].tempfactor = PDBAtom::default_temperaturefactor;
00534 #else
00535     // Copy name, resname and resid from "amber_data"
00536     for (j=0; j<4; ++j)
00537     { resname[j] = amber_data->ResNames[amber_data->AtomRes[i]*4+j];
00538       atomname[j] = amber_data->AtomNames[i*4+j];
00539     }
00540     resname[4] = atomname[4] = '\0';
00541     // Create a new PDB record, and fill in its entries
00542     pdb = new PDBAtomRecord("");
00543     pdb->name(atomname);
00544     pdb->residuename(resname);
00545     pdb->serialnumber(i+1);
00546     pdb->residueseq(amber_data->AtomRes[i]+1);
00547     pdb->coordinates(coor);
00548     atomArray[i] = pdb;  // Include the new record into the array
00549 #endif
00550   }
00551 }

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

Definition at line 559 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().

00559                                                              {
00560   int i;
00561   char buf[LINESIZE];
00562   FILE *infile;
00563   
00564   /* open up the coordinate file */
00565   infile=Fopen(filename, "r");
00566   if (infile == NULL)
00567     NAMD_err("Can't open GROMACS coordinate file!");
00568 
00569   fgets(buf,LINESIZE-1,infile); // get the title
00570   // if(strcmp(buf,topology->getSystemName()) != 0)
00571   //   NAMD_die("System names in topology and coordinate files differ.");
00572 
00573   fgets(buf,LINESIZE-1,infile); // get the number of atoms
00574   sscanf(buf,"%d",&atomCount);
00575   if (atomCount != topology->getNumAtoms())
00576     NAMD_die("Num of atoms in coordinate file is different from that in topology file!");
00577 
00578   /* read in the atoms */
00579 #ifdef MEM_OPT_VERSION
00580   atomArray = new PDBCoreData[atomCount];
00581 #else
00582   atomArray = new PDBAtomPtr[atomCount];
00583 #endif
00584   if ( atomArray == NULL )
00585     NAMD_die("memory allocation failed in PDB::PDB");
00586 
00587 #ifdef MEM_OPT_VERSION
00588   for(i=0; i<atomCount; i++){
00589       fgets(buf,LINESIZE-1,infile); // get a line
00590       char *buf2 = buf+20; // skip three fields to get to the coordinates
00591       BigReal coor[3];
00592       if(3 != sscanf(buf2,"%lf%lf%lf", &coor[0],&coor[1],&coor[2]))
00593           NAMD_die("Couldn't get three coordinates from file.");
00594 
00595       coor[0] *= 10; // convert to angstroms from nanometers
00596       coor[1] *= 10;
00597       coor[2] *= 10;
00598 
00599       atomArray[i].coor[0] = coor[0];
00600       atomArray[i].coor[1] = coor[1];
00601       atomArray[i].coor[2] = coor[2];
00602       atomArray[i].myoccupancy = PDBAtom::default_occupancy;
00603       atomArray[i].tempfactor = PDBAtom::default_temperaturefactor;
00604   }
00605 #else
00606   for (i=0;i<atomCount;i++) {
00607     char *buf2, resname[11], atomname[11], atmtype[11];
00608     int resnum, typenum;
00609     Real charge,mass;
00610     BigReal coor[3];
00611     PDBAtom *pdb = new PDBAtomRecord("");  
00612     
00613     fgets(buf,LINESIZE-1,infile); // get a line
00614     buf2 = buf+20; // skip three fields to get to the coordinates
00615     if(3 != sscanf(buf2,"%lf%lf%lf",
00616                    &coor[0],&coor[1],&coor[2]))
00617       NAMD_die("Couldn't get three coordinates from file.");
00618     topology->getAtom(i,&resnum,resname,
00619                       atomname,atmtype,&typenum,&charge,&mass);
00620     coor[0] *= 10; // convert to angstroms from nanometers
00621     coor[1] *= 10;
00622     coor[2] *= 10;
00623     
00624     pdb->name(atomname);
00625     pdb->residuename(resname);
00626     pdb->serialnumber(i+1);
00627     pdb->residueseq(resnum+1);
00628     pdb->coordinates(coor);
00629     
00630     atomArray[i] = pdb;  // Include the new record into the array
00631   }
00632 #endif
00633 }

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 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 80 of file PDB.h.

References PDBAtomList.

00080 { return atomListHead; }  

void PDB::find_extremes BigReal min,
BigReal max,
Vector  rec,
BigReal  frac = 1.0
const
 

Definition at line 394 of file PDB.C.

References atom(), ResizeArray< Elem >::begin(), BigReal, PDBAtomPtr, ResizeArray< Elem >::resize(), SortableResizeArray< Elem >::sort(), PDBAtom::xcoor(), PDBAtom::ycoor(), and PDBAtom::zcoor().

Referenced by WorkDistrib::patchMapInit().

00395 {
00396     SortableResizeArray<BigReal> coor;
00397     coor.resize(atomCount);
00398     SortableResizeArray<BigReal>::iterator c_i = coor.begin();
00399 #ifdef MEM_OPT_VERSION
00400     PDBCoreData *atomptr = atomArray;
00401     for(int i=0; i<atomCount; i++, atomptr++){
00402         Vector pos(atomptr->xcoor(),atomptr->ycoor(),atomptr->zcoor());
00403         c_i[i] = rec*pos;
00404     }
00405 #else
00406     PDBAtomPtr *atomptr = atomArray;
00407     for (int i=0; i<atomCount; ++i, ++atomptr) {
00408       PDBAtom *atom = *atomptr;
00409       Vector pos(atom->xcoor(),atom->ycoor(),atom->zcoor());
00410       c_i[i] = rec*pos;
00411     }
00412 #endif
00413     coor.sort();
00414     int ilow = (int)((1.0 - frac) * atomCount);
00415     if ( ilow < 0 ) ilow = 0;
00416     if ( ilow > atomCount/2 ) ilow = atomCount/2;
00417     int ihigh = atomCount - ilow - 1;
00418     BigReal span = coor[ihigh] - coor[ilow];
00419     BigReal extension = (1.0 - frac) * span / (2.0 * frac - 1.0);
00420     *max = coor[ihigh] + extension;
00421     *min = coor[ilow] - extension;
00422 }

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_position_for_atom Vector ,
int 
 

Definition at line 342 of file PDB.C.

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

Referenced by WorkDistrib::caclNumAtomsInEachPatch(), and WorkDistrib::fillOnePatchAtoms().

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 WorkDistrib::caclNumAtomsInEachPatch(), NamdState::configListInit(), WorkDistrib::createAtomLists(), WorkDistrib::initAndSendHomePatch(), 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 Mon Nov 23 05:00:05 2009 for NAMD by  doxygen 1.3.9.1