Search in sources :

Example 1 with PseudoAtom

use of org.openscience.cdk.PseudoAtom in project Smiles2Monomers by yoann-dufresne.

the class SmilesGenerator method parseAtom.

/**
 *  Generates the SMILES string for the atom
 *
 *@param  a                        the atom to generate the SMILES for.
 *@param  buffer                   the string buffer that the atom is to be
 *      apended to.
 *@param  container                the AtomContainer to analyze.
 *@param  chiral                   is a chiral smiles wished?
 *@param  parent                   the atom we came from.
 *@param  atomsInOrderOfSmiles     a vector containing the atoms in the order
 *      they are in the smiles.
 *@param  currentChain             The chain we currently deal with.
 *@param useAromaticity				true=aromaticity or sp2 will trigger lower case letters, wrong=only sp2
 */
private void parseAtom(IAtom a, StringBuffer buffer, IAtomContainer container, boolean chiral, boolean[] doubleBondConfiguration, IAtom parent, List<IAtom> atomsInOrderOfSmiles, List<Object> currentChain, boolean useAromaticity) {
    if (!"H".equals(a.getSymbol()))
        this.order.add(a);
    String symbol = a.getSymbol();
    if (a instanceof PseudoAtom)
        symbol = "*";
    boolean stereo = false;
    if (chiral)
        stereo = BondTools.isStereo(container, a);
    boolean brackets = symbol.equals("B") || symbol.equals("C") || symbol.equals("N") || symbol.equals("O") || symbol.equals("P") || symbol.equals("S") || symbol.equals("F") || symbol.equals("Br") || symbol.equals("I") || symbol.equals("Cl");
    brackets = !brackets;
    // Deal with the start of a double bond configuration
    if (chiral && isStartOfDoubleBond(container, a, parent, doubleBondConfiguration)) {
        buffer.append('/');
    }
    String mass = generateMassString(a);
    brackets = brackets | !mass.equals("");
    String charge = generateChargeString(a);
    brackets = brackets | !charge.equals("");
    // we put in a special check for N.planar3 cases such
    // as for indole and pyrrole, which require an explicit
    // H on the nitrogen. However this only makes sense when
    // the connectivity is not 3 - so for a case such as n1ncn(c1)CC
    // the PLANAR3 N already has 3 bonds, so don't add a H for this case
    boolean isSpecialNitrogen = a.getSymbol().equals("N") && a.getHybridization() == IAtomType.Hybridization.PLANAR3 && container.getConnectedAtomsList(a).size() != 3 && (a.getFormalCharge() == null || a.getFormalCharge() == 0);
    brackets = brackets | isSpecialNitrogen;
    if (chiral && stereo && (BondTools.isTrigonalBipyramidalOrOctahedral(container, a) != 0 || BondTools.isSquarePlanar(container, a) || BondTools.isTetrahedral(container, a, false) != 0 || BondTools.isSquarePlanar(container, a))) {
        brackets = true;
    }
    if (brackets) {
        buffer.append('[');
    }
    buffer.append(mass);
    if ((useAromaticity && a.getFlag(CDKConstants.ISAROMATIC))) {
        // the PLANAR3 N already has 3 bonds, so don't add a H for this case
        if (isSpecialNitrogen) {
            buffer.append(a.getSymbol().toLowerCase()).append("H");
        } else
            buffer.append(a.getSymbol().toLowerCase());
    } else {
        buffer.append(symbol);
        if (symbol.equals("*") && a.getImplicitHydrogenCount() != null && a.getImplicitHydrogenCount() > 0)
            buffer.append("H").append(a.getImplicitHydrogenCount());
    }
    if (a.getProperty(RING_CONFIG) != null && a.getProperty(RING_CONFIG).equals(UP)) {
        buffer.append('/');
    }
    if (a.getProperty(RING_CONFIG) != null && a.getProperty(RING_CONFIG).equals(DOWN)) {
        buffer.append('\\');
    }
    if (chiral && stereo && (BondTools.isTrigonalBipyramidalOrOctahedral(container, a) != 0 || BondTools.isSquarePlanar(container, a) || BondTools.isTetrahedral(container, a, false) != 0)) {
        buffer.append('@');
    }
    if (chiral && stereo && BondTools.isSquarePlanar(container, a)) {
        buffer.append("SP1");
    }
    // chiral
    // hcount
    buffer.append(charge);
    if (brackets) {
        buffer.append(']');
    }
    // Deal with the end of a double bond configuration
    if (chiral && isEndOfDoubleBond(container, a, parent, doubleBondConfiguration)) {
        IAtom viewFrom = null;
        for (int i = 0; i < currentChain.size(); i++) {
            if (currentChain.get(i) == parent) {
                int k = i - 1;
                while (k > -1) {
                    if (currentChain.get(k) instanceof IAtom) {
                        viewFrom = (IAtom) currentChain.get(k);
                        break;
                    }
                    k--;
                }
            }
        }
        if (viewFrom == null) {
            for (int i = 0; i < atomsInOrderOfSmiles.size(); i++) {
                if (atomsInOrderOfSmiles.get(i) == parent) {
                    viewFrom = (IAtom) atomsInOrderOfSmiles.get(i - 1);
                }
            }
        }
        boolean afterThisAtom = false;
        IAtom viewTo = null;
        for (int i = 0; i < currentChain.size(); i++) {
            if (afterThisAtom && currentChain.get(i) instanceof IAtom) {
                viewTo = (IAtom) currentChain.get(i);
                break;
            }
            if (afterThisAtom && currentChain.get(i) instanceof List) {
                viewTo = (IAtom) ((List<?>) currentChain.get(i)).get(0);
                break;
            }
            if (a == currentChain.get(i)) {
                afterThisAtom = true;
            }
        }
        try {
            if (BondTools.isCisTrans(viewFrom, a, parent, viewTo, container)) {
                buffer.append('\\');
            } else {
                buffer.append('/');
            }
        } catch (CDKException ex) {
        // If the user wants a double bond configuration, where there is none, we ignore this.
        }
    }
    List<IAtom> v = new Vector<IAtom>();
    Iterator<Integer> it = getRingOpenings(a, v).iterator();
    Iterator<IAtom> it2 = v.iterator();
    // logger.debug("in parseAtom() after checking for Ring openings");
    while (it.hasNext()) {
        Integer integer = (Integer) it.next();
        IAtom a2 = (IAtom) it2.next();
        IBond b = container.getBond(a2, a);
        IBond.Order type = b.getOrder();
        if (!(useAromaticity && a.getFlag(CDKConstants.ISAROMATIC) && a2.getFlag(CDKConstants.ISAROMATIC))) {
            if (type == IBond.Order.DOUBLE) {
                buffer.append("=");
            } else if (type == IBond.Order.TRIPLE) {
                buffer.append("#");
            }
        }
        if (integer >= 10)
            buffer.append("%" + integer);
        else
            buffer.append(integer);
    }
    atomsInOrderOfSmiles.add(a);
// logger.debug("End of parseAtom()");
}
Also used : CDKException(org.openscience.cdk.exception.CDKException) IBond(org.openscience.cdk.interfaces.IBond) IPseudoAtom(org.openscience.cdk.interfaces.IPseudoAtom) PseudoAtom(org.openscience.cdk.PseudoAtom) ArrayList(java.util.ArrayList) List(java.util.List) Vector(java.util.Vector) IAtom(org.openscience.cdk.interfaces.IAtom)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Vector (java.util.Vector)1 PseudoAtom (org.openscience.cdk.PseudoAtom)1 CDKException (org.openscience.cdk.exception.CDKException)1 IAtom (org.openscience.cdk.interfaces.IAtom)1 IBond (org.openscience.cdk.interfaces.IBond)1 IPseudoAtom (org.openscience.cdk.interfaces.IPseudoAtom)1