pdbrust 0.7.0

A comprehensive Rust library for parsing and analyzing Protein Data Bank (PDB) files
Documentation
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//! RCSB PDB Search API v2 client.
//!
//! This module provides functionality for searching the RCSB PDB using their
//! Search API v2.

use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::fmt;

use super::RCSB_SEARCH_URL;

/// Errors that can occur during RCSB search operations.
#[derive(Debug)]
pub enum SearchError {
    /// HTTP request failed
    RequestFailed(String),
    /// Failed to parse response
    ParseError(String),
    /// API returned an error
    ApiError(String),
    /// Invalid query parameters
    InvalidQuery(String),
}

impl fmt::Display for SearchError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SearchError::RequestFailed(msg) => write!(f, "Request failed: {}", msg),
            SearchError::ParseError(msg) => write!(f, "Parse error: {}", msg),
            SearchError::ApiError(msg) => write!(f, "API error: {}", msg),
            SearchError::InvalidQuery(msg) => write!(f, "Invalid query: {}", msg),
        }
    }
}

impl std::error::Error for SearchError {}

impl From<reqwest::Error> for SearchError {
    fn from(err: reqwest::Error) -> Self {
        SearchError::RequestFailed(err.to_string())
    }
}

impl From<serde_json::Error> for SearchError {
    fn from(err: serde_json::Error) -> Self {
        SearchError::ParseError(err.to_string())
    }
}

/// Experimental method filter for searches.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ExperimentalMethod {
    /// X-ray crystallography
    XRay,
    /// Nuclear Magnetic Resonance
    Nmr,
    /// Electron Microscopy
    Em,
    /// Other methods
    Other,
}

impl ExperimentalMethod {
    /// Get the RCSB API value for this method.
    pub fn api_value(&self) -> &'static str {
        match self {
            ExperimentalMethod::XRay => "X-RAY DIFFRACTION",
            ExperimentalMethod::Nmr => "SOLUTION NMR",
            ExperimentalMethod::Em => "ELECTRON MICROSCOPY",
            ExperimentalMethod::Other => "OTHER",
        }
    }
}

/// Polymer type filter for searches.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PolymerType {
    /// Protein structures
    Protein,
    /// DNA structures
    Dna,
    /// RNA structures
    Rna,
    /// Hybrid structures
    Hybrid,
}

impl PolymerType {
    /// Get the RCSB API value for this polymer type.
    pub fn api_value(&self) -> &'static str {
        match self {
            PolymerType::Protein => "Protein",
            PolymerType::Dna => "DNA",
            PolymerType::Rna => "RNA",
            PolymerType::Hybrid => "NA-hybrid",
        }
    }
}

/// Builder for RCSB search queries.
///
/// This struct provides a fluent interface for building search queries
/// against the RCSB PDB Search API v2.
///
/// # Examples
///
/// ```
/// use pdbrust::rcsb::SearchQuery;
///
/// let query = SearchQuery::new()
///     .with_text("kinase")
///     .with_organism("Homo sapiens")
///     .with_resolution_max(2.0);
/// ```
#[derive(Debug, Clone, Default)]
pub struct SearchQuery {
    /// Full-text search term
    pub text: Option<String>,
    /// Organism/source filter (scientific name)
    pub organism: Option<String>,
    /// Maximum resolution in Angstroms
    pub resolution_max: Option<f64>,
    /// Minimum resolution in Angstroms
    pub resolution_min: Option<f64>,
    /// Experimental method filter
    pub experimental_method: Option<ExperimentalMethod>,
    /// Polymer type filter
    pub polymer_type: Option<PolymerType>,
    /// Minimum release date (YYYY-MM-DD format)
    pub release_date_min: Option<String>,
    /// Maximum release date (YYYY-MM-DD format)
    pub release_date_max: Option<String>,
    /// Minimum sequence length
    pub sequence_length_min: Option<usize>,
    /// Maximum sequence length
    pub sequence_length_max: Option<usize>,
    /// PDB ID prefix filter
    pub pdb_id_prefix: Option<String>,
    /// Enzyme classification (EC) number
    pub ec_number: Option<String>,
}

impl SearchQuery {
    /// Create a new empty search query.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a full-text search term.
    pub fn with_text(mut self, text: &str) -> Self {
        self.text = Some(text.to_string());
        self
    }

    /// Filter by source organism (scientific name).
    pub fn with_organism(mut self, organism: &str) -> Self {
        self.organism = Some(organism.to_string());
        self
    }

    /// Set maximum resolution (Angstroms).
    pub fn with_resolution_max(mut self, resolution: f64) -> Self {
        self.resolution_max = Some(resolution);
        self
    }

    /// Set minimum resolution (Angstroms).
    pub fn with_resolution_min(mut self, resolution: f64) -> Self {
        self.resolution_min = Some(resolution);
        self
    }

    /// Filter by experimental method.
    pub fn with_experimental_method(mut self, method: ExperimentalMethod) -> Self {
        self.experimental_method = Some(method);
        self
    }

    /// Filter by polymer type.
    pub fn with_polymer_type(mut self, polymer_type: PolymerType) -> Self {
        self.polymer_type = Some(polymer_type);
        self
    }

    /// Set minimum release date (YYYY-MM-DD format).
    pub fn with_release_date_min(mut self, date: &str) -> Self {
        self.release_date_min = Some(date.to_string());
        self
    }

    /// Set maximum release date (YYYY-MM-DD format).
    pub fn with_release_date_max(mut self, date: &str) -> Self {
        self.release_date_max = Some(date.to_string());
        self
    }

    /// Set minimum sequence length.
    pub fn with_sequence_length_min(mut self, length: usize) -> Self {
        self.sequence_length_min = Some(length);
        self
    }

    /// Set maximum sequence length.
    pub fn with_sequence_length_max(mut self, length: usize) -> Self {
        self.sequence_length_max = Some(length);
        self
    }

    /// Filter by PDB ID prefix.
    pub fn with_pdb_id_prefix(mut self, prefix: &str) -> Self {
        self.pdb_id_prefix = Some(prefix.to_string());
        self
    }

    /// Filter by enzyme classification (EC) number.
    pub fn with_ec_number(mut self, ec: &str) -> Self {
        self.ec_number = Some(ec.to_string());
        self
    }

    /// Check if the query has any search criteria.
    pub fn is_empty(&self) -> bool {
        self.text.is_none()
            && self.organism.is_none()
            && self.resolution_max.is_none()
            && self.resolution_min.is_none()
            && self.experimental_method.is_none()
            && self.polymer_type.is_none()
            && self.release_date_min.is_none()
            && self.release_date_max.is_none()
            && self.sequence_length_min.is_none()
            && self.sequence_length_max.is_none()
            && self.pdb_id_prefix.is_none()
            && self.ec_number.is_none()
    }

    /// Convert the query to a JSON string for the RCSB API.
    pub fn to_json(&self) -> String {
        let query_value = self.build_query();
        serde_json::to_string(&query_value).unwrap_or_default()
    }

    /// Build the query JSON value.
    fn build_query(&self) -> Value {
        let mut nodes: Vec<Value> = Vec::new();

        // Full-text search
        if let Some(ref text) = self.text {
            nodes.push(json!({
                "type": "terminal",
                "service": "full_text",
                "parameters": {
                    "value": text
                }
            }));
        }

        // Organism filter
        if let Some(ref organism) = self.organism {
            nodes.push(json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "rcsb_entity_source_organism.ncbi_scientific_name",
                    "operator": "exact_match",
                    "value": organism
                }
            }));
        }

        // Resolution range
        if let Some(max) = self.resolution_max {
            nodes.push(json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "rcsb_entry_info.resolution_combined",
                    "operator": "less_or_equal",
                    "value": max
                }
            }));
        }

        if let Some(min) = self.resolution_min {
            nodes.push(json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "rcsb_entry_info.resolution_combined",
                    "operator": "greater_or_equal",
                    "value": min
                }
            }));
        }

        // Experimental method
        if let Some(ref method) = self.experimental_method {
            nodes.push(json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "exptl.method",
                    "operator": "exact_match",
                    "value": method.api_value()
                }
            }));
        }

        // Polymer type
        if let Some(ref polymer) = self.polymer_type {
            nodes.push(json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "entity_poly.rcsb_entity_polymer_type",
                    "operator": "exact_match",
                    "value": polymer.api_value()
                }
            }));
        }

        // Release date range
        if let Some(ref date) = self.release_date_min {
            nodes.push(json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "rcsb_accession_info.initial_release_date",
                    "operator": "greater_or_equal",
                    "value": date
                }
            }));
        }

        if let Some(ref date) = self.release_date_max {
            nodes.push(json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "rcsb_accession_info.initial_release_date",
                    "operator": "less_or_equal",
                    "value": date
                }
            }));
        }

        // Sequence length range
        if let Some(min) = self.sequence_length_min {
            nodes.push(json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "entity_poly.rcsb_sample_sequence_length",
                    "operator": "greater_or_equal",
                    "value": min
                }
            }));
        }

        if let Some(max) = self.sequence_length_max {
            nodes.push(json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "entity_poly.rcsb_sample_sequence_length",
                    "operator": "less_or_equal",
                    "value": max
                }
            }));
        }

        // EC number
        if let Some(ref ec) = self.ec_number {
            nodes.push(json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "rcsb_polymer_entity.rcsb_ec_lineage.id",
                    "operator": "exact_match",
                    "value": ec
                }
            }));
        }

        // PDB ID prefix
        if let Some(ref prefix) = self.pdb_id_prefix {
            nodes.push(json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "rcsb_entry_container_identifiers.entry_id",
                    "operator": "contains_phrase",
                    "value": prefix
                }
            }));
        }

        // Build the final query
        let query = if nodes.is_empty() {
            // Default to returning all structures if no criteria specified
            json!({
                "type": "terminal",
                "service": "text",
                "parameters": {
                    "attribute": "rcsb_entry_info.resolution_combined",
                    "operator": "exists"
                }
            })
        } else if nodes.len() == 1 {
            nodes.into_iter().next().unwrap()
        } else {
            json!({
                "type": "group",
                "logical_operator": "and",
                "nodes": nodes
            })
        };

        json!({
            "query": query,
            "return_type": "entry",
            "request_options": {
                "return_all_hits": true,
                "results_content_type": ["experimental"]
            }
        })
    }
}

/// Result from an RCSB search.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    /// List of PDB IDs matching the query
    pub pdb_ids: Vec<String>,
    /// Total number of results found
    pub total_count: usize,
}

/// Perform a search against the RCSB PDB.
///
/// # Arguments
///
/// * `query` - The search query
/// * `max_results` - Maximum number of results to return (0 for all)
///
/// # Returns
///
/// A `SearchResult` containing the matching PDB IDs.
///
/// # Errors
///
/// Returns a `SearchError` if the request fails or the response cannot be parsed.
///
/// # Examples
///
/// ```ignore
/// use pdbrust::rcsb::{SearchQuery, rcsb_search};
///
/// let query = SearchQuery::new()
///     .with_text("ubiquitin")
///     .with_resolution_max(2.0);
///
/// let result = rcsb_search(&query, 10)?;
/// println!("Found {} structures", result.pdb_ids.len());
/// ```
pub fn rcsb_search(query: &SearchQuery, max_results: usize) -> Result<SearchResult, SearchError> {
    let client = reqwest::blocking::Client::new();

    let json_body = query.to_json();

    let response = client
        .post(RCSB_SEARCH_URL)
        .header("Content-Type", "application/json")
        .body(json_body)
        .send()?;

    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().unwrap_or_default();
        return Err(SearchError::ApiError(format!("HTTP {}: {}", status, body)));
    }

    let body: Value = response.json()?;

    // Parse the response
    let total_count = body
        .get("total_count")
        .and_then(|v| v.as_u64())
        .unwrap_or(0) as usize;

    let mut pdb_ids: Vec<String> = body
        .get("result_set")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|item| {
                    item.get("identifier")
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string())
                })
                .collect()
        })
        .unwrap_or_default();

    // Limit results if requested
    if max_results > 0 && pdb_ids.len() > max_results {
        pdb_ids.truncate(max_results);
    }

    Ok(SearchResult {
        pdb_ids,
        total_count,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_search_query_default() {
        let query = SearchQuery::new();
        assert!(query.is_empty());
    }

    #[test]
    fn test_search_query_with_text() {
        let query = SearchQuery::new().with_text("kinase");
        assert_eq!(query.text, Some("kinase".to_string()));
        assert!(!query.is_empty());
    }

    #[test]
    fn test_search_query_chain() {
        let query = SearchQuery::new()
            .with_text("ubiquitin")
            .with_organism("Homo sapiens")
            .with_resolution_max(2.0)
            .with_experimental_method(ExperimentalMethod::XRay);

        assert_eq!(query.text, Some("ubiquitin".to_string()));
        assert_eq!(query.organism, Some("Homo sapiens".to_string()));
        assert_eq!(query.resolution_max, Some(2.0));
        assert_eq!(query.experimental_method, Some(ExperimentalMethod::XRay));
    }

    #[test]
    fn test_search_query_to_json() {
        let query = SearchQuery::new().with_text("kinase");
        let json = query.to_json();

        assert!(json.contains("kinase"));
        assert!(json.contains("full_text"));
    }

    #[test]
    fn test_search_query_resolution_range() {
        let query = SearchQuery::new()
            .with_resolution_min(1.5)
            .with_resolution_max(2.5);

        let json = query.to_json();
        assert!(json.contains("resolution_combined"));
    }

    #[test]
    fn test_experimental_method_api_value() {
        assert_eq!(ExperimentalMethod::XRay.api_value(), "X-RAY DIFFRACTION");
        assert_eq!(ExperimentalMethod::Nmr.api_value(), "SOLUTION NMR");
        assert_eq!(ExperimentalMethod::Em.api_value(), "ELECTRON MICROSCOPY");
    }

    #[test]
    fn test_polymer_type_api_value() {
        assert_eq!(PolymerType::Protein.api_value(), "Protein");
        assert_eq!(PolymerType::Dna.api_value(), "DNA");
        assert_eq!(PolymerType::Rna.api_value(), "RNA");
    }

    #[test]
    fn test_search_error_display() {
        let err = SearchError::RequestFailed("timeout".to_string());
        assert_eq!(err.to_string(), "Request failed: timeout");

        let err = SearchError::ApiError("404".to_string());
        assert_eq!(err.to_string(), "API error: 404");
    }
}