changed reading hint
[gromacs/adressmacs.git] / src / kernel / xlate.c
blob8a503e02f40c9470962f8ffb9debdc0ae33bdf1b
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 2.0
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * Please refer to:
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * GRowing Old MAkes el Chrono Sweat
29 static char *SRCID_xlate_c = "$Id$";
31 #include <ctype.h>
32 #include <string.h>
33 #include "typedefs.h"
34 #include "strdb.h"
35 #include "string2.h"
36 #include "smalloc.h"
37 #include "symtab.h"
38 #include "index.h"
40 typedef struct {
41 char *res;
42 char *atom;
43 char *replace;
44 } t_xlate_atom;
46 static t_xlate_atom *get_xlatoms(int *nxlatom)
48 static char *xlfile="xlateat.dat";
50 t_xlate_atom *xl=NULL;
51 char rbuf[32],abuf[32],repbuf[32];
52 char **lines,*_ptr;
53 int nlines,i,n;
55 nlines = get_lines(xlfile,&lines);
56 if (nlines > 0)
57 snew(xl,nlines);
59 n = 0;
60 for(i=0; (i<nlines); i++) {
61 if (sscanf(lines[i],"%s%s%s",rbuf,abuf,repbuf) != 3)
62 fprintf(stderr,"Invalid line '%s' in %s\n",lines[i],xlfile);
63 else {
64 /* Use wildcards... */
65 if (strcmp(rbuf,"*") != 0)
66 xl[n].res = strdup(rbuf);
67 else
68 xl[n].res = NULL;
70 /* Replace underscores in the string by spaces */
71 while ((_ptr = strchr(abuf,'_')) != 0)
72 *_ptr = ' ';
74 xl[n].atom = strdup(abuf);
75 xl[n].replace = strdup(repbuf);
76 n++;
78 sfree(lines[i]);
80 if (nlines > 0)
81 sfree(lines);
82 fprintf(stderr,"%d out of %d lines of %s converted succesfully\n",
83 n,nlines,xlfile);
85 *nxlatom = n;
87 return xl;
90 void rename_atoms(t_atoms *atoms)
92 t_symtab symtab;
93 int nxlate,a,i;
94 t_xlate_atom *xlatom;
95 char c,*res,**atom;
96 bool bRenamed;
98 xlatom = get_xlatoms(&nxlate);
100 open_symtab(&symtab);
102 for(a=0; (a<atoms->nr); a++) {
103 res=*atoms->resname[atoms->atom[a].resnr];
104 atom=atoms->atomname[a];
105 if (isdigit(*atom[0])) {
106 c=*atom[0];
107 for (i=0; (i<strlen(*atom)-1); i++)
108 *atom[i]=*atom[i+1];
109 srenew(*atom,strlen(*atom)+2);
110 *atom[i]=c;
112 bRenamed=FALSE;
113 for(i=0; (i<nxlate) && !bRenamed; i++) {
114 if ((xlatom[i].res == NULL) || (strcasecmp(res,xlatom[i].res) == 0) ||
115 ((strcasecmp("protein",xlatom[i].res) == 0) && is_protein(res)))
116 if (strcasecmp(*atom,xlatom[i].atom) == 0) {
117 /* don't free the old atomname, since it might be in the symtab */
118 atoms->atomname[a]=put_symtab(&symtab,xlatom[i].replace);
119 bRenamed=TRUE;
123 close_symtab(&symtab);
124 free_symtab(&symtab);
125 for(i=0; i<nxlate; i++) {
126 sfree(xlatom[i].res);
127 sfree(xlatom[i].atom);
128 sfree(xlatom[i].replace);
130 sfree(xlatom);