3

我有一个包含 60,000 个文件的目录,这些文件由它们的 molid 命名。我有第二个 CSV 格式的文件,第 1 列中有 molid,第 2 列中有它们各自的 CHEMBLID。我需要将目录中的文件名 molid 与 CSV 文件中的 molid 匹配。如果找到匹配项,则将 chemblid 添加到文件中(重写文件以包含 chemblid)。我还使用 RDKit 来计算一些我需要写入修改文件的属性。我需要找到一种方法来优化它,因为我必须很快在 200 万个文件上运行它。

我与 arg parse 交互的方式是使用 bash 并行命令列出我目录中的所有 molid.sdf 文件。

csv 文件如下所示:

molid,chembl
319855,CHEMBLtest
187481,CHEMBL1527718

https://www.dropbox.com/s/6ynd9vbwwf6lqka/output_2.csv?dl=0

需要修改的文件如下所示:

298512 from gamess16 based ATb pipeline
 OpenBabel06141815083D

 34 35  0  0  1  0  0  0  0  0999 V2000
    4.3885   -1.0129    1.6972 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.3885   -0.7157    0.5784 C   0  0  2  0  0  0  0  0  0  0  0  0
    3.6479   -1.5425   -0.5699 O   0  0  0  0  0  0  0  0  0  0  0  0
    3.4599    0.7380    0.1087 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.4770    1.0889   -1.0314 C   0  0  0  0  0  0  0  0  0  0  0  0
    1.0165    0.9826   -0.6438 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.3679    2.0729    0.0029 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.9531    1.9980    0.3853 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.7151    0.8214    0.1489 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.0800    0.7051    0.5321 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.8067   -0.4453    0.2969 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.2581   -0.5636    0.6988 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.1581   -1.5376   -0.3496 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.8397   -1.4605   -0.7357 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.0762   -0.2830   -0.5007 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.2871   -0.1675   -0.8844 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.1834   -0.3978    2.5815 H   0  0  0  0  0  0  0  0  0  0  0  0
    5.4123   -0.8100    1.3616 H   0  0  0  0  0  0  0  0  0  0  0  0
    4.3301   -2.0654    2.0016 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.3709   -0.9175    0.9451 H   0  0  0  0  0  0  0  0  0  0  0  0
    3.4809   -2.4622   -0.3076 H   0  0  0  0  0  0  0  0  0  0  0  0
    3.2757    1.3897    0.9729 H   0  0  0  0  0  0  0  0  0  0  0  0
    4.4830    0.9450   -0.2346 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.6837    0.4273   -1.8785 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.6901    2.1132   -1.3637 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.9314    2.9850    0.1903 H   0  0  0  0  0  0  0  0  0  0  0  0
   -1.4318    2.8450    0.8726 H   0  0  0  0  0  0  0  0  0  0  0  0
   -3.5539    1.5524    1.0253 H   0  0  0  0  0  0  0  0  0  0  0  0
   -5.9075   -0.6633   -0.1810 H   0  0  0  0  0  0  0  0  0  0  0  0
   -5.4288   -1.4505    1.3221 H   0  0  0  0  0  0  0  0  0  0  0  0
   -5.5904    0.3146    1.2616 H   0  0  0  0  0  0  0  0  0  0  0  0
   -3.7228   -2.4486   -0.5381 H   0  0  0  0  0  0  0  0  0  0  0  0
   -1.3620   -2.3059   -1.2268 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.7671   -1.0133   -1.3738 H   0  0  0  0  0  0  0  0  0  0  0  0
  1 19  1  0  0  0  0
  1 17  1  0  0  0  0
  2 20  1  1  0  0  0
  2  1  1  0  0  0  0
  3 21  1  0  0  0  0
  3  2  1  0  0  0  0
  4  2  1  0  0  0  0
  4 22  1  0  0  0  0
  5  6  1  0  0  0  0
  5  4  1  0  0  0  0
  6  7  1  0  0  0  0
  7 26  1  0  0  0  0
  7  8  2  0  0  0  0
  8 27  1  0  0  0  0
  9  8  1  0  0  0  0
  9 10  2  0  0  0  0
 10 28  1  0  0  0  0
 11 10  1  0  0  0  0
 11 12  1  0  0  0  0
 12 31  1  0  0  0  0
 12 30  1  0  0  0  0
 13 11  2  0  0  0  0
 14 15  2  0  0  0  0
 14 13  1  0  0  0  0
 15  9  1  0  0  0  0
 16  6  2  0  0  0  0
 16 15  1  0  0  0  0
 18  1  1  0  0  0  0
 23  4  1  0  0  0  0
 24  5  1  0  0  0  0
 25  5  1  0  0  0  0
 29 12  1  0  0  0  0
 32 13  1  0  0  0  0
 33 14  1  0  0  0  0
 34 16  1  0  0  0  0
M  END
>  <molid>
298512

$$$$

https://www.dropbox.com/s/9r9kandkbahgexj/298512.sdf?dl=0

当前脚本工作方式的修改文件如下所示:

298512 from gamess16 based ATb pipeline
     RDKit          3D

 34 35  0  0  1  0  0  0  0  0999 V2000
    4.3885   -1.0129    1.6972 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.3885   -0.7157    0.5784 C   0  0  2  0  0  0  0  0  0  0  0  0
    3.6479   -1.5425   -0.5699 O   0  0  0  0  0  0  0  0  0  0  0  0
    3.4599    0.7380    0.1087 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.4770    1.0889   -1.0314 C   0  0  0  0  0  0  0  0  0  0  0  0
    1.0165    0.9826   -0.6438 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.3679    2.0729    0.0029 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.9531    1.9980    0.3853 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.7151    0.8214    0.1489 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.0800    0.7051    0.5321 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.8067   -0.4453    0.2969 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.2581   -0.5636    0.6988 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.1581   -1.5376   -0.3496 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.8397   -1.4605   -0.7357 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.0762   -0.2830   -0.5007 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.2871   -0.1675   -0.8844 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.1834   -0.3978    2.5815 H   0  0  0  0  0  0  0  0  0  0  0  0
    5.4123   -0.8100    1.3616 H   0  0  0  0  0  0  0  0  0  0  0  0
    4.3301   -2.0654    2.0016 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.3709   -0.9175    0.9451 H   0  0  0  0  0  0  0  0  0  0  0  0
    3.4809   -2.4622   -0.3076 H   0  0  0  0  0  0  0  0  0  0  0  0
    3.2757    1.3897    0.9729 H   0  0  0  0  0  0  0  0  0  0  0  0
    4.4830    0.9450   -0.2346 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.6837    0.4273   -1.8785 H   0  0  0  0  0  0  0  0  0  0  0  0
    2.6901    2.1132   -1.3637 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.9314    2.9850    0.1903 H   0  0  0  0  0  0  0  0  0  0  0  0
   -1.4318    2.8450    0.8726 H   0  0  0  0  0  0  0  0  0  0  0  0
   -3.5539    1.5524    1.0253 H   0  0  0  0  0  0  0  0  0  0  0  0
   -5.9075   -0.6633   -0.1810 H   0  0  0  0  0  0  0  0  0  0  0  0
   -5.4288   -1.4505    1.3221 H   0  0  0  0  0  0  0  0  0  0  0  0
   -5.5904    0.3146    1.2616 H   0  0  0  0  0  0  0  0  0  0  0  0
   -3.7228   -2.4486   -0.5381 H   0  0  0  0  0  0  0  0  0  0  0  0
   -1.3620   -2.3059   -1.2268 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.7671   -1.0133   -1.3738 H   0  0  0  0  0  0  0  0  0  0  0  0
  1 19  1  0
  1 17  1  0
  2 20  1  1
  2  1  1  0
  3 21  1  0
  3  2  1  0
  4  2  1  0
  4 22  1  0
  5  6  1  0
  5  4  1  0
  6  7  1  0
  7 26  1  0
  7  8  2  0
  8 27  1  0
  9  8  1  0
  9 10  2  0
 10 28  1  0
 11 10  1  0
 11 12  1  0
 12 31  1  0
 12 30  1  0
 13 11  2  0
 14 15  2  0
 14 13  1  0
 15  9  1  0
 16  6  2  0
 16 15  1  0
 18  1  1  0
 23  4  1  0
 24  5  1  0
 25  5  1  0
 29 12  1  0
 32 13  1  0
 33 14  1  0
 34 16  1  0
M  END
>  <molid>  (1) 
298512

>  <CHEMBLID>  (1) 
CHEMBL3278713

>  <i_user_TOTAL_CHARGE>  (1) 
0

>  <SMILES>  (1) 
'[H]OC([H])(C([H])([H])[H])C([H])([H])C([H])([H])C1C([H])=C([H])C2=C([H])C(=C([H])C([H])=C2C=1[H])C([H])([H])[H]'

>  <InChI>  (1) 
'InChI=1S/C15H18O/c1-11-3-7-15-10-13(5-4-12(2)16)6-8-14(15)9-11/h3,6-10,12,16H,4-5H2,1-2H3/t12-/m0/s1'

$$$$

https://www.dropbox.com/s/dfcmiv7d298s1fl/298512.chembl.sdf?dl=0

import os,shutil,csv
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-molid", help="molids from file names", type=str)
args = parser.parse_args()
print(args)
fn = args.molid
print(fn)
suppl = Chem.SDMolSupplier(fn, removeHs=False, sanitize=False)
ms = [x for x in suppl if x is not None] #sanity check loop to make sure the files were read
print("This is the number of entries read in")
print(len(ms))
print(len(suppl))
w=Chem.SDWriter('totaltest_with_chembl.sdf') #writes new file with all of the chemblid
new_files_with_chembl_id=os.path.splitext(fn)[0]
w=Chem.SDWriter(new_files_with_chembl_id+'.chembl.sdf')


molid_chemblid = open('output_2.csv','r')
csv_f = csv.reader(molid_chemblid)
header = next(csv_f)
molidIndex = header.index("molid")
chemblidIndex = header.index("chembl")
molid_chemblidList = []
for line in csv_f:
    molid = line[molidIndex]
#    print(molid)
    chembl = line[chemblidIndex]
#    print(chembl)
    molid_chemblidList.append([molid,chembl])
    for m in suppl: #molecule in MoleculeSet
        print(m)
        atbname=m.GetProp("_Name")
        fillmein=atbname.split( )[0]
        moleculeCharge=Chem.GetFormalCharge(m)
        smiles_string=Chem.MolToSmiles(m)
        inchi_string=Chem.MolToInchi(m)
        print("molecularCharge")
        print(moleculeCharge)
        print("smile_string")
        print(smiles_string)
        print("inchi string")
        print(inchi_string)
        if fillmein == molid:
        print(chembl)
        print(chembl)
        print(line)
        print("this is line in molid_chemblid",line)
        m.SetProp("CHEMBLID",chembl)
        m.SetProp("i_user_TOTAL_CHARGE",repr(moleculeCharge))
        m.SetProp("SMILES",repr(smiles_string))
        m.SetProp("InChI",repr(inchi_string))
        w.write(m)
4

1 回答 1

2

CSV 文件中的 molid 听起来像是一个唯一键。将 CSV 文件读入映射/关联数组,其中 molid 是键,行的其余部分是值,根据需要进行解析或不解析。Python 具有内置的 CSV 解析器,带有import csv.

然后在文件上循环一次,通过从地图中的文件名中查找 molid 来找到 chemblid。

这将总体工作量减少到大约 k*N,其中 N 是文件和 molids 的数量,k 是一个小数字。

您的算法在循环中有一个循环,这使其复杂度为 N*N。N=200 万确实需要一些时间 :-)

200 万个文件仍然很多,可能需要几个小时到几天的时间,具体取决于文件的大小、磁盘访问的速度等等。然后并行运行几个线程会有所帮助,直到 I/O 饱和。但首先要测试,因为实现并行方法可能会变得复杂。如果您只需要通过一次,则可以等待更长的时间。

于 2018-06-26T17:55:35.357 回答