def test_transcription_model(self): #use simple production and degradation of mRNA and protein for testing # mRNA production rate is k1, degradation rate is g1 # protein production rate is k2, degradation rate is g2 stoichiometry_matrix = sympy.Matrix([[1, -1, 0, 0], [0, 0, 1, -1]]) propensities = to_sympy_matrix(['k1', 'g1*x', 'k2*x', 'g2*y']) species = to_sympy_matrix(['x', 'y']) correct_rhs = to_sympy_matrix( ["k1 - g1 * x", "k2 * x - g2 * y", "k1 + g1 * x - 2 * g1 * V_0_0", "k2 * V_0_0 - (g1 + g2) * V_0_1", "k2 * x + g2 * y + k2 * V_0_1 + k2 * V_0_1 - 2 * g2 * V_1_1"]) correct_lhs = to_sympy_matrix(['x','y','V_0_0', 'V_0_1', 'V_1_1']) constants = ["k1","k2","g1","g2"] model = Model(species, constants, propensities, stoichiometry_matrix) lna = LinearNoiseApproximation(model) problem = lna.run() answer_rhs = problem.right_hand_side answer_lhs = problem.left_hand_side assert_sympy_expressions_equal(correct_rhs, answer_rhs) self.assertEqual(correct_lhs, answer_lhs)
def test_creation_from_matrix_returns_itself(self): """ Given a `sympy.Matrix`, `to_sympy_matrix` should return the said matrix. """ m = sympy.Matrix([[1, 2, 3], [4, 5, 6]]) assert_sympy_expressions_equal(m, to_sympy_matrix(m))
def test_creation_from_list_of_integers_returns_matrix(self): """ Given a list of integers, to_sympy_matrix should be able to convert it to a matrix of these integers :return: """ m = sympy.Matrix([[1, 2, 3], [4, 5, 6]]) m_as_list = [[1, 2, 3], [4, 5, 6]] assert_sympy_expressions_equal(m, to_sympy_matrix(m_as_list))
def test_creation_of_column_matrix_from_list_of_strings(self): """ Given a list of strings, to_sympy_matrix should be able to convert them into a column matrix of expresions """ m = sympy.Matrix([sympy.sympify('x+y+3'), sympy.sympify('x+3'), sympy.sympify('y-x'), sympy.sympify('x+y+166')]) m_as_string = ['x+y+3', 'x+3', 'y-x', 'x+y+166'] matrix = to_sympy_matrix(m_as_string) assert_sympy_expressions_equal(m, matrix)
def test_creation_from_list_of_strings_returns_matrix(self): """ Given a list of strings, to_sympy_matrix should be able to convert them into a matrix of expressions. """ m = sympy.Matrix([[sympy.sympify('x+y+3'), sympy.sympify('x+3')], [sympy.sympify('y-x'), sympy.sympify('x+y+166')]]) m_as_string = [['x+y+3', 'x+3'], ['y-x', 'x+y+166']] matrix = to_sympy_matrix(m_as_string) assert_sympy_expressions_equal(m, matrix)
def test_initialisation_of_stoichiometry_matrix_as_numpy_array(self): """ The model constructor should accept stoichiometry_matrix as a numpy matrix e.g. np.array(['y_0+y_1', 'y_1+y_2']) and return them as sympy Matrix e.g. sympy.Matrix([[-1, 1, 0], [0, 0, 1]]) """ answer = sympy.Matrix([[-1, 1, 0], [0, 0, 1]]) # Column m = Model(self.SAMPLE_VARIABLES, self.SAMPLE_CONSTANTS, self.SAMPLE_PROPENSITIES, sympy.Matrix([[-1, 1, 0], [0, 0, 1]])) assert_sympy_expressions_equal(m.stoichiometry_matrix, answer)
def test_creation_of_column_matrix_from_list_of_strings(self): """ Given a list of strings, to_sympy_matrix should be able to convert them into a column matrix of expresions """ m = sympy.Matrix([ sympy.sympify('x+y+3'), sympy.sympify('x+3'), sympy.sympify('y-x'), sympy.sympify('x+y+166') ]) m_as_string = ['x+y+3', 'x+3', 'y-x', 'x+y+166'] matrix = to_sympy_matrix(m_as_string) assert_sympy_expressions_equal(m, matrix)
def test_for_p53(self): """ Given the preopensities, Given the soichiometry matrix, Given the counter (list of Moments), Given the species list, Given k_vector and Given ek_counter (list of moment) The answer should match exactly the expected result :return: """ stoichio = sympy.Matrix([ [1, -1, -1, 0, 0, 0], [0, 0, 0, 1, -1, 0], [0, 0, 0, 0, 1, -1] ]) propensities = to_sympy_matrix([ [" c_0"], [" c_1*y_0"], ["c_2*y_0*y_2/(c_6 + y_0)"], [" c_3*y_0"], [" c_4*y_1"], [" c_5*y_2"]]) counter = [ Moment([0, 0, 0], 1), Moment([0, 0, 2], sympy.Symbol("yx1")), Moment([0, 1, 1], sympy.Symbol("yx2")), Moment([0, 2, 0], sympy.Symbol("yx3")), Moment([1, 0, 1], sympy.Symbol("yx4")), Moment([1, 1, 0], sympy.Symbol("yx5")), Moment([2, 0, 0], sympy.Symbol("yx6")) ] species = sympy.Matrix(["y_0", "y_1", "y_2"]) dbdt_calc = DBetaOverDtCalculator(propensities, counter,stoichio, species) k_vec = [1, 0, 0] ek_counter = [Moment([1, 0, 0], sympy.Symbol("y_0"))] answer = dbdt_calc.get(k_vec,ek_counter).T result = to_sympy_matrix(["c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)"," 0"," 0"," 0"," c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0)"," 0"," -c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2"]) assert_sympy_expressions_equal(answer, result)
def test_centralmoments_using_MM_model(self): """ Given the MM model hard codded bellow,the result of central moment should match exactly the expected one :return: """ counter_nvecs = [[0, 0], [0, 2], [1, 1], [2, 0]] mcounter_nvecs = [[0, 0], [0, 1], [1, 0], [0, 2], [1, 1], [2, 0]] counter = [Moment(c,sympy.Symbol("YU{0}".format(i))) for i,c in enumerate(counter_nvecs)] mcounter = [Moment(c,sympy.Symbol("y_{0}".format(i))) for i,c in enumerate(mcounter_nvecs)] m = to_sympy_matrix([ ['-c_0*y_0*(y_0 + y_1 - 181) + c_1*(-y_0 - y_1 + 301)', 0, '-c_0', '-c_0'], [ 'c_2*(-y_0 - y_1 + 301)', 0, 0, 0] ]) species = sympy.Matrix(map(sympy.var, ['y_0', 'y_1'])) propensities = to_sympy_matrix(['c_0*y_0*(y_0 + y_1 - 181)', 'c_1*(-y_0 - y_1 + 301)', 'c_2*(-y_0 - y_1 + 301)']) stoichiometry_matrix = sympy.Matrix([[-1, 1, 0], [0, 0, 1]]) expected = to_sympy_matrix([ ["c_2*(-y_0 - y_1 + 301)"," -2*c_2"," -2*c_2"," 0"], ["-c_0*y_0*y_1*(y_0 + y_1 - 181) + c_1*y_1*(-y_0 - y_1 + 301) + c_2*y_0*(-y_0 - y_1 + 301) - c_2*y_2*(-y_0 - y_1 + 301) - y_1*(-c_0*y_0*(y_0 + y_1 - 181) + c_1*(-y_0 - y_1 + 301))"," -c_0*y_0 - c_1"," -c_0*y_0 - c_0*(y_0 + y_1 - 181) - c_1 - c_2"," -c_2"], ["-2*c_0*y_0**2*(y_0 + y_1 - 181) + c_0*y_0*(y_0 + y_1 - 181) + 2*c_1*y_0*(-y_0 - y_1 + 301) + c_1*(-y_0 - y_1 + 301) - 2*y_2*(-c_0*y_0*(y_0 + y_1 - 181) + c_1*(-y_0 - y_1 + 301))"," 0"," -4*c_0*y_0 + 2*c_0*y_2 + c_0 - 2*c_1"," -4*c_0*y_0 + 2*c_0*y_2 - 2*c_0*(y_0 + y_1 - 181) + c_0 - 2*c_1"] ]) answer = eq_central_moments(counter, mcounter, m, species, propensities, stoichiometry_matrix, 2) assert_sympy_expressions_equal(answer, expected)
def test_derive_expr_from_counter_entry(self): """ Given the tuples of integers a, b and c Then, the "composite derivatives" should be exactly "a_result", "b_result" and "c_result", respectively :return: """ expr = sp.simplify("c_0*y_0*(y_0 + y_1 - 181)/(y_2+c_1*y_1)") vars = sp.simplify(["y_0", "y_1", "y_2"]) count_entr_a = (0, 1, 3) count_entr_b = (1, 1, 0) count_entr_c = (0, 0, 0) a_result = derive_expr_from_counter_entry(expr, vars, count_entr_a) b_result = derive_expr_from_counter_entry(expr, vars, count_entr_b) c_result = derive_expr_from_counter_entry(expr, vars, count_entr_c) a_expected = sp.diff(sp.diff(expr, "y_2", 3), "y_1") b_expected = sp.diff(sp.diff(expr, "y_0"), "y_1") c_expected = expr assert_sympy_expressions_equal(a_expected, a_result) assert_sympy_expressions_equal(b_expected, b_result) assert_sympy_expressions_equal(c_expected, c_result)
def test_centralmoments_using_p53model(self): """ Given the p53 model hard codded bellow,the result of central moment should match exactly the expected one :return: """ counter_nvecs = [[0, 0, 0], [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0]] mcounter_nvecs = [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0]] counter = [Moment(c,sympy.Symbol("YU{0}".format(i))) for i,c in enumerate(counter_nvecs)] mcounter = [Moment(c,sympy.Symbol("y_{0}".format(i))) for i,c in enumerate(mcounter_nvecs)] m = to_sympy_matrix([ ['c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)', 0, 0, 0, 'c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0)', 0, '-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2'], [ 'c_3*y_0 - c_4*y_1', 0, 0, 0, 0, 0, 0], [ 'c_4*y_1 - c_5*y_2', 0, 0, 0, 0, 0, 0 ]]) species = to_sympy_matrix(['y_0', 'y_1', 'y_2']) propensities = to_sympy_matrix(['c_0', 'c_1 * y_0', 'c_2*y_0*y_2/(c_6 + y_0)', 'c_3*y_0', 'c_4*y_1', 'c_5*y_2']) stoichiometry_matrix = to_sympy_matrix([[1, -1, -1, 0, 0, 0], [0, 0, 0, 1, -1, 0], [0, 0, 0, 0, 1, -1]]) answer = eq_central_moments(counter, mcounter, m, species, propensities, stoichiometry_matrix, 2) expected = to_sympy_matrix([ [" 2*c_4*y_1*y_2 + c_4*y_1 - 2*c_5*y_2**2 + c_5*y_2 - 2*y_1*(c_4*y_1 - c_5*y_2)"," -2*c_5"," 2*c_4"," 0"," 0"," 0"," 0"], ["c_3*y_0*y_2 + c_4*y_1**2 - c_4*y_1*y_2 - c_4*y_1 - c_5*y_1*y_2 - y_1*(c_3*y_0 - c_4*y_1) - y_2*(c_4*y_1 - c_5*y_2)"," 0"," -c_4 - c_5"," c_4"," c_3"," 0"," 0"], ["2*c_3*y_0*y_1 + c_3*y_0 - 2*c_4*y_1**2 + c_4*y_1 - 2*y_2*(c_3*y_0 - c_4*y_1)"," 0"," 0"," -2*c_4"," 0"," 2*c_3","0"], ["c_0*y_2 - c_1*y_0*y_2 - c_2*y_0*y_2**2/(c_6 + y_0) + c_4*y_0*y_1 - c_5*y_0*y_2 - y_1*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) - y_3*(c_4*y_1 - c_5*y_2)"," -c_2*y_0/(c_6 + y_0)"," 0"," 0"," -c_1 + 2*c_2*y_0*y_2/(c_6 + y_0)**2 - 2*c_2*y_2/(c_6 + y_0) - c_5 - y_1*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))","c_4"," -c_2*y_0*y_2**2/(c_6 + y_0)**3 + c_2*y_2**2/(c_6 + y_0)**2 - y_1*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)"], ["c_0*y_1 - c_1*y_0*y_1 - c_2*y_0*y_1*y_2/(c_6 + y_0) + c_3*y_0**2 - c_4*y_0*y_1 - y_2*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) - y_3*(c_3*y_0 - c_4*y_1)"," 0"," -c_2*y_0/(c_6 + y_0)"," 0"," c_2*y_0*y_1/(c_6 + y_0)**2 - c_2*y_1/(c_6 + y_0) - y_2*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))"," -c_1 + c_2*y_0*y_2/(c_6 + y_0)**2 - c_2*y_2/(c_6 + y_0) - c_4"," -c_2*y_0*y_1*y_2/(c_6 + y_0)**3 + c_2*y_1*y_2/(c_6 + y_0)**2 + c_3 - y_2*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)"], ["2*c_0*y_0 + c_0 - 2*c_1*y_0**2 + c_1*y_0 - 2*c_2*y_0**2*y_2/(c_6 + y_0) + c_2*y_0*y_2/(c_6 + y_0) - 2*y_3*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0))"," 0"," 0"," 0"," 2*c_2*y_0**2/(c_6 + y_0)**2 - 4*c_2*y_0/(c_6 + y_0) - c_2*y_0/(c_6 + y_0)**2 + c_2/(c_6 + y_0) - 2*y_3*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))"," 0"," -2*c_1 - 2*c_2*y_0**2*y_2/(c_6 + y_0)**3 + 4*c_2*y_0*y_2/(c_6 + y_0)**2 + c_2*y_0*y_2/(c_6 + y_0)**3 - 2*c_2*y_2/(c_6 + y_0) - c_2*y_2/(c_6 + y_0)**2 - 2*y_3*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)"] ]) assert_sympy_expressions_equal(answer, expected)