annonars 0.31.2

Rust template repository
//! Data structures for (de-)serialization as generated by `prost-build`.

use std::str::FromStr;

pub use crate::pbs::dbsnp::Record;
use noodles_vcf::record::info::field;

impl Record {
    /// Creates a new `Record` from a VCF record and allele number.
    pub fn from_vcf_allele(
        record: &noodles_vcf::record::Record,
        allele_no: usize,
    ) -> Result<Self, anyhow::Error> {
        let chrom = record.chromosome().to_string();
        let pos: usize = record.position().into();
        let pos: i32 = pos.try_into()?;
        let ref_allele = record.reference_bases().to_string();
        let alt_allele = record
            .alternate_bases()
            .get(allele_no)
            .ok_or_else(|| anyhow::anyhow!("no such allele: {}", allele_no))?
            .to_string();
        let rs_id = if let Some(Some(field::Value::Integer(rs))) =
            record.info().get(&field::Key::from_str("RS")?)
        {
            *rs
        } else {
            anyhow::bail!("no rs id in dbSNP record")
        };

        Ok(Record {
            chrom,
            pos,
            ref_allele,
            alt_allele,
            rs_id,
        })
    }
}