2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements simple keyword selection methods.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
46 #include "gromacs/legacyheaders/macros.h"
48 #include "gromacs/topology/topology.h"
49 #include "gromacs/selection/position.h"
50 #include "gromacs/selection/selmethod.h"
51 #include "gromacs/utility/exceptions.h"
53 /** Evaluates the \p all selection keyword. */
55 evaluate_all(t_topology *top, t_trxframe *fr, t_pbc *pbc,
56 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
57 /** Evaluates the \p none selection keyword. */
59 evaluate_none(t_topology *top, t_trxframe *fr, t_pbc *pbc,
60 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
61 /** Evaluates the \p atomnr selection keyword. */
63 evaluate_atomnr(t_topology *top, t_trxframe *fr, t_pbc *pbc,
64 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
65 /** Evaluates the \p resnr selection keyword. */
67 evaluate_resnr(t_topology *top, t_trxframe *fr, t_pbc *pbc,
68 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
69 /** Evaluates the \p resindex selection keyword. */
71 evaluate_resindex(t_topology *top, t_trxframe *fr, t_pbc *pbc,
72 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
74 * Checks whether molecule information is present in the topology.
76 * \param[in] top Topology structure.
77 * \param npar Not used.
78 * \param param Not used.
79 * \param data Not used.
80 * \returns 0 if molecule info is present in the topology, -1 otherwise.
82 * If molecule information is not found, also prints an error message.
85 check_molecules(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
86 /** Evaluates the \p molindex selection keyword. */
88 evaluate_molindex(t_topology *top, t_trxframe *fr, t_pbc *pbc,
89 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
90 /** Evaluates the \p atomname selection keyword. */
92 evaluate_atomname(t_topology *top, t_trxframe *fr, t_pbc *pbc,
93 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
94 /** Evaluates the \p pdbatomname selection keyword. */
96 evaluate_pdbatomname(t_topology *top, t_trxframe *fr, t_pbc *pbc,
97 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
99 * Checks whether atom types are present in the topology.
101 * \param[in] top Topology structure.
102 * \param npar Not used.
103 * \param param Not used.
104 * \param data Not used.
105 * \returns 0 if atom types are present in the topology, -1 otherwise.
107 * If the atom types are not found, also prints an error message.
110 check_atomtype(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
111 /** Evaluates the \p atomtype selection keyword. */
113 evaluate_atomtype(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 insertcode selection keyword. */
117 evaluate_insertcode(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 chain selection keyword. */
121 evaluate_chain(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 mass selection keyword. */
125 evaluate_mass(t_topology *top, t_trxframe *fr, t_pbc *pbc,
126 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
127 /** Evaluates the \p charge selection keyword. */
129 evaluate_charge(t_topology *top, t_trxframe *fr, t_pbc *pbc,
130 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
132 * Checks whether PDB info is present in the topology.
134 * \param[in] top Topology structure.
135 * \param npar Not used.
136 * \param param Not used.
137 * \param data Not used.
138 * \returns 0 if PDB info is present in the topology, -1 otherwise.
140 * If PDB info is not found, also prints an error message.
143 check_pdbinfo(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
144 /** Evaluates the \p altloc selection keyword. */
146 evaluate_altloc(t_topology *top, t_trxframe *fr, t_pbc *pbc,
147 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
148 /** Evaluates the \p occupancy selection keyword. */
150 evaluate_occupancy(t_topology *top, t_trxframe *fr, t_pbc *pbc,
151 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
152 /** Evaluates the \p betafactor selection keyword. */
154 evaluate_betafactor(t_topology *top, t_trxframe *fr, t_pbc *pbc,
155 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
156 /** Evaluates the \p resname selection keyword. */
158 evaluate_resname(t_topology *top, t_trxframe *fr, t_pbc *pbc,
159 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
161 /** Evaluates the \p x selection keyword. */
163 evaluate_x(t_topology *top, t_trxframe *fr, t_pbc *pbc,
164 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data);
165 /** Evaluates the \p y selection keyword. */
167 evaluate_y(t_topology *top, t_trxframe *fr, t_pbc *pbc,
168 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data);
169 /** Evaluates the \p z selection keyword. */
171 evaluate_z(t_topology *top, t_trxframe *fr, t_pbc *pbc,
172 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data);
174 /** Help text for atom name selection keywords. */
175 static const char *help_atomname[] = {
176 "ATOM NAME SELECTION KEYWORDS[PAR]",
178 "[TT]name[tt] [TT]pdbname[tt] [TT]atomname[tt] [TT]pdbatomname[tt][PAR]",
180 "These keywords select atoms by name. [TT]name[tt] selects atoms using",
181 "the Gromacs atom naming convention.",
182 "For input formats other than PDB, the atom names are matched exactly",
183 "as they appear in the input file. For PDB files, 4 character atom names",
184 "that start with a digit are matched after moving the digit to the end",
185 "(e.g., to match 3HG2 from a PDB file, use [TT]name HG23[tt]).",
186 "[TT]pdbname[tt] can only be used with a PDB input file, and selects",
187 "atoms based on the exact name given in the input file, without the",
188 "transformation described above.[PAR]",
190 "[TT]atomname[tt] and [TT]pdbatomname[tt] are synonyms for the above two",
194 /** Selection method data for \p all selection keyword. */
195 gmx_ana_selmethod_t sm_all = {
196 "all", GROUP_VALUE, 0,
208 /** Selection method data for \p none selection keyword. */
209 gmx_ana_selmethod_t sm_none = {
210 "none", GROUP_VALUE, 0,
222 /** Selection method data for \p atomnr selection keyword. */
223 gmx_ana_selmethod_t sm_atomnr = {
224 "atomnr", INT_VALUE, 0,
236 /** Selection method data for \p resnr selection keyword. */
237 gmx_ana_selmethod_t sm_resnr = {
238 "resnr", INT_VALUE, SMETH_REQTOP,
250 /** Selection method data for \p resindex selection keyword. */
251 gmx_ana_selmethod_t sm_resindex = {
252 "resindex", INT_VALUE, SMETH_REQTOP,
264 /** Selection method data for \p molindex selection keyword. */
265 gmx_ana_selmethod_t sm_molindex = {
266 "molindex", INT_VALUE, SMETH_REQTOP,
278 /** Selection method data for \p atomname selection keyword. */
279 gmx_ana_selmethod_t sm_atomname = {
280 "atomname", STR_VALUE, SMETH_REQTOP,
290 {NULL, asize(help_atomname), help_atomname}
293 /** Selection method data for \p pdbatomname selection keyword. */
294 gmx_ana_selmethod_t sm_pdbatomname = {
295 "pdbatomname", STR_VALUE, SMETH_REQTOP,
303 &evaluate_pdbatomname,
305 {NULL, asize(help_atomname), help_atomname}
308 /** Selection method data for \p atomtype selection keyword. */
309 gmx_ana_selmethod_t sm_atomtype = {
310 "atomtype", STR_VALUE, SMETH_REQTOP,
322 /** Selection method data for \p resname selection keyword. */
323 gmx_ana_selmethod_t sm_resname = {
324 "resname", STR_VALUE, SMETH_REQTOP,
336 /** Selection method data for \p chain selection keyword. */
337 gmx_ana_selmethod_t sm_insertcode = {
338 "insertcode", STR_VALUE, SMETH_REQTOP | SMETH_CHARVAL,
346 &evaluate_insertcode,
350 /** Selection method data for \p chain selection keyword. */
351 gmx_ana_selmethod_t sm_chain = {
352 "chain", STR_VALUE, SMETH_REQTOP | SMETH_CHARVAL,
364 /** Selection method data for \p mass selection keyword. */
365 gmx_ana_selmethod_t sm_mass = {
366 "mass", REAL_VALUE, SMETH_REQTOP,
378 /** Selection method data for \p charge selection keyword. */
379 gmx_ana_selmethod_t sm_charge = {
380 "charge", REAL_VALUE, SMETH_REQTOP,
392 /** Selection method data for \p chain selection keyword. */
393 gmx_ana_selmethod_t sm_altloc = {
394 "altloc", STR_VALUE, SMETH_REQTOP | SMETH_CHARVAL,
406 /** Selection method data for \p occupancy selection keyword. */
407 gmx_ana_selmethod_t sm_occupancy = {
408 "occupancy", REAL_VALUE, SMETH_REQTOP,
420 /** Selection method data for \p betafactor selection keyword. */
421 gmx_ana_selmethod_t sm_betafactor = {
422 "betafactor", REAL_VALUE, SMETH_REQTOP,
430 &evaluate_betafactor,
434 /** Selection method data for \p x selection keyword. */
435 gmx_ana_selmethod_t sm_x = {
436 "x", REAL_VALUE, SMETH_DYNAMIC,
448 /** Selection method data for \p y selection keyword. */
449 gmx_ana_selmethod_t sm_y = {
450 "y", REAL_VALUE, SMETH_DYNAMIC,
462 /** Selection method data for \p z selection keyword. */
463 gmx_ana_selmethod_t sm_z = {
464 "z", REAL_VALUE, SMETH_DYNAMIC,
477 * See sel_updatefunc() for description of the parameters.
478 * \p data is not used.
480 * Copies \p g to \p out->u.g.
483 evaluate_all(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */,
484 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
486 gmx_ana_index_copy(out->u.g, g, false);
490 * See sel_updatefunc() for description of the parameters.
491 * \p data is not used.
493 * Returns an empty \p out->u.g.
496 evaluate_none(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */,
497 gmx_ana_index_t * /* g */, gmx_ana_selvalue_t *out, void * /* data */)
503 * See sel_updatefunc() for description of the parameters.
504 * \p data is not used.
506 * Returns the indices for each atom in \p out->u.i.
509 evaluate_atomnr(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */,
510 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
515 for (i = 0; i < g->isize; ++i)
517 out->u.i[i] = g->index[i] + 1;
522 * See sel_updatefunc() for description of the parameters.
523 * \p data is not used.
525 * Returns the residue numbers for each atom in \p out->u.i.
528 evaluate_resnr(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
529 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
535 for (i = 0; i < g->isize; ++i)
537 resind = top->atoms.atom[g->index[i]].resind;
538 out->u.i[i] = top->atoms.resinfo[resind].nr;
543 * See sel_updatefunc() for description of the parameters.
544 * \p data is not used.
546 * Returns the residue indices for each atom in \p out->u.i.
549 evaluate_resindex(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
550 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
555 for (i = 0; i < g->isize; ++i)
557 out->u.i[i] = top->atoms.atom[g->index[i]].resind + 1;
562 check_molecules(t_topology *top, int /* npar */, gmx_ana_selparam_t * /* param */, void * /* data */)
566 bOk = (top != NULL && top->mols.nr > 0);
569 GMX_THROW(gmx::InconsistentInputError("Molecule information not available in topology"));
574 * See sel_updatefunc() for description of the parameters.
575 * \p data is not used.
577 * Returns the molecule indices for each atom in \p out->u.i.
580 evaluate_molindex(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
581 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
586 for (i = j = 0; i < g->isize; ++i)
588 while (top->mols.index[j + 1] <= g->index[i])
597 * See sel_updatefunc() for description of the parameters.
598 * \p data is not used.
600 * Returns the atom name for each atom in \p out->u.s.
603 evaluate_atomname(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
604 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
609 for (i = 0; i < g->isize; ++i)
611 out->u.s[i] = *top->atoms.atomname[g->index[i]];
616 * See sel_updatefunc() for description of the parameters.
617 * \p data is not used.
619 * Returns the PDB atom name for each atom in \p out->u.s.
622 evaluate_pdbatomname(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
623 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
628 for (i = 0; i < g->isize; ++i)
630 char *s = top->atoms.pdbinfo[g->index[i]].atomnm;
631 while (std::isspace(*s))
640 check_atomtype(t_topology *top, int /* npar */, gmx_ana_selparam_t * /* param */, void * /* data */)
644 bOk = (top != NULL && top->atoms.atomtype != NULL);
647 GMX_THROW(gmx::InconsistentInputError("Atom types not available in topology"));
652 * See sel_updatefunc() for description of the parameters.
653 * \p data is not used.
655 * Returns the atom type for each atom in \p out->u.s.
656 * Segfaults if atom types are not found in the topology.
659 evaluate_atomtype(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
660 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
665 for (i = 0; i < g->isize; ++i)
667 out->u.s[i] = *top->atoms.atomtype[g->index[i]];
672 * See sel_updatefunc() for description of the parameters.
673 * \p data is not used.
675 * Returns the residue name for each atom in \p out->u.s.
678 evaluate_resname(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
679 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
685 for (i = 0; i < g->isize; ++i)
687 resind = top->atoms.atom[g->index[i]].resind;
688 out->u.s[i] = *top->atoms.resinfo[resind].name;
693 * See sel_updatefunc() for description of the parameters.
694 * \p data is not used.
696 * Returns the insertion code for each atom in \p out->u.s.
699 evaluate_insertcode(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
700 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
706 for (i = 0; i < g->isize; ++i)
708 resind = top->atoms.atom[g->index[i]].resind;
709 out->u.s[i][0] = top->atoms.resinfo[resind].ic;
714 * See sel_updatefunc() for description of the parameters.
715 * \p data is not used.
717 * Returns the chain for each atom in \p out->u.s.
720 evaluate_chain(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
721 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
727 for (i = 0; i < g->isize; ++i)
729 resind = top->atoms.atom[g->index[i]].resind;
730 out->u.s[i][0] = top->atoms.resinfo[resind].chainid;
735 * See sel_updatefunc() for description of the parameters.
736 * \p data is not used.
738 * Returns the mass for each atom in \p out->u.r.
741 evaluate_mass(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
742 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
747 for (i = 0; i < g->isize; ++i)
749 out->u.r[i] = top->atoms.atom[g->index[i]].m;
754 * See sel_updatefunc() for description of the parameters.
755 * \p data is not used.
757 * Returns the charge for each atom in \p out->u.r.
760 evaluate_charge(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
761 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
766 for (i = 0; i < g->isize; ++i)
768 out->u.r[i] = top->atoms.atom[g->index[i]].q;
773 check_pdbinfo(t_topology *top, int /* npar */, gmx_ana_selparam_t * /* param */, void * /* data */)
777 bOk = (top != NULL && top->atoms.pdbinfo != NULL);
780 GMX_THROW(gmx::InconsistentInputError("PDB info not available in topology"));
785 * See sel_updatefunc() for description of the parameters.
786 * \p data is not used.
788 * Returns the alternate location identifier for each atom in \p out->u.s.
791 evaluate_altloc(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
792 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
797 for (i = 0; i < g->isize; ++i)
799 out->u.s[i][0] = top->atoms.pdbinfo[g->index[i]].altloc;
804 * See sel_updatefunc() for description of the parameters.
805 * \p data is not used.
807 * Returns the occupancy numbers for each atom in \p out->u.r.
808 * Segfaults if PDB info is not found in the topology.
811 evaluate_occupancy(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
812 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
817 for (i = 0; i < g->isize; ++i)
819 out->u.r[i] = top->atoms.pdbinfo[g->index[i]].occup;
824 * See sel_updatefunc() for description of the parameters.
825 * \p data is not used.
827 * Returns the B-factors for each atom in \p out->u.r.
828 * Segfaults if PDB info is not found in the topology.
831 evaluate_betafactor(t_topology *top, t_trxframe * /* fr */, t_pbc * /* pbc */,
832 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void * /* data */)
837 for (i = 0; i < g->isize; ++i)
839 out->u.r[i] = top->atoms.pdbinfo[g->index[i]].bfac;
844 * Internal utility function for position keyword evaluation.
846 * \param[out] out Output array.
847 * \param[in] pos Position data to use instead of atomic coordinates
849 * \param[in] d Coordinate index to evaluate (\p XX, \p YY or \p ZZ).
851 * This function is used internally by evaluate_x(), evaluate_y() and
852 * evaluate_z() to do the actual evaluation.
855 evaluate_coord(real out[], gmx_ana_pos_t *pos, int d)
857 for (int b = 0; b < pos->count(); ++b)
859 const real v = pos->x[b][d];
860 for (int i = pos->m.mapb.index[b]; i < pos->m.mapb.index[b+1]; ++i)
865 // TODO: Make this more efficient by directly extracting the coordinates
866 // from the frame coordinates for atomic positions instead of going through
867 // a position calculation.
871 * See sel_updatefunc_pos() for description of the parameters.
872 * \p data is not used.
874 * Returns the \p x coordinate for each atom in \p out->u.r.
877 evaluate_x(t_topology * /*top*/, t_trxframe * /*fr*/, t_pbc * /*pbc*/,
878 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void * /*data*/)
880 out->nr = pos->m.mapb.nra;
881 evaluate_coord(out->u.r, pos, XX);
885 * See sel_updatefunc() for description of the parameters.
886 * \p data is not used.
888 * Returns the \p y coordinate for each atom in \p out->u.r.
891 evaluate_y(t_topology * /*top*/, t_trxframe * /*fr*/, t_pbc * /*pbc*/,
892 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void * /*data*/)
894 out->nr = pos->m.mapb.nra;
895 evaluate_coord(out->u.r, pos, YY);
899 * See sel_updatefunc() for description of the parameters.
900 * \p data is not used.
902 * Returns the \p z coordinate for each atom in \p out->u.r.
905 evaluate_z(t_topology * /*top*/, t_trxframe * /*fr*/, t_pbc * /*pbc*/,
906 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void * /*data*/)
908 out->nr = pos->m.mapb.nra;
909 evaluate_coord(out->u.r, pos, ZZ);