Reading Molecules From a File

    IJC can read a variety of molecule file types, handled via MolImporter. MolImporter automatically recognizes file structures, making import a very simple process. The scriptlet below sets up a while loop, one of the most common uses of MolImporter in scripts. This cycles through a SD file, echoing the raw molecule. While it is not echoed, the grabbed molecule (String molStr) is a marvin structure, which is the format to use when inserting into a structures table.

    import chemaxon.formats.MolImporter
    
    String file = 'C:/data/import.sdf'
    
    importer = new MolImporter(file)
    importer.grabbingEnabled = true
    mol = importer.createMol()
    
    while (importer.read(mol)) {
        String molStr = importer.grabbedMoleculeString
        print "Molecule is $mol"   
    }

    Files can be specified in the code themselves (as above), or you can use JFileChooser to prompt the user for a file.

    import javax.swing.*
    
    def chooser = new JFileChooser()
    chooser.setDialogTitle('Select file')
    if (chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
        fileName = chooser.selectedFile.canonicalPath
        // fileName is then used when creating a MolImporter
    } else {
        return 
    }