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
//! Code to handle the PSI-MOD ontology
use std::borrow::Cow;
use context_error::{BoxedError, CreateError, FullErrorContent, StaticErrorContent};
use mzcv::{
CVData, CVError, CVFile, CVSource, CVVersion, HashBufReader, OboOntology, OboStanzaType,
SynonymScope,
};
use crate::{
chemistry::MolecularFormula,
ontology::{
Ontology,
ontology_modification::{ModData, OntologyModification},
},
sequence::{
ModificationId, PlacementRule, Position, SimpleModification, SimpleModificationInner,
},
};
/// PSI-MOD modifications
#[allow(missing_copy_implementations, missing_debug_implementations)]
pub struct PsiMod {}
impl CVData for SimpleModificationInner {
type Index = u32;
fn index(&self) -> Option<u32> {
self.description().and_then(ModificationId::id)
}
fn name(&self) -> Option<Cow<'_, str>> {
self.description().map(|d| Cow::Borrowed(d.name.as_ref()))
}
fn synonyms(&self) -> impl Iterator<Item = &str> {
self.description().into_iter().flat_map(|d| {
d.synonyms
.iter()
.filter(|(s, _)| *s == SynonymScope::Exact)
.map(|(_, s)| s.as_ref())
})
}
}
impl CVSource for PsiMod {
type Data = SimpleModificationInner;
type Structure = Vec<SimpleModification>;
fn cv_name() -> &'static str {
"PSI-MOD"
}
fn files() -> &'static [CVFile] {
&[CVFile {
name: "PSI-MOD",
extension: "obo",
url: Some(
"https://raw.githubusercontent.com/HUPO-PSI/psi-mod-CV/refs/heads/master/PSI-MOD.obo",
),
compression: mzcv::CVCompression::None,
}]
}
fn static_data() -> Option<(CVVersion, Self::Structure)> {
#[cfg(not(feature = "internal-no-data"))]
{
use bincode::config::Configuration;
let cache = bincode::decode_from_slice::<(CVVersion, Self::Structure), Configuration>(
include_bytes!("../databases/psimod.dat"),
Configuration::default(),
)
.unwrap()
.0;
Some(cache)
}
#[cfg(feature = "internal-no-data")]
None
}
fn parse(
mut reader: impl Iterator<Item = HashBufReader<Box<dyn std::io::Read>, impl sha2::Digest>>,
) -> Result<(CVVersion, Self::Structure), Vec<BoxedError<'static, CVError>>> {
let reader = reader.next().unwrap();
OboOntology::from_raw(reader)
.map_err(|e| {
vec![
BoxedError::small(
CVError::FileCouldNotBeParsed,
e.get_short_description(),
e.get_long_description(),
)
.add_contexts(e.get_contexts().iter().cloned()),
]
})
.map(|obo| {
(obo.version(), {
let mut mods: Vec<SimpleModification> = Vec::new();
for obj in obo.objects {
if obj.stanza_type != OboStanzaType::Term
|| obj.id == (Some("MOD".into()), "00000".into())
|| obj.id == (Some("MOD".into()), "00004".into())
|| obj.id == (Some("MOD".into()), "00008".into())
{
continue;
}
let mut modification = OntologyModification {
id: obj
.id
.1
.parse()
.expect("Incorrect psi mod id, should be numerical"),
name: obj.lines["name"][0].0.clone(),
ontology: Ontology::Psimod,
obsolete: obj.obsolete,
..OntologyModification::default()
};
if let Some((description, cross_ids, _, _)) = obj.definition {
modification.description = description;
modification.cross_ids = cross_ids.into();
}
for synonym in obj.synonyms {
modification
.synonyms
.push((synonym.scope, synonym.synonym.clone())); // TODO: retain the scope
}
let mut rules = Vec::new();
let mut origins = Vec::new();
let mut term = None;
for (id, values) in obj.property_values {
for (value, _, _) in values {
match (id.as_ref(), value) {
("DiffFormula", mzcv::OboValue::String(s)) => {
modification.formula =
MolecularFormula::psi_mod(&s).unwrap();
}
("Origin", mzcv::OboValue::String(value)) => {
origins = value
.split(',')
.map(|s| s.trim().to_string())
.collect();
}
("TermSpec", mzcv::OboValue::String(value)) => {
if value == "N-term" {
term = Some(Position::AnyNTerm);
} else if value == "C-term" {
term = Some(Position::AnyCTerm);
} else {
panic!("Invalid TermSpec: {value}");
}
}
_ => (), // ignore
}
}
}
// If the list of possible origins contains "X" than the mod can be placed on any aminoacid
// But if there is a TermSpec definition that should still be accounted for
let all_aminoacids = origins.contains(&"X".to_string());
if !all_aminoacids {
for origin in &origins {
if origin.len() == 1 {
rules.push((
vec![PlacementRule::AminoAcid(
vec![origin.try_into().unwrap()].into(),
term.unwrap_or(Position::Anywhere),
)],
Vec::new(),
Vec::new(),
));
} else {
rules.push((
vec![PlacementRule::PsiModification(
origin
.split_once(':')
.expect(
"Incorrect psi mod id, should contain a colon",
)
.1
.parse()
.expect(
"Incorrect psi mod id, should be numerical",
),
term.unwrap_or(Position::Anywhere),
)],
Vec::new(),
Vec::new(),
));
}
}
}
if (origins.is_empty() || all_aminoacids)
&& let Some(term) = term
{
rules.push((
vec![PlacementRule::Terminal(term)],
Vec::new(),
Vec::new(),
));
}
modification.data = ModData::Mod {
specificities: rules,
};
mods.push(modification.into());
}
mods
})
})
}
}