Search in sources :

Example 31 with Residue

use of model.Residue in project Smiles2Monomers by yoann-dufresne.

the class IsomorphismFamilyMatcher method matchFamilly.

@Override
public Coverage matchFamilly(Family family) {
    // Initialization
    Coverage cov = new Coverage(co);
    IMolecule mol = co.getMolecule();
    Set<Residue> markSet = new HashSet<>();
    Stack<Residue> searchSet = new Stack<>();
    searchSet.addAll(family.getRoots());
    // For all the nodes in the search group.
    while (!searchSet.isEmpty()) {
        Residue currentRes = searchSet.pop();
        // Search the current residue in mol.
        try {
            this.sqt.setSmarts(currentRes.getSmiles());
        } catch (CDKException e) {
            e.printStackTrace();
        }
        boolean isMatching = false;
        try {
            long time = System.currentTimeMillis();
            isMatching = this.sqt.matches(mol);
            if (verbose)
                System.out.println("    Search for " + currentRes.getName() + " in " + (System.currentTimeMillis() - time));
        } catch (CDKException e) {
            e.printStackTrace();
        }
        // If there is at least one occurrence.
        if (isMatching) {
            // Add matches to the coverage
            List<Match> matches = new ArrayList<>();
            for (List<Integer> lMatch : sqt.getMatchingAtoms()) {
                Match match = new Match(currentRes);
                for (int i : lMatch) match.addAtom(i);
                matches.add(match);
            }
            // Change to compare.
            cov.addListMatches(currentRes, matches);
            // Mark current residue to possibly add children.
            markSet.add(currentRes);
            // Add children with all parents marked
            Set<Residue> children = family.getChildrenOf(currentRes);
            for (Residue child : children) {
                boolean canBeAdded = true;
                for (Link dependance : family.getDepandances()) if (dependance.getTo().equals(child))
                    if (!markSet.contains(dependance.getFrom())) {
                        canBeAdded = false;
                        break;
                    }
                if (canBeAdded)
                    searchSet.add(child);
            }
        }
    }
    return cov;
}
Also used : CDKException(org.openscience.cdk.exception.CDKException) ArrayList(java.util.ArrayList) Coverage(algorithms.utils.Coverage) Stack(java.util.Stack) Match(algorithms.utils.Match) IMolecule(org.openscience.cdk.interfaces.IMolecule) Residue(model.Residue) Link(model.Family.Link) HashSet(java.util.HashSet)

Example 32 with Residue

use of model.Residue in project Smiles2Monomers by yoann-dufresne.

the class ChainLearning method initLearn.

// -------------------------------- Initialization ----------------------------------
private void initLearn(List<Residue> roots, ResidueMappings mappings) {
    for (Residue res : roots) {
        List<MappedChain> resMappings = new ArrayList<>();
        for (IBond bond : res.getMolecule().bonds()) {
            Extension ext = new Extension(bond);
            Chain bloc = new Chain(ext);
            String smiles = bloc.getMySmiles();
            this.chains.put(smiles, bloc);
            this.frequence.put(smiles, 0);
            // Creation of residue matchings (sequences of size 1)
            for (BondMapping bm : ext.match(bond, MatchingType.EXACT)) {
                MappedChain resMap = this.createMappingFromMatch(res, bond, bloc, bm);
                resMappings.add(resMap);
            }
        }
        mappings.put(res, resMappings);
    }
    this.frequencesInit();
}
Also used : Residue(model.Residue) ArrayList(java.util.ArrayList) IBond(org.openscience.cdk.interfaces.IBond) BondMapping(algorithms.isomorphism.chains.Extension.BondMapping)

Example 33 with Residue

use of model.Residue in project Smiles2Monomers by yoann-dufresne.

the class ChainLearning method addAddsToSons.

// -------------------------------------- Children extensions -------------------------------------
private void addAddsToSons(FamilyChainsDB fc, Family fam) {
    for (Residue root : fam.getRoots()) {
        fc.addRootChain(root, this.finalChains.get(root.getId()));
        Set<Residue> children = fam.getChildrenOf(root);
        this.recurChildrenAdds(fc, root, children);
    }
}
Also used : Residue(model.Residue)

Example 34 with Residue

use of model.Residue 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 35 with Residue

use of model.Residue in project Smiles2Monomers by yoann-dufresne.

the class ResidueCreator method createResidues.

/**
 * Create all residues from the argument monomers database according to rules entered in paramaters of the constructor.
 * @param monosDBName A monomers database.
 * @return All possible residues found.
 */
public FamilyDB createResidues(MonomersDB monosDB) {
    FamilyDB famDB = new FamilyDB();
    famDB.init(monosDB);
    for (Family family : famDB.getFamilies()) {
        this.residuesFromMonomers(family);
        for (Residue res : family.getResidues()) {
            IMolecule oldMol = res.getMolecule();
            res.setMol(new Molecule(AtomContainerManipulator.removeHydrogens(res.getMolecule())));
            IMolecule newMol = res.getMolecule();
            Map<IAtom, IAtom> conversion = new HashMap<>();
            int idx = 0;
            for (IAtom a : oldMol.atoms()) {
                if (!"H".equals(a.getSymbol())) {
                    conversion.put(a, newMol.getAtom(idx));
                    idx++;
                }
            }
            Map<IAtom, Rule> oldLinks = new HashMap<>(res.getAtomicLinks());
            res.getAtomicLinks().clear();
            for (IAtom oldA : oldLinks.keySet()) {
                Rule rule = oldLinks.get(oldA);
                res.addLink(conversion.get(oldA), rule);
            }
        }
    }
    return famDB;
}
Also used : Molecule(org.openscience.cdk.Molecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) Residue(model.Residue) HashMap(java.util.HashMap) Family(model.Family) FamilyDB(db.FamilyDB) Rule(model.Rule) IAtom(org.openscience.cdk.interfaces.IAtom)

Aggregations

Residue (model.Residue)35 Family (model.Family)11 ArrayList (java.util.ArrayList)10 IAtom (org.openscience.cdk.interfaces.IAtom)8 JSONArray (org.json.simple.JSONArray)7 JSONObject (org.json.simple.JSONObject)7 IMolecule (org.openscience.cdk.interfaces.IMolecule)7 Monomer (model.Monomer)6 Rule (model.Rule)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 Coverage (algorithms.utils.Coverage)4 Match (algorithms.utils.Match)4 FamilyDB (db.FamilyDB)4 File (java.io.File)4 FamilyChainsDB (algorithms.isomorphism.chains.FamilyChainsDB)3 RulesDB (db.RulesDB)3 ResidueJsonLoader (io.loaders.json.ResidueJsonLoader)3 Color (java.awt.Color)3 Link (model.Family.Link)3