Ejemplo n.º 1
0
def save_model_to_file(model, filename):
    """ Save a model to a file based on the extension of the file name.

    Parameters
    ----------
    model : cobra.core.Model
        Model object loaded from file
    filename : str
        Path to model file

    Raises
    ------
    IOError
        If model file extension is not supported.
    """

    (root, ext) = splitext(filename)
    if ext == '.mat':
        save_matlab_model(model, filename)
    elif ext == '.xml' or ext == '.sbml':
        write_sbml_model(model, filename)
    elif ext == '.json':
        save_json_model(model, filename)
    else:
        raise IOError(
            'Model file extension not supported for {0}'.format(filename))
    return
Ejemplo n.º 2
0
 def test_mat_read_write(self):
     test_output_filename = "test_mat_write.mat"
     io.save_matlab_model(self.model, test_output_filename)
     reread = io.load_matlab_model(test_output_filename)
     self.assertEqual(len(self.model.reactions), len(reread.reactions))
     self.assertEqual(len(self.model.metabolites), len(reread.metabolites))
     for i in range(len(self.model.reactions)):
         self.assertEqual(len(self.model.reactions[i]._metabolites), len(reread.reactions[i]._metabolites))
         self.assertEqual(self.model.reactions[i].id, reread.reactions[i].id)
     unlink(test_output_filename)
Ejemplo n.º 3
0
def test_save_matlab_model(tmpdir, mini_model, raven_model):
    """Test the writing of MAT model."""
    mini_output_file = tmpdir.join("mini.mat")
    raven_output_file = tmpdir.join("raven.mat")
    # scipy.io.savemat() doesn't support anything other than
    # str or file-stream object, hence the str conversion
    io.save_matlab_model(mini_model, str(mini_output_file))
    io.save_matlab_model(raven_model, str(raven_output_file))
    assert mini_output_file.check()
    assert raven_output_file.check()
Ejemplo n.º 4
0
def test_save_matlab_model(tmpdir, mini_model, raven_model):
    """Test the writing of MAT model."""
    mini_output_file = tmpdir.join("mini.mat")
    raven_output_file = tmpdir.join("raven.mat")
    # scipy.io.savemat() doesn't support anything other than
    # str or file-stream object, hence the str conversion
    io.save_matlab_model(mini_model, str(mini_output_file))
    io.save_matlab_model(raven_model, str(raven_output_file))
    assert mini_output_file.check()
    assert raven_output_file.check()
Ejemplo n.º 5
0
 def test_mat_read_write(self):
     from warnings import catch_warnings
     test_output_filename = join(gettempdir(), "test_mat_write.mat")
     io.save_matlab_model(self.model, test_output_filename)
     with catch_warnings(record=True) as w:
         reread = io.load_matlab_model(test_output_filename)
     self.assertEqual(len(self.model.reactions), len(reread.reactions))
     self.assertEqual(len(self.model.metabolites), len(reread.metabolites))
     for i in range(len(self.model.reactions)):
         self.assertEqual(len(self.model.reactions[i]._metabolites), \
             len(reread.reactions[i]._metabolites))
         self.assertEqual(self.model.reactions[i].id, reread.reactions[i].id)
     unlink(test_output_filename)
Ejemplo n.º 6
0
 def test_mat_read_write(self):
     """read and write COBRA toolbox models in .mat files"""
     from warnings import catch_warnings
     test_output_filename = join(gettempdir(), "test_mat_write.mat")
     io.save_matlab_model(self.model, test_output_filename)
     with catch_warnings(record=True) as w:
         reread = io.load_matlab_model(test_output_filename)
     self.assertEqual(len(self.model.reactions), len(reread.reactions))
     self.assertEqual(len(self.model.metabolites), len(reread.metabolites))
     for i in range(len(self.model.reactions)):
         self.assertEqual(len(self.model.reactions[i]._metabolites), \
             len(reread.reactions[i]._metabolites))
         self.assertEqual(self.model.reactions[i].id,
                          reread.reactions[i].id)
     unlink(test_output_filename)
Ejemplo n.º 7
0
def mvgem(input_model, output_model):
    """Convert GEM format INPUT_MODEL to OUTPUT_MODEL.

    The format will be infered based on the extension of OUTPUT_MODEL.
    Supported formats: .sbml, .xml, .json, .mat
    """
    model = load_model(input_model)
    out_format = get_destination_format(output_model)
    if out_format == "SBML":
        write_sbml_model(model, output_model)
    elif out_format == "MAT":
        save_matlab_model(model, output_model)
    elif out_format == "JSON":
        save_json_model(model, output_model)
    else:
        click.BadParameter(
            "Output format %s could not be recognised" % out_format
        ).show()
Ejemplo n.º 8
0
    mini.reactions.get_by_id(i).lower_bound = mini.reactions.PGI.lower_bound
# set names and annotation
for g in mini.genes:
    try:
        tg = textbook.genes.get_by_id(g.id)
    except KeyError:
        continue
    g.name = tg.name
    g.annotation = tg.annotation
mini.reactions.sort()
mini.genes.sort()
mini.metabolites.sort()
# output to various formats
with open("mini.pickle", "wb") as outfile:
    dump(mini, outfile, protocol=2)
save_matlab_model(mini, "mini.mat")
save_json_model(mini, "mini.json", pretty=True)
write_sbml_model(mini, "mini_fbc2.xml")
write_sbml_model(mini, "mini_fbc2.xml.bz2")
write_sbml_model(mini, "mini_fbc2.xml.gz")
write_sbml2(mini, "mini_fbc1.xml", use_fbc_package=True)
write_sbml_model(mini, "mini_cobra.xml", use_fbc_package=False)

# fva results
fva_result = cobra.flux_analysis.flux_variability_analysis(textbook)
clean_result = OrderedDict()
for key in sorted(fva_result):
    clean_result[key] = {k: round(v, 5) for k, v in fva_result[key].items()}
with open("textbook_fva.json", "w") as outfile:
    json_dump(clean_result, outfile)
Ejemplo n.º 9
0
def raise_scipy_errors():
    with pytest.raises(ImportError):
        io.save_matlab_model(None, 'test')
    with pytest.raises(ImportError):
        io.load_matlab_model('test')
Ejemplo n.º 10
0
    mini.reactions.get_by_id(i).lower_bound = mini.reactions.PGI.lower_bound
# set names and annotation
for g in mini.genes:
    try:
        tg = textbook.genes.get_by_id(g.id)
    except KeyError:
        continue
    g.name = tg.name
    g.annotation = tg.annotation
mini.reactions.sort()
mini.genes.sort()
mini.metabolites.sort()
# output to various formats
with open("mini.pickle", "wb") as outfile:
    dump(mini, outfile, protocol=2)
save_matlab_model(mini, "mini.mat")
save_json_model(mini, "mini.json", pretty=True)
write_sbml_model(mini, "mini_fbc2.xml")
write_sbml_model(mini, "mini_fbc2.xml.bz2")
write_sbml_model(mini, "mini_fbc2.xml.gz")
write_sbml2(mini, "mini_fbc1.xml", use_fbc_package=True)
write_sbml_model(mini, "mini_cobra.xml", use_fbc_package=False)
raven = load_matlab_model("raven.mat")
with open("raven.pickle", "wb") as outfile:
    dump(raven, outfile, protocol=2)

# fva results
fva_result = cobra.flux_analysis.flux_variability_analysis(textbook)
clean_result = OrderedDict()
for key in sorted(fva_result):
    clean_result[key] = {k: round(v, 5) for k, v in fva_result[key].items()}
Ejemplo n.º 11
0
from cobra.test import create_test_model

model_names = ['salmonella', 'iJO1366', 'Yersinia_pestis_CO92_iPC815']

for model_name in model_names:
    # read in old pickle and model from sbml
    model_pickle = model_name + '.pickle'
    if model_name == "iJO1366":
        new_model = read_legacy_sbml(model_name + '.xml')
    else:
        new_model = read_sbml_model(model_name + '.xml')
    # update other attributes
    if isfile(model_name + ".genes"):
        with open(model_name + ".genes", "rb") as infile:
            gene_names = load(infile)
        for gene in new_model.genes:
            gene.name = gene_names[gene.id]
    if isfile(model_name + ".media"):
        with open(model_name + ".media", "rb") as infile:
            new_model.media_compositions = load(infile)
    new_model._cobra_version = get_version()
    # write out new pickle
    with open(model_pickle, 'wb') as outfile:
        dump(new_model, outfile, protocol=2)
    # write out other formats for iJO1366
    if model_name == "iJO1366":
        save_matlab_model(new_model, model_name + ".mat")
        save_json_model(new_model, model_name + ".json")
    if model_name == "salmonella":
        write_sbml_model(new_model, model_name + "_fbc.xml")
Ejemplo n.º 12
0
def raise_scipy_errors():
    with pytest.raises(ImportError):
        io.save_matlab_model(None, 'test')
    with pytest.raises(ImportError):
        io.load_matlab_model('test')