Search in sources :

Example 1 with Replacement

use of model.Rule.Replacement in project Smiles2Monomers by yoann-dufresne.

the class RulesJsonLoader method getArrayOfElements.

@SuppressWarnings("unchecked")
@Override
protected JSONArray getArrayOfElements(Rule obj) {
    JSONObject jso = new JSONObject();
    jso.put("name", obj.getName());
    jso.put("formula", obj.getFormula());
    jso.put("alone", obj.getAlone());
    JSONArray exclusions = new JSONArray();
    for (String exclu : obj.getExclusion()) exclusions.add(exclu);
    jso.put("exclusion", exclusions);
    JSONArray transformations = new JSONArray();
    for (Replacement rep : obj.getReplacements()) transformations.add(rep.formula);
    jso.put("transformations", transformations);
    JSONArray weights = new JSONArray();
    for (int w : obj.getWeights()) weights.add(new Integer(w));
    jso.put("weights", weights);
    JSONArray array = new JSONArray();
    array.add(jso);
    return array;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) Replacement(model.Rule.Replacement)

Example 2 with Replacement

use of model.Rule.Replacement 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)

Aggregations

Replacement (model.Rule.Replacement)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Residue (model.Residue)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 Molecule (org.openscience.cdk.Molecule)1 IAtom (org.openscience.cdk.interfaces.IAtom)1 IBond (org.openscience.cdk.interfaces.IBond)1 IMolecule (org.openscience.cdk.interfaces.IMolecule)1 RMap (org.openscience.cdk.isomorphism.mcss.RMap)1