📄 mdlv3000reader.java
字号:
/* $Revision: 7636 $ $Author: egonw $ $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $ * * Copyright (C) 2006-2007 Egon Willighagen <egonw@sci.kun.nl> * * Contact: cdk-devel@lists.sourceforge.net * * This library 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. * * This library 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 library; 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 java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import java.io.StringReader;import java.util.Enumeration;import java.util.Hashtable;import java.util.StringTokenizer;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.vecmath.Point2d;import javax.vecmath.Point3d;import org.openscience.cdk.interfaces.IAtom;import org.openscience.cdk.interfaces.IAtomContainer;import org.openscience.cdk.interfaces.IBond;import org.openscience.cdk.interfaces.IChemObjectBuilder;import org.openscience.cdk.CDKConstants;import org.openscience.cdk.interfaces.IChemObject;import org.openscience.cdk.interfaces.IMolecule;import org.openscience.cdk.interfaces.IPseudoAtom;import org.openscience.cdk.config.IsotopeFactory;import org.openscience.cdk.exception.CDKException;import org.openscience.cdk.io.formats.IResourceFormat;import org.openscience.cdk.io.formats.MDLV3000Format;import org.openscience.cdk.io.setting.IOSetting;import org.openscience.cdk.tools.LoggingTool;/** * Class that implements the MDL mol V3000 format. This reader reads the * element symbol and 2D or 3D coordinates from the ATOM block. * * @cdk.module io * * @author Egon Willighagen <egonw@users.sf.net> * @cdk.created 2006 * * @cdk.keyword MDL RXN V3000 * @cdk.require java1.4+ */public class MDLV3000Reader extends DefaultChemObjectReader { BufferedReader input = null; private LoggingTool logger = null; private Pattern keyValueTuple; private Pattern keyValueTuple2; public MDLV3000Reader(Reader in) { logger = new LoggingTool(this); input = new BufferedReader(in); initIOSettings(); /* compile patterns */ keyValueTuple = Pattern.compile("\\s*(\\w+)=([^\\s]*)(.*)"); // e.g. CHG=-1 keyValueTuple2 = Pattern.compile("\\s*(\\w+)=\\(([^\\)]*)\\)(.*)"); // e.g. ATOMS=(1 31) } public MDLV3000Reader(InputStream input) { this(new InputStreamReader(input)); } public MDLV3000Reader() { this(new StringReader("")); } public IResourceFormat getFormat() { return MDLV3000Format.getInstance(); } public void setReader(Reader input) throws CDKException { if (input instanceof BufferedReader) { this.input = (BufferedReader)input; } else { this.input = new BufferedReader(input); } } public void setReader(InputStream input) throws CDKException { setReader(new InputStreamReader(input)); } 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; } return false; } public IChemObject read(IChemObject object) throws CDKException { if (object instanceof IMolecule) { return readMolecule(object.getBuilder()); } return null; } public IMolecule readMolecule(IChemObjectBuilder builder) throws CDKException { return builder.newMolecule(readConnectionTable(builder)); } public IAtomContainer readConnectionTable(IChemObjectBuilder builder) throws CDKException { IAtomContainer readData = builder.newAtomContainer(); boolean foundEND = false; readHeader(readData); while (isReady() && !foundEND) { String command = readCommand(); if ("END CTAB".equals(command)) { foundEND = true; } else if ("BEGIN CTAB".equals(command)) { // that's fine } else if ("COUNTS".equals(command)) { // don't think I need to parse this } else if ("BEGIN ATOM".equals(command)) { readAtomBlock(readData); } else if ("BEGIN BOND".equals(command)) { readBondBlock(readData); } else if ("BEGIN SGROUP".equals(command)) { readSGroup(readData); } else { logger.warn("Unrecognized command: " + command); } } return readData; } public void readHeader(IAtomContainer readData) throws CDKException { // read four lines String line1 = readLine(); if (line1.length() > 0) readData.setProperty(CDKConstants.TITLE, line1); readLine(); String line3 = readLine(); if (line3.length() > 0) readData.setProperty(CDKConstants.COMMENT, line3); readLine(); } /** * Reads the atoms, coordinates and charges. * * <p>IMPORTANT: it does not support the atom list and its negation! */ public void readAtomBlock(IAtomContainer readData) throws CDKException { boolean foundEND = false; while (isReady() && !foundEND) { String command = readCommand(); if ("END ATOM".equals(command)) { // FIXME: should check wether 3D is really 2D foundEND = true; } else { logger.debug("Parsing atom from: " + command); IAtom atom = readData.getBuilder().newAtom(); StringTokenizer tokenizer = new StringTokenizer(command); // parse the index try { atom.setID(tokenizer.nextToken()); } catch (Exception exception) { String error = "Error while parsing atom index"; logger.error(error); logger.debug(exception); throw new CDKException(error, exception); } // parse the element String element = tokenizer.nextToken(); boolean isElement = false; try { isElement = IsotopeFactory.getInstance(atom.getBuilder()).isElement(element); } catch (Exception exception) { throw new CDKException("Could not determine if element exists!", exception); } if (isPseudoAtom(element)) { atom = readData.getBuilder().newPseudoAtom(atom); } else if (isElement) { atom.setSymbol(element); } else { String error = "Cannot parse element of type: " + element; logger.error(error); throw new CDKException("(Possible CDK bug) " + error); } // parse atom coordinates (in Angstrom) try { String xString = tokenizer.nextToken(); String yString = tokenizer.nextToken(); String zString = tokenizer.nextToken(); double x = Double.parseDouble(xString); double y = Double.parseDouble(yString); double z = Double.parseDouble(zString); atom.setPoint3d(new Point3d(x, y, z)); atom.setPoint2d(new Point2d(x, y)); // FIXME: dirty! } catch (Exception exception) { String error = "Error while parsing atom coordinates"; logger.error(error); logger.debug(exception); throw new CDKException(error, exception); } // atom-atom mapping String mapping = tokenizer.nextToken(); if (!mapping.equals("0")) { logger.warn("Skipping atom-atom mapping: " + mapping); } // else: default 0 is no mapping defined // the rest are key value things if (command.indexOf("=") != -1) { Hashtable options = parseOptions(exhaustStringTokenizer(tokenizer)); Enumeration keys = options.keys(); while (keys.hasMoreElements()) { String key = (String)keys.nextElement(); String value = (String)options.get(key); try { if (key.equals("CHG")) { int charge = Integer.parseInt(value); if (charge != 0) { // zero is no charge specified atom.setFormalCharge(charge); } } else { logger.warn("Not parsing key: " + key); } } catch (Exception exception) { String error = "Error while parsing key/value " + key + "=" + value + ": " + exception.getMessage(); logger.error(error); logger.debug(exception); throw new CDKException(error, exception); } } } // store atom readData.addAtom(atom); logger.debug("Added atom: " + atom); } } } /** * Reads the bond atoms, order and stereo configuration. */ public void readBondBlock(IAtomContainer readData) throws CDKException { boolean foundEND = false; while (isReady() && !foundEND) { String command = readCommand(); if ("END BOND".equals(command)) { foundEND = true; } else { logger.debug("Parsing bond from: " + command); StringTokenizer tokenizer = new StringTokenizer(command); IBond bond = readData.getBuilder().newBond(); // parse the index try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -