Skip to content

Commit

Permalink
feat: supporting parameters, starting with 'reduced' of the trapspace…
Browse files Browse the repository at this point in the history
…s method
  • Loading branch information
jonrkarr committed Jul 8, 2021
1 parent fb1da3b commit b43208a
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 34 deletions.
43 changes: 38 additions & 5 deletions biosimulators.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,21 @@
"pages": "646",
"identifiers": [{
"namespace": "doi",
"id": "10.3389/fphys.2018.00646 ",
"url": "https://doi.org/10.3389/fphys.2018.00646 "
"id": "10.3389/fphys.2018.00646",
"url": "https://doi.org/10.3389/fphys.2018.00646"
}]
},
{
"title": "BioLQM: A Java toolkit for the manipulation and conversion of logical qualitative models of biological networks",
"authors": "Aurélien Naldi",
"journal": "Frontiers of Physiology",
"volume": 9,
"year": 2018,
"pages": "1605",
"identifiers": [{
"namespace": "doi",
"id": "10.3389/fphys.2018.01605",
"url": "https://doi.org/10.3389/fphys.2018.01605"
}]
},
{
Expand Down Expand Up @@ -395,7 +408,17 @@
],
"simulationFormats": [],
"archiveFormats": [],
"parameters": [],
"parameters": [{
"kisaoId": {
"namespace": "KISAO",
"id": "KISAO_0000216"
},
"name": "use reduced model",
"type": "boolean",
"value": "true",
"recommendedRange": null,
"availableSoftwareInterfaceTypes": ["desktop application", "library", "command-line application", "BioSimulators Docker image"]
}],
"dependentDimensions": [],
"dependentVariableTargetPatterns": [{
"variables": "species levels",
Expand All @@ -404,7 +427,7 @@
"availableSoftwareInterfaceTypes": ["desktop application", "library", "command-line application", "BioSimulators Docker image"],
"dependencies": null,
"citations": [{
"title": " Computing maximal and minimal trap spaces of Boolean networks",
"title": "Computing maximal and minimal trap spaces of Boolean networks",
"authors": "Hannes Klarner, Alexander Bockmayr & Heike Siebert",
"journal": "Natural Computing",
"volume": 14,
Expand Down Expand Up @@ -444,7 +467,17 @@
],
"simulationFormats": [],
"archiveFormats": [],
"parameters": [],
"parameters": [{
"kisaoId": {
"namespace": "KISAO",
"id": "KISAO_0000216"
},
"name": "use reduced model",
"type": "boolean",
"value": "true",
"recommendedRange": null,
"availableSoftwareInterfaceTypes": ["desktop application", "library", "command-line application", "BioSimulators Docker image"]
}],
"dependentDimensions": [],
"dependentVariableTargetPatterns": [{
"variables": "species levels",
Expand Down
85 changes: 72 additions & 13 deletions biosimulators_ginsim/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
:License: MIT
"""

from biosimulators_utils.data_model import ValueType
from biosimulators_utils.sedml.data_model import SteadyStateSimulation, UniformTimeCourseSimulation
import collections
import enum
Expand All @@ -30,59 +31,117 @@ class UpdatePolicy(str, enum.Enum):
'name': 'synchronous logical simulation',
'simulation_type': UniformTimeCourseSimulation,
'method': 'trace',
'method_args': lambda simulation:
'-m {:d} -u {}'.format(int(simulation.output_end_time), UpdatePolicy.synchronous.value),
'method_args': lambda simulation: [
'-m {:d}'.format(int(simulation.output_end_time)),
'-u {}'.format(UpdatePolicy.synchronous.value),
],
'parameters': {},
}),
('KISAO_0000657', {
'kisao_id': 'KISAO_0000657',
'name': 'sequential logical simulation',
'simulation_type': UniformTimeCourseSimulation,
'method': 'trace',
'method_args': lambda simulation:
'-m {:d} -u {}'.format(int(simulation.output_end_time), UpdatePolicy.sequential.value),
'method_args': lambda simulation: [
'-m {:d}'.format(int(simulation.output_end_time)),
'-u {}'.format(UpdatePolicy.sequential.value),
],
'parameters': {},
}),
('KISAO_0000450', {
'kisao_id': 'KISAO_0000450',
'name': 'asynchronous logical simulation',
'simulation_type': UniformTimeCourseSimulation,
'method': 'random',
'method_args': lambda simulation:
'-m {:d} -u {}'.format(int(simulation.output_end_time), UpdatePolicy.asynchronous.value),
'method_args': lambda simulation: [
'-m {:d}'.format(int(simulation.output_end_time)),
'-u {}'.format(UpdatePolicy.asynchronous.value),
],
'parameters': {},
}),
('KISAO_0000573', {
'kisao_id': 'KISAO_0000573',
'name': 'complete logical simulation',
'simulation_type': UniformTimeCourseSimulation,
'method': 'random',
'method_args': lambda simulation:
'-m {:d} -u {}'.format(int(simulation.output_end_time), UpdatePolicy.complete.value),
'method_args': lambda simulation: [
'-m {:d}'.format(int(simulation.output_end_time)),
'-u {}'.format(UpdatePolicy.complete.value),
],
'parameters': {},
}),
('KISAO_0000659', {
'kisao_id': 'KISAO_0000659',
'name': 'Naldi MDD stable state search method',
'simulation_type': SteadyStateSimulation,
'method': 'fixpoints',
'method_args': lambda simulation: collections.OrderedDict([]),
'method_args': lambda simulation: [],
'parameters': {},
}),
('KISAO_0000662', {
'kisao_id': 'KISAO_0000662',
'name': 'Klarner ASP trap space identification method',
'simulation_type': SteadyStateSimulation,
'method': 'trapspaces',
'method_args': lambda simulation: None,
'parameters': {}, # TODO: options: all, reduce, terminal, tree
'method_args': lambda simulation: [],
'parameters': {
'KISAO_0000216': {
'kisao_id': 'KISAO_0000216',
'name': 'use reduced model',
'method_args': lambda value: ['reduced'] if value else [],
'type': ValueType.boolean,
},
# 'KISAO_0000XXX': {
# 'kisao_id': 'KISAO_0000XXX',
# 'name': 'get all trapped variables',
# 'method_args': lambda value: ['all'] if value else [],
# 'type': ValueType.boolean,
# },
# 'KISAO_0000XXX': {
# 'kisao_id': 'KISAO_0000XXX',
# 'name': 'only get terminal trap spaces',
# 'method_args': lambda value: ['terminal'] if value else [],
# 'type': ValueType.boolean,
# },
# 'KISAO_0000XXX': {
# 'kisao_id': 'KISAO_0000XXX',
# 'name': 'get trap space hierarchy',
# 'method_args': lambda value: ['tree'] if value else [],
# 'type': ValueType.boolean,
# },
},
}),
('KISAO_0000663', {
'kisao_id': 'KISAO_0000663',
'name': 'BDD trap space identification method',
'simulation_type': SteadyStateSimulation,
'method': 'trapspaces',
'method_args': lambda simulation: 'BDD',
'parameters': {}, # TODO: options: all, reduce, terminal, tree
'method_args': lambda simulation: ['BDD'],
'parameters': {
'KISAO_0000216': {
'kisao_id': 'KISAO_0000216',
'name': 'use reduced model',
'method_args': lambda value: ['reduced'] if value else [],
'type': ValueType.boolean,
},
# 'KISAO_0000XXX': {
# 'kisao_id': 'KISAO_0000XXX',
# 'name': 'get all trapped variables',
# 'method_args': lambda value: ['all'] if value else [],
# 'type': ValueType.boolean,
# },
# 'KISAO_0000XXX': {
# 'kisao_id': 'KISAO_0000XXX',
# 'name': 'only get terminal trap spaces',
# 'method_args': lambda value: ['terminal'] if value else [],
# 'type': ValueType.boolean,
# },
# 'KISAO_0000XXX': {
# 'kisao_id': 'KISAO_0000XXX',
# 'name': 'get trap space hierarchy',
# 'method_args': lambda value: ['tree'] if value else [],
# 'type': ValueType.boolean,
# },
},
}),
])
39 changes: 30 additions & 9 deletions biosimulators_ginsim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from biosimulators_utils.sedml.data_model import ( # noqa: F401
ModelLanguage, Simulation, UniformTimeCourseSimulation, Symbol)
from biosimulators_utils.simulator.utils import get_algorithm_substitution_policy
from biosimulators_utils.utils.core import validate_str_value, parse_value
from biosimulators_utils.warnings import warn, BioSimulatorsWarning
from kisao.data_model import AlgorithmSubstitutionPolicy, ALGORITHM_SUBSTITUTION_POLICY_LEVELS
from kisao.utils import get_preferred_substitute_algorithm_by_ids
Expand Down Expand Up @@ -160,7 +161,7 @@ def set_up_simulation(simulation):
* :obj:`str`: KiSAO of algorithm to execute
* :obj:`str`: name of the :obj:`biolqm` simulation/analysis method
* :obj:`str`: arguments for simulation method
* :obj:`list` of :obj:`str`: arguments for simulation method
"""
# simulation algorithm
alg_kisao_id = simulation.algorithm.kisao_id
Expand All @@ -179,13 +180,33 @@ def set_up_simulation(simulation):
# Apply the algorithm parameter changes specified by `simulation.algorithm.parameter_changes`
if exec_kisao_id == alg_kisao_id:
for change in simulation.algorithm.changes:
if (
ALGORITHM_SUBSTITUTION_POLICY_LEVELS[alg_substitution_policy]
> ALGORITHM_SUBSTITUTION_POLICY_LEVELS[AlgorithmSubstitutionPolicy.NONE]
):
warn('Unsuported algorithm parameter `{}` was ignored.'.format(change.kisao_id), BioSimulatorsWarning)
param_props = alg_props['parameters'].get(change.kisao_id, None)

if param_props is None:
if (
ALGORITHM_SUBSTITUTION_POLICY_LEVELS[alg_substitution_policy]
> ALGORITHM_SUBSTITUTION_POLICY_LEVELS[AlgorithmSubstitutionPolicy.NONE]
):
warn('Unsuported algorithm parameter `{}` was ignored.'.format(change.kisao_id), BioSimulatorsWarning)
else:
raise NotImplementedError('Algorithm parameter `{}` is not supported.'.format(change.kisao_id))
else:
raise NotImplementedError('Algorithm parameter `{}` is not supported.'.format(change.kisao_id))
if validate_str_value(change.new_value, param_props['type']):
parsed_value = parse_value(change.new_value, param_props['type'])
method_args.extend(param_props['method_args'](parsed_value))

else:
if (
ALGORITHM_SUBSTITUTION_POLICY_LEVELS[alg_substitution_policy]
> ALGORITHM_SUBSTITUTION_POLICY_LEVELS[AlgorithmSubstitutionPolicy.NONE]
):
msg = 'Unsuported algorithm parameter value `{}` of `{}` was ignored. The value must be a {}.'.format(
change.new_value, change.kisao_id, param_props['type'].value)
warn(msg, BioSimulatorsWarning)
else:
msg = '`{}` is not a valid value of algorithm parameter `{}`. The value must be a {}.'.format(
change.new_value, change.kisao_id, param_props['type'].value)
raise ValueError(msg)
else:
for change in simulation.algorithm.changes:
warn('Unsuported algorithm parameter `{}` was ignored.'.format(change.kisao_id), BioSimulatorsWarning)
Expand All @@ -200,14 +221,14 @@ def exec_simulation(method_name, model, args=None):
Args:
method_name (:obj:`str`): name of the :obj:`biolqm` simulation/analysis method
model (:obj:`py4j.java_gateway.JavaObject`): model
args (:obj:`str`, optional): argument to :obj:`method`
args (:obj:`list` of :obj:`str`, optional): argument to :obj:`method`
Returns:
:obj:`list` of :obj:`dict`: result of :obj:`method` for :obj:`model` and :obj:`args`
"""
method = getattr(biolqm, method_name)
if args:
args_list = [args]
args_list = [' '.join(args)]
else:
args_list = []
result = method(model, *args_list)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
biosimulators_utils[logging,sbml] >= 0.1.92
ginsim
kisao
kisao >= 2.20
lxml
numpy
4 changes: 4 additions & 0 deletions tests/test_data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ def test_data_model_matches_specs(self):
alg_props = KISAO_ALGORITHM_MAP[alg_specs['kisaoId']['id']]
self.assertEqual(set(alg_props['parameters'].keys()),
set(param_specs['kisaoId']['id'] for param_specs in alg_specs['parameters']))

for param_specs in alg_specs['parameters']:
param_props = alg_props['parameters'][param_specs['kisaoId']['id']]
self.assertEqual(param_props['type'].value, param_specs['type'])
55 changes: 49 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from biosimulators_ginsim.utils import (validate_time_course, validate_simulation, get_variable_target_xpath_ids,
read_model, set_up_simulation,
exec_simulation, get_variable_results)
from biosimulators_utils.sedml.data_model import (ModelLanguage, UniformTimeCourseSimulation,
from biosimulators_utils.sedml.data_model import (ModelLanguage, SteadyStateSimulation,
UniformTimeCourseSimulation,
Algorithm, AlgorithmParameterChange,
Variable, Symbol)
from biosimulators_utils.warnings import BioSimulatorsWarning
Expand Down Expand Up @@ -76,7 +77,7 @@ def test_set_up_simulation(self):
kisao_id, method, method_args = set_up_simulation(simulation)
self.assertEqual(kisao_id, 'KISAO_0000449')
self.assertEqual(method, 'trace')
self.assertEqual(method_args, '-m {:d} -u {}'.format(100, UpdatePolicy.synchronous.value))
self.assertEqual(method_args, ['-m 100', '-u {}'.format(UpdatePolicy.synchronous.value)])

simulation.algorithm.kisao_id = 'KISAO_0000448'
with mock.patch.dict('os.environ', {'ALGORITHM_SUBSTITUTION_POLICY': 'NONE'}):
Expand All @@ -89,7 +90,7 @@ def test_set_up_simulation(self):
kisao_id, method, method_args = set_up_simulation(simulation)
self.assertEqual(kisao_id, 'KISAO_0000449')
self.assertEqual(method, 'trace')
self.assertEqual(method_args, '-m {:d} -u {}'.format(100, UpdatePolicy.synchronous.value))
self.assertEqual(method_args, ['-m 100', '-u {}'.format(UpdatePolicy.synchronous.value)])

simulation.algorithm.kisao_id = 'KISAO_0000449'
simulation.algorithm.changes = [AlgorithmParameterChange()]
Expand All @@ -113,10 +114,52 @@ def test_set_up_simulation(self):
with self.assertRaises(NotImplementedError):
set_up_simulation(simulation)

simulation = SteadyStateSimulation(algorithm=Algorithm())
simulation.algorithm.kisao_id = 'KISAO_0000662'
with mock.patch.dict('os.environ', {'ALGORITHM_SUBSTITUTION_POLICY': 'NONE'}):
kisao_id, method, method_args = set_up_simulation(simulation)
self.assertEqual(kisao_id, 'KISAO_0000662')
self.assertEqual(method, 'trapspaces')
self.assertEqual(method_args, [])

simulation.algorithm.kisao_id = 'KISAO_0000663'
with mock.patch.dict('os.environ', {'ALGORITHM_SUBSTITUTION_POLICY': 'NONE'}):
kisao_id, method, method_args = set_up_simulation(simulation)
self.assertEqual(kisao_id, 'KISAO_0000663')
self.assertEqual(method, 'trapspaces')
self.assertEqual(method_args, ['BDD'])

simulation.algorithm.changes.append(AlgorithmParameterChange(
kisao_id='KISAO_0000216',
new_value='false',
))
with mock.patch.dict('os.environ', {'ALGORITHM_SUBSTITUTION_POLICY': 'NONE'}):
kisao_id, method, method_args = set_up_simulation(simulation)
self.assertEqual(kisao_id, 'KISAO_0000663')
self.assertEqual(method, 'trapspaces')
self.assertEqual(method_args, ['BDD'])

simulation.algorithm.changes[0].new_value = 'true'
with mock.patch.dict('os.environ', {'ALGORITHM_SUBSTITUTION_POLICY': 'NONE'}):
kisao_id, method, method_args = set_up_simulation(simulation)
self.assertEqual(kisao_id, 'KISAO_0000663')
self.assertEqual(method, 'trapspaces')
self.assertEqual(method_args, ['BDD', 'reduced'])

simulation.algorithm.changes[0].new_value = 'unsupported'
with mock.patch.dict('os.environ', {'ALGORITHM_SUBSTITUTION_POLICY': 'NONE'}):
with self.assertRaises(ValueError):
set_up_simulation(simulation)

simulation.algorithm.changes[0].new_value = 'unsupported'
with mock.patch.dict('os.environ', {'ALGORITHM_SUBSTITUTION_POLICY': 'SIMILAR_VARIABLES'}):
with self.assertWarns(BioSimulatorsWarning):
set_up_simulation(simulation)

def test_exec_simulation(self):
filename = os.path.join(os.path.dirname(__file__), 'fixtures', 'SuppMat_Model_Master_Model.zginml')
model = read_model(filename)
raw_results = exec_simulation('trace', model, '-m {:d} -u {}'.format(10, UpdatePolicy.synchronous.value))
raw_results = exec_simulation('trace', model, ['-m 10', '-u {}'.format(UpdatePolicy.synchronous.value)])
self.assertLessEqual(len(raw_results), 10 + 1)

raw_results = exec_simulation('fixpoints', model)
Expand All @@ -125,7 +168,7 @@ def test_exec_simulation(self):
def test_get_variable_results_sbml(self):
filename = os.path.join(os.path.dirname(__file__), 'fixtures', 'example-model.xml')
model = read_model(filename)
raw_results = exec_simulation('trace', model, '-m {:d} -u {}'.format(10, UpdatePolicy.synchronous.value))
raw_results = exec_simulation('trace', model, ['-m 10', '-u {}'.format(UpdatePolicy.synchronous.value)])

namespaces = {
'sbml': 'http://www.sbml.org/sbml/level3/version1/core',
Expand Down Expand Up @@ -182,7 +225,7 @@ def test_get_variable_results_sbml(self):
def test_get_variable_results_zginml(self):
filename = os.path.join(os.path.dirname(__file__), 'fixtures', 'SuppMat_Model_Master_Model.zginml')
model = read_model(filename)
raw_results = exec_simulation('trace', model, '-m {:d} -u {}'.format(10, UpdatePolicy.synchronous.value))
raw_results = exec_simulation('trace', model, ['-m 10', '-u {}'.format(UpdatePolicy.synchronous.value)])

variables = [
Variable(
Expand Down

0 comments on commit b43208a

Please sign in to comment.