/** * Read an IChemModel from a given file. * * @param file * @param type * @param panel * @return * @throws CDKException * @throws FileNotFoundException */ public static IChemModel readFromFile(final File file, final String type, final AbstractJChemPaintPanel panel) throws CDKException, FileNotFoundException { final String url = file.toURI().toString(); ISimpleChemObjectReader cor = null; try { cor = FileHandler.createReader(file.toURI().toURL(), url, type); } catch (final MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (cor instanceof CMLReader) { cor.setReader(new FileInputStream(file)); // hack } else { cor.setReader(new FileReader(file)); // hack } final IChemModel chemModel = JChemPaint.getChemModelFromReader(cor, panel); boolean avoidOverlap = true; if (cor instanceof RGroupQueryReader) { avoidOverlap = false; } JChemPaint.cleanUpChemModel(chemModel, avoidOverlap, panel); return chemModel; }
/** Returns true if another IMolecule can be read. */ public boolean hasNext() { if (!nextAvailableIsKnown) { hasNext = false; // now try to parse the next Molecule try { if ((currentLine = input.readLine()) != null) { currentFormat = (IChemFormat) MDLFormat.getInstance(); StringBuffer buffer = new StringBuffer(); while (currentLine != null && !currentLine.equals("M END")) { // still in a molecule buffer.append(currentLine); buffer.append(System.getProperty("line.separator")); currentLine = input.readLine(); // do MDL molfile version checking if (currentLine.contains("V2000") || currentLine.contains("v2000")) { currentFormat = (IChemFormat) MDLV2000Format.getInstance(); } else if (currentLine.contains("V3000") || currentLine.contains("v3000")) { currentFormat = (IChemFormat) MDLV3000Format.getInstance(); } } buffer.append(currentLine); buffer.append(System.getProperty("line.separator")); logger.debug("MDL file part read: ", buffer); ISimpleChemObjectReader reader = factory.createReader(currentFormat); reader.setReader(new StringReader(buffer.toString())); if (currentFormat instanceof MDLV2000Format) { reader.addChemObjectIOListener(this); ((MDLV2000Reader) reader).customizeJob(); } nextMolecule = (IMolecule) reader.read(builder.newInstance(IMolecule.class)); // note that a molecule may have 0 atoms, but still // be useful (by having SD tags for example), so just // check for null'ness rather than atom count hasNext = nextMolecule != null; // now read the data part currentLine = input.readLine(); readDataBlockInto(nextMolecule); } else { hasNext = false; } } catch (Exception exception) { logger.error("Error while reading next molecule: " + exception.getMessage()); logger.debug(exception); hasNext = false; } if (!hasNext) nextMolecule = null; nextAvailableIsKnown = true; } return hasNext; }