forked from zangtw/Gromacs_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenStrucAfterLSF.c
305 lines (260 loc) · 7.89 KB
/
genStrucAfterLSF.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#define GMX_DOUBLE
#define GMX_INTEGER_BIG_ENDIAN
#define GMXLIBDIR "/home/tz4/softwares/share/gromacs/top"
#define PACKAGE "Gromacs"
#define VERSION "4.6.3"
#define USE_VERSION_H
#include "smalloc.h"
#include <math.h>
#include "macros.h"
#include "typedefs.h"
#include "xvgr.h"
#include "copyrite.h"
#include "statutil.h"
#include "string2.h"
#include "vec.h"
#include "index.h"
#include "gmx_fatal.h"
#include "futil.h"
#include "princ.h"
#include "rmpbc.h"
#include "do_fit.h"
#include "matio.h"
#include "tpxio.h"
#include "cmat.h"
#include "viewit.h"
#include "gmx_ana.h"
static int obtainCoorFromPDB(FILE *fp, rvec **x)
{
int natoms;
char title[STRLEN];
int ePBC, model_nr;
t_atoms atoms;
matrix boxpdb;
get_pdb_coordnum(fp, &natoms);
if (natoms == 0)
gmx_fatal(FARGS, "\nNo coordinates in pdb file\n");
frewind(fp);
srenew(*x, natoms);
atoms.nr = natoms;
atoms.atom = NULL;
atoms.pdbinfo = NULL;
read_pdbfile(fp, title, &model_nr, &atoms, *x, &ePBC, boxpdb, TRUE, NULL);
return natoms;
}
int main(int argc, char *argv[])
{
const char *desc[] = { "Generate structure in pdb format", \
"after least-square fit to a reference structure." };
static gmx_bool bMassWeighted = TRUE;
int natoms_trx, natoms;
int i;
real t, *w_rls, *w_rms;
gmx_bool bMass;
t_topology top;
int ePBC;
matrix box;
rvec *x = NULL, *xp = NULL, *xAv = NULL;
rvec *xAv2 = NULL, *xFluc = NULL;
char buf[256];
FILE *flist, *fp, *fout, *ffluc;
int nfiles;
char line[STRLEN];
real rls;
gmx_bool bTop = FALSE, bFluc = FALSE;
gmx_bool bSelf = TRUE, bStd = TRUE, bR = FALSE;
int ifit, irms;
atom_id *ind_fit, *ind_rms;
char *gn_fit, *gn_rms;
output_env_t oenv;
t_filenm fnm[] =
{
{ efTPS, NULL, NULL, ffREAD },
{ efLOG, "-f", NULL, ffREAD },
{ efPDB, "-o", NULL, ffWRITE },
{ efLOG, "-fluc", NULL, ffOPTWR }
};
t_pargs pa[] =
{
{ "-self", FALSE, etBOOL, {&bSelf},
"Calculate self-standard deviation (fluctuation) if TRUE (default); calculate standard deviation (displacement) with respect to the referenece structure if FALSE. "},
{ "-std", FALSE, etBOOL, {&bStd},
"Calculate standard deviation (root of variance) if TRUE (default); calculate variance instead if FALSE. "},
{ "-r", FALSE, etBOOL, {&bR},
"Calculate radial deviation if TRUE; calculate deviation in (x,y,z) if FALSE (default). "}
};
#define NFILE asize(fnm)
#define NPA asize(pa)
CopyRight(stderr, argv[0]);
parse_common_args(&argc, argv, PCA_CAN_TIME | PCA_TIME_UNIT | PCA_CAN_VIEW
| PCA_BE_NICE, NFILE, fnm, NPA, pa, asize(desc),
desc, 0, NULL, &oenv);
bTop = read_tps_conf(ftp2fn(efTPS, NFILE, fnm), buf, &top, &ePBC, &xp,
NULL, box, TRUE);
snew(w_rls, top.atoms.nr);
snew(w_rms, top.atoms.nr);
bFluc = opt2bSet("-fluc", NFILE, fnm);
fprintf(stderr, "Select group for least squares fit\n");
get_index(&(top.atoms), ftp2fn_null(efNDX, NFILE, fnm), 1, &ifit, &ind_fit,
&gn_fit);
if (ifit < 3)
gmx_fatal(FARGS, "Need >= 3 points to fit!\n" );
bMass = FALSE;
for (i = 0; i < ifit; i++)
{
if (bMassWeighted)
w_rls[ind_fit[i]] = top.atoms.atom[ind_fit[i]].m;
else
w_rls[ind_fit[i]] = 1;
bMass = bMass || (top.atoms.atom[ind_fit[i]].m != 0);
}
if (!bMass)
{
fprintf(stderr, "All masses in the fit group are 0, using masses of 1\n");
for (i = 0; i < ifit; i++)
w_rls[ind_fit[i]] = 1;
}
get_index(&(top.atoms), ftp2fn_null(efNDX, NFILE, fnm),
1, &irms, &ind_rms, &gn_rms);
bMass = FALSE;
for (i = 0; i < irms; i++)
{
if (bMassWeighted)
w_rms[ind_rms[i]] = top.atoms.atom[ind_rms[i]].m;
else
w_rms[ind_rms[i]] = 1.0;
bMass = bMass || (top.atoms.atom[ind_rms[i]].m != 0);
}
if (!bMass)
{
fprintf(stderr, "All masses in the target group are 0, using masses of 1\n");
for (i = 0; i < irms; i++)
w_rms[ind_rms[i]] = 1;
}
/* Prepare reference structure */
reset_x(ifit, ind_fit, top.atoms.nr, NULL, xp, w_rls);
flist = fopen(opt2fn("-f", NFILE, fnm), "r");
nfiles = 0;
/* loop over target structures */
while (fgets2(line, STRLEN, flist))
{
fp = fopen(line, "r");
natoms_trx = obtainCoorFromPDB(fp, &x);
if (natoms_trx != top.atoms.nr)
{
fprintf(stderr,
"\nWARNING: topology has %d atoms, whereas trajectory has %d\n",
top.atoms.nr, natoms_trx);
}
natoms = min(top.atoms.nr, natoms_trx);
if (xAv == NULL) snew(xAv, natoms);
if (bFluc)
{
if (xAv2 == NULL) snew(xAv2, ifit);
if (xFluc == NULL) snew(xFluc, ifit);
}
/* Prepare target structure */
reset_x(ifit, ind_fit, natoms, NULL, x, w_rls);
/* do the least squares fit to original structure */
do_fit(natoms, w_rls, xp, x);
for (i = 0; i< natoms; i++)
{
xAv[i][0] += x[i][0];
xAv[i][1] += x[i][1];
xAv[i][2] += x[i][2];
}
if (bFluc)
{
for (i = 0; i< ifit; i++)
{
xAv2[i][0] += x[ind_fit[i]][0] * x[ind_fit[i]][0];
xAv2[i][1] += x[ind_fit[i]][1] * x[ind_fit[i]][1];
xAv2[i][2] += x[ind_fit[i]][2] * x[ind_fit[i]][2];
}
}
/* calc RMSD */
rls = calc_similar_ind(FALSE, irms, ind_rms, w_rms, x, xp);
nfiles++;
fprintf(stderr, "\nnfiles: %d RMSD: %12.7f\n", nfiles, rls);
fclose(fp);
}
fout = fopen(opt2fn("-o", NFILE, fnm), "w");
real temp = 10.0 / nfiles;
for (i = 0; i< natoms; i++)
{
xAv[i][0] *= temp;
xAv[i][1] *= temp;
xAv[i][2] *= temp;
fprintf(fout, "%8.3f%8.3f%8.3f\n", xAv[i][0], xAv[i][1], xAv[i][2]);
}
fclose(fout);
if (bFluc)
{
real Rfluc = 0;
ffluc = fopen(opt2fn("-fluc", NFILE, fnm), "w");
temp = 100.0 / nfiles;
for (i = 0; i< ifit; i++)
{
xAv2[i][0] *= temp;
xAv2[i][1] *= temp;
xAv2[i][2] *= temp;
if (bSelf)
{
xFluc[i][0] = xAv2[i][0] - xAv[ind_fit[i]][0] * xAv[ind_fit[i]][0];
xFluc[i][1] = xAv2[i][1] - xAv[ind_fit[i]][1] * xAv[ind_fit[i]][1];
xFluc[i][2] = xAv2[i][2] - xAv[ind_fit[i]][2] * xAv[ind_fit[i]][2];
}
else
{
real mu = xp[ind_fit[i]][0] * 10;
xFluc[i][0] = xAv2[i][0];
xFluc[i][0] += (mu - xAv[ind_fit[i]][0] - xAv[ind_fit[i]][0]) * mu;
mu = xp[ind_fit[i]][1] * 10;
xFluc[i][1] = xAv2[i][1];
xFluc[i][1] += (mu - xAv[ind_fit[i]][1] - xAv[ind_fit[i]][1]) * mu;
mu = xp[ind_fit[i]][2] * 10;
xFluc[i][2] = xAv2[i][2];
xFluc[i][2] += (mu - xAv[ind_fit[i]][2] - xAv[ind_fit[i]][2]) * mu;
}
xFluc[i][0] = xFluc[i][0] > 0 ? xFluc[i][0] : (-xFluc[i][0]);
xFluc[i][1] = xFluc[i][1] > 0 ? xFluc[i][1] : (-xFluc[i][1]);
xFluc[i][2] = xFluc[i][2] > 0 ? xFluc[i][2] : (-xFluc[i][2]);
if(bR)
{
Rfluc += xFluc[i][0];
Rfluc += xFluc[i][1];
Rfluc += xFluc[i][2];
}
if(bStd)
{
xFluc[i][0] = sqrt(xFluc[i][0]);
xFluc[i][1] = sqrt(xFluc[i][1]);
xFluc[i][2] = sqrt(xFluc[i][2]);
if(bR)
Rfluc = sqrt(Rfluc);
}
if(bR)
fprintf(ffluc, "%f\n", Rfluc);
else
{
fprintf(ffluc, "%f\n", xFluc[i][0]);
fprintf(ffluc, "%f\n", xFluc[i][1]);
fprintf(ffluc, "%f\n", xFluc[i][2]);
}
}
fclose(ffluc);
}
sfree(w_rls);
sfree(w_rms);
sfree(ind_fit);
sfree(ind_rms);
sfree(x);
sfree(xp);
sfree(xAv);
if (bFluc)
{
sfree(xAv2);
sfree(xFluc);
}
return 0;
}