chemaxon.marvin.io
Class MolExportModule

java.lang.Object
  extended by chemaxon.marvin.io.MolExportModule
Direct Known Subclasses:
MolExport, MolExportModule

public abstract class MolExportModule
extends java.lang.Object

Abstract base class of molecule export modules. An export module for format "XXX" must have the name chemaxon.marvin.modules.XxxExport. Converted structures are returned by molecule.toFormat("xxx") or toBinFormat("xxx") method calls, as Strings or byte[] arrays.

Example
The export module that can produce molecule files like the one in the MolImportModule example, is the following:


 package myio;

 import chemaxon.struc.*;

 public class MyFormatExport extends chemaxon.marvin.io.MolExportModule
 {
     public Object convert(Molecule mol) {
         StringBuffer s = stringBuffer;
         s.setLength(0);
         s.append(mol.getName());
         s.append('\n');
         // atoms
         s.append(String.valueOf(mol.getAtomCount()));
         s.append('\n');
         for(int i = 0; i < mol.getAtomCount(); ++i) {
             MolAtom a = mol.getAtom(i);
             s.append(a.getSymbol());
             s.append('\t');
             s.append(String.valueOf(a.getX()));
             s.append('\t');
             s.append(String.valueOf(a.getY()));
             s.append('\n');
         }
         // bonds
         s.append(String.valueOf(mol.getBondCount()));
         s.append('\n');
         for(int i = 0; i < mol.getBondCount(); ++i) {
             MolBond b = mol.getBond(i);
             s.append(String.valueOf(mol.indexOf(b.getAtom1()) + 1));
             s.append('\t');
             s.append(String.valueOf(mol.indexOf(b.getAtom2()) + 1));
             s.append('\t');
             s.append(String.valueOf(b.getType()));
             s.append('\n');
         }
         return s.toString();
     }
 }
 

After compiling and placing the class into Marvin's CLASSPATH, you can create "myformat" files with MolConverter,

 molconvert myformat pyrrole.mol -o pyrrole.myf
 
and the applets:
 s = msketch.getMol("myformat");
 

Since:
Marvin 2.7.9
Version:
5.2.1, 05/19/2009
Author:
Peter Csizmadia, Szabolcs Csepregi, Erika Biro
See Also:
Molecule.toFormat(java.lang.String), Molecule.toBinFormat(java.lang.String)

Field Summary
protected  boolean addNumbering
          Add numbers to atoms corresponding to the (IUPAC) naming of the molecule.
protected  int aromatize
          Aromatize molecule according to basic aromatization if MoleculeGraph.AROM_BASIC + 1, according to general aromatization if MoleculeGraph.AROM_GENERAL + 1, dearomatize if -1, do nothing if 0.
protected  int hydrogenize
          Add Hydrogen atoms if 1, remove if -1, do nothing if 0.
protected  java.lang.StringBuffer stringBuffer
          This buffer can contain the molecule file contents, in case of a text format.
 
Constructor Summary
MolExportModule()
           
 
Method Summary
protected  void addData(Molecule mol, MolAtom ma, java.lang.String fieldName, java.lang.String value)
          Adds a data sgroup to the given atom in the given molecule with fieldName and value.
protected  void appendChars(int n, char c)
          Append a character n times to the string buffer.
protected  void appendLeft(java.lang.String t, int n)
          Append a string to the buffer in %-ns format.
protected  void appendRight(int t, int n, char c)
          Append an integer to the buffer in the right hand side of an n-characters wide field.
protected  void appendRight(java.lang.String t, int n, char c)
          Append a string to the buffer in the right hand side of an n-characters wide field.
 java.lang.Object close()
          Close the stream.
abstract  java.lang.Object convert(Molecule mol)
          Convert a molecule to a string or byte array.
 java.lang.String getEncoding()
          Gets the output encoding.
 java.lang.String getFormat()
          Returns the output format.
protected static java.lang.String getOptionDescriptors(java.util.ResourceBundle rc, java.lang.String fmtname, java.lang.String optnames, java.util.List l)
          Gets an array of option descriptors.
 OptionDescriptor[] getOptionDescriptors(java.lang.String fmtname)
          Gets an array of option descriptors for the specified format.
protected  void getOptionDescriptors(java.lang.String fmtname, java.lang.String optnames, java.util.List l)
          Gets an array of option descriptors.
protected  java.lang.String getOptions()
          Returns the output options.
protected  int getOptionSign()
          Gets the sign of the options.
 boolean isCleanable()
          Tests whether 2D or 3D cleaning is meaningful for this output format.
 boolean isDocumentExport()
          Tests if this export module is document export instead of a simple molecule export.
static boolean isImplicitHcountImportant(MolAtom a)
          Checks whether the number of implicit hydrogens is important information for an atom or not.
 boolean isImplicitHcountImportant(MoleculeGraph mol)
          Checks whether the number of implicit hydrogens is important information for the atoms of mol or not.
protected static int nextOpt(java.lang.String opts, int i, java.lang.String s)
          Tests whether the option string contains the specified substring at the specified position.
 java.lang.Object open(java.lang.String fmtopts)
          Opens the exporter stream.
 java.lang.Object open(java.lang.String fmtopts, MPropertyContainer props)
          Opens the exporter stream.
protected  int parseCharIfOptionSign(java.lang.String opts, int i)
          Parse the current character if it is an option sign.
protected  int parseOption(java.lang.String opts, int i)
          Parses the following option in the option string.
protected  Molecule preconvert(Molecule mol)
          Optionally performs aromatization or addition of explicit Hydrogens atoms.
protected  Molecule preconvert(Molecule mol, boolean xg)
          Optionally performs aromatization or addition of explicit Hydrogens atoms.
protected  Molecule preconvert(Molecule mol, boolean xg, int xopts, boolean ih)
          Optionally performs aromatization or addition of explicit Hydrogens atoms.
protected  MoleculeGraph preconvert(MoleculeGraph molg, boolean xg, int xopts, boolean ih)
          Optionally performs aromatization or addition of explicit Hydrogens atoms.
 java.lang.String setEncoding(java.lang.String opts, java.lang.String enc)
          Sets the output encoding using the export options.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

stringBuffer

protected java.lang.StringBuffer stringBuffer
This buffer can contain the molecule file contents, in case of a text format.


aromatize

protected int aromatize
Aromatize molecule according to basic aromatization if MoleculeGraph.AROM_BASIC + 1, according to general aromatization if MoleculeGraph.AROM_GENERAL + 1, dearomatize if -1, do nothing if 0.

See Also:
MoleculeGraph.AROM_BASIC, MoleculeGraph.AROM_GENERAL

hydrogenize

protected int hydrogenize
Add Hydrogen atoms if 1, remove if -1, do nothing if 0.


addNumbering

protected boolean addNumbering
Add numbers to atoms corresponding to the (IUPAC) naming of the molecule.

Constructor Detail

MolExportModule

public MolExportModule()
Method Detail

getOptionDescriptors

public final OptionDescriptor[] getOptionDescriptors(java.lang.String fmtname)
Gets an array of option descriptors for the specified format.

Parameters:
fmtname - the format name or null
Since:
Marvin 5.0, 06/06/2007

getOptionDescriptors

protected void getOptionDescriptors(java.lang.String fmtname,
                                    java.lang.String optnames,
                                    java.util.List l)
Gets an array of option descriptors.

Parameters:
fmtname - the format name or null
optnames - option names or null for all
l - the output list
Since:
Marvin 5.0, 06/06/2007

open

public java.lang.Object open(java.lang.String fmtopts)
                      throws MolExportException
Opens the exporter stream. Overriding methods should call super.open(fmtopts) at the beginning. In case of some many-molecule formats such as RDfile, the files begin with a header. This header must be returned by open(), either as a String object or a byte[] array.

Parameters:
fmtopts - output file format and options
Returns:
null (file header or null in overriding methods)
Throws:
MolExportException - Invalid format string.
See Also:
getFormat(), getOptions()

open

public java.lang.Object open(java.lang.String fmtopts,
                             MPropertyContainer props)
                      throws MolExportException
Opens the exporter stream. Overriding methods should call super.open(fmtopts, props) at the beginning. In case of some many-molecule formats such as RDfile, the files begin with a header. This header must be returned by open(), either as a String object or a byte[] array.

Parameters:
fmtopts - output file format and options
props - global GUI properties (currently allowed for MRV only in CmlExport)
Returns:
null (file header or null in overriding methods)
Throws:
MolExportException - Invalid format string.
Since:
Marvin 4.1, 03/22/2006

convert

public abstract java.lang.Object convert(Molecule mol)
                                  throws MolExportException
Convert a molecule to a string or byte array. When converted to a String, it should end with a '\n' character.

Parameters:
mol - the molecule
Returns:
a string or a byte array, null for failure
Throws:
MolExportException - molecule cannot be exported in this format

close

public java.lang.Object close()
                       throws MolExportException
Close the stream. This method is called after the last convert().

Returns:
last section of the file as a string or a byte array, or null
Throws:
MolExportException - molecule cannot be exported in this format

getFormat

public final java.lang.String getFormat()
Returns the output format.


getOptions

protected final java.lang.String getOptions()
Returns the output options.


isDocumentExport

public boolean isDocumentExport()
Tests if this export module is document export instead of a simple molecule export.

Returns:
false in the default implementation
Since:
Marvin 4.0, 07/01/2005

isCleanable

public boolean isCleanable()
Tests whether 2D or 3D cleaning is meaningful for this output format.

Returns:
true in the default implementation
Since:
Marvin 4.1, 02/12/2006

isImplicitHcountImportant

public static boolean isImplicitHcountImportant(MolAtom a)
Checks whether the number of implicit hydrogens is important information for an atom or not. Implicit H count is exported if possible for Nitrogen, Phosphorus and Arsenic atoms with two aromatic bonds.

Parameters:
a - the atom
Returns:
true if implicit H count is important on the atom.
Since:
Marvin 3.5, 10/08/2004

isImplicitHcountImportant

public boolean isImplicitHcountImportant(MoleculeGraph mol)
Checks whether the number of implicit hydrogens is important information for the atoms of mol or not.

Parameters:
mol - the molecule
Returns:
true if there is important implicit H in the molecule.
Since:
Marvin 4.0.2, 08/24/2005
See Also:
isImplicitHcountImportant(chemaxon.struc.MolAtom)

setEncoding

public java.lang.String setEncoding(java.lang.String opts,
                                    java.lang.String enc)
Sets the output encoding using the export options.

Parameters:
opts - the export options
enc - the default encoding
Returns:
the value of the encoding field or null
Throws:
java.nio.charset.IllegalCharsetNameException - if the encoding is illegal
java.nio.charset.UnsupportedCharsetException - if the encoding is unsupported
Since:
Marvin 4.0.4, 01/03/2006

getEncoding

public java.lang.String getEncoding()
Gets the output encoding.

Returns:
the value of the encoding field or null

parseOption

protected int parseOption(java.lang.String opts,
                          int i)
                   throws java.lang.IllegalArgumentException
Parses the following option in the option string.

Parameters:
opts - the option string
i - current index
Returns:
index of the next option, or i if the current option is unknown
Throws:
java.lang.IllegalArgumentException - in case of parsing error

nextOpt

protected static int nextOpt(java.lang.String opts,
                             int i,
                             java.lang.String s)
Tests whether the option string contains the specified substring at the specified position.

Parameters:
opts - the option string
i - current index
s - the substring
Returns:
i + length of s if opts contains s at position i, i otherwise
Since:
Marvin 5.1.4, 11/09/2008

getOptionSign

protected final int getOptionSign()
Gets the sign of the options. The sign is a + or - character preceding the option(s) in the string.

Returns:
1, 0 or -1

parseCharIfOptionSign

protected int parseCharIfOptionSign(java.lang.String opts,
                                    int i)
Parse the current character if it is an option sign.

Parameters:
opts - the option string
i - current character index
Returns:
i + 1 if character at i is an option sign, i otherwise
Since:
Marvin 4.1.9, 06/06/2007

preconvert

protected Molecule preconvert(Molecule mol)
Optionally performs aromatization or addition of explicit Hydrogens atoms. Contracted S-groups are also expanded.

Parameters:
mol - the molecule
Returns:
the converted molecule or the argument (if conversion is not perfomed)

preconvert

protected final Molecule preconvert(Molecule mol,
                                    boolean xg)
Optionally performs aromatization or addition of explicit Hydrogens atoms. Contracted S-groups can also be expanded.

Parameters:
mol - the molecule
xg - expand S-groups (true) or not (false)
Returns:
the converted molecule or the argument (if conversion is not perfomed)

preconvert

protected final Molecule preconvert(Molecule mol,
                                    boolean xg,
                                    int xopts,
                                    boolean ih)
Optionally performs aromatization or addition of explicit Hydrogens atoms. Contracted S-groups can also be expanded.

Parameters:
mol - the molecule
xg - expand S-groups (true) or not (false)
xopts - expansion options
ih - if true, important implicit H-s are converted to attached data.
Returns:
the converted molecule or the argument (if conversion is not perfomed)
See Also:
Expandable.expand(int), Expandable.DEFAULT_OPTIONS, Expandable.MDL_EXPAND

preconvert

protected final MoleculeGraph preconvert(MoleculeGraph molg,
                                         boolean xg,
                                         int xopts,
                                         boolean ih)
Optionally performs aromatization or addition of explicit Hydrogens atoms. Contracted S-groups can also be expanded.

Parameters:
molg - the molecule
xg - expand S-groups (true) or not (false)
xopts - expansion options
ih - if true, important implicit H-s are converted to attached data.
Returns:
the converted molecule or the argument (if conversion is not perfomed)
Since:
Marvin 3.5.1, 11/16/2004
See Also:
Expandable.expand(int), Expandable.DEFAULT_OPTIONS, Expandable.MDL_EXPAND

addData

protected void addData(Molecule mol,
                       MolAtom ma,
                       java.lang.String fieldName,
                       java.lang.String value)
Adds a data sgroup to the given atom in the given molecule with fieldName and value.

Since:
Marvin 4.0.1 8/19/2005

appendChars

protected final void appendChars(int n,
                                 char c)
Append a character n times to the string buffer.

See Also:
stringBuffer

appendLeft

protected final void appendLeft(java.lang.String t,
                                int n)
Append a string to the buffer in %-ns format.

See Also:
stringBuffer

appendRight

protected final void appendRight(java.lang.String t,
                                 int n,
                                 char c)
Append a string to the buffer in the right hand side of an n-characters wide field. The left side is filled with the specified characters.

See Also:
stringBuffer

appendRight

protected final void appendRight(int t,
                                 int n,
                                 char c)
Append an integer to the buffer in the right hand side of an n-characters wide field. The left side is filled with the specified characters.

See Also:
stringBuffer

getOptionDescriptors

protected static java.lang.String getOptionDescriptors(java.util.ResourceBundle rc,
                                                       java.lang.String fmtname,
                                                       java.lang.String optnames,
                                                       java.util.List l)
Gets an array of option descriptors.

Parameters:
rc - the resource bundle containing the option info
fmtname - the format name or null
optnames - space separated list of option names
l - the output list
Returns:
the nonprocessed option names or null
Since:
Marvin 5.0, 06/06/2007