sbol 0.2.0

Rust implementation of the SBOL 3.1.0 specification.
Documentation
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use std::collections::BTreeMap;
use std::path::Path;

use crate::client::{
    Activity, Agent, Association, Attachment, BinaryPrefix, Collection, CombinatorialDerivation,
    Component, ComponentReference, CompoundUnit, Constraint, Cut, EntireSequence, Experiment,
    ExperimentalData, ExternallyDefined, IdentifiedExtension, Implementation, Interaction,
    Interface, LocalSubComponent, Measure, Model, Participation, Plan, Prefix, PrefixedUnit, Range,
    SIPrefix, SbolObject, Sequence, SequenceFeature, SingularUnit, SubComponent, TryFromObject,
    Unit, UnitDivision, UnitExponentiation, UnitMultiplication, Usage, VariableFeature,
};
use crate::error::{ReadError, WriteError};
use crate::object::collect_objects;
use crate::upgrade::{UpgradeError, UpgradeOptions, UpgradeReport, sbol2_to_sbol3};
use crate::validation::{ValidationContext, ValidationOptions, ValidationReport, Validator};
use crate::{Object, RdfFormat, RdfGraph, Resource};

/// An SBOL document parsed from RDF.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Document {
    graph: RdfGraph,
    objects: BTreeMap<Resource, Object>,
    typed: Vec<SbolObject>,
}

macro_rules! typed_doc_iter {
    ($method:ident, $variant:ident, $ty:ty) => {
        pub fn $method(&self) -> impl Iterator<Item = &$ty> {
            self.typed.iter().filter_map(|o| match o {
                SbolObject::$variant(v) => Some(v),
                _ => None,
            })
        }
    };
}

impl Document {
    /// Parses an SBOL document from an in-memory RDF serialization.
    pub fn read(input: &str, format: RdfFormat) -> Result<Self, ReadError> {
        let graph = RdfGraph::parse(input, format).map_err(ReadError::Rdf)?;
        Ok(Self::from_rdf_graph(graph))
    }

    /// Parses an SBOL document from a file. The format is inferred from the
    /// path's extension (`.ttl`, `.rdf`, `.jsonld`, `.nt`). Returns
    /// [`ReadError::UnknownFormat`] for any other extension.
    pub fn read_path(path: impl AsRef<Path>) -> Result<Self, ReadError> {
        let path = path.as_ref();
        let format = RdfFormat::from_path(path).ok_or_else(|| ReadError::UnknownFormat {
            path: path.to_path_buf(),
            extension: path
                .extension()
                .and_then(|ext| ext.to_str())
                .map(str::to_owned),
        })?;
        let input = std::fs::read_to_string(path).map_err(|source| ReadError::Io {
            path: path.to_path_buf(),
            source,
        })?;
        Self::read(&input, format)
    }

    /// Reads a Turtle serialization into an SBOL document.
    pub fn read_turtle(input: &str) -> Result<Self, ReadError> {
        Self::read(input, RdfFormat::Turtle)
    }

    /// Upgrades an SBOL 2 RDF document to SBOL 3 and returns the resulting
    /// [`Document`] alongside an [`UpgradeReport`] of any non-fatal issues
    /// encountered during conversion.
    ///
    /// The returned [`Document`] is always produced when the input parses as
    /// valid SBOL 2 RDF — call [`Document::check`] if you want a strict
    /// pipeline that rejects content the upgrade could not coerce into
    /// fully-conformant SBOL 3.
    pub fn upgrade_from_sbol2(
        input: &str,
        format: RdfFormat,
    ) -> Result<(Self, UpgradeReport), UpgradeError> {
        Self::upgrade_from_sbol2_with(input, format, UpgradeOptions::default())
    }

    /// Like [`Document::upgrade_from_sbol2`], with explicit
    /// [`UpgradeOptions`].
    pub fn upgrade_from_sbol2_with(
        input: &str,
        format: RdfFormat,
        options: UpgradeOptions,
    ) -> Result<(Self, UpgradeReport), UpgradeError> {
        let parsed = RdfGraph::parse(input, format).map_err(UpgradeError::Parse)?;
        let (upgraded, report) = sbol2_to_sbol3(&parsed, options)?;
        Ok((Self::from_rdf_graph(upgraded), report))
    }

    /// Reads an SBOL 2 RDF file from disk and upgrades it to SBOL 3. The
    /// format is inferred from the path's extension (`.ttl`, `.rdf`, `.xml`,
    /// `.jsonld`, `.nt`).
    pub fn upgrade_from_sbol2_path(
        path: impl AsRef<Path>,
    ) -> Result<(Self, UpgradeReport), UpgradeFromPathError> {
        let path = path.as_ref();
        let format =
            infer_sbol2_rdf_format(path).ok_or_else(|| UpgradeFromPathError::UnknownFormat {
                path: path.to_path_buf(),
                extension: path
                    .extension()
                    .and_then(|ext| ext.to_str())
                    .map(str::to_owned),
            })?;
        let input = std::fs::read_to_string(path).map_err(|source| UpgradeFromPathError::Io {
            path: path.to_path_buf(),
            source,
        })?;
        Self::upgrade_from_sbol2(&input, format).map_err(UpgradeFromPathError::Upgrade)
    }

    pub(crate) fn from_rdf_graph(graph: RdfGraph) -> Self {
        let objects = collect_objects(&graph);
        let typed = objects
            .values()
            .filter_map(SbolObject::try_from_object)
            .collect();
        Self {
            graph,
            objects,
            typed,
        }
    }

    pub(crate) fn from_parts(
        graph: RdfGraph,
        objects: BTreeMap<Resource, Object>,
        typed: Vec<SbolObject>,
    ) -> Self {
        Self {
            graph,
            objects,
            typed,
        }
    }

    /// Serializes the document in the given RDF format.
    pub fn write(&self, format: RdfFormat) -> Result<String, WriteError> {
        self.graph.write(format).map_err(WriteError::Rdf)
    }

    /// Writes the document to a file in the given RDF format. The caller
    /// chooses the format explicitly; no inference from the path's
    /// extension is performed.
    pub fn write_path(&self, path: impl AsRef<Path>, format: RdfFormat) -> Result<(), WriteError> {
        let path = path.as_ref();
        let serialized = self.write(format)?;
        std::fs::write(path, serialized).map_err(|source| WriteError::Io {
            path: path.to_path_buf(),
            source,
        })
    }

    /// Serializes the underlying RDF graph as Turtle.
    pub fn write_turtle(&self) -> Result<String, WriteError> {
        self.write(RdfFormat::Turtle)
    }

    /// Returns the underlying RDF graph.
    pub fn rdf_graph(&self) -> &RdfGraph {
        &self.graph
    }

    /// Returns RDF-backed objects indexed by identity.
    ///
    /// These are property-bag values preserving every triple under each
    /// subject — including PROV/OM and extension classes that do not yet
    /// have an owned typed representation. For SBOL classes with an owned
    /// surface, prefer [`Document::components`] and friends.
    pub fn objects(&self) -> &BTreeMap<Resource, Object> {
        &self.objects
    }

    /// Returns the RDF-backed object at `identity`, if any.
    pub fn get(&self, identity: &Resource) -> Option<&Object> {
        self.objects.get(identity)
    }

    /// Returns the owned typed SBOL objects in the document, in identity order.
    pub fn typed_objects(&self) -> &[SbolObject] {
        &self.typed
    }

    /// Returns the owned typed object whose identity matches `identity`.
    pub fn resolve(&self, identity: &Resource) -> Option<&SbolObject> {
        self.typed.iter().find(|o| o.identity() == identity)
    }

    /// Returns the owned typed object whose compliant identity matches
    /// `{namespace}/[local/]{display_id}`. The `local` path is optional
    /// per SBOL 3.1.0 §5.1, so this scans every typed object whose
    /// identity has the right namespace prefix and ends in the given
    /// display_id rather than constructing a fixed IRI.
    pub fn find_by_display_id(&self, namespace: &str, display_id: &str) -> Option<&SbolObject> {
        let prefix = if namespace.ends_with('/') {
            namespace.to_owned()
        } else {
            format!("{namespace}/")
        };
        let suffix = format!("/{display_id}");
        let exact = format!("{prefix}{display_id}");
        self.typed.iter().find(|object| {
            let identity = object.identity();
            let iri = match identity.as_iri() {
                Some(iri) => iri.as_str(),
                None => return false,
            };
            if iri == exact {
                return true;
            }
            iri.starts_with(&prefix) && iri.ends_with(&suffix)
        })
    }

    typed_doc_iter!(attachments, Attachment, Attachment);
    typed_doc_iter!(collections, Collection, Collection);
    typed_doc_iter!(
        combinatorial_derivations,
        CombinatorialDerivation,
        CombinatorialDerivation
    );
    typed_doc_iter!(components, Component, Component);
    typed_doc_iter!(component_references, ComponentReference, ComponentReference);
    typed_doc_iter!(constraints, Constraint, Constraint);
    typed_doc_iter!(cuts, Cut, Cut);
    typed_doc_iter!(entire_sequences, EntireSequence, EntireSequence);
    typed_doc_iter!(experiments, Experiment, Experiment);
    typed_doc_iter!(experimental_data, ExperimentalData, ExperimentalData);
    typed_doc_iter!(externally_defined, ExternallyDefined, ExternallyDefined);
    typed_doc_iter!(implementations, Implementation, Implementation);
    typed_doc_iter!(interactions, Interaction, Interaction);
    typed_doc_iter!(interfaces, Interface, Interface);
    typed_doc_iter!(local_sub_components, LocalSubComponent, LocalSubComponent);
    typed_doc_iter!(models, Model, Model);
    typed_doc_iter!(participations, Participation, Participation);
    typed_doc_iter!(ranges, Range, Range);
    typed_doc_iter!(sequences, Sequence, Sequence);
    typed_doc_iter!(sequence_features, SequenceFeature, SequenceFeature);
    typed_doc_iter!(sub_components, SubComponent, SubComponent);
    typed_doc_iter!(variable_features, VariableFeature, VariableFeature);
    typed_doc_iter!(activities, Activity, Activity);
    typed_doc_iter!(agents, Agent, Agent);
    typed_doc_iter!(associations, Association, Association);
    typed_doc_iter!(plans, Plan, Plan);
    typed_doc_iter!(usages, Usage, Usage);
    typed_doc_iter!(measures, Measure, Measure);
    typed_doc_iter!(units, Unit, Unit);
    typed_doc_iter!(singular_units, SingularUnit, SingularUnit);
    typed_doc_iter!(compound_units, CompoundUnit, CompoundUnit);
    typed_doc_iter!(unit_divisions, UnitDivision, UnitDivision);
    typed_doc_iter!(unit_exponentiations, UnitExponentiation, UnitExponentiation);
    typed_doc_iter!(unit_multiplications, UnitMultiplication, UnitMultiplication);
    typed_doc_iter!(prefixed_units, PrefixedUnit, PrefixedUnit);
    typed_doc_iter!(prefixes, Prefix, Prefix);
    typed_doc_iter!(si_prefixes, SIPrefix, SIPrefix);
    typed_doc_iter!(binary_prefixes, BinaryPrefix, BinaryPrefix);
    typed_doc_iter!(
        identified_extensions,
        IdentifiedExtension,
        IdentifiedExtension
    );

    /// Iterates over the TopLevel typed objects in the document.
    pub fn top_levels(&self) -> impl Iterator<Item = &SbolObject> {
        self.typed
            .iter()
            .filter(|o| o.top_level_namespace().is_some())
    }

    /// Iterates over the distinct namespaces declared by TopLevel objects in
    /// the document.
    pub fn namespaces(&self) -> impl Iterator<Item = &crate::Iri> + '_ {
        let mut seen: std::collections::BTreeSet<&str> = std::collections::BTreeSet::new();
        self.typed.iter().filter_map(move |object| {
            let ns = object.top_level_namespace()?;
            if seen.insert(ns.as_str()) {
                Some(ns)
            } else {
                None
            }
        })
    }

    /// Builds a structured validation report.
    pub fn validate(&self) -> ValidationReport {
        self.validate_with(ValidationOptions::default())
    }

    /// Builds a structured validation report with explicit validation options.
    pub fn validate_with(&self, options: ValidationOptions) -> ValidationReport {
        let mut validator = Validator::new(self, options);
        validator.validate();
        validator.finish()
    }

    /// Builds a structured validation report with resolver-aware validation context.
    pub fn validate_with_context(&self, context: ValidationContext<'_>) -> ValidationReport {
        let mut validator = Validator::new_with_context(self, context);
        validator.validate();
        validator.finish()
    }

    /// Runs validation and returns the report wrapped as `Ok` when no
    /// fully-evaluated rule reported an error, or `Err` carrying the
    /// same report when any rule did. Coverage gaps from `Partial` rules
    /// do not on their own cause `Err`; use [`check_complete`] for that.
    ///
    /// [`check_complete`]: Document::check_complete
    pub fn check(&self) -> Result<ValidationReport, ValidationReport> {
        check_outcome(self.validate(), false)
    }

    /// `check` with explicit validation options.
    pub fn check_with(
        &self,
        options: ValidationOptions,
    ) -> Result<ValidationReport, ValidationReport> {
        check_outcome(self.validate_with(options), false)
    }

    /// `check` with explicit resolver-aware validation context.
    pub fn check_with_context(
        &self,
        context: ValidationContext<'_>,
    ) -> Result<ValidationReport, ValidationReport> {
        check_outcome(self.validate_with_context(context), false)
    }

    /// Like [`check`], but also returns `Err` when any rule's coverage
    /// is partial — i.e. the validator was unable to fully evaluate it
    /// for this run. Use for CI gates against documents the team
    /// controls end-to-end.
    ///
    /// [`check`]: Document::check
    pub fn check_complete(&self) -> Result<ValidationReport, ValidationReport> {
        check_outcome(self.validate(), true)
    }

    /// `check_complete` with explicit validation options.
    pub fn check_complete_with(
        &self,
        options: ValidationOptions,
    ) -> Result<ValidationReport, ValidationReport> {
        check_outcome(self.validate_with(options), true)
    }

    /// `check_complete` with explicit resolver-aware validation context.
    pub fn check_complete_with_context(
        &self,
        context: ValidationContext<'_>,
    ) -> Result<ValidationReport, ValidationReport> {
        check_outcome(self.validate_with_context(context), true)
    }
}

fn check_outcome(
    report: ValidationReport,
    require_complete: bool,
) -> Result<ValidationReport, ValidationReport> {
    if report.has_errors() {
        return Err(report);
    }
    if require_complete && !report.coverage().partially_applied.is_empty() {
        return Err(report);
    }
    Ok(report)
}

fn infer_sbol2_rdf_format(path: &Path) -> Option<RdfFormat> {
    if let Some(format) = RdfFormat::from_path(path) {
        return Some(format);
    }
    let extension = path.extension()?.to_str()?.to_ascii_lowercase();
    (extension == "xml").then_some(RdfFormat::RdfXml)
}

/// Errors returned by [`Document::upgrade_from_sbol2_path`].
#[derive(Debug)]
#[non_exhaustive]
pub enum UpgradeFromPathError {
    /// Failed to read the file at the given path.
    Io {
        path: std::path::PathBuf,
        source: std::io::Error,
    },
    /// The path's extension did not match any supported RDF serialization.
    UnknownFormat {
        path: std::path::PathBuf,
        extension: Option<String>,
    },
    /// The file was loaded but the upgrade itself failed.
    Upgrade(UpgradeError),
}

impl std::fmt::Display for UpgradeFromPathError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io { path, source } => write!(f, "failed to read {}: {source}", path.display()),
            Self::UnknownFormat { path, extension } => {
                let ext = extension.as_deref().unwrap_or("<none>");
                write!(
                    f,
                    "unsupported extension `{ext}` for {} — supported: .ttl, .rdf, .jsonld, .nt",
                    path.display()
                )
            }
            Self::Upgrade(err) => write!(f, "{err}"),
        }
    }
}

impl std::error::Error for UpgradeFromPathError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io { source, .. } => Some(source),
            Self::UnknownFormat { .. } => None,
            Self::Upgrade(err) => Some(err),
        }
    }
}