Skip to content

Commit 12c59fb

Browse files
committed
feat(polychem): add modify_only_groups()
1 parent 7dda2bb commit 12c59fb

5 files changed

+121
-4
lines changed

crates/polychem/src/polymerizer/mod.rs

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
polymer_database::{BondDescription, ModificationDescription, PolymerDatabase},
1313
target::{Index, Target},
1414
},
15-
AnyMod, AnyModification, Bond, BondTarget, FunctionalGroup, GroupState, Id, NamedMod,
15+
AnyMod, AnyModification, Bond, BondTarget, Count, FunctionalGroup, GroupState, Id, NamedMod,
1616
PolychemError, Residue, Result,
1717
};
1818

@@ -79,14 +79,12 @@ impl<'a, 'p> Polymerizer<'a, 'p> {
7979
Ok(residues)
8080
}
8181

82-
// FIXME: Might want to call this `modify` and either delete or rename the other, less-useful `modify`
8382
pub fn modify(
8483
&mut self,
8584
modification: impl Into<AnyModification<'a, 'p>>,
8685
target: &mut Residue<'a, 'p>,
8786
) -> Result<()> {
8887
let modification = modification.into();
89-
// FIXME: Don't forget to be clever about the multiplier!
9088
match modification.kind {
9189
AnyMod::Named(m) => {
9290
self.modify_with_optional_groups(m.abbr(), target, modification.multiplier)
@@ -103,6 +101,15 @@ impl<'a, 'p> Polymerizer<'a, 'p> {
103101
self.modify_with_optional_groups(abbr, target, 1)
104102
}
105103

104+
pub fn modify_only_groups(
105+
&mut self,
106+
abbr: impl AsRef<str>,
107+
target: &mut Residue<'a, 'p>,
108+
number: Count,
109+
) -> Result<()> {
110+
self.modify_with_optional_groups(abbr, target, number)
111+
}
112+
106113
// PERF: Could create an `_unchecked` version for when you've already called `self.free_*_groups()` — skip straight
107114
// to `self.update_group()`!
108115
pub fn modify_group(
@@ -476,7 +483,8 @@ mod tests {
476483
use rust_decimal_macros::dec;
477484

478485
use crate::{
479-
testing_tools::assert_miette_snapshot, Massive, Modification, OffsetKind, OffsetMod,
486+
testing_tools::assert_miette_snapshot, Charged, Massive, Modification, OffsetKind,
487+
OffsetMod,
480488
};
481489

482490
use super::*;
@@ -873,6 +881,67 @@ mod tests {
873881
assert_miette_snapshot!(nonexistent_modification);
874882
}
875883

884+
#[test]
885+
fn modify_only_groups() {
886+
let mut polymerizer = Polymerizer::new(&ATOMIC_DB, &POLYMER_DB);
887+
let mut murnac = polymerizer.residue("m").unwrap();
888+
assert_eq!(murnac.monoisotopic_mass(), dec!(293.11106657336));
889+
890+
polymerizer
891+
.modify_only_groups("Met", &mut murnac, 3)
892+
.unwrap();
893+
assert_eq!(murnac.monoisotopic_mass(), dec!(335.15801676674));
894+
let reducing_end = FunctionalGroup::new("Hydroxyl", "Reducing End");
895+
let nonreducing_end = FunctionalGroup::new("Hydroxyl", "Nonreducing End");
896+
let six_position = FunctionalGroup::new("Hydroxyl", "6-Position");
897+
for hydroxyl_group in [reducing_end, nonreducing_end, six_position] {
898+
assert!(matches!(
899+
murnac.group_state(&hydroxyl_group).unwrap(),
900+
GroupState::Modified(_)
901+
));
902+
}
903+
904+
let mut murnac = polymerizer.residue("m").unwrap();
905+
assert_eq!(murnac.monoisotopic_mass(), dec!(293.11106657336));
906+
polymerizer
907+
.modify_only_groups("Ca", &mut murnac, 4)
908+
.unwrap();
909+
assert_eq!(murnac.charge(), 4);
910+
assert_eq!(murnac.monoisotopic_mass(), dec!(448.927935519603480));
911+
912+
let mut alanine = polymerizer.residue("A").unwrap();
913+
assert_eq!(alanine.monoisotopic_mass(), dec!(89.04767846918));
914+
polymerizer
915+
.modify_only_groups("Ca", &mut alanine, 2)
916+
.unwrap();
917+
assert_eq!(alanine.charge(), 2);
918+
assert_eq!(alanine.monoisotopic_mass(), dec!(166.956112942301740));
919+
920+
let mut murnac = polymerizer.residue("m").unwrap();
921+
polymerizer
922+
.modify_only_groups("Met", &mut murnac, 3)
923+
.unwrap();
924+
let all_groups_occupied = polymerizer.modify_only_groups("Ca", &mut murnac, 4);
925+
assert_miette_snapshot!(all_groups_occupied);
926+
let still_all_groups_occupied = polymerizer.modify_only_groups("Ca", &mut murnac, 2);
927+
assert_miette_snapshot!(still_all_groups_occupied);
928+
929+
assert_eq!(murnac.monoisotopic_mass(), dec!(335.15801676674));
930+
polymerizer
931+
.modify_only_groups("Ca", &mut murnac, 1)
932+
.unwrap();
933+
assert_eq!(murnac.monoisotopic_mass(), dec!(374.112234003300870));
934+
935+
// Start a new polymer by resetting the polymerizer
936+
let mut polymerizer = polymerizer.reset();
937+
let residue_not_in_polymer = polymerizer.modify_only_groups("Ca", &mut alanine, 2);
938+
assert_miette_snapshot!(residue_not_in_polymer);
939+
940+
let mut murnac = polymerizer.residue("m").unwrap();
941+
let no_matching_groups = polymerizer.modify_only_groups("Anh", &mut murnac, 2);
942+
assert_miette_snapshot!(no_matching_groups);
943+
}
944+
876945
#[test]
877946
fn bond() {
878947
let mut polymerizer = Polymerizer::new(&ATOMIC_DB, &POLYMER_DB);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: crates/polychem/src/polymerizer/mod.rs
3+
description: still_all_groups_occupied
4+
expression: out
5+
---
6+
× failed to apply the modification Calcium Adduct (Ca) to residue 4 (N-
7+
Acetylmuramic Acid)
8+
╰─▶ × too few functional groups on residue 4 matching the target were free
9+
│ (1/2): "Carboxyl" at="Lactyl Ether" is free, "Hydroxyl" at="6-
10+
Position"
11+
│ is modified, "Hydroxyl" at="Nonreducing End" is modified, and
12+
"Hydroxyl"
13+
at="Reducing End" is modified
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: crates/polychem/src/polymerizer/mod.rs
3+
description: residue_not_in_polymer
4+
expression: out
5+
---
6+
× failed to apply the modification Calcium Adduct (Ca) to residue 3
7+
│ (Alanine)
8+
╰─▶ × residue 3 does not belong to the current polymer
9+
help: the referenced residue was likely created by a different
10+
Polymerizer
11+
instance
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: crates/polychem/src/polymerizer/mod.rs
3+
description: no_matching_groups
4+
expression: out
5+
---
6+
× failed to apply the modification 1,6-Anhydro (Anh) to residue 1 (N-
7+
Acetylmuramic Acid)
8+
╰─▶ × too few functional groups on residue 1 matched the target "Hydroxyl"
9+
at="Reducing End" of="N-Acetylmuramic Acid" (1/2): "Hydroxyl"
10+
at="Reducing
11+
End"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: crates/polychem/src/polymerizer/mod.rs
3+
description: all_groups_occupied
4+
expression: out
5+
---
6+
× failed to apply the modification Calcium Adduct (Ca) to residue 4 (N-
7+
Acetylmuramic Acid)
8+
╰─▶ × too few functional groups on residue 4 matching the target were free
9+
│ (1/4): "Carboxyl" at="Lactyl Ether" is free, "Hydroxyl" at="6-
10+
Position"
11+
│ is modified, "Hydroxyl" at="Nonreducing End" is modified, and
12+
"Hydroxyl"
13+
at="Reducing End" is modified

0 commit comments

Comments
 (0)