Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature check sbml success #118

Merged
merged 12 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
uses: actions/cache@v2
with:
path: ${{ matrix.path }}
key: ${{ runner.os }}-${{ matrix.python-version }}-ci-base
key: ci-base-${{ runner.os }}-${{ matrix.python-version }}

- name: Install tox
run: pip install tox
Expand Down Expand Up @@ -68,14 +68,10 @@ jobs:
uses: actions/cache@v1
with:
path: ~/.cache/pip
key: ${{ runner.os }}-${{ matrix.python-version }}-ci-notebooks
key: ci-notebooks-${{ runner.os }}-${{ matrix.python-version }}

- name: Install apt dependencies
run: |
.github/workflows/install_deps.sh amici

- name: Install tox
run: pip install tox
- name: Install dependencies
run: .github/workflows/install_deps.sh amici

- name: Test notebooks
run: tox -e notebooks
Expand All @@ -99,14 +95,10 @@ jobs:
uses: actions/cache@v1
with:
path: ~/.cache/pip
key: ${{ runner.os }}-${{ matrix.python-version }}-ci-doc
key: ci-doc-${{ runner.os }}-${{ matrix.python-version }}

- name: Install apt dependencies
run: |
.github/workflows/install_deps.sh doc

- name: Install tox
run: pip install tox
- name: Install dependencies
run: .github/workflows/install_deps.sh doc

- name: Test documentation build
run: tox -e doc
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

jobs:
deploy:
runs-on: ubuntu-latest
runs-on: ubuntu-18.04

steps:
- name: Check out repository
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/install_deps.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/bin/sh

# Install tox
pip install tox

# Update aptitude
sudo apt-get update

# iterate over optional dependencies
for par in "$@"; do
case $par in
Expand Down
117 changes: 117 additions & 0 deletions tests/test_yaml2sbml/ode_input_invalid_SBML_identifier.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
time:
variable: t

parametersR:

- parameterId: c1
nominalValue: 1

parameterScale: lin
lowerBound: 0
upperBound: 10
estimate: 0

- parameterId: Shalve
nominalValue: 0.1

parameterScale: log10
lowerBound: 0.1
upperBound: 10
estimaete: 1

- parameterId: Vh
nominalValue: 0.1

parameterScale: lin
lowerBound: 0
upperBound: 50
estimate: 0

- parameterId: h
nominalValue: 0.1

parameterScale: lin
lowerBound: 0
upperBound: 10
estimate: 0

- parameterId: Vmm
nominalValue: 0.1

parameterScale: lin
lowerBound: 0
upperBound: 10
estimate: 0

- parameterId: Km
nominalValue: 0.1

parameterScale: lin
lowerBound: 0
upperBound: 10
estimate: 0

- parameterId: v1
nominalValue: 0.1

parameterScale: lin
lowerBound: 0
upperBound: 10
estimate: 0

- parameterId: k4
nominalValue: 0.1

parameterScale: lin
lowerBound: 0
upperBound: 10
estimate: 0


functions:
- functionId: hill
arguments: V, s, Shalve, h
formula: V * (s/Shalve)^h / (1 + (s/Shalve)^h )

- functionId: MM
arguments: V, s, Km
formula: V*s / (Km + s)


odes:
- stateId: S1
rightHandSide: v1 - MM(S1, Vmm, Km)*c1^2
initialValue: 0.1

- stateId: S2
rightHaandSide: MM(S1, Vmm, Km) - hill(S2, Shalve, Vh, h)
initialValue: 1

- stateId: S3
rightHandSide: hill(S2, Shalve, Vh, h) - k4*S3 + log10(c1)
initialValue: 1


observables:
- observableId: Obs_1
observableFormula: S1 + S2

noiseFormula: noiseParameter1
noiseDistribution: normal

- observableId: Obs_2
observableFormula: s * S3

noiseFormula: noiseParameter1
noiseDistribution: normal


conditions2:

- conditionId: condition1
conditionName: condition1
S1: 42
Km: 3.14



64 changes: 53 additions & 11 deletions yaml2sbml/yaml2sbml.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,17 @@ def _create_time(model: sbml.Model, time_var: str):
model: the SBML model to which the species will be added.
time_var: str, the time variable
"""

if time_var == 'time':
return

time_parameter = model.createParameter()
time_parameter.setId(time_var)

if time_parameter.setId(time_var) != sbml.LIBSBML_OPERATION_SUCCESS:

raise RuntimeError(f'Unable to generate time parameter with id '
f'{time_var}. Invalid SBML identifier.')

time_parameter.setName(time_var)
time_parameter.setConstant(False)

Expand Down Expand Up @@ -266,7 +275,11 @@ def _create_parameter(model: sbml.Model, parameter_id: str, value: str = None):
value: the parameter value, if value is None, no parameter is set.
"""
k = model.createParameter()
k.setId(parameter_id)

if k.setId(parameter_id) != sbml.LIBSBML_OPERATION_SUCCESS:
raise RuntimeError(f'Unable to generate parameter with id '
f'{parameter_id}. Invalid SBML identifier.')

k.setName(parameter_id)
k.setConstant(True)

Expand Down Expand Up @@ -306,14 +319,26 @@ def _create_assignment(model: sbml.Model, assignment_id: str, formula: str):
formula: str: contains the equation for the assignment rule
"""
assignment_parameter = model.createParameter()
assignment_parameter.setId(assignment_id)

if assignment_parameter.setId(assignment_id):
raise RuntimeError(f'Unable to generate assignment with id '
f'{assignment_id}. Invalid SBML identifier.')

assignment_parameter.setName(assignment_id)
assignment_parameter.setConstant(False)
assignment_parameter.setUnits('dimensionless')

assignment_rule = model.createAssignmentRule()
assignment_rule.setVariable(assignment_id)
assignment_rule.setMath(sbml.parseL3Formula(formula))

sbml_formula = sbml.parseL3Formula(formula)

if sbml_formula is not None:
assignment_rule.setMath(sbml.parseL3Formula(formula))
else:
raise RuntimeError(f'Unable to generate assignment for formula '
f'{formula}, libsbml can not parse the given '
f'expression.')
Comment on lines +339 to +341
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise RuntimeError(f'Unable to generate assignment for formula '
f'{formula}, libsbml can not parse the given '
f'expression.')
raise RuntimeError('Unable to generate assignment for formula '
f'{formula}, libsbml can not parse the given '
'expression.')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would also work, matter of style I guess

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I once had a style checker complain about this :D So slightly in favor of leaving it consistent :)



def _read_functions_block(model: sbml.Model, functions_list: list):
Expand Down Expand Up @@ -351,9 +376,20 @@ def _create_function(model: sbml.Model,
formula: the formula of the function
"""
f = model.createFunctionDefinition()
f.setId(function_id)

if f.setId(function_id) != sbml.LIBSBML_OPERATION_SUCCESS:
raise RuntimeError(f'Unable to generate function with id '
f'{function_id}. Invalid SBML identifier.')

math = sbml.parseL3Formula('lambda(' + arguments + ', ' + formula + ')')
f.setMath(math)

if math is not None:
f.setMath(math)
else:
raise RuntimeError(f'Unable to generate assignment for funtion '
f'{function_id}, libsbml can not parse the given '
f'function expression, given by '
f'lambda({arguments} , {formula}).')


def _read_odes_block(model: sbml.Model, odes_list: list):
Expand Down Expand Up @@ -391,7 +427,9 @@ def _create_species(model: sbml.Model, species_id: str, initial_amount: str):
s: the SBML species
"""
s = model.createSpecies()
s.setId(species_id)
if s.setId(species_id) != sbml.LIBSBML_OPERATION_SUCCESS:
raise RuntimeError(f'Unable to generate species with id '
f'{species_id}. Invalid SBML identifier.')

try:
s.setInitialAmount(float(initial_amount))
Expand All @@ -411,21 +449,25 @@ def _create_species(model: sbml.Model, species_id: str, initial_amount: str):
return s


def _create_rate_rule(model: sbml.Model, species: str, formula: str):
def _create_rate_rule(model: sbml.Model, species_id: str, formula: str):
"""
Create an SBML rateRule for a species and add it to the SBML model.

This is where the ODEs are encoded.

Arguments:
model: SBML model to which the rate rule will be added.
species: the species name of the ODE
species_id: the id of the state of the ODE
formula: the right-hand-side of the ODE
"""
r = model.createRateRule()
r.setId('d/dt_' + species)
r.setVariable(species)
r.setId('d_dt_' + species_id)
r.setVariable(species_id)
math_ast = sbml.parseL3Formula(formula)
if math_ast is None:
raise RuntimeError(f'Unable to generate the rate rule for the state '
f'{species_id}, libsbml can not parse the right-'
f'hand side, given by {formula}).')
r.setMath(math_ast)


Expand Down