def create_smatrix(cpids, rctnids, stm, obfunc): """Create the stiochiomatrix with the fba module""" # Use stoichiometric Dictionary to create S matrix # Preallocate size of array st_mat = [[0.0] * len(rctnids) for i in cpids] if verbose: print(f"Number of total compounds: {len(cpids)}") print(f"Number of total reactions: {len(rctnids)}") print( f"Stiochiomatrix dimensions before fill: {len(st_mat)} x {len(st_mat[0])}" ) # Fill in S matrix for cidx, c in enumerate(cpids): for ridx, r in enumerate(rctnids): try: val = stm[c][r] except KeyError: val = 0.0 st_mat[cidx][ridx] = val if verbose: print( f"Stiochiomatrix dimensions after fill: fill: {len(st_mat)} x {len(st_mat[0])}" ) # Load the data lp.load(st_mat, cpids, rctnids) # Objective function lp.objective_coefficients(obfunc) return cpids, rctnids
def createSMatrix(cpdIds, rxnIds, sm, objFunc): """Create the SMatrix with the fba module""" # Use stoichiometric Dictionary to create S matrix # Preallocate size of array SMat = [[0.0] * len(rxnIds) for i in cpdIds] if verbose: print "Number of total compounds: %d" % len(cpdIds) print "Number of total reactions: %d" % len(rxnIds) print "SMat dimensions before fill: %d x %d" % (len(SMat), len( SMat[0])) # Fill in S matrix for cIdx, c in enumerate(cpdIds): for rIdx, r in enumerate(rxnIds): try: val = sm[c][r] except KeyError: val = 0.0 SMat[cIdx][rIdx] = val if verbose: print "SMat dimensions after fill: %d x %d" % (len(SMat), len(SMat[0])) # Load the data lp.load(SMat, cpdIds, rxnIds) # Objective function lp.objective_coefficients(objFunc) return cpdIds, rxnIds
def createSMatrix(cpdIds, rxnIds, sm, objFunc): """Create the SMatrix with the fba module""" # Use stoichiometric Dictionary to create S matrix # Preallocate size of array SMat = [[0.0] * len(rxnIds) for i in cpdIds] if verbose: print "Number of total compounds: %d" % len(cpdIds) print "Number of total reactions: %d" % len(rxnIds) print "SMat dimensions before fill: %d x %d" % (len(SMat), len(SMat[0])) # Fill in S matrix for cIdx, c in enumerate(cpdIds): for rIdx, r in enumerate(rxnIds): try: val = sm[c][r] except KeyError: val = 0.0 SMat[cIdx][rIdx] = val if verbose: print "SMat dimensions after fill: %d x %d" % (len(SMat), len(SMat[0])) # Load the data lp.load(SMat, cpdIds, rxnIds) # Objective function lp.objective_coefficients(objFunc) return cpdIds, rxnIds
def test_matrix(self): """Just initialize with a simple matrix""" mat = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ] lp.load(mat)
def test_objective_coeff(self): """Test adding the objective coefficients. Note that there should be as many coefficients as columns in the matrix.""" mat = [ [ 1.0, 1.0, 1.0], [10.0, 4.0, 5.0], [ 2.0, 2.0, 6.0], [ 2.0, 2.0, 6.0], ] lp.load(mat) lp.objective_coefficients([ 10.0, 6.0, 4.0 ])
def test_solve(self): """Test the complete linear programing solution, using the example from the documentation""" mat = [ [ 1.0, 1.0, 1.0], [10.0, 4.0, 5.0], [ 2.0, 2.0, 6.0], ] lp.load(mat) lp.objective_coefficients([ 10.0, 6.0, 4.0 ]) lp.row_bounds([(None, 100.0), (None, 600.0), (None, 300.0)]) lp.col_bounds([(0, None), (0, None), (0, None)]) status, result = lp.solve() r = "%0.3f" % result self.assertEqual(r, "733.333") self.assertEqual(status, 'opt')
def test_bound_cols(self): """Test adding tuples of boundary conditions for columns""" mat = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ] lp.load(mat) boundsc = [] # test that we don't have enough values self.assertRaises(ValueError, lp.col_bounds, boundsc) # a boundary of None should be infiniity! boundsc = [(None, 1000), (1000, None), (1000, 1000), (None, None)] lp.col_bounds(boundsc)
def test_primals(self): """Test getting the primals back as a list""" mat = [ [ 1.0, 1.0, 1.0], [10.0, 4.0, 5.0], [ 2.0, 2.0, 6.0], ] rh = ['a', 'b', 'c'] ch = ['x', 'y', 'z'] lp.load(mat, rh, ch) lp.objective_coefficients([ 10.0, 6.0, 4.0 ]) lp.row_bounds([(None, 100.0), (None, 600.0), (None, 300.0)]) lp.col_bounds([(0, None), (0, None), (0, None)]) status, result = lp.solve() col_pri = [33.333333333333336, 66.66666666666666, 0.0] col_res = lp.col_primals() self.assertEqual(col_pri, col_res)
def test_col_bounds(self): '''Testing the assertion of column bounds''' # define an FBA matrix of the right size newfba = lp.load([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]) ca = PyFBA.metabolism.Compound("A", "e") cb = PyFBA.metabolism.Compound("B", "c") cc = PyFBA.metabolism.Compound("C", "h") compounds = {ca, cb, cc} col_bounds = PyFBA.fba.compound_bounds(compounds) self.assertEqual(col_bounds[ca], (0, 0)) self.assertEqual(col_bounds[cb], (0, 0)) self.assertEqual(col_bounds[cc], (0, 0))
def test_headers(self): """Test adding headers to the rows and columns""" mat = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ] rh = ['a', 'b', 'c'] ch = ['w', 'x', 'y', 'z'] lp.load(mat, rh, ch) rh.append('should fail') self.assertRaises(ValueError, lp.load, mat, rh, ch) rh = ['a', 'b', 'c'] lp.load(mat, rh, ch) ch.append("also should fail") self.assertRaises(ValueError, lp.load, mat, rh, ch)
def test_row_bounds(self): '''Testing the assertion of row bounds and the logic''' # define a media compound media_compound = PyFBA.metabolism.CompoundWithLocation( "m0011", 'Media', 'e') media = {media_compound} # define an FBA matrix of the right size newfba = lp.load([[1, 2, 3, 4, 5, 6]]) # make up some reactions ra = PyFBA.metabolism.Reaction('rA', 'reaction A') ra.direction = '>' rb = PyFBA.metabolism.Reaction('rB', 'reaction B') rb.direction = '<' rc = PyFBA.metabolism.Reaction('rC', 'reaction C') rc.direction = '=' rd = PyFBA.metabolism.Reaction('rUS', 'reaction US') rd.direction = '=' rd.is_uptake_secretion = True re = PyFBA.metabolism.Reaction('biomass_equation', 'biomass_equation') re.direction = '>' rf = PyFBA.metabolism.Reaction('rUSM', 'reaction US Media') rf.direction = '=' rf.is_uptake_secretion = True rf.add_left_compounds({media_compound}) # test our reactions all_reactions = {ra: ra, rb: rb, rc: rc, rd: rd, re: re, rf: rf} reactions2run = set(all_reactions.keys()) rbvals = PyFBA.fba.reaction_bounds(all_reactions, reactions2run, media) self.assertEqual(rbvals[ra], (0, 1000)) self.assertEqual(rbvals[rb], (-1000, 1000)) self.assertEqual(rbvals[rc], (-1000, 1000)) self.assertEqual(rbvals[rd], (0, 1000)) self.assertEqual(rbvals[re], (0, 1000)) self.assertEqual(rbvals[rf], (-1000, 1000))
def test_row_bounds(self): '''Testing the assertion of row bounds and the logic''' # define a media compound media_compound = PyFBA.metabolism.Compound('Media', 'e') media = {media_compound} # define an FBA matrix of the right size newfba = lp.load([[1,2,3,4,5,6]]) # make up some reactions ra = PyFBA.metabolism.Reaction('reaction A') ra.direction = '>' rb = PyFBA.metabolism.Reaction('reaction B') rb.direction = '<' rc = PyFBA.metabolism.Reaction('reaction C') rc.direction = '=' rd = PyFBA.metabolism.Reaction('reaction US') rd.direction = '=' rd.is_uptake_secretion = True re = PyFBA.metabolism.Reaction('biomass_equation') re.direction = '>' rf = PyFBA.metabolism.Reaction('reaction US Media') rf.direction = '=' rf.is_uptake_secretion = True rf.add_left_compounds({media_compound}) # test our reactions all_reactions = {ra: ra, rb: rb, rc: rc, rd: rd, re: re, rf: rf} reactions2run = set(all_reactions.keys()) rbvals = PyFBA.fba.reaction_bounds(all_reactions, reactions2run, media) self.assertEqual(rbvals[ra], (0, 1000)) self.assertEqual(rbvals[rb], (-1000, 1000)) self.assertEqual(rbvals[rc], (-1000, 1000)) self.assertEqual(rbvals[rd], (0, 1000)) self.assertEqual(rbvals[re], (0, 1000)) self.assertEqual(rbvals[rf], (-1000, 1000))