use of org.openscience.cdk.interfaces.IMolecule in project Smiles2Monomers by yoann-dufresne.
the class CoveragesJsonLoader method getJSONMatches.
@SuppressWarnings("unchecked")
private JSONObject getJSONMatches(Coverage cov) {
JSONObject graph = new JSONObject();
JSONArray atoms = new JSONArray();
graph.put("atoms", atoms);
JSONArray bonds = new JSONArray();
graph.put("bonds", bonds);
Set<IBond> usedBonds = new HashSet<IBond>();
int matchId = 0;
for (Match match : cov.getUsedMatches()) {
// Atoms
for (int a : match.getAtoms()) {
JSONObject atom = new JSONObject();
// CDK informations
atom.put("cdk_idx", a);
// Atom informations
IAtom ia = cov.getChemicalObject().getMolecule().getAtom(a);
atom.put("name", ia.getSymbol());
atom.put("hydrogens", match.getHydrogensFrom(a));
// Residue informations
atom.put("res", match.getResidue().getId());
atom.put("matchIdx", matchId);
atoms.add(atom);
}
// Bonds
for (int b : match.getBonds()) {
IBond ib = cov.getChemicalObject().getMolecule().getBond(b);
usedBonds.add(ib);
JSONObject bond = new JSONObject();
// CDK informations
bond.put("cdk_idx", b);
// atoms linked
JSONArray linkedAtoms = new JSONArray();
for (IAtom a : ib.atoms()) {
linkedAtoms.add(cov.getChemicalObject().getMolecule().getAtomNumber(a));
}
bond.put("arity", ib.getOrder().numeric());
bond.put("atoms", linkedAtoms);
bond.put("res", match.getResidue().getId());
bonds.add(bond);
}
matchId++;
}
IMolecule mol = cov.getChemicalObject().getMolecule();
for (IBond ib : mol.bonds()) {
if (!usedBonds.contains(ib)) {
JSONObject bond = new JSONObject();
// CDK informations
bond.put("cdk_idx", mol.getBondNumber(ib));
// atoms linked
JSONArray linkedAtoms = new JSONArray();
for (IAtom a : ib.atoms()) {
linkedAtoms.add(mol.getAtomNumber(a));
}
bond.put("arity", ib.getOrder().numeric());
bond.put("atoms", linkedAtoms);
bonds.add(bond);
}
}
return graph;
}
use of org.openscience.cdk.interfaces.IMolecule in project Smiles2Monomers by yoann-dufresne.
the class Residue method explicitToImplicitHydrogens.
public void explicitToImplicitHydrogens() {
IMolecule mol = this.getMolecule();
for (IAtom a : mol.atoms()) a.setImplicitHydrogenCount(0);
List<IAtom> toRemove = new ArrayList<>();
for (IAtom a : mol.atoms()) if (a.getAtomTypeName().equals("H")) {
IAtom connected = mol.getConnectedAtomsList(a).get(0);
connected.setImplicitHydrogenCount(connected.getImplicitHydrogenCount() + 1);
toRemove.add(a);
}
for (IAtom a : toRemove) {
mol.removeBond(mol.getConnectedBondsList(a).get(0));
mol.removeAtom(a);
}
}
use of org.openscience.cdk.interfaces.IMolecule 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;
}
use of org.openscience.cdk.interfaces.IMolecule 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;
}
use of org.openscience.cdk.interfaces.IMolecule in project Smiles2Monomers by yoann-dufresne.
the class ChainLearning method learnGreedy.
// --------------------------------- Greedy ------------------------------------
public MappedChain learnGreedy(MappedChain mb) {
IMolecule mol = mb.getChemObject().getMolecule();
List<Integer> neighbors = mb.getNeighborsBonds(mol);
if (neighbors.size() == 0)
return mb;
Extension ext = this.getBestGreedyExt(mol, neighbors);
MappedChain greedyMB = Isomorphism.searchFromPreviousMapping(mb, ext, MatchingType.EXACT).get(0);
return this.learnGreedy(greedyMB);
}
Aggregations