📄 convertor.java
字号:
/* $Revision: 10144 $ $Author: egonw $ $Date: 2008-02-16 22:38:46 +0100 (Sat, 16 Feb 2008) $ * * Copyright (C) 2005-2007 Stefan Kuhn <shk3@users.sf.net> * * Contact: jchempaint-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.libio.cml;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OptionalDataException;import java.util.Enumeration;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.Map;import org.openscience.cdk.CDKConstants;import org.openscience.cdk.Monomer;import org.openscience.cdk.Strand;import org.openscience.cdk.config.Elements;import org.openscience.cdk.config.IsotopeFactory;import org.openscience.cdk.dict.DictRef;import org.openscience.cdk.dict.DictionaryDatabase;import org.openscience.cdk.geometry.CrystalGeometryTools;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.IChemSequence;import org.openscience.cdk.interfaces.ICrystal;import org.openscience.cdk.interfaces.IIsotope;import org.openscience.cdk.interfaces.IMolecule;import org.openscience.cdk.interfaces.IMoleculeSet;import org.openscience.cdk.interfaces.IPseudoAtom;import org.openscience.cdk.interfaces.IReaction;import org.openscience.cdk.interfaces.IReactionSet;import org.openscience.cdk.protein.data.PDBPolymer;import org.openscience.cdk.tools.IDCreator;import org.openscience.cdk.tools.LoggingTool;import org.xmlcml.cml.base.CMLElement;import org.xmlcml.cml.base.CMLException;import org.xmlcml.cml.element.CMLAtom;import org.xmlcml.cml.element.CMLBond;import org.xmlcml.cml.element.CMLBondStereo;import org.xmlcml.cml.element.CMLBondType;import org.xmlcml.cml.element.CMLCml;import org.xmlcml.cml.element.CMLCrystal;import org.xmlcml.cml.element.CMLIdentifier;import org.xmlcml.cml.element.CMLList;import org.xmlcml.cml.element.CMLMolecule;import org.xmlcml.cml.element.CMLProduct;import org.xmlcml.cml.element.CMLProductList;import org.xmlcml.cml.element.CMLReactant;import org.xmlcml.cml.element.CMLReactantList;import org.xmlcml.cml.element.CMLReaction;import org.xmlcml.cml.element.CMLReactionList;import org.xmlcml.cml.element.CMLScalar;import org.xmlcml.cml.element.CMLSubstance;import org.xmlcml.cml.element.CMLSubstanceList;/** * @cdk.module libio-cml * @cdk.keyword CML * @cdk.keyword class convertor * @cdk.builddepends jumbo52.jar * @cdk.require java1.5+ * @cdk.require jumbo52 */public class Convertor { public final static String NS_CML = "http://www.xml-cml.org/schema"; private LoggingTool logger; private final static String CUSTOMIZERS_LIST = "libio-cml-customizers.set"; private static Map customizers = null; private boolean useCMLIDs; private String prefix; /** * Constructs a CML convertor. * * @param useCMLIDs Uses object IDs like 'a1' instead of 'a<hash>'. * @param prefix Namespace prefix to use. If null, then no prefix is used; */ public Convertor(boolean useCMLIDs, String prefix) { logger = new LoggingTool(this); this.useCMLIDs = useCMLIDs; this.prefix = prefix; setupCustomizers(); } public void registerCustomizer(ICMLCustomizer customizer) { if (customizers == null) customizers = new HashMap(); if (!customizers.containsKey(customizer.getClass().getName())) { customizers.put(customizer.getClass().getName(), customizer); logger.info("Loaded Customizer: ", customizer.getClass().getName()); } else { logger.warn("Duplicate attempt to register a customizer"); } } private void setupCustomizers() { if (customizers == null) customizers = new HashMap(); try { logger.debug("Starting loading Customizers..."); BufferedReader reader = new BufferedReader(new InputStreamReader( this.getClass().getClassLoader().getResourceAsStream(CUSTOMIZERS_LIST) )); int customizerCount = 0; while (reader.ready()) { // load them one by one String customizerName = reader.readLine(); customizerCount++; if (customizers.containsKey(customizerName)) { try { ICMLCustomizer customizer = (ICMLCustomizer) this.getClass().getClassLoader(). loadClass(customizerName).newInstance(); customizers.put(customizer.getClass().getName(), customizer); logger.info("Loaded Customizer: ", customizer.getClass().getName()); } catch (ClassNotFoundException exception) { logger.info("Could not find this Customizer: ", customizerName); logger.debug(exception); } catch (Exception exception) { logger.warn("Could not load this Customizer: ", customizerName); logger.warn(exception.getMessage()); logger.debug(exception); } } else { logger.warn("Duplicate attempt to register a customizer"); } } logger.info("Number of loaded customizers: ", customizerCount); } catch (Exception exception) { logger.error("Could not load this list: ", CUSTOMIZERS_LIST); logger.debug(exception); } } public CMLCml cdkChemFileToCMLList(IChemFile file) { return cdkChemFileToCMLList(file, true); } private CMLCml cdkChemFileToCMLList(IChemFile file, boolean setIDs) { CMLCml cmlList = new CMLCml(); cmlList.setDictRef("cdk:document"); if (useCMLIDs && setIDs) { IDCreator.createIDs(file); } if (file.getChemSequenceCount() > 0) { java.util.Iterator sequences = file.chemSequences(); while (sequences.hasNext()) { cmlList.appendChild(cdkChemSequenceToCMLList((IChemSequence)sequences.next())); } } return cmlList; } public CMLList cdkChemSequenceToCMLList(IChemSequence sequence) { return cdkChemSequenceToCMLList(sequence, true); } private CMLList cdkChemSequenceToCMLList(IChemSequence sequence, boolean setIDs) { CMLList cmlList = new CMLList(); cmlList.setDictRef("cdk:sequence"); if (useCMLIDs && setIDs) { IDCreator.createIDs(sequence); } if (sequence.getChemModelCount() > 0) { for (int i = 0; i < sequence.getChemModelCount(); i++) { cmlList.appendChild(cdkChemModelToCMLList(sequence.getChemModel(i))); } } return cmlList; } public CMLList cdkChemModelToCMLList(IChemModel model) { return cdkChemModelToCMLList(model, true); } private CMLList cdkChemModelToCMLList(IChemModel model, boolean setIDs) { CMLList cmlList = new CMLList(); cmlList.setDictRef("cdk:model"); if (useCMLIDs && setIDs) { IDCreator.createIDs(model); } if (model.getCrystal() != null) { cmlList.appendChild(cdkCrystalToCMLMolecule(model.getCrystal(), false)); } if (model.getReactionSet() != null) { cmlList.appendChild(cdkReactionSetToCMLReactionList(model.getReactionSet(), false)); } if (model.getMoleculeSet() != null) { cmlList.appendChild(cdkMoleculeSetToCMLList(model.getMoleculeSet(), false)); } return cmlList; } public CMLReactionList cdkReactionSetToCMLReactionList(IReactionSet reactionSet) { return cdkReactionSetToCMLReactionList(reactionSet, true); } private CMLReactionList cdkReactionSetToCMLReactionList(IReactionSet reactionSet, boolean setIDs) { CMLReactionList reactionList = new CMLReactionList(); if (useCMLIDs && setIDs) { IDCreator.createIDs(reactionSet); } java.util.Iterator reactionIter = reactionSet.reactions(); while (reactionIter.hasNext()) { reactionList.appendChild(cdkReactionToCMLReaction((IReaction)reactionIter.next(), false)); } return reactionList; } public CMLList cdkMoleculeSetToCMLList(IMoleculeSet moleculeSet) { return cdkMoleculeSetToCMLList(moleculeSet, true); } private CMLList cdkMoleculeSetToCMLList(IMoleculeSet moleculeSet, boolean setIDs) { CMLList cmlList = new CMLList(); cmlList.setDictRef("cdk:moleculeSet"); if (useCMLIDs && setIDs) { IDCreator.createIDs(moleculeSet); } for (int i = 0; i < moleculeSet.getAtomContainerCount(); i++) { cmlList.appendChild(cdkMoleculeToCMLMolecule(moleculeSet.getMolecule(i), false)); } return cmlList; } public CMLReaction cdkReactionToCMLReaction(IReaction reaction) { return cdkReactionToCMLReaction(reaction, true); } private CMLReaction cdkReactionToCMLReaction(IReaction reaction, boolean setIDs) { CMLReaction cmlReaction = new CMLReaction(); if (useCMLIDs && setIDs) { IDCreator.createIDs(reaction); } // reactants CMLReactantList cmlReactants = new CMLReactantList(); java.util.Iterator reactants = reaction.getReactants().molecules(); while (reactants.hasNext()) { CMLReactant cmlReactant = new CMLReactant(); cmlReactant.addMolecule(cdkMoleculeToCMLMolecule((IMolecule)reactants.next(), false)); cmlReactants.addReactant(cmlReactant); } // products CMLProductList cmlProducts = new CMLProductList(); java.util.Iterator products = reaction.getProducts().molecules(); while (products.hasNext()) { CMLProduct cmlProduct = new CMLProduct(); cmlProduct.addMolecule(cdkMoleculeToCMLMolecule((IMolecule)products.next(), false)); cmlProducts.addProduct(cmlProduct); } // substance CMLSubstanceList cmlSubstances = new CMLSubstanceList(); java.util.Iterator substance = reaction.getAgents().molecules(); while (substance.hasNext()) { CMLSubstance cmlSubstance = new CMLSubstance(); cmlSubstance.addMolecule(cdkMoleculeToCMLMolecule((IMolecule)substance.next(), false)); cmlSubstances.addSubstance(cmlSubstance); } if (reaction.getID() != null) cmlReaction.setId(reaction.getID()); cmlReaction.addReactantList(cmlReactants); cmlReaction.addProductList(cmlProducts); cmlReaction.addSubstanceList(cmlSubstances); return cmlReaction; } public CMLMolecule cdkCrystalToCMLMolecule(ICrystal crystal) { return cdkCrystalToCMLMolecule(crystal, true); } private CMLMolecule cdkCrystalToCMLMolecule(ICrystal crystal, boolean setIDs) { CMLMolecule molecule = cdkAtomContainerToCMLMolecule(crystal, false); CMLCrystal cmlCrystal = new CMLCrystal(); if (useCMLIDs && setIDs) { IDCreator.createIDs(crystal); } this.checkPrefix(cmlCrystal); cmlCrystal.setZ(crystal.getZ()); double[] params = CrystalGeometryTools.cartesianToNotional( crystal.getA(), crystal.getB(), crystal.getC() ); logger.debug("Number of cell params: ", params.length); try { cmlCrystal.setCellParameters(params); } catch (CMLException exception) { logger.error("Could not set crystal cell parameters!"); } molecule.addCrystal(cmlCrystal); return molecule; } public CMLMolecule cdkPDBPolymerToCMLMolecule(PDBPolymer pdbPolymer) { return cdkPDBPolymerToCMLMolecule(pdbPolymer, true); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -