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
//! Variant record samples genotype value allele.
use std::{error, fmt, num, str::FromStr};
use crate::variant::record::samples::series::value::genotype::Phasing;
/// A VCF record genotype value allele.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Allele {
position: Option<usize>,
phasing: Phasing,
}
impl Allele {
/// Creates a variant record samples genotype value allele.
///
/// # Examples
///
/// ```
/// use noodles_vcf::variant::{
/// record::samples::series::value::genotype::Phasing,
/// record_buf::samples::sample::value::genotype::Allele,
/// };
///
/// let allele = Allele::new(Some(0), Phasing::Phased);
/// ```
pub fn new(position: Option<usize>, phasing: Phasing) -> Self {
Self { position, phasing }
}
/// Returns the position of the allele.
///
/// # Examples
///
/// ```
/// use noodles_vcf::variant::{
/// record::samples::series::value::genotype::Phasing,
/// record_buf::samples::sample::value::genotype::Allele,
/// };
///
/// let allele = Allele::new(Some(0), Phasing::Phased);
/// assert_eq!(allele.position(), Some(0));
/// ```
pub fn position(&self) -> Option<usize> {
self.position
}
/// Returns a mutable reference to the position of the allele.
///
/// # Examples
///
/// ```
/// use noodles_vcf::variant::{
/// record::samples::series::value::genotype::Phasing,
/// record_buf::samples::sample::value::genotype::Allele,
/// };
///
/// let mut allele = Allele::new(Some(0), Phasing::Phased);
/// *allele.position_mut() = Some(1);
/// assert_eq!(allele.position(), Some(1));
/// ```
pub fn position_mut(&mut self) -> &mut Option<usize> {
&mut self.position
}
/// Returns the phasing of the allele.
///
/// # Examples
///
/// ```
/// use noodles_vcf::variant::{
/// record::samples::series::value::genotype::Phasing,
/// record_buf::samples::sample::value::genotype::Allele,
/// };
///
/// let allele = Allele::new(Some(0), Phasing::Phased);
/// assert_eq!(allele.phasing(), Phasing::Phased);
/// ```
pub fn phasing(&self) -> Phasing {
self.phasing
}
/// Returns a mutable reference to the phasing of the allele.
///
/// # Examples
///
/// ```
/// use noodles_vcf::variant::{
/// record::samples::series::value::genotype::Phasing,
/// record_buf::samples::sample::value::genotype::Allele,
/// };
///
/// let mut allele = Allele::new(Some(0), Phasing::Phased);
/// *allele.phasing_mut() = Phasing::Unphased;
/// assert_eq!(allele.phasing(), Phasing::Unphased);
/// ```
pub fn phasing_mut(&mut self) -> &mut Phasing {
&mut self.phasing
}
}
/// An error returned when a raw VCF record genotype value allele fails to parse.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
/// The input is empty.
Empty,
/// The position is invalid.
InvalidPosition(num::ParseIntError),
/// The phasing is invalid.
InvalidPhasing,
}
impl error::Error for ParseError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::InvalidPosition(e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => f.write_str("empty input"),
Self::InvalidPosition(_) => f.write_str("invalid position"),
Self::InvalidPhasing => f.write_str("invalid phasing"),
}
}
}
impl FromStr for Allele {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
return Err(ParseError::Empty);
}
let phasing = parse_phasing(&s[..1])?;
let position = parse_position(&s[1..])?;
Ok(Allele::new(position, phasing))
}
}
pub(super) fn parse_phasing(s: &str) -> Result<Phasing, ParseError> {
const PHASED: &str = "|";
const UNPHASED: &str = "/";
match s {
PHASED => Ok(Phasing::Phased),
UNPHASED => Ok(Phasing::Unphased),
_ => Err(ParseError::InvalidPhasing),
}
}
pub(super) fn parse_position(s: &str) -> Result<Option<usize>, ParseError> {
const MISSING: &str = ".";
match s {
MISSING => Ok(None),
_ => s.parse().map(Some).map_err(ParseError::InvalidPosition),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_str() {
assert_eq!("/.".parse(), Ok(Allele::new(None, Phasing::Unphased)));
assert_eq!("/0".parse(), Ok(Allele::new(Some(0), Phasing::Unphased)));
assert_eq!("/13".parse(), Ok(Allele::new(Some(13), Phasing::Unphased)));
assert_eq!("".parse::<Allele>(), Err(ParseError::Empty));
assert!(matches!(
"/ndls".parse::<Allele>(),
Err(ParseError::InvalidPosition(_))
));
assert_eq!(":0".parse::<Allele>(), Err(ParseError::InvalidPhasing));
}
}