def __init__(self, model=None, path=None, use_second_model=False): """ Assumes projectConfig class works correctly """ self.path = path if self.path is None: self.path = tempfile.mkdtemp() self.mdl_path = tempfile.mkstemp() self.model = model if self.model is None: self.model = load_model(_IAF_MODEL_PATH) self.second_model = None if use_second_model: self.second_model = load_model(_CORE_MODEL_PATH)
def test_gene_removal(): model_a = load_model(_CORE_MODEL_PATH) model_b = load_model(_CORE_MODEL_PATH) cobra.manipulation.remove_genes(model_b, ['b0008'], remove_reactions=False) diff = gsmodutils.model_diff.model_diff(model_a, model_b) assert len(diff['removed_genes']) == 1 diff._repr_html_() model_a = load_model(_CORE_MODEL_PATH) model_b = load_model(_CORE_MODEL_PATH) cobra.manipulation.remove_genes(model_a, ['b0008'], remove_reactions=False) diff = gsmodutils.model_diff.model_diff(model_a, model_b) assert len(diff['genes']) == 1 diff._repr_html_()
def iconditions(path, ident, project_path, apply_to, growth): """ Add a given set of media condtions from a model (this ignores any added or removed reactions or metabolites)""" model = load_model(path) project = _load_project(project_path) project.save_conditions(model, ident, apply_to=apply_to, observe_growth=growth)
def test_reaction_lb_change(): """ Tests that changing the lower bound on a reaction causes it to be included in the diff The example also checks that the change goes from L -> R """ model_a = load_model(_CORE_MODEL_PATH) model_b = load_model(_CORE_MODEL_PATH) model_b.reactions.ATPM.lower_bound = 8.0 diff = gsmodutils.model_diff.model_diff(model_a, model_b) assert len(diff['removed_reactions']) == 0 assert len(diff['removed_metabolites']) == 0 assert len(diff['metabolites']) == 0 assert len(diff['reactions']) == 1 assert diff['reactions'][0]['id'] == model_b.reactions.ATPM.id assert diff['reactions'][0][ 'lower_bound'] == model_b.reactions.ATPM.lower_bound diff._repr_html_()
def test_model_ident(): """ Tests that an identical model does not produce any differences """ model_a = load_model(_CORE_MODEL_PATH) diff = gsmodutils.model_diff.model_diff(model_a, model_a.copy()) assert len(diff['removed_reactions']) == 0 assert len(diff['removed_metabolites']) == 0 assert len(diff['reactions']) == 0 assert len(diff['metabolites']) == 0 diff._repr_html_()
def test_metabolite_formula_change(): """ This tests that a small (but important) forumla change to a metabolite is picked up Note the test also makes sure that the metabolite included in the change goes from L -> R """ model_a = load_model(_CORE_MODEL_PATH) model_b = load_model(_CORE_MODEL_PATH) model_a.metabolites.h2o_c.formula = 'H202' diff = gsmodutils.model_diff.model_diff(model_a, model_b) assert len(diff['removed_reactions']) == 0 assert len(diff['removed_metabolites']) == 0 assert len(diff['reactions']) == 0 assert len(diff['metabolites']) == 1 assert diff['metabolites'][0]['id'] == model_a.metabolites.h2o_c.id assert diff['metabolites'][0][ 'formula'] == model_b.metabolites.h2o_c.formula diff._repr_html_()
def dimport(model_path, identifier, name, description, project_path, parent, base_model, overwrite, from_diff): """ Import a design into a model. This can be new or overwrite an existing design. """ project = _load_project(project_path) try: if from_diff: with open(model_path) as diff_file: df = json.load(diff_file) # model_path is a json diff file model = project.load_diff(df, base_model=base_model) else: model = load_model(model_path) except ValidationError as exp: click.echo('Validation Error with design: {}'.format(exp)) exit(-1) # Check if the design already exists new = True if identifier in project.list_designs and not overwrite: click.echo( 'Error: Design {} already exists. Use --overwrite to replace'. format(identifier)) exit(-2) elif identifier in project.list_designs: new = False if parent is not None and parent not in project.list_designs: click.echo('Error: Parent design {} does not exist'.format(parent)) exit(-3) if name is None and new: name = click.prompt('Please enter a name for this design', type=str) if description is None and new: description = click.prompt( 'Please enter a description for this design', type=str) try: project.save_design(model, identifier, name=name, description=description, parent=parent, base_model=base_model, overwrite=overwrite) except ValidationError as exp: click.echo('Validation Error with design: {}'.format(exp)) exit(-4) click.echo('Design successfully added to project')
def diff(model_path, base_model, project_path, parent, output, names): """ View the changed reactions between a model and a base model """ project = _load_project(project_path) base_model = project.load_model(base_model) if parent is not None: base_model = project.load_design(parent, base_model) nmdl = load_model(model_path) click.echo('Comparing models...') mdiff = model_diff(base_model, nmdl) click.echo('new model has {} removed reactions'.format( len(mdiff['removed_reactions']))) click.echo('new model has {} added or changed reactions'.format( len(mdiff['reactions']))) click.echo('new model has {} removed metabolites'.format( len(mdiff['removed_metabolites']))) click.echo('new model has {} added or changed metabolites'.format( len(mdiff['metabolites']))) if names: if len(mdiff['removed_reactions']): click.echo('Removed reactions:') for reaction in mdiff['removed_reactions']: click.echo('\t {}'.format(reaction)) if len(mdiff['reactions']): click.echo('Added or changed reactions:') for reaction in mdiff['reactions']: click.echo('\t {} - {}'.format(reaction['id'], reaction['name'])) if len(mdiff['removed_metabolites']): click.echo('Removed metabolites:') for metabolite in mdiff['removed_metabolites']: click.echo('\t {}'.format(metabolite)) if len(mdiff['metabolites']): click.echo('Added or changed metabolites:') for metabolite in mdiff['metabolites']: click.echo('\t {} - {}'.format(metabolite['id'], metabolite['name'])) if output is not None: with open(output, 'w+') as outfile: json.dump(mdiff, outfile)
def test_addmodel_validation(): # Test adding a model that fails validation with FakeProjectContext() as ctx: runner = CliRunner() # Create a fake model which can't grow npath = os.path.join(ctx.path, 'tmodel.xml') model = load_model(_CORE_MODEL_PATH) for media in model.medium: reaction = model.reactions.get_by_id(media) reaction.lower_bound = 0 reaction.upper_bound = 0 cobra.io.write_sbml_model(model, npath) # Try adding it to the project, it should fail with validation on result = runner.invoke(gsmodutils.cli.addmodel, [npath, '--project_path', ctx.path]) assert result.exit_code == -1 # Pass with validation off result = runner.invoke( gsmodutils.cli.addmodel, [npath, '--project_path', ctx.path, '--no-validate']) assert result.exit_code == 0
def test_export(fmt, excode): with FakeProjectContext() as ctx: runner = CliRunner() opt = '{}/testmdl.{}'.format(ctx.path, fmt) with CleanUpFile(opt): result = runner.invoke(gsmodutils.cli.export, [fmt, opt, '--project_path', ctx.path]) assert result.exit_code == excode if excode == 0: # Should create the file as well as returning saying it has! assert os.path.exists(opt) # Model should load again without exceptions load_model(opt) # Should not allow overwriting/removing without flag result = runner.invoke(gsmodutils.cli.export, [fmt, opt, '--project_path', ctx.path]) assert result.exit_code == -1 assert os.path.exists(opt) os.remove(opt) # Test export of designs and conditions project = GSMProject(ctx.path) model = project.load_model() model.reactions.EX_xyl__D_e.lower_bound = -8.00 model.reactions.EX_glc__D_e.lower_bound = 0.0 project.save_conditions(model, 'xylose_growth') model.remove_reactions(["EX_glc__D_e"]) project.save_design(model, 'tt1', 'tt1') result = runner.invoke( gsmodutils.cli.export, [fmt, opt, '--project_path', ctx.path, '--design', 'tt1']) assert result.exit_code == 0 assert os.path.exists(opt) # test that design works correctly # more covered by tests for StrainDesign class, this just checks it is actually exporting a design l_model = load_model(opt) assert "EX_glc__D_e" not in l_model.reactions os.remove(opt) assert not os.path.exists(opt) # The same applies for exporting conditions result = runner.invoke(gsmodutils.cli.export, [ fmt, opt, '--project_path', ctx.path, '--conditions', 'xylose_growth' ]) assert result.exit_code == 0 assert os.path.exists(opt) l_model = load_model(opt) assert l_model.reactions.EX_xyl__D_e.lower_bound == -8.00 assert l_model.reactions.EX_glc__D_e.lower_bound == 0.0
def test_model_error(): model_a = load_model(_CORE_MODEL_PATH) model_b = None with pytest.raises(TypeError): gsmodutils.model_diff.model_diff(model_a, model_b)