Exemplo n.º 1
0
 def setUp(self):
     # BIOMD0000000383
     self.simple1 = SimpleSBML()
     self.simple1.initialize(cn.TEST_FILE_GAMES_PP1)
     self.games_pp = GAMES_PP(self.simple1)
     # BIOMD0000000018
     self.simple2 = SimpleSBML()
     self.simple2.initialize(cn.TEST_FILE_GAMES_PP2)
Exemplo n.º 2
0
 def testProcessErrorReaction(self):
     if IGNORE_TEST:
         return
     games_pp1 = GAMES_PP(self.simple1)
     self.assertTrue(len(games_pp1.echelon_errors) == ZERO)
     reaction = self.games_pp.simple.getReaction(PGA_PROD_VO)
     games_pp1.processErrorReaction(reaction)
     self.assertTrue(len(games_pp1.echelon_errors) == ONE)
     self.assertTrue(reaction in games_pp1.echelon_errors)
Exemplo n.º 3
0
 def testReportReactionsInSOM(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple4)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     som = m.getNode(m.simple.getMolecule(PSTATDIMER_NUC))
     report, error_num = gr.reportReactionsInSOM(som, 0)
     common_part = "1. PstatDimer__import: PstatDimer_sol -> PstatDimer_nuc\n"
     self.assertEqual(error_num, 1)
     self.assertTrue(report == common_part)
Exemplo n.º 4
0
 def setUp(self):
     self.simple = SimpleSBML()
     # BIOMD0000000248 - originally for canceling_error
     self.simple.initialize(cn.TEST_FILE_GAMESREPORT1)
     self.mesgraph = GAMES_PP(self.simple)
     self.mesgraph.analyze(error_details=False)
     # Construct SimplifiedReaction
     self.reaction = self.simple.getReaction(CREATINEKINASE)
     self.simplified_reaction = SimplifiedReaction(self.reaction.reactants,
                                                   self.reaction.products,
                                                   self.reaction.label,
                                                   self.mesgraph)
Exemplo n.º 5
0
 def testGetMoleculeEqualityPath(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple2)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     som = m.getNode(self.simple2.getMolecule(G2K))
     equality_path = gr.getMoleculeEqualityPath(som, G2K, PG2R)
     self.assertTrue(len(equality_path) == 2)
     self.assertEqual(type(equality_path[0]), cn.PathComponents)
     self.assertEqual(equality_path[0].node1, G2K)
     self.assertEqual(equality_path[0].reactions, [CDC2PHOS])
     self.assertEqual(equality_path[1].node2, PG2R)
     self.assertEqual(equality_path[1].reactions, [RUM1DEGINPG2R])
Exemplo n.º 6
0
 def testAddTypeOneError(self):
     if IGNORE_TEST:
         return
     games_pp2 = GAMES_PP(self.simple2)
     self.assertTrue(len(games_pp2.type_one_errors) == ZERO)
     reaction = games_pp2.simple.getReaction(CH2FH4toHCHO)
     reactant1 = games_pp2.simple.getMolecule(FH4)
     product1 = games_pp2.simple.getMolecule(CH2FH4)
     games_pp2.addTypeOneError(reactant1, product1, reaction)
     self.assertTrue(len(games_pp2.type_one_errors) == ONE)
     error = games_pp2.type_one_errors[ZERO]
     self.assertEqual(error.node1, reactant1.name)
     self.assertEqual(error.node2, product1.name)
     self.assertEqual(error.reactions, [reaction.label])
Exemplo n.º 7
0
 def testReportEchelonError(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple4)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     report, error_num = gr.reportEchelonError(m.echelon_errors,
                                               explain_details=True)
     self.assertEqual(error_num, [3])
     extended_report = NULL_STR
     extended_report = extended_report + "will result in empty reactant with zero mass:\n\n:  -> {species_test}\n\n"
     extended_report = extended_report + "\n----------------------------------------------------------------------\n"
     extended_report = extended_report + "\n----------------------------------------------------------------------\n\n"
     extended_report = extended_report + "\n\n**********************************************************************\n\n"
     self.assertEqual(report[-288:], extended_report)
Exemplo n.º 8
0
 def testGetMoleculeInequalityPathReport(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple2)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     count, report1 = gr.getMoleculeInequalityPathReport(
         G2K, PG2R, ["G2R_Creation"], 0, explain_details=False)
     self.assertEqual(count, 1)
     self.assertEqual(report1, "1. G2R_Creation: G2K + R -> G2R\n")
     count, report2 = gr.getMoleculeInequalityPathReport(
         G2K, PG2R, ["G2R_Creation"], 0, explain_details=True)
     self.assertEqual(count, 1)
     self.assertEqual(
         report2,
         "G2K < PG2R by reaction(s):\n1. G2R_Creation: G2K + R -> G2R\n")
Exemplo n.º 9
0
 def testGetOperationStoichiometryMatrix(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple4)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     op = pd.Series([1.0, 0.5, 0.0],
                    index=[
                        STATPHOSPHORYLATION, PSTATDIMERISATION,
                        PSTATDIMERISATIONNUC
                    ])
     ro = gr.convertOperationSeriesToReactionOperations(op)
     osm = gr.getOperationStoichiometryMatrix(ro)
     self.assertEqual(osm.loc[SPECIES_TEST, STATPHOSPHORYLATION], 1.0)
     self.assertEqual(osm.loc[SPECIES_TEST, PSTATDIMERISATION], 0.0)
     self.assertEqual(osm.loc[PSTAT_SOL, STATPHOSPHORYLATION], 1.0)
     self.assertEqual(osm.loc[PSTAT_SOL, PSTATDIMERISATION], -2.0)
Exemplo n.º 10
0
 def testConvertOperationSeriesToReactionOperations(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple4)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     op = pd.Series([1.0, 0.5, 0.0],
                    index=[
                        STATPHOSPHORYLATION, PSTATDIMERISATION,
                        PSTATDIMERISATIONNUC
                    ])
     ro = gr.convertOperationSeriesToReactionOperations(op)
     self.assertEqual(len(ro), 2)
     self.assertEqual(ro[0].reaction, STATPHOSPHORYLATION)
     self.assertEqual(ro[0].operation, 1.0)
     self.assertEqual(ro[1].reaction, PSTATDIMERISATION)
     self.assertEqual(ro[1].operation, 0.5)
Exemplo n.º 11
0
 def testReportTypeOneError(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple2)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     error = [
         cn.PathComponents(node1=G2K, node2=G2R, reactions=[G2R_CREATION])
     ]
     report, error_num = gr.reportTypeOneError(error, explain_details=True)
     self.assertEqual(error_num, [2])
     extended_report = NULL_STR
     extended_report = extended_report + "\nG2K = G2R by reaction(s):\n1. Rum1DegInG2R: G2R -> G2K\n\n"
     extended_report = extended_report + "However, G2K < G2R by reaction(s):\n2. G2R_Creation: G2K + R -> G2R\n\n"
     extended_report = extended_report + "\n----------------------------------------------------------------------\n\n"
     extended_report = extended_report + "\n\n**********************************************************************\n\n"
     self.assertEqual(report, extended_report)
Exemplo n.º 12
0
 def testProcessUnequalSOMReaction(self):
     if IGNORE_TEST:
         return
     games_pp2 = GAMES_PP(self.simple2)
     self.assertTrue(len(games_pp2.edges) == 0)
     reaction = games_pp2.simple.getReaction(CH2FH4toHCHO)
     som_reaction = games_pp2.convertReactionToSOMReaction(reaction)
     games_pp2.processUnequalSOMReaction(som_reaction)
     self.assertTrue(len(games_pp2.edges) == 2)
     reactant1 = games_pp2.simple.getMolecule(FH4)
     reactant2 = games_pp2.simple.getMolecule(HCHO)
     product1 = games_pp2.simple.getMolecule(CH2FH4)
     games_pp2.processUniMultiReaction(reaction)
     som_product1 = games_pp2.getNode(product1)
     som_reactant1 = games_pp2.getNode(reactant1)
     som_reactant2 = games_pp2.getNode(reactant2)
     self.assertTrue(games_pp2.has_edge(som_reactant1, som_product1))
     self.assertTrue(games_pp2.has_edge(som_reactant2, som_product1))
Exemplo n.º 13
0
 def testReportTypeThreeError(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple4)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     report, error_num = gr.reportTypeThreeError(m.type_three_errors,
                                                 explain_details=True)
     self.assertEqual(error_num, [3])
     pseudo_inequality_report = NULL_STR
     pseudo_inequality_report = pseudo_inequality_report + "6. statPhosphorylation: stat_sol -> Pstat_sol + species_test\n"
     pseudo_inequality_report = pseudo_inequality_report + "(pseudo 6.) statPhosphorylation: {Pstat_nuc=stat_nuc=stat_sol} -> "
     pseudo_inequality_report1 = pseudo_inequality_report + "{species_test} + {Pstat_sol}"
     pseudo_inequality_report2 = pseudo_inequality_report + "{Pstat_sol} + {species_test}"
     inference_report1 = "the masses of {Pstat_sol} and {Pstat_nuc=stat_nuc=stat_sol} are unequal."
     inference_report2 = "the masses of {Pstat_nuc=stat_nuc=stat_sol} and {Pstat_sol} are unequal."
     self.assertTrue(report[-460:-305] == pseudo_inequality_report1
                     or report[-460:-305] == pseudo_inequality_report2)
     self.assertTrue(report[-293:-221] == inference_report1
                     or report[-293:-221] == inference_report2)
Exemplo n.º 14
0
 def testReportCancelingError(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple1)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     report, error_num = gr.reportCancelingError(m.canceling_errors,
                                                 explain_details=True)
     extended_report = NULL_STR
     extended_report = extended_report + "We detected a mass imbalance\n"
     extended_report = extended_report + ": OxidativePhosphorylation: CTtis -> \n\n"
     extended_report = extended_report + "from the following reaction isolation set:\n\n"
     extended_report = extended_report + "1. OxidativePhosphorylation: 6.00 ADP + CTtis -> 6.00 ATP\n"
     extended_report = extended_report + "2. ATPase: ATP -> ADP\n"
     extended_report = extended_report + "*ATP and ADP have the same mass according to the above reaction\n"
     extended_report = extended_report + "\n%s%s\n" % (PARAGRAPH_DIVIDER,
                                                       PARAGRAPH_DIVIDER)
     extended_report = extended_report + "\n%s\n" % REPORT_DIVIDER
     self.assertEqual(extended_report, report)
     self.assertEqual(error_num, [2])
Exemplo n.º 15
0
 def testGetMoleculeEqualityPathReport(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple2)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     count, report1 = gr.getMoleculeEqualityPathReport(
         G2K, PG2R, 0, explain_details=False)
     self.assertEqual(count, 2)
     self.assertEqual(
         report1,
         "1. Cdc2Phos: G2K -> PG2\n2. Rum1DegInPG2R: PG2R -> PG2\n")
     count, report2 = gr.getMoleculeEqualityPathReport(G2K,
                                                       PG2R,
                                                       0,
                                                       explain_details=True)
     self.assertEqual(count, 2)
     self.assertEqual(
         report2,
         "\nG2K = PG2 by reaction(s):\n1. Cdc2Phos: G2K -> PG2\n\nPG2 = PG2R by reaction(s):\n2. Rum1DegInPG2R: PG2R -> PG2\n"
     )
Exemplo n.º 16
0
class TestSimplifiedReaction(unittest.TestCase):
    def setUp(self):
        self.simple = SimpleSBML()
        # BIOMD0000000248 - originally for canceling_error
        self.simple.initialize(cn.TEST_FILE_GAMESREPORT1)
        self.mesgraph = GAMES_PP(self.simple)
        self.mesgraph.analyze(error_details=False)
        # Construct SimplifiedReaction
        self.reaction = self.simple.getReaction(CREATINEKINASE)
        self.simplified_reaction = SimplifiedReaction(self.reaction.reactants,
                                                      self.reaction.products,
                                                      self.reaction.label,
                                                      self.mesgraph)

    def testConstructor(self):
        if IGNORE_TEST:
            return
        self.assertEqual(type(self.simplified_reaction.reactants[0]),
                         MoleculeStoichiometry)
        self.assertEqual(type(self.simplified_reaction.products[0]),
                         MoleculeStoichiometry)
        self.assertEqual(self.simplified_reaction.label, CREATINEKINASE)
        self.assertEqual(type(self.simplified_reaction.mesgraph), GAMES_PP)
        self.assertEqual(
            self.simplified_reaction.makeIdentifier(),
            self.reaction.makeIdentifier(is_include_kinetics=False))

    def testReduceBySOMs(self):
        if IGNORE_TEST:
            return
        self.simplified_reaction.reduceBySOMs()
        self.assertEqual(len(self.simplified_reaction.reactants), 1)
        self.assertEqual(len(self.simplified_reaction.products), 1)
        self.assertTrue(
            self.simplified_reaction.reactants[0].molecule.name == PCR)
        self.assertTrue(
            self.simplified_reaction.products[0].molecule.name == CR)
Exemplo n.º 17
0
 def testGeInferredReaction(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple4)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     op = pd.Series([1.0, 0.5, 0.0],
                    index=[
                        STATPHOSPHORYLATION, PSTATDIMERISATION,
                        PSTATDIMERISATIONNUC
                    ])
     ro = gr.convertOperationSeriesToReactionOperations(op)
     inferred_reaction = gr.getInferredReaction(ro)
     self.assertEqual(len(inferred_reaction.reactants), 1)
     self.assertEqual(len(inferred_reaction.products), 2)
     self.assertEqual(inferred_reaction.reactants[0].molecule.name,
                      STAT_SOL)
     self.assertTrue(inferred_reaction.products[0].molecule.name in
                     {PSTATDIMER_SOL, SPECIES_TEST})
     self.assertTrue(inferred_reaction.products[1].molecule.name in
                     {PSTATDIMER_SOL, SPECIES_TEST})
     self.assertEqual(inferred_reaction.reactants[0].stoichiometry, 1.0)
     self.assertEqual({p.stoichiometry
                       for p in inferred_reaction.products}, {0.5, 1.0})
Exemplo n.º 18
0
 def testAnalyze(self):
     if IGNORE_TEST:
         return
     games_pp1 = GAMES_PP(self.simple1)
     games_pp2 = GAMES_PP(self.simple2)
     self.assertTrue(games_pp1.analyze(rref=False))
     self.assertTrue(games_pp2.analyze())
     self.assertTrue(len(games_pp1.echelon_errors) > ZERO)
     self.assertTrue(len(games_pp1.type_one_errors) == ZERO)
     self.assertTrue(len(games_pp1.type_two_errors) == ZERO)
     self.assertTrue(len(games_pp2.type_one_errors) > ZERO)
Exemplo n.º 19
0
 def testProcessMultiUniReaction(self):
     if IGNORE_TEST:
         return
     games_pp2 = GAMES_PP(self.simple2)
     self.assertTrue(isinstance(games_pp2, GAMES_PP))
     reaction = games_pp2.simple.getReaction(HCHOtoCH2FH4)
     reactant1 = games_pp2.simple.getMolecule(FH4)
     reactant2 = games_pp2.simple.getMolecule(HCHO)
     product1 = games_pp2.simple.getMolecule(CH2FH4)
     games_pp2.processMultiUniReaction(reaction)
     som_product1 = games_pp2.getNode(product1)
     som_reactant1 = games_pp2.getNode(reactant1)
     som_reactant2 = games_pp2.getNode(reactant2)
     self.assertTrue(games_pp2.has_edge(som_reactant1, som_product1))
     self.assertTrue(games_pp2.has_edge(som_reactant2, som_product1))
Exemplo n.º 20
0
 def testGetOperationMatrix(self):
     if IGNORE_TEST:
         return
     m1 = GAMES_PP(self.simple1)
     m1.analyze(error_details=False)
     gr1 = GAMESReport(m1)
     self.assertTrue(gr1.getOperationMatrix() is None)
     m4 = GAMES_PP(self.simple4)
     m4.analyze(error_details=False)
     gr4 = GAMESReport(m4)
     op_mat = gr4.getOperationMatrix()
     self.assertEqual(op_mat.loc[STATPHOSPHORYLATION, STATPHOSPHORYLATION],
                      1.0)
     self.assertEqual(op_mat.loc[PSTATDIMERISATION, PSTATDIMERISATION], 1.0)
     self.assertEqual(
         op_mat.loc[PSTATDIMERISATIONNUC, PSTATDIMERISATIONNUC], 1.0)
     self.assertEqual(op_mat.loc[PSTATDIMERISATIONNUC, STATPHOSPHORYLATION],
                      0.0)
     self.assertEqual(op_mat.loc[STATPHOSPHORYLATION, PSTATDIMERISATIONNUC],
                      -0.5)
Exemplo n.º 21
0
 def testGetResultingSeries(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple4)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     resulting_series = gr.getResultingSeries(STATPHOSPHORYLATION)
     print(resulting_series)
     self.assertEqual(resulting_series["{" + SPECIES_TEST + "}"], 1.0)
     self.assertEqual(resulting_series["{" + PSTAT_SOL + "}"], 0.0)
     self.assertEqual(
         resulting_series[m.getNode(
             m.simple.getMolecule(PSTATDIMER_NUC)).identifier], 0.0)
     self.assertEqual(
         resulting_series[m.getNode(
             m.simple.getMolecule(PSTAT_NUC)).identifier], 0.0)
Exemplo n.º 22
0
 def testChekcTypeOneError(self):
     if IGNORE_TEST:
         return
     games_pp1 = GAMES_PP(self.simple1)
     reaction = games_pp1.simple.getReaction(PGA_CONS)
     pga = games_pp1.simple.getMolecule(PGA)
     rubp = games_pp1.simple.getMolecule(RUBP)
     som_pga = games_pp1.getNode(pga)
     som_rubp = games_pp1.getNode(rubp)
     som_pga_rubp = games_pp1.mergeNodes(som_pga, som_rubp, reaction)
     self.assertTrue(len(games_pp1.type_one_errors) == ZERO)
     is_error = games_pp1.checkTypeOneError((pga, rubp), reaction)
     self.assertTrue(is_error)
     error = games_pp1.type_one_errors[ZERO]
     self.assertEqual(error.node1, pga.name)
     self.assertEqual(error.node2, rubp.name)
     self.assertEqual(error.reactions, [reaction.label])
Exemplo n.º 23
0
 def testReportTypeTwoError(self):
     if IGNORE_TEST:
         return
     m = GAMES_PP(self.simple3)
     m.analyze(error_details=False)
     gr = GAMESReport(m)
     som1 = m.getNode(self.simple3.getMolecule(CH3FH4))
     som2 = m.getNode(self.simple3.getMolecule(FH4))
     error = [[som1, som2]]
     report, error_num = gr.reportTypeTwoError(error, explain_details=True)
     self.assertEqual(error_num, [5])
     LOC_START = 241
     extended_report = NULL_STR
     extended_report = extended_report + "{CH3FH4} < {CH2FH4=FFH2=FH2f=FH4} < {CH3FH4}\n\n"
     extended_report = extended_report + "This indicates a mass conflict between reactions.\n"
     extended_report = extended_report + "%s%s\n" % (PARAGRAPH_DIVIDER,
                                                     PARAGRAPH_DIVIDER)
     self.assertEqual(report[-LOC_START:], extended_report)
Exemplo n.º 24
0
def lint(model_reference=None,
         file_out=sys.stdout,
         mass_balance_check=GAMES,
         config_fid=None,
         is_report=True,
         implicit_games=False):
    """
  Reports on errors found in a model
  :param str model_reference: 
      libsbml_model file in
      file, antimony string, xml string
  :param TextIOWrapper model_fid: fid for an XML file
  :param TextIOWrapper file_out:
  :param str mass_balance_check: how check for mass balance
  :param TextIOWrapper config_fid: readable stream
  :param bool is_report: print result
  :return MoietyComparatorResult/null/None:
  """
    config.setConfiguration(fid=config_fid)
    config_dct = config.getConfiguration()
    if util.isSBMLModel(model_reference):
        model = model_reference
    else:
        xml = util.getXML(model_reference)
        reader = libsbml.SBMLReader()
        document = reader.readSBMLFromString(xml)
        util.checkSBMLDocument(document)
        model = document.getModel()
    #
    simple = SimpleSBML()
    simple.initialize(model)
    if mass_balance_check == cn.MOIETY_ANALYSIS:
        result = MoietyComparator.analyzeReactions(simple)
        if is_report:
            for line in result.report.split('\n'):
                file_out.write("%s\n" % line)
        return result
    elif mass_balance_check == GAMES:
        if implicit_games:
            for ignored in config_dct[cn.CFG_IGNORED_MOLECULES]:
                simple = removeIgnored(simple, ignored)
        m = GAMES_PP(simple)
        games_result = m.analyze(simple.reactions)
        if games_result and is_report:
            gr = GAMESReport(
                m, explain_threshold=config_dct[cn.CFG_GAMES_THRESHOLD])
            errortype_dic = {
                TYPE_I: gr.reportTypeOneError,
                TYPE_II: gr.reportTypeTwoError,
                TYPE_III: gr.reportTypeThreeError,
                CANCELING: gr.reportCancelingError,
                ECHELON: gr.reportEchelonError
            }
            for errors in m.error_summary:
                for category in errortype_dic.keys():
                    if errors.type == category:
                        func = errortype_dic[category]
                        report, _ = func(errors.errors, explain_details=True)
                        print(report)
        return games_result
    else:
        print("Specified method doesn't exist")
        return None
Exemplo n.º 25
0
 def testCheckTypeTwoError(self):
     if IGNORE_TEST:
         return
     # Create a dummy cycle by adding two conflicting arcs
     games_pp2 = GAMES_PP(self.simple2)
     self.assertTrue(isinstance(games_pp2, GAMES_PP))
     unimulti_reaction = games_pp2.simple.getReaction(CH2FH4toHCHO)
     multiuni_reaction = games_pp2.simple.getReaction(HCHOtoCH2FH4)
     fh4 = games_pp2.simple.getMolecule(FH4)
     # hcho = games_pp2.simple.getMolecule(HCHO)
     ch2fh4 = games_pp2.simple.getMolecule(CH2FH4)
     som_fh4 = games_pp2.getNode(fh4)
     som_ch2fh4 = games_pp2.getNode(ch2fh4)
     # do we need the next two methods if we're giving a cycle?
     games_pp2.addArc(som_fh4, som_ch2fh4, unimulti_reaction)
     games_pp2.addArc(som_ch2fh4, som_fh4, multiuni_reaction)
     self.assertTrue(len(games_pp2.type_two_errors) == ZERO)
     games_pp2.checkTypeTwoError()
     self.assertTrue(len(games_pp2.type_two_errors) == ONE)
     error = games_pp2.type_two_errors[ZERO]
     self.assertTrue(games_pp2.has_edge(error[ZERO], error[ONE]))
     self.assertTrue(games_pp2.has_edge(error[ONE], error[ZERO]))
     error_reactions = set(
         games_pp2.get_edge_data(error[ZERO], error[ONE])[REACTION])
     error_reactions = error_reactions.union(
         set(games_pp2.get_edge_data(error[ONE], error[ZERO])[REACTION]))
     self.assertTrue(CH2FH4toHCHO in error_reactions)
     self.assertTrue(HCHOtoCH2FH4 in error_reactions)
Exemplo n.º 26
0
class TestGAMES_PP(unittest.TestCase):
    def setUp(self):
        # BIOMD0000000383
        self.simple1 = SimpleSBML()
        self.simple1.initialize(cn.TEST_FILE_GAMES_PP1)
        self.games_pp = GAMES_PP(self.simple1)
        # BIOMD0000000018
        self.simple2 = SimpleSBML()
        self.simple2.initialize(cn.TEST_FILE_GAMES_PP2)

    def testConstructor(self):
        if IGNORE_TEST:
            return
        self.assertEqual(len(self.games_pp.reactions), NUM_REACTIONS)
        for r in self.games_pp.reactions:
            print(r.category)
            self.assertFalse(r.category == cn.REACTION_BOUNDARY)
        self.assertEqual(len(self.games_pp.molecules), NUM_MOLECULES)
        for m in self.games_pp.molecules:
            self.assertTrue(isinstance(m, Molecule))
        self.assertEqual(len(self.games_pp.soms), NUM_MOLECULES)
        self.assertTrue(isinstance(self.games_pp.soms[0], SOM))
        init_identifier = ""
        for som in self.games_pp.soms:
            init_identifier = init_identifier + som.identifier
            if som != self.games_pp.soms[-1]:
                init_identifier = init_identifier + ";"
        self.assertEqual(self.games_pp.identifier, init_identifier)

    def testConvertReactionToSOMReaction(self):
        if IGNORE_TEST:
            return
        reaction1 = self.games_pp.simple.getReaction(PGA_CONS)
        reaction2 = self.games_pp.simple.getReaction(PGA_PROD_VC)
        som_reaction1 = self.games_pp.convertReactionToSOMReaction(reaction1)
        som_reaction2 = self.games_pp.convertReactionToSOMReaction(reaction2)
        self.assertTrue(isinstance(som_reaction1, SOMReaction))
        self.assertTrue(isinstance(som_reaction2, SOMReaction))
        ms_rubp = som_reaction1.products[0]
        self.assertTrue(isinstance(ms_rubp, SOMStoichiometry))
        self.assertEqual(
            list(ms_rubp.som.molecules)[0],
            self.games_pp.simple.getMolecule(RUBP))
        ms_nadph = None
        for ms in som_reaction2.reactants:
            if ms.som.identifier == SOM_NADPH:
                ms_nadph = ms
                break
        self.assertTrue(ms_nadph.som.identifier == SOM_NADPH)
        self.assertEqual(ms_nadph.stoichiometry, NADPH_STOICHIOMETRY)

    def testGetStoichiometryMatrix(self):
        if IGNORE_TEST:
            return
        # For regular stoichiometry matrix
        mat = self.games_pp.getStoichiometryMatrix(self.games_pp.reactions,
                                                   self.games_pp.molecules,
                                                   som=False)
        self.assertTrue(isinstance(mat, pd.DataFrame))
        self.assertEqual(mat.shape, (NUM_MOLECULES, NUM_REACTIONS))
        self.assertEqual(mat[PGA_CONS][PGA], PGA_CONS_WITH_PGA)
        self.assertEqual(mat[PGA_PROD_VC][RUBP], PGA_PROD_VC_WITH_RUBP)
        # For SOM stoichiometry matrix
        som_reactions = []
        for r in self.games_pp.reactions:
            som_reactions.append(self.games_pp.convertReactionToSOMReaction(r))
        som_mat = self.games_pp.getStoichiometryMatrix(som_reactions,
                                                       self.games_pp.nodes,
                                                       som=True)
        self.assertTrue(isinstance(som_mat, pd.DataFrame))
        self.assertEqual(som_mat[PGA_CONS][SOM_PGA], PGA_CONS_WITH_PGA)
        self.assertEqual(som_mat[PGA_PROD_VC][SOM_RUBP], PGA_PROD_VC_WITH_RUBP)

    def testDecomposeMatrix(self):
        if IGNORE_TEST:
            return
        # should be all None before decomposition
        self.assertTrue(self.games_pp.perm_inverse is None)
        self.assertTrue(self.games_pp.permuted_matrix is None)
        self.assertTrue(self.games_pp.lower_inverse is None)
        self.assertTrue(self.games_pp.echelon_df is None)
        som_reactions = []
        for r in self.games_pp.reactions:
            som_reactions.append(self.games_pp.convertReactionToSOMReaction(r))
        som_mat = self.games_pp.getStoichiometryMatrix(som_reactions,
                                                       self.games_pp.nodes,
                                                       som=True)
        echelon = self.games_pp.decomposeMatrix(som_mat).T
        self.assertFalse(self.games_pp.perm_inverse is None)
        self.assertFalse(self.games_pp.permuted_matrix is None)
        self.assertFalse(self.games_pp.lower_inverse is None)
        self.assertFalse(self.games_pp.echelon_df is None)
        self.assertTrue(isinstance(self.games_pp.perm_inverse, np.ndarray))
        self.assertTrue(isinstance(self.games_pp.permuted_matrix,
                                   pd.DataFrame))
        self.assertTrue(isinstance(self.games_pp.lower_inverse, pd.DataFrame))
        self.assertTrue(isinstance(self.games_pp.echelon_df, pd.DataFrame))
        # Checking lower echelon - lower left element should be 0.0 (zero)
        self.assertTrue(echelon.iloc[echelon.shape[0] - 1][0] == ZERO_F)
        # On the ohter hand, upper left should be nonzero
        self.assertFalse(echelon.iloc[0][0] == ZERO_F)

    def testGetRREFMatrix(self):
        if IGNORE_TEST:
            return
        self.assertTrue(self.games_pp.rref_operation is None)
        self.assertTrue(self.games_pp.rref_df is None)
        som_reactions = []
        for r in self.games_pp.reactions:
            som_reactions.append(self.games_pp.convertReactionToSOMReaction(r))
        som_mat = self.games_pp.getStoichiometryMatrix(som_reactions,
                                                       self.games_pp.nodes,
                                                       som=True)
        echelon = self.games_pp.decomposeMatrix(som_mat).T
        rref = self.games_pp.getRREFMatrix(echelon)
        self.assertTrue(isinstance(self.games_pp.rref_operation, pd.DataFrame))
        self.assertTrue(isinstance(self.games_pp.rref_df, pd.DataFrame))
        # Choose the last row of rref.T
        last_row = rref.T.iloc[-1:]
        # Get the first nonzero index
        nonzero_species = (last_row != 0).idxmax(axis=1)[0]
        # The SUM of nonzero species column must be the same as the single value
        self.assertEqual(last_row[nonzero_species][0],
                         sum(rref.T[nonzero_species]))

    def testConverMatrixToSOMReactions(self):
        if IGNORE_TEST:
            return
        som_reactions1 = []
        for r in self.games_pp.reactions:
            som_reactions1.append(
                self.games_pp.convertReactionToSOMReaction(r))
        som_mat = self.games_pp.getStoichiometryMatrix(som_reactions1,
                                                       self.games_pp.nodes,
                                                       som=True)
        som_reactions2 = self.games_pp.convertMatrixToSOMReactions(som_mat)
        sr1 = None
        sr2 = None
        for sr in som_reactions1:
            if sr.label == PGA_PROD_VC:
                sr1 = sr
                break
        for sr in som_reactions2:
            if sr.label == PGA_PROD_VC:
                sr2 = sr
                break
        sr1_reactants = {ms.som for ms in sr1.reactants}
        sr2_reactants = {ms.som for ms in sr2.reactants}
        sr1_products = {ms.som for ms in sr1.products}
        sr2_products = {ms.som for ms in sr2.products}
        sr1_reactsom = sum([ms.stoichiometry for ms in sr1.reactants])
        sr2_reactsom = sum([ms.stoichiometry for ms in sr2.reactants])
        sr1_prodsom = sum([ms.stoichiometry for ms in sr1.products])
        sr2_prodsom = sum([ms.stoichiometry for ms in sr2.products])
        self.assertEqual(sr1_reactants, sr2_reactants)
        self.assertEqual(sr1_products, sr2_products)
        self.assertEqual(sr1_reactsom, sr2_reactsom)
        self.assertEqual(sr1_prodsom, sr2_prodsom)

    def testGetNode(self):
        if IGNORE_TEST:
            return
        co2 = self.games_pp.simple.getMolecule(CO2)
        co2_node = self.games_pp.getNode(co2)
        self.assertEqual(type(co2_node), SOM)
        self.assertEqual(co2_node.molecules, {co2})

    def testMergeNodes(self):
        if IGNORE_TEST:
            return
        reaction = self.games_pp.simple.getReaction(PGA_CONS)
        pga = self.games_pp.simple.getMolecule(PGA)
        rubp = self.games_pp.simple.getMolecule(RUBP)
        som_pga = self.games_pp.getNode(pga)
        som_rubp = self.games_pp.getNode(rubp)
        som_pga_rubp = self.games_pp.mergeNodes(som_pga, som_rubp, reaction)
        self.assertTrue(isinstance(som_pga_rubp, SOM))
        self.assertTrue(pga in som_pga_rubp.molecules)
        self.assertTrue(rubp in som_pga_rubp.molecules)
        self.assertTrue(reaction in som_pga_rubp.reactions)

    def testAddReaction(self):
        if IGNORE_TEST:
            return
        self.assertEqual(len(self.games_pp.reactions_lu), 0)
        reaction = self.games_pp.simple.getReaction(PGA_CONS)
        self.games_pp.addReaction(reaction)
        self.assertTrue(reaction in self.games_pp.reactions_lu)
        self.games_pp.addReaction(reaction)
        self.assertTrue(len(self.games_pp.reactions_lu), 1)

    def testProcessUniUniReaction(self):
        if IGNORE_TEST:
            return
        reaction = self.games_pp.simple.getReaction(PGA_CONS)
        som = self.games_pp.processUniUniReaction(reaction)
        self.assertTrue(isinstance(som, SOM))
        self.assertTrue(som in self.games_pp.nodes)
        pga = self.games_pp.simple.getMolecule(PGA)
        rubp = self.games_pp.simple.getMolecule(RUBP)
        self.assertTrue(pga in som.molecules)
        self.assertTrue(rubp in som.molecules)

    def testAddArc(self):
        if IGNORE_TEST:
            return
        reaction = self.games_pp.simple.getReaction(PGA_PROD_VC)
        co2 = self.games_pp.simple.getMolecule(CO2)
        pga = self.games_pp.simple.getMolecule(PGA)
        som_co2 = self.games_pp.getNode(co2)
        som_pga = self.games_pp.getNode(pga)
        self.games_pp.addArc(som_co2, som_pga, reaction)
        self.assertTrue(self.games_pp.has_edge(som_co2, som_pga))
        self.assertEqual(
            self.games_pp.get_edge_data(som_co2, som_pga)[REACTION],
            [PGA_PROD_VC])

    def testProcessUniMultiReaction(self):
        if IGNORE_TEST:
            return
        games_pp2 = GAMES_PP(self.simple2)
        self.assertTrue(isinstance(games_pp2, GAMES_PP))
        reaction = games_pp2.simple.getReaction(CH2FH4toHCHO)
        reactant1 = games_pp2.simple.getMolecule(CH2FH4)
        product1 = games_pp2.simple.getMolecule(FH4)
        product2 = games_pp2.simple.getMolecule(HCHO)
        games_pp2.processUniMultiReaction(reaction)
        som_reactant1 = games_pp2.getNode(reactant1)
        som_product1 = games_pp2.getNode(product1)
        som_product2 = games_pp2.getNode(product2)
        self.assertTrue(games_pp2.has_edge(som_product1, som_reactant1))
        self.assertTrue(games_pp2.has_edge(som_product2, som_reactant1))

    def testProcessMultiUniReaction(self):
        if IGNORE_TEST:
            return
        games_pp2 = GAMES_PP(self.simple2)
        self.assertTrue(isinstance(games_pp2, GAMES_PP))
        reaction = games_pp2.simple.getReaction(HCHOtoCH2FH4)
        reactant1 = games_pp2.simple.getMolecule(FH4)
        reactant2 = games_pp2.simple.getMolecule(HCHO)
        product1 = games_pp2.simple.getMolecule(CH2FH4)
        games_pp2.processMultiUniReaction(reaction)
        som_product1 = games_pp2.getNode(product1)
        som_reactant1 = games_pp2.getNode(reactant1)
        som_reactant2 = games_pp2.getNode(reactant2)
        self.assertTrue(games_pp2.has_edge(som_reactant1, som_product1))
        self.assertTrue(games_pp2.has_edge(som_reactant2, som_product1))

    def testProcessEqualSOMReaction(self):
        if IGNORE_TEST:
            return
        print("type three", self.games_pp.type_three_errors)
        self.assertEqual(len(self.games_pp.type_three_errors), ZERO)
        # Add an arc
        reaction = self.games_pp.simple.getReaction(PGA_PROD_VC)
        co2 = self.games_pp.simple.getMolecule(CO2)
        pga = self.games_pp.simple.getMolecule(PGA)
        som_co2 = self.games_pp.getNode(co2)
        som_pga = self.games_pp.getNode(pga)
        self.games_pp.addArc(som_co2, som_pga, reaction)
        # Process a dummy reaction
        soms_co2 = SOMStoichiometry(som_co2, 1.0)
        soms_pga = SOMStoichiometry(som_pga, 1.0)
        som_reaction = SOMReaction([soms_co2], [soms_pga], "dummy")
        self.games_pp.processEqualSOMReaction(som_reaction)
        self.assertTrue(len(self.games_pp.type_three_errors) > ZERO)

    def testProcessUnequalSOMReaction(self):
        if IGNORE_TEST:
            return
        games_pp2 = GAMES_PP(self.simple2)
        self.assertTrue(len(games_pp2.edges) == 0)
        reaction = games_pp2.simple.getReaction(CH2FH4toHCHO)
        som_reaction = games_pp2.convertReactionToSOMReaction(reaction)
        games_pp2.processUnequalSOMReaction(som_reaction)
        self.assertTrue(len(games_pp2.edges) == 2)
        reactant1 = games_pp2.simple.getMolecule(FH4)
        reactant2 = games_pp2.simple.getMolecule(HCHO)
        product1 = games_pp2.simple.getMolecule(CH2FH4)
        games_pp2.processUniMultiReaction(reaction)
        som_product1 = games_pp2.getNode(product1)
        som_reactant1 = games_pp2.getNode(reactant1)
        som_reactant2 = games_pp2.getNode(reactant2)
        self.assertTrue(games_pp2.has_edge(som_reactant1, som_product1))
        self.assertTrue(games_pp2.has_edge(som_reactant2, som_product1))

    def testAddTypeOneError(self):
        if IGNORE_TEST:
            return
        games_pp2 = GAMES_PP(self.simple2)
        self.assertTrue(len(games_pp2.type_one_errors) == ZERO)
        reaction = games_pp2.simple.getReaction(CH2FH4toHCHO)
        reactant1 = games_pp2.simple.getMolecule(FH4)
        product1 = games_pp2.simple.getMolecule(CH2FH4)
        games_pp2.addTypeOneError(reactant1, product1, reaction)
        self.assertTrue(len(games_pp2.type_one_errors) == ONE)
        error = games_pp2.type_one_errors[ZERO]
        self.assertEqual(error.node1, reactant1.name)
        self.assertEqual(error.node2, product1.name)
        self.assertEqual(error.reactions, [reaction.label])

    def testChekcTypeOneError(self):
        if IGNORE_TEST:
            return
        games_pp1 = GAMES_PP(self.simple1)
        reaction = games_pp1.simple.getReaction(PGA_CONS)
        pga = games_pp1.simple.getMolecule(PGA)
        rubp = games_pp1.simple.getMolecule(RUBP)
        som_pga = games_pp1.getNode(pga)
        som_rubp = games_pp1.getNode(rubp)
        som_pga_rubp = games_pp1.mergeNodes(som_pga, som_rubp, reaction)
        self.assertTrue(len(games_pp1.type_one_errors) == ZERO)
        is_error = games_pp1.checkTypeOneError((pga, rubp), reaction)
        self.assertTrue(is_error)
        error = games_pp1.type_one_errors[ZERO]
        self.assertEqual(error.node1, pga.name)
        self.assertEqual(error.node2, rubp.name)
        self.assertEqual(error.reactions, [reaction.label])

    def testCheckTypeTwoError(self):
        if IGNORE_TEST:
            return
        # Create a dummy cycle by adding two conflicting arcs
        games_pp2 = GAMES_PP(self.simple2)
        self.assertTrue(isinstance(games_pp2, GAMES_PP))
        unimulti_reaction = games_pp2.simple.getReaction(CH2FH4toHCHO)
        multiuni_reaction = games_pp2.simple.getReaction(HCHOtoCH2FH4)
        fh4 = games_pp2.simple.getMolecule(FH4)
        # hcho = games_pp2.simple.getMolecule(HCHO)
        ch2fh4 = games_pp2.simple.getMolecule(CH2FH4)
        som_fh4 = games_pp2.getNode(fh4)
        som_ch2fh4 = games_pp2.getNode(ch2fh4)
        # do we need the next two methods if we're giving a cycle?
        games_pp2.addArc(som_fh4, som_ch2fh4, unimulti_reaction)
        games_pp2.addArc(som_ch2fh4, som_fh4, multiuni_reaction)
        self.assertTrue(len(games_pp2.type_two_errors) == ZERO)
        games_pp2.checkTypeTwoError()
        self.assertTrue(len(games_pp2.type_two_errors) == ONE)
        error = games_pp2.type_two_errors[ZERO]
        self.assertTrue(games_pp2.has_edge(error[ZERO], error[ONE]))
        self.assertTrue(games_pp2.has_edge(error[ONE], error[ZERO]))
        error_reactions = set(
            games_pp2.get_edge_data(error[ZERO], error[ONE])[REACTION])
        error_reactions = error_reactions.union(
            set(games_pp2.get_edge_data(error[ONE], error[ZERO])[REACTION]))
        self.assertTrue(CH2FH4toHCHO in error_reactions)
        self.assertTrue(HCHOtoCH2FH4 in error_reactions)

    def testProcessErrorReaction(self):
        if IGNORE_TEST:
            return
        games_pp1 = GAMES_PP(self.simple1)
        self.assertTrue(len(games_pp1.echelon_errors) == ZERO)
        reaction = self.games_pp.simple.getReaction(PGA_PROD_VO)
        games_pp1.processErrorReaction(reaction)
        self.assertTrue(len(games_pp1.echelon_errors) == ONE)
        self.assertTrue(reaction in games_pp1.echelon_errors)

    def testAnalyze(self):
        if IGNORE_TEST:
            return
        games_pp1 = GAMES_PP(self.simple1)
        games_pp2 = GAMES_PP(self.simple2)
        self.assertTrue(games_pp1.analyze(rref=False))
        self.assertTrue(games_pp2.analyze())
        self.assertTrue(len(games_pp1.echelon_errors) > ZERO)
        self.assertTrue(len(games_pp1.type_one_errors) == ZERO)
        self.assertTrue(len(games_pp1.type_two_errors) == ZERO)
        self.assertTrue(len(games_pp2.type_one_errors) > ZERO)