-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
139 lines (116 loc) · 4.49 KB
/
cli.py
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
from argparse import ArgumentParser, RawDescriptionHelpFormatter
from itertools import islice
import json
import sys
import textwrap
import bonesis
def json_to_bn(args):
str_facts = json.load(sys.stdin)
bn = bonesis.ASPModel_DNF.minibn_of_json_facts(str_facts)
sys.stdout.write(bn.source())
def main_utils():
ap = ArgumentParser()
sap = ap.add_subparsers(help="commands")
p = sap.add_parser("json-to-bn",
help="Convert json facts (stdin) to BooleanNet (stdout)")
p.set_defaults(func=json_to_bn)
args = ap.parse_args()
if not hasattr(args, "func"):
return ap.print_help()
return args.func(args)
def _load_domain(args):
ext = args.input.lower().split(".")[-1]
if ext == "bnet":
dom = bonesis.BooleanNetwork(args.input)
elif ext == "aeon":
from bonesis.aeon import AEONDomain
dom = AEONDomain.from_aeon_file(args.input, canonic=False)
elif ext == "sif":
dom = bonesis.InfluenceGraph.from_sif(args.input, canonic=False,
unsource=False, exact=True)
else:
raise ValueError("Unknon file type for input")
return dom
def _setup_argument_domain(ap):
ap.add_argument("input",
help="file specifying the domain of Boolean networks (supported: .bnet, .sif, .aeon)")
def main_attractors():
ap = ArgumentParser(description=textwrap.dedent("""\
This program lists the attractors (possibly restricted to fixed points) of
the given Boolean network or domain of Boolean networks.
The command line currently supports two types of inputs:
- a single Boolean network, given in BoolNet format (.bnet extension).
In that case, we recommend using mpbn <https://github.com/pauleve/mpbn> instead.
- a domain of Boolean networks specifed with an AEON file (.aeon extension)
In that case, the union of the attractors of all the Boolean networks in
that domain is returned.
"""),
formatter_class=RawDescriptionHelpFormatter
)
_setup_argument_domain(ap)
ap.add_argument("--fixpoints-only", action="store_true",
help="Enumerate only fixed points")
ap.add_argument("--limit", type=int,
help="Maximum number of solutions")
args = ap.parse_args()
bonesis.settings["quiet"] = True
dom = _load_domain(args)
bo = bonesis.BoNesis(dom)
x = bo.cfg() if args.fixpoints_only else bo.hypercube()
bo.fixed(x)
it = x.assignments()
if args.limit:
it = islice(it, args.limit)
publish = print
for sol in it:
publish(sol)
def main_reprogramming():
ap = ArgumentParser()
_setup_argument_domain(ap)
ap.add_argument("marker",
help="Marker specification (partial configuration) - JSON format")
ap.add_argument("max_size", type=int,
help="Maximum number of perturbation")
ap.add_argument("--reachable-from",
help="Initial configuration for source-marker reprogramming - JSON format")
ap.add_argument("--fixpoints", action="store_true",
help="Reprogram fixed points only")
ap.add_argument("--allow-no-fixpoint", action="store_true",
help="When reprogramming fixed points, allow having no fixed points")
ap.add_argument("--exclude",
help="Perturbation blacklist - JSON format")
ap.add_argument("--limit", type=int,
help="Maximum number of solutions")
ap.add_argument("--verbose", action="store_true")
ap.add_argument("--parallel", "-t", default=1, help="Parallel solving")
args = ap.parse_args()
bonesis.settings["quiet"] = not args.verbose
bonesis.settings["parallel"] = args.parallel
dom = _load_domain(args)
M = json.loads(args.marker)
k = args.max_size
meth_prefix = ""
meth_suffix = ""
meth_args = (dom, M, k)
meth_kwargs = {}
if args.exclude:
meth_kwargs["exclude"] = json.loads(args.exclude)
if args.reachable_from:
z = json.loads(args.reachable_from)
meth_prefix = "source_"
meth_args = (dom, z, M, k)
if args.fixpoints:
meth_suffix = "_fixpoints"
meth_kwargs["at_least_one"] = not args.allow_no_fixpoint
from bonesis import reprogramming
meth = f"{meth_prefix}marker_reprogramming{meth_suffix}"
meth = getattr(reprogramming, meth)
it = meth(*meth_args, **meth_kwargs)
has_one = False
if args.limit:
it = islice(it, args.limit)
for sol in it:
has_one = True
print(sol)
if not has_one:
print("No solution", file=sys.stderr)