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
//! A string parser for different chemical nomenclature. Focused on SMILES.

extern crate regex;
#[macro_use] extern crate lazy_static;

mod formula;
mod smiles;

pub mod types;

use formula::tokenize_formula;
use smiles::tokenize_smiles;
use types::Substance;

/// Tokenizes a string describing a chemical, yielding a Substance with
/// (optional) functional groups corresponding to (more) fundamental components.
///
/// "kind" can be one of two strings: "formula" or "smiles". "formula" expects a
/// simple molecular formula notation. "smiles" expects an OpenSMILES-specified
/// string. See [the tutorial at Daylight][ref] for info.
///
/// [ref]: http://www.daylight.com/dayhtml_tutorials/languages/smiles/index.html
///
/// # Examples
///
/// ## Molecular formula parsing
///
/// Here are a couple examples of what molecular formula parsing can do.
///
/// Parsing H2SO4 (sulfuric acid) without any structural annotations:
///
/// ```
/// 
/// use acetylene_parser::tokenize;
/// use acetylene_parser::types::Substance;
///
/// assert_eq!(
///   tokenize("H2SO4", "formula"),
///   Substance {
///     symbol: String::from("H2SO4"),
///     quantity: 1,
///     charge: None,
///     groups: Some(Box::new(vec![
///       Substance { symbol: String::from("H"), quantity: 2, charge: None, groups: None },
///       Substance { symbol: String::from("S"), quantity: 1, charge: None, groups: None },
///       Substance { symbol: String::from("O"), quantity: 4, charge: None, groups: None }
///     ]))
///   }
/// );
/// ```
///
/// In this case, the parser just pulls out the individual elements and tallies
/// them up.
///
/// If we annotate the structure with parentheses (denoting groups), we'll get
/// something slightly different:
///
/// 
///
/// ```
/// 
/// use acetylene_parser::tokenize;
/// use acetylene_parser::types::Substance;
///
/// assert_eq!(
///   tokenize("H2(SO4)", "formula"),
///   Substance {
///     symbol: String::from("H2(SO4)"),
///     quantity: 1,
///     charge: None,
///     groups: Some(Box::new(vec![
///       Substance { symbol: String::from("H"), quantity: 2, charge: None, groups: None },
///       Substance { symbol: String::from("(SO4)"), quantity: 1, charge: None, groups: None },
///       Substance { symbol: String::from("S"), quantity: 1, charge: None, groups: None },
///       Substance { symbol: String::from("O"), quantity: 4, charge: None, groups: None },
///     ]))
///   }
/// );
/// ```
///
/// This time we can explicitly parse out both dihydrogen (H2) and sulfate
/// (SO4). Note that charges are not inferred.
///
/// There's not much error checking going on in either case--only regex for now.
///
/// ## SMILES parsing
///
/// SMILES parsing has access to much more elaborate structural information. For
/// example, ring closures are annotations that allow a ring of molecules to be
/// unrolled into a 1-dimensional string, by marking the two breaks with a
/// number following the atoms that need to reconnect.
///
/// For example, benzene looks like this:
///
/// ```
/// 
/// use acetylene_parser::tokenize;
/// use acetylene_parser::types::Substance;
///
/// let test_str = r"c1ccccc1";
/// let res = tokenize(test_str, "smiles");
/// 
/// let sub = Substance {
///   symbol: String::from("c1ccccc1"),
///   quantity: 1,
///   charge: None,
///   groups: Some(Box::new(vec![
///     Substance {
///       symbol: String::from("c1ccccc1"),
///       quantity: 1,
///       charge: None,
///       groups: None
///     },
///     Substance { symbol: String::from("c"), quantity: 1, charge: None, groups: None },
///     Substance { symbol: String::from("c"), quantity: 1, charge: None, groups: None },
///     Substance { symbol: String::from("c"), quantity: 1, charge: None, groups: None },
///     Substance { symbol: String::from("c"), quantity: 1, charge: None, groups: None },
///     Substance { symbol: String::from("c"), quantity: 1, charge: None, groups: None },
///     Substance { symbol: String::from("c"), quantity: 1, charge: None, groups: None }      
///   ]))
/// };
///
/// assert_eq!(res, sub);
/// ```
///
/// The first functional group listed is identical to the "whole substance"
/// described with the input string, which makes sense--the whole string is
/// composed of one big ring closure, and so the parser detects that and rolls
/// it all up.
///
/// Note that the individual atoms in the ring aren't collected up into a single
/// molecular formula. I'm still working out how I want to go about it.
///
/// Here's one last example with hydronium. Hydronium is an ion, so it's
/// required to be in brackets.
/// 
/// ```
/// 
/// use acetylene_parser::tokenize;
/// use acetylene_parser::types::Substance;
///
/// let test_str = r"[OH3+]";
/// let res = tokenize(test_str, "smiles");
///
/// let sub = Substance {
///   symbol: String::from("[OH3+]"),
///   quantity: 1,
///   charge: Some(1),
///   groups: Some(Box::new(vec![
///     Substance {
///       symbol: String::from("[OH3+]"),
///       quantity: 1,
///       charge: Some(1),
///       groups: None
///     }
///   ]))
/// };
///
/// assert_eq!(res, sub);
/// ```
///
pub fn tokenize(input: &str, kind: &str) -> Substance {
  match kind {
    "formula" => tokenize_formula(input),
    "smiles" => tokenize_smiles(input),
//    "iupac" => tokenize_iupac(input),
    _ => tokenize_formula(input)
  }
}