From aeaac92f5aa7c1f8cc7fb8a58fb3755da06d3fd9 Mon Sep 17 00:00:00 2001 From: Ayush Pandey Date: Sat, 3 Apr 2021 14:49:33 -0600 Subject: [PATCH] Fix for SBML kinetic law formula when importing and writing SBML #89 (#93) * add priors, and custom prior * py_inference wrapper added * merge master * add beta and gamma prior * add custom prior documenting notebook * all notebooks work * typo bug fix * minor update to inference notebooks * gaussian prior fix * bug fixes for log uniform prior and pdf computation bound checks * add checks to rate law math ast --- bioscrape/pid_interfaces.py | 4 ---- bioscrape/sbmlutil.py | 9 +++++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/bioscrape/pid_interfaces.py b/bioscrape/pid_interfaces.py index 74e3d69..8ee5dcb 100644 --- a/bioscrape/pid_interfaces.py +++ b/bioscrape/pid_interfaces.py @@ -119,7 +119,6 @@ def exponential_prior(self, param_name, param_value): lambda_p = prior_dict[param_name][1] prob = lambda_p * np.exp(-lambda_p * param_value) - if prob < 0: warnings.warn('Probability less than 0 while checking Exponential prior! Current parameter name and value: {0}:{1}.'.format(param_name, param_value)) return np.inf @@ -180,10 +179,8 @@ def log_uniform_prior(self, param_name, param_value): return np.inf prob = 1/(param_value* (np.log(upper_bound) - np.log(lower_bound))) - if prob < 0: warnings.warn('Probability less than 0 while checking Log-Uniform prior! Current parameter name and value: {0}:{1}.'.format(param_name, param_value)) - return np.inf else: return np.log(prob) @@ -204,7 +201,6 @@ def log_gaussian_prior(self, param_name, param_value): prob = 1/(param_value * np.sqrt(2*np.pi) * sigma) * np.exp((-0.5 * (np.log(param_value) - mu)**2)/sigma**2) if prob < 0: warnings.warn('Probability less than 0 while checking log-normal prior! Current parameter name and value: {0}:{1}.'.format(param_name, param_value)) - return np.inf else: return np.log(prob) diff --git a/bioscrape/sbmlutil.py b/bioscrape/sbmlutil.py index bc0c001..95811bb 100644 --- a/bioscrape/sbmlutil.py +++ b/bioscrape/sbmlutil.py @@ -92,7 +92,10 @@ def import_sbml(sbml_file, bioscrape_model = None, input_printout = False, **kwa allparams[pid] = p.getValue() # get the formula as a string and then add # a leading _ to parameter names - kl_formula = libsbml.formulaToL3String(kl.getMath()) + math_ast = kl.getMath() + if math_ast is None: + raise ValueError("Could not import the rate law for reaction to SBML.") + kl_formula = libsbml.formulaToL3String(math_ast) #We should no longer add underscores to parameters #rate_string = _add_underscore_to_parameters(kl_formula, allparams) rate_string = kl_formula @@ -629,7 +632,9 @@ def add_reaction(model, inputs_list, outputs_list, ratestring = _remove_underscore_from_parameters(ratestring, allparams) # Set the ratelaw to the ratestring math_ast = libsbml.parseL3Formula(ratestring) - ratelaw.setMath(math_ast) + flag = ratelaw.setMath(math_ast) + if not flag == libsbml.LIBSBML_OPERATION_SUCCESS or math_ast is None: + raise ValueError("Could not write the rate law for reaction to SBML. Check the reaction definition.") #Add propensity annotation if propensity_annotation and propensity_type != "general":