• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

src/generic/valist.c

Go to the documentation of this file.
00001 
00048 #include "apbscfg.h"
00049 #include "apbs/valist.h"
00050 
00051 VEMBED(rcsid="$Id: valist.c 1613 2010-10-19 14:58:22Z sobolevnrm $")
00052 
00053 VPRIVATE char *Valist_whiteChars = " \t\r\n";
00054 VPRIVATE char *Valist_commChars  = "#%";
00055 VPRIVATE char *Valist_xmlwhiteChars = " \t\r\n<>";
00056 
00057 #if !defined(VINLINE_VATOM)
00058 
00059 VPUBLIC double Valist_getCenterX(Valist *thee) {
00060  
00061     if (thee == NULL) {
00062   Vnm_print(2, "Valist_getCenterX:  Found null pointer when getting the center of X coordinate!\n");
00063   VASSERT(0);
00064  }
00065     return thee->center[0];
00066 
00067 }
00068 
00069 VPUBLIC double Valist_getCenterY(Valist *thee) {
00070 
00071     if (thee == NULL) {
00072   Vnm_print(2, "Valist_getCenterY:  Found null pointer when getting the center of Y coordinate!\n");
00073   VASSERT(0);
00074  }
00075     return thee->center[1];
00076 
00077 }
00078 VPUBLIC double Valist_getCenterZ(Valist *thee) {
00079 
00080     if (thee == NULL) {
00081   Vnm_print(2, "Valist_getCenterZ:  Found null pointer when getting the center of Z coordinate!\n");
00082   VASSERT(0);
00083  }
00084     return thee->center[2];
00085 
00086 }
00087 
00088 VPUBLIC Vatom* Valist_getAtomList(Valist *thee) {
00089 
00090     if (thee == NULL) {
00091   Vnm_print(2, "Valist_getAtomList:  Found null pointer when getting the atom list!\n");
00092   VASSERT(0);
00093  }
00094     return thee->atoms;
00095 
00096 }
00097 
00098 VPUBLIC int Valist_getNumberAtoms(Valist *thee) {
00099 
00100     if (thee == NULL) {
00101   Vnm_print(2, "Valist_getNumberAtoms:  Found null pointer when getting the number of atoms!\n");
00102   VASSERT(0);
00103  }
00104     return thee->number;
00105 
00106 }
00107 
00108 VPUBLIC Vatom* Valist_getAtom(Valist *thee, int i) {
00109 
00110     if (thee == NULL) {
00111   Vnm_print(2, "Valist_getAtom:  Found null pointer when getting atoms!\n");
00112   VASSERT(0);
00113  }
00114     if (i >= thee->number) {
00115   Vnm_print(2, "Valist_getAtom:  Requested atom number (%d) outside of atom list range (%d)!\n", i, thee->number);
00116   VASSERT(0);
00117  }
00118     return &(thee->atoms[i]);
00119 
00120 }
00121 
00122 VPUBLIC unsigned long int Valist_memChk(Valist *thee) {
00123 
00124     if (thee == NULL) return 0;
00125     return Vmem_bytes(thee->vmem);
00126 
00127 }
00128 
00129 #endif /* if !defined(VINLINE_VATOM) */
00130 
00131 VPUBLIC Valist* Valist_ctor() {
00132 
00133     /* Set up the structure */
00134     Valist *thee = VNULL;
00135     thee = Vmem_malloc(VNULL, 1, sizeof(Valist));
00136     if ( thee == VNULL) {
00137   Vnm_print(2, "Valist_ctor:  Got NULL pointer when constructing the atom list object!\n");
00138   VASSERT(0);
00139  }
00140     if ( Valist_ctor2(thee) != VRC_SUCCESS) {
00141   Vnm_print(2, "Valist_ctor:   Error in constructing the atom list object!\n");
00142   VASSERT(0);
00143  }
00144  
00145     return thee;
00146 }
00147 
00148 VPUBLIC Vrc_Codes Valist_ctor2(Valist *thee) {
00149   
00150     thee->atoms = VNULL;
00151     thee->number = 0;
00152 
00153     /* Initialize the memory management object */
00154     thee->vmem = Vmem_ctor("APBS:VALIST");
00155 
00156     return VRC_SUCCESS;    
00157 
00158 }
00159 
00160 VPUBLIC void Valist_dtor(Valist **thee)
00161 {
00162     if ((*thee) != VNULL) {
00163         Valist_dtor2(*thee);
00164         Vmem_free(VNULL, 1, sizeof(Valist), (void **)thee);
00165         (*thee) = VNULL;
00166     }
00167 }
00168 
00169 VPUBLIC void Valist_dtor2(Valist *thee) {
00170 
00171     Vmem_free(thee->vmem, thee->number, sizeof(Vatom), (void **)&(thee->atoms));
00172     thee->atoms = VNULL;
00173     thee->number = 0;
00174 
00175     Vmem_dtor(&(thee->vmem));
00176 } 
00177 
00178 /* Read serial number from PDB ATOM/HETATM field */
00179 VPRIVATE Vrc_Codes Valist_readPDBSerial(Valist *thee, Vio *sock, int *serial) {
00180 
00181     char tok[VMAX_BUFSIZE];
00182     int ti = 0;
00183 
00184     if (Vio_scanf(sock, "%s", tok) != 1) {
00185         Vnm_print(2, "Valist_readPDB:  Ran out of tokens while parsing serial!\n");
00186         return VRC_FAILURE;
00187     } 
00188     if (sscanf(tok, "%d", &ti) != 1) {
00189         Vnm_print(2, "Valist_readPDB:  Unable to parse serial token (%s) as int!\n",
00190                 tok);
00191         return VRC_FAILURE;
00192     } 
00193     *serial = ti;
00194 
00195     return VRC_SUCCESS;
00196 }
00197 
00198 /* Read atom name from PDB ATOM/HETATM field */
00199 VPRIVATE Vrc_Codes Valist_readPDBAtomName(Valist *thee, Vio *sock, 
00200         char atomName[VMAX_ARGLEN]) {
00201 
00202     char tok[VMAX_BUFSIZE];
00203 
00204     if (Vio_scanf(sock, "%s", tok) != 1) {
00205         Vnm_print(2, "Valist_readPDB:  Ran out of tokens while parsing atom name!\n");
00206         return VRC_FAILURE;
00207     }
00208     if (strlen(tok) < VMAX_ARGLEN) strcpy(atomName, tok);
00209     else {
00210         Vnm_print(2, "Valist_readPDB:  Atom name (%s) too long!\n", tok);
00211         return VRC_FAILURE;
00212     }
00213     return VRC_SUCCESS;
00214 }
00215 
00216 /* Read residue name from PDB ATOM/HETATM field */
00217 VPRIVATE Vrc_Codes Valist_readPDBResidueName(Valist *thee, Vio *sock, 
00218         char resName[VMAX_ARGLEN]) {
00219 
00220     char tok[VMAX_BUFSIZE];
00221 
00222     if (Vio_scanf(sock, "%s", tok) != 1) {
00223         Vnm_print(2, "Valist_readPDB:  Ran out of tokens while parsing residue name!\n");
00224         return VRC_FAILURE;
00225     }
00226     if (strlen(tok) < VMAX_ARGLEN) strcpy(resName, tok);
00227     else {
00228         Vnm_print(2, "Valist_readPDB:  Residue name (%s) too long!\n", tok);
00229         return VRC_FAILURE;
00230     }
00231     return VRC_SUCCESS;
00232 }
00233 
00234 /* Read residue number from PDB ATOM/HETATM field */
00235 VPRIVATE Vrc_Codes Valist_readPDBResidueNumber(
00236         Valist *thee, Vio *sock, int *resSeq) {
00237 
00238     char tok[VMAX_BUFSIZE];
00239  char *resstring;
00240     int ti = 0;
00241 
00242     if (Vio_scanf(sock, "%s", tok) != 1) {
00243         Vnm_print(2, "Valist_readPDB:  Ran out of tokens while parsing resSeq!\n");
00244         return VRC_FAILURE;
00245     } 
00246     if (sscanf(tok, "%d", &ti) != 1) {
00247 
00248      /* One of three things can happen here:
00249    1)  There is a chainID in the line:    THR A   1
00250    2)  The chainID is merged with resSeq: THR A1001
00251    3)  An actual error:                   THR foo
00252 
00253   */
00254 
00255   if (strlen(tok) == 1) {
00256    /* Case 1: Chain ID Present 
00257                        Read the next field and hope its a float */
00258 
00259    if (Vio_scanf(sock, "%s", tok) != 1) {
00260           Vnm_print(2, "Valist_readPDB:  Ran out of tokens while parsing resSeq!\n");
00261           return VRC_FAILURE;
00262       }   
00263       if (sscanf(tok, "%d", &ti) != 1) {
00264     Vnm_print(2, "Valist_readPDB:  Unable to parse resSeq token (%s) as int!\n",
00265                 tok);
00266           return VRC_FAILURE;
00267    }
00268     
00269   } else {
00270    /* Case 2: Chain ID, merged string.
00271         Move pointer forward past the chainID and check
00272          */
00273       //strcpy(resstring, tok);
00274    resstring = tok;
00275    resstring++;
00276 
00277    if (sscanf(resstring, "%d", &ti) != 1) {
00278     /* Case 3:  More than one non-numeral char is present. Error.*/
00279                 Vnm_print(2, "Valist_readPDB:  Unable to parse resSeq token (%s) as int!\n",
00280                 resstring);
00281              return VRC_FAILURE;
00282    }
00283   } 
00284     } 
00285     *resSeq = ti;
00286 
00287     return VRC_SUCCESS;
00288 }
00289 
00290 /* Read atom coordinate from PDB ATOM/HETATM field */
00291 VPRIVATE Vrc_Codes Valist_readPDBAtomCoord(Valist *thee, Vio *sock, double *coord) {
00292 
00293     char tok[VMAX_BUFSIZE];
00294     double tf = 0;
00295 
00296     if (Vio_scanf(sock, "%s", tok) != 1) {
00297         Vnm_print(2, "Valist_readPDB:  Ran out of tokens while parsing atom coordinate!\n");
00298         return VRC_FAILURE;
00299     } 
00300     if (sscanf(tok, "%lf", &tf) != 1) {
00301         return VRC_FAILURE;
00302     } 
00303     *coord = tf;
00304 
00305     return VRC_SUCCESS;
00306 }
00307 
00308 /* Read charge and radius from PQR ATOM/HETATM field */
00309 VPRIVATE Vrc_Codes Valist_readPDBChargeRadius(Valist *thee, Vio *sock, 
00310         double *charge, double *radius) {
00311 
00312     char tok[VMAX_BUFSIZE];
00313     double tf = 0;
00314 
00315     if (Vio_scanf(sock, "%s", tok) != 1) {
00316         Vnm_print(2, "Valist_readPQR:  Ran out of tokens while parsing charge!\n");
00317         return VRC_FAILURE;
00318     } 
00319     if (sscanf(tok, "%lf", &tf) != 1) {
00320         return VRC_FAILURE;
00321     } 
00322     *charge = tf;
00323 
00324     if (Vio_scanf(sock, "%s", tok) != 1) {
00325         Vnm_print(2, "Valist_readPQR:  Ran out of tokens while parsing radius!\n");
00326         return VRC_FAILURE;
00327     } 
00328     if (sscanf(tok, "%lf", &tf) != 1) {
00329         return VRC_FAILURE;
00330     } 
00331     *radius = tf;
00332 
00333     return VRC_SUCCESS;
00334 }
00335 
00336 /* Read ATOM/HETATM field of PDB through the X/Y/Z fields */
00337 VPRIVATE Vrc_Codes Valist_readPDB_throughXYZ(
00338         Valist *thee, 
00339         Vio *sock, /* Socket ready for reading */
00340         int *serial, /* Set to atom number */
00341         char atomName[VMAX_ARGLEN], /* Set to atom name */
00342         char resName[VMAX_ARGLEN], /* Set to residue name */
00343         int *resSeq, /* Set to residue number */
00344         double *x, /* Set to x-coordinate */
00345         double *y, /* Set to y-coordinate */
00346         double *z  /* Set to z-coordinate */
00347         ) {
00348 
00349 
00350     int i, njunk, gotit;
00351 
00352     /* Grab serial */
00353     if (Valist_readPDBSerial(thee, sock, serial) == VRC_FAILURE) {
00354         Vnm_print(2, "Valist_readPDB:  Error while parsing serial!\n");
00355     }
00356 
00357     /* Grab atom name */
00358     if (Valist_readPDBAtomName(thee, sock, atomName) == VRC_FAILURE) {
00359         Vnm_print(2, "Valist_readPDB:  Error while parsing atom name!\n");
00360         return VRC_FAILURE;
00361     }
00362 
00363     /* Grab residue name */
00364     if (Valist_readPDBResidueName(thee, sock, resName) == VRC_FAILURE) {
00365         Vnm_print(2, "Valist_readPDB:  Error while parsing residue name!\n");
00366         return VRC_FAILURE;
00367     }
00368 
00369 
00370     /* Grab residue number */
00371     if (Valist_readPDBResidueNumber(thee, sock, resSeq) == VRC_FAILURE) {
00372         Vnm_print(2, "Valist_readPDB:  Error while parsing residue name!\n");
00373         return VRC_FAILURE;
00374     }
00375 
00376 
00377     /* Read tokens until we find one that can be parsed as an atom
00378      * x-coordinate.  We will allow njunk=1 intervening field that
00379      * cannot be parsed as a coordinate */
00380     njunk = 1;
00381     gotit = 0;
00382     for (i=0; i<(njunk+1); i++) {
00383         if (Valist_readPDBAtomCoord(thee, sock, x) == VRC_SUCCESS) {
00384             gotit = 1;
00385             break;
00386         }
00387     }
00388     if (!gotit) {
00389         Vnm_print(2, "Valist_readPDB:  Can't find x!\n");
00390         return VRC_FAILURE;
00391     }
00392     /* Read y-coordinate */
00393     if (Valist_readPDBAtomCoord(thee, sock, y) == VRC_FAILURE) {
00394         Vnm_print(2, "Valist_readPDB:  Can't find y!\n");
00395         return VRC_FAILURE;
00396     }
00397     /* Read z-coordinate */
00398     if (Valist_readPDBAtomCoord(thee, sock, z) == VRC_FAILURE) {
00399         Vnm_print(2, "Valist_readPDB:  Can't find z!\n");
00400         return VRC_FAILURE;
00401     }
00402 
00403 #if 0 /* Set to 1 if you want to debug */
00404     Vnm_print(1, "Valist_readPDB:  serial = %d\n", *serial);
00405     Vnm_print(1, "Valist_readPDB:  atomName = %s\n", atomName);
00406     Vnm_print(1, "Valist_readPDB:  resName = %s\n", resName);
00407     Vnm_print(1, "Valist_readPDB:  resSeq = %d\n", *resSeq);
00408     Vnm_print(1, "Valist_readPDB:  pos = (%g, %g, %g)\n", 
00409               *x, *y, *z);
00410 #endif
00411 
00412     return VRC_SUCCESS;
00413 }
00414 
00415 /* Get a the next available atom storage location, increasing the storage
00416  * space if necessary.  Return VNULL if something goes wrong. */
00417 VPRIVATE Vatom* Valist_getAtomStorage(
00418         Valist *thee,
00419         Vatom **plist, /* Pointer to existing list of atoms */
00420         int *pnlist, /* Size of existing list, may be changed */
00421         int *pnatoms /* Existing number of atoms in list; incremented 
00422                        before exit */
00423         ) {
00424 
00425     Vatom *oldList, *newList, *theList;
00426     Vatom *oldAtom, *newAtom;
00427     int iatom, inext, oldLength, newLength, natoms;
00428 
00429     newList = VNULL;
00430 
00431     /* See if we need more space */
00432     if (*pnatoms >= *pnlist) {
00433 
00434         /* Double the storage space */
00435         oldLength = *pnlist;
00436         newLength = 2*oldLength;
00437         newList = Vmem_malloc(thee->vmem, newLength, sizeof(Vatom));
00438         oldList = *plist;
00439 
00440         /* Check the allocation */
00441         if (newList == VNULL) {
00442             Vnm_print(2, "Valist_readPDB:  failed to allocate space for %d (Vatom)s!\n", newLength);
00443             return VNULL;
00444         }
00445 
00446         /* Copy the atoms over */
00447         natoms = *pnatoms;
00448         for (iatom=0; iatom<natoms; iatom++) { 
00449             oldAtom = &(oldList[iatom]);
00450             newAtom = &(newList[iatom]);
00451             Vatom_copyTo(oldAtom, newAtom);
00452             Vatom_dtor2(oldAtom);
00453         }
00454 
00455         /* Free the old list */
00456         Vmem_free(thee->vmem, oldLength, sizeof(Vatom), (void **)plist);
00457 
00458         /* Copy new list to plist */
00459         *plist = newList;
00460         *pnlist = newLength;
00461     }
00462 
00463     theList = *plist;
00464     inext = *pnatoms;
00465 
00466     /* Get the next available spot and increment counters */
00467     newAtom = &(theList[inext]);
00468     *pnatoms = inext + 1;
00469 
00470     return newAtom;
00471 }
00472 
00473 VPRIVATE Vrc_Codes Valist_setAtomArray(Valist *thee, 
00474         Vatom **plist, /* Pointer to list of atoms to store */
00475         int nlist, /* Length of list */
00476         int natoms /* Number of real atom entries in list */
00477         ) {
00478 
00479     Vatom *list, *newAtom, *oldAtom;
00480     int i;
00481 
00482     list = *plist;
00483 
00484     /* Allocate necessary space */
00485     thee->number = 0;
00486     thee->atoms = Vmem_malloc(thee->vmem, natoms, sizeof(Vatom));
00487     if (thee->atoms == VNULL) {
00488         Vnm_print(2, "Valist_readPDB:  Unable to allocate space for %d (Vatom)s!\n", 
00489                 natoms);
00490         return VRC_FAILURE;
00491     }
00492     thee->number = natoms;
00493 
00494     /* Copy over data */
00495     for (i=0; i<thee->number; i++) {
00496         newAtom = &(thee->atoms[i]);
00497         oldAtom = &(list[i]);
00498         Vatom_copyTo(oldAtom, newAtom);
00499         Vatom_dtor2(oldAtom);
00500     }
00501 
00502     /* Free old array */
00503     Vmem_free(thee->vmem, nlist, sizeof(Vatom), (void **)plist);
00504 
00505     return VRC_SUCCESS;
00506 }
00507 
00508 VPUBLIC Vrc_Codes Valist_readPDB(Valist *thee, Vparam *param, Vio *sock) {
00509 
00510     /* WE DO NOT DIRECTLY CONFORM TO PDB STANDARDS -- TO ALLOW LARGER FILES, WE
00511      * REQUIRE ALL FIELDS TO BE WHITESPACE DELIMITED */
00512 
00513     Vatom *atoms = VNULL;
00514     Vatom *nextAtom = VNULL;
00515     Vparam_AtomData *atomData = VNULL;
00516  
00517     char tok[VMAX_BUFSIZE];
00518     char atomName[VMAX_ARGLEN], resName[VMAX_ARGLEN]; 
00519     
00520  int nlist, natoms, serial, resSeq;
00521     
00522  double x, y, z, charge, radius, epsilon;
00523     double pos[3];
00524  
00525     if (thee == VNULL) {
00526   Vnm_print(2, "Valist_readPDB:  Got NULL pointer when reading PDB file!\n");
00527   VASSERT(0);
00528  }
00529     thee->number = 0;
00530 
00531     Vio_setWhiteChars(sock, Valist_whiteChars);
00532     Vio_setCommChars(sock, Valist_commChars);
00533 
00534     /* Allocate some initial space for the atoms */
00535     nlist = 200;
00536     atoms = Vmem_malloc(thee->vmem, nlist, sizeof(Vatom));
00537 
00538     natoms = 0;
00539     /* Read until we run out of lines */
00540     while (Vio_scanf(sock, "%s", tok) == 1) {
00541 
00542         /* Parse only ATOM/HETATOM fields */
00543         if ((Vstring_strcasecmp(tok, "ATOM") == 0) || 
00544             (Vstring_strcasecmp(tok, "HETATM") == 0)) {
00545 
00546             /* Read ATOM/HETATM field of PDB through the X/Y/Z fields */
00547             if (Valist_readPDB_throughXYZ(thee, sock, &serial, atomName, 
00548                         resName, &resSeq, &x, &y, &z) == VRC_FAILURE) {
00549                 Vnm_print(2, "Valist_readPDB:  Error parsing atom %d!\n", 
00550                           serial);
00551                 return VRC_FAILURE;
00552             }
00553 
00554             /* Try to find the parameters. */
00555             atomData = Vparam_getAtomData(param, resName, atomName);
00556             if (atomData == VNULL) {
00557                 Vnm_print(2, "Valist_readPDB:  Couldn't find parameters for \
00558 atom = %s, residue = %s\n", atomName, resName);
00559                 return VRC_FAILURE;
00560             }
00561             charge = atomData->charge;
00562             radius = atomData->radius;
00563    epsilon = atomData->epsilon;
00564 
00565             /* Get pointer to next available atom position */
00566             nextAtom = Valist_getAtomStorage(thee, &atoms, &nlist, &natoms);
00567             if (nextAtom == VNULL) {
00568                 Vnm_print(2, "Valist_readPDB:  Error in allocating spacing for atoms!\n");
00569                 return VRC_FAILURE;
00570             }
00571 
00572             /* Store the information */
00573             pos[0] = x; pos[1] = y; pos[2] = z; 
00574             Vatom_setPosition(nextAtom, pos);
00575             Vatom_setCharge(nextAtom, charge);
00576             Vatom_setRadius(nextAtom, radius);
00577    Vatom_setEpsilon(nextAtom, epsilon);
00578             Vatom_setAtomID(nextAtom, natoms-1);
00579    Vatom_setResName(nextAtom, resName);
00580             Vatom_setAtomName(nextAtom, atomName);
00581 
00582         } /* if ATOM or HETATM */
00583     } /* while we haven't run out of tokens */
00584 
00585     Vnm_print(0, "Valist_readPDB: Counted %d atoms\n", natoms);
00586     fflush(stdout);
00587 
00588     /* Store atoms internally */
00589     if (Valist_setAtomArray(thee, &atoms, nlist, natoms) == VRC_FAILURE) {
00590         Vnm_print(2, "Valist_readPDB:  unable to store atoms!\n");
00591         return VRC_FAILURE;
00592     }
00593 
00594     return Valist_getStatistics(thee);
00595 
00596 
00597 }
00598 
00599 VPUBLIC Vrc_Codes Valist_readPQR(Valist *thee, Vparam *params, Vio *sock) {
00600 
00601     /* WE DO NOT DIRECTLY CONFORM TO PDB STANDARDS -- TO ALLOW LARGER FILES, WE
00602      * REQUIRE ALL FIELDS TO BE WHITESPACE DELIMITED */
00603 
00604 
00605     Vatom *atoms = VNULL;
00606     Vatom *nextAtom = VNULL;
00607  Vparam_AtomData *atomData = VNULL;
00608  
00609     char tok[VMAX_BUFSIZE];
00610     char atomName[VMAX_ARGLEN], resName[VMAX_ARGLEN]; 
00611     
00612  int use_params = 0;
00613  int nlist, natoms, serial, resSeq;
00614  
00615     double x, y, z, charge, radius, epsilon;
00616     double pos[3];
00617  
00618  epsilon = 0.0;
00619 
00620     if (thee == VNULL) {
00621   Vnm_print(2, "Valist_readPQR:  Got NULL pointer when reading PQR file!\n");
00622   VASSERT(0);
00623  }
00624     thee->number = 0;
00625 
00626     Vio_setWhiteChars(sock, Valist_whiteChars);
00627     Vio_setCommChars(sock, Valist_commChars);
00628 
00629     /* Allocate some initial space for the atoms */
00630     nlist = 200;
00631     atoms = Vmem_malloc(thee->vmem, nlist, sizeof(Vatom));
00632  
00633  /* Check if we are using a parameter file or not */
00634  if(params != VNULL) use_params = 1;
00635  
00636     natoms = 0;
00637     /* Read until we run out of lines */
00638     while (Vio_scanf(sock, "%s", tok) == 1) {
00639 
00640         /* Parse only ATOM/HETATOM fields */
00641         if ((Vstring_strcasecmp(tok, "ATOM") == 0) || 
00642             (Vstring_strcasecmp(tok, "HETATM") == 0)) {
00643 
00644             /* Read ATOM/HETATM field of PDB through the X/Y/Z fields */
00645             if (Valist_readPDB_throughXYZ(thee, sock, &serial, atomName, 
00646                         resName, &resSeq, &x, &y, &z) == VRC_FAILURE) {
00647                 Vnm_print(2, "Valist_readPQR:  Error parsing atom %d!\n",serial);
00648                 Vnm_print(2, "Please double check this atom in the pqr file, e.g., make sure there are no concatenated fields.\n");
00649                 return VRC_FAILURE;
00650             }
00651 
00652             /* Read Q/R fields */
00653             if (Valist_readPDBChargeRadius(thee, sock, &charge, &radius) == VRC_FAILURE) {
00654                 Vnm_print(2, "Valist_readPQR:  Error parsing atom %d!\n", 
00655                           serial);
00656                 Vnm_print(2, "Please double check this atom in the pqr file, e.g., make sure there are no concatenated fields.\n");
00657                 return VRC_FAILURE;
00658             }
00659 
00660    if(use_params){
00661     /* Try to find the parameters. */
00662     atomData = Vparam_getAtomData(params, resName, atomName);
00663     if (atomData == VNULL) {
00664      Vnm_print(2, "Valist_readPDB:  Couldn't find parameters for \
00665 atom = %s, residue = %s\n", atomName, resName);
00666      return VRC_FAILURE;
00667     }
00668     charge = atomData->charge;
00669     radius = atomData->radius;
00670     epsilon = atomData->epsilon;
00671    }
00672    
00673             /* Get pointer to next available atom position */
00674             nextAtom = Valist_getAtomStorage(thee, &atoms, &nlist, &natoms);
00675             if (nextAtom == VNULL) {
00676                 Vnm_print(2, "Valist_readPQR:  Error in allocating spacing for atoms!\n");
00677                 return VRC_FAILURE;
00678             }
00679 
00680             /* Store the information */
00681             pos[0] = x; pos[1] = y; pos[2] = z; 
00682             Vatom_setPosition(nextAtom, pos);
00683             Vatom_setCharge(nextAtom, charge);
00684             Vatom_setRadius(nextAtom, radius);
00685    Vatom_setEpsilon(nextAtom, epsilon);
00686             Vatom_setAtomID(nextAtom, natoms-1);
00687    Vatom_setResName(nextAtom, resName);
00688             Vatom_setAtomName(nextAtom, atomName);
00689 
00690         } /* if ATOM or HETATM */
00691     } /* while we haven't run out of tokens */
00692 
00693     Vnm_print(0, "Valist_readPQR: Counted %d atoms\n", natoms);
00694     fflush(stdout);
00695 
00696     /* Store atoms internally */
00697     if (Valist_setAtomArray(thee, &atoms, nlist, natoms) == VRC_FAILURE) {
00698         Vnm_print(2, "Valist_readPDB:  unable to store atoms!\n");
00699         return VRC_FAILURE;
00700     }
00701 
00702     return Valist_getStatistics(thee);
00703 
00704 
00705 }
00706 
00707 VPUBLIC Vrc_Codes Valist_readXML(Valist *thee, Vparam *params, Vio *sock) {
00708 
00709     Vatom *atoms = VNULL;
00710     Vatom *nextAtom = VNULL;
00711  
00712     char tok[VMAX_BUFSIZE];
00713     char endtag[VMAX_BUFSIZE];
00714     
00715  int nlist, natoms;
00716     int xset, yset, zset, chgset, radset;
00717     
00718  double x, y, z, charge, radius, dtmp;
00719     double pos[3];
00720  
00721     if (thee == VNULL) {
00722   Vnm_print(2, "Valist_readXML:  Got NULL pointer when reading XML file!\n");
00723   VASSERT(0);
00724  }
00725     thee->number = 0;
00726 
00727     Vio_setWhiteChars(sock, Valist_xmlwhiteChars);
00728     Vio_setCommChars(sock, Valist_commChars);
00729 
00730     /* Allocate some initial space for the atoms */
00731     nlist = 200;
00732     atoms = Vmem_malloc(thee->vmem, nlist, sizeof(Vatom));
00733 
00734     /* Initialize some variables */
00735     natoms = 0;
00736     xset = 0;
00737     yset = 0;
00738     zset = 0;
00739     chgset = 0;
00740     radset = 0;
00741     strcpy(endtag,"/");
00742  
00743  if(params == VNULL){
00744   Vnm_print(1,"\nValist_readXML: Warning Warning Warning Warning Warning\n");
00745   Vnm_print(1,"Valist_readXML: The use of XML input files with parameter\n");
00746   Vnm_print(1,"Valist_readXML: files is currently not supported.\n");
00747   Vnm_print(1,"Valist_readXML: Warning Warning Warning Warning Warning\n\n");
00748  }
00749  
00750     /* Read until we run out of lines */
00751     while (Vio_scanf(sock, "%s", tok) == 1) {
00752 
00753         /* The first tag taken is the start tag - save it to detect end */
00754         if (Vstring_strcasecmp(endtag, "/") == 0) strcat(endtag, tok);
00755 
00756         if (Vstring_strcasecmp(tok, "x") == 0) {
00757             Vio_scanf(sock, "%s", tok);
00758             if (sscanf(tok, "%lf", &dtmp) != 1) {
00759                 Vnm_print(2, "Valist_readXML:  Unexpected token (%s) while \
00760 reading x!\n", tok);
00761                   return VRC_FAILURE;
00762               }
00763             x = dtmp;
00764             xset = 1;
00765         } else if (Vstring_strcasecmp(tok, "y") == 0) {
00766             Vio_scanf(sock, "%s", tok);
00767             if (sscanf(tok, "%lf", &dtmp) != 1) {
00768                 Vnm_print(2, "Valist_readXML:  Unexpected token (%s) while \
00769 reading y!\n", tok);
00770                   return VRC_FAILURE;
00771               }
00772             y = dtmp;
00773             yset = 1;
00774         } else if (Vstring_strcasecmp(tok, "z") == 0) {
00775             Vio_scanf(sock, "%s", tok);
00776             if (sscanf(tok, "%lf", &dtmp) != 1) {
00777                 Vnm_print(2, "Valist_readXML:  Unexpected token (%s) while \
00778 reading z!\n", tok);
00779                   return VRC_FAILURE;
00780               }
00781             z = dtmp;
00782             zset = 1;
00783         } else if (Vstring_strcasecmp(tok, "charge") == 0) {
00784             Vio_scanf(sock, "%s", tok);
00785             if (sscanf(tok, "%lf", &dtmp) != 1) {
00786                 Vnm_print(2, "Valist_readXML:  Unexpected token (%s) while \
00787 reading charge!\n", tok);
00788                   return VRC_FAILURE;
00789               }
00790             charge = dtmp;
00791             chgset = 1;
00792         } else if (Vstring_strcasecmp(tok, "radius") == 0) {
00793             Vio_scanf(sock, "%s", tok);
00794             if (sscanf(tok, "%lf", &dtmp) != 1) {
00795                 Vnm_print(2, "Valist_readXML:  Unexpected token (%s) while \
00796 reading radius!\n", tok);
00797                   return VRC_FAILURE;
00798               }
00799             radius = dtmp;
00800             radset = 1;
00801         } else if (Vstring_strcasecmp(tok, "/atom") == 0) {
00802  
00803           /* Get pointer to next available atom position */
00804             nextAtom = Valist_getAtomStorage(thee, &atoms, &nlist, &natoms);
00805             if (nextAtom == VNULL) {
00806                 Vnm_print(2, "Valist_readXML:  Error in allocating spacing for atoms!\n");
00807                 return VRC_FAILURE;
00808             }
00809 
00810             if (xset && yset && zset && chgset && radset){
00811               
00812                 /* Store the information */
00813                 pos[0] = x; pos[1] = y; pos[2] = z; 
00814                 Vatom_setPosition(nextAtom, pos);
00815                 Vatom_setCharge(nextAtom, charge);
00816                 Vatom_setRadius(nextAtom, radius);
00817                 Vatom_setAtomID(nextAtom, natoms-1);
00818 
00819                 /* Reset the necessary flags */
00820                 xset = 0;
00821                 yset = 0;
00822                 zset = 0;
00823                 chgset = 0;
00824                 radset = 0;
00825             } else {
00826                 Vnm_print(2,  "Valist_readXML:  Missing field(s) in atom tag:\n"); 
00827                 if (!xset) Vnm_print(2,"\tx value not set!\n"); 
00828                 if (!yset) Vnm_print(2,"\ty value not set!\n"); 
00829                 if (!zset) Vnm_print(2,"\tz value not set!\n"); 
00830                 if (!chgset) Vnm_print(2,"\tcharge value not set!\n"); 
00831                 if (!radset) Vnm_print(2,"\tradius value not set!\n"); 
00832                 return VRC_FAILURE;
00833             }
00834         } else if (Vstring_strcasecmp(tok, endtag) == 0) break;
00835     }
00836 
00837     Vnm_print(0, "Valist_readXML: Counted %d atoms\n", natoms);
00838     fflush(stdout);
00839 
00840     /* Store atoms internally */
00841     if (Valist_setAtomArray(thee, &atoms, nlist, natoms) == VRC_FAILURE) {
00842         Vnm_print(2, "Valist_readXML:  unable to store atoms!\n");
00843         return VRC_FAILURE;
00844     }
00845 
00846     return Valist_getStatistics(thee);
00847 
00848 }
00849 
00850 /* Load up Valist with various statistics */
00851 VPUBLIC Vrc_Codes Valist_getStatistics(Valist *thee) {
00852 
00853     Vatom *atom;
00854     int i, j;
00855 
00856     if (thee == VNULL) {
00857   Vnm_print(2, "Valist_getStatistics:  Got NULL pointer when loading up Valist with various statistics!\n");
00858   VASSERT(0);
00859  }
00860 
00861     thee->center[0] = 0.;
00862     thee->center[1] = 0.;
00863     thee->center[2] = 0.;
00864     thee->maxrad = 0.;
00865     thee->charge = 0.;
00866 
00867     if (thee->number == 0) return VRC_FAILURE;
00868 
00869     /* Reset stat variables */
00870     atom = &(thee->atoms[0]);
00871     for (i=0; i<3; i++) {
00872         thee->maxcrd[i] = thee->mincrd[i] = atom->position[i];
00873     }
00874     thee->maxrad = atom->radius;
00875     thee->charge = 0.0;
00876 
00877     for (i=0; i<thee->number; i++) {
00878 
00879         atom = &(thee->atoms[i]);
00880         for (j=0; j<3; j++) {
00881             if (atom->position[j] < thee->mincrd[j]) 
00882               thee->mincrd[j] = atom->position[j];
00883             if (atom->position[j] > thee->maxcrd[j]) 
00884               thee->maxcrd[j] = atom->position[j];
00885         }
00886         if (atom->radius > thee->maxrad) thee->maxrad = atom->radius;
00887         thee->charge = thee->charge + atom->charge;
00888     } 
00889 
00890     thee->center[0] = 0.5*(thee->maxcrd[0] + thee->mincrd[0]);
00891     thee->center[1] = 0.5*(thee->maxcrd[1] + thee->mincrd[1]);
00892     thee->center[2] = 0.5*(thee->maxcrd[2] + thee->mincrd[2]);
00893 
00894  Vnm_print(0, "Valist_getStatistics:  Max atom coordinate:  (%g, %g, %g)\n",
00895      thee->maxcrd[0], thee->maxcrd[1], thee->maxcrd[2]);
00896  Vnm_print(0, "Valist_getStatistics:  Min atom coordinate:  (%g, %g, %g)\n",
00897      thee->mincrd[0], thee->mincrd[1], thee->mincrd[2]);
00898  Vnm_print(0, "Valist_getStatistics:  Molecule center:  (%g, %g, %g)\n",
00899      thee->center[0], thee->center[1], thee->center[2]);
00900  
00901     return VRC_SUCCESS;
00902 }

Generated on Wed Oct 20 2010 12:01:33 for APBS by  doxygen 1.7.2