Skip to content

Commit

Permalink
Bringing testing branch up to date with main
Browse files Browse the repository at this point in the history
  • Loading branch information
cjarmstrong97 committed Sep 2, 2021
1 parent 811e0f0 commit e8b0d46
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 42 deletions.
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,20 @@ Small bugfix to ensure the model file is opened with UTF-8 encoding which caused
Removing numpy dependency from setup.py, action reading off of BNGL file.

## 0.3.8
Moving imports around, removing unnecessary ones to speed up CLI performance.
Moving imports around, removing unnecessary ones to speed up CLI performance.

## 0.3.9
A couple bugfixes to plotting, running the CLI on a model can now generate a log file with the option
-l/--log. Failing to run now raises a ValueError (will be changed with custom errors in the future). Added some input and output file checks to notebook subcommand.

## 0.4.0
Fixed a but where "0" species was being printed as "0()". Action block is now a list and not a dictionary which was disallowing multiple actions of the same type.

## 0.4.1
Changed `bionetgen.run` behavior when called with a `bngmodel` object. Now the model file is saved and if it exists, it's overwritten with a warning. Slightly better error reporting when the `run` call fails.

## 0.4.2
Changed `bionetgen.run` behavior again, how calling the method with an `out` argument doesn't leave you in the output folder when it's done executing and it will return you back to the folder you started with. Bugfix where parsing a model without actions failed.

## 0.4.3
Bugfix where the libroadrunner simulator object was not handled correctly.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ with open("new_model.bngl", "w") as f:
f.write(str(model)) # writes the changed model to new_model file
# this will give you a libRoadRunner instance of the model
librr_sim = model.setup_simulator()._simulator
librr_sim = model.setup_simulator()
```

More documentation and tutorials are in progress.
You can find more tutorials [here](https://pybionetgen.readthedocs.io/en/latest/tutorials.html).

### Environment Setup

Expand Down
2 changes: 1 addition & 1 deletion bionetgen/assets/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0 3 8 alpha 0
0 4 3 alpha 0
7 changes: 5 additions & 2 deletions bionetgen/core/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ def _set_output(self, output):
os.chdir(output)

def run(self):
import ipdb

ipdb.set_trace()
try:
stdout_loc = getattr(subprocess, self.stdout)
except:
Expand Down Expand Up @@ -198,7 +201,7 @@ def run(self):
# print(rc.stdout.decode('utf-8'))
# if rc.stderr is not None:
# print(rc.stderr.decode('utf-8'))
import ipdb;ipdb.set_trace()
# import ipdb;ipdb.set_trace()
if rc == 0:
# load in the result
self.result = BNGResult(os.getcwd())
Expand All @@ -213,4 +216,4 @@ def run(self):
os.environ["BNGPATH"] = self.old_bngpath
raise ValueError(
"Failed to run your BNGL file, there might be an issue with your model!"
)
)
7 changes: 5 additions & 2 deletions bionetgen/core/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ def _datplot(self):
ymin = self.kwargs.get("ymin", False) or oymin
ymax = self.kwargs.get("ymax", False) or oymax

fax.set_xlim(xmin, xmax)
fax.set_ylim(ymin, ymax)
assert xmax > xmin, "--xmin is bigger than --xmax!"
assert ymax > ymin, "--ymin is bigger than --ymax!"

fax.set_xlim(left=xmin, right=xmax)
fax.set_ylim(bottom=ymin, top=ymax)
# labels and title
_ = plt.xlabel(self.kwargs.get("xlabel") or x_name)
_ = plt.ylabel(self.kwargs.get("ylabel") or "concentration")
Expand Down
36 changes: 34 additions & 2 deletions bionetgen/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from bionetgen.modelapi.utils import run_command
import cement
import subprocess
import subprocess, os
import bionetgen as bng
from cement.core.exc import CaughtSignal
from .core.exc import BioNetGenError
Expand Down Expand Up @@ -74,6 +74,15 @@ class Meta:
"type": str,
},
),
(
["-l", "--log"],
{
"help": "saves BNG2.pl log to a file given (default: None)",
"default": None,
"type": str,
"dest": "log_file",
},
),
],
)
def run(self):
Expand Down Expand Up @@ -132,6 +141,16 @@ def notebook(self):
args = self.app.pargs
if args.input is not None:
# we want to use the template to write a custom notebok
assert args.input.endswith(
".bngl"
), f"File {args.input} doesn't have bngl extension!"
try:
import bionetgen

m = bionetgen.bngmodel(args.input)
str(m)
except:
raise RuntimeError(f"Couldn't import given model: {args.input}!")
notebook = BNGNotebook(
CONFIG["bionetgen"]["notebook"]["template"], INPUT_ARG=args.input
)
Expand All @@ -144,13 +163,22 @@ def notebook(self):
else:
fname = args.output
# write the notebook out
if os.path.isdir(fname):
if args.input is not None:
basename = os.path.basename(args.input)
mname = basename.replace(".bngl", "")
fname = mname + ".ipynb"
else:
mname = CONFIG["bionetgen"]["notebook"]["name"]
fname = os.path.join(args.output, mname)

notebook.write(fname)
# open the notebook with nbopen
stdout = getattr(subprocess, CONFIG["bionetgen"]["stdout"])
stderr = getattr(subprocess, CONFIG["bionetgen"]["stderr"])
if args.open:
command = ["nbopen", fname]
rc = run_command(command)
rc, _ = run_command(command)

@cement.ex(
help="Rudimentary plotting of gdat/cdat/scan files",
Expand Down Expand Up @@ -186,27 +214,31 @@ def notebook(self):
{
"help": "x-axis minimum (default: determined from data)",
"default": None,
"type": float,
},
),
(
["--xmax"],
{
"help": "x-axis maximum (default: determined from data)",
"default": False,
"type": float,
},
),
(
["--ymin"],
{
"help": "y-axis minimum (default: determined from data)",
"default": False,
"type": float,
},
),
(
["--ymax"],
{
"help": "y-axis maximum (default: determined from data)",
"default": False,
"type": float,
},
),
(["--xlabel"], {"help": "x-axis label (default: time)", "default": False}),
Expand Down
38 changes: 36 additions & 2 deletions bionetgen/modelapi/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,44 @@ def __init__(self) -> None:
self.name = "actions"
AList = ActionList()
self._action_list = AList.possible_types
self.items = []

def __setattr__(self, name, value) -> None:
self.__dict__[name] = value

def add_item(self, item_tpl) -> None:
name, value = item_tpl
# set the line
self.items.append(value)

def __repr__(self) -> str:
# overwrites what the class representation
# shows the items in the model block in
# say ipython
repr_str = "{} block with {} item(s): {}".format(
self.name, len(self.items), self.items
)
return repr_str

def __getitem__(self, key):
return self.items[key]

def __setitem__(self, key, value) -> None:
self.items[key] = value

def __delitem__(self, key) -> None:
try:
return self.items.pop(key)
# TODO: more specific except statements
except:
print("Item {} not found".format(key))

def __iter__(self):
return self.items.__iter__()

def __contains__(self, key) -> bool:
return key in self.items

def add_action(self, action_type, action_args) -> None:
"""
adds action, needs type as string and args as list of tuples
Expand All @@ -564,7 +598,7 @@ def clear_actions(self) -> None:
def gen_string(self) -> str:
block_lines = []
# we just loop over lines for actions
for item in self.items.keys():
block_lines.append(self.items[item].print_line())
for item in self.items:
block_lines.append(item.print_line())
# join everything with new lines
return "\n".join(block_lines)
8 changes: 3 additions & 5 deletions bionetgen/modelapi/bngfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ def generate_xml(self, xml_file, model_file=None) -> bool:
# run with --xml
os.chdir(temp_folder)
# TODO: take stdout option from app instead
# rc = subprocess.run(["perl",self.bngexec, "--xml", stripped_bngl], stdout=bng.defaults.stdout)
rc = run_command(["perl", self.bngexec, "--xml", stripped_bngl])
rc, _ = run_command(["perl", self.bngexec, "--xml", stripped_bngl])
if rc == 1:
# if we fail, print out what we have to
# let the user know what BNG2.pl says
Expand Down Expand Up @@ -145,7 +144,7 @@ def write_xml(self, open_file, xml_type="bngxml", bngl_str=None) -> bool:
# run with --xml
# TODO: Make output supression an option somewhere
if xml_type == "bngxml":
rc = run_command(["perl", self.bngexec, "--xml", "temp.bngl"])
rc, _ = run_command(["perl", self.bngexec, "--xml", "temp.bngl"])
if rc == 1:
print("XML generation failed")
# go back to our original location
Expand All @@ -162,7 +161,7 @@ def write_xml(self, open_file, xml_type="bngxml", bngl_str=None) -> bool:
return True
elif xml_type == "sbml":
command = ["perl", self.bngexec, "temp.bngl"]
rc = run_command(command)
rc, _ = run_command(command)
if rc == 1:
print("SBML generation failed")
# go back to our original location
Expand All @@ -179,4 +178,3 @@ def write_xml(self, open_file, xml_type="bngxml", bngl_str=None) -> bool:
else:
print("XML type {} not recognized".format(xml_type))
return False
return False
2 changes: 1 addition & 1 deletion bionetgen/modelapi/bngparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def parse_actions(self, model_obj):
ablock.add_action(atype, arg_tuples)
else:
ablock.add_action(atype, [])
model_obj.add_block(ablock)
model_obj.add_block(ablock)

def parse_xml(self, xml_str, model_obj) -> None:
xml_dict = xmltodict.parse(xml_str)
Expand Down
12 changes: 8 additions & 4 deletions bionetgen/modelapi/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,12 @@ def setup_simulator(self, sim_type="libRR"):
# temporary file
with TemporaryFile(mode="w+") as tpath:
# write the sbml
self.bngparser.bngfile.write_xml(
tpath, xml_type="sbml", bngl_str=str(self)
)
if not (
self.bngparser.bngfile.write_xml(
tpath, xml_type="sbml", bngl_str=str(self)
)
):
raise ValueError("SBML couldn't be generated for libRR simulator")
# TODO: Only clear the writeSBML action
# by adding a mechanism to do so
self.actions.clear_actions()
Expand All @@ -279,7 +282,8 @@ def setup_simulator(self, sim_type="libRR"):
)
)
return None
return self.simulator
# for now we return the underlying simulator
return self.simulator.simulator


###### CORE OBJECT AND PARSING FRONT-END ######
11 changes: 9 additions & 2 deletions bionetgen/modelapi/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,20 @@ def __str__(self):
mol_str = self.name
if self.label is not None:
mol_str += "%{}".format(self.label)
mol_str += "("
# we have a null species
if not self.name == "0":
mol_str += "("
# we _could_ just not do () if components
# don't exist but that has other issues,
# especially for extension highlighting
if len(self.components) > 0:
for icomp, comp in enumerate(self.components):
if icomp > 0:
mol_str += ","
mol_str += str(comp)
mol_str += ")"
# we have a null species
if not self.name == "0":
mol_str += ")"
if self.compartment is not None:
mol_str += "@{}".format(self.compartment)
return mol_str
Expand Down
2 changes: 1 addition & 1 deletion bionetgen/modelapi/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def run(inp, out=None, suppress=False):
into. If it doesn't exist, it will be created.
"""
# if out is None we make a temp directory
cur_dir = os.getcwd()
if out is None:
cur_dir = os.getcwd()
with TemporaryDirectory() as out:
# instantiate a CLI object with the info
cli = BNGCLI(inp, out, conf["bngpath"], suppress=suppress)
Expand Down
11 changes: 7 additions & 4 deletions bionetgen/modelapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_bngexec(bngexec):
path to BNG2.pl to test
"""
command = ["perl", bngexec]
rc = run_command(command, suppress=True)
rc, _ = run_command(command, suppress=True)
if rc == 0:
return True
else:
Expand All @@ -112,14 +112,17 @@ def run_command(command, suppress=False):
process = subprocess.Popen(
command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, bufsize=-1
)
return process.poll()
return process.poll(), None
else:
process = subprocess.Popen(command, stdout=subprocess.PIPE, encoding="utf8")
out = []
while True:
output = process.stdout.readline()
if output == "" and process.poll() is not None:
break
if output:
print(output.strip())
o = output.strip()
out.append(o)
print(o)
rc = process.poll()
return rc
return rc, out
4 changes: 2 additions & 2 deletions bionetgen/simulator/simulators.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def sim_getter(model_file=None, model_str=None, sim_type="libRR"):
"""
if model_file is not None:
if sim_type == "libRR":
return libRRSimulator(model_file=model_file).simulator
return libRRSimulator(model_file=model_file)
else:
print("simulator type {} not supported".format(sim_type))
elif model_str is not None:
if sim_type == "libRR":
return libRRSimulator(model_str=model_str).simulator
return libRRSimulator(model_str=model_str)
else:
print("simulator type {} not supported".format(sim_type))

Expand Down
4 changes: 2 additions & 2 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Indices and Tables:
* :ref:`modindex`
* :ref:`search`

BioNetGen Home & GitHub Links
=============================
BioNetGen Home & GitHub Links:
==============================

* `BioNetGen <https://bionetgen.org/>`_
* `BNG VSCode Extension GitHub <https://github.com/RuleWorld/BNG_vscode_extension>`_
Expand Down
Loading

0 comments on commit e8b0d46

Please sign in to comment.