Access RDKit functionality via Java Snippet node

For the sake of those who will google this in the future, I can confirm that this now works great in KNIME 3.3.0; just add org.RDKit.jar among the "Additional libraries" while configuring the Java Snippet node.

The Java snippet below counts the number of chiral centres in the molecules provided on input, as expected.

Thanks a lot to the KNIME team for making this possible!

Paolo

 

// system imports
import org.knime.base.node.jsnippet.expression.AbstractJSnippet;
import org.knime.base.node.jsnippet.expression.Abort;
import org.knime.base.node.jsnippet.expression.Cell;
import org.knime.base.node.jsnippet.expression.ColumnException;
import org.knime.base.node.jsnippet.expression.TypeException;
import static org.knime.base.node.jsnippet.expression.Type.*;
import java.util.Date;
import java.util.Calendar;
import org.w3c.dom.Document;


// Your custom imports:
import org.RDKit.ROMol;
import org.RDKit.RWMol;
import org.RDKit.Atom;
import org.RDKit.RDKFuncs;

// system variables
public class JSnippet extends AbstractJSnippet {
  // Fields for input columns
/** Input column: "Molecule" */
  public String c_Molecule;

  // Fields for output columns
/** Output column: "nChiral" */
  public Integer out_nChiral;

// Your custom variables:

// expression start
    public void snippet() throws TypeException, ColumnException, Abort {
// Enter your code here:
ROMol m = RWMol.MolFromMolBlock(c_Molecule, true, false);
if (m != null) {
    RDKFuncs.assignStereochemistry(m, false, true, true);
    out_nChiral = 0;
    for (int i = 0; i < m.getNumAtoms(); ++i) {
        if ((m.getAtomWithIdx(i).getChiralTag() != Atom.ChiralType.CHI_UNSPECIFIED)
            || m.getAtomWithIdx(i).hasProp("_ChiralityPossible"))
            ++out_nChiral;
    }
}


// expression end
    }
}