📄 mdlwriter.java
字号:
/* $RCSfile$ * $Author: egonw $ * $Date: 2007-08-28 10:29:51 +0200 (Tue, 28 Aug 2007) $ * $Revision: 8719 $ * * Copyright (C) 1997-2007 The Chemistry Development Kit (CDK) project * * Contact: cdk-devel@lists.sourceforge.net * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 * of the License, or (at your option) any later version. * All we ask is that proper credit is given for our work, which includes * - but is not limited to - adding the above copyright notice to the beginning * of your source code files, and to any copyright notice that you may distribute * with programs based on this work. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * */package org.openscience.cdk.io;import org.openscience.cdk.CDKConstants;import org.openscience.cdk.config.IsotopeFactory;import org.openscience.cdk.exception.CDKException;import org.openscience.cdk.interfaces.*;import org.openscience.cdk.io.formats.IResourceFormat;import org.openscience.cdk.io.formats.MDLFormat;import org.openscience.cdk.tools.LoggingTool;import org.openscience.cdk.tools.manipulator.ChemFileManipulator;import java.io.*;import java.text.NumberFormat;import java.text.SimpleDateFormat;import java.util.*;/** * Writes MDL mol files and SD files. * <BR><BR> * A MDL mol file contains a single molecule, whereas a MDL SD file contains * one or more molecules. This class is capable of writing both mol files and * SD files. The correct format is automatically chosen: * <ul> * <li>if {@link #write(IChemObject)} is called with a {@link org.openscience.cdk.MoleculeSet MoleculeSet} * as an argument a SD files is written</li> * <li>if one of the two writeMolecule methods (either {@link #writeMolecule(IMolecule) this one} or * {@link #writeMolecule(org.openscience.cdk.interfaces.IMolecule)} that one}) is called the first time, a mol file is written</li> * <li>if one of the two writeMolecule methods is called more than once the output is a SD file</li> * </ul> * * <p>Thus, to write several molecules to a single SD file you can either use {@link #write(IChemObject)} and pass * a {@link org.openscience.cdk.MoleculeSet MoleculeSet} or you can repeatedly call one of the two * writeMolecule methods. * <p>For writing a MDL molfile you can this code: * <pre> * MDLWriter writer = new MDLWriter(new FileWriter(new File("output.mol"))); * writer.write((Molecule)molecule); * writer.close(); * </pre> * * See {@cdk.cite DAL92}. * * @cdk.module io * @cdk.keyword file format, MDL molfile * @cdk.bug 1524466 */public class MDLWriter extends DefaultChemObjectWriter { private BufferedWriter writer; private LoggingTool logger; private int moleculeNumber; public Map sdFields=null; //private boolean writeAromatic=true; /** * Contructs a new MDLWriter that can write an array of * Molecules to a Writer. * * @param out The Writer to write to */ public MDLWriter(Writer out) { logger = new LoggingTool(this); try { if (out instanceof BufferedWriter) { writer = (BufferedWriter)out; } else { writer = new BufferedWriter(out); } } catch (Exception exc) { } this.moleculeNumber = 1; } /** * Contructs a new MDLWriter that can write an array of * Molecules to a given OutputStream. * * @param output The OutputStream to write to */ public MDLWriter(OutputStream output) { this(new OutputStreamWriter(output)); } public MDLWriter() { this(new StringWriter()); } public IResourceFormat getFormat() { return MDLFormat.getInstance(); } public void setWriter(Writer out) throws CDKException { if (out instanceof BufferedWriter) { writer = (BufferedWriter)out; } else { writer = new BufferedWriter(out); } } public void setWriter(OutputStream output) throws CDKException { setWriter(new OutputStreamWriter(output)); } /** * * Method does not do anything until now. * */ public void dontWriteAromatic(){ //writeAromatic=false; } /** * Here you can set a map which will be used to build sd fields in the file. * The entries will be translated to sd fields like this:<br> * > <key><br> * > value<br> * empty line<br> * * @param map The map to be used, map of String-String pairs */ public void setSdFields(Map map){ sdFields=map; } /** * Flushes the output and closes this object. */ public void close() throws IOException { writer.close(); } public boolean accepts(Class classObject) { Class[] interfaces = classObject.getInterfaces(); for (int i=0; i<interfaces.length; i++) { if (IMolecule.class.equals(interfaces[i])) return true; if (IChemFile.class.equals(interfaces[i])) return true; if (IChemModel.class.equals(interfaces[i])) return true; if (IMoleculeSet.class.equals(interfaces[i])) return true; } return false; } /** * Writes a IChemObject to the MDL molfile formated output. * It can only output ChemObjects of type ChemFile, Molecule and * MoleculeSet. * * @param object class must be of type ChemFile, Molecule or MoleculeSet. * * @see org.openscience.cdk.ChemFile */ public void write(IChemObject object) throws CDKException { try { if (object instanceof IMoleculeSet) { writeMoleculeSet((IMoleculeSet)object); return; } else if (object instanceof IChemFile) { writeChemFile((IChemFile)object); return; } else if (object instanceof IChemModel) { IChemFile file = object.getBuilder().newChemFile(); IChemSequence sequence = object.getBuilder().newChemSequence(); sequence.addChemModel((IChemModel)object); file.addChemSequence(sequence); writeChemFile((IChemFile)file); return; } else if (object instanceof IMolecule) { writeMolecule((IMolecule)object); return; } } catch (Exception ex) { logger.error(ex.getMessage()); logger.debug(ex); throw new CDKException("Exception while writing MDL file: " + ex.getMessage(), ex); } throw new CDKException("Only supported is writing of ChemFile, MoleculeSet, AtomContainer and Molecule objects."); } /** * Writes an array of Molecules to an OutputStream in MDL sdf format. * * @param som Array of Molecules that is written to an OutputStream */ private void writeMoleculeSet(IMoleculeSet som) { java.util.Iterator molecules = som.molecules(); while (molecules.hasNext()) { IMolecule mol = (IMolecule)molecules.next(); try { boolean[] isVisible=new boolean[mol.getAtomCount()]; for(int k=0;k<isVisible.length;k++){ isVisible[k]=true; } writeMolecule(mol); } catch (Exception exc) { } } } private void writeChemFile(IChemFile file) throws Exception { List moleculesList = ChemFileManipulator.getAllAtomContainers(file); for (int i=0; i<moleculesList.size(); i++) { writeMolecule(file.getBuilder().newMolecule((IAtomContainer)moleculesList.get(i))); } } /** * Writes a Molecule to an OutputStream in MDL sdf format. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -