~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/io/iterator/IteratingSMILESReader.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $RCSfile$
 
2
 * $Author: egonw $
 
3
 * $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $
 
4
 * $Revision: 7636 $
 
5
 *
 
6
 * Copyright (C) 2004-2007  The Chemistry Development Kit (CDK) project
 
7
 *
 
8
 * Contact: cdk-devel@lists.sourceforge.net
 
9
 *
 
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.
 
18
 *
 
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.
 
23
 *
 
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.
 
27
 */
 
28
package org.openscience.cdk.io.iterator;
 
29
 
 
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;
 
36
 
 
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;
 
43
 
 
44
/**
 
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.
 
49
 *
 
50
 * <p>For parsing each SMILES it still uses the normal SMILESReader.
 
51
 *
 
52
 * @cdk.module smiles
 
53
 *
 
54
 * @see org.openscience.cdk.io.SMILESReader
 
55
 * 
 
56
 * @author     Egon Willighagen <egonw@sci.kun.nl>
 
57
 * @cdk.created    2004-12-16
 
58
 *
 
59
 * @cdk.keyword    file format, SMILES
 
60
 */
 
61
public class IteratingSMILESReader extends DefaultIteratingChemObjectReader {
 
62
 
 
63
    private BufferedReader input;
 
64
    private LoggingTool logger;
 
65
    private String currentLine;
 
66
    private SmilesParser sp = null;
 
67
    
 
68
    private boolean nextAvailableIsKnown;
 
69
    private boolean hasNext;
 
70
    private IMolecule nextMolecule;
 
71
    
 
72
    /**
 
73
     * Contructs a new IteratingSMILESReader that can read Molecule from a given Reader.
 
74
     *
 
75
     * @param  in  The Reader to read from
 
76
     */
 
77
    public IteratingSMILESReader(Reader in) {
 
78
        logger = new LoggingTool(this);
 
79
        input = new BufferedReader(in);
 
80
        sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
 
81
        nextMolecule = null;
 
82
        nextAvailableIsKnown = false;
 
83
        hasNext = false;
 
84
    }
 
85
 
 
86
    /**
 
87
     * Contructs a new IteratingSMILESReader that can read Molecule from a given InputStream.
 
88
     *
 
89
     * @param  in  The InputStream to read from
 
90
     */
 
91
    public IteratingSMILESReader(InputStream in) {
 
92
        this(new InputStreamReader(in));
 
93
    }
 
94
 
 
95
    public IResourceFormat getFormat() {
 
96
        return SMILESFormat.getInstance();
 
97
    }
 
98
 
 
99
    public boolean hasNext() {
 
100
        if (!nextAvailableIsKnown) {
 
101
            hasNext = false;
 
102
            
 
103
            // now try to parse the next Molecule
 
104
            try {
 
105
                if (input.ready()) {
 
106
                    currentLine = input.readLine().trim();
 
107
                    logger.debug("Line: ", currentLine);
 
108
                    int indexSpace = currentLine.indexOf(" ");
 
109
                    String SMILES = currentLine;
 
110
                    String name = null;
 
111
                
 
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,
 
117
                                     " + " , name);
 
118
                    }
 
119
                
 
120
                    nextMolecule = sp.parseSmiles(SMILES);
 
121
                    if (name != null) {
 
122
                        nextMolecule.setProperty("SMIdbNAME", name);
 
123
                    }
 
124
                    if (nextMolecule.getAtomCount() > 0) {
 
125
                        hasNext = true;
 
126
                    } else {
 
127
                        hasNext = false;
 
128
                    }
 
129
                } else {
 
130
                    hasNext = false;
 
131
                }
 
132
            } catch (Exception exception) {
 
133
                logger.error("Error while reading next molecule: ", exception.getMessage());
 
134
                logger.debug(exception);
 
135
                hasNext = false;
 
136
            }
 
137
            if (!hasNext) nextMolecule = null;
 
138
            nextAvailableIsKnown = true;
 
139
        }
 
140
        return hasNext;
 
141
    }
 
142
    
 
143
    public Object next() {
 
144
        if (!nextAvailableIsKnown) {
 
145
            hasNext();
 
146
        }
 
147
        nextAvailableIsKnown = false;
 
148
        if (!hasNext) {
 
149
            throw new NoSuchElementException();
 
150
        }
 
151
        return nextMolecule;
 
152
    }
 
153
    
 
154
    public void close() throws IOException {
 
155
        input.close();
 
156
    }
 
157
    
 
158
    public void remove() {
 
159
        throw new UnsupportedOperationException();
 
160
    }
 
161
}
 
162