I/O

Table of contents


Sources of data

When importing structures to ChemAxon tools, we will refer to different sources the data cames from:

Supported formats

The detailed documentation about the supported file formats can be found in the File Formats in Marvin document.

Listing all available formats programmatically

Programmatic listing of the available file formats is available via the MFileFormat and MFileFormatUtil API.

The code below lists the available molecule formats:

  MFileFormat[] formats = MFileFormatUtil.findFormats(
      null,
      MFileFormat.F_EXPORT,
      MFileFormat.F_EXPORT | MFileFormat.F_ARCHIVE | MFileFormat.F_GRAPHICS);     

In this example archive formats (like GZip or BASE64) are excluded by the MFileFormat.F_ARCHIVE flag, graphics formats are excluded by the MFileFormat.F_GRAPHICS flag given by the 3rd parameter.

Import

Molecule import is the operation when sources of data, i.e. structures defined in various formats are converted to Molecule objects so that ChemAxon applications can operate with them.

Basic import using the API

The most frequently used API for molecule import is defined in chemaxon.formats.MolImporter. MolImporter has lots of utility functions.

A newer API of ChemAxon that performs concurrent import and it can also be used by applets is chemaxon.marvin.io.MRecordImporter.

Importing from String

Importing one molecule where the molecule source is available as String.
    String molSource = "CCC(N)c1cc(Cl)cc(C(N)CC)c1Br";
    try {
        Molecule mol = MolImporter.importMol(molSource);
        // do something with the molecule
    } catch (MolFormatException e) {
        e.printStackTrace();
    }
For a complete source code, please see ImportMoleculeSource.java.

Importing from InputStream

Importing one molecule where the molecule source is available via an InputStream.
    try {
        MolImporter mi = new MolImporter(stream);
        Molecule mol = mi.read();
        // do something with the molecule
        mi.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
For a complete source code, please see ImportFromStream.java.

Importing a multi-molecule file

Importing molecules from a multi-molecule file given with URL:

    try {
        URL url = new URL(path);
        MolImporter importer = new MolImporter(url.openStream());
        Molecule mol;
        while ((mol = importer.read()) != null) {
            // do something with the molecule
        }
        importer.close();        
    } catch (IOException e) {
        e.printStackTrace();
    }
For a complete source code, please see ImportMultiMoleculeFile.java, importMoleculeWithMolImporter method.

Importing with MRecordImporter

With this method importing with machines having multiple processors will be concurrent, otherwise single-threaded.

    try {
        URL url = new URL(path);
        MolInputStream mis = new MolInputStream(url.openStream(), null, null, null);
        MRecordImporter importer = new MRecordImporter(mis, null);
        MDocument mDocument;
        while ((mDocument = importer.readDoc()) != null) {
            Molecule mol = (Molecule) mDocument.getMainMoleculeGraph();
            // do something with the molecule
        }
        importer.close();
    } catch (MRecordParseException ex) {
        ex.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
For a complete source code, please see ImportMultiMoleculeFile.java, importMoleculeWithMRecordImporter method.

Importing molecular properties

In case we know the name of the properties, we can use the following code to get the values in text format:

    String value = molecule.getProperty(LOGP_FIELD); //LOGP_FIELD is the name of the property

Otherwise we need to go through the property names. An example is below:

    for (int i=0; i<molecule.getPropertyCount(); i++) {
        String key = molecule.getPropertyKey(i); // name of the property
        String value = molecule.getProperty(key); // the property value
    }

For a complete source code, please see ImportProperties.java.

Import into Marvin Applets

There are two possible ways of importing molecules to MarvinSketch and MarvinView applets.

The simpler way is to use applet parameters. This is the recommended way in case the applet should start up with a molecule already imported.

The other way is to use JavaScript calls. This allows importing molecules a while after the applet is loaded, typically invoked by a user interaction. Both MarvinSketch and MarvinView applets have convenience methods to import molecules with JavaScript calls.

JMSketch.setMol(String) takes molecule source, URL or filename as an argument.

The corresponding JMView.setMol(String) method of the MarvinView Applet is very similar. The consecutive calls of this method will place molecules in separate cells.

Import into MarvinView Applet with the mol parameter

With the mol parameter, molecules of a multi-molecule file will be loaded to separate cells.

    mview_begin("../../..",430,440);
    // Setting number of rows, columns and the layout
    mview_param("mol","../../../mols-2d/mols.sdf");
    mview_end();
For a complete example please visit: MarvinView Example - Loading molecules from an SD file to a molecule table

Import into MarvinView Applet with cell parameter

The cell parameter defines the contents of each cell individually.

    mview_begin("../../..", 300, 300);
    mview_param("rows", "2");
    mview_param("cols", "2");
    mview_param("cell0", "|../../../mols-2d/caffeine.csmol");
    mview_param("cell1", "|O[C@H](\C=C\[C@@H](O)C1=CC=CC=C1)C2=CC=CC=C2");
    mview_end();
For a complete example please visit: MarvinView Example - Creating an advanced molecule table

Import into MarvinSketch Applet with JavaScript

    function importMol(opts) {
            if(document.MSketch != null) {
                    var s = document.MolForm.MolTxt.value; //Value is read from a HTML text area
                    document.MSketch.setMol(s, opts);
            } else {
                    alert("Cannot import molecule:\n"+
                          "no JavaScript to Java communication in your browser.\n");
            }
    }
For a complete example, please visit: MarvinSketch Example - Import/export of built-in and user defined molecule formats in JavaScript

Import into Marvin Beans

Import into MarvinSketch

Import into MarvinView

Export