Skip to main content

hpo/
lib.rs

1#![deny(clippy::pedantic)]
2#![allow(clippy::must_use_candidate)]
3#![allow(clippy::module_name_repetitions)]
4#![warn(missing_docs)]
5#![doc = include_str!("../README.md")]
6
7use core::fmt::Debug;
8use std::num::ParseIntError;
9use thiserror::Error;
10
11pub mod annotations;
12pub mod matrix;
13mod ontology;
14mod parser;
15mod set;
16pub mod similarity;
17pub mod stats;
18pub mod term;
19pub mod utils;
20
21pub use ontology::builder;
22pub use ontology::comparison;
23pub use ontology::Ontology;
24pub use set::HpoSet;
25#[doc(inline)]
26pub use term::{HpoTerm, HpoTermId};
27
28const DEFAULT_NUM_PARENTS: usize = 10;
29const DEFAULT_NUM_ALL_PARENTS: usize = 30;
30const DEFAULT_NUM_GENES: usize = 50;
31const DEFAULT_NUM_OMIM: usize = 20;
32const DEFAULT_NUM_ORPHA: usize = 20;
33const MAX_HPO_ID_INTEGER: usize = 10_000_000;
34
35const OBO_FILENAME: &str = "hp.obo";
36const GENE_FILENAME: &str = "phenotype_to_genes.txt";
37const GENE_TO_PHENO_FILENAME: &str = "genes_to_phenotype.txt";
38const DISEASE_FILENAME: &str = "phenotype.hpoa";
39
40/// The `HpoTermId` of `HP:0000118 | Phenotypic abnormality`
41pub const PHENOTYPE_ID: HpoTermId = HpoTermId::from_u32(118);
42
43#[derive(Error, Debug)]
44/// Main Error type for this crate
45pub enum HpoError {
46    /// Indicates that a method or feature is not yet implemented
47    #[error("not implemented")]
48    NotImplemented,
49    /// The term does not exist in the Ontology
50    #[error("term does not exist")]
51    DoesNotExist,
52    /// Parsing of an integer failed
53    #[error("unable to parse Integer")]
54    ParseIntError,
55    /// An error occured during parsing the binary HPO data
56    #[error("unable to parse binary data")]
57    ParseBinaryError,
58    /// Opening the file was not able - check if the file is present
59    #[error("cannot open file {0}")]
60    CannotOpenFile(String),
61    /// Failed to convert an integer to a float
62    #[error("cannot convert int to float")]
63    TryFromIntError(#[from] std::num::TryFromIntError),
64    /// Failed to parse a line of input data from the JAX obo
65    #[error("invalid input data: {0}")]
66    InvalidInput(String),
67}
68
69impl From<ParseIntError> for HpoError {
70    fn from(_: ParseIntError) -> Self {
71        HpoError::ParseIntError
72    }
73}
74
75/// Shortcut for `Result<T, HpoError>`
76pub type HpoResult<T> = Result<T, HpoError>;
77
78/// Returns a u32 from the 4 first 4 bytes
79///
80/// This function is used in parsing binary ontology data
81/// where u32 are used frequently. It is not recommended
82/// to use this function elsewhere.
83///
84/// The u32 sould be big-endian encoded
85fn u32_from_bytes(bytes: &[u8]) -> u32 {
86    u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
87}
88
89fn f32_from_usize(n: usize) -> HpoResult<f32> {
90    let intermediate: u16 = n.try_into()?;
91    Ok(intermediate.into())
92}