Building a Boolean-Based Model¶
This simple example demonstrates how to create a boolean-based model, its components, regulators, conditions and sub-conditions into Cell Collective.
We’ll attempt to reconstruct the Cortical Area Development authored by CE Giacomantonio.
Begin by importing the ccapi module into your workspace.
[1]:
import ccapi
Now, let’s try creating a client object in order to interact with services provided by Cell Collective.
[2]:
client = ccapi.Client()
Authenticate your client using a *password flow type authentication* scheme.
NOTE: Before you can authenticate using ccapi, you must first register an application of the appropriate type on `Cell Collective <https://cellcollective.org>`__. If you do not require a user context, it is read only.
[3]:
client.auth(email = "test@cellcollective.org", password = "test")
Creating a Base Model¶
Create a Base Model using ccapi and instantize it with an authenticated client.
[4]:
model = ccapi.Model("Cortical Area Development", client = client)
model.save()
[4]:
ID | 163454 |
Name | Cortical Area Development |
Memory Address | 0x010d4ee490 |
Number of Versions | 1 |
Versions | [<BooleanModel 163454 version 1 at 0x010d4ee650>] |
A ccapi.Model
consists of various ccapi.ModelVersion
objects that help you build various versions to a model. By default, a ccapi.Model
provides you a default model version of a boolean type.
[5]:
# get the default model version
boolean = model.versions[0]
boolean.name = "Version 1"
boolean
[5]:
ID | 163454 |
Version | 1 |
Name | Version 1 |
Memory Address | 0x010d4ee650 |
Number of Components | 0 |
Components |
Adding Components to a Boolean-Based Model¶
First, we need to create a list of component objects for this model.
[6]:
# create components
COUP_TFI = ccapi.InternalComponent("COUP-TFI")
EMX2 = ccapi.InternalComponent("EMX2")
FGF8 = ccapi.InternalComponent("FGF8")
PAX6 = ccapi.InternalComponent("PAX6")
Sp8 = ccapi.InternalComponent("Sp8")
Now let us add a list of components to our Boolean Model.
[7]:
# add components to model
boolean.add_components(COUP_TFI, EMX2, FGF8, PAX6, Sp8)
Saving a Model¶
Ensure you save your model in order to commit your work.
[8]:
model.save()
[8]:
ID | 163454 |
Name | Cortical Area Development |
Memory Address | 0x010d4ee490 |
Number of Versions | 1 |
Versions | [<BooleanModel 163454 version 1 at 0x010d4ee650 name='Version 1'>] |
Adding Regulators, Conditions and Sub-Conditions¶
Let’s add a list of regulators and conditions to our components. A list of regulators and conditions as well as sub-conditions can all be added at once to a component.
[9]:
# add regulators to components
COUP_TFI.add_regulators(
ccapi.NegativeRegulator(Sp8),
ccapi.NegativeRegulator(FGF8)
)
EMX2.add_regulators(
ccapi.PositiveRegulator(COUP_TFI),
ccapi.NegativeRegulator(FGF8),
ccapi.NegativeRegulator(PAX6),
ccapi.NegativeRegulator(Sp8)
)
Sp8.add_regulators(
ccapi.PositiveRegulator(FGF8),
ccapi.NegativeRegulator(EMX2)
)
FGF8.add_regulators(
# add conditions to regulators
ccapi.PositiveRegulator(FGF8, conditions = [
ccapi.Condition(components = Sp8)
])
)
PAX6.add_regulators(
ccapi.PositiveRegulator(Sp8),
ccapi.NegativeRegulator(COUP_TFI)
)
[10]:
model.save()
[10]:
ID | 163454 |
Name | Cortical Area Development |
Memory Address | 0x010d4ee490 |
Number of Versions | 1 |
Versions | [<BooleanModel 163454 version 1 at 0x010d4ee650 name='Version 1'>] |
We’ve now got things within our Boolean Model.
[11]:
boolean.components
[11]:
ID | Name | Number of Positive Regulators | Number of Negative Regulators |
---|---|---|---|
3202260 | COUP-TFI | 0 | 2 |
3202258 | EMX2 | 1 | 3 |
3202261 | FGF8 | 1 | 0 |
3202257 | PAX6 | 1 | 1 |
3202259 | Sp8 | 1 | 1 |
[12]:
FGF8.positive_regulators
[12]:
ID | Name | Number of Conditions |
---|---|---|
4136618 | 1 |
Model Summary¶
You can view detailed summary of your model using the summary
function provided.
[13]:
boolean.summary()
Internal Components (+, -) External Components
-------------------------- -------------------
COUP-TFI (0,2)
EMX2 (1,3)
FGF8 (1,0)
PAX6 (1,1)
Sp8 (1,1)
…or view detailed information within your jupyter notebook.
[14]:
boolean
[14]:
ID | 163454 |
Version | 1 |
Name | Version 1 |
Memory Address | 0x010d4ee650 |
Number of Components | 5 |
Components | COUP-TFI, EMX2, FGF8, PAX6, Sp8 |
Model Rendering¶
You can also attempt to visualize a Boolean Model using the draw
function provided.
[15]:
# boolean.draw()
Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/Users/achillesrasquinha/.venv/lib/python3.7/site-packages/pygraphviz/agraph.py", line 44, in run
chunk = self.pipe.read()
File "/Users/achillesrasquinha/.venv/lib/python3.7/site-packages/gevent/_fileobjectposix.py", line 164, in readall
data = self.__read(DEFAULT_BUFFER_SIZE)
File "/Users/achillesrasquinha/.venv/lib/python3.7/site-packages/gevent/_fileobjectposix.py", line 158, in __read
wait_on_watcher(self._read_watcher, None, None, self.hub)
File "src/gevent/_hub_primitives.py", line 326, in gevent._gevent_c_hub_primitives.wait_on_watcher
File "src/gevent/_hub_primitives.py", line 350, in gevent._gevent_c_hub_primitives.wait_on_watcher
File "src/gevent/_hub_primitives.py", line 304, in gevent._gevent_c_hub_primitives._primitive_wait
File "src/gevent/_hub_primitives.py", line 46, in gevent._gevent_c_hub_primitives.WaitOperationsGreenlet.wait
File "src/gevent/_hub_primitives.py", line 46, in gevent._gevent_c_hub_primitives.WaitOperationsGreenlet.wait
File "src/gevent/_hub_primitives.py", line 55, in gevent._gevent_c_hub_primitives.WaitOperationsGreenlet.wait
File "src/gevent/_waiter.py", line 151, in gevent._gevent_c_waiter.Waiter.get
File "src/gevent/_greenlet_primitives.py", line 61, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 61, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 65, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_gevent_c_greenlet_primitives.pxd", line 35, in gevent._gevent_c_greenlet_primitives._greenlet_switch
greenlet.error: cannot switch to a different thread
Exception in thread Thread-5:
Traceback (most recent call last):
File "/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/Users/achillesrasquinha/.venv/lib/python3.7/site-packages/pygraphviz/agraph.py", line 44, in run
chunk = self.pipe.read()
File "/Users/achillesrasquinha/.venv/lib/python3.7/site-packages/gevent/_fileobjectposix.py", line 164, in readall
data = self.__read(DEFAULT_BUFFER_SIZE)
File "/Users/achillesrasquinha/.venv/lib/python3.7/site-packages/gevent/_fileobjectposix.py", line 158, in __read
wait_on_watcher(self._read_watcher, None, None, self.hub)
File "src/gevent/_hub_primitives.py", line 326, in gevent._gevent_c_hub_primitives.wait_on_watcher
File "src/gevent/_hub_primitives.py", line 350, in gevent._gevent_c_hub_primitives.wait_on_watcher
File "src/gevent/_hub_primitives.py", line 304, in gevent._gevent_c_hub_primitives._primitive_wait
File "src/gevent/_hub_primitives.py", line 46, in gevent._gevent_c_hub_primitives.WaitOperationsGreenlet.wait
File "src/gevent/_hub_primitives.py", line 46, in gevent._gevent_c_hub_primitives.WaitOperationsGreenlet.wait
File "src/gevent/_hub_primitives.py", line 55, in gevent._gevent_c_hub_primitives.WaitOperationsGreenlet.wait
File "src/gevent/_waiter.py", line 151, in gevent._gevent_c_waiter.Waiter.get
File "src/gevent/_greenlet_primitives.py", line 61, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 61, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 65, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_gevent_c_greenlet_primitives.pxd", line 35, in gevent._gevent_c_greenlet_primitives._greenlet_switch
greenlet.error: cannot switch to a different thread
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-61b8d2fd863a> in <module>
----> 1 boolean.draw()
~/dev/dev/hl/ccapi/src/ccapi/model/model/boolean/__init__.py in draw(self, type_, **kwargs)
154 graph.add_nodes_from([c.name for c in self.components])
155
--> 156 layout = graphviz_layout(graph)
157
158 nx.draw_networkx_nodes(graph, layout,
~/.venv/lib/python3.7/site-packages/networkx/drawing/nx_agraph.py in graphviz_layout(G, prog, root, args)
235 This is a wrapper for pygraphviz_layout.
236 """
--> 237 return pygraphviz_layout(G, prog=prog, root=root, args=args)
238
239
~/.venv/lib/python3.7/site-packages/networkx/drawing/nx_agraph.py in pygraphviz_layout(G, prog, root, args)
283 args += f"-Groot={root}"
284 A = to_agraph(G)
--> 285 A.layout(prog=prog, args=args)
286 node_pos = {}
287 for n in G:
~/.venv/lib/python3.7/site-packages/pygraphviz/agraph.py in layout(self, prog, args)
1420 """
1421 fmt = "dot"
-> 1422 data = self._run_prog(prog, " ".join([args, "-T", fmt]))
1423 self.from_string(data)
1424 self.has_layout = True
~/.venv/lib/python3.7/site-packages/pygraphviz/agraph.py in _run_prog(self, prog, args)
1377 t.start()
1378
-> 1379 self.write(child_stdin)
1380 child_stdin.close()
1381
~/.venv/lib/python3.7/site-packages/pygraphviz/agraph.py in write(self, path)
1256 fh = self._get_fh(path, "w")
1257 try:
-> 1258 gv.agwrite(self.handle, fh)
1259 except OSError:
1260 print("IO error writing file")
~/.venv/lib/python3.7/site-packages/pygraphviz/graphviz.py in agwrite(g, file)
86
87 def agwrite(g, file):
---> 88 return _graphviz.agwrite(g, file)
89
90 def agisundirected(g):
TypeError: not a file handle
[ ]: