Search in sources :

Example 1 with Sgroup

use of org.openscience.cdk.sgroup.Sgroup in project cdk by cdk.

the class GeometryUtil method translate2D.

/**
 * Translates a molecule from the origin to a new point denoted by a vector. See comment for
 * center(IAtomContainer atomCon, Dimension areaDim, HashMap renderingCoordinates) for details
 * on coordinate sets
 *
 * @param atomCon molecule to be translated
 * @param vector  dimension that represents the translation vector
 */
public static void translate2D(IAtomContainer atomCon, Vector2d vector) {
    for (IAtom atom : atomCon.atoms()) {
        if (atom.getPoint2d() != null) {
            atom.getPoint2d().add(vector);
        } else {
            logger.warn("Could not translate atom in 2D space");
        }
    }
    // translate Sgroup brackets
    if (atomCon.getProperty(CDKConstants.CTAB_SGROUPS) != null) {
        List<Sgroup> sgroups = atomCon.getProperty(CDKConstants.CTAB_SGROUPS);
        for (Sgroup sgroup : sgroups) {
            List<SgroupBracket> brackets = sgroup.getValue(SgroupKey.CtabBracket);
            if (brackets != null) {
                for (SgroupBracket bracket : brackets) {
                    bracket.getFirstPoint().add(vector);
                    bracket.getSecondPoint().add(vector);
                }
            }
        }
    }
}
Also used : SgroupBracket(org.openscience.cdk.sgroup.SgroupBracket) IAtom(org.openscience.cdk.interfaces.IAtom) Sgroup(org.openscience.cdk.sgroup.Sgroup)

Example 2 with Sgroup

use of org.openscience.cdk.sgroup.Sgroup in project cdk by cdk.

the class GeometryUtil method scaleMolecule.

/**
 * Multiplies all the coordinates of the atoms of the given molecule with the scalefactor. See
 * comment for center(IAtomContainer atomCon, Dimension areaDim, HashMap renderingCoordinates)
 * for details on coordinate sets
 *
 * @param atomCon     The molecule to be scaled
 * @param scaleFactor Description of the Parameter
 */
public static void scaleMolecule(IAtomContainer atomCon, double scaleFactor) {
    for (int i = 0; i < atomCon.getAtomCount(); i++) {
        if (atomCon.getAtom(i).getPoint2d() != null) {
            atomCon.getAtom(i).getPoint2d().scale(scaleFactor);
        }
    }
    // scale Sgroup brackets
    if (atomCon.getProperty(CDKConstants.CTAB_SGROUPS) != null) {
        List<Sgroup> sgroups = atomCon.getProperty(CDKConstants.CTAB_SGROUPS);
        for (Sgroup sgroup : sgroups) {
            List<SgroupBracket> brackets = sgroup.getValue(SgroupKey.CtabBracket);
            if (brackets != null) {
                for (SgroupBracket bracket : brackets) {
                    bracket.getFirstPoint().scale(scaleFactor);
                    bracket.getSecondPoint().scale(scaleFactor);
                }
            }
        }
    }
}
Also used : SgroupBracket(org.openscience.cdk.sgroup.SgroupBracket) Sgroup(org.openscience.cdk.sgroup.Sgroup)

Example 3 with Sgroup

use of org.openscience.cdk.sgroup.Sgroup 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)

Example 4 with Sgroup

use of org.openscience.cdk.sgroup.Sgroup in project cdk by cdk.

the class AtomContainer method setAtom.

/**
 * {@inheritDoc}
 */
@Override
public void setAtom(int idx, IAtom atom) {
    if (idx >= atomCount)
        throw new IndexOutOfBoundsException("No atom at index: " + idx);
    int aidx = indexOf(atom);
    if (aidx >= 0)
        throw new IllegalArgumentException("Atom already in container at index: " + idx);
    final IAtom oldAtom = atoms[idx];
    atoms[idx] = atom;
    // update electron containers
    for (IBond bond : bonds()) {
        for (int i = 0; i < bond.getAtomCount(); i++) {
            if (oldAtom.equals(bond.getAtom(i))) {
                bond.setAtom(atom, i);
            }
        }
    }
    for (ISingleElectron ec : singleElectrons()) {
        if (oldAtom.equals(ec.getAtom()))
            ec.setAtom(atom);
    }
    for (ILonePair lp : lonePairs()) {
        if (oldAtom.equals(lp.getAtom()))
            lp.setAtom(atom);
    }
    // update stereo
    List<IStereoElement> oldStereo = null;
    List<IStereoElement> newStereo = null;
    for (IStereoElement se : stereoElements()) {
        if (se.contains(oldAtom)) {
            if (oldStereo == null) {
                oldStereo = new ArrayList<>();
                newStereo = new ArrayList<>();
            }
            oldStereo.add(se);
            Map<IAtom, IAtom> amap = Collections.singletonMap(oldAtom, atom);
            Map<IBond, IBond> bmap = Collections.emptyMap();
            newStereo.add(se.map(amap, bmap));
        }
    }
    if (oldStereo != null) {
        stereoElements.removeAll(oldStereo);
        stereoElements.addAll(newStereo);
    }
    // update Sgroups
    List<Sgroup> sgroups = getProperty(CDKConstants.CTAB_SGROUPS);
    if (sgroups != null) {
        for (Sgroup sgroup : sgroups) {
            if (sgroup.getAtoms().contains(oldAtom)) {
                sgroup.removeAtom(oldAtom);
                sgroup.addAtom(atom);
            }
        }
    }
}
Also used : ILonePair(org.openscience.cdk.interfaces.ILonePair) IBond(org.openscience.cdk.interfaces.IBond) Sgroup(org.openscience.cdk.sgroup.Sgroup) ISingleElectron(org.openscience.cdk.interfaces.ISingleElectron) IAtom(org.openscience.cdk.interfaces.IAtom) IStereoElement(org.openscience.cdk.interfaces.IStereoElement)

Example 5 with Sgroup

use of org.openscience.cdk.sgroup.Sgroup in project cdk by cdk.

the class AtomContainer2 method validateStereoAndSgroups.

private void validateStereoAndSgroups() {
    // remove any Stereochemistry and Sgroups which now have "stale" atoms
    if (!stereo.isEmpty()) {
        List<IStereoElement<?, ?>> staleStereo = new ArrayList<>();
        for (IStereoElement<?, ?> se : stereo) {
            if (isStale(se))
                staleStereo.add(se);
        }
        stereo.removeAll(staleStereo);
    }
    List<Sgroup> sgroups = getProperty(CDKConstants.CTAB_SGROUPS);
    if (sgroups != null) {
        List<Sgroup> staleSgroups = new ArrayList<>();
        for (Sgroup sgroup : sgroups) {
            if (isStale(sgroup))
                staleSgroups.add(sgroup);
        }
        if (!staleSgroups.isEmpty()) {
            // ensure mutable
            sgroups = new ArrayList<>(sgroups);
            sgroups.removeAll(staleSgroups);
            setProperty(CDKConstants.CTAB_SGROUPS, sgroups);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IStereoElement(org.openscience.cdk.interfaces.IStereoElement) Sgroup(org.openscience.cdk.sgroup.Sgroup)

Aggregations

Sgroup (org.openscience.cdk.sgroup.Sgroup)112 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)71 Test (org.junit.Test)61 IAtom (org.openscience.cdk.interfaces.IAtom)51 IBond (org.openscience.cdk.interfaces.IBond)40 ArrayList (java.util.ArrayList)38 IStereoElement (org.openscience.cdk.interfaces.IStereoElement)24 HashMap (java.util.HashMap)20 SgroupBracket (org.openscience.cdk.sgroup.SgroupBracket)17 ISingleElectron (org.openscience.cdk.interfaces.ISingleElectron)14 Point2d (javax.vecmath.Point2d)13 HashSet (java.util.HashSet)12 ILonePair (org.openscience.cdk.interfaces.ILonePair)12 IChemObject (org.openscience.cdk.interfaces.IChemObject)11 Map (java.util.Map)10 CDKException (org.openscience.cdk.exception.CDKException)10 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)10 List (java.util.List)9 Vector2d (javax.vecmath.Vector2d)8 AtomContainer (org.openscience.cdk.AtomContainer)8