chemaxon.marvin.util
Class MolExportModule

java.lang.Object
  extended bychemaxon.marvin.util.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 MolImportIface example, is the following:


 package chemaxon.marvin.modules;
 
 import chemaxon.struc.*;
 
 public class MyformatExport extends chemaxon.marvin.util.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.getNode1()) + 1));
             s.append('\t');
             s.append(String.valueOf(mol.indexOf(b.getNode2()) + 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:
2.7.9
Version:
3.3, 12/17/2003
Author:
Peter Csizmadia
See Also:
Molecule.toFormat(java.lang.String), Molecule.toBinFormat(java.lang.String)

Field Summary
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 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 getFormat()
          Returns the output format.
protected  java.lang.String getOptions()
          Returns the output options.
protected  int getOptionSign()
          Gets the sign of an option.
static MolExportModule loadMarvinModule(java.lang.String fmt)
          Loads a molecule export module for the specified format.
 java.lang.Object open(java.lang.String fmtopts)
          Open the exporter stream.
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)
          Optionally performs aromatization or addition of explicit Hydrogens atoms.
 
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.

Constructor Detail

MolExportModule

public MolExportModule()
Method Detail

loadMarvinModule

public static MolExportModule loadMarvinModule(java.lang.String fmt)
                                        throws java.lang.SecurityException
Loads a molecule export module for the specified format.

Parameters:
fmt - the format descriptor string
Throws:
java.lang.SecurityException - if the module cannot be loaded because of a firewall problem

open

public java.lang.Object open(java.lang.String fmtopts)
                      throws java.lang.IllegalArgumentException
Open 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:
java.lang.IllegalArgumentException - Invalid format string.
See Also:
getFormat(), getOptions()

convert

public abstract java.lang.Object convert(Molecule mol)
                                  throws MolExportException
Convert a molecule to a string or byte array.

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.


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

getOptionSign

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

Returns:
1 or -1

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)
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
Returns:
the converted molecule or the argument (if conversion is not perfomed)
See Also:
Expandable.expand(int), Expandable.DEFAULT_OPTIONS, Expandable.MDL_EXPAND

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