Search in sources :

Example 26 with IChemObject

use of org.openscience.cdk.interfaces.IChemObject in project cdk by cdk.

the class DebugBondTest method testDebugBond_IAtom_IAtom.

@Test
public void testDebugBond_IAtom_IAtom() {
    IChemObject object = newChemObject();
    IAtom c = object.getBuilder().newInstance(IAtom.class, "C");
    IAtom o = object.getBuilder().newInstance(IAtom.class, "O");
    IBond bond = new DebugBond(c, o);
    Assert.assertEquals(2, bond.getAtomCount());
    Assert.assertEquals(c, bond.getBegin());
    Assert.assertEquals(o, bond.getEnd());
    Assert.assertEquals(IBond.Order.SINGLE, bond.getOrder());
    Assert.assertEquals(IBond.Stereo.NONE, bond.getStereo());
}
Also used : IChemObject(org.openscience.cdk.interfaces.IChemObject) IBond(org.openscience.cdk.interfaces.IBond) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test) AbstractBondTest(org.openscience.cdk.test.interfaces.AbstractBondTest)

Example 27 with IChemObject

use of org.openscience.cdk.interfaces.IChemObject in project cdk by cdk.

the class DebugChemObjectTest method testDebugChemObject_IChemObject.

@Test
public void testDebugChemObject_IChemObject() {
    IChemObject chemObject1 = new DebugChemObject();
    IChemObject chemObject = new DebugChemObject(chemObject1);
    Assert.assertNotNull(chemObject);
}
Also used : IChemObject(org.openscience.cdk.interfaces.IChemObject) AbstractChemObjectTest(org.openscience.cdk.test.interfaces.AbstractChemObjectTest) Test(org.junit.Test)

Example 28 with IChemObject

use of org.openscience.cdk.interfaces.IChemObject in project cdk by cdk.

the class DebugElementTest method testDebugElement.

@Test
public void testDebugElement() {
    IElement e = new DebugElement();
    Assert.assertTrue(e instanceof IChemObject);
}
Also used : IElement(org.openscience.cdk.interfaces.IElement) IChemObject(org.openscience.cdk.interfaces.IChemObject) AbstractElementTest(org.openscience.cdk.test.interfaces.AbstractElementTest) Test(org.junit.Test)

Example 29 with IChemObject

use of org.openscience.cdk.interfaces.IChemObject in project cdk by cdk.

the class DebugAtomTest method testDebugAtom_IElement.

@Test
public void testDebugAtom_IElement() {
    IChemObject object = newChemObject();
    IElement element = object.getBuilder().newInstance(IElement.class);
    IAtom a = new DebugAtom(element);
    Assert.assertNotNull(a);
}
Also used : IElement(org.openscience.cdk.interfaces.IElement) IChemObject(org.openscience.cdk.interfaces.IChemObject) IAtom(org.openscience.cdk.interfaces.IAtom) AbstractAtomTest(org.openscience.cdk.test.interfaces.AbstractAtomTest) Test(org.junit.Test)

Example 30 with IChemObject

use of org.openscience.cdk.interfaces.IChemObject in project cdk by cdk.

the class AtomContainer method clone.

/**
 * {@inheritDoc}
 */
@Override
public IAtomContainer clone() throws CloneNotSupportedException {
    // this is pretty wasteful as we need to delete most the data
    // we can't simply create an empty instance as the sub classes (e.g. AminoAcid)
    // would have a ClassCastException when they invoke clone
    IAtomContainer clone = (IAtomContainer) super.clone();
    // remove existing elements - we need to set the stereo elements list as list.clone() doesn't
    // work as expected and will also remove all elements from the original
    clone.setStereoElements(new ArrayList<>(stereoElements.size()));
    clone.removeAllElements();
    // create a mapping of the original atoms/bonds to the cloned atoms/bonds
    // we need this mapping to correctly clone bonds, single/paired electrons
    // and stereo elements
    // - the expected size stop the map be resized - method from Google Guava
    Map<IAtom, IAtom> atomMap = new HashMap<>(atomCount >= 3 ? atomCount + atomCount / 3 : atomCount + 1);
    Map<IBond, IBond> bondMap = new HashMap<>(bondCount >= 3 ? bondCount + bondCount / 3 : bondCount + 1);
    // clone atoms
    IAtom[] atoms = new IAtom[this.atomCount];
    for (int i = 0; i < atoms.length; i++) {
        atoms[i] = this.atoms[i].clone();
        atomMap.put(this.atoms[i], atoms[i]);
    }
    clone.setAtoms(atoms);
    // clone bonds using a the mappings from the original to the clone
    IBond[] bonds = new IBond[this.bondCount];
    for (int i = 0; i < bonds.length; i++) {
        IBond original = this.bonds[i];
        IBond bond = original.clone();
        int n = bond.getAtomCount();
        IAtom[] members = new IAtom[n];
        for (int j = 0; j < n; j++) {
            members[j] = atomMap.get(original.getAtom(j));
        }
        bond.setAtoms(members);
        bondMap.put(this.bonds[i], bond);
        bonds[i] = bond;
    }
    clone.setBonds(bonds);
    // clone lone pairs (we can't use an array to buffer as there is no setLonePairs())
    for (int i = 0; i < lonePairCount; i++) {
        ILonePair original = this.lonePairs[i];
        ILonePair pair = (ILonePair) original.clone();
        if (pair.getAtom() != null)
            pair.setAtom(atomMap.get(original.getAtom()));
        clone.addLonePair(pair);
    }
    // clone single electrons (we can't use an array to buffer as there is no setSingleElectrons())
    for (int i = 0; i < singleElectronCount; i++) {
        ISingleElectron original = this.singleElectrons[i];
        ISingleElectron electron = (ISingleElectron) original.clone();
        if (electron.getAtom() != null)
            electron.setAtom(atomMap.get(original.getAtom()));
        clone.addSingleElectron(electron);
    }
    // map each stereo element to a new instance in the clone
    for (IStereoElement element : stereoElements) {
        clone.addStereoElement(element.map(atomMap, bondMap));
    }
    // update sgroups
    Collection<Sgroup> sgroups = getProperty(CDKConstants.CTAB_SGROUPS);
    if (sgroups != null) {
        Map<IChemObject, IChemObject> replace = new HashMap<>();
        replace.putAll(atomMap);
        replace.putAll(bondMap);
        clone.setProperty(CDKConstants.CTAB_SGROUPS, SgroupManipulator.copy(sgroups, replace));
    }
    return clone;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) HashMap(java.util.HashMap) ILonePair(org.openscience.cdk.interfaces.ILonePair) IBond(org.openscience.cdk.interfaces.IBond) Sgroup(org.openscience.cdk.sgroup.Sgroup) ISingleElectron(org.openscience.cdk.interfaces.ISingleElectron) IChemObject(org.openscience.cdk.interfaces.IChemObject) IAtom(org.openscience.cdk.interfaces.IAtom) IStereoElement(org.openscience.cdk.interfaces.IStereoElement)

Aggregations

IChemObject (org.openscience.cdk.interfaces.IChemObject)195 Test (org.junit.Test)139 IAtom (org.openscience.cdk.interfaces.IAtom)61 IBond (org.openscience.cdk.interfaces.IBond)51 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)32 HashMap (java.util.HashMap)21 SuppleAtomContainer (ambit2.core.groups.SuppleAtomContainer)16 AbstractBondTest (org.openscience.cdk.test.interfaces.AbstractBondTest)16 ArrayList (java.util.ArrayList)15 ISGroup (ambit2.core.groups.ISGroup)14 IStereoElement (org.openscience.cdk.interfaces.IStereoElement)12 InputStream (java.io.InputStream)11 Sgroup (org.openscience.cdk.sgroup.Sgroup)11 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)9 ISingleElectron (org.openscience.cdk.interfaces.ISingleElectron)9 Map (java.util.Map)8 CDKException (org.openscience.cdk.exception.CDKException)8 ChemObject (org.openscience.cdk.ChemObject)7 IElement (org.openscience.cdk.interfaces.IElement)7 IOException (java.io.IOException)6