⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 convertor.java

📁 化学图形处理软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    private CMLMolecule cdkPDBPolymerToCMLMolecule(PDBPolymer pdbPolymer, boolean setIDs) {    	CMLMolecule cmlMolecule = new CMLMolecule();       	cmlMolecule.setConvention("PDB");       	cmlMolecule.setDictRef("pdb:model");       	       	Map mapS = pdbPolymer.getStrands();       	Iterator iter = mapS.keySet().iterator();        while (iter.hasNext()) {            Object key = iter.next();            Strand strand = (Strand) mapS.get(key);            Map mapM = strand.getMonomers();           	Iterator iterM = mapM.keySet().iterator();            while (iterM.hasNext()) {                Monomer monomer = (Monomer) mapM.get(iterM.next());                CMLMolecule clmono = cdkMonomerToCMLMolecule(monomer, true);               	cmlMolecule.appendChild(clmono);            }        }       	        return cmlMolecule;    }        public CMLMolecule cdkMonomerToCMLMolecule(Monomer monomer) {        return cdkMonomerToCMLMolecule(monomer, true);    }    private CMLMolecule cdkMonomerToCMLMolecule(Monomer monomer, boolean setIDs) {    	CMLMolecule cmlMolecule = new CMLMolecule();       	cmlMolecule.setDictRef("pdb:sequence");       	       	if (monomer.getMonomerName() != null)cmlMolecule.setId(monomer.getMonomerName());       	       	for(int i = 0 ; i < monomer.getAtomCount(); i++){       		IAtom cdkAtom = monomer.getAtom(i);            CMLAtom cmlAtom = cdkAtomToCMLAtom(monomer, cdkAtom);            if (monomer.getConnectedSingleElectronsCount(cdkAtom) > 0) {                cmlAtom.setSpinMultiplicity(monomer.getConnectedSingleElectronsCount(cdkAtom) + 1);            }            cmlMolecule.addAtom(cmlAtom, false);       	}        return cmlMolecule;    }    public CMLMolecule cdkMoleculeToCMLMolecule(IMolecule structure) {        return cdkMoleculeToCMLMolecule(structure, true);    }    private CMLMolecule cdkMoleculeToCMLMolecule(IMolecule structure, boolean setIDs) {        return cdkAtomContainerToCMLMolecule(structure, setIDs);    }    public CMLMolecule cdkAtomContainerToCMLMolecule(IAtomContainer structure) {        return cdkAtomContainerToCMLMolecule(structure, true);    }    private CMLMolecule cdkAtomContainerToCMLMolecule(IAtomContainer structure, boolean setIDs) {        CMLMolecule cmlMolecule = new CMLMolecule();        if (useCMLIDs && setIDs) {            IDCreator.createIDs(structure);        }        this.checkPrefix(cmlMolecule);        if (structure.getID() != null) cmlMolecule.setId(structure.getID());        if (structure.getProperty(CDKConstants.TITLE) != null) {            cmlMolecule.setTitle((String) structure.getProperty(CDKConstants.TITLE));        }        if (structure.getProperty(CDKConstants.INCHI) != null) {        	CMLIdentifier ident = new CMLIdentifier();        	ident.setConvention("iupac:inchi");        	ident.setCMLValue(structure.getProperty(CDKConstants.INCHI).toString());        	cmlMolecule.addIdentifier(ident);        }        for (int i = 0; i < structure.getAtomCount(); i++) {            IAtom cdkAtom = structure.getAtom(i);            CMLAtom cmlAtom = cdkAtomToCMLAtom(structure, cdkAtom);            if (structure.getConnectedSingleElectronsCount(cdkAtom) > 0) {                cmlAtom.setSpinMultiplicity(structure.getConnectedSingleElectronsCount(cdkAtom) + 1);            }            cmlMolecule.addAtom(cmlAtom, false);        }        for (int i = 0; i < structure.getBondCount(); i++) {            CMLBond cmlBond = cdkBondToCMLBond(structure.getBond(i));            cmlMolecule.addBond(cmlBond, true);        }                // ok, output molecular properties, but not TITLE, INCHI, or DictRef's        Map props = structure.getProperties();        Iterator keys = props.keySet().iterator();        while (keys.hasNext()) {        	Object key = keys.next();        	// but only if a String        	if (key instanceof String && props.get(key) instanceof String) {        		Object value = props.get(key);        		if (!key.toString().equals(CDKConstants.TITLE) &&        			!key.toString().equals(CDKConstants.INCHI)) {        			// ok, should output this        			CMLScalar scalar = new CMLScalar();                    this.checkPrefix(scalar);                    scalar.setDictRef("cdk:molecularProperty");                    scalar.setTitle(key.toString());                    scalar.setValue(value.toString());                    cmlMolecule.addScalar(scalar);        		}        	}        }        Iterator elements = customizers.keySet().iterator();        while (elements.hasNext()) {            ICMLCustomizer customizer = (ICMLCustomizer)customizers.get(elements.next());            try {                customizer.customize(structure, cmlMolecule);            } catch (Exception exception) {                logger.error("Error while customizing CML output with customizer: ",                        customizer.getClass().getName());                logger.debug(exception);            }        }        return cmlMolecule;    }    private boolean addDictRef(IChemObject object, CMLElement cmlElement) {        Hashtable properties = object.getProperties();        Iterator iter = properties.keySet().iterator();        while (iter.hasNext()) {            Object key = iter.next();            if (key instanceof String) {                String keyName = (String) key;                if (keyName.startsWith(DictionaryDatabase.DICTREFPROPERTYNAME)) {                    String dictRef = (String) properties.get(keyName);                    cmlElement.setProperty("dictRef", dictRef);                    return true;                }            }        }        return false;    }    private boolean addAtomID(IAtom cdkAtom, CMLAtom cmlAtom) {        if (cdkAtom.getID() != null && !cdkAtom.getID().equals("")) {            cmlAtom.setId(cdkAtom.getID());        } else {            cmlAtom.setId("a" + new Integer(cdkAtom.hashCode()).toString());        }        return true;    }    public CMLAtom cdkAtomToCMLAtom(IAtomContainer container, IAtom cdkAtom) {        CMLAtom cmlAtom = new CMLAtom();        this.checkPrefix(cmlAtom);        addAtomID(cdkAtom, cmlAtom);        addDictRef(cdkAtom, cmlAtom);        cmlAtom.setElementType(cdkAtom.getSymbol());        if (cdkAtom instanceof IPseudoAtom) {            String label = ((IPseudoAtom) cdkAtom).getLabel();            if (label != null) cmlAtom.setTitle(label);            cmlAtom.setElementType("Du");        }        map2DCoordsToCML(cmlAtom, cdkAtom);        map3DCoordsToCML(cmlAtom, cdkAtom);        mapFractionalCoordsToCML(cmlAtom, cdkAtom);        cmlAtom.setFormalCharge(cdkAtom.getFormalCharge());        // CML's hydrogen count consists of the sum of implicit and explicit        // hydrogens (see bug #1655045).        int totalHydrogen = cdkAtom.getHydrogenCount();        if (container != null) {        	Iterator bonds = container.getConnectedBondsList(cdkAtom).iterator();        	while (bonds.hasNext()) {        		Iterator atoms = ((IBond)bonds.next()).atoms();        		while (atoms.hasNext()) {        			IAtom atom=(IAtom)atoms.next();        			if (Elements.HYDROGEN.getSymbol().equals(atom.getSymbol()) && atom!=cdkAtom) totalHydrogen++;        		}        	}        } // else: it is the implicit hydrogen count        cmlAtom.setHydrogenCount(totalHydrogen);        int massNumber = cdkAtom.getMassNumber();        if (!(cdkAtom instanceof IPseudoAtom)) {            try {                IIsotope majorIsotope = IsotopeFactory.getInstance(cdkAtom.getBuilder()).getMajorIsotope(cdkAtom.getSymbol());                if (majorIsotope != null) {                    int majorMassNumber = majorIsotope.getMassNumber();                    if (massNumber != 0 && massNumber != majorMassNumber) {                        cmlAtom.setIsotope(massNumber);                    }                }            } catch (OptionalDataException e) {                logger.debug(e);            } catch (IOException e) {                logger.debug(e);            }        }        if (cdkAtom.getCharge() != 0.0) {            CMLScalar scalar = new CMLScalar();            this.checkPrefix(scalar);//            scalar.setDataType("xsd:float");            scalar.setDictRef("cdk:partialCharge");            scalar.setValue(cdkAtom.getCharge());            cmlAtom.addScalar(scalar);        }        writeProperties(cdkAtom, cmlAtom);        Iterator elements = customizers.keySet().iterator();        while (elements.hasNext()) {            ICMLCustomizer customizer = (ICMLCustomizer)customizers.get(elements.next());            try {                customizer.customize(cdkAtom, cmlAtom);            } catch (Exception exception) {                logger.error("Error while customizing CML output with customizer: ",                        customizer.getClass().getName());                logger.debug(exception);            }        }        return cmlAtom;    }    public CMLBond cdkBondToCMLBond(IBond cdkBond) {        CMLBond cmlBond = new CMLBond();        this.checkPrefix(cmlBond);        if (cdkBond.getID() == null || cdkBond.getID().length() == 0) {            cmlBond.setId("b" + cdkBond.hashCode());        } else {            cmlBond.setId(cdkBond.getID());        }        String[] atomRefArray = new String[cdkBond.getAtomCount()];        for (int i = 0; i < cdkBond.getAtomCount(); i++) {            String atomID = cdkBond.getAtom(i).getID();            if (atomID == null || atomID.length() == 0) {                atomRefArray[i] = "a" + new Integer(cdkBond.getAtom(i).hashCode()).toString();            } else {                atomRefArray[i] = atomID;            }        }        if (atomRefArray.length == 2) {            cmlBond.setAtomRefs2(atomRefArray);        } else {            cmlBond.setAtomRefs(atomRefArray);        }        double border = cdkBond.getOrder();        if (border == CDKConstants.BONDORDER_SINGLE) {            cmlBond.setOrder("S");        } else if (border == CDKConstants.BONDORDER_DOUBLE) {            cmlBond.setOrder("D");        } else if (border == CDKConstants.BONDORDER_TRIPLE) {            cmlBond.setOrder("T");        } else {            CMLScalar scalar = new CMLScalar();            this.checkPrefix(scalar);//            scalar.setDataType("xsd:float");            scalar.setDictRef("cdk:bondOrder");            scalar.setTitle("order");            scalar.setValue(cdkBond.getOrder());            cmlBond.appendChild(scalar);        }        if (cdkBond.getFlag(CDKConstants.ISAROMATIC)) {        	CMLBondType bType = new CMLBondType();        	bType.setDictRef("cdk:aromaticBond");        }        if (cdkBond.getStereo() == CDKConstants.STEREO_BOND_UP ||                cdkBond.getStereo() == CDKConstants.STEREO_BOND_DOWN) {        	CMLBondStereo bondStereo = new CMLBondStereo();            this.checkPrefix(bondStereo);            if (cdkBond.getStereo() == CDKConstants.STEREO_BOND_UP) {                bondStereo.setDictRef("cml:W");            } else {                bondStereo.setDictRef("cml:H");            }            cmlBond.appendChild(bondStereo);        }        if (cdkBond.getProperties().size() > 0) writeProperties(cdkBond, cmlBond);        return cmlBond;    }    private void writeProperties(IChemObject object, CMLElement cmlElement) {        Hashtable props = object.getProperties();        Enumeration keys = props.keys();        CMLElement propList = null;        while (keys.hasMoreElements()) {            Object key = keys.nextElement();            if (key instanceof DictRef) {                Object value = props.get(key);                CMLScalar scalar = new CMLScalar();                this.checkPrefix(scalar);                scalar.setDictRef(((DictRef) key).getType());                scalar.setValue(value.toString());                cmlElement.appendChild(scalar);            } else if (key instanceof String) {                String stringKey = (String) key;                if (stringKey.equals(CDKConstants.TITLE)) {                    // don't output this one. It's covered by addTitle()                } else if (!(stringKey.startsWith("org.openscience.cdk"))) {                    Object value = props.get(key);                    CMLScalar scalar = new CMLScalar();                    this.checkPrefix(scalar);                    scalar.setTitle((String) key);                    scalar.setValue(value.toString());                    cmlElement.appendChild(scalar);                }            }        }        if (propList != null) {            cmlElement.appendChild(propList);        }    }    private void mapFractionalCoordsToCML(CMLAtom cmlAtom, IAtom cdkAtom) {        if (cdkAtom.getFractionalPoint3d() != null) {            cmlAtom.setXFract(cdkAtom.getFractionalPoint3d().x);            cmlAtom.setYFract(cdkAtom.getFractionalPoint3d().y);            cmlAtom.setZFract(cdkAtom.getFractionalPoint3d().z);        }    }    private void map3DCoordsToCML(CMLAtom cmlAtom, IAtom cdkAtom) {        if (cdkAtom.getPoint3d() != null) {            cmlAtom.setX3(cdkAtom.getPoint3d().x);            cmlAtom.setY3(cdkAtom.getPoint3d().y);            cmlAtom.setZ3(cdkAtom.getPoint3d().z);        }    }    private void map2DCoordsToCML(CMLAtom cmlAtom, IAtom cdkAtom) {        if (cdkAtom.getPoint2d() != null) {            cmlAtom.setX2(cdkAtom.getPoint2d().x);            cmlAtom.setY2(cdkAtom.getPoint2d().y);        }    }    private void checkPrefix(CMLElement element) {        if (this.prefix != null) {            this.prefix.trim();            if (this.prefix.length() == 0) prefix = null;        }        if (this.prefix != null) element.setNamespacePrefix(this.prefix);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -