Search in sources :

Example 16 with IBond

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

IBond (org.openscience.cdk.interfaces.IBond)16 IAtom (org.openscience.cdk.interfaces.IAtom)11 ArrayList (java.util.ArrayList)8 IMolecule (org.openscience.cdk.interfaces.IMolecule)8 HashMap (java.util.HashMap)4 Chain (algorithms.isomorphism.chains.Chain)3 BondMapping (algorithms.isomorphism.chains.Extension.BondMapping)3 MappedChain (algorithms.isomorphism.chains.MappedChain)3 HashSet (java.util.HashSet)3 Residue (model.Residue)3 Molecule (org.openscience.cdk.Molecule)3 Match (algorithms.utils.Match)2 ChemicalObject (model.ChemicalObject)2 Bond (org.openscience.cdk.Bond)2 CDKException (org.openscience.cdk.exception.CDKException)2 Extension (algorithms.isomorphism.chains.Extension)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Vector (java.util.Vector)1 Monomer (model.Monomer)1