Skip to content

Commit

Permalink
Add nitrile group (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
pumken authored Mar 21, 2023
1 parent 047033b commit 6bb3e56
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::chain;
use crate::chain::{endpoint_head_chains, longest_chain};
use crate::groups::InvalidGraphError::{Other, UnrecognizedGroup};
use crate::molecule::Group::{
Alkene, Alkyne, Bromo, Carbonyl, Carboxyl, Chloro, Fluoro, Hydroxyl, Iodo,
Alkene, Alkyne, Bromo, Carbonyl, Carboxyl, Chloro, Fluoro, Hydroxyl, Iodo, Nitrile,
};
use crate::molecule::{Atom, BondOrder, Branch, Element, Group, GroupNode, Substituent};
use crate::pointer::Pointer;
Expand Down Expand Up @@ -108,6 +108,7 @@ fn identify_single_bond_group(node: GroupNode) -> Fallible<Group> {
"2O" => Carbonyl,
"2C" => Alkene,
"3C" => Alkyne,
"3N" => Nitrile,
_ => return Err(UnrecognizedGroup),
};

Expand Down
36 changes: 21 additions & 15 deletions src/molecule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::str::FromStr;
/// Represents a type of functional group on a molecule.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Group {
/* Chain groups */
Alkane,
Alkene,
Alkyne,
Expand All @@ -32,6 +33,8 @@ pub enum Group {
/* Phenyl later */
Ester,
Ether,
/* Nitrogen groups */
Nitrile,
}

impl Group {
Expand All @@ -47,9 +50,10 @@ impl Group {
Group::Bromo | Group::Chloro | Group::Fluoro | Group::Iodo => return None,
Group::Hydroxyl => 3,
Group::Carbonyl => 4,
Group::Carboxyl => 6,
Group::Ester => 5,
Group::Carboxyl => 7,
Group::Ester => 6,
Group::Ether => return None,
Group::Nitrile => 5,
};
Some(priority)
}
Expand All @@ -70,11 +74,12 @@ impl Display for Group {
Group::Chloro => "chloro",
Group::Fluoro => "fluoro",
Group::Iodo => "iodo",
Group::Hydroxyl => "hydroxyl",
Group::Hydroxyl => "hydroxy",
Group::Carbonyl => "oxo",
Group::Carboxyl => "carboxyl",
Group::Carboxyl => "carboxy",
Group::Ester => "ester",
Group::Ether => "ether",
Group::Nitrile => "cyano",
};
write!(f, "{str}")
}
Expand Down Expand Up @@ -104,11 +109,12 @@ impl FromStr for Group {
"chloro" => Group::Chloro,
"fluoro" => Group::Fluoro,
"iodo" => Group::Iodo,
"hydroxyl" => Group::Hydroxyl,
"hydroxy" => Group::Hydroxyl,
"oxo" => Group::Carbonyl,
"carboxyl" => Group::Carboxyl,
"carboxy" => Group::Carboxyl,
"ester" => Group::Ester,
"ether" => Group::Ether,
"cyano" => Group::Nitrile,
_ => return Err(()),
};
Ok(out)
Expand Down Expand Up @@ -488,9 +494,9 @@ mod tests {
fn group_to_string() {
let groups = vec![Group::Carboxyl, Carbonyl, Hydroxyl];

assert_eq!(groups[0].to_string(), "carboxyl");
assert_eq!(groups[0].to_string(), "carboxy");
assert_eq!(groups[1].to_string(), "oxo");
assert_eq!(groups[2].to_string(), "hydroxyl");
assert_eq!(groups[2].to_string(), "hydroxy");
}

#[test]
Expand Down Expand Up @@ -525,15 +531,15 @@ mod tests {
let a = Substituent::Group(Hydroxyl);
let b = Substituent::Group(Bromo);

assert_eq!(a.to_string(), "hydroxyl".to_string());
assert_eq!(a.to_string(), "hydroxy".to_string());
assert_eq!(b.to_string(), "bromo".to_string())
}

#[test]
fn branch_reversed_correctly_reverses_chain() {
let branch = Branch::from_str("0: hydroxyl, bromo, alkene; 1: chloro").unwrap();
let branch = Branch::from_str("0: hydroxy, bromo, alkene; 1: chloro").unwrap();
let reversed = branch.reversed();
let expected = Branch::from_str("0: chloro, alkene; 1: hydroxyl, bromo").unwrap();
let expected = Branch::from_str("0: chloro, alkene; 1: hydroxy, bromo").unwrap();

assert_eq!(reversed, expected)
}
Expand All @@ -557,22 +563,22 @@ mod tests {
],
parent_alpha: None,
};
let expected = Branch::from_str("0: hydroxyl, oxo; 1: bromo").unwrap();
let expected = Branch::from_str("0: hydroxy, oxo; 1: bromo").unwrap();

assert_eq!(branch, expected);
}

#[test]
fn branch_from_string_parses_str() {
let branch = Branch::from_str("0: hydroxyl").unwrap();
let branch = Branch::from_str("0: hydroxy").unwrap();
let expected = vec![vec![Substituent::Group(Hydroxyl)]];

assert_eq!(branch.groups, expected);
}

#[test]
fn branch_from_string_multiple_groups() {
let branch = Branch::from_str("0: hydroxyl; 1: hydroxyl").unwrap();
let branch = Branch::from_str("0: hydroxy; 1: hydroxy").unwrap();
let expected = vec![
vec![Substituent::Group(Hydroxyl)],
vec![Substituent::Group(Hydroxyl)],
Expand All @@ -583,7 +589,7 @@ mod tests {

#[test]
fn branch_from_string_multiple_compound_groups() {
let branch = Branch::from_str("0: hydroxyl, oxo; 1: hydroxyl, oxo").unwrap();
let branch = Branch::from_str("0: hydroxy, oxo; 1: hydroxy, oxo").unwrap();
let expected = vec![
vec![Substituent::Group(Hydroxyl), Substituent::Group(Carbonyl)],
vec![Substituent::Group(Hydroxyl), Substituent::Group(Carbonyl)],
Expand Down
7 changes: 4 additions & 3 deletions src/naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ fn suffix(fragment: SubFragment) -> Result<String, NamingError> {
Group::Carbonyl if fragment.locants == vec![0] => return Ok("al".to_string()),
Group::Carbonyl => "one",
Group::Hydroxyl => "ol",
Group::Nitrile => "onitrile",
_ => return Ok("e".to_string()),
};

Expand Down Expand Up @@ -295,17 +296,17 @@ mod tests {
#[test]
fn prefix_creates_correct_strings() {
let branch =
Branch::from_str("0: bromo, iodo; 1: oxo, hydroxyl; 2: bromo, hydroxyl").unwrap();
Branch::from_str("0: bromo, iodo; 1: oxo, hydroxy; 2: bromo, hydroxy").unwrap();
let collection = GroupCollection::new(branch);
let str = prefix(collection.secondary_group_fragments()).unwrap();

assert_eq!(str, "1,3-dibromo-2,3-dihydroxyl-1-iodo")
assert_eq!(str, "1,3-dibromo-2,3-dihydroxy-1-iodo")
}

#[test]
fn collect_groups_aggregates_correctly() {
let branch =
Branch::from_str("0: bromo, iodo; 1: oxo, hydroxyl; 2: bromo, hydroxyl").unwrap();
Branch::from_str("0: bromo, iodo; 1: oxo, hydroxy; 2: bromo, hydroxy").unwrap();
let groups = GroupCollection::new(branch);
let expected = vec![
SubFragment::new(vec![0, 2], Substituent::Group(Bromo)),
Expand Down

0 comments on commit 6bb3e56

Please sign in to comment.