Package chemaxon.formats
Class MolExporter
- java.lang.Object
-
- chemaxon.formats.MolExporter
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
@PublicAPI public class MolExporter extends Object implements Closeable
Molecule exporter class.The output file format can be specified as an argument to the constructor of this class. Many different output file formats are supported like "mol", "sdf", "smiles", "png", "jpeg", etc.
By default, in case of machines with multiple processors processing is concurrent, otherwise single-threaded. By default, the number of worker threads is the number of processors - the number of concurrent threads can be set in
setThreadCount(int)
. Experimental feature (may change in the future!): "chemaxon.struc.Molecule" format for testing Molecule serialization. In this case processing is single-threaded.
-
-
Field Summary
Fields Modifier and Type Field Description static int
C_CLOSE_STREAM
Close the underlying stream.static int
C_FLUSH_STREAM
Flush the output stream and force any buffered output bytes to be written out.static MolFilter
FILTER_DIM0
Molecule filter for cleaning0D
molecules only.static int
MULTIPLE
Use this flag to produce multiple output files.static int
TEXT
Use this flag to write text files with OS dependent line separators.
-
Constructor Summary
Constructors Constructor Description MolExporter(OutputStream out, String fmtWithOpts)
Creates a molecule exporter.MolExporter(OutputStream out, String fmtWithOpts, boolean text, String[] incFields)
Creates a molecule exporter.MolExporter(OutputStream out, String fmtWithOpts, boolean text, String[] incFields, String enc)
Creates a molecule exporter.MolExporter(OutputStream out, String fmtWithOpts, String enc)
Creates a molecule exporter.MolExporter(String fname, String fmtWithOpts)
Creates a molecule exporter.MolExporter(String fname, String fmtWithOpts, int outflags, String[] incFields, String enc)
Creates a molecule exporter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes theMolExportModule
and the underlying stream.void
close(int opts)
Closes theMolExportModule
and, optionally, the underlying stream.static String
convertToString(MDocument document, String fmt, int flags)
Converts the document to text format.static byte[]
exportToBinFormat(MDocument document, String fmt)
Creates a binary representation of the document.static byte[]
exportToBinFormat(Molecule molecule, String fmt)
Creates a binary representation of the molecule.static String
exportToFormat(MDocument document, String fmt)
Creates a string representation of the document.static String
exportToFormat(Molecule molecule, String fmt)
Creates a string representation of the molecule.static Object
exportToObject(MDocument document, String fmt)
Creates a String, byte[] array or Image representation of the document.static Object
exportToObject(Molecule[] mols, String fmt, MolExportModule cv)
Creates a String, byte[] array or Image representation of the given molecules.static Object
exportToObject(Molecule molecule, String fmt)
Creates a String, byte[] array or Image representation of the molecule.protected void
finalize()
void
flush()
Flushes the output stream and forces any buffered output bytes to be written out.String
getFormat()
Gets the output file format.boolean
isCleanable()
Tests whether cleaning is meaningful for the chosen output format.void
setClean(int cleanDim, String cleanOpts)
Sets molecule cleaning in exporter.void
setClean(int cleanDim, String cleanOpts, MolFilter cleanFilter)
Sets molecule cleaning in exporter.void
setErrorProcessor(ErrorProcessor errorProcessor)
Sets the error handler interface for concurrent mode.void
setThreadCount(int threadCount)
Sets the number of threads for concurrent processing.boolean
write(Molecule m)
Writes a molecule into the stream.
-
-
-
Field Detail
-
TEXT
public static final int TEXT
Use this flag to write text files with OS dependent line separators. If not specified, then LF ('\n') is used as in Unix.- Since:
- Marvin 5.2.2, 04/20/2009
- See Also:
MolExporter(String, String, int, String[], String)
, Constant Field Values
-
MULTIPLE
public static final int MULTIPLE
Use this flag to produce multiple output files.- Since:
- Marvin 5.2.2, 04/20/2009
- See Also:
MolExporter(String, String, int, String[], String)
, Constant Field Values
-
C_CLOSE_STREAM
public static final int C_CLOSE_STREAM
Close the underlying stream.- Since:
- Marvin 5.1.1, 08/17/2008
- See Also:
close(int)
, Constant Field Values
-
C_FLUSH_STREAM
public static final int C_FLUSH_STREAM
Flush the output stream and force any buffered output bytes to be written out.- Since:
- Marvin 5.1.1, 08/17/2008
- See Also:
close(int)
, Constant Field Values
-
FILTER_DIM0
public static final MolFilter FILTER_DIM0
Molecule filter for cleaning0D
molecules only.
-
-
Constructor Detail
-
MolExporter
public MolExporter(String fname, String fmtWithOpts, int outflags, String[] incFields, String enc) throws MolExportException, IOException, IllegalArgumentException
Creates a molecule exporter.- Parameters:
fname
- the output filenamefmtWithOpts
- the output file format and the optionsoutflags
- output flags, it can be a combination ofTEXT
andMULTIPLE
incFields
- SDF fields to include in table or nullenc
- charset name or null- Throws:
MolExportException
- export error occurredIOException
- output error occurred when writing the headerIllegalArgumentException
- Invalid format string (fmtWithOpts).IllegalCharsetNameException
- if the encoding is illegalUnsupportedCharsetException
- if the encoding is unsupported- Since:
- Marvin 5.2.2, 04/20/2009
-
MolExporter
public MolExporter(String fname, String fmtWithOpts) throws IOException, IllegalArgumentException
Creates a molecule exporter.- Parameters:
fname
- the output filename with extensionfmtWithOpts
- the output file format and the options- Throws:
MolExportException
- export error occurredIOException
- output error occurred when writing the headerIllegalArgumentException
- Invalid format string (fmtWithOpts).IllegalCharsetNameException
- if the encoding is illegalUnsupportedCharsetException
- if the encoding is unsupported- Since:
- Marvin 5.4.1, 01/10/2010
-
MolExporter
public MolExporter(OutputStream out, String fmtWithOpts, boolean text, String[] incFields, String enc) throws MolExportException, IOException, IllegalArgumentException
Creates a molecule exporter.- Parameters:
out
- the output streamfmtWithOpts
- the output file format and the optionstext
- write OS dependent line.separators instead of '\n'incFields
- SDF fields to include in table or nullenc
- charset name or null- Throws:
MolExportException
- export error occurredIOException
- output error occurred when writing the headerIllegalArgumentException
- Invalid format string (fmtWithOpts).IllegalCharsetNameException
- if the encoding is illegalUnsupportedCharsetException
- if the encoding is unsupported
-
MolExporter
public MolExporter(OutputStream out, String fmtWithOpts) throws IOException, IllegalArgumentException
Creates a molecule exporter.- Parameters:
out
- the output streamfmtWithOpts
- the output file format and the options- Throws:
MolExportException
- export error occurredIOException
- output error occurred when writing the headerIllegalArgumentException
- Invalid format string (fmtWithOpts).IllegalCharsetNameException
- if the encoding is illegalUnsupportedCharsetException
- if the encoding is unsupported
-
MolExporter
public MolExporter(OutputStream out, String fmtWithOpts, boolean text, String[] incFields) throws MolExportException, IOException, IllegalArgumentException
Creates a molecule exporter.- Parameters:
out
- the output streamfmtWithOpts
- the output file format and the optionstext
- write OS dependent line.separators instead of '\n'incFields
- SDF fields to include in table or null- Throws:
MolExportException
- export error occurredIOException
- output error occurred when writing the headerIllegalArgumentException
- Invalid format string (fmtWithOpts).IllegalCharsetNameException
- if the encoding is illegalUnsupportedCharsetException
- if the encoding is unsupported
-
MolExporter
public MolExporter(OutputStream out, String fmtWithOpts, String enc) throws IOException, IllegalArgumentException
Creates a molecule exporter.- Parameters:
out
- the output streamfmtWithOpts
- the output file format and the optionsenc
- charset name or null- Throws:
MolExportException
- export error occurredIOException
- output error occurred when writing the headerIllegalArgumentException
- Invalid format string (fmtWithOpts).IllegalCharsetNameException
- if the encoding is illegalUnsupportedCharsetException
- if the encoding is unsupported
-
-
Method Detail
-
setThreadCount
public void setThreadCount(int threadCount) throws IllegalStateException
Sets the number of threads for concurrent processing. Default: the number of CPUs, single-threaded processing if there is1
CPU.- Parameters:
threadCount
- the number of threads, set0
for the number of CPUs,1
for single-threaded mode- Throws:
IllegalStateException
- if called after call towrite(chemaxon.struc.Molecule)
- Since:
- Marvin 5.3
-
setClean
public void setClean(int cleanDim, String cleanOpts) throws IllegalStateException
Sets molecule cleaning in exporter. Sets cleaning dimension and options. If processing is in concurrent mode, then cleaning is also performed concurrently.- Parameters:
cleanDim
- the cleaning dimensioncleanOpts
- the cleaning options- Throws:
IllegalStateException
- if called after call towrite(chemaxon.struc.Molecule)
- Since:
- Marvin 5.3
- See Also:
setThreadCount(int)
-
setClean
public void setClean(int cleanDim, String cleanOpts, MolFilter cleanFilter) throws IllegalStateException
Sets molecule cleaning in exporter. Sets cleaning dimension and options. If processing is in concurrent mode, then cleaning is also performed concurrently.- Parameters:
cleanDim
- the cleaning dimensioncleanOpts
- the cleaning optionscleanFilter
- the molecule filter to tell which molecule is to be cleaned, setFILTER_DIM0
to clean only0D
moleculesnull
for no filtering (cleaning all)- Throws:
IllegalStateException
- if called after call towrite(chemaxon.struc.Molecule)
- Since:
- Marvin 5.3
- See Also:
setThreadCount(int)
-
setErrorProcessor
public void setErrorProcessor(ErrorProcessor errorProcessor) throws IllegalStateException
Sets the error handler interface for concurrent mode. By default, export is stopped on first error and this error is thrown inclose()
in concurrent mode. To change this, set specificErrorProcessor
implementation which can handle each error individually during the writing process and can decide whether to continue the export or not. If the export is stopped on an error then this error is always thrown inclose()
. In single-threaded mode export errors are thrown inwrite(chemaxon.struc.Molecule)
, separately for each molecule.- Throws:
IllegalStateException
- if called after call towrite(chemaxon.struc.Molecule)
- Since:
- Marvin 5.3
- See Also:
setThreadCount(int)
-
getFormat
public String getFormat()
Gets the output file format.- Returns:
- the format
-
isCleanable
public boolean isCleanable()
Tests whether cleaning is meaningful for the chosen output format.- Returns:
- true if cleaning is meaningful, false otherwise
- Since:
- Marvin 4.1, 02/12/2006
-
write
public boolean write(Molecule m) throws MolExportException, IOException
Writes a molecule into the stream.- Parameters:
m
- the molecule- Returns:
true
on success,false
if Molecule is not written due to a previous error in concurrent mode - in this case, callclose()
which will throw the error and close the exporter- Throws:
MolExportException
- export errorIOException
-
close
public void close() throws MolExportException, IOException
Closes theMolExportModule
and the underlying stream. IMPORTANT: call this orclose(int)
at the end, after the last call towrite(chemaxon.struc.Molecule)
, because this method flushes the output stream and closes concurrent processing properly.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
MolExportException
- on export error.IOException
- in case of any other I/O error.
-
close
public void close(int opts) throws MolExportException, IOException
Closes theMolExportModule
and, optionally, the underlying stream. IMPORTANT: call this orclose()
at the end, after the last call towrite(chemaxon.struc.Molecule)
, because this method flushes the output stream and closes concurrent processing properly.- Parameters:
opts
- closing options,C_CLOSE_STREAM
orC_FLUSH_STREAM
.- Throws:
MolExportException
- export error.IOException
- in case of any other I/O error.- Since:
- Marvin 5.1.1, 08/17/2008
-
flush
public void flush() throws MolExportException, IOException
Flushes the output stream and forces any buffered output bytes to be written out.- Throws:
MolExportException
- on export error.IOException
- if an I/O error occurs.- Since:
- Marvin 5.1.1, 08/15/2008
-
finalize
protected void finalize() throws MolExportException, IOException
- Overrides:
finalize
in classObject
- Throws:
MolExportException
IOException
-
exportToObject
public static Object exportToObject(MDocument document, String fmt) throws IOException
Creates a String, byte[] array or Image representation of the document. When creating an image, display options can also be specified in the format descriptor string. Format and options are separated by a colon, multiple options can be separated by commas. See the possible formats and their options on the File Formats in Marvin page. Examples:import java.awt.Image; ... String s1 = (String)MolExporter.exportToObject(document, "mol"); String s2 = (String)MolExporter.exportToObject(document, "smiles:a-H"); // aromatize, remove H Image img = (Image)MolExporter.exportToObject(document, "image:w300,h300,#ffff00"); byte[] d3 = (byte[])MolExporter.exportToObject(document, "png:w300,h300,b32,#00ffff00"); byte[] d4 = (byte[])MolExporter.exportToObject(document, "jpeg:w300,h300,Q95,#ffff00,spacefill");
- Parameters:
document
- the document to exportfmt
- the format descriptor string- Returns:
- a String, a byte[] array or an Image
- Throws:
IOException
- Export error.IllegalArgumentException
- Invalid format string.SecurityException
- Export module cannot be loaded because of security reasons (firewall).- Since:
- Marvin 5.8, 08/25/2011
- See Also:
exportToFormat(chemaxon.struc.MDocument, java.lang.String)
,exportToBinFormat(chemaxon.struc.MDocument, java.lang.String)
-
convertToString
public static String convertToString(MDocument document, String fmt, int flags) throws IOException
Converts the document to text format.- Parameters:
document
- the document to convertfmt
- the molecule file format ornull
flags
- conversion options- Returns:
- the string
- Throws:
IOException
- Export error- Since:
- Marvin 5.8, 08/25/2011
-
exportToFormat
public static final String exportToFormat(MDocument document, String fmt) throws IOException
Creates a string representation of the document. The format descriptor string can also contain export options. Format and options are separated by a colon, multiple options can be separated by commas. The line separator is '\n'. See the text formats and their options on the File Formats in Marvin page. Examples:s1 = MolExporter.exportToFormat(document, "mol"); s2 = MolExporter.exportToFormat(document, "smiles:a-H"); // aromatize and remove Hydrogens
- Parameters:
document
- the document to exportfmt
- the format descriptor string- Returns:
- string representation of the document in the specified format
- Throws:
IOException
- Export errorIllegalArgumentException
- Invalid format string.SecurityException
- Export module cannot be loaded because of security reasons (firewall).- Since:
- Marvin 5.8, 08/25/2011
- See Also:
exportToBinFormat(chemaxon.struc.MDocument, java.lang.String)
,exportToObject(chemaxon.struc.MDocument, java.lang.String)
-
exportToBinFormat
public static final byte[] exportToBinFormat(MDocument document, String fmt) throws IOException
Creates a binary representation of the document. Works with all the text formats that are supported by theexportToFormat
method, and also with the binary image formats (jpeg, msbmp, png). When creating an image, display options can also be specified in the format descriptor string. Format and options are separated by a colon, multiple options can be separated by commas. See the possible formats and their options on the File Formats in Marvin page. Examples:byte[] d1 = MolExporter.exportToBinFormat(document, "mrv"); byte[] d2 = MolExporter.exportToBinFormat(document, "smiles:a-H"); // aromatize and remove H byte[] d3 = MolExporter.exportToBinFormat(document, "png:w300,h300,b32,#00ffff00"); byte[] d4 = MolExporter.exportToBinFormat(document, "jpeg:w300,h300,Q95,#ffff00,spacefill");
- Parameters:
document
- the document to exportfmt
- the format descriptor string- Returns:
- a binary representation of the document in the specified format
- Throws:
IOException
- Export error.IllegalArgumentException
- Invalid format string.SecurityException
- Export module cannot be loaded because of security reasons (firewall).- Since:
- Marvin 5.8, 08/25/2011
- See Also:
exportToFormat(chemaxon.struc.MDocument, java.lang.String)
,exportToObject(chemaxon.struc.MDocument, java.lang.String)
-
exportToFormat
public static final String exportToFormat(Molecule molecule, String fmt) throws IOException
Creates a string representation of the molecule. The format descriptor string can also contain export options. Format and options are separated by a colon, multiple options can be separated by commas. The line separator is '\n'. See the text formats and their options on the File Formats in Marvin page. Examples:s1 = MolExporter.exportToFormat(molecule, "mol"); s2 = MolExporter.exportToFormat(molecule, "smiles:a-H"); // aromatize and remove Hydrogens
- Parameters:
molecule
- the molecule to exportfmt
- the format descriptor string- Returns:
- string representation of the molecule in the specified format
- Throws:
IOException
- Export error.IllegalArgumentException
- Invalid format string.SecurityException
- Export module cannot be loaded because of security reasons (firewall).- Since:
- Marvin 5.8, 08/25/2011
-
exportToBinFormat
public static final byte[] exportToBinFormat(Molecule molecule, String fmt) throws IOException
Creates a binary representation of the molecule. Works with all the text formats that are supported by theexportToFormat
method, and also with the binary image formats (jpeg, msbmp, png). When creating an image, display options can also be specified in the format descriptor string. Format and options are separated by a colon, multiple options can be separated by commas. See the possible formats and their options on the File Formats in Marvin page. Examples:byte[] d1 = MolExporter.exportToBinFormat(molecule, "mol"); byte[] d2 = MolExporter.exportToBinFormat(molecule, "smiles:a-H"); // aromatize and remove H byte[] d3 = MolExporter.exportToBinFormat(molecule, "png:w300,h300,b32,#00ffff00"); byte[] d4 = MolExporter.exportToBinFormat(molecule, "jpeg:w300,h300,Q95,#ffff00,spacefill");
- Parameters:
molecule
- the molecule to exportfmt
- the format descriptor string- Returns:
- a binary representation of the molecule in the specified format
- Throws:
IOException
- Export error.IllegalArgumentException
- Invalid format string.SecurityException
- Export module cannot be loaded because of security reasons (firewall).- Since:
- Marvin 5.8, 08/25/2011
-
exportToObject
public static Object exportToObject(Molecule molecule, String fmt) throws IOException
Creates a String, byte[] array or Image representation of the molecule. When creating an image, display options can also be specified in the format descriptor string. Format and options are separated by a colon, multiple options can be separated by commas. See the possible formats and their options on the File Formats in Marvin page. Examples:import java.awt.Image; ... String s1 = (String)MolExporter.exportToObject(molecule, "mol"); String s2 = (String)MolExporter.exportToObject(molecule, "smiles:a-H"); // aromatize, remove H Image img = (Image)MolExporter.exportToObject(molecule, "image:w300,h300,#ffff00"); byte[] d3 = (byte[])MolExporter.exportToObject(molecule, "png:w300,h300,b32,#00ffff00"); byte[] d4 = (byte[])MolExporter.exportToObject(molecule, "jpeg:w300,h300,Q95,#ffff00,spacefill");
- Parameters:
molecule
- the molecule to exportfmt
- the format descriptor string- Returns:
- a String, a byte[] array or an Image
- Throws:
IOException
- Export error.IllegalArgumentException
- Invalid format string.SecurityException
- Export module cannot be loaded because of security reasons (firewall).- Since:
- Marvin 5.8, 08/25/2011
-
exportToObject
public static Object exportToObject(Molecule[] mols, String fmt, MolExportModule cv) throws IOException
Creates a String, byte[] array or Image representation of the given molecules.- Parameters:
mols
- the molecules to exportfmt
- the format descriptor stringcv
- the export module- Returns:
- a String, a byte[] array or an Image
- Throws:
IOException
- export error- See Also:
exportToObject(Molecule, String)
-
-