Example #1
0
def main():
    with open("../mmodes/ModelsInput/media.json") as jdata:
        media = json.load(jdata)[0]
    # 1) instantiate Consortium
    # if "manifest" contains a non-empty string, it will generate COMETS-like ouput
    cons = mmodes.Consortium(stcut=1e-7,
                             mets_to_plot=["ac[e]", "thr_L[e]"],
                             v=1,
                             manifest="COMETS_manifest.txt")
    # 2) instantiate dMetabolites
    # for instance, https://www.ncbi.nlm.nih.gov/pubmed/18791026?dopt=Abstract
    glc = mmodes.dMetabolite(id="glc_D[e]", Km=14.8, Vmax=0.13)
    # 3) add model
    # model from AGORA. A single strain to agilize example.
    cons.add_model(
        "../mmodes/ModelsInput/BGN4_eu.xml",
        0.0003,
        solver="glpk",
        method="fba",
        dMets={glc.id:
               glc})  # Bifidobacterium_adolescentis_ATCC_15703.xml (euavg)
    # 4) instantiate media
    cons.media = cons.set_media(media, True)
    # 5) run it, plotting the output
    cons.run(maxT=10,
             outp="plot_example.png",
             outf="tab_example.tsv",
             verbose=True,
             integrator="fea",
             stepChoiceLevel=(0.008, 0.5, 10000))
    # 6) print stuff on screen
    for mod in cons.models:
        print(mod, cons.models[mod].volume.q, sep=" -> ")
    print("Glucose", str(cons.media["glc_D[e]"]), sep=" -> ")
    print("Acetate", str(cons.media["ac[e]"]), sep=" -> ")
    print()
def runn(md=""):
    lock.acquire()
    print("Starting simulation on", md)
    lock.release()

    # make working directory based on argument
    subprocess.run(["mkdir", md])
    # clean directory if needed
    files = [
        f'{md}/{f}' for f in os.listdir(".") if
        f.find("log_template.txt") != -1 or f[-3:] == "tsv" or f[-3:] == 'png'
    ]
    if files:
        subprocess.run(["rm"] + files)

    # PARAMETERS
    # 1) Common parameters of simulation
    media_file = "media.json"
    mod_dir = "/home/cleanapp/draft/ModelsInput/"
    intervl = 1  # time of simulation between perturbations; total time will be 2h

    # 2) random parameters of simulation
    # 2.1) Medium
    with open(mod_dir + media_file) as json_file:
        gen_media = json.load(json_file)
    # 2.2) Biomasses
    brand = lambda: random.uniform(0.000013, 0.000055)
    biomasses = [
        brand()
    ]  # BGJ: Biomass for athrobacter that always will be in the consortium
    # we want to take all the possibilities with *equal* probabilities
    # BGJ: add 2 additional values to the biomasses vector, that could be only one, both ones or any.
    chosen = random.random()
    if chosen > 0.75:
        biomasses += [brand(), 0]
    elif chosen > 0.5:
        biomasses += [0, brand()]
    elif chosen > 0.25:
        biomasses += [brand(), brand()]
    else:
        biomasses += [0, 0]
    ar, hb, hl = biomasses  # BGJ: split biomasses in 3 different variables

    # SIMULATION
    # 1) instantiate Consortium
    volume_petri = 2 * 3.141593 * 45 * 45 * 15 * 1e-6  # 2pi*r²*h -> mm³ to L
    cons = mmodes.Consortium(stcut=1e-8,
                             v=volume_petri,
                             comets_output=False,
                             manifest=f"{md}/fluxes.tsv",
                             work_based_on="id",
                             max_growth=10,
                             mets_to_plot=["cpd03959_e0", "cpd00027_e0"],
                             title="Atrazine " + md)

    # 2.1) add models, with random biomass
    cons.add_model(mod_dir + "Arthrobacter_CORRECTED.json",
                   float(ar),
                   solver="glpk",
                   method="fba")
    cons.add_model(mod_dir + "Halobacillus_sp_CORRECTED.json",
                   float(hb),
                   solver="glpk",
                   method="fba")
    cons.add_model(mod_dir + "Halomonas_stevensii_CORRECTED.json",
                   float(hl),
                   solver="glpk",
                   method="fba")

    st_ids = [id for id in cons.models if not id.startswith("k")]
    print(f"Models were loaded on {md}.")

    # 2.2) define initial medium => medium or exudate, already in JSON file
    root = deepcopy(gen_media[1])
    del (gen_media[random.randint(0, 1)])  # choose medium or exudate

    # 2.3) add several random perturbations : strain | both | none
    fix_biomass = 0.000034  # BGJ: fix biomass to add in the perturbation as probiotics
    for pert in range(3):  # BGJ: 2019.11.08
        # perturbations always carry atrazine
        gen_media.append({
            "MEDIA": {
                "cpd03959_e0": 0.15
            },
            "PERTURBATION": "ATRAZINE"
        })
        chosen = random.random()
        if chosen > 0.8:
            gen_media[-1]["MEDIA"][st_ids[0]] = fix_biomass * 100
            gen_media[-1]["PERTURBATION"] = st_ids[0]
        elif chosen > 0.6:  # 2019.11.12: BGJ: to increase biomass 2nd strain: st_ids[1]
            gen_media[-1]["MEDIA"][st_ids[1]] = fix_biomass * 100
            gen_media[-1]["PERTURBATION"] = st_ids[1]
        elif chosen > 0.4:
            gen_media[-1]["PERTURBATION"] = st_ids[0] + "_" + st_ids[1]
            gen_media[-1]["MEDIA"][st_ids[0]] = fix_biomass * 100
            gen_media[-1]["MEDIA"][st_ids[1]] = fix_biomass * 100

#    elif chosen > 0.2:
#        gen_media[-1]["MEDIA"] = root["MEDIA"]
#        gen_media[-1]["PERTURBATION"] = "ROOT_EXUDATE"
        else:
            gen_media[-1]["MEDIA"]['cpd00009_e0'] = 1
            gen_media[-1]["PERTURBATION"] = 'PHOSPHATE'
    #else: none perturbation (only atrazine)

    # 2.3) add 2nd perturbation (nothing)
    # we need this to evaluate the last state of the consortium because the
    # reward function in MDPbiome for this particular case evaluates degradation of
    # atrazine 1 h after the simulation
    gen_media.append({
        "MEDIA": {
            "cpd03959_e0": 0.15
        },
        "PERTURBATION": "ATRAZINE"
    })

    # 3) Write log
    lock.acquire()
    log(cons, gen_media)
    lock.release()

    it = 1
    t_pers = []
    pers = []
    for mper in gen_media:
        if it == 1:
            # 4) instantiate media
            cons.media = cons.set_media(mper["MEDIA"], True)
        else:
            for k in mper["MEDIA"]:  # not needed at all...
                if mper["MEDIA"][k] == 0:
                    cons.media[k] = 0
            cons.add_mets(mper["MEDIA"], True)
        pers.append(mper["PERTURBATION"])
        # 5) run it
        t_pers.append(cons.T[-1])
        cons.run(verbose=False,
                 plot=False,
                 maxT=intervl + cons.T[-1],
                 integrator="FEA",
                 stepChoiceLevel=(0.00027, 0.5, 100000.),
                 outp=f'{md}/{md}_plot.png',
                 outf=f'{md}/plot.tsv')
        it += 1
    txpers = {t_pers[i]: pers[i] for i in range(len(t_pers))}

    # 6. Save simulation
    with open(f'{md}/cons.p', 'wb') as f:
        pickle.dump(cons, f)
    with open(f'{md}/txpers.p', 'wb') as f:
        pickle.dump(txpers, f)
    tsv_filter(f'{md}/plot.tsv',
               f'{md}/fluxes.tsv',
               txpers,
               inplace=False,
               v=cons.v)
    if os.path.isfile(f'{md}/fluxes_filtered.tsv'):
        os.unlink(f'{md}/fluxes.tsv')
    mmodes.vis.plot_comm(cons)

    lock.acquire()
    print(f"\033[1;32;40mProcess with directory {md} out!\033[0m")
    lock.release()

    del (cons)
    del (txpers)
    del (gen_media)

    return
Example #3
0
def runn(md=""):
    lock.acquire()
    print("Starting simulation on", md)
    lock.release()

    # make working directory based on argument
    subprocess.run(["mkdir", md])
    # clean directory if needed
    files = [
        f'{md}/{f}' for f in os.listdir(".") if
        f.find("log_template.txt") != -1 or f[-3:] == "tsv" or f[-3:] == 'png'
    ]
    if files:
        subprocess.run(["rm"] + files)

    # PARAMETERS
    # 1) Common parameters of simulation
    media_file = "4_medios.json"
    mod_dir = "/home/javi/Documentos/Root_con_P/"  #Directorio donde está el modelo
    intervl = 1  # time of simulation between perturbations; total time will be 2h

    # 2) random parameters of simulation
    # 2.1) Medium
    with open(mod_dir + media_file) as json_file:
        gen_media = json.load(
            json_file
        )  #Abrimos el contenido del archivo y lo metemos en gen_media
    # 2.2) Biomasses
    brand = lambda: random.uniform(
        0.000013, 0.000055
    )  #Función lambda, se crea así y hace lo que está detras de ':'
    #cada vez que llamemos a lambda se nos generará un número random entre ese intervalo
    biomasses = [
        brand()
    ]  # BGJ: Biomass for athrobacter that always will be in the consortium
    # we want to take all the possibilities with *equal* probabilities
    # BGJ: add 2 additional values to the biomasses vector, that could be only one, both ones or any.

    #Este bloque lo que hace es que de manera aleatoria genera unas biomasas iniciales distintas para cada microorganismo
    #Para que cada experimento sea distinto. Por ello, siempre habrá athrobacter (cantidad aleatoria), pero las cantidades de las otras dos
    #Las ponemos de manera aleatoria, poniendo solo uno de ellos, los dos o ninguno.
    chosen = random.random()  #Generamos un número aleatorio entre cero y uno.
    if chosen > 0.75:
        biomasses += [brand(), 0]
    elif chosen > 0.5:
        biomasses += [0, brand()]
    elif chosen > 0.25:
        biomasses += [brand(), brand()]
    else:
        biomasses += [0, 0]
    #Ponemos cada uno de los valores de biomasa en tres variables que usaremos después.
    ar, hb, hl = biomasses  # BGJ: split biomasses in 3 different variables

    # SIMULATION
    # 1) instantiate Consortium
    volume_petri = 2 * 3.141593 * 45 * 45 * 15 * 1e-6  # 2pi*r²*h -> mm³ to L
    cons = mmodes.Consortium(stcut=1e-8,
                             v=volume_petri,
                             comets_output=False,
                             manifest=f"{md}/fluxes.tsv",
                             work_based_on="id",
                             max_growth=10,
                             mets_to_plot=["cpd03959_e0", "cpd00027_e0"],
                             title="Atrazine " + md)

    # 2.1) add models, with random biomass
    cons.add_model(mod_dir + "Arthrobacter_CORRECTED.json",
                   float(ar),
                   solver="glpk",
                   method="fba")
    cons.add_model(mod_dir + "Halobacillus_sp_CORRECTED.json",
                   float(hb),
                   solver="glpk",
                   method="fba")
    cons.add_model(mod_dir + "Halomonas_stevensii_CORRECTED.json",
                   float(hl),
                   solver="glpk",
                   method="fba")

    #Se mete el ID de las bacterias. Si recorremos cons.models, ahí está el nombre de cada modelo, Arthrobacter empieza por k,
    #por eso no nos interesa almacenarlo. Solo almacenamos los otros dos, que serán los que se añadan como perturbación.
    st_ids = [id for id in cons.models if not id.startswith("k")]
    print(f"Models were loaded on {md}.")

    # 2.2) define initial medium => medium or exudate, already in JSON file
    #root = deepcopy(gen_media[1])
    del (gen_media[0])  # choose medium or exudate
    del (gen_media[1])
    del (gen_media[1])

    # 2.3) add several random perturbations : strain | both | none
    fix_biomass = 0.000034  # BGJ: fix biomass to add in the perturbation as probiotics
    for pert in range(
            3
    ):  # BGJ: 2019.11.08 #SI QUEREMOS CAMBIAR EL NÚMERO DE PERTURBACIONES POR SIMULACIÓN, ES AQUÍ.
        # perturbations always carry atrazine
        gen_media.append({
            "MEDIA": {
                "cpd03959_e0": 0.15
            },
            "PERTURBATION": "ATRAZINE"
        })  #Metemos una nueva entrada al diccionario
        chosen = random.random()
        if chosen > 0.75:
            gen_media[-1]["MEDIA"][st_ids[
                0]] = fix_biomass * 100  #Esto lo que hace es meter una nueva entrada en el diccionario dentro de "MEDIA"
            #Con ese id y ese valor. Esta entrada se mete al últmo diccionario de gen_media que es lo último que appendeamos.
            gen_media[-1]["PERTURBATION"] = st_ids[
                0]  #Nos referimos al último diccionario, al key "PERTURBATION" y lo cambiamos por ese valor.
        elif chosen > 0.5:  # 2019.11.12: BGJ: to increase biomass 2nd strain: st_ids[1]
            gen_media[-1]["MEDIA"][st_ids[1]] = fix_biomass * 100
            gen_media[-1]["PERTURBATION"] = st_ids[1]
        elif chosen > 0.25:
            gen_media[-1]["PERTURBATION"] = st_ids[0] + "_" + st_ids[1]
            gen_media[-1]["MEDIA"][st_ids[0]] = fix_biomass * 100
            gen_media[-1]["MEDIA"][st_ids[1]] = fix_biomass * 100

#    elif chosen > 0.2:
#        gen_media[-1]["MEDIA"] = root["MEDIA"]
#        gen_media[-1]["PERTURBATION"] = "ROOT_EXUDATE"
        else:
            gen_media[-1]["MEDIA"]['cpd00009_e0'] = 1
            gen_media[-1]["PERTURBATION"] = 'PHOSPHATE'
    #else: none perturbation (only atrazine)

    # 2.3) add 2nd perturbation (nothing)
    # we need this to evaluate the last state of the consortium because the
    # reward function in MDPbiome for this particular case evaluates degradation of
    # atrazine 1 h after the simulation
    gen_media.append({
        "MEDIA": {
            "cpd03959_e0": 0.15
        },
        "PERTURBATION": "ATRAZINE"
    })

    # 3) Write log
    lock.acquire()
    log(
        cons, gen_media
    )  #Escribe en un archivo el objeto cons (nuestro experimento), junto con gen_media (están descritas las perturbaciones)
    lock.release()

    it = 1
    t_pers = []
    pers = []
    for mper in gen_media:
        if it == 1:
            # 4) instantiate media
            cons.media = cons.set_media(
                mper["MEDIA"],
                True)  #El primer diccionario es el medio, lo instanciamos.
        else:
            for k in mper[
                    "MEDIA"]:  # not needed at all... #Realizamos cambios en el medio.
                if mper["MEDIA"][
                        k] == 0:  #Si no hay de un metabolito, cambiamos su valor en el cons.media
                    cons.media[k] = 0
            cons.add_mets(mper["MEDIA"],
                          True)  #El resto de metabolitos los añadimos
        pers.append(
            mper["PERTURBATION"]
        )  #Metemos en esa lista el valor de la perturbación al final de cada vuelta de bucle.
        # 5) run it
        t_pers.append(cons.T[-1])
        cons.run(verbose=False,
                 plot=False,
                 maxT=intervl + cons.T[-1],
                 integrator="FEA",
                 stepChoiceLevel=(0.00027, 0.5, 100000.),
                 outp=f'{md}/{md}_plot.png',
                 outf=f'{md}/plot.tsv')
        it += 1
    txpers = {
        t_pers[i]: pers[i]
        for i in range(len(t_pers))
    }  #creamos un diccionario een el que se guardan las perturbaciones en el orden que suceden (no termino de entenderlo)

    # 6. Save simulation
    with open(f'{md}/cons.p', 'wb') as f:
        pickle.dump(cons, f)  #El pickle es para guardar objetos en ficheros
    with open(f'{md}/txpers.p', 'wb') as f:
        pickle.dump(txpers, f)
    tsv_filter(
        f'{md}/plot.tsv', f'{md}/fluxes.tsv', txpers, inplace=False, v=cons.v
    )  #plot.tsv es el output de correr cons. fluxes.tsv se crea al instanciar Consortium.
    if os.path.isfile(f'{md}/fluxes_filtered.tsv'):
        os.unlink(f'{md}/fluxes.tsv')
    mmodes.vis.plot_comm(cons)

    lock.acquire()
    print(f"\033[1;32;40mProcess with directory {md} out!\033[0m")
    lock.release()

    del (cons)
    del (txpers)
    del (gen_media)

    return
def runn(md, all_random=False):
    global lock
    lock.acquire()
    print("Starting simulation on", md)
    lock.release()

    subprocess.run(["mkdir", md])
    subprocess.run(["cp", "-r", "fb1/ModelsInput", md])
    path_curr = os.getcwd()
    os.chdir(md)
    mod_file = "ModelsInput"
    intervl = 8

    files = [
        f for f in os.listdir(".")
        if f.find("log_template.txt") != -1 or f[-3:] == "tsv"
    ]
    subprocess.run(["rm"] + files)
    with open(mod_file + "/media.json") as json_file:
        gen_media = json.load(json_file)

    inulin = {"PERTURBATION": "Inulin", "MEDIA": {"Inulin_29FruGlc": "0.0005"}}
    fos = {"PERTURBATION": "FOS", "MEDIA": {"Kestose_C18H32O16": "0.005"}}
    starch = {"PERTURBATION": "Starch", "MEDIA": {"Starch_11Glc": "0.00128"}}
    faepraa2165 = {
        "PERTURBATION": "Pro-Fprausx3.5",
        "MEDIA": {
            "FAEPRAA2165": "0.00035"
        }
    }

    media = [gen_media[0]]
    pers_perm = [inulin, fos, starch]
    if all_random:
        for i in range(0, 5):
            if random.random() > 0.75:
                pertadd = dcp(faepraa2165)
                rand = [0.00001, 0.0001]
            else:
                pertadd = dcp(random.choice(pers_perm))
                if pertadd == fos:
                    rand = [0.005, 0.015]
                elif pertadd == inulin:
                    rand = [0.0002, 0.0006]
                else:
                    rand = [0.0005, 0.0015]
            perval = random.uniform(rand[0], rand[1])
            pertadd["MEDIA"][list(pertadd["MEDIA"])[0]] = perval
            pertadd["PERTURBATION"] += str(round(perval, 5))
            media.append(pertadd)
    else:
        for i in range(0, 5):
            if random.random() > 0.9:
                media.append(faepraa2165)
            else:
                media.append(random.choice(pers_perm))

    fp = random.uniform(0.0001, 0.00055)
    ba = random.uniform(0.0001, 0.00055)
    # 1) instantiate Consortium
    volume_petri = 2 * 3.141593 * 45 * 45 * 15 * 1e-6  # 2pi*r²*h -> mm³ to L
    cons = mmodes.Consortium(
        stcut=-10,
        mets_to_plot=["Kestose_C18H32O16", "Inulin_29FruGlc", "Starch_11Glc"],
        v=volume_petri,
        manifest="COMETS_manifest.txt",
        work_based_on="name",
        max_growth=10,
        title=md)
    # 2) add models
    cons.add_model(mod_file + "/iFap484.V01.00.xml",
                   float(fp),
                   solver="glpk",
                   method="fba")
    cons.add_model(mod_file + "/iBif452.V01.00.xml",
                   float(ba),
                   solver="glpk",
                   method="fba")  # dMets = {glc.id: glc})

    lock.acquire()
    log(cons, media)
    lock.release()

    it = 1
    t_pers = []
    pers = []
    for mper in media:
        if it == 1:
            # 4) instantiate media
            cons.media = cons.set_media(mper["MEDIA"], True)
        else:
            # 4.2) or add perturbation
            for k in mper["MEDIA"]:
                if mper["MEDIA"][k] == 0:
                    cons.media[k] = 0
            cons.add_mets(mper["MEDIA"], True)
        pers.append(mper["PERTURBATION"])
        # 5) run it
        t_pers.append(cons.T[-1])
        cons.run(verbose=False,
                 plot=False,
                 maxT=intervl + cons.T[-1],
                 integrator="vode",
                 stepChoiceLevel=(0., 0.5, 100000.),
                 outp=md + "_plot.png")
        it += 1

    txpers = {t_pers[i]: pers[i] for i in range(len(t_pers))}
    # 6. Save relevant objects and generate output files
    pickle.dump(cons, open("cons.p", "wb"))
    pickle.dump(txpers, open("txpers.p", "wb"))
    pickle.dump(media, open("media.p", "wb"))
    #mmodes.vis.plot_comm(cons)
    gen_biomass("plot.tsv", cons.v, cons.T[:-1], txpers)
    gen_fluxes("flux_log_template.txt", cons.T, cons.manifest.models)
    os.chdir(path_curr)

    lock.acquire()
    print(f"\033[1;32;40mProcess with directory {md} out!\033[0m")
    del (cons)
    del (txpers)
    del (media)
    lock.release()

    return