MarvinSketch Example - Import/export of structures

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 'Import' button to import the molfile into MarvinSketch. Press the 'Export as Molfile' or the 'Export as SMILES' button to export the molecule in the specified format.

If the browser does not support LiveConnect, pressing the 'Export as Molfile' button displays the molecule string in gzip format encoded with Base64.

If the browser does not support LiveConnect a workaround is needed to import / export molecules. See the else if(!isJs2Java) branch in the code of the importMol and exportMol functions.

<script type="text/javascript">
<!--
//-->
</script>

<form NAME=MolForm onSubmit="return false">
<textarea NAME="MolTxt" ROWS=20 COLS=70>
3,7-Dihydro-1,3,7-trimethyl-1H-purine-2,6-dione
  Marvin  07099920012D

 14 15  0  0  0  0  0  0  0  0999 V2000
   -2.0245   -2.6287    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
   -2.0245   -1.0107    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
   -1.7156   -0.0596    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.4367   -1.8197    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.8415   -0.8197    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.5735   -0.8197    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -4.7075   -2.3196    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.8415   -3.8197    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.9755   -2.3196    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.9755   -1.3197    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.8415    0.1803    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
   -5.5735   -2.8197    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
   -4.7075   -1.3197    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
   -3.8415   -2.8197    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
  5 13  1  0  0  0  0
 13  7  1  0  0  0  0
  7 14  1  0  0  0  0
 14  9  1  0  0  0  0
  9 10  2  0  0  0  0
  5 10  1  0  0  0  0
  4  2  1  0  0  0  0
  2 10  1  0  0  0  0
  9  1  1  0  0  0  0
  4  1  2  0  0  0  0
  2  3  1  0  0  0  0
  5 11  2  0  0  0  0
 13  6  1  0  0  0  0
  7 12  2  0  0  0  0
 14  8  1  0  0  0  0
M  END
</textarea>
<p>
<input TYPE=BUTTON VALUE="Import" onClick="importMol('null')">
<input TYPE=BUTTON VALUE="Export as Molfile" onClick="exportMol('mol')">
<input TYPE=BUTTON VALUE="Export as SMILES" onClick="exportMol('smiles')">
</p>
</form>
<script type="text/javascript" SRC="../../../marvin.js"></script>
<script type="text/javascript" SRC="../../../js2java.js"></script>
<script type="text/javascript">
<!--
var isJs2Java = isLiveConnect(); // Is JavaScript - Java communication available?
setPath("../../..");  // Sets the relative path of the Marvin directory.

function importMol(opts) {
	var s = document.MolForm.MolTxt.value;
	if((document.MSketch != null) && isJs2Java) {
	    document.MSketch.setMol(s, opts);
	} else if(!isJs2Java) {
	    mparams = "java.lang.String";
	    if(opts != null) {
	        mparams += ",java.lang.String";
	    }
	    setMethod("MSketch.setMol",mparams);
	    addMethodParam(s);
	    if(opts != null) {
	        addMethodParam(opts);
	    }
	    runMethod();
	} else {
	    alert("Cannot import molecule:\n"+
	          "no JavaScript to Java communication in your browser.\n");
	}
}

function printMol() {
    var s = getResult();
    s = convertJs2Html(s);
    document.MolForm.MolTxt.value = s;
}

function exportMol(format) {
	if((document.MSketch != null) && isJs2Java) {
		var s = document.MSketch.getMol(format);
		document.MolForm.MolTxt.value = s;
	} else if(!isJs2Java) {
		if(format == 'smiles') {
			setIsCompressed(false);
		} else {
			setIsCompressed(true);
		}
	    setMethod("MSketch.getMol","java.lang.String");
	    addMethodParam(format);
	    setPostJsMethod("parent.printMol()");
	    runMethod();
}
msketch_name = "MSketch";
msketch_begin("../../..", 540, 480);
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 compression of the result data is highly recommended. Use the setIsCompressed method to specify that the compression of the result is required or not. If you set it to true, the the data is sent in compressed (gzip) format with Base 64 encoding. (true is the default value.)
Marvin supports molecule import as well as export in base64:gzip format.

Since getResult() returns a string that may contain special characters in an encoded format, using the convertJs2Html function is recommended to decode these characters before displaying the text in a textbox.

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!