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
//! Contains implementation for some Similarity algorithms that are
//! considered to be default implementations.
//!
//! All of the algorithms can also be accessed via [`crate::similarity::Builtins`]

use std::collections::HashSet;
use std::hash::Hash;

use crate::similarity::{usize_to_f32, Similarity};
use crate::term::InformationContentKind;
use crate::HpoTerm;

// Clippy thinks the `PLoS` is a struct or should have backticks for some reason
#[allow(clippy::doc_markdown)]
/// Graph based Information coefficient similarity
///
/// For a detailed description see [Deng Y, et. al., PLoS One, (2015)](https://pubmed.ncbi.nlm.nih.gov/25664462/)
#[derive(Debug)]
pub struct GraphIc {
    kind: InformationContentKind,
}

impl GraphIc {
    /// Constructs a new struct to calculate `GraphIC` based similarity scores
    /// between two terms
    ///
    /// # Examples
    ///
    /// ```
    /// use hpo::similarity::GraphIc;
    /// use hpo::term::InformationContentKind;
    ///
    /// // use Omim-based InformationContent for similarity calculation
    /// let graphic = GraphIc::new(InformationContentKind::Omim);
    /// ```
    ///
    pub fn new(kind: InformationContentKind) -> Self {
        Self { kind }
    }
}

impl Similarity for GraphIc {
    fn calculate(&self, a: &HpoTerm, b: &HpoTerm) -> f32 {
        if a.id() == b.id() {
            return 1.0;
        }

        let ic_union: f32 = a
            .all_union_ancestors(b)
            .iter()
            .map(|p| p.information_content().get_kind(&self.kind))
            .sum();

        if ic_union == 0.0 {
            return 0.0;
        }

        let ic_common: f32 = a
            .all_common_ancestors(b)
            .iter()
            .map(|p| p.information_content().get_kind(&self.kind))
            .sum();

        ic_common / ic_union
    }
}

/// Similarity score from Resnik
///
/// For a detailed description see [Resnik P, Proceedings of the 14th IJCAI, (1995)](https://www.ijcai.org/Proceedings/95-1/Papers/059.pdf)
#[derive(Debug)]
pub struct Resnik {
    kind: InformationContentKind,
}

impl Resnik {
    /// Constructs a new struct to calculate the Resnik based similarity scores
    /// between two terms
    ///
    /// # Examples
    ///
    /// ```
    /// use hpo::similarity::Resnik;
    /// use hpo::term::InformationContentKind;
    ///
    /// // use Omim-based InformationContent for similarity calculation
    /// let resnik = Resnik::new(InformationContentKind::Omim);
    /// ```
    ///
    pub fn new(kind: InformationContentKind) -> Self {
        Self { kind }
    }
}

impl Similarity for Resnik {
    fn calculate(&self, a: &HpoTerm, b: &HpoTerm) -> f32 {
        a.all_common_ancestors(b)
            .iter()
            .map(|term| term.information_content().get_kind(&self.kind))
            .fold(0.0, |max, term| if term > max { term } else { max })
    }
}

/// Similarity score from Lin
///
/// For a detailed description see [Lin D, Proceedings of the 15th ICML, (1998)](https://dl.acm.org/doi/10.5555/645527.657297)
#[derive(Debug)]
pub struct Lin {
    kind: InformationContentKind,
}

impl Lin {
    /// Constructs a new struct to calculate the Lin based similarity scores
    /// between two terms
    ///
    /// # Examples
    ///
    /// ```
    /// use hpo::similarity::Lin;
    /// use hpo::term::InformationContentKind;
    ///
    /// // use Omim-based InformationContent for similarity calculation
    /// let lin = Lin::new(InformationContentKind::Omim);
    /// ```
    ///
    pub fn new(kind: InformationContentKind) -> Self {
        Self { kind }
    }
}

impl Similarity for Lin {
    fn calculate(&self, a: &HpoTerm, b: &HpoTerm) -> f32 {
        let ic_combined = a.information_content().get_kind(&self.kind)
            + b.information_content().get_kind(&self.kind);

        if ic_combined == 0.0 {
            return 0.0;
        }

        let resnik = Resnik::new(self.kind).calculate(a, b);

        2.0 * resnik / ic_combined
    }
}

/// Similarity score from Jiang & Conrath
///
/// For a detailed description see [Jiang J, Conrath D, Rocling X, (1997)](https://aclanthology.org/O97-1002.pdf)
///
/// # Note
///
/// This algorithm is an implementation as described in the paper cited above, with minor
/// modifications. It is different from the `JC` implementation in the `HPOSim` R library.
/// For a discussion on the correct implementation see
/// [this issue from pyhpo](https://github.com/anergictcell/pyhpo/issues/20).
///
/// # Note
///
/// The logic of the JC similarity was changed in version `0.8.3`. Ensure you update
/// to at least `0.8.3` before using it.
#[derive(Debug)]
pub struct Jc {
    kind: InformationContentKind,
}

impl Jc {
    /// Constructs a new struct to calculate the Jiang & Conrath based similarity scores
    /// between two terms
    ///
    /// # Examples
    ///
    /// ```
    /// use hpo::similarity::Jc;
    /// use hpo::term::InformationContentKind;
    ///
    /// // use Omim-based InformationContent for similarity calculation
    /// let jc = Jc::new(InformationContentKind::Omim);
    /// ```
    ///
    pub fn new(kind: InformationContentKind) -> Self {
        Self { kind }
    }
}

impl Similarity for Jc {
    fn calculate(&self, a: &HpoTerm, b: &HpoTerm) -> f32 {
        if a.id() == b.id() {
            return 1.0;
        }

        let ic1 = a.information_content().get_kind(&self.kind);
        let ic2 = b.information_content().get_kind(&self.kind);

        if ic1 == 0.0 || ic2 == 0.0 {
            return 0.0;
        }

        let resnik = Resnik::new(self.kind).calculate(a, b);

        1.0 / (ic1 + ic2 - 2.0 * resnik + 1.0)
    }
}

/// Relevance Similarity score from Schlicker
///
/// For a detailed description see [Schlicker A, et.al., BMC Bioinformatics, (2006)](https://bmcbioinformatics.biomedcentral.com/articles/10.1186/1471-2105-7-302)
///
#[derive(Debug)]
pub struct Relevance {
    kind: InformationContentKind,
}

impl Relevance {
    /// Constructs a new struct to calculate the Schlicker based similarity scores
    /// between two terms
    ///
    /// # Examples
    ///
    /// ```
    /// use hpo::similarity::Relevance;
    /// use hpo::term::InformationContentKind;
    ///
    /// // use Omim-based InformationContent for similarity calculation
    /// let rel = Relevance::new(InformationContentKind::Omim);
    /// ```
    ///
    pub fn new(kind: InformationContentKind) -> Self {
        Self { kind }
    }
}

impl Similarity for Relevance {
    fn calculate(&self, a: &HpoTerm, b: &HpoTerm) -> f32 {
        let resnik = Resnik::new(self.kind).calculate(a, b);
        let lin = Lin::new(self.kind).calculate(a, b);

        lin * (1.0 - (resnik * -1.0).exp())
    }
}

/// Information Coefficient Similarity score from Li
///
/// For a detailed description see [Li B, et. al., arXiv, (2010)](https://arxiv.org/abs/1001.0958)
///
#[derive(Debug)]
pub struct InformationCoefficient {
    kind: InformationContentKind,
}

impl InformationCoefficient {
    /// Constructs a new struct to calculate the Jiang & Conrath based similarity scores
    /// between two terms
    ///
    /// # Examples
    ///
    /// ```
    /// use hpo::similarity::InformationCoefficient;
    /// use hpo::term::InformationContentKind;
    ///
    /// // use Omim-based InformationContent for similarity calculation
    /// let ic = InformationCoefficient::new(InformationContentKind::Omim);
    /// ```
    ///
    pub fn new(kind: InformationContentKind) -> Self {
        Self { kind }
    }
}

impl Similarity for InformationCoefficient {
    fn calculate(&self, a: &HpoTerm, b: &HpoTerm) -> f32 {
        let resnik = Resnik::new(self.kind).calculate(a, b);
        let lin = Lin::new(self.kind).calculate(a, b);

        lin * (1.0 - (1.0 / (1.0 + resnik)))
    }
}

/// Similarity score based on distance between terms
#[derive(Default, Debug)]
pub struct Distance {}

impl Distance {
    /// Constructs a new struct to calculate the distance based similarity scores
    /// between two terms
    ///
    /// # Examples
    ///
    /// ```
    /// use hpo::similarity::Distance;
    /// use hpo::term::InformationContentKind;
    ///
    /// let dist = Distance::new();
    /// ```
    ///
    pub fn new() -> Self {
        Self::default()
    }
}

impl Similarity for Distance {
    fn calculate(&self, a: &HpoTerm, b: &HpoTerm) -> f32 {
        a.distance_to_term(b)
            .map_or(0.0, |n| 1.0 / (usize_to_f32(n) + 1.0))
    }
}

/// Similarity score based on the difference in associated genes or diseases
///
/// The [`Mutation`] algorithm calculates how many genes or diseases are
/// associated to only one term and not the other. If two terms are linked
/// to the same annotations, their similarity score will be `1`. If both
/// terms do not have any associated terms, they are considered completely
/// different, i.e. have a similarity of `0`.
#[derive(Debug)]
pub struct Mutation {
    kind: InformationContentKind,
}

impl Mutation {
    /// Constructs a new struct to calculate the Mutation based similarity scores
    /// between two terms
    ///
    /// # Examples
    ///
    /// ```
    /// use hpo::similarity::Mutation;
    /// use hpo::term::InformationContentKind;
    ///
    /// // use Omim-based InformationContent for similarity calculation
    /// let jc = Mutation::new(InformationContentKind::Omim);
    /// ```
    ///
    pub fn new(kind: InformationContentKind) -> Self {
        Self { kind }
    }

    fn gene_similarity(a: &HpoTerm, b: &HpoTerm) -> f32 {
        let genes_a = a.gene_ids();
        let genes_b = b.gene_ids();

        let all = genes_a | genes_b;
        let common = genes_a & genes_b;

        usize_to_f32(common.len()) / usize_to_f32(all.len())
    }

    fn omim_disease_similarity(a: &HpoTerm, b: &HpoTerm) -> f32 {
        let diseases_a = a.omim_disease_ids();
        let diseases_b = b.omim_disease_ids();

        Self::disease_similarity(diseases_a, diseases_b)
    }

    fn orpha_disease_similarity(a: &HpoTerm, b: &HpoTerm) -> f32 {
        let diseases_a = a.orpha_disease_ids();
        let diseases_b = b.orpha_disease_ids();

        Self::disease_similarity(diseases_a, diseases_b)
    }

    fn disease_similarity<T: Eq + Hash + Clone>(
        disease_a: &HashSet<T>,
        disease_b: &HashSet<T>,
    ) -> f32 {
        let all = disease_a | disease_b;
        let common = disease_a & disease_b;

        if all.is_empty() {
            return 0.0;
        }

        usize_to_f32(common.len()) / usize_to_f32(all.len())
    }
}

impl Similarity for Mutation {
    fn calculate(&self, a: &HpoTerm, b: &HpoTerm) -> f32 {
        if a == b {
            return 1.0;
        }
        match self.kind {
            InformationContentKind::Gene => Mutation::gene_similarity(a, b),
            InformationContentKind::Omim => Mutation::omim_disease_similarity(a, b),
            InformationContentKind::Orpha => Mutation::orpha_disease_similarity(a, b),
        }
    }
}