Search in sources :

Example 1 with RMap

use of org.openscience.cdk.isomorphism.mcss.RMap in project Smiles2Monomers by yoann-dufresne.

the class ResidueCreator method getResiduesWithRule.

/*
	 * Search Residues for one monomer and one rule.
	 */
private Set<Residue> getResiduesWithRule(Residue res, Rule rule) {
    Set<Residue> residues = new HashSet<>();
    IMolecule ruleMol = null;
    try {
        ruleMol = SmilesConverter.conv.transform(rule.getFormula(), false, false, true);
    } catch (InvalidSmilesException e) {
        System.err.println("Impossible to parse " + rule.getName() + " rule");
        return residues;
    }
    boolean status = false;
    try {
        status = UniversalIsomorphismTester.isSubgraph(res.getMolecule(), ruleMol);
    } catch (CDKException e) {
        e.printStackTrace();
    }
    // If rule is found
    if (status) {
        List<List<RMap>> matches = null;
        try {
            matches = UniversalIsomorphismTester.getSubgraphAtomsMaps(res.getMolecule(), ruleMol);
        } catch (CDKException e) {
            e.printStackTrace();
        }
        if (!"".equals(rule.getExclusion()))
            for (String exclusion : rule.getExclusion()) this.removeExclusions(matches, exclusion, res.getMolecule());
        for (List<RMap> match : matches) {
            Set<Residue> residuesByMatch = this.createResidue(match, rule, res);
            residues.addAll(residuesByMatch);
        }
    }
    return residues;
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) Residue(model.Residue) CDKException(org.openscience.cdk.exception.CDKException) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException) RMap(org.openscience.cdk.isomorphism.mcss.RMap) HashSet(java.util.HashSet)

Example 2 with RMap

use of org.openscience.cdk.isomorphism.mcss.RMap 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)

Example 3 with RMap

use of org.openscience.cdk.isomorphism.mcss.RMap in project Smiles2Monomers by yoann-dufresne.

the class ResidueCreator method removeExclusions.

/*
	 * Remove results corresponding to an exclusion in rule
	 */
private void removeExclusions(List<List<RMap>> matches, String ex, IMolecule mol) {
    // Verifying exclusions
    boolean areExclusion = false;
    IMolecule exclusionMol = null;
    try {
        exclusionMol = SmilesConverter.conv.transform(ex, false, false, true);
    } catch (InvalidSmilesException e) {
        System.err.println("Impossible to parse " + ex);
        return;
    }
    try {
        areExclusion = UniversalIsomorphismTester.isSubgraph(mol, exclusionMol);
    } catch (CDKException e) {
        e.printStackTrace();
    }
    if (areExclusion) {
        List<RMap> toExclude = null;
        boolean flag = false;
        List<List<RMap>> exclusions = null;
        try {
            exclusions = UniversalIsomorphismTester.getSubgraphAtomsMaps(mol, exclusionMol);
        } catch (CDKException e) {
            e.printStackTrace();
        }
        for (List<RMap> exclusion : exclusions) {
            for (List<RMap> match : matches) {
                for (RMap rmMatch : match) {
                    for (RMap exId : exclusion) {
                        if (rmMatch.getId1() == exId.getId1()) {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                        break;
                }
                if (flag) {
                    toExclude = match;
                    break;
                }
            }
            if (flag) {
                matches.remove(toExclude);
                toExclude = null;
                flag = false;
            }
        }
    }
/**/
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) CDKException(org.openscience.cdk.exception.CDKException) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException) RMap(org.openscience.cdk.isomorphism.mcss.RMap)

Aggregations

ArrayList (java.util.ArrayList)3 IMolecule (org.openscience.cdk.interfaces.IMolecule)3 RMap (org.openscience.cdk.isomorphism.mcss.RMap)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Residue (model.Residue)2 CDKException (org.openscience.cdk.exception.CDKException)2 InvalidSmilesException (org.openscience.cdk.exception.InvalidSmilesException)2 HashMap (java.util.HashMap)1 Replacement (model.Rule.Replacement)1 Molecule (org.openscience.cdk.Molecule)1 IAtom (org.openscience.cdk.interfaces.IAtom)1 IBond (org.openscience.cdk.interfaces.IBond)1