def merge_models(worm_m, wol_m, out_name, out_file): rxn_pref_remove = ('TEST_', ) worm_m += wol_m rxns_to_remove = [] for rxn in worm_m.reactions: if rxn.id.startswith(rxn_pref_remove): rxns_to_remove.append(rxn.id) worm_m.remove_reactions(rxns_to_remove) worm_m.id = out_name write_excel(worm_m, out_file) print('%s saved to %s' % (worm_m, out_file))
def compare_reaction_mtbs(model1, model2, missing_mtb, outfile): rxn_mtbs = set() exchanges, transports = set(), set() for rxn in model1.reactions: rs = tuple(sorted(m.id.strip()[1:] for m in rxn.reactants)) ps = tuple(sorted(m.id.strip()[1:] for m in rxn.products)) if any(m[0].isalpha() for m in rs + ps): # Using a custom metabolite. continue if rs == ps: transports.add(rs) elif not rs: exchanges.add(ps) elif not ps: exchanges.add(rs) else: rxn_mtbs.add((rs, ps)) print('\nParsed %i reactions, %i transports, and %i exchanges.' % (len(rxn_mtbs), len(transports), len(exchanges))) missed_exs = set() bad_ids = set() missing_mtb_ids = set() for rxn in model2.reactions: rs = tuple(sorted(m.id.strip()[1:] for m in rxn.reactants)) ps = tuple(sorted(m.id.strip()[1:] for m in rxn.products)) if not rs or not ps: ms = rs if rs else ps if ms not in exchanges: missed_exs.add(ms) else: if (rs, ps) in rxn_mtbs or (ps, rs) in rxn_mtbs: pass else: coef = check_rxn_missing_mtb(missing_mtb, rs, ps, rxn_mtbs) if coef: missing_mtb_ids.add(rxn.id) rxn.add_metabolites({missing_mtb: coef}) else: bad_ids.add(rxn.id) print( '%s is missing %i exchanges present in %s, with %i inconsistencies.' % (model2, len(missed_exs), model1, len(bad_ids))) write_excel(model2, outfile) print('%i reactions were missing %s; modified and saved as %s' % (len(missing_mtb_ids), missing_mtb, outfile))
old_directions.append('blocked') else: old_directions.append('missing') agreed = False if len(set(old_directions)) == 1 and old_directions[0] == 'missing': continue # No matching reactions found in old_m elif 'blocked' in old_directions or 'missing' in old_directions: broken += 1 # Part of the reaction set exists in old_m, but part is missing or blocked. elif cel_direction == 'reversible': if 'irreversible' not in old_directions: agreed = True else: rvsb += 1 else: # Should be irreversible if 'irreversible' in old_directions: agreed = True else: irrvsb += 1 if not agreed: disagreements.append((', '.join(r_ids), cel_direction, ', '.join(old_directions), products)) disagreements.sort(key=lambda d: d[0]) disagreements.sort(key=lambda d: d[1]) print('\n%i should have been reversible, %i irreversible, and %i were broken' % (rvsb, irrvsb, broken)) unresolved = use_directionalities(old_m, disagreements) print('\n'.join('%s should be %s, but is: %s' % (d[:3]) for d in unresolved)) print len(unresolved) write_excel(old_m, model_out_file)
% (wm, wm.solution.f)) if test_removed_wol_rxns: for m, fva, r_ids in zip(in_models, fvas, unique_wol_ids): print('\nEvaluating %i reactions from %s. Initial flux: %.1f.' % (len(r_ids), m, m.solution.f)) diffs = test_changed_constraints(r_ids, m, fva, bounds_deltas=None, cumulative=False) for d in diffs: print('%i loss [%s]: %s' % (d[0], d[1][0], ', '.join(x for x in d[3]))) if save_wol_models: out_wol_model_files = [ os.path.join(files_dir, '%s-sep.xlsx' % m_name) for m_name in wol_model_names ] for wm, m_file in zip(wol_models, out_wol_model_files): write_excel(wm, m_file) # add in common reactions. if save_nem_models: out_nem_model_files = [ os.path.join(files_dir, '%s-sep.xlsx' % m_name) for m_name in out_nem_model_names ] for m, m_file in zip(in_models, out_nem_model_files): write_excel(m, m_file)
model_files = ['model_o_vol_3.xlsx', 'model_b_mal_3.xlsx'] out_files = ['model_o_vol_3.5-wip.xlsx', 'model_b_mal_3.5-wip.xlsx'] do_deletions = True cel_m = read_excel(os.path.join(files_dir, 'iCEL1273.xlsx'), verbose=False) #cel_m.reactions.BIO0101.objective_coefficient = 1.0 # # # TESTING ONLY #cobra.flux_analysis.parsimonious.optimize_minimal_flux(cel_m) models = [ read_excel(os.path.join(files_dir, m_file), verbose=False) for m_file in model_files ] for m, out_file in zip(models, out_files): if do_deletions: modify_model(m, cel_m, do_deletions) write_excel(m, os.path.join(files_dir, out_file)) else: print('Obj before modifications: %.1f' % (m.optimize().f)) rxns_to_delete = modify_model(m, cel_m, do_deletions) diffs = [] cur_f = m.optimize().f for rxn in rxns_to_delete: rxn.delete() diff_f = m.optimize().f - cur_f diffs.append((diff_f, rxn)) cur_f += diff_f if cur_f < 1.0: break diffs.sort() for d in diffs: print('%.1f by %s' % (d[0], d[1].id))
""" import os from read_excel import read_excel, write_excel files_dir = '/mnt/hgfs/win_projects/brugia_project' in_model_names = ['model_o_vol_3.5', 'model_b_mal_3.5'] in_model_files = [os.path.join(files_dir, m_file+'.xlsx') for m_file in in_model_names] out_model_files = [os.path.join(files_dir, m_file+'-wip.xlsx') for m_file in in_model_names] in_models = [read_excel(m_file, verbose=False) for m_file in in_model_files] for m in in_models: for mtb in m.metabolites: if mtb.id == 'FA_storage_mix': continue elif mtb.id[0] == 'C': mtb.compartment = 'c' if mtb.id[1] == 'C': continue elif mtb.id[0] == 'M': mtb.compartment = 'm' if mtb.id[1] == 'C' or mtb.name: continue c_id = 'C' + mtb.id[1:] if c_id in m.metabolites and m.metabolites.get_by_id(c_id).name: mtb.name = m.metabolites.get_by_id(c_id).name elif mtb.id[0] == 'G': pass for m, out_file in zip(in_models, out_model_files): write_excel(m, out_file)