3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements simple keyword selection methods.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_selection
44 #include "gromacs/legacyheaders/macros.h"
46 #include "gromacs/selection/position.h"
47 #include "gromacs/selection/selmethod.h"
48 #include "gromacs/utility/exceptions.h"
50 /** Evaluates the \p all selection keyword. */
52 evaluate_all(t_topology *top, t_trxframe *fr, t_pbc *pbc,
53 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
54 /** Evaluates the \p none selection keyword. */
56 evaluate_none(t_topology *top, t_trxframe *fr, t_pbc *pbc,
57 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
58 /** Evaluates the \p atomnr selection keyword. */
60 evaluate_atomnr(t_topology *top, t_trxframe *fr, t_pbc *pbc,
61 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
62 /** Evaluates the \p resnr selection keyword. */
64 evaluate_resnr(t_topology *top, t_trxframe *fr, t_pbc *pbc,
65 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
66 /** Evaluates the \p resindex selection keyword. */
68 evaluate_resindex(t_topology *top, t_trxframe *fr, t_pbc *pbc,
69 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
70 /** Checks whether molecule information is present in the topology. */
72 check_molecules(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
73 /** Evaluates the \p molindex selection keyword. */
75 evaluate_molindex(t_topology *top, t_trxframe *fr, t_pbc *pbc,
76 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
77 /** Evaluates the \p atomname selection keyword. */
79 evaluate_atomname(t_topology *top, t_trxframe *fr, t_pbc *pbc,
80 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
81 /** Evaluates the \p pdbatomname selection keyword. */
83 evaluate_pdbatomname(t_topology *top, t_trxframe *fr, t_pbc *pbc,
84 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
85 /** Checks whether atom types are present in the topology. */
87 check_atomtype(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
88 /** Evaluates the \p atomtype selection keyword. */
90 evaluate_atomtype(t_topology *top, t_trxframe *fr, t_pbc *pbc,
91 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
92 /** Evaluates the \p insertcode selection keyword. */
94 evaluate_insertcode(t_topology *top, t_trxframe *fr, t_pbc *pbc,
95 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
96 /** Evaluates the \p chain selection keyword. */
98 evaluate_chain(t_topology *top, t_trxframe *fr, t_pbc *pbc,
99 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
100 /** Evaluates the \p mass selection keyword. */
102 evaluate_mass(t_topology *top, t_trxframe *fr, t_pbc *pbc,
103 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
104 /** Evaluates the \p charge selection keyword. */
106 evaluate_charge(t_topology *top, t_trxframe *fr, t_pbc *pbc,
107 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
108 /** Checks whether PDB info is present in the topology. */
110 check_pdbinfo(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
111 /** Evaluates the \p altloc selection keyword. */
113 evaluate_altloc(t_topology *top, t_trxframe *fr, t_pbc *pbc,
114 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
115 /** Evaluates the \p occupancy selection keyword. */
117 evaluate_occupancy(t_topology *top, t_trxframe *fr, t_pbc *pbc,
118 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
119 /** Evaluates the \p betafactor selection keyword. */
121 evaluate_betafactor(t_topology *top, t_trxframe *fr, t_pbc *pbc,
122 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
123 /** Evaluates the \p resname selection keyword. */
125 evaluate_resname(t_topology *top, t_trxframe *fr, t_pbc *pbc,
126 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
128 /** Evaluates the \p x selection keyword. */
130 evaluate_x(t_topology *top, t_trxframe *fr, t_pbc *pbc,
131 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data);
132 /** Evaluates the \p y selection keyword. */
134 evaluate_y(t_topology *top, t_trxframe *fr, t_pbc *pbc,
135 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data);
136 /** Evaluates the \p z selection keyword. */
138 evaluate_z(t_topology *top, t_trxframe *fr, t_pbc *pbc,
139 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data);
141 /** Help text for atom name selection keywords. */
142 static const char *help_atomname[] = {
143 "ATOM NAME SELECTION KEYWORDS[PAR]",
145 "[TT]name[tt] [TT]pdbname[tt] [TT]atomname[tt] [TT]pdbatomname[tt][PAR]",
147 "These keywords select atoms by name. [TT]name[tt] selects atoms using",
148 "the Gromacs atom naming convention.",
149 "For input formats other than PDB, the atom names are matched exactly",
150 "as they appear in the input file. For PDB files, 4 character atom names",
151 "that start with a digit are matched after moving the digit to the end",
152 "(e.g., to match 3HG2 from a PDB file, use [TT]name HG23[tt]).",
153 "[TT]pdbname[tt] can only be used with a PDB input file, and selects",
154 "atoms based on the exact name given in the input file, without the",
155 "transformation described above.[PAR]",
157 "[TT]atomname[tt] and [TT]pdbatomname[tt] are synonyms for the above two",
161 /** \internal Selection method data for \p all selection keyword. */
162 gmx_ana_selmethod_t sm_all = {
163 "all", GROUP_VALUE, 0,
175 /** \internal Selection method data for \p none selection keyword. */
176 gmx_ana_selmethod_t sm_none = {
177 "none", GROUP_VALUE, 0,
189 /** \internal Selection method data for \p atomnr selection keyword. */
190 gmx_ana_selmethod_t sm_atomnr = {
191 "atomnr", INT_VALUE, 0,
203 /** \internal Selection method data for \p resnr selection keyword. */
204 gmx_ana_selmethod_t sm_resnr = {
205 "resnr", INT_VALUE, SMETH_REQTOP,
217 /** \internal Selection method data for \p resindex selection keyword. */
218 gmx_ana_selmethod_t sm_resindex = {
219 "resindex", INT_VALUE, SMETH_REQTOP,
231 /** \internal Selection method data for \p molindex selection keyword. */
232 gmx_ana_selmethod_t sm_molindex = {
233 "molindex", INT_VALUE, SMETH_REQTOP,
245 /** \internal Selection method data for \p atomname selection keyword. */
246 gmx_ana_selmethod_t sm_atomname = {
247 "atomname", STR_VALUE, SMETH_REQTOP,
257 {NULL, asize(help_atomname), help_atomname}
260 /** \internal Selection method data for \p pdbatomname selection keyword. */
261 gmx_ana_selmethod_t sm_pdbatomname = {
262 "pdbatomname", STR_VALUE, SMETH_REQTOP,
270 &evaluate_pdbatomname,
272 {NULL, asize(help_atomname), help_atomname}
275 /** \internal Selection method data for \p atomtype selection keyword. */
276 gmx_ana_selmethod_t sm_atomtype = {
277 "atomtype", STR_VALUE, SMETH_REQTOP,
289 /** \internal Selection method data for \p resname selection keyword. */
290 gmx_ana_selmethod_t sm_resname = {
291 "resname", STR_VALUE, SMETH_REQTOP,
303 /** \internal Selection method data for \p chain selection keyword. */
304 gmx_ana_selmethod_t sm_insertcode = {
305 "insertcode", STR_VALUE, SMETH_REQTOP | SMETH_CHARVAL,
313 &evaluate_insertcode,
317 /** \internal Selection method data for \p chain selection keyword. */
318 gmx_ana_selmethod_t sm_chain = {
319 "chain", STR_VALUE, SMETH_REQTOP | SMETH_CHARVAL,
331 /** \internal Selection method data for \p mass selection keyword. */
332 gmx_ana_selmethod_t sm_mass = {
333 "mass", REAL_VALUE, SMETH_REQTOP,
345 /** \internal Selection method data for \p charge selection keyword. */
346 gmx_ana_selmethod_t sm_charge = {
347 "charge", REAL_VALUE, SMETH_REQTOP,
359 /** \internal Selection method data for \p chain selection keyword. */
360 gmx_ana_selmethod_t sm_altloc = {
361 "altloc", STR_VALUE, SMETH_REQTOP | SMETH_CHARVAL,
373 /** \internal Selection method data for \p occupancy selection keyword. */
374 gmx_ana_selmethod_t sm_occupancy = {
375 "occupancy", REAL_VALUE, SMETH_REQTOP,
387 /** \internal Selection method data for \p betafactor selection keyword. */
388 gmx_ana_selmethod_t sm_betafactor = {
389 "betafactor", REAL_VALUE, SMETH_REQTOP,
397 &evaluate_betafactor,
401 /** \internal Selection method data for \p x selection keyword. */
402 gmx_ana_selmethod_t sm_x = {
403 "x", REAL_VALUE, SMETH_DYNAMIC,
415 /** \internal Selection method data for \p y selection keyword. */
416 gmx_ana_selmethod_t sm_y = {
417 "y", REAL_VALUE, SMETH_DYNAMIC,
429 /** \internal Selection method data for \p z selection keyword. */
430 gmx_ana_selmethod_t sm_z = {
431 "z", REAL_VALUE, SMETH_DYNAMIC,
444 * See sel_updatefunc() for description of the parameters.
445 * \p data is not used.
447 * Copies \p g to \p out->u.g.
450 evaluate_all(t_topology *top, t_trxframe *fr, t_pbc *pbc,
451 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
453 gmx_ana_index_copy(out->u.g, g, false);
457 * See sel_updatefunc() for description of the parameters.
458 * \p data is not used.
460 * Returns an empty \p out->u.g.
463 evaluate_none(t_topology *top, t_trxframe *fr, t_pbc *pbc,
464 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
470 * See sel_updatefunc() for description of the parameters.
471 * \p data is not used.
473 * Returns the indices for each atom in \p out->u.i.
476 evaluate_atomnr(t_topology *top, t_trxframe *fr, t_pbc *pbc,
477 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
482 for (i = 0; i < g->isize; ++i)
484 out->u.i[i] = g->index[i] + 1;
489 * See sel_updatefunc() for description of the parameters.
490 * \p data is not used.
492 * Returns the residue numbers for each atom in \p out->u.i.
495 evaluate_resnr(t_topology *top, t_trxframe *fr, t_pbc *pbc,
496 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
502 for (i = 0; i < g->isize; ++i)
504 resind = top->atoms.atom[g->index[i]].resind;
505 out->u.i[i] = top->atoms.resinfo[resind].nr;
510 * See sel_updatefunc() for description of the parameters.
511 * \p data is not used.
513 * Returns the residue indices for each atom in \p out->u.i.
516 evaluate_resindex(t_topology *top, t_trxframe *fr, t_pbc *pbc,
517 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
522 for (i = 0; i < g->isize; ++i)
524 out->u.i[i] = top->atoms.atom[g->index[i]].resind + 1;
529 * \param[in] top Topology structure.
530 * \param npar Not used.
531 * \param param Not used.
532 * \param data Not used.
533 * \returns 0 if molecule info is present in the topology, -1 otherwise.
535 * If molecule information is not found, also prints an error message.
538 check_molecules(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
542 bOk = (top != NULL && top->mols.nr > 0);
545 GMX_THROW(gmx::InconsistentInputError("Molecule information not available in topology"));
550 * See sel_updatefunc() for description of the parameters.
551 * \p data is not used.
553 * Returns the molecule indices for each atom in \p out->u.i.
556 evaluate_molindex(t_topology *top, t_trxframe *fr, t_pbc *pbc,
557 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
562 for (i = j = 0; i < g->isize; ++i)
564 while (top->mols.index[j + 1] <= g->index[i]) ++j;
570 * See sel_updatefunc() for description of the parameters.
571 * \p data is not used.
573 * Returns the atom name for each atom in \p out->u.s.
576 evaluate_atomname(t_topology *top, t_trxframe *fr, t_pbc *pbc,
577 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
582 for (i = 0; i < g->isize; ++i)
584 out->u.s[i] = *top->atoms.atomname[g->index[i]];
589 * See sel_updatefunc() for description of the parameters.
590 * \p data is not used.
592 * Returns the PDB atom name for each atom in \p out->u.s.
595 evaluate_pdbatomname(t_topology *top, t_trxframe *fr, t_pbc *pbc,
596 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
601 for (i = 0; i < g->isize; ++i)
603 char *s = top->atoms.pdbinfo[g->index[i]].atomnm;
604 while (std::isspace(*s))
613 * \param[in] top Topology structure.
614 * \param npar Not used.
615 * \param param Not used.
616 * \param data Not used.
617 * \returns 0 if atom types are present in the topology, -1 otherwise.
619 * If the atom types are not found, also prints an error message.
622 check_atomtype(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
626 bOk = (top != NULL && top->atoms.atomtype != NULL);
629 GMX_THROW(gmx::InconsistentInputError("Atom types not available in topology"));
634 * See sel_updatefunc() for description of the parameters.
635 * \p data is not used.
637 * Returns the atom type for each atom in \p out->u.s.
638 * Segfaults if atom types are not found in the topology.
641 evaluate_atomtype(t_topology *top, t_trxframe *fr, t_pbc *pbc,
642 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
647 for (i = 0; i < g->isize; ++i)
649 out->u.s[i] = *top->atoms.atomtype[g->index[i]];
654 * See sel_updatefunc() for description of the parameters.
655 * \p data is not used.
657 * Returns the residue name for each atom in \p out->u.s.
660 evaluate_resname(t_topology *top, t_trxframe *fr, t_pbc *pbc,
661 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
667 for (i = 0; i < g->isize; ++i)
669 resind = top->atoms.atom[g->index[i]].resind;
670 out->u.s[i] = *top->atoms.resinfo[resind].name;
675 * See sel_updatefunc() for description of the parameters.
676 * \p data is not used.
678 * Returns the insertion code for each atom in \p out->u.s.
681 evaluate_insertcode(t_topology *top, t_trxframe *fr, t_pbc *pbc,
682 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
688 for (i = 0; i < g->isize; ++i)
690 resind = top->atoms.atom[g->index[i]].resind;
691 out->u.s[i][0] = top->atoms.resinfo[resind].ic;
696 * See sel_updatefunc() for description of the parameters.
697 * \p data is not used.
699 * Returns the chain for each atom in \p out->u.s.
702 evaluate_chain(t_topology *top, t_trxframe *fr, t_pbc *pbc,
703 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
709 for (i = 0; i < g->isize; ++i)
711 resind = top->atoms.atom[g->index[i]].resind;
712 out->u.s[i][0] = top->atoms.resinfo[resind].chainid;
717 * See sel_updatefunc() for description of the parameters.
718 * \p data is not used.
720 * Returns the mass for each atom in \p out->u.r.
723 evaluate_mass(t_topology *top, t_trxframe *fr, t_pbc *pbc,
724 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
729 for (i = 0; i < g->isize; ++i)
731 out->u.r[i] = top->atoms.atom[g->index[i]].m;
736 * See sel_updatefunc() for description of the parameters.
737 * \p data is not used.
739 * Returns the charge for each atom in \p out->u.r.
742 evaluate_charge(t_topology *top, t_trxframe *fr, t_pbc *pbc,
743 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
748 for (i = 0; i < g->isize; ++i)
750 out->u.r[i] = top->atoms.atom[g->index[i]].q;
755 * \param[in] top Topology structure.
756 * \param npar Not used.
757 * \param param Not used.
758 * \param data Not used.
759 * \returns 0 if PDB info is present in the topology, -1 otherwise.
761 * If PDB info is not found, also prints an error message.
764 check_pdbinfo(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
768 bOk = (top != NULL && top->atoms.pdbinfo != NULL);
771 GMX_THROW(gmx::InconsistentInputError("PDB info not available in topology"));
776 * See sel_updatefunc() for description of the parameters.
777 * \p data is not used.
779 * Returns the alternate location identifier for each atom in \p out->u.s.
782 evaluate_altloc(t_topology *top, t_trxframe *fr, t_pbc *pbc,
783 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
788 for (i = 0; i < g->isize; ++i)
790 out->u.s[i][0] = top->atoms.pdbinfo[g->index[i]].altloc;
795 * See sel_updatefunc() for description of the parameters.
796 * \p data is not used.
798 * Returns the occupancy numbers for each atom in \p out->u.r.
799 * Segfaults if PDB info is not found in the topology.
802 evaluate_occupancy(t_topology *top, t_trxframe *fr, t_pbc *pbc,
803 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
808 for (i = 0; i < g->isize; ++i)
810 out->u.r[i] = top->atoms.pdbinfo[g->index[i]].occup;
815 * See sel_updatefunc() for description of the parameters.
816 * \p data is not used.
818 * Returns the B-factors for each atom in \p out->u.r.
819 * Segfaults if PDB info is not found in the topology.
822 evaluate_betafactor(t_topology *top, t_trxframe *fr, t_pbc *pbc,
823 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
828 for (i = 0; i < g->isize; ++i)
830 out->u.r[i] = top->atoms.pdbinfo[g->index[i]].bfac;
835 * Internal utility function for position keyword evaluation.
837 * \param[in] fr Current frame.
838 * \param[in] g Index group for which the coordinates should be evaluated.
839 * \param[out] out Output array.
840 * \param[in] pos Position data to use instead of atomic coordinates
842 * \param[in] d Coordinate index to evaluate (\p XX, \p YY or \p ZZ).
844 * This function is used internally by evaluate_x(), evaluate_y() and
845 * evaluate_z() to do the actual evaluation.
848 evaluate_coord(t_trxframe *fr, gmx_ana_index_t *g, real out[],
849 gmx_ana_pos_t *pos, int d)
856 for (b = 0; b < pos->nr; ++b)
859 for (i = pos->m.mapb.index[b]; i < pos->m.mapb.index[b+1]; ++i)
867 for (i = 0; i < g->isize; ++i)
869 out[i] = fr->x[g->index[i]][d];
875 * See sel_updatefunc_pos() for description of the parameters.
876 * \p data is not used.
878 * Returns the \p x coordinate for each atom in \p out->u.r.
881 evaluate_x(t_topology *top, t_trxframe *fr, t_pbc *pbc,
882 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data)
884 out->nr = pos->g->isize;
885 evaluate_coord(fr, pos->g, out->u.r, pos, XX);
889 * See sel_updatefunc() for description of the parameters.
890 * \p data is not used.
892 * Returns the \p y coordinate for each atom in \p out->u.r.
895 evaluate_y(t_topology *top, t_trxframe *fr, t_pbc *pbc,
896 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data)
898 out->nr = pos->g->isize;
899 evaluate_coord(fr, pos->g, out->u.r, pos, YY);
903 * See sel_updatefunc() for description of the parameters.
904 * \p data is not used.
906 * Returns the \p z coordinate for each atom in \p out->u.r.
909 evaluate_z(t_topology *top, t_trxframe *fr, t_pbc *pbc,
910 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data)
912 out->nr = pos->g->isize;
913 evaluate_coord(fr, pos->g, out->u.r, pos, ZZ);