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 427 428 429 430 431 432 433 434 435
use core::fmt::Debug;
use std::cmp::PartialEq;
use std::collections::HashSet;
use std::convert::TryFrom;
use std::fmt::Display;
use std::hash::Hash;
use tracing::error;
use crate::annotations::AnnotationId;
use crate::set::HpoSet;
use crate::term::HpoGroup;
use crate::u32_from_bytes;
use crate::HpoError;
use crate::HpoResult;
use crate::HpoTermId;
use crate::Ontology;
/// A set of genes
///
/// The set does not contain [`Gene`]s itself, but only their [`GeneId`]s.
/// Currently implemented using [`HashSet`] but any other implementation
/// should work as well given that each [`GeneId`] must appear only once
/// and it provides an iterator of [`GeneId`]
pub type Genes = HashSet<GeneId>;
/// A unique identifier for a [`Gene`]
///
/// This value can - in theory - represent any numerical unique value.
/// When using the default JAX provided masterdata, it represents
/// the NCBI Gene ID
#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct GeneId {
inner: u32,
}
impl AnnotationId for GeneId {
/// Convert `self` to `u32`
fn as_u32(&self) -> u32 {
self.inner
}
}
impl TryFrom<&str> for GeneId {
type Error = HpoError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(GeneId {
inner: value.parse::<u32>()?,
})
}
}
impl From<u32> for GeneId {
fn from(inner: u32) -> Self {
GeneId { inner }
}
}
impl Display for GeneId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "NCBI-GeneID:{}", self.inner)
}
}
/// A single gene
///
/// A gene has a unique [`GeneId`] and a name (symbol) and is
/// connected to a set of HPO terms
#[derive(Default, Debug, Clone)]
pub struct Gene {
id: GeneId,
name: String,
hpos: HpoGroup,
}
impl Gene {
/// Initializes a new Gene
///
/// This method should rarely, if ever, be used directly. The
/// preferred way to create new genes is through [`Ontology::add_gene`]
/// to ensure that each gene exists only once.
pub fn new(id: GeneId, name: &str) -> Gene {
Gene {
id,
name: name.to_string(),
hpos: HpoGroup::default(),
}
}
/// Initializes a new Gene from `str` values
///
/// This method should rarely, if ever, be used directly. The
/// preferred way to create new genes is through [`Ontology::add_gene`]
/// to ensure that each gene exists only once.
///
/// # Errors
///
/// If the id is not a correct `GeneId`, returns [`HpoError::ParseIntError`]
pub fn from_parts(id: &str, name: &str) -> HpoResult<Gene> {
Ok(Gene {
id: GeneId::try_from(id)?,
name: name.to_string(),
hpos: HpoGroup::default(),
})
}
/// The unique [`GeneId`] of the gene, most likely the NCBI Gene ID
pub fn id(&self) -> &GeneId {
&self.id
}
/// The name of the gene (gene symbol)
pub fn name(&self) -> &str {
&self.name
}
/// The gene symbol (identical to [`Gene::id`])
pub fn symbol(&self) -> &str {
&self.name
}
/// Connect another [HPO term](`crate::HpoTerm`) to the gene
pub fn add_term<I: Into<HpoTermId>>(&mut self, term_id: I) -> bool {
self.hpos.insert(term_id)
}
/// The set of connected HPO terms
pub fn hpo_terms(&self) -> &HpoGroup {
&self.hpos
}
/// Returns a binary representation of the `Gene`
///
/// 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 Gene ID as big-endian `u32` |
/// | 8 | 1 | The length of the Gene Name / Symbol (converted to a u8 vector) as a `u8` |
/// | 9 | n | The Gene name/symbol as u8 vector. If the name has more than 255 bytes, it is trimmed to 255 |
/// | 9 + n | 4 | The number of associated HPO terms as big-endian `u32` |
/// | 13 + n | x * 4 | The HPO Term IDs of the associated terms, each encoded as big-endian `u32` |
///
/// # Examples
///
/// ```
/// use hpo::annotations::Gene;
///
/// let mut gene = Gene::from_parts("123", "FooBar").unwrap();
/// let bytes = gene.as_bytes();
///
/// assert_eq!(bytes.len(), 4 + 4 + 1 + 6 + 4);
/// assert_eq!(bytes[4..8], [0u8, 0u8, 0u8, 123u8]);
/// assert_eq!(bytes[8], 6u8);
/// ```
pub 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 = std::cmp::min(name.len(), 255);
let size = 4 + 4 + 1 + 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 Gene-ID
res.append(&mut self.id.to_be_bytes().to_vec());
// 1 byte for Length of Gene Name (can't be longer than 255 bytes)
// casting is safe, since name_length is < 256
#[allow(clippy::cast_possible_truncation)]
res.push(name_length as u8);
// Gene name/symbol (up to 255 bytes)
for c in name.iter().take(255) {
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 `Gene`
pub fn to_hpo_set<'a>(&self, ontology: &'a Ontology) -> HpoSet<'a> {
HpoSet::new(ontology, self.hpos.clone())
}
}
impl PartialEq for Gene {
fn eq(&self, other: &Gene) -> bool {
self.id == other.id
}
}
impl Eq for Gene {}
impl TryFrom<&[u8]> for Gene {
type Error = HpoError;
/// Returns a [`Gene`] from a bytes vector
///
/// The byte layout for this method is defined in
/// [`Gene::as_bytes`]
///
/// # Examples
///
/// ```
/// use hpo::annotations::{Gene, GeneId};
///
/// let bytes = vec![
/// 0u8, 0u8, 0u8, 19u8, // Total size of Blop
/// 0u8, 0u8, 0u8, 123u8, // ID of the gene => 123
/// 6u8, // Length of gene symbol
/// b'F', b'o', b'o', b'b', b'a', b'r', // Foobar
/// 0u8, 0u8, 0u8, 0u8 // Number of associated HPO Terms => 0
/// ];
/// let gene = Gene::try_from(&bytes[..]).unwrap();
///
/// assert_eq!(gene.name(), "Foobar");
/// assert_eq!(gene.id(), &GeneId::from(123u32));
/// ```
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
// minimum length for a Gene without name and no HPO terms
// This check is important because we're accessing the bytes
// for size and ID directly and don't want to panic
if bytes.len() < 4 + 4 + 1 + 4 {
error!("Too few bytes for a Gene");
return Err(HpoError::ParseBinaryError);
}
let total_len = u32_from_bytes(&bytes[0..]) as usize;
if bytes.len() != total_len {
error!(
"Too few bytes to build gene. Expected {}, received {}",
total_len,
bytes.len()
);
return Err(HpoError::ParseBinaryError);
}
let id = u32_from_bytes(&bytes[4..]);
let name_len = bytes[8] as usize;
// Minimum length considering the name
if bytes.len() < 13 + name_len {
error!("Too few bytes for an Gene (including the name)");
return Err(HpoError::ParseBinaryError);
}
let Ok(name) = String::from_utf8(bytes[9..9 + name_len].to_vec()) else {
error!("Unable to parse the name of the Gene");
return Err(HpoError::ParseBinaryError);
};
let mut gene = Gene::new(id.into(), &name);
let mut idx_terms = 9 + name_len;
let n_terms = u32_from_bytes(&bytes[idx_terms..]);
if bytes.len() < 13 + name_len + n_terms as usize * 4 {
error!(
"Too few bytes in {}. {} terms, but {} bytes",
name,
n_terms,
bytes.len()
);
return Err(HpoError::ParseBinaryError);
}
idx_terms += 4;
for _ in 0..n_terms {
let term_id = HpoTermId::from([
bytes[idx_terms],
bytes[idx_terms + 1],
bytes[idx_terms + 2],
bytes[idx_terms + 3],
]);
idx_terms += 4;
gene.add_term(term_id);
}
if idx_terms == total_len && idx_terms == bytes.len() {
Ok(gene)
} else {
error!(
"The length of the bytes blob did not match: {} vs {}",
total_len, idx_terms
);
Err(HpoError::ParseBinaryError)
}
}
}
impl Hash for Gene {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
/// [`Gene`] Iterator
pub struct GeneIterator<'a> {
ontology: &'a Ontology,
genes: std::collections::hash_set::Iter<'a, GeneId>,
}
impl<'a> GeneIterator<'a> {
/// Initialize a new [`GeneIterator`]
///
/// This method requires the [`Ontology`] as a parameter since
/// the actual [`Gene`] entities are stored in it and not in [`Genes`]
/// itself
pub fn new(genes: &'a Genes, ontology: &'a Ontology) -> Self {
GeneIterator {
genes: genes.iter(),
ontology,
}
}
}
impl<'a> std::iter::Iterator for GeneIterator<'a> {
type Item = &'a Gene;
fn next(&mut self) -> Option<Self::Item> {
match self.genes.next() {
Some(gene_id) => Some(self.ontology.gene(gene_id).unwrap()),
None => None,
}
}
}
impl Debug for GeneIterator<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "GeneIterator")
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn gene_to_binary() {
let mut gene = Gene::from_parts("123", "FooBar").unwrap();
gene.add_term(66u32);
gene.add_term(77u32);
let bin = gene.as_bytes();
assert_eq!(bin.len(), 4 + 4 + 1 + 6 + 4 + 8);
}
#[test]
fn gene_to_and_from_binary() {
let mut gene = Gene::from_parts("123", "FooBar").unwrap();
gene.add_term(66u32);
gene.add_term(77u32);
let bin = gene.as_bytes();
let gene2: Gene = bin[..].try_into().unwrap();
assert_eq!(gene.name(), gene2.name());
assert_eq!(gene.id(), gene2.id());
assert_eq!(gene.hpo_terms().len(), gene2.hpo_terms().len());
for (a, b) in gene.hpo_terms().iter().zip(gene2.hpo_terms().iter()) {
assert_eq!(a, b);
}
}
#[test]
fn gene_with_non_utf8_name() {
let mut gene = Gene::new(123u32.into(), "Foo😀Bar");
gene.add_term(66u32);
gene.add_term(77u32);
let bin = gene.as_bytes();
// the smiley uses 4 bytes, so the name length is 10
assert_eq!(bin.len(), 4 + 4 + 1 + 10 + 4 + 8);
let gene2 = Gene::try_from(&bin[..]).expect("Can't build Gene");
assert_eq!(gene.name(), gene2.name());
assert_eq!(gene.id(), gene2.id());
assert_eq!(gene.hpo_terms().len(), gene2.hpo_terms().len());
for (a, b) in gene.hpo_terms().iter().zip(gene2.hpo_terms().iter()) {
assert_eq!(a, b);
}
}
#[test]
fn gene_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 gene = Gene::new(123u32.into(), name);
gene.add_term(66u32);
gene.add_term(77u32);
let bin = gene.as_bytes();
let gene2 = Gene::try_from(&bin[..]).expect("Can't build Disease");
assert_ne!(gene.name(), gene2.name());
assert_eq!(gene.name()[0..255], gene2.name()[0..255]);
assert_eq!(gene.id(), gene2.id());
assert_eq!(gene.hpo_terms().len(), gene2.hpo_terms().len());
for (a, b) in gene.hpo_terms().iter().zip(gene2.hpo_terms().iter()) {
assert_eq!(a, b);
}
}
#[test]
fn gene_with_wrong_length() {
let mut gene = Gene::new(123u32.into(), "foobar");
gene.add_term(66u32);
gene.add_term(77u32);
let mut bin = gene.as_bytes();
assert!(Gene::try_from(&bin[..12]).is_err());
assert!(Gene::try_from(&bin[..18]).is_err());
assert!(Gene::try_from(&bin[..26]).is_err());
assert!(Gene::try_from(&bin[..27]).is_ok());
bin.push(1);
assert!(Gene::try_from(&bin[..27]).is_ok());
assert!(Gene::try_from(&bin[..28]).is_err());
}
}