Skip to content

Commit

Permalink
Used trait IsAminoAcid
Browse files Browse the repository at this point in the history
  • Loading branch information
douweschulte committed Feb 10, 2025
1 parent ba6dad7 commit 2cf2767
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 223 deletions.
14 changes: 11 additions & 3 deletions rustyms-generate-imgt/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;

use crate::imgt_gene::IMGTGene;
use crate::shared::{AnnotatedSequence, Gene, Species};
use rustyms::AminoAcid;
use rustyms::{AminoAcid, IsAminoAcid};

#[derive(Debug)]
pub struct DataItem {
Expand Down Expand Up @@ -57,7 +57,12 @@ impl Display for Region {
// self.found_seq.0,
self.found_seq
.as_ref()
.map(|seq| seq.1 .0.iter().map(|a| a.char()).collect::<String>())
.map(|seq| seq
.1
.0
.iter()
.map(|a| a.pro_forma_definition())
.collect::<String>())
.unwrap_or_else(|e| format!("<NO SEQ!>: {e}")),
)
}
Expand Down Expand Up @@ -237,7 +242,10 @@ impl std::fmt::Debug for AASequence {
write!(
f,
"[{}]",
self.0.iter().map(|a| a.char()).collect::<String>()
self.0
.iter()
.map(|a| a.pro_forma_definition())
.collect::<String>()
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions rustyms-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::num::NonZeroU16;
use ordered_float::OrderedFloat;
use pyo3::{exceptions::PyValueError, prelude::*, types::PyType};

use rustyms::{AnnotatableSpectrum, Chemical, Linked, MultiChemical};
use rustyms::{AnnotatableSpectrum, Chemical, IsAminoAcid, Linked, MultiChemical};

/// Mass mode enum.
#[pyclass(eq, eq_int)]
Expand Down Expand Up @@ -440,7 +440,7 @@ impl AminoAcid {
}

fn __str__(&self) -> String {
self.0.char().to_string()
self.0.pro_forma_definition().to_string()
}

fn __repr__(&self) -> String {
Expand Down Expand Up @@ -1124,7 +1124,7 @@ impl Peptidoform {
self.0
.sequence()
.iter()
.map(|x| x.aminoacid.char())
.map(|x| x.aminoacid.pro_forma_definition())
.collect()
}

Expand Down
2 changes: 1 addition & 1 deletion rustyms/src/align/multi_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<Complexity> MultiAlignmentLine<'_, Complexity> {
{
print!(
"{}{}",
piece.1.aminoacid.char(),
piece.1.aminoacid,
"·".repeat(piece.0.step as usize - 1)
);
}
Expand Down
491 changes: 295 additions & 196 deletions rustyms/src/aminoacids.rs

Large diffs are not rendered by default.

51 changes: 38 additions & 13 deletions rustyms/src/checked_aminoacid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::marker::PhantomData;
use serde::{Deserialize, Serialize};

use crate::{
AminoAcid, Chemical, MolecularFormula, Multi, MultiChemical, SemiAmbiguous, UnAmbiguous,
aminoacids::IsAminoAcid, AminoAcid, Chemical, MolecularFormula, Multi, MultiChemical,
SemiAmbiguous, UnAmbiguous,
};

/// A checked amino acid. This wraps an [`AminoAcid`] to keep track of the maximal complexity of
Expand Down Expand Up @@ -279,24 +280,48 @@ impl<T> CheckedAminoAcid<T> {
self.aminoacid.canonical_identical(rhs.aminoacid)
}

/// Get the description of the amino acid as a single character
pub const fn char(self) -> char {
self.aminoacid.char()
/// Get the underlying (unchecked) amino acid
pub const fn aminoacid(self) -> AminoAcid {
self.aminoacid
}
}

/// Get the 3 letter code for the amino acid
pub const fn code(self) -> &'static str {
self.aminoacid.code()
impl<T> IsAminoAcid for CheckedAminoAcid<T> {
fn name(&self) -> std::borrow::Cow<'_, str> {
self.aminoacid.name()
}

/// Get the full name of the amino acid
pub const fn name(self) -> &'static str {
self.aminoacid.name()
fn three_letter_code(&self) -> Option<std::borrow::Cow<'_, str>> {
self.aminoacid.three_letter_code()
}

/// Get the underlying (unchecked) amino acid
pub const fn aminoacid(self) -> AminoAcid {
fn one_letter_code(&self) -> Option<char> {
self.aminoacid.one_letter_code()
}

fn pro_forma_definition(&self) -> std::borrow::Cow<'_, str> {
self.aminoacid.pro_forma_definition()
}

fn immonium_losses(&self) -> std::borrow::Cow<'_, [crate::NeutralLoss]> {
self.aminoacid.immonium_losses()
}

fn satellite_ion_fragments(
&self,
sequence_index: crate::SequencePosition,
peptidoform_index: usize,
) -> Option<std::borrow::Cow<'_, Multi<MolecularFormula>>> {
self.aminoacid
.satellite_ion_fragments(sequence_index, peptidoform_index)
}

fn side_chain(
&self,
sequence_index: crate::SequencePosition,
peptidoform_index: usize,
) -> std::borrow::Cow<'_, Multi<MolecularFormula>> {
self.aminoacid.side_chain(sequence_index, peptidoform_index)
}
}

Expand Down Expand Up @@ -398,7 +423,7 @@ impl<T> Default for CheckedAminoAcid<T> {

impl<T> std::fmt::Display for CheckedAminoAcid<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.char())
write!(f, "{}", self.pro_forma_definition())
}
}

Expand Down
8 changes: 3 additions & 5 deletions rustyms/src/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,18 +481,16 @@ impl FragmentType {
Self::(_) => Cow::Borrowed("z·"),
Self::B(_) => Cow::Borrowed("B"),
Self::Y(_) | Self::YComposition(_, _) => Cow::Borrowed("Y"),
Self::Diagnostic(DiagnosticPosition::Peptide(_, aa)) => {
Cow::Owned(format!("d{}", aa.char()))
}
Self::Diagnostic(DiagnosticPosition::Peptide(_, aa)) => Cow::Owned(format!("d{aa}")),
Self::Diagnostic(DiagnosticPosition::Reporter) => Cow::Borrowed("r"),
Self::Diagnostic(DiagnosticPosition::Labile(m)) => Cow::Owned(format!("d{m}")),
Self::Diagnostic(
DiagnosticPosition::Glycan(_, sug)
| DiagnosticPosition::GlycanCompositional(sug, _),
) => Cow::Owned(format!("d{sug}")),
Self::Oxonium(_) | Self::OxoniumComposition(_, _) => Cow::Borrowed("oxonium"),
Self::Immonium(_, aa) => Cow::Owned(format!("i{}", aa.aminoacid.char())),
Self::PrecursorSideChainLoss(_, aa) => Cow::Owned(format!("p-s{}", aa.char())),
Self::Immonium(_, aa) => Cow::Owned(format!("i{}", aa.aminoacid)),
Self::PrecursorSideChainLoss(_, aa) => Cow::Owned(format!("p-s{aa}")),
Self::Precursor => Cow::Borrowed("p"),
Self::Internal(fragmentation, _, _) => Cow::Owned(format!(
"m{}",
Expand Down
2 changes: 1 addition & 1 deletion rustyms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub use crate::sequence_element::SequenceElement;
pub use crate::sequence_position::*;
pub use crate::spectrum::{AnnotatableSpectrum, AnnotatedSpectrum, RawSpectrum};
pub use crate::tolerance::*;
pub use aminoacids::AminoAcid;
pub use aminoacids::{AminoAcid, IsAminoAcid};
pub use checked_aminoacid::CheckedAminoAcid;
pub use fragment::Fragment;
pub use peptidoform::{CompoundPeptidoformIon, Peptidoform, PeptidoformIon};
Expand Down
2 changes: 1 addition & 1 deletion rustyms/src/sequence_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<T> SequenceElement<T> {
if self.ambiguous.is_some() && last_ambiguous != self.ambiguous {
write!(f, "(?")?;
}
write!(f, "{}", self.aminoacid.char())?;
write!(f, "{}", self.aminoacid)?;
for m in &self.modifications {
let mut display_ambiguous = false;
if let Modification::Ambiguous { id, .. } = m {
Expand Down

0 comments on commit 2cf2767

Please sign in to comment.