use of org.openscience.cdk.interfaces.IBond 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