January 2, 2012

Using CDK to output a ChemDoodle web component canvas

A very short description of how to get your CDK java code to output a canvas that can be displayed using the ChemDoodle web components java script library.
What ChemDoodle web components needs is actually a molfile with 2D coordinates on one line without line breaks (and explicit \n in place of the line breaks). So that's what we're generating here.
The molecule (variable name "molecule" is assumed to have 2D coordinates, if it only has 3D coordinates it will not look very nice, and without coordinates it will not work at all.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MDLV2000Writer writer = new MDLV2000Writer(baos);

//force 2D coordinates even if 3D exists
Properties customSettings = new Properties();
customSettings.setProperty("ForceWriteAs2DCoordinates", "true");
PropertiesListener listener = new PropertiesListener(customSettings);
//end force 2D coordinates

//write the molecule to the outputstream
try {
} catch (CDKException e) {

//convert the outputstream to a string
String MoleculeString = baos.toString();

//now split MoleculeString into multiple lines to enable explicit printout of \n
String Moleculelines[] = MoleculeString.split("\\r?\\n");

//now we print the javascript variable in the canvas
outfile.print("var myMolFile = '");
for(int i=0; i<Moleculelines.length; i++){
And then you just have to surround this javascript variable with the required settings and canvas definitions as described in my earlier posts about ChemDoodle: Chemdoodle web components tricks #1: scaling molecules, Chemdoodle web components tricks #2: showing atom numbers , Chemdoodle web components tricks #3: Using mouseover functions to do multiple repaints on a canvas

1 comment:

  1. Wall art is one of the most important aspects of a beautifully crafted space. That being said, we know that buying good art takes a lot of time and money. For this reason, we’ve created Wall26™ - the online hub for high quality and affordable art. canvas decor