1.1.1.2. Retrieval and processing of mdl sd-file structure data

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

Download source file