2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
51 #include "fflibutil.h"
52 #include "hackblock.h"
53 #include "gmx_fatal.h"
63 static void get_xlatoms(const char *fn,FILE *fp,
64 int *nptr,t_xlate_atom **xlptr)
66 char filebase[STRLEN];
68 char abuf[1024],rbuf[1024],repbuf[1024],dumbuf[1024];
73 fflib_filename_base(fn,filebase,STRLEN);
78 while (get_a_line(fp,line,STRLEN))
80 na = sscanf(line,"%s%s%s%s",rbuf,abuf,repbuf,dumbuf);
81 /* Check if we are reading an old format file with the number of items
84 if (na == 1 && n == *nptr && sscanf(rbuf,"%d",&idum) == 1)
90 gmx_fatal(FARGS,"Expected a residue name and two atom names in file '%s', not '%s'",fn,line);
94 xl[n].filebase = strdup(filebase);
96 /* Use wildcards... */
97 if (strcmp(rbuf,"*") != 0)
99 xl[n].res = strdup(rbuf);
106 /* Replace underscores in the string by spaces */
107 while ((_ptr = strchr(abuf,'_')) != 0)
112 xl[n].atom = strdup(abuf);
113 xl[n].replace = strdup(repbuf);
121 static void done_xlatom(int nxlate,t_xlate_atom *xlatom)
125 for(i=0; (i<nxlate); i++)
127 sfree(xlatom[i].filebase);
128 if (xlatom[i].res != NULL)
130 sfree(xlatom[i].res);
132 sfree(xlatom[i].atom);
133 sfree(xlatom[i].replace);
138 void rename_atoms(const char *xlfile,const char *ffdir,
139 t_atoms *atoms,t_symtab *symtab,const t_restp *restp,
140 gmx_bool bResname,gmx_residuetype_t rt,gmx_bool bReorderNum,
144 int nxlate,a,i,resind;
145 t_xlate_atom *xlatom;
148 char c,*rnm,atombuf[32],*ptr0,*ptr1;
149 gmx_bool bReorderedNum,bRenamed,bMatch;
155 fp = libopen(xlfile);
156 get_xlatoms(xlfile,fp,&nxlate,&xlatom);
161 nf = fflib_search_file_end(ffdir,".arn",FALSE,&f);
164 fp = fflib_open(f[i]);
165 get_xlatoms(f[i],fp,&nxlate,&xlatom);
172 for(a=0; (a<atoms->nr); a++)
174 resind = atoms->atom[a].resind;
177 rnm = *(atoms->resinfo[resind].name);
181 rnm = *(atoms->resinfo[resind].rtp);
184 strcpy(atombuf,*(atoms->atomname[a]));
185 bReorderedNum = FALSE;
188 if (isdigit(atombuf[0]))
191 for (i=0; ((size_t)i<strlen(atombuf)-1); i++)
193 atombuf[i] = atombuf[i+1];
196 bReorderedNum = TRUE;
200 for(i=0; (i<nxlate) && !bRenamed; i++) {
201 /* Check if the base file name of the rtp and arn entry match */
203 gmx_strcasecmp(restp[resind].filebase,xlatom[i].filebase) == 0)
205 /* Match the residue name */
206 bMatch = (xlatom[i].res == NULL ||
207 (gmx_strcasecmp("protein",xlatom[i].res) == 0 &&
208 gmx_residuetype_is_protein(rt,rnm)) ||
209 (gmx_strcasecmp("DNA",xlatom[i].res) == 0 &&
210 gmx_residuetype_is_dna(rt,rnm)) ||
211 (gmx_strcasecmp("RNA",xlatom[i].res) == 0 &&
212 gmx_residuetype_is_rna(rt,rnm)));
216 ptr1 = xlatom[i].res;
217 while (ptr0[0] != '\0' && ptr1[0] != '\0' &&
218 (ptr0[0] == ptr1[0] || ptr1[0] == '?'))
223 bMatch = (ptr0[0] == '\0' && ptr1[0] == '\0');
225 if (bMatch && strcmp(atombuf,xlatom[i].atom) == 0)
227 /* We have a match. */
228 /* Don't free the old atomname,
229 * since it might be in the symtab.
231 ptr0 = strdup(xlatom[i].replace);
234 printf("Renaming atom '%s' in residue %d %s to '%s'\n",
236 atoms->resinfo[resind].nr,
237 *atoms->resinfo[resind].name,
240 atoms->atomname[a] = put_symtab(symtab,ptr0);
245 if (bReorderedNum && !bRenamed)
247 atoms->atomname[a] = put_symtab(symtab,atombuf);
251 done_xlatom(nxlate,xlatom);