Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#652[Feature request] Provide compartments as objects #696

Closed
wants to merge 22 commits into from
Closed

#652[Feature request] Provide compartments as objects #696

wants to merge 22 commits into from

Conversation

MaxGreil
Copy link
Contributor

@MaxGreil MaxGreil commented Apr 18, 2018

#652
Implemented model.compartments as objects, therefore added new class compartment in cobra.core.

  • In cobra.core: modified class cobra.model to store new class compartment correctly.
  • In cobra.io: modified json.py, dict.py, mat.py, sbml.py and sbml3.py.
  • In cobra.manipulation: modified modify.py.
  • In cobra.test.data: run update_pickles.py and modified existing data files according to new class compartment.
  • In cobra.test: modified tests according to new class compartment.

EDIT 20.04.:

  • In cobra.util: modified version_info.py by importing pkg_resources from setuptools for function pkg_resources.working_set. Dropped pip.get_installed_distributions().

EDIT 21.04.:

  • In cobra.test: modified test_io.py to be compartible for pickles from both python2 and python3

@codecov-io
Copy link

codecov-io commented Apr 20, 2018

Codecov Report

Merging #696 into devel will increase coverage by 0.04%.
The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##            devel     #696      +/-   ##
==========================================
+ Coverage   81.72%   81.76%   +0.04%     
==========================================
  Files          36       37       +1     
  Lines        3644     3691      +47     
  Branches      832      843      +11     
==========================================
+ Hits         2978     3018      +40     
- Misses        472      477       +5     
- Partials      194      196       +2
Impacted Files Coverage Δ
cobra/manipulation/modify.py 92.39% <ø> (ø) ⬆️
cobra/util/version_info.py 100% <ø> (ø) ⬆️
cobra/io/json.py 60% <ø> (ø) ⬆️
cobra/io/mat.py 68.2% <100%> (ø) ⬆️
cobra/io/sbml3.py 82.08% <100%> (ø) ⬆️
cobra/io/sbml.py 65.77% <60%> (ø) ⬆️
cobra/core/compartment.py 71.42% <71.42%> (ø)
cobra/core/model.py 90.09% <84.61%> (-0.46%) ⬇️
cobra/io/dict.py 85.12% <94.73%> (+1.47%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e897956...20833b9. Read the comment docs.

@MaxGreil
Copy link
Contributor Author

MaxGreil commented Apr 21, 2018

All TravisCI tests are now working.
The new funcionality of the PR is as requested in #652 . Compartments are now not just a dictionary but objects like all the other model components. They can be accessed like all other model components with model.compartments which returns the DictList containing all compartments of the model.
New class compartment inherits from class object and therefore possesses the same functionality like all other model components.
Code reviewer: @cdiener

@cdiener cdiener self-requested a review April 23, 2018 14:40
@cdiener cdiener mentioned this pull request Apr 24, 2018
Copy link
Member

@Midnighter Midnighter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One concern about the files changed for the tests. Did you intentionally and (more or less) manually change the mini_cobra.xml and mini_fbc*.xml? And why did the textbook* solutions change? If this all happened due to the update script then I would ask you please to checkout the previous versions of the solutions. Simply to ensure that there are no changes creeping in over time.

I want to look at the compartment class a little more but it looks like great work!

@MaxGreil
Copy link
Contributor Author

MaxGreil commented Apr 26, 2018

@Midnighter I only used the update_pickles.py script.
As far as I can tell, the files like mini_cobra.xml and mini_fbc*.xml were shifted around a bit but this did not change the context/variables/values.
The same goes for the textbook* solutions which have no changed values but were also shifted around a bit.

Copy link
Member

@cdiener cdiener left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

model.comaprtments should assemble a list of compartments dynamically from the metabolites in the model like before. For instance the following is now working incorrectly:

In [2]: from cobra.test import create_test_model

In [3]: mod = create_test_model("textbook")

In [5]: mod.compartments
Out[5]: [<Compartment c at 0x7f4d164bf048>, <Compartment e at 0x7f4d164bf0b8>]

In [6]: mod.metabolites.ac_e.compartment = "m"

In [7]: mod.metabolites.ac_e.compartment
Out[7]: 'm'

In [8]: mod.compartments
Out[8]: [<Compartment c at 0x7f4d164bf048>, <Compartment e at 0x7f4d164bf0b8>]

This should include a compartment "m" with an empty name.

Also we will need to maintain backwards compatibility so compartments should be optional in JSON and YAML.

@@ -161,32 +166,6 @@ def get_metabolite_compartments(self):
return {met.compartment for met in self.metabolites
if met.compartment is not None}

@property
def compartments(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should not be removed see general comment.

compartment_list = [x for x in compartment_list
if x.id not in self.compartments]

bad_ids = [m for m in compartment_list
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces should probably be disallowed as well since we will get problems with optlang otherwise.

cobra/io/json.py Outdated
},
"notes": {"type": "object"},
"annotation": {"type": "object"},
},
"required": ["id", "reactions", "metabolites", "genes"],
"required": ["id", "reactions", "metabolites", "genes", "compartments"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to maintain backwards compatibility so compartments should be optional for all input formats.

"c": "cytosol",
"e": "extracellular"
},
"compartments": [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also include a version ("mini_legacy.json") without those to test backwards compatibility.

@@ -1143,6 +1143,10 @@
- name: G_s0001
- id: mini_textbook
- compartments:
e: extracellular
c: cytosol
- !!omap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks a bit weird. What was wrong with the old notation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordered map is introduced by the YAML IO automatically but I agree, let's keep the old format of id: name.

try:
reference_model = load(infile)
except UnicodeDecodeError:
reference_model = load(infile, encoding='latin1')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why that? I think we should not include any files with latin-1. Everything should be unicode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, please store files in utf-8 format.

@@ -182,7 +185,7 @@ def compare_models(cls, name, model1, model2):

@classmethod
def extra_comparisons(cls, name, model1, model2):
assert model1.compartments == model2.compartments
assert len(model1.compartments) == len(model2.compartments)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also check if they contain the same IDs.

@@ -12,6 +12,8 @@

import pkg_resources

import pkg_resources
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that trick!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert the changes to this file since the issue was addressed in #697.

"""
def __init__(self, id=None, name=""):
Object.__init__(self, id, name)
self._model = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it needs a back-ref to the model. That just creates problems.

@cdiener
Copy link
Member

cdiener commented Apr 27, 2018

Thanks for all the work! Sorry that there are so many comments but for new features we have to ensure that nothing breaks. Cheers!

@MaxGreil
Copy link
Contributor Author

MaxGreil commented Apr 29, 2018

Compartments of cobra.model are now a property.
In file test_io.py in case of a UnicodeDecodeError first an encoding with utf-8 is tried and if this fails an encoding with latin1 is tried. This is a work-around when loading python2 pickles in python3. See https://stackoverflow.com/questions/28218466/unpickling-a-python-2-object-with-python-3.

@cdiener
Copy link
Member

cdiener commented May 29, 2018

Appveyor seems to work again.

@Midnighter
Copy link
Member

Dear @MaxGreil, I'm very sorry to be doing this but we need to move things along right now. Thus I took your branch, rebased it on the current devel and will take over development on that new branch. Thank you very much for contribution and I hope this is not offending you. Please see #725 for the new PR.

@Midnighter Midnighter closed this Jun 13, 2018
@MaxGreil
Copy link
Contributor Author

Hi @Midnighter, I didn't have the time to continue working on this issue.

@Midnighter
Copy link
Member

That's completely understandable. Would you like to finish it up, though?

@MaxGreil
Copy link
Contributor Author

I would still like to. What is left to be done?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants