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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use crate::sketchbook::ids::VarId;
use crate::sketchbook::model::{Essentiality, Monotonicity};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Error, Formatter};

use regex::Regex;

/// **(internal)** A regex string of an identifier which we currently allow to appear.
/// This regex does not enforce beginning/ending as it is used inside of larger regulation
/// regex.
const ID_REGEX_STR: &str = r"[a-zA-Z_][a-zA-Z0-9_]*";

/// **(internal)** Regex which matches the regulation arrow string with `regulation_sign`
/// and `essential` groups.
const REGULATION_ARROW_REGEX_STR: &str = r"-(?P<regulation_sign>[|>?*])(?P<essential>X|\?|)";

lazy_static! {
    /// **(internal)** A regex which reads one line specifying a regulation.
    static ref REGULATION_REGEX: Regex = Regex::new(
        format!(
            // regulator ID, whitespace?, arrow string, whitespace?, target ID
            r"^(?P<regulator>{})\s*{}\s*(?P<target>{})$",
            ID_REGEX_STR,
            REGULATION_ARROW_REGEX_STR,
            ID_REGEX_STR,
        ).as_str()
    ).unwrap();
}

/// Describes an interaction between two variables, `regulator` and `target`.
/// Every regulation can be *monotonous* and can be set as *essential*:
///
/// Monotonicity is `positive`, `negative`, `dual`, or `unknown`. The monotonicity signifies how
/// the presence of the `regulator` affects the value of the `target`:
///  - if the regulation is `positive`, it might only *increase* the `target` value
///  - if the regulation is `negative`, it might only *decrease* the `target` value
///  - if the regulation is `dual`, it might both *increase* or *decrease* the `target` value (in
///    different contexts)
///
/// If essentiality is set to *true*, the `regulator` *must* have influence on the outcome
/// of the `target` update function in *some* context. If set to `False`, this regulation must have
/// no effect. If it is `Unknown`, the essentiality is not enforced (i.e. the `regulator` *can*
/// have an influence on the `target`, but it is not required).
///
/// Regulations can be represented as strings in the
/// form `"regulator_name 'relationship' target_name"`. The 'relationship' starts with `-`, which
/// is followed by `>` for activation (positive monotonicity), `|` for inhibition (negative
/// monotonicity), `*` for dual effect (non-monotonic) or `?` for unspecified monotonicity.
/// Finally, an additional `X`, `?` at the end of 'relationship' signifies that the the regulation
/// is non-essential (non-essential) or the essentiality is unknown, respectively.
/// Together, this gives the following options:  `->, ->?, -|, -|?, -*, -*?, -?, -??`.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Regulation {
    regulator: VarId,
    target: VarId,
    essential: Essentiality,
    regulation_sign: Monotonicity,
}

/// Methods for safely generating new `Regulations`.
impl Regulation {
    /// Create new `Regulation` given all the components.
    pub fn new(
        regulator: VarId,
        target: VarId,
        essential: Essentiality,
        regulation_sign: Monotonicity,
    ) -> Regulation {
        Regulation {
            regulator,
            target,
            essential,
            regulation_sign,
        }
    }

    /// Try to read the regulation from a given string in the standard format.
    /// Returns error if the string is invalid.
    pub fn try_from_string(regulation_str: &str) -> Result<Regulation, String> {
        let (regulator, regulation_sign, essential, target) =
            Regulation::try_components_from_string(regulation_str)?;

        Ok(Regulation {
            regulator: VarId::new(regulator.as_str())?,
            target: VarId::new(target.as_str())?,
            regulation_sign,
            essential,
        })
    }

    /// Try to read all available information about a regulation from a given string
    /// in the standard format.
    ///
    /// The returned data correspond to the items as they appear in the string, i.e. `regulator`,
    /// `regulation_sign`, `essentiality` and `target`. If the string is not valid, returns `None`.
    pub fn try_components_from_string(
        regulation_str: &str,
    ) -> Result<(String, Monotonicity, Essentiality, String), String> {
        REGULATION_REGEX
            .captures(regulation_str.trim())
            .map(|captures| {
                let regulation_sign = match &captures["regulation_sign"] {
                    "|" => Monotonicity::Inhibition,
                    ">" => Monotonicity::Activation,
                    "*" => Monotonicity::Dual,
                    "?" => Monotonicity::Unknown,
                    _ => unreachable!("Nothing else matches this group."),
                };
                let essential = match &captures["essential"] {
                    "" => Essentiality::True,
                    "X" => Essentiality::False,
                    "?" => Essentiality::Unknown,
                    _ => unreachable!("Nothing else matches this group."),
                };
                (
                    captures["regulator"].to_string(),
                    regulation_sign,
                    essential,
                    captures["target"].to_string(),
                )
            })
            .ok_or(format!("Regulation string is invalid: {regulation_str}"))
    }
}

/// Basic getters and other non-modifying methods.
impl Regulation {
    /// Check if the regulation is marked as essential.
    ///
    /// Note that both negative or unknown essentiality results in `false`.
    pub fn is_essential(&self) -> bool {
        self.essential == Essentiality::True
    }

    /// Get the essentiality of the regulation.
    pub fn get_essentiality(&self) -> &Essentiality {
        &self.essential
    }

    /// Get the sign of the regulation.
    pub fn get_sign(&self) -> &Monotonicity {
        &self.regulation_sign
    }

    /// Get the `VarId` of the regulator.
    pub fn get_regulator(&self) -> &VarId {
        &self.regulator
    }

    /// Get the `VarId` of the target.
    pub fn get_target(&self) -> &VarId {
        &self.target
    }
}

/// Methods for editing `Regulations`.
impl Regulation {
    /// Directly swap original regulator with a given one.
    pub fn swap_regulator(&mut self, new_regulator: VarId) {
        self.regulator = new_regulator;
    }

    /// Directly swap original target with a given one.
    pub fn swap_target(&mut self, new_target: VarId) {
        self.target = new_target;
    }

    /// Directly swap original sign with a given one.
    pub fn swap_sign(&mut self, new_sign: Monotonicity) {
        self.regulation_sign = new_sign;
    }

    /// Directly swap original essentiality with a given one.
    pub fn swap_essentiality(&mut self, new_essentiality: Essentiality) {
        self.essential = new_essentiality;
    }
}

impl Display for Regulation {
    /// Standard format that can be parsed back.
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        let regulation_sign = self.get_sign().to_string();
        let essentiality = match self.get_essentiality() {
            Essentiality::True => "",
            Essentiality::False => "X",
            Essentiality::Unknown => "?",
        };

        write!(
            f,
            "{} -{}{} {}",
            self.regulator, regulation_sign, essentiality, self.target
        )
    }
}

#[cfg(test)]
mod tests {
    use crate::sketchbook::model::{Essentiality, Monotonicity, Regulation};

    #[test]
    fn regulation_conversion() {
        let regulation_strings = vec![
            "a -?? b", "b -? c", "c ->? d", "d -> e", "e -|? f", "f -| g", "g -*? h", "h -* i",
        ];

        let regulators = vec!["a", "b", "c", "d", "e", "f", "g", "h"];
        let targets = vec!["b", "c", "d", "e", "f", "g", "h", "i"];
        let essentiality = vec![
            Essentiality::Unknown,
            Essentiality::True,
            Essentiality::Unknown,
            Essentiality::True,
            Essentiality::Unknown,
            Essentiality::True,
            Essentiality::Unknown,
            Essentiality::True,
        ];
        let regulation_sign = vec![
            Monotonicity::Unknown,
            Monotonicity::Unknown,
            Monotonicity::Activation,
            Monotonicity::Activation,
            Monotonicity::Inhibition,
            Monotonicity::Inhibition,
            Monotonicity::Dual,
            Monotonicity::Dual,
        ];

        for i in 0..regulation_strings.len() {
            let regulation = Regulation::try_from_string(regulation_strings[i.clone()]).unwrap();
            assert_eq!(regulation.to_string().as_str(), regulation_strings[i]);

            assert_eq!(regulation.regulator.as_str(), regulators[i.clone()]);
            assert_eq!(regulation.target.as_str(), targets[i.clone()]);
            assert_eq!(regulation.regulation_sign, regulation_sign[i.clone()]);
            assert_eq!(regulation.essential, essentiality[i.clone()]);
        }

        assert!(Regulation::try_from_string("a --> b").is_err());
        assert!(Regulation::try_from_string("-a -> b").is_err());
        assert!(Regulation::try_from_string("a - b").is_err());
    }
}