Skip to content

Commit

Permalink
Updated io classes to use IAtomContainer*
Browse files Browse the repository at this point in the history
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
rajarshi authored and egonw committed Dec 8, 2011
1 parent f0cf950 commit 68f2a65
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 63 deletions.
46 changes: 23 additions & 23 deletions src/main/org/openscience/cdk/io/HINWriter.java
Expand Up @@ -23,29 +23,28 @@
*/
package org.openscience.cdk.io;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;

import javax.vecmath.Point3d;

import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IAtomContainerSet;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IChemObject;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.interfaces.IMoleculeSet;
import org.openscience.cdk.io.formats.HINFormat;
import org.openscience.cdk.io.formats.IResourceFormat;
import org.openscience.cdk.tools.LoggingToolFactory;

import javax.vecmath.Point3d;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;

/**
* Writer that outputs in the HIN format.
*
Expand Down Expand Up @@ -113,25 +112,25 @@ public void close() throws IOException {
@TestMethod("testAccepts")
public boolean accepts(Class classObject) {
Class[] interfaces = classObject.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
if (IMolecule.class.equals(interfaces[i])) return true;
if (IMoleculeSet.class.equals(interfaces[i])) return true;
for (Class anInterface : interfaces) {
if (IAtomContainer.class.equals(anInterface)) return true;
if (IAtomContainerSet.class.equals(anInterface)) return true;
}
return false;
}

public void write(IChemObject object) throws CDKException {
if (object instanceof IMolecule) {
if (object instanceof IAtomContainer) {
try {
IMoleculeSet som = object.getBuilder().newInstance(IMoleculeSet.class);
som.addAtomContainer((IMolecule) object);
writeMolecule(som);
IAtomContainerSet som = object.getBuilder().newInstance(IAtomContainerSet.class);
som.addAtomContainer((IAtomContainer) object);
writeAtomContainer(som);
} catch (Exception ex) {
throw new CDKException("Error while writing HIN file: " + ex.getMessage(), ex);
}
} else if (object instanceof IMoleculeSet) {
} else if (object instanceof IAtomContainerSet) {
try {
writeMolecule((IMoleculeSet) object);
writeAtomContainer((IAtomContainerSet) object);
} catch (IOException ex) {
//
}
Expand All @@ -146,8 +145,9 @@ public void write(IChemObject object) throws CDKException {
* as well
*
* @param som the set of molecules to write
* @throws java.io.IOException if there is a problem writing the molecule
*/
private void writeMolecule(IMoleculeSet som) throws IOException {
private void writeAtomContainer(IAtomContainerSet som) throws IOException {

//int na = 0;
//String info = "";
Expand All @@ -157,7 +157,7 @@ private void writeMolecule(IMoleculeSet som) throws IOException {

for (int molnum = 0; molnum < som.getAtomContainerCount(); molnum++) {

IMolecule mol = som.getMolecule(molnum);
IAtomContainer mol = som.getAtomContainer(molnum);

try {
String molname = "mol " + (molnum + 1) + " " + mol.getProperty(CDKConstants.TITLE);
Expand Down
30 changes: 17 additions & 13 deletions src/main/org/openscience/cdk/io/MoSSOutputReader.java
Expand Up @@ -21,17 +21,11 @@
*/
package org.openscience.cdk.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;

import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.exception.InvalidSmilesException;
import org.openscience.cdk.interfaces.IAtomContainerSet;
import org.openscience.cdk.interfaces.IChemFile;
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IChemObject;
Expand All @@ -44,6 +38,13 @@
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;

/**
* Reader for MoSS output files {@cdk.cite BOR2002} which present the results
* of a substructure mining study. These files look like:
Expand Down Expand Up @@ -135,10 +136,10 @@ public boolean accepts(Class testClass) {
* @return the content in a {@link IMoleculeSet} object
*/
public <T extends IChemObject> T read(T object) throws CDKException {
if (object instanceof IMoleculeSet) {
IMoleculeSet cf = (IMoleculeSet)object;
if (object instanceof IAtomContainerSet) {
IAtomContainerSet cf = (IAtomContainerSet)object;
try {
cf = readMoleculeSet(cf);
cf = readAtomContainerSet(cf);
} catch (IOException e) {
logger.error("Input/Output error while reading from input.");
}
Expand All @@ -147,9 +148,9 @@ public <T extends IChemObject> T read(T object) throws CDKException {
IChemFile chemFile = (IChemFile)object;
IChemSequence chemSeq = object.getBuilder().newInstance(IChemSequence.class);
IChemModel chemModel = object.getBuilder().newInstance(IChemModel.class);
IMoleculeSet molSet = object.getBuilder().newInstance(IMoleculeSet.class);
IAtomContainerSet molSet = object.getBuilder().newInstance(IAtomContainerSet.class);
try {
molSet = readMoleculeSet(molSet);
molSet = readAtomContainerSet(molSet);
} catch (IOException e) {
logger.error("Input/Output error while reading from input.");
}
Expand All @@ -164,8 +165,11 @@ public <T extends IChemObject> T read(T object) throws CDKException {

/**
* Read the file content into a {@link IMoleculeSet}.
* @param molSet an {@link IAtomContainerSet} to store the structures
* @return the {@link IAtomContainerSet} containing the molecules read in
* @throws java.io.IOException if there is an error during reading
*/
private IMoleculeSet readMoleculeSet(IMoleculeSet molSet) throws IOException {
private IAtomContainerSet readAtomContainerSet(IAtomContainerSet molSet) throws IOException {
SmilesParser parser = new SmilesParser(molSet.getBuilder());
parser.setPreservingAromaticity(true);
String line = input.readLine();
Expand Down
26 changes: 13 additions & 13 deletions src/main/org/openscience/cdk/io/SMILESReader.java
Expand Up @@ -28,16 +28,10 @@
*/
package org.openscience.cdk.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;

import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtomContainerSet;
import org.openscience.cdk.interfaces.IChemFile;
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IChemObject;
Expand All @@ -50,6 +44,13 @@
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;

/**
* This Reader reads files which has one SMILES string on each
* line, where the format is given as below:
Expand Down Expand Up @@ -80,7 +81,6 @@ public class SMILESReader extends DefaultChemObjectReader {
* Construct a new reader from a Reader and a specified builder object.
*
* @param input The Reader object from which to read structures
* @param builder the builder
*/
public SMILESReader(Reader input) {
this.input = new BufferedReader(input);
Expand Down Expand Up @@ -138,14 +138,14 @@ public boolean accepts(Class classObject) {
public <T extends IChemObject> T read(T object) throws CDKException {
sp = new SmilesParser(object.getBuilder());

if (object instanceof IMoleculeSet) {
return (T)readMoleculeSet((IMoleculeSet)object);
if (object instanceof IAtomContainerSet) {
return (T) readAtomContainerSet((IAtomContainerSet) object);
} else if (object instanceof IChemFile) {
IChemFile file = (IChemFile)object;
IChemSequence sequence = file.getBuilder().newInstance(IChemSequence.class);
IChemModel chemModel = file.getBuilder().newInstance(IChemModel.class);
chemModel.setMoleculeSet(readMoleculeSet(
file.getBuilder().newInstance(IMoleculeSet.class)
chemModel.setMoleculeSet(readAtomContainerSet(
file.getBuilder().newInstance(IMoleculeSet.class)
));
sequence.addChemModel(chemModel);
file.addChemSequence(sequence);
Expand All @@ -164,7 +164,7 @@ public <T extends IChemObject> T read(T object) throws CDKException {
* @param som The set of molecules that came fron the file
* @return A ChemFile containing the data parsed from input.
*/
private IMoleculeSet readMoleculeSet(IMoleculeSet som) {
private IAtomContainerSet readAtomContainerSet(IAtomContainerSet som) {
try {
String line = input.readLine().trim();
while (line != null) {
Expand Down
30 changes: 16 additions & 14 deletions src/main/org/openscience/cdk/io/SMILESWriter.java
Expand Up @@ -28,17 +28,11 @@
*/
package org.openscience.cdk.io;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;

import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IAtomContainerSet;
import org.openscience.cdk.interfaces.IChemObject;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.interfaces.IMoleculeSet;
Expand All @@ -50,6 +44,14 @@
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;

/**
* Writes the SMILES strings to a plain text file.
*
Expand Down Expand Up @@ -146,9 +148,9 @@ public boolean accepts(Class classObject) {
*/
public void write(IChemObject object) throws CDKException {
if (object instanceof IMoleculeSet) {
writeMoleculeSet((IMoleculeSet)object);
writeAtomContainerSet((IMoleculeSet) object);
} else if (object instanceof IMolecule) {
writeMolecule((IMolecule)object);
writeAtomContainer((IMolecule) object);
} else {
throw new CDKException("Only supported is writing of ChemFile and Molecule objects.");
}
Expand All @@ -159,12 +161,12 @@ public void write(IChemObject object) throws CDKException {
*
* @param som MoleculeSet that is written to an OutputStream
*/
public void writeMoleculeSet(IMoleculeSet som)
public void writeAtomContainerSet(IAtomContainerSet som)
{
writeMolecule(som.getMolecule(0));
writeAtomContainer(som.getAtomContainer(0));
for (int i = 1; i <= som.getAtomContainerCount() - 1; i++) {
try {
writeMolecule(som.getMolecule(i));
writeAtomContainer(som.getAtomContainer(i));
} catch (Exception exc) {
}
}
Expand All @@ -175,7 +177,7 @@ public void writeMoleculeSet(IMoleculeSet som)
*
* @param molecule Molecule of which the data is outputted.
*/
public void writeMolecule(IMolecule molecule) {
public void writeAtomContainer(IAtomContainer molecule) {
SmilesGenerator sg = new SmilesGenerator();
sg.setUseAromaticityFlag(useAromaticityFlag.isSet());
String smiles = "";
Expand Down

0 comments on commit 68f2a65

Please sign in to comment.