MarvinSketch Example - Retrieving a structure
In Macintosh systems, there are several browsers that do not support standard JavaScript to Java communication (LiveConnect). This example shows a workaround for this problem.
Press the 'Submit as Molfile' or the 'Submit as SMILES' button to retrieve the
molecule from MarvinSketch in the specified format and post it to another page to display the structure in MarvinView.
If LiveConnect is available the document.MSketch.getMol(format) statement can be used
to get the structure from the MSketch applet.
Otherwise a workaround is needed.
See the else if(!isjs2java) branch in the code of the getMol method.
In this case, instead of document.MSketch.getMol the following code should be used:
setMethod("MSketch.getMol","java.lang.String");
addMethodParam(format);
setPostJsMethod("parent.submitMethod()");
runMethod();
where
- The
setMethod(methodname,paramtypes)call specifies the name and the types of the parameters of the applet method that you want to call.- The
addMethodParam(paramvalue)call sets the value of then-th parameter of the method. (Follow the order of the parameters in the method specification.)- The
setPostJsMethod(paramvalue)call defines which JavaScript function has to be called after the result of the applet method is retrieved.runMethod()submits the request and sends it to the applet. Since therunMethod()does not wait for the result of the request,runMethod()has to be the last statement in thegetMolfunction.
The submitMethod() operation sets the result molecule in the molecule field of the form and submits
the form.
Since this method will be called after the applet has sent back the result, you can already
refer to the result of the applet method with getResult().
When the form is submited, the molecule is posted to the display-result.jsp page.
<script type="text/javascript" src="../../../marvin.js"></script>
<script type="text/javascript" src="../../../js2java.js"></script>
<form name="molform" method="post" action="../display-result.jsp">
<input type="button" value="Submit as Molfile"
onClick="getMol('mol')">
<input type="button" value="Submit as SMILES"
onClick="getMol('smiles')">
<input type="hidden" name="molecule">
</form>
<script type="text/javascript">
<!--
var isJs2Java = isLiveConnect(); // Is JavaScript - Java communication available?
setPath("../../.."); // Sets the relative path of the Marvin directory.
/* Sets form variable and submits the form.*/
function submitMethod() {
document.molform.molecule.value = getResult();
document.molform.submit();
}
/* Retrieves the molecule (in the specified format) from the sketcher and
calls 'submitMethod()'.
The 'runMethod' method should be the last statement in this function.*/
function getMol(format) {
if((document.MSketch != null) && isJs2Java) {
var s = document.MSketch.getMol(format);
document.molform.molecule.value = s;
document.molform.submit();
} else if(!isJs2Java) {
setMethod("MSketch.getMol","java.lang.String");
addMethodParam(format);
setPostJsMethod("parent.submitMethod()");
runMethod();
} else {
alert("Cannot import molecule:\n" +
"no JavaScript to Java communication in your browser.\n");
}
}
msketch_name="MSketch";
msketch_begin("../../../",540,480);
msketch_param("mol","../../../mols-2d/caffeine.mol");
msketch_end();
//--></script>
The limitation of this workaround is that the result string can be 2K characters the maximum.
Because of the limited transfer size, the data is sent in compressed (gzip) format with Base 64 encoding.
Marvin supports molecule import as well as export in
base64:gzip format.
Do you have a question? Would you like to learn more? Please browse among the related topics on our support forum or search the website. If you want to suggest modifications or improvements to our documentation email our support directly!


