Exemplo n.º 1
0
def test_all_optimization_scripts():
    """TODO: To be deprecated or convert to example in Jupyter notebook.
    """
    logger = create_logger(name="optstoicpy.test.testAll")

    logger.info("Test optstoic")
    lp_prob1, pathways1 = optstoic.test_optstoic()

    logger.info(
        "Test optstoic glycolysis. Runtime depends on the solver used.")
    lp_prob2, pathways2 = optstoic_gly.test_optstoic_glycolysis()

    logger.info("Generate kegg_model and draw pathway")
    res_dir = os.path.join(current_dir, 'result/')
    if not os.path.exists(res_dir):
        os.makedirs(res_dir)

    f = open(os.path.join(res_dir, 'test_KeggModel.txt'), 'w+')

    for ind, p in pathways2.items():
        p.rearrange_reaction_order()
        generate_kegg_model(p, filehandle=f)
        graph_title = "{0}_{1}ATP_P{2}".format(p.name, p.nATP, p.id)
        drawpathway.draw_pathway(p,
                                 os.path.join(res_dir +
                                              '/pathway_{0:03d}'.format(p.id)),
                                 imageFormat='png',
                                 graphTitle=graph_title)
    f.close()

    logger.info("optstoicpy.test.testAll completed!")
Exemplo n.º 2
0
def runAnalysis(resultDict, numATP, outputFilePath, imgFormat='png', shift_pathway_id_by=0,\
                sourceSubstrateID='C00031', endSubstrateID='C00022', darkBackgroundMode=False):
    logging.info("Analyzing results... \n")

    pathway_objects = []

    outputFileName='OptStoic_gams_{0}ATP'.format(numATP)

    f = open(os.path.join(outputFilePath, outputFileName + '_KeggModel.txt'), 'w+')

    for ind, res in sorted(resultDict.items()):
        logging.debug("Pathway %s"%ind)
        if "pathway" not in res:
            logging.info("OptStoic terminated with infeasible solution.")
            continue
        elif "num_reaction" not in res:
            logging.info("Pathway is incomplete...")
            continue

        p = Pathway(id=int(ind)+shift_pathway_id_by, name='OptStoic_gams', reaction_ids=list(res['pathway'].keys()),
                    fluxes=list(res['pathway'].values()), sourceSubstrateID=sourceSubstrateID, endSubstrateID=endSubstrateID,
                    total_flux_no_exchange=res['total_flux_no_exchange'],
                    note={'modelstat': res.get("modelstat"), 'solvestat': res.get("solvestat")})
        p.rearrange_reaction_order()
        pathway_objects.append(p)
        generate_kegg_model(p, filehandle=f)

        graph_title = "{0}_{1}ATP_P{2}".format(p.name, p.nATP, p.id)
        if res['modelstat'] != 1:
            graph_title += '; Modelstat={0}'.format(res['modelstat'])
        if imgFormat:
            draw_pathway(p, os.path.join(outputFilePath+'/pathway_{0:03d}'.format(p.id)),
                        imageFormat=imgFormat, graphTitle=graph_title, darkBackgroundMode=darkBackgroundMode)


    #pickle.dump(resultDict, open(outputFilePath+outputFileName+'_pathways_dict.pkl', 'w+'))
    pickle.dump(pathway_objects, open(outputFilePath + outputFileName + '_pathways_obj.pkl', 'w+'))

    logging.info("\nDone!\n")
    logging.info("Check your output folder: %s"%outputFilePath)

    return pathway_objects
Exemplo n.º 3
0
    def test_kegg_model_generation(self):
        self.logger.info(
            "Creating 'res' folder in the current directory if not exist...")
        # outputFilepath = 'res'
        # outputFilename = 'OptStoic'
        # try:
        #     os.makedirs(outputFilepath)
        # except OSError:
        #     if not os.path.isdir(outputFilepath):
        #         raise Exception

        self.logger.info("Test create KEGG model file")

        filename = "./test_kegg_model_generation.txt"
        f = open(filename, 'a+')
        kegg_model_text = generate_kegg_model(self.p1, filehandle=f)
        print(kegg_model_text)
        self.assertIn('R01512', kegg_model_text)
        self.assertIn('R01512', kegg_model_text)
        f.close()

        if os.path.exists(filename):
            os.remove(filename)