Skip to content

Calculate MolWeight and generate SMILES

/*
* Calculate Molweight and Smiles
*
*
* Usage:
* 1. Edit the name of the Structure field
* 2. Run button script
*
* For more details see:
* https://docs.chemaxon.com/display/jchembase/Utilities
*
* @author David Pech <dpech@chemaxon.com>
*/
import com.im.df.api.*
import com.im.df.api.support.SelectionDescription
import com.im.ijc.core.api.util.IJCCoreUtils
import chemaxon.struc.Molecule
import chemaxon.util.MolHandler

init = { widget ->
}
destroy = { widget ->
}
evaluate = { widget ->
    def ety = dataTree.rootVertex.entity // assumes you have reference to the data tree
    def molFld = ety.fields.items.find { it.name == 'Structure' } // find the structure field
    def rs = ety.schema.dataProvider.getDefaultResultSet(dataTree, false, DFEnvironmentRO.DEV_NULL) // find the ResultSet
    def rootVS = rs.getVertexState(dataTree.rootVertex) // obtain the VertexState

    List ids = rootVS.getSelectedRowsIds() // get the selected IDs
    if (ids.size == 1) {
        Map rows = rootVS.getData(ids, DFEnvironmentRO.DEV_NULL) // get the data
        Map row = rows[ids[0]] // get the first and only row
        MarvinStructure mol = row[molFld.id] // Get the Structure. Its a com.im.df.api.chem.MarvinStructure instance
        Molecule cxnMol = mol.getNative() // obtain the chemaxon.struc.Molecule instance

        // Convert molecule to SMILES
        String molSmiles = cxnMol.toFormat("smiles")
        // Canonical SMILES
        // String molSmiles = cxnMol.toFormat("smiles:u")
        println molSmiles
        // create a MolHandler instance
        // set molecule from SMILES
        // calculate molecular weight
        MolHandler mh = new MolHandler();
        mh.setMolecule(molSmiles);
        println mh.calcMolWeight()

    } else {
        println "bad selection"
    }
}
on_change = { widget, button ->

}