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
use md5;
use serde::{Deserialize, Serialize};
use std::{fmt, str::FromStr};

// ? --------------------------------------------------------------------------
// ? Wrapper for Query Sequences
// ? --------------------------------------------------------------------------

#[derive(Debug)]
pub struct QuerySequence {
    pub hash_header: md5::Digest,
    pub full_identifier: &'static str,
    pub sequence: &'static str,
}

impl QuerySequence {
    /// This is the constructor like method for the `QuerySequenceDTO` object.
    #[allow(dead_code)]
    pub fn create(
        header: &'static str,
        sequence: &'static str,
    ) -> QuerySequence {
        let sequence_hash = md5::compute(sequence);

        QuerySequence {
            hash_header: sequence_hash,
            full_identifier: header,
            sequence,
        }
    }
}

// ? --------------------------------------------------------------------------
// ? Wrapper for Blast Builder
// ? --------------------------------------------------------------------------

#[derive(Clone, Debug, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "camelCase")]
pub enum Taxon {
    Fungi,
    Bacteria,
    Eukaryotes,
}

impl FromStr for Taxon {
    type Err = ();

    fn from_str(input: &str) -> Result<Taxon, Self::Err> {
        match input {
            "f" | "Fungi" | "fungi" => Ok(Taxon::Fungi),
            "b" | "Bacteria" | "bacteria" => Ok(Taxon::Bacteria),
            _ => Err(()),
        }
    }
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum Strand {
    Both,
    Plus,
    Minus,
}

impl fmt::Display for Strand {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Strand::Both => write!(f, "both"),
            Strand::Plus => write!(f, "plus"),
            Strand::Minus => write!(f, "minus"),
        }
    }
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BlastBuilder {
    // ? IO related parameters
    pub subject_reads: String,
    pub taxon: Taxon,

    // ? BlastN configuration related parameters
    pub out_format: i8,
    pub max_target_seqs: i32,
    pub perc_identity: i32,
    pub query_cov: i32,
    pub strand: Strand,
    pub e_value: f32,
    pub min_consensus: f32,
}

impl BlastBuilder {
    pub fn default(subject_reads: &str, taxon: Taxon) -> Self {
        BlastBuilder {
            subject_reads: subject_reads.to_string(),
            taxon,
            out_format: 6,
            max_target_seqs: 10,
            perc_identity: 80,
            query_cov: 80,
            strand: Strand::Both,
            e_value: 0.001,
            min_consensus: 0.51,
        }
    }

    pub fn with_max_target_seqs(mut self, max_target_seqs: i32) -> Self {
        self.max_target_seqs = max_target_seqs;
        self
    }

    pub fn with_perc_identity(mut self, perc_identity: i32) -> Self {
        self.perc_identity = perc_identity;
        self
    }

    pub fn with_query_cov(mut self, query_cov: i32) -> Self {
        self.query_cov = query_cov;
        self
    }

    pub fn with_strand(mut self, strand: Strand) -> Self {
        self.strand = strand;
        self
    }

    pub fn with_e_value(mut self, e_value: f32) -> Self {
        self.e_value = e_value;
        self
    }

    pub fn with_min_consensus(mut self, min_consensus: f32) -> Self {
        self.min_consensus = min_consensus;
        self
    }
}