Package chemaxon.formats
Class MolImportModule
java.lang.Object
chemaxon.formats.MolImportModule
Base class of Molecule 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 myio;
import chemaxon.struc.*;
import chemaxon.formats.MolInputStream;
import chemaxon.formats.MolFormatException;
import java.util.StringTokenizer;
public class MyFormatImport extends chemaxon.formats.MolImportModule
{
private MolInputStream istream;
public void initMolImport(MolInputStream mis) {
istream = mis;
}
public boolean readMol(Molecule mol) throws MolFormatException {
// initialize molecule object
mol.clearForImport("myformat");
mol.setStartPosition(istream.getFilePointer());
// 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 invalid input: '<' 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);
}
mol.endReuse(numAtoms);
line = istream.readLine();
int numBonds = Integer.parseInt(line.trim());
for(int i = 0; i invalid input: '<' 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.add(b);
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
mol.setEndPosition(istream.getFilePointer());
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,
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:)");
- Since:
- Marvin 5.0, 09/07/2007
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract MoleculeCreates a new target molecule object for the import.intReturns the line count in the molecule input stream.voidInitializes the import module.voidinitMolImport(chemaxon.formats.util.MRecord record, String fname) Deprecated, for removal: This API element is subject to removal in a future version.Should not be public.booleanTests whether this module is a document importer or not.readDocument(MDocument doc) Reads the next document.abstract booleanReads the next molecule.voidsetOptions(String options) Sets the import options.
-
Constructor Details
-
MolImportModule
public MolImportModule()
-
-
Method Details
-
setOptions
Sets the import options. The default implementation is empty.- Parameters:
options- import options- Throws:
IllegalArgumentException- if the specified options cannot be interpreted by the import module
-
initMolImport
@SubjectToRemoval(date=JUL_01_2027) @Deprecated(forRemoval=true) public void initMolImport(chemaxon.formats.util.MRecord record, String fname) throws IOException Deprecated, for removal: This API element is subject to removal in a future version.Should not be public. Visibility will be restricted inRemovalDate.JUL_01_2027Initializes the import module. By default, it callsinitMolImport(chemaxon.formats.MolInputStream)with the input stream generated from the molecule string stored in the specified molecule record.- Parameters:
record- the molecule recordfname- the input filename or URL- Throws:
MolFormatException- if the file cannot be read because of a molecule file format related errorIOException- an I/O error occurred while reading the file- Since:
- Marvin 5.3
-
initMolImport
Initializes the import module. The default implementation is empty.- Parameters:
is- the molecule input stream- Throws:
MolFormatException- if the file cannot be read because of a molecule file format related errorIOException- an I/O error occurred while reading the file
-
readMol
Reads the next molecule.- Returns:
trueif reading was successful,falseif there is nothing to read (EOF reached)- Throws:
MolFormatException- if the file cannot be read because of a molecule file format related errorIOException- an I/O error occurred while reading the file
-
createMol
Creates a new target molecule object for the import.- Since:
- Marvin 3.4, 05/08/2004
-
isDocumentImporter
public boolean isDocumentImporter()Tests whether this module is a document importer or not.- Returns:
trueif it it is a document readers,falseif it is a simple molecule reader- See Also:
-
readDocument
Reads the next document. This method should only be called ifisDocumentImporter()returnstrue.- Parameters:
doc- target document or null- Returns:
- the target document object
- Throws:
IOException- the file is in bad format or could not read- See Also:
-
getLineCount
public int getLineCount()Returns the line count in the molecule input stream.- Returns:
- the line count in the molecule input stream
- Since:
- Marvin 5.3
-