This repository was archived by the owner on Sep 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcore.rs
More file actions
303 lines (275 loc) · 9.94 KB
/
core.rs
File metadata and controls
303 lines (275 loc) · 9.94 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#[rustfmt::skip]
// The structure follows the input data as descibed in the
// https://github.com/sdsc-ordes/catplus-ontology see here for the expected Synth input data:
// https://github.com/sdsc-ordes/catplus-ontology/tree/96091fd2e75e03de8a4c4d66ad502b2db27998bd/json-file/1-Synth
use crate::{
graph::{
insert_into::{InsertIntoGraph, Link},
namespaces::{allores, cat, cat_resource, obo, purl, qudt, schema},
},
models::enums::Unit,
graph::utils::hash_identifier,
};
use anyhow;
use serde::{Deserialize, Serialize};
use sophia::{
api::{ns::rdf, prelude::*},
inmem::graph::LightGraph,
};
use sophia_api::term::{SimpleTerm, Term};
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Plate {
#[serde(rename = "containerID")]
pub container_id: String,
pub container_barcode: Option<String>,
}
impl InsertIntoGraph for Plate {
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
for (prop, value) in [
(rdf::type_, &cat::Plate.as_simple() as &dyn InsertIntoGraph),
(cat::containerID, &self.container_id.as_simple() as &dyn InsertIntoGraph),
(
cat::containerBarcode,
&self.container_barcode.as_ref().clone().map(|s| s.as_simple()),
),
] {
value.attach_into(
graph,
Link { source_iri: iri.clone(), pred: prop.as_simple(), target_iri: None },
)?;
}
Ok(())
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Observation {
pub value: f64,
pub unit: Unit,
pub error_margin: Option<ErrorMargin>,
}
/// Implementation for concrete [Observation].
impl InsertIntoGraph for Observation {
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
for (prop, value) in [
(rdf::type_, &cat::Observation.as_simple() as &dyn InsertIntoGraph),
(qudt::unit, &self.unit.iri().as_simple() as &dyn InsertIntoGraph),
(qudt::value, &self.value.as_simple()),
(cat::errorMargin, &self.error_margin),
] {
value.attach_into(
graph,
Link { source_iri: iri.clone(), pred: prop.as_simple(), target_iri: None },
)?;
}
Ok(())
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ErrorMargin {
pub value: f64,
pub unit: Unit,
}
impl InsertIntoGraph for ErrorMargin {
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
for (prop, value) in [
(rdf::type_, &cat::errorMargin.as_simple() as &dyn InsertIntoGraph),
(qudt::unit, &self.unit.iri().as_simple() as &dyn InsertIntoGraph),
(qudt::value, &self.value.as_simple()),
] {
value.attach_into(
graph,
Link { source_iri: iri.clone(), pred: prop.as_simple(), target_iri: None },
)?;
}
Ok(())
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Chemical {
#[serde(rename = "chemicalID")]
pub chemical_id: String,
pub chemical_name: String,
#[serde(rename = "CASNumber")]
pub cas_number: Option<String>,
pub molecular_mass: Observation,
pub smiles: String,
pub swiss_cat_number: Option<String>,
#[serde(rename = "Inchi")]
pub inchi: String,
pub keywords: Option<String>,
pub molecular_formula: String,
pub density: Option<Observation>,
}
impl InsertIntoGraph for Chemical {
fn get_uri(&self) -> SimpleTerm<'static> {
// build URI based on self.inchi
let mut uri = cat_resource::ns.clone().as_str().to_owned();
uri.push_str(&hash_identifier(&self.inchi));
IriRef::new_unchecked(uri).try_into_term().unwrap()
}
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
for (prop, value) in [
(rdf::type_, &obo::CHEBI_25367.as_simple() as &dyn InsertIntoGraph),
(purl::identifier, &self.chemical_id.as_simple()),
(allores::AFR_0002292, &self.chemical_name.as_simple()),
(allores::AFR_0001952, &self.molecular_formula.as_simple()),
(allores::AFR_0002295, &self.smiles.as_simple()),
(allores::AFR_0002294, &self.molecular_mass),
(allores::AFR_0002296, &self.inchi.as_simple()),
(cat::casNumber, &self.cas_number.as_ref().clone().map(|s| s.as_simple())),
(cat::swissCatNumber, &self.swiss_cat_number.as_ref().clone().map(|s| s.as_simple())),
(schema::keywords, &self.keywords.as_ref().clone().map(|s| s.as_simple())),
(obo::PATO_0001019, &self.density),
] {
value.attach_into(
graph,
Link { source_iri: iri.clone(), pred: prop.as_simple(), target_iri: None },
)?;
}
Ok(())
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PeakList {
pub peak: Vec<Peak>,
}
impl InsertIntoGraph for PeakList {
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
for (pred, value) in [
(rdf::type_, &cat::PeakList.as_simple() as &dyn InsertIntoGraph),
(cat::peak, &self.peak),
] {
value.attach_into(
graph,
Link { source_iri: iri.clone(), pred: pred.as_simple(), target_iri: None },
)?;
}
let product = cat::Product.as_simple();
let rdf_type = rdf::type_.as_simple();
let product_object = graph.triples().filter_map(Result::ok).find_map(|[s, p, o]| {
if *p == rdf_type && *o == product {
Some(s.clone())
} else {
None
}
});
if let Some(subject) = product_object {
graph.insert(iri.clone(), cat::hasProduct.as_simple(), subject)?;
}
Ok(())
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Peak {
#[serde(rename = "@index")]
pub index: i64,
#[serde(rename = "identifier")]
pub peak_identifier: String,
#[serde(rename = "peak area")]
pub peak_area: Measurement,
#[serde(rename = "retention time")]
pub retention_time: Measurement,
#[serde(rename = "peak start")]
pub peak_start: Measurement,
#[serde(rename = "peak end")]
pub peak_end: Measurement,
#[serde(rename = "peak height")]
pub peak_height: Measurement,
#[serde(rename = "relative peak area")]
pub relative_peak_area: Measurement,
#[serde(rename = "relative peak height")]
pub relative_peak_height: Measurement,
#[serde(rename = "peak value at start")]
pub peak_value_at_start: Measurement,
#[serde(rename = "peak value at end")]
pub peak_value_at_end: Measurement,
}
impl InsertIntoGraph for Peak {
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
for (pred, value) in [
(rdf::type_, &allores::AFR_0000413.as_simple() as &dyn InsertIntoGraph),
(allores::AFR_0001164, &self.peak_identifier.as_simple()),
(allores::AFR_0001073, &self.peak_area),
(allores::AFR_0001089, &self.retention_time),
(allores::AFR_0001178, &self.peak_start),
(allores::AFR_0001180, &self.peak_end),
(allores::AFR_0000948, &self.peak_height),
(allores::AFR_0001165, &self.relative_peak_area),
(allores::AFR_0000949, &self.relative_peak_height),
(allores::AFR_0001179, &self.peak_value_at_start),
(allores::AFR_0001181, &self.peak_value_at_end),
] {
value.attach_into(
graph,
Link { source_iri: iri.clone(), pred: pred.as_simple(), target_iri: None },
)?;
}
Ok(())
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Measurement {
pub value: f64,
pub unit: Unit,
}
impl InsertIntoGraph for Measurement {
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
for (prop, value) in [
(rdf::type_, &cat::Measurement.as_simple() as &dyn InsertIntoGraph),
(qudt::unit, &self.unit.iri().as_simple() as &dyn InsertIntoGraph),
(qudt::value, &self.value.as_simple()),
] {
value.attach_into(
graph,
Link { source_iri: iri.clone(), pred: prop.as_simple(), target_iri: None },
)?;
}
Ok(())
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Well {
#[serde(flatten)]
pub has_plate: Plate,
pub position: String,
}
impl InsertIntoGraph for Well {
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
for (pred, value) in [
(rdf::type_, &cat::Well.as_simple() as &dyn InsertIntoGraph),
(cat::hasPlate, &self.has_plate),
(allores::AFR_0002240, &self.position.as_simple()),
] {
value.attach_into(
graph,
Link { source_iri: iri.clone(), pred: pred.as_simple(), target_iri: None },
)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use sophia::iri::IriRef;
use sophia_api::term::Term;
use crate::{
graph::{graph_builder::GraphBuilder, insert_into::InsertIntoGraph},
models::{ErrorMargin, Observation},
};
#[test]
fn test_observation_to_triples() -> anyhow::Result<()> {
let observation = Observation {
value: 42.0,
unit: Unit::DegC,
error_margin: Some(ErrorMargin { value: 0.5, unit: Unit::DegC }),
};
let mut b = GraphBuilder::new();
let i = IriRef::new_unchecked("http://test.com/my-observation");
observation.insert_into(&mut b.graph, i.as_simple())?;
Ok(())
}
}