/** 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; }
@Test public void checkUniqueMolecularDescriptorNames() throws Exception { DescriptorEngine engine = new DescriptorEngine(DescriptorEngine.MOLECULAR); List<DescriptorSpecification> specs = engine.getDescriptorSpecifications(); // we work with a simple molecule with 3D coordinates String filename = "data/mdl/lobtest2.sdf"; InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); ISimpleChemObjectReader reader = new MDLV2000Reader(ins); ChemFile content = (ChemFile) reader.read(new ChemFile()); List cList = ChemFileManipulator.getAllAtomContainers(content); IAtomContainer ac = (IAtomContainer) cList.get(0); AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(ac); engine.process(ac); int ncalc = 0; List<String> descNames = new ArrayList<String>(); for (DescriptorSpecification spec : specs) { DescriptorValue value = (DescriptorValue) ac.getProperty(spec); if (value == null) continue; ncalc++; String[] names = value.getNames(); descNames.addAll(Arrays.asList(names)); } List<String> dups = new ArrayList<String>(); Set<String> uniqueNames = new HashSet<String>(); for (String name : descNames) { if (!uniqueNames.add(name)) dups.add(name); } Assert.assertEquals(specs.size(), ncalc); Assert.assertEquals(descNames.size(), uniqueNames.size()); if (dups.size() != 0) { System.out.println("Following names were duplicated"); for (String dup : dups) { System.out.println("dup = " + dup); } } }
/** * Returns an IChemModel, using the reader provided (picked). * * @param cor * @param panel * @return * @throws CDKException */ public static IChemModel getChemModelFromReader( final ISimpleChemObjectReader cor, final AbstractJChemPaintPanel panel) throws CDKException { panel.get2DHub().setRGroupHandler(null); String error = null; ChemModel chemModel = null; IChemFile chemFile = null; if (cor.accepts(IChemFile.class) && chemModel == null) { // try to read a ChemFile try { chemFile = (IChemFile) cor.read((IChemObject) new ChemFile()); if (chemFile == null) { error = "The object chemFile was empty unexpectedly!"; } } catch (final Exception exception) { error = "Error while reading file: " + exception.getMessage(); exception.printStackTrace(); } } if (error != null) { throw new CDKException(error); } if (chemModel == null && chemFile != null) { chemModel = (ChemModel) chemFile.getChemSequence(0).getChemModel(0); } if (cor.accepts(ChemModel.class) && chemModel == null) { // try to read a ChemModel try { chemModel = (ChemModel) cor.read((IChemObject) new ChemModel()); if (chemModel == null) { error = "The object chemModel was empty unexpectedly!"; } } catch (final Exception exception) { error = "Error while reading file: " + exception.getMessage(); exception.printStackTrace(); } } // Smiles reading if (cor.accepts(AtomContainerSet.class) && chemModel == null) { // try to read a AtomContainer set try { final IAtomContainerSet som = cor.read(new AtomContainerSet()); chemModel = new ChemModel(); chemModel.setMoleculeSet(som); if (chemModel == null) { error = "The object chemModel was empty unexpectedly!"; } } catch (final Exception exception) { error = "Error while reading file: " + exception.getMessage(); exception.printStackTrace(); } } // MDLV3000 reading if (cor.accepts(AtomContainer.class) && chemModel == null) { // try to read a AtomContainer final IAtomContainer mol = cor.read(new AtomContainer()); if (mol != null) { try { final IAtomContainerSet newSet = new AtomContainerSet(); newSet.addAtomContainer(mol); chemModel = new ChemModel(); chemModel.setMoleculeSet(newSet); if (chemModel == null) { error = "The object chemModel was empty unexpectedly!"; } } catch (final Exception exception) { error = "Error while reading file: " + exception.getMessage(); exception.printStackTrace(); } } } // RGroupQuery reading if (cor.accepts(RGroupQuery.class) && chemModel == null) { final IRGroupQuery rgroupQuery = cor.read(new RGroupQuery()); if (rgroupQuery != null) { try { chemModel = new ChemModel(); final RGroupHandler rgHandler = new RGroupHandler( rgroupQuery); panel.get2DHub().setRGroupHandler(rgHandler); chemModel.setMoleculeSet(rgHandler .getMoleculeSet(chemModel)); rgHandler.layoutRgroup(); } catch (final Exception exception) { error = "Error while reading file: " + exception.getMessage(); exception.printStackTrace(); } } } if (error != null) { throw new CDKException(error); } if (chemModel == null && chemFile != null) { chemModel = (ChemModel) chemFile.getChemSequence(0).getChemModel(0); } // SmilesParser sets valencies, switch off by default. if (cor instanceof SMILESReader) { final IAtomContainer allinone = JChemPaintPanel .getAllAtomContainersInOne(chemModel); for (int k = 0; k < allinone.getAtomCount(); k++) { allinone.getAtom(k).setValency(null); } } return chemModel; }
/** * Loads one or more files into IAtomContainer objects. * * <p>This method does not need knowledge of the format since it is autodetected. Note that if * aromaticity detection or atom typing is specified and fails for a specific molecule, that * molecule will be set to <i>null</i> * * @param filenames An array of String's containing the filenames of the structures we want to * load * @param doAromaticity If true, then aromaticity perception is performed * @param doTyping If true, atom typing and configuration is performed. This will use the internal * CDK atom typing scheme * @return An array of AtoContainer's * @throws CDKException if there is an error when reading a file */ public static IAtomContainer[] loadMolecules( String[] filenames, boolean doAromaticity, boolean doTyping, boolean doIsotopes) throws CDKException, IOException { Vector<IAtomContainer> v = new Vector<IAtomContainer>(); IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance(); try { int i; int j; for (i = 0; i < filenames.length; i++) { File input = new File(filenames[i]); ReaderFactory readerFactory = new ReaderFactory(); ISimpleChemObjectReader reader = readerFactory.createReader(new FileReader(input)); if (reader == null) { // see if it's a SMI file if (filenames[i].endsWith(".smi")) { reader = new SMILESReader(new FileReader(input)); } } IChemFile content = (IChemFile) reader.read(builder.newInstance(IChemFile.class)); if (content == null) continue; List<IAtomContainer> c = ChemFileManipulator.getAllAtomContainers(content); // we should do this loop in case we have files // that contain multiple molecules v.addAll(c); } } catch (Exception e) { e.printStackTrace(); throw new CDKException(e.toString()); } // convert the vector to a simple array IAtomContainer[] retValues = new IAtomContainer[v.size()]; for (int i = 0; i < v.size(); i++) { retValues[i] = v.get(i); } // before returning, lets make see if we // need to perceive aromaticity and atom typing if (doAromaticity) { for (int i = 0; i < retValues.length; i++) { try { CDKHueckelAromaticityDetector.detectAromaticity(retValues[i]); } catch (CDKException e) { retValues[i] = null; } } } if (doTyping) { for (int i = 0; i < retValues.length; i++) { try { AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(retValues[i]); } catch (CDKException e) { retValues[i] = null; } } } if (doIsotopes) { IsotopeFactory ifac = IsotopeFactory.getInstance(DefaultChemObjectBuilder.getInstance()); for (IAtomContainer retValue : retValues) { ifac.configureAtoms(retValue); } } return retValues; }