use of org.openscience.cdk.interfaces.IBond in project Smiles2Monomers by yoann-dufresne.
the class IsomorphismTests method setUp.
@Before
public void setUp() throws Exception {
// Database
Monomer[] monos = new Monomer[1];
Polymer pepTest = new Polymer(0, "malformin A1", "O=C1NC2C(=O)NC(C(=O)NC(C(=O)NC(C(=O)NC1CSSC2)C(C)CC)CC(C)C)C(C)C", monos);
// Extensions
IAtom a = new Atom("C");
IBond b1 = new Bond(new Atom("S"), a, Order.SINGLE);
this.ext1 = new Extension(b1);
a = new Atom("C");
IAtom a2 = new Atom("C");
IBond b2 = new Bond(a, a2, Order.SINGLE);
this.ext2 = new Extension(b2);
// Mapped blocs
this.mb0 = new MappedChain(pepTest, null, new ArrayList<Integer>(), new ArrayList<Integer>(), new ArrayList<MatchingType>(), new HashMap<Integer, Integer>());
// For blocs Tests
this.bloc = new Chain("S,0,c,0,0,-1,-1;c,0,c,0,0,-1,1");
}
use of org.openscience.cdk.interfaces.IBond 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.IBond 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.IBond 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.IBond in project Smiles2Monomers by yoann-dufresne.
the class BondAdd method searchAtoms.
public void searchAtoms(MappedChain mc, MatchingType type) throws AtomNotFound {
this.mol = mc.getChemObject().getMolecule();
// Init real atom idxs.
int atomIdx = -1;
int neiIdx = -1;
this.expected = null;
if (this.idx1 == -1) {
atomIdx = mc.getAtomsMapping().get(this.idx2);
neiIdx = this.idx1;
expected = this.ext.getBond().getAtom(0);
} else {
atomIdx = mc.getAtomsMapping().get(this.idx1);
neiIdx = this.idx2;
this.expected = this.ext.getBond().getAtom(1);
}
// Search in neighborhood.
IAtom atom = this.mol.getAtom(atomIdx);
List<IAtom> neighbors = this.mol.getConnectedAtomsList(atom);
this.neiAtom = null;
// Adding with two atoms already in the previous mapped chain
if (neiIdx != -1) {
this.neiAtom = this.mol.getAtom(mc.getAtomsMapping().get(neiIdx));
if (!neighbors.contains(this.neiAtom)) {
throw new AtomNotFound();
}
} else // Adding with only one atom in the previous matching
{
for (IAtom a : neighbors) {
if (mc.getAtomsMapping().contains(this.mol.getAtomNumber(a)))
continue;
IBond bond = this.mol.getBond(atom, a);
if (this.expected.getSymbol().equals(a.getSymbol())) {
if (bond.getOrder() == this.ext.getBond().getOrder() && this.expected.getFlag(CDKConstants.ISAROMATIC) == a.getFlag(CDKConstants.ISAROMATIC) && this.expected.getImplicitHydrogenCount().intValue() <= a.getImplicitHydrogenCount().intValue()) {
this.neiAtom = a;
this.neiHydro = this.expected.getImplicitHydrogenCount().intValue();
if (this.expected.getImplicitHydrogenCount().intValue() == a.getImplicitHydrogenCount().intValue()) {
this.matchingType = MatchingType.EXACT;
break;
} else if (type != MatchingType.EXACT) {
this.matchingType = MatchingType.STRONG;
break;
}
} else if (type == MatchingType.LIGHT) {
this.neiAtom = a;
this.neiHydro = 0;
this.matchingType = MatchingType.LIGHT;
break;
}
}
}
if (this.neiAtom == null) {
throw new AtomNotFound();
}
}
this.bond = this.mol.getBond(atom, neiAtom);
}
Aggregations