无法在python / pybel中捕获/重定向OpenBabel警告

时间:2019-03-13 18:29:48

标签: openbabel pybel

我正在使用pybel将数据库中的1000多个SMILES字符串转换为规范形式。当我这样做时,我会收到约300条警告。大多数是:

==============================
*** Open Babel Warning  in OpenBabel::OBSmilesParser::ParseSmiles
  Ignoring stereochemistry. Not enough connections to this atom. 
==============================

我猜没有人认为将字符串包含在问题中很重要。

我假设警告将被写入sys.stderr。显然这是正确的,因为此命令行:python foo.py 2> err.txt确实将警告发送到了文件。但是,警告似乎已被缓冲。因此,我尝试在写入要转换的字符串之前和之后刷新sys.stderr

def canonicalize(smiles):
    """
    Returns canonical form of a SMILES string.  Uses OpenBabel.
    """
    print(file=sys.stderr, flush=True)
    print(smiles,file=sys.stderr,flush=True)
    canonicalizer = OBConversion()
    canonicalizer.SetOutFormat('smi')
    canonicalizer.SetOptions('inc', OBConversion.OUTOPTIONS)
    c = canonicalizer.WriteString(readstring('smi', smiles).OBMol)[:-1]
    print(file=sys.stderr, flush=True)
    print("----->", c, file=sys.stderr)
    print(file=sys.stderr, flush=True)
    return  c

但是我仍然得到异步结果。 (正如您在此搜索中的行号上看到的那样,警告是成组发出的,实际上此组中大约有35条警告。)

Line 2965:   Ignoring stereochemistry. Not enough connections to this atom. 
Line 2968:   Ignoring stereochemistry. Not enough connections to this atom. 
Line 2971:   Ignoring stereochemistry. Not enough connections to this atom. 
Line 2974:   Ignoring stereochemistry. Not enough connections to this atom. 
Line 2977:   Ignoring stereochemistry. Not enough connections to this atom. 

如何通过将流重定向到python中的stderr或从Python抓取流并将其写入,或者只是从python刷新缓冲区,将跟踪信息写入与OpenBabel相同的位置?

0 个答案:

没有答案