1

我正在构建一个 Flask webapp,它使用 OpenBabel API(化学工具包)为我生成一些文件。当我调用这个特定的函数时,它似乎工作正常并在我想要的目录中生成文件。但是,一旦它回到 Flask 就会崩溃,并且 Flask 不会呈现 html 模板,而是将我重定向到This page isn’t working 127.0.0.1 didn’t send any data. 我发现在删除函数中的代码时它可以正常工作。所以这可能是 OpenBabel 的问题。Openbabel 函数本身不会输出任何错误,并且基于调试似乎甚至在最后返回。

我从其他 SO 答案中尝试了很多东西,包括将我的主机更改为 0.0.0.0、添加 threaded=True 和其他一些解决方案。一切都无济于事。我尝试调试了很长时间,但现在我迷路了,因为我已经尝试了我所知道的一切。我所能得到的只是来自 Flask 的 SystemExit 异常。有时它能够在它之后运行 print 语句,但更多时候它会立即崩溃。我不知道问题可能出在哪里。感谢我能得到的任何帮助。代码示例(稍微缩短了一点):

@app.route("/", methods=["POST", "GET"])
def form_handler():
    if request.method == "POST":
        smiles = request.form["smiles_molecule"]
        pdb_file = request.files["pdb_molecule"]
        no_conformers = int(request.form["no_conformers"])
        scoring = request.form["score"]
        if smiles:
            pattern = re.compile('[^A-Za-z0-9]+')
            smiles_no_special_chars = re.sub(pattern, "", smiles)
            mol_path = os.path.join(app.config["MOLECULE_UPLOADS"], smiles_no_special_chars[0:10])
            if os.path.exists(mol_path):
                shutil.rmtree(mol_path)
            os.mkdir(mol_path)
            os.chdir(mol_path)
            x = conf_gen.gen_and_write_confs(smiles, no_conformers, scoring) #<- breaks down here
            print(x)
            return render_template("index.html", mole=smiles_no_special_chars[0:10])

调用的函数:

def gen_and_write_confs(molecule, no_confs, scoring):
    
    """Generate and write the conformers to PDB. Takes mol, number of conformers and
    scoring method: RCS, MECS and ECS: OBRMSDConformerScore,
    OBMinimizingEnergyConformerScore and OBEnergyConformerScore. See OpenBabel docs."""

    mole = pybel.readstring("can", molecule)
    mole.addh()
    mole.make3D()
    mole = mole.OBMol
    mole.SetChainsPerceived(True)

    cs_obj = ob.OBConformerSearch()
    cs_obj.Setup(mole, no_confs, 5, 5, 25)
    if scoring == "RCS":
        score = ob.OBRMSDConformerScore()
    elif scoring == "MECS":
        score = ob.OBMinimizingEnergyConformerScore() 
    else:
        score = ob.OBEnergyConformerScore()
    cs_obj.SetScore(score)
    cs_obj.Search()
    cs_obj.GetConformers(mole)
    mole.DeleteNonPolarHydrogens()

    return "Test"

如果需要,我可以将项目上传到 Github。它确实需要安装一些依赖项,我现在正在使用 conda,但我也可以通过 pip 使其可用,因为 OpenBabel 在 pip 中可用。

Ps:崩溃后没有显示任何错误消息

4

0 回答 0