sdfrust 0.6.0

A fast, pure-Rust parser for SDF, MOL2, and XYZ chemical structure 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
//! XYZ format parser.
//!
//! XYZ is a simple format for molecular coordinates containing only atoms
//! (no bond information). It is commonly used in computational chemistry.
//!
//! ## Format
//!
//! ```text
//! 3                        <- atom count (integer)
//! water molecule           <- comment/title (molecule name)
//! O  0.000000  0.000000  0.117300    <- element x y z
//! H  0.756950  0.000000 -0.469200
//! H -0.756950  0.000000 -0.469200
//! ```
//!
//! Multi-molecule files concatenate blocks (no delimiter).

use std::collections::HashMap;
use std::io::BufRead;

use crate::atom::Atom;
use crate::error::{Result, SdfError};
use crate::molecule::Molecule;

/// Maps atomic numbers to element symbols.
fn atomic_number_to_symbol(num: u8) -> Option<&'static str> {
    match num {
        1 => Some("H"),
        2 => Some("He"),
        3 => Some("Li"),
        4 => Some("Be"),
        5 => Some("B"),
        6 => Some("C"),
        7 => Some("N"),
        8 => Some("O"),
        9 => Some("F"),
        10 => Some("Ne"),
        11 => Some("Na"),
        12 => Some("Mg"),
        13 => Some("Al"),
        14 => Some("Si"),
        15 => Some("P"),
        16 => Some("S"),
        17 => Some("Cl"),
        18 => Some("Ar"),
        19 => Some("K"),
        20 => Some("Ca"),
        26 => Some("Fe"),
        29 => Some("Cu"),
        30 => Some("Zn"),
        35 => Some("Br"),
        53 => Some("I"),
        _ => None,
    }
}

/// Normalizes an element symbol to proper case (first letter uppercase, rest lowercase).
fn normalize_element(element: &str) -> String {
    let element = element.trim();
    if element.is_empty() {
        return String::new();
    }

    let mut chars = element.chars();
    let first = chars.next().unwrap().to_uppercase().to_string();
    let rest: String = chars.collect::<String>().to_lowercase();
    first + &rest
}

/// XYZ format parser.
pub struct XyzParser<R> {
    reader: R,
    line_number: usize,
    current_line: String,
    peeked: bool,
}

impl<R: BufRead> XyzParser<R> {
    /// Creates a new parser from a buffered reader.
    pub fn new(reader: R) -> Self {
        Self {
            reader,
            line_number: 0,
            current_line: String::new(),
            peeked: false,
        }
    }

    /// Reads the next line from the input.
    fn read_line(&mut self) -> Result<bool> {
        if self.peeked {
            self.peeked = false;
            return Ok(!self.current_line.is_empty());
        }

        self.current_line.clear();
        let bytes_read = self.reader.read_line(&mut self.current_line)?;
        if bytes_read > 0 {
            self.line_number += 1;
            // Remove trailing newline
            if self.current_line.ends_with('\n') {
                self.current_line.pop();
                if self.current_line.ends_with('\r') {
                    self.current_line.pop();
                }
            }
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Skips blank lines and returns true if a non-blank line was found.
    fn skip_blank_lines(&mut self) -> Result<bool> {
        loop {
            if !self.read_line()? {
                return Ok(false);
            }
            if !self.current_line.trim().is_empty() {
                self.peeked = true;
                return Ok(true);
            }
        }
    }

    /// Parses the atom count line (first line of XYZ block).
    fn parse_atom_count_line(&self) -> Result<usize> {
        let trimmed = self.current_line.trim();
        trimmed
            .parse::<usize>()
            .map_err(|_| SdfError::InvalidCountsLine(format!("Invalid atom count: {}", trimmed)))
    }

    /// Parses a single atom line.
    /// Format: element x y z [additional columns ignored]
    fn parse_atom_line(&self, index: usize) -> Result<Atom> {
        let parts: Vec<&str> = self.current_line.split_whitespace().collect();

        if parts.len() < 4 {
            return Err(SdfError::Parse {
                line: self.line_number,
                message: format!("Atom line too short: {}", self.current_line),
            });
        }

        // First field is element (could be symbol like "C" or atomic number like "6")
        let element_str = parts[0];
        let element = if let Ok(atomic_num) = element_str.parse::<u8>() {
            // It's an atomic number
            atomic_number_to_symbol(atomic_num)
                .map(|s| s.to_string())
                .ok_or_else(|| SdfError::Parse {
                    line: self.line_number,
                    message: format!("Unknown atomic number: {}", atomic_num),
                })?
        } else {
            // It's an element symbol - normalize case
            normalize_element(element_str)
        };

        let x: f64 = parts[1]
            .parse()
            .map_err(|_| SdfError::InvalidCoordinate(parts[1].to_string()))?;
        let y: f64 = parts[2]
            .parse()
            .map_err(|_| SdfError::InvalidCoordinate(parts[2].to_string()))?;
        let z: f64 = parts[3]
            .parse()
            .map_err(|_| SdfError::InvalidCoordinate(parts[3].to_string()))?;

        Ok(Atom {
            index,
            element,
            x,
            y,
            z,
            formal_charge: 0,
            mass_difference: 0,
            stereo_parity: None,
            hydrogen_count: None,
            valence: None,
            v3000_id: None,
            atom_atom_mapping: None,
            rgroup_label: None,
            radical: None,
        })
    }

    /// Parses a single molecule from the input.
    /// Returns None if end of file is reached.
    pub fn parse_molecule(&mut self) -> Result<Option<Molecule>> {
        // Skip any leading blank lines
        if !self.skip_blank_lines()? {
            return Ok(None);
        }

        // Line 1: Atom count
        if !self.read_line()? {
            return Ok(None);
        }
        let num_atoms = self.parse_atom_count_line()?;

        // Line 2: Comment/title (molecule name)
        if !self.read_line()? {
            return Err(SdfError::MissingSection("XYZ comment line".to_string()));
        }
        let name = self.current_line.trim().to_string();

        // Parse atoms
        let mut atoms = Vec::with_capacity(num_atoms);
        for i in 0..num_atoms {
            if !self.read_line()? {
                return Err(SdfError::AtomCountMismatch {
                    expected: num_atoms,
                    found: i,
                });
            }
            let atom = self.parse_atom_line(i)?;
            atoms.push(atom);
        }

        Ok(Some(Molecule {
            name,
            program_line: None,
            comment: None,
            atoms,
            bonds: Vec::new(), // XYZ has no bond information
            properties: HashMap::new(),
            format_version: crate::molecule::SdfFormat::V2000,
            stereogroups: Vec::new(),
            sgroups: Vec::new(),
            collections: Vec::new(),
        }))
    }
}

/// Iterator over molecules in an XYZ file.
pub struct XyzIterator<R> {
    parser: XyzParser<R>,
    finished: bool,
}

impl<R: BufRead> XyzIterator<R> {
    /// Creates a new iterator from a buffered reader.
    pub fn new(reader: R) -> Self {
        Self {
            parser: XyzParser::new(reader),
            finished: false,
        }
    }
}

impl<R: BufRead> Iterator for XyzIterator<R> {
    type Item = Result<Molecule>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.finished {
            return None;
        }

        match self.parser.parse_molecule() {
            Ok(Some(mol)) => Some(Ok(mol)),
            Ok(None) => {
                self.finished = true;
                None
            }
            Err(e) => {
                self.finished = true;
                Some(Err(e))
            }
        }
    }
}

/// Parses a single molecule from an XYZ string.
pub fn parse_xyz_string(content: &str) -> Result<Molecule> {
    let cursor = std::io::Cursor::new(content);
    let reader = std::io::BufReader::new(cursor);
    let mut parser = XyzParser::new(reader);

    parser.parse_molecule()?.ok_or(SdfError::EmptyFile)
}

/// Parses all molecules from an XYZ string.
pub fn parse_xyz_string_multi(content: &str) -> Result<Vec<Molecule>> {
    let cursor = std::io::Cursor::new(content);
    let reader = std::io::BufReader::new(cursor);
    let iter = XyzIterator::new(reader);

    iter.collect()
}

/// Parses a single molecule from an XYZ file.
///
/// When the `gzip` feature is enabled, files ending in `.gz` are automatically
/// decompressed.
pub fn parse_xyz_file<P: AsRef<std::path::Path>>(path: P) -> Result<Molecule> {
    #[cfg(feature = "gzip")]
    {
        let reader = super::compression::open_maybe_gz(&path)?;
        let mut parser = XyzParser::new(reader);
        parser.parse_molecule()?.ok_or(SdfError::EmptyFile)
    }

    #[cfg(not(feature = "gzip"))]
    {
        if path
            .as_ref()
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("gz"))
        {
            return Err(SdfError::GzipNotEnabled);
        }
        let file = std::fs::File::open(path)?;
        let reader = std::io::BufReader::new(file);
        let mut parser = XyzParser::new(reader);
        parser.parse_molecule()?.ok_or(SdfError::EmptyFile)
    }
}

/// Parses all molecules from an XYZ file.
///
/// When the `gzip` feature is enabled, files ending in `.gz` are automatically
/// decompressed.
pub fn parse_xyz_file_multi<P: AsRef<std::path::Path>>(path: P) -> Result<Vec<Molecule>> {
    #[cfg(feature = "gzip")]
    {
        let reader = super::compression::open_maybe_gz(&path)?;
        let iter = XyzIterator::new(reader);
        iter.collect()
    }

    #[cfg(not(feature = "gzip"))]
    {
        if path
            .as_ref()
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("gz"))
        {
            return Err(SdfError::GzipNotEnabled);
        }
        let file = std::fs::File::open(path)?;
        let reader = std::io::BufReader::new(file);
        let iter = XyzIterator::new(reader);
        iter.collect()
    }
}

/// Returns an iterator over molecules in an XYZ file.
///
/// When the `gzip` feature is enabled, files ending in `.gz` are automatically
/// decompressed. Note that the return type differs based on the feature flag.
#[cfg(feature = "gzip")]
pub fn iter_xyz_file<P: AsRef<std::path::Path>>(
    path: P,
) -> Result<XyzIterator<super::compression::MaybeGzReader>> {
    let reader = super::compression::open_maybe_gz(&path)?;
    Ok(XyzIterator::new(reader))
}

/// Returns an iterator over molecules in an XYZ file.
#[cfg(not(feature = "gzip"))]
pub fn iter_xyz_file<P: AsRef<std::path::Path>>(
    path: P,
) -> Result<XyzIterator<std::io::BufReader<std::fs::File>>> {
    if path
        .as_ref()
        .extension()
        .is_some_and(|ext| ext.eq_ignore_ascii_case("gz"))
    {
        return Err(SdfError::GzipNotEnabled);
    }
    let file = std::fs::File::open(path)?;
    let reader = std::io::BufReader::new(file);
    Ok(XyzIterator::new(reader))
}

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

    const WATER_XYZ: &str = r#"3
water molecule
O  0.000000  0.000000  0.117300
H  0.756950  0.000000 -0.469200
H -0.756950  0.000000 -0.469200
"#;

    const METHANE_XYZ: &str = r#"5
methane
C   0.000000   0.000000   0.000000
H   0.628900   0.628900   0.628900
H  -0.628900  -0.628900   0.628900
H  -0.628900   0.628900  -0.628900
H   0.628900  -0.628900  -0.628900
"#;

    #[test]
    fn test_parse_water() {
        let mol = parse_xyz_string(WATER_XYZ).unwrap();

        assert_eq!(mol.name, "water molecule");
        assert_eq!(mol.atom_count(), 3);
        assert_eq!(mol.bond_count(), 0); // XYZ has no bonds
        assert_eq!(mol.formula(), "H2O");

        // Check oxygen
        assert_eq!(mol.atoms[0].element, "O");
        assert!((mol.atoms[0].x - 0.0).abs() < 1e-6);
        assert!((mol.atoms[0].y - 0.0).abs() < 1e-6);
        assert!((mol.atoms[0].z - 0.1173).abs() < 1e-6);

        // Check first hydrogen
        assert_eq!(mol.atoms[1].element, "H");
        assert!((mol.atoms[1].x - 0.75695).abs() < 1e-6);
    }

    #[test]
    fn test_parse_methane() {
        let mol = parse_xyz_string(METHANE_XYZ).unwrap();

        assert_eq!(mol.name, "methane");
        assert_eq!(mol.atom_count(), 5);
        assert_eq!(mol.formula(), "CH4");
    }

    #[test]
    fn test_parse_multi() {
        let multi = format!("{}{}", WATER_XYZ, METHANE_XYZ);
        let mols = parse_xyz_string_multi(&multi).unwrap();

        assert_eq!(mols.len(), 2);
        assert_eq!(mols[0].name, "water molecule");
        assert_eq!(mols[1].name, "methane");
    }

    #[test]
    fn test_parse_atomic_numbers() {
        let xyz = r#"3
atomic number test
8  0.0  0.0  0.0
1  1.0  0.0  0.0
1 -1.0  0.0  0.0
"#;
        let mol = parse_xyz_string(xyz).unwrap();

        assert_eq!(mol.atoms[0].element, "O");
        assert_eq!(mol.atoms[1].element, "H");
        assert_eq!(mol.atoms[2].element, "H");
    }

    #[test]
    fn test_normalize_element_case() {
        let xyz = r#"3
case test
o  0.0  0.0  0.0
h  1.0  0.0  0.0
CA -1.0  0.0  0.0
"#;
        let mol = parse_xyz_string(xyz).unwrap();

        assert_eq!(mol.atoms[0].element, "O");
        assert_eq!(mol.atoms[1].element, "H");
        assert_eq!(mol.atoms[2].element, "Ca");
    }

    #[test]
    fn test_extra_columns_ignored() {
        let xyz = r#"2
extra columns test
C  0.0  0.0  0.0  0.5  extra_data
H  1.0  0.0  0.0  -0.2
"#;
        let mol = parse_xyz_string(xyz).unwrap();

        assert_eq!(mol.atom_count(), 2);
        assert_eq!(mol.atoms[0].element, "C");
        assert_eq!(mol.atoms[1].element, "H");
    }

    #[test]
    fn test_blank_lines_between_molecules() {
        let xyz = format!("{}\n\n{}", WATER_XYZ, METHANE_XYZ);
        let mols = parse_xyz_string_multi(&xyz).unwrap();

        assert_eq!(mols.len(), 2);
    }

    #[test]
    fn test_empty_file() {
        let result = parse_xyz_string("");
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), SdfError::EmptyFile));
    }

    #[test]
    fn test_invalid_atom_count() {
        let xyz = r#"abc
test
C  0.0  0.0  0.0
"#;
        let result = parse_xyz_string(xyz);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            SdfError::InvalidCountsLine(_)
        ));
    }

    #[test]
    fn test_fewer_atoms_than_declared() {
        let xyz = r#"5
missing atoms
C  0.0  0.0  0.0
H  1.0  0.0  0.0
"#;
        let result = parse_xyz_string(xyz);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            SdfError::AtomCountMismatch {
                expected: 5,
                found: 2
            }
        ));
    }

    #[test]
    fn test_invalid_coordinate() {
        let xyz = r#"1
bad coords
C  abc  0.0  0.0
"#;
        let result = parse_xyz_string(xyz);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            SdfError::InvalidCoordinate(_)
        ));
    }

    #[test]
    fn test_iterator() {
        let multi = format!("{}{}{}", WATER_XYZ, METHANE_XYZ, WATER_XYZ);
        let cursor = std::io::Cursor::new(multi);
        let reader = std::io::BufReader::new(cursor);
        let iter = XyzIterator::new(reader);

        let mols: Vec<_> = iter.map(|r| r.unwrap()).collect();
        assert_eq!(mols.len(), 3);
        assert_eq!(mols[0].name, "water molecule");
        assert_eq!(mols[1].name, "methane");
        assert_eq!(mols[2].name, "water molecule");
    }
}