如何避免在Linux上杀死我的代码?

发布于 2025-02-11 13:45:09 字数 2839 浏览 1 评论 0原文

我正在尝试运行应该计算分子描述符的代码,但是即使(我认为?)我有足够的内存来处理它,但它被设置的错误杀死。如何编辑代码以避免被OOM杀死?文件“ data.csv”包含〜62K行,其中包含字符串数据。

import deepchem
import os
import numpy as np
import pandas as pd

import tempfile

from rdkit import Chem
from rdkit.Chem import AllChem
import deepchem as dc

from deepchem.utils import download_url, load_from_disk
from simtk.openmm.app import PDBFile
from pdbfixer import PDBFixer

from deepchem.utils.vina_utils import prepare_inputs
import pandas as pd
import os
data = pd.read_csv('data.csv')

pdbid = data['pdb_id'].iloc[1]
ligand = data['smiles'].iloc[1]

fixer = PDBFixer(pdbid=pdbid)
PDBFile.writeFile(fixer.topology, fixer.positions, open('%s.pdb' % (pdbid), 'w'))

p, m = None, None
# fix protein, optimize ligand geometry, and sanitize molecules
try:
    p, m = prepare_inputs('%s.pdb' % (pdbid), ligand)
except:
    print('%s failed PDB fixing' % (pdbid)) 

if p and m:  # protein and molecule are readable by RDKit
    print(pdbid, p.GetNumAtoms())
    Chem.rdmolfiles.MolToPDBFile(p, '%s.pdb' % (pdbid))
    Chem.rdmolfiles.MolToPDBFile(m, 'ligand_%s.pdb' % (pdbid))

pdbids = data['pdb_id'].values
ligand_smiles = data['smiles'].values


l = []
for (pdbid, ligand) in zip(pdbids, ligand_smiles):
  fixer = PDBFixer(url='https://files.rcsb.org/download/%s.pdb' % (pdbid))
  PDBFile.writeFile(fixer.topology, fixer.positions, open('%s.pdb' % (pdbid), 'w'))
  
  p, m = None, None
  # skip pdb fixing for speed
  try:
    p, m = prepare_inputs('%s.pdb' % (pdbid), ligand, replace_nonstandard_residues=False,
                          remove_heterogens=False, remove_water=False,
                          add_hydrogens=False)
  except:
    print('%s failed sanitization' % (pdbid)) 

  if p and m:  # protein and molecule are readable by RDKit
    one = Chem.rdmolfiles.MolToPDBFile(p, '%s.pdb' % (pdbid))
    two = Chem.rdmolfiles.MolToPDBFile(m, 'ligand_%s.pdb' % (pdbid))
    l.append(p)
    l.append(m)

proteins = [f for f in os.listdir('.') if len(f) == 8 and f.endswith('.pdb')]
ligands = [f for f in os.listdir('.') if f.startswith('ligand') and f.endswith('.pdb')]

# Handle failed sanitizations
failures = set([f[:-4] for f in proteins]) - set([f[7:-4] for f in ligands])
for pdbid in failures:
  proteins.remove(pdbid + '.pdb')

pdbids = [f[:-4] for f in proteins]
small_dataset = data[data['pdb_id'].isin(pdbids)]
labels = small_dataset.labels
fp_featurizer = dc.feat.CircularFingerprint(size=2048)
features = fp_featurizer.featurize([Chem.MolFromPDBFile(l) for l in ligands])
dataset = dc.data.NumpyDataset(X=features, y=labels, ids=pdbids)
a = dataset.to_dataframe()
a.to_csv('descr.csv')

OOM错误和内存状态的屏幕截图

I'm trying to run my code which is supposed to calculate molecular descriptors, but it gets killed by an Out-of-memory error, even though (I think?) I have enough memory to process it. How can I edit my code to avoid killing by OOM? The file 'Data.csv' contains ~62k rows with string data inside.

import deepchem
import os
import numpy as np
import pandas as pd

import tempfile

from rdkit import Chem
from rdkit.Chem import AllChem
import deepchem as dc

from deepchem.utils import download_url, load_from_disk
from simtk.openmm.app import PDBFile
from pdbfixer import PDBFixer

from deepchem.utils.vina_utils import prepare_inputs
import pandas as pd
import os
data = pd.read_csv('data.csv')

pdbid = data['pdb_id'].iloc[1]
ligand = data['smiles'].iloc[1]

fixer = PDBFixer(pdbid=pdbid)
PDBFile.writeFile(fixer.topology, fixer.positions, open('%s.pdb' % (pdbid), 'w'))

p, m = None, None
# fix protein, optimize ligand geometry, and sanitize molecules
try:
    p, m = prepare_inputs('%s.pdb' % (pdbid), ligand)
except:
    print('%s failed PDB fixing' % (pdbid)) 

if p and m:  # protein and molecule are readable by RDKit
    print(pdbid, p.GetNumAtoms())
    Chem.rdmolfiles.MolToPDBFile(p, '%s.pdb' % (pdbid))
    Chem.rdmolfiles.MolToPDBFile(m, 'ligand_%s.pdb' % (pdbid))

pdbids = data['pdb_id'].values
ligand_smiles = data['smiles'].values


l = []
for (pdbid, ligand) in zip(pdbids, ligand_smiles):
  fixer = PDBFixer(url='https://files.rcsb.org/download/%s.pdb' % (pdbid))
  PDBFile.writeFile(fixer.topology, fixer.positions, open('%s.pdb' % (pdbid), 'w'))
  
  p, m = None, None
  # skip pdb fixing for speed
  try:
    p, m = prepare_inputs('%s.pdb' % (pdbid), ligand, replace_nonstandard_residues=False,
                          remove_heterogens=False, remove_water=False,
                          add_hydrogens=False)
  except:
    print('%s failed sanitization' % (pdbid)) 

  if p and m:  # protein and molecule are readable by RDKit
    one = Chem.rdmolfiles.MolToPDBFile(p, '%s.pdb' % (pdbid))
    two = Chem.rdmolfiles.MolToPDBFile(m, 'ligand_%s.pdb' % (pdbid))
    l.append(p)
    l.append(m)

proteins = [f for f in os.listdir('.') if len(f) == 8 and f.endswith('.pdb')]
ligands = [f for f in os.listdir('.') if f.startswith('ligand') and f.endswith('.pdb')]

# Handle failed sanitizations
failures = set([f[:-4] for f in proteins]) - set([f[7:-4] for f in ligands])
for pdbid in failures:
  proteins.remove(pdbid + '.pdb')

pdbids = [f[:-4] for f in proteins]
small_dataset = data[data['pdb_id'].isin(pdbids)]
labels = small_dataset.labels
fp_featurizer = dc.feat.CircularFingerprint(size=2048)
features = fp_featurizer.featurize([Chem.MolFromPDBFile(l) for l in ligands])
dataset = dc.data.NumpyDataset(X=features, y=labels, ids=pdbids)
a = dataset.to_dataframe()
a.to_csv('descr.csv')

screenshot of OOM error and memory status

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文