chemaxon.marvin.util
Interface MolImportIface

All Known Subinterfaces:
MDocumentImportIface

public interface MolImportIface

Molecule reader interface. Implemented by import modules.

Example
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:

 Pyrrole
 5
 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
 5
 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 chemaxon.marvin.modules;
 
 import chemaxon.struc.*;
 import chemaxon.formats.MolInputStream;
 import java.io.IOException;
 import java.util.StringTokenizer;
 
 public class MyformatImport implements chemaxon.marvin.util.MolImportIface
 {
     private MolInputStream istream;
 
     public void initMolImport(MolInputStream mis, String opts) {
         istream = mis;
     }
 
     public boolean readMol(Molecule mol) throws IOException {
         // initialize molecule object
         mol.clearForImport("myformat");
         mol.setStartPosition(istream.getFilePointer());
 
         // read the molecule
         int lineCount = 1;
         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 {
             ++lineCount;
             line = istream.readLine();
             int numAtoms = Integer.parseInt(line.trim());
             for(int i = 0; i < numAtoms; ++i) {
                 ++lineCount;
                 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);
             }
             mol.endReuse(numAtoms);
             ++lineCount;
             line = istream.readLine();
	       int numBonds = Integer.parseInt(line.trim());
             for(int i = 0; i < numBonds; ++i) {
                 ++lineCount;
                 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.add(b);
                 mol.valenceCheck(null); // to set the implicit hydrogens
             }
         } catch(NumberFormatException ex) {
             throw new IOException(String.valueOf(lineCount)+": error in molecule file");
         }
 
         // memorize the last file position corresponding to this molecule
         mol.setEndPosition(istream.getFilePointer());
         return true;
     }

     public boolean skipToNext() {
         return false;
     }
 }
 

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:)">

 msketch.setMol("pyrrole.myf(myformat:)");
 

Since:
2.2
Version:
2.9.12, 04/05/2002
Author:
Peter Csizmadia

Method Summary
 void initMolImport(MolInputStream is, java.lang.String options)
          Initialization.
 boolean readMol(Molecule m)
          Reads the next molecule.
 boolean skipToNext()
          Skips the remaining parts of the current molecule and positions the file pointer to the next one.
 

Method Detail

initMolImport

public void initMolImport(MolInputStream is,
                          java.lang.String options)
                   throws java.io.IOException
Initialization.

Parameters:
is - readMol will read this stream
options - import options or null
Throws:
java.io.IOException - an error occured while reading the file

readMol

public boolean readMol(Molecule m)
                throws java.io.IOException
Reads the next molecule. Implementations must call clearForImport and setStartPosition before beginning to read the stream, and setEndPosition after the last byte. (File position information is needed by Marvin when overwriting multi-molecule files.)

Throws:
java.io.IOException - the file is in bad format or could not read

skipToNext

public boolean skipToNext()
Skips the remaining parts of the current molecule and positions the file pointer to the next one. Use this method if an error occured but you want to continue reading the file.

Returns:
true if the end of the current molecule is found, false if there is no chance to continue