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.
47 #include "gromacs/utility/cstringutil.h"
50 #include "gromacs/utility/smalloc.h"
56 #include "gromacs/fileio/futil.h"
57 #include "gromacs/fileio/gmxfio.h"
58 #include "gromacs/fileio/strdb.h"
59 #include "gmx_fatal.h"
61 const char gmx_residuetype_undefined[] = "Other";
63 struct gmx_residuetype
72 static gmx_bool gmx_ask_yesno(gmx_bool bASK)
80 c = toupper(fgetc(stdin));
82 while ((c != 'Y') && (c != 'N'));
92 t_blocka *new_blocka(void)
97 snew(block->index, 1);
102 void write_index(const char *outf, t_blocka *b, char **gnames, gmx_bool bDuplicate, int natoms)
107 out = gmx_fio_fopen(outf, "w");
108 /* fprintf(out,"%5d %5d\n",b->nr,b->nra); */
109 for (i = 0; (i < b->nr); i++)
111 fprintf(out, "[ %s ]\n", gnames[i]);
112 for (k = 0, j = b->index[i]; j < b->index[i+1]; j++, k++)
114 fprintf(out, "%4d ", b->a[j]+1);
123 /* Duplicate copy, useful for computational electrophysiology double-layer setups */
126 fprintf(stderr, "Duplicating the whole system with an atom offset of %d atoms.\n", natoms);
127 for (i = 0; (i < b->nr); i++)
129 fprintf(out, "[ %s_copy ]\n", gnames[i]);
130 for (k = 0, j = b->index[i]; j < b->index[i+1]; j++, k++)
132 fprintf(out, "%4d ", b->a[j]+1 + natoms );
145 void add_grp(t_blocka *b, char ***gnames, int nra, atom_id a[], const char *name)
149 srenew(b->index, b->nr+2);
150 srenew(*gnames, b->nr+1);
151 (*gnames)[b->nr] = strdup(name);
153 srenew(b->a, b->nra+nra);
154 for (i = 0; (i < nra); i++)
156 b->a[b->nra++] = a[i];
159 b->index[b->nr] = b->nra;
162 /* compare index in `a' with group in `b' at `index',
163 when `index'<0 it is relative to end of `b' */
164 static gmx_bool grp_cmp(t_blocka *b, int nra, atom_id a[], int index)
170 index = b->nr-1+index;
174 gmx_fatal(FARGS, "no such index group %d in t_blocka (nr=%d)", index, b->nr);
177 if (nra != b->index[index+1] - b->index[index])
181 for (i = 0; i < nra; i++)
183 if (a[i] != b->a[b->index[index]+i])
192 p_status(const char **restype, int nres, const char **typenames, int ntypes)
199 snew(counter, ntypes);
200 for (i = 0; i < ntypes; i++)
204 for (i = 0; i < nres; i++)
207 for (j = 0; j < ntypes; j++)
209 if (!gmx_strcasecmp(restype[i], typenames[j]))
216 for (i = 0; (i < ntypes); i++)
220 printf("There are: %5d %10s residues\n", counter[i], typenames[i]);
229 mk_aid(t_atoms *atoms, const char ** restype, const char * typestring, int *nra, gmx_bool bMatch)
230 /* Make an array of atom_ids for all atoms with residuetypes matching typestring, or the opposite if bMatch is false */
238 for (i = 0; (i < atoms->nr); i++)
240 res = !gmx_strcasecmp(restype[atoms->atom[i].resind], typestring);
260 static void analyse_other(const char ** restype, t_atoms *atoms,
261 t_blocka *gb, char ***gn, gmx_bool bASK, gmx_bool bVerb)
263 restp_t *restp = NULL;
266 atom_id *other_ndx, *aid, *aaid;
267 int i, j, k, l, resind, naid, naaid, natp, nrestp = 0;
269 for (i = 0; (i < atoms->nres); i++)
271 if (gmx_strcasecmp(restype[i], "Protein") && gmx_strcasecmp(restype[i], "DNA") && gmx_strcasecmp(restype[i], "RNA") && gmx_strcasecmp(restype[i], "Water"))
281 printf("Analysing residues not classified as Protein/DNA/RNA/Water and splitting into groups...\n");
283 snew(other_ndx, atoms->nr);
284 for (k = 0; (k < atoms->nr); k++)
286 resind = atoms->atom[k].resind;
287 rname = *atoms->resinfo[resind].name;
288 if (gmx_strcasecmp(restype[resind], "Protein") && gmx_strcasecmp(restype[resind], "DNA") &&
289 gmx_strcasecmp(restype[resind], "RNA") && gmx_strcasecmp(restype[resind], "Water"))
292 for (l = 0; (l < nrestp); l++)
294 if (strcmp(restp[l].rname, rname) == 0)
301 srenew(restp, nrestp+1);
302 restp[nrestp].rname = strdup(rname);
303 restp[nrestp].bNeg = FALSE;
304 restp[nrestp].gname = strdup(rname);
310 for (i = 0; (i < nrestp); i++)
312 snew(aid, atoms->nr);
314 for (j = 0; (j < atoms->nr); j++)
316 rname = *atoms->resinfo[atoms->atom[j].resind].name;
317 if ((strcmp(restp[i].rname, rname) == 0 && !restp[i].bNeg) ||
318 (strcmp(restp[i].rname, rname) != 0 && restp[i].bNeg))
323 add_grp(gb, gn, naid, aid, restp[i].gname);
326 printf("split %s into atoms (y/n) ? ", restp[i].gname);
328 if (gmx_ask_yesno(bASK))
331 for (k = 0; (k < naid); k++)
333 aname = *atoms->atomname[aid[k]];
334 for (l = 0; (l < natp); l++)
336 if (strcmp(aname, attp[l]) == 0)
343 srenew(attp, ++natp);
344 attp[natp-1] = aname;
349 for (l = 0; (l < natp); l++)
353 for (k = 0; (k < naid); k++)
355 aname = *atoms->atomname[aid[k]];
356 if (strcmp(aname, attp[l]) == 0)
358 aaid[naaid++] = aid[k];
361 add_grp(gb, gn, naaid, aaid, attp[l]);
376 * Cata necessary to construct a single (protein) index group in
379 typedef struct gmx_help_make_index_group
381 /** The set of atom names that will be used to form this index group */
382 const char **defining_atomnames;
383 /** Size of the defining_atomnames array */
384 const int num_defining_atomnames;
385 /** Name of this index group */
386 const char *group_name;
387 /** Whether the above atom names name the atoms in the group, or
388 those not in the group */
389 gmx_bool bTakeComplement;
390 /** The index in wholename gives the first item in the arrays of
391 atomnames that should be tested with 'gmx_strncasecmp' in stead of
392 gmx_strcasecmp, or -1 if all items should be tested with strcasecmp
393 This is comparable to using a '*' wildcard at the end of specific
394 atom names, but that is more involved to implement...
397 /** Only create this index group if it differs from the one specified in compareto,
398 where -1 means to always create this group. */
400 } t_gmx_help_make_index_group;
402 static void analyse_prot(const char ** restype, t_atoms *atoms,
403 t_blocka *gb, char ***gn, gmx_bool bASK, gmx_bool bVerb)
405 /* lists of atomnames to be used in constructing index groups: */
406 static const char *pnoh[] = { "H", "HN" };
407 static const char *pnodum[] = {
408 "MN1", "MN2", "MCB1", "MCB2", "MCG1", "MCG2",
409 "MCD1", "MCD2", "MCE1", "MCE2", "MNZ1", "MNZ2"
411 static const char *calpha[] = { "CA" };
412 static const char *bb[] = { "N", "CA", "C" };
413 static const char *mc[] = { "N", "CA", "C", "O", "O1", "O2", "OC1", "OC2", "OT", "OXT" };
414 static const char *mcb[] = { "N", "CA", "CB", "C", "O", "O1", "O2", "OC1", "OC2", "OT", "OXT" };
415 static const char *mch[] = {
416 "N", "CA", "C", "O", "O1", "O2", "OC1", "OC2", "OT", "OXT",
417 "H1", "H2", "H3", "H", "HN"
420 static const t_gmx_help_make_index_group constructing_data[] =
421 {{ NULL, 0, "Protein", TRUE, -1, -1},
422 { pnoh, asize(pnoh), "Protein-H", TRUE, 0, -1},
423 { calpha, asize(calpha), "C-alpha", FALSE, -1, -1},
424 { bb, asize(bb), "Backbone", FALSE, -1, -1},
425 { mc, asize(mc), "MainChain", FALSE, -1, -1},
426 { mcb, asize(mcb), "MainChain+Cb", FALSE, -1, -1},
427 { mch, asize(mch), "MainChain+H", FALSE, -1, -1},
428 { mch, asize(mch), "SideChain", TRUE, -1, -1},
429 { mch, asize(mch), "SideChain-H", TRUE, 11, -1},
430 { pnodum, asize(pnodum), "Prot-Masses", TRUE, -1, 0}, };
431 const int num_index_groups = asize(constructing_data);
435 int nra, nnpres, npres;
437 char ndx_name[STRLEN], *atnm;
442 printf("Analysing Protein...\n");
444 snew(aid, atoms->nr);
446 /* calculate the number of protein residues */
448 for (i = 0; (i < atoms->nres); i++)
450 if (0 == gmx_strcasecmp(restype[i], "Protein"))
455 /* find matching or complement atoms */
456 for (i = 0; (i < (int)num_index_groups); i++)
459 for (n = 0; (n < atoms->nr); n++)
461 if (0 == gmx_strcasecmp(restype[atoms->atom[n].resind], "Protein"))
464 for (j = 0; (j < constructing_data[i].num_defining_atomnames); j++)
466 /* skip digits at beginning of atomname, e.g. 1H */
467 atnm = *atoms->atomname[n];
468 while (isdigit(atnm[0]))
472 if ( (constructing_data[i].wholename == -1) || (j < constructing_data[i].wholename) )
474 if (0 == gmx_strcasecmp(constructing_data[i].defining_atomnames[j], atnm))
481 if (0 == gmx_strncasecmp(constructing_data[i].defining_atomnames[j], atnm, strlen(constructing_data[i].defining_atomnames[j])))
487 if (constructing_data[i].bTakeComplement != match)
493 /* if we want to add this group always or it differs from previous
495 if (-1 == constructing_data[i].compareto || !grp_cmp(gb, nra, aid, constructing_data[i].compareto-i) )
497 add_grp(gb, gn, nra, aid, constructing_data[i].group_name);
503 for (i = 0; (i < (int)num_index_groups); i++)
505 printf("Split %12s into %5d residues (y/n) ? ", constructing_data[i].group_name, npres);
506 if (gmx_ask_yesno(bASK))
510 for (n = 0; ((atoms->atom[n].resind < npres) && (n < atoms->nr)); )
512 resind = atoms->atom[n].resind;
513 for (; ((atoms->atom[n].resind == resind) && (n < atoms->nr)); n++)
516 for (j = 0; (j < constructing_data[i].num_defining_atomnames); j++)
518 if (0 == gmx_strcasecmp(constructing_data[i].defining_atomnames[j], *atoms->atomname[n]))
523 if (constructing_data[i].bTakeComplement != match)
528 /* copy the residuename to the tail of the groupname */
532 ri = &atoms->resinfo[resind];
533 sprintf(ndx_name, "%s_%s%d%c",
534 constructing_data[i].group_name, *ri->name, ri->nr, ri->ic == ' ' ? '\0' : ri->ic);
535 add_grp(gb, gn, nra, aid, ndx_name);
541 printf("Make group with sidechain and C=O swapped (y/n) ? ");
542 if (gmx_ask_yesno(bASK))
544 /* Make swap sidechain C=O index */
547 for (n = 0; ((atoms->atom[n].resind < npres) && (n < atoms->nr)); )
549 resind = atoms->atom[n].resind;
551 for (; ((atoms->atom[n].resind == resind) && (n < atoms->nr)); n++)
553 if (strcmp("CA", *atoms->atomname[n]) == 0)
559 else if (strcmp("C", *atoms->atomname[n]) == 0)
563 gmx_incons("Atom naming problem");
567 else if (strcmp("O", *atoms->atomname[n]) == 0)
571 gmx_incons("Atom naming problem");
575 else if (strcmp("O1", *atoms->atomname[n]) == 0)
579 gmx_incons("Atom naming problem");
589 /* copy the residuename to the tail of the groupname */
592 add_grp(gb, gn, nra, aid, "SwapSC-CO");
603 /* Return 0 if the name was found, otherwise -1.
604 * p_restype is set to a pointer to the type name, or 'Other' if we did not find it.
607 gmx_residuetype_get_type(gmx_residuetype_t rt, const char * resname, const char ** p_restype)
612 for (i = 0; i < rt->n && rc; i++)
614 rc = gmx_strcasecmp(rt->resname[i], resname);
617 *p_restype = (rc == 0) ? rt->restype[i-1] : gmx_residuetype_undefined;
623 gmx_residuetype_add(gmx_residuetype_t rt, const char *newresname, const char *newrestype)
627 const char * p_oldtype;
629 found = !gmx_residuetype_get_type(rt, newresname, &p_oldtype);
631 if (found && gmx_strcasecmp(p_oldtype, newrestype))
633 fprintf(stderr, "Warning: Residue '%s' already present with type '%s' in database, ignoring new type '%s'.",
634 newresname, p_oldtype, newrestype);
639 srenew(rt->resname, rt->n+1);
640 srenew(rt->restype, rt->n+1);
641 rt->resname[rt->n] = strdup(newresname);
642 rt->restype[rt->n] = strdup(newrestype);
651 gmx_residuetype_init(gmx_residuetype_t *prt)
655 char resname[STRLEN], restype[STRLEN], dum[STRLEN];
658 struct gmx_residuetype *rt;
667 db = libopen("residuetypes.dat");
669 while (get_a_line(db, line, STRLEN))
675 if (sscanf(line, "%s %s %s", resname, restype, dum) != 2)
677 gmx_fatal(FARGS, "Incorrect number of columns (2 expected) for line in residuetypes.dat");
679 gmx_residuetype_add(rt, resname, restype);
691 gmx_residuetype_destroy(gmx_residuetype_t rt)
695 for (i = 0; i < rt->n; i++)
697 sfree(rt->resname[i]);
698 sfree(rt->restype[i]);
708 gmx_residuetype_get_alltypes(gmx_residuetype_t rt,
709 const char *** p_typenames,
714 const char ** my_typename;
720 for (i = 0; i < rt->n; i++)
724 for (j = 0; j < n && !found; j++)
726 found = !gmx_strcasecmp(p, my_typename[j]);
731 srenew(my_typename, n+1);
737 *p_typenames = my_typename;
745 gmx_residuetype_is_protein(gmx_residuetype_t rt, const char *resnm)
750 if (gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
751 gmx_strcasecmp(p_type, "Protein") == 0)
763 gmx_residuetype_is_dna(gmx_residuetype_t rt, const char *resnm)
768 if (gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
769 gmx_strcasecmp(p_type, "DNA") == 0)
781 gmx_residuetype_is_rna(gmx_residuetype_t rt, const char *resnm)
786 if (gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
787 gmx_strcasecmp(p_type, "RNA") == 0)
798 /* Return the size of the arrays */
800 gmx_residuetype_get_size(gmx_residuetype_t rt)
805 /* Search for a residuetype with name resnm within the
806 * gmx_residuetype database. Return the index if found,
810 gmx_residuetype_get_index(gmx_residuetype_t rt, const char *resnm)
815 for (i = 0; i < rt->n && rc; i++)
817 rc = gmx_strcasecmp(rt->resname[i], resnm);
820 return (0 == rc) ? i-1 : -1;
823 /* Return the name of the residuetype with the given index, or
824 * NULL if not found. */
826 gmx_residuetype_get_name(gmx_residuetype_t rt, int index)
828 if (index >= 0 && index < rt->n)
830 return rt->resname[index];
840 void analyse(t_atoms *atoms, t_blocka *gb, char ***gn, gmx_bool bASK, gmx_bool bVerb)
842 gmx_residuetype_t rt = NULL;
845 const char ** restype;
851 const char ** p_typename;
858 printf("Analysing residue names:\n");
860 /* Create system group, every single atom */
861 snew(aid, atoms->nr);
862 for (i = 0; i < atoms->nr; i++)
866 add_grp(gb, gn, atoms->nr, aid, "System");
869 /* For every residue, get a pointer to the residue type name */
870 gmx_residuetype_init(&rt);
873 snew(restype, atoms->nres);
876 for (i = 0; i < atoms->nres; i++)
878 resnm = *atoms->resinfo[i].name;
879 gmx_residuetype_get_type(rt, resnm, &(restype[i]));
881 /* Note that this does not lead to a N*N loop, but N*K, where
882 * K is the number of residue _types_, which is small and independent of N.
885 for (k = 0; k < ntypes && !found; k++)
887 found = !strcmp(restype[i], p_typename[k]);
891 srenew(p_typename, ntypes+1);
892 p_typename[ntypes++] = strdup(restype[i]);
898 p_status(restype, atoms->nres, p_typename, ntypes);
901 for (k = 0; k < ntypes; k++)
903 aid = mk_aid(atoms, restype, p_typename[k], &nra, TRUE);
905 /* Check for special types to do fancy stuff with */
907 if (!gmx_strcasecmp(p_typename[k], "Protein") && nra > 0)
911 analyse_prot(restype, atoms, gb, gn, bASK, bVerb);
913 /* Create a Non-Protein group */
914 aid = mk_aid(atoms, restype, "Protein", &nra, FALSE);
915 if ((nra > 0) && (nra < atoms->nr))
917 add_grp(gb, gn, nra, aid, "non-Protein");
921 else if (!gmx_strcasecmp(p_typename[k], "Water") && nra > 0)
923 add_grp(gb, gn, nra, aid, p_typename[k]);
924 /* Add this group as 'SOL' too, for backward compatibility with older gromacs versions */
925 add_grp(gb, gn, nra, aid, "SOL");
929 /* Solvent, create a negated group too */
930 aid = mk_aid(atoms, restype, "Water", &nra, FALSE);
931 if ((nra > 0) && (nra < atoms->nr))
933 add_grp(gb, gn, nra, aid, "non-Water");
940 add_grp(gb, gn, nra, aid, p_typename[k]);
942 analyse_other(restype, atoms, gb, gn, bASK, bVerb);
948 gmx_residuetype_destroy(rt);
950 /* Create a merged water_and_ions group */
956 for (i = 0; i < gb->nr; i++)
958 if (!gmx_strcasecmp((*gn)[i], "Water"))
961 nwater = gb->index[i+1]-gb->index[i];
963 else if (!gmx_strcasecmp((*gn)[i], "Ion"))
966 nion = gb->index[i+1]-gb->index[i];
970 if (nwater > 0 && nion > 0)
972 srenew(gb->index, gb->nr+2);
973 srenew(*gn, gb->nr+1);
974 (*gn)[gb->nr] = strdup("Water_and_ions");
975 srenew(gb->a, gb->nra+nwater+nion);
978 for (i = gb->index[iwater]; i < gb->index[iwater+1]; i++)
980 gb->a[gb->nra++] = gb->a[i];
985 for (i = gb->index[iion]; i < gb->index[iion+1]; i++)
987 gb->a[gb->nra++] = gb->a[i];
991 gb->index[gb->nr] = gb->nra;
996 void check_index(char *gname, int n, atom_id index[], char *traj, int natoms)
1000 for (i = 0; i < n; i++)
1002 if (index[i] >= natoms)
1004 gmx_fatal(FARGS, "%s atom number (index[%d]=%d) is larger than the number of atoms in %s (%d)",
1005 gname ? gname : "Index", i+1, index[i]+1,
1006 traj ? traj : "the trajectory", natoms);
1008 else if (index[i] < 0)
1010 gmx_fatal(FARGS, "%s atom number (index[%d]=%d) is less than zero",
1011 gname ? gname : "Index", i+1, index[i]+1);
1016 t_blocka *init_index(const char *gfile, char ***grpname)
1021 int i, j, ng, nread;
1022 char line[STRLEN], *pt, str[STRLEN];
1024 in = gmx_fio_fopen(gfile, "r");
1026 get_a_line(in, line, STRLEN);
1038 if (get_header(line, str))
1041 srenew(b->index, b->nr+1);
1042 srenew(*grpname, b->nr);
1047 b->index[b->nr] = b->index[b->nr-1];
1048 (*grpname)[b->nr-1] = strdup(str);
1054 gmx_fatal(FARGS, "The first header of your indexfile is invalid");
1057 while (sscanf(pt, "%s", str) == 1)
1059 i = b->index[b->nr];
1060 if (i >= maxentries)
1063 srenew(b->a, maxentries);
1065 b->a[i] = strtol(str, NULL, 10)-1;
1068 pt = strstr(pt, str)+strlen(str);
1072 while (get_a_line(in, line, STRLEN));
1077 sscanf(line, "%d%d", &b->nr, &b->nra);
1078 snew(b->index, b->nr+1);
1079 snew(*grpname, b->nr);
1082 for (i = 0; (i < b->nr); i++)
1084 nread = fscanf(in, "%s%d", str, &ng);
1085 (*grpname)[i] = strdup(str);
1086 b->index[i+1] = b->index[i]+ng;
1087 if (b->index[i+1] > b->nra)
1089 gmx_fatal(FARGS, "Something wrong in your indexfile at group %s", str);
1091 for (j = 0; (j < ng); j++)
1093 nread = fscanf(in, "%d", &a);
1094 b->a[b->index[i]+j] = a;
1100 for (i = 0; (i < b->nr); i++)
1102 for (j = b->index[i]; (j < b->index[i+1]); j++)
1106 fprintf(stderr, "\nWARNING: negative index %d in group %s\n\n",
1107 b->a[j], (*grpname)[i]);
1115 static void minstring(char *str)
1119 for (i = 0; (i < (int)strlen(str)); i++)
1128 int find_group(char s[], int ngrps, char **grpname)
1131 char string[STRLEN];
1137 /* first look for whole name match */
1140 for (i = 0; i < ngrps; i++)
1142 if (gmx_strcasecmp_min(s, grpname[i]) == 0)
1152 /* second look for first string match */
1155 for (i = 0; i < ngrps; i++)
1157 if (gmx_strncasecmp_min(s, grpname[i], n) == 0)
1167 /* last look for arbitrary substring match */
1172 for (i = 0; i < ngrps; i++)
1174 strcpy(string, grpname[i]);
1177 if (strstr(string, s) != NULL)
1189 printf("Error: Multiple groups '%s' selected\n", s);
1195 static int qgroup(int *a, int ngrps, char **grpname)
1204 fprintf(stderr, "Select a group: ");
1207 if (scanf("%s", s) != 1)
1209 gmx_fatal(FARGS, "Cannot read from input");
1211 trim(s); /* remove spaces */
1213 while (strlen(s) == 0);
1214 aa = strtol(s, &end, 10);
1215 if (aa == 0 && end[0] != '\0') /* string entered */
1217 aa = find_group(s, ngrps, grpname);
1219 bInRange = (aa >= 0 && aa < ngrps);
1222 printf("Error: No such group '%s'\n", s);
1226 printf("Selected %d: '%s'\n", aa, grpname[aa]);
1231 static void rd_groups(t_blocka *grps, char **grpname, char *gnames[],
1232 int ngrps, int isize[], atom_id *index[], int grpnr[])
1238 gmx_fatal(FARGS, "Error: no groups in indexfile");
1240 for (i = 0; (i < grps->nr); i++)
1242 fprintf(stderr, "Group %5d (%15s) has %5d elements\n", i, grpname[i],
1243 grps->index[i+1]-grps->index[i]);
1245 for (i = 0; (i < ngrps); i++)
1251 gnr1 = qgroup(&grpnr[i], grps->nr, grpname);
1252 if ((gnr1 < 0) || (gnr1 >= grps->nr))
1254 fprintf(stderr, "Select between %d and %d.\n", 0, grps->nr-1);
1257 while ((gnr1 < 0) || (gnr1 >= grps->nr));
1261 fprintf(stderr, "There is one group in the index\n");
1264 gnames[i] = strdup(grpname[gnr1]);
1265 isize[i] = grps->index[gnr1+1]-grps->index[gnr1];
1266 snew(index[i], isize[i]);
1267 for (j = 0; (j < isize[i]); j++)
1269 index[i][j] = grps->a[grps->index[gnr1]+j];
1274 void rd_index(const char *statfile, int ngrps, int isize[],
1275 atom_id *index[], char *grpnames[])
1284 gmx_fatal(FARGS, "No index file specified");
1286 grps = init_index(statfile, &gnames);
1287 rd_groups(grps, gnames, grpnames, ngrps, isize, index, grpnr);
1290 void rd_index_nrs(char *statfile, int ngrps, int isize[],
1291 atom_id *index[], char *grpnames[], int grpnr[])
1298 gmx_fatal(FARGS, "No index file specified");
1300 grps = init_index(statfile, &gnames);
1302 rd_groups(grps, gnames, grpnames, ngrps, isize, index, grpnr);
1305 void get_index(t_atoms *atoms, const char *fnm, int ngrps,
1306 int isize[], atom_id *index[], char *grpnames[])
1309 t_blocka *grps = NULL;
1316 grps = init_index(fnm, gnames);
1321 snew(grps->index, 1);
1322 analyse(atoms, grps, gnames, FALSE, FALSE);
1326 gmx_incons("You need to supply a valid atoms structure or a valid index file name");
1329 rd_groups(grps, *gnames, grpnames, ngrps, isize, index, grpnr);
1332 t_cluster_ndx *cluster_index(FILE *fplog, const char *ndx)
1338 c->clust = init_index(ndx, &c->grpname);
1340 for (i = 0; (i < c->clust->nra); i++)
1342 c->maxframe = max(c->maxframe, c->clust->a[i]);
1344 fprintf(fplog ? fplog : stdout,
1345 "There are %d clusters containing %d structures, highest framenr is %d\n",
1346 c->clust->nr, c->clust->nra, c->maxframe);
1349 pr_blocka(debug, 0, "clust", c->clust, TRUE);
1350 for (i = 0; (i < c->clust->nra); i++)
1352 if ((c->clust->a[i] < 0) || (c->clust->a[i] > c->maxframe))
1354 gmx_fatal(FARGS, "Range check error for c->clust->a[%d] = %d\n"
1355 "should be within 0 and %d", i, c->clust->a[i], c->maxframe+1);
1359 c->inv_clust = make_invblocka(c->clust, c->maxframe);