def test_load_glpk_cmd(self): solver = load_pulp_solver(solver_names=['GLPK_CMD']) if not solver: self.skipTest("GLPK_CMD is not available!") else: self.assertListEqual(GLPK_CMD_OPTIONS, solver.options)
def test_load_scip_cmd(self): solver = load_pulp_solver(solver_names=['SCIP_CMD']) if not solver: self.skipTest("SCIP_CMD is not available!") else: self.assertIn("-s", solver.options)
def test_optstoic_glycolysis(): logger = create_logger(name='optstoicpy.script.optstoic_glycolysis.main') #logger.debug('Testing optstoic output filepath: %s', res_dir) logger.info("Test optstoic_glycolysis") pulp_solver = load_pulp_solver( solver_names=[ 'SCIP_CMD', 'GUROBI', 'GUROBI_CMD', 'CPLEX_CMD', 'GLPK_CMD'], logger=logger) test = OptStoicGlycolysis( objective='MinFlux', nATP=1, zlb=10, # setting this may slow down the optimization, but integer cut constraints will work max_iteration=1, pulp_solver=pulp_solver, result_filepath='./result/', M=1000, logger=logger) if sys.platform == 'cygwin': lp_prob, pathways = test.solve_gurobi_cl( outputfile='test_optstoic_cyg.txt', cleanup=False) #test.max_iteration = test.max_iteration + 2 #lp_prob, pathways = test.solve_gurobi_cl(outputfile='test_optstoic_cyg.txt', exclude_existing_solution=True, cleanup=False) else: lp_prob, pathways = test.solve(outputfile='test_optstoic.txt') #test.max_iteration = test.max_iteration + 1 #lp_prob, pathways = test.solve(outputfile='test_optstoic.txt', exclude_existing_solution=True) return lp_prob, pathways
def test_optstoic(): """An alternative to the nosetest due to issue with PULP/SCIP_CMD """ logger = create_logger(name='optstoicpy.script.optstoic.main') logger.info("Test generalized optstoic") # Set the following reactions as allowable export reactions db3 = load_db_v3(reduce_model_size=True, user_defined_export_rxns_Sji={ 'EX_glc': { 'C00031': -1.0 }, 'EX_nad': { 'C00003': -1.0 }, 'EX_adp': { 'C00008': -1.0 }, 'EX_phosphate': { 'C00009': -1.0 }, 'EX_pyruvate': { 'C00022': -1.0 }, 'EX_nadh': { 'C00004': -1.0 }, 'EX_atp': { 'C00002': -1.0 }, 'EX_h2o': { 'C00001': -1.0 }, 'EX_hplus': { 'C00080': -1.0 }, 'EX_nadp': { 'C00006': -1.0 }, 'EX_nadph': { 'C00005': -1.0 } }) #logger.debug('Testing optstoic output filepath: %s', res_dir) pulp_solver = load_pulp_solver(solver_names=[ 'SCIP_CMD', 'GUROBI', 'GUROBI_CMD', 'CPLEX_CMD', 'GLPK_CMD' ], logger=logger) # How to add custom flux constraints: # E.g., # v('EX_nadph') + v('EX_nadh') = 2; # v('EX_nadp') + v('EX_nad') = -2; # v('EX_nadh') + v('EX_nad') = 0; # v('EX_nadph') + v('EX_nadp') = 0; # became # lp_prob += v['EX_nadph'] + v['EX_nadh'] == 2, 'nadphcons1' # lp_prob += v['EX_nadp'] + v['EX_nad'] == -2, 'nadphcons2' # lp_prob += v['EX_nadh'] + v['EX_nad'] == 0, 'nadphcons3' # lp_prob += v['EX_nadph'] + v['EX_nadp'] == 0, 'nadphcons4' custom_flux_constraints = [{ 'constraint_name': 'nadphcons1', 'reactions': ['EX_nadph', 'EX_nadh'], 'UB': 2, 'LB': 2 }, { 'constraint_name': 'nadphcons2', 'reactions': ['EX_nadp', 'EX_nad'], 'UB': -2, 'LB': -2 }, { 'constraint_name': 'nadphcons3', 'reactions': ['EX_nadh', 'EX_nad'], 'UB': 0, 'LB': 0 }, { 'constraint_name': 'nadphcons4', 'reactions': ['EX_nadph', 'EX_nadp'], 'UB': 0, 'LB': 0 }] specific_bounds = { 'EX_glc': { 'LB': -1, 'UB': -1 }, 'EX_pyruvate': { 'LB': 2, 'UB': 2 }, 'EX_nad': { 'LB': -2, 'UB': 0 }, 'EX_nadh': { 'LB': 0, 'UB': 2 }, 'EX_nadp': { 'LB': -2, 'UB': 0 }, 'EX_nadph': { 'LB': 0, 'UB': 2 }, 'EX_adp': { 'LB': -1, 'UB': -1 }, 'EX_phosphate': { 'LB': -1, 'UB': -1 }, 'EX_atp': { 'LB': 1, 'UB': 1 }, 'EX_h2o': { 'LB': 1, 'UB': 1 }, 'EX_hplus': { 'LB': -10, 'UB': 10 } } # pulp/gurobi has issue with "h+" test = OptStoic(database=db3, objective='MinFlux', zlb=None, specific_bounds=specific_bounds, custom_flux_constraints=custom_flux_constraints, add_loopless_constraints=True, max_iteration=2, pulp_solver=pulp_solver, result_filepath='./result/', M=1000, logger=logger) if sys.platform == 'cygwin': lp_prob, pathways = test.solve_gurobi_cl( outputfile='test_optstoic_cyg.txt', cleanup=False) #test.max_iteration = test.max_iteration + 2 #lp_prob, pathways = test.solve_gurobi_cl(outputfile='test_optstoic_cyg.txt', exclude_existing_solution=True, cleanup=False) else: lp_prob, pathways = test.solve(outputfile='test_optstoic.txt') #test.max_iteration = test.max_iteration + 1 #lp_prob, pathways = test.solve(outputfile='test_optstoic.txt', exclude_existing_solution=True) return lp_prob, pathways
def test_at_least_one_pulp_solver_loading(self): pulp_solver = load_pulp_solver(solver_names=ORDERED_SOLVERS) self.assertNotEqual(pulp_solver, None) self.assertIn(pulp_solver.name, ORDERED_SOLVERS)
def setUp(self): self.logger = create_logger(name='Test generalized optstoic') self.pulp_solver = load_pulp_solver(solver_names=ORDERED_SOLVERS) if self.pulp_solver.name not in ['GUROBI', 'GUROBI_CMD', 'CPLEX_CMD']: self.skipTest("Skip because it will take a long time to solve.")
def test_blocked_reactions_analysis(self): """Test blocked reactions analysis. A Pulp solver must be loaded for this test to complete. Otherwise this test will be skipped. """ user_defined_export_rxns_Sji = { 'EX_glc': { 'C00031': -1.0 }, 'EX_nad': { 'C00003': -1.0 }, 'EX_adp': { 'C00008': -1.0 }, 'EX_phosphate': { 'C00009': -1.0 }, 'EX_pyruvate': { 'C00022': -1.0 }, 'EX_nadh': { 'C00004': -1.0 }, 'EX_atp': { 'C00002': -1.0 }, 'EX_h2o': { 'C00001': -1.0 }, 'EX_hplus': { 'C00080': -1.0 }, 'EX_nadp': { 'C00006': -1.0 }, 'EX_nadph': { 'C00005': -1.0 } } db = load_base_reaction_db( user_defined_export_rxns_Sji=user_defined_export_rxns_Sji) custom_flux_constraints = [{ 'constraint_name': 'nadphcons1', 'reactions': ['EX_nadph', 'EX_nadh'], 'UB': 2, 'LB': 2 }, { 'constraint_name': 'nadphcons2', 'reactions': ['EX_nadp', 'EX_nad'], 'UB': -2, 'LB': -2 }, { 'constraint_name': 'nadphcons3', 'reactions': ['EX_nadh', 'EX_nad'], 'UB': 0, 'LB': 0 }, { 'constraint_name': 'nadphcons4', 'reactions': ['EX_nadph', 'EX_nadp'], 'UB': 0, 'LB': 0 }] specific_bounds = { 'EX_glc': { 'LB': -1, 'UB': -1 }, 'EX_pyruvate': { 'LB': 2, 'UB': 2 }, 'EX_nad': { 'LB': -2, 'UB': 0 }, 'EX_nadh': { 'LB': 0, 'UB': 2 }, 'EX_nadp': { 'LB': -2, 'UB': 0 }, 'EX_nadph': { 'LB': 0, 'UB': 2 }, 'EX_adp': { 'LB': -5, 'UB': -1 }, # range 1-5 ATP 'EX_phosphate': { 'LB': -5, 'UB': -1 }, 'EX_atp': { 'LB': 1, 'UB': 5 }, # range 1-5 ATP 'EX_h2o': { 'LB': 1, 'UB': 5 }, 'EX_hplus': { 'LB': -10, 'UB': 10 } } # pulp/gurobi has issue with "h+" pulp_solver = load_pulp_solver(solver_names=ORDERED_SOLVERS, logger=None) if not pulp_solver: self.skipTest( "Solver is not found. Please install at least one solver.") exclude_reactions = load_custom_reactions_to_be_excluded() blocked_reactions_list, FVA_res = blocked_reactions_analysis( database=db, pulp_solver=pulp_solver, specific_bounds=specific_bounds, custom_flux_constraints=custom_flux_constraints, excluded_reactions=exclude_reactions, target_reactions_list=['R01266', 'R07882', 'R00658', 'R01059']) self.assertEqual(set(blocked_reactions_list), set(['R01266', 'R07882']))