📄 pdbreader.java
字号:
/* $Revision: 8242 $ $Author: egonw $ $Date: 2007-04-22 22:49:31 +0200 (Sun, 22 Apr 2007) $ * * 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 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.Map;import javax.vecmath.Point3d;import org.openscience.cdk.CDKConstants;import org.openscience.cdk.config.AtomTypeFactory;import org.openscience.cdk.exception.CDKException;import org.openscience.cdk.exception.NoSuchAtomTypeException;import org.openscience.cdk.graph.rebond.RebondTool;import org.openscience.cdk.interfaces.IAtom;import org.openscience.cdk.interfaces.IAtomType;import org.openscience.cdk.interfaces.IBioPolymer;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.IMolecule;import org.openscience.cdk.interfaces.IMoleculeSet;import org.openscience.cdk.interfaces.IMonomer;import org.openscience.cdk.interfaces.IStrand;import org.openscience.cdk.io.formats.IResourceFormat;import org.openscience.cdk.io.formats.PDBFormat;import org.openscience.cdk.io.setting.BooleanIOSetting;import org.openscience.cdk.io.setting.IOSetting;import org.openscience.cdk.protein.data.PDBAtom;import org.openscience.cdk.protein.data.PDBMonomer;import org.openscience.cdk.protein.data.PDBPolymer;import org.openscience.cdk.protein.data.PDBStrand;import org.openscience.cdk.protein.data.PDBStructure;import org.openscience.cdk.tools.LoggingTool;import org.openscience.cdk.tools.manipulator.AtomTypeManipulator;/** * Reads the contents of a PDBFile. * * <p>A description can be found at <a href="http://www.rcsb.org/pdb/static.do?p=file_formats/pdb/index.html"> * http://www.rcsb.org/pdb/static.do?p=file_formats/pdb/index.html</a>. * * @cdk.module pdb * * @author Edgar Luttmann * @author Bradley Smith <bradley@baysmith.com> * @author Martin Eklund <martin.eklund@farmbio.uu.se> * @author Ola Spjuth <ola.spjuth@farmbio.uu.se> * @cdk.created 2001-08-06 * @cdk.keyword file format, PDB */public class PDBReader extends DefaultChemObjectReader { private LoggingTool logger; private BufferedReader _oInput; // The internal used BufferedReader private BooleanIOSetting useRebondTool; private BooleanIOSetting readConnect; private Map atomNumberMap; private static AtomTypeFactory pdbFactory; /** * * Contructs a new PDBReader that can read Molecules from a given * InputStream. * * @param oIn The InputStream to read from * */ public PDBReader(InputStream oIn) { this(new InputStreamReader(oIn)); } /** * * Contructs a new PDBReader that can read Molecules from a given * Reader. * * @param oIn The Reader to read from * */ public PDBReader(Reader oIn) { logger = new LoggingTool(this.getClass()); _oInput = new BufferedReader(oIn); initIOSettings(); pdbFactory = null; } public PDBReader() { this(new StringReader("")); } public IResourceFormat getFormat() { return PDBFormat.getInstance(); } public void setReader(Reader input) throws CDKException { if (input instanceof BufferedReader) { this._oInput = (BufferedReader)input; } else { this._oInput = 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; } /** * * Takes an object which subclasses IChemObject, e.g. Molecule, and will * read this (from file, database, internet etc). If the specific * implementation does not support a specific IChemObject it will throw * an Exception. * * @param oObj The object that subclasses IChemObject * @return The IChemObject read * @exception CDKException * */ public IChemObject read(IChemObject oObj) throws CDKException { if (oObj instanceof IChemFile) { if (pdbFactory == null) { try { pdbFactory = AtomTypeFactory.getInstance("org/openscience/cdk/config/data/pdb_atomtypes.xml", ((IChemFile)oObj).getBuilder()); } catch (Exception exception) { logger.debug(exception); throw new CDKException("Could not setup list of PDB atom types! " + exception.getMessage()); } } return readChemFile((IChemFile)oObj); } else { throw new CDKException("Only supported is reading of ChemFile objects."); } } /** * Read a <code>ChemFile</code> from a file in PDB format. The molecules * in the file are stored as <code>BioPolymer</code>s in the * <code>ChemFile</code>. The residues are the monomers of the * <code>BioPolymer</code>, and their names are the concatenation of the * residue, chain id, and the sequence number. Separate chains (denoted by * TER records) are stored as separate <code>BioPolymer</code> molecules. * * <p>Connectivity information is not currently read. * * @return The ChemFile that was read from the PDB file. */ private IChemFile readChemFile(IChemFile oFile) { // initialize all containers IChemSequence oSeq = oFile.getBuilder().newChemSequence(); IChemModel oModel = oFile.getBuilder().newChemModel(); IMoleculeSet oSet = oFile.getBuilder().newMoleculeSet(); // some variables needed String cCol; PDBAtom oAtom; PDBPolymer oBP = new PDBPolymer(); IMolecule molecularStructure = oFile.getBuilder().newMolecule(); StringBuffer cResidue; String oObj; IMonomer oMonomer; String cRead = ""; char chain = 'A'; // To ensure stringent name giving of monomers IStrand oStrand; int lineLength = 0; boolean isProteinStructure = false; atomNumberMap = new Hashtable(); // do the reading of the Input try { do { cRead = _oInput.readLine(); logger.debug("Read line: ", cRead); if (cRead != null) { lineLength = cRead.length(); if (lineLength < 80) { logger.warn("Line is not of the expected length 80!"); } // make sure the record name is 6 characters long if (lineLength < 6) { cRead = cRead + " "; } // check the first column to decide what to do cCol = cRead.substring(0,6); if ("SEQRES".equalsIgnoreCase(cCol)) { isProteinStructure = true; } else if ("ATOM ".equalsIgnoreCase(cCol)) { // read an atom record oAtom = readAtom(cRead, lineLength); if (isProteinStructure) { // construct a string describing the residue cResidue = new StringBuffer(8); oObj = oAtom.getResName(); if (oObj != null) { cResidue = cResidue.append(oObj.trim()); } oObj = oAtom.getChainID(); if (oObj != null) { // cResidue = cResidue.append(((String)oObj).trim()); cResidue = cResidue.append(String.valueOf(chain)); } oObj = oAtom.getResSeq(); if (oObj != null) { cResidue = cResidue.append(oObj.trim()); } // search for an existing strand or create a new one. String strandName = oAtom.getChainID(); if (strandName == null || strandName.length() == 0) { strandName = String.valueOf(chain); } oStrand = oBP.getStrand(strandName); if (oStrand == null) { oStrand = new PDBStrand(); oStrand.setStrandName(strandName); oStrand.setID(String.valueOf(chain)); } // search for an existing monomer or create a new one. oMonomer = oBP.getMonomer(cResidue.toString(), String.valueOf(chain)); if (oMonomer == null) { PDBMonomer monomer = new PDBMonomer(); monomer.setMonomerName(cResidue.toString()); monomer.setMonomerType(oAtom.getResName()); monomer.setChainID(oAtom.getChainID()); monomer.setICode(oAtom.getICode()); monomer.setResSeq(oAtom.getResSeq()); oMonomer = monomer; } // add the atom oBP.addAtom(oAtom, oMonomer, oStrand); if (readConnect.isSet() && atomNumberMap.put(new Integer(oAtom.getSerial()), oAtom) != null) { logger.warn("Duplicate serial ID found for atom: ", oAtom); } } else { molecularStructure.addAtom(oAtom); } logger.debug("Added ATOM: ", oAtom); /** As HETATMs cannot be considered to either belong to a certain monomer or strand, * they are dealt with seperately.*/ } else if("HETATM".equalsIgnoreCase(cCol)) { // read an atom record oAtom = readAtom(cRead, lineLength); oAtom.setHetAtom(true); oBP.addAtom(oAtom); if (atomNumberMap.put(new Integer(oAtom.getSerial()), oAtom) != null) { logger.warn("Duplicate serial ID found for atom: ", oAtom); } logger.debug("Added HETATM: ", oAtom); } else if ("TER ".equalsIgnoreCase(cCol)) { // start new strand chain++; oStrand = new PDBStrand(); oStrand.setStrandName(String.valueOf(chain)); logger.debug("Added new STRAND"); } else if ("END ".equalsIgnoreCase(cCol)) { atomNumberMap.clear(); if (isProteinStructure) { // create bonds and finish the molecule if (useRebondTool.isSet()) { try { if(!createBondsWithRebondTool(oBP)) { // Get rid of all potentially created bonds. logger.info("Bonds could not be created using the RebondTool when PDB file was read."); oBP.removeAllBonds(); } } catch (Exception exception) { logger.info("Bonds could not be created when PDB file was read."); logger.debug(exception); } } oSet.addMolecule(oBP); } else { oSet.addMolecule(molecularStructure); } } else if (cCol.equals("MODEL ")) { // OK, start a new model and save the current one first *if* it contains atoms
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -