Beispiel #1
0
    def difference(self):
        """
    Calculates the moieties present in one set and not in the other.
    :return pd.DataFrame: row index is moiety,
        column Value: counts of moieties present in 0 
        and not in second. Negative values are present in 1 and
        not in 0.
    Handles boundary reactions, when one set of moieties
    is all zeroes.
    """
        def addDFIndex(df, index):
            # Adds missing indices to df
            missing = set(index).difference(df.index)
            for item in missing:
                df.loc[item] = 0

        def isZeroColumn(df):
            col = df.columns[0]
            return df[col].sum() == 0

        #
        dfs = self._makeDFS()
        addDFIndex(dfs[0], dfs[1].index)
        addDFIndex(dfs[1], dfs[0].index)
        drops = set(self._implicits).intersection(dfs[0].index)
        df0 = dfs[0].drop(drops)
        df1 = dfs[1].drop(drops)
        df = df0 - df1
        # Handle boundaries
        config_dict = config.getConfiguration()
        if not config_dict[cn.CFG_PROCESS_BOUNDARY_REACTIONS]:
            if isZeroColumn(df0) or isZeroColumn(df1):
                df[df.columns[0]] = 0
        #
        return df
Beispiel #2
0
    def __init__(self,
                 mol_stoichs1,
                 mol_stoichs2,
                 names=["reactants", "products"]):
        """
    :param set-MoleculeStoichiometry mol_stoichs1:
    :param set-MoleculeStoichiometry mol_stoichs2:
    :param list-str names: names to refer to the two sets
    """
        def checkType(objs):
            trues = [isinstance(o, MoleculeStoichiometry) for o in objs]
            if all(trues):
                return
            raise ValueError("Argument must be collection of type %s" %
                             str(MoleculeStoichiometry))

        checkType(mol_stoichs1)
        checkType(mol_stoichs2)
        config_dct = dict(config.getConfiguration())
        self._process_boundary_reactions =  \
            util.getKey(config_dct, cn.CFG_PROCESS_BOUNDARY_REACTIONS)
        if self._process_boundary_reactions is None:
            self._process_boundary_reactions = False
        self._ignored_molecules = util.setList(
            util.getKey(config_dct, cn.CFG_IGNORED_MOLECULES))
        self._ignored_moieties = util.setList(
            util.getKey(config_dct, cn.CFG_IGNORED_MOIETIES))
        self.molecule_stoichiometry_collections = [
            self._removeIgnoredMolecules(mol_stoichs1),
            self._removeIgnoredMolecules(mol_stoichs2),
        ]
        self.names = names
Beispiel #3
0
 def testMakeFromDct(self):
     if IGNORE_TEST:
         return
     config.setConfiguration(TEST_CFG_FILE)
     config_dct = config.getConfiguration()
     dct = config_dct[cn.CFG_MOIETY_STRUCTURE]
     mss1 = MoietyStoichiometry.makeFromDct(dct["ATP"])
     mss2 = [MoietyStoichiometry("A", 1), MoietyStoichiometry("P", 3)]
     self.assertTrue(all([m1.isEqual(m2) for m1, m2 in zip(mss1, mss2)]))
Beispiel #4
0
 def moiety_stoichiometrys(self):
     done = False
     if self._moiety_stoichiometrys is None:
         config_dct = config.getConfiguration()
         if cn.CFG_MOIETY_STRUCTURE in config_dct:
             dct = config_dct[cn.CFG_MOIETY_STRUCTURE]
             if self.name in dct.keys():
                 self._moiety_stoichiometrys =  \
                     MoietyStoichiometry.makeFromDct(dct[self.name])
                 done = True
     else:
         done = True
     if not done:
         new_name = self._reformat()
         stgs = new_name.split(cn.MOIETY_DOUBLE_SEPARATOR)
         result = [MoietyStoichiometry.make(ms) for ms in stgs]
         result.sort()
         self._moiety_stoichiometrys = result
     return self._moiety_stoichiometrys
 def testSetConfiguration(self):
     config._config_dict = {}
     self.assertEqual(len(config.getConfiguration()), 0)
     config.setConfiguration()
     self.verifyDefaultConfiguration()
 def verifyDefaultConfiguration(self):
     result = config.getConfiguration()
     self.assertTrue(isinstance(result, dict))
     for k, v in cn.CFG_DEFAULTS.items():
         self.assertTrue(k in result)
         self.assertEqual(result[k], cn.CFG_DEFAULTS[k])
Beispiel #7
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