3
* $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $
6
* Copyright (C) 2004-2007 The Chemistry Development Kit (CDK) project
8
* Contact: cdk-devel@lists.sourceforge.net
10
* This program is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public License
12
* as published by the Free Software Foundation; either version 2.1
13
* of the License, or (at your option) any later version.
14
* All we ask is that proper credit is given for our work, which includes
15
* - but is not limited to - adding the above copyright notice to the beginning
16
* of your source code files, and to any copyright notice that you may distribute
17
* with programs based on this work.
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU Lesser General Public License for more details.
24
* You should have received a copy of the GNU Lesser General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28
package org.openscience.cdk.io.iterator;
30
import java.io.BufferedReader;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.io.InputStreamReader;
34
import java.io.Reader;
35
import java.util.NoSuchElementException;
37
import org.openscience.cdk.DefaultChemObjectBuilder;
38
import org.openscience.cdk.interfaces.IMolecule;
39
import org.openscience.cdk.io.formats.IResourceFormat;
40
import org.openscience.cdk.io.formats.SMILESFormat;
41
import org.openscience.cdk.smiles.SmilesParser;
42
import org.openscience.cdk.tools.LoggingTool;
45
* Iterating SMILES file reader. It allows to iterate over all molecules
46
* in the SMILES file, without being read into memory all. Suitable
47
* for very large SMILES files. These SMILES files are expected to have one
48
* molecule on each line.
50
* <p>For parsing each SMILES it still uses the normal SMILESReader.
54
* @see org.openscience.cdk.io.SMILESReader
56
* @author Egon Willighagen <egonw@sci.kun.nl>
57
* @cdk.created 2004-12-16
59
* @cdk.keyword file format, SMILES
61
public class IteratingSMILESReader extends DefaultIteratingChemObjectReader {
63
private BufferedReader input;
64
private LoggingTool logger;
65
private String currentLine;
66
private SmilesParser sp = null;
68
private boolean nextAvailableIsKnown;
69
private boolean hasNext;
70
private IMolecule nextMolecule;
73
* Contructs a new IteratingSMILESReader that can read Molecule from a given Reader.
75
* @param in The Reader to read from
77
public IteratingSMILESReader(Reader in) {
78
logger = new LoggingTool(this);
79
input = new BufferedReader(in);
80
sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
82
nextAvailableIsKnown = false;
87
* Contructs a new IteratingSMILESReader that can read Molecule from a given InputStream.
89
* @param in The InputStream to read from
91
public IteratingSMILESReader(InputStream in) {
92
this(new InputStreamReader(in));
95
public IResourceFormat getFormat() {
96
return SMILESFormat.getInstance();
99
public boolean hasNext() {
100
if (!nextAvailableIsKnown) {
103
// now try to parse the next Molecule
106
currentLine = input.readLine().trim();
107
logger.debug("Line: ", currentLine);
108
int indexSpace = currentLine.indexOf(" ");
109
String SMILES = currentLine;
112
if (indexSpace != -1) {
113
logger.debug("Space found at index: ", indexSpace);
114
SMILES = currentLine.substring(0,indexSpace);
115
name = currentLine.substring(indexSpace+1);
116
logger.debug("Line contains SMILES and name: ", SMILES,
120
nextMolecule = sp.parseSmiles(SMILES);
122
nextMolecule.setProperty("SMIdbNAME", name);
124
if (nextMolecule.getAtomCount() > 0) {
132
} catch (Exception exception) {
133
logger.error("Error while reading next molecule: ", exception.getMessage());
134
logger.debug(exception);
137
if (!hasNext) nextMolecule = null;
138
nextAvailableIsKnown = true;
143
public Object next() {
144
if (!nextAvailableIsKnown) {
147
nextAvailableIsKnown = false;
149
throw new NoSuchElementException();
154
public void close() throws IOException {
158
public void remove() {
159
throw new UnsupportedOperationException();