c5fc90e91b64e5d2e5c227c1974cf42143b63b03
[alexxy/gromacs.git] / src / programs / gmx / g_protonate.c
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <math.h>
40 #include "string2.h"
41 #include "typedefs.h"
42 #include "macros.h"
43 #include "smalloc.h"
44 #include "statutil.h"
45 #include "confio.h"
46 #include "genhydro.h"
47 #include "tpxio.h"
48 #include "index.h"
49 #include "vec.h"
50 #include "hackblock.h"
51
52 int gmx_protonate(int argc, char *argv[])
53 {
54     const char     *desc[] = {
55         "[TT]g_protonate[tt] reads (a) conformation(s) and adds all missing",
56         "hydrogens as defined in [TT]gmx2.ff/aminoacids.hdb[tt]. If only [TT]-s[tt] is",
57         "specified, this conformation will be protonated, if also [TT]-f[tt]",
58         "is specified, the conformation(s) will be read from this file, ",
59         "which can be either a single conformation or a trajectory.",
60         "[PAR]",
61         "If a [TT].pdb[tt] file is supplied, residue names might not correspond to",
62         "to the GROMACS naming conventions, in which case these residues will",
63         "probably not be properly protonated.",
64         "[PAR]",
65         "If an index file is specified, please note that the atom numbers",
66         "should correspond to the [BB]protonated[bb] state."
67     };
68
69     char            title[STRLEN+1];
70     const char     *infile;
71     char           *grpnm;
72     t_topology      top;
73     int             ePBC;
74     t_atoms        *atoms, *iatoms;
75     t_protonate     protdata;
76     atom_id        *index;
77     t_trxstatus    *status;
78     t_trxstatus    *out;
79     t_trxframe      fr, frout;
80     rvec           *x, *ix;
81     int             nidx, natoms, natoms_out;
82     matrix          box;
83     int             i, frame, resind;
84     gmx_bool        bReadMultiple;
85     output_env_t    oenv;
86
87     const char     *bugs[] = {
88         "For the moment, only .pdb files are accepted to the -s flag"
89     };
90
91     t_filenm        fnm[] = {
92         { efTPS, NULL, NULL,         ffREAD  },
93         { efTRX, "-f", NULL,         ffOPTRD },
94         { efNDX, NULL, NULL,         ffOPTRD },
95         { efTRO, "-o", "protonated", ffWRITE }
96     };
97 #define NFILE asize(fnm)
98
99     if (!parse_common_args(&argc, argv, PCA_CAN_TIME,
100                            NFILE, fnm, 0, NULL, asize(desc), desc, asize(bugs), bugs, &oenv))
101     {
102         return 0;
103     }
104
105     infile = opt2fn("-s", NFILE, fnm);
106     read_tps_conf(infile, title, &top, &ePBC, &x, NULL, box, FALSE);
107     atoms = &(top.atoms);
108     printf("Select group to process:\n");
109     get_index(atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &nidx, &index, &grpnm);
110     bReadMultiple = opt2bSet("-f", NFILE, fnm);
111     if (bReadMultiple)
112     {
113         infile = opt2fn("-f", NFILE, fnm);
114         if (!read_first_frame(oenv, &status, infile, &fr, TRX_NEED_X ) )
115         {
116             gmx_fatal(FARGS, "cannot read coordinate file %s", infile);
117         }
118         natoms = fr.natoms;
119     }
120     else
121     {
122         clear_trxframe(&fr, TRUE);
123         fr.natoms = atoms->nr;
124         fr.bTitle = TRUE;
125         fr.title  = title;
126         fr.bX     = TRUE;
127         fr.x      = x;
128         fr.bBox   = TRUE;
129         copy_mat(box, fr.box);
130         natoms = fr.natoms;
131     }
132
133     /* check input */
134     if (natoms == 0)
135     {
136         gmx_fatal(FARGS, "no atoms in coordinate file %s", infile);
137     }
138
139     if (natoms > atoms->nr)
140     {
141         gmx_fatal(FARGS, "topology with %d atoms does not match "
142                   "coordinates with %d atoms", atoms->nr, natoms);
143     }
144
145     for (i = 0; i < nidx; i++)
146     {
147         if (index[i] > natoms)
148         {
149             gmx_fatal(FARGS, "An atom number in group %s is larger than the number of "
150                       "atoms (%d) in the coordinate file %s", grpnm, natoms, infile);
151         }
152     }
153
154     /* get indexed copy of atoms */
155     snew(iatoms, 1);
156     init_t_atoms(iatoms, nidx, FALSE);
157     snew(iatoms->atom, iatoms->nr);
158     resind = 0;
159     for (i = 0; i < nidx; i++)
160     {
161         iatoms->atom[i]     = atoms->atom[index[i]];
162         iatoms->atomname[i] = atoms->atomname[index[i]];
163         if (i > 0 && (atoms->atom[index[i]].resind != atoms->atom[index[i-1]].resind) )
164         {
165             resind++;
166         }
167         iatoms->atom[i].resind  = resind;
168         iatoms->resinfo[resind] = atoms->resinfo[atoms->atom[index[i]].resind];
169         /* allocate some space for the rtp name and copy from name */
170         snew(iatoms->resinfo[resind].rtp, 1);
171         *iatoms->resinfo[resind].rtp = gmx_strdup(*atoms->resinfo[resind].name);
172
173         iatoms->nres = max(iatoms->nres, iatoms->atom[i].resind+1);
174     }
175
176     init_t_protonate(&protdata);
177
178     out = open_trx(opt2fn("-o", NFILE, fnm), "w");
179     snew(ix, nidx);
180     frame = 0;
181     do
182     {
183         if (debug)
184         {
185             fprintf(debug, "FRAME %d (%d %g)\n", frame, fr.step, fr.time);
186         }
187         /* get indexed copy of x */
188         for (i = 0; i < nidx; i++)
189         {
190             copy_rvec(fr.x[index[i]], ix[i]);
191         }
192         /* protonate */
193         natoms_out = protonate(&iatoms, &ix, &protdata);
194
195         /* setup output frame */
196         frout        = fr;
197         frout.natoms = natoms_out;
198         frout.bAtoms = TRUE;
199         frout.atoms  = iatoms;
200         frout.bV     = FALSE;
201         frout.bF     = FALSE;
202         frout.x      = ix;
203
204         /* write output */
205         write_trxframe(out, &frout, NULL);
206         frame++;
207     }
208     while (bReadMultiple && read_next_frame(oenv, status, &fr) );
209
210     sfree(ix);
211     sfree(iatoms);
212
213     return 0;
214 }