Search in sources :

Example 6 with MappedChain

use of algorithms.isomorphism.chains.MappedChain in project Smiles2Monomers by yoann-dufresne.

the class ChainsFamilyMatching method matchFamilly.

@Override
public Coverage matchFamilly(Family family) {
    if (this.co == null)
        return null;
    this.coverage = new Coverage(co);
    FamilyChainsDB fc = this.chains.getObject(family.getJsonName());
    this.toMatch.addAll(family.getRoots());
    while (!toMatch.isEmpty()) {
        Residue res = toMatch.remove();
        List<MappedChain> mcs = null;
        List<ChainAdd> adds = fc.getAdds(res);
        // If the residue is a root
        if (adds.size() == 0) {
            Chain rootChain = fc.getRootChains().get(res);
            mcs = Isomorphism.searchAChain(rootChain, this.co, this.matchingType);
        } else // From previous mapping
        {
            mcs = new ArrayList<>();
            Residue from = adds.get(0).getFrom();
            for (MappedChain mc : mappings.get(from)) {
                try {
                    MappedChain clone = mc;
                    for (ChainAdd add : adds) clone = add.applyAndClone(mc, this.matchingType);
                    mcs.add(clone);
                } catch (Exception e) {
                }
            }
        }
        // Save results and recursive add
        if (mcs.size() > 0) {
            this.mappings.put(res, mcs);
            this.addToCoverage(family, mcs, res);
            for (Residue child : fc.getFamily().getChildrenOf(res)) {
                if (fc.getAdds(child).get(0).getFrom().equals(res)) {
                    this.toMatch.add(child);
                }
            }
        }
    }
    this.mappings.clear();
    return this.coverage;
}
Also used : MappedChain(algorithms.isomorphism.chains.MappedChain) Chain(algorithms.isomorphism.chains.Chain) MappedChain(algorithms.isomorphism.chains.MappedChain) FamilyChainsDB(algorithms.isomorphism.chains.FamilyChainsDB) Residue(model.Residue) ChainAdd(algorithms.isomorphism.chains.ChainAdd) Coverage(algorithms.utils.Coverage)

Example 7 with MappedChain

use of algorithms.isomorphism.chains.MappedChain in project Smiles2Monomers by yoann-dufresne.

the class Isomorphism method searchFromPreviousMapping.

/**/
public static List<MappedChain> searchFromPreviousMapping(MappedChain mc, Extension ext, int idx1, int idx2, MatchingType type) {
    IMolecule mol = mc.getChemObject().getMolecule();
    int extIdx = idx1 == -1 ? idx2 : idx1;
    List<MappedChain> mappings = new ArrayList<>();
    // Connected bonds search
    List<IBond> connectedBonds = null;
    if (extIdx == -1) {
        connectedBonds = new ArrayList<>();
        for (IBond bond : mol.bonds()) connectedBonds.add(bond);
    } else {
        IAtom extAtom = mol.getAtom(mc.getAtomsMapping().get(extIdx));
        connectedBonds = mol.getConnectedBondsList(extAtom);
    }
    for (IBond neighbor : connectedBonds) {
        // No need to search bonds already present in mapping.
        if (mc.getBondsMapping().contains(mol.getBondNumber(neighbor)))
            continue;
        List<BondMapping> bms = ext.match(neighbor, type);
        for (BondMapping bm : bms) {
            // Verification of the compatibility with previous matching
            if (idx1 != -1 && mol.getAtomNumber(bm.a0) != mc.getAtomsMapping().get(idx1))
                continue;
            if (idx2 != -1 && mol.getAtomNumber(bm.a1) != mc.getAtomsMapping().get(idx2))
                continue;
            // Creation of the new mapping
            Chain chain = new Chain(mc.getChain(), ext, idx1, idx2);
            List<Integer> atoms = new ArrayList<>(mc.getAtomsMapping());
            Map<Integer, Integer> hydrogens = new HashMap<>(mc.getHydrogensMapping());
            if (idx1 == -1) {
                int an = mol.getAtomNumber(bm.a0);
                // To avoid cycles where there is no cycle.
                if (mc.getAtomsMapping().contains(an))
                    continue;
                atoms.add(an);
                hydrogens.put(an, bm.h0);
            }
            if (idx2 == -1) {
                int an = mol.getAtomNumber(bm.a1);
                // To avoid cycles where there is no cycle.
                if (mc.getAtomsMapping().contains(an))
                    continue;
                atoms.add(an);
                hydrogens.put(an, bm.h1);
            }
            List<Integer> bonds = new ArrayList<>(mc.getBondsMapping());
            bonds.add(mol.getBondNumber(neighbor));
            List<MatchingType> matchings = new ArrayList<>(mc.getMatchings());
            matchings.add(bm.matchingType);
            MappedChain newMc = new MappedChain(mc.getChemObject(), chain, atoms, bonds, matchings, hydrogens);
            mappings.add(newMc);
        }
    }
    return mappings;
}
Also used : MappedChain(algorithms.isomorphism.chains.MappedChain) Chain(algorithms.isomorphism.chains.Chain) MappedChain(algorithms.isomorphism.chains.MappedChain) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IBond(org.openscience.cdk.interfaces.IBond) IMolecule(org.openscience.cdk.interfaces.IMolecule) BondMapping(algorithms.isomorphism.chains.Extension.BondMapping) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 8 with MappedChain

use of algorithms.isomorphism.chains.MappedChain in project Smiles2Monomers by yoann-dufresne.

the class Isomorphism method searchFromPreviousMapping.

/**
 * Search a list of mapped blocs from a smaller mapped bloc
 * @param mb Initial mapped bloc
 * @param ext Extension to the previous mapped bloc
 * @return New mapped blocs.
 */
public static List<MappedChain> searchFromPreviousMapping(MappedChain mb, Extension ext, MatchingType type) {
    List<MappedChain> mbs = new ArrayList<>();
    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);
        List<BondMapping> matchings = ext.match(nb, type);
        for (BondMapping bm : matchings) {
            int atomIdx0 = mol.getAtomNumber(bm.a0);
            int atomIdx1 = mol.getAtomNumber(bm.a1);
            int blocPosition0 = mb.getMappingIdx(atomIdx0);
            int blocPosition1 = mb.getMappingIdx(atomIdx1);
            int hydrogen0 = bm.h0;
            int hydrogen1 = bm.h1;
            Chain bloc = new Chain(mb.getChain(), ext, blocPosition0, blocPosition1);
            List<Integer> atoms = new ArrayList<>(mb.getAtomsMapping());
            if (blocPosition0 == -1)
                atoms.add(atomIdx0);
            if (blocPosition1 == -1)
                atoms.add(atomIdx1);
            List<Integer> bonds = new ArrayList<>(mb.getBondsMapping());
            bonds.add(mol.getBondNumber(nb));
            List<MatchingType> matchingTypes = new ArrayList<>(mb.getMatchings());
            matchingTypes.add(bm.matchingType);
            Map<Integer, Integer> hydrogens = new HashMap<>(mb.getHydrogensMapping());
            if (!hydrogens.containsKey(atomIdx0) || hydrogens.get(atomIdx0) <= hydrogen0)
                hydrogens.put(atomIdx0, hydrogen0);
            if (!hydrogens.containsKey(atomIdx1) || hydrogens.get(atomIdx1) <= hydrogen1)
                hydrogens.put(atomIdx1, hydrogen1);
            MappedChain newMb = new MappedChain(co, bloc, atoms, bonds, matchingTypes, hydrogens);
            if (!mbs.contains(newMb))
                mbs.add(newMb);
        }
    }
    return mbs;
}
Also used : MappedChain(algorithms.isomorphism.chains.MappedChain) Chain(algorithms.isomorphism.chains.Chain) MappedChain(algorithms.isomorphism.chains.MappedChain) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IBond(org.openscience.cdk.interfaces.IBond) IMolecule(org.openscience.cdk.interfaces.IMolecule) ChemicalObject(model.ChemicalObject) BondMapping(algorithms.isomorphism.chains.Extension.BondMapping)

Example 9 with MappedChain

use of algorithms.isomorphism.chains.MappedChain in project Smiles2Monomers by yoann-dufresne.

the class IsomorphismTests method MappingTest.

@Test
public void MappingTest() {
    List<MappedChain> mbs = Isomorphism.searchFromPreviousMapping(this.mb0, this.ext1, MatchingType.STRONG);
    boolean isGood = true;
    for (MappedChain mb : mbs) {
        List<MappedChain> extendedMbs = Isomorphism.searchFromPreviousMapping(mb, this.ext2, MatchingType.STRONG);
        MappedChain newMb = extendedMbs.get(0);
        IMolecule mol = mb.getChemObject().getMolecule();
        IAtom newA = (mol.getAtom(newMb.getAtomsMapping().get(2)));
        if (!(// Same first atom
        (newMb.getAtomsMapping().get(0) == mb.getAtomsMapping().get(0)) && // Same second atom
        (newMb.getAtomsMapping().get(1) == mb.getAtomsMapping().get(1)) && // Extended by C atom
        (newA.getSymbol().equals("C"))))
            isGood = false;
    }
    Assert.assertTrue(isGood);
}
Also used : MappedChain(algorithms.isomorphism.chains.MappedChain) IMolecule(org.openscience.cdk.interfaces.IMolecule) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

Example 10 with MappedChain

use of algorithms.isomorphism.chains.MappedChain 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)

Aggregations

MappedChain (algorithms.isomorphism.chains.MappedChain)11 Test (org.junit.Test)5 IAtom (org.openscience.cdk.interfaces.IAtom)5 Chain (algorithms.isomorphism.chains.Chain)4 ArrayList (java.util.ArrayList)4 Polymer (model.Polymer)4 IMolecule (org.openscience.cdk.interfaces.IMolecule)4 HashMap (java.util.HashMap)3 IBond (org.openscience.cdk.interfaces.IBond)3 BondMapping (algorithms.isomorphism.chains.Extension.BondMapping)2 Residue (model.Residue)2 ChainAdd (algorithms.isomorphism.chains.ChainAdd)1 Extension (algorithms.isomorphism.chains.Extension)1 FamilyChainsDB (algorithms.isomorphism.chains.FamilyChainsDB)1 Coverage (algorithms.utils.Coverage)1 Match (algorithms.utils.Match)1 ChemicalObject (model.ChemicalObject)1 Monomer (model.Monomer)1 Rule (model.Rule)1 Before (org.junit.Before)1