Clean up xvgr.h and viewit.h usage
[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 "physics.h"
47 #include "typedefs.h"
48 #include "gromacs/utility/smalloc.h"
49 #include "macros.h"
50 #include "vec.h"
51 #include "pbc.h"
52 #include "pbc.h"
53 #include "index.h"
54 #include "gstat.h"
55 #include "gromacs/utility/fatalerror.h"
56 #include "gmx_ana.h"
57
58 #include "gromacs/commandline/pargs.h"
59 #include "gromacs/utility/futil.h"
60 #include "gromacs/fileio/strdb.h"
61 #include "gromacs/fileio/writeps.h"
62
63 static gmx_bool *bPhobics(int nres, char *resnm[])
64 {
65     int       i, nb;
66     char    **cb;
67     gmx_bool *bb;
68
69     nb = get_strings("phbres.dat", &cb);
70     snew(bb, nres);
71
72     for (i = 0; (i < nres); i++)
73     {
74         if (search_str(nb, cb, resnm[i]) != -1)
75         {
76             bb[i] = TRUE;
77         }
78     }
79     return bb;
80 }
81
82 void wheel(const char *fn, int nres, char *resnm[], int r0, real rot0, char *title)
83 {
84     const real fontsize  = 16;
85     const real gray      = 0.9;
86     const real fontasp   = 0.6;
87     const real fontwidth = fontsize*fontasp;
88
89     t_psdata   out;
90     int        i, sl, slen;
91     real       ring, inner, outer;
92     real       xc, yc, box;
93     gmx_bool  *bPh;
94     char     **rnms;
95     char       sign;
96
97     inner = 75.0;
98     slen  = 0;
99     snew(rnms, nres);
100     for (i = 0; (i < nres); i++)
101     {
102         snew(rnms[i], 256);
103         sl   = strlen(resnm[i]);
104         sign = resnm[i][sl-1];
105         if ((sign == '+') || (sign == '-'))
106         {
107             resnm[i][sl-1] = '\0';
108         }
109         sprintf(rnms[i], "%s-%d", resnm[i], i+r0);
110         if ((sign == '+') || (sign == '-'))
111         {
112             sl            = strlen(rnms[i]);
113             rnms[i][sl]   = sign;
114             rnms[i][sl+1] = '\0';
115         }
116
117         slen = max(slen, (int)strlen(rnms[i]));
118     }
119     ring  = (2+slen)*fontwidth;
120     outer = inner+ring;
121     box   = inner*1.5+(1+(nres / 18))*ring;
122
123     bPh = bPhobics(nres, resnm);
124
125     out = ps_open(fn, 0, 0, 2.0*box, 2.0*box);
126     xc  = box;
127     yc  = box;
128
129     ps_font(out, efontHELV, 1.5*fontsize);
130     ps_translate(out, xc, yc);
131     if (title)
132     {
133         ps_ctext(out, 0, -fontsize*1.5/2.0, title, eXCenter);
134     }
135     ps_font(out, efontHELV, fontsize);
136     ps_rotate(out, rot0);
137     for (i = 0; (i < nres); )
138     {
139         if (bPh[i])
140         {
141             ps_color(out, gray, gray, gray);
142             ps_fillarcslice(out, 0, 0, inner, outer, -10, 10);
143             ps_color(out, 0, 0, 0);
144         }
145         ps_arcslice(out, 0, 0, inner, outer, -10, 10);
146
147         ps_ctext(out, inner+fontwidth, -fontsize/2.0, rnms[i], eXLeft);
148         ps_rotate(out, -100);
149         i++;
150
151         if ((i % 18) == 0)
152         {
153             inner  = outer;
154             outer += ring;
155         }
156     }
157     ps_close(out);
158 }
159
160 void wheel2(const char *fn, int nres, char *resnm[], real rot0, char *title)
161 {
162     const real fontsize  = 14;
163     const real gray      = 0.9;
164     const real fontasp   = 0.45;
165     const int  angle     = 9;
166     const real fontwidth = fontsize*fontasp;
167
168     t_psdata   out;
169     int        i, slen;
170     real       ring, inner, outer;
171     real       xc, yc, box;
172
173     inner = 60.0;
174     slen  = 0;
175     for (i = 0; (i < nres); i++)
176     {
177         slen = max(slen, (int)strlen(resnm[i]));
178     }
179     fprintf(stderr, "slen = %d\n", slen);
180     ring  = (slen)*fontwidth;
181     outer = inner+ring;
182     box   = (1+(nres / (2*angle)))*outer;
183
184     out = ps_open(fn, 0, 0, 2.0*box, 2.0*box);
185     xc  = box;
186     yc  = box;
187
188     ps_font(out, efontHELV, 1.5*fontsize);
189     ps_translate(out, xc, yc);
190     ps_color(out, 0, 0, 0);
191     if (title)
192     {
193         ps_ctext(out, 0, -fontsize*1.5/2.0, title, eXCenter);
194     }
195     ps_font(out, efontHELV, fontsize);
196
197     ps_rotate(out, rot0);
198     for (i = 0; (i < nres); )
199     {
200         if ((i % 5) == 4)
201         {
202             ps_color(out, gray, gray, 1.0);
203             ps_fillarcslice(out, 0, 0, inner, outer, -angle, angle);
204             ps_color(out, 0, 0, 0);
205         }
206         ps_arcslice(out, 0, 0, inner, outer, -angle, angle);
207
208         ps_ctext(out, inner+fontwidth, -fontsize/2.0, resnm[i], eXLeft);
209         ps_rotate(out, -2*angle);
210         i++;
211
212         if ((i % (2*angle)) == 0)
213         {
214             inner  = outer;
215             outer += ring;
216         }
217     }
218     ps_close(out);
219 }
220
221 int gmx_wheel(int argc, char *argv[])
222 {
223     const char     *desc[] = {
224         "[THISMODULE] plots a helical wheel representation of your sequence.",
225         "The input sequence is in the [TT].dat[tt] file where the first line contains",
226         "the number of residues and each consecutive line contains a residue "
227         "name."
228     };
229     output_env_t    oenv;
230     static real     rot0  = 0;
231     static gmx_bool bNum  = TRUE;
232     static char    *title = NULL;
233     static int      r0    = 1;
234     t_pargs         pa [] = {
235         { "-r0",  FALSE, etINT, {&r0},
236           "The first residue number in the sequence" },
237         { "-rot0", FALSE, etREAL, {&rot0},
238           "Rotate around an angle initially (90 degrees makes sense)" },
239         { "-T",   FALSE, etSTR, {&title},
240           "Plot a title in the center of the wheel (must be shorter than 10 characters, or it will overwrite the wheel)" },
241         { "-nn",  FALSE, etBOOL, {&bNum},
242           "Toggle numbers" }
243     };
244     t_filenm        fnm[] = {
245         { efDAT, "-f", NULL,  ffREAD  },
246         { efEPS, "-o", NULL,  ffWRITE }
247     };
248 #define NFILE asize(fnm)
249
250     int    i, nres;
251     char **resnm;
252
253     if (!parse_common_args(&argc, argv, PCA_BE_NICE, NFILE, fnm, asize(pa), pa,
254                            asize(desc), desc, 0, NULL, &oenv))
255     {
256         return 0;
257     }
258
259     for (i = 1; (i < argc); i++)
260     {
261         if (strcmp(argv[i], "-r0") == 0)
262         {
263             r0 = strtol(argv[++i], NULL, 10);
264             fprintf(stderr, "First residue is %d\n", r0);
265         }
266         else if (strcmp(argv[i], "-rot0") == 0)
267         {
268             rot0 = strtod(argv[++i], NULL);
269             fprintf(stderr, "Initial rotation is %g\n", rot0);
270         }
271         else if (strcmp(argv[i], "-T") == 0)
272         {
273             title = strdup(argv[++i]);
274             fprintf(stderr, "Title will be '%s'\n", title);
275         }
276         else if (strcmp(argv[i], "-nn") == 0)
277         {
278             bNum = FALSE;
279             fprintf(stderr, "No residue numbers\n");
280         }
281         else
282         {
283             gmx_fatal(FARGS, "Incorrect usage of option %s", argv[i]);
284         }
285     }
286
287     nres = get_lines(ftp2fn(efDAT, NFILE, fnm), &resnm);
288     if (bNum)
289     {
290         wheel(ftp2fn(efEPS, NFILE, fnm), nres, resnm, r0, rot0, title);
291     }
292     else
293     {
294         wheel2(ftp2fn(efEPS, NFILE, fnm), nres, resnm, rot0, title);
295     }
296
297     return 0;
298 }