Class MolImportModule

  • Direct Known Subclasses:

    public abstract class MolImportModule
    extends Object
    Base class of Molecule import modules.

    I want to read a molecule from a file in a special format ("myformat") that stores the molecule name, the atom symbols, the x, y coordinates and the bond orders:

     N -0.09625  2.75245
     C -1.42989  3.52245
     C -1.42989  5.06245
     C  1.23739  5.06245
     C  1.23739  3.52245
     1 2 1
     2 3 2
     3 4 1
     4 5 2
     5 1 1
    To read this file ("pyrrole.myf"), I create an import module for "myformat":

     package myio;
     import chemaxon.struc.*;
     import chemaxon.formats.MolInputStream;
     import chemaxon.formats.MolFormatException;
     import java.util.StringTokenizer;
     public class MyFormatImport extends
         private MolInputStream istream;
         public void initMolImport(MolInputStream mis) {
             istream = mis;
         public boolean readMol(Molecule mol) throws MolFormatException {
             // initialize molecule object
             // read the molecule
             String line = istream.readLine();
             if(line == null) // end of file
                 return false;
             mol.setName(line.trim()); // set molecule name
             mol.setDim(2);            // set number of dimensions
             try {
                 line = istream.readLine();
                 int numAtoms = Integer.parseInt(line.trim());
                 for(int i = 0; i < numAtoms; ++i) {
                     line = istream.readLine();
                     StringTokenizer st = new StringTokenizer(line);
                     int atno = MolAtom.numOf(st.nextToken());
                     double x = Double.valueOf(st.nextToken()).doubleValue();
                     double y = Double.valueOf(st.nextToken()).doubleValue();
                     MolAtom a = mol.reuseAtom(atno, i);
                     a.setXY(x, y);
                 line = istream.readLine();
                   int numBonds = Integer.parseInt(line.trim());
                 for(int i = 0; i < numBonds; ++i) {
                     line = istream.readLine();
                     StringTokenizer st = new StringTokenizer(line);
                     int atomIndex1 = Integer.parseInt(st.nextToken()) - 1;
                     int atomIndex2 = Integer.parseInt(st.nextToken()) - 1;
                     int order = Integer.parseInt(st.nextToken());
                     MolBond b = new MolBond(mol.getAtom(atomIndex1),
                                             mol.getAtom(atomIndex2), order);
                     mol.valenceCheck(null); // to set the implicit hydrogens
             } catch(NumberFormatException ex) {
                 throw new MolFormatException("error in molecule file");
             // memorize the last file position corresponding to this molecule
             return true;
         public boolean createMol() {
             return new Molecule();

    After compiling and placing the class into Marvin's CLASSPATH, I can read pyrrole.myf with Marvin applications,

     molconvert smiles "pyrrole.myf(myformat:)"
     msketch "pyrrole.myf(myformat:)"
     mview "pyrrole.myf(myformat:)"
    and the applets:
     <param NAME="mol" VALUE="pyrrole.myf(myformat:)">
    Marvin 5.0, 09/07/2007
    • Constructor Detail

      • MolImportModule

        public MolImportModule()
    • Method Detail

      • setOptions

        public void setOptions​(String options)
                        throws IllegalArgumentException
        Sets the import options. The default implementation is empty.
        options - import options
        IllegalArgumentException - if the specified options cannot be interpreted by the import module
      • readMol

        public abstract boolean readMol​(Molecule m)
                                 throws MolFormatException,
        Reads the next molecule.
        true if reading was successful, false if there is nothing to read (EOF reached)
        MolFormatException - if the file cannot be read because of a molecule file format related error
        IOException - an I/O error occurred while reading the file
      • createMol

        public abstract Molecule createMol()
        Creates a new target molecule object for the import.
        Marvin 3.4, 05/08/2004
      • isDocumentImporter

        public boolean isDocumentImporter()
        Tests whether this module is a document importer or not.
        true if it it is a document readers, false if it is a simple molecule reader
        See Also:
      • getLineCount

        public int getLineCount()
        Returns the line count in the molecule input stream.
        the line count in the molecule input stream
        Marvin 5.3
      • getPreferredView

        public MolImportModule.PreferredView getPreferredView()
        Specifies which view style is preferred (for instance in MarvinView) for this format.