-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathselect.cpp
More file actions
334 lines (313 loc) · 9.74 KB
/
select.cpp
File metadata and controls
334 lines (313 loc) · 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2018 Global Phasing Ltd.
#include "gemmi/select.hpp"
#include <cstdlib> // for strtol
#include <cctype> // for isalpha
#include "gemmi/sprintf.hpp" // for to_str
#include "gemmi/atof.hpp" // for fast_from_chars
namespace gemmi {
namespace {
[[noreturn]]
inline GEMMI_COLD void wrong_syntax(const std::string& cid, size_t pos,
const char* info=nullptr) {
std::string msg = "Invalid selection syntax";
if (info)
msg += info;
if (pos != 0)
cat_to(msg, " near \"", cid.substr(pos, 8), '"');
cat_to(msg, ": ", cid);
fail(msg);
}
inline int determine_omitted_cid_fields(const std::string& cid) {
if (cid[0] == '/')
return 0; // model
if (std::isdigit(cid[0]) || cid[0] == '.' || cid[0] == '(' || cid[0] == '-')
return 2; // residue
size_t sep = cid.find_first_of("/([:;");
if (sep == std::string::npos || cid[sep] == '/' || cid[sep] == ';')
return 1; // chain
if (cid[sep] == '(')
return 2; // residue
return 3; // atom
}
inline Selection::List make_cid_list(const std::string& cid, size_t pos, size_t end,
const char* disallowed_chars="-[]()!/*.:;") {
Selection::List list;
list.all = (cid[pos] == '*');
list.inverted = (cid[pos] == '!');
if (list.all || list.inverted)
++pos;
list.list = cid.substr(pos, end - pos);
// if a list have punctuation other than ',' something must be wrong
size_t idx = list.list.find_first_of(disallowed_chars);
if (idx != std::string::npos)
wrong_syntax(cid, pos + idx, cat(" ('", list.list[idx], "' in a list)").c_str());
return list;
}
inline void parse_cid_elements(const std::string& cid, size_t pos,
std::vector<char>& elements) {
elements.clear(); // just in case
if (cid[pos] == '*')
return;
bool inverted = false;
if (cid[pos] == '!') {
inverted = true;
++pos;
}
elements.resize((size_t)El::END, char(inverted));
for (;;) {
size_t sep = cid.find_first_of(",]", pos);
if (sep == pos || sep > pos + 2) {
if (sep == pos + 6 && cid.compare(pos, 6, "metals", 6) == 0) {
for (size_t i = 0; i < elements.size(); ++i)
if (is_metal(static_cast<El>(i)))
elements[i] = char(!inverted);
} else if (sep == pos + 9 && cid.compare(pos, 9, "nonmetals", 9) == 0) {
for (size_t i = 0; i < elements.size(); ++i)
if (!is_metal(static_cast<El>(i)))
elements[i] = char(!inverted);
} else {
wrong_syntax(cid, 0, " in [...]");
}
} else {
char elem_str[2] = {cid[pos], sep > pos+1 ? cid[pos+1] : '\0'};
Element el = find_element(elem_str);
if (el == El::X && (alpha_up(elem_str[0]) != 'X' || elem_str[1] != '\0'))
wrong_syntax(cid, 0, " (invalid element in [...])");
elements[el.ordinal()] = char(!inverted);
}
if (cid[sep] == ']')
break;
pos = sep + 1;
}
}
inline Selection::SequenceId parse_cid_seqid(const std::string& cid, size_t& pos,
int default_seqnum) {
size_t initial_pos = pos;
int seqnum = default_seqnum;
char icode = ' ';
if (cid[pos] == '*') {
++pos;
icode = '*';
} else if (std::isdigit(cid[pos]) || cid[pos] == '-') {
char* endptr;
seqnum = std::strtol(&cid[pos], &endptr, 10);
pos = endptr - &cid[0];
}
if (cid[pos] == '.')
++pos;
if (initial_pos != pos && (std::isalpha(cid[pos]) || cid[pos] == '*'))
icode = cid[pos++];
return {seqnum, icode};
}
inline Selection::AtomInequality parse_atom_inequality(const std::string& cid,
size_t pos, size_t end) {
Selection::AtomInequality r;
if (cid[pos] != 'q' && cid[pos] != 'b')
wrong_syntax(cid, pos);
r.property = cid[pos];
++pos;
while (cid[pos] == ' ')
++pos;
if (cid[pos] == '<')
r.relation = -1;
else if (cid[pos] == '>')
r.relation = 1;
else if (cid[pos] == '=')
r.relation = 0;
else
wrong_syntax(cid, pos);
++pos;
auto result = fast_from_chars(cid.c_str() + pos, r.value);
if (result.ec != std::errc())
wrong_syntax(cid, pos, " (expected number)");
pos = size_t(result.ptr - cid.c_str());
while (cid[pos] == ' ')
++pos;
if (pos != end)
wrong_syntax(cid, pos);
return r;
}
inline bool has_inequality(const std::string& cid, size_t start, size_t end) {
for (size_t i = start; i < end; ++i)
if (cid[i] == '<' || cid[i] == '=' || cid[i] == '>')
return true;
return false;
}
inline void parse_cid(const std::string& cid, Selection& sel) {
if (cid.empty() || (cid.size() == 1 && cid[0] == '*'))
return;
int omit = determine_omitted_cid_fields(cid);
size_t sep = 0;
size_t semi = cid.find(';');
// model
if (omit == 0) {
sep = std::min(cid.find('/', 1), semi);
if (sep != 1 && cid[1] != '*') {
char* endptr;
sel.mdl = std::strtol(&cid[1], &endptr, 10);
size_t end_pos = endptr - &cid[0];
if (end_pos != sep && end_pos != cid.size())
wrong_syntax(cid, 0, " (at model number)");
}
}
// chain
if (omit <= 1 && sep < semi) {
size_t pos = (sep == 0 ? 0 : sep + 1);
sep = std::min(cid.find('/', pos), semi);
// These characters are not really disallowed, but are unexpected.
// "-" is expected, it's in chain IDs in bioassembly files from RCSB.
const char* disallowed_chars = "[]()!/*.:;";
sel.chain_ids = make_cid_list(cid, pos, sep, disallowed_chars);
}
// residue; MMDB CID syntax: s1.i1-s2.i2 or *(res).ic
// In gemmi both 14.a and 14a are accepted.
// *(ALA). and *(ALA) and (ALA). can be used instead of (ALA) for
// compatibility with MMDB.
if (omit <= 2 && sep < semi) {
size_t pos = (sep == 0 ? 0 : sep + 1);
if (cid[pos] != '(')
sel.from_seqid = parse_cid_seqid(cid, pos, INT_MIN);
if (cid[pos] == '(') {
++pos;
size_t right_br = cid.find(')', pos);
sel.residue_names = make_cid_list(cid, pos, right_br);
pos = right_br + 1;
}
// allow "(RES)." and "(RES).*" and "(RES)*"
if (cid[pos] == '.')
++pos;
if (cid[pos] == '*')
++pos;
if (cid[pos] == '-') {
++pos;
sel.to_seqid = parse_cid_seqid(cid, pos, INT_MAX);
} else if (sel.from_seqid.seqnum != INT_MIN) {
sel.to_seqid = sel.from_seqid;
}
sep = pos;
if (cid[sep] != '/' && cid[sep] != ';' && cid[sep] != '\0')
wrong_syntax(cid, 0, " (at residue)");
}
// atom; at[el]:aloc
if (sep < std::min(cid.size(), semi)) {
size_t pos = (sep == 0 ? 0 : sep + 1);
size_t end = cid.find_first_of("[:;", pos);
if (end != pos) {
sel.atom_names = make_cid_list(cid, pos, end);
// Chain name can be empty, but not atom name,
// so we interpret empty atom name as *.
if (!sel.atom_names.inverted && sel.atom_names.list.empty())
sel.atom_names.all = true;
if (end == std::string::npos)
return;
}
if (cid[end] == '[') {
pos = end + 1;
end = cid.find(']', pos);
if (end == std::string::npos)
wrong_syntax(cid, 0, " (no matching ']')");
parse_cid_elements(cid, pos, sel.elements);
++end;
}
if (cid[end] == ':') {
pos = end + 1;
sel.altlocs = make_cid_list(cid, pos, semi);
}
}
// extensions after semicolon(s)
while (semi < cid.size()) {
size_t pos = semi + 1;
while (cid[pos] == ' ')
++pos;
semi = std::min(cid.find(';', pos), cid.size());
size_t end = semi;
while (end > pos && cid[end-1] == ' ')
--end;
if (has_inequality(cid, pos, end)) {
sel.atom_inequalities.push_back(parse_atom_inequality(cid, pos, end));
} else {
sel.entity_types = make_cid_list(cid, pos, end);
bool inv = sel.entity_types.inverted;
std::fill(sel.et_flags.begin(), sel.et_flags.end(), char(inv));
for (const std::string& item : split_str(sel.entity_types.list, ',')) {
EntityType et = EntityType::Unknown;
if (item == "polymer")
et = EntityType::Polymer;
else if (item == "solvent")
et = EntityType::Water;
else
wrong_syntax(cid, 0, (" at " + item).c_str());
sel.et_flags[(int)et] = char(!inv);
}
}
}
}
} // anonymous namespace
Selection::Selection(const std::string& cid) {
parse_cid(cid, *this);
}
std::string Selection::SequenceId::str() const {
std::string s;
if (!empty()) {
s = std::to_string(seqnum);
if (icode != '*') {
s += '.';
if (icode != ' ')
s += icode;
}
}
return s;
}
std::string Selection::AtomInequality::str() const {
std::string r = ";";
r += property;
r += relation == 0 ? '=' : relation < 0 ? '<' : '>';
r += to_str(value);
return r;
}
std::string Selection::str() const {
std::string cid = "/";
if (mdl != 0)
cid += std::to_string(mdl);
cid += '/';
cid += chain_ids.str();
cid += '/';
cid += from_seqid.str();
if (!residue_names.all) {
cid += '(';
cid += residue_names.str();
cid += ')';
}
if ((!from_seqid.empty() || !to_seqid.empty()) &&
(from_seqid.seqnum != to_seqid.seqnum || from_seqid.icode != to_seqid.icode)) {
cid += '-';
cid += to_seqid.str();
}
cid += '/';
if (!atom_names.all)
cid += atom_names.str();
if (!elements.empty()) {
cid += '[';
bool inv = (std::count(elements.begin(), elements.end(), 1) > 64);
if (inv)
cid += '!';
for (size_t i = 0; i < elements.size(); ++i)
if (elements[i] != char(inv)) {
cid += element_name(static_cast<El>(i));
cid += ',';
}
cid.back() = ']';
}
if (!altlocs.all) {
cid += ':';
cid += altlocs.str();
}
if (!entity_types.all) {
cid += ';';
cid += entity_types.str();
}
for (const AtomInequality& ai : atom_inequalities)
cid += ai.str();
return cid;
}
} // namespace gemmi