|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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:
To read this file ("pyrrole.myf"), I create an import module for "myformat":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
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 voidinitMolImport
(MolInputStream
mis, String opts) { istream = mis; } public booleanreadMol
(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 booleanskipToNext
() { return false; } }
After compiling and placing the class into Marvin's CLASSPATH, I can read pyrrole.myf with Marvin applications,
and the applets:molconvert smiles "pyrrole.myf(myformat:)" msketch "pyrrole.myf(myformat:)" mview "pyrrole.myf(myformat:)"
<param NAME="mol" VALUE="pyrrole.myf(myformat:)"> msketch.setMol("pyrrole.myf(myformat:)");
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 |
public void initMolImport(MolInputStream is, java.lang.String options) throws java.io.IOException
is
- readMol will read this streamoptions
- import options or null
java.io.IOException
- an error occured while reading the filepublic boolean readMol(Molecule m) throws java.io.IOException
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.)
java.io.IOException
- the file is in bad format or could not readpublic boolean skipToNext()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |