Search in sources :

Example 1 with CMLScalar

use of org.xmlcml.cml.element.CMLScalar in project cdk by cdk.

the class Convertor method cdkAtomContainerToCMLMolecule.

private CMLMolecule cdkAtomContainerToCMLMolecule(IAtomContainer structure, boolean setIDs, boolean isRef) {
    CMLMolecule cmlMolecule = new CMLMolecule();
    if (useCMLIDs && setIDs) {
        IDCreator.createIDs(structure);
    }
    this.checkPrefix(cmlMolecule);
    if (structure.getID() != null && !structure.getID().equals(""))
        if (!isRef)
            cmlMolecule.setId(structure.getID());
        else
            cmlMolecule.setRef(structure.getID());
    if (structure.getTitle() != null) {
        cmlMolecule.setTitle(structure.getTitle());
    }
    if (structure.getProperty(CDKConstants.INCHI) != null) {
        CMLIdentifier ident = new CMLIdentifier();
        ident.setConvention("iupac:inchi");
        ident.setCMLValue(structure.getProperty(CDKConstants.INCHI).toString());
        cmlMolecule.appendChild(ident);
    }
    if (!isRef) {
        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);
        }
        for (int i = 0; i < structure.getBondCount(); i++) {
            CMLBond cmlBond = cdkBondToCMLBond(structure.getBond(i));
            cmlMolecule.addBond(cmlBond);
        }
    }
    // ok, output molecular properties, but not TITLE, INCHI, or DictRef's
    Map<Object, Object> props = structure.getProperties();
    for (Object key : props.keySet()) {
        // but only if a String
        if (key instanceof String && !isRef && 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);
            }
        }
        // but it should be that IMolecularFormula is a extension of IAtomContainer
        if (key instanceof String && !isRef && key.toString().equals(CDKConstants.FORMULA)) {
            if (props.get(key) instanceof IMolecularFormula) {
                IMolecularFormula cdkFormula = (IMolecularFormula) props.get(key);
                CMLFormula cmlFormula = new CMLFormula();
                List<IIsotope> isotopesList = MolecularFormulaManipulator.putInOrder(MolecularFormulaManipulator.generateOrderEle(), cdkFormula);
                for (IIsotope iIsotope : isotopesList) {
                    cmlFormula.add(iIsotope.getSymbol(), cdkFormula.getIsotopeCount(iIsotope));
                }
                cmlMolecule.appendChild(cmlFormula);
            } else if (props.get(key) instanceof IMolecularFormulaSet) {
                IMolecularFormulaSet cdkFormulaSet = (IMolecularFormulaSet) props.get(key);
                for (IMolecularFormula cdkFormula : cdkFormulaSet.molecularFormulas()) {
                    List<IIsotope> isotopesList = MolecularFormulaManipulator.putInOrder(MolecularFormulaManipulator.generateOrderEle(), cdkFormula);
                    CMLFormula cmlFormula = new CMLFormula();
                    cmlFormula.setDictRef("cdk:possibleMachts");
                    for (IIsotope iIsotope : isotopesList) {
                        cmlFormula.add(iIsotope.getSymbol(), cdkFormula.getIsotopeCount(iIsotope));
                    }
                    cmlMolecule.appendChild(cmlFormula);
                }
            }
        }
    }
    for (String s : customizers.keySet()) {
        ICMLCustomizer customizer = customizers.get(s);
        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;
}
Also used : CMLAtom(org.xmlcml.cml.element.CMLAtom) CMLScalar(org.xmlcml.cml.element.CMLScalar) InvocationTargetException(java.lang.reflect.InvocationTargetException) CMLBond(org.xmlcml.cml.element.CMLBond) CMLMolecule(org.xmlcml.cml.element.CMLMolecule) CMLIdentifier(org.xmlcml.cml.element.CMLIdentifier) ArrayList(java.util.ArrayList) CMLReactionList(org.xmlcml.cml.element.CMLReactionList) CMLMoleculeList(org.xmlcml.cml.element.CMLMoleculeList) CMLProductList(org.xmlcml.cml.element.CMLProductList) CMLSubstanceList(org.xmlcml.cml.element.CMLSubstanceList) CMLList(org.xmlcml.cml.element.CMLList) List(java.util.List) CMLReactantList(org.xmlcml.cml.element.CMLReactantList) CMLFormula(org.xmlcml.cml.element.CMLFormula)

Example 2 with CMLScalar

use of org.xmlcml.cml.element.CMLScalar in project cdk by cdk.

the class Convertor method cdkAtomToCMLAtom.

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);
    Integer formalCharge = cdkAtom.getFormalCharge();
    if (formalCharge != null)
        cmlAtom.setFormalCharge(formalCharge);
    // CML's hydrogen count consists of the sum of implicit and explicit
    // hydrogens (see bug #1655045).
    Integer totalHydrogen = cdkAtom.getImplicitHydrogenCount();
    if (totalHydrogen != null) {
        if (container != null) {
            for (IBond iBond : container.getConnectedBondsList(cdkAtom)) {
                for (IAtom atom : iBond.atoms()) {
                    if (atom.getAtomicNumber() == IElement.H && !Objects.equals(atom, cdkAtom))
                        totalHydrogen++;
                }
            }
        }
        // else: it is the implicit hydrogen count
        cmlAtom.setHydrogenCount(totalHydrogen);
    }
    // else: don't report it, people can count the explicit Hs themselves
    Integer massNumber = cdkAtom.getMassNumber();
    if (!(cdkAtom instanceof IPseudoAtom)) {
        if (massNumber != null) {
            cmlAtom.setIsotopeNumber(massNumber);
        }
    }
    if (cdkAtom.getCharge() != CDKConstants.UNSET) {
        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);
    if (cdkAtom.getFlag(CDKConstants.ISAROMATIC)) {
        CMLScalar aromAtom = new CMLScalar();
        aromAtom.setDictRef("cdk:aromaticAtom");
        cmlAtom.appendChild(aromAtom);
    }
    for (String s : customizers.keySet()) {
        ICMLCustomizer customizer = customizers.get(s);
        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;
}
Also used : CMLAtom(org.xmlcml.cml.element.CMLAtom) CMLScalar(org.xmlcml.cml.element.CMLScalar) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with CMLScalar

use of org.xmlcml.cml.element.CMLScalar in project cdk by cdk.

the class QSARCustomizer method createScalar.

private Element createScalar(IDescriptorResult value) {
    Element scalar;
    if (value instanceof DoubleResult) {
        scalar = new CMLScalar();
        scalar.addAttribute(new Attribute("dataType", "xsd:double"));
        scalar.appendChild("" + ((DoubleResult) value).doubleValue());
    } else if (value instanceof IntegerResult) {
        scalar = new CMLScalar();
        scalar.addAttribute(new Attribute("dataType", "xsd:int"));
        scalar.appendChild("" + ((IntegerResult) value).intValue());
    } else if (value instanceof BooleanResult) {
        scalar = new CMLScalar();
        scalar.addAttribute(new Attribute("dataType", "xsd:boolean"));
        scalar.appendChild("" + ((BooleanResult) value).booleanValue());
    } else if (value instanceof IntegerArrayResult) {
        IntegerArrayResult result = (IntegerArrayResult) value;
        scalar = new CMLArray();
        scalar.addAttribute(new Attribute("dataType", "xsd:int"));
        scalar.addAttribute(new Attribute("size", "" + result.length()));
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < result.length(); i++) {
            buffer.append(result.get(i) + " ");
        }
        scalar.appendChild(buffer.toString());
    } else if (value instanceof DoubleArrayResult) {
        DoubleArrayResult result = (DoubleArrayResult) value;
        scalar = new CMLArray();
        scalar.addAttribute(new Attribute("dataType", "xsd:double"));
        scalar.addAttribute(new Attribute("size", "" + result.length()));
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < result.length(); i++) {
            buffer.append(result.get(i) + " ");
        }
        scalar.appendChild(buffer.toString());
    } else {
        // logger.error("Could not convert this object to a scalar element: ", value);
        scalar = new CMLScalar();
        scalar.appendChild(value.toString());
    }
    return scalar;
}
Also used : BooleanResult(org.openscience.cdk.qsar.result.BooleanResult) Attribute(nu.xom.Attribute) Element(nu.xom.Element) IntegerArrayResult(org.openscience.cdk.qsar.result.IntegerArrayResult) DoubleArrayResult(org.openscience.cdk.qsar.result.DoubleArrayResult) DoubleResult(org.openscience.cdk.qsar.result.DoubleResult) CMLScalar(org.xmlcml.cml.element.CMLScalar) IntegerResult(org.openscience.cdk.qsar.result.IntegerResult) CMLArray(org.xmlcml.cml.element.CMLArray)

Example 4 with CMLScalar

use of org.xmlcml.cml.element.CMLScalar in project cdk by cdk.

the class PDBAtomCustomizer method customize.

@Override
public void customize(IAtom atom, Object nodeToAdd) throws Exception {
    if (!(nodeToAdd instanceof Element))
        throw new CDKException("NodeToAdd must be of type nu.xom.Element!");
    Element element = (Element) nodeToAdd;
    if (atom instanceof IPDBAtom) {
        IPDBAtom pdbAtom = (IPDBAtom) atom;
        if (hasContent(pdbAtom.getAltLoc())) {
            CMLScalar scalar = new CMLScalar();
            scalar.addAttribute(new Attribute("dictRef", "pdb:altLoc"));
            scalar.appendChild(pdbAtom.getAltLoc());
            element.appendChild(scalar);
        }
        if (hasContent(pdbAtom.getChainID())) {
            Element scalar = new CMLScalar();
            scalar.addAttribute(new Attribute("dictRef", "pdb:chainID"));
            scalar.appendChild(pdbAtom.getChainID());
            element.appendChild(scalar);
        }
        {
            Element scalar = new CMLScalar();
            scalar.addAttribute(new Attribute("dictRef", "pdb:hetAtom"));
            scalar.appendChild("" + pdbAtom.getHetAtom());
            element.appendChild(scalar);
        }
        if (hasContent(pdbAtom.getICode())) {
            Element scalar = new CMLScalar();
            scalar.addAttribute(new Attribute("dictRef", "pdb:iCode"));
            scalar.appendChild(pdbAtom.getICode());
            element.appendChild(scalar);
        }
        if (hasContent(pdbAtom.getName())) {
            Element scalar = new Element("label");
            scalar.addAttribute(new Attribute("dictRef", "pdb:name"));
            scalar.appendChild(pdbAtom.getName());
            element.appendChild(scalar);
        }
        {
            Element scalar = new CMLScalar();
            scalar.addAttribute(new Attribute("dictRef", "pdb:oxt"));
            scalar.appendChild("" + pdbAtom.getOxt());
            element.appendChild(scalar);
        }
        if (hasContent(pdbAtom.getRecord())) {
            Element scalar = new CMLScalar();
            scalar.addAttribute(new Attribute("dictRef", "pdb:record"));
            scalar.appendChild(pdbAtom.getRecord());
            element.appendChild(scalar);
        }
        if (hasContent(pdbAtom.getResName())) {
            Element scalar = new CMLScalar();
            scalar.addAttribute(new Attribute("dictRef", "pdb:resName"));
            scalar.appendChild(pdbAtom.getResName());
            element.appendChild(scalar);
        }
        if (hasContent(pdbAtom.getResSeq())) {
            Element scalar = new CMLScalar();
            scalar.addAttribute(new Attribute("dictRef", "pdb:resSeq"));
            scalar.appendChild(pdbAtom.getResSeq());
            element.appendChild(scalar);
        }
        if (hasContent(pdbAtom.getSegID())) {
            Element scalar = new CMLScalar();
            scalar.addAttribute(new Attribute("dictRef", "pdb:segID"));
            scalar.appendChild(pdbAtom.getSegID());
            element.appendChild(scalar);
        }
        if (pdbAtom.getSerial() != 0) {
            Element scalar = new CMLScalar();
            scalar.addAttribute(new Attribute("dictRef", "pdb:serial"));
            scalar.appendChild("" + pdbAtom.getSerial());
            element.appendChild(scalar);
        }
        if (pdbAtom.getTempFactor() != -1.0) {
            Element scalar = new CMLScalar();
            scalar.addAttribute(new Attribute("dictRef", "pdb:tempFactor"));
            scalar.appendChild("" + pdbAtom.getTempFactor());
            element.appendChild(scalar);
        }
        element.addAttribute(new Attribute("occupancy", "" + pdbAtom.getOccupancy()));
        // remove isotope info
        Attribute isotopeInfo = element.getAttribute("isotopeNumber");
        if (isotopeInfo != null)
            element.removeAttribute(isotopeInfo);
    }
}
Also used : IPDBAtom(org.openscience.cdk.interfaces.IPDBAtom) Attribute(nu.xom.Attribute) CDKException(org.openscience.cdk.exception.CDKException) Element(nu.xom.Element) CMLScalar(org.xmlcml.cml.element.CMLScalar)

Example 5 with CMLScalar

use of org.xmlcml.cml.element.CMLScalar in project cdk by cdk.

the class MDMoleculeCustomizer method customize.

/**
 * Customize Molecule.
 */
@Override
public void customize(IAtomContainer molecule, Object nodeToAdd) throws Exception {
    if (!(nodeToAdd instanceof CMLMolecule))
        throw new CDKException("NodeToAdd must be of type nu.xom.Element!");
    // The nodeToAdd
    CMLMolecule molToCustomize = (CMLMolecule) nodeToAdd;
    if ((molecule instanceof MDMolecule)) {
        MDMolecule mdmol = (MDMolecule) molecule;
        molToCustomize.setConvention("md:mdMolecule");
        molToCustomize.addNamespaceDeclaration("md", "http://www.bioclipse.net/mdmolecule/");
        // Residues
        if (mdmol.getResidues().size() > 0) {
            for (Residue residue : mdmol.getResidues()) {
                int number = residue.getNumber();
                CMLMolecule resMol = new CMLMolecule();
                resMol.setDictRef("md:residue");
                resMol.setTitle(residue.getName());
                // Append resNo
                CMLScalar residueNumber = new CMLScalar(number);
                residueNumber.addAttribute(new Attribute("dictRef", "md:resNumber"));
                resMol.addScalar(residueNumber);
                // prefix for residue atom id
                String rprefix = "r" + number;
                // Append atoms
                CMLAtomArray ar = new CMLAtomArray();
                for (int i = 0; i < residue.getAtomCount(); i++) {
                    CMLAtom cmlAtom = new CMLAtom();
                    // System.out.println("atom ID: "+ residue.getAtom(i).getID());
                    // cmlAtom.addAttribute(new Attribute("ref", residue.getAtom(i).getID()));
                    // the next thing is better, but throws an exception
                    // 
                    // setRef to keep consistent usage
                    // setId to satisfy Jumbo 54. need for all atoms to have id
                    cmlAtom.setRef(residue.getAtom(i).getID());
                    cmlAtom.setId(rprefix + "_" + residue.getAtom(i).getID());
                    ar.addAtom(cmlAtom);
                }
                resMol.addAtomArray(ar);
                molToCustomize.appendChild(resMol);
            }
        }
        // Chargegroups
        if (mdmol.getChargeGroups().size() > 0) {
            for (ChargeGroup chargeGroup : mdmol.getChargeGroups()) {
                int number = chargeGroup.getNumber();
                // FIXME: persist the ChargeGroup
                CMLMolecule cgMol = new CMLMolecule();
                cgMol.setDictRef("md:chargeGroup");
                // etc: add name, refs to atoms etc
                // Append chgrpNo
                CMLScalar cgNo = new CMLScalar(number);
                cgNo.addAttribute(new Attribute("dictRef", "md:cgNumber"));
                cgMol.appendChild(cgNo);
                // prefix for residue atom id
                String cprefix = "cg" + number;
                // Append atoms from chargeGroup as it is an AC
                CMLAtomArray ar = new CMLAtomArray();
                for (int i = 0; i < chargeGroup.getAtomCount(); i++) {
                    CMLAtom cmlAtom = new CMLAtom();
                    // setRef to keep consistent usage
                    // setId to satisfy Jumbo 5.4 need for all atoms to have id
                    cmlAtom.setRef(chargeGroup.getAtom(i).getID());
                    cmlAtom.setId(cprefix + "_" + chargeGroup.getAtom(i).getID());
                    // Append switching atom?
                    if (chargeGroup.getAtom(i).equals(chargeGroup.getSwitchingAtom())) {
                        CMLScalar scalar = new CMLScalar();
                        scalar.setDictRef("md:switchingAtom");
                        cmlAtom.addScalar(scalar);
                    }
                    ar.addAtom(cmlAtom);
                }
                cgMol.addAtomArray(ar);
                molToCustomize.appendChild(cgMol);
            }
        }
    }
}
Also used : MDMolecule(org.openscience.cdk.libio.md.MDMolecule) CMLMolecule(org.xmlcml.cml.element.CMLMolecule) Residue(org.openscience.cdk.libio.md.Residue) Attribute(nu.xom.Attribute) ChargeGroup(org.openscience.cdk.libio.md.ChargeGroup) CDKException(org.openscience.cdk.exception.CDKException) CMLAtom(org.xmlcml.cml.element.CMLAtom) CMLAtomArray(org.xmlcml.cml.element.CMLAtomArray) CMLScalar(org.xmlcml.cml.element.CMLScalar)

Aggregations

CMLScalar (org.xmlcml.cml.element.CMLScalar)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Attribute (nu.xom.Attribute)3 CMLAtom (org.xmlcml.cml.element.CMLAtom)3 Element (nu.xom.Element)2 CDKException (org.openscience.cdk.exception.CDKException)2 CMLBond (org.xmlcml.cml.element.CMLBond)2 CMLMolecule (org.xmlcml.cml.element.CMLMolecule)2 CMLProductList (org.xmlcml.cml.element.CMLProductList)2 CMLReactantList (org.xmlcml.cml.element.CMLReactantList)2 CMLSubstanceList (org.xmlcml.cml.element.CMLSubstanceList)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DictRef (org.openscience.cdk.dict.DictRef)1 Order (org.openscience.cdk.interfaces.IBond.Order)1 IPDBAtom (org.openscience.cdk.interfaces.IPDBAtom)1 ChargeGroup (org.openscience.cdk.libio.md.ChargeGroup)1 MDMolecule (org.openscience.cdk.libio.md.MDMolecule)1 Residue (org.openscience.cdk.libio.md.Residue)1 BooleanResult (org.openscience.cdk.qsar.result.BooleanResult)1