Fixed a bug in the pdb-writing code.
[gromacs.git] / src / kernel / rtp2xml.c
blob5284f7c538f18cc24312c3bd7ad00204effbf456
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 3.1
11 * Copyright (c) 1991-2001, University of Groningen, The Netherlands
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * If you want to redistribute modifications, please consider that
18 * scientific software is very special. Version control is crucial -
19 * bugs must be traceable. We will be happy to consider code for
20 * inclusion in the official distribution, but derived work must not
21 * be called official GROMACS. Details are found in the README & COPYING
22 * files - if they are missing, get the official version at www.gromacs.org.
24 * To help us fund GROMACS development, we humbly ask that you cite
25 * the papers on the package - you can find them in the top README file.
27 * For more info, check our website at http://www.gromacs.org
29 * And Hey:
30 * GROningen Mixture of Alchemy and Childrens' Stories
32 static char *SRCID_pdb2gmx_c = "$Id$";
33 #include <time.h>
34 #include <ctype.h>
35 #include "assert.h"
36 #include "sysstuff.h"
37 #include "typedefs.h"
38 #include "smalloc.h"
39 #include "copyrite.h"
40 #include "string2.h"
41 #include "confio.h"
42 #include "symtab.h"
43 #include "vec.h"
44 #include "statutil.h"
45 #include "futil.h"
46 #include "fatal.h"
47 #include "pdbio.h"
48 #include "toputil.h"
49 #include "h_db.h"
50 #include "physics.h"
51 #include "pgutil.h"
52 #include "calch.h"
53 #include "resall.h"
54 #include "pdb2top.h"
55 #include "ter_db.h"
56 #include "strdb.h"
57 #include "txtdump.h"
58 #include "gbutil.h"
59 #include "genhydro.h"
60 #include "readinp.h"
61 #include "xlate.h"
62 #include "specbond.h"
63 #include "index.h"
64 #include "hizzie.h"
66 static void dump_res(FILE *fp,int indent,t_restp *restp,int nah,
67 t_hackblock *ah,int nres_long,char **res_long)
69 char *rtype[ebtsNR] = { "rbond", "rangle", "rdihedral", "rimproper" };
70 int ntype[ebtsNR] = { 2, 3, 4, 4 };
71 int i,j,k,nn;
72 char *tmp,descr[128];
74 descr[0] = '\0';
75 for(nn=0; (nn < nres_long) && (strstr(res_long[nn],restp->resname) == NULL); nn++)
77 if (nn < nres_long) {
78 tmp = res_long[nn] + strlen(restp->resname);
79 while (*tmp && isspace(*tmp)) {
80 tmp++;
82 if (strlen(tmp) > 0)
83 strcpy(descr,tmp);
86 pr_indent(fp,indent);
87 fprintf(fp,"<residue restype=\"%s\" longname=\"%s\">\n",restp->resname,descr);
88 indent += 2;
90 for(i=0; (i<restp->natom); i++) {
91 pr_indent(fp,indent);
92 fprintf(fp,"<ratom name=\"%s\">\n",*restp->atomname[i]);
94 for(i=0; (i<ebtsNR); i++) {
95 for(j=0; (j<restp->rb[i].nb); j++) {
96 pr_indent(fp,indent);
97 fprintf(fp,"<%s",rtype[i]);
98 for(k=0; (k<ntype[i]); k++)
99 fprintf(fp," a%d=\"%s\"",k+1,restp->rb[i].b[j].a[k]);
100 fprintf(fp,"/>\n");
103 for(i=0; (i<nah); i++) {
104 if (strcmp(restp->resname,ah[i].name) == 0) {
105 for(j=0; (j<ah[i].nhack); j++) {
106 pr_indent(fp,indent);
107 if ((ah[i].hack[j].a[0][0] == 'O') || (ah[i].hack[j].a[0][0] == 'N'))
108 fprintf(fp,"<raddh hclass=\"polar\" ");
109 else
110 fprintf(fp,"<raddh hclass=\"aliphatic\"");
111 fprintf(fp," addgeom=\"%d\" addnum=\"%d\"",
112 ah[i].hack[j].tp,ah[i].hack[j].nr);
113 fprintf(fp," addto=\"%s",ah[i].hack[j].a[0]);
114 for(k=1; ((k <= 3) && (ah[i].hack[j].a[k] > 0)); k++)
115 fprintf(fp," %s",ah[i].hack[j].a[k]);
116 fprintf(fp,"\"/>\n");
118 break;
121 indent -= 2;
122 pr_indent(fp,indent);
123 fprintf(fp,"</residue>\n\n");
126 static void dump_hack_add(FILE *fp,int indent,t_hack *hack)
128 pr_indent(fp,indent);
129 fprintf(fp,"<modadd addname=\"%s\" addgeom=\"%d\" addto=\"%s %s %s\"/>\n",
130 hack->nname,hack->tp,hack->a[0],hack->a[1],hack->a[2]);
133 static void dump_hack_del(FILE *fp,int indent,t_hack *hack)
135 pr_indent(fp,indent);
136 fprintf(fp,"<moddelete delname=\"%s\"/>\n",hack->oname);
139 static void dump_hack_rep(FILE *fp,int indent,t_hack *hack)
141 pr_indent(fp,indent);
142 fprintf(fp,"<modreplace oldname=\"%s\" newname=\"%s\"/>\n",hack->oname,hack->nname);
145 static void dump_mod(FILE *fp,int indent,t_hackblock *tdb,t_atomtype *atype)
147 int i,j,k;
149 pr_indent(fp,indent);
150 fprintf(fp,"<moddef modtype=\"%s\"\n",tdb->name);
151 indent += 2;
152 for(j=0; (j<tdb->nhack); j++) {
153 if (tdb->hack[j].oname == NULL)
154 dump_hack_add(fp,indent,&(tdb->hack[j]));
155 else if (tdb->hack[j].nname == NULL)
156 dump_hack_del(fp,indent,&(tdb->hack[j]));
157 else
158 dump_hack_rep(fp,indent,&(tdb->hack[j]));
160 indent -= 2;
161 pr_indent(fp,indent);
162 fprintf(fp,"</moddef>\n\n");
165 static void dump_mods(FILE *fp,int indent,
166 int n,t_hackblock tdb[],t_atomtype *atype)
168 int i,j;
170 for(i=0; (i<n); i++)
171 dump_mod(fp,indent,&(tdb[i]),atype);
174 static void do_specbonds(FILE *fp,int indent)
176 t_specbond *sb;
177 int i,nsb;
179 pr_indent(fp,indent);
180 fprintf(fp,"<linkdef linktype=\"peptide\" restype=\"* *\" atomprev=\"C\" atomnext=\"N\" refdist=\"0.133\"/>\n");
181 sb = get_specbonds(&nsb);
182 for(i=0; (i<nsb); i++) {
183 pr_indent(fp,indent);
184 fprintf(fp,"<linkdef linktype=\"unknown\" restype=\"%s %s\" atomprev=\"%s\" atomnext=\"%s\" refdist=\"%g\"/>\n",
185 sb[i].res1,sb[i].res2,sb[i].atom1,sb[i].atom2,sb[i].length);
187 done_specbonds(nsb,sb);
188 sfree(sb);
189 fprintf(fp,"\n");
192 int main(int argc, char *argv[])
194 static char *desc[] = {
195 "This program reads an rtp file and dumps an xml file."
198 typedef struct {
199 char chain;
200 int start;
201 int natom;
202 bool bAllWat;
203 int nterpairs;
204 int *chainstart;
205 } t_pdbchain;
207 typedef struct {
208 char chain;
209 bool bAllWat;
210 int nterpairs;
211 int *chainstart;
212 t_hackblock **ntdb;
213 t_hackblock **ctdb;
214 int *rN;
215 int *rC;
216 t_atoms *pdba;
217 rvec *x;
218 } t_chain;
220 FILE *fp;
221 int natom,nres;
222 t_atoms pdba_all,*pdba;
223 t_atoms *atoms;
224 t_block *block;
225 int chain,nch,maxch,nwaterchain;
226 t_pdbchain *pdb_ch;
227 t_chain *chains,*cc;
228 char **res_long;
229 int nres_long;
230 char *ff;
231 int i,j,k,l,nrtp;
232 int *swap_index,si;
233 int bts[ebtsNR];
234 t_restp *restp;
235 t_hackblock *ah;
236 t_symtab symtab;
237 t_atomtype *atype;
238 char fn[256],*top_fn,itp_fn[STRLEN],posre_fn[STRLEN],buf_fn[STRLEN];
239 char molname[STRLEN],title[STRLEN];
240 char *c;
241 int nah,nNtdb,nCtdb;
242 t_hackblock *ntdb,*ctdb;
243 int nssbonds;
244 t_ssbond *ssbonds;
245 rvec *pdbx,*x;
246 bool bUsed,bDummies=FALSE,bWat,bPrevWat=FALSE,bITP,bDummyAromatics=FALSE;
247 real mHmult=0;
248 int nrexcl;
249 bool bAlldih;
251 CopyRight(stderr,argv[0]);
253 ff = strdup("ffgmx2");
255 /* Read long residue names */
256 nres_long = get_file("res-long.dat",&res_long);
258 /* Read atomtypes... */
259 atype=read_atype(ff,&symtab);
261 /* read residue database */
262 printf("Reading residue database... (%s)\n",ff);
263 nrtp=read_resall(ff,bts,&restp,atype,&symtab,&bAlldih,&nrexcl);
265 /* read hydrogen database */
266 nah=read_h_db(ff,&ah);
268 /* Read Termini database... */
269 nNtdb=read_ter_db(ff,'n',&ntdb,atype);
270 nCtdb=read_ter_db(ff,'c',&ctdb,atype);
272 fp=fopen("residues.xml","w");
273 /* fprintf(fp,"<?xml version=\"1.0\"?>\n");
274 fprintf(fp,"<!DOCTYPE residues SYSTEM \"residues.dtd\">\n"); */
275 fprintf(fp,"\n<residues>\n");
276 for(i=0; (i<nrtp); i++) {
277 dump_res(fp,2,&(restp[i]),nah,ah,nres_long,res_long);
279 do_specbonds(fp,2);
280 dump_mods(fp,2,nNtdb,ntdb,atype);
281 dump_mods(fp,2,nCtdb,ctdb,atype);
282 fprintf(fp,"</residues>\n");
283 fclose(fp);
285 thanx(stderr);
287 return 0;