Search in sources :

Example 41 with IAtom

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

the class DeepenMatcher method mask.

/**
 * Create a new molecule without covered atoms
 */
public static Molecule mask(Coverage coverage) {
    Molecule covMol = coverage.getChemicalObject().getMolecule();
    Molecule mol = new Molecule();
    List<Integer> coveredIdxs = DeepenMatcher.calculateCoveredAtoms(coverage);
    for (IAtom atom : covMol.atoms()) if (!coveredIdxs.contains(covMol.getAtomNumber(atom)))
        mol.addAtom(atom);
    HashSet<IBond> bonds = new HashSet<>();
    for (IAtom atom : mol.atoms()) {
        for (IBond bond : covMol.getConnectedBondsList(atom)) if (mol.contains(bond.getAtom(0)) && mol.contains(bond.getAtom(1))) {
            if (!bonds.contains(bond)) {
                mol.addBond(bond);
                bonds.add(bond);
            }
        }
    }
    return mol;
}
Also used : Molecule(org.openscience.cdk.Molecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) IBond(org.openscience.cdk.interfaces.IBond) IAtom(org.openscience.cdk.interfaces.IAtom) HashSet(java.util.HashSet)

Example 42 with IAtom

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

the class ChainsFamilyMatching method addToCoverage.

private void addToCoverage(Family family, MappedChain mc, Residue res) {
    Match match = new Match(res);
    match.addAtoms(mc.getAtomsMapping());
    match.addBonds(mc.getBondsMapping());
    match.addHydrogens(mc.getHydrogensMapping());
    for (int idx = 0; idx < mc.getMatchings().size(); idx++) {
        match.addQuality(mc.getBondsMapping().get(idx), mc.getMatchings().get(idx));
    }
    // Compute external bonds by aligning chain on residue
    // 1- Retrieve the chain on root residue
    int current = 200;
    List<MappedChain> onResidue = null;
    Residue rootMol = null;
    for (Residue root : family.getRoots()) {
        List<MappedChain> mcs = Isomorphism.searchAChain(mc.getChain(), root, MatchingType.EXACT);
        if (mcs.size() > 0 && current > mcs.size()) {
            current = mcs.size();
            onResidue = mcs;
            rootMol = root;
        }
    }
    // 2- Look for atoms of interest
    MappedChain resMapping = onResidue.get(0);
    for (IAtom ia : rootMol.getAtomicLinks().keySet()) {
        Rule r = rootMol.getAtomicLinks().get(ia);
        int resMolIdx = rootMol.getMolecule().getAtomNumber(ia);
        int chainIdx = resMapping.getAtomsMapping().indexOf(resMolIdx);
        match.addExtLink(mc.getAtomsMapping().get(chainIdx), r);
    }
    // Add the match
    if (!this.coverage.getMatches().contains(match))
        this.coverage.addMatch(match);
}
Also used : MappedChain(algorithms.isomorphism.chains.MappedChain) Residue(model.Residue) Rule(model.Rule) IAtom(org.openscience.cdk.interfaces.IAtom) Match(algorithms.utils.Match)

Example 43 with IAtom

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

the class Chain method getMolecule.

/**
 * construct recursively a molecule corresponding to the bloc.
 * Attention, this function is not very efficient because of multiple molecules creation.
 * @return Bloc molecule. Can return null if there are problems of compatibility between sub-bloc and extension positions.
 */
public IMolecule getMolecule() {
    if (this.mol != null)
        return this.mol;
    IMolecule mol = null;
    this.atoms = new ArrayList<>();
    // Sub-bloc molecule
    if (this.getSize() > 1) {
        IMolecule subMol = this.subBlc.getMolecule();
        mol = new Molecule();
        for (IAtom a : subMol.atoms()) mol.addAtom(a);
        for (IBond b : subMol.bonds()) mol.addBond(b);
        for (IAtom a : this.subBlc.atoms) {
            IAtom cloneA = mol.getAtom(subMol.getAtomNumber(a));
            this.atoms.add(cloneA);
        }
    } else {
        mol = new Molecule();
    }
    // Extension of mol
    IAtom a1 = this.ext.getBond().getAtom(0);
    if (this.position1 == -1) {
        try {
            a1 = (IAtom) a1.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        mol.addAtom(a1);
        this.atoms.add(a1);
        // Hydrogens
        int hydrogens = this.ext.getBond().getAtom(0).getImplicitHydrogenCount();
        a1.setImplicitHydrogenCount(hydrogens);
        // Aromatic
        if (this.ext.getBond().getAtom(0).getFlag(CDKConstants.ISAROMATIC))
            a1.setFlag(CDKConstants.ISAROMATIC, true);
        else
            a1.setFlag(CDKConstants.ISAROMATIC, false);
    } else {
        if (a1.getAtomicNumber() == this.atoms.get(this.position1).getAtomicNumber())
            a1 = this.atoms.get(this.position1);
        else
            return null;
    }
    IAtom a2 = this.ext.getBond().getAtom(1);
    if (this.position2 == -1) {
        try {
            a2 = (IAtom) a2.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        mol.addAtom(a2);
        this.atoms.add(a2);
        // Hydrogen
        int hydrogens = this.ext.getBond().getAtom(1).getImplicitHydrogenCount();
        a2.setImplicitHydrogenCount(hydrogens);
        // Aromatic
        if (this.ext.getBond().getAtom(1).getFlag(CDKConstants.ISAROMATIC))
            a2.setFlag(CDKConstants.ISAROMATIC, true);
        else
            a2.setFlag(CDKConstants.ISAROMATIC, false);
    } else if (a2.getAtomicNumber() == this.atoms.get(this.position2).getAtomicNumber())
        a2 = this.atoms.get(this.position2);
    else
        return null;
    mol.addBond(new Bond(a1, a2, this.ext.getBond().getOrder()));
    this.mol = mol;
    return mol;
}
Also used : Molecule(org.openscience.cdk.Molecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) IBond(org.openscience.cdk.interfaces.IBond) Bond(org.openscience.cdk.Bond) IBond(org.openscience.cdk.interfaces.IBond) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 44 with IAtom

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

the class Chain method getPrevArity.

public int getPrevArity() {
    IAtom a = null;
    if (this.atoms == null)
        this.getMolecule();
    if (this.position1 == -1 && this.position2 == -1)
        return 1;
    else if (this.position1 != -1) {
        a = this.atoms.get(this.position1);
    } else {
        a = this.atoms.get(this.position2);
    }
    IMolecule mol = this.getMolecule();
    int valency = 1;
    try {
        valency = sc.calculateNumberOfImplicitHydrogens(a);
    } catch (CDKException e) {
        e.printStackTrace();
    }
    int hydrogens = a.getImplicitHydrogenCount();
    int connected = mol.getConnectedAtomsCount(a);
    int arity = valency - connected - hydrogens + 1;
    return arity < 1 ? 1 : arity;
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) CDKException(org.openscience.cdk.exception.CDKException) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 45 with IAtom

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

the class ResidueCreator method createResidue.

// Create residue when it was found
private Set<Residue> createResidue(List<RMap> match, Rule rule, Residue res) {
    Set<Residue> residues = new HashSet<>();
    // Create index
    int[] index = new int[match.size()];
    for (RMap rm : match) {
        index[rm.getId2()] = rm.getId1();
    }
    for (Replacement replace : rule.getReplacements()) {
        // Clone molecule
        Molecule oldMol = res.getMolecule();
        Molecule mol = null;
        try {
            mol = (Molecule) oldMol.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        // Save links atoms
        Map<IAtom, IAtom> convesionAtom = new HashMap<>();
        for (IAtom a : res.getAtomicLinks().keySet()) {
            int idx = oldMol.getAtomNumber(a);
            IAtom newA = mol.getAtom(idx);
            convesionAtom.put(a, newA);
        }
        // Prepare deletions
        List<IAtom> deletedAtoms = new ArrayList<>();
        List<IAtom> linkedAtoms = new ArrayList<>();
        for (int i : replace.toDelete) deletedAtoms.add(mol.getAtom(index[i]));
        for (int i : replace.toReplace) {
            IAtom atom = mol.getAtom(index[i]);
            deletedAtoms.add(atom);
            for (IAtom neighbor : mol.getConnectedAtomsList(atom)) if (!neighbor.getSymbol().equals("H") && !deletedAtoms.contains(neighbor))
                linkedAtoms.add(neighbor);
        }
        // Delete atoms
        for (IAtom a : deletedAtoms) {
            for (IBond b : mol.getConnectedBondsList(a)) mol.removeBond(b);
            mol.removeAtom(a);
        }
        String smarts = this.sg.createSMILES(mol);
        if (!Residue.existingResidue(smarts, res.getMonoName())) {
            Residue residue = Residue.constructResidue(res.getMonoName(), smarts);
            residue.setMol(mol);
            // Add old links
            for (IAtom oldA : convesionAtom.keySet()) {
                IAtom newA = convesionAtom.get(oldA);
                // int oldIdx = oldMol.getAtomNumber(oldA);
                // int newIdx = mol.getAtomNumber(newA);
                residue.getAtomicLinks().put(newA, res.getAtomicLinks().get(oldA));
            }
            // Add new Links
            for (IAtom a : linkedAtoms) residue.addLink(a, rule);
            residues.add(residue);
        } else {
            Residue residue = Residue.constructResidue(res.getMonoName(), smarts);
            residues.add(residue);
        }
    }
    return residues;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IBond(org.openscience.cdk.interfaces.IBond) Replacement(model.Rule.Replacement) RMap(org.openscience.cdk.isomorphism.mcss.RMap) Molecule(org.openscience.cdk.Molecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) Residue(model.Residue) IAtom(org.openscience.cdk.interfaces.IAtom) HashSet(java.util.HashSet)

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