Search in sources :

Example 6 with Match

use of algorithms.utils.Match in project Smiles2Monomers by yoann-dufresne.

the class DeepenMatcher method transform.

/**
 * Transformation of mappings from tmp molecule to entire molecule.
 */
public static Match transform(Match match, IMolecule sub, IMolecule entire) {
    Match transformed = new Match(match.getResidue());
    // Atoms & Hydrogens
    for (int idx : match.getAtoms()) {
        IAtom a = sub.getAtom(idx);
        int realIdx = entire.getAtomNumber(a);
        transformed.addAtom(realIdx);
        transformed.addHydrogens(realIdx, match.getHydrogensFrom(idx));
    }
    // Bonds
    for (int idx : match.getBonds()) {
        IBond b = sub.getBond(idx);
        transformed.addBond(entire.getBondNumber(b));
    }
    return transformed;
}
Also used : IBond(org.openscience.cdk.interfaces.IBond) IAtom(org.openscience.cdk.interfaces.IAtom) Match(algorithms.utils.Match)

Example 7 with Match

use of algorithms.utils.Match in project Smiles2Monomers by yoann-dufresne.

the class CoveragesJsonLoader method objectFromJson.

@Override
protected Coverage objectFromJson(JSONObject obj) {
    // TODO : Create new parser with the files created by the new output functions
    System.err.println("From classe '" + CoveragesJsonLoader.class.getName() + "' method 'objectFromJson':");
    System.err.println("This is deprecated ! You will have some problems with recent json files !");
    ChemicalObject co = null;
    try {
        co = this.db.getObject("" + ((Number) obj.get("peptide")).intValue());
    } catch (NullPointerException e) {
        System.err.println(e.getMessage());
        System.err.println("Maybe coverage json and molecule json don't match");
        System.exit(2);
    }
    Coverage cov = new Coverage(co);
    JSONArray array = (JSONArray) obj.get("matches");
    for (Object o : array) {
        JSONObject jso = (JSONObject) o;
        Residue res = null;
        try {
            res = this.residues.getObject("" + ((Number) jso.get("residue")).intValue());
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        JSONObject jMatch = (JSONObject) jso.get("match");
        Match match = new Match(res);
        JSONArray atoms = (JSONArray) jMatch.get("atoms");
        for (Object atObj : atoms) {
            JSONObject atom = (JSONObject) atObj;
            int idx = ((Number) atom.get("a")).intValue();
            match.addAtom(idx);
            match.addHydrogens(idx, ((Number) atom.get("h")).intValue());
        }
        JSONArray bonds = (JSONArray) jMatch.get("bonds");
        for (Object boObj : bonds) {
            int bond = ((Number) boObj).intValue();
            match.addBond(bond);
        }
        cov.addMatch(match);
    }
    cov.calculateGreedyCoverage();
    return cov;
}
Also used : JSONObject(org.json.simple.JSONObject) Residue(model.Residue) JSONArray(org.json.simple.JSONArray) ChemicalObject(model.ChemicalObject) Coverage(algorithms.utils.Coverage) JSONObject(org.json.simple.JSONObject) ChemicalObject(model.ChemicalObject) Match(algorithms.utils.Match)

Example 8 with Match

use of algorithms.utils.Match in project Smiles2Monomers by yoann-dufresne.

the class DeepenMatcher method calculateCoveredAtoms.

/**
 * Create an ordered list of covered atoms indexes
 */
public static List<Integer> calculateCoveredAtoms(Coverage coverage) {
    List<Integer> covered = new ArrayList<>();
    for (Match match : coverage.getUsedMatches()) covered.addAll(match.getAtoms());
    Collections.sort(covered);
    return covered;
}
Also used : ArrayList(java.util.ArrayList) Match(algorithms.utils.Match)

Example 9 with Match

use of algorithms.utils.Match in project Smiles2Monomers by yoann-dufresne.

the class Modulation method recursiveModulation.

private boolean recursiveModulation(Coverage cov, HashSet<Match> banned, HashSet<Integer> unmovable, int depth) {
    if (depth == 0 || (System.currentTimeMillis() - this.startTime) / 1000 > maxTime)
        return false;
    List<Match> matchsOnUncoveredAtoms = this.getMatchOnUncoveredAtoms(cov, banned, unmovable);
    int idx = 0;
    for (Match match : matchsOnUncoveredAtoms) {
        if (idx++ >= 50)
            break;
        Set<Match> removed = null;
        try {
            removed = this.removeNReplaceMatches(match, cov, banned, unmovable);
        } catch (ImpossibleToReplace e) {
            continue;
        }
        if (cov.getCoverageRatio() == 1.0) {
            this.best = cov;
            return true;
        }
        if (this.recursiveModulation(cov, banned, unmovable, depth - 1))
            return true;
        else {
            if (best.getCoverageRatio() < cov.getCoverageRatio())
                this.best = cov.clone();
            for (int atomIdx : match.getAtoms()) unmovable.remove(atomIdx);
            banned.removeAll(removed);
            this.rollBack(cov, match, removed);
        }
    }
    return false;
}
Also used : Match(algorithms.utils.Match)

Example 10 with Match

use of algorithms.utils.Match in project Smiles2Monomers by yoann-dufresne.

the class Modulation method removeNReplaceMatches.

/**
 * Remove matching incompatible with match and place it
 * @param match
 * @param cov
 * @param banned
 * @param unmovable
 * @return
 */
private Set<Match> removeNReplaceMatches(Match match, Coverage cov, HashSet<Match> banned, HashSet<Integer> unmovable) throws ImpossibleToReplace {
    HashSet<Match> removed = new HashSet<>();
    // banned.add(match);
    unmovable.addAll(match.getAtoms());
    for (int idx : match.getAtoms()) {
        for (Match candidate : this.usedIndex.get(idx)) if (!banned.contains(candidate)) {
            removed.add(candidate);
            banned.add(candidate);
        }
    }
    for (Match toRemove : removed) {
        cov.removeUsedMatch(toRemove);
        for (int idx : toRemove.getAtoms()) this.usedIndex.get(idx).remove(toRemove);
    }
    cov.addUsedMatch(match);
    for (int idx : match.getAtoms()) this.usedIndex.get(idx).add(match);
    return removed;
}
Also used : Match(algorithms.utils.Match) HashSet(java.util.HashSet)

Aggregations

Match (algorithms.utils.Match)13 HashSet (java.util.HashSet)4 Residue (model.Residue)4 IAtom (org.openscience.cdk.interfaces.IAtom)4 IMolecule (org.openscience.cdk.interfaces.IMolecule)4 Coverage (algorithms.utils.Coverage)3 ArrayList (java.util.ArrayList)3 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2 IBond (org.openscience.cdk.interfaces.IBond)2 MappedChain (algorithms.isomorphism.chains.MappedChain)1 AtomColorer (io.imgs.coloration.ColoredAtomGenerator.AtomColorer)1 Color (java.awt.Color)1 HashMap (java.util.HashMap)1 Stack (java.util.Stack)1 ChemicalObject (model.ChemicalObject)1 Family (model.Family)1 Link (model.Family.Link)1 Rule (model.Rule)1 CDKException (org.openscience.cdk.exception.CDKException)1