📄 cifreader.java
字号:
/* $RCSfile$ * $Author: egonw $ * $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $ * $Revision: 7636 $ * * Copyright (C) 2003-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 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.StringTokenizer;import javax.vecmath.Point3d;import javax.vecmath.Vector3d;import org.openscience.cdk.interfaces.IAtom;import org.openscience.cdk.interfaces.IChemFile;import org.openscience.cdk.interfaces.IChemModel;import org.openscience.cdk.interfaces.IChemObject;import org.openscience.cdk.interfaces.IChemSequence;import org.openscience.cdk.interfaces.ICrystal;import org.openscience.cdk.exception.CDKException;import org.openscience.cdk.geometry.CrystalGeometryTools;import org.openscience.cdk.io.formats.CIFFormat;import org.openscience.cdk.io.formats.IResourceFormat;import org.openscience.cdk.tools.LoggingTool;/** * This is not a reader for the CIF and mmCIF crystallographic formats. * It is able, however, to extract some content from such files. * It's very ad hoc, not written * using any dictionary. So please complain if something is not working. * In addition, the things it does read are considered experimental. * * <p>The CIF example on the IUCR website has been tested, as well as Crambin (1CRN) * in the PDB database. * * @cdk.module io * * @cdk.keyword file format, CIF * @cdk.keyword file format, mmCIF * * @author E.L. Willighagen * @cdk.created 2003-10-12 */public class CIFReader extends DefaultChemObjectReader { private BufferedReader input; private LoggingTool logger; private ICrystal crystal = null; // cell parameters private double a = 0.0; private double b = 0.0; private double c = 0.0; private double alpha = 0.0; private double beta = 0.0; private double gamma = 0.0; /** * Create an CIF like file reader. * * @param input source of CIF data */ public CIFReader(Reader input) { this.input = new BufferedReader(input); this.logger = new LoggingTool(this); } public CIFReader(InputStream input) { this(new InputStreamReader(input)); } public CIFReader() { this(new StringReader("")); } public IResourceFormat getFormat() { return CIFFormat.getInstance(); } public void setReader(Reader reader) throws CDKException { this.input = new BufferedReader(input); } public void setReader(InputStream input) throws CDKException { setReader(new InputStreamReader(input)); } public boolean accepts(Class testClass) { Class[] interfaces = testClass.getInterfaces(); for (int i=0; i<interfaces.length; i++) { if (IChemFile.class.equals(interfaces[i])) return true; } return false; } /** * Read a ChemFile from input * * @return the content in a ChemFile object */ public IChemObject read(IChemObject object) throws CDKException { if (object instanceof IChemFile) { IChemFile cf = (IChemFile)object; try { cf = readChemFile(cf); } catch (IOException e) { logger.error("Input/Output error while reading from input."); } return cf; } else { throw new CDKException("Only supported is reading of ChemFile."); } } /** * Read the ShelX from input. Each ShelX document is expected to contain * one crystal structure. * * @return a ChemFile with the coordinates, charges, vectors, etc. */ private IChemFile readChemFile(IChemFile file) throws IOException { IChemSequence seq = file.getBuilder().newChemSequence(); IChemModel model = file.getBuilder().newChemModel(); crystal = file.getBuilder().newCrystal(); String line = input.readLine(); boolean end_found = false; while (input.ready() && line != null && !end_found) { if (line.startsWith("#")) { logger.warn("Skipping comment: " + line); // skip comment lines } else if (line.length() == 0) { logger.debug("Skipping empty line"); // skip empty lines } else if (!(line.startsWith("_") || line.startsWith("loop_"))) { logger.warn("Skipping unrecognized line: " + line); // skip line } else { /* determine CIF command */ String command = ""; int spaceIndex = line.indexOf(" "); if (spaceIndex != -1) { // everything upto space is command try { command = new String(line.substring(0, spaceIndex)); } catch (StringIndexOutOfBoundsException sioobe) { // disregard this line break; } } else { // complete line is command command = line; } logger.debug("command: " + command); if (command.startsWith("_cell")) { processCellParameter(command, line); } else if (command.equals("loop_")) { processLoopBlock(); } else if (command.equals("_symmetry_space_group_name_H-M")) { String value = line.substring(29).trim(); crystal.setSpaceGroup(value); } else { // skip command logger.warn("Skipping command: " + command); line = input.readLine(); if (line.startsWith(";")) { logger.debug("Skipping block content"); line = input.readLine().trim(); while (!line.equals(";")) { line = input.readLine().trim(); logger.debug("Skipping block line: " + line); } line = input.readLine(); } } } line = input.readLine(); } logger.info("Adding crystal to file with #atoms: " + crystal.getAtomCount()); model.setCrystal(crystal); seq.addChemModel(model); file.addChemSequence(seq); return file; } private void processCellParameter(String command, String line) { command = command.substring(6); // skip the "_cell." part if (command.equals("length_a")) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -