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;
}
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;
}
Aggregations