#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i8)]
pub enum Strand {
Forward = 1,
Reverse = -1,
}
impl Strand {
#[inline]
pub fn from_i8(val: i8) -> Option<Self> {
match val {
1 => Some(Strand::Forward),
-1 => Some(Strand::Reverse),
_ => None,
}
}
#[inline]
pub fn to_byte(self) -> u8 {
self as i8 as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ManeStatus {
pub mane_select: bool,
pub mane_clinical: bool,
}
impl ManeStatus {
#[inline]
pub fn from_flags_byte(byte: u8) -> Self {
Self {
mane_select: byte & 0x01 != 0,
mane_clinical: byte & 0x02 != 0,
}
}
#[inline]
pub fn to_flags_byte(&self) -> u8 {
let mut flags = 0u8;
if self.mane_select {
flags |= 0x01;
}
if self.mane_clinical {
flags |= 0x02;
}
flags
}
#[inline]
pub fn is_mane(&self) -> bool {
self.mane_select || self.mane_clinical
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Exon {
pub start: u32,
pub end: u32,
}
impl Exon {
#[inline]
pub fn len(&self) -> u32 {
self.end - self.start
}
#[inline]
pub fn is_empty(&self) -> bool {
self.end == self.start
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Transcript {
pub accession: String,
pub gene: String,
pub chrom_digest: [u8; 24],
pub strand: Strand,
pub cds_start: Option<u32>,
pub cds_end: Option<u32>,
pub exons: Vec<Exon>,
pub mane: ManeStatus,
}
impl Transcript {
pub fn transcript_length(&self) -> u32 {
self.exons.iter().map(|e| e.len()).sum()
}
pub fn cds_length(&self) -> u32 {
match (self.cds_start, self.cds_end) {
(Some(cds_s), Some(cds_e)) => self
.exons
.iter()
.filter_map(|exon| {
let s = exon.start.max(cds_s);
let e = exon.end.min(cds_e);
if s < e {
Some(e - s)
} else {
None
}
})
.sum(),
_ => 0,
}
}
#[inline]
pub fn is_coding(&self) -> bool {
self.cds_start.is_some() && self.cds_end.is_some()
}
pub fn accession_base(&self) -> &str {
self.accession.split('.').next().unwrap_or(&self.accession)
}
}