Class MolExportModule

  • Direct Known Subclasses:
    MolExport

    @PublicAPI
    public abstract class MolExportModule
    extends 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
    See Also:
    Molecule.toFormat(java.lang.String), Molecule.toBinFormat(java.lang.String)
    • Field Detail

      • stringBuffer

        protected StringBuffer stringBuffer
        This buffer can contain the molecule file contents, in case of a text format.
      • 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​(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​(String fmtname,
                                            String optnames,
                                            List<OptionDescriptor> 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 Object open​(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 Object open​(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 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 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 String getFormat()
        Returns the output format.
      • getOptions

        protected final 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 atom)
        Checks whether the number of implicit hydrogens is important information for an atom or not. Implicit H count is exported: - if the atom has aromatic bond, has exactly one implicit hydrogen and can be ambiguous according to local aromatic valence check. Possible atoms are: B, C-, C+, N, P, As, S+, O+ - if the atom is an S atom and has 3 bonds and an implicit H.
        Parameters:
        atom - 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 String setEncoding​(String opts,
                                  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:
        IllegalCharsetNameException - if the encoding is illegal
        UnsupportedCharsetException - if the encoding is unsupported
        Since:
        Marvin 4.0.4, 01/03/2006
      • getEncoding

        public String getEncoding()
        Gets the output encoding.
        Returns:
        the value of the encoding field or null
      • parseOption

        protected int parseOption​(String opts,
                                  int i)
                           throws 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:
        IllegalArgumentException - in case of parsing error
      • nextOpt

        protected static int nextOpt​(String opts,
                                     int i,
                                     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​(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 optionsRe
        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
      • addData

        protected void addData​(Molecule mol,
                               MolAtom ma,
                               String fieldName,
                               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​(String t,
                                        int n)
        Append a string to the buffer in %-ns format.
        See Also:
        stringBuffer
      • appendRight

        protected final void appendRight​(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 String getOptionDescriptors​(ResourceBundle rc,
                                                     String fmtname,
                                                     String optnames,
                                                     List<OptionDescriptor> 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
      • setGlobalGUIProperties

        public void setGlobalGUIProperties​(MPropertyContainer ggp)
        Pass and set the global GUI properties container.
        Parameters:
        ggp -
        Since:
        Marvin 6.0, 2013.03.26.