EnzymeML / PyEnzyme

🧬 - Data management and modeling framework based on EnzymeML.
BSD 2-Clause "Simplified" License
23 stars 9 forks source link

Measurement.exportData() no longer works #13

Closed jmrohwer closed 3 years ago

jmrohwer commented 3 years ago

After the reaction hierarchy has been removed from the Measurement class, the exportData() method no longer works:

m1 = enzmldoc.getMeasurementDict()['m1']

m1.exportData()

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-4-f873839397e7> in <module>
----> 1 m1.exportData()

~/src/git/EnzymeML/PyEnzyme/pyenzyme/enzymeml/core/measurement.py in exportData(self, enzmldoc)
     84 
     85             proteins = self.__combineReplicates(
---> 86                 measurementSpecies=reaction['proteins'],
     87                 reactionID=reactionID,
     88                 enzmldoc=enzmldoc,

KeyError: 'proteins'
jmrohwer commented 3 years ago

It would be fixed by the following diff:

diff --git a/pyenzyme/enzymeml/core/measurement.py b/pyenzyme/enzymeml/core/measurement.py
index bd53a83..b86fd8f 100644
--- a/pyenzyme/enzymeml/core/measurement.py
+++ b/pyenzyme/enzymeml/core/measurement.py
@@ -79,33 +79,28 @@ class Measurement(EnzymeMLBase):

         measurements = dict()

-        # Combine Replicate objects for each reaction
-        for reactionID, reaction in self.__speciesDict.items():
-
-            proteins = self.__combineReplicates(
-                measurementSpecies=reaction['proteins'],
-                reactionID=reactionID,
-                enzmldoc=enzmldoc,
-                speciesType='proteins'
-            )
-            reactants = self.__combineReplicates(
-                measurementSpecies=reaction['reactants'],
-                reactionID=reactionID,
-                enzmldoc=enzmldoc,
-                speciesType='reactants'
-            )
+        # Combine Replicate objects
+        proteins = self.__combineReplicates(
+            measurementSpecies=self.__speciesDict['proteins'],
+            enzmldoc=enzmldoc,
+            speciesType='proteins'
+        )
+        reactants = self.__combineReplicates(
+            measurementSpecies=self.__speciesDict['reactants'],
+            enzmldoc=enzmldoc,
+            speciesType='reactants'
+        )

-            measurements[reactionID] = {
-                "proteins": proteins,
-                "reactants": reactants
-            }
+        measurements = {
+            "proteins": proteins,
+            "reactants": reactants
+        }

         return measurements

     def __combineReplicates(
         self,
         measurementSpecies,
-        reactionID,
         enzmldoc,
         speciesType
     ):
@@ -114,7 +109,6 @@ class Measurement(EnzymeMLBase):
         columns = [self.__globalTime]
         header = [f"time/{self.__globalTimeUnit}"]
         initConcs = dict()
-        stoichiometries = dict()

         # Iterate over measurementData to fill columns
         for speciesID, data in measurementSpecies.items():
@@ -133,19 +127,12 @@ class Measurement(EnzymeMLBase):
             # Fetch initial concentration
             initConcs[speciesID] = (data.getInitConc(), data.getUnit())

-            # Fetch the stoichiometry of the reactant
-            if speciesType == 'reactants' and enzmldoc is not None:
-                stoichiometries[speciesID] = self.__getStoichiometry(
-                    reaction=enzmldoc.getReaction(reactionID),
-                    speciesID=speciesID
-                )
-
-        if len(columns) > 1:
-            return {
-                "data": pd.DataFrame(np.array(columns).T, columns=header),
-                "initConc": initConcs,
-                "stoichiometries": stoichiometries
-            }
+        return {
+            'data': pd.DataFrame(np.array(columns).T, columns=header)
+            if len(columns) > 1
+            else None,
+            'initConc': initConcs,
+        }

     def __getStoichiometry(self, reaction, speciesID):
JR-1991 commented 3 years ago

Thanks for bringing this up! The function was fixed in cc7929c.