logo
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
use std::fmt;
use std::str::FromStr;

use log::debug;
use serde::{Deserialize, Serialize};

use crate::models::{Transcript, Transcripts};
use crate::utils::errors::ReadWriteError;

/// The Status of the start of stop codon of a CDS
///
/// This is used by RefGene and inferred in GTF based
/// on the absence or presence of dedicated Start- & Stop codon records.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum CdsStat {
    None,
    Unknown,
    Incomplete,
    Complete,
}

impl fmt::Display for CdsStat {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                CdsStat::None => "none",
                CdsStat::Unknown => "unk",
                CdsStat::Incomplete => "incmpl",
                CdsStat::Complete => "cmpl",
            }
        )
    }
}

impl FromStr for CdsStat {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "incmpl" => Ok(CdsStat::Incomplete),
            "incompl" => Ok(CdsStat::Incomplete),
            "incomplete" => Ok(CdsStat::Incomplete),
            "cmpl" => Ok(CdsStat::Complete),
            "compl" => Ok(CdsStat::Complete),
            "complete" => Ok(CdsStat::Complete),
            "none" => Ok(CdsStat::None),
            "unk" => Ok(CdsStat::Unknown),
            _ => Err(format!("Invaid CdsStat {}.", s)),
        }
    }
}

/// Indicates the strandness of the transcript
///
/// # Examples
/// ```
/// use std::str::FromStr;
/// use atglib::models::Strand;
///
/// let strand = Strand::from_str("+").unwrap();
/// assert_eq!(strand, Strand::Plus);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum Strand {
    Plus,
    Minus,
    Unknown,
}

impl fmt::Display for Strand {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Strand::Plus => "+",
                Strand::Minus => "-",
                Strand::Unknown => ".",
            }
        )
    }
}

impl Strand {
    pub fn from_string(s: &str) -> Result<Self, String> {
        Strand::from_str(s)
    }
}

impl FromStr for Strand {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "+" => Ok(Strand::Plus),
            "-" => Ok(Strand::Minus),
            "." => Ok(Strand::Unknown),
            _ => Err(format!(
                "invalid strand {}. Strand must be either `+`, `-` or `.`.",
                s
            )),
        }
    }
}

/// Trait to write ['Transcripts'] or ['Transcript'] into a `Writer` instance
pub trait TranscriptWrite {
    fn writeln_single_transcript(&mut self, transcript: &Transcript) -> Result<(), std::io::Error>;

    fn write_single_transcript(&mut self, transcript: &Transcript) -> Result<(), std::io::Error>;

    fn write_transcript_vec(&mut self, transcripts: &[Transcript]) -> Result<(), std::io::Error> {
        debug!("Writing transcripts");
        for transcript in transcripts {
            self.writeln_single_transcript(transcript)?;
        }
        debug!("Finished writing transcripts");
        Ok(())
    }

    fn write_transcripts(&mut self, transcripts: &Transcripts) -> Result<(), std::io::Error> {
        self.write_transcript_vec(transcripts.as_vec())
    }
}

/// Trait for parsing the input into [`Transcripts`]
pub trait TranscriptRead {
    /// Consumes the `Reader` and returns `Transcripts`
    fn transcripts(&mut self) -> Result<Transcripts, ReadWriteError>;
}