24 #ifdef MEM_OPT_VERSION 26 PDB::PDB(
const char *pdbfilename,
int expectedNumAtoms){
31 atomArray =
new PDBCoreData[expectedNumAtoms];
34 atomListHead = atomListTail = NULL;
35 infile =
Fopen(pdbfilename,
"r");
38 sprintf(s,
"Cannot open file '%s' for input in PDB::PDB.", pdbfilename);
43 while ( fgets(buf, 150, infile) ) {
46 for (s=buf; *s && *s!=
'\n'; s++)
49 if ( s == (buf + 149) ) {
51 sprintf( s,
"Line too long in pdbfile %s:\n%s\n", pdbfilename, buf);
59 NAMD_die(
"Could not allocate PDBData.\n");
69 atomArray[atomCount].coor[0] = coor[0];
70 atomArray[atomCount].coor[1] = coor[1];
71 atomArray[atomCount].coor[2] = coor[2];
72 atomArray[atomCount].myoccupancy = thisAtom->
occupancy();
76 if(atomCount > expectedNumAtoms)
NAMD_die(
"Number of pdb and psf atoms are not the same!");
83 PDB::PDB(molfile_plugin_t *pIOHdl,
void *pIOFileHdl,
int numAtoms,
const float *occupancy,
const float *bfactor){
84 #ifdef MEM_OPT_VERSION 85 NAMD_die(
"Sorry, plugin IO is not supported in the memory optimized version.");
87 molfile_timestep_t ts;
89 memset(&ts, 0,
sizeof(molfile_timestep_t));
92 ts.A = ts.B = ts.C = 0.0f;
93 ts.alpha = ts.beta = ts.gamma = 90.0f;
97 atomcoords = (
float *) malloc(3*numAtoms*
sizeof(
float));
98 memset(atomcoords, 0, 3*numAtoms*
sizeof(
float));
99 ts.coords = atomcoords;
101 if (pIOHdl->read_next_timestep(pIOFileHdl, numAtoms, &ts)) {
103 pIOHdl->close_file_read(pIOFileHdl);
104 NAMD_die(
"ERROR: failed reading atom coordinates");
112 for(
int i=0; i<numAtoms; i++) {
113 PDBAtom *thisAtom = tmpAtoms+i;
114 atomArray[i] = thisAtom;
116 tmpCoords[0] = atomcoords[0];
117 tmpCoords[1] = atomcoords[1];
118 tmpCoords[2] = atomcoords[2];
125 for(
int i=0; i<numAtoms; i++) {
126 PDBAtom *thisAtom = tmpAtoms+i;
131 for(
int i=0; i<numAtoms; i++) {
132 PDBAtom *thisAtom = tmpAtoms+i;
146 atomListHead = atomListTail = NULL;
147 infile =
Fopen(pdbfilename,
"r");
150 sprintf(s,
"Cannot open file '%s' for input in PDB::PDB.", pdbfilename);
155 while ( fgets(buf, 150, infile) ) {
158 for (s=buf; *s && *s!=
'\n'; s++)
161 if ( s == (buf + 149) ) {
163 sprintf( s,
"Line too long in pdbfile %s:\n%s\n", pdbfilename, buf);
171 NAMD_die(
"Could not allocate PDBData.\n");
179 add_atom_element( (
PDBAtom *) newelement);
189 #ifdef MEM_OPT_VERSION 191 altlocArray =
new char[atomCount];
192 atomArray =
new PDBCoreData[atomCount];
193 if(atomArray == NULL)
194 NAMD_die(
"memory allocation failed in PDB::PDB");
197 for(
int i=0; tmp!=NULL; tmp=tmp->
next, i++){
199 atomArray[i].coor[0] = coor[0];
200 atomArray[i].coor[1] = coor[1];
201 atomArray[i].coor[2] = coor[2];
215 atomListHead = atomListTail = NULL;
219 if ( atomArray == NULL )
221 NAMD_die(
"memory allocation failed in PDB::PDB");
225 for (i=0, tmp = atomListHead; tmp != NULL; tmp = tmp ->
next, i++) {
226 atomArray[i] = tmp -> data;
230 for (tmp2 = tmp = atomListHead; tmp != NULL; tmp = tmp2) {
234 atomListHead = atomListTail = NULL;
244 #ifndef MEM_OPT_VERSION 249 for (i=atomCount-1; i>=0; i--)
253 delete [] altlocArray;
261 void PDB::write(
const char *outfilename,
const char *commentline)
266 if ((outfile = fopen(outfilename,
"w")) == NULL) {
267 sprintf(s,
"Cannot open file '%s' in PDB::write.", outfilename);
271 if (commentline != NULL)
273 sprintf(s,
"REMARK %s\n", commentline);
274 if (fputs(s, outfile) == EOF)
276 NAMD_err(
"EOF in PDB::write writing the comment line - file system full?");
280 for (i=0; i<atomCount; i++){
281 #ifdef MEM_OPT_VERSION 286 if ( (fputs(s, outfile) == EOF) ||
287 (fputs(
"\n", outfile) == EOF) ) {
288 sprintf(s,
"EOF in PDB::write line %d - file system full?", i);
292 if (fputs(
"END\n", outfile) == EOF) {
293 NAMD_err(
"EOF in PDB::write while printing 'END' -- file system full?");
295 if (fclose(outfile) == EOF) {
296 NAMD_err(
"EOF in PDB::write while closing -- file system full?");
302 void PDB::add_atom_element(
PDBAtom *newAtom)
307 NAMD_die(
"memory allocation failed in PDB::add_atom_element");
309 tmp -> data = newAtom;
312 if (atomListHead == NULL) {
313 atomListHead = atomListTail = tmp;
315 atomListTail ->
next = tmp;
333 #ifdef MEM_OPT_VERSION 334 for(
int i=0; i<atomCount; i++){
335 atomArray[i].coor[0] = pos[i].
x;
336 atomArray[i].coor[1] = pos[i].
y;
337 atomArray[i].coor[2] = pos[i].
z;
342 for (i=0, atomptr=atomArray; i<atomCount; atomptr++, i++)
344 (*atomptr)->
xcoor(pos[i].x);
345 (*atomptr)->ycoor(pos[i].y);
346 (*atomptr)->zcoor(pos[i].z);
352 #ifdef MEM_OPT_VERSION 353 pos->
x = atomArray[aid].coor[0];
354 pos->
y = atomArray[aid].coor[1];
355 pos->
z = atomArray[aid].coor[2];
358 pos->
x = (*atomptr)->xcoor();
359 pos->
y = (*atomptr)->ycoor();
360 pos->
z = (*atomptr)->zcoor();
367 #ifdef MEM_OPT_VERSION 368 for(
int i=0; i<atomCount; i++){
369 pos[i].
x = atomArray[i].coor[0];
370 pos[i].
y = atomArray[i].coor[1];
371 pos[i].
z = atomArray[i].coor[2];
377 for (i=0, atomptr=atomArray; i<atomCount; atomptr++, i++)
379 pos[i].
x = (*atomptr)->xcoor();
380 pos[i].
y = (*atomptr)->ycoor();
381 pos[i].
z = (*atomptr)->zcoor();
387 #ifdef MEM_OPT_VERSION 389 if(place<0 || place>=atomCount)
return NULL;
390 return &atomArray[place];
395 if (place <0 || place >= atomCount)
397 return atomArray[place];
404 void PDB::find_extremes_helper(
410 #ifdef MEM_OPT_VERSION 411 PDBCoreData *atomptr = atomArray;
412 for(
int i=0; i<atomCount; i++, atomptr++){
413 Vector pos(atomptr->xcoor(),atomptr->ycoor(),atomptr->zcoor());
418 for (
int i=0; i<atomCount; ++i, ++atomptr) {
425 int ilow = (int)((1.0 - frac) * atomCount);
426 if ( ilow < 0 ) ilow = 0;
427 if ( ilow > atomCount/2 ) ilow = atomCount/2;
428 int ihigh = atomCount - ilow - 1;
429 BigReal span = coor[ihigh] - coor[ilow];
430 BigReal extension = (1.0 - frac) * span / (2.0 * frac - 1.0);
431 max = coor[ihigh] + extension;
432 min = coor[ilow] - extension;
438 if (atomCount == 0) {
441 else if (frac < 1.0) {
446 find_extremes_helper(coor, smin.
x, smax.
x, lattice.
a_r(), frac);
447 find_extremes_helper(coor, smin.
y, smax.
y, lattice.
b_r(), frac);
448 find_extremes_helper(coor, smin.
z, smax.
z, lattice.
c_r(), frac);
452 #ifdef MEM_OPT_VERSION 453 PDBCoreData *atomptr = atomArray;
454 Vector p(atomptr->xcoor(),atomptr->ycoor(),atomptr->zcoor());
463 for(
int i=1; i<atomCount; i++, atomptr++){
464 #ifdef MEM_OPT_VERSION 465 p =
Vector(atomptr->xcoor(),atomptr->ycoor(),atomptr->zcoor());
471 if (smin.x > s.
x) smin.
x = s.
x;
472 else if (smax.
x < s.
x) smax.
x = s.
x;
473 if (smin.y > s.
y) smin.y = s.
y;
474 else if (smax.
y < s.
y) smax.
y = s.
y;
475 if (smin.z > s.
z) smin.z = s.
z;
476 else if (smax.
z < s.
z) smax.
z = s.
z;
481 origin_shift = lattice.
a_r() * lattice.
origin();
482 smin.
x -= origin_shift;
483 smax.
x -= origin_shift;
484 origin_shift = lattice.
b_r() * lattice.
origin();
485 smin.
y -= origin_shift;
486 smax.
y -= origin_shift;
487 origin_shift = lattice.
c_r() * lattice.
origin();
488 smin.
z -= origin_shift;
489 smax.
z -= origin_shift;
493 #ifdef TEST_PDB_CLASS 497 PDB *pdb =
new PDB(
"pti.pdb");
498 if ( atomArray == NULL )
500 NAMD_die(
"memory allocation failed in main of test PDB class");
502 ilist = pdb->find_atom_name(
"CA");
504 printf(
"None found.\n");
508 for (i=0; i<ilist->num(); i++) {
509 pdb->
atom((*ilist)[i]) -> sprint(s);
515 printf(
"Now, search through space.\n");
517 ilist = pdb->find_atoms_in_region(4.38, 19.5, 3.0,
520 printf(
"None found.\n");
524 printf(
"%d found\n", ilist -> num());
525 for (i=0; i<ilist->num(); i++) {
526 pdb->
atom((*ilist)[i]) -> sprint(s);
532 #endif // TEST_PDB_CLASS 542 while((c = getc(f)) !=
'\n') {
556 char buf[13],resname[5],atomname[5];
560 if ((infile=
Fopen(filename,
"r")) == NULL)
561 NAMD_err(
"Can't open AMBER coordinate file!");
565 fscanf(infile,
"%d",&atomCount);
566 if (atomCount != amber_data->
Natom)
567 NAMD_die(
"Num of atoms in coordinate file is different from that in parm file!");
570 #ifdef MEM_OPT_VERSION 572 atomArray =
new PDBCoreData[atomCount];
578 if ( atomArray == NULL )
580 NAMD_die(
"memory allocation failed in PDB::PDB");
585 for (i=0; i<atomCount; ++i)
588 {
for (k=0; k<12; ++k)
589 { buf[k]=getc(infile);
590 if (buf[k]==
'\n' || buf[k]==
'\0' || buf[k]==EOF)
591 NAMD_die(
"Error reading AMBER coordinate file!");
598 #ifdef MEM_OPT_VERSION 599 atomArray[i].coor[0] = coor[0];
600 atomArray[i].coor[1] = coor[1];
601 atomArray[i].coor[2] = coor[2];
608 atomname[j] = amber_data->
AtomNames[i*4+j];
610 resname[4] = atomname[4] =
'\0';
627 char buf[13],resname[5],atomname[5];
631 if ((infile=
Fopen(filename,
"r")) == NULL)
632 NAMD_err(
"Can't open AMBER coordinate file!");
636 fscanf(infile,
"%d",&atomCount);
637 if (atomCount != amber_data->
Natom)
638 NAMD_die(
"Num of atoms in coordinate file is different from that in parm file!");
641 #ifdef MEM_OPT_VERSION 643 atomArray =
new PDBCoreData[atomCount];
649 if ( atomArray == NULL )
651 NAMD_die(
"memory allocation failed in PDB::PDB");
656 for (i=0; i<atomCount; ++i)
659 {
for (k=0; k<12; ++k)
660 { buf[k]=getc(infile);
661 if (buf[k]==
'\n' || buf[k]==
'\0' || buf[k]==EOF)
662 NAMD_die(
"Error reading AMBER coordinate file!");
669 #ifdef MEM_OPT_VERSION 670 atomArray[i].coor[0] = coor[0];
671 atomArray[i].coor[1] = coor[1];
672 atomArray[i].coor[2] = coor[2];
677 strncpy(resname, amber_data->
ResNames[amber_data->
AtomRes[i]].c_str(), 5);
678 strncpy(atomname, amber_data->
AtomNames[i].c_str(), 5);
682 pdb->residuename(resname);
683 pdb->serialnumber(i+1);
684 pdb->residueseq(amber_data->
AtomRes[i]+1);
685 pdb->coordinates(coor);
703 infile=
Fopen(filename,
"r");
705 NAMD_err(
"Can't open GROMACS coordinate file!");
712 sscanf(buf,
"%d",&atomCount);
714 NAMD_die(
"Num of atoms in coordinate file is different from that in topology file!");
717 #ifdef MEM_OPT_VERSION 719 atomArray =
new PDBCoreData[atomCount];
724 if ( atomArray == NULL )
725 NAMD_die(
"memory allocation failed in PDB::PDB");
727 #ifdef MEM_OPT_VERSION 728 for(i=0; i<atomCount; i++){
732 if(3 != sscanf(buf2,
"%lf%lf%lf", &coor[0],&coor[1],&coor[2]))
733 NAMD_die(
"Couldn't get three coordinates from file.");
739 atomArray[i].coor[0] = coor[0];
740 atomArray[i].coor[1] = coor[1];
741 atomArray[i].coor[2] = coor[2];
746 for (i=0;i<atomCount;i++) {
747 char *buf2, resname[11], atomname[11], atmtype[11];
755 if(3 != sscanf(buf2,
"%lf%lf%lf",
756 &coor[0],&coor[1],&coor[2]))
757 NAMD_die(
"Couldn't get three coordinates from file.");
758 topology->
getAtom(i,&resnum,resname,
759 atomname,atmtype,&typenum,&charge,&mass);
775 #ifdef MEM_OPT_VERSION 779 for(
int i=0; i<79; i++) outstr[i] = 32;
790 sprintf(tmpstr,
" #");
793 strcat(outstr, tmpstr);
795 sprintf(tmpstr,
" #");
798 strcat(outstr, tmpstr);
800 sprintf(tmpstr,
" #");
803 strcat(outstr, tmpstr);
806 strcat(outstr, tmpstr);
809 strcat(outstr, tmpstr);
static const BigReal default_coor
void getAtom(int num, int *residue_number, char *residue_name, char *atom_name, char *atom_type, int *atom_typenum, Real *charge, Real *mass) const
void NAMD_err(const char *err_msg)
static const BigReal default_temperaturefactor
void get_position_for_atom(Vector *, int)
vector< string > ResNames
BigReal temperaturefactor(void)
void write(const char *outfilename, const char *commentline=NULL)
void set_all_positions(Vector *)
static int readtoeoln(FILE *f)
const char * residuename(void)
static Units next(Units u)
static void sprintcol(char *s, int start, int len, const char *val)
PDBAtom * atom(int place)
const BigReal * coordinates(void)
FILE * Fopen(const char *filename, const char *mode)
const char * alternatelocation(void)
NAMD_HOST_DEVICE Vector a_r() const
NAMD_HOST_DEVICE Vector b_r() const
void NAMD_die(const char *err_msg)
NAMD_HOST_DEVICE Vector c_r() const
PDB(const char *pdbfilename)
vector< string > AtomNames
void find_extremes(const Lattice &, BigReal frac=1.0)
int main(int argc, char *argv[])
PDBData * new_PDBData(const char *data)
void get_all_positions(Vector *)
void sprint(char *s, PDBFormatStyle usestyle=COLUMNS)
NAMD_HOST_DEVICE Vector origin() const
static const BigReal default_occupancy