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.
37 #include "gromacs/topology/index.h"
50 #include "gromacs/legacyheaders/macros.h"
51 #include "gromacs/legacyheaders/txtdump.h"
53 #include "gromacs/fileio/gmxfio.h"
54 #include "gromacs/fileio/strdb.h"
55 #include "gromacs/topology/atoms.h"
56 #include "gromacs/topology/block.h"
57 #include "gromacs/topology/invblock.h"
58 #include "gromacs/utility/common.h"
59 #include "gromacs/utility/cstringutil.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/futil.h"
62 #include "gromacs/utility/smalloc.h"
64 const char gmx_residuetype_undefined[] = "Other";
66 struct gmx_residuetype
75 static gmx_bool gmx_ask_yesno(gmx_bool bASK)
83 c = toupper(fgetc(stdin));
85 while ((c != 'Y') && (c != 'N'));
95 void write_index(const char *outf, t_blocka *b, char **gnames, gmx_bool bDuplicate, int natoms)
100 out = gmx_fio_fopen(outf, "w");
101 /* fprintf(out,"%5d %5d\n",b->nr,b->nra); */
102 for (i = 0; (i < b->nr); i++)
104 fprintf(out, "[ %s ]\n", gnames[i]);
105 for (k = 0, j = b->index[i]; j < b->index[i+1]; j++, k++)
107 fprintf(out, "%4d ", b->a[j]+1);
116 /* Duplicate copy, useful for computational electrophysiology double-layer setups */
119 fprintf(stderr, "Duplicating the whole system with an atom offset of %d atoms.\n", natoms);
120 for (i = 0; (i < b->nr); i++)
122 fprintf(out, "[ %s_copy ]\n", gnames[i]);
123 for (k = 0, j = b->index[i]; j < b->index[i+1]; j++, k++)
125 fprintf(out, "%4d ", b->a[j]+1 + natoms );
138 void add_grp(t_blocka *b, char ***gnames, int nra, atom_id a[], const char *name)
142 srenew(b->index, b->nr+2);
143 srenew(*gnames, b->nr+1);
144 (*gnames)[b->nr] = strdup(name);
146 srenew(b->a, b->nra+nra);
147 for (i = 0; (i < nra); i++)
149 b->a[b->nra++] = a[i];
152 b->index[b->nr] = b->nra;
155 /* compare index in `a' with group in `b' at `index',
156 when `index'<0 it is relative to end of `b' */
157 static gmx_bool grp_cmp(t_blocka *b, int nra, atom_id a[], int index)
163 index = b->nr-1+index;
167 gmx_fatal(FARGS, "no such index group %d in t_blocka (nr=%d)", index, b->nr);
170 if (nra != b->index[index+1] - b->index[index])
174 for (i = 0; i < nra; i++)
176 if (a[i] != b->a[b->index[index]+i])
185 p_status(const char **restype, int nres, const char **typenames, int ntypes)
190 snew(counter, ntypes);
191 for (i = 0; i < ntypes; i++)
195 for (i = 0; i < nres; i++)
197 for (j = 0; j < ntypes; j++)
199 if (!gmx_strcasecmp(restype[i], typenames[j]))
206 for (i = 0; (i < ntypes); i++)
210 printf("There are: %5d %10s residues\n", counter[i], typenames[i]);
219 mk_aid(t_atoms *atoms, const char ** restype, const char * typestring, int *nra, gmx_bool bMatch)
220 /* Make an array of atom_ids for all atoms with residuetypes matching typestring, or the opposite if bMatch is false */
228 for (i = 0; (i < atoms->nr); i++)
230 res = !gmx_strcasecmp(restype[atoms->atom[i].resind], typestring);
250 static void analyse_other(const char ** restype, t_atoms *atoms,
251 t_blocka *gb, char ***gn, gmx_bool bASK, gmx_bool bVerb)
253 restp_t *restp = NULL;
256 atom_id *other_ndx, *aid, *aaid;
257 int i, j, k, l, resind, naid, naaid, natp, nrestp = 0;
259 for (i = 0; (i < atoms->nres); i++)
261 if (gmx_strcasecmp(restype[i], "Protein") && gmx_strcasecmp(restype[i], "DNA") && gmx_strcasecmp(restype[i], "RNA") && gmx_strcasecmp(restype[i], "Water"))
271 printf("Analysing residues not classified as Protein/DNA/RNA/Water and splitting into groups...\n");
273 snew(other_ndx, atoms->nr);
274 for (k = 0; (k < atoms->nr); k++)
276 resind = atoms->atom[k].resind;
277 rname = *atoms->resinfo[resind].name;
278 if (gmx_strcasecmp(restype[resind], "Protein") && gmx_strcasecmp(restype[resind], "DNA") &&
279 gmx_strcasecmp(restype[resind], "RNA") && gmx_strcasecmp(restype[resind], "Water"))
282 for (l = 0; (l < nrestp); l++)
284 if (strcmp(restp[l].rname, rname) == 0)
291 srenew(restp, nrestp+1);
292 restp[nrestp].rname = strdup(rname);
293 restp[nrestp].bNeg = FALSE;
294 restp[nrestp].gname = strdup(rname);
300 for (i = 0; (i < nrestp); i++)
302 snew(aid, atoms->nr);
304 for (j = 0; (j < atoms->nr); j++)
306 rname = *atoms->resinfo[atoms->atom[j].resind].name;
307 if ((strcmp(restp[i].rname, rname) == 0 && !restp[i].bNeg) ||
308 (strcmp(restp[i].rname, rname) != 0 && restp[i].bNeg))
313 add_grp(gb, gn, naid, aid, restp[i].gname);
316 printf("split %s into atoms (y/n) ? ", restp[i].gname);
318 if (gmx_ask_yesno(bASK))
321 for (k = 0; (k < naid); k++)
323 aname = *atoms->atomname[aid[k]];
324 for (l = 0; (l < natp); l++)
326 if (strcmp(aname, attp[l]) == 0)
333 srenew(attp, ++natp);
334 attp[natp-1] = aname;
339 for (l = 0; (l < natp); l++)
343 for (k = 0; (k < naid); k++)
345 aname = *atoms->atomname[aid[k]];
346 if (strcmp(aname, attp[l]) == 0)
348 aaid[naaid++] = aid[k];
351 add_grp(gb, gn, naaid, aaid, attp[l]);
366 * Cata necessary to construct a single (protein) index group in
369 typedef struct gmx_help_make_index_group
371 /** The set of atom names that will be used to form this index group */
372 const char **defining_atomnames;
373 /** Size of the defining_atomnames array */
374 int num_defining_atomnames;
375 /** Name of this index group */
376 const char *group_name;
377 /** Whether the above atom names name the atoms in the group, or
378 those not in the group */
379 gmx_bool bTakeComplement;
380 /** The index in wholename gives the first item in the arrays of
381 atomnames that should be tested with 'gmx_strncasecmp' in stead of
382 gmx_strcasecmp, or -1 if all items should be tested with strcasecmp
383 This is comparable to using a '*' wildcard at the end of specific
384 atom names, but that is more involved to implement...
387 /** Only create this index group if it differs from the one specified in compareto,
388 where -1 means to always create this group. */
390 } t_gmx_help_make_index_group;
392 static void analyse_prot(const char ** restype, t_atoms *atoms,
393 t_blocka *gb, char ***gn, gmx_bool bASK, gmx_bool bVerb)
395 /* lists of atomnames to be used in constructing index groups: */
396 static const char *pnoh[] = { "H", "HN" };
397 static const char *pnodum[] = {
398 "MN1", "MN2", "MCB1", "MCB2", "MCG1", "MCG2",
399 "MCD1", "MCD2", "MCE1", "MCE2", "MNZ1", "MNZ2"
401 static const char *calpha[] = { "CA" };
402 static const char *bb[] = { "N", "CA", "C" };
403 static const char *mc[] = { "N", "CA", "C", "O", "O1", "O2", "OC1", "OC2", "OT", "OXT" };
404 static const char *mcb[] = { "N", "CA", "CB", "C", "O", "O1", "O2", "OC1", "OC2", "OT", "OXT" };
405 static const char *mch[] = {
406 "N", "CA", "C", "O", "O1", "O2", "OC1", "OC2", "OT", "OXT",
407 "H1", "H2", "H3", "H", "HN"
410 static const t_gmx_help_make_index_group constructing_data[] =
411 {{ NULL, 0, "Protein", TRUE, -1, -1},
412 { pnoh, asize(pnoh), "Protein-H", TRUE, 0, -1},
413 { calpha, asize(calpha), "C-alpha", FALSE, -1, -1},
414 { bb, asize(bb), "Backbone", FALSE, -1, -1},
415 { mc, asize(mc), "MainChain", FALSE, -1, -1},
416 { mcb, asize(mcb), "MainChain+Cb", FALSE, -1, -1},
417 { mch, asize(mch), "MainChain+H", FALSE, -1, -1},
418 { mch, asize(mch), "SideChain", TRUE, -1, -1},
419 { mch, asize(mch), "SideChain-H", TRUE, 11, -1},
420 { pnodum, asize(pnodum), "Prot-Masses", TRUE, -1, 0}, };
421 const int num_index_groups = asize(constructing_data);
427 char ndx_name[STRLEN], *atnm;
432 printf("Analysing Protein...\n");
434 snew(aid, atoms->nr);
436 /* calculate the number of protein residues */
438 for (i = 0; (i < atoms->nres); i++)
440 if (0 == gmx_strcasecmp(restype[i], "Protein"))
445 /* find matching or complement atoms */
446 for (i = 0; (i < (int)num_index_groups); i++)
449 for (n = 0; (n < atoms->nr); n++)
451 if (0 == gmx_strcasecmp(restype[atoms->atom[n].resind], "Protein"))
454 for (j = 0; (j < constructing_data[i].num_defining_atomnames); j++)
456 /* skip digits at beginning of atomname, e.g. 1H */
457 atnm = *atoms->atomname[n];
458 while (isdigit(atnm[0]))
462 if ( (constructing_data[i].wholename == -1) || (j < constructing_data[i].wholename) )
464 if (0 == gmx_strcasecmp(constructing_data[i].defining_atomnames[j], atnm))
471 if (0 == gmx_strncasecmp(constructing_data[i].defining_atomnames[j], atnm, strlen(constructing_data[i].defining_atomnames[j])))
477 if (constructing_data[i].bTakeComplement != match)
483 /* if we want to add this group always or it differs from previous
485 if (-1 == constructing_data[i].compareto || !grp_cmp(gb, nra, aid, constructing_data[i].compareto-i) )
487 add_grp(gb, gn, nra, aid, constructing_data[i].group_name);
493 for (i = 0; (i < (int)num_index_groups); i++)
495 printf("Split %12s into %5d residues (y/n) ? ", constructing_data[i].group_name, npres);
496 if (gmx_ask_yesno(bASK))
500 for (n = 0; ((atoms->atom[n].resind < npres) && (n < atoms->nr)); )
502 resind = atoms->atom[n].resind;
503 for (; ((atoms->atom[n].resind == resind) && (n < atoms->nr)); n++)
506 for (j = 0; (j < constructing_data[i].num_defining_atomnames); j++)
508 if (0 == gmx_strcasecmp(constructing_data[i].defining_atomnames[j], *atoms->atomname[n]))
513 if (constructing_data[i].bTakeComplement != match)
518 /* copy the residuename to the tail of the groupname */
522 ri = &atoms->resinfo[resind];
523 sprintf(ndx_name, "%s_%s%d%c",
524 constructing_data[i].group_name, *ri->name, ri->nr, ri->ic == ' ' ? '\0' : ri->ic);
525 add_grp(gb, gn, nra, aid, ndx_name);
531 printf("Make group with sidechain and C=O swapped (y/n) ? ");
532 if (gmx_ask_yesno(bASK))
534 /* Make swap sidechain C=O index */
537 for (n = 0; ((atoms->atom[n].resind < npres) && (n < atoms->nr)); )
539 resind = atoms->atom[n].resind;
541 for (; ((atoms->atom[n].resind == resind) && (n < atoms->nr)); n++)
543 if (strcmp("CA", *atoms->atomname[n]) == 0)
549 else if (strcmp("C", *atoms->atomname[n]) == 0)
553 gmx_incons("Atom naming problem");
557 else if (strcmp("O", *atoms->atomname[n]) == 0)
561 gmx_incons("Atom naming problem");
565 else if (strcmp("O1", *atoms->atomname[n]) == 0)
569 gmx_incons("Atom naming problem");
579 /* copy the residuename to the tail of the groupname */
582 add_grp(gb, gn, nra, aid, "SwapSC-CO");
592 /* Return 0 if the name was found, otherwise -1.
593 * p_restype is set to a pointer to the type name, or 'Other' if we did not find it.
596 gmx_residuetype_get_type(gmx_residuetype_t rt, const char * resname, const char ** p_restype)
601 for (i = 0; i < rt->n && rc; i++)
603 rc = gmx_strcasecmp(rt->resname[i], resname);
606 *p_restype = (rc == 0) ? rt->restype[i-1] : gmx_residuetype_undefined;
612 gmx_residuetype_add(gmx_residuetype_t rt, const char *newresname, const char *newrestype)
615 const char * p_oldtype;
617 found = !gmx_residuetype_get_type(rt, newresname, &p_oldtype);
619 if (found && gmx_strcasecmp(p_oldtype, newrestype))
621 fprintf(stderr, "Warning: Residue '%s' already present with type '%s' in database, ignoring new type '%s'.",
622 newresname, p_oldtype, newrestype);
627 srenew(rt->resname, rt->n+1);
628 srenew(rt->restype, rt->n+1);
629 rt->resname[rt->n] = strdup(newresname);
630 rt->restype[rt->n] = strdup(newrestype);
639 gmx_residuetype_init(gmx_residuetype_t *prt)
643 char resname[STRLEN], restype[STRLEN], dum[STRLEN];
644 struct gmx_residuetype *rt;
653 db = libopen("residuetypes.dat");
655 while (get_a_line(db, line, STRLEN))
661 if (sscanf(line, "%s %s %s", resname, restype, dum) != 2)
663 gmx_fatal(FARGS, "Incorrect number of columns (2 expected) for line in residuetypes.dat");
665 gmx_residuetype_add(rt, resname, restype);
677 gmx_residuetype_destroy(gmx_residuetype_t rt)
681 for (i = 0; i < rt->n; i++)
683 sfree(rt->resname[i]);
684 sfree(rt->restype[i]);
694 gmx_residuetype_get_alltypes(gmx_residuetype_t rt,
695 const char *** p_typenames,
699 const char ** my_typename;
703 for (i = 0; i < rt->n; i++)
705 const char *const p = rt->restype[i];
707 for (int j = 0; j < n && !bFound; j++)
709 assert(my_typename != NULL);
710 bFound = !gmx_strcasecmp(p, my_typename[j]);
714 srenew(my_typename, n+1);
720 *p_typenames = my_typename;
728 gmx_residuetype_is_protein(gmx_residuetype_t rt, const char *resnm)
733 if (gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
734 gmx_strcasecmp(p_type, "Protein") == 0)
746 gmx_residuetype_is_dna(gmx_residuetype_t rt, const char *resnm)
751 if (gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
752 gmx_strcasecmp(p_type, "DNA") == 0)
764 gmx_residuetype_is_rna(gmx_residuetype_t rt, const char *resnm)
769 if (gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
770 gmx_strcasecmp(p_type, "RNA") == 0)
781 /* Return the size of the arrays */
783 gmx_residuetype_get_size(gmx_residuetype_t rt)
788 /* Search for a residuetype with name resnm within the
789 * gmx_residuetype database. Return the index if found,
793 gmx_residuetype_get_index(gmx_residuetype_t rt, const char *resnm)
798 for (i = 0; i < rt->n && rc; i++)
800 rc = gmx_strcasecmp(rt->resname[i], resnm);
803 return (0 == rc) ? i-1 : -1;
806 /* Return the name of the residuetype with the given index, or
807 * NULL if not found. */
809 gmx_residuetype_get_name(gmx_residuetype_t rt, int index)
811 if (index >= 0 && index < rt->n)
813 return rt->resname[index];
823 void analyse(t_atoms *atoms, t_blocka *gb, char ***gn, gmx_bool bASK, gmx_bool bVerb)
825 gmx_residuetype_t rt = NULL;
828 const char ** restype;
832 const char ** p_typename;
839 printf("Analysing residue names:\n");
841 /* Create system group, every single atom */
842 snew(aid, atoms->nr);
843 for (i = 0; i < atoms->nr; i++)
847 add_grp(gb, gn, atoms->nr, aid, "System");
850 /* For every residue, get a pointer to the residue type name */
851 gmx_residuetype_init(&rt);
854 snew(restype, atoms->nres);
857 for (i = 0; i < atoms->nres; i++)
859 resnm = *atoms->resinfo[i].name;
860 gmx_residuetype_get_type(rt, resnm, &(restype[i]));
862 /* Note that this does not lead to a N*N loop, but N*K, where
863 * K is the number of residue _types_, which is small and independent of N.
866 for (k = 0; k < ntypes && !found; k++)
868 assert(p_typename != NULL);
869 found = !strcmp(restype[i], p_typename[k]);
873 srenew(p_typename, ntypes+1);
874 p_typename[ntypes++] = strdup(restype[i]);
880 p_status(restype, atoms->nres, p_typename, ntypes);
883 for (k = 0; k < ntypes; k++)
885 aid = mk_aid(atoms, restype, p_typename[k], &nra, TRUE);
887 /* Check for special types to do fancy stuff with */
889 if (!gmx_strcasecmp(p_typename[k], "Protein") && nra > 0)
893 analyse_prot(restype, atoms, gb, gn, bASK, bVerb);
895 /* Create a Non-Protein group */
896 aid = mk_aid(atoms, restype, "Protein", &nra, FALSE);
897 if ((nra > 0) && (nra < atoms->nr))
899 add_grp(gb, gn, nra, aid, "non-Protein");
903 else if (!gmx_strcasecmp(p_typename[k], "Water") && nra > 0)
905 add_grp(gb, gn, nra, aid, p_typename[k]);
906 /* Add this group as 'SOL' too, for backward compatibility with older gromacs versions */
907 add_grp(gb, gn, nra, aid, "SOL");
911 /* Solvent, create a negated group too */
912 aid = mk_aid(atoms, restype, "Water", &nra, FALSE);
913 if ((nra > 0) && (nra < atoms->nr))
915 add_grp(gb, gn, nra, aid, "non-Water");
922 add_grp(gb, gn, nra, aid, p_typename[k]);
924 analyse_other(restype, atoms, gb, gn, bASK, bVerb);
930 gmx_residuetype_destroy(rt);
932 /* Create a merged water_and_ions group */
938 for (i = 0; i < gb->nr; i++)
940 if (!gmx_strcasecmp((*gn)[i], "Water"))
943 nwater = gb->index[i+1]-gb->index[i];
945 else if (!gmx_strcasecmp((*gn)[i], "Ion"))
948 nion = gb->index[i+1]-gb->index[i];
952 if (nwater > 0 && nion > 0)
954 srenew(gb->index, gb->nr+2);
955 srenew(*gn, gb->nr+1);
956 (*gn)[gb->nr] = strdup("Water_and_ions");
957 srenew(gb->a, gb->nra+nwater+nion);
960 for (i = gb->index[iwater]; i < gb->index[iwater+1]; i++)
962 gb->a[gb->nra++] = gb->a[i];
967 for (i = gb->index[iion]; i < gb->index[iion+1]; i++)
969 gb->a[gb->nra++] = gb->a[i];
973 gb->index[gb->nr] = gb->nra;
978 void check_index(char *gname, int n, atom_id index[], char *traj, int natoms)
982 for (i = 0; i < n; i++)
984 if (index[i] >= natoms)
986 gmx_fatal(FARGS, "%s atom number (index[%d]=%d) is larger than the number of atoms in %s (%d)",
987 gname ? gname : "Index", i+1, index[i]+1,
988 traj ? traj : "the trajectory", natoms);
990 else if (index[i] < 0)
992 gmx_fatal(FARGS, "%s atom number (index[%d]=%d) is less than zero",
993 gname ? gname : "Index", i+1, index[i]+1);
998 t_blocka *init_index(const char *gfile, char ***grpname)
1004 char line[STRLEN], *pt, str[STRLEN];
1006 in = gmx_fio_fopen(gfile, "r");
1008 get_a_line(in, line, STRLEN);
1020 if (get_header(line, str))
1023 srenew(b->index, b->nr+1);
1024 srenew(*grpname, b->nr);
1029 b->index[b->nr] = b->index[b->nr-1];
1030 (*grpname)[b->nr-1] = strdup(str);
1036 gmx_fatal(FARGS, "The first header of your indexfile is invalid");
1039 while (sscanf(pt, "%s", str) == 1)
1041 i = b->index[b->nr];
1042 if (i >= maxentries)
1045 srenew(b->a, maxentries);
1047 b->a[i] = strtol(str, NULL, 10)-1;
1050 pt = strstr(pt, str)+strlen(str);
1054 while (get_a_line(in, line, STRLEN));
1059 sscanf(line, "%d%d", &b->nr, &b->nra);
1060 snew(b->index, b->nr+1);
1061 snew(*grpname, b->nr);
1064 for (i = 0; (i < b->nr); i++)
1066 GMX_IGNORE_RETURN_VALUE(fscanf(in, "%s%d", str, &ng));
1067 (*grpname)[i] = strdup(str);
1068 b->index[i+1] = b->index[i]+ng;
1069 if (b->index[i+1] > b->nra)
1071 gmx_fatal(FARGS, "Something wrong in your indexfile at group %s", str);
1073 for (j = 0; (j < ng); j++)
1075 GMX_IGNORE_RETURN_VALUE(fscanf(in, "%d", &a));
1076 b->a[b->index[i]+j] = a;
1082 for (i = 0; (i < b->nr); i++)
1084 for (j = b->index[i]; (j < b->index[i+1]); j++)
1088 fprintf(stderr, "\nWARNING: negative index %d in group %s\n\n",
1089 b->a[j], (*grpname)[i]);
1097 static void minstring(char *str)
1101 for (i = 0; (i < (int)strlen(str)); i++)
1110 int find_group(char s[], int ngrps, char **grpname)
1113 char string[STRLEN];
1119 /* first look for whole name match */
1122 for (i = 0; i < ngrps; i++)
1124 if (gmx_strcasecmp_min(s, grpname[i]) == 0)
1134 /* second look for first string match */
1137 for (i = 0; i < ngrps; i++)
1139 if (gmx_strncasecmp_min(s, grpname[i], n) == 0)
1149 /* last look for arbitrary substring match */
1154 for (i = 0; i < ngrps; i++)
1156 strcpy(string, grpname[i]);
1159 if (strstr(string, s) != NULL)
1171 printf("Error: Multiple groups '%s' selected\n", s);
1177 static int qgroup(int *a, int ngrps, char **grpname)
1186 fprintf(stderr, "Select a group: ");
1189 if (scanf("%s", s) != 1)
1191 gmx_fatal(FARGS, "Cannot read from input");
1193 trim(s); /* remove spaces */
1195 while (strlen(s) == 0);
1196 aa = strtol(s, &end, 10);
1197 if (aa == 0 && end[0] != '\0') /* string entered */
1199 aa = find_group(s, ngrps, grpname);
1201 bInRange = (aa >= 0 && aa < ngrps);
1204 printf("Error: No such group '%s'\n", s);
1208 printf("Selected %d: '%s'\n", aa, grpname[aa]);
1213 static void rd_groups(t_blocka *grps, char **grpname, char *gnames[],
1214 int ngrps, int isize[], atom_id *index[], int grpnr[])
1220 gmx_fatal(FARGS, "Error: no groups in indexfile");
1222 for (i = 0; (i < grps->nr); i++)
1224 fprintf(stderr, "Group %5d (%15s) has %5d elements\n", i, grpname[i],
1225 grps->index[i+1]-grps->index[i]);
1227 for (i = 0; (i < ngrps); i++)
1233 gnr1 = qgroup(&grpnr[i], grps->nr, grpname);
1234 if ((gnr1 < 0) || (gnr1 >= grps->nr))
1236 fprintf(stderr, "Select between %d and %d.\n", 0, grps->nr-1);
1239 while ((gnr1 < 0) || (gnr1 >= grps->nr));
1243 fprintf(stderr, "There is one group in the index\n");
1246 gnames[i] = strdup(grpname[gnr1]);
1247 isize[i] = grps->index[gnr1+1]-grps->index[gnr1];
1248 snew(index[i], isize[i]);
1249 for (j = 0; (j < isize[i]); j++)
1251 index[i][j] = grps->a[grps->index[gnr1]+j];
1256 void rd_index(const char *statfile, int ngrps, int isize[],
1257 atom_id *index[], char *grpnames[])
1266 gmx_fatal(FARGS, "No index file specified");
1268 grps = init_index(statfile, &gnames);
1269 rd_groups(grps, gnames, grpnames, ngrps, isize, index, grpnr);
1272 void rd_index_nrs(char *statfile, int ngrps, int isize[],
1273 atom_id *index[], char *grpnames[], int grpnr[])
1280 gmx_fatal(FARGS, "No index file specified");
1282 grps = init_index(statfile, &gnames);
1284 rd_groups(grps, gnames, grpnames, ngrps, isize, index, grpnr);
1287 void get_index(t_atoms *atoms, const char *fnm, int ngrps,
1288 int isize[], atom_id *index[], char *grpnames[])
1291 t_blocka *grps = NULL;
1298 grps = init_index(fnm, gnames);
1303 snew(grps->index, 1);
1304 analyse(atoms, grps, gnames, FALSE, FALSE);
1308 gmx_incons("You need to supply a valid atoms structure or a valid index file name");
1311 rd_groups(grps, *gnames, grpnames, ngrps, isize, index, grpnr);
1314 t_cluster_ndx *cluster_index(FILE *fplog, const char *ndx)
1320 c->clust = init_index(ndx, &c->grpname);
1322 for (i = 0; (i < c->clust->nra); i++)
1324 c->maxframe = std::max(c->maxframe, c->clust->a[i]);
1326 fprintf(fplog ? fplog : stdout,
1327 "There are %d clusters containing %d structures, highest framenr is %d\n",
1328 c->clust->nr, c->clust->nra, c->maxframe);
1331 pr_blocka(debug, 0, "clust", c->clust, TRUE);
1332 for (i = 0; (i < c->clust->nra); i++)
1334 if ((c->clust->a[i] < 0) || (c->clust->a[i] > c->maxframe))
1336 gmx_fatal(FARGS, "Range check error for c->clust->a[%d] = %d\n"
1337 "should be within 0 and %d", i, c->clust->a[i], c->maxframe+1);
1341 c->inv_clust = make_invblocka(c->clust, c->maxframe);