Exporting Data

    Exporting data to a file is easily done via the built-in class MolExporter. This supports output to several different formats, including mol, sdf, smiles, png, jpeg, etc. This example will discuss the most common usage, the SD file format.

    The exporter itself simply writes the information, such as a molecule and associated properties. In the case of RDfile/SDfile, the associated properties must be built into the molecule first. This is done by constructing a map with setProperty, as shown below. In this example, a new exporter is created. The scriptlet then cycles through a list of IDs, gets the molecule from a structures table, and sets the property "REGID" with the value from the table. It then writes the molecule to the open file and closes the file when finished. The scriptlet should work unchanged against the PubChem demo data.

    import chemaxon.formats.MolExporter
    import com.im.commons.progress.*
    def parent = dataTree.rootVertex.entity // assumes you have reference to the data tree
    def molFld = parent.fields.items.find { it.name == 'Structure' } // find the structure field
    def dbregidFld = parent.fields.items.find { it.name == 'DB regid' } // find the structure field
    MolExporter exporter = new MolExporter('C:/tmp/save_file_name.sdf', 'sdf')
    try {
        def rs = parent.schema.dataProvider.getDefaultResultSet(dataTree, false, DFEnvironmentRO.DEV_NULL)
        def rootVS = rs.getVertexState(dataTree.rootVertex)
        List ids = rootVS.getSelectedRowsIds() // get the selected IDs
        ids.each { id ->
            def getData = rootVS.getData([id], DFEnvironmentRO.DEV_NULL)
            def mol = getData[id][molFld.id] // get the moelcule
            def expMol = mol.native.cloneMolecule() // clone the molecule to prevent modifying the original
            def regid = getData[id][dbregidFld.id] // get the reg id
            expMol.setProperty('REGID', regid) // set the extra property
    } finally {
        exporter.close() // IMPORTANT: close the exporter so that the file is closed.