Move gmx_fatal.* to utility/fatalerror.*
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_wheel.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <math.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "sysstuff.h"
47 #include "physics.h"
48 #include "typedefs.h"
49 #include "gromacs/utility/smalloc.h"
50 #include "macros.h"
51 #include "vec.h"
52 #include "xvgr.h"
53 #include "pbc.h"
54 #include "pbc.h"
55 #include "index.h"
56 #include "gstat.h"
57 #include "gromacs/utility/fatalerror.h"
58 #include "gmx_ana.h"
59
60 #include "gromacs/commandline/pargs.h"
61 #include "gromacs/fileio/futil.h"
62 #include "gromacs/fileio/strdb.h"
63 #include "gromacs/fileio/writeps.h"
64
65 static gmx_bool *bPhobics(int nres, char *resnm[])
66 {
67     int       i, nb;
68     char    **cb;
69     gmx_bool *bb;
70
71     nb = get_strings("phbres.dat", &cb);
72     snew(bb, nres);
73
74     for (i = 0; (i < nres); i++)
75     {
76         if (search_str(nb, cb, resnm[i]) != -1)
77         {
78             bb[i] = TRUE;
79         }
80     }
81     return bb;
82 }
83
84 void wheel(const char *fn, int nres, char *resnm[], int r0, real rot0, char *title)
85 {
86     const real fontsize  = 16;
87     const real gray      = 0.9;
88     const real fontasp   = 0.6;
89     const real fontwidth = fontsize*fontasp;
90
91     t_psdata   out;
92     int        i, sl, slen;
93     real       ring, inner, outer;
94     real       xc, yc, box;
95     gmx_bool  *bPh;
96     char     **rnms;
97     char       sign;
98
99     inner = 75.0;
100     slen  = 0;
101     snew(rnms, nres);
102     for (i = 0; (i < nres); i++)
103     {
104         snew(rnms[i], 256);
105         sl   = strlen(resnm[i]);
106         sign = resnm[i][sl-1];
107         if ((sign == '+') || (sign == '-'))
108         {
109             resnm[i][sl-1] = '\0';
110         }
111         sprintf(rnms[i], "%s-%d", resnm[i], i+r0);
112         if ((sign == '+') || (sign == '-'))
113         {
114             sl            = strlen(rnms[i]);
115             rnms[i][sl]   = sign;
116             rnms[i][sl+1] = '\0';
117         }
118
119         slen = max(slen, (int)strlen(rnms[i]));
120     }
121     ring  = (2+slen)*fontwidth;
122     outer = inner+ring;
123     box   = inner*1.5+(1+(nres / 18))*ring;
124
125     bPh = bPhobics(nres, resnm);
126
127     out = ps_open(fn, 0, 0, 2.0*box, 2.0*box);
128     xc  = box;
129     yc  = box;
130
131     ps_font(out, efontHELV, 1.5*fontsize);
132     ps_translate(out, xc, yc);
133     if (title)
134     {
135         ps_ctext(out, 0, -fontsize*1.5/2.0, title, eXCenter);
136     }
137     ps_font(out, efontHELV, fontsize);
138     ps_rotate(out, rot0);
139     for (i = 0; (i < nres); )
140     {
141         if (bPh[i])
142         {
143             ps_color(out, gray, gray, gray);
144             ps_fillarcslice(out, 0, 0, inner, outer, -10, 10);
145             ps_color(out, 0, 0, 0);
146         }
147         ps_arcslice(out, 0, 0, inner, outer, -10, 10);
148
149         ps_ctext(out, inner+fontwidth, -fontsize/2.0, rnms[i], eXLeft);
150         ps_rotate(out, -100);
151         i++;
152
153         if ((i % 18) == 0)
154         {
155             inner  = outer;
156             outer += ring;
157         }
158     }
159     ps_close(out);
160 }
161
162 void wheel2(const char *fn, int nres, char *resnm[], real rot0, char *title)
163 {
164     const real fontsize  = 14;
165     const real gray      = 0.9;
166     const real fontasp   = 0.45;
167     const int  angle     = 9;
168     const real fontwidth = fontsize*fontasp;
169
170     t_psdata   out;
171     int        i, slen;
172     real       ring, inner, outer;
173     real       xc, yc, box;
174
175     inner = 60.0;
176     slen  = 0;
177     for (i = 0; (i < nres); i++)
178     {
179         slen = max(slen, (int)strlen(resnm[i]));
180     }
181     fprintf(stderr, "slen = %d\n", slen);
182     ring  = (slen)*fontwidth;
183     outer = inner+ring;
184     box   = (1+(nres / (2*angle)))*outer;
185
186     out = ps_open(fn, 0, 0, 2.0*box, 2.0*box);
187     xc  = box;
188     yc  = box;
189
190     ps_font(out, efontHELV, 1.5*fontsize);
191     ps_translate(out, xc, yc);
192     ps_color(out, 0, 0, 0);
193     if (title)
194     {
195         ps_ctext(out, 0, -fontsize*1.5/2.0, title, eXCenter);
196     }
197     ps_font(out, efontHELV, fontsize);
198
199     ps_rotate(out, rot0);
200     for (i = 0; (i < nres); )
201     {
202         if ((i % 5) == 4)
203         {
204             ps_color(out, gray, gray, 1.0);
205             ps_fillarcslice(out, 0, 0, inner, outer, -angle, angle);
206             ps_color(out, 0, 0, 0);
207         }
208         ps_arcslice(out, 0, 0, inner, outer, -angle, angle);
209
210         ps_ctext(out, inner+fontwidth, -fontsize/2.0, resnm[i], eXLeft);
211         ps_rotate(out, -2*angle);
212         i++;
213
214         if ((i % (2*angle)) == 0)
215         {
216             inner  = outer;
217             outer += ring;
218         }
219     }
220     ps_close(out);
221 }
222
223 int gmx_wheel(int argc, char *argv[])
224 {
225     const char     *desc[] = {
226         "[THISMODULE] plots a helical wheel representation of your sequence.",
227         "The input sequence is in the [TT].dat[tt] file where the first line contains",
228         "the number of residues and each consecutive line contains a residue "
229         "name."
230     };
231     output_env_t    oenv;
232     static real     rot0  = 0;
233     static gmx_bool bNum  = TRUE;
234     static char    *title = NULL;
235     static int      r0    = 1;
236     t_pargs         pa [] = {
237         { "-r0",  FALSE, etINT, {&r0},
238           "The first residue number in the sequence" },
239         { "-rot0", FALSE, etREAL, {&rot0},
240           "Rotate around an angle initially (90 degrees makes sense)" },
241         { "-T",   FALSE, etSTR, {&title},
242           "Plot a title in the center of the wheel (must be shorter than 10 characters, or it will overwrite the wheel)" },
243         { "-nn",  FALSE, etBOOL, {&bNum},
244           "Toggle numbers" }
245     };
246     t_filenm        fnm[] = {
247         { efDAT, "-f", NULL,  ffREAD  },
248         { efEPS, "-o", NULL,  ffWRITE }
249     };
250 #define NFILE asize(fnm)
251
252     int    i, nres;
253     char **resnm;
254
255     if (!parse_common_args(&argc, argv, PCA_BE_NICE, NFILE, fnm, asize(pa), pa,
256                            asize(desc), desc, 0, NULL, &oenv))
257     {
258         return 0;
259     }
260
261     for (i = 1; (i < argc); i++)
262     {
263         if (strcmp(argv[i], "-r0") == 0)
264         {
265             r0 = strtol(argv[++i], NULL, 10);
266             fprintf(stderr, "First residue is %d\n", r0);
267         }
268         else if (strcmp(argv[i], "-rot0") == 0)
269         {
270             rot0 = strtod(argv[++i], NULL);
271             fprintf(stderr, "Initial rotation is %g\n", rot0);
272         }
273         else if (strcmp(argv[i], "-T") == 0)
274         {
275             title = strdup(argv[++i]);
276             fprintf(stderr, "Title will be '%s'\n", title);
277         }
278         else if (strcmp(argv[i], "-nn") == 0)
279         {
280             bNum = FALSE;
281             fprintf(stderr, "No residue numbers\n");
282         }
283         else
284         {
285             gmx_fatal(FARGS, "Incorrect usage of option %s", argv[i]);
286         }
287     }
288
289     nres = get_lines(ftp2fn(efDAT, NFILE, fnm), &resnm);
290     if (bNum)
291     {
292         wheel(ftp2fn(efEPS, NFILE, fnm), nres, resnm, r0, rot0, title);
293     }
294     else
295     {
296         wheel2(ftp2fn(efEPS, NFILE, fnm), nres, resnm, rot0, title);
297     }
298
299     return 0;
300 }