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
//! *Structures to deserialize [OBO Foundry](http://www.obofoundry.org) listings into.*
//!
//! [![TravisCI](https://img.shields.io/travis/althonos/obofoundry.rs/master.svg?maxAge=600&style=flat-square)](https://travis-ci.org/althonos/obofoundry.rs/branches)
//! [![Codecov](https://img.shields.io/codecov/c/gh/althonos/obofoundry.rs/master.svg?style=flat-square&maxAge=600)](https://codecov.io/gh/althonos/obofoundry.rs)
//! [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square&maxAge=2678400)](https://choosealicense.com/licenses/mit/)
//! [![Source](https://img.shields.io/badge/source-GitHub-303030.svg?maxAge=2678400&style=flat-square)](https://github.com/althonos/obofoundry.rs)
//! [![Crate](https://img.shields.io/crates/v/obofoundry.svg?maxAge=600&style=flat-square)](https://crates.io/crates/obofoundry)
//! [![Documentation](https://img.shields.io/badge/docs.rs-latest-4d76ae.svg?maxAge=2678400&style=flat-square)](https://docs.rs/obofoundry)
//! [![Changelog](https://img.shields.io/badge/keep%20a-changelog-8A0707.svg?maxAge=2678400&style=flat-square)](https://github.com/althonos/obofoundry.rs/blob/master/CHANGELOG.md)
//!
//! This library provides structs that can make use of `serde_yaml` and
//! `serde_json` to deserialize the table of ontologies provided by the
//! OBO Foundry. It provides a safe and efficient alternative to manually
//! parsing the obtained JSON/YAML, which is actually quite tricky since
//! there is no actual scheme available. Use the [`Foundry`]("struct.Foundry.html")
//! type as an entry point for deserialization.
//!
//! # Example
//!
//! Download the `ontologies.yml` table from the OBO Foundry and use it to
//! extract all PURLs to ontologies in the OBO format:
//!
//! ```rust
//! extern crate obofoundry;
//! extern crate reqwest;
//! extern crate serde_yaml;
//! use std::io::Read;
//!
//! const URL: &'static str = "http://www.obofoundry.org/registry/ontologies.yml";
//!
//! fn main() {
//!
//!     let mut res = reqwest::get(URL).expect("could not get YAML file");
//!     let mut yml = String::new();
//!     res.read_to_string(&mut yml).expect("could not read response");
//!
//!     let foundry: obofoundry::Foundry = serde_yaml::from_str(&yml).unwrap();
//!     for ontology in &foundry.ontologies {
//!         for product in &ontology.products {
//!             if product.id.ends_with(".obo") {
//!                 println!("{} - {}", product.id, product.ontology_purl)
//!             }
//!         }
//!     }
//! }
//! ```
//!
//! # Changelog
//!
//! This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html)
//! and provides a [changelog](https://github.com/althonos/obofoundry.rs/blob/master/CHANGELOG.md)
//! in the [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) format.

#![doc(html_logo_url = "https://github.com/OBOFoundry/OBOFoundry.github.io/raw/master/images/foundrylogo.png")]

extern crate serde;
extern crate url;

use serde::de::Deserializer;
use serde::Deserialize;
use serde::Serialize;
use url::Url;

/// Deserialize an optional `bool` encoded as a 0 or a 1.
fn optional_bool01<'de, D>(deserializer: D) -> Result<Option<bool>, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize, Debug)]
    #[serde(untagged)]
    pub enum MaybeBool {
        Opt(Option<u8>),
        Bool(u8),
    }

    match MaybeBool::deserialize(deserializer) {
        Ok(MaybeBool::Opt(opt)) => Ok(opt.map(|n| n != 0)),
        Ok(MaybeBool::Bool(n)) => Ok(Some(n != 0)),
        Err(e) => Err(e),
    }
}

/// Deserialize a possibly missing vector into an empty one.
fn optional_vector<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
    D: Deserializer<'de>,
    T: Deserialize<'de>,
{
    match Option::deserialize(deserializer) {
        Ok(Some(v)) => Ok(v),
        Ok(None) => Ok(Vec::new()),
        Err(e) => Err(e),
    }
}

/// Deserialize a vector of `Example`.
fn examples_vector<'de, D>(deserializer: D) -> Result<Vec<Example>, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize, Debug)]
    #[serde(untagged)]
    pub enum MaybeExample {
        #[serde(with = "url_serde")]
        String(Url),
        Example(Example),
    }

    Vec::<MaybeExample>::deserialize(deserializer).map(|v| {
        v.into_iter()
            .map(|mex| match mex {
                MaybeExample::Example(e) => e,
                MaybeExample::String(url) => Example {
                    url: url,
                    description: None,
                },
            })
            .collect()
    })
}

/// Returns `true`.
const fn bool_true() -> bool {
    true
}

/// Returns `false`.
const fn bool_false() -> bool {
    false
}

/// An index of ontologies following the OBO Foundry principles.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Foundry {
    pub ontologies: Vec<Ontology>,
}

/// A comprehensive table of informations about an ontology.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Ontology {
    pub activity_status: ActivityStatus,
    #[serde(rename = "alternativePrefix", alias = "alternatePrefix")]
    pub alternative_prefix: Option<String>,
    pub biosharing: Option<String>,
    #[serde(default, deserialize_with = "optional_vector")]
    pub browsers: Vec<Browser>,
    pub build: Option<Build>,
    pub canonical: Option<String>,
    pub contact: Option<Contact>,
    #[serde(rename = "createdWith")]
    pub created_with: Option<String>,
    pub description: Option<String>,
    #[serde(default, deserialize_with = "optional_vector")]
    pub dependencies: Vec<Dependency>,
    pub development: Option<Development>,
    pub depicted_by: Option<String>,
    #[serde(default, with = "url_serde")]
    pub documentation: Option<Url>,
    pub domain: Option<String>,
    #[serde(default, rename = "DO wiki", with = "url_serde")]
    pub do_wiki: Option<Url>,
    #[serde(rename = "exampleClass")]
    pub example_class: Option<String>,
    #[serde(default, with = "url_serde")]
    pub facebook: Option<Url>,
    #[serde(default, deserialize_with = "optional_vector")]
    pub funded_by: Vec<String>,
    pub google_plus: Option<String>,
    pub homepage: Option<String>,
    pub id: String,
    #[serde(default = "bool_true")]
    pub in_foundry: bool,
    pub in_foundry_order: Option<usize>,
    pub integration_server: Option<String>,
    #[serde(default = "bool_false")]
    pub is_obsolete: bool,
    #[serde(default, deserialize_with = "optional_vector")]
    pub jobs: Vec<Job>,
    pub label: Option<String>,
    pub layout: String,
    pub license: Option<License>,
    pub mailing_list: Option<String>,
    #[serde(default, with = "url_serde")]
    pub ontology_purl: Option<Url>,
    #[serde(default, with = "url_serde")]
    pub page: Option<Url>,
    #[serde(rename = "preferredPrefix")]
    pub preferred_prefix: Option<String>,
    #[serde(default, deserialize_with = "optional_vector")]
    pub products: Vec<Product>,
    #[serde(default, deserialize_with = "optional_vector")]
    pub publications: Vec<Publication>,
    #[serde(default, deserialize_with = "optional_vector")]
    pub redirects: Vec<Redirect>,
    pub releases: Option<String>,
    pub replaced_by: Option<String>,
    #[serde(default, with = "url_serde")]
    pub repository: Option<Url>,
    pub source: Option<String>,
    #[serde(default, with = "url_serde")]
    pub source_url: Option<Url>,
    pub taxon: Option<Taxon>,
    pub termgenie: Option<String>,
    pub title: String,
    #[serde(default, alias = "issue", with = "url_serde")]
    pub tracker: Option<Url>,
    #[serde(rename = "type")]
    pub ty: Option<String>,
    pub twitter: Option<String>,
    #[serde(default, alias = "used_by", deserialize_with = "optional_vector")]
    pub usages: Vec<Usage>,
    pub validate: Option<bool>,
    #[serde(rename = "wasDerivedFrom")]
    pub was_derived_from: Option<String>,
    pub wikidata_template: Option<String>,
}

/// A redirection to another location.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Redirect {
    #[serde(rename = "match")]
    pub path: String,
    #[serde(with = "url_serde")]
    pub url: Url,
}

/// Metadata concerning the development of the ontology.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Development {
    pub id_policy: String,
}

/// Reference to a particular dependency.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Dependency {
    pub id: String,
    pub title: Option<String>,
    #[serde(rename = "type")]
    pub ty: Option<String>,
    pub subset: Option<String>,
    pub description: Option<String>,
    #[serde(default, deserialize_with = "optional_vector")]
    pub connects: Vec<Dependency>,
    #[serde(default, deserialize_with = "optional_vector")]
    pub publications: Vec<Publication>,
}

/// Information about the way an ontology is built.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Build {
    pub checkout: Option<String>,
    #[serde(deserialize_with = "optional_bool01", default = "Default::default")]
    pub infallible: Option<bool>,
    pub insert_ontology_id: Option<bool>,
    pub method: Option<BuildMethod>,
    pub notes: Option<String>,
    pub oort_args: Option<String>,
    pub path: Option<String>,
    #[serde(default, with = "url_serde")]
    pub source_url: Option<Url>,
    pub system: Option<BuildSystem>,
    pub email_cc: Option<String>,
}

/// The build method for an ontology build.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum BuildMethod {
    Archive,
    Obo2Owl,
    Owl2Obo,
    Vcs,
}

/// The build system for an ontology build.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum BuildSystem {
    Git,
    Svn,
}

/// The legal information about an ontology.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct License {
    pub label: String,
    pub logo: Option<String>,
    #[serde(with = "url_serde")]
    pub url: Url,
}

/// The corresponding editor of an ontology.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Contact {
    pub email: String,
    #[serde(alias = "contact")]
    pub github: Option<String>,
    pub label: String,
}

/// A CI/CD job pipeline running for the ontology.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Job {
    pub id: String,
    #[serde(rename = "type")]
    pub ty: JobType,
}

/// The type of a job pipeline.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum JobType {
    #[serde(rename = "travis-ci")]
    TravisCi,
    DryRunBuild,
    ReleaseBuild,
}

/// A released product of an ontology.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Product {
    pub id: String,
    pub is_canonical: Option<bool>,
    pub contact: Option<Contact>,
    #[serde(default, deserialize_with = "optional_vector")]
    pub connects: Vec<Dependency>,
    pub derived_from: Option<String>,
    pub description: Option<String>,
    pub format: Option<String>,
    #[serde(default, with = "url_serde")]
    pub homepage: Option<Url>,
    pub license: Option<String>,
    pub mireots_from: Option<String>,
    #[serde(with = "url_serde")]
    pub ontology_purl: Url,
    pub page: Option<String>,
    pub title: Option<String>,
    #[serde(default, deserialize_with = "optional_vector")]
    pub uses: Vec<String>,
    pub taxon: Option<String>,
    #[serde(rename = "type")]
    pub ty: Option<String>,
}

/// A publication relevant to the ontology.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Publication {
    pub id: String,
    pub title: Option<String>,
}

/// A taxon specifically relevant to the ontology.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Taxon {
    pub id: String,
    pub label: Option<String>,
}

/// A relevant project an ontology is used in.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Usage {
    pub description: Option<String>,
    #[serde(default, deserialize_with = "examples_vector", alias = "example")]
    pub examples: Vec<Example>,
    #[serde(alias = "url", with = "url_serde")]
    pub user: Url,
    pub label: Option<String>,
    #[serde(rename = "type")]
    pub ty: Option<UsageType>,
    #[serde(rename = "seeAlso")]
    pub see_also: Option<String>,
    pub reference: Option<String>,
}

/// The way an ontology can be used in a project.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum UsageType {
    Annotation,
    OwlImport,
    Query,
    #[serde(rename = "Database")]
    Database,
    Application,
}

/// A reference to an example usage of the ontology.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Example {
    pub description: Option<String>,
    #[serde(with = "url_serde")]
    pub url: Url,
}

/// The current development status of the ontology development.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "lowercase")]
pub enum ActivityStatus {
    Active,
    Inactive,
    Orphaned,
}

/// A reference to a browser for the ontology.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Browser {
    pub label: String,
    pub title: String,
    #[serde(with = "url_serde")]
    pub url: Url,
}