Page tree

Molecule export is the operation when Molecule objects are converted to targets in various formats.

Target of data export

When exporting structures via ChemAxon tools, we will refer to different target of the data to be written:

  • Structure file where location is given with absolute or relative path
  • Stream output
  • String representation of the molecule in various formats
  • Binary representation of the molecule in various formats
  • Image representation of the molecule in various formats

Basic export using the API

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

Exporting to String

Exporting one molecule to String:

	Molecule molecule = buildSimpleMolecule();
	System.out.println("Water molecule in mrv: " );
	System.out.println( MolExporter.exportToFormat(molecule, "mrv:P"));
	System.out.println("Water molecule in smiles: ");
	System.out.println(MolExporter.exportToFormat(molecule, "smiles"));
	RgMolecule rgMol = buildRgMolecule();
	System.out.println("Molecule with R-groups in mrv: ");
	System.out.println(MolExporter.exportToFormat(rgMol, "mrv:P"));
	System.out.println("Molecule with R-groups in smiles: ");
	System.out.println(MolExporter.exportToFormat(rgMol, "smiles"));

Please note that some features, for example R-groups cannot be exported to all formats. Here the SMILES export for R-groups throws an exception because the SMILES format doesn't support R-groups!

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

Exporting multiple molecules

Writing more molecules into one target is also possible:

MolExporter exporter = new MolExporter(outputStream, "cml");
Molecule fragments[] = molecule.findFrags(Molecule.class, Molecule.FRAG_BASIC);
for (Molecule fragment : fragments ){
    exporter.write(fragment);
}
exporter.close();	


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

Exporting options

Additional options of MolExporter allow to refine behavior further. Options can be general or dependent on file formats. Options can be set in the constructor MolExporter(InputStream is, String opts) or during import with static method MolExporter.exportToFormat. The most important option is the file format option which specifies the expected file format. General or file format dependent options are separated by a colon from the file format option and by a comma from each other. In the code example below the molecule is exported without the explicit hydrogens with the general -H option. This is a general export option and can be applied in case of any file format. File format option is mol for MDL MOL file format and file format specific option is V3 to export the file in extended MOL format.


try {
	MolExporter exporter = new MolExporter("outputMolecule.mol", "mol:V3,-H");
	exporter.write(molecule);
	exporter.close();
} catch (MolFormatException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}


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