267VPUBLIC Vparam_AtomData* Vparam_getAtomData(Vparam *thee,
268 char resName[VMAX_ARGLEN], char atomName[VMAX_ARGLEN]) {
271 Vparam_ResData *res = VNULL;
272 Vparam_AtomData *atom = VNULL;
274 VASSERT(thee != VNULL);
276 if ((thee->nResData == 0) || (thee->resData == VNULL)) {
281 /* Look for the matching residue */
282 res = Vparam_getResData(thee, resName);
285 Vnm_print(2, "Vparam_getAtomData: Unable to find residue %s!\n", resName);
288 for (i=0; i<res->nAtomData; i++) {
289 atom = &(res->atomData[i]);
291 Vnm_print(2, "Vparam_getAtomData: got NULL atom!\n");
294 if (Vstring_strcasecmp(atomName, atom->atomName) == 0) {
299 /* Didn't find a matching atom/residue */
301 Vnm_print(2, "Vparam_getAtomData: unable to find atom '%s', res '%s'\n",
306VPUBLIC int Vparam_readXMLFile(Vparam *thee, const char *iodev,
307 const char *iofmt, const char *thost, const char *fname) {
309 int i, ires, natoms, nalloc, ralloc;
310 Vparam_AtomData *atoms = VNULL;
311 Vparam_AtomData *tatoms = VNULL;
312 Vparam_AtomData *atom = VNULL;
313 Vparam_ResData *res = VNULL;
314 Vparam_ResData *residues = VNULL;
315 Vparam_ResData *tresidues = VNULL;
317 char currResName[VMAX_ARGLEN];
318 char tok[VMAX_ARGLEN];
319 char endtag[VMAX_ARGLEN];
321 VASSERT(thee != VNULL);
323 /* Setup communication */
324 sock = Vio_ctor(iodev,iofmt,thost,fname,"r");
326 Vnm_print(2, "Vparam_readXMLFile: Problem opening virtual socket %s\n",
330 if (Vio_accept(sock, 0) < 0) {
331 Vnm_print(2, "Vparam_readXMLFile: Problem accepting virtual socket %s\n",
335 Vio_setWhiteChars(sock, MCxmlwhiteChars);
336 Vio_setCommChars(sock, MCcommChars);
338 /* Clear existing parameters */
339 if (thee->nResData > 0) {
340 Vnm_print(2, "WARNING -- CLEARING PARAMETER DATABASE!\n");
341 for (i=0; i<thee->nResData; i++) {
342 Vparam_ResData_dtor2(&(thee->resData[i]));
344 Vmem_free(thee->vmem, thee->nResData,
345 sizeof(Vparam_ResData), (void **)&(thee->resData));
350 /* Set up temporary residue list */
353 residues = (Vparam_ResData*)Vmem_malloc(thee->vmem, ralloc, sizeof(Vparam_ResData));
355 /* Read until we run out of entries, allocating space as needed */
358 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1);
360 /* The first token should be the start tag */
362 if (Vstring_strcasecmp(endtag, "/") == 0) strcat(endtag, tok);
364 if (Vstring_strcasecmp(tok, "residue") == 0) {
365 if (thee->nResData >= ralloc) {
366 tresidues = (Vparam_ResData*)Vmem_malloc(thee->vmem, 2*ralloc, sizeof(Vparam_ResData));
367 VASSERT(tresidues != VNULL);
368 for (i=0; i<thee->nResData; i++) {
369 Vparam_ResData_copyTo(&(residues[i]), &(tresidues[i]));
371 Vmem_free(thee->vmem, ralloc, sizeof(Vparam_ResData),
372 (void **)&(residues));
373 residues = tresidues;
378 /* Initial space for this residue's atoms */
381 atoms = (Vparam_AtomData*)Vmem_malloc(thee->vmem, nalloc, sizeof(Vparam_AtomData));
383 } else if (Vstring_strcasecmp(tok, "name") == 0) {
384 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1); /* value */
385 strcpy(currResName, tok);
386 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1); /* </name> */
387 } else if (Vstring_strcasecmp(tok, "atom") == 0) {
388 if (natoms >= nalloc) {
389 tatoms = (Vparam_AtomData*)Vmem_malloc(thee->vmem, 2*nalloc, sizeof(Vparam_AtomData));
390 VASSERT(tatoms != VNULL);
391 for (i=0; i<natoms; i++) {
392 Vparam_AtomData_copyTo(&(atoms[i]), &(tatoms[i]));
394 Vmem_free(thee->vmem, nalloc, sizeof(Vparam_AtomData),
400 atom = &(atoms[natoms]);
401 if (!readXMLFileAtom(sock, atom)) break;
404 } else if (Vstring_strcasecmp(tok, "/residue") == 0) {
406 res = &(residues[thee->nResData]);
407 Vparam_ResData_ctor2(res, thee->vmem);
408 res->atomData = (Vparam_AtomData*)Vmem_malloc(thee->vmem, natoms,
409 sizeof(Vparam_AtomData));
410 res->nAtomData = natoms;
411 strcpy(res->name, currResName);
412 for (i=0; i<natoms; i++) {
413 strcpy(atoms[i].resName, currResName);
414 Vparam_AtomData_copyTo(&(atoms[i]), &(res->atomData[i]));
416 Vmem_free(thee->vmem, nalloc, sizeof(Vparam_AtomData), (void **)&(atoms));
419 } else if (Vstring_strcasecmp(tok, endtag) == 0) break;
422 /* Initialize and copy the residues into the Vparam object */
424 thee->resData = (Vparam_ResData*)Vmem_malloc(thee->vmem, thee->nResData,
425 sizeof(Vparam_ResData));
426 for (ires=0; ires<thee->nResData; ires++) {
427 Vparam_ResData_copyTo(&(residues[ires]), &(thee->resData[ires]));
430 /* Destroy temporary atom space */
431 Vmem_free(thee->vmem, ralloc, sizeof(Vparam_ResData), (void **)&(residues));
433 /* Shut down communication */
434 Vio_acceptFree(sock);
440 Vnm_print(2, "Vparam_readXMLFile: Got unexpected EOF reading parameter file!\n");
445VPUBLIC int Vparam_readFlatFile(Vparam *thee, const char *iodev,
446 const char *iofmt, const char *thost, const char *fname) {
448 int i, iatom, jatom, ires, natoms, nalloc;
449 Vparam_AtomData *atoms = VNULL;
450 Vparam_AtomData *tatoms = VNULL;
451 Vparam_AtomData *atom = VNULL;
452 Vparam_ResData *res = VNULL;
454 char currResName[VMAX_ARGLEN];
456 VASSERT(thee != VNULL);
458 /* Setup communication */
459 sock = Vio_ctor(iodev,iofmt,thost,fname,"r");
461 Vnm_print(2, "Vparam_readFlatFile: Problem opening virtual socket %s\n",
465 if (Vio_accept(sock, 0) < 0) {
466 Vnm_print(2, "Vparam_readFlatFile: Problem accepting virtual socket %s\n",
470 Vio_setWhiteChars(sock, MCwhiteChars);
471 Vio_setCommChars(sock, MCcommChars);
473 /* Clear existing parameters */
474 if (thee->nResData > 0) {
475 Vnm_print(2, "WARNING -- CLEARING PARAMETER DATABASE!\n");
476 for (i=0; i<thee->nResData; i++) {
477 Vparam_ResData_dtor2(&(thee->resData[i]));
479 Vmem_free(thee->vmem, thee->nResData,
480 sizeof(Vparam_ResData), (void **)&(thee->resData));
483 /* Initial space for atoms */
486 atoms = (Vparam_AtomData*)Vmem_malloc(thee->vmem, nalloc, sizeof(Vparam_AtomData));
488 /* Read until we run out of entries, allocating space as needed */
490 if (natoms >= nalloc) {
491 tatoms = (Vparam_AtomData*)Vmem_malloc(thee->vmem, 2*nalloc, sizeof(Vparam_AtomData));
492 VASSERT(tatoms != VNULL);
493 for (i=0; i<natoms; i++) {
494 Vparam_AtomData_copyTo(&(atoms[i]), &(tatoms[i]));
496 Vmem_free(thee->vmem, nalloc, sizeof(Vparam_AtomData),
502 atom = &(atoms[natoms]);
503 if (!readFlatFileLine(sock, atom)) break;
506 if (natoms == 0) return 0;
508 /* Count the number of residues */
510 strcpy(currResName, atoms[0].resName);
511 for (i=1; i<natoms; i++) {
512 if (Vstring_strcasecmp(atoms[i].resName, currResName) != 0) {
513 strcpy(currResName, atoms[i].resName);
518 /* Create the residues */
519 thee->resData = (Vparam_ResData*)Vmem_malloc(thee->vmem, thee->nResData,
520 sizeof(Vparam_ResData));
521 VASSERT(thee->resData != VNULL);
522 for (i=0; i<(thee->nResData); i++) {
523 res = &(thee->resData[i]);
524 Vparam_ResData_ctor2(res, thee->vmem);
527 /* Count the number of atoms per residue */
529 res = &(thee->resData[ires]);
531 strcpy(res->name, atoms[0].resName);
532 for (i=1; i<natoms; i++) {
533 if (Vstring_strcasecmp(atoms[i].resName, res->name) != 0) {
535 res = &(thee->resData[ires]);
537 strcpy(res->name, atoms[i].resName);
538 } else (res->nAtomData)++;
541 /* Allocate per-residue space for atoms */
542 for (ires=0; ires<thee->nResData; ires++) {
543 res = &(thee->resData[ires]);
544 res->atomData = (Vparam_AtomData*)Vmem_malloc(thee->vmem, res->nAtomData,
545 sizeof(Vparam_AtomData));
548 /* Copy atoms into residues */
550 Vparam_AtomData_copyTo(&(atoms[0]), &(res->atomData[iatom]));
551 for (ires=0; ires<thee->nResData; ires++) {
552 res = &(thee->resData[ires]);
553 for (jatom=0; jatom<res->nAtomData; jatom++) {
554 Vparam_AtomData_copyTo(&(atoms[iatom]), &(res->atomData[jatom]));
560 /* Shut down communication */
561 Vio_acceptFree(sock);
564 /* Destroy temporary atom space */
565 Vmem_free(thee->vmem, nalloc, sizeof(Vparam_AtomData), (void **)&(atoms));
610VPRIVATE int readXMLFileAtom(Vio *sock, Vparam_AtomData *atom) {
613 char tok[VMAX_BUFSIZE];
614 int chgflag, radflag, nameflag;
616 VASSERT(atom != VNULL);
618 if (Vio_scanf(sock, "%s", tok) != 1) return 0;
626 if (Vstring_strcasecmp(tok, "name") == 0) {
627 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1);
628 if (strlen(tok) > VMAX_ARGLEN) {
629 Vnm_print(2, "Vparam_readXMLFileAtom: string (%s) too long \
630(%d)!\n", tok, strlen(tok));
634 strcpy(atom->atomName, tok);
635 } else if (Vstring_strcasecmp(tok, "charge") == 0) {
636 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1);
637 if (sscanf(tok, "%lf", &dtmp) != 1) {
638 Vnm_print(2, "Vparam_readXMLFileAtom: Unexpected token (%s) while \
639parsing charge!\n", tok);
644 } else if (Vstring_strcasecmp(tok, "radius") == 0) {
645 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1);
646 if (sscanf(tok, "%lf", &dtmp) != 1) {
647 Vnm_print(2, "Vparam_readXMLFileAtom: Unexpected token (%s) while \
648parsing radius!\n", tok);
653 } else if (Vstring_strcasecmp(tok, "epsilon") == 0) {
654 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1);
655 if (sscanf(tok, "%lf", &dtmp) != 1) {
656 Vnm_print(2, "Vparam_readXMLFileAtom: Unexpected token (%s) while \
657parsing epsilon!\n", tok);
660 atom->epsilon = dtmp;
661 } else if ((Vstring_strcasecmp(tok, "/atom") == 0) ||
662 (Vstring_strcasecmp(tok, "atom") == 0)){
663 if (chgflag && radflag && nameflag) return 1;
665 Vnm_print(2, "Vparam_readXMLFileAtom: Reached end of atom without \
666setting the charge!\n");
668 } else if (!radflag) {
669 Vnm_print(2, "Vparam_readXMLFileAtom: Reached end of atom without \
670setting the radius!\n");
672 } else if (!nameflag) {
673 Vnm_print(2, "Vparam_readXMLFileAtom: Reached end of atom without \
674setting the name!\n");
678 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1);
681 /* If we get here something wrong has happened */
686 Vnm_print(2, "Vparam_readXMLFileAtom: Got unexpected EOF reading parameter file!\n");
691VPRIVATE int readFlatFileLine(Vio *sock, Vparam_AtomData *atom) {
694 char tok[VMAX_BUFSIZE];
696 VASSERT(atom != VNULL);
698 if (Vio_scanf(sock, "%s", tok) != 1) return 0;
699 if (strlen(tok) > VMAX_ARGLEN) {
700 Vnm_print(2, "Vparam_readFlatFile: string (%s) too long (%d)!\n",
704 strcpy(atom->resName, tok);
705 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1);
706 if (strlen(tok) > VMAX_ARGLEN) {
707 Vnm_print(2, "Vparam_readFlatFile: string (%s) too long (%d)!\n",
711 strcpy(atom->atomName, tok);
712 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1);
713 if (sscanf(tok, "%lf", &dtmp) != 1) {
714 Vnm_print(2, "Vparam_readFlatFile: Unexpected token (%s) while \
715parsing charge!\n", tok);
719 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1);
720 if (sscanf(tok, "%lf", &dtmp) != 1) {
721 Vnm_print(2, "Vparam_readFlatFile: Unexpected token (%s) while \
722parsing radius!\n", tok);
726 VJMPERR1(Vio_scanf(sock, "%s", tok) == 1);
727 if (sscanf(tok, "%lf", &dtmp) != 1) {
728 Vnm_print(2, "Vparam_readFlatFile: Unexpected token (%s) while \
729parsing radius!\n", tok);
732 atom->epsilon = dtmp;
737 Vnm_print(2, "Vparam_readFlatFile: Got unexpected EOF reading parameter file!\n");