def _growth(args): p, tradeoff, medium, weights, atol, rtol = args com = load_pickle(p) if atol is None: atol = com.solver.configuration.tolerances.optimality if rtol is None: rtol = com.solver.configuration.tolerances.optimality com = load_pickle(p) if "glpk" in interface_to_str(com.solver.interface): logger.error( "Community models were not built with a QP-capable solver. " "This means that you did not install CPLEX or Gurobi. " "If you did install one of the two please file a bug report " "at https://github.com/micom-dev/micom/issues." ) return None ex_ids = [r.id for r in com.exchanges] logger.info( "%d/%d import reactions found in model.", medium.index.isin(ex_ids).sum(), len(medium), ) com.medium = medium[medium.index.isin(ex_ids)] # Get growth rates try: sol = com.cooperative_tradeoff(fraction=tradeoff) rates = sol.members rates["taxon"] = rates.index rates["tradeoff"] = tradeoff rates["sample_id"] = com.id except Exception: logger.warning( "Could not solve cooperative tradeoff for %s. " "This can often be fixed by chosing ore permissive atol and rtol " "arguments." % com.id) return None # Get the minimal medium and the solution at the same time sol = minimal_medium( com, exchanges=None, community_growth=sol.growth_rate, min_growth=rates.growth_rate.drop("medium"), solution=True, weights=weights, atol=atol, rtol=rtol )["solution"] fluxes = sol.fluxes.loc[:, sol.fluxes.columns.str.startswith("EX_")].copy() fluxes["sample_id"] = com.id fluxes["tolerance"] = atol anns = annotate_metabolites_from_exchanges(com) return {"growth": rates, "exchanges": fluxes, "annotations": anns}
def _growth(args): p, tradeoff, medium = args com = load_pickle(p) ex_ids = [r.id for r in com.exchanges] logger.info( "%d/%d import reactions found in model.", medium.index.isin(ex_ids).sum(), len(medium), ) com.medium = medium[medium.index.isin(ex_ids)] # Get growth rates try: sol = com.cooperative_tradeoff(fraction=tradeoff) rates = sol.members rates["taxon"] = rates.index rates["tradeoff"] = tradeoff rates["sample_id"] = com.id except Exception: logger.warning("Could not solve cooperative tradeoff for %s." % com.id) return None # Get the minimal medium med = minimal_medium(com, 0.95 * sol.growth_rate) # Apply medium and reoptimize com.medium = med[med > 0] sol = com.cooperative_tradeoff(fraction=tradeoff, fluxes=True, pfba=False) fluxes = sol.fluxes.loc[:, sol.fluxes.columns.str.startswith("EX_")].copy() fluxes["sample_id"] = com.id return {"growth": rates, "exchanges": fluxes}
def _fix_medium(args): """Get the fixed medium for a model.""" sid, p, min_growth, max_import, min_c, medium, weights = args com = load_pickle(p) try: fixed = mm.complete_medium(com, medium, min_growth=min_growth, max_import=max_import, minimize_components=min_c, weights=weights) except Exception: logger.warning("Can't reach the specified growth rates for model %s." % sid) return None fixed = pd.DataFrame({"reaction": fixed.index, "flux": fixed.values}) fixed["metabolite"] = [ list(com.reactions.get_by_id(r).metabolites.keys())[0].id for r in fixed.reaction ] fixed["description"] = [ list(com.reactions.get_by_id(r).metabolites.keys())[0].name for r in fixed.reaction ] fixed["sample_id"] = sid return fixed
def _tradeoff(args): p, tradeoffs, medium = args com = load_pickle(p) ex_ids = [r.id for r in com.exchanges] logger.info( "%d/%d import reactions found in model.", medium.index.isin(ex_ids).sum(), len(medium), ) com.medium = medium[medium.index.isin(ex_ids)] sol = com.optimize() rates = sol.members rates["taxon"] = rates.index rates["tradeoff"] = np.nan rates["sample_id"] = com.id df = [rates] # Get growth rates try: sol = com.cooperative_tradeoff(fraction=tradeoffs) except Exception as e: logger.warning("Sample %s could not be optimized\n %s" % (com.id, str(e))) return None for i, s in enumerate(sol.solution): rates = s.members rates["taxon"] = rates.index rates["tradeoff"] = sol.tradeoff[i] rates["sample_id"] = com.id df.append(rates) df = pd.concat(df) return df[df.taxon != "medium"]
def growth_rates(sam): com = load_pickle("data/models/" + sam + ".pickle") sol = com.optimize() rates = sol.members rates["tradeoff"] = np.nan rates["sample"] = sam df = [rates] # Get growth rates try: sol = com.cooperative_tradeoff(fraction=tradeoffs) except Exception as e: logger.warning("Sample %s could not be optimized\n %s" % (sam, str(e))) return pd.DataFrame({ "tradeoff": tradeoffs, "growth_rate": np.nan, "sample": sam }) for i, s in enumerate(sol.solution): rates = s.members rates["tradeoff"] = sol.tradeoff[i] rates["sample"] = sam df.append(rates) df = pd.concat(df) return df
def _medium(args): """Get minimal medium for a single model.""" p, min_growth = args com = load_pickle(p) medium = mm.minimal_medium(com, 0.0, min_growth=min_growth).to_frame() medium.columns = ["flux"] medium.index.name = "reaction" return medium.reset_index()
def elasticity_worker(sam): """Get the exchange elasticities for a sample.""" model_file = "data/models/" + sam + ".pickle" out_file = "data/elasticities_" + sam + ".csv" if isfile(out_file): return com = load_pickle(model_file) elast = elasticities(com, fraction=0.5) elast.to_csv(out_file, index=False)
def elasticities(sam): """Get the exchange elasticities for a sample.""" model_file = "models/" + sam + ".pickle" out_file = "intervention/" + sam + ".csv" if isfile(out_file): return com = load_pickle(model_file) elast = exchange_elasticities(com, fraction=0.5, progress=False) elast.to_csv(out_file, index=False)
def test_build(): d = q2m.build( table, taxa, models.view(q2m._formats_and_types.JSONDirectory), 1, 0.01, True ) manifest = d.manifest.view(pd.DataFrame) assert manifest.shape[0] == 3 for sa in manifest.sample_id.unique(): com = micom.load_pickle(d.model_files.path_maker(model_id=sa)) assert isinstance(com, micom.Community) assert len(com.reactions) > 3 * 95 assert len(com.abundances) == 3
def _medium(args): """Get minimal medium for a single model.""" s, p, min_growth = args com = load_pickle(p) # open the bounds for ex in com.exchanges: ex.bounds = (-1000.0, 1000.0) try: medium = mm.minimal_medium(com, 0.0, min_growth=min_growth).to_frame() except Exception: return None medium.columns = ["flux"] medium["sample_id"] = s medium.index.name = "reaction" return medium.reset_index()
def media_and_gcs(sam): com = load_pickle("data/models/" + sam + ".pickle") # Get growth rates try: sol = com.cooperative_tradeoff(fraction=0.5) rates = sol.members["growth_rate"].copy() rates["community"] = sol.growth_rate rates.name = sam except Exception: logger.warning("Could not solve cooperative tradeoff for %s." % sam) return None # Get the minimal medium med = minimal_medium(com, 0.95 * sol.growth_rate, exports=True) med.name = sam # Apply medium and reoptimize com.medium = med[med > 0] sol = com.cooperative_tradeoff(fraction=0.5, fluxes=True, pfba=False) fluxes = sol.fluxes fluxes["sample"] = sam return {"medium": med, "gcs": rates, "fluxes": fluxes}
def media_and_gcs(sam): com = load_pickle(pickles_path +"/"+ sam) # Get growth rates try: sol = com.cooperative_tradeoff(fraction=trade_off) rates = sol.members["growth_rate"].copy() rates["community"] = sol.growth_rate rates.name = sam except Exception: logger.warning("Could not solve cooperative tradeoff for %s." % sam) return None # Get the minimal medium med = minimal_medium(com, 0.95 * sol.growth_rate, exports=True) med.name = sam # Apply medium and reoptimize com.medium = med[med > 0] sol = com.cooperative_tradeoff(fraction=0.5, fluxes=True, pfba=False) # uses the 'classic' FBA instead of the parsimonious FBA fluxes = sol.fluxes fluxes["sample"] = sam return {"medium": med, "gcs": rates, "fluxes": fluxes}
def test_community_pickling(community, tmpdir): filename = str(tmpdir.join("com.pickle")) community.to_pickle(filename) loaded = load_pickle(filename) assert len(community.reactions) == len(loaded.reactions)
def knockout(sam): com = load_pickle("data/models/" + sam + ".pickle") ko = com.knockout_species(fraction=0.5) ko["sample"] = sam return ko
# community type and file_paths: pickles_path = args.comm_fp sample = args.sample trade_off = args.trade_off th = int(args.threads) out_dir = args.out_folder #pickles_path = '1_communities' #sample = 'SRR6784563' #trade_off = 0.5 #th = 2 # Load community: comm_fp = pickles_path + "/" + sample + '.pickle' com = load_pickle(comm_fp) ############################# #### SIMULATE GROWTH print("\n simulating growth...\n") #generate a manifest: comm_filename = sample + '.pickle' manifest = pd.DataFrame({ "sample_id": sample, "file": comm_filename }, index=[0]) # Medium (western diet, after diluting nutrients absorbed in the small intestine) med = pd.DataFrame(com.medium.items(), columns=['reaction', 'flux'])