Search in sources :

Example 26 with IAtom

use of org.openscience.cdk.interfaces.IAtom 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)

Example 27 with IAtom

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

the class ResidueCreatorTests method linksLoadingTest.

@Test
public void linksLoadingTest() {
    ResidueJsonLoader rjl = new ResidueJsonLoader(this.rules, this.monos);
    rjl.saveFile(this.families, "tmp.json");
    Residue.resetResidues();
    FamilyDB loaded = rjl.loadFile("tmp.json");
    new File("tmp.json").delete();
    Family famTyr = null;
    try {
        famTyr = loaded.getObject("Tyr");
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    Residue tyrN = null;
    for (Residue res : famTyr.getResidues()) if ("Tyr_pepN".equals(res.getName())) {
        tyrN = res;
        break;
    }
    Entry<IAtom, Rule> entry = tyrN.getAtomicLinks().entrySet().iterator().next();
    IAtom a = entry.getKey();
    Assert.assertEquals(a.getSymbol(), "N");
}
Also used : Residue(model.Residue) Family(model.Family) FamilyDB(db.FamilyDB) Rule(model.Rule) ResidueJsonLoader(io.loaders.json.ResidueJsonLoader) File(java.io.File) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

Example 28 with IAtom

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

the class ResidueCreatorTests method linksCreationTest.

@Test
public void linksCreationTest() {
    Residue tyrN = null;
    for (Residue res : this.residues) if ("Tyr_pepN".equals(res.getName())) {
        tyrN = res;
        break;
    }
    Entry<IAtom, Rule> entry = tyrN.getAtomicLinks().entrySet().iterator().next();
    IAtom a = entry.getKey();
    Assert.assertEquals(a.getSymbol(), "N");
}
Also used : Residue(model.Residue) Rule(model.Rule) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

Example 29 with IAtom

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

the class ResidueJsonLoader method fillLinksJSO.

@SuppressWarnings("unchecked")
private String fillLinksJSO(JSONArray links, Residue res) {
    IMolecule mol = res.getMolecule();
    AtomContainerManipulator.convertImplicitToExplicitHydrogens(mol);
    String smiles = SmilesConverter.conv.mol2Smiles(mol, false);
    List<IAtom> order = SmilesConverter.conv.getOrder();
    for (IAtom a : res.getAtomicLinks().keySet()) {
        JSONObject jso = new JSONObject();
        Rule rule = res.getAtomicLinks().get(a);
        jso.put("name", rule.getName());
        jso.put("atom", order.indexOf(a));
        links.add(jso);
    }
    AtomContainerManipulator.removeHydrogens(mol);
    return smiles;
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) JSONObject(org.json.simple.JSONObject) Rule(model.Rule) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 30 with IAtom

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

the class MonomersSerialization method serializeObject.

private void serializeObject(Monomer mono, ObjectOutputStream oos) {
    try {
        oos.writeInt(mono.getMolecule().getAtomCount());
        for (IAtom a : mono.getMolecule().atoms()) {
            Point2d p = a.getPoint2d();
            oos.writeDouble(p.x);
            oos.writeDouble(p.y);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Point2d(javax.vecmath.Point2d) IOException(java.io.IOException) IAtom(org.openscience.cdk.interfaces.IAtom)

Aggregations

IAtom (org.openscience.cdk.interfaces.IAtom)46 IMolecule (org.openscience.cdk.interfaces.IMolecule)16 IBond (org.openscience.cdk.interfaces.IBond)13 ArrayList (java.util.ArrayList)10 Residue (model.Residue)8 Rule (model.Rule)6 Bond (org.openscience.cdk.Bond)6 Molecule (org.openscience.cdk.Molecule)6 MappedChain (algorithms.isomorphism.chains.MappedChain)5 HashMap (java.util.HashMap)5 CDKException (org.openscience.cdk.exception.CDKException)5 Match (algorithms.utils.Match)4 Test (org.junit.Test)4 Chain (algorithms.isomorphism.chains.Chain)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Vector (java.util.Vector)3 Family (model.Family)3 JSONObject (org.json.simple.JSONObject)3 Before (org.junit.Before)3