Skip to content

Commit

Permalink
Makes the smiles module independent from the data module
Browse files Browse the repository at this point in the history
Signed-off-by: Rajarshi  Guha <rajarshi.guha@gmail.com>
  • Loading branch information
egonw authored and rajarshi committed Nov 8, 2011
1 parent edadc8f commit a7920c1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 54 deletions.
1 change: 0 additions & 1 deletion src/META-INF/smiles.cdkdepends
@@ -1,5 +1,4 @@
cdk-annotation.jar
cdk-data.jar
cdk-core.jar
cdk-interfaces.jar
cdk-ioformats.jar
Expand Down
36 changes: 12 additions & 24 deletions src/main/org/openscience/cdk/io/SMILESReader.java
Expand Up @@ -28,14 +28,19 @@
*/
package org.openscience.cdk.io;

import org.openscience.cdk.DefaultChemObjectBuilder;
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.IChemFile;
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IChemObject;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IChemSequence;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.interfaces.IMoleculeSet;
Expand All @@ -45,13 +50,6 @@
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 All @@ -78,33 +76,21 @@ public class SMILESReader extends DefaultChemObjectReader {
private static ILoggingTool logger =
LoggingToolFactory.createLoggingTool(SMILESReader.class);

/*
* Construct a new reader from a Reader type object.
*
* By default the {@link DefaultChemObjectBuilder} is used.
*
* @param input reader from which input is read
*/
public SMILESReader(Reader input) {
this(input, DefaultChemObjectBuilder.getInstance());
}

/**
* 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, IChemObjectBuilder builder) {
this.input = new BufferedReader(input);
sp = new SmilesParser(builder);
public SMILESReader(Reader input) {
this.input = new BufferedReader(input);
}


public SMILESReader(InputStream input) {
this(new InputStreamReader(input));
}

public SMILESReader() {
this(new StringReader(""));
}
Expand Down Expand Up @@ -150,6 +136,8 @@ public boolean accepts(Class classObject) {
*/
@TestMethod("testReading,testReadingSmiFile_1,testReadingSmiFile_2,testReadingSmiFile_3")
public <T extends IChemObject> T read(T object) throws CDKException {
sp = new SmilesParser(object.getBuilder());

if (object instanceof IMoleculeSet) {
return (T)readMoleculeSet((IMoleculeSet)object);
} else if (object instanceof IChemFile) {
Expand Down
@@ -1,9 +1,4 @@
/* $RCSfile$
* $Author$
* $Date$
* $Revision$
*
* Copyright (C) 2004-2007 The Chemistry Development Kit (CDK) project
/* Copyright (C) 2004-2007 The Chemistry Development Kit (CDK) project
*
* Contact: cdk-devel@lists.sourceforge.net
*
Expand Down Expand Up @@ -35,7 +30,6 @@
import java.util.NoSuchElementException;

import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.interfaces.IChemObject;
Expand Down Expand Up @@ -92,18 +86,6 @@ public IteratingSMILESReader(Reader in, IChemObjectBuilder builder) {
setReader(in);
}

/**
* Constructs a new IteratingSMILESReader that can read Molecule from a given InputStream.
*
* This method will use @link{DefaultChemObjectBuilder} to build the actual molecules
*
* @param in The InputStream to read from
*/
@TestMethod("testSMILESFileWithNames")
public IteratingSMILESReader(InputStream in) {
this(new InputStreamReader(in), DefaultChemObjectBuilder.getInstance());
}

/**
* Constructs a new IteratingSMILESReader that can read Molecule from a given InputStream and IChemObjectBuilder.
*
Expand Down
3 changes: 1 addition & 2 deletions src/main/org/openscience/cdk/normalize/Normalizer.java
Expand Up @@ -26,7 +26,6 @@
import java.util.Iterator;
import java.util.List;

import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.exception.CDKException;
Expand Down Expand Up @@ -71,7 +70,7 @@ public class Normalizer {
@TestMethod("testNormalize")
public static boolean normalize(IAtomContainer ac, Document doc) throws InvalidSmilesException, CDKException {
NodeList nl = doc.getElementsByTagName("replace-set");
SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
SmilesParser sp = new SmilesParser(ac.getBuilder());
boolean change=false;
for (int i = 0; i < nl.getLength(); i++) {
Element child = (Element) nl.item(i);
Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.junit.Test;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.CDKTestCase;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.Molecule;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.io.formats.IResourceFormat;
Expand All @@ -56,7 +57,9 @@ public void testSMILESFileWithNames() throws Exception {
String filename = "data/smiles/test.smi";
logger.info("Testing: " + filename);
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
IteratingSMILESReader reader = new IteratingSMILESReader(ins);
IteratingSMILESReader reader = new IteratingSMILESReader(
ins, DefaultChemObjectBuilder.getInstance()
);

int molCount = 0;
while (reader.hasNext()) {
Expand All @@ -76,7 +79,9 @@ public void testSMILESFileWithSpacesAndTabs() throws Exception {
String filename = "data/smiles/tabs.smi";
logger.info("Testing: " + filename);
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
IteratingSMILESReader reader = new IteratingSMILESReader(ins);
IteratingSMILESReader reader = new IteratingSMILESReader(
ins, DefaultChemObjectBuilder.getInstance()
);

int molCount = 0;
while (reader.hasNext()) {
Expand All @@ -96,7 +101,9 @@ public void testSMILESTitles() throws Exception {
String filename = "data/smiles/tabs.smi";
logger.info("Testing: " + filename);
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
IteratingSMILESReader reader = new IteratingSMILESReader(ins);
IteratingSMILESReader reader = new IteratingSMILESReader(
ins, DefaultChemObjectBuilder.getInstance()
);

while (reader.hasNext()) {

Expand All @@ -112,7 +119,9 @@ public void testSMILESFile() {
String filename = "data/smiles/test2.smi";
logger.info("Testing: " + filename);
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
IteratingSMILESReader reader = new IteratingSMILESReader(ins);
IteratingSMILESReader reader = new IteratingSMILESReader(
ins, DefaultChemObjectBuilder.getInstance()
);

int molCount = 0;
while (reader.hasNext()) {
Expand All @@ -130,7 +139,9 @@ public void testGetFormat() {
String filename = "data/smiles/test2.smi";
logger.info("Testing: " + filename);
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
IteratingSMILESReader reader = new IteratingSMILESReader(ins);
IteratingSMILESReader reader = new IteratingSMILESReader(
ins, DefaultChemObjectBuilder.getInstance()
);
IResourceFormat format = reader.getFormat();
Assert.assertTrue(format instanceof SMILESFormat);
}
Expand All @@ -139,7 +150,9 @@ public void testGetFormat() {
public void testSetReader1() {
String filename = "data/smiles/test2.smi";
InputStream ins1 = this.getClass().getClassLoader().getResourceAsStream(filename);
IteratingSMILESReader reader = new IteratingSMILESReader(ins1);
IteratingSMILESReader reader = new IteratingSMILESReader(
ins1, DefaultChemObjectBuilder.getInstance()
);
int molCount = 0;
while (reader.hasNext()) {
reader.next();
Expand All @@ -160,7 +173,9 @@ public void testSetReader1() {
public void testRemove() {
String filename = "data/smiles/test2.smi";
InputStream ins1 = this.getClass().getClassLoader().getResourceAsStream(filename);
IteratingSMILESReader reader = new IteratingSMILESReader(ins1);
IteratingSMILESReader reader = new IteratingSMILESReader(
ins1, DefaultChemObjectBuilder.getInstance()
);
int molCount = 0;
while (reader.hasNext()) {
reader.next();
Expand Down
Expand Up @@ -311,7 +311,9 @@ public void match(String smarts, String smiles) throws Exception {
@Test public void testBasicAmineOnDrugs() throws Exception {
String filename = "data/smiles/drugs.smi";
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
IteratingSMILESReader reader = new IteratingSMILESReader(ins);
IteratingSMILESReader reader = new IteratingSMILESReader(
ins, DefaultChemObjectBuilder.getInstance()
);

SMARTSQueryTool sqt = new SMARTSQueryTool("[NX3;H2,H1;!$(NC=O)]");
int nmatch = 0;
Expand Down

0 comments on commit a7920c1

Please sign in to comment.