Skip to content

Commit

Permalink
Update render_network_bokeh to use Scatter instead of Square + other …
Browse files Browse the repository at this point in the history
…small fixes (#281)

* change some docstrings using math to use raw strings

* ignore files created by Emacs

* update networkx plots to not use Square + unit test

* update specialized tutorials to run with latest versions
  • Loading branch information
murrayrm authored Aug 27, 2024
1 parent 33c66a6 commit 8bac636
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 250 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Tests/bioscrape_test.xml
Tests/dcas9_repression_test.xml
Tests/sbml_test_file.xml


# Sphinx documentation
docs/build/

Expand Down Expand Up @@ -74,3 +73,6 @@ docs/build/
### BioCRNPlyer
# Ignore xml files generated by this toolbox
*.xml

### Emacs backup files
*~
51 changes: 41 additions & 10 deletions Tests/Unit/test_plotting.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@

# Copyright (c) 2020, Build-A-Cell. All rights reserved.
# See LICENSE file in the project root directory for details.

from biocrnpyler.dna_construct import RNA_construct
import copy
import re
import warnings

import pytest
#from unittest import TestCase

import biocrnpyler as bcp
from biocrnpyler import CRNPlotter, DNA_construct, Promoter,IntegraseSite,\
RBS,CDS,Terminator, Complex, Species, DNA_part, RegulatedPromoter
import copy
RBS,CDS,Terminator, Complex, Species, DNA_part, RegulatedPromoter
from biocrnpyler.dna_construct import RNA_construct

def test_CRNPlotter():
class dummy_renderer:
Expand All @@ -25,7 +28,7 @@ def renderDNA(self,ax,designs,renderers,**keywords):
ax.plot([0],[1])

return(0,100)

d_r = dummy_renderer()
test_plotter = CRNPlotter(dna_renderer=dummy_renderer(),rna_renderer=dummy_renderer(),cmap=[0,1,2,3,4,5,6])

Expand Down Expand Up @@ -77,7 +80,7 @@ def renderDNA(self,ax,designs,renderers,**keywords):
boundspec = test_plotter.make_dpl_from_species(Species("ooga",material_type="booga")) #make a species to be bound to the promoter
revpart = test_plotter.part_dpl_dict[RegulatedPromoter("p1",regulators=["r1","r2"])].get_directed("reverse",bound=[boundspec]) #return reverse promoter with things bound
assert(revpart.get_dpl()[0]["type"]=="Operator") #reverse promoter has operators on the left


test_plotter.colordict = {"Promoter":"purple"}
prom_dpl = test_plotter.make_dpl_from_part(Promoter("pconst")) #make a promoter part
Expand All @@ -94,7 +97,7 @@ def renderDNA(self,ax,designs,renderers,**keywords):
test_construct4 = DNA_construct([RegulatedPromoter("p1",regulators=["r1","r2"]),DNA_part("test")])
test_plotter.make_dpl_from_species(test_construct4.get_species())
assert(test_construct4.get_species() in test_plotter.species_dpl_dict) #construct is in the dictionary

rna_construct = RNA_construct([RBS("ooga"),CDS("booga")])
condpl1 = test_plotter.make_dpls_from_construct(rna_construct)
assert(condpl1.material_type == "rna") #material type is set correctly
Expand All @@ -103,7 +106,7 @@ def renderDNA(self,ax,designs,renderers,**keywords):
condpl2 = test_plotter.make_dpls_from_construct(rna_construct2)
assert(condpl2==condpl1) #this construct is already in the dictionary! Just return it



bound_construct = Complex([test_construct4.get_species()[1],Species("integrase",material_type="protein")]).parent
#making a construct that is bound to something
Expand All @@ -118,5 +121,33 @@ def renderDNA(self,ax,designs,renderers,**keywords):
#make sure the part inside the construct made it into the species dict
assert(Species("mydna",material_type="dna") in test_plotter.species_dpl_dict)



def test_render_network_bokeh():
# Create a simple reaction
parameters = {
(None, None, "ktx"): 0.05,
(None, None, "kb"): 100,
(None, None, "ku"): 10,
(None, None, "ktl"): 0.05,
("rna_degredation_mm", None, "kdeg"): 0.001,
(None, None, "cooperativity"): 2}
txtl = bcp.TxTlExtract('mixture1', parameters=parameters)
dna = bcp.DNAassembly(
'mydna', promoter=bcp.RegulatedPromoter('plac', ['laci']),
rbs='UTR1', protein='GFP', initial_concentration=10)
txtl.add_component(dna)
crn1 = txtl.compile_crn()
crn1.add_reactions([
bcp.Reaction.from_massaction(
[bcp.Species('mydna', material_type='rna')], [], k_forward=0.1)])

# Generate a graph and make sure it is created
with warnings.catch_warnings(record=True) as records:
plot = bcp.render_network_bokeh(crn1)

for w in records:
if re.search("plotting disabled", w.message.args[0]):
pytest.fail("plotting library import error")
else:
warnings.showwarning(
w.message.args[0], w.category, w.filename, w.lineno)
2 changes: 1 addition & 1 deletion biocrnpyler/chemical_reaction_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


class ChemicalReactionNetwork(object):
"""A chemical reaction network is a container of species and reactions
r"""A chemical reaction network is a container of species and reactions
chemical reaction networks can be compiled into SBML.
reaction types:
Expand Down
33 changes: 18 additions & 15 deletions biocrnpyler/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@
PLOT_NETWORK = False
try:
import networkx as nx
from bokeh.models import (BoxSelectTool, Circle, EdgesAndLinkedNodes,
from bokeh.models import (BoxSelectTool, EdgesAndLinkedNodes,
HoverTool, MultiLine, NodesAndLinkedEdges,
PanTool, Plot, Range1d, TapTool,
PanTool, Plot, Range1d, Scatter, TapTool,
WheelZoomTool)
from bokeh.models.markers import Square
from bokeh.plotting import from_networkx
from bokeh.palettes import Spectral4
from bokeh.io import export_svgs, output_notebook
from fa2 import ForceAtlas2
from fa2_modified import ForceAtlas2
PLOT_NETWORK = True
except ModuleNotFoundError:
pass
Expand Down Expand Up @@ -184,8 +183,9 @@ def graphPlot(DG,DGspecies,DGreactions,plot,layout="force",positions=None,plot_s
ybounds[1] += max_glyph

# edges
edges_renderer.node_renderer.glyph = Circle(
size=species_glyph_size, line_alpha=0, fill_alpha=0, fill_color="color")
edges_renderer.node_renderer.glyph = Scatter(
marker="circle", size=species_glyph_size, line_alpha=0,
fill_alpha=0, fill_color="color")
edges_renderer.edge_renderer.glyph = MultiLine(
line_alpha=0.2, line_width=4, line_join="round", line_color="color")
edges_renderer.edge_renderer.selection_glyph = MultiLine(
Expand Down Expand Up @@ -217,17 +217,20 @@ def graphPlot(DG,DGspecies,DGreactions,plot,layout="force",positions=None,plot_s
plot.y_range = Range1d(ylim[0], ylim[1])

# reactions
reaction_renderer.node_renderer.glyph = Square(
size=reaction_glyph_size, fill_color="color")
reaction_renderer.node_renderer.selection_glyph = Square(
size=reaction_glyph_size, fill_color=Spectral4[2])
reaction_renderer.node_renderer.hover_glyph = Square(
size=reaction_glyph_size, fill_color=Spectral4[1])
reaction_renderer.node_renderer.glyph = Scatter(
marker="square", size=reaction_glyph_size, fill_color="color")
reaction_renderer.node_renderer.selection_glyph = Scatter(
marker="square", size=reaction_glyph_size, fill_color=Spectral4[2])
reaction_renderer.node_renderer.hover_glyph = Scatter(
marker="square", size=reaction_glyph_size, fill_color=Spectral4[1])

# nodes
species_renderer.node_renderer.glyph = Circle(size=12, fill_color="color")
species_renderer.node_renderer.selection_glyph = Circle(size=15, fill_color=Spectral4[2])
species_renderer.node_renderer.hover_glyph = Circle(size=15, fill_color=Spectral4[1])
species_renderer.node_renderer.glyph = Scatter(
marker="circle", size=12, fill_color="color")
species_renderer.node_renderer.selection_glyph = Scatter(
marker="circle", size=15, fill_color=Spectral4[2])
species_renderer.node_renderer.hover_glyph = Scatter(
marker="circle", size=15, fill_color=Spectral4[1])

#this part adds the interactive elements that make it so that the lines are highlighted
#when you mouse over and click
Expand Down
2 changes: 1 addition & 1 deletion biocrnpyler/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


class Reaction(object):
"""An abstract representation of a chemical reaction in a CRN.
r"""An abstract representation of a chemical reaction in a CRN.
A reaction has the form:
.. math::
Expand Down
Loading

0 comments on commit 8bac636

Please sign in to comment.