Search in sources :

Example 11 with IMolecule

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

the class ChainLearning method recurChildrenAdds.

private void recurChildrenAdds(FamilyChainsDB fc, Residue from, Set<Residue> children) {
    for (Residue child : children) {
        List<ChainAdd> adds = fc.getAdds(child);
        // If extensions already exist, jump to the next son.
        if (adds.size() > 0)
            continue;
        List<MappedChain> mcs = this.calculateMapping(fc, child, from, new MappedChain(child));
        // Collections.sort(mcs, new MappedChainComparator());
        MappedChain mc = mcs.get(0);
        // Hydrogen adds
        for (int atomIdx : mc.getAtomsMapping()) {
            IAtom a = mc.getChemObject().getMolecule().getAtom(atomIdx);
            int chainIdx = mc.getAtomsMapping().indexOf(atomIdx);
            Map<Integer, Integer> mapping = mc.getHydrogensMapping();
            int mappingHydrogens = mapping.containsKey(atomIdx) ? mapping.get(atomIdx) : 0;
            int molHydrogens = a.getImplicitHydrogenCount();
            if (mappingHydrogens < molHydrogens)
                adds.add(new HydrogenAdd(from, chainIdx, molHydrogens - mappingHydrogens));
        }
        // Extensions
        IMolecule resMol = mc.getChemObject().getMolecule();
        for (IBond bond : resMol.bonds()) {
            int bondIdx = resMol.getBondNumber(bond);
            if (mc.getBondsMapping().contains(bondIdx))
                continue;
            Extension ext = new Extension(bond);
            List<MappedChain> tmpMappings = Isomorphism.searchFromPreviousMapping(mc, ext, MatchingType.EXACT);
            for (MappedChain tmp : tmpMappings) if (tmp.getBondsMapping().get(tmp.getBondsMapping().size() - 1) == bondIdx) {
                Chain chain = tmp.getChain();
                BondAdd ba = new BondAdd(from, ext, chain.getPosition1(), chain.getPosition2());
                adds.add(ba);
            }
        }
        /**/
        this.recurChildrenAdds(fc, child, fc.getFamily().getChildrenOf(child));
    }
}
Also used : IBond(org.openscience.cdk.interfaces.IBond) IMolecule(org.openscience.cdk.interfaces.IMolecule) Residue(model.Residue) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 12 with IMolecule

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

the class Modulation method indexCoverage.

/**
 * Read a coverage and extract for each position all the possible matchings
 * @param cov
 * @return
 */
private void indexCoverage(Coverage cov) {
    this.index = new HashMap<>();
    this.usedIndex = new HashMap<>();
    IMolecule mol = cov.getChemicalObject().getMolecule();
    for (IAtom a : mol.atoms()) {
        this.index.put(mol.getAtomNumber(a), new HashSet<Match>());
        this.usedIndex.put(mol.getAtomNumber(a), new HashSet<Match>());
    }
    // Global index
    for (Match match : cov.getMatches()) for (int i : match.getAtoms()) this.index.get(i).add(match);
    // Used index
    for (Match match : cov.getUsedMatches()) for (int i : match.getAtoms()) this.usedIndex.get(i).add(match);
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) IAtom(org.openscience.cdk.interfaces.IAtom) Match(algorithms.utils.Match)

Example 13 with IMolecule

use of org.openscience.cdk.interfaces.IMolecule 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 14 with IMolecule

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

the class ResidueCreator method residuesFromMonomers.

/*
	 * Create residues from a monomer according to the rules database.
	 */
private Set<Residue> residuesFromMonomers(Family family) {
    Monomer mono = family.getMonomers().get(0);
    Set<Residue> residues = new HashSet<>();
    RulesDB rules = new RulesDB();
    rules.addDB(this.dbAlone);
    // List of residues in witch we search other residues.
    Set<Residue> searchResidues = new HashSet<>();
    IMolecule m = null;
    try {
        m = mono.getMolecule().clone();
        AtomContainerManipulator.convertImplicitToExplicitHydrogens(m);
    } catch (CloneNotSupportedException e1) {
        e1.printStackTrace();
    }
    searchResidues.add(new Residue(mono.getCode(), this.sg.createSMILES(m), true));
    boolean firstTime = true;
    while (searchResidues.size() != 0) {
        Set<Residue> nextLevelResidues = new HashSet<>();
        for (Residue res : searchResidues) {
            res.setExplicitHydrogens(true);
            Set<Residue> newResidues = new HashSet<>();
            for (Rule rule : rules.getObjects()) {
                Set<Residue> ruleResidues = this.getResiduesWithRule(res, rule);
                newResidues.addAll(ruleResidues);
                for (Residue r : ruleResidues) nextLevelResidues.add(r);
            }
            for (Residue newRes : newResidues) {
                family.addResidue(newRes);
                if (family.containsMonomer(res.getMonoName()) && res.getAtomicLinks().size() > 0)
                    family.addDependance(newRes, res);
            }
            res.setExplicitHydrogens(false);
        }
        searchResidues = nextLevelResidues;
        residues.addAll(searchResidues);
        if (firstTime) {
            firstTime = false;
            rules.addDB(this.dbNotAlone);
        }
    }
    if (residues.size() == 0) {
        String smiles = this.sg.createSMILES(m);
        Residue res = Residue.constructResidue(mono.getName(), smiles);
        family.addResidue(res);
        residues.add(res);
    }
    if (this.verbose) {
        System.out.println("Nb residues of " + mono.getCode() + " : " + residues.size());
        for (Residue res : residues) System.out.println("  " + res.getSmiles());
        System.out.println();
    }
    return residues;
}
Also used : RulesDB(db.RulesDB) IMolecule(org.openscience.cdk.interfaces.IMolecule) Residue(model.Residue) Rule(model.Rule) Monomer(model.Monomer) HashSet(java.util.HashSet)

Example 15 with IMolecule

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

the class ChainLearning method createBlocsFromPrevious.

private List<MappedChain> createBlocsFromPrevious(List<MappedChain> prevMbs) {
    List<MappedChain> nextMbs = new ArrayList<>();
    for (MappedChain mb : prevMbs) {
        ChemicalObject co = mb.getChemObject();
        IMolecule mol = co.getMolecule();
        List<Integer> neighbors = mb.getNeighborsBonds(mol);
        // Create a new bloc for each neighbor
        for (int idx : neighbors) {
            // Create bloc
            IBond nb = mol.getBond(idx);
            Extension ext = new Extension(nb);
            // Extension.setAromacityTest(false);
            List<MappedChain> newMbs = Isomorphism.searchFromPreviousMapping(mb, ext, MatchingType.EXACT);
            // Extension.setAromacityTest(true);
            nextMbs.addAll(newMbs);
        }
    }
    return nextMbs;
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) ArrayList(java.util.ArrayList) IBond(org.openscience.cdk.interfaces.IBond) ChemicalObject(model.ChemicalObject)

Aggregations

IMolecule (org.openscience.cdk.interfaces.IMolecule)28 IAtom (org.openscience.cdk.interfaces.IAtom)12 ArrayList (java.util.ArrayList)7 InvalidSmilesException (org.openscience.cdk.exception.InvalidSmilesException)7 Residue (model.Residue)6 IBond (org.openscience.cdk.interfaces.IBond)6 HashSet (java.util.HashSet)5 Test (org.junit.Test)5 CDKException (org.openscience.cdk.exception.CDKException)5 MappedChain (algorithms.isomorphism.chains.MappedChain)4 Match (algorithms.utils.Match)4 HashMap (java.util.HashMap)4 Rule (model.Rule)3 JSONObject (org.json.simple.JSONObject)3 Chain (algorithms.isomorphism.chains.Chain)2 BondMapping (algorithms.isomorphism.chains.Extension.BondMapping)2 MonomersDB (db.MonomersDB)2 PolymersDB (db.PolymersDB)2 PolymersJsonLoader (io.loaders.json.PolymersJsonLoader)2 IOException (java.io.IOException)2