Search in sources :

Example 1 with LonePairElectronChecker

use of org.openscience.cdk.tools.LonePairElectronChecker in project cdk by cdk.

the class HomolyticCleavageReactionTest method testEthylbenzaldehydeMapping.

/**
 * A unit test suite for JUnit. Reaction: Ethylbenzaldehyde.
 * CCc1ccc(C=O)cc1  =>  C+ Cc1ccc(C=O)cc1
 * CCc1ccc(C=O)cc1  =>  CC + c1ccc(C=O)cc1
 * Automatically looks for the active center.
 *
 * @cdk.inchi InChI=1/C9H10O/c1-2-8-3-5-9(7-10)6-4-8/h3-7H,2H2,1H3
 *
 * @see       #testEthylbenzaldehydeManual()
 */
@Test
public void testEthylbenzaldehydeMapping() throws Exception {
    IReactionProcess type = new HomolyticCleavageReaction();
    IAtomContainerSet setOfReactants = builder.newInstance(IAtomContainerSet.class);
    // smiles("CCc1ccc(C=O)cc1")
    IAtomContainer molecule = builder.newInstance(IAtomContainer.class);
    molecule.addAtom(builder.newInstance(IAtom.class, "C"));
    molecule.addAtom(builder.newInstance(IAtom.class, "C"));
    molecule.addBond(0, 1, IBond.Order.SINGLE);
    molecule.addAtom(builder.newInstance(IAtom.class, "C"));
    molecule.addBond(1, 2, IBond.Order.SINGLE);
    molecule.addAtom(builder.newInstance(IAtom.class, "C"));
    molecule.addBond(2, 3, IBond.Order.SINGLE);
    molecule.addAtom(builder.newInstance(IAtom.class, "C"));
    molecule.addBond(3, 4, IBond.Order.DOUBLE);
    molecule.addAtom(builder.newInstance(IAtom.class, "C"));
    molecule.addBond(4, 5, IBond.Order.SINGLE);
    molecule.addAtom(builder.newInstance(IAtom.class, "C"));
    molecule.addBond(5, 6, IBond.Order.SINGLE);
    molecule.addAtom(builder.newInstance(IAtom.class, "O"));
    molecule.addBond(6, 7, IBond.Order.DOUBLE);
    molecule.addAtom(builder.newInstance(IAtom.class, "C"));
    molecule.addBond(5, 8, IBond.Order.DOUBLE);
    molecule.addAtom(builder.newInstance(IAtom.class, "C"));
    molecule.addBond(8, 9, IBond.Order.SINGLE);
    molecule.addBond(9, 2, IBond.Order.DOUBLE);
    addExplicitHydrogens(molecule);
    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
    LonePairElectronChecker lpcheck = new LonePairElectronChecker();
    lpcheck.saturate(molecule);
    molecule.getAtom(0).setFlag(CDKConstants.REACTIVE_CENTER, true);
    molecule.getAtom(1).setFlag(CDKConstants.REACTIVE_CENTER, true);
    molecule.getAtom(2).setFlag(CDKConstants.REACTIVE_CENTER, true);
    molecule.getBond(0).setFlag(CDKConstants.REACTIVE_CENTER, true);
    molecule.getBond(1).setFlag(CDKConstants.REACTIVE_CENTER, true);
    setOfReactants.addAtomContainer(molecule);
    /* has active center */
    List<IParameterReact> paramList = new ArrayList<>();
    IParameterReact param = new SetReactionCenter();
    param.setParameter(Boolean.TRUE);
    paramList.add(param);
    type.setParameterList(paramList);
    /* initiate */
    makeSureAtomTypesAreRecognized(molecule);
    IReactionSet setOfReactions = type.initiate(setOfReactants, null);
    IAtomContainer product11 = setOfReactions.getReaction(0).getProducts().getAtomContainer(0);
    IAtomContainer product12 = setOfReactions.getReaction(0).getProducts().getAtomContainer(1);
    Assert.assertEquals(20, setOfReactions.getReaction(0).getMappingCount());
    IAtom mappedProductA1 = (IAtom) ReactionManipulator.getMappedChemObject(setOfReactions.getReaction(0), molecule.getAtom(0));
    Assert.assertEquals(mappedProductA1, product11.getAtom(0));
    IAtom mappedProductA2 = (IAtom) ReactionManipulator.getMappedChemObject(setOfReactions.getReaction(0), molecule.getAtom(1));
    Assert.assertEquals(mappedProductA2, product12.getAtom(0));
    IAtomContainer product21 = setOfReactions.getReaction(1).getProducts().getAtomContainer(0);
    IAtomContainer product22 = setOfReactions.getReaction(1).getProducts().getAtomContainer(1);
    Assert.assertEquals(20, setOfReactions.getReaction(0).getMappingCount());
    mappedProductA1 = (IAtom) ReactionManipulator.getMappedChemObject(setOfReactions.getReaction(1), molecule.getAtom(1));
    Assert.assertEquals(mappedProductA1, product21.getAtom(1));
    mappedProductA2 = (IAtom) ReactionManipulator.getMappedChemObject(setOfReactions.getReaction(1), molecule.getAtom(2));
    Assert.assertEquals(mappedProductA2, product22.getAtom(0));
}
Also used : IReactionProcess(org.openscience.cdk.reaction.IReactionProcess) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IReactionSet(org.openscience.cdk.interfaces.IReactionSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IParameterReact(org.openscience.cdk.reaction.type.parameters.IParameterReact) ArrayList(java.util.ArrayList) SetReactionCenter(org.openscience.cdk.reaction.type.parameters.SetReactionCenter) IAtom(org.openscience.cdk.interfaces.IAtom) LonePairElectronChecker(org.openscience.cdk.tools.LonePairElectronChecker) Test(org.junit.Test) ReactionProcessTest(org.openscience.cdk.reaction.ReactionProcessTest)

Example 2 with LonePairElectronChecker

use of org.openscience.cdk.tools.LonePairElectronChecker in project cdk by cdk.

the class ProtonAffinityHOSEDescriptor method calculate.

/**
 *  This method calculates the protonation affinity of an atom.
 *
 *@param  atom              The IAtom to protonate
 *@param  container         Parameter is the IAtomContainer.
 *@return                   The protonation affinity. Not possible the ionization.
 */
@Override
public DescriptorValue calculate(IAtom atom, IAtomContainer container) {
    double value;
    try {
        int i = container.indexOf(atom);
        if (i < 0)
            throw new CDKException("atom was not a memeber of the provided container");
        // don't modify the original
        container = container.clone();
        atom = container.getAtom(i);
        AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(container);
        LonePairElectronChecker lpcheck = new LonePairElectronChecker();
        lpcheck.saturate(container);
    } catch (CDKException | CloneNotSupportedException e) {
        return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new DoubleResult(Double.NaN), NAMES, null);
    }
    value = db.extractAffinity(container, atom);
    return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new DoubleResult(value), NAMES);
}
Also used : CDKException(org.openscience.cdk.exception.CDKException) DoubleResult(org.openscience.cdk.qsar.result.DoubleResult) LonePairElectronChecker(org.openscience.cdk.tools.LonePairElectronChecker) DescriptorValue(org.openscience.cdk.qsar.DescriptorValue)

Example 3 with LonePairElectronChecker

use of org.openscience.cdk.tools.LonePairElectronChecker in project cdk by cdk.

the class PartialPiChargeDescriptor method calculate.

/**
 *  The method returns apha partial charges assigned to an heavy atom through Gasteiger Marsili
 *  It is needed to call the addExplicitHydrogensToSatisfyValency method from the class tools.HydrogenAdder.
 *  For this method will be only possible if the heavy atom has single bond.
 *
 *@param  atom              The IAtom for which the DescriptorValue is requested
 *@param  ac                AtomContainer
 *@return                   Value of the alpha partial charge
 */
@Override
public DescriptorValue calculate(IAtom atom, IAtomContainer ac) {
    // FIXME: for now I'll cache a few modified atomic properties, and restore them at the end of this method
    Double originalCharge = atom.getCharge();
    String originalAtomtypeName = atom.getAtomTypeName();
    Integer originalNeighborCount = atom.getFormalNeighbourCount();
    Integer originalValency = atom.getValency();
    IAtomType.Hybridization originalHybridization = atom.getHybridization();
    Double originalBondOrderSum = atom.getBondOrderSum();
    Order originalMaxBondOrder = atom.getMaxBondOrder();
    if (!isCachedAtomContainer(ac)) {
        try {
            AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(ac);
        } catch (CDKException e) {
            return getDummyDescriptorValue(e);
        }
        if (lpeChecker) {
            LonePairElectronChecker lpcheck = new LonePairElectronChecker();
            try {
                lpcheck.saturate(ac);
            } catch (CDKException e) {
                return getDummyDescriptorValue(e);
            }
        }
        if (maxIterations != -1)
            pepe.setMaxGasteigerIters(maxIterations);
        if (maxResonStruc != -1)
            pepe.setMaxResoStruc(maxResonStruc);
        try {
            for (int i = 0; i < ac.getAtomCount(); i++) ac.getAtom(i).setCharge(0.0);
            pepe.assignGasteigerPiPartialCharges(ac, true);
            for (int i = 0; i < ac.getAtomCount(); i++) {
                // assume same order, so mol.getAtom(i) == ac.getAtom(i)
                cacheDescriptorValue(ac.getAtom(i), ac, new DoubleResult(ac.getAtom(i).getCharge()));
            }
        } catch (Exception exception) {
            return getDummyDescriptorValue(exception);
        }
    }
    // restore original props
    atom.setCharge(originalCharge);
    atom.setAtomTypeName(originalAtomtypeName);
    atom.setFormalNeighbourCount(originalNeighborCount);
    atom.setValency(originalValency);
    atom.setHybridization(originalHybridization);
    atom.setMaxBondOrder(originalMaxBondOrder);
    atom.setBondOrderSum(originalBondOrderSum);
    return getCachedDescriptorValue(atom) != null ? new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), getCachedDescriptorValue(atom), NAMES) : null;
}
Also used : IAtomType(org.openscience.cdk.interfaces.IAtomType) Order(org.openscience.cdk.interfaces.IBond.Order) CDKException(org.openscience.cdk.exception.CDKException) DoubleResult(org.openscience.cdk.qsar.result.DoubleResult) LonePairElectronChecker(org.openscience.cdk.tools.LonePairElectronChecker) CDKException(org.openscience.cdk.exception.CDKException) DescriptorValue(org.openscience.cdk.qsar.DescriptorValue)

Example 4 with LonePairElectronChecker

use of org.openscience.cdk.tools.LonePairElectronChecker in project cdk by cdk.

the class AtomHybridizationVSEPRDescriptorTest method testAtomHybridizationVSEPRDescriptorTest_7.

/**
 *  A unit test for JUnit with F-C=C
 */
@Test
public void testAtomHybridizationVSEPRDescriptorTest_7() throws java.lang.Exception {
    int[] testResult = { IAtomType.Hybridization.SP3.ordinal(), IAtomType.Hybridization.SP2.ordinal(), IAtomType.Hybridization.SP2.ordinal() };
    /*
                                                         * from Petra online:
                                                         * http
                                                         * ://www2.chemie.uni
                                                         * -erlangen
                                                         * .de/services/
                                                         * petra/smiles.phtml
                                                         */
    AtomHybridizationVSEPRDescriptor descriptor = new AtomHybridizationVSEPRDescriptor();
    SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
    IAtomContainer mol = sp.parseSmiles("F-C=C");
    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
    assertAtomTypesPerceived(mol);
    addExplicitHydrogens(mol);
    LonePairElectronChecker lpcheck = new LonePairElectronChecker();
    lpcheck.saturate(mol);
    assertAtomTypesPerceived(mol);
    for (int i = 0; i < 3; i++) {
        Assert.assertEquals(testResult[i], ((IntegerResult) descriptor.calculate(mol.getAtom(i), mol).getValue()).intValue());
    }
}
Also used : SmilesParser(org.openscience.cdk.smiles.SmilesParser) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) LonePairElectronChecker(org.openscience.cdk.tools.LonePairElectronChecker) Test(org.junit.Test)

Example 5 with LonePairElectronChecker

use of org.openscience.cdk.tools.LonePairElectronChecker in project cdk by cdk.

the class BCUTDescriptor method calculate.

/**
 * Calculates the three classes of BCUT descriptors.
 *
 * @param container Parameter is the atom container.
 * @return An ArrayList containing the descriptors. The default is to return
 *         all calculated eigenvalues of the Burden matrices in the order described
 *         above. If a parameter list was supplied, then only the specified number
 *         of highest and lowest eigenvalues (for each class of BCUT) will be returned.
 */
@Override
public DescriptorValue calculate(IAtomContainer container) {
    int counter;
    IAtomContainer molecule;
    try {
        molecule = container.clone();
    } catch (CloneNotSupportedException e) {
        logger.debug("Error during clone");
        return getDummyDescriptorValue(new CDKException("Error occurred during clone " + e));
    }
    // add H's in case they're not present
    try {
        AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
        CDKHydrogenAdder hAdder = CDKHydrogenAdder.getInstance(molecule.getBuilder());
        hAdder.addImplicitHydrogens(molecule);
        AtomContainerManipulator.convertImplicitToExplicitHydrogens(molecule);
    } catch (Exception e) {
        return getDummyDescriptorValue(new CDKException("Could not add hydrogens: " + e.getMessage(), e));
    }
    // do aromaticity detecttion for calculating polarizability later on
    if (this.checkAromaticity) {
        try {
            AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
        } catch (CDKException e) {
            return getDummyDescriptorValue(new CDKException("Error in atom typing: " + e.getMessage(), e));
        }
        try {
            Aromaticity.cdkLegacy().apply(molecule);
        } catch (CDKException e) {
            return getDummyDescriptorValue(new CDKException("Error in aromaticity perception: " + e.getMessage()));
        }
    }
    // find number of heavy atoms
    int nheavy = 0;
    for (int i = 0; i < molecule.getAtomCount(); i++) {
        if (molecule.getAtom(i).getAtomicNumber() != IElement.H)
            nheavy++;
    }
    if (nheavy == 0)
        return getDummyDescriptorValue(new CDKException("No heavy atoms in the molecule"));
    double[] diagvalue = new double[nheavy];
    // get atomic mass weighted BCUT
    counter = 0;
    try {
        for (int i = 0; i < molecule.getAtomCount(); i++) {
            if (molecule.getAtom(i).getAtomicNumber() == IElement.H)
                continue;
            diagvalue[counter] = Isotopes.getInstance().getMajorIsotope(molecule.getAtom(i).getSymbol()).getExactMass();
            counter++;
        }
    } catch (Exception e) {
        return getDummyDescriptorValue(new CDKException("Could not calculate weight: " + e.getMessage(), e));
    }
    double[][] burdenMatrix = BurdenMatrix.evalMatrix(molecule, diagvalue);
    if (hasUndefined(burdenMatrix))
        return getDummyDescriptorValue(new CDKException("Burden matrix has undefined values"));
    Matrix matrix = new Matrix(burdenMatrix);
    EigenvalueDecomposition eigenDecomposition = new EigenvalueDecomposition(matrix);
    double[] eval1 = eigenDecomposition.getRealEigenvalues();
    // get charge weighted BCUT
    LonePairElectronChecker lpcheck = new LonePairElectronChecker();
    GasteigerMarsiliPartialCharges peoe;
    try {
        lpcheck.saturate(molecule);
        double[] charges = new double[molecule.getAtomCount()];
        // pepe = new GasteigerPEPEPartialCharges();
        // pepe.calculateCharges(molecule);
        // for (int i = 0; i < molecule.getAtomCount(); i++) charges[i] = molecule.getAtom(i).getCharge();
        peoe = new GasteigerMarsiliPartialCharges();
        peoe.assignGasteigerMarsiliSigmaPartialCharges(molecule, true);
        for (int i = 0; i < molecule.getAtomCount(); i++) charges[i] += molecule.getAtom(i).getCharge();
        for (int i = 0; i < molecule.getAtomCount(); i++) {
            molecule.getAtom(i).setCharge(charges[i]);
        }
    } catch (Exception e) {
        return getDummyDescriptorValue(new CDKException("Could not calculate partial charges: " + e.getMessage(), e));
    }
    counter = 0;
    for (int i = 0; i < molecule.getAtomCount(); i++) {
        if (molecule.getAtom(i).getAtomicNumber() == IElement.H)
            continue;
        diagvalue[counter] = molecule.getAtom(i).getCharge();
        counter++;
    }
    burdenMatrix = BurdenMatrix.evalMatrix(molecule, diagvalue);
    if (hasUndefined(burdenMatrix))
        return getDummyDescriptorValue(new CDKException("Burden matrix has undefined values"));
    matrix = new Matrix(burdenMatrix);
    eigenDecomposition = new EigenvalueDecomposition(matrix);
    double[] eval2 = eigenDecomposition.getRealEigenvalues();
    int[][] topoDistance = PathTools.computeFloydAPSP(AdjacencyMatrix.getMatrix(molecule));
    // get polarizability weighted BCUT
    Polarizability pol = new Polarizability();
    counter = 0;
    for (int i = 0; i < molecule.getAtomCount(); i++) {
        if (molecule.getAtom(i).getAtomicNumber() == IElement.H)
            continue;
        diagvalue[counter] = pol.calculateGHEffectiveAtomPolarizability(molecule, molecule.getAtom(i), false, topoDistance);
        counter++;
    }
    burdenMatrix = BurdenMatrix.evalMatrix(molecule, diagvalue);
    if (hasUndefined(burdenMatrix))
        return getDummyDescriptorValue(new CDKException("Burden matrix has undefined values"));
    matrix = new Matrix(burdenMatrix);
    eigenDecomposition = new EigenvalueDecomposition(matrix);
    double[] eval3 = eigenDecomposition.getRealEigenvalues();
    // return only the n highest & lowest eigenvalues
    int lnlow, lnhigh, enlow, enhigh;
    if (nlow > nheavy) {
        lnlow = nheavy;
        enlow = nlow - nheavy;
    } else {
        lnlow = nlow;
        enlow = 0;
    }
    if (nhigh > nheavy) {
        lnhigh = nheavy;
        enhigh = nhigh - nheavy;
    } else {
        lnhigh = nhigh;
        enhigh = 0;
    }
    DoubleArrayResult retval = new DoubleArrayResult((lnlow + enlow + lnhigh + enhigh) * 3);
    for (int i = 0; i < lnlow; i++) retval.add(eval1[i]);
    for (int i = 0; i < enlow; i++) retval.add(Double.NaN);
    for (int i = 0; i < lnhigh; i++) retval.add(eval1[eval1.length - i - 1]);
    for (int i = 0; i < enhigh; i++) retval.add(Double.NaN);
    for (int i = 0; i < lnlow; i++) retval.add(eval2[i]);
    for (int i = 0; i < enlow; i++) retval.add(Double.NaN);
    for (int i = 0; i < lnhigh; i++) retval.add(eval2[eval2.length - i - 1]);
    for (int i = 0; i < enhigh; i++) retval.add(Double.NaN);
    for (int i = 0; i < lnlow; i++) retval.add(eval3[i]);
    for (int i = 0; i < enlow; i++) retval.add(Double.NaN);
    for (int i = 0; i < lnhigh; i++) retval.add(eval3[eval3.length - i - 1]);
    for (int i = 0; i < enhigh; i++) retval.add(Double.NaN);
    return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), retval, getDescriptorNames());
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) Polarizability(org.openscience.cdk.charges.Polarizability) CDKException(org.openscience.cdk.exception.CDKException) EigenvalueDecomposition(Jama.EigenvalueDecomposition) CDKException(org.openscience.cdk.exception.CDKException) DescriptorValue(org.openscience.cdk.qsar.DescriptorValue) GasteigerMarsiliPartialCharges(org.openscience.cdk.charges.GasteigerMarsiliPartialCharges) Matrix(Jama.Matrix) AdjacencyMatrix(org.openscience.cdk.graph.matrix.AdjacencyMatrix) CDKHydrogenAdder(org.openscience.cdk.tools.CDKHydrogenAdder) DoubleArrayResult(org.openscience.cdk.qsar.result.DoubleArrayResult) LonePairElectronChecker(org.openscience.cdk.tools.LonePairElectronChecker)

Aggregations

LonePairElectronChecker (org.openscience.cdk.tools.LonePairElectronChecker)37 DoubleResult (org.openscience.cdk.qsar.result.DoubleResult)29 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)27 Test (org.junit.Test)24 SmilesParser (org.openscience.cdk.smiles.SmilesParser)17 IAtomicDescriptor (org.openscience.cdk.qsar.IAtomicDescriptor)12 CDKException (org.openscience.cdk.exception.CDKException)11 DescriptorValue (org.openscience.cdk.qsar.DescriptorValue)11 IAtomType (org.openscience.cdk.interfaces.IAtomType)8 Order (org.openscience.cdk.interfaces.IBond.Order)7 IAtom (org.openscience.cdk.interfaces.IAtom)6 ArrayList (java.util.ArrayList)4 Category (org.junit.experimental.categories.Category)4 SlowTest (org.openscience.cdk.test.SlowTest)4 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)2 IBond (org.openscience.cdk.interfaces.IBond)2 IReactionSet (org.openscience.cdk.interfaces.IReactionSet)2 DoubleArrayResult (org.openscience.cdk.qsar.result.DoubleArrayResult)2 IReactionProcess (org.openscience.cdk.reaction.IReactionProcess)2 ReactionProcessTest (org.openscience.cdk.reaction.ReactionProcessTest)2