Package chemaxon.formats
Class MolConverter
java.lang.Object
chemaxon.formats.MolConverter
Converts between molecule file formats.
See supported formats and their import and/or export options on the
File Formats in Marvin page.
The processing is single-threaded by default. Concurrent mode can be enabled using setThreadCount(int)
.
Examples:
- Simple SDfile to SMILES conversion:
MolConverter.Builder
mcbld = new MolConverter.Builder(); mcbld.addInput
("input.sdf", ""); mcbld.setOutput
("out.smiles", "smiles"); mcbld.setOutputFlags
(MolExporter.TEXT
); MolConverter mc = mcbld.build(); while(mc.convert()
); mc.close()
; - To split a multi-molecule input file into multiple Molfiles named
out1.mol, out2.mol etc., use the following settings:
mcbld.
setOutput
("out.mol", "mol"); mcbld.setOutputFlags
(MolExporter.TEXT
|MolExporter.MULTIPLE
);
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
MolConverter builder.static class
Deprecated, for removal: This API element is subject to removal in a future version. -
Constructor Summary
ConstructorDescriptionMolConverter
(MolConverter.Builder mcbld) Creates a MolConverter object.Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, useMolConverter.Builder.build()
instead.MolConverter
(InputStream in, OutputStream out, String fmtopts, boolean useSysEOL) Creates a MolConverter object.MolConverter
(InputStream in, String ofname, String fmtopts, boolean useSysEOL) Use this constructor if you do not need output buffering but want to determine output file positions.MolConverter
(List<Object> inFiles, OutputStream out, String fmtopts, boolean useSysEOL) Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
MolConverter
(List<Object> inFiles, String ofname, String fmtopts, boolean useSysEOL) Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
MolConverter
(List<Object> inFiles, List<String> inOptions, OutputStream out, String fmtopts, boolean useSysEOL) Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
MolConverter
(List<Object> inFiles, List<String> inOptions, OutputStream out, String fmtopts, boolean useSysEOL, String[] incFields) Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
MolConverter
(List<Object> inFiles, List<String> inOptions, OutputStream out, String fmtopts, boolean useSysEOL, String[] incFields, String enc) Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
MolConverter
(List<Object> inFiles, List<String> inOptions, String ofname, String fmtopts, boolean useSysEOL) Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the exporter and the output stream.void
close
(int opts) Closes the exporter.boolean
convert()
Convert the next molecule.static MolConverter
createMolConverter
(InputStream instream, OutputStream outstream, String[] args, int[] verbosityLevel) Creates a MolConverter instance with the attributes specified in the parameters.static void
Main method.static void
mainWithoutExit
(String[] args) Same as main(), but never calls System.exit().long
otell()
Get the current file position.void
setIgnoreError
(boolean ignoreError) Sets the 'ignoreError' option.void
setThreadCount
(int threadCount) Sets the number of threads for concurrent processing.
-
Constructor Details
-
MolConverter
Creates a MolConverter object.- Parameters:
mcbld
- the builder- Throws:
MolFormatException
- If the molecule file is in a format that cannot be readMolExportException
- If the molecule cannot be exported in the specified formatIOException
- If I/O error occurred when determining the file format.IllegalArgumentException
- Invalid format string (fmt).- Since:
- Marvin 5.2.3, 05/28/2009
-
MolConverter
public MolConverter(InputStream in, OutputStream out, String fmtopts, boolean useSysEOL) throws IOException, IllegalArgumentException Creates a MolConverter object.- Parameters:
in
- the input molecule stream to convertout
- the output streamfmtopts
- output file format with or without optionsuseSysEOL
- write OS dependent line.separators instead of '\n'- Throws:
MolFormatException
- If the molecule file is in a format that cannot be readMolExportException
- If the molecule cannot be exported in the specified formatIOException
- If I/O error occurred when determining the file format.IllegalArgumentException
- Invalid format string (fmt).
-
MolConverter
public MolConverter(InputStream in, String ofname, String fmtopts, boolean useSysEOL) throws IOException, IllegalArgumentException Use this constructor if you do not need output buffering but want to determine output file positions.- Parameters:
in
- the input molecule stream to convertofname
- the output file namefmtopts
- output file format with or without optionsuseSysEOL
- write OS dependent line.separators instead of '\n'- Throws:
MolFormatException
- If the molecule file is in a format that cannot be readMolExportException
- If the molecule cannot be exported in the specified formatIOException
- If I/O error occurred when determining the file format.IllegalArgumentException
- Invalid format string (fmt).- See Also:
-
MolConverter
@Deprecated(forRemoval=true) @SubjectToRemoval(date=JUL_01_2025) public MolConverter(List<Object> inFiles, OutputStream out, String fmtopts, boolean useSysEOL) throws MolFormatException, MolExportException, IOException Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
- Parameters:
inFiles
- not used in this method since it is unsupported now.out
- not used in this method since it is unsupported now.fmtopts
- not used in this method since it is unsupported now.useSysEOL
- not used in this method since it is unsupported now.- Throws:
MolFormatException
- not thrown from this method since it is unsupported now.MolExportException
- not thrown from this method since it is unsupported now.IOException
- not thrown from this method since it is unsupported now.
-
MolConverter
@Deprecated(forRemoval=true) @SubjectToRemoval(date=JUL_01_2025) public MolConverter(List<Object> inFiles, String ofname, String fmtopts, boolean useSysEOL) throws MolFormatException, MolExportException, IOException Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
- Parameters:
inFiles
- not used in this method since it is unsupported now.ofname
- not used in this method since it is unsupported now.fmtopts
- not used in this method since it is unsupported now.useSysEOL
- not used in this method since it is unsupported now.- Throws:
MolFormatException
- not thrown from this method since it is unsupported now.MolExportException
- not thrown from this method since it is unsupported now.IOException
- not thrown from this method since it is unsupported now.
-
MolConverter
@Deprecated(forRemoval=true) @SubjectToRemoval(date=JUL_01_2025) public MolConverter(List<Object> inFiles, List<String> inOptions, String ofname, String fmtopts, boolean useSysEOL) throws MolFormatException, MolExportException, IOException, IllegalArgumentException Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
- Parameters:
inFiles
- not used in this method since it is unsupported now.inOptions
- not used in this method since it is unsupported now.ofname
- not used in this method since it is unsupported now.fmtopts
- not used in this method since it is unsupported now.useSysEOL
- not used in this method since it is unsupported now.- Throws:
MolFormatException
- not thrown from this method since it is unsupported now.MolExportException
- not thrown from this method since it is unsupported now.IOException
- not thrown from this method since it is unsupported now.IllegalArgumentException
-
MolConverter
@Deprecated(forRemoval=true) @SubjectToRemoval(date=JUL_01_2025) public MolConverter(MolConverter.Options opts) throws MolFormatException, MolExportException, IOException, IllegalArgumentException Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, useMolConverter.Builder.build()
instead.- Parameters:
opts
- not used in this method since it is unsupported now.- Throws:
MolFormatException
- not thrown from this method since it is unsupported now.MolExportException
- not thrown from this method since it is unsupported now.IOException
- not thrown from this method since it is unsupported now.IllegalArgumentException
-
MolConverter
@Deprecated(forRemoval=true) @SubjectToRemoval(date=JUL_01_2025) public MolConverter(List<Object> inFiles, List<String> inOptions, OutputStream out, String fmtopts, boolean useSysEOL) throws MolFormatException, MolExportException, IOException Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
- Parameters:
inFiles
- not used in this method since it is unsupported now.inOptions
- not used in this method since it is unsupported now.out
- not used in this method since it is unsupported now.fmtopts
- not used in this method since it is unsupported now.useSysEOL
- not used in this method since it is unsupported now.- Throws:
MolFormatException
- not thrown from this method since it is unsupported now.MolExportException
- not thrown from this method since it is unsupported now.IOException
- not thrown from this method since it is unsupported now.
-
MolConverter
@Deprecated(forRemoval=true) @SubjectToRemoval(date=JUL_01_2025) public MolConverter(List<Object> inFiles, List<String> inOptions, OutputStream out, String fmtopts, boolean useSysEOL, String[] incFields) throws MolFormatException, MolExportException, IOException Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
- Parameters:
inFiles
- not used in this method since it is unsupported now.inOptions
- not used in this method since it is unsupported now.out
- not used in this method since it is unsupported now.fmtopts
- not used in this method since it is unsupported now.useSysEOL
- not used in this method since it is unsupported now.incFields
- not used in this method since it is unsupported now.- Throws:
MolFormatException
- not thrown from this method since it is unsupported now.MolExportException
- not thrown from this method since it is unsupported now.IOException
- not thrown from this method since it is unsupported now.
-
MolConverter
@Deprecated(forRemoval=true) @SubjectToRemoval(date=JUL_01_2025) public MolConverter(List<Object> inFiles, List<String> inOptions, OutputStream out, String fmtopts, boolean useSysEOL, String[] incFields, String enc) throws MolFormatException, MolExportException, IOException Deprecated, for removal: This API element is subject to removal in a future version.as of Marvin 5.2.3, replaced byMolConverter(Builder)
- Parameters:
inFiles
- not used in this method since it is unsupported now.inOptions
- not used in this method since it is unsupported now.out
- not used in this method since it is unsupported now.fmtopts
- not used in this method since it is unsupported now.useSysEOL
- not used in this method since it is unsupported now.incFields
- not used in this method since it is unsupported now.enc
- not used in this method since it is unsupported now.- Throws:
MolFormatException
- not thrown from this method since it is unsupported now.MolExportException
- not thrown from this method since it is unsupported now.IOException
- not thrown from this method since it is unsupported now.
-
-
Method Details
-
setIgnoreError
public void setIgnoreError(boolean ignoreError) Sets the 'ignoreError' option. Iftrue
then processing is continued with the next molecule on error, otherwise processing is interrupted on error. Default:false
.- Parameters:
ignoreError
-true
if processing should be continued on error- Since:
- Marvin 5.3
-
setThreadCount
Sets the number of threads for concurrent processing. The default value is 1 (single-threaded mode).In concurrent mode, multiple threads are started in the background, which adds a considerable overhead to the processing, so only use this mode if the number of molecules is huge. In single-threaded mode, no background thread is used, every action is done on the caller thread.
- Parameters:
threadCount
- the number of threads, set0
for the number of CPUs,1
for single-threaded mode- Throws:
IllegalStateException
- if called after call toconvert()
.- Since:
- Marvin 5.3
-
convert
Convert the next molecule.- Returns:
- true if success, false if no more molecules can be read
- Throws:
IOException
- If an I/O error has occurred.IllegalCharsetNameException
- if the encoding is illegalUnsupportedCharsetException
- if the encoding is unsupported
-
otell
Get the current file position.- Returns:
- the position
- Throws:
IOException
- if the position cannot be determined
-
close
Closes the exporter and the output stream.- Throws:
MolExportException
- export error.IOException
- in case of any other I/O error.
-
close
Closes the exporter. If the output stream is specified by the user, then this method only closes it if theMolExporter.C_CLOSE_STREAM
option is used. If the input stream was opened internally, then theopts
argument has no effect, the output stream is always closed.- Parameters:
opts
- closing options for the exporter, applied only if the output stream is specified by the user- Throws:
MolExportException
- export error.IOException
- in case of any other I/O error.- Since:
- Marvin 5.2.2, 04/21/2009
- See Also:
-
createMolConverter
public static MolConverter createMolConverter(InputStream instream, OutputStream outstream, String[] args, int[] verbosityLevel) throws IOException Creates a MolConverter instance with the attributes specified in the parameters. Basically allows two usage scenario:- command line usage (from within the main method),
- programmatica usage (like from within the JChem Cartridge Servlet).
- Parameters:
instream
- the input stream to take the molecule definition from. It must benull
when called for command line usage.outstream
- the output stream to write the result onto. It must benull
when called for command line usage.args
- space separated options of attributes/options for the desired MolConverter instance.verbosityLevel
- may be used by the caller to retrieve (as the first element of the array) the verbosity level specified inargs
. Must be either at least one element long or (if the caller is not interested in this value)null
.- Throws:
IllegalCharsetNameException
- if illegal input or output encoding name specifiedUnsupportedCharsetException
- if the specified input or output encoding is unsupportedIOException
-
main
Main method.- Parameters:
args
- command line arguments- Throws:
IOException
-
mainWithoutExit
Same as main(), but never calls System.exit(). Useful to call from other java programs. Instead of exiting the JVM, a RuntimeException may be thrown with text "molconvert exit: <exit status>"- Parameters:
args
- arguments passed to main().- Throws:
IOException
-
MolConverter.Builder
instead.