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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//! TRIPOS MOL2 format parser.
//!
//! MOL2 is a common format for storing molecular structures with atom types
//! and partial charges. It uses a record-based format with sections marked
//! by `@<TRIPOS>SECTION_NAME`.
//!
//! ## Supported Sections
//! - `@<TRIPOS>MOLECULE` - Molecule name and counts
//! - `@<TRIPOS>ATOM` - Atom coordinates, types, and charges
//! - `@<TRIPOS>BOND` - Bond connectivity and types

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

use crate::atom::Atom;
use crate::bond::{Bond, BondOrder, BondStereo};
use crate::error::{Result, SdfError};
use crate::molecule::Molecule;

/// MOL2 bond type to BondOrder mapping
fn mol2_bond_type_to_order(bond_type: &str) -> BondOrder {
    match bond_type {
        "1" | "1n" => BondOrder::Single,
        "2" => BondOrder::Double,
        "3" => BondOrder::Triple,
        "ar" | "am" => BondOrder::Aromatic,
        "du" => BondOrder::Single, // Dummy bond
        "un" => BondOrder::Single, // Unknown, treat as single
        "nc" => BondOrder::Single, // Not connected, treat as single
        _ => BondOrder::Single,    // Default to single
    }
}

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

impl<R: BufRead> Mol2Parser<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)
        }
    }

    /// Peeks at the current line without consuming it.
    fn peek_line(&mut self) -> Result<Option<&str>> {
        if !self.peeked {
            if !self.read_line()? {
                return Ok(None);
            }
            self.peeked = true;
        }
        Ok(Some(&self.current_line))
    }

    /// Skips lines until a section header is found.
    fn skip_to_section(&mut self) -> Result<Option<String>> {
        loop {
            if !self.read_line()? {
                return Ok(None);
            }
            if self.current_line.starts_with("@<TRIPOS>") {
                let section = self.current_line[9..].trim().to_uppercase();
                return Ok(Some(section));
            }
        }
    }

    /// Parses a single molecule from the input.
    /// Returns None if end of file is reached.
    pub fn parse_molecule(&mut self) -> Result<Option<Molecule>> {
        // Find @<TRIPOS>MOLECULE section
        loop {
            match self.skip_to_section()? {
                Some(section) if section == "MOLECULE" => break,
                Some(_) => continue,
                None => return Ok(None),
            }
        }

        // Parse MOLECULE section
        // Line 1: molecule name
        if !self.read_line()? {
            return Err(SdfError::MissingSection("MOLECULE name".to_string()));
        }
        let name = self.current_line.trim().to_string();

        // Line 2: num_atoms num_bonds [num_subst num_feat num_sets]
        if !self.read_line()? {
            return Err(SdfError::MissingSection("MOLECULE counts".to_string()));
        }
        let counts: Vec<&str> = self.current_line.split_whitespace().collect();
        if counts.len() < 2 {
            return Err(SdfError::InvalidCountsLine(self.current_line.clone()));
        }

        let num_atoms: usize = counts[0].parse().map_err(|_| {
            SdfError::InvalidCountsLine(format!("Invalid atom count: {}", counts[0]))
        })?;
        let num_bonds: usize = counts[1].parse().map_err(|_| {
            SdfError::InvalidCountsLine(format!("Invalid bond count: {}", counts[1]))
        })?;

        // Line 3: molecule type (optional, skip)
        if !self.read_line()? {
            return Err(SdfError::MissingSection("MOLECULE type".to_string()));
        }

        // Line 4: charge type (optional, skip)
        if !self.read_line()? {
            return Err(SdfError::MissingSection("MOLECULE charge type".to_string()));
        }

        // Skip remaining lines until next section
        // (status bits, comment, etc.)

        let mut atoms = Vec::with_capacity(num_atoms);
        let mut bonds = Vec::with_capacity(num_bonds);
        let properties = HashMap::new();

        // Parse sections until we hit another MOLECULE or EOF
        loop {
            match self.peek_line()? {
                None => break,
                Some(line) if line.starts_with("@<TRIPOS>MOLECULE") => break,
                Some(line) if line.starts_with("@<TRIPOS>") => {
                    let section = line[9..].trim().to_uppercase();
                    self.read_line()?; // consume the section header

                    match section.as_str() {
                        "ATOM" => {
                            atoms = self.parse_atom_section(num_atoms)?;
                        }
                        "BOND" => {
                            bonds = self.parse_bond_section(num_bonds, atoms.len())?;
                        }
                        "SUBSTRUCTURE" | "COMMENT" | "HEADTAIL" | "SET" | "DICT" | "EXTENSION" => {
                            // Skip these sections
                            self.skip_section()?;
                        }
                        _ => {
                            // Unknown section, skip
                            self.skip_section()?;
                        }
                    }
                }
                _ => {
                    self.read_line()?; // consume non-section line
                }
            }
        }

        // Validate counts
        if atoms.len() != num_atoms {
            return Err(SdfError::AtomCountMismatch {
                expected: num_atoms,
                found: atoms.len(),
            });
        }

        Ok(Some(Molecule {
            name,
            program_line: None,
            comment: None,
            atoms,
            bonds,
            properties,
            format_version: crate::molecule::SdfFormat::V2000,
            stereogroups: Vec::new(),
            sgroups: Vec::new(),
            collections: Vec::new(),
        }))
    }

    /// Parses the ATOM section.
    fn parse_atom_section(&mut self, expected_count: usize) -> Result<Vec<Atom>> {
        let mut atoms = Vec::with_capacity(expected_count);

        for i in 0..expected_count {
            if !self.read_line()? {
                return Err(SdfError::AtomCountMismatch {
                    expected: expected_count,
                    found: i,
                });
            }

            // Check if we hit another section
            if self.current_line.starts_with("@<TRIPOS>") {
                self.peeked = true;
                return Err(SdfError::AtomCountMismatch {
                    expected: expected_count,
                    found: i,
                });
            }

            let atom = self.parse_atom_line(i)?;
            atoms.push(atom);
        }

        Ok(atoms)
    }

    /// Parses a single atom line.
    /// Format: atom_id atom_name x y z atom_type [subst_id [subst_name [charge [status_bit]]]]
    fn parse_atom_line(&self, index: usize) -> Result<Atom> {
        let parts: Vec<&str> = self.current_line.split_whitespace().collect();

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

        // atom_id is 1-based, we use 0-based index
        // parts[0] = atom_id (ignored, we use our own index)
        // parts[1] = atom_name
        // parts[2] = x
        // parts[3] = y
        // parts[4] = z
        // parts[5] = atom_type (e.g., "C.3", "N.ar", "O.2")
        // parts[6] = subst_id (optional)
        // parts[7] = subst_name (optional)
        // parts[8] = charge (optional)

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

        // Extract element from atom_type (e.g., "C.3" -> "C", "N.ar" -> "N")
        let atom_type = parts[5];
        let element = atom_type.split('.').next().unwrap_or(atom_type).to_string();

        // Parse partial charge if present
        let partial_charge: f64 = if parts.len() > 8 {
            parts[8].parse().unwrap_or(0.0)
        } else {
            0.0
        };

        // Round partial charge to formal charge (rough approximation)
        let formal_charge = partial_charge.round() as i8;

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

    /// Parses the BOND section.
    fn parse_bond_section(
        &mut self,
        expected_count: usize,
        atom_count: usize,
    ) -> Result<Vec<Bond>> {
        let mut bonds = Vec::with_capacity(expected_count);

        for i in 0..expected_count {
            if !self.read_line()? {
                return Err(SdfError::BondCountMismatch {
                    expected: expected_count,
                    found: i,
                });
            }

            // Check if we hit another section
            if self.current_line.starts_with("@<TRIPOS>") {
                self.peeked = true;
                return Err(SdfError::BondCountMismatch {
                    expected: expected_count,
                    found: i,
                });
            }

            let bond = self.parse_bond_line(atom_count)?;
            bonds.push(bond);
        }

        Ok(bonds)
    }

    /// Parses a single bond line.
    /// Format: bond_id origin_atom_id target_atom_id bond_type [status_bits]
    fn parse_bond_line(&self, atom_count: usize) -> Result<Bond> {
        let parts: Vec<&str> = self.current_line.split_whitespace().collect();

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

        // parts[0] = bond_id (ignored)
        // parts[1] = origin_atom_id (1-based)
        // parts[2] = target_atom_id (1-based)
        // parts[3] = bond_type

        let atom1: usize = parts[1]
            .parse::<usize>()
            .map_err(|_| SdfError::Parse {
                line: self.line_number,
                message: "Invalid atom1 index".to_string(),
            })?
            .checked_sub(1)
            .ok_or(SdfError::InvalidAtomIndex {
                index: 0,
                atom_count,
            })?;

        let atom2: usize = parts[2]
            .parse::<usize>()
            .map_err(|_| SdfError::Parse {
                line: self.line_number,
                message: "Invalid atom2 index".to_string(),
            })?
            .checked_sub(1)
            .ok_or(SdfError::InvalidAtomIndex {
                index: 0,
                atom_count,
            })?;

        // Validate atom indices
        if atom1 >= atom_count {
            return Err(SdfError::InvalidAtomIndex {
                index: atom1 + 1,
                atom_count,
            });
        }
        if atom2 >= atom_count {
            return Err(SdfError::InvalidAtomIndex {
                index: atom2 + 1,
                atom_count,
            });
        }

        let bond_type = parts[3].to_lowercase();
        let order = mol2_bond_type_to_order(&bond_type);

        Ok(Bond {
            atom1,
            atom2,
            order,
            stereo: BondStereo::None,
            topology: None,
            v3000_id: None,
            reacting_center: None,
        })
    }

    /// Skips lines until the next section header.
    fn skip_section(&mut self) -> Result<()> {
        loop {
            match self.peek_line()? {
                None => return Ok(()),
                Some(line) if line.starts_with("@<TRIPOS>") => return Ok(()),
                _ => {
                    self.read_line()?;
                }
            }
        }
    }
}

/// Iterator over molecules in a MOL2 file.
pub struct Mol2Iterator<R> {
    parser: Mol2Parser<R>,
    finished: bool,
}

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

impl<R: BufRead> Iterator for Mol2Iterator<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 a MOL2 string.
pub fn parse_mol2_string(content: &str) -> Result<Molecule> {
    let cursor = std::io::Cursor::new(content);
    let reader = std::io::BufReader::new(cursor);
    let mut parser = Mol2Parser::new(reader);

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

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

    iter.collect()
}

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

/// Parses all molecules from a MOL2 file.
///
/// When the `gzip` feature is enabled, files ending in `.gz` are automatically
/// decompressed.
pub fn parse_mol2_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 = Mol2Iterator::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 = Mol2Iterator::new(reader);
        iter.collect()
    }
}

/// Returns an iterator over molecules in a MOL2 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_mol2_file<P: AsRef<std::path::Path>>(
    path: P,
) -> Result<Mol2Iterator<super::compression::MaybeGzReader>> {
    let reader = super::compression::open_maybe_gz(&path)?;
    Ok(Mol2Iterator::new(reader))
}

/// Returns an iterator over molecules in a MOL2 file.
#[cfg(not(feature = "gzip"))]
pub fn iter_mol2_file<P: AsRef<std::path::Path>>(
    path: P,
) -> Result<Mol2Iterator<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(Mol2Iterator::new(reader))
}

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

    const SIMPLE_MOL2: &str = r#"@<TRIPOS>MOLECULE
methane
 5 4 0 0 0
SMALL
NO_CHARGES

@<TRIPOS>ATOM
      1 C1          0.0000    0.0000    0.0000 C.3       1 MOL       0.0000
      2 H1          0.6289    0.6289    0.6289 H         1 MOL       0.0000
      3 H2         -0.6289   -0.6289    0.6289 H         1 MOL       0.0000
      4 H3         -0.6289    0.6289   -0.6289 H         1 MOL       0.0000
      5 H4          0.6289   -0.6289   -0.6289 H         1 MOL       0.0000
@<TRIPOS>BOND
     1     1     2 1
     2     1     3 1
     3     1     4 1
     4     1     5 1
"#;

    #[test]
    fn test_parse_simple_mol2() {
        let mol = parse_mol2_string(SIMPLE_MOL2).unwrap();

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

    #[test]
    fn test_parse_mol2_atoms() {
        let mol = parse_mol2_string(SIMPLE_MOL2).unwrap();

        // Check carbon
        assert_eq!(mol.atoms[0].element, "C");
        assert_eq!(mol.atoms[0].x, 0.0);

        // Check hydrogen
        assert_eq!(mol.atoms[1].element, "H");
    }

    #[test]
    fn test_parse_mol2_bonds() {
        let mol = parse_mol2_string(SIMPLE_MOL2).unwrap();

        // All bonds should be single
        for bond in &mol.bonds {
            assert_eq!(bond.order, BondOrder::Single);
        }

        // First bond connects C to H
        assert_eq!(mol.bonds[0].atom1, 0);
        assert_eq!(mol.bonds[0].atom2, 1);
    }

    #[test]
    fn test_parse_mol2_aromatic() {
        let benzene_mol2 = r#"@<TRIPOS>MOLECULE
benzene
 6 6 0 0 0
SMALL
NO_CHARGES

@<TRIPOS>ATOM
      1 C1          1.2124    0.7000    0.0000 C.ar      1 MOL       0.0000
      2 C2          1.2124   -0.7000    0.0000 C.ar      1 MOL       0.0000
      3 C3          0.0000   -1.4000    0.0000 C.ar      1 MOL       0.0000
      4 C4         -1.2124   -0.7000    0.0000 C.ar      1 MOL       0.0000
      5 C5         -1.2124    0.7000    0.0000 C.ar      1 MOL       0.0000
      6 C6          0.0000    1.4000    0.0000 C.ar      1 MOL       0.0000
@<TRIPOS>BOND
     1     1     2 ar
     2     2     3 ar
     3     3     4 ar
     4     4     5 ar
     5     5     6 ar
     6     6     1 ar
"#;

        let mol = parse_mol2_string(benzene_mol2).unwrap();

        assert_eq!(mol.name, "benzene");
        assert_eq!(mol.atom_count(), 6);
        assert_eq!(mol.bond_count(), 6);

        // All bonds should be aromatic
        for bond in &mol.bonds {
            assert_eq!(bond.order, BondOrder::Aromatic);
        }
    }

    #[test]
    fn test_parse_mol2_with_charges() {
        let charged_mol2 = r#"@<TRIPOS>MOLECULE
ammonium
 5 4 0 0 0
SMALL
USER_CHARGES

@<TRIPOS>ATOM
      1 N1          0.0000    0.0000    0.0000 N.4       1 MOL       1.0000
      2 H1          0.6289    0.6289    0.6289 H         1 MOL       0.0000
      3 H2         -0.6289   -0.6289    0.6289 H         1 MOL       0.0000
      4 H3         -0.6289    0.6289   -0.6289 H         1 MOL       0.0000
      5 H4          0.6289   -0.6289   -0.6289 H         1 MOL       0.0000
@<TRIPOS>BOND
     1     1     2 1
     2     1     3 1
     3     1     4 1
     4     1     5 1
"#;

        let mol = parse_mol2_string(charged_mol2).unwrap();

        assert_eq!(mol.name, "ammonium");
        assert_eq!(mol.atoms[0].formal_charge, 1);
        assert_eq!(mol.total_charge(), 1);
    }

    #[test]
    fn test_parse_multi_mol2() {
        let multi = format!("{}{}", SIMPLE_MOL2, SIMPLE_MOL2);
        let mols = parse_mol2_string_multi(&multi).unwrap();

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