2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
46 #include "gromacs/utility/smalloc.h"
48 #include "gromacs/commandline/pargs.h"
49 #include "gromacs/fileio/tpxio.h"
50 #include "gromacs/fileio/trxio.h"
51 #include "gromacs/utility/cstringutil.h"
52 #include "gromacs/math/vec.h"
55 #include "gromacs/random/random.h"
56 #include "gromacs/pbcutil/pbc.h"
57 #include "gromacs/pbcutil/rmpbc.h"
58 #include "gromacs/fileio/xvgr.h"
59 #include "gromacs/utility/futil.h"
60 #include "gromacs/fileio/matio.h"
62 #include "gromacs/fileio/trnio.h"
66 #include "gromacs/linearalgebra/eigensolver.h"
67 #include "gromacs/math/do_fit.h"
68 #include "gromacs/utility/fatalerror.h"
70 /* print to two file pointers at once (i.e. stderr and log) */
72 void lo_ffprintf(FILE *fp1
, FILE *fp2
, const char *buf
)
74 fprintf(fp1
, "%s", buf
);
75 fprintf(fp2
, "%s", buf
);
78 /* just print a prepared buffer to fp1 and fp2 */
80 void ffprintf(FILE *fp1
, FILE *fp2
, const char *buf
)
82 lo_ffprintf(fp1
, fp2
, buf
);
85 /* prepare buffer with one argument, then print to fp1 and fp2 */
87 void ffprintf_d(FILE *fp1
, FILE *fp2
, char *buf
, const char *fmt
, int arg
)
89 sprintf(buf
, fmt
, arg
);
90 lo_ffprintf(fp1
, fp2
, buf
);
93 /* prepare buffer with one argument, then print to fp1 and fp2 */
95 void ffprintf_g(FILE *fp1
, FILE *fp2
, char *buf
, const char *fmt
, real arg
)
97 sprintf(buf
, fmt
, arg
);
98 lo_ffprintf(fp1
, fp2
, buf
);
101 /* prepare buffer with one argument, then print to fp1 and fp2 */
103 void ffprintf_s(FILE *fp1
, FILE *fp2
, char *buf
, const char *fmt
, const char *arg
)
105 sprintf(buf
, fmt
, arg
);
106 lo_ffprintf(fp1
, fp2
, buf
);
109 /* prepare buffer with two arguments, then print to fp1 and fp2 */
111 void ffprintf_dd(FILE *fp1
, FILE *fp2
, char *buf
, const char *fmt
, int arg1
, int arg2
)
113 sprintf(buf
, fmt
, arg1
, arg2
);
114 lo_ffprintf(fp1
, fp2
, buf
);
117 /* prepare buffer with two arguments, then print to fp1 and fp2 */
119 void ffprintf_gg(FILE *fp1
, FILE *fp2
, char *buf
, const char *fmt
, real arg1
, real arg2
)
121 sprintf(buf
, fmt
, arg1
, arg2
);
122 lo_ffprintf(fp1
, fp2
, buf
);
125 /* prepare buffer with two arguments, then print to fp1 and fp2 */
127 void ffprintf_ss(FILE *fp1
, FILE *fp2
, char *buf
, const char *fmt
, const char *arg1
, const char *arg2
)
129 sprintf(buf
, fmt
, arg1
, arg2
);
130 lo_ffprintf(fp1
, fp2
, buf
);
143 void pr_energy(FILE *fp
, real e
)
145 fprintf(fp
, "Energy: %8.4f\n", e
);
148 void cp_index(int nn
, int from
[], int to
[])
152 for (i
= 0; (i
< nn
); i
++)
158 void mc_optimize(FILE *log
, t_mat
*m
, real
*time
,
159 int maxiter
, int nrandom
,
161 const char *conv
, output_env_t oenv
)
164 real ecur
, enext
, emin
, prob
, enorm
;
165 int i
, j
, iswap
, jswap
, nn
, nuphill
= 0;
171 fprintf(stderr
, "Can not do Monte Carlo optimization with a non-square matrix.\n");
174 printf("\nDoing Monte Carlo optimization to find the smoothest trajectory\n");
175 printf("by reordering the frames to minimize the path between the two structures\n");
176 printf("that have the largest pairwise RMSD.\n");
179 enorm
= m
->mat
[0][0];
180 for (i
= 0; (i
< m
->n1
); i
++)
182 for (j
= 0; (j
< m
->nn
); j
++)
184 if (m
->mat
[i
][j
] > enorm
)
186 enorm
= m
->mat
[i
][j
];
192 if ((iswap
== -1) || (jswap
== -1))
194 fprintf(stderr
, "Matrix contains identical values in all fields\n");
197 swap_rows(m
, 0, iswap
);
198 swap_rows(m
, m
->n1
-1, jswap
);
199 emin
= ecur
= mat_energy(m
);
200 printf("Largest distance %g between %d and %d. Energy: %g.\n",
201 enorm
, iswap
, jswap
, emin
);
203 rng
= gmx_rng_init(seed
);
206 /* Initiate and store global minimum */
207 minimum
= init_mat(nn
, m
->b1D
);
209 copy_t_mat(minimum
, m
);
213 fp
= xvgropen(conv
, "Convergence of the MC optimization",
214 "Energy", "Step", oenv
);
216 for (i
= 0; (i
< maxiter
); i
++)
218 /* Generate new swapping candidates */
221 iswap
= 1+(nn
-2)*gmx_rng_uniform_real(rng
);
222 jswap
= 1+(nn
-2)*gmx_rng_uniform_real(rng
);
224 while ((iswap
== jswap
) || (iswap
>= nn
-1) || (jswap
>= nn
-1));
226 /* Apply swap and compute energy */
227 swap_rows(m
, iswap
, jswap
);
228 enext
= mat_energy(m
);
230 /* Compute probability */
232 if ((enext
< ecur
) || (i
< nrandom
))
237 /* Store global minimum */
238 copy_t_mat(minimum
, m
);
244 /* Try Monte Carlo step */
245 prob
= exp(-(enext
-ecur
)/(enorm
*kT
));
248 if ((prob
== 1) || (gmx_rng_uniform_real(rng
) < prob
))
255 fprintf(log
, "Iter: %d Swapped %4d and %4d (energy: %g prob: %g)\n",
256 i
, iswap
, jswap
, enext
, prob
);
259 fprintf(fp
, "%6d %10g\n", i
, enext
);
265 swap_rows(m
, jswap
, iswap
);
268 fprintf(log
, "%d uphill steps were taken during optimization\n",
271 /* Now swap the matrix to get it into global minimum mode */
272 copy_t_mat(m
, minimum
);
274 fprintf(log
, "Global minimum energy %g\n", mat_energy(minimum
));
275 fprintf(log
, "Global minimum energy %g\n", mat_energy(m
));
276 fprintf(log
, "Swapped time and frame indices and RMSD to next neighbor:\n");
277 for (i
= 0; (i
< m
->nn
); i
++)
279 fprintf(log
, "%10g %5d %10g\n",
282 (i
< m
->nn
-1) ? m
->mat
[m
->m_ind
[i
]][m
->m_ind
[i
+1]] : 0);
291 static void calc_dist(int nind
, rvec x
[], real
**d
)
297 for (i
= 0; (i
< nind
-1); i
++)
300 for (j
= i
+1; (j
< nind
); j
++)
302 /* Should use pbc_dx when analysing multiple molecueles,
303 * but the box is not stored for every frame.
305 rvec_sub(xi
, x
[j
], dx
);
311 static real
rms_dist(int isize
, real
**d
, real
**d_r
)
317 for (i
= 0; (i
< isize
-1); i
++)
319 for (j
= i
+1; (j
< isize
); j
++)
321 r
= d
[i
][j
]-d_r
[i
][j
];
325 r2
/= (isize
*(isize
-1))/2;
330 static int rms_dist_comp(const void *a
, const void *b
)
337 if (da
->dist
- db
->dist
< 0)
341 else if (da
->dist
- db
->dist
> 0)
348 static int clust_id_comp(const void *a
, const void *b
)
355 return da
->clust
- db
->clust
;
358 static int nrnb_comp(const void *a
, const void *b
)
365 /* return the b-a, we want highest first */
366 return db
->nr
- da
->nr
;
369 void gather(t_mat
*m
, real cutoff
, t_clusters
*clust
)
373 int i
, j
, k
, nn
, cid
, n1
, diff
;
376 /* First we sort the entries in the RMSD matrix */
380 for (i
= k
= 0; (i
< n1
); i
++)
382 for (j
= i
+1; (j
< n1
); j
++, k
++)
386 d
[k
].dist
= m
->mat
[i
][j
];
391 gmx_incons("gather algortihm");
393 qsort(d
, nn
, sizeof(d
[0]), rms_dist_comp
);
395 /* Now we make a cluster index for all of the conformations */
398 /* Now we check the closest structures, and equalize their cluster numbers */
399 fprintf(stderr
, "Linking structures ");
402 fprintf(stderr
, "*");
404 for (k
= 0; (k
< nn
) && (d
[k
].dist
< cutoff
); k
++)
406 diff
= c
[d
[k
].j
].clust
- c
[d
[k
].i
].clust
;
412 c
[d
[k
].j
].clust
= c
[d
[k
].i
].clust
;
416 c
[d
[k
].i
].clust
= c
[d
[k
].j
].clust
;
422 fprintf(stderr
, "\nSorting and renumbering clusters\n");
423 /* Sort on cluster number */
424 qsort(c
, n1
, sizeof(c
[0]), clust_id_comp
);
426 /* Renumber clusters */
428 for (k
= 1; k
< n1
; k
++)
430 if (c
[k
].clust
!= c
[k
-1].clust
)
443 for (k
= 0; (k
< n1
); k
++)
445 fprintf(debug
, "Cluster index for conformation %d: %d\n",
446 c
[k
].conf
, c
[k
].clust
);
450 for (k
= 0; k
< n1
; k
++)
452 clust
->cl
[c
[k
].conf
] = c
[k
].clust
;
459 gmx_bool
jp_same(int **nnb
, int i
, int j
, int P
)
465 for (k
= 0; nnb
[i
][k
] >= 0; k
++)
467 bIn
= bIn
|| (nnb
[i
][k
] == j
);
475 for (k
= 0; nnb
[j
][k
] >= 0; k
++)
477 bIn
= bIn
|| (nnb
[j
][k
] == i
);
485 for (ii
= 0; nnb
[i
][ii
] >= 0; ii
++)
487 for (jj
= 0; nnb
[j
][jj
] >= 0; jj
++)
489 if ((nnb
[i
][ii
] == nnb
[j
][jj
]) && (nnb
[i
][ii
] != -1))
499 static void jarvis_patrick(int n1
, real
**mat
, int M
, int P
,
500 real rmsdcut
, t_clusters
*clust
)
505 int i
, j
, k
, cid
, diff
, max
;
514 /* First we sort the entries in the RMSD matrix row by row.
515 * This gives us the nearest neighbor list.
519 for (i
= 0; (i
< n1
); i
++)
521 for (j
= 0; (j
< n1
); j
++)
524 row
[j
].dist
= mat
[i
][j
];
526 qsort(row
, n1
, sizeof(row
[0]), rms_dist_comp
);
529 /* Put the M nearest neighbors in the list */
531 for (j
= k
= 0; (k
< M
) && (j
< n1
) && (mat
[i
][row
[j
].j
] < rmsdcut
); j
++)
535 nnb
[i
][k
] = row
[j
].j
;
543 /* Put all neighbors nearer than rmsdcut in the list */
546 for (j
= 0; (j
< n1
) && (mat
[i
][row
[j
].j
] < rmsdcut
); j
++)
555 nnb
[i
][k
] = row
[j
].j
;
561 srenew(nnb
[i
], max
+1);
569 fprintf(debug
, "Nearest neighborlist. M = %d, P = %d\n", M
, P
);
570 for (i
= 0; (i
< n1
); i
++)
572 fprintf(debug
, "i:%5d nbs:", i
);
573 for (j
= 0; nnb
[i
][j
] >= 0; j
++)
575 fprintf(debug
, "%5d[%5.3f]", nnb
[i
][j
], mat
[i
][nnb
[i
][j
]]);
577 fprintf(debug
, "\n");
582 fprintf(stderr
, "Linking structures ");
583 /* Use mcpy for temporary storage of booleans */
584 mcpy
= mk_matrix(n1
, n1
, FALSE
);
585 for (i
= 0; i
< n1
; i
++)
587 for (j
= i
+1; j
< n1
; j
++)
589 mcpy
[i
][j
] = jp_same(nnb
, i
, j
, P
);
594 fprintf(stderr
, "*");
596 for (i
= 0; i
< n1
; i
++)
598 for (j
= i
+1; j
< n1
; j
++)
602 diff
= c
[j
].clust
- c
[i
].clust
;
608 c
[j
].clust
= c
[i
].clust
;
612 c
[i
].clust
= c
[j
].clust
;
621 fprintf(stderr
, "\nSorting and renumbering clusters\n");
622 /* Sort on cluster number */
623 qsort(c
, n1
, sizeof(c
[0]), clust_id_comp
);
625 /* Renumber clusters */
627 for (k
= 1; k
< n1
; k
++)
629 if (c
[k
].clust
!= c
[k
-1].clust
)
641 for (k
= 0; k
< n1
; k
++)
643 clust
->cl
[c
[k
].conf
] = c
[k
].clust
;
647 for (k
= 0; (k
< n1
); k
++)
649 fprintf(debug
, "Cluster index for conformation %d: %d\n",
650 c
[k
].conf
, c
[k
].clust
);
654 /* Again, I don't see the point in this... (AF) */
655 /* for(i=0; (i<n1); i++) { */
656 /* for(j=0; (j<n1); j++) */
657 /* mcpy[c[i].conf][c[j].conf] = mat[i][j]; */
659 /* for(i=0; (i<n1); i++) { */
660 /* for(j=0; (j<n1); j++) */
661 /* mat[i][j] = mcpy[i][j]; */
663 done_matrix(n1
, &mcpy
);
666 for (i
= 0; (i
< n1
); i
++)
673 static void dump_nnb (FILE *fp
, const char *title
, int n1
, t_nnb
*nnb
)
677 /* dump neighbor list */
678 fprintf(fp
, "%s", title
);
679 for (i
= 0; (i
< n1
); i
++)
681 fprintf(fp
, "i:%5d #:%5d nbs:", i
, nnb
[i
].nr
);
682 for (j
= 0; j
< nnb
[i
].nr
; j
++)
684 fprintf(fp
, "%5d", nnb
[i
].nb
[j
]);
690 static void gromos(int n1
, real
**mat
, real rmsdcut
, t_clusters
*clust
)
694 int i
, j
, k
, j1
, max
;
696 /* Put all neighbors nearer than rmsdcut in the list */
697 fprintf(stderr
, "Making list of neighbors within cutoff ");
700 for (i
= 0; (i
< n1
); i
++)
704 /* put all neighbors within cut-off in list */
705 for (j
= 0; j
< n1
; j
++)
707 if (mat
[i
][j
] < rmsdcut
)
712 srenew(nnb
[i
].nb
, max
);
718 /* store nr of neighbors, we'll need that */
720 if (i
%(1+n1
/100) == 0)
722 fprintf(stderr
, "%3d%%\b\b\b\b", (i
*100+1)/n1
);
725 fprintf(stderr
, "%3d%%\n", 100);
728 /* sort neighbor list on number of neighbors, largest first */
729 qsort(nnb
, n1
, sizeof(nnb
[0]), nrnb_comp
);
733 dump_nnb(debug
, "Nearest neighborlist after sort.\n", n1
, nnb
);
736 /* turn first structure with all its neighbors (largest) into cluster
737 remove them from pool of structures and repeat for all remaining */
738 fprintf(stderr
, "Finding clusters %4d", 0);
739 /* cluster id's start at 1: */
743 /* set cluster id (k) for first item in neighborlist */
744 for (j
= 0; j
< nnb
[0].nr
; j
++)
746 clust
->cl
[nnb
[0].nb
[j
]] = k
;
752 /* adjust number of neighbors for others, taking removals into account: */
753 for (i
= 1; i
< n1
&& nnb
[i
].nr
; i
++)
756 for (j
= 0; j
< nnb
[i
].nr
; j
++)
758 /* if this neighbor wasn't removed */
759 if (clust
->cl
[nnb
[i
].nb
[j
]] == 0)
761 /* shift the rest (j1<=j) */
762 nnb
[i
].nb
[j1
] = nnb
[i
].nb
[j
];
767 /* now j1 is the new number of neighbors */
770 /* sort again on nnb[].nr, because we have new # neighbors: */
771 /* but we only need to sort upto i, i.e. when nnb[].nr>0 */
772 qsort(nnb
, i
, sizeof(nnb
[0]), nrnb_comp
);
774 fprintf(stderr
, "\b\b\b\b%4d", k
);
778 fprintf(stderr
, "\n");
782 fprintf(debug
, "Clusters (%d):\n", k
);
783 for (i
= 0; i
< n1
; i
++)
785 fprintf(debug
, " %3d", clust
->cl
[i
]);
787 fprintf(debug
, "\n");
793 rvec
**read_whole_trj(const char *fn
, int isize
, atom_id index
[], int skip
,
794 int *nframe
, real
**time
, const output_env_t oenv
, gmx_bool bPBC
, gmx_rmpbc_t gpbc
)
799 int i
, i0
, j
, max_nf
;
807 natom
= read_first_x(oenv
, &status
, fn
, &t
, &x
, box
);
814 gmx_rmpbc(gpbc
, natom
, box
, x
);
820 srenew(*time
, max_nf
);
825 /* Store only the interesting atoms */
826 for (j
= 0; (j
< isize
); j
++)
828 copy_rvec(x
[index
[j
]], xx
[i0
][j
]);
835 while (read_next_x(oenv
, status
, &t
, x
, box
));
836 fprintf(stderr
, "Allocated %lu bytes for frames\n",
837 (unsigned long) (max_nf
*isize
*sizeof(**xx
)));
838 fprintf(stderr
, "Read %d frames from trajectory %s\n", i0
, fn
);
845 static int plot_clusters(int nf
, real
**mat
, t_clusters
*clust
,
848 int i
, j
, ncluster
, ci
;
849 int *cl_id
, *nstruct
, *strind
;
854 for (i
= 0; i
< nf
; i
++)
857 cl_id
[i
] = clust
->cl
[i
];
861 for (i
= 0; i
< nf
; i
++)
863 if (nstruct
[i
] >= minstruct
)
866 for (j
= 0; (j
< nf
); j
++)
870 strind
[j
] = ncluster
;
876 fprintf(stderr
, "There are %d clusters with at least %d conformations\n",
877 ncluster
, minstruct
);
879 for (i
= 0; (i
< nf
); i
++)
882 for (j
= 0; j
< i
; j
++)
884 if ((ci
== cl_id
[j
]) && (nstruct
[ci
] >= minstruct
))
886 /* color different clusters with different colors, as long as
887 we don't run out of colors */
888 mat
[i
][j
] = strind
[i
];
903 static void mark_clusters(int nf
, real
**mat
, real val
, t_clusters
*clust
)
907 for (i
= 0; i
< nf
; i
++)
909 for (j
= 0; j
< i
; j
++)
911 if (clust
->cl
[i
] == clust
->cl
[j
])
923 static char *parse_filename(const char *fn
, int maxnr
)
932 gmx_fatal(FARGS
, "will not number filename %s containing '%c'", fn
, '%');
934 /* number of digits needed in numbering */
935 i
= (int)(log(maxnr
)/log(10)) + 1;
936 /* split fn and ext */
937 ext
= strrchr(fn
, '.');
940 gmx_fatal(FARGS
, "cannot separate extension in filename %s", fn
);
943 /* insert e.g. '%03d' between fn and ext */
944 sprintf(buf
, "%s%%0%dd.%s", fn
, i
, ext
);
945 snew(fnout
, strlen(buf
)+1);
951 static void ana_trans(t_clusters
*clust
, int nf
,
952 const char *transfn
, const char *ntransfn
, FILE *log
,
953 t_rgb rlo
, t_rgb rhi
, const output_env_t oenv
)
958 int i
, ntranst
, maxtrans
;
961 snew(ntrans
, clust
->ncl
);
962 snew(trans
, clust
->ncl
);
963 snew(axis
, clust
->ncl
);
964 for (i
= 0; i
< clust
->ncl
; i
++)
967 snew(trans
[i
], clust
->ncl
);
971 for (i
= 1; i
< nf
; i
++)
973 if (clust
->cl
[i
] != clust
->cl
[i
-1])
976 ntrans
[clust
->cl
[i
-1]-1]++;
977 ntrans
[clust
->cl
[i
]-1]++;
978 trans
[clust
->cl
[i
-1]-1][clust
->cl
[i
]-1]++;
979 maxtrans
= max(maxtrans
, trans
[clust
->cl
[i
]-1][clust
->cl
[i
-1]-1]);
982 ffprintf_dd(stderr
, log
, buf
, "Counted %d transitions in total, "
983 "max %d between two specific clusters\n", ntranst
, maxtrans
);
986 fp
= gmx_ffopen(transfn
, "w");
987 i
= min(maxtrans
+1, 80);
988 write_xpm(fp
, 0, "Cluster Transitions", "# transitions",
989 "from cluster", "to cluster",
990 clust
->ncl
, clust
->ncl
, axis
, axis
, trans
,
991 0, maxtrans
, rlo
, rhi
, &i
);
996 fp
= xvgropen(ntransfn
, "Cluster Transitions", "Cluster #", "# transitions",
998 for (i
= 0; i
< clust
->ncl
; i
++)
1000 fprintf(fp
, "%5d %5d\n", i
+1, ntrans
[i
]);
1005 for (i
= 0; i
< clust
->ncl
; i
++)
1013 static void analyze_clusters(int nf
, t_clusters
*clust
, real
**rmsd
,
1014 int natom
, t_atoms
*atoms
, rvec
*xtps
,
1015 real
*mass
, rvec
**xx
, real
*time
,
1016 int ifsize
, atom_id
*fitidx
,
1017 int iosize
, atom_id
*outidx
,
1018 const char *trxfn
, const char *sizefn
,
1019 const char *transfn
, const char *ntransfn
,
1020 const char *clustidfn
, gmx_bool bAverage
,
1021 int write_ncl
, int write_nst
, real rmsmin
,
1022 gmx_bool bFit
, FILE *log
, t_rgb rlo
, t_rgb rhi
,
1023 const output_env_t oenv
)
1026 char buf
[STRLEN
], buf1
[40], buf2
[40], buf3
[40], *trxsfn
;
1027 t_trxstatus
*trxout
= NULL
;
1028 t_trxstatus
*trxsout
= NULL
;
1029 int i
, i1
, cl
, nstr
, *structure
, first
= 0, midstr
;
1030 gmx_bool
*bWrite
= NULL
;
1031 real r
, clrmsd
, midrmsd
;
1037 ffprintf_d(stderr
, log
, buf
, "\nFound %d clusters\n\n", clust
->ncl
);
1041 /* do we write all structures? */
1044 trxsfn
= parse_filename(trxfn
, max(write_ncl
, clust
->ncl
));
1047 ffprintf_ss(stderr
, log
, buf
, "Writing %s structure for each cluster to %s\n",
1048 bAverage
? "average" : "middle", trxfn
);
1051 /* find out what we want to tell the user:
1052 Writing [all structures|structures with rmsd > %g] for
1053 {all|first %d} clusters {with more than %d structures} to %s */
1056 sprintf(buf1
, "structures with rmsd > %g", rmsmin
);
1060 sprintf(buf1
, "all structures");
1062 buf2
[0] = buf3
[0] = '\0';
1063 if (write_ncl
>= clust
->ncl
)
1067 sprintf(buf2
, "all ");
1072 sprintf(buf2
, "the first %d ", write_ncl
);
1076 sprintf(buf3
, " with more than %d structures", write_nst
);
1078 sprintf(buf
, "Writing %s for %sclusters%s to %s\n", buf1
, buf2
, buf3
, trxsfn
);
1079 ffprintf(stderr
, log
, buf
);
1082 /* Prepare a reference structure for the orientation of the clusters */
1085 reset_x(ifsize
, fitidx
, natom
, NULL
, xtps
, mass
);
1087 trxout
= open_trx(trxfn
, "w");
1088 /* Calculate the average structure in each cluster, *
1089 * all structures are fitted to the first struture of the cluster */
1093 if (transfn
|| ntransfn
)
1095 ana_trans(clust
, nf
, transfn
, ntransfn
, log
, rlo
, rhi
, oenv
);
1100 fp
= xvgropen(clustidfn
, "Clusters", output_env_get_xvgr_tlabel(oenv
), "Cluster #", oenv
);
1101 fprintf(fp
, "@ s0 symbol 2\n");
1102 fprintf(fp
, "@ s0 symbol size 0.2\n");
1103 fprintf(fp
, "@ s0 linestyle 0\n");
1104 for (i
= 0; i
< nf
; i
++)
1106 fprintf(fp
, "%8g %8d\n", time
[i
], clust
->cl
[i
]);
1112 fp
= xvgropen(sizefn
, "Cluster Sizes", "Cluster #", "# Structures", oenv
);
1113 fprintf(fp
, "@g%d type %s\n", 0, "bar");
1115 snew(structure
, nf
);
1116 fprintf(log
, "\n%3s | %3s %4s | %6s %4s | cluster members\n",
1117 "cl.", "#st", "rmsd", "middle", "rmsd");
1118 for (cl
= 1; cl
<= clust
->ncl
; cl
++)
1120 /* prepare structures (fit, middle, average) */
1123 for (i
= 0; i
< natom
; i
++)
1129 for (i1
= 0; i1
< nf
; i1
++)
1131 if (clust
->cl
[i1
] == cl
)
1133 structure
[nstr
] = i1
;
1135 if (trxfn
&& (bAverage
|| write_ncl
) )
1139 reset_x(ifsize
, fitidx
, natom
, NULL
, xx
[i1
], mass
);
1147 do_fit(natom
, mass
, xx
[first
], xx
[i1
]);
1151 for (i
= 0; i
< natom
; i
++)
1153 rvec_inc(xav
[i
], xx
[i1
][i
]);
1161 fprintf(fp
, "%8d %8d\n", cl
, nstr
);
1166 for (i1
= 0; i1
< nstr
; i1
++)
1171 for (i
= 0; i
< nstr
; i
++)
1175 r
+= rmsd
[structure
[i
]][structure
[i1
]];
1179 r
+= rmsd
[structure
[i1
]][structure
[i
]];
1186 midstr
= structure
[i1
];
1193 /* dump cluster info to logfile */
1196 sprintf(buf1
, "%6.3f", clrmsd
);
1201 sprintf(buf2
, "%5.3f", midrmsd
);
1209 sprintf(buf1
, "%5s", "");
1210 sprintf(buf2
, "%5s", "");
1212 fprintf(log
, "%3d | %3d %s | %6g%s |", cl
, nstr
, buf1
, time
[midstr
], buf2
);
1213 for (i
= 0; i
< nstr
; i
++)
1215 if ((i
% 7 == 0) && i
)
1217 sprintf(buf
, "\n%3s | %3s %4s | %6s %4s |", "", "", "", "", "");
1224 fprintf(log
, "%s %6g", buf
, time
[i1
]);
1228 /* write structures to trajectory file(s) */
1233 for (i
= 0; i
< nstr
; i
++)
1238 if (cl
< write_ncl
+1 && nstr
> write_nst
)
1240 /* Dump all structures for this cluster */
1241 /* generate numbered filename (there is a %d in trxfn!) */
1242 sprintf(buf
, trxsfn
, cl
);
1243 trxsout
= open_trx(buf
, "w");
1244 for (i
= 0; i
< nstr
; i
++)
1249 for (i1
= 0; i1
< i
&& bWrite
[i
]; i1
++)
1253 bWrite
[i
] = rmsd
[structure
[i1
]][structure
[i
]] > rmsmin
;
1259 write_trx(trxsout
, iosize
, outidx
, atoms
, i
, time
[structure
[i
]], zerobox
,
1260 xx
[structure
[i
]], NULL
, NULL
);
1265 /* Dump the average structure for this cluster */
1268 for (i
= 0; i
< natom
; i
++)
1270 svmul(1.0/nstr
, xav
[i
], xav
[i
]);
1275 for (i
= 0; i
< natom
; i
++)
1277 copy_rvec(xx
[midstr
][i
], xav
[i
]);
1281 reset_x(ifsize
, fitidx
, natom
, NULL
, xav
, mass
);
1286 do_fit(natom
, mass
, xtps
, xav
);
1289 write_trx(trxout
, iosize
, outidx
, atoms
, cl
, time
[midstr
], zerobox
, xav
, NULL
, NULL
);
1309 static void convert_mat(t_matrix
*mat
, t_mat
*rms
)
1314 matrix2real(mat
, rms
->mat
);
1315 /* free input xpm matrix data */
1316 for (i
= 0; i
< mat
->nx
; i
++)
1318 sfree(mat
->matrix
[i
]);
1322 for (i
= 0; i
< mat
->nx
; i
++)
1324 for (j
= i
; j
< mat
->nx
; j
++)
1326 rms
->sumrms
+= rms
->mat
[i
][j
];
1327 rms
->maxrms
= max(rms
->maxrms
, rms
->mat
[i
][j
]);
1330 rms
->minrms
= min(rms
->minrms
, rms
->mat
[i
][j
]);
1337 int gmx_cluster(int argc
, char *argv
[])
1339 const char *desc
[] = {
1340 "[THISMODULE] can cluster structures using several different methods.",
1341 "Distances between structures can be determined from a trajectory",
1342 "or read from an [TT].xpm[tt] matrix file with the [TT]-dm[tt] option.",
1343 "RMS deviation after fitting or RMS deviation of atom-pair distances",
1344 "can be used to define the distance between structures.[PAR]",
1346 "single linkage: add a structure to a cluster when its distance to any",
1347 "element of the cluster is less than [TT]cutoff[tt].[PAR]",
1349 "Jarvis Patrick: add a structure to a cluster when this structure",
1350 "and a structure in the cluster have each other as neighbors and",
1351 "they have a least [TT]P[tt] neighbors in common. The neighbors",
1352 "of a structure are the M closest structures or all structures within",
1353 "[TT]cutoff[tt].[PAR]",
1355 "Monte Carlo: reorder the RMSD matrix using Monte Carlo such that",
1356 "the order of the frames is using the smallest possible increments.",
1357 "With this it is possible to make a smooth animation going from one",
1358 "structure to another with the largest possible (e.g.) RMSD between",
1359 "them, however the intermediate steps should be as small as possible.",
1360 "Applications could be to visualize a potential of mean force",
1361 "ensemble of simulations or a pulling simulation. Obviously the user",
1362 "has to prepare the trajectory well (e.g. by not superimposing frames).",
1363 "The final result can be inspect visually by looking at the matrix",
1364 "[TT].xpm[tt] file, which should vary smoothly from bottom to top.[PAR]",
1366 "diagonalization: diagonalize the RMSD matrix.[PAR]",
1368 "gromos: use algorithm as described in Daura [IT]et al.[it]",
1369 "([IT]Angew. Chem. Int. Ed.[it] [BB]1999[bb], [IT]38[it], pp 236-240).",
1370 "Count number of neighbors using cut-off, take structure with",
1371 "largest number of neighbors with all its neighbors as cluster",
1372 "and eliminate it from the pool of clusters. Repeat for remaining",
1373 "structures in pool.[PAR]",
1375 "When the clustering algorithm assigns each structure to exactly one",
1376 "cluster (single linkage, Jarvis Patrick and gromos) and a trajectory",
1377 "file is supplied, the structure with",
1378 "the smallest average distance to the others or the average structure",
1379 "or all structures for each cluster will be written to a trajectory",
1380 "file. When writing all structures, separate numbered files are made",
1381 "for each cluster.[PAR]",
1383 "Two output files are always written:[BR]",
1384 "[TT]-o[tt] writes the RMSD values in the upper left half of the matrix",
1385 "and a graphical depiction of the clusters in the lower right half",
1386 "When [TT]-minstruct[tt] = 1 the graphical depiction is black",
1387 "when two structures are in the same cluster.",
1388 "When [TT]-minstruct[tt] > 1 different colors will be used for each",
1390 "[TT]-g[tt] writes information on the options used and a detailed list",
1391 "of all clusters and their members.[PAR]",
1393 "Additionally, a number of optional output files can be written:[BR]",
1394 "[TT]-dist[tt] writes the RMSD distribution.[BR]",
1395 "[TT]-ev[tt] writes the eigenvectors of the RMSD matrix",
1396 "diagonalization.[BR]",
1397 "[TT]-sz[tt] writes the cluster sizes.[BR]",
1398 "[TT]-tr[tt] writes a matrix of the number transitions between",
1399 "cluster pairs.[BR]",
1400 "[TT]-ntr[tt] writes the total number of transitions to or from",
1401 "each cluster.[BR]",
1402 "[TT]-clid[tt] writes the cluster number as a function of time.[BR]",
1403 "[TT]-cl[tt] writes average (with option [TT]-av[tt]) or central",
1404 "structure of each cluster or writes numbered files with cluster members",
1405 "for a selected set of clusters (with option [TT]-wcl[tt], depends on",
1406 "[TT]-nst[tt] and [TT]-rmsmin[tt]). The center of a cluster is the",
1407 "structure with the smallest average RMSD from all other structures",
1408 "of the cluster.[BR]",
1412 int nf
, i
, i1
, i2
, j
;
1413 gmx_int64_t nrms
= 0;
1416 rvec
*xtps
, *usextps
, *x1
, **xx
= NULL
;
1417 const char *fn
, *trx_out_fn
;
1419 t_mat
*rms
, *orig
= NULL
;
1424 t_matrix
*readmat
= NULL
;
1427 int isize
= 0, ifsize
= 0, iosize
= 0;
1428 atom_id
*index
= NULL
, *fitidx
, *outidx
;
1430 real rmsd
, **d1
, **d2
, *time
= NULL
, time_invfac
, *mass
= NULL
;
1431 char buf
[STRLEN
], buf1
[80], title
[STRLEN
];
1432 gmx_bool bAnalyze
, bUseRmsdCut
, bJP_RMSD
= FALSE
, bReadMat
, bReadTraj
, bPBC
= TRUE
;
1434 int method
, ncluster
= 0;
1435 static const char *methodname
[] = {
1436 NULL
, "linkage", "jarvis-patrick", "monte-carlo",
1437 "diagonalization", "gromos", NULL
1440 m_null
, m_linkage
, m_jarvis_patrick
,
1441 m_monte_carlo
, m_diagonalize
, m_gromos
, m_nr
1443 /* Set colors for plotting: white = zero RMS, black = maximum */
1444 static t_rgb rlo_top
= { 1.0, 1.0, 1.0 };
1445 static t_rgb rhi_top
= { 0.0, 0.0, 0.0 };
1446 static t_rgb rlo_bot
= { 1.0, 1.0, 1.0 };
1447 static t_rgb rhi_bot
= { 0.0, 0.0, 1.0 };
1448 static int nlevels
= 40, skip
= 1;
1449 static real scalemax
= -1.0, rmsdcut
= 0.1, rmsmin
= 0.0;
1450 gmx_bool bRMSdist
= FALSE
, bBinary
= FALSE
, bAverage
= FALSE
, bFit
= TRUE
;
1451 static int niter
= 10000, nrandom
= 0, seed
= 1993, write_ncl
= 0, write_nst
= 1, minstruct
= 1;
1452 static real kT
= 1e-3;
1453 static int M
= 10, P
= 3;
1455 gmx_rmpbc_t gpbc
= NULL
;
1458 { "-dista", FALSE
, etBOOL
, {&bRMSdist
},
1459 "Use RMSD of distances instead of RMS deviation" },
1460 { "-nlevels", FALSE
, etINT
, {&nlevels
},
1461 "Discretize RMSD matrix in this number of levels" },
1462 { "-cutoff", FALSE
, etREAL
, {&rmsdcut
},
1463 "RMSD cut-off (nm) for two structures to be neighbor" },
1464 { "-fit", FALSE
, etBOOL
, {&bFit
},
1465 "Use least squares fitting before RMSD calculation" },
1466 { "-max", FALSE
, etREAL
, {&scalemax
},
1467 "Maximum level in RMSD matrix" },
1468 { "-skip", FALSE
, etINT
, {&skip
},
1469 "Only analyze every nr-th frame" },
1470 { "-av", FALSE
, etBOOL
, {&bAverage
},
1471 "Write average iso middle structure for each cluster" },
1472 { "-wcl", FALSE
, etINT
, {&write_ncl
},
1473 "Write the structures for this number of clusters to numbered files" },
1474 { "-nst", FALSE
, etINT
, {&write_nst
},
1475 "Only write all structures if more than this number of structures per cluster" },
1476 { "-rmsmin", FALSE
, etREAL
, {&rmsmin
},
1477 "minimum rms difference with rest of cluster for writing structures" },
1478 { "-method", FALSE
, etENUM
, {methodname
},
1479 "Method for cluster determination" },
1480 { "-minstruct", FALSE
, etINT
, {&minstruct
},
1481 "Minimum number of structures in cluster for coloring in the [TT].xpm[tt] file" },
1482 { "-binary", FALSE
, etBOOL
, {&bBinary
},
1483 "Treat the RMSD matrix as consisting of 0 and 1, where the cut-off "
1484 "is given by [TT]-cutoff[tt]" },
1485 { "-M", FALSE
, etINT
, {&M
},
1486 "Number of nearest neighbors considered for Jarvis-Patrick algorithm, "
1487 "0 is use cutoff" },
1488 { "-P", FALSE
, etINT
, {&P
},
1489 "Number of identical nearest neighbors required to form a cluster" },
1490 { "-seed", FALSE
, etINT
, {&seed
},
1491 "Random number seed for Monte Carlo clustering algorithm: <= 0 means generate" },
1492 { "-niter", FALSE
, etINT
, {&niter
},
1493 "Number of iterations for MC" },
1494 { "-nrandom", FALSE
, etINT
, {&nrandom
},
1495 "The first iterations for MC may be done complete random, to shuffle the frames" },
1496 { "-kT", FALSE
, etREAL
, {&kT
},
1497 "Boltzmann weighting factor for Monte Carlo optimization "
1498 "(zero turns off uphill steps)" },
1499 { "-pbc", FALSE
, etBOOL
,
1500 { &bPBC
}, "PBC check" }
1503 { efTRX
, "-f", NULL
, ffOPTRD
},
1504 { efTPS
, "-s", NULL
, ffOPTRD
},
1505 { efNDX
, NULL
, NULL
, ffOPTRD
},
1506 { efXPM
, "-dm", "rmsd", ffOPTRD
},
1507 { efXPM
, "-om", "rmsd-raw", ffWRITE
},
1508 { efXPM
, "-o", "rmsd-clust", ffWRITE
},
1509 { efLOG
, "-g", "cluster", ffWRITE
},
1510 { efXVG
, "-dist", "rmsd-dist", ffOPTWR
},
1511 { efXVG
, "-ev", "rmsd-eig", ffOPTWR
},
1512 { efXVG
, "-conv", "mc-conv", ffOPTWR
},
1513 { efXVG
, "-sz", "clust-size", ffOPTWR
},
1514 { efXPM
, "-tr", "clust-trans", ffOPTWR
},
1515 { efXVG
, "-ntr", "clust-trans", ffOPTWR
},
1516 { efXVG
, "-clid", "clust-id.xvg", ffOPTWR
},
1517 { efTRX
, "-cl", "clusters.pdb", ffOPTWR
}
1519 #define NFILE asize(fnm)
1521 if (!parse_common_args(&argc
, argv
,
1522 PCA_CAN_VIEW
| PCA_CAN_TIME
| PCA_TIME_UNIT
| PCA_BE_NICE
,
1523 NFILE
, fnm
, asize(pa
), pa
, asize(desc
), desc
, 0, NULL
,
1530 bReadMat
= opt2bSet("-dm", NFILE
, fnm
);
1531 bReadTraj
= opt2bSet("-f", NFILE
, fnm
) || !bReadMat
;
1532 if (opt2parg_bSet("-av", asize(pa
), pa
) ||
1533 opt2parg_bSet("-wcl", asize(pa
), pa
) ||
1534 opt2parg_bSet("-nst", asize(pa
), pa
) ||
1535 opt2parg_bSet("-rmsmin", asize(pa
), pa
) ||
1536 opt2bSet("-cl", NFILE
, fnm
) )
1538 trx_out_fn
= opt2fn("-cl", NFILE
, fnm
);
1544 if (bReadMat
&& output_env_get_time_factor(oenv
) != 1)
1547 "\nWarning: assuming the time unit in %s is %s\n",
1548 opt2fn("-dm", NFILE
, fnm
), output_env_get_time_unit(oenv
));
1550 if (trx_out_fn
&& !bReadTraj
)
1552 fprintf(stderr
, "\nWarning: "
1553 "cannot write cluster structures without reading trajectory\n"
1554 " ignoring option -cl %s\n", trx_out_fn
);
1558 while (method
< m_nr
&& gmx_strcasecmp(methodname
[0], methodname
[method
]) != 0)
1564 gmx_fatal(FARGS
, "Invalid method");
1567 bAnalyze
= (method
== m_linkage
|| method
== m_jarvis_patrick
||
1568 method
== m_gromos
);
1571 log
= ftp2FILE(efLOG
, NFILE
, fnm
, "w");
1573 fprintf(stderr
, "Using %s method for clustering\n", methodname
[0]);
1574 fprintf(log
, "Using %s method for clustering\n", methodname
[0]);
1576 /* check input and write parameters to log file */
1577 bUseRmsdCut
= FALSE
;
1578 if (method
== m_jarvis_patrick
)
1580 bJP_RMSD
= (M
== 0) || opt2parg_bSet("-cutoff", asize(pa
), pa
);
1581 if ((M
< 0) || (M
== 1))
1583 gmx_fatal(FARGS
, "M (%d) must be 0 or larger than 1", M
);
1587 sprintf(buf1
, "Will use P=%d and RMSD cutoff (%g)", P
, rmsdcut
);
1594 gmx_fatal(FARGS
, "Number of neighbors required (P) must be less than M");
1598 sprintf(buf1
, "Will use P=%d, M=%d and RMSD cutoff (%g)", P
, M
, rmsdcut
);
1603 sprintf(buf1
, "Will use P=%d, M=%d", P
, M
);
1606 ffprintf_s(stderr
, log
, buf
, "%s for determining the neighbors\n\n", buf1
);
1608 else /* method != m_jarvis */
1610 bUseRmsdCut
= ( bBinary
|| method
== m_linkage
|| method
== m_gromos
);
1612 if (bUseRmsdCut
&& method
!= m_jarvis_patrick
)
1614 fprintf(log
, "Using RMSD cutoff %g nm\n", rmsdcut
);
1616 if (method
== m_monte_carlo
)
1618 fprintf(log
, "Using %d iterations\n", niter
);
1623 gmx_fatal(FARGS
, "skip (%d) should be >= 1", skip
);
1629 /* don't read mass-database as masses (and top) are not used */
1630 read_tps_conf(ftp2fn(efTPS
, NFILE
, fnm
), buf
, &top
, &ePBC
, &xtps
, NULL
, box
,
1634 gpbc
= gmx_rmpbc_init(&top
.idef
, ePBC
, top
.atoms
.nr
);
1637 fprintf(stderr
, "\nSelect group for least squares fit%s:\n",
1638 bReadMat
? "" : " and RMSD calculation");
1639 get_index(&(top
.atoms
), ftp2fn_null(efNDX
, NFILE
, fnm
),
1640 1, &ifsize
, &fitidx
, &grpname
);
1643 fprintf(stderr
, "\nSelect group for output:\n");
1644 get_index(&(top
.atoms
), ftp2fn_null(efNDX
, NFILE
, fnm
),
1645 1, &iosize
, &outidx
, &grpname
);
1646 /* merge and convert both index groups: */
1647 /* first copy outidx to index. let outidx refer to elements in index */
1648 snew(index
, iosize
);
1650 for (i
= 0; i
< iosize
; i
++)
1652 index
[i
] = outidx
[i
];
1655 /* now lookup elements from fitidx in index, add them if necessary
1656 and also let fitidx refer to elements in index */
1657 for (i
= 0; i
< ifsize
; i
++)
1660 while (j
< isize
&& index
[j
] != fitidx
[i
])
1666 /* slow this way, but doesn't matter much */
1668 srenew(index
, isize
);
1670 index
[j
] = fitidx
[i
];
1674 else /* !trx_out_fn */
1678 for (i
= 0; i
< ifsize
; i
++)
1680 index
[i
] = fitidx
[i
];
1688 /* Loop over first coordinate file */
1689 fn
= opt2fn("-f", NFILE
, fnm
);
1691 xx
= read_whole_trj(fn
, isize
, index
, skip
, &nf
, &time
, oenv
, bPBC
, gpbc
);
1692 output_env_conv_times(oenv
, nf
, time
);
1693 if (!bRMSdist
|| bAnalyze
)
1695 /* Center all frames on zero */
1697 for (i
= 0; i
< ifsize
; i
++)
1699 mass
[fitidx
[i
]] = top
.atoms
.atom
[index
[fitidx
[i
]]].m
;
1703 for (i
= 0; i
< nf
; i
++)
1705 reset_x(ifsize
, fitidx
, isize
, NULL
, xx
[i
], mass
);
1711 gmx_rmpbc_done(gpbc
);
1717 fprintf(stderr
, "Reading rms distance matrix ");
1718 read_xpm_matrix(opt2fn("-dm", NFILE
, fnm
), &readmat
);
1719 fprintf(stderr
, "\n");
1720 if (readmat
[0].nx
!= readmat
[0].ny
)
1722 gmx_fatal(FARGS
, "Matrix (%dx%d) is not square",
1723 readmat
[0].nx
, readmat
[0].ny
);
1725 if (bReadTraj
&& bAnalyze
&& (readmat
[0].nx
!= nf
))
1727 gmx_fatal(FARGS
, "Matrix size (%dx%d) does not match the number of "
1728 "frames (%d)", readmat
[0].nx
, readmat
[0].ny
, nf
);
1733 time
= readmat
[0].axis_x
;
1734 time_invfac
= output_env_get_time_invfactor(oenv
);
1735 for (i
= 0; i
< nf
; i
++)
1737 time
[i
] *= time_invfac
;
1740 rms
= init_mat(readmat
[0].nx
, method
== m_diagonalize
);
1741 convert_mat(&(readmat
[0]), rms
);
1743 nlevels
= readmat
[0].nmap
;
1745 else /* !bReadMat */
1747 rms
= init_mat(nf
, method
== m_diagonalize
);
1748 nrms
= ((gmx_int64_t
)nf
*((gmx_int64_t
)nf
-1))/2;
1751 fprintf(stderr
, "Computing %dx%d RMS deviation matrix\n", nf
, nf
);
1752 /* Initialize work array */
1754 for (i1
= 0; i1
< nf
; i1
++)
1756 for (i2
= i1
+1; i2
< nf
; i2
++)
1758 for (i
= 0; i
< isize
; i
++)
1760 copy_rvec(xx
[i1
][i
], x1
[i
]);
1764 do_fit(isize
, mass
, xx
[i2
], x1
);
1766 rmsd
= rmsdev(isize
, mass
, xx
[i2
], x1
);
1767 set_mat_entry(rms
, i1
, i2
, rmsd
);
1769 nrms
-= (gmx_int64_t
) (nf
-i1
-1);
1770 fprintf(stderr
, "\r# RMSD calculations left: " "%"GMX_PRId64
" ", nrms
);
1776 fprintf(stderr
, "Computing %dx%d RMS distance deviation matrix\n", nf
, nf
);
1778 /* Initiate work arrays */
1781 for (i
= 0; (i
< isize
); i
++)
1786 for (i1
= 0; i1
< nf
; i1
++)
1788 calc_dist(isize
, xx
[i1
], d1
);
1789 for (i2
= i1
+1; (i2
< nf
); i2
++)
1791 calc_dist(isize
, xx
[i2
], d2
);
1792 set_mat_entry(rms
, i1
, i2
, rms_dist(isize
, d1
, d2
));
1795 fprintf(stderr
, "\r# RMSD calculations left: " "%"GMX_PRId64
" ", nrms
);
1797 /* Clean up work arrays */
1798 for (i
= 0; (i
< isize
); i
++)
1806 fprintf(stderr
, "\n\n");
1808 ffprintf_gg(stderr
, log
, buf
, "The RMSD ranges from %g to %g nm\n",
1809 rms
->minrms
, rms
->maxrms
);
1810 ffprintf_g(stderr
, log
, buf
, "Average RMSD is %g\n", 2*rms
->sumrms
/(nf
*(nf
-1)));
1811 ffprintf_d(stderr
, log
, buf
, "Number of structures for matrix %d\n", nf
);
1812 ffprintf_g(stderr
, log
, buf
, "Energy of the matrix is %g.\n", mat_energy(rms
));
1813 if (bUseRmsdCut
&& (rmsdcut
< rms
->minrms
|| rmsdcut
> rms
->maxrms
) )
1815 fprintf(stderr
, "WARNING: rmsd cutoff %g is outside range of rmsd values "
1816 "%g to %g\n", rmsdcut
, rms
->minrms
, rms
->maxrms
);
1818 if (bAnalyze
&& (rmsmin
< rms
->minrms
) )
1820 fprintf(stderr
, "WARNING: rmsd minimum %g is below lowest rmsd value %g\n",
1821 rmsmin
, rms
->minrms
);
1823 if (bAnalyze
&& (rmsmin
> rmsdcut
) )
1825 fprintf(stderr
, "WARNING: rmsd minimum %g is above rmsd cutoff %g\n",
1829 /* Plot the rmsd distribution */
1830 rmsd_distribution(opt2fn("-dist", NFILE
, fnm
), rms
, oenv
);
1834 for (i1
= 0; (i1
< nf
); i1
++)
1836 for (i2
= 0; (i2
< nf
); i2
++)
1838 if (rms
->mat
[i1
][i2
] < rmsdcut
)
1840 rms
->mat
[i1
][i2
] = 0;
1844 rms
->mat
[i1
][i2
] = 1;
1854 /* Now sort the matrix and write it out again */
1855 gather(rms
, rmsdcut
, &clust
);
1858 /* Do a diagonalization */
1859 snew(eigenvalues
, nf
);
1860 snew(eigenvectors
, nf
*nf
);
1861 memcpy(eigenvectors
, rms
->mat
[0], nf
*nf
*sizeof(real
));
1862 eigensolver(eigenvectors
, nf
, 0, nf
, eigenvalues
, rms
->mat
[0]);
1863 sfree(eigenvectors
);
1865 fp
= xvgropen(opt2fn("-ev", NFILE
, fnm
), "RMSD matrix Eigenvalues",
1866 "Eigenvector index", "Eigenvalues (nm\\S2\\N)", oenv
);
1867 for (i
= 0; (i
< nf
); i
++)
1869 fprintf(fp
, "%10d %10g\n", i
, eigenvalues
[i
]);
1874 orig
= init_mat(rms
->nn
, FALSE
);
1876 copy_t_mat(orig
, rms
);
1877 mc_optimize(log
, rms
, time
, niter
, nrandom
, seed
, kT
,
1878 opt2fn_null("-conv", NFILE
, fnm
), oenv
);
1880 case m_jarvis_patrick
:
1881 jarvis_patrick(rms
->nn
, rms
->mat
, M
, P
, bJP_RMSD
? rmsdcut
: -1, &clust
);
1884 gromos(rms
->nn
, rms
->mat
, rmsdcut
, &clust
);
1887 gmx_fatal(FARGS
, "DEATH HORROR unknown method \"%s\"", methodname
[0]);
1890 if (method
== m_monte_carlo
|| method
== m_diagonalize
)
1892 fprintf(stderr
, "Energy of the matrix after clustering is %g.\n",
1900 ncluster
= plot_clusters(nf
, rms
->mat
, &clust
, minstruct
);
1904 mark_clusters(nf
, rms
->mat
, rms
->maxrms
, &clust
);
1906 init_t_atoms(&useatoms
, isize
, FALSE
);
1907 snew(usextps
, isize
);
1908 useatoms
.resinfo
= top
.atoms
.resinfo
;
1909 for (i
= 0; i
< isize
; i
++)
1911 useatoms
.atomname
[i
] = top
.atoms
.atomname
[index
[i
]];
1912 useatoms
.atom
[i
].resind
= top
.atoms
.atom
[index
[i
]].resind
;
1913 useatoms
.nres
= max(useatoms
.nres
, useatoms
.atom
[i
].resind
+1);
1914 copy_rvec(xtps
[index
[i
]], usextps
[i
]);
1916 useatoms
.nr
= isize
;
1917 analyze_clusters(nf
, &clust
, rms
->mat
, isize
, &useatoms
, usextps
, mass
, xx
, time
,
1918 ifsize
, fitidx
, iosize
, outidx
,
1919 bReadTraj
? trx_out_fn
: NULL
,
1920 opt2fn_null("-sz", NFILE
, fnm
),
1921 opt2fn_null("-tr", NFILE
, fnm
),
1922 opt2fn_null("-ntr", NFILE
, fnm
),
1923 opt2fn_null("-clid", NFILE
, fnm
),
1924 bAverage
, write_ncl
, write_nst
, rmsmin
, bFit
, log
,
1925 rlo_bot
, rhi_bot
, oenv
);
1929 if (bBinary
&& !bAnalyze
)
1931 /* Make the clustering visible */
1932 for (i2
= 0; (i2
< nf
); i2
++)
1934 for (i1
= i2
+1; (i1
< nf
); i1
++)
1936 if (rms
->mat
[i1
][i2
])
1938 rms
->mat
[i1
][i2
] = rms
->maxrms
;
1944 fp
= opt2FILE("-o", NFILE
, fnm
, "w");
1945 fprintf(stderr
, "Writing rms distance/clustering matrix ");
1948 write_xpm(fp
, 0, readmat
[0].title
, readmat
[0].legend
, readmat
[0].label_x
,
1949 readmat
[0].label_y
, nf
, nf
, readmat
[0].axis_x
, readmat
[0].axis_y
,
1950 rms
->mat
, 0.0, rms
->maxrms
, rlo_top
, rhi_top
, &nlevels
);
1954 sprintf(buf
, "Time (%s)", output_env_get_time_unit(oenv
));
1955 sprintf(title
, "RMS%sDeviation / Cluster Index",
1956 bRMSdist
? " Distance " : " ");
1959 write_xpm_split(fp
, 0, title
, "RMSD (nm)", buf
, buf
,
1960 nf
, nf
, time
, time
, rms
->mat
, 0.0, rms
->maxrms
, &nlevels
,
1961 rlo_top
, rhi_top
, 0.0, (real
) ncluster
,
1962 &ncluster
, TRUE
, rlo_bot
, rhi_bot
);
1966 write_xpm(fp
, 0, title
, "RMSD (nm)", buf
, buf
,
1967 nf
, nf
, time
, time
, rms
->mat
, 0.0, rms
->maxrms
,
1968 rlo_top
, rhi_top
, &nlevels
);
1971 fprintf(stderr
, "\n");
1975 fp
= opt2FILE("-om", NFILE
, fnm
, "w");
1976 sprintf(buf
, "Time (%s)", output_env_get_time_unit(oenv
));
1977 sprintf(title
, "RMS%sDeviation", bRMSdist
? " Distance " : " ");
1978 write_xpm(fp
, 0, title
, "RMSD (nm)", buf
, buf
,
1979 nf
, nf
, time
, time
, orig
->mat
, 0.0, orig
->maxrms
,
1980 rlo_top
, rhi_top
, &nlevels
);
1985 /* now show what we've done */
1986 do_view(oenv
, opt2fn("-o", NFILE
, fnm
), "-nxy");
1987 do_view(oenv
, opt2fn_null("-sz", NFILE
, fnm
), "-nxy");
1988 if (method
== m_diagonalize
)
1990 do_view(oenv
, opt2fn_null("-ev", NFILE
, fnm
), "-nxy");
1992 do_view(oenv
, opt2fn("-dist", NFILE
, fnm
), "-nxy");
1995 do_view(oenv
, opt2fn_null("-tr", NFILE
, fnm
), "-nxy");
1996 do_view(oenv
, opt2fn_null("-ntr", NFILE
, fnm
), "-nxy");
1997 do_view(oenv
, opt2fn_null("-clid", NFILE
, fnm
), "-nxy");
1999 do_view(oenv
, opt2fn_null("-conv", NFILE
, fnm
), NULL
);