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