1.1.1.2. Retrieval and Processing of MDL SD-File Structure Data

 1import sys
 2
 3import CDPL.Chem as Chem
 4
 5
 6# retrieves and prints the MDL structure data of the given molecule
 7def printStructureData(mol: Chem.Molecule) -> None: 
 8    if not Chem.hasStructureData(mol):        # is a structure data property available?
 9        print('Error: no structure data available for molecule \'%s\'' % Chem.getName(mol))
10        return
11
12    struct_data = Chem.getStructureData(mol)  # retrieve structure data
13    
14    print('Structure data (%s entries) of molecule \'%s\':\n' % (str(len(struct_data)), Chem.getName(mol)))
15
16    i = 1
17    
18    for entry in struct_data:                 # iterate of structure data entries consisting of a header line and the actual data
19        print('----- Entry %s -----' % str(i))
20        print('Header: ' + entry.header)
21        print('Data: ' + entry.data)
22
23        i += 1
24    
25def main() -> None:
26    if len(sys.argv) < 2:
27        sys.exit('Usage: %s <input SD-file>' % sys.argv[0])
28
29    # create reader for MDL SD-files
30    reader = Chem.FileSDFMoleculeReader(sys.argv[1])
31    
32    # create an instance of the default implementation of the Chem.Molecule interface
33    mol = Chem.BasicMolecule()
34    
35    # read and process molecules one after the other until the end of input has been reached
36    try:
37        while reader.read(mol): 
38            try:
39                printStructureData(mol)
40            except Exception as e:
41                sys.exit('Error: processing of molecule failed: ' + str(e))
42                
43    except Exception as e: # handle exception raised in case of severe read errors
44        sys.exit('Error: reading molecule failed: ' + str(e))
45
46    sys.exit(0)
47        
48if __name__ == '__main__':
49    main()

Download source file