Esempio n. 1
0
 def getInferredReaction(self, reaction_operations):
 	"""
 	Create an inferred reaction from reaction_operations.
 	An inferred reaction is a linear combination of reactions.
 	:param list-ReactionOperation reaction_operations:
 	:return SimplifiedReaction: inferred_reaction
 	"""
 	INFERRED_REACTION = "Inferred Reaction"
 	stoichiometry_df = self.getOperationStoichiometryMatrix(reaction_operations)
 	reaction_index = [op.reaction for op in reaction_operations]
 	operation_series = pd.Series([val.operation for val in reaction_operations], index=reaction_index)
 	resulting_reaction = stoichiometry_df.dot(operation_series)
 	# Create a simplified reaction based on the resulting_reaction
 	reactants = []
 	products = []
 	for ms in resulting_reaction.iteritems():
 	  if abs(ms[1]) < TOLERANCE:
 	  	continue
 	  elif ms[1] > 0:
 	  	products.append(MoleculeStoichiometry(molecule=self.mesgraph.simple.getMolecule(ms[0]),
 	  	                                      stoichiometry=ms[1]))
 	  else:
 	  	reactants.append(MoleculeStoichiometry(molecule=self.mesgraph.simple.getMolecule(ms[0]),
 	  	                                      stoichiometry=abs(ms[1])))
 	inferred_reaction = SimplifiedReaction(reactants,
 		                                   products,
 		                                   NULL_STR,
 		                                   self.mesgraph)
 	inferred_reaction.reduceBySOMs()
 	return inferred_reaction
Esempio n. 2
0
 def reduceBySOMs(self):
     """
 Cancel reactants and products by SOMs
 SOMs is given by existing MESGraph.
 Return a new identifier. 
 :return str:
 """
     reactant_soms = {
         self.mesgraph.getNode(r.molecule)
         for r in self.reactants
     }
     product_soms = {
         self.mesgraph.getNode(p.molecule)
         for p in self.products
     }
     common_soms = list(reactant_soms.intersection(product_soms))
     if common_soms:
         for som in common_soms:
             reactants_in = collections.deque([
                 ms for ms in self.reactants
                 if self.mesgraph.getNode(ms.molecule) == som
             ])
             reactants_out = [
                 ms for ms in self.reactants
                 if self.mesgraph.getNode(ms.molecule) != som
             ]
             products_in = collections.deque([
                 ms for ms in self.products
                 if self.mesgraph.getNode(ms.molecule) == som
             ])
             products_out = [
                 ms for ms in self.products
                 if self.mesgraph.getNode(ms.molecule) != som
             ]
             #
             while reactants_in and products_in:
                 reactant = reactants_in[0]
                 product = products_in[0]
                 if reactant.stoichiometry > product.stoichiometry:
                     reactants_in[0] = MoleculeStoichiometry(
                         reactant.molecule,
                         reactant.stoichiometry - product.stoichiometry)
                     products_in.popleft()
                 elif reactant.stoichiometry < product.stoichiometry:
                     products_in[0] = MoleculeStoichiometry(
                         product.molecule,
                         product.stoichiometry - reactant.stoichiometry)
                     reactants_in.popleft()
                 else:
                     reactants_in.popleft()
                     products_in.popleft()
             reactants = list(reactants_in) + reactants_out
             products = list(products_in) + products_out
             #
             if (len(self.reactants) > len(reactants)) | \
               (len(self.products) > len(products)):
                 self.reactants = reactants
                 self.products = products
         self.identifier = self.makeIdentifier()
     return self.identifier
Esempio n. 3
0
 def testDifference(self):
     if IGNORE_TEST:
         return
     df = self.comparator.difference()
     self.assertLess(df.loc[MOIETY_NAME1].tolist()[0], 0)
     #
     comparator = MoietyComparator(self.molecules1,
                                   self.molecules2,
                                   implicits=[MOIETY_NAME1])
     df = comparator.difference()
     self.assertFalse(MOIETY_NAME1 in df.index)
     #
     config._config_dict[cn.CFG_PROCESS_BOUNDARY_REACTIONS] = False
     molecules1 = [
         MoleculeStoichiometry(Molecule(n), 0)
         for n in MOLECULE_NAME_SET[:3]
     ]
     comparator = MoietyComparator(molecules1, self.molecules2)
     df = comparator.difference()
     self.assertEqual(df[df.columns[0]].sum(), 0)
     #
     config._config_dict[cn.CFG_PROCESS_BOUNDARY_REACTIONS] = True
     molecules1 = [
         MoleculeStoichiometry(Molecule(n), 0)
         for n in MOLECULE_NAME_SET[:3]
     ]
     comparator = MoietyComparator(molecules1, self.molecules2)
     df = comparator.difference()
     self.assertNotEqual(df[df.columns[0]].sum(), 0)
Esempio n. 4
0
 def testGetMolecules(self):
     if IGNORE_TEST:
         return
     names = ["a", "b", "c"]
     full_names = list(names)
     full_names.extend(names)
     m_ss = [MoleculeStoichiometry(Molecule(n), NUM1) for n in full_names]
     molecules = MoleculeStoichiometry.getMolecules(m_ss)
     self.assertEqual(len(molecules), len(names))
Esempio n. 5
0
 def testCountMoietys(self):
     if IGNORE_TEST:
         return
     moiety_stoich = MoietyStoichiometry(Molecule(MOIETY_NAME1), NUM1)
     mole_stoich = MoleculeStoichiometry(Molecule(str(moiety_stoich)), NUM2)
     df = mole_stoich.countMoietys()
     self.assertEqual(df.columns.tolist(), [cn.VALUE])
     expected = NUM1 * NUM2
     trues = [expected == n for n in df[cn.VALUE]]
Esempio n. 6
0
 def testCountMoietysInCollection(self):
     if IGNORE_TEST:
         return
     m_ss = [
         MoleculeStoichiometry(Molecule(n), NUM1)
         for n in MOLECULE_NAME_SET[:3]
     ]
     df = MoleculeStoichiometry.countMoietysInCollection(m_ss)
     indexes = df.index.tolist()
     self.assertEqual(len(indexes), len(set(indexes)))
Esempio n. 7
0
 def _getMolecules(self):
     """
 :return dict: key is species name, value is species object
 """
     molecules = []
     for reaction in self.reactions:
         molecules.extend(
             MoleculeStoichiometry.getMolecules(reaction.reactants))
         molecules.extend(
             MoleculeStoichiometry.getMolecules(reaction.products))
     return util.uniqueify(molecules)
Esempio n. 8
0
 def testCountMoietys2(self):
     if IGNORE_TEST:
         return
     m_ss = [
         MoleculeStoichiometry(Molecule("A_P_P_P"), 1),
         MoleculeStoichiometry(Molecule("A__P_3"), 1),
     ]
     dfs = []
     for m_s in m_ss:
         dfs.append(m_s.countMoietys())
     self.assertTrue(dfs[0].equals(dfs[1]))
Esempio n. 9
0
 def reduceReaction(self, reaction):
     """
 Reduce the given reaction 
 :param Reaction reaction:
 :return False/Reaction reaction:
 """
     if reaction.category != cn.REACTION_n_n:
         return False
     # Reduces the reaction by examining for each SOM
     for som in list(self.nodes):
         reactants_in = collections.deque([
             mole_stoich for mole_stoich in reaction.reactants
             if self.getNode(mole_stoich.molecule) == som
         ])
         reactants_out = [
             mole_stoich for mole_stoich in reaction.reactants
             if self.getNode(mole_stoich.molecule) != som
         ]
         products_in = collections.deque([
             mole_stoich for mole_stoich in reaction.products
             if self.getNode(mole_stoich.molecule) == som
         ])
         products_out = [
             mole_stoich for mole_stoich in reaction.products
             if self.getNode(mole_stoich.molecule) != som
         ]
         #
         while reactants_in and products_in:
             reactant = reactants_in[0]
             product = products_in[0]
             if reactant.stoichiometry > product.stoichiometry:
                 reactants_in[0] = MoleculeStoichiometry(
                     reactant.molecule,
                     reactant.stoichiometry - product.stoichiometry)
                 products_in.popleft()
             elif reactant.stoichiometry < product.stoichiometry:
                 products_in[0] = MoleculeStoichiometry(
                     product.molecule,
                     product.stoichiometry - reactant.stoichiometry)
                 reactants_in.popleft()
             else:
                 reactants_in.popleft()
                 products_in.popleft()
         reactants = list(reactants_in) + reactants_out
         products = list(products_in) + products_out
         #
         if (len(reaction.reactants) > len(reactants)) | \
           (len(reaction.products) > len(products)):
             reaction.reactants = reactants
             reaction.products = products
     reaction.identifier = reaction.makeIdentifier()
     reaction.category = reaction.getCategory()
     # reduced_reaction = cn.ReactionComponents(label = reaction.label, reactants=reactants, products=products)
     return reaction
Esempio n. 10
0
 def setUp(self):
     self.molecules1 = [
         MoleculeStoichiometry(Molecule(n), NUM1)
         for n in MOLECULE_NAME_SET[:3]
     ]
     self.molecules2 = [
         MoleculeStoichiometry(Molecule(n), NUM2)
         for n in MOLECULE_NAME_SET[3:]
     ]
     self.comparator = MoietyComparator(self.molecules1, self.molecules2)
     self.config_dict = copy.deepcopy(config._config_dict)
Esempio n. 11
0
 def testConstructor(self):
     if IGNORE_TEST:
         return
     molecule = Molecule(MOLECULE_NAME)
     m_s = MoleculeStoichiometry(molecule, NUM1)
     self.assertTrue(m_s.molecule.isEqual(molecule))
     self.assertEqual(m_s.stoichiometry, NUM1)
Esempio n. 12
0
 def makeMoleculeStoichiometrys(self, func_get_one, func_get_num):
   """
   Creates a list of MoleculeStoichiometry
   :param Function func_get_one: get one element by index
   :param Function func_get_num: get number of elements
   :return list-MoleculeStoichiometry:
   """
   result = []
   collection = [func_get_one(n) for n in range(func_get_num())]
   for s_r in collection:
     molecule = Molecule(s_r.species)
     stoich = s_r.getStoichiometry()
     result.append(MoleculeStoichiometry(molecule, stoich))
   return result
Esempio n. 13
0
 def _makeDFS(self):
     dfs = []
     for collection in self.molecule_stoichiometry_collections:
         if len(collection) > 0:
             df = MoleculeStoichiometry.countMoietysInCollection(collection)
             dfs.append(df)
     if len(dfs) == 0:
         dfs = [pd.DataFrame(), pd.DataFrame()]
     elif len(dfs) == 1:
         col = dfs[0].columns.tolist()[0]
         df = dfs[0].copy()
         df[col] = 0
         dfs.append(df)
     return dfs
Esempio n. 14
0
def getMolecules(libsbml_reaction, func):
    """
  Constructs molecules for the species returned by function.
  :param Function func: gets libsbml.SpeciesReference
  :return list-MoleculeStoichiometry:
  """
    species = func(libsbml_reaction)
    molecule_stoichiometrys = []
    for spc in species:
        molecule = Molecule.getMolecule(spc.getSpecies())
        if molecule is None:
            molecule = Molecule(spc.getSpecies())
        molecule_stoichiometrys.append(
            MoleculeStoichiometry(molecule, spc.getStoichiometry()))
    return molecule_stoichiometrys
Esempio n. 15
0
 def _makeDFS(self):
     """
 Constructs a list of dataframes containing counts of moieties.
 :return list-pd.DataFrame: columns in DataFrame
     cn.VALUE
 """
     dfs = []
     for collection in self.molecule_stoichiometry_collections:
         if len(collection) > 0:
             df = MoleculeStoichiometry.countMoietysInCollection(collection)
             dfs.append(df)
     if len(dfs) == 0:
         dfs = [pd.DataFrame(), pd.DataFrame()]
     elif len(dfs) == 1:
         col = dfs[0].columns.tolist()[0]
         df = dfs[0].copy()
         df[col] = 0
         dfs.append(df)
     return dfs
Esempio n. 16
0
 def testExtractMoietyStoichiometrys(self):
     if IGNORE_TEST:
         return
     moiety_stoich = MoietyStoichiometry(Molecule(MOIETY_NAME1), NUM1)
     mole_stoich = MoleculeStoichiometry(Molecule(str(moiety_stoich)), NUM2)