MOL file compression

Molfile compression

MarvinSketch and MarvinView can handle compressed molfiles that are typically five times smaller than their original, uncompressed version. This reduces the download time of HTML pages containing molecule applets.

Compressed molfiles can be created by choosing Edit/Source, then Format/Compressed Molfile in MarvinSketch or MarvinView. If you cannot find the Edit menu, then click on the upper left arrow in MarvinSketch, right click or double click the compound in MarvinView.

Code: csmol, cssdf, csrxn, csrdf

Extensions: .csmol, .cssdf, .csrxn, .csrdf

Using the MdlCompressor class in Java

The chemaxon.formats.MdlCompressor can compress or decompress MDL Molfiles, SDfiles, RGfiles and Rxnfiles in two ways:

  1. In one step: if the input file contents are in a byte array or in a String.
    MdlCompressor has two static methods that convert in one step:

    public static byte[] convert(byte[] mol, int flags) throws IOException;
    public static String convert(String mol, int flags) throws IOException;

    The following flags can be specified:

    • COMPRESS for compression

    • DECOMPRESS for decompression

  2. Molecule by molecule: using an InputStream and an OutputStream.

    import java.io.*;
    import chemaxon.formats.*;
     
    public class Example {
    public static void main(String args[]) {
    int n = 0;
    try {
    FileInputStream in = new FileInputStream("2000.sdf");
    MdlCompressor mc = new MdlCompressor(in, System.out, MdlCompressor.COMPRESS);
    while(mc.convert()) ++n;
    } catch(FileNotFoundException ex) {
    System.err.println("File not found");
    } catch(MolFormatException ex) {
    System.err.println("Bad file format");
    } catch(IOException ex) {
    System.err.println("Unexpected end of file");
    }
    System.out.println("Number of molecules: "+n);
    }
    }

Note that there is a more general converter class in the Chemaxon class library (too general to be open source), chemaxon.formats.MolConverter, that is also a command line application (it has a main() method). In a Java program, it can be used similarly to MdlCompressor. Only the constructor differs:

MolConverter mc = new MolConverter(in, System.out, "csmol");

Decompression:

MolConverter mc = new MolConverter(in, System.out, "mol");

Using molCompress() in JavaScript

At first you should include the file molcompress.js in the HTML page, in the following way:

<script LANGUAGE="JavaScript1.1" SRC="molcompress.js"></script>

Note that the JavaScript version of molCompress can only compress simple Molfiles. SDfiles, RGfiles and Rxnfiles are not supported.

Because of the difference of operating systems in text file formats, you might need a function that converts a string to DOS/Windows format:

<script LANGUAGE="JavaScript1.1">
<!--
//molCompress() returns a string with \n newline characters.
// The <textarea> HTML element needs \r\n end-of-line characters
// in MS Windows, so we must fix the molCompress() output before
// setting the value of a <textarea>.
function eolfix(s) {
if(navigator.userAgent.lastIndexOf("(Win") >= 0) {
return s.split("\n").join("\r\n");
} else {
return s;
}
}
//-->
</script>

In this example, an HTML textarea is used to display the input and output of the molfile compression or decompression.

<form onSubmit="return false;">
<textarea NAME="mol" ROWS=5 COLS=60>
 
MSketch 11289810322D
 
1 0 0 0 0 0 0 0 0 0999 V2000
-2.5313 0.7188 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
 
M END
</textarea>

The second argument of molCompress() must be true for compression, false for decompession.

<input TYPE="BUTTON" VALUE="Compress" onClick="mol.value=eolfix(molCompress(mol.value, true))">
<input TYPE="BUTTON" VALUE="Inflate" onClick="mol.value=eolfix(molCompress(mol.value, false))">
</form>

You may want to try the compression demo and view its source.