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
//! PyMOL/VMD-style selection language for atom filtering.
//!
//! This module provides a powerful query language for selecting atoms
//! from PDB structures, inspired by PyMOL, VMD, and MDAnalysis.
//!
//! # Syntax
//!
//! ## Basic Selectors
//!
//! | Selector | Description | Example |
//! |----------|-------------|---------|
//! | `chain X` | Select atoms from chain X | `chain A` |
//! | `name X` | Select atoms named X | `name CA` |
//! | `resname X` | Select residue type X | `resname ALA` |
//! | `resid N` | Select residue number N | `resid 50` |
//! | `resid N:M` | Select residues N through M | `resid 1:100` |
//! | `element X` | Select element type X | `element C` |
//!
//! ## Keywords
//!
//! | Keyword | Description |
//! |---------|-------------|
//! | `backbone` | N, CA, C, O atoms |
//! | `protein` | Standard amino acids |
//! | `nucleic` | Standard nucleotides |
//! | `water` | Water molecules (HOH, WAT) |
//! | `hetero` | HETATM records (non-protein, non-nucleic) |
//! | `hydrogen` | Hydrogen atoms |
//! | `all` or `*` | All atoms |
//!
//! ## Numeric Comparisons
//!
//! | Selector | Description |
//! |----------|-------------|
//! | `bfactor < 30.0` | B-factor less than 30 |
//! | `bfactor >= 20.0` | B-factor at least 20 |
//! | `occupancy > 0.5` | Occupancy greater than 0.5 |
//!
//! ## Boolean Operations
//!
//! - `and` - Both conditions must match
//! - `or` - Either condition matches
//! - `not` - Negation
//! - `()` - Grouping with parentheses
//!
//! # Examples
//!
//! ```ignore
//! use pdbrust::{parse_pdb_file, PdbStructure};
//!
//! let structure = parse_pdb_file("protein.pdb")?;
//!
//! // Select CA atoms from chain A
//! let selected = structure.select("chain A and name CA")?;
//!
//! // Select protein atoms with low B-factor
//! let low_bfactor = structure.select("protein and bfactor < 30.0")?;
//!
//! // Complex selection with grouping
//! let complex = structure.select("(chain A or chain B) and backbone and not hydrogen")?;
//!
//! // Select residue range
//! let residues = structure.select("resid 1:50 and name CA")?;
//! ```
//!
//! # Alternative Keyword Forms
//!
//! The selection language supports alternative forms for keywords:
//!
//! - `chain` / `chainid`
//! - `name` / `atomname`
//! - `resname` / `resn`
//! - `resid` / `resi` / `resnum`
//! - `element` / `elem`
//! - `bfactor` / `b` / `tempfactor`
//! - `occupancy` / `occ`
//! - `backbone` / `bb`
//! - `water` / `waters` / `solvent`
//! - `hetero` / `hetatm` / `het`
//! - `hydrogen` / `hydrogens` / `h`
pub use ;
pub use SelectionError;
use cratePdbStructure;
use Lexer;
use Parser;
/// Parse a selection string into an AST.
///
/// This function parses the selection string and returns the abstract syntax tree.
/// It does not execute the selection against a structure.
///
/// # Arguments
///
/// * `selection` - A selection string (e.g., "chain A and name CA")
///
/// # Returns
///
/// The parsed AST, or a `SelectionError` if the selection string is invalid.
///
/// # Examples
///
/// ```ignore
/// use pdbrust::filter::selection::parse_selection;
///
/// let ast = parse_selection("chain A and name CA")?;
/// ```
/// Execute a selection expression against a structure.
///
/// This function filters atoms from a structure based on the given AST.
///
/// # Arguments
///
/// * `structure` - The structure to filter
/// * `expr` - The selection expression AST
///
/// # Returns
///
/// A new `PdbStructure` containing only the selected atoms.
/// Validate a selection string without executing it.
///
/// This function parses the selection string and returns an error if it's invalid.
/// Useful for validation in user interfaces or configuration parsing.
///
/// # Arguments
///
/// * `selection` - A selection string to validate
///
/// # Returns
///
/// `Ok(())` if the selection is valid, or a `SelectionError` if invalid.
///
/// # Examples
///
/// ```ignore
/// use pdbrust::filter::selection::validate_selection;
///
/// assert!(validate_selection("chain A and name CA").is_ok());
/// assert!(validate_selection("invalid syntax (").is_err());
/// ```