def getMolecules(cls, molecule_stoichiometrys): """ Obtainins the molecules from a collection of MoleculeStoichiometry. :param list-MoleculeStoichiometry molecule_stoichiometrys; :return list-Molecule: unique molecules """ molecules = [m_s.molecule for m_s in molecule_stoichiometrys] return util.uniqueify(molecules)
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)
def _getMoietys(self): """ Sees if there is a valid moiety structure. If not, the molecule is a single moiety. """ moietys = [] for molecule in self.molecules: try: new_moietys = ([ m_s.moiety for m_s in molecule.moiety_stoichiometrys ]) except ValueError: new_moietys = [Moiety(molecule.name)] moietys.extend(new_moietys) return util.uniqueify(moietys)
def add(self, element): """ Adds an element of the type to its list """ type_list = { Moiety: self.moietys, Molecule: self.molecules, Reaction: self.reactions, } this_list = type_list[element.__class__] appended_list = list(this_list) appended_list.append(element) new_list = util.uniqueify(appended_list) if len(new_list) > len(this_list): this_list.append(element)
def testUniqueify(self): class Tester(): def __init__(self, name): self.name = name def __repr__(self): return self.name def isEqual(self, other): return self.name == other.name # STRING = 'abc' REPEATED_STRING = STRING + STRING collection = [Tester(s) for s in REPEATED_STRING] result = util.uniqueify(collection) self.assertEqual(len(result), len(STRING))
def getMoietys(cls, moiety_stoichiometrys): """ Extract moieties from MoietyStoichiometrys """ moietys = util.uniqueify([m_s.moiety for m_s in moiety_stoichiometrys]) return moietys
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)