libcootapi
 
Loading...
Searching...
No Matches
residue-and-atom-specs.hh
1/* coot-utils/residue-and-atom-specs.hh
2 *
3 * Copyright 2011, 2012 by The University of Oxford
4 * Copyright 2013, 2015 by Medical Research Council
5 * Author: Paul Emsley
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License and
18 * the GNU Lesser General Public License along with this program; if not,
19 * write to the Free Software Foundation, Inc., 51 Franklin Street,
20 * Fifth Floor, Boston, MA, 02110-1301, USA.
21 */
22
23#ifndef RESIDUE_AND_ATOM_SPEC_HH
24#define RESIDUE_AND_ATOM_SPEC_HH
25
26#include <sstream>
27#ifndef __MMDB_Defs__
28#include <mmdb2/mmdb_manager.h>
29#endif
30
31namespace coot {
32
33 // using an atom spec as the key in a map: strange order/find() failures?
34 //
35 class atom_spec_t {
36 public:
37 std::string chain_id;
38 int res_no;
39 std::string ins_code;
40 std::string atom_name;
41 std::string alt_conf;
42 int int_user_data;
43 float float_user_data;
44 std::string string_user_data;
45 int model_number;
46 atom_spec_t() : chain_id("unset") {
47 res_no = mmdb::MinInt4;
48 model_number = -1;
49 int_user_data = -1;
50 float_user_data = -1;
51 }
52 atom_spec_t(const std::string &chain_in,
53 int resno_in,
54 const std::string &insertion_code_in,
55 const std::string &atom_name_in,
56 const std::string &alt_conf_in) : chain_id(chain_in), ins_code(insertion_code_in), atom_name(atom_name_in), alt_conf(alt_conf_in) {
57 res_no = resno_in;
58 model_number = 1;
59 int_user_data = -1;
60 float_user_data = -1;
61 }
62 // This presumes at is a member of a coordinate hierarchy.
63 explicit atom_spec_t(mmdb::Atom *at) {
64 if (at) {
65 chain_id = at->GetChainID();
66 res_no = at->GetSeqNum();
67 ins_code = at->GetInsCode();
68 model_number = at->GetModelNum();
69 atom_name = at->name;
70 alt_conf = at->altLoc;
71 } else {
72 chain_id = "unset";
73 res_no = mmdb::MinInt4;
74 ins_code = "";
75 model_number = -1;
76 }
77 int_user_data = -1; // mark as "unset" (better than not setting it)
78 float_user_data = -1;
79 }
80 // This presumes at is a member of a coordinate hierarchy.
81 atom_spec_t(mmdb::Atom *at, const std::string &user_data_string) {
82 model_number = at->GetModelNum();
83 chain_id = at->GetChainID();
84 res_no = at->GetSeqNum();
85 ins_code = at->GetInsCode();
86 atom_name = at->name;
87 alt_conf = at->altLoc;
88 string_user_data = user_data_string;
89 float_user_data = -1;
90 int_user_data = -1;
91 }
92
93 bool empty() const {
94 if (res_no == mmdb::MinInt4)
95 return true;
96 else
97 return false;
98 }
99
100 void selectatoms(mmdb::Manager *mol, int SelHnd) {
101 const char *chainid = chain_id.c_str();
102 const char *inscode = ins_code.c_str();
103 const char *atname = atom_name.c_str(); // atom name
104 const char *altconf = alt_conf.c_str();
105
106 mol->SelectAtoms(SelHnd, 0, chainid, res_no, inscode, res_no, inscode,
107 "*", atname, "*", altconf);
108 }
109
110 // Presumes that atom can get to SeqNum() and InsCode()? Need
111 // tested against a residue not in a hierarchy.
112 bool matches_spec(mmdb::Atom *atom) const;
113
114 std::string format() const;
115
116 std::string label() const;
117
118 std::string label(const std::string &residue_name) const;
119
120 // A 65 CA
121 std::string simple_label(const std::string &residue_name="") const;
122
123#ifndef SWIG
124 bool operator==(const atom_spec_t &matcher) const {
125 bool r = false;
126 if (matcher.model_number == model_number) {
127 if (matcher.chain_id == chain_id) {
128 if (matcher.res_no == res_no) {
129 if (matcher.ins_code == ins_code) {
130 if (matcher.atom_name == atom_name) {
131 if (matcher.alt_conf == alt_conf) {
132 r = true;
133 }
134 }
135 }
136 }
137 }
138 }
139 return r;
140 }
141 bool operator !=(const atom_spec_t &matcher) const {
142 return ! operator==(matcher);
143 }
144#endif
145
146#ifndef SWIG
147 // we need this if atom_spec_t are used in a std::map.
148 bool operator<(const atom_spec_t &matcher) const {
149 if (matcher.empty())
150 return false;
151 if (empty())
152 return true;
153 if (matcher.model_number < model_number) {
154 return true;
155 } else {
156 if (matcher.chain_id < chain_id) {
157 return true;
158 } else {
159 if (matcher.res_no < res_no) {
160 return true;
161 } else {
162 if (matcher.ins_code < ins_code) {
163 return true;
164 } else {
165 if (matcher.atom_name < atom_name) {
166 return true;
167 } else {
168 if (matcher.alt_conf < alt_conf) {
169 return true;
170 }
171 }
172 }
173 }
174 }
175 }
176 return false;
177 }
178#endif // SWIG
179
180 // like operator==() but we don't test the model
181 bool is_same(const atom_spec_t &matcher) const {
182 bool r = false;
183 if (matcher.chain_id == chain_id) {
184 if (matcher.res_no == res_no) {
185 if (matcher.ins_code == ins_code) {
186 if (matcher.atom_name == atom_name) {
187 if (matcher.alt_conf == alt_conf) {
188 r = true;
189 }
190 }
191 }
192 }
193 }
194 return r;
195 }
196
197 // return null on failure to find atom in mol
198 // (this is the inside out version of the function in molecule_class_info_t)
199 mmdb::Atom *get_atom(mmdb::Manager *mol) const;
200
201#ifndef SWIG
202 friend std::ostream& operator<< (std::ostream& s, const atom_spec_t &spec);
203#endif // SWIG
204 };
205#ifndef SWIG
206 std::ostream& operator<< (std::ostream& s, const atom_spec_t &spec);
207#endif // SWIG
208
209 bool compare_atom_specs_user_float(const atom_spec_t &a1,
210 const atom_spec_t &a2);
211 bool compare_atom_specs_user_float_in_pair(const std::pair<atom_spec_t, std::string> &a,
212 const std::pair<atom_spec_t, std::string> &b);
213 std::pair<atom_spec_t, atom_spec_t> link_atoms(mmdb::Link *link, mmdb::Model *model_p=0);
214 std::pair<atom_spec_t, atom_spec_t> link_atoms(mmdb::LinkR *link, mmdb::Model *model_p=0);
215
216 class residue_spec_t {
217 public:
218 int model_number;
219 std::string chain_id;
220 int res_no;
221 std::string ins_code;
222 int int_user_data;
223 float float_user_data;
224 std::string string_user_data;
225 explicit residue_spec_t(int r) : res_no(r) {
226 model_number = -1;
227 int_user_data = -1;
228 float_user_data = -1;
229 }
230 residue_spec_t(const std::string &chain_in, int r) : chain_id(chain_in) {
231 model_number = mmdb::MinInt4;
232 res_no = r;
233 int_user_data = -1;
234 float_user_data = -1;
235 }
236 residue_spec_t(int model_number_in,
237 const std::string &chain_in, int r,
238 const std::string &ins_code_in) : chain_id(chain_in), ins_code(ins_code_in) {
239 model_number = model_number_in;
240 res_no = r;
241 int_user_data = -1;
242 float_user_data = -1;
243 }
244 residue_spec_t(const std::string &chain_in, int r,
245 const std::string &ins_code_in) : chain_id(chain_in), ins_code(ins_code_in) {
246 model_number = mmdb::MinInt4;
247 res_no = r;
248 int_user_data = -1;
249 float_user_data = -1;
250 }
251 explicit residue_spec_t(mmdb::Residue *res) {
252 if (! res) {
253 chain_id = "";
254 model_number = mmdb::MinInt4;
255 res_no = mmdb::MinInt4;
256 ins_code = "";
257 } else {
258 chain_id = res->GetChainID();
259 model_number = res->GetModelNum();
260 res_no = res->GetSeqNum();
261 ins_code = res->GetInsCode();
262 }
263 int_user_data = -1;
264 }
265 explicit residue_spec_t(const atom_spec_t &atom_spec) : chain_id(atom_spec.chain_id), ins_code(atom_spec.ins_code) {
266 model_number = atom_spec.model_number;
267 res_no = atom_spec.res_no;
268 int_user_data = -1;
269 float_user_data = -1;
270 }
271 // This one for coot_wrap_guile
272 residue_spec_t() {
273 model_number = mmdb::MinInt4;
274 res_no = mmdb::MinInt4;
275 int_user_data = -1;
276 float_user_data = -1;
277 }
278 bool unset_p() const {
279 bool u = true;
280 if (res_no != mmdb::MinInt4)
281 u = false;
282 return u;
283 }
284 bool empty() const {
285 return unset_p();
286 }
287 residue_spec_t next() const {
288 residue_spec_t r = *this;
289 if (res_no != mmdb::MinInt4)
290 r.res_no += 1;
291 return r;
292 }
293 residue_spec_t previous() const {
294 residue_spec_t r = *this;
295 if (res_no != mmdb::MinInt4)
296 r.res_no -= 1;
297 return r;
298 }
299#ifndef SWIG
300 bool operator==(const residue_spec_t &matcher) const {
301 if (matcher.chain_id == chain_id) {
302 if (matcher.res_no == res_no) {
303 if (matcher.ins_code == ins_code) {
304 return 1;
305 }
306 }
307 }
308 return 0;
309 }
310#endif // SWIG
311
312#ifdef SWIG
313#else
314 bool operator<(const residue_spec_t &matcher) const{
315 if (matcher.chain_id == chain_id) {
316 if (matcher.res_no == res_no) {
317 if (matcher.ins_code == ins_code) {
318 return 0;
319 } else {
320 if (matcher.ins_code < ins_code)
321 return 0;
322 else
323 return 1;
324 }
325 } else {
326 if (matcher.res_no < res_no)
327 return 0;
328 else
329 return 1;
330 }
331 } else {
332 if (matcher.chain_id < chain_id)
333 return 0;
334 else
335 return 1;
336 }
337 // return 0; we can't here
338 }
339#endif
340
341 std::string format() const;
342
343 std::string label() const;
344
345 std::string label(const std::string &residue_name) const;
346
347 // return null on failure to find residue in mol
348 mmdb::Residue *get_residue(mmdb::Manager *mol) const;
349
350 // return an atom selection handle for the selection in the mol
351 // that matches the spec. Caller is responsible for deleting
352 // the atom selection.
353 //
354 // selection_key_type is typically either mmdb::SKEY_NEW or mmdb::SKEY_OR
355 //
356 int select_atoms(mmdb::Manager *mol, int selhnd,
357 mmdb::SELECTION_KEY selection_key);
358
359#ifndef SWIG
360 friend std::ostream& operator<< (std::ostream& s, const residue_spec_t &spec);
361#endif // SWIG
362 };
363#ifndef SWIG
364 std::ostream& operator<< (std::ostream& s, const residue_spec_t &spec);
365#endif // SWIG
366
367}
368
369#endif // RESIDUE_AND_ATOM_SPEC_HH
Definition residue-and-atom-specs.hh:35
Definition residue-and-atom-specs.hh:216