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
use std::collections::hash_map::Values;
use std::collections::HashSet;
use std::fmt::{Debug, Display};
use std::hash::Hash;

use crate::annotations::disease::DiseaseIterator;
use crate::annotations::{AnnotationId, Disease};
use crate::term::HpoGroup;
use crate::{HpoError, HpoSet, HpoTermId, Ontology};

/// A set of OMIM diseases
///
/// The set does not contain [`OmimDisease`]s itself, but only
/// their [`OmimDiseaseId`]s.
/// Currently implemented using [`HashSet`] but any other implementation
/// should work as well given that each [`OmimDiseaseId`] must appear only once
/// and it provides an iterator of [`OmimDiseaseId`]
pub type OmimDiseases = HashSet<OmimDiseaseId>;

/// A unique identifier for an [`OmimDisease`]
///
/// This value can - in theory - represent any numerical unique value.
/// When using the default JAX provided masterdata, it represents
/// the actual OMIM MIM ID.
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct OmimDiseaseId {
    inner: u32,
}

impl AnnotationId for OmimDiseaseId {
    /// Convert `self` to `u32`
    fn as_u32(&self) -> u32 {
        self.inner
    }
}

impl TryFrom<&str> for OmimDiseaseId {
    type Error = HpoError;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Ok(OmimDiseaseId {
            inner: value.parse::<u32>()?,
        })
    }
}

impl From<u32> for OmimDiseaseId {
    fn from(inner: u32) -> Self {
        OmimDiseaseId { inner }
    }
}

impl Display for OmimDiseaseId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "OMIM:{}", self.inner)
    }
}

/// A single OMIM disease
///
/// A disease has a unique [`OmimDiseaseId`] and a name and is
/// connected to a set of HPO terms
#[derive(Default, Debug, Clone)]
pub struct OmimDisease {
    id: OmimDiseaseId,
    name: String,
    hpos: HpoGroup,
}

impl Disease for OmimDisease {
    type AnnoID = OmimDiseaseId;

    /// Initializes a new OMIM disease
    ///
    /// This method should rarely, if ever, be used directly. The
    /// preferred way to create new genes is through [`crate::Ontology::add_omim_disease`]
    /// to ensure that each disease exists only once.
    fn new(id: Self::AnnoID, name: &str) -> OmimDisease {
        Self {
            name: name.to_string(),
            id,
            hpos: HpoGroup::default(),
        }
    }

    /// The unique [`OmimDiseaseId`] of the disease, the OMIM MIM number
    fn id(&self) -> &Self::AnnoID {
        &self.id
    }

    /// The OMIM disease name
    fn name(&self) -> &str {
        &self.name
    }

    /// The set of connected HPO terms
    fn hpo_terms(&self) -> &HpoGroup {
        &self.hpos
    }

    /// Returns a binary representation of the `OmimDisease`
    ///
    /// The binary layout is defined as:
    ///
    /// | Byte offset | Number of bytes | Description |
    /// | --- | --- | --- |
    /// | 0 | 4 | The total length of the binary data blob as big-endian `u32` |
    /// | 4 | 4 | The `OmimDiseaseId` as big-endian `u32` |
    /// | 8 | 4 | The length of the `OmimDisease` Name as big-endian `u32` |
    /// | 12 | n | The `OmimDisease` name as u8 vector |
    /// | 12 + n | 4 | The number of associated HPO terms as big-endian `u32` |
    /// | 16 + n | x * 4 | The [`HpoTermId`]s of the associated terms, each encoded as big-endian `u32` |
    ///
    /// # Examples
    ///
    /// ```
    /// use hpo::annotations::{Disease, OmimDisease};
    ///
    /// let mut disease = OmimDisease::new(123.into(), "FooBar");
    /// let bytes = disease.as_bytes();
    ///
    /// assert_eq!(bytes.len(), 4 + 4 + 4 + 6 + 4);
    /// assert_eq!(bytes[4..8], [0u8, 0u8, 0u8, 123u8]); // ID of disease => 123
    /// assert_eq!(bytes[8..12], [0u8, 0u8, 0u8, 6u8]); // Length of Name => 6
    /// ```
    fn as_bytes(&self) -> Vec<u8> {
        fn usize_to_u32(n: usize) -> u32 {
            n.try_into().expect("unable to convert {n} to u32")
        }
        let name = self.name().as_bytes();
        let name_length = name.len();
        let size = 4 + 4 + 4 + name_length + 4 + self.hpos.len() * 4;

        let mut res = Vec::new();

        // 4 bytes for total length
        res.append(&mut usize_to_u32(size).to_be_bytes().to_vec());

        // 4 bytes for OMIM Disease-ID
        res.append(&mut self.id.to_be_bytes().to_vec());

        // 4 bytes for Length of OMIM Disease Name
        res.append(&mut usize_to_u32(name_length).to_be_bytes().to_vec());

        // OMIM Disease name (n bytes)
        for c in name {
            res.push(*c);
        }

        // 4 bytes for number of HPO terms
        res.append(&mut usize_to_u32(self.hpos.len()).to_be_bytes().to_vec());

        // HPO terms
        res.append(&mut self.hpos.as_bytes());

        res
    }

    /// Returns an [`HpoSet`] from the `OmimDisease`
    fn to_hpo_set<'a>(&self, ontology: &'a Ontology) -> HpoSet<'a> {
        HpoSet::new(ontology, self.hpos.clone())
    }

    /// Connect another [HPO term](`crate::HpoTerm`) to the disease
    ///
    /// # Note
    ///
    /// This method does **not** add the [`OmimDisease`] to the [HPO term](`crate::HpoTerm`).
    /// Clients should not use this method, unless they are creating their own Ontology.
    fn add_term<I: Into<HpoTermId>>(&mut self, term_id: I) -> bool {
        self.hpos.insert(term_id)
    }
}

impl PartialEq for OmimDisease {
    fn eq(&self, other: &OmimDisease) -> bool {
        self.id == other.id
    }
}

impl Eq for OmimDisease {}

impl TryFrom<&[u8]> for OmimDisease {
    type Error = HpoError;
    /// Returns an [`OmimDisease`] from a bytes vector
    ///
    /// The byte layout for this method is defined in
    /// [`Disease::as_bytes`]
    ///
    /// # Examples
    ///
    /// ```
    /// use hpo::annotations::{Disease, OmimDisease, OmimDiseaseId};
    ///
    /// let bytes = vec![
    ///     0u8, 0u8, 0u8, 22u8, // Total size of Blop
    ///     0u8, 0u8, 0u8, 123u8, // ID of the disease => 123
    ///     0u8, 0u8, 0u8, 6u8, // Length of name => 6
    ///     b'F', b'o', b'o', b'b', b'a', b'r', // Foobar
    ///     0u8, 0u8, 0u8, 0u8  // Number of associated HPO Terms => 0
    /// ];
    /// let disease = OmimDisease::try_from(&bytes[..]).unwrap();
    ///
    /// assert_eq!(disease.name(), "Foobar");
    /// assert_eq!(disease.id(), &OmimDiseaseId::from(123u32));
    /// ```
    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        Self::from_bytes(bytes)
    }
}

impl Hash for OmimDisease {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

/// Iterates [`OmimDisease`]
pub type OmimDiseaseIterator<'a> = DiseaseIterator<'a, OmimDiseaseId>;

impl<'a> std::iter::Iterator for DiseaseIterator<'a, OmimDiseaseId> {
    type Item = &'a OmimDisease;
    fn next(&mut self) -> Option<Self::Item> {
        self.diseases.next().map(|omim_id| {
            self.ontology
                .omim_disease(omim_id)
                .expect("disease must exist in Ontology")
        })
    }
}

/// Iterates [`OmimDisease`] that match the query string
///
/// This struct is returned by [`crate::Ontology::omim_diseases_by_name`]
pub struct OmimDiseaseFilter<'a> {
    iter: Values<'a, OmimDiseaseId, OmimDisease>,
    query: &'a str,
}

impl<'a> OmimDiseaseFilter<'a> {
    pub(crate) fn new(iter: Values<'a, OmimDiseaseId, OmimDisease>, query: &'a str) -> Self {
        OmimDiseaseFilter { iter, query }
    }
}

impl<'a> Iterator for OmimDiseaseFilter<'a> {
    type Item = &'a OmimDisease;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter
            .by_ref()
            .find(|&item| item.name().contains(self.query))
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn disease_to_binary() {
        let mut disease = OmimDisease::new(123u32.into(), "FooBar");
        disease.add_term(66u32);
        disease.add_term(77u32);

        let bin = disease.as_bytes();

        assert_eq!(bin.len(), 4 + 4 + 4 + 6 + 4 + 8);
    }

    #[test]
    fn disease_to_and_from_binary() {
        let mut disease = OmimDisease::new(123u32.into(), "FooBar");
        disease.add_term(66u32);
        disease.add_term(77u32);

        let bin = disease.as_bytes();

        let disease2 = OmimDisease::try_from(&bin[..]).expect("Can't build Disease");

        assert_eq!(disease.name(), disease2.name());
        assert_eq!(disease.id(), disease2.id());
        assert_eq!(disease.hpo_terms().len(), disease2.hpo_terms().len());
        for (a, b) in disease.hpo_terms().iter().zip(disease2.hpo_terms().iter()) {
            assert_eq!(a, b);
        }
    }

    #[test]
    fn disease_with_non_utf8_name() {
        let mut disease = OmimDisease::new(123u32.into(), "Foo😀Bar");
        disease.add_term(66u32);
        disease.add_term(77u32);

        let bin = disease.as_bytes();

        // the smiley uses 4 bytes, so the name length is 10
        assert_eq!(bin.len(), 4 + 4 + 4 + 10 + 4 + 8);

        let disease2 = OmimDisease::try_from(&bin[..]).expect("Can't build Disease");

        assert_eq!(disease.name(), disease2.name());
        assert_eq!(disease.id(), disease2.id());
        assert_eq!(disease.hpo_terms().len(), disease2.hpo_terms().len());
        for (a, b) in disease.hpo_terms().iter().zip(disease2.hpo_terms().iter()) {
            assert_eq!(a, b);
        }
    }

    #[test]
    fn disease_with_long_name() {
        let name = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
        let mut disease = OmimDisease::new(123u32.into(), name);
        disease.add_term(66u32);
        disease.add_term(77u32);

        let bin = disease.as_bytes();

        let disease2 = OmimDisease::try_from(&bin[..]).expect("Can't build Disease");

        assert_eq!(disease.name(), disease2.name());
        assert_eq!(disease.id(), disease2.id());
        assert_eq!(disease.hpo_terms().len(), disease2.hpo_terms().len());
        for (a, b) in disease.hpo_terms().iter().zip(disease2.hpo_terms().iter()) {
            assert_eq!(a, b);
        }
    }

    #[test]
    fn disease_with_wrong_length() {
        let mut disease = OmimDisease::new(123u32.into(), "foobar");
        disease.add_term(66u32);
        disease.add_term(77u32);

        let mut bin = disease.as_bytes();

        assert!(OmimDisease::try_from(&bin[..15]).is_err());
        assert!(OmimDisease::try_from(&bin[..21]).is_err());
        assert!(OmimDisease::try_from(&bin[..29]).is_err());
        assert!(OmimDisease::try_from(&bin[..30]).is_ok());

        bin.push(1);
        assert!(OmimDisease::try_from(&bin[..30]).is_ok());
        assert!(OmimDisease::try_from(&bin[..31]).is_err());
    }
}