File: testMultithreadedMolSupplier.py

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (140 lines) | stat: -rw-r--r-- 5,109 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import doctest
import gzip
import os
import sys
import unittest

from rdkit import Chem, RDConfig, __version__, rdBase


class TestCase(unittest.TestCase):

  def testMultiSmiMolSupplier(self):
    fileN = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'FileParsers', 'test_data',
                         'first_200.tpsa.csv')
    # fileN = "../FileParsers/test_data/first_200.tpsa.csv"
    smiSup = Chem.MultithreadedSmilesMolSupplier(fileN, ",", 0, -1)
    i = 0
    while not smiSup.atEnd():
      mol = next(smiSup)
      if (mol):
        i += 1
    self.assertTrue(i == 200)
    fileN = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'FileParsers', 'test_data',
                         'fewSmi.csv')
    # fileN = "../FileParsers/test_data/fewSmi.csv"
    smiSup = Chem.MultithreadedSmilesMolSupplier(fileN, delimiter=",", smilesColumn=1, nameColumn=0,
                                                 titleLine=0)
    names = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
    props = [
      "34.14", "25.78", "106.51", "82.78", "60.16", "87.74", "37.38", "77.28", "65.18", "0.00"
    ]
    confusedNames = []
    confusedProps = []
    i = 0
    for mol in smiSup:
      if mol is not None:
        self.assertTrue(mol.HasProp("_Name"))
        self.assertTrue(mol.HasProp("Column_2"))
        prop = mol.GetProp("Column_2")
        name = mol.GetProp("_Name")
        confusedProps.append(prop)
        confusedNames.append(name)
        i += 1
    self.assertTrue(i == 10)
    self.assertTrue(sorted(confusedNames) == sorted(names))
    self.assertTrue(sorted(confusedProps) == sorted(props))

    # context manager
    confusedNames = []
    confusedProps = []
    i = 0
    with Chem.MultithreadedSmilesMolSupplier(fileN, delimiter=",", smilesColumn=1, nameColumn=0,
                                             titleLine=0) as smiSup:
      for mol in smiSup:
        if mol is not None:
          self.assertTrue(mol.HasProp("_Name"))
          self.assertTrue(mol.HasProp("Column_2"))
          prop = mol.GetProp("Column_2")
          name = mol.GetProp("_Name")
          confusedProps.append(prop)
          confusedNames.append(name)
          i += 1
      self.assertTrue(i == 10)
      self.assertTrue(sorted(confusedNames) == sorted(names))
      self.assertTrue(sorted(confusedProps) == sorted(props))

  def testMultiSDMolSupplier(self):
    fileN = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'FileParsers', 'test_data',
                         'NCI_aids_few.sdf')
    # fileN = "../FileParsers/test_data/NCI_aids_few.sdf"
    sdSup = Chem.MultithreadedSDMolSupplier(fileN)
    molNames = [
      "48", "78", "128", "163", "164", "170", "180", "186", "192", "203", "210", "211", "213",
      "220", "229", "256"
    ]
    confusedMolNames = []
    i = 0
    for mol in sdSup:
      if mol is not None:
        confusedMolNames.append(mol.GetProp("_Name"))
        i += 1
    self.assertTrue(len(molNames) == i)
    self.assertTrue(sorted(confusedMolNames) == sorted(molNames))

    # context manager
    confusedMolNames = []
    i = 0
    with Chem.MultithreadedSDMolSupplier(fileN) as sdSup:
      for mol in sdSup:
        if mol is not None:
          confusedMolNames.append(mol.GetProp("_Name"))
          i += 1
    self.assertTrue(len(molNames) == i)
    self.assertTrue(sorted(confusedMolNames) == sorted(molNames))

  # NOTE these are disabled until we rewrite the code to construct a
  #      MultithreadedSDMolSupplier from a python stream
  @unittest.skip("Skipping construction from stream")
  def testMultiSDMolSupplierFromStream(self):
    fileN = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'FileParsers', 'test_data',
                         'NCI_aids_few.sdf')
    molNames = [
      "48", "78", "128", "163", "164", "170", "180", "186", "192", "203", "210", "211", "213",
      "220", "229", "256"
    ]
    # try opening with streambuf
    inf = open(fileN, 'rb')
    if (inf):
      gSup = Chem.SDMolSupplierFromStream(inf)
      confusedMolNames = []
      i = 0
      for mol in gSup:
        # print("!!",i,file=sys.stderr);sys.stderr.flush()
        if (mol):
          confusedMolNames.append(mol.GetProp("_Name"))
          i += 1
      self.assertTrue(len(molNames) == i)
      self.assertTrue(sorted(confusedMolNames) == sorted(molNames))
    #   print("done!",file=sys.stderr);sys.stderr.flush()
    # try opening with streambuf
    fileN = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'FileParsers', 'test_data',
                         'NCI_aids_few.sdf.gz')
    # try opening with gzip
    inf = gzip.open(fileN)
    if (inf):
      gSup = Chem.SDMolSupplierFromStream(inf)
      confusedMolNames = []
      i = 0
      for mol in gSup:
        # print("!",i,file=sys.stderr);sys.stderr.flush()
        if (mol):
          confusedMolNames.append(mol.GetProp("_Name"))
          i += 1
      self.assertTrue(len(molNames) == i)
      self.assertTrue(sorted(confusedMolNames) == sorted(molNames))


if __name__ == '__main__':
  print("Testing Smiles and SD MultithreadedMolSupplier")
  unittest.main()