def _testIterator(self, itr): for item in itr: model = item.model self.assertTrue(isinstance(model.getSpecies(0), libsbml.Species)) COUNT = 20 itr = simple_sbml.modelIterator(final=COUNT) item_number = -1 for item in itr: self.assertTrue(isinstance(item.filename, str)) self.assertTrue(util.isSBMLModel(item.model)) item_number = item.number self.assertEqual(item_number, COUNT - 1)
def calcStats(initial=0, final=50, out_path=OUTPUT_PATH, report_interval=50, report_progress=True, min_frc=-1, data_dir=cn.BIOMODELS_DIR): """ Calculates statistics for structured names. :param int initial: Index of first model to process :param int final: Index of final model to process :param str out_path: Path to the output CSV file :param int report_interval: Number of files processed before a report is written :param bool report_progress: report file being processed :param float min_frc: Filter to select only those models that have at least the specified fraction of reactions balanced according to moiety_analysis """ def writeDF(dfs): df_count = pd.concat(dfs) df_count[cn.NUM_BALANCED_REACTIONS] = \ df_count[cn.TOTAL_REACTIONS] \ - df_count[cn.NUM_IMBALANCED_REACTIONS] denom = (df_count[cn.TOTAL_REACTIONS] - df_count[cn.NUM_BOUNDARY_REACTIONS]) denom = [np.nan if np.isclose(v, 0) else v for v in denom] df_count[cn.FRAC_BALANCED_REACTIONS] = \ 1.0*df_count[cn.NUM_BALANCED_REACTIONS] / denom df_count[cn.FRAC_BOUNDARY_REACTIONS] = \ 1.0*df_count[cn.NUM_BOUNDARY_REACTIONS] / ( df_count[cn.TOTAL_REACTIONS]) if min_frc < 0: df = df_count else: df = df_count[df_count[cn.FRAC_BALANCED_REACTIONS] > min_frc] df = df.sort_values(cn.FRAC_BALANCED_REACTIONS) df.to_csv(out_path, index=False) # dfs = [] sbmliter = simple_sbml.modelIterator(initial=initial, final=final, data_dir=data_dir) for item in sbmliter: if report_progress: print("*Processing file %s, number %d" % (item.filename, item.number)) simple = simple_sbml.SimpleSBML() try: simple.initialize(item.model) except: print(" Error in model number %d." % item.number) continue row = {cn.FILENAME: [item.filename], cn.IS_STRUCTURED: [False], cn.NUM_BOUNDARY_REACTIONS: [0], cn.TOTAL_REACTIONS: [0], cn.NUM_IMBALANCED_REACTIONS: [0], } for reaction in simple.reactions: if (len(reaction.reactants) == 0) or (len(reaction.products) == 0): row[cn.NUM_BOUNDARY_REACTIONS] = \ [row[cn.NUM_BOUNDARY_REACTIONS][0] + 1] molecules = util.uniqueify([m.molecule for m in set(reaction.reactants).union(reaction.products)]) if any([isStructuredName(m.name) for m in molecules]): row[cn.IS_STRUCTURED] = [True] try: mcr = sbmllint.lint(model_reference=item.model, is_report=False) row[cn.TOTAL_REACTIONS] = [mcr.num_reactions if mcr.num_reactions > 0 else np.nan] row[cn.NUM_IMBALANCED_REACTIONS] = [mcr.num_imbalances] except: row[cn.TOTAL_REACTIONS] = [None] row[cn.NUM_IMBALANCED_REACTIONS] = [0] dfs.append(pd.DataFrame(row)) if item.number % report_interval == 0: writeDF(dfs) writeDF(dfs)
def testModelIterator2(self): if IGNORE_TEST: return self._testIterator( simple_sbml.modelIterator(final=1, zip_filename=None))
def testModelIterator1(self): if IGNORE_TEST: return self._testIterator(simple_sbml.modelIterator(final=1))