$darkmode
DENOPTIM
IteratingAtomContainerReader.java
Go to the documentation of this file.
1package denoptim.io;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.io.FileReader;
8import java.io.IOException;
9import java.util.Iterator;
10import java.util.List;
11
12import org.openscience.cdk.DefaultChemObjectBuilder;
13import org.openscience.cdk.exception.CDKException;
14import org.openscience.cdk.interfaces.IAtomContainer;
15import org.openscience.cdk.io.FormatFactory;
16import org.openscience.cdk.io.formats.IChemFormat;
17import org.openscience.cdk.io.formats.MDLV2000Format;
18import org.openscience.cdk.io.formats.MDLV3000Format;
19import org.openscience.cdk.io.iterator.DefaultIteratingChemObjectReader;
20import org.openscience.cdk.io.iterator.IteratingSDFReader;
21import org.openscience.cdk.io.iterator.IteratingSMILESReader;
22
34public class IteratingAtomContainerReader implements Iterator<IAtomContainer>
35{
36
40 private List<IAtomContainer> results;
41
46 private Iterator<IAtomContainer> listIterator;
47
52 private DefaultIteratingChemObjectReader<IAtomContainer> fileIterator;
53
58 private boolean usingIteratingReader = false;
59
60//------------------------------------------------------------------------------
61
70 public IteratingAtomContainerReader(File input)
71 throws FileNotFoundException, IOException, CDKException
72 {
73 FormatFactory factory = new FormatFactory();
74 factory.registerFormat(new SMILESListFormat());
75
76 BufferedReader headReader = new BufferedReader(new FileReader(input));
77 IChemFormat chemFormat = factory.guessFormat(headReader);
78 headReader.close();
79
80 if (chemFormat instanceof MDLV2000Format
81 || chemFormat instanceof MDLV3000Format)
82 {
83 FileInputStream fis = new FileInputStream(input);
84 fileIterator = new IteratingSDFReader(fis,
85 DefaultChemObjectBuilder.getInstance());
87 } else if (chemFormat instanceof SMILESListFormat) {
88
89 FileInputStream fis = new FileInputStream(input);
90 fileIterator = new IteratingSMILESReader(fis,
91 DefaultChemObjectBuilder.getInstance());
93 } else {
95 listIterator = results.iterator();
96 }
97 }
98
99//------------------------------------------------------------------------------
100
101 @Override
102 public boolean hasNext()
103 {
105 return fileIterator.hasNext();
106 else
107 return listIterator.hasNext();
108 }
109
110//------------------------------------------------------------------------------
111
112 @Override
113 public IAtomContainer next()
114 {
116 return fileIterator.next();
117 else
118 return listIterator.next();
119 }
120
121//------------------------------------------------------------------------------
122
128 public void close() throws IOException
129 {
130 if (fileIterator!=null)
131 fileIterator.close();
132 }
133
134//------------------------------------------------------------------------------
135
139 public Class<?> getIteratorType()
140 {
142 return fileIterator.getClass();
143 else
144 return listIterator.getClass();
145 }
146}
Utility methods for input/output.
static List< IAtomContainer > readAllAtomContainers(File file)
Returns a single collection with all atom containers found in a file of any format.
An iterator that take IAtomContainers from a file, possibly using an available iterating reader,...
DefaultIteratingChemObjectReader< IAtomContainer > fileIterator
Reference to the memory-efficient iterator, or null if we have had to pre-load all containers.
Iterator< IAtomContainer > listIterator
Reference to the iterator over pre-loaded containers or null if we use the memory-efficient iterator ...
IteratingAtomContainerReader(File input)
Constructs an iterator over the containers that can be found in the given file.
List< IAtomContainer > results
The containers loaded in memory, or null if we use an Iterator.
void close()
Close the memory-efficient iterator if any is open.
boolean usingIteratingReader
Flag indicating whether we are using a memory-efficient iterator (true of id we have had to pre-load ...
Class for recognizing file containing a list of SMILES .