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