Esempio n. 1
0
def test_cli_tool():
    """ Test the command line interface parser of scrumpy files """
    output_file = ''.join(
        random.choice(string.ascii_uppercase + string.digits)
        for n in range(10)) + '.json'
    output_file_path = os.path.join(tempfile.gettempdir(), output_file)

    # Test loading of scrumpy models
    model = load_model(scrumpy_model_path)
    assert isinstance(model, cobra.Model)

    runner = CliRunner()
    result = runner.invoke(gsmodutils.utils.scrumpy.scrumpy_to_cobra, [
        scrumpy_model_path,
        'MetaSal',
        '--output',
        output_file_path,
        '--fixed_fluxes',
        scrumpy_biomass_path,
        '--name',
        'samonella scrumpy model',
        '--objective',
        'Glc_tx',
    ])

    assert result.exit_code == 0
    model = load_model(output_file_path)
    assert isinstance(model, cobra.Model)
    solution = model.optimize()
    assert solution.objective_value != 0.0
    os.remove(output_file_path)

    # Check the number of reactions is correct
    assert len(model.reactions) == 1229  # 1158 ??
    # Check the number of metabolites is correct
    assert len(model.metabolites) == 1130  # 1129
    # Number of transporters + metabolites should equal scrumpy's number of metabolites
    assert len(model.exchanges) == 71  # 67

    result = runner.invoke(gsmodutils.utils.scrumpy.scrumpy_to_cobra, [
        scrumpy_model_path,
        'MetaSal',
        '--output',
        output_file_path,
        '--media',
        scrumpy_media_path,
        '--name',
        'samonella scrumpy model',
        '--objective',
        'Glc_tx',
    ])
Esempio n. 2
0
def build_universal_model(path,
                          use_cache=True,
                          cache_location='.metacyc_universal.json'):
    """
    Constructs a universal model from all the reactions in the metacyc database

    :param path: path to folder containing metacyc dat files
    :param use_cache: optionally store the resulting model in cached form
    :return:
    """
    try:
        if use_cache and os.path.exists(cache_location):
            model = load_model(cache_location)
            return model
    except (IOError, TypeError):
        pass  # This means cached model couldn't be loaded, we'll create a new one

    db = parse_db(path)
    model = Model()
    for reaction_id in db['reactions']:
        try:
            add_reaction(model, reaction_id, db)
        except KeyError:
            # Exception handling ignores badly formatted reactions
            pass

    if use_cache:
        save_json_model(model, cache_location)

    return model
Esempio n. 3
0
def test_load_model():
    """ Force exceptions to be thrown """
    with pytest.raises(IOError):
        load_model('/this/path/does/not/exist')

    with pytest.raises(TypeError):
        # Create a fake file, format is not valid
        with tempfile.NamedTemporaryFile() as fp:
            load_model(fp.name, file_format="foo")

    with FakeProjectContext() as ctx:
        model = ctx.project.model

        load_medium(model, {}, copy=True)
        with pytest.raises(TypeError):
            load_medium(model, set())

        with pytest.raises(TypeError):
            load_medium(set(), dict())
Esempio n. 4
0
    def _load_cobra_model(self):
        """
        Loads the cobra model
        :return: cobra.Model instance of self
        """
        if self.model_path is not None and not os.path.exists(self.model_path):
            raise IOError("Model file not found")

        if self.design is not None:
            return self.design.load()

        return load_model(self.model_path)
Esempio n. 5
0
def validate_model_file(model_path):
    """
    Simple wrapper to load a model from a given path (handles JSON and SBML)
    """
    model = load_model(model_path)
    return validate_model(model)