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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use crate::symbolic_async_graph::SymbolicContext;
use crate::{
BooleanNetwork, FnUpdate, Monotonicity, Parameter, ParameterId, ParameterIdIterator,
RegulatoryGraph, Variable, VariableId, VariableIdIterator, ID_REGEX,
};
use biodivine_lib_bdd::bdd;
use std::collections::HashMap;
use std::ops::Index;
impl BooleanNetwork {
pub fn new(graph: RegulatoryGraph) -> BooleanNetwork {
BooleanNetwork {
update_functions: vec![None; graph.num_vars()],
graph,
parameters: Vec::new(),
parameter_to_index: HashMap::new(),
}
}
pub fn add_parameter(&mut self, name: &str, arity: u32) -> Result<ParameterId, String> {
self.assert_no_such_variable(name)?;
self.assert_no_such_parameter(name)?;
let id = ParameterId(self.parameters.len());
self.parameter_to_index.insert(name.to_string(), id);
self.parameters.push(Parameter::new(name, arity));
Ok(id)
}
pub fn add_update_function(
&mut self,
variable: VariableId,
function: FnUpdate,
) -> Result<(), String> {
self.assert_no_update_function(variable)?;
self.assert_arguments_are_valid(variable, function.collect_arguments())?;
self.update_functions[variable.0] = Some(function);
Ok(())
}
pub fn set_update_function(
&mut self,
variable: VariableId,
function: Option<FnUpdate>,
) -> Result<(), String> {
if let Some(function) = function.as_ref() {
self.assert_arguments_are_valid(variable, function.collect_arguments())?;
}
self.update_functions[variable.0] = function;
Ok(())
}
fn assert_no_such_variable(&self, name: &str) -> Result<(), String> {
if self.graph.find_variable(name) == None {
Ok(())
} else {
Err(format!(
"Cannot add parameter. '{}' is already a variable.",
name
))
}
}
fn assert_no_such_parameter(&self, name: &str) -> Result<(), String> {
if self.find_parameter(name) == None {
Ok(())
} else {
Err(format!("Cannot add parameter. '{}' already added.", name))
}
}
fn assert_no_update_function(&self, variable: VariableId) -> Result<(), String> {
return if self.update_functions[variable.0] == None {
Ok(())
} else {
Err(format!(
"Cannot set update function for {}. Function already set.",
self.graph.get_variable(variable)
))
};
}
fn assert_arguments_are_valid(
&self,
variable: VariableId,
actual: Vec<VariableId>,
) -> Result<(), String> {
let expected = self.graph.regulators(variable);
let mut i_expected = 0;
let mut i_actual = 0;
while i_expected < expected.len() && i_actual < actual.len() {
if expected[i_expected] == actual[i_actual] {
i_actual += 1;
}
i_expected += 1;
}
return if i_actual == actual.len() {
Ok(())
} else {
let expected_names: Vec<String> = expected
.into_iter()
.map(|v| self.graph.get_variable(v).name.clone())
.collect();
let actual_names: Vec<String> = actual
.into_iter()
.map(|v| self.graph.get_variable(v).name.clone())
.collect();
let var_name = self.graph.get_variable(variable);
Err(format!(
"Variable '{}' is regulated by {:?}, but {:?} were found as arguments",
var_name, expected_names, actual_names
))
};
}
}
impl BooleanNetwork {
pub fn as_graph(&self) -> &RegulatoryGraph {
&self.graph
}
pub fn as_graph_mut(&mut self) -> &mut RegulatoryGraph {
&mut self.graph
}
pub fn num_vars(&self) -> usize {
self.graph.num_vars()
}
pub fn num_parameters(&self) -> usize {
self.parameters.len()
}
pub fn num_implicit_parameters(&self) -> usize {
self.update_functions
.iter()
.filter(|it| it.is_none())
.count()
}
pub fn variables(&self) -> VariableIdIterator {
self.graph.variables()
}
pub fn get_variable(&self, id: VariableId) -> &Variable {
self.graph.get_variable(id)
}
pub fn get_variable_name(&self, id: VariableId) -> &String {
self.graph.get_variable_name(id)
}
pub fn regulators(&self, target: VariableId) -> Vec<VariableId> {
self.graph.regulators(target)
}
pub fn targets(&self, regulator: VariableId) -> Vec<VariableId> {
self.graph.targets(regulator)
}
pub fn find_parameter(&self, name: &str) -> Option<ParameterId> {
self.parameter_to_index.get(name).cloned()
}
pub fn get_parameter(&self, id: ParameterId) -> &Parameter {
&self.parameters[id.0]
}
pub fn get_update_function(&self, variable: VariableId) -> &Option<FnUpdate> {
&self.update_functions[variable.0]
}
pub fn parameters(&self) -> ParameterIdIterator {
(0..self.parameters.len()).map(ParameterId)
}
pub fn implicit_parameters(&self) -> Vec<VariableId> {
(0..self.update_functions.len())
.filter(|it| self.update_functions[*it].is_none())
.map(VariableId)
.collect()
}
pub fn is_valid_name(name: &str) -> bool {
ID_REGEX.is_match(name)
}
}
impl BooleanNetwork {
pub fn infer_valid_graph(&self) -> Result<BooleanNetwork, String> {
let ctx = SymbolicContext::new(self)?;
let var_names = self
.variables()
.map(|id| self.get_variable_name(id))
.cloned()
.collect::<Vec<_>>();
let mut new_rg = RegulatoryGraph::new(var_names);
for target_var in self.variables() {
let target_name = self.get_variable_name(target_var);
if let Some(function) = self.get_update_function(target_var) {
let fn_is_true = ctx.mk_fn_update_true(function);
let fn_is_false = fn_is_true.not();
for regulator_var in self.as_graph().regulators(target_var) {
let regulator_name = self.get_variable_name(regulator_var);
let regulator = ctx.state_variables()[regulator_var.0];
let regulator_is_true = ctx.bdd_variable_set().mk_var(regulator);
let regulator_is_false = ctx.bdd_variable_set().mk_not_var(regulator);
let observability = {
let fn_x1_to_1 =
bdd!(fn_is_true & regulator_is_true).var_project(regulator);
let fn_x0_to_1 =
bdd!(fn_is_true & regulator_is_false).var_project(regulator);
bdd!(fn_x1_to_1 ^ fn_x0_to_1).project(ctx.state_variables())
};
if !observability.is_false() {
let activation = {
let fn_x1_to_0 =
bdd!(fn_is_false & regulator_is_true).var_project(regulator);
let fn_x0_to_1 =
bdd!(fn_is_true & regulator_is_false).var_project(regulator);
bdd!(fn_x0_to_1 & fn_x1_to_0).project(ctx.state_variables())
}
.not();
let inhibition = {
let fn_x0_to_0 =
bdd!(fn_is_false & regulator_is_false).var_project(regulator);
let fn_x1_to_1 =
bdd!(fn_is_true & regulator_is_true).var_project(regulator);
bdd!(fn_x0_to_0 & fn_x1_to_1).project(ctx.state_variables())
}
.not();
let monotonicity = match (activation.is_false(), inhibition.is_false()) {
(false, true) => Some(Monotonicity::Activation),
(true, false) => Some(Monotonicity::Inhibition),
_ => None,
};
new_rg
.add_regulation(regulator_name, target_name, true, monotonicity)
.unwrap();
}
}
} else {
for regulator in self.as_graph().regulators(target_var) {
let regulator_name = self.get_variable_name(regulator);
new_rg
.add_regulation(regulator_name, target_name, false, None)
.unwrap();
}
}
}
let mut new_bn = BooleanNetwork::new(new_rg);
for var in self.variables() {
let name = self.get_variable_name(var);
if let Some(update) = self.get_update_function(var) {
let fn_bdd = ctx.mk_fn_update_true(update);
let fn_string = fn_bdd
.to_boolean_expression(ctx.bdd_variable_set())
.to_string();
new_bn.add_string_update_function(name, &fn_string).unwrap();
}
}
Ok(new_bn)
}
}
impl Index<VariableId> for BooleanNetwork {
type Output = Variable;
fn index(&self, index: VariableId) -> &Self::Output {
self.graph.get_variable(index)
}
}
impl Index<ParameterId> for BooleanNetwork {
type Output = Parameter;
fn index(&self, index: ParameterId) -> &Self::Output {
&self.parameters[index.0]
}
}
#[cfg(test)]
mod test {
use crate::BooleanNetwork;
use std::convert::TryFrom;
#[test]
fn test_rg_inference() {
let bn = BooleanNetwork::try_from_bnet(
r"
A, B | !C
B, B & !(A | C)
C, (A | !A) & (C <=> B)
",
)
.unwrap();
let expected = BooleanNetwork::try_from(
r"
B -> A
C -| A
A -| B
B -> B
C -| B
B -? C
C -? C
",
)
.unwrap();
let inferred = bn.infer_valid_graph().unwrap();
assert_eq!(expected.as_graph(), inferred.as_graph());
}
}