Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/KarrLab/wc_lang
Browse files Browse the repository at this point in the history
  • Loading branch information
artgoldberg committed Apr 20, 2018
2 parents 9a2999c + ef150e2 commit 04cb57d
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 186 deletions.
1 change: 1 addition & 0 deletions .karr_lab_build_utils.yml
Expand Up @@ -4,6 +4,7 @@ downstream_dependencies:
- mycoplasma_pneumoniae
- random_wc_model_generator
- wc_analysis
- wc_model_gen
- wc_sim
email_notifications:
- jonrkarr@gmail.com
Expand Down
45 changes: 44 additions & 1 deletion tests/test_io.py
Expand Up @@ -12,7 +12,7 @@
RateLaw, RateLawEquation, SubmodelAlgorithm, Concentration)
from wc_lang import io
from wc_lang.io import Writer, Reader, convert, create_template
from wc_utils.workbook.io import read as read_workbook
from wc_utils.workbook.io import read as read_workbook, write as write_workbook
import obj_model.io
import os
import shutil
Expand Down Expand Up @@ -181,6 +181,24 @@ def test_write_read(self):
self.assertTrue(model.is_equal(self.model))
self.assertEqual(self.model.difference(model), '')

def test_write_read_sloppy(self):
filename = os.path.join(self.dirname, 'model.xlsx')

Writer().run(self.model, filename)

wb = read_workbook(filename)
row = wb['Model'].pop(0)
wb['Model'].insert(1, row)
write_workbook(filename, wb)

with self.assertRaisesRegexp(ValueError, 'The attributes must be defined in this order'):
Reader().run(filename)
model = Reader().run(filename, strict=False)
self.assertEqual(model.validate(), None)

self.assertTrue(model.is_equal(self.model))
self.assertEqual(self.model.difference(model), '')

def test_convert(self):
filename_xls1 = os.path.join(self.dirname, 'model1.xlsx')
filename_xls2 = os.path.join(self.dirname, 'model2.xlsx')
Expand All @@ -198,6 +216,31 @@ def test_convert(self):
model = Reader().run(filename_xls2)
self.assertTrue(model.is_equal(self.model))

def test_convert_sloppy(self):
filename_xls1 = os.path.join(self.dirname, 'model1.xlsx')
filename_xls2 = os.path.join(self.dirname, 'model2.xlsx')
filename_csv = os.path.join(self.dirname, 'model-*.csv')

Writer().run(self.model, filename_xls1)

wb = read_workbook(filename_xls1)
row = wb['Model'].pop(0)
wb['Model'].insert(1, row)
write_workbook(filename_xls1, wb)

with self.assertRaisesRegexp(ValueError, 'The attributes must be defined in this order'):
convert(filename_xls1, filename_csv)
convert(filename_xls1, filename_csv, strict=False)

self.assertTrue(os.path.isfile(os.path.join(self.dirname, 'model-Model.csv')))
self.assertTrue(os.path.isfile(os.path.join(self.dirname, 'model-Taxon.csv')))
model = Reader().run(filename_csv)
self.assertTrue(model.is_equal(self.model))

convert(filename_csv, filename_xls2)
model = Reader().run(filename_xls2)
self.assertTrue(model.is_equal(self.model))


class TestExampleModel(unittest.TestCase):

Expand Down
12 changes: 6 additions & 6 deletions tests/test_main.py
Expand Up @@ -97,20 +97,20 @@ def test_transform(self):
model = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.0')
Writer().run(model, source)

destination = path.join(self.tempdir, 'destination.xlsx')
with __main__.App(argv=['transform', source, destination, '--transform', 'MergeAlgorithmicallyLikeSubmodels']) as app:
dest = path.join(self.tempdir, 'dest.xlsx')
with __main__.App(argv=['transform', source, dest, '--transform', 'MergeAlgorithmicallyLikeSubmodels']) as app:
app.run()

self.assertTrue(path.isfile(destination))
self.assertTrue(path.isfile(dest))

def test_transform_exception(self):
source = path.join(self.tempdir, 'source.xlsx')
model = Model(id='model', name='test model', version='0.0.1a', wc_lang_version='0.0.0')
Writer().run(model, source)

destination = path.join(self.tempdir, 'destination.xlsx')
dest = path.join(self.tempdir, 'dest.xlsx')
with self.assertRaisesRegexp(ValueError, 'Please select at least one transform'):
with __main__.App(argv=['transform', source, destination]) as app:
with __main__.App(argv=['transform', source, dest]) as app:
app.run()

def test_normalize(self):
Expand All @@ -128,7 +128,7 @@ def test_normalize(self):
self.assertTrue(model2.is_equal(model))

# with different destination
with __main__.App(argv=['normalize', filename_xls_1, '--destination', filename_xls_2]) as app:
with __main__.App(argv=['normalize', filename_xls_1, '--dest', filename_xls_2]) as app:
app.run()

model2 = Reader().run(filename_xls_2)
Expand Down
51 changes: 0 additions & 51 deletions tests/test_model_gen.py

This file was deleted.

1 change: 0 additions & 1 deletion wc_lang/__init__.py
Expand Up @@ -13,7 +13,6 @@
DatabaseReference)
from . import config
from . import io
from . import model_gen
from . import prepare
from . import rate_law_utils
from . import sbml
Expand Down
43 changes: 28 additions & 15 deletions wc_lang/__main__.py
Expand Up @@ -37,13 +37,15 @@ class Meta:
stacked_type = 'nested'
arguments = [
(['path'], dict(type=str, help='Path to model definition')),
(['--sloppy'], dict(dest='strict', default=True, action='store_false',
help='If set, do not validate the format of the model file(s)')),
]

@expose(hide=True)
def default(self):
args = self.app.pargs
try:
Reader().run(args.path) # reader already does validation
Reader().run(args.path, strict=args.strict) # reader already does validation
print('Model is valid')
except ValueError as exception:
raise ValueError('Model is invalid: ' + str(exception))
Expand All @@ -62,6 +64,8 @@ class Meta:
(['path_2'], dict(type=str, help='Path to second model definition')),
(['--compare-files'], dict(dest='compare_files', default=False, action='store_true',
help='If true, compare models; otherwise compare files directly')),
(['--sloppy'], dict(dest='strict', default=True, action='store_false',
help='If set, do not validate the format of the model file(s)')),
]

@expose(hide=True)
Expand All @@ -74,8 +78,8 @@ def default(self):
diff = model1.difference(model2)

else:
model1 = Reader().run(args.path_1)
model2 = Reader().run(args.path_2)
model1 = Reader().run(args.path_1, strict=args.strict)
model2 = Reader().run(args.path_2, strict=args.strict)
diff = model1.difference(model2)

if diff:
Expand All @@ -99,9 +103,11 @@ class Meta:
stacked_type = 'nested'
arguments = [
(['source'], dict(type=str, help='Path to model definition')),
(['destination'], dict(type=str, help='Path to save transformed model definition')),
(['dest'], dict(type=str, help='Path to save transformed model definition')),
(['--transform'], dict(dest='transforms', action='append',
help='Model transform:' + transform_list)),
(['--sloppy'], dict(dest='strict', default=True, action='store_false',
help='If set, do not validate the format of the model file(s)')),
]

@expose(hide=True)
Expand All @@ -112,7 +118,7 @@ def default(self):
raise ValueError('Please select at least one transform')

# read model
model = Reader().run(args.source)
model = Reader().run(args.source, strict=args.strict)

# apply transforms
transforms = transform.get_transforms()
Expand All @@ -122,7 +128,7 @@ def default(self):
instance.run(model)

# write model
Writer().run(model, args.destination)
Writer().run(model, args.dest)


class NormalizeController(CementBaseController):
Expand All @@ -135,36 +141,41 @@ class Meta:
stacked_type = 'nested'
arguments = [
(['source'], dict(type=str, help='Path to model definition')),
(['--destination'], dict(default='', type=str, help='Path to save normalized model definition')),
(['--dest'], dict(default='', type=str, help='Path to save normalized model definition')),
(['--sloppy'], dict(dest='strict', default=True, action='store_false',
help='If set, do not validate the format of the model file(s)')),
]

@expose(hide=True)
def default(self):
args = self.app.pargs
model = Reader().run(args.source)
if args.destination:
Writer().run(model, args.destination)
model = Reader().run(args.source, strict=args.strict)
if args.dest:
Writer().run(model, args.dest)
else:
Writer().run(model, args.source)


class ConvertController(CementBaseController):
""" Convert model definition among Excel (.xlsx), comma separated (.csv), and tab separated formats (.tsv) """
""" Convert model definition among Excel (.xlsx), comma separated (.csv), JavaScript Object Notation (.json),
tab separated (.tsv), and Yet Another Markup Language (.yaml, .yml) formats """

class Meta:
label = 'convert'
description = 'Convert model definition among Excel (.xlsx), comma separated (.csv), and tab separated formats (.tsv)'
description = 'Convert model definition among .csv, .json, .tsv, .xlsx, .yaml, and .yml formats'
stacked_on = 'base'
stacked_type = 'nested'
arguments = [
(['source'], dict(type=str, help='Path to model definition')),
(['destination'], dict(type=str, help='Path to save model in converted format')),
(['dest'], dict(type=str, help='Path to save model in converted format')),
(['--sloppy'], dict(dest='strict', default=True, action='store_false',
help='If set, do not validate the format of the model file(s)')),
]

@expose(hide=True)
def default(self):
args = self.app.pargs
convert(args.source, args.destination)
convert(args.source, args.dest, strict=args.strict)


class CreateTemplateController(CementBaseController):
Expand Down Expand Up @@ -195,12 +206,14 @@ class Meta:
stacked_type = 'nested'
arguments = [
(['path'], dict(type=str, help='Path to model')),
(['--sloppy'], dict(dest='strict', default=True, action='store_false',
help='If set, do not validate the format of the model file(s)')),
]

@expose(hide=True)
def default(self):
args = self.app.pargs
model = Reader().run(args.path)
model = Reader().run(args.path, strict=args.strict)
model.wc_lang_version = wc_lang.__version__
Writer().run(model, args.path)

Expand Down

0 comments on commit 04cb57d

Please sign in to comment.