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)
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 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 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 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)
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)
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 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 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])
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 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)
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")
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)
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)
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)
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 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)
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 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)
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])
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)
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" )
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})
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