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
use crate::_aeon_parser::{FnUpdateTemp, RegulationTemp};
use crate::{BooleanNetwork, Parameter, RegulatoryGraph};
use regex::Regex;
use std::collections::HashSet;
use std::convert::TryFrom;
impl TryFrom<&str> for BooleanNetwork {
type Error = String;
fn try_from(value: &str) -> Result<Self, Self::Error> {
let lines = value.lines().filter_map(|l| {
let line = l.trim();
if line.is_empty() || line.starts_with('#') {
None
} else {
Some(line)
}
});
let mut update_functions = Vec::new();
let mut regulations = Vec::new();
let function_re =
Regex::new(r"^\$\s*(?P<name>[a-zA-Z0-9_]+)\s*:\s*(?P<function>.+)$").unwrap();
for line in lines {
if let Some(captures) = function_re.captures(line.trim()) {
update_functions.push((
captures["name"].to_string(),
FnUpdateTemp::try_from(&captures["function"])?,
));
} else {
regulations.push(RegulationTemp::try_from(line)?);
}
}
let mut variable_names = HashSet::new();
for reg in ®ulations {
variable_names.insert(reg.regulator.clone());
variable_names.insert(reg.target.clone());
}
let mut variable_names: Vec<String> = variable_names.into_iter().collect();
variable_names.sort();
let mut rg = RegulatoryGraph::new(variable_names);
for reg in regulations {
rg.add_temp_regulation(reg)?;
}
let update_functions: Vec<(String, FnUpdateTemp)> = update_functions
.into_iter()
.map(|(name, fun)| (name, *fun.unknown_variables_to_parameters(&rg)))
.collect();
let mut bn = BooleanNetwork::new(rg);
let mut parameters = HashSet::new();
for (_, fun) in &update_functions {
fun.dump_parameters(&mut parameters);
}
let mut parameters: Vec<Parameter> = parameters.into_iter().collect();
parameters.sort_by_key(|p| p.name.clone());
for parameter in ¶meters {
bn.add_parameter(¶meter.name, parameter.arity)?;
}
for (name, function) in update_functions {
bn.add_template_update_function(&name, function)?;
}
Ok(bn)
}
}
#[cfg(test)]
mod tests {
use crate::biodivine_std::structs::build_index_map;
use crate::BinaryOp::{And, Iff, Imp, Or, Xor};
use crate::{BooleanNetwork, FnUpdate, Parameter, ParameterId, RegulatoryGraph, VariableId};
use std::convert::TryFrom;
#[test]
fn test_boolean_network_parser() {
let bn_string = "
a -> b
a -?? a
b -|? c
a ->? c
# Also some comments are allowed
c -? a
c -| d
$a: a & (p(c) => (c | c))
$b: p(a) <=> q(a, a)
# Notice that a regulates c but does not appear in the function!
$c: q(b, b) => !(b ^ k)
";
let f1 = FnUpdate::Binary(
And,
Box::new(FnUpdate::Var(VariableId(0))),
Box::new(FnUpdate::Binary(
Imp,
Box::new(FnUpdate::Param(ParameterId(1), vec![VariableId(2)])),
Box::new(FnUpdate::Binary(
Or,
Box::new(FnUpdate::Var(VariableId(2))),
Box::new(FnUpdate::Var(VariableId(2))),
)),
)),
);
let f2 = FnUpdate::Binary(
Iff,
Box::new(FnUpdate::Param(ParameterId(1), vec![VariableId(0)])),
Box::new(FnUpdate::Param(
ParameterId(2),
vec![VariableId(0), VariableId(0)],
)),
);
let f3 = FnUpdate::Binary(
Imp,
Box::new(FnUpdate::Param(
ParameterId(2),
vec![VariableId(1), VariableId(1)],
)),
Box::new(FnUpdate::Not(Box::new(FnUpdate::Binary(
Xor,
Box::new(FnUpdate::Var(VariableId(1))),
Box::new(FnUpdate::Param(ParameterId(0), Vec::new())),
)))),
);
let mut rg = RegulatoryGraph::new(vec![
"a".to_string(),
"b".to_string(),
"c".to_string(),
"d".to_string(),
]);
rg.add_string_regulation("a -> b").unwrap();
rg.add_string_regulation("a -?? a").unwrap();
rg.add_string_regulation("b -|? c").unwrap();
rg.add_string_regulation("a ->? c").unwrap();
rg.add_string_regulation("c -? a").unwrap();
rg.add_string_regulation("c -| d").unwrap();
let parameters = vec![
Parameter {
name: "k".to_string(),
arity: 0,
},
Parameter {
name: "p".to_string(),
arity: 1,
},
Parameter {
name: "q".to_string(),
arity: 2,
},
];
let bn = BooleanNetwork {
graph: rg,
parameter_to_index: build_index_map(
¶meters
.iter()
.map(|p| p.name.clone())
.collect::<Vec<_>>(),
|_, i| ParameterId(i),
),
parameters,
update_functions: vec![Some(f1), Some(f2), Some(f3), None],
};
assert_eq!(bn, BooleanNetwork::try_from(bn_string).unwrap());
}
#[test]
fn test_bn_from_and_to_string() {
let bn_string = "a -> b
a -?? a
b -|? c
c -? a
c -> d
$a: (a & (p(c) => (c | c)))
$b: (p(a) <=> q(a, a))
$c: (q(b, b) => !(b ^ k))
";
assert_eq!(
bn_string,
BooleanNetwork::try_from(bn_string).unwrap().to_string()
);
}
}