📄 pmpreader.java
字号:
/* $Revision: 7636 $ $Author: egonw $ $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $ * * Copyright (C) 2004-2007 Egon Willighagen <egonw@users.sf.net> * * 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 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.Hashtable;import java.util.Iterator;import java.util.StringTokenizer;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.vecmath.Point3d;import javax.vecmath.Vector3d;import org.openscience.cdk.exception.CDKException;import org.openscience.cdk.graph.rebond.RebondTool;import org.openscience.cdk.interfaces.IAtom;import org.openscience.cdk.interfaces.IAtomContainer;import org.openscience.cdk.interfaces.IBond;import org.openscience.cdk.interfaces.IChemFile;import org.openscience.cdk.interfaces.IChemModel;import org.openscience.cdk.interfaces.IChemObject;import org.openscience.cdk.interfaces.IChemObjectBuilder;import org.openscience.cdk.interfaces.IChemSequence;import org.openscience.cdk.interfaces.ICrystal;import org.openscience.cdk.io.formats.IResourceFormat;import org.openscience.cdk.io.formats.PMPFormat;import org.openscience.cdk.tools.LoggingTool;/** * Reads an frames from a PMP formated input. * Both compilation and use of this class requires Java 1.4. * * @cdk.module io * * @cdk.keyword file format, Polymorph Predictor (tm) * * @author E.L. Willighagen * @cdk.require java1.4+ */public class PMPReader extends DefaultChemObjectReader { private static final String PMP_ZORDER = "ZOrder"; private static final String PMP_ID = "Id"; private BufferedReader input; private LoggingTool logger; /* Keep a copy of the PMP model */ private IAtomContainer modelStructure; private IChemObject chemObject; /* Keep an index of PMP id -> AtomCountainer id */ private Hashtable atomids = new Hashtable(); private Hashtable atomGivenIds = new Hashtable(); private Hashtable atomZOrders = new Hashtable(); private Hashtable bondids = new Hashtable(); private Hashtable bondAtomOnes = new Hashtable(); private Hashtable bondAtomTwos = new Hashtable(); private Hashtable bondOrders = new Hashtable(); /* Often used patterns */ Pattern objHeader; Pattern objCommand; Pattern atomTypePattern; int lineNumber = 0; int bondCounter = 0; private RebondTool rebonder; /* * construct a new reader from a Reader type object * * @param input reader from which input is read */ public PMPReader(Reader input) { this.input = new BufferedReader(input); logger = new LoggingTool(this); this.lineNumber = 0; /* compile patterns */ objHeader = Pattern.compile(".*\\((\\d+)\\s(\\w+)$"); objCommand = Pattern.compile(".*\\(A\\s(C|F|D|I|O)\\s(\\w+)\\s+\"?(.*?)\"?\\)$"); atomTypePattern = Pattern.compile("^(\\d+)\\s+(\\w+)$"); rebonder = new RebondTool(2.0, 0.5, 0.5); } public PMPReader(InputStream input) { this(new InputStreamReader(input)); } public PMPReader() { this(new StringReader("")); } public IResourceFormat getFormat() { return PMPFormat.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 (IChemFile.class.equals(interfaces[i])) return true; } return false; } /** * reads the content from a PMP input. It can only return a * IChemObject of type ChemFile * * @param object class must be of type ChemFile * * @see IChemFile */ public IChemObject read(IChemObject object) throws CDKException { if (object instanceof IChemFile) { return (IChemObject)readChemFile((IChemFile)object); } else { throw new CDKException("Only supported is reading of ChemFile objects."); } } // private procedures private String readLine() throws IOException { String line = input.readLine(); lineNumber = lineNumber + 1; logger.debug("LINE (" + lineNumber + "): ", line); return line; } /** * Private method that actually parses the input to read a ChemFile * object. * * Each PMP frame is stored as a Crystal in a ChemModel. The PMP * file is stored as a ChemSequence of ChemModels. * * @return A ChemFile containing the data parsed from input. */ private IChemFile readChemFile(IChemFile chemFile) { IChemSequence chemSequence = chemFile.getBuilder().newChemSequence(); IChemModel chemModel = chemFile.getBuilder().newChemModel(); ICrystal crystal = chemFile.getBuilder().newCrystal(); try { String line = readLine(); while (input.ready() && line != null) { if (line.startsWith("%%Header Start")) { // parse Header section while (input.ready() && line != null && !(line.startsWith("%%Header End"))) { if (line.startsWith("%%Version Number")) { String version = readLine().trim(); if (!version.equals("3.00")) { logger.error("The PMPReader only supports PMP files with version 3.00"); return null; } } line = readLine(); } } else if (line.startsWith("%%Model Start")) { // parse Model section modelStructure = chemFile.getBuilder().newAtomContainer(); while (input.ready() && line != null && !(line.startsWith("%%Model End"))) { Matcher objHeaderMatcher = objHeader.matcher(line); if (objHeaderMatcher.matches()) { String object = objHeaderMatcher.group(2); constructObject(chemFile.getBuilder(), object); int id = Integer.parseInt(objHeaderMatcher.group(1)); // logger.debug(object + " id: " + id); line = readLine(); while (input.ready() && line != null && !(line.trim().equals(")"))) { // parse object command (or new object header) Matcher objCommandMatcher = objCommand.matcher(line); objHeaderMatcher = objHeader.matcher(line); if (objHeaderMatcher.matches()) { // ok, forget about nesting and hope for the best object = objHeaderMatcher.group(2); id = Integer.parseInt(objHeaderMatcher.group(1)); constructObject(chemFile.getBuilder(), object); } else if (objCommandMatcher.matches()) { String format = objCommandMatcher.group(1); String command = objCommandMatcher.group(2); String field = objCommandMatcher.group(3); processModelCommand(object, command, format, field); } else { logger.warn("Skipping line: " + line); } line = readLine(); } if (chemObject instanceof IAtom) { atomids.put(new Integer(id), new Integer(modelStructure.getAtomCount())); atomZOrders.put(new Integer((String)chemObject.getProperty(PMP_ZORDER)), new Integer(id)); atomGivenIds.put(new Integer((String)chemObject.getProperty(PMP_ID)), new Integer(id)); modelStructure.addAtom((IAtom)chemObject);// } else if (chemObject instanceof IBond) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -